pfctl: Match prototype of pfctl_load_hostid.
[freebsd-src.git] / libexec / getty / subr.c
blob4716e97d7bffbb3972eda5ae72118b8ecf52cb8d
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93";
33 #endif
34 static const char rcsid[] =
35 "$FreeBSD$";
36 #endif /* not lint */
39 * Melbourne getty.
41 #include <sys/ioctl.h>
42 #include <sys/param.h>
43 #include <sys/time.h>
45 #include <poll.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <syslog.h>
49 #include <termios.h>
50 #include <unistd.h>
52 #include "gettytab.h"
53 #include "pathnames.h"
54 #include "extern.h"
59 * Get a table entry.
61 void
62 gettable(const char *name, char *buf)
64 struct gettystrs *sp;
65 struct gettynums *np;
66 struct gettyflags *fp;
67 long n;
68 int l;
69 char *p;
70 char *msg = NULL;
71 const char *dba[2];
73 static int firsttime = 1;
75 dba[0] = _PATH_GETTYTAB;
76 dba[1] = NULL;
78 if (firsttime) {
80 * we need to strdup() anything in the strings array
81 * initially in order to simplify things later
83 for (sp = gettystrs; sp->field; sp++)
84 if (sp->value != NULL) {
85 /* handle these ones more carefully */
86 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
87 l = 2;
88 else
89 l = strlen(sp->value) + 1;
90 if ((p = malloc(l)) != NULL) {
91 strncpy(p, sp->value, l);
92 p[l-1] = '\0';
95 * replace, even if NULL, else we'll
96 * have problems with free()ing static mem
98 sp->value = p;
100 firsttime = 0;
103 switch (cgetent(&buf, (char **)dba, (char *)name)) {
104 case 1:
105 msg = "%s: couldn't resolve 'tc=' in gettytab '%s'";
106 case 0:
107 break;
108 case -1:
109 msg = "%s: unknown gettytab entry '%s'";
110 break;
111 case -2:
112 msg = "%s: retrieving gettytab entry '%s': %m";
113 break;
114 case -3:
115 msg = "%s: recursive 'tc=' reference gettytab entry '%s'";
116 break;
117 default:
118 msg = "%s: unexpected cgetent() error for entry '%s'";
119 break;
122 if (msg != NULL) {
123 syslog(LOG_ERR, msg, "getty", name);
124 return;
127 for (sp = gettystrs; sp->field; sp++) {
128 if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) {
129 if (sp->value) {
130 /* prefer existing value */
131 if (strcmp(p, sp->value) != 0)
132 free(sp->value);
133 else {
134 free(p);
135 p = sp->value;
138 sp->value = p;
139 } else if (l == -1) {
140 free(sp->value);
141 sp->value = NULL;
145 for (np = gettynums; np->field; np++) {
146 if (cgetnum(buf, (char*)np->field, &n) == -1)
147 np->set = 0;
148 else {
149 np->set = 1;
150 np->value = n;
154 for (fp = gettyflags; fp->field; fp++) {
155 if (cgetcap(buf, (char *)fp->field, ':') == NULL)
156 fp->set = 0;
157 else {
158 fp->set = 1;
159 fp->value = 1 ^ fp->invrt;
164 void
165 gendefaults(void)
167 struct gettystrs *sp;
168 struct gettynums *np;
169 struct gettyflags *fp;
171 for (sp = gettystrs; sp->field; sp++)
172 if (sp->value)
173 sp->defalt = strdup(sp->value);
174 for (np = gettynums; np->field; np++)
175 if (np->set)
176 np->defalt = np->value;
177 for (fp = gettyflags; fp->field; fp++)
178 if (fp->set)
179 fp->defalt = fp->value;
180 else
181 fp->defalt = fp->invrt;
184 void
185 setdefaults(void)
187 struct gettystrs *sp;
188 struct gettynums *np;
189 struct gettyflags *fp;
191 for (sp = gettystrs; sp->field; sp++)
192 if (!sp->value)
193 sp->value = !sp->defalt ? sp->defalt
194 : strdup(sp->defalt);
195 for (np = gettynums; np->field; np++)
196 if (!np->set)
197 np->value = np->defalt;
198 for (fp = gettyflags; fp->field; fp++)
199 if (!fp->set)
200 fp->value = fp->defalt;
203 static char **
204 charnames[] = {
205 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
206 &SU, &DS, &RP, &FL, &WE, &LN, 0
209 static char *
210 charvars[] = {
211 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
212 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
213 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
214 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
215 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
218 void
219 setchars(void)
221 int i;
222 const char *p;
224 for (i = 0; charnames[i]; i++) {
225 p = *charnames[i];
226 if (p && *p)
227 *charvars[i] = *p;
228 else
229 *charvars[i] = _POSIX_VDISABLE;
233 /* Macros to clear/set/test flags. */
234 #define SET(t, f) (t) |= (f)
235 #define CLR(t, f) (t) &= ~(f)
236 #define ISSET(t, f) ((t) & (f))
238 void
239 set_flags(int n)
241 tcflag_t iflag, oflag, cflag, lflag;
244 switch (n) {
245 case 0:
246 if (C0set && I0set && L0set && O0set) {
247 tmode.c_cflag = C0;
248 tmode.c_iflag = I0;
249 tmode.c_lflag = L0;
250 tmode.c_oflag = O0;
251 return;
253 break;
254 case 1:
255 if (C1set && I1set && L1set && O1set) {
256 tmode.c_cflag = C1;
257 tmode.c_iflag = I1;
258 tmode.c_lflag = L1;
259 tmode.c_oflag = O1;
260 return;
262 break;
263 default:
264 if (C2set && I2set && L2set && O2set) {
265 tmode.c_cflag = C2;
266 tmode.c_iflag = I2;
267 tmode.c_lflag = L2;
268 tmode.c_oflag = O2;
269 return;
271 break;
274 iflag = omode.c_iflag;
275 oflag = omode.c_oflag;
276 cflag = omode.c_cflag;
277 lflag = omode.c_lflag;
279 if (NP) {
280 CLR(cflag, CSIZE|PARENB);
281 SET(cflag, CS8);
282 CLR(iflag, ISTRIP|INPCK|IGNPAR);
283 } else if (AP || EP || OP) {
284 CLR(cflag, CSIZE);
285 SET(cflag, CS7|PARENB);
286 SET(iflag, ISTRIP);
287 if (OP && !EP) {
288 SET(iflag, INPCK|IGNPAR);
289 SET(cflag, PARODD);
290 if (AP)
291 CLR(iflag, INPCK);
292 } else if (EP && !OP) {
293 SET(iflag, INPCK|IGNPAR);
294 CLR(cflag, PARODD);
295 if (AP)
296 CLR(iflag, INPCK);
297 } else if (AP || (EP && OP)) {
298 CLR(iflag, INPCK|IGNPAR);
299 CLR(cflag, PARODD);
301 } /* else, leave as is */
303 #if 0
304 if (UC)
305 f |= LCASE;
306 #endif
308 if (HC)
309 SET(cflag, HUPCL);
310 else
311 CLR(cflag, HUPCL);
313 if (MB)
314 SET(cflag, MDMBUF);
315 else
316 CLR(cflag, MDMBUF);
318 if (HW)
319 SET(cflag, CRTSCTS);
320 else
321 CLR(cflag, CRTSCTS);
323 if (NL) {
324 SET(iflag, ICRNL);
325 SET(oflag, ONLCR|OPOST);
326 } else {
327 CLR(iflag, ICRNL);
328 CLR(oflag, ONLCR);
331 if (!HT)
332 SET(oflag, OXTABS|OPOST);
333 else
334 CLR(oflag, OXTABS);
336 #ifdef XXX_DELAY
337 SET(f, delaybits());
338 #endif
340 if (n == 1) { /* read mode flags */
341 if (RW) {
342 iflag = 0;
343 CLR(oflag, OPOST);
344 CLR(cflag, CSIZE|PARENB);
345 SET(cflag, CS8);
346 lflag = 0;
347 } else {
348 CLR(lflag, ICANON);
350 goto out;
353 if (n == 0)
354 goto out;
356 #if 0
357 if (CB)
358 SET(f, CRTBS);
359 #endif
361 if (CE)
362 SET(lflag, ECHOE);
363 else
364 CLR(lflag, ECHOE);
366 if (CK)
367 SET(lflag, ECHOKE);
368 else
369 CLR(lflag, ECHOKE);
371 if (PE)
372 SET(lflag, ECHOPRT);
373 else
374 CLR(lflag, ECHOPRT);
376 if (EC)
377 SET(lflag, ECHO);
378 else
379 CLR(lflag, ECHO);
381 if (XC)
382 SET(lflag, ECHOCTL);
383 else
384 CLR(lflag, ECHOCTL);
386 if (DX)
387 SET(lflag, IXANY);
388 else
389 CLR(lflag, IXANY);
391 out:
392 tmode.c_iflag = iflag;
393 tmode.c_oflag = oflag;
394 tmode.c_cflag = cflag;
395 tmode.c_lflag = lflag;
399 #ifdef XXX_DELAY
400 struct delayval {
401 unsigned delay; /* delay in ms */
402 int bits;
406 * below are random guesses, I can't be bothered checking
409 struct delayval crdelay[] = {
410 { 1, CR1 },
411 { 2, CR2 },
412 { 3, CR3 },
413 { 83, CR1 },
414 { 166, CR2 },
415 { 0, CR3 },
418 struct delayval nldelay[] = {
419 { 1, NL1 }, /* special, calculated */
420 { 2, NL2 },
421 { 3, NL3 },
422 { 100, NL2 },
423 { 0, NL3 },
426 struct delayval bsdelay[] = {
427 { 1, BS1 },
428 { 0, 0 },
431 struct delayval ffdelay[] = {
432 { 1, FF1 },
433 { 1750, FF1 },
434 { 0, FF1 },
437 struct delayval tbdelay[] = {
438 { 1, TAB1 },
439 { 2, TAB2 },
440 { 3, XTABS }, /* this is expand tabs */
441 { 100, TAB1 },
442 { 0, TAB2 },
446 delaybits(void)
448 int f;
450 f = adelay(CD, crdelay);
451 f |= adelay(ND, nldelay);
452 f |= adelay(FD, ffdelay);
453 f |= adelay(TD, tbdelay);
454 f |= adelay(BD, bsdelay);
455 return (f);
459 adelay(int ms, struct delayval *dp)
461 if (ms == 0)
462 return (0);
463 while (dp->delay && ms > dp->delay)
464 dp++;
465 return (dp->bits);
467 #endif
469 char editedhost[MAXHOSTNAMELEN];
471 void
472 edithost(const char *pat)
474 const char *host = HN;
475 char *res = editedhost;
477 if (!pat)
478 pat = "";
479 while (*pat) {
480 switch (*pat) {
482 case '#':
483 if (*host)
484 host++;
485 break;
487 case '@':
488 if (*host)
489 *res++ = *host++;
490 break;
492 default:
493 *res++ = *pat;
494 break;
497 if (res == &editedhost[sizeof editedhost - 1]) {
498 *res = '\0';
499 return;
501 pat++;
503 if (*host)
504 strncpy(res, host, sizeof editedhost - (res - editedhost) - 1);
505 else
506 *res = '\0';
507 editedhost[sizeof editedhost - 1] = '\0';
510 static struct speedtab {
511 int speed;
512 int uxname;
513 } speedtab[] = {
514 { 50, B50 },
515 { 75, B75 },
516 { 110, B110 },
517 { 134, B134 },
518 { 150, B150 },
519 { 200, B200 },
520 { 300, B300 },
521 { 600, B600 },
522 { 1200, B1200 },
523 { 1800, B1800 },
524 { 2400, B2400 },
525 { 4800, B4800 },
526 { 9600, B9600 },
527 { 19200, EXTA },
528 { 19, EXTA }, /* for people who say 19.2K */
529 { 38400, EXTB },
530 { 38, EXTB },
531 { 7200, EXTB }, /* alternative */
532 { 57600, B57600 },
533 { 115200, B115200 },
534 { 230400, B230400 },
535 { 0 }
539 speed(int val)
541 struct speedtab *sp;
543 if (val <= B230400)
544 return (val);
546 for (sp = speedtab; sp->speed; sp++)
547 if (sp->speed == val)
548 return (sp->uxname);
550 return (B300); /* default in impossible cases */
553 void
554 makeenv(char *env[])
556 static char termbuf[128] = "TERM=";
557 char *p, *q;
558 char **ep;
560 ep = env;
561 if (TT && *TT) {
562 strlcat(termbuf, TT, sizeof(termbuf));
563 *ep++ = termbuf;
565 if ((p = EV)) {
566 q = p;
567 while ((q = strchr(q, ','))) {
568 *q++ = '\0';
569 *ep++ = p;
570 p = q;
572 if (*p)
573 *ep++ = p;
575 *ep = (char *)0;
579 * This speed select mechanism is written for the Develcon DATASWITCH.
580 * The Develcon sends a string of the form "B{speed}\n" at a predefined
581 * baud rate. This string indicates the user's actual speed.
582 * The routine below returns the terminal type mapped from derived speed.
584 struct portselect {
585 const char *ps_baud;
586 const char *ps_type;
587 } portspeeds[] = {
588 { "B110", "std.110" },
589 { "B134", "std.134" },
590 { "B150", "std.150" },
591 { "B300", "std.300" },
592 { "B600", "std.600" },
593 { "B1200", "std.1200" },
594 { "B2400", "std.2400" },
595 { "B4800", "std.4800" },
596 { "B9600", "std.9600" },
597 { "B19200", "std.19200" },
598 { NULL, NULL }
601 const char *
602 portselector(void)
604 char c, baud[20];
605 const char *type = "default";
606 struct portselect *ps;
607 size_t len;
609 alarm(5*60);
610 for (len = 0; len < sizeof (baud) - 1; len++) {
611 if (read(STDIN_FILENO, &c, 1) <= 0)
612 break;
613 c &= 0177;
614 if (c == '\n' || c == '\r')
615 break;
616 if (c == 'B')
617 len = 0; /* in case of leading garbage */
618 baud[len] = c;
620 baud[len] = '\0';
621 for (ps = portspeeds; ps->ps_baud; ps++)
622 if (strcmp(ps->ps_baud, baud) == 0) {
623 type = ps->ps_type;
624 break;
626 sleep(2); /* wait for connection to complete */
627 return (type);
631 * This auto-baud speed select mechanism is written for the Micom 600
632 * portselector. Selection is done by looking at how the character '\r'
633 * is garbled at the different speeds.
635 const char *
636 autobaud(void)
638 struct pollfd set[1];
639 struct timespec timeout;
640 char c;
641 const char *type = "9600-baud";
643 (void)tcflush(0, TCIOFLUSH);
644 set[0].fd = STDIN_FILENO;
645 set[0].events = POLLIN;
646 if (poll(set, 1, 5000) <= 0)
647 return (type);
648 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
649 return (type);
650 timeout.tv_sec = 0;
651 timeout.tv_nsec = 20000;
652 (void)nanosleep(&timeout, NULL);
653 (void)tcflush(0, TCIOFLUSH);
654 switch (c & 0377) {
656 case 0200: /* 300-baud */
657 type = "300-baud";
658 break;
660 case 0346: /* 1200-baud */
661 type = "1200-baud";
662 break;
664 case 015: /* 2400-baud */
665 case 0215:
666 type = "2400-baud";
667 break;
669 default: /* 4800-baud */
670 type = "4800-baud";
671 break;
673 case 0377: /* 9600-baud */
674 type = "9600-baud";
675 break;
677 return (type);