xref: /illumos-gate/usr/src/cmd/ttymon/ttyadm.c (revision 3bb2c156)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved	*/
28 
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
34 #include <ctype.h>
35 #include <sys/stat.h>
36 #include <stdarg.h>
37 #include "tmstruct.h"
38 #include "tmextern.h"
39 #include "ttymon.h"
40 
41 /*
42  *	ttyadm	- format ttymon specific information and
43  *		  print it to stdout
44  *
45  *	Usage:	ttyadm [options] -d device -s service -l ttylabel
46  *		ttyadm -V
47  *
48  *		valid options are:
49  *		-c
50  *		-h
51  *		-b
52  *		-I
53  *		-S y|n
54  *		-T term
55  *		-r count
56  *		-t timeout
57  *		-p prompt
58  *		-m modules
59  *		-i msg
60  */
61 
62 static void usage(void);
63 static	int	check_label(char *);
64 
65 int
main(int argc,char * argv[])66 main(int argc, char *argv[])
67 {
68 	int c;			/* option letter */
69 	int errflg = 0;		/* error indicator */
70 
71 	struct pmtab *ptr;
72 	char *timeout = "";
73 	char *count = "";
74 	char prompt[BUFSIZ];
75 	char dmsg[BUFSIZ];
76 	char ttyflags[BUFSIZ], *tf;
77 
78 	int  dflag = 0;		/* -d seen */
79 	int  sflag = 0;		/* -s seen */
80 	int  lflag = 0;		/* -l seen */
81 	int  mflag = 0;		/* -m seen */
82 
83 	if (argc == 1)
84 		usage();
85 	if ((ptr = ALLOC_PMTAB) == NULL) {
86 		(void) fprintf(stderr, "calloc failed\n");
87 		exit(1);
88 	}
89 	ptr->p_modules = "";
90 	ptr->p_dmsg = "";
91 	ptr->p_termtype = "";
92 	ptr->p_softcar = "";
93 	ptr->p_prompt = "login\\: ";
94 	ttyflags[0] = '\0';
95 	tf = ttyflags;
96 	while ((c = getopt(argc, argv, "IT:S:Vd:s:chbr:t:l:m:p:i:")) != -1) {
97 		switch (c) {
98 		case 'V':
99 			if ((argc > 2) || (optind < argc))
100 				usage();
101 			(void) fprintf(stdout, "%d\n", PMTAB_VERS);
102 			exit(0);
103 			break;	/*NOTREACHED*/
104 		case 'd':
105 			ptr->p_device = optarg;
106 			dflag = 1;
107 			break;
108 		case 'c':
109 			tf = strcat(tf, "c");
110 			break;
111 		case 'h':
112 			tf = strcat(tf, "h");
113 			break;
114 		case 'b':
115 			tf = strcat(tf, "b");
116 			break;
117 		case 'I':
118 			tf = strcat(tf, "I");
119 			break;
120 		case 'r':
121 			tf = strcat(tf, "r");
122 			count = optarg;
123 			if (strcheck(optarg, NUM) != 0) {
124 				(void) fprintf(stderr,
125 		"Invalid argument for \"-r\" -- positive number expected.\n");
126 				usage();
127 			}
128 			break;
129 		case 'T':
130 			ptr->p_termtype = optarg;
131 			break;
132 		case 'S':
133 			switch (*optarg) {
134 			case 'Y':
135 			case 'y':
136 				ptr->p_softcar = "y";
137 				break;
138 			case 'N':
139 			case 'n':
140 				ptr->p_softcar = "n";
141 				break;
142 			default:
143 				usage();
144 			}
145 			break;
146 		case 's':
147 			ptr->p_server = optarg;
148 			sflag = 1;
149 			break;
150 		case 't':
151 			timeout = optarg;
152 			if (strcheck(optarg, NUM) != 0) {
153 				(void) fprintf(stderr,
154 		"Invalid argument for \"-t\" -- positive number expected.\n");
155 				usage();
156 			}
157 			break;
158 		case 'l':
159 			ptr->p_ttylabel = optarg;
160 			lflag = 1;
161 			break;
162 		case 'm':
163 			ptr->p_modules = optarg;
164 			mflag = 1;
165 			break;
166 		case 'p':
167 			ptr->p_prompt = prompt;
168 			copystr(ptr->p_prompt, optarg);
169 			break;
170 		case 'i':
171 			ptr->p_dmsg = dmsg;
172 			copystr(ptr->p_dmsg, optarg);
173 			break;
174 		case '?':
175 			usage();
176 			break;
177 		}
178 	}
179 	if (optind < argc)
180 		usage();
181 
182 	if ((!dflag) || (!sflag) || (!lflag))
183 		usage();
184 
185 	if (check_device(ptr->p_device) != 0)
186 		errflg++;
187 	if (check_cmd(ptr->p_server) != 0)
188 		errflg++;
189 	if (check_label(ptr->p_ttylabel) != 0)
190 		errflg++;
191 	if (mflag && (vml(ptr->p_modules) != 0))
192 		errflg++;
193 	if (errflg)
194 		exit(1);
195 	(void) fprintf(stdout, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:",
196 	    ptr->p_device, ttyflags, count, ptr->p_server,
197 	    timeout, ptr->p_ttylabel, ptr->p_modules,
198 	    ptr->p_prompt, ptr->p_dmsg, ptr->p_termtype,
199 	    ptr->p_softcar);
200 	return (0);
201 }
202 
203 /*
204  * usage - print out a usage message
205  */
206 
207 static void
usage(void)208 usage(void)
209 {
210 	(void) fprintf(stderr,
211 	    "Usage:\tttyadm [ options ] -d device -s service -l ttylabel\n");
212 	(void) fprintf(stderr, "\tttyadm -V\n");
213 	(void) fprintf(stderr, "\n\tValid options are:\n");
214 	(void) fprintf(stderr, "\t-c\n");
215 	(void) fprintf(stderr, "\t-h\n");
216 	(void) fprintf(stderr, "\t-b\n");
217 	(void) fprintf(stderr, "\t-I\n");
218 	(void) fprintf(stderr, "\t-S y|n\n");
219 	(void) fprintf(stderr, "\t-T term\n");
220 	(void) fprintf(stderr, "\t-r count\n");
221 	(void) fprintf(stderr, "\t-t timeout\n");
222 	(void) fprintf(stderr, "\t-p prompt\n");
223 	(void) fprintf(stderr, "\t-m modules\n");
224 	(void) fprintf(stderr, "\t-i msg\n");
225 	exit(1);
226 }
227 
228 /*
229  *	check_label	- if ttylabel exists in /etc/ttydefs, return 0
230  *			- otherwise, return -1
231  */
232 
233 static int
check_label(char * ttylabel)234 check_label(char *ttylabel)
235 {
236 	FILE *fp;
237 
238 	if ((ttylabel == NULL) || (*ttylabel == '\0')) {
239 		(void) fprintf(stderr, "error -- ttylabel is missing");
240 		return (-1);
241 	}
242 	if ((fp = fopen(TTYDEFS, "r")) == NULL) {
243 		(void) fprintf(stderr, "error -- \"%s\" does not exist, "
244 		    "can't verify ttylabel <%s>\n", TTYDEFS, ttylabel);
245 		return (-1);
246 	}
247 	if (find_label(fp, ttylabel)) {
248 		(void) fclose(fp);
249 		return (0);
250 	}
251 	(void) fclose(fp);
252 	(void) fprintf(stderr, "error -- can't find ttylabel <%s> in \"%s\"\n",
253 	    ttylabel, TTYDEFS);
254 	return (-1);
255 }
256 
257 /*
258  *	log	- print a message to stderr
259  */
260 void
log(const char * msg,...)261 log(const char *msg, ...)
262 {
263 	va_list ap;
264 	va_start(ap, msg);
265 	(void) vfprintf(stderr, msg, ap);
266 	va_end(ap);
267 	(void) fprintf(stderr, "\n");
268 }
269