File: gettydefs.c

package info (click to toggle)
mgetty 1.1.33-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,092 kB
  • ctags: 3,594
  • sloc: ansic: 38,868; sh: 5,973; perl: 5,637; makefile: 1,473; tcl: 756; lisp: 283
file content (633 lines) | stat: -rw-r--r-- 13,929 bytes parent folder | download | duplicates (13)
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
#ident "$Id: gettydefs.c,v 4.1 1997/01/12 14:53:39 gert Exp $ Copyright (c) 1993 Gert Doering/Chris Lewis"

/* gettydefs.c
 *
 * Read /etc/gettydefs file, and permit retrieval of individual entries.
 *
 * Code in this module by Chris Lewis
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include "syslibs.h"

#include "mgetty.h"
#include "policy.h"

boolean verbose;

char * mydup _P1 ((s), register char *s)
{
    register char *p = (char *) malloc(strlen(s) + 1);
    if (!p) {
	lprintf(L_ERROR, "mydup can't malloc");
	exit(1);
    }
    strcpy(p, s);
    return(p);
}
#ifdef USE_GETTYDEFS

static char gettydefs_ID[] = "@(#)gettydefs.c compiled with USE_GETTYDEFS";

#include "tio.h"

struct modeword {
    char *name;
    tioflag_t turnon;
    tioflag_t turnoff;
    unsigned short metaon;
    unsigned short metaoff;
};

/*	Meta tokens */
#define SANE	0x0001
#define	ODDP	0x0002

#define	PARITY	0x0004
#define	NPARITY	0x0008

#define RAW	0x0010
#define	COOKED	0x0020

#define NL	0x0040
#define NNL	0x0080

#define LCASE	0x0100
#define NLCASE	0x0200

#define TABS	0x0400
#define	NTABS	0x0800

/* input modes */

static struct modeword iflags[] = {
    { "IGNBRK", IGNBRK, IGNBRK },
    { "BRKINT", BRKINT, BRKINT, SANE },
    { "IGNPAR", IGNPAR, IGNPAR, SANE },
    { "PARMRK", PARMRK, PARMRK },
    { "INPCK", INPCK, INPCK },
    { "ISTRIP", ISTRIP, ISTRIP, SANE },
    { "INLCR", INLCR, INLCR, 0, NNL },
    { "IGNCR", IGNCR, IGNCR, 0, NNL },
    { "ICRNL", ICRNL, ICRNL, (SANE|NL), NNL },
    { "IUCLC", IUCLC, IUCLC, LCASE, NLCASE },
    { "IXON", IXON, IXON, SANE },
    { "IXANY", IXANY, IXANY },
    { "IXOFF", IXOFF, IXOFF },
    { NULL }
};

/* output modes */

static struct modeword oflags[] = {
    { "OPOST", OPOST, OPOST, (SANE|COOKED), RAW },
    { "OLCUC", OLCUC, OLCUC, LCASE, NLCASE },
    { "ONLCR", ONLCR, ONLCR, NL, NNL },
    { "OCRNL", OCRNL, OCRNL, 0, NNL },
    { "ONOCR", ONOCR, ONOCR },
    { "ONLRET", ONLRET, ONLRET, NNL },
    { "OFILL", OFILL, OFILL },
    { "OFDEL", OFDEL, OFDEL },
    { "NLDLY", NLDLY, NLDLY },
    { "NL0", NL0, NLDLY },
    { "NL1", NL1, NLDLY },
    { "CR0", CR0, CRDLY },
    { "CR1", CR1, CRDLY },
    { "CR2", CR2, CRDLY },
    { "CR3", CR3, CRDLY },
    { "TAB0", TAB0, TABDLY, TABS },
    { "TAB1", TAB1, TABDLY },
    { "TAB2", TAB2, TABDLY },
    { "TAB3", TAB3, TABDLY, NTABS },
    { "BS0", BS0, BSDLY },
    { "BS1", BS1, BSDLY },
    { "VT0", VT0, VTDLY },
    { "VT1", VT1, VTDLY },
    { "FF0", FF0, FFDLY },
    { "FF1", FF1, FFDLY },
    { NULL }
};

/* control modes */

static struct modeword cflags[] = {
    { "B0", B0, CBAUD },
    { "B50", B50, CBAUD },
    { "B75", B75, CBAUD },
    { "B110", B110, CBAUD },
    { "B134", B134, CBAUD },
    { "B150", B150, CBAUD },
    { "B200", B200, CBAUD },
    { "B300", B300, CBAUD },
    { "B600", B600, CBAUD },
#ifdef B900
    { "B900", B900, CBAUD },
#endif
    { "B1200", B1200, CBAUD },
    { "B1800", B1800, CBAUD },
    { "B2400", B2400, CBAUD },
#ifdef B3600
    { "B3600", B3600, CBAUD },
#endif
    { "B4800", B4800, CBAUD },
#ifdef B7200
    { "B7200", B7200, CBAUD },
#endif
    { "B9600", B9600, CBAUD },
#ifdef B19200
    { "B19200", B19200, CBAUD },
#endif
#ifdef B38400
    { "B38400", B38400, CBAUD },
#endif
#ifdef B57600
    { "B57600", B57600, CBAUD },
#endif
#ifdef B76800
    { "B76800", B76800, CBAUD },
#endif
#ifdef B115200
    { "B115200", B115200, CBAUD },
#endif
#ifdef B230400
    { "B230400", B230400, CBAUD },
#endif
#ifdef B230400
    { "B230400", B230400, CBAUD },
#endif
#ifdef B460800
    { "B460800", B460800, CBAUD },
#endif
    { "EXTA", EXTA, CBAUD },
    { "EXTB", EXTB, CBAUD },
    { "CS5", CS5, CSIZE },
    { "CS6", CS6, CSIZE },
    { "CS7", CS7, CSIZE, (ODDP|PARITY) },
    { "CS8", CS8, CSIZE, (SANE|NPARITY) },
    { "CSTOPB", CSTOPB, CSTOPB },
    { "CREAD", CREAD, CREAD, SANE },
    { "PARENB", PARENB, PARENB, (ODDP|PARITY), (NPARITY) },
    { "PARODD", PARODD, PARODD, ODDP },
    { "HUPCL", HUPCL, HUPCL },
    { "CLOCAL", CLOCAL, CLOCAL },
/* Various handshaking defines */
#ifdef CTSCD
    { "CTSCD", CTSCD, CTSCD },
#endif
#ifdef CRTSCTS
    { "CRTSCTS", CRTSCTS, CRTSCTS },
#endif
#ifdef CRTSFL
    { "CRTSFL", CRTSFL, CRTSFL },
#endif
#ifdef RTSFLOW
    { "RTSFLOW", RTSFLOW, RTSFLOW },
    { "CTSFLOW", CTSFLOW, CTSFLOW },
#endif
#ifdef HDX
    { "HDX", HDX, HDX },
#endif
    { NULL }
};

/* line discipline */
static struct modeword lflags[] =  {
    { "ISIG", ISIG, ISIG, SANE },
    { "ICANON", ICANON, ICANON, (SANE|COOKED), RAW },
    { "XCASE", XCASE, XCASE, LCASE, NLCASE },
    { "ECHO", ECHO, ECHO, SANE },
    { "ECHOE", ECHOE, ECHOE },
    { "ECHOK", ECHOK, ECHOK, SANE },
    { "ECHONL", ECHONL, ECHONL },
    { "NOFLSH", NOFLSH, NOFLSH },
    { NULL }
};

/* c_cc special characters */
static struct modeword ccchars[] = {
    {"VINTR", VINTR, CINTR},
    {"VQUIT", VQUIT, CQUIT},
    {"VERASE", VERASE, CERASE},
    {"VKILL", VKILL, CKILL},
    {"VEOF", VEOF, CEOF},
#if defined(VEOL) && VEOL < TIONCC
    {"VEOL", VEOL, CEOL},
#endif
#if defined(CEOL2) && defined(VEOL2) && VEOL2 < TIONCC
    {"VEOL2", VEOL2, CEOL2},
#endif
#if defined(VSUSP) && VSUSP < TIONCC
    {"VSUSP", VSUSP, CSUSP},
#endif
#if defined(VSTART) && VSTART < TIONCC
    {"VSTART", VSTART, CSTART},
#endif
#if defined(VSTOP) && VSTOP < TIONCC
    {"VSTOP", VSTOP, CSTOP},
#endif
#if defined(VSWTCH) && VSWTCH < TIONCC
    {"VSWTCH", VSWTCH, CSWTCH},
#endif
/* SVR4.2 */
#if defined(VDSUSP) && VDSUSP < TIONCC
   {"VDSUSP", VDSUSP, CDSUSP},
#endif
#if defined(VREPRINT) && VREPRINT < TIONCC
   {"VREPRINT", VREPRINT, CRPRNT},
#endif
#if defined(VDISCARD) && VDISCARD < TIONCC
   {"VDISCARD", VDISCARD, CFLUSH},
#endif
#if defined(VWERASE) && VWERASE < TIONCC
   {"VWERASE", VWERASE, CWERASE},
#endif
#if defined(VLNEXT) && VLNEXT < TIONCC
   {"VLNEXT", VLNEXT, CLNEXT},
#endif
    {"VMIN", VMIN, 0},
    {"VTIME", VTIME, 0},
    { NULL }
};

struct modeword metatokens[] = {
    { "SANE", SANE },
    { "ODDP", ODDP },

    { "PARITY", PARITY },
    { "EVENP", PARITY },
    { "-ODDP", NPARITY },
    { "-PARITY", NPARITY },
    { "-EVENP", NPARITY },

    { "RAW", RAW },
    { "-RAW", COOKED },
    { "COOKED", COOKED },

    { "NL", NL },
    { "-NL", NNL },

    { "LCASE", LCASE },
    { "-LCASE", NLCASE },

    { "TABS", TABS },
    { "-TABS", NTABS },
    { "TAB3", NTABS },

    { NULL }
};

#define GDCHUNK	5

GDE *gdep = (GDE *) NULL;
GDE *cur = (GDE *) NULL;
static int cntalloc = 0;

static struct modeword *
findmode _P2 ((modes, tok), struct modeword *modes, register char *tok)
{
    for( ; modes->name; modes++)
	if (strcmp(modes->name, tok) == 0)
	    return(modes);
    return((struct modeword *) NULL);
}

static void
metaset _P3((tc, modes, key), tioflag_t *tc, struct modeword *modes, int key)
{
    for ( ; modes->name; modes++) {
	if (modes->metaon&key)
	    *tc = (*tc & ~ modes->turnoff) | modes->turnon;
	if (modes->metaoff&key)
	    *tc = (*tc & ~ modes->turnoff);
    }
}

static void
parsetermio _P2((ti, str), TIO *ti, char *str)
{
    register char *p;
    struct modeword *m;
    tioflag_t *flag;
    int metakey;

    /* initialize c_cc[] array (tio_* doesn't init INTR/ERASE!) */
    tio_default_cc( ti );
    ti->c_cc[VINTR] = CINTR;
    ti->c_cc[VERASE] = CERASE;

#ifndef POSIX_TERMIOS
    ti->c_line = 0;
#endif

    for (p = str; *p; p++)
	if (islower(*p))
	    *p = toupper(*p);

    while ( (p = strtok(str, " \t")) != NULL ) {
	int not = FALSE;

	str = NULL;

	metakey = 0;

	if (strcmp(p, "EK") == 0) {
	    ti->c_cc[VERASE] = '#';
	    ti->c_cc[VKILL] = CKILL;
	    continue;
	}

	for (m = metatokens; m->name; m++)
	    if (strcmp(p, m->name) == 0) {
		metakey = m->turnon;
		break;
	    }
	
	if (metakey) {
	    metaset(&ti->c_lflag, lflags, metakey);
	    metaset(&ti->c_oflag, oflags, metakey);
	    metaset(&ti->c_iflag, iflags, metakey);
	    metaset(&ti->c_cflag, cflags, metakey);
	    continue;
	}

	if (*p == '-') {
	    not = TRUE;
	    p++;
	}

	if      ((m = findmode(lflags, p)) != NULL)
	    flag = &ti->c_lflag;
	else if ((m = findmode(oflags, p)) != NULL)
	    flag = &ti->c_oflag;
	else if ((m = findmode(iflags, p)) != NULL)
	    flag = &ti->c_iflag;
	else if ((m = findmode(cflags, p)) != NULL)
	    flag = &ti->c_cflag;
	if (m) {
	    if (not)
		*flag = (*flag & ~ m->turnoff);
	    else
		*flag = (*flag & ~ m->turnoff) | m->turnon;
	} else {
	    if ((m = findmode(ccchars, p)) != NULL) {
		char *p2;
		p2 = strtok(str, " \t");
		if (!p2) {
		    if (verbose)
			fprintf(stderr, "No value after %s\n", p);
		    return;
		}
		if (*p2 == '\\')
		    switch(*(p2+1)) {
			case 'n': *p2 = '\n'; break;
			case 'r': *p2 = '\r'; break;
			case 'b': *p2 = '\010'; break;
			case 'v': *p2 = '\013'; break;
			case 'g': *p2 = '\007'; break;
			case 'f': *p2 = '\f'; break;
			case '0': case '1': case '2': case '3':
			case '4': case '5': case '6': case '7': {
			    char tbuf[4];
			    strncpy(tbuf, p2+1, 3);
			    tbuf[3] = '\0';
			    *p2 = strtol(tbuf, (char **) NULL, 8);
			    break;
			}
			default:
			    *p2 = *(p2+1);
		    }
		else if (*p2 == '^')		/* ^x means C-x, ^? is DEL */
		    *p2 = (*(p2+1) == '?')? 0x7f : *(p2+1) - '@';

		ti->c_cc[m->turnon] = *p2;
	    } else
		if (verbose)
		    fprintf(stderr, "Can't parse %s\n", p);
	}
    }

}

static char *
stripblanks _P1 ((s), register char *s)
{
    register char *p;
    while(*s && isspace(*s)) s++;
    p = s;
    while(*p && !isspace(*p)) p++;
    *p = '\0';
    return(s);
}

#define	GETTYBUFSIZE	(10*BUFSIZ)

int
getentry _P3((entry, elen, f), char *entry, int elen, FILE *f) {
    char buf[BUFSIZ*2];
    register char *p;

    entry[0] = '\0';

    do {
	if (!fgets(buf, sizeof(buf), f))
	    return(0);
	for (p = buf; isspace(*p); p++);
    } while(*p == '#' || *p == '\n');

    p = strchr(buf, '\n');
    if (p)
	*p = '\0';
    strcat(entry, buf);

    while (1) {
	if (!fgets(buf, sizeof(buf), f))
	    break;
	p = strchr(buf, '\n');
	if (p)
	    *p = '\0';
	for (p = buf; isspace(*p); p++);
	if (!*p)
	    break;
	strcat(entry, " ");
	strcat(entry, p);
    }
    return(1);
}

/*
 * loads all of the entries from the gettydefs file
 * returns 0 if it fails.
 */
int
loadgettydefs _P1((file), char *file ) {
    FILE *gd = fopen(file, "r");
    char buf[GETTYBUFSIZE];
    register char *p;
    char *tag, *prompt, *nexttag, *before, *after;

    if (!gd) {
	lprintf(L_WARN, "Can't open %s\n", file);
	return(0);
    }

    while(getentry(buf, sizeof(buf), gd)) {
	
	p = buf;

	tag = strtok(p, "#");
	if (!tag)
	    continue;
	tag = stripblanks(tag);
	tag = mydup(tag);

	before = strtok(NULL, "#");
	if (!before)
	    continue;

	after = strtok(NULL, "#");
	if (!after)
	    continue;

	prompt = strtok(NULL, "#");
	if (!prompt)
	    continue;

	/* do NOT escape prompt here - it may contain \D and \T, and
	 * for that, the real time at login should be used
	 */
	prompt = mydup(prompt);

	nexttag = strtok(NULL, "#");
	if (!nexttag)
	    continue;

	p = strchr(nexttag, '\n');
	if (p)
	    *p = '\0';

	nexttag = stripblanks(nexttag);
	nexttag = mydup(nexttag);

#ifdef NEVER
	printf("tag: %s\nbefore: %s\nafter: %s\nprompt: %s\nnexttag: %s\n\n",
	    tag, before, after, prompt, nexttag);
#endif

	if (cur - gdep >= cntalloc-2) {
	    GDE *sav;
	    sav = gdep;
	    if (!gdep) {
		gdep = (GDE *) malloc(sizeof(GDE) * GDCHUNK);
		cur = gdep;
	    } else {
		gdep = (GDE *) realloc(gdep, sizeof(GDE) * (GDCHUNK + cntalloc));
		cur = gdep + (cur - sav);
	    }
	    cntalloc += GDCHUNK;
	}

	memset(cur, sizeof(*cur), '\0');
	
	cur->tag = tag;
	cur->prompt = prompt;
	cur->nexttag = nexttag;
	parsetermio(&cur->before, before);
	parsetermio(&cur->after, after);
	if (verbose)
	    printf("Processed `%s' gettydefs entry\n", tag);
	cur++;
	cur->tag = (char *) NULL;
    }
    fclose(gd);
    return(1);
}

GDE *
getgettydef _P1 ((s), register char *s)
{
    for (cur = gdep; cur && cur->tag; cur++)
	if (strcmp(cur->tag, s) == 0)
	    return(cur);
    if (gdep && gdep->tag) {
	lprintf(L_WARN, "getgettydef(%s) entry not found using %s",
	    s, gdep->tag);
	return(gdep);
    }
    lprintf(L_WARN, "getgettydef(%s) no entry found", s);
    return((GDE *) NULL);
}

void
dumpflag _P3((type, modes, flag),
	     char *type,
	     struct modeword *modes, tioflag_t flag)
{
    printf("%s: %08lo", type, (unsigned long) flag);
    for(; modes->name; modes++)
	if ((flag&modes->turnoff) == modes->turnon)
	    printf(" %s", modes->name);
    putchar('\n');
}

void
dump _P2((ti, s), TIO *ti, char *s)
{
    register int i;
    register struct modeword *modes;

    printf("%s:", s);
    dumpflag("\tiflags", iflags, ti->c_iflag);
    dumpflag("\toflags", oflags, ti->c_oflag);
    dumpflag("\tcflags", cflags, ti->c_cflag);
    dumpflag("\tlflags", lflags, ti->c_lflag);
    printf("\tc_cc:\t");
    for (i = 0; i < TIONCC; i++) {
	if (i == 6)
	    printf("\n\t\t");
	for (modes = ccchars; modes->name; modes++)
	    if (modes->turnon == i) {
		printf("%s(", modes->name);
		break;
	    }
	if (!modes->name)	/* skip unallocated ones */
	    continue;
	/* Yeah, I know.  But who's ever heard of getty on a EBCDIC system ;-) */
	if (ti->c_cc[i] < ' ')
	    printf("^%c", ti->c_cc[i] + '@');
	else if (ti->c_cc[i] == (0xff & _POSIX_VDISABLE))
	    printf("disabled");
	else if (ti->c_cc[i] >= 0x7f)
	    printf("\\%03o", 0xff&ti->c_cc[i]);
	else
	    putchar(ti->c_cc[i]);
	printf(") ");
    }
    printf("\n\n");
}

static void 
spew _P1 ((gd), GDE *gd)
{
    printf("tag: `%s'\nprompt: `%s'\nnexttag: `%s'\n",
	gd->tag, gd->prompt, gd->nexttag);
    dump(&gd->before, "before");
    dump(&gd->after, "after");
    printf("\n");
}

void
dumpgettydefs _P1((file), char *file) {
    if (! loadgettydefs(file)) {
	fprintf(stderr, "Couldn't read %s\n", file);
	exit(1);
    }
    printf("loaded entries:\n");
    for (cur = gdep; cur->tag; cur++)
	spew(cur);

}
#endif