summaryrefslogtreecommitdiffstats
path: root/innbbsd/daemon.c
blob: a60d7d732c8b78bad235943156f5b1331e1f1020 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "daemon.h"
/*
 * typedef struct daemoncmd { char *cmdname; char *usage; int argc; int
 * (*main) ARG((FILE*,FILE*,int,char**,char*)); } daemoncmd_t;
 * 
 */

void deargify   ARG((char ***));
static daemoncmd_t *dcmdp = NULL;
static char    *startupmessage = NULL;
static int      startupcode = 100;
static FILE    *DIN, *DOUT, *DIO;
typedef int     (*F) ();

void 
installdaemon(cmds, code, startupmsg)
    daemoncmd_t    *cmds;
    int             code;
    char           *startupmsg;
{
    dcmdp = cmds;
    startupcode = code;
    startupmessage = startupmsg;
}

daemoncmd_t    *
searchcmd(cmd)
    char           *cmd;
{
    daemoncmd_t    *p;
    for (p = dcmdp; p->name != NULL; p++) {
#ifdef DEBUGCMD
    printf("searching name %s for cmd %s\n", p->name, cmd);
#endif
    if (!strncasecmp(p->name, cmd, 1024))
        return p;
    }
    return NULL;
}

#if 0
int 
daemon(dfd)
    int             dfd;
{
    static char     BUF[1024];
    /* hash_init(); */
    if (dfd > 0) {
    DIO = fdopen(dfd, "rw");
    DIN = fdopen(dfd, "r");
    DOUT = fdopen(dfd, "w");
    if (DIO == NULL || DIN == NULL || DOUT == NULL) {
        perror("fdopen");
        return -1;
    }
    }
    if (startupmessage) {
    fprintf(DOUT, "%d %s\n", startupcode, startupmessage);
    fflush(DOUT);
    }
    while (fgets(BUF, 1024, DIN) != NULL) {
    int             i;
    int             (*Main) ();
    daemoncmd_t    *dp;
    argv_t          Argv;

    char           *p = (char *)strchr(BUF, '\r');
    if (p == NULL)
        p = (char *)strchr(BUF, '\n');
    if (p == NULL)
        continue;
    *p = '\0';
    if (p == BUF)
        continue;

    Argv.argc = 0, Argv.argv = NULL, Argv.inputline = BUF;
    Argv.in = DIN, Argv.out = DOUT;
    printf("command entered: %s\n", BUF);
#ifdef DEBUGSERVER
    fprintf(DOUT, "BUF in client %s\n", BUF);
    fprintf(stdout, "BUF in server %s\n", BUF);
    fflush(DOUT);
#endif
    Argv.argc = argify(BUF, &Argv.argv);
#ifdef DEBUGSERVER
    fprintf(stdout, "argc %d argv ", Argv.argc);
    for (i = 0; i < Argv.argc; ++i)
        fprintf(stdout, "%s ", Argv.argv[i]);
    fprintf(stdout, "\n");
#endif
    dp = searchcmd(Argv.argv[0]);
    Argv.dc = dp;
    if (dp) {
#ifdef DEBUGSERVER
        printf("find cmd %s by %s\n", dp->name, dp->usage);
#endif
        if (Argv.argc < dp->argc) {
        fprintf(DOUT, "%d Usage: %s\n", dp->errorcode, dp->usage);
        fflush(DOUT);
        goto cont;
        }
        if (dp->argno != 0 && Argv.argc > dp->argno) {
        fprintf(DOUT, "%d Usage: %s\n", dp->errorcode, dp->usage);
        fflush(DOUT);
        goto cont;
        }
        Main = dp->main;
        if (Main) {
        fflush(stdout);
        (*Main) (&Argv);
        }
    } else {
        fprintf(DOUT, "99 command %s not available\n", Argv.argv[0]);
        fflush(DOUT);
    }
cont:
    deargify(&Argv.argv);
    }
    /* hash_reclaim(); */
}
#endif

#define MAX_ARG 32
#define MAX_ARG_SIZE 16384

int 
argify(line, argvp)
    char           *line, ***argvp;
{
    static char    *argvbuffer[MAX_ARG + 2];
    char          **argv = argvbuffer;
    int             i;
    static char     argifybuffer[MAX_ARG_SIZE];
    char           *p;
    while (strchr("\t\n\r ", *line))
    line++;
    i = strlen(line);
    /* p=(char*) mymalloc(i+1); */
    p = argifybuffer;
    strncpy(p, line, sizeof argifybuffer);
    for (*argvp = argv, i = 0; *p && i < MAX_ARG;) {
    for (*argv++ = p; *p && !strchr("\t\r\n ", *p); p++);
    if (*p == '\0')
        break;
    for (*p++ = '\0'; strchr("\t\r\n ", *p) && *p; p++);
    }
    *argv = NULL;
    return argv - *argvp;
}

void 
deargify(argv)
    char         ***argv;
{
    return;
    /*
     * if (*argv != NULL) { if (*argv[0] != NULL){ free(*argv[0]); argv[0] =
     * NULL; } free(*argv); argv = NULL; }
     */
}

int 
daemonprintf(format)
    char           *format;
{
    fprintf(DOUT, format);
    fflush(DOUT);
}

int 
daemonputs(output)
    char           *output;
{
    fputs(output, DOUT);
    fflush(DOUT);
}