File: smain.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (450 lines) | stat: -rw-r--r-- 9,424 bytes parent folder | download
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <termios.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/wait.h>
#include <sys/time.h>

#include <config.h>

#ifdef	HAVE_READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif

#include <support.h>
#include <xcio.h>
#include <msgcat.h>

#include "str_smsg.h"

#define	FILE_KEYBIND	"/keybind"
#define	FILE_CATCAP	"/catcap"
#define	DEFAULT_PROMPT	"ppxp> "

static Keymap ttyKeymap;
static Keymap stdKeymap;

int ppxpFd, ttyMode=0;
static char ttyPrompt[sizeof(DEFAULT_PROMPT)];
static fd_set orgRfds;
static struct termios newTio, oldTio;
static bool_t tioBackuped;

void
TtyEcho(bool_t sw)
{
    static bool_t old;

    if (old == sw) return;
    old = sw;
    if (sw) newTio.c_lflag |= ECHO;
    else newTio.c_lflag &= ~ECHO;
    tcsetattr(0, TCSAFLUSH, &newTio);
}

static void
TtyUpdate(unsigned char *pppinfo)
{
/*    if (pppInfo.l_stat != pppinfo->l_stat) ttyMode = LSTAT_TTY;*/
    memcpy(&pppInfo, pppinfo, sizeof(pppInfo));
    if (pppinfo && !(pppInfo.l_stat & ttyMode)) {
	if (pppInfo.l_stat & LSTAT_CHAT)
	    memcpy(ttyPrompt, "chat", 4);
	else if (pppInfo.l_stat & LSTAT_DIAL)
	    memcpy(ttyPrompt, "dial", 4);
	else if (pppInfo.l_stat & LSTAT_TTY)
	    memcpy(ttyPrompt, "term", 4);
	else {
	    ttyPrompt[0] = (pppInfo.phase >= PS_ESTABLISH) ? 'P': 'p';
	    ttyPrompt[1] = (pppInfo.l_stat & LSTAT_PPP) ? 'P': 'p';
	    ttyPrompt[2] = (pppInfo.phase > PS_ESTABLISH) ?
		(pppInfo.phase > PS_AUTHENTICATE) ? 'X': '+': 'x';
	    ttyPrompt[3] = (pppInfo.n_stat) ? 'P': 'p';
	}
	rl_expand_prompt(ttyPrompt);
    } else rl_expand_prompt("");
    rl_refresh_line(0, 0);
}

static int
TtyOut(char *buf, int len)
{
    write(0, buf, len);
    return(0);
}

void
XcDo(struct xcio_s *xc)
{
    int n, argc;
    char *argv[10], *msg;

    switch(xc->type & XCIO_MASK) {
    case XCIO_UP_INFO:
	TtyUpdate(xc->buf);
	break;
    case XCIO_S_OUT:
	TtyOut(xc->buf, xc->len);
	break;
    case XCIO_MESSAGE:
	printf("\r");
	if ((msg = SysMsgGet(xc->buf[0])) != NULL) {
	    if (xc->len > 2)
		printf(msg, &xc->buf[1]);
	    else
		printf("%s", msg);
	    printf("\n");
	}
	break;
    }
}

void
SetTerminal(bool_t set)
{
    if (tioBackuped) tcsetattr(0, TCSAFLUSH, set ? &newTio: &oldTio);
}

static void
restore(int sig)
{
    SetTerminal(FALSE);
    putchar('\r');
    exit(0);
}

static int
RunCommand(int argc, char *argv[])
{
    struct xcio_s xc;
    xcmd_t xcmd;
    int id, ret, n;

    ret = RunBuiltinCommand(argc, argv);
    if (ret < 0 && ((xcmd = PPxPCommandType(argv[0])) < XCMD_MAX)) {
	if ((xcmd == XCMD_CONNECT || xcmd == XCMD_TERMINAL)) {
	    ttyMode = LSTAT_TTY;
	}
	id = PPxPCommand(ppxpFd, xcmd, argc - 1, &argv[1]);
	if (xcmd == XCMD_QUIT || xcmd == XCMD_BYE) restore(0);
	if (id >= 0) {
	    while ((n = PPxPRead(ppxpFd, id, &xc)) >= 0) if (n > 0) {
		if (xc.type == XCIO_RETURN) {
		    ret = xc.buf[0];
		    break;
		}
		XcDo(&xc);
	    }
	}
    }
    return(ret);
}

static int
RunCommandLine(char *line)
{
    int n, ret=-1;
    int argc=0;
    char *argv[128], *p, *buf, *home;

    buf = line;
    if ((p = strpbrk(line, "\n\r")) != NULL) *p = '\0';
    while (*buf) {
	while (*buf && strchr("\t ", *buf)) buf ++;
	if (*buf) {
	    if ((p = strpbrk(buf, "\t ")) != NULL) *p = '\0';
	    argv[argc ++] = strdup(buf);
	    if (!p) break;
	    buf = p + 1;
	}
    }
    if (argc <= 0) return(-1);
    argv[argc] = NULL;

    buf = line;
    *buf = '\0';
    home = getenv("HOME");
    for (n = 0; n < argc;) {
	strcat(buf, argv[n]);
	if (home && *argv[n] == '~' && *(argv[n] + 1) == '/') {
	    p = argv[n];
	    argv[n] = Malloc(strlen(p) + strlen(home));
	    strcpy(argv[n], home);
	    strcat(argv[n], p + 1);
	    free(p);
	}
	n ++;
	if (n < argc) strcat(buf, " ");
    }
    add_history(buf);
#if 1
    ret = RunCommand(argc, argv);
#else
    ret = RunBuiltinCommand(argc, argv);
    if (ret < 0) {
	struct xcio_s xc;
	xcmd_t xcmd;
	int id;

	if ((xcmd = PPxPCommandType(argv[0])) < XCMD_MAX) {
	    if ((xcmd == XCMD_CONNECT || xcmd == XCMD_TERMINAL)) {
		ttyMode = LSTAT_TTY;
	    }
	    id = PPxPCommand(ppxpFd, xcmd, argc - 1, &argv[1]);
	    if (!(xcmd == XCMD_QUIT || xcmd == XCMD_BYE) && id >= 0) {
		while ((n = PPxPRead(ppxpFd, id, &xc)) >= 0) if (n > 0) {
		    if (xc.type == XCIO_RETURN) {
			ret = xc.buf[0];
			break;
		    }
		    XcDo(&xc);
		}
	    }
	}
    }
#endif
    if (ret < 0) Exec(argc, argv, buf);
    return(ret);
}

static int
hook_poll()
{
    static char buf[XCBUFSIZ];
    static Keymap map;
    struct xcio_s xc;
    fd_set rfds;
    Function *func;
    int n, i;

    do {
	rfds = orgRfds;
	while ((n = PPxPRead(ppxpFd, XID_ANY, &xc)) > 0) XcDo(&xc);
	if (n < 0) restore(0);
	n = select(ppxpFd + 1, &rfds, NULL, NULL, NULL);
    } while (n <= 0 && errno == EINTR);
    if ((pppInfo.l_stat & ttyMode) && FD_ISSET(0, &rfds)) {
	if ((n = read(0, buf, sizeof(buf))) > 0) {
	    for (i = 0; i < n; i ++) {
		int key=buf[i];

		if (!map) map = ttyKeymap;
		func = map[key].function;
		switch(map[key].type) {
		case ISKMAP:
		    map = (Keymap)((long)map[key].function);
		    break;
		case ISFUNC:
		    if (map[key].function) {
			key = (*map[key].function)(0, 0);
		    } else key = 0;
		    map = NULL;
		    if (key) n = 0;
		}
	    }
	}
	if (n > 0) {
	    xc.len = n;
	    xc.type = XCIO_S_IN;
	    memcpy(xc.buf, buf, xc.len);
	    xc.xid = 1;
	    XcioWrite(ppxpFd, &xc);
	}
    }
    return(0);
}

static int
EscConsole()
{
    ttyMode = 0;
    PPxPUpdateRequest(ppxpFd);
    TtyEcho(TRUE);
}

static int
EscActivate()
{
    printf("activate\n");
    PPxPRequest(ppxpFd, XCIO_ACTIVATE);
}

static int
EscHelp()
{
/*
    Keymap km;

    km = rl_get_keymap_by_name("esc-console");
    km = rl_get_keymap_by_name("esc-activate");
    km = rl_get_keymap_by_name("esc-help");
*/
    printf("help\n");
}

static void
MakeDirectories()
{
    char *path, *sub[]={"conf", "chat", "log", "rc"};
    unsigned n;
    int fd;

    path = Malloc(strlen(usrPPxP) + 20);

    if (mkdir(usrPPxP, 0700) < 0 && errno != EEXIST) return;
    for (n = 0; n < sizeof(sub)/sizeof(sub[0]); n ++) {
	sprintf(path, "%s/%s", usrPPxP, sub[n]);
	mkdir(path, 0700);
    }
    sprintf(path, "%s/passwd", usrPPxP);
    fd = open(path, O_CREAT, 0600);
    /* ... */
    close(fd);
    free(path);
}

static int
LoadRcFiles()
{
    FILE *fp;
    char *kbf, *tlf, *lang, *nlang, *term;

    DirNameInit(getuid());

    tlf = Malloc(strlen(sysPPxP) + sizeof(FILE_CATCAP) + 1);
    strcpy(tlf, sysPPxP);
    strcat(tlf, FILE_CATCAP);

    term = getenv("TERM");
    lang = getenv("LANG");
    nlang = NULL;
    if (lang && term && (fp = fopen(tlf, "r")) != NULL) {
	char *p, buf[256];
	int len;

	while (fgets(buf, sizeof(buf), fp)) {
	    if ((p = strchr(buf, ':')) != NULL) {
		*p = '\0';
		len = p - buf;
		p ++;
		if (!strncmp(buf, lang, len)) {
		    if (strstr(p, term)) {
			nlang = lang;
			break;
		    }
		}
	    }
	}
	fclose(fp);
    }
    Free(tlf);
    if (!nlang) setenv("LANG", "C", 1);	/* change */

    MsgInit("ppxp", strMsg);

    /* if (!nlang) setenv("LANG", lang, 1); /* restore */

#ifdef	HAVE_READLINE
    stdKeymap = rl_get_keymap();
    ttyKeymap = rl_make_bare_keymap();
    rl_set_keymap(ttyKeymap);

    rl_add_funmap_entry("esc-console", EscConsole);
    rl_add_funmap_entry("esc-activate", EscActivate);
    rl_add_funmap_entry("esc-help", EscHelp);

    kbf = Malloc(strlen(sysPPxP) + sizeof(FILE_KEYBIND) + 1);
    strcpy(kbf, sysPPxP);
    strcat(kbf, FILE_KEYBIND);
    rl_read_init_file(kbf);
    Free(kbf);

    if (usrPPxP) {
	MakeDirectories();
	kbf = Malloc(strlen(usrPPxP) + sizeof(FILE_KEYBIND) + 1);
	strcpy(kbf, usrPPxP);
	strcat(kbf, FILE_KEYBIND);
	rl_read_init_file(kbf);
	Free(kbf);
    }

    rl_set_keymap(stdKeymap);
#else
    if (usrPPxP) MakeDirectories();
#endif
    return(0);
}

int
main(int argc, char *argv[])
{
    char *line, *path, *p;
    struct xcio_s xc;
    u_int8_t xid;

    if ((p = strrchr(argv[0], '/')) != NULL) p ++;
    else p = argv[0];
    if (!strcmp(p, "plast")) return(PlastMain(argc, argv));

    LoadRcFiles();

    if ((ppxpFd = PPxPSetup(&argc, argv)) < 0) exit(-1);

    if (argc > 1 && !strcmp(argv[1], "-C")) {
	if (argc > 2) RunCommand(argc - 2, &argv[2]);
	restore(0);
    }

    xid = PPxPEnvRequestv(ppxpFd, "version", NULL);
    printf("PPxP version %s\n", PPxPEnvGet(ppxpFd, xid));
    xid = PPxPEnvRequestv(ppxpFd, "IF.DEV", NULL);
    printf("interface: %s\n", PPxPEnvGet(ppxpFd, xid));

    PPxPUpdateRequest(ppxpFd);
    strcpy(ttyPrompt, DEFAULT_PROMPT);

    FD_ZERO(&orgRfds);
    FD_SET(0, &orgRfds);
    FD_SET(ppxpFd, &orgRfds);
    rl_event_hook = hook_poll;

    if (tcgetattr(0, &oldTio)) return(-1);
    tioBackuped = TRUE;
    newTio = oldTio;

    newTio.c_lflag &= ~(ISIG|ICANON);
    newTio.c_iflag &= ~(INLCR|ICRNL|ISTRIP|IXON|BRKINT);
    newTio.c_cc[VMIN] = 1;
    newTio.c_cc[VTIME] = 0;
    newTio.c_cc[VQUIT] = 0;
    TtyEcho(TRUE);
    InitSignals();
    signal(SIGPIPE, restore);

/*
    xc.xid = 100;
    xc.type = XCIO_LIST;
    xc.len = 0;
    XcioWrite(ppxpFd, &xc);
*/

    while (1) {
	if (!(pppInfo.l_stat & ttyMode)) {
	    WaitJob();

	    if ((line = readline(ttyPrompt)) == NULL) {
		line = Strdup("BYE BYE !!");
		printf("%s\n", line);
	    }
	    RunCommandLine(line);
	    Free(line);
	} else {
	    TtyEcho(FALSE);
	    hook_poll();
	}
    }
}