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
|
/*
** $Id: table.c,v 2.0 90/09/19 20:18:46 paul Rel $
**
** Routines to process the gettytab file.
*/
/*
** Copyright 1989,1990 by Paul Sutcliffe Jr.
**
** Permission is hereby granted to copy, reproduce, redistribute,
** or otherwise use this software as long as: there is no monetary
** profit gained specifically from the use or reproduction or this
** software, it is not sold, rented, traded or otherwise marketed,
** and this copyright notice is included prominently in any copy
** made.
**
** The author make no claims as to the fitness or correctness of
** this software for any use whatsoever, and it is provided as is.
** Any use of this software is at the user's own risk.
*/
#include "getty.h"
#include "table.h"
#include <errno.h>
/* Sane conditions.
*/
#define ISANE ( BRKINT | IGNPAR | ISTRIP | ICRNL | IXON | IXANY )
#define OSANE ( OPOST | ONLCR )
#define CSANE ( DEF_CFL | CREAD | HUPCL )
#define LSANE ( ISIG | ICANON | ECHO | ECHOE | ECHOK )
#define CC_SANE { CINTR, CQUIT, CERASE, CKILL, CEOF, CNUL, CNUL, CNUL }
/* States for gtabvalue()
*/
#define ENTRY 0 /* looking for an entry line */
#define QUIT 1 /* error occurred */
#define NULLPTR (char *) NULL
/* All possible mode flags.
*/
SYMTAB imodes[] = {
{ "IGNBRK", IGNBRK },
{ "BRKINT", BRKINT },
{ "IGNPAR", IGNPAR },
{ "PARMRK", PARMRK },
{ "INPCK", INPCK },
{ "ISTRIP", ISTRIP },
{ "INLCR", INLCR },
{ "IGNCR", IGNCR },
{ "ICRNL", ICRNL },
{ "IUCLC", IUCLC },
{ "IXON", IXON },
{ "IXANY", IXANY },
{ "IXOFF", IXOFF },
{ "IMAXBEL", IMAXBEL },
{ NULLPTR, 0 }
};
SYMTAB omodes[] = {
{ "OPOST", OPOST },
{ "OLCUC", OLCUC },
{ "ONLCR", ONLCR },
{ "OCRNL", OCRNL },
{ "ONOCR", ONOCR },
{ "ONLRET", ONLRET },
{ "OFILL", OFILL },
{ "OFDEL", OFDEL },
{ "NLDLY", NLDLY },
{ "NL0", NL0 },
{ "NL1", NL1 },
{ "CRDLY", CRDLY },
{ "CR0", CR0 },
{ "CR1", CR1 },
{ "CR2", CR2 },
{ "CR3", CR3 },
{ "TABDLY", TABDLY },
{ "TAB0", TAB0 },
{ "TAB1", TAB1 },
{ "TAB2", TAB2 },
{ "TAB3", TAB3 },
{ "BSDLY", BSDLY },
{ "BS0", BS0 },
{ "BS1", BS1 },
{ "VTDLY", VTDLY },
{ "VT0", VT0 },
{ "VT1", VT1 },
{ "FFDLY", FFDLY },
{ "FF0", FF0 },
{ "FF1", FF1 },
{ NULLPTR, 0 }
};
SYMTAB cmodes[] = {
{ "B2400", B2400 },
{ "B4800", B4800 },
{ "B9600", B9600 },
#ifdef B19200
{ "B19200", B19200 },
#endif /* B19200 */
#ifdef B38400
{ "B38400", B38400 },
#endif /* B38400 */
#ifdef B57600
{ "B57600", B57600 },
#endif /* B57600 */
#ifdef B115200
{ "B115200", B115200 },
#endif /* B115200 */
#ifdef B230400
{ "B230400", B230400 },
#endif /* B230400 */
{ "EXTA", EXTA },
{ "EXTB", EXTB },
{ "CS5", CS5 },
{ "CS6", CS6 },
{ "CS7", CS7 },
{ "CS8", CS8 },
{ "CSTOPB", CSTOPB },
{ "CREAD", CREAD },
{ "PARENB", PARENB },
{ "PARODD", PARODD },
{ "HUPCL", HUPCL },
{ "CLOCAL", CLOCAL },
#ifdef LOBLK
{ "LOBLK", LOBLK },
#endif /* LOBLK */
{ "CRTSCTS", CRTSCTS },
{ NULLPTR, 0 },
};
SYMTAB lmodes[] = {
{ "ISIG", ISIG },
{ "ICANON", ICANON },
{ "XCASE", XCASE },
{ "ECHO", ECHO },
{ "ECHOE", ECHOE },
{ "ECHOK", ECHOK },
{ "ECHONL", ECHONL },
{ "NOFLSH", NOFLSH },
{ "TOSTOP", TOSTOP },
{ "ECHOCTL", ECHOCTL },
{ "ECHOPRT", ECHOPRT },
{ "ECHOKE", ECHOKE },
{ "FLUSHO", FLUSHO },
{ "PENDIN", PENDIN },
{ "IEXTEN", IEXTEN },
#ifdef XCLUDE
{ "XCLUDE", XCLUDE },
#endif /* XCLUDE */
{ NULLPTR, 0 }
};
SYMTAB ldiscs[] = {
{ "LDISC0", LDISC0 },
{ NULLPTR, 0 }
};
/*
* Gettytab entry to use if no other can be determined
*/
GTAB Default = {
"default",
{ 0, 0, ( SSPEED | CSANE ), 0, 0, CC_SANE, },
{ ISANE, OSANE, ( SSPEED | CSANE ), LSANE, LDISC0, CC_SANE, },
"login: ",
"default"
};
#define VALUE(cptr) ((cptr == (char *) NULL) ? "NULL" : cptr)
int errors = 0;
int nextentry(), parseGtab(), findsym();
char *nextword();
void addfield(), chkerr();
/*
** gtabvalue() - find a gettytab entry that matches "id."
**
** Returns (GTAB *)NULL if not found or an error occurs.
*/
GTAB *
gtabvalue(id, mode)
register char *id;
int mode;
{
register int state;
register char *p;
register char *gettytab; /* gettytab file to use */
char buf[MAXLINE+1]; /* buffer for Gtab entries */
char buf_id[MAXID+1]; /* buffer to compare initial label */
char *this = "First"; /* First or Next entry */
static GTAB gtab; /* structure to be returned */
FILE *fp;
debug(D_GTAB, "gtabvalue(%s) called", VALUE(id));
gettytab = (Check) ? CheckFile : GETTYTAB;
debug(D_GTAB, "gettytab=%s", gettytab);
/* open the gettytab file
*/
if ((fp = fopen(gettytab, "r")) == (FILE *) NULL) {
logerr("open failed on \"%s\": %s", gettytab, strerror(errno));
return(&Default);
}
/* search through the file for "id", unless
* id is NULL, in which case we drop down
* to get the 'default' entry.
*/
state = (!Check && (id == (char *) NULL)) ? QUIT : ENTRY;
while (state != QUIT && nextentry(buf, sizeof(buf), fp) == SUCCESS) {
if (buf[0] == '#' || buf[0] == '\n')
continue; /* keep looking */
if (Check) {
(void) printf("*** %s Entry ***\n", this);
(void) printf("%s\n", buf);
this = "Next";
}
if (buf[strlen(buf)-1] != '\n') {
/* last char not \n, line is too long */
chkerr("line too long", FAIL);
state = QUIT;
continue;
}
/* get the first (label) field
*/
(void) strncpy(buf_id, buf, MAXID);
buf_id[sizeof(buf_id)-1] = '\0';
if ((p = strpbrk(buf_id, "# \t")) != (char *) NULL)
*p = '\0';
/* if Check is set, parse all entries;
* otherwise, parse only a matching entry
*/
if (Check || strequal(id, buf_id)) {
if (parseGtab(>ab, buf) == FAIL) {
chkerr("*** Invalid Entry ***", FAIL);
state = QUIT;
continue;
}
if (!Check) {
(void) fclose(fp);
goto success;
}
}
}
if (Check) {
if (errors)
(void) printf("*** %d errors found ***\n", errors);
(void) printf("*** Check Complete ***\n");
(void) fclose(fp);
return((GTAB *) NULL);
}
if (mode == G_FIND)
return((GTAB *) NULL);
if (id != (char *) NULL) {
logerr("%s: entry for \"%s\" not found", gettytab, id);
}
/* matching entry not found or defective;
* use the first line of the file
*/
rewind(fp);
(void) nextentry(buf, sizeof(buf), fp);
(void) fclose(fp);
if (parseGtab(>ab, buf) == FAIL)
return(&Default); /* punt: first line defective */
success:
debug(D_GTAB, "gtabvalue() successful");
return(>ab);
}
/*
** nextentry() - retrieve next entry from gettytab file
**
** Returns FAIL if an error occurs.
*/
int
nextentry(buf, len, stream)
register char *buf;
register int len;
FILE *stream;
{
register int count = 0;
char line[MAXLINE+1];
*buf = '\0'; /* erase buffer */
while (fgets(line, MAXLINE, stream) != (char *) NULL) {
debug(D_GTAB, "line read = (%s)", dprint(line));
if (count)
buf[strlen(buf)-1] = '\0';
if ((count += strlen(line)) >= len)
return(FAIL); /* entry too long */
(void) strcat(buf, line);
if ((line[0] == '\n') || (line[0] == '#'))
return(SUCCESS); /* blank line */
}
return (*buf == '\0' ? QUIT : SUCCESS);
}
/*
** parseGtab() - fill in GTAB structure from buffer
**
** Returns FAIL if an error occurs.
*/
int
parseGtab(gtab, line)
GTAB *gtab;
register char *line;
{
register int field;
register char *p;
static int count;
static char p_cur[MAXID+1], p_next[MAXID+1];
static char p_login[MAXLOGIN+1];
debug(D_GTAB, "parseGtab() called");
/* initialize gtab to empty
*/
gtab->cur_id = (char *) NULL;
gtab->itermio.c_iflag = 0;
gtab->itermio.c_oflag = 0;
gtab->itermio.c_cflag = 0;
gtab->itermio.c_lflag = 0;
gtab->itermio.c_line = 0;
gtab->ftermio.c_iflag = 0;
gtab->ftermio.c_oflag = 0;
gtab->ftermio.c_cflag = 0;
gtab->ftermio.c_lflag = 0;
gtab->ftermio.c_line = 0;
gtab->login = (char *) NULL;
gtab->next_id = (char *) NULL;
if (LineD != (char *) NULL) { /* line disc given on command line */
addfield(&(gtab->itermio), LineD);
addfield(&(gtab->ftermio), LineD);
}
/* parse the line
*/
debug(D_GTAB, "parsing line:");
field = 1;
while (field != FAIL && field != SUCCESS) {
if ((p = nextword(line, &count)) == (char *) NULL) {
field = FAIL;
continue;
}
debug(D_GTAB, "field=%d, nextword=(%s)", field, p);
switch (field) {
case 1:
/* cur_id label
*/
(void) strncpy(p_cur, p, MAXID);
gtab->cur_id = p_cur;
field++;
break;
case 2:
/* '#' field separator
*/
if (*p != '#') {
field = FAIL;
continue;
}
field++;
break;
case 3:
/* initial termio flags
*/
if (*p == '#')
field++;
else
addfield(&(gtab->itermio), p);
break;
case 4:
/* final termio flags
*/
if (*p == '#')
field++;
else
addfield(&(gtab->ftermio), p);
break;
case 5:
/* login message --
* nextword won't be the whole message; look
* ahead to the next '#' and terminate string there
*/
if ((p = index(line, '#')) == (char *) NULL) {
field = FAIL;
continue;
}
*p = '\0';
p = line; /* point p to line again */
count = strlen(p)+1; /* adjust count accordingly */
debug(D_GTAB, "login=(%s)", p);
(void) strncpy(p_login, p, MAXLOGIN);
gtab->login = p_login;
field++;
break;
case 6:
/* next_id label
*/
(void) strncpy(p_next, p, MAXID);
gtab->next_id = p_next;
field = SUCCESS;
continue;
}
/* skip over word just processed
*/
line += count;
}
if (Check) {
(void) printf("id: \"%s\"\n", gtab->cur_id);
(void) printf("initial termio flags:\n");
(void) printf(" iflag: %o, oflag: %o, cflag: %o, lflag: %o\n",
(unsigned int)gtab->itermio.c_iflag,
(unsigned int)gtab->itermio.c_oflag,
(unsigned int)gtab->itermio.c_cflag,
(unsigned int)gtab->itermio.c_lflag);
(void) printf(" line disc: %o\n",
(unsigned int)gtab->itermio.c_line);
(void) printf("final termio flags:\n");
(void) printf(" iflag: %o, oflag: %o, cflag: %o, lflag: %o\n",
(unsigned int)gtab->ftermio.c_iflag,
(unsigned int)gtab->ftermio.c_oflag,
(unsigned int)gtab->ftermio.c_cflag,
(unsigned int)gtab->ftermio.c_lflag);
(void) printf(" line disc: %o\n",
(unsigned int)gtab->ftermio.c_line);
(void) printf("login prompt: \"%s\"\n", gtab->login);
(void) printf("next id: \"%s\"\n\n", gtab->next_id);
}
return(field);
}
/*
** nextword() - get next "word" from buffer
*/
char *
nextword(buf, count)
register char *buf;
register int *count;
{
register int num = 0;
register char *p;
static char word[MAXLINE+1];
while (*buf == ' ' || *buf == '\t' || *buf == '\\' ||
*buf == '\n') { /* skip leading whitespace */
buf++; num++;
}
p = word;
if (*buf == '#') { /* first char is '#' ? */
*p++ = *buf;
num++;
} else {
while (*buf && *buf != ' ' && *buf != '\t' && *buf != '\\' &&
*buf != '#' && *buf != '\n') {
*p++ = *buf++;
num++;
}
}
*p = '\0';
*count = num;
return(word);
}
#define TOGGLE(f,b) \
{if (inverted) (f) &= ~(b); else (f) |= (b);}
/*
** addfield() - add symbol to termio structure
*/
void
addfield(termio, field)
register TERMIO *termio;
register char *field;
{
register unsigned long val;
int inverted;
if (strequal(field, "SANE")) {
termio->c_iflag |= ISANE;
termio->c_oflag |= OSANE;
termio->c_cflag |= CSANE;
termio->c_lflag |= LSANE;
} else {
if (*field == '-'){
inverted = TRUE;
field++;
}
else inverted = FALSE;
if ((val = findsym(field, imodes)) != FAIL)
/* termio->c_iflag |= (ushort) val; */
TOGGLE (termio->c_iflag, val)
else if ((val = findsym(field, omodes)) != FAIL)
/* termio->c_oflag |= (ushort) val; */
TOGGLE (termio->c_oflag, val)
else if ((val = findsym(field, cmodes)) != FAIL)
/* termio->c_cflag |= (ushort) val; */
TOGGLE (termio->c_cflag, val)
else if ((val = findsym(field, lmodes)) != FAIL)
/* termio->c_lflag |= (ushort) val; */
TOGGLE (termio->c_lflag, val)
else if ((val = findsym(field, ldiscs)) != FAIL)
/* termio->c_line |= (ushort) val; */
TOGGLE (termio->c_line, val)
else if (Check) {
(void) sprintf(MsgBuf, "undefined symbol: %s", field);
chkerr(MsgBuf, OK);
}
}
}
/*
** findsym() - look for field in SYMTAB list
*/
int
findsym(field, symtab)
register char *field;
register SYMTAB *symtab;
{
for (; symtab->symbol != (char *) NULL; symtab++)
if (strequal(symtab->symbol, field))
return((int) symtab->value);
return(FAIL);
}
/*
** chkerr() - display error message from check routines
*/
void
chkerr(msg, status)
char *msg;
int status;
{
(void) printf("*** parsing error: %s ***\n", msg);
if (status)
(void) printf("*** checking halted ***\n");
else
(void) printf("*** checking continued ***\n");
errors++;
}
/* end of table.c */
|