main.c revision 1.32 1 1.32 mjl /* $NetBSD: main.c,v 1.32 2000/09/19 16:17:48 mjl Exp $ */
2 1.18 thorpej
3 1.1 cgd /*-
4 1.8 pk * Copyright (c) 1980, 1993
5 1.8 pk * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.22 mikel #include <sys/cdefs.h>
37 1.22 mikel
38 1.1 cgd #ifndef lint
39 1.22 mikel __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
40 1.22 mikel The Regents of the University of California. All rights reserved.\n");
41 1.1 cgd #endif /* not lint */
42 1.1 cgd
43 1.1 cgd #ifndef lint
44 1.18 thorpej #if 0
45 1.18 thorpej static char sccsid[] = "from: @(#)main.c 8.1 (Berkeley) 6/20/93";
46 1.18 thorpej #else
47 1.32 mjl __RCSID("$NetBSD: main.c,v 1.32 2000/09/19 16:17:48 mjl Exp $");
48 1.18 thorpej #endif
49 1.1 cgd #endif /* not lint */
50 1.1 cgd
51 1.1 cgd #include <sys/param.h>
52 1.1 cgd #include <sys/stat.h>
53 1.14 jtc #include <termios.h>
54 1.7 pk #include <sys/ioctl.h>
55 1.8 pk #include <sys/resource.h>
56 1.3 mycroft #include <sys/utsname.h>
57 1.21 mikel
58 1.15 cgd #include <errno.h>
59 1.1 cgd #include <fcntl.h>
60 1.1 cgd #include <time.h>
61 1.1 cgd #include <ctype.h>
62 1.8 pk #include <fcntl.h>
63 1.23 cjs #include <pwd.h>
64 1.1 cgd #include <setjmp.h>
65 1.8 pk #include <signal.h>
66 1.8 pk #include <stdlib.h>
67 1.8 pk #include <string.h>
68 1.1 cgd #include <syslog.h>
69 1.8 pk #include <time.h>
70 1.1 cgd #include <unistd.h>
71 1.17 jtc #include <util.h>
72 1.30 ad #include <limits.h>
73 1.8 pk
74 1.1 cgd #include "gettytab.h"
75 1.1 cgd #include "pathnames.h"
76 1.8 pk #include "extern.h"
77 1.8 pk
78 1.23 cjs extern char *__progname;
79 1.23 cjs
80 1.8 pk /*
81 1.8 pk * Set the amount of running time that getty should accumulate
82 1.8 pk * before deciding that something is wrong and exit.
83 1.8 pk */
84 1.8 pk #define GETTY_TIMEOUT 60 /* seconds */
85 1.1 cgd
86 1.27 tsarna /* defines for auto detection of incoming PPP calls (->PAP/CHAP) */
87 1.27 tsarna
88 1.27 tsarna #define PPP_FRAME 0x7e /* PPP Framing character */
89 1.27 tsarna #define PPP_STATION 0xff /* "All Station" character */
90 1.27 tsarna #define PPP_ESCAPE 0x7d /* Escape Character */
91 1.27 tsarna #define PPP_CONTROL 0x03 /* PPP Control Field */
92 1.27 tsarna #define PPP_CONTROL_ESCAPED 0x23 /* PPP Control Field, escaped */
93 1.27 tsarna #define PPP_LCP_HI 0xc0 /* LCP protocol - high byte */
94 1.27 tsarna #define PPP_LCP_LOW 0x21 /* LCP protocol - low byte */
95 1.27 tsarna
96 1.8 pk struct termios tmode, omode;
97 1.1 cgd
98 1.1 cgd int crmod, digit, lower, upper;
99 1.1 cgd
100 1.26 mrg char hostname[MAXHOSTNAMELEN + 1];
101 1.3 mycroft struct utsname kerninfo;
102 1.29 drochner char name[LOGIN_NAME_MAX];
103 1.1 cgd char dev[] = _PATH_DEV;
104 1.1 cgd char ttyn[32];
105 1.23 cjs char lockfile[512];
106 1.23 cjs uid_t ttyowner;
107 1.1 cgd
108 1.1 cgd #define OBUFSIZ 128
109 1.1 cgd #define TABBUFSIZ 512
110 1.1 cgd
111 1.1 cgd char defent[TABBUFSIZ];
112 1.1 cgd char tabent[TABBUFSIZ];
113 1.1 cgd
114 1.1 cgd char *env[128];
115 1.1 cgd
116 1.1 cgd char partab[] = {
117 1.1 cgd 0001,0201,0201,0001,0201,0001,0001,0201,
118 1.1 cgd 0202,0004,0003,0205,0005,0206,0201,0001,
119 1.1 cgd 0201,0001,0001,0201,0001,0201,0201,0001,
120 1.1 cgd 0001,0201,0201,0001,0201,0001,0001,0201,
121 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
122 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0200,
123 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0200,
124 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
125 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
126 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0200,
127 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0200,
128 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
129 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0200,
130 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
131 1.1 cgd 0200,0000,0000,0200,0000,0200,0200,0000,
132 1.1 cgd 0000,0200,0200,0000,0200,0000,0000,0201
133 1.1 cgd };
134 1.1 cgd
135 1.7 pk #define ERASE tmode.c_cc[VERASE]
136 1.7 pk #define KILL tmode.c_cc[VKILL]
137 1.7 pk #define EOT tmode.c_cc[VEOF]
138 1.1 cgd
139 1.22 mikel static void dingdong __P((int));
140 1.22 mikel static void interrupt __P((int));
141 1.22 mikel void timeoverrun __P((int));
142 1.22 mikel
143 1.1 cgd jmp_buf timeout;
144 1.1 cgd
145 1.1 cgd static void
146 1.22 mikel dingdong(signo)
147 1.22 mikel int signo;
148 1.1 cgd {
149 1.1 cgd
150 1.1 cgd alarm(0);
151 1.1 cgd signal(SIGALRM, SIG_DFL);
152 1.1 cgd longjmp(timeout, 1);
153 1.1 cgd }
154 1.1 cgd
155 1.1 cgd jmp_buf intrupt;
156 1.1 cgd
157 1.1 cgd static void
158 1.22 mikel interrupt(signo)
159 1.22 mikel int signo;
160 1.1 cgd {
161 1.1 cgd
162 1.1 cgd signal(SIGINT, interrupt);
163 1.1 cgd longjmp(intrupt, 1);
164 1.1 cgd }
165 1.1 cgd
166 1.8 pk /*
167 1.8 pk * Action to take when getty is running too long.
168 1.8 pk */
169 1.8 pk void
170 1.8 pk timeoverrun(signo)
171 1.8 pk int signo;
172 1.8 pk {
173 1.8 pk
174 1.27 tsarna syslog(LOG_ERR, "getty exiting due to excessive running time");
175 1.8 pk exit(1);
176 1.8 pk }
177 1.8 pk
178 1.22 mikel int main __P((int, char **));
179 1.8 pk static int getname __P((void));
180 1.8 pk static void oflush __P((void));
181 1.8 pk static void prompt __P((void));
182 1.8 pk static void putchr __P((int));
183 1.24 thorpej static void putf __P((const char *));
184 1.24 thorpej static void putpad __P((const char *));
185 1.24 thorpej static void xputs __P((const char *));
186 1.8 pk
187 1.8 pk int
188 1.1 cgd main(argc, argv)
189 1.1 cgd int argc;
190 1.8 pk char *argv[];
191 1.1 cgd {
192 1.8 pk extern char **environ;
193 1.1 cgd char *tname;
194 1.30 ad int repcnt = 0, failopenlogged = 0, uugetty = 0, first_time = 1;
195 1.8 pk struct rlimit limit;
196 1.23 cjs struct passwd *pw;
197 1.27 tsarna int rval;
198 1.1 cgd
199 1.22 mikel #ifdef __GNUC__
200 1.22 mikel (void)&tname; /* XXX gcc -Wall */
201 1.22 mikel #endif
202 1.22 mikel
203 1.1 cgd signal(SIGINT, SIG_IGN);
204 1.1 cgd /*
205 1.1 cgd signal(SIGQUIT, SIG_DFL);
206 1.1 cgd */
207 1.26 mrg openlog("getty", LOG_ODELAY|LOG_PID, LOG_AUTH);
208 1.1 cgd gethostname(hostname, sizeof(hostname));
209 1.26 mrg hostname[sizeof(hostname) - 1] = '\0';
210 1.1 cgd if (hostname[0] == '\0')
211 1.1 cgd strcpy(hostname, "Amnesiac");
212 1.3 mycroft uname(&kerninfo);
213 1.8 pk
214 1.23 cjs if (__progname[0] == 'u' && __progname[1] == 'u')
215 1.23 cjs uugetty = 1;
216 1.23 cjs
217 1.23 cjs /*
218 1.23 cjs * Find id of uucp login (if present) so we can chown tty properly.
219 1.23 cjs */
220 1.23 cjs if (uugetty && (pw = getpwnam("uucp")))
221 1.23 cjs ttyowner = pw->pw_uid;
222 1.23 cjs else
223 1.23 cjs ttyowner = 0;
224 1.23 cjs
225 1.8 pk /*
226 1.8 pk * Limit running time to deal with broken or dead lines.
227 1.8 pk */
228 1.8 pk (void)signal(SIGXCPU, timeoverrun);
229 1.8 pk limit.rlim_max = RLIM_INFINITY;
230 1.8 pk limit.rlim_cur = GETTY_TIMEOUT;
231 1.8 pk (void)setrlimit(RLIMIT_CPU, &limit);
232 1.8 pk
233 1.1 cgd /*
234 1.1 cgd * The following is a work around for vhangup interactions
235 1.1 cgd * which cause great problems getting window systems started.
236 1.1 cgd * If the tty line is "-", we do the old style getty presuming
237 1.1 cgd * that the file descriptors are already set up for us.
238 1.1 cgd * J. Gettys - MIT Project Athena.
239 1.1 cgd */
240 1.20 tls if (argc <= 2 || strcmp(argv[2], "-") == 0) {
241 1.32 mjl strlcpy(ttyn, ttyname(0), sizeof(ttyn));
242 1.20 tls }
243 1.1 cgd else {
244 1.1 cgd int i;
245 1.1 cgd
246 1.32 mjl strlcpy(ttyn, dev, sizeof(ttyn));
247 1.32 mjl strlcat(ttyn, argv[2], sizeof(ttyn));
248 1.23 cjs
249 1.23 cjs if (uugetty) {
250 1.23 cjs chown(ttyn, ttyowner, 0);
251 1.23 cjs strcpy(lockfile, _PATH_LOCK);
252 1.32 mjl strlcat(lockfile, argv[2], sizeof(lockfile));
253 1.23 cjs /* wait for lockfiles to go away before we try to open */
254 1.23 cjs if ( pidlock(lockfile, 0, 0, 0) != 0 ) {
255 1.23 cjs syslog(LOG_ERR, "%s: can't create lockfile", ttyn);
256 1.23 cjs exit(1);
257 1.23 cjs }
258 1.23 cjs unlink(lockfile);
259 1.23 cjs }
260 1.1 cgd if (strcmp(argv[0], "+") != 0) {
261 1.23 cjs chown(ttyn, ttyowner, 0);
262 1.1 cgd chmod(ttyn, 0600);
263 1.1 cgd revoke(ttyn);
264 1.19 gwr if (ttyaction(ttyn, "getty", "root"))
265 1.19 gwr syslog(LOG_ERR,"%s: ttyaction failed", ttyn);
266 1.1 cgd /*
267 1.1 cgd * Delay the open so DTR stays down long enough to be detected.
268 1.1 cgd */
269 1.1 cgd sleep(2);
270 1.1 cgd while ((i = open(ttyn, O_RDWR)) == -1) {
271 1.15 cgd if ((repcnt % 10 == 0) &&
272 1.15 cgd (errno != ENXIO || !failopenlogged)) {
273 1.1 cgd syslog(LOG_ERR, "%s: %m", ttyn);
274 1.1 cgd closelog();
275 1.15 cgd failopenlogged = 1;
276 1.1 cgd }
277 1.1 cgd repcnt++;
278 1.1 cgd sleep(60);
279 1.1 cgd }
280 1.23 cjs if (uugetty && pidlock(lockfile, 0, 0, 0) != 0) {
281 1.23 cjs syslog(LOG_ERR, "%s: can't create lockfile", ttyn);
282 1.23 cjs exit(1);
283 1.23 cjs }
284 1.23 cjs (void) chown(lockfile, ttyowner, 0);
285 1.1 cgd login_tty(i);
286 1.1 cgd }
287 1.1 cgd }
288 1.1 cgd
289 1.7 pk /* Start with default tty settings */
290 1.7 pk if (tcgetattr(0, &tmode) < 0) {
291 1.7 pk syslog(LOG_ERR, "%s: %m", ttyn);
292 1.7 pk exit(1);
293 1.7 pk }
294 1.8 pk omode = tmode;
295 1.7 pk
296 1.5 cgd gettable("default", defent);
297 1.1 cgd gendefaults();
298 1.1 cgd tname = "default";
299 1.1 cgd if (argc > 1)
300 1.1 cgd tname = argv[1];
301 1.1 cgd for (;;) {
302 1.8 pk int off;
303 1.1 cgd
304 1.5 cgd gettable(tname, tabent);
305 1.1 cgd if (OPset || EPset || APset)
306 1.1 cgd APset++, OPset++, EPset++;
307 1.1 cgd setdefaults();
308 1.8 pk off = 0;
309 1.12 pk (void)tcflush(0, TCIOFLUSH); /* clear out the crap */
310 1.1 cgd ioctl(0, FIONBIO, &off); /* turn off non-blocking mode */
311 1.1 cgd ioctl(0, FIOASYNC, &off); /* ditto for async mode */
312 1.7 pk
313 1.1 cgd if (IS)
314 1.9 mycroft cfsetispeed(&tmode, IS);
315 1.1 cgd else if (SP)
316 1.9 mycroft cfsetispeed(&tmode, SP);
317 1.1 cgd if (OS)
318 1.9 mycroft cfsetospeed(&tmode, OS);
319 1.1 cgd else if (SP)
320 1.9 mycroft cfsetospeed(&tmode, SP);
321 1.7 pk setflags(0);
322 1.1 cgd setchars();
323 1.7 pk if (tcsetattr(0, TCSANOW, &tmode) < 0) {
324 1.7 pk syslog(LOG_ERR, "%s: %m", ttyn);
325 1.7 pk exit(1);
326 1.7 pk }
327 1.1 cgd if (AB) {
328 1.1 cgd tname = autobaud();
329 1.1 cgd continue;
330 1.1 cgd }
331 1.1 cgd if (PS) {
332 1.1 cgd tname = portselector();
333 1.1 cgd continue;
334 1.1 cgd }
335 1.1 cgd if (CL && *CL)
336 1.1 cgd putpad(CL);
337 1.1 cgd edithost(HE);
338 1.30 ad
339 1.30 ad /*
340 1.30 ad * If this is the first time through this, and an
341 1.30 ad * issue file has been given, then send it.
342 1.30 ad */
343 1.31 ad if (first_time != 0 && IF != NULL) {
344 1.31 ad char buf[_POSIX2_LINE_MAX];
345 1.31 ad FILE *fd;
346 1.31 ad
347 1.31 ad if ((fd = fopen(IF, "r")) != NULL) {
348 1.31 ad while (fgets(buf, sizeof(buf) - 1, fd) != NULL)
349 1.31 ad putf(buf);
350 1.30 ad fclose(fd);
351 1.31 ad }
352 1.31 ad }
353 1.31 ad first_time = 0;
354 1.30 ad
355 1.1 cgd if (IM && *IM)
356 1.1 cgd putf(IM);
357 1.28 kml oflush();
358 1.1 cgd if (setjmp(timeout)) {
359 1.7 pk tmode.c_ispeed = tmode.c_ospeed = 0;
360 1.7 pk (void)tcsetattr(0, TCSANOW, &tmode);
361 1.1 cgd exit(1);
362 1.1 cgd }
363 1.1 cgd if (TO) {
364 1.1 cgd signal(SIGALRM, dingdong);
365 1.1 cgd alarm(TO);
366 1.1 cgd }
367 1.30 ad if (AL) {
368 1.30 ad const char *p = AL;
369 1.30 ad char *q = name;
370 1.30 ad
371 1.30 ad while (*p && q < &name[sizeof name - 1]) {
372 1.30 ad if (isupper(*p))
373 1.30 ad upper = 1;
374 1.30 ad else if (islower(*p))
375 1.30 ad lower = 1;
376 1.30 ad else if (isdigit(*p))
377 1.30 ad digit++;
378 1.30 ad *q++ = *p++;
379 1.30 ad }
380 1.30 ad } else if ((rval = getname()) == 2) {
381 1.27 tsarna execle(PP, "ppplogin", ttyn, (char *) 0, env);
382 1.27 tsarna syslog(LOG_ERR, "%s: %m", PP);
383 1.27 tsarna exit(1);
384 1.30 ad }
385 1.30 ad
386 1.30 ad if (rval || AL) {
387 1.25 mrg int i;
388 1.1 cgd
389 1.1 cgd oflush();
390 1.1 cgd alarm(0);
391 1.1 cgd signal(SIGALRM, SIG_DFL);
392 1.1 cgd if (name[0] == '-') {
393 1.24 thorpej xputs("user names may not start with '-'.");
394 1.1 cgd continue;
395 1.1 cgd }
396 1.1 cgd if (!(upper || lower || digit))
397 1.1 cgd continue;
398 1.7 pk setflags(2);
399 1.7 pk if (crmod) {
400 1.7 pk tmode.c_iflag |= ICRNL;
401 1.7 pk tmode.c_oflag |= ONLCR;
402 1.7 pk }
403 1.7 pk #if XXX
404 1.1 cgd if (upper || UC)
405 1.1 cgd tmode.sg_flags |= LCASE;
406 1.1 cgd if (lower || LC)
407 1.1 cgd tmode.sg_flags &= ~LCASE;
408 1.7 pk #endif
409 1.7 pk if (tcsetattr(0, TCSANOW, &tmode) < 0) {
410 1.7 pk syslog(LOG_ERR, "%s: %m", ttyn);
411 1.7 pk exit(1);
412 1.7 pk }
413 1.1 cgd signal(SIGINT, SIG_DFL);
414 1.1 cgd for (i = 0; environ[i] != (char *)0; i++)
415 1.1 cgd env[i] = environ[i];
416 1.1 cgd makeenv(&env[i]);
417 1.8 pk
418 1.8 pk limit.rlim_max = RLIM_INFINITY;
419 1.8 pk limit.rlim_cur = RLIM_INFINITY;
420 1.8 pk (void)setrlimit(RLIMIT_CPU, &limit);
421 1.30 ad execle(LO, "login", AL ? "-fp" : "-p", "--", name,
422 1.30 ad (char *)0, env);
423 1.1 cgd syslog(LOG_ERR, "%s: %m", LO);
424 1.1 cgd exit(1);
425 1.1 cgd }
426 1.1 cgd alarm(0);
427 1.1 cgd signal(SIGALRM, SIG_DFL);
428 1.1 cgd signal(SIGINT, SIG_IGN);
429 1.1 cgd if (NX && *NX)
430 1.1 cgd tname = NX;
431 1.23 cjs unlink(lockfile);
432 1.1 cgd }
433 1.1 cgd }
434 1.1 cgd
435 1.8 pk static int
436 1.1 cgd getname()
437 1.1 cgd {
438 1.25 mrg int c;
439 1.25 mrg char *np;
440 1.27 tsarna unsigned char cs;
441 1.27 tsarna int ppp_state, ppp_connection;
442 1.27 tsarna
443 1.1 cgd /*
444 1.1 cgd * Interrupt may happen if we use CBREAK mode
445 1.1 cgd */
446 1.1 cgd if (setjmp(intrupt)) {
447 1.1 cgd signal(SIGINT, SIG_IGN);
448 1.1 cgd return (0);
449 1.1 cgd }
450 1.1 cgd signal(SIGINT, interrupt);
451 1.7 pk setflags(1);
452 1.1 cgd prompt();
453 1.1 cgd if (PF > 0) {
454 1.1 cgd oflush();
455 1.1 cgd sleep(PF);
456 1.1 cgd PF = 0;
457 1.1 cgd }
458 1.7 pk if (tcsetattr(0, TCSANOW, &tmode) < 0) {
459 1.7 pk syslog(LOG_ERR, "%s: %m", ttyn);
460 1.7 pk exit(1);
461 1.7 pk }
462 1.1 cgd crmod = digit = lower = upper = 0;
463 1.27 tsarna ppp_state = ppp_connection = 0;
464 1.1 cgd np = name;
465 1.1 cgd for (;;) {
466 1.1 cgd oflush();
467 1.1 cgd if (read(STDIN_FILENO, &cs, 1) <= 0)
468 1.1 cgd exit(0);
469 1.1 cgd if ((c = cs&0177) == 0)
470 1.1 cgd return (0);
471 1.27 tsarna
472 1.27 tsarna /*
473 1.27 tsarna * PPP detection state machine..
474 1.27 tsarna * Look for sequences:
475 1.27 tsarna * PPP_FRAME, PPP_STATION, PPP_ESCAPE, PPP_CONTROL_ESCAPED or
476 1.27 tsarna * PPP_FRAME, PPP_STATION, PPP_CONTROL (deviant from RFC)
477 1.27 tsarna * See RFC1662.
478 1.27 tsarna * Derived from code from Michael Hancock <michaelh (at) cet.co.jp>
479 1.27 tsarna * and Erik 'PPP' Olson <eriko (at) wrq.com>
480 1.27 tsarna */
481 1.27 tsarna if (PP && cs == PPP_FRAME) {
482 1.27 tsarna ppp_state = 1;
483 1.27 tsarna } else if (ppp_state == 1 && cs == PPP_STATION) {
484 1.27 tsarna ppp_state = 2;
485 1.27 tsarna } else if (ppp_state == 2 && cs == PPP_ESCAPE) {
486 1.27 tsarna ppp_state = 3;
487 1.27 tsarna } else if ((ppp_state == 2 && cs == PPP_CONTROL) ||
488 1.27 tsarna (ppp_state == 3 && cs == PPP_CONTROL_ESCAPED)) {
489 1.27 tsarna ppp_state = 4;
490 1.27 tsarna } else if (ppp_state == 4 && cs == PPP_LCP_HI) {
491 1.27 tsarna ppp_state = 5;
492 1.27 tsarna } else if (ppp_state == 5 && cs == PPP_LCP_LOW) {
493 1.27 tsarna ppp_connection = 1;
494 1.27 tsarna break;
495 1.27 tsarna } else {
496 1.27 tsarna ppp_state = 0;
497 1.27 tsarna }
498 1.27 tsarna
499 1.1 cgd if (c == EOT)
500 1.1 cgd exit(1);
501 1.29 drochner if (c == '\r' || c == '\n' ||
502 1.29 drochner np >= &name[LOGIN_NAME_MAX - 1]) {
503 1.29 drochner *np = '\0';
504 1.1 cgd putf("\r\n");
505 1.1 cgd break;
506 1.1 cgd }
507 1.1 cgd if (islower(c))
508 1.1 cgd lower = 1;
509 1.1 cgd else if (isupper(c))
510 1.1 cgd upper = 1;
511 1.1 cgd else if (c == ERASE || c == '#' || c == '\b') {
512 1.1 cgd if (np > name) {
513 1.1 cgd np--;
514 1.10 mycroft if (cfgetospeed(&tmode) >= 1200)
515 1.24 thorpej xputs("\b \b");
516 1.1 cgd else
517 1.1 cgd putchr(cs);
518 1.1 cgd }
519 1.1 cgd continue;
520 1.1 cgd } else if (c == KILL || c == '@') {
521 1.1 cgd putchr(cs);
522 1.1 cgd putchr('\r');
523 1.10 mycroft if (cfgetospeed(&tmode) < 1200)
524 1.1 cgd putchr('\n');
525 1.1 cgd /* this is the way they do it down under ... */
526 1.1 cgd else if (np > name)
527 1.24 thorpej xputs(
528 1.24 thorpej " \r");
529 1.1 cgd prompt();
530 1.1 cgd np = name;
531 1.1 cgd continue;
532 1.1 cgd } else if (isdigit(c))
533 1.1 cgd digit++;
534 1.1 cgd if (IG && (c <= ' ' || c > 0176))
535 1.1 cgd continue;
536 1.1 cgd *np++ = c;
537 1.1 cgd putchr(cs);
538 1.1 cgd }
539 1.1 cgd signal(SIGINT, SIG_IGN);
540 1.1 cgd *np = 0;
541 1.1 cgd if (c == '\r')
542 1.1 cgd crmod = 1;
543 1.22 mikel if ((upper && !lower && !LC) || UC)
544 1.1 cgd for (np = name; *np; np++)
545 1.1 cgd if (isupper(*np))
546 1.1 cgd *np = tolower(*np);
547 1.27 tsarna return (1 + ppp_connection);
548 1.1 cgd }
549 1.1 cgd
550 1.8 pk static void
551 1.1 cgd putpad(s)
552 1.25 mrg const char *s;
553 1.1 cgd {
554 1.25 mrg int pad = 0;
555 1.10 mycroft speed_t ospeed = cfgetospeed(&tmode);
556 1.1 cgd
557 1.1 cgd if (isdigit(*s)) {
558 1.1 cgd while (isdigit(*s)) {
559 1.1 cgd pad *= 10;
560 1.1 cgd pad += *s++ - '0';
561 1.1 cgd }
562 1.1 cgd pad *= 10;
563 1.1 cgd if (*s == '.' && isdigit(s[1])) {
564 1.1 cgd pad += s[1] - '0';
565 1.1 cgd s += 2;
566 1.1 cgd }
567 1.1 cgd }
568 1.1 cgd
569 1.24 thorpej xputs(s);
570 1.1 cgd /*
571 1.1 cgd * If no delay needed, or output speed is
572 1.1 cgd * not comprehensible, then don't try to delay.
573 1.1 cgd */
574 1.10 mycroft if (pad == 0 || ospeed <= 0)
575 1.1 cgd return;
576 1.1 cgd
577 1.1 cgd /*
578 1.1 cgd * Round up by a half a character frame, and then do the delay.
579 1.1 cgd * Too bad there are no user program accessible programmed delays.
580 1.1 cgd * Transmitting pad characters slows many terminals down and also
581 1.1 cgd * loads the system.
582 1.1 cgd */
583 1.10 mycroft pad = (pad * ospeed + 50000) / 100000;
584 1.10 mycroft while (pad--)
585 1.1 cgd putchr(*PC);
586 1.1 cgd }
587 1.1 cgd
588 1.8 pk static void
589 1.24 thorpej xputs(s)
590 1.25 mrg const char *s;
591 1.1 cgd {
592 1.1 cgd while (*s)
593 1.1 cgd putchr(*s++);
594 1.1 cgd }
595 1.1 cgd
596 1.1 cgd char outbuf[OBUFSIZ];
597 1.1 cgd int obufcnt = 0;
598 1.1 cgd
599 1.8 pk static void
600 1.1 cgd putchr(cc)
601 1.8 pk int cc;
602 1.1 cgd {
603 1.1 cgd char c;
604 1.1 cgd
605 1.1 cgd c = cc;
606 1.1 cgd if (!NP) {
607 1.1 cgd c |= partab[c&0177] & 0200;
608 1.1 cgd if (OP)
609 1.1 cgd c ^= 0200;
610 1.1 cgd }
611 1.1 cgd if (!UB) {
612 1.1 cgd outbuf[obufcnt++] = c;
613 1.1 cgd if (obufcnt >= OBUFSIZ)
614 1.1 cgd oflush();
615 1.1 cgd } else
616 1.1 cgd write(STDOUT_FILENO, &c, 1);
617 1.1 cgd }
618 1.1 cgd
619 1.8 pk static void
620 1.1 cgd oflush()
621 1.1 cgd {
622 1.1 cgd if (obufcnt)
623 1.1 cgd write(STDOUT_FILENO, outbuf, obufcnt);
624 1.1 cgd obufcnt = 0;
625 1.1 cgd }
626 1.1 cgd
627 1.8 pk static void
628 1.1 cgd prompt()
629 1.1 cgd {
630 1.1 cgd
631 1.1 cgd putf(LM);
632 1.1 cgd if (CO)
633 1.1 cgd putchr('\n');
634 1.1 cgd }
635 1.1 cgd
636 1.8 pk static void
637 1.1 cgd putf(cp)
638 1.25 mrg const char *cp;
639 1.1 cgd {
640 1.1 cgd extern char editedhost[];
641 1.1 cgd time_t t;
642 1.1 cgd char *slash, db[100];
643 1.1 cgd
644 1.1 cgd while (*cp) {
645 1.1 cgd if (*cp != '%') {
646 1.1 cgd putchr(*cp++);
647 1.1 cgd continue;
648 1.1 cgd }
649 1.1 cgd switch (*++cp) {
650 1.1 cgd
651 1.1 cgd case 't':
652 1.8 pk slash = strrchr(ttyn, '/');
653 1.32 mjl if (slash == NULL)
654 1.24 thorpej xputs(ttyn);
655 1.1 cgd else
656 1.24 thorpej xputs(&slash[1]);
657 1.1 cgd break;
658 1.1 cgd
659 1.1 cgd case 'h':
660 1.24 thorpej xputs(editedhost);
661 1.1 cgd break;
662 1.1 cgd
663 1.1 cgd case 'd': {
664 1.13 mycroft static char fmt[] = "%l:% %p on %A, %d %B %Y";
665 1.1 cgd
666 1.1 cgd fmt[4] = 'M'; /* I *hate* SCCS... */
667 1.1 cgd (void)time(&t);
668 1.1 cgd (void)strftime(db, sizeof(db), fmt, localtime(&t));
669 1.24 thorpej xputs(db);
670 1.3 mycroft break;
671 1.3 mycroft
672 1.3 mycroft case 's':
673 1.24 thorpej xputs(kerninfo.sysname);
674 1.3 mycroft break;
675 1.3 mycroft
676 1.3 mycroft case 'm':
677 1.24 thorpej xputs(kerninfo.machine);
678 1.3 mycroft break;
679 1.3 mycroft
680 1.3 mycroft case 'r':
681 1.24 thorpej xputs(kerninfo.release);
682 1.3 mycroft break;
683 1.3 mycroft
684 1.3 mycroft case 'v':
685 1.24 thorpej xputs(kerninfo.version);
686 1.1 cgd break;
687 1.1 cgd }
688 1.1 cgd
689 1.1 cgd case '%':
690 1.1 cgd putchr('%');
691 1.1 cgd break;
692 1.1 cgd }
693 1.1 cgd cp++;
694 1.1 cgd }
695 1.1 cgd }
696