calloc - Work around gcc-8 bug (2).
[dragonfly.git] / libexec / getty / subr.c
blobe0dde385f4ebc6062401e7798bbf28d58efb5e88
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 0
297 if (UC)
298 f |= LCASE;
299 #endif
301 if (HC)
302 SET(cflag, HUPCL);
303 else
304 CLR(cflag, HUPCL);
306 if (MB)
307 SET(cflag, MDMBUF);
308 else
309 CLR(cflag, MDMBUF);
311 if (HW)
312 SET(cflag, CRTSCTS);
313 else
314 CLR(cflag, CRTSCTS);
316 if (NL) {
317 SET(iflag, ICRNL);
318 SET(oflag, ONLCR|OPOST);
319 } else {
320 CLR(iflag, ICRNL);
321 CLR(oflag, ONLCR);
324 if (!HT)
325 SET(oflag, OXTABS|OPOST);
326 else
327 CLR(oflag, OXTABS);
329 #ifdef XXX_DELAY
330 SET(f, delaybits());
331 #endif
333 if (n == 1) { /* read mode flags */
334 if (RW) {
335 iflag = 0;
336 CLR(oflag, OPOST);
337 CLR(cflag, CSIZE|PARENB);
338 SET(cflag, CS8);
339 lflag = 0;
340 } else {
341 CLR(lflag, ICANON);
343 goto out;
346 if (n == 0)
347 goto out;
349 #if 0
350 if (CB)
351 SET(f, CRTBS);
352 #endif
354 if (CE)
355 SET(lflag, ECHOE);
356 else
357 CLR(lflag, ECHOE);
359 if (CK)
360 SET(lflag, ECHOKE);
361 else
362 CLR(lflag, ECHOKE);
364 if (PE)
365 SET(lflag, ECHOPRT);
366 else
367 CLR(lflag, ECHOPRT);
369 if (EC)
370 SET(lflag, ECHO);
371 else
372 CLR(lflag, ECHO);
374 if (XC)
375 SET(lflag, ECHOCTL);
376 else
377 CLR(lflag, ECHOCTL);
379 if (DX)
380 SET(lflag, IXANY);
381 else
382 CLR(lflag, IXANY);
384 out:
385 tmode.c_iflag = iflag;
386 tmode.c_oflag = oflag;
387 tmode.c_cflag = cflag;
388 tmode.c_lflag = lflag;
392 #ifdef XXX_DELAY
393 struct delayval {
394 unsigned delay; /* delay in ms */
395 int bits;
399 * below are random guesses, I can't be bothered checking
402 struct delayval crdelay[] = {
403 { 1, CR1 },
404 { 2, CR2 },
405 { 3, CR3 },
406 { 83, CR1 },
407 { 166, CR2 },
408 { 0, CR3 },
411 struct delayval nldelay[] = {
412 { 1, NL1 }, /* special, calculated */
413 { 2, NL2 },
414 { 3, NL3 },
415 { 100, NL2 },
416 { 0, NL3 },
419 struct delayval bsdelay[] = {
420 { 1, BS1 },
421 { 0, 0 },
424 struct delayval ffdelay[] = {
425 { 1, FF1 },
426 { 1750, FF1 },
427 { 0, FF1 },
430 struct delayval tbdelay[] = {
431 { 1, TAB1 },
432 { 2, TAB2 },
433 { 3, XTABS }, /* this is expand tabs */
434 { 100, TAB1 },
435 { 0, TAB2 },
439 delaybits(void)
441 int f;
443 f = adelay(CD, crdelay);
444 f |= adelay(ND, nldelay);
445 f |= adelay(FD, ffdelay);
446 f |= adelay(TD, tbdelay);
447 f |= adelay(BD, bsdelay);
448 return (f);
452 adelay(int ms, struct delayval *dp)
454 if (ms == 0)
455 return (0);
456 while (dp->delay && ms > dp->delay)
457 dp++;
458 return (dp->bits);
460 #endif
462 char editedhost[MAXHOSTNAMELEN];
464 void
465 edithost(const char *pattern)
467 regex_t regex;
468 regmatch_t *match;
469 int found;
471 if (pattern == NULL || *pattern == '\0')
472 goto copyasis;
473 if (regcomp(&regex, pattern, REG_EXTENDED) != 0)
474 goto copyasis;
476 match = calloc(regex.re_nsub + 1, sizeof(*match));
477 if (match == NULL) {
478 regfree(&regex);
479 goto copyasis;
482 found = !regexec(&regex, HN, regex.re_nsub + 1, match, 0);
483 if (found) {
484 size_t subex, totalsize;
487 * We found a match. If there were no parenthesized
488 * subexpressions in the pattern, use entire matched
489 * string as ``editedhost''; otherwise use the first
490 * matched subexpression.
492 subex = !!regex.re_nsub;
493 totalsize = match[subex].rm_eo - match[subex].rm_so + 1;
494 strlcpy(editedhost, HN + match[subex].rm_so, totalsize >
495 sizeof(editedhost) ? sizeof(editedhost) : totalsize);
497 free(match);
498 regfree(&regex);
499 if (found)
500 return;
502 * In case of any errors, or if the pattern did not match, pass
503 * the original hostname as is.
505 copyasis:
506 strlcpy(editedhost, HN, sizeof(editedhost));
509 static struct speedtab {
510 int speed;
511 int uxname;
512 } speedtab[] = {
513 { 50, B50 },
514 { 75, B75 },
515 { 110, B110 },
516 { 134, B134 },
517 { 150, B150 },
518 { 200, B200 },
519 { 300, B300 },
520 { 600, B600 },
521 { 1200, B1200 },
522 { 1800, B1800 },
523 { 2400, B2400 },
524 { 4800, B4800 },
525 { 9600, B9600 },
526 { 19200, EXTA },
527 { 19, EXTA }, /* for people who say 19.2K */
528 { 38400, EXTB },
529 { 38, EXTB },
530 { 7200, EXTB }, /* alternative */
531 { 57600, B57600 },
532 { 115200, B115200 },
533 { 230400, B230400 },
534 { 0, 0 }
538 speed(int val)
540 struct speedtab *sp;
542 if (val <= B230400)
543 return (val);
545 for (sp = speedtab; sp->speed; sp++)
546 if (sp->speed == val)
547 return (sp->uxname);
549 return (B300); /* default in impossible cases */
552 void
553 makeenv(char *env[])
555 static char termbuf[128] = "TERM=";
556 char *p, *q;
557 char **ep;
559 ep = env;
560 if (TT && *TT) {
561 strlcat(termbuf, TT, sizeof(termbuf));
562 *ep++ = termbuf;
564 if ((p = EV)) {
565 q = p;
566 while ((q = strchr(q, ','))) {
567 *q++ = '\0';
568 *ep++ = p;
569 p = q;
571 if (*p)
572 *ep++ = p;
574 *ep = NULL;
578 * This speed select mechanism is written for the Develcon DATASWITCH.
579 * The Develcon sends a string of the form "B{speed}\n" at a predefined
580 * baud rate. This string indicates the user's actual speed.
581 * The routine below returns the terminal type mapped from derived speed.
583 static struct portselect {
584 const char *ps_baud;
585 const char *ps_type;
586 } portspeeds[] = {
587 { "B110", "std.110" },
588 { "B134", "std.134" },
589 { "B150", "std.150" },
590 { "B300", "std.300" },
591 { "B600", "std.600" },
592 { "B1200", "std.1200" },
593 { "B2400", "std.2400" },
594 { "B4800", "std.4800" },
595 { "B9600", "std.9600" },
596 { "B19200", "std.19200" },
597 { NULL, NULL }
600 const char *
601 portselector(void)
603 char c, baud[20];
604 const char *type = "default";
605 struct portselect *ps;
606 size_t len;
608 alarm(5*60);
609 for (len = 0; len < sizeof (baud) - 1; len++) {
610 if (read(STDIN_FILENO, &c, 1) <= 0)
611 break;
612 c &= 0177;
613 if (c == '\n' || c == '\r')
614 break;
615 if (c == 'B')
616 len = 0; /* in case of leading garbage */
617 baud[len] = c;
619 baud[len] = '\0';
620 for (ps = portspeeds; ps->ps_baud; ps++)
621 if (strcmp(ps->ps_baud, baud) == 0) {
622 type = ps->ps_type;
623 break;
625 sleep(2); /* wait for connection to complete */
626 return (type);
630 * This auto-baud speed select mechanism is written for the Micom 600
631 * portselector. Selection is done by looking at how the character '\r'
632 * is garbled at the different speeds.
634 const char *
635 autobaud(void)
637 struct pollfd set[1];
638 struct timespec timeout;
639 char c;
640 const char *type = "9600-baud";
642 (void)tcflush(0, TCIOFLUSH);
643 set[0].fd = STDIN_FILENO;
644 set[0].events = POLLIN;
645 if (poll(set, 1, 5000) <= 0)
646 return (type);
647 if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char))
648 return (type);
649 timeout.tv_sec = 0;
650 timeout.tv_nsec = 20000;
651 (void)nanosleep(&timeout, NULL);
652 (void)tcflush(0, TCIOFLUSH);
653 switch (c & 0377) {
655 case 0200: /* 300-baud */
656 type = "300-baud";
657 break;
659 case 0346: /* 1200-baud */
660 type = "1200-baud";
661 break;
663 case 015: /* 2400-baud */
664 case 0215:
665 type = "2400-baud";
666 break;
668 default: /* 4800-baud */
669 type = "4800-baud";
670 break;
672 case 0377: /* 9600-baud */
673 type = "9600-baud";
674 break;
676 return (type);