xref: /titanic_41/usr/src/cmd/ttymon/ttyadm.c (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
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 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 
33 #include <stdio.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38 #include <ctype.h>
39 #include <sys/stat.h>
40 #include <stdarg.h>
41 #include "tmstruct.h"
42 #include "ttymon.h"
43 
44 /*
45  *	ttyadm 	- format ttymon specific information and
46  *		  print it to stdout
47  *
48  *	Usage: 	ttyadm [options] -d device -s service -l ttylabel
49  *		ttyadm -V
50  *
51  *		valid options are:
52  *		-c
53  *		-h
54  *		-b
55  *		-I
56  *		-S y|n
57  *		-T term
58  *		-r count
59  *		-t timeout
60  *		-p prompt
61  *		-m modules
62  *		-i msg
63  */
64 
65 static void usage();
66 static	int	check_label();
67 
68 extern	int	check_device();
69 extern	int	check_cmd();
70 extern	int	vml();
71 
72 int
73 main(int argc, char *argv[])
74 {
75 	int c;			/* option letter */
76 	int errflg = 0;		/* error indicator */
77 
78 	struct pmtab *ptr;
79 	char *timeout = "";
80 	char *count = "";
81 	char prompt[BUFSIZ];
82 	char dmsg[BUFSIZ];
83 	char ttyflags[BUFSIZ], *tf;
84 
85 	int  dflag = 0;		/* -d seen */
86 	int  sflag = 0;		/* -s seen */
87 	int  lflag = 0;		/* -l seen */
88 	int  mflag = 0;		/* -m seen */
89 
90 	extern	void 	copystr();
91 	extern	char	*optarg;
92 	extern	int	optind;
93 	extern	int	strcheck();
94 
95 	if (argc == 1)
96 		usage();
97 	if ((ptr = ALLOC_PMTAB) == PNULL) {
98 		(void)fprintf(stderr, "calloc failed\n");
99 		exit(1);
100 	}
101 	ptr->p_modules = "";
102 	ptr->p_dmsg = "";
103 	ptr->p_termtype = "";
104 	ptr->p_softcar = "";
105 	ptr->p_prompt = "login\\: ";
106 	ttyflags[0] = '\0';
107 	tf = ttyflags;
108 	while ((c = getopt(argc, argv, "IT:S:Vd:s:chbr:t:l:m:p:i:")) != -1) {
109 		switch (c) {
110 		case 'V':
111 			if ((argc > 2) || (optind < argc))
112 				usage();
113 			(void)fprintf(stdout,"%d\n", PMTAB_VERS);
114 			exit(0);
115 			break;	/*NOTREACHED*/
116 		case 'd':
117 			ptr->p_device = optarg;
118 			dflag = 1;
119 			break;
120 		case 'c':
121 			tf = strcat(tf,"c");
122 			break;
123 		case 'h':
124 			tf = strcat(tf,"h");
125 			break;
126 		case 'b':
127 			tf = strcat(tf,"b");
128 			break;
129 		case 'I':
130 			tf = strcat(tf,"I");
131 			break;
132 		case 'r':
133 			tf = strcat(tf,"r");
134 			count = optarg;
135 			if (strcheck(optarg,NUM) != 0) {
136 				(void)fprintf(stderr,
137 		"Invalid argument for \"-r\" -- positive number expected.\n");
138 				usage();
139 			}
140 			break;
141 		case 'T':
142 			ptr->p_termtype = optarg;
143 			break;
144 		case 'S':
145 			switch (*optarg) {
146 			case 'Y':
147 			case 'y':
148 				ptr->p_softcar = "y";
149 				break;
150 			case 'N':
151 			case 'n':
152 				ptr->p_softcar = "n";
153 				break;
154 			default:
155 				usage();
156 			}
157 			break;
158 		case 's':
159 			ptr->p_server = optarg;
160 			sflag = 1;
161 			break;
162 		case 't':
163 			timeout = optarg;
164 			if (strcheck(optarg,NUM) != 0) {
165 				(void)fprintf(stderr,
166 		"Invalid argument for \"-t\" -- positive number expected.\n");
167 				usage();
168 			}
169 			break;
170 		case 'l':
171 			ptr->p_ttylabel = optarg;
172 			lflag = 1;
173 			break;
174 		case 'm':
175 			ptr->p_modules = optarg;
176 			mflag = 1;
177 			break;
178 		case 'p':
179 			ptr->p_prompt = prompt;
180 			copystr(ptr->p_prompt,optarg);
181 			break;
182 		case 'i':
183 			ptr->p_dmsg = dmsg;
184 			copystr(ptr->p_dmsg,optarg);
185 			break;
186 		case '?':
187 			usage();
188 			break;
189 		}
190 	}
191 	if (optind < argc)
192 		usage();
193 
194 	if ((!dflag) || (!sflag) || (!lflag))
195 		usage();
196 
197 	if (check_device(ptr->p_device) != 0)
198 		errflg++;
199 	if (check_cmd(ptr->p_server) != 0)
200 		errflg++;
201 	if (check_label(ptr->p_ttylabel) != 0)
202 		errflg++;
203 	if (mflag && (vml(ptr->p_modules) != 0))
204 		errflg++;
205 	if (errflg)
206 		exit(1);
207 	(void)fprintf(stdout, "%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:",
208 			ptr->p_device, ttyflags, count, ptr->p_server,
209 			timeout, ptr->p_ttylabel, ptr->p_modules,
210 			ptr->p_prompt, ptr->p_dmsg, ptr->p_termtype,
211 			ptr->p_softcar);
212 	return (0);
213 }
214 
215 /*
216  * usage - print out a usage message
217  */
218 
219 static void
220 usage()
221 {
222 	(void)fprintf(stderr, "Usage:\tttyadm [ options ] -d device -s service -l ttylabel\n");
223 	(void)fprintf(stderr, "\tttyadm -V\n");
224 	(void)fprintf(stderr, "\n\tValid options are:\n");
225 	(void)fprintf(stderr, "\t-c\n");
226 	(void)fprintf(stderr, "\t-h\n");
227 	(void)fprintf(stderr, "\t-b\n");
228 	(void)fprintf(stderr, "\t-I\n");
229 	(void)fprintf(stderr, "\t-S y|n\n");
230 	(void)fprintf(stderr, "\t-T term\n");
231 	(void)fprintf(stderr, "\t-r count\n");
232 	(void)fprintf(stderr, "\t-t timeout\n");
233 	(void)fprintf(stderr, "\t-p prompt\n");
234 	(void)fprintf(stderr, "\t-m modules\n");
235 	(void)fprintf(stderr, "\t-i msg\n");
236 	exit(1);
237 }
238 
239 /*
240  *	check_label	- if ttylabel exists in /etc/ttydefs, return 0
241  *			- otherwise, return -1
242  */
243 
244 static int
245 check_label(ttylabel)
246 char	*ttylabel;
247 {
248 	FILE *fp;
249 	extern	int	find_label();
250 
251 	if ((ttylabel == NULL) || (*ttylabel == '\0')) {
252 		(void)fprintf(stderr, "error -- ttylabel is missing");
253 		return(-1);
254 	}
255 	if ((fp = fopen(TTYDEFS, "r")) == NULL) {
256 		(void)fprintf(stderr, "error -- \"%s\" does not exist, can't verify ttylabel <%s>\n", TTYDEFS, ttylabel);
257 		return(-1);
258 	}
259 	if (find_label(fp,ttylabel)) {
260 		(void)fclose(fp);
261 		return(0);
262 	}
263 	(void)fclose(fp);
264 	(void)fprintf(stderr,"error -- can't find ttylabel <%s> in \"%s\"\n",
265 		ttylabel, TTYDEFS);
266 	return(-1);
267 }
268 
269 /*
270  *	log	- print a message to stderr
271  */
272 void
273 log(const char *msg, ...)
274 {
275 	va_list ap;
276 	va_start(ap, msg);
277 	(void) vfprintf(stderr, msg, ap);
278 	va_end(ap);
279 	(void) fprintf(stderr, "\n");
280 }
281