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