dhcpcd: update README.DRAGONFLY
[dragonfly.git] / libexec / getty / subr.c
blob86eda77d031270454cc3643eaabc2bc12d92dfbb
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
31 * @(#)from: subr.c 8.1 (Berkeley) 6/4/93
32 * $FreeBSD: head/libexec/getty/subr.c 329724 2018-02-21 15:57:24Z trasz $
36 * Melbourne getty.
38 #include <sys/ioctl.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
42 #include <poll.h>
43 #include <regex.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <termios.h>
48 #include <unistd.h>
50 #include "gettytab.h"
51 #include "pathnames.h"
52 #include "extern.h"
55 * Get a table entry.
57 void
58 gettable(const char *name, char *buf)
60 struct gettystrs *sp;
61 struct gettynums *np;
62 struct gettyflags *fp;
63 long n;
64 int l;
65 char *p;
66 static char path_gettytab[PATH_MAX];
67 char *dba[2];
69 static int firsttime = 1;
71 strlcpy(path_gettytab, _PATH_GETTYTAB, sizeof(path_gettytab));
72 dba[0] = path_gettytab;
73 dba[1] = NULL;
75 if (firsttime) {
77 * we need to strdup() anything in the strings array
78 * initially in order to simplify things later
80 for (sp = gettystrs; sp->field; sp++)
81 if (sp->value != NULL) {
82 /* handle these ones more carefully */
83 if (sp >= &gettystrs[4] && sp <= &gettystrs[6])
84 l = 2;
85 else
86 l = strlen(sp->value) + 1;
87 if ((p = malloc(l)) != NULL) {
88 strncpy(p, sp->value, l);
89 p[l-1] = '\0';
92 * replace, even if NULL, else we'll
93 * have problems with free()ing static mem
95 sp->value = p;
97 firsttime = 0;
100 switch (cgetent(&buf, dba, name)) {
101 case 1:
102 syslog(LOG_ERR, "getty: couldn't resolve 'tc=' in gettytab '%s'", name);
103 return;
104 case 0:
105 break;
106 case -1:
107 syslog(LOG_ERR, "getty: unknown gettytab entry '%s'", name);
108 return;
109 case -2:
110 syslog(LOG_ERR, "getty: retrieving gettytab entry '%s': %m", name);
111 return;
112 case -3:
113 syslog(LOG_ERR, "getty: recursive 'tc=' reference gettytab entry '%s'", name);
114 return;
115 default:
116 syslog(LOG_ERR, "getty: unexpected cgetent() error for entry '%s'", name);
117 return;
120 for (sp = gettystrs; sp->field; sp++) {
121 if ((l = cgetstr(buf, sp->field, &p)) >= 0) {
122 if (sp->value) {
123 /* prefer existing value */
124 if (strcmp(p, sp->value) != 0)
125 free(sp->value);
126 else {
127 free(p);
128 p = sp->value;
131 sp->value = p;
132 } else if (l == -1) {
133 free(sp->value);
134 sp->value = NULL;
138 for (np = gettynums; np->field; np++) {
139 if (cgetnum(buf, np->field, &n) == -1)
140 np->set = 0;
141 else {
142 np->set = 1;
143 np->value = n;
147 for (fp = gettyflags; fp->field; fp++) {
148 if (cgetcap(buf, fp->field, ':') == NULL)
149 fp->set = 0;
150 else {
151 fp->set = 1;
152 fp->value = 1 ^ fp->invrt;
157 void
158 gendefaults(void)
160 struct gettystrs *sp;
161 struct gettynums *np;
162 struct gettyflags *fp;
164 for (sp = gettystrs; sp->field; sp++)
165 if (sp->value)
166 sp->defalt = strdup(sp->value);
167 for (np = gettynums; np->field; np++)
168 if (np->set)
169 np->defalt = np->value;
170 for (fp = gettyflags; fp->field; fp++)
171 if (fp->set)
172 fp->defalt = fp->value;
173 else
174 fp->defalt = fp->invrt;
177 void
178 setdefaults(void)
180 struct gettystrs *sp;
181 struct gettynums *np;
182 struct gettyflags *fp;
184 for (sp = gettystrs; sp->field; sp++)
185 if (!sp->value)
186 sp->value = !sp->defalt ? sp->defalt
187 : strdup(sp->defalt);
188 for (np = gettynums; np->field; np++)
189 if (!np->set)
190 np->value = np->defalt;
191 for (fp = gettyflags; fp->field; fp++)
192 if (!fp->set)
193 fp->value = fp->defalt;
196 static char **
197 charnames[] = {
198 &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK,
199 &SU, &DS, &RP, &FL, &WE, &LN, 0
202 static char *
203 charvars[] = {
204 &tmode.c_cc[VERASE], &tmode.c_cc[VKILL], &tmode.c_cc[VINTR],
205 &tmode.c_cc[VQUIT], &tmode.c_cc[VSTART], &tmode.c_cc[VSTOP],
206 &tmode.c_cc[VEOF], &tmode.c_cc[VEOL], &tmode.c_cc[VSUSP],
207 &tmode.c_cc[VDSUSP], &tmode.c_cc[VREPRINT], &tmode.c_cc[VDISCARD],
208 &tmode.c_cc[VWERASE], &tmode.c_cc[VLNEXT], 0
211 void
212 setchars(void)
214 int i;
215 const char *p;
217 for (i = 0; charnames[i]; i++) {
218 p = *charnames[i];
219 if (p && *p)
220 *charvars[i] = *p;
221 else
222 *charvars[i] = _POSIX_VDISABLE;
226 /* Macros to clear/set/test flags. */
227 #define SET(t, f) (t) |= (f)
228 #define CLR(t, f) (t) &= ~(f)
229 #define ISSET(t, f) ((t) & (f))
231 void
232 set_flags(int n)
234 tcflag_t iflag, oflag, cflag, lflag;
237 switch (n) {
238 case 0:
239 if (C0set && I0set && L0set && O0set) {
240 tmode.c_cflag = C0;
241 tmode.c_iflag = I0;
242 tmode.c_lflag = L0;
243 tmode.c_oflag = O0;
244 return;
246 break;
247 case 1:
248 if (C1set && I1set && L1set && O1set) {
249 tmode.c_cflag = C1;
250 tmode.c_iflag = I1;
251 tmode.c_lflag = L1;
252 tmode.c_oflag = O1;
253 return;
255 break;
256 default:
257 if (C2set && I2set && L2set && O2set) {
258 tmode.c_cflag = C2;
259 tmode.c_iflag = I2;
260 tmode.c_lflag = L2;
261 tmode.c_oflag = O2;
262 return;
264 break;
267 iflag = omode.c_iflag;
268 oflag = omode.c_oflag;
269 cflag = omode.c_cflag;
270 lflag = omode.c_lflag;
272 if (NP) {
273 CLR(cflag, CSIZE|PARENB);
274 SET(cflag, CS8);
275 CLR(iflag, ISTRIP|INPCK|IGNPAR);
276 } else if (AP || EP || OP) {
277 CLR(cflag, CSIZE);
278 SET(cflag, CS7|PARENB);
279 SET(iflag, ISTRIP);
280 if (OP && !EP) {
281 SET(iflag, INPCK|IGNPAR);
282 SET(cflag, PARODD);
283 if (AP)
284 CLR(iflag, INPCK);
285 } else if (EP && !OP) {
286 SET(iflag, INPCK|IGNPAR);
287 CLR(cflag, PARODD);
288 if (AP)
289 CLR(iflag, INPCK);
290 } else if (AP || (EP && OP)) {
291 CLR(iflag, INPCK|IGNPAR);
292 CLR(cflag, PARODD);
294 } /* else, leave as is */
296 if (HC)
297 SET(cflag, HUPCL);
298 else
299 CLR(cflag, HUPCL);
301 if (MB)
302 SET(cflag, MDMBUF);
303 else
304 CLR(cflag, MDMBUF);
306 if (HW)
307 SET(cflag, CRTSCTS);
308 else
309 CLR(cflag, CRTSCTS);
311 if (NL) {
312 SET(iflag, ICRNL);
313 SET(oflag, ONLCR|OPOST);
314 } else {
315 CLR(iflag, ICRNL);
316 CLR(oflag, ONLCR);
319 if (!HT)
320 SET(oflag, OXTABS|OPOST);
321 else
322 CLR(oflag, OXTABS);
324 #ifdef XXX_DELAY
325 SET(f, delaybits());
326 #endif
328 if (n == 1) { /* read mode flags */
329 if (RW) {
330 iflag = 0;
331 CLR(oflag, OPOST);
332 CLR(cflag, CSIZE|PARENB);
333 SET(cflag, CS8);
334 lflag = 0;
335 } else {
336 CLR(lflag, ICANON);
338 goto out;
341 if (n == 0)
342 goto out;
344 #if 0
345 if (CB)
346 SET(f, CRTBS);
347 #endif
349 if (CE)
350 SET(lflag, ECHOE);
351 else
352 CLR(lflag, ECHOE);
354 if (CK)
355 SET(lflag, ECHOKE);
356 else
357 CLR(lflag, ECHOKE);
359 if (PE)
360 SET(lflag, ECHOPRT);
361 else
362 CLR(lflag, ECHOPRT);
364 if (EC)
365 SET(lflag, ECHO);
366 else
367 CLR(lflag, ECHO);
369 if (XC)
370 SET(lflag, ECHOCTL);
371 else
372 CLR(lflag, ECHOCTL);
374 if (DX)
375 SET(lflag, IXANY);
376 else
377 CLR(lflag, IXANY);
379 out:
380 tmode.c_iflag = iflag;
381 tmode.c_oflag = oflag;
382 tmode.c_cflag = cflag;
383 tmode.c_lflag = lflag;
387 #ifdef XXX_DELAY
388 struct delayval {
389 unsigned delay; /* delay in ms */
390 int bits;
394 * below are random guesses, I can't be bothered checking
397 struct delayval crdelay[] = {
398 { 1, CR1 },
399 { 2, CR2 },
400 { 3, CR3 },
401 { 83, CR1 },
402 { 166, CR2 },
403 { 0, CR3 },
406 struct delayval nldelay[] = {
407 { 1, NL1 }, /* special, calculated */
408 { 2, NL2 },
409 { 3, NL3 },
410 { 100, NL2 },
411 { 0, NL3 },
414 struct delayval bsdelay[] = {
415 { 1, BS1 },
416 { 0, 0 },
419 struct delayval ffdelay[] = {
420 { 1, FF1 },
421 { 1750, FF1 },
422 { 0, FF1 },
425 struct delayval tbdelay[] = {
426 { 1, TAB1 },
427 { 2, TAB2 },
428 { 3, XTABS }, /* this is expand tabs */
429 { 100, TAB1 },
430 { 0, TAB2 },
434 delaybits(void)
436 int f;
438 f = adelay(CD, crdelay);
439 f |= adelay(ND, nldelay);
440 f |= adelay(FD, ffdelay);
441 f |= adelay(TD, tbdelay);
442 f |= adelay(BD, bsdelay);
443 return (f);
447 adelay(int ms, struct delayval *dp)
449 if (ms == 0)
450 return (0);
451 while (dp->delay && ms > dp->delay)
452 dp++;
453 return (dp->bits);
455 #endif
457 char editedhost[MAXHOSTNAMELEN];
459 void
460 edithost(const char *pattern)
462 regex_t regex;
463 regmatch_t *match;
464 int found;
466 if (pattern == NULL || *pattern == '\0')
467 goto copyasis;
468 if (regcomp(&regex, pattern, REG_EXTENDED) != 0)
469 goto copyasis;
471 match = calloc(regex.re_nsub + 1, sizeof(*match));
472 if (match == NULL) {
473 regfree(&regex);
474 goto copyasis;
477 found = !regexec(&regex, HN, regex.re_nsub + 1, match, 0);
478 if (found) {
479 size_t subex, totalsize;
482 * We found a match. If there were no parenthesized
483 * subexpressions in the pattern, use entire matched
484 * string as ``editedhost''; otherwise use the first
485 * matched subexpression.
487 subex = !!regex.re_nsub;
488 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
489 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
490 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
492 free(match);
493 regfree(&regex);
494 if (found)
495 return;
497 * In case of any errors, or if the pattern did not match, pass
498 * the original hostname as is.
500 copyasis:
501 strlcpy(editedhost, HN, sizeof(editedhost));
504 static struct speedtab {
505 int speed;
506 int uxname;
507 } speedtab[] = {
508 { 50, B50 },
509 { 75, B75 },
510 { 110, B110 },
511 { 134, B134 },
512 { 150, B150 },
513 { 200, B200 },
514 { 300, B300 },
515 { 600, B600 },
516 { 1200, B1200 },
517 { 1800, B1800 },
518 { 2400, B2400 },
519 { 4800, B4800 },
520 { 9600, B9600 },
521 { 19200, EXTA },
522 { 19, EXTA }, /* for people who say 19.2K */
523 { 38400, EXTB },
524 { 38, EXTB },
525 { 7200, EXTB }, /* alternative */
526 { 57600, B57600 },
527 { 115200, B115200 },
528 { 230400, B230400 },
529 { 0, 0 }
533 speed(int val)
535 struct speedtab *sp;
537 if (val <= B230400)
538 return (val);
540 for (sp = speedtab; sp->speed; sp++)
541 if (sp->speed == val)
542 return (sp->uxname);
544 return (B300); /* default in impossible cases */
547 void
548 makeenv(char *env[])
550 static char termbuf[128] = "TERM=";
551 char *p, *q;
552 char **ep;
554 ep = env;
555 if (TT && *TT) {
556 strlcat(termbuf, TT, sizeof(termbuf));
557 *ep++ = termbuf;
559 if ((p = EV)) {
560 q = p;
561 while ((q = strchr(q, ','))) {
562 *q++ = '\0';
563 *ep++ = p;
564 p = q;
566 if (*p)
567 *ep++ = p;
569 *ep = NULL;
573 * This speed select mechanism is written for the Develcon DATASWITCH.
574 * The Develcon sends a string of the form "B{speed}\n" at a predefined
575 * baud rate. This string indicates the user's actual speed.
576 * The routine below returns the terminal type mapped from derived speed.
578 static struct portselect {
579 const char *ps_baud;
580 const char *ps_type;
581 } portspeeds[] = {
582 { "B110", "std.110" },
583 { "B134", "std.134" },
584 { "B150", "std.150" },
585 { "B300", "std.300" },
586 { "B600", "std.600" },
587 { "B1200", "std.1200" },
588 { "B2400", "std.2400" },
589 { "B4800", "std.4800" },
590 { "B9600", "std.9600" },
591 { "B19200", "std.19200" },
592 { "B38400", "std.38400" },
593 { "B57600", "std.57600" },
594 { "B115200", "std.115200" },
595 { "B230400", "std.230400" },
596 { NULL, NULL }
599 const char *
600 portselector(void)
602 char c, baud[20];
603 const char *type = "default";
604 struct portselect *ps;
605 size_t len;
607 alarm(5*60);
608 for (len = 0; len < sizeof (baud) - 1; len++) {
609 if (read(STDIN_FILENO, &c, 1) <= 0)
610 break;
611 c &= 0177;
612 if (c == '\n' || c == '\r')
613 break;
614 if (c == 'B')
615 len = 0; /* in case of leading garbage */
616 baud[len] = c;
618 baud[len] = '\0';
619 for (ps = portspeeds; ps->ps_baud; ps++)
620 if (strcmp(ps->ps_baud, baud) == 0) {
621 type = ps->ps_type;
622 break;
624 sleep(2); /* wait for connection to complete */
625 return (type);
629 * This auto-baud speed select mechanism is written for the Micom 600
630 * portselector. Selection is done by looking at how the character '\r'
631 * is garbled at the different speeds.
633 const char *
634 autobaud(void)
636 struct pollfd set[1];
637 struct timespec timeout;
638 char c;
639 const char *type = "9600-baud";
641 (void)tcflush(0, TCIOFLUSH);
642 set[0].fd = STDIN_FILENO;
643 set[0].events = POLLIN;
644 if (poll(set, 1, 5000) <= 0)
645 return (type);
646 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
647 return (type);
648 timeout.tv_sec = 0;
649 timeout.tv_nsec = 20000;
650 (void)nanosleep(&timeout, NULL);
651 (void)tcflush(0, TCIOFLUSH);
652 switch (c & 0377) {
654 case 0200: /* 300-baud */
655 type = "300-baud";
656 break;
658 case 0346: /* 1200-baud */
659 type = "1200-baud";
660 break;
662 case 015: /* 2400-baud */
663 case 0215:
664 type = "2400-baud";
665 break;
667 default: /* 4800-baud */
668 type = "4800-baud";
669 break;
671 case 0377: /* 9600-baud */
672 type = "9600-baud";
673 break;
675 return (type);