]>
git.cameronkatri.com Git - apple_cmds.git/blob - system_cmds/getty.tproj/subr.c
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
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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93";
40 __unused
static const char rcsid
[] =
41 "$FreeBSD: src/libexec/getty/subr.c,v 1.19 2004/06/25 10:11:28 phk Exp $";
54 #include <sys/ioctl.h>
55 #include <sys/param.h>
60 #include "pathnames.h"
67 gettable(const char *name
)
72 struct gettyflags
*fp
;
79 static int firsttime
= 1;
81 dba
[0] = _PATH_GETTYTAB
;
86 * we need to strdup() anything in the strings array
87 * initially in order to simplify things later
89 for (sp
= gettystrs
; sp
->field
; sp
++)
90 if (sp
->value
!= NULL
) {
91 /* handle these ones more carefully */
92 if (sp
>= &gettystrs
[4] && sp
<= &gettystrs
[6])
95 l
= (int)strlen(sp
->value
) + 1;
96 if ((p
= malloc(l
)) != NULL
) {
97 strncpy(p
, sp
->value
, l
);
101 * replace, even if NULL, else we'll
102 * have problems with free()ing static mem
109 switch (cgetent(&buf
, (char **)dba
, (char *)name
)) {
111 msg
= "%s: couldn't resolve 'tc=' in gettytab '%s'";
115 msg
= "%s: unknown gettytab entry '%s'";
118 msg
= "%s: retrieving gettytab entry '%s': %m";
121 msg
= "%s: recursive 'tc=' reference gettytab entry '%s'";
124 msg
= "%s: unexpected cgetent() error for entry '%s'";
129 syslog(LOG_ERR
, msg
, "getty", name
);
133 for (sp
= gettystrs
; sp
->field
; sp
++) {
134 if ((l
= cgetstr(buf
, (char*)sp
->field
, &p
)) >= 0) {
136 /* prefer existing value */
137 if (strcmp(p
, sp
->value
) != 0)
145 } else if (l
== -1) {
151 for (np
= gettynums
; np
->field
; np
++) {
152 if (cgetnum(buf
, (char*)np
->field
, &n
) == -1)
160 for (fp
= gettyflags
; fp
->field
; fp
++) {
161 if (cgetcap(buf
, (char *)fp
->field
, ':') == NULL
)
165 fp
->value
= 1 ^ fp
->invrt
;
170 printf("name=\"%s\", buf=\"%s\"\r\n", name
, buf
);
171 for (sp
= gettystrs
; sp
->field
; sp
++)
172 printf("cgetstr: %s=%s\r\n", sp
->field
, sp
->value
);
173 for (np
= gettynums
; np
->field
; np
++)
174 printf("cgetnum: %s=%d\r\n", np
->field
, np
->value
);
175 for (fp
= gettyflags
; fp
->field
; fp
++)
176 printf("cgetflags: %s='%c' set='%c'\r\n", fp
->field
,
177 fp
->value
+ '0', fp
->set
+ '0');
186 struct gettystrs
*sp
;
187 struct gettynums
*np
;
188 struct gettyflags
*fp
;
190 for (sp
= gettystrs
; sp
->field
; sp
++)
192 sp
->defalt
= strdup(sp
->value
);
193 for (np
= gettynums
; np
->field
; np
++)
195 np
->defalt
= np
->value
;
196 for (fp
= gettyflags
; fp
->field
; fp
++)
198 fp
->defalt
= fp
->value
;
200 fp
->defalt
= fp
->invrt
;
206 struct gettystrs
*sp
;
207 struct gettynums
*np
;
208 struct gettyflags
*fp
;
210 for (sp
= gettystrs
; sp
->field
; sp
++)
212 sp
->value
= !sp
->defalt
? sp
->defalt
213 : strdup(sp
->defalt
);
214 for (np
= gettynums
; np
->field
; np
++)
216 np
->value
= np
->defalt
;
217 for (fp
= gettyflags
; fp
->field
; fp
++)
219 fp
->value
= fp
->defalt
;
224 &ER
, &KL
, &IN
, &QU
, &XN
, &XF
, &ET
, &BK
,
225 &SU
, &DS
, &RP
, &FL
, &WE
, &LN
, 0
230 (char*)&tmode
.c_cc
[VERASE
],
231 (char*)&tmode
.c_cc
[VKILL
],
232 (char*)&tmode
.c_cc
[VINTR
],
233 (char*)&tmode
.c_cc
[VQUIT
],
234 (char*)&tmode
.c_cc
[VSTART
],
235 (char*)&tmode
.c_cc
[VSTOP
],
236 (char*)&tmode
.c_cc
[VEOF
],
237 (char*)&tmode
.c_cc
[VEOL
],
238 (char*)&tmode
.c_cc
[VSUSP
],
239 (char*)&tmode
.c_cc
[VDSUSP
],
240 (char*)&tmode
.c_cc
[VREPRINT
],
241 (char*)&tmode
.c_cc
[VDISCARD
],
242 (char*)&tmode
.c_cc
[VWERASE
],
243 (char*)&tmode
.c_cc
[VLNEXT
],
253 for (i
= 0; charnames
[i
]; i
++) {
258 *charvars
[i
] = _POSIX_VDISABLE
;
262 /* Macros to clear/set/test flags. */
263 #define SET(t, f) (t) |= (f)
264 #define CLR(t, f) (t) &= ~(f)
265 #define ISSET(t, f) ((t) & (f))
270 tcflag_t iflag
, oflag
, cflag
, lflag
;
275 if (C0set
&& I0set
&& L0set
&& O0set
) {
284 if (C1set
&& I1set
&& L1set
&& O1set
) {
293 if (C2set
&& I2set
&& L2set
&& O2set
) {
303 iflag
= omode
.c_iflag
;
304 oflag
= omode
.c_oflag
;
305 cflag
= omode
.c_cflag
;
306 lflag
= omode
.c_lflag
;
309 CLR(cflag
, CSIZE
|PARENB
);
311 CLR(iflag
, ISTRIP
|INPCK
|IGNPAR
);
312 } else if (AP
|| EP
|| OP
) {
314 SET(cflag
, CS7
|PARENB
);
317 SET(iflag
, INPCK
|IGNPAR
);
321 } else if (EP
&& !OP
) {
322 SET(iflag
, INPCK
|IGNPAR
);
326 } else if (AP
|| (EP
&& OP
)) {
327 CLR(iflag
, INPCK
|IGNPAR
);
330 } /* else, leave as is */
354 SET(oflag
, ONLCR
|OPOST
);
361 SET(oflag
, OXTABS
|OPOST
);
369 if (n
== 1) { /* read mode flags */
373 CLR(cflag
, CSIZE
|PARENB
);
421 tmode
.c_iflag
= iflag
;
422 tmode
.c_oflag
= oflag
;
423 tmode
.c_cflag
= cflag
;
424 tmode
.c_lflag
= lflag
;
430 unsigned delay
; /* delay in ms */
435 * below are random guesses, I can't be bothered checking
438 struct delayval crdelay
[] = {
447 struct delayval nldelay
[] = {
448 { 1, NL1
}, /* special, calculated */
455 struct delayval bsdelay
[] = {
460 struct delayval ffdelay
[] = {
466 struct delayval tbdelay
[] = {
469 { 3, XTABS
}, /* this is expand tabs */
479 f
= adelay(CD
, crdelay
);
480 f
|= adelay(ND
, nldelay
);
481 f
|= adelay(FD
, ffdelay
);
482 f
|= adelay(TD
, tbdelay
);
483 f
|= adelay(BD
, bsdelay
);
488 adelay(int ms
, struct delayval
*dp
)
492 while (dp
->delay
&& ms
> dp
->delay
)
498 char editedhost
[MAXHOSTNAMELEN
];
501 edithost(const char *pat
)
503 const char *host
= HN
;
504 char *res
= editedhost
;
526 if (res
== &editedhost
[sizeof editedhost
- 1]) {
533 strncpy(res
, host
, sizeof editedhost
- (res
- editedhost
) - 1);
536 editedhost
[sizeof editedhost
- 1] = '\0';
539 static struct speedtab
{
557 { 19, EXTA
}, /* for people who say 19.2K */
560 { 7200, EXTB
}, /* alternative */
575 for (sp
= speedtab
; sp
->speed
; sp
++)
576 if (sp
->speed
== val
)
579 return (B300
); /* default in impossible cases */
585 static char termbuf
[128] = "TERM=";
591 strlcat(termbuf
, TT
, sizeof(termbuf
));
596 while ((q
= strchr(q
, ','))) {
608 * This speed select mechanism is written for the Develcon DATASWITCH.
609 * The Develcon sends a string of the form "B{speed}\n" at a predefined
610 * baud rate. This string indicates the user's actual speed.
611 * The routine below returns the terminal type mapped from derived speed.
617 { "B110", "std.110" },
618 { "B134", "std.134" },
619 { "B150", "std.150" },
620 { "B300", "std.300" },
621 { "B600", "std.600" },
622 { "B1200", "std.1200" },
623 { "B2400", "std.2400" },
624 { "B4800", "std.4800" },
625 { "B9600", "std.9600" },
626 { "B19200", "std.19200" },
634 const char *type
= "default";
635 struct portselect
*ps
;
639 for (len
= 0; len
< sizeof (baud
) - 1; len
++) {
640 if (read(STDIN_FILENO
, &c
, 1) <= 0)
643 if (c
== '\n' || c
== '\r')
646 len
= 0; /* in case of leading garbage */
650 for (ps
= portspeeds
; ps
->ps_baud
; ps
++)
651 if (strcmp(ps
->ps_baud
, baud
) == 0) {
655 sleep(2); /* wait for connection to complete */
660 * This auto-baud speed select mechanism is written for the Micom 600
661 * portselector. Selection is done by looking at how the character '\r'
662 * is garbled at the different speeds.
668 struct timeval timeout
;
670 const char *type
= "9600-baud";
672 (void)tcflush(0, TCIOFLUSH
);
676 if (select(32, (fd_set
*)&rfds
, (fd_set
*)NULL
,
677 (fd_set
*)NULL
, &timeout
) <= 0)
679 if (read(STDIN_FILENO
, &c
, sizeof(char)) != sizeof(char))
682 timeout
.tv_usec
= 20;
683 (void) select(32, (fd_set
*)NULL
, (fd_set
*)NULL
,
684 (fd_set
*)NULL
, &timeout
);
685 (void)tcflush(0, TCIOFLUSH
);
688 case 0200: /* 300-baud */
692 case 0346: /* 1200-baud */
696 case 015: /* 2400-baud */
701 default: /* 4800-baud */
705 case 0377: /* 9600-baud */