]>
Commit | Line | Data |
---|---|---|
1815bff5 A |
1 | /* |
2 | * Copyright (c) 1983, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
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. 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. | |
20 | * | |
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 | |
31 | * SUCH DAMAGE. | |
32 | */ | |
33 | ||
34 | #ifndef lint | |
34d340d7 A |
35 | #if 0 |
36 | static char sccsid[] = "@(#)from: subr.c 8.1 (Berkeley) 6/4/93"; | |
37 | #endif | |
38 | static const char rcsid[] = | |
39 | "$FreeBSD: src/libexec/getty/subr.c,v 1.19 2004/06/25 10:11:28 phk Exp $"; | |
1815bff5 A |
40 | #endif /* not lint */ |
41 | ||
42 | /* | |
43 | * Melbourne getty. | |
44 | */ | |
34d340d7 A |
45 | #ifdef DEBUG |
46 | #include <stdio.h> | |
47 | #endif | |
1815bff5 | 48 | #include <stdlib.h> |
1815bff5 A |
49 | #include <string.h> |
50 | #include <termios.h> | |
34d340d7 | 51 | #include <unistd.h> |
1815bff5 | 52 | #include <sys/ioctl.h> |
34d340d7 A |
53 | #include <sys/param.h> |
54 | #include <sys/time.h> | |
55 | #include <syslog.h> | |
1815bff5 A |
56 | |
57 | #include "gettytab.h" | |
58 | #include "pathnames.h" | |
59 | #include "extern.h" | |
60 | ||
1815bff5 | 61 | |
1815bff5 A |
62 | |
63 | /* | |
64 | * Get a table entry. | |
65 | */ | |
66 | void | |
34d340d7 | 67 | gettable(const char *name, char *buf) |
1815bff5 | 68 | { |
34d340d7 A |
69 | struct gettystrs *sp; |
70 | struct gettynums *np; | |
71 | struct gettyflags *fp; | |
1815bff5 | 72 | long n; |
34d340d7 A |
73 | int l; |
74 | char *p; | |
75 | char *msg = NULL; | |
76 | const char *dba[2]; | |
77 | ||
78 | static int firsttime = 1; | |
79 | ||
1815bff5 A |
80 | dba[0] = _PATH_GETTYTAB; |
81 | dba[1] = 0; | |
82 | ||
34d340d7 A |
83 | if (firsttime) { |
84 | /* | |
85 | * we need to strdup() anything in the strings array | |
86 | * initially in order to simplify things later | |
87 | */ | |
88 | for (sp = gettystrs; sp->field; sp++) | |
89 | if (sp->value != NULL) { | |
90 | /* handle these ones more carefully */ | |
91 | if (sp >= &gettystrs[4] && sp <= &gettystrs[6]) | |
92 | l = 2; | |
93 | else | |
94 | l = strlen(sp->value) + 1; | |
95 | if ((p = malloc(l)) != NULL) { | |
96 | strncpy(p, sp->value, l); | |
97 | p[l-1] = '\0'; | |
98 | } | |
99 | /* | |
100 | * replace, even if NULL, else we'll | |
101 | * have problems with free()ing static mem | |
102 | */ | |
103 | sp->value = p; | |
104 | } | |
105 | firsttime = 0; | |
106 | } | |
107 | ||
108 | switch (cgetent(&buf, (char **)dba, (char *)name)) { | |
109 | case 1: | |
110 | msg = "%s: couldn't resolve 'tc=' in gettytab '%s'"; | |
111 | case 0: | |
112 | break; | |
113 | case -1: | |
114 | msg = "%s: unknown gettytab entry '%s'"; | |
115 | break; | |
116 | case -2: | |
117 | msg = "%s: retrieving gettytab entry '%s': %m"; | |
118 | break; | |
119 | case -3: | |
120 | msg = "%s: recursive 'tc=' reference gettytab entry '%s'"; | |
121 | break; | |
122 | default: | |
123 | msg = "%s: unexpected cgetent() error for entry '%s'"; | |
124 | break; | |
125 | } | |
126 | ||
127 | if (msg != NULL) { | |
128 | syslog(LOG_ERR, msg, "getty", name); | |
1815bff5 | 129 | return; |
34d340d7 A |
130 | } |
131 | ||
132 | for (sp = gettystrs; sp->field; sp++) { | |
133 | if ((l = cgetstr(buf, (char*)sp->field, &p)) >= 0) { | |
134 | if (sp->value) { | |
135 | /* prefer existing value */ | |
136 | if (strcmp(p, sp->value) != 0) | |
137 | free(sp->value); | |
138 | else { | |
139 | free(p); | |
140 | p = sp->value; | |
141 | } | |
142 | } | |
143 | sp->value = p; | |
144 | } else if (l == -1) { | |
145 | free(sp->value); | |
146 | sp->value = NULL; | |
147 | } | |
148 | } | |
1815bff5 | 149 | |
1815bff5 | 150 | for (np = gettynums; np->field; np++) { |
34d340d7 | 151 | if (cgetnum(buf, (char*)np->field, &n) == -1) |
1815bff5 A |
152 | np->set = 0; |
153 | else { | |
154 | np->set = 1; | |
155 | np->value = n; | |
156 | } | |
157 | } | |
34d340d7 | 158 | |
1815bff5 | 159 | for (fp = gettyflags; fp->field; fp++) { |
34d340d7 | 160 | if (cgetcap(buf, (char *)fp->field, ':') == NULL) |
1815bff5 A |
161 | fp->set = 0; |
162 | else { | |
163 | fp->set = 1; | |
164 | fp->value = 1 ^ fp->invrt; | |
165 | } | |
166 | } | |
34d340d7 | 167 | |
1815bff5 | 168 | #ifdef DEBUG |
34d340d7 | 169 | printf("name=\"%s\", buf=\"%s\"\r\n", name, buf); |
1815bff5 | 170 | for (sp = gettystrs; sp->field; sp++) |
34d340d7 | 171 | printf("cgetstr: %s=%s\r\n", sp->field, sp->value); |
1815bff5 | 172 | for (np = gettynums; np->field; np++) |
34d340d7 | 173 | printf("cgetnum: %s=%d\r\n", np->field, np->value); |
1815bff5 | 174 | for (fp = gettyflags; fp->field; fp++) |
34d340d7 | 175 | printf("cgetflags: %s='%c' set='%c'\r\n", fp->field, |
1815bff5 | 176 | fp->value + '0', fp->set + '0'); |
1815bff5 A |
177 | #endif /* DEBUG */ |
178 | } | |
179 | ||
180 | void | |
34d340d7 | 181 | gendefaults(void) |
1815bff5 | 182 | { |
34d340d7 A |
183 | struct gettystrs *sp; |
184 | struct gettynums *np; | |
185 | struct gettyflags *fp; | |
1815bff5 A |
186 | |
187 | for (sp = gettystrs; sp->field; sp++) | |
188 | if (sp->value) | |
34d340d7 | 189 | sp->defalt = strdup(sp->value); |
1815bff5 A |
190 | for (np = gettynums; np->field; np++) |
191 | if (np->set) | |
192 | np->defalt = np->value; | |
193 | for (fp = gettyflags; fp->field; fp++) | |
194 | if (fp->set) | |
195 | fp->defalt = fp->value; | |
196 | else | |
197 | fp->defalt = fp->invrt; | |
198 | } | |
199 | ||
200 | void | |
34d340d7 | 201 | setdefaults(void) |
1815bff5 | 202 | { |
34d340d7 A |
203 | struct gettystrs *sp; |
204 | struct gettynums *np; | |
205 | struct gettyflags *fp; | |
1815bff5 A |
206 | |
207 | for (sp = gettystrs; sp->field; sp++) | |
208 | if (!sp->value) | |
34d340d7 A |
209 | sp->value = !sp->defalt ? sp->defalt |
210 | : strdup(sp->defalt); | |
1815bff5 A |
211 | for (np = gettynums; np->field; np++) |
212 | if (!np->set) | |
213 | np->value = np->defalt; | |
214 | for (fp = gettyflags; fp->field; fp++) | |
215 | if (!fp->set) | |
216 | fp->value = fp->defalt; | |
217 | } | |
218 | ||
219 | static char ** | |
220 | charnames[] = { | |
221 | &ER, &KL, &IN, &QU, &XN, &XF, &ET, &BK, | |
222 | &SU, &DS, &RP, &FL, &WE, &LN, 0 | |
223 | }; | |
224 | ||
225 | static char * | |
226 | charvars[] = { | |
34d340d7 A |
227 | (char*)&tmode.c_cc[VERASE], |
228 | (char*)&tmode.c_cc[VKILL], | |
229 | (char*)&tmode.c_cc[VINTR], | |
230 | (char*)&tmode.c_cc[VQUIT], | |
231 | (char*)&tmode.c_cc[VSTART], | |
232 | (char*)&tmode.c_cc[VSTOP], | |
233 | (char*)&tmode.c_cc[VEOF], | |
234 | (char*)&tmode.c_cc[VEOL], | |
235 | (char*)&tmode.c_cc[VSUSP], | |
236 | (char*)&tmode.c_cc[VDSUSP], | |
237 | (char*)&tmode.c_cc[VREPRINT], | |
238 | (char*)&tmode.c_cc[VDISCARD], | |
239 | (char*)&tmode.c_cc[VWERASE], | |
240 | (char*)&tmode.c_cc[VLNEXT], | |
241 | 0 | |
1815bff5 A |
242 | }; |
243 | ||
244 | void | |
34d340d7 | 245 | setchars(void) |
1815bff5 | 246 | { |
34d340d7 A |
247 | int i; |
248 | const char *p; | |
1815bff5 A |
249 | |
250 | for (i = 0; charnames[i]; i++) { | |
251 | p = *charnames[i]; | |
252 | if (p && *p) | |
253 | *charvars[i] = *p; | |
254 | else | |
255 | *charvars[i] = _POSIX_VDISABLE; | |
256 | } | |
257 | } | |
258 | ||
34d340d7 A |
259 | /* Macros to clear/set/test flags. */ |
260 | #define SET(t, f) (t) |= (f) | |
261 | #define CLR(t, f) (t) &= ~(f) | |
262 | #define ISSET(t, f) ((t) & (f)) | |
263 | ||
1815bff5 | 264 | void |
34d340d7 | 265 | set_flags(int n) |
1815bff5 | 266 | { |
34d340d7 | 267 | tcflag_t iflag, oflag, cflag, lflag; |
1815bff5 | 268 | |
1815bff5 A |
269 | |
270 | switch (n) { | |
271 | case 0: | |
272 | if (C0set && I0set && L0set && O0set) { | |
273 | tmode.c_cflag = C0; | |
274 | tmode.c_iflag = I0; | |
275 | tmode.c_lflag = L0; | |
276 | tmode.c_oflag = O0; | |
277 | return; | |
278 | } | |
279 | break; | |
280 | case 1: | |
281 | if (C1set && I1set && L1set && O1set) { | |
282 | tmode.c_cflag = C1; | |
283 | tmode.c_iflag = I1; | |
284 | tmode.c_lflag = L1; | |
285 | tmode.c_oflag = O1; | |
286 | return; | |
287 | } | |
288 | break; | |
289 | default: | |
290 | if (C2set && I2set && L2set && O2set) { | |
291 | tmode.c_cflag = C2; | |
292 | tmode.c_iflag = I2; | |
293 | tmode.c_lflag = L2; | |
294 | tmode.c_oflag = O2; | |
295 | return; | |
296 | } | |
297 | break; | |
298 | } | |
299 | ||
1815bff5 A |
300 | iflag = omode.c_iflag; |
301 | oflag = omode.c_oflag; | |
302 | cflag = omode.c_cflag; | |
303 | lflag = omode.c_lflag; | |
304 | ||
305 | if (NP) { | |
34d340d7 A |
306 | CLR(cflag, CSIZE|PARENB); |
307 | SET(cflag, CS8); | |
308 | CLR(iflag, ISTRIP|INPCK|IGNPAR); | |
309 | } else if (AP || EP || OP) { | |
310 | CLR(cflag, CSIZE); | |
311 | SET(cflag, CS7|PARENB); | |
312 | SET(iflag, ISTRIP); | |
313 | if (OP && !EP) { | |
314 | SET(iflag, INPCK|IGNPAR); | |
315 | SET(cflag, PARODD); | |
316 | if (AP) | |
317 | CLR(iflag, INPCK); | |
318 | } else if (EP && !OP) { | |
319 | SET(iflag, INPCK|IGNPAR); | |
320 | CLR(cflag, PARODD); | |
321 | if (AP) | |
322 | CLR(iflag, INPCK); | |
323 | } else if (AP || (EP && OP)) { | |
324 | CLR(iflag, INPCK|IGNPAR); | |
325 | CLR(cflag, PARODD); | |
326 | } | |
1815bff5 A |
327 | } /* else, leave as is */ |
328 | ||
329 | #if 0 | |
330 | if (UC) | |
331 | f |= LCASE; | |
332 | #endif | |
333 | ||
334 | if (HC) | |
34d340d7 A |
335 | SET(cflag, HUPCL); |
336 | else | |
337 | CLR(cflag, HUPCL); | |
338 | ||
339 | if (MB) | |
340 | SET(cflag, MDMBUF); | |
341 | else | |
342 | CLR(cflag, MDMBUF); | |
343 | ||
344 | if (HW) | |
345 | SET(cflag, CRTSCTS); | |
1815bff5 | 346 | else |
34d340d7 | 347 | CLR(cflag, CRTSCTS); |
1815bff5 A |
348 | |
349 | if (NL) { | |
34d340d7 A |
350 | SET(iflag, ICRNL); |
351 | SET(oflag, ONLCR|OPOST); | |
352 | } else { | |
353 | CLR(iflag, ICRNL); | |
354 | CLR(oflag, ONLCR); | |
1815bff5 A |
355 | } |
356 | ||
34d340d7 A |
357 | if (!HT) |
358 | SET(oflag, OXTABS|OPOST); | |
359 | else | |
360 | CLR(oflag, OXTABS); | |
361 | ||
1815bff5 | 362 | #ifdef XXX_DELAY |
34d340d7 | 363 | SET(f, delaybits()); |
1815bff5 A |
364 | #endif |
365 | ||
366 | if (n == 1) { /* read mode flags */ | |
367 | if (RW) { | |
368 | iflag = 0; | |
34d340d7 A |
369 | CLR(oflag, OPOST); |
370 | CLR(cflag, CSIZE|PARENB); | |
371 | SET(cflag, CS8); | |
1815bff5 A |
372 | lflag = 0; |
373 | } else { | |
34d340d7 | 374 | CLR(lflag, ICANON); |
1815bff5 A |
375 | } |
376 | goto out; | |
377 | } | |
378 | ||
1815bff5 A |
379 | if (n == 0) |
380 | goto out; | |
381 | ||
382 | #if 0 | |
383 | if (CB) | |
34d340d7 | 384 | SET(f, CRTBS); |
1815bff5 A |
385 | #endif |
386 | ||
387 | if (CE) | |
34d340d7 A |
388 | SET(lflag, ECHOE); |
389 | else | |
390 | CLR(lflag, ECHOE); | |
1815bff5 A |
391 | |
392 | if (CK) | |
34d340d7 A |
393 | SET(lflag, ECHOKE); |
394 | else | |
395 | CLR(lflag, ECHOKE); | |
1815bff5 A |
396 | |
397 | if (PE) | |
34d340d7 A |
398 | SET(lflag, ECHOPRT); |
399 | else | |
400 | CLR(lflag, ECHOPRT); | |
1815bff5 A |
401 | |
402 | if (EC) | |
34d340d7 A |
403 | SET(lflag, ECHO); |
404 | else | |
405 | CLR(lflag, ECHO); | |
1815bff5 A |
406 | |
407 | if (XC) | |
34d340d7 A |
408 | SET(lflag, ECHOCTL); |
409 | else | |
410 | CLR(lflag, ECHOCTL); | |
1815bff5 A |
411 | |
412 | if (DX) | |
34d340d7 | 413 | SET(lflag, IXANY); |
1815bff5 | 414 | else |
34d340d7 | 415 | CLR(lflag, IXANY); |
1815bff5 A |
416 | |
417 | out: | |
418 | tmode.c_iflag = iflag; | |
419 | tmode.c_oflag = oflag; | |
420 | tmode.c_cflag = cflag; | |
421 | tmode.c_lflag = lflag; | |
422 | } | |
423 | ||
1815bff5 A |
424 | |
425 | #ifdef XXX_DELAY | |
426 | struct delayval { | |
427 | unsigned delay; /* delay in ms */ | |
428 | int bits; | |
429 | }; | |
430 | ||
431 | /* | |
432 | * below are random guesses, I can't be bothered checking | |
433 | */ | |
434 | ||
435 | struct delayval crdelay[] = { | |
436 | { 1, CR1 }, | |
437 | { 2, CR2 }, | |
438 | { 3, CR3 }, | |
439 | { 83, CR1 }, | |
440 | { 166, CR2 }, | |
441 | { 0, CR3 }, | |
442 | }; | |
443 | ||
444 | struct delayval nldelay[] = { | |
445 | { 1, NL1 }, /* special, calculated */ | |
446 | { 2, NL2 }, | |
447 | { 3, NL3 }, | |
448 | { 100, NL2 }, | |
449 | { 0, NL3 }, | |
450 | }; | |
451 | ||
452 | struct delayval bsdelay[] = { | |
453 | { 1, BS1 }, | |
454 | { 0, 0 }, | |
455 | }; | |
456 | ||
457 | struct delayval ffdelay[] = { | |
458 | { 1, FF1 }, | |
459 | { 1750, FF1 }, | |
460 | { 0, FF1 }, | |
461 | }; | |
462 | ||
463 | struct delayval tbdelay[] = { | |
34d340d7 A |
464 | { 1, TAB1 }, |
465 | { 2, TAB2 }, | |
1815bff5 | 466 | { 3, XTABS }, /* this is expand tabs */ |
34d340d7 A |
467 | { 100, TAB1 }, |
468 | { 0, TAB2 }, | |
1815bff5 A |
469 | }; |
470 | ||
471 | int | |
34d340d7 | 472 | delaybits(void) |
1815bff5 | 473 | { |
34d340d7 | 474 | int f; |
1815bff5 A |
475 | |
476 | f = adelay(CD, crdelay); | |
477 | f |= adelay(ND, nldelay); | |
478 | f |= adelay(FD, ffdelay); | |
479 | f |= adelay(TD, tbdelay); | |
480 | f |= adelay(BD, bsdelay); | |
481 | return (f); | |
482 | } | |
483 | ||
484 | int | |
34d340d7 | 485 | adelay(int ms, struct delayval *dp) |
1815bff5 A |
486 | { |
487 | if (ms == 0) | |
488 | return (0); | |
489 | while (dp->delay && ms > dp->delay) | |
490 | dp++; | |
491 | return (dp->bits); | |
492 | } | |
493 | #endif | |
494 | ||
34d340d7 | 495 | char editedhost[MAXHOSTNAMELEN]; |
1815bff5 A |
496 | |
497 | void | |
34d340d7 | 498 | edithost(const char *pat) |
1815bff5 | 499 | { |
34d340d7 A |
500 | const char *host = HN; |
501 | char *res = editedhost; | |
1815bff5 A |
502 | |
503 | if (!pat) | |
504 | pat = ""; | |
505 | while (*pat) { | |
506 | switch (*pat) { | |
507 | ||
508 | case '#': | |
509 | if (*host) | |
510 | host++; | |
511 | break; | |
512 | ||
513 | case '@': | |
514 | if (*host) | |
515 | *res++ = *host++; | |
516 | break; | |
517 | ||
518 | default: | |
519 | *res++ = *pat; | |
520 | break; | |
521 | ||
522 | } | |
523 | if (res == &editedhost[sizeof editedhost - 1]) { | |
524 | *res = '\0'; | |
525 | return; | |
526 | } | |
527 | pat++; | |
528 | } | |
529 | if (*host) | |
530 | strncpy(res, host, sizeof editedhost - (res - editedhost) - 1); | |
531 | else | |
532 | *res = '\0'; | |
533 | editedhost[sizeof editedhost - 1] = '\0'; | |
534 | } | |
535 | ||
34d340d7 A |
536 | static struct speedtab { |
537 | int speed; | |
538 | int uxname; | |
539 | } speedtab[] = { | |
540 | { 50, B50 }, | |
541 | { 75, B75 }, | |
542 | { 110, B110 }, | |
543 | { 134, B134 }, | |
544 | { 150, B150 }, | |
545 | { 200, B200 }, | |
546 | { 300, B300 }, | |
547 | { 600, B600 }, | |
548 | { 1200, B1200 }, | |
549 | { 1800, B1800 }, | |
550 | { 2400, B2400 }, | |
551 | { 4800, B4800 }, | |
552 | { 9600, B9600 }, | |
553 | { 19200, EXTA }, | |
554 | { 19, EXTA }, /* for people who say 19.2K */ | |
555 | { 38400, EXTB }, | |
556 | { 38, EXTB }, | |
557 | { 7200, EXTB }, /* alternative */ | |
558 | { 57600, B57600 }, | |
559 | { 115200, B115200 }, | |
560 | { 230400, B230400 }, | |
561 | { 0 } | |
562 | }; | |
563 | ||
564 | int | |
565 | speed(int val) | |
566 | { | |
567 | struct speedtab *sp; | |
568 | ||
569 | if (val <= B230400) | |
570 | return (val); | |
571 | ||
572 | for (sp = speedtab; sp->speed; sp++) | |
573 | if (sp->speed == val) | |
574 | return (sp->uxname); | |
575 | ||
576 | return (B300); /* default in impossible cases */ | |
577 | } | |
578 | ||
1815bff5 | 579 | void |
34d340d7 | 580 | makeenv(char *env[]) |
1815bff5 A |
581 | { |
582 | static char termbuf[128] = "TERM="; | |
34d340d7 A |
583 | char *p, *q; |
584 | char **ep; | |
1815bff5 A |
585 | |
586 | ep = env; | |
587 | if (TT && *TT) { | |
34d340d7 | 588 | strlcat(termbuf, TT, sizeof(termbuf)); |
1815bff5 A |
589 | *ep++ = termbuf; |
590 | } | |
34d340d7 | 591 | if ((p = EV)) { |
1815bff5 | 592 | q = p; |
34d340d7 | 593 | while ((q = strchr(q, ','))) { |
1815bff5 A |
594 | *q++ = '\0'; |
595 | *ep++ = p; | |
596 | p = q; | |
597 | } | |
598 | if (*p) | |
599 | *ep++ = p; | |
600 | } | |
601 | *ep = (char *)0; | |
602 | } | |
603 | ||
604 | /* | |
605 | * This speed select mechanism is written for the Develcon DATASWITCH. | |
606 | * The Develcon sends a string of the form "B{speed}\n" at a predefined | |
607 | * baud rate. This string indicates the user's actual speed. | |
608 | * The routine below returns the terminal type mapped from derived speed. | |
609 | */ | |
610 | struct portselect { | |
34d340d7 A |
611 | const char *ps_baud; |
612 | const char *ps_type; | |
1815bff5 A |
613 | } portspeeds[] = { |
614 | { "B110", "std.110" }, | |
615 | { "B134", "std.134" }, | |
616 | { "B150", "std.150" }, | |
617 | { "B300", "std.300" }, | |
618 | { "B600", "std.600" }, | |
619 | { "B1200", "std.1200" }, | |
620 | { "B2400", "std.2400" }, | |
621 | { "B4800", "std.4800" }, | |
622 | { "B9600", "std.9600" }, | |
623 | { "B19200", "std.19200" }, | |
624 | { 0 } | |
625 | }; | |
626 | ||
34d340d7 A |
627 | const char * |
628 | portselector(void) | |
1815bff5 | 629 | { |
34d340d7 A |
630 | char c, baud[20]; |
631 | const char *type = "default"; | |
632 | struct portselect *ps; | |
1815bff5 A |
633 | int len; |
634 | ||
635 | alarm(5*60); | |
636 | for (len = 0; len < sizeof (baud) - 1; len++) { | |
637 | if (read(STDIN_FILENO, &c, 1) <= 0) | |
638 | break; | |
639 | c &= 0177; | |
640 | if (c == '\n' || c == '\r') | |
641 | break; | |
642 | if (c == 'B') | |
643 | len = 0; /* in case of leading garbage */ | |
644 | baud[len] = c; | |
645 | } | |
646 | baud[len] = '\0'; | |
647 | for (ps = portspeeds; ps->ps_baud; ps++) | |
648 | if (strcmp(ps->ps_baud, baud) == 0) { | |
649 | type = ps->ps_type; | |
650 | break; | |
651 | } | |
652 | sleep(2); /* wait for connection to complete */ | |
653 | return (type); | |
654 | } | |
655 | ||
656 | /* | |
657 | * This auto-baud speed select mechanism is written for the Micom 600 | |
658 | * portselector. Selection is done by looking at how the character '\r' | |
659 | * is garbled at the different speeds. | |
660 | */ | |
34d340d7 A |
661 | const char * |
662 | autobaud(void) | |
1815bff5 A |
663 | { |
664 | int rfds; | |
665 | struct timeval timeout; | |
34d340d7 A |
666 | char c; |
667 | const char *type = "9600-baud"; | |
1815bff5 | 668 | |
34d340d7 | 669 | (void)tcflush(0, TCIOFLUSH); |
1815bff5 A |
670 | rfds = 1 << 0; |
671 | timeout.tv_sec = 5; | |
672 | timeout.tv_usec = 0; | |
673 | if (select(32, (fd_set *)&rfds, (fd_set *)NULL, | |
674 | (fd_set *)NULL, &timeout) <= 0) | |
675 | return (type); | |
676 | if (read(STDIN_FILENO, &c, sizeof(char)) != sizeof(char)) | |
677 | return (type); | |
678 | timeout.tv_sec = 0; | |
679 | timeout.tv_usec = 20; | |
680 | (void) select(32, (fd_set *)NULL, (fd_set *)NULL, | |
681 | (fd_set *)NULL, &timeout); | |
34d340d7 | 682 | (void)tcflush(0, TCIOFLUSH); |
1815bff5 A |
683 | switch (c & 0377) { |
684 | ||
685 | case 0200: /* 300-baud */ | |
686 | type = "300-baud"; | |
687 | break; | |
688 | ||
689 | case 0346: /* 1200-baud */ | |
690 | type = "1200-baud"; | |
691 | break; | |
692 | ||
693 | case 015: /* 2400-baud */ | |
694 | case 0215: | |
695 | type = "2400-baud"; | |
696 | break; | |
697 | ||
698 | default: /* 4800-baud */ | |
699 | type = "4800-baud"; | |
700 | break; | |
701 | ||
702 | case 0377: /* 9600-baud */ | |
703 | type = "9600-baud"; | |
704 | break; | |
705 | } | |
706 | return (type); | |
707 | } |