1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termio.h>
#include <sys/stermio.h>
#include <sys/termiox.h>
#include "ttymon.h"
#include "tmstruct.h"
#include "tmextern.h"
#include "stty.h"
static void insert_def(struct Gdef *);
static void zero(char *, int);
/*
* read_ttydefs - read in the /etc/ttydefs and store in Gdef array
* - if id is not NULL, only get entry with that id
* - if check is TRUE, print out the entries
*/
void
read_ttydefs(const char *id, int check)
{
FILE *fp;
static struct Gdef def;
struct Gdef *gptr;
static char line[BUFSIZ];
static char dbuf[BUFSIZ];
char *ptr;
int len;
int input, state, size, rawc, field;
char oldc;
static char d_id[MAXID+1],
d_nextid[MAXID+1],
d_autobaud[MAXID+1],
d_if[BUFSIZ],
d_ff[BUFSIZ];
static char *states[] = {
"", "tty label", "Initial flags", "Final flags", "Autobaud",
"Next label"
};
if ((fp = fopen(TTYDEFS, "r")) == NULL) {
log("can't open \"%s\".\n", TTYDEFS);
return;
}
if (check) {
for (len = 0; len < (size_t)(BUFSIZ - 1); len++)
dbuf[len] = '-';
dbuf[len] = '\0';
}
/* Start searching for the line with the proper "id". */
input = ACTIVE;
field = FAILURE;
do {
line[0] = '\0';
for (ptr = line, oldc = '\0'; ptr < &line[sizeof (line) - 1] &&
(rawc = getc(fp)) != '\n' && rawc != EOF;
ptr++, oldc = (char)rawc) {
if ((rawc == '#') && (oldc != '\\'))
break;
*ptr = (char)rawc;
}
*ptr = '\0';
/* skip rest of the line */
if (rawc != EOF && rawc != '\n') {
if (check && rawc != '#')
log("Entry too long.");
while ((rawc = getc(fp)) != EOF && rawc != '\n')
;
}
if (rawc == EOF) {
if (ptr == line)
break;
else
input = FINISHED;
}
/* if empty line, skip */
for (ptr = line; *ptr != '\0' && isspace(*ptr); ptr++)
;
if (*ptr == '\0')
continue;
/* Now we have the complete line */
/* Initialize "def" and "gptr". */
gptr = &def;
zero((char *)gptr, sizeof (struct Gdef));
ptr = line;
state = T_TTYLABEL;
(void) strncpy(d_id, getword(ptr, &size, 0), MAXID);
gptr->g_id = d_id;
ptr += size;
if (*ptr != ':') {
field = state;
state = FAILURE;
} else {
ptr++; /* Skip the ':' */
state++;
}
/* If "id" != NULL, and it does not match, go to next entry */
if ((id != NULL) && (strcmp(id, gptr->g_id) != 0))
continue;
if (check) {
len = strlen(line);
dbuf[len] = '\0';
log("\n%s", dbuf);
log("%s", line);
log("%s\n", dbuf);
dbuf[len] = '-';
}
for (; state != FAILURE && state != SUCCESS; ) {
switch (state) {
case T_IFLAGS:
(void) strncpy(d_if, getword(ptr, &size, 1),
BUFSIZ);
gptr->g_iflags = d_if;
ptr += size;
if ((*ptr != ':') || (check_flags(d_if) != 0)) {
field = state;
state = FAILURE;
} else {
ptr++;
state++;
}
break;
case T_FFLAGS:
(void) strncpy(d_ff, getword(ptr, &size, 1),
BUFSIZ);
gptr->g_fflags = d_ff;
ptr += size;
if ((*ptr != ':') || (check_flags(d_ff) != 0)) {
field = state;
state = FAILURE;
} else {
ptr++;
state++;
}
break;
case T_AUTOBAUD:
(void) strncpy(d_autobaud,
getword(ptr, &size, 0), MAXID);
if (size > 1) {
ptr += size;
field = state;
state = FAILURE;
break;
}
if (size == 1) {
if (*d_autobaud == 'A') {
gptr->g_autobaud |= A_FLAG;
} else {
ptr += size;
field = state;
state = FAILURE;
break;
}
}
ptr += size;
if (*ptr != ':') {
field = state;
state = FAILURE;
} else {
ptr++; /* Skip the ':' */
state++;
}
break;
case T_NEXTLABEL:
(void) strncpy(d_nextid,
getword(ptr, &size, 0), MAXID);
gptr->g_nextid = d_nextid;
ptr += size;
if (*ptr != '\0') {
field = state;
state = FAILURE;
} else {
state = SUCCESS;
}
break;
} /* end switch */
} /* end for loop */
if (state == SUCCESS) {
if (check) {
log("ttylabel:\t%s", gptr->g_id);
log("initial flags:\t%s", gptr->g_iflags);
log("final flags:\t%s", gptr->g_fflags);
if (gptr->g_autobaud & A_FLAG)
log("autobaud:\tyes");
else
log("autobaud:\tno");
log("nextlabel:\t%s", gptr->g_nextid);
}
if (Ndefs < MAXDEFS) {
insert_def(gptr);
} else {
log("can't add more entries to ttydefs table, "
" Maximum entries = %d", MAXDEFS);
(void) fclose(fp);
return;
}
if (id != NULL) {
return;
}
} else {
*++ptr = '\0';
log("Parsing failure in the \"%s\" field\n"
"%s<--error detected here\n", states[field], line);
}
} while (input == ACTIVE);
(void) fclose(fp);
}
/*
* zero - zero out the buffer
*/
static void
zero(char *adr, int size)
{
if (adr != NULL) {
while (size--)
*adr++ = '\0';
}
}
/*
* find_def(ttylabel)
* - scan Gdef table for an entry with requested "ttylabel".
* - return a Gdef ptr if entry with "ttylabel" is found
* - return NULL if no entry with matching "ttylabel"
*/
struct Gdef *
find_def(char *ttylabel)
{
int i;
struct Gdef *tp;
tp = &Gdef[0];
for (i = 0; i < Ndefs; i++, tp++) {
if (strcmp(ttylabel, tp->g_id) == 0) {
return (tp);
}
}
return (NULL);
}
/*
* check_flags - check to see if the flags contains options that are
* recognizable by stty
* - return 0 if no error. Otherwise return -1
*/
int
check_flags(char *flags)
{
struct termio termio;
struct termios termios;
struct termiox termiox;
struct winsize winsize;
int term;
int cnt = 1;
char *argvp[MAXARGS]; /* stty args */
static char *binstty = "/usr/bin/stty";
static char buf[BUFSIZ];
char *s_arg; /* this will point to invalid option */
/* put flags into buf, because strtok will break up buffer */
(void) strcpy(buf, flags);
argvp[0] = binstty; /* just a place holder */
mkargv(buf, &argvp[1], &cnt, MAXARGS - 1);
argvp[cnt] = NULL;
/*
* because we don't know what type of terminal we have now,
* just set term = everything, so all possible stty options
* are accepted
*/
term = ASYNC|TERMIOS|FLOW;
if ((s_arg = sttyparse(cnt, argvp, term, &termio, &termios,
&termiox, &winsize)) != NULL) {
log("invalid mode: %s", s_arg);
return (-1);
}
return (0);
}
/*
* insert_def - insert one entry into Gdef table
*/
static void
insert_def(struct Gdef *gptr)
{
struct Gdef *tp;
if (find_def(gptr->g_id) != NULL) {
log("Warning -- duplicate entry <%s>, ignored", gptr->g_id);
return;
}
tp = &Gdef[Ndefs];
tp->g_id = strsave(gptr->g_id);
tp->g_iflags = strsave(gptr->g_iflags);
tp->g_fflags = strsave(gptr->g_fflags);
tp->g_autobaud = gptr->g_autobaud;
tp->g_nextid = strsave(gptr->g_nextid);
Ndefs++;
}
/*
* mkargv - parse the string into args, starting from args[cnt]
*/
void
mkargv(char *string, char **args, int *cnt, int maxargs)
{
char *ptrin, *ptrout;
int i;
int qsize;
for (i = 0; i < maxargs; i++)
args[i] = NULL;
ptrin = ptrout = string;
for (i = 0; *ptrin != '\0' && i < maxargs; i++) {
/* Skip excess white spaces between arguments. */
while (*ptrin == ' ' || *ptrin == '\t') {
ptrin++;
ptrout++;
}
/* Save the address of argument if there is something there. */
if (*ptrin == '\0')
break;
else
args[i] = ptrout;
/* Span the argument itself. The '\' character causes quoting */
/* of the next character to take place (except for '\0'). */
while (*ptrin != '\0') {
if (*ptrin == '\\') {
*ptrout++ = quoted(ptrin, &qsize);
ptrin += qsize;
/* Is this the end of the argument? If so quit loop. */
} else if (*ptrin == ' ' || *ptrin == '\t') {
ptrin++;
break;
/* If this is a normal letter of the argument, save it, advancing */
/* the pointers at the same time. */
} else *ptrout++ = *ptrin++;
}
/* Null terminate the string. */
*ptrout++ = '\0';
}
(*cnt) += i;
}
#ifdef DEBUG
/*
* dump_ttydefs - dump Gdef table to log file
*/
void
dump_ttydefs(void)
{
int i;
struct Gdef *gptr;
gptr = &Gdef[0];
log("********** dumping ttydefs table **********");
log("Ndefs = %d", Ndefs);
log(" ");
for (i = 0; i < Ndefs; i++, gptr++) {
log("----------------------------------------");
log("ttylabel:\t%s", gptr->g_id);
log("initial flags:\t%s", gptr->g_iflags);
log("final flags:\t%s", gptr->g_fflags);
if (gptr->g_autobaud & A_FLAG)
log("autobaud:\tyes");
else
log("Autobaud:\tno");
log("nextlabel:\t%s", gptr->g_nextid);
log(" ");
}
log("********** end dumping ttydefs table **********");
}
#endif
/*
* this is copies from uucp/strsave.c
* and is modified that if malloc fails, it will exit
*/
char *
strsave(char *str)
{
char *rval;
if (str == NULL) {
if ((rval = malloc(1)) == NULL) {
log("strsave: malloc failed");
exit(1);
}
*rval = '\0';
} else {
if ((rval = malloc(strlen(str) + 1)) == NULL) {
log("strsave: malloc failed");
exit(1);
}
(void) strcpy(rval, str);
}
return (rval);
}
|