xref: /original-bsd/usr.sbin/amd/amd/get_args.c (revision 4092c5cc)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)get_args.c	8.1 (Berkeley) 06/06/93
13  *
14  * $Id: get_args.c,v 5.2.2.1 1992/02/09 15:08:23 jsp beta $
15  *
16  */
17 
18 /*
19  * Argument decode
20  */
21 
22 #include "am.h"
23 #ifdef HAS_SYSLOG
24 #include <syslog.h>
25 #endif /* HAS_SYSLOG */
26 #include <sys/stat.h>
27 
28 extern int optind;
29 extern char *optarg;
30 
31 #if defined(DEBUG) && defined(PARANOID)
32 char **gargv;
33 #endif /* defined(DEBUG) && defined(PARANOID) */
34 int restart_existing_mounts;
35 int print_pid;
36 int normalize_hosts;
37 char *karch;			/* Kernel architecture */
38 char *cluster;			/* Cluster name */
39 #ifdef HAS_NIS_MAPS
40 char *domain;			/* YP domain */
41 #endif /* HAS_NIS_MAPS */
42 #ifdef UPDATE_MTAB
43 char *mtab;
44 #endif /* UPDATE_MTAB */
45 int afs_timeo = -1;
46 int afs_retrans = -1;
47 int am_timeo = AM_TTL;
48 int am_timeo_w = AM_TTL_W;
49 
50 #ifdef DEBUG
51 /*
52  * List of debug options.
53  */
54 static struct opt_tab dbg_opt[] = {
55 	{ "all", D_ALL },		/* All */
56 	{ "amq", D_AMQ },		/* Register for AMQ program */
57 	{ "daemon", D_DAEMON },		/* Enter daemon mode */
58 	{ "full", D_FULL },		/* Program trace */
59 	{ "mem", D_MEM },		/* Trace memory allocations */
60 	{ "mtab", D_MTAB },		/* Use local mtab file */
61 	{ "str", D_STR },		/* Debug string munging */
62 	{ "test", D_TEST },		/* Full debug - but no daemon */
63 	{ "trace", D_TRACE },		/* Protocol trace */
64 	{ 0, 0 }
65 };
66 
67 int debug_flags = D_AMQ			/* Register AMQ */
68 		 |D_DAEMON		/* Enter daemon mode */
69 		 ;
70 
71 /*
72  * Switch on/off debug options
73  */
debug_option(opt)74 int debug_option(opt)
75 char *opt;
76 {
77 	return cmdoption(opt, dbg_opt, &debug_flags);
78 }
79 #endif /* DEBUG */
80 
get_args(c,v)81 void get_args(c, v)
82 int c;
83 char *v[];
84 {
85 	int opt_ch;
86 	int usage = 0;
87 	char *logfile = 0;
88 	char *sub_domain = 0;
89 
90 	while ((opt_ch = getopt(c, v, "mnprva:c:d:h:k:l:t:w:x:y:C:D:")) != EOF)
91 	switch (opt_ch) {
92 	case 'a':
93 		if (*optarg != '/') {
94 			fprintf(stderr, "%s: -a option must begin with a '/'\n",
95 					progname);
96 			exit(1);
97 		}
98 		auto_dir = optarg;
99 		break;
100 
101 	case 'c':
102 		am_timeo = atoi(optarg);
103 		if (am_timeo <= 0)
104 			am_timeo = AM_TTL;
105 		break;
106 
107 	case 'd':
108 		sub_domain = optarg;
109 		break;
110 
111 	case 'h':
112 #if defined(HAS_HOST) && defined(HOST_EXEC)
113 		host_helper = optarg;
114 #else
115 		plog(XLOG_USER, "-h: option ignored.  HOST_EXEC is not enabled.");
116 		break;
117 #endif /* defined(HAS_HOST) && defined(HOST_EXEC) */
118 
119 	case 'k':
120 		karch = optarg;
121 		break;
122 
123 	case 'l':
124 		logfile = optarg;
125 		break;
126 
127 	case 'm':
128 		plog(XLOG_USER, "The -m option is no longer supported.");
129 		plog(XLOG_USER, "... Use `ypcat -k am.master` on the command line instead");
130 		break;
131 
132 	case 'n':
133 		normalize_hosts = 1;
134 		break;
135 
136 	case 'p':
137 		print_pid = 1;
138 		break;
139 
140 	case 'r':
141 		restart_existing_mounts = 1;
142 		break;
143 
144 	case 't':
145 		/* timeo.retrans */
146 		{ char *dot = strchr(optarg, '.');
147 		  if (dot) *dot = '\0';
148 		  if (*optarg) {
149 			afs_timeo = atoi(optarg);
150 		  }
151 		  if (dot) {
152 		  	afs_retrans = atoi(dot+1);
153 			*dot = '.';
154 		  }
155 		}
156 		break;
157 
158 	case 'v':
159 		fprintf(stderr, "%s%s (%s-endian).\n", copyright, version, endian);
160 		fputs("Map support for: ", stderr);
161 		mapc_showtypes(stderr);
162 		fputs(".\nFS: ", stderr);
163 		ops_showfstypes(stderr);
164 		fputs(".\n", stderr);
165 		fprintf(stderr, "Primary network is %s.\n", wire);
166 		exit(0);
167 		break;
168 
169 	case 'w':
170 		am_timeo_w = atoi(optarg);
171 		if (am_timeo_w <= 0)
172 			am_timeo_w = AM_TTL_W;
173 		break;
174 
175 	case 'x':
176 		usage += switch_option(optarg);
177 		break;
178 
179 	case 'y':
180 #ifdef HAS_NIS_MAPS
181 		domain = optarg;
182 #else
183 		plog(XLOG_USER, "-y: option ignored.  No NIS support available.");
184 #endif /* HAS_NIS_MAPS */
185 		break;
186 
187 	case 'C':
188 		cluster = optarg;
189 		break;
190 
191 	case 'D':
192 #ifdef DEBUG
193 		usage += debug_option(optarg);
194 #else
195 		fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n", progname);
196 #endif /* DEBUG */
197 		break;
198 
199 	default:
200 		usage = 1;
201 		break;
202 	}
203 
204 	if (xlog_level_init == ~0) {
205 		(void) switch_option("");
206 #ifdef DEBUG
207 		usage += switch_option("debug");
208 #endif /* DEBUG */
209 	} else {
210 #ifdef DEBUG
211 		usage += switch_option("debug");
212 #endif /* DEBUG */
213 	}
214 
215 	if (usage)
216 		goto show_usage;
217 
218 	while (optind <= c-2) {
219 		char *dir = v[optind++];
220 		char *map = v[optind++];
221 		char *opts = "";
222 		if (v[optind] && *v[optind] == '-')
223 			opts = &v[optind++][1];
224 
225 		root_newmap(dir, opts, map);
226 	}
227 
228 	if (optind == c) {
229 #ifdef hpux
230 		/*
231 		 * HP-UX can't handle ./mtab
232 		 * That system is sick - really.
233 		 */
234 #ifdef	DEBUG
235 		debug_option("nomtab");
236 #endif	/* DEBUG */
237 #endif	/* hpux */
238 
239 		/*
240 		 * Append domain name to hostname.
241 		 * sub_domain overrides hostdomain
242 		 * if given.
243 		 */
244 		if (sub_domain)
245 			hostdomain = sub_domain;
246 		if (*hostdomain == '.')
247 			hostdomain++;
248 		strcat(hostd,  ".");
249 		strcat(hostd, hostdomain);
250 
251 #ifdef UPDATE_MTAB
252 #ifdef DEBUG
253 		if (debug_flags & D_MTAB)
254 			mtab = DEBUG_MTAB;
255 		else
256 #endif /* DEBUG */
257 		mtab = MOUNTED;
258 #else
259 #ifdef DEBUG
260 		{ if (debug_flags & D_MTAB) {
261 			dlog("-D mtab option ignored");
262 		} }
263 #endif /* DEBUG */
264 #endif /* UPDATE_MTAB */
265 
266 		if (switch_to_logfile(logfile) != 0)
267 			plog(XLOG_USER, "Cannot switch logfile");
268 
269 		/*
270 		 * If the kernel architecture was not specified
271 		 * then use the machine architecture.
272 		 */
273 		if (karch == 0)
274 			karch = arch;
275 
276 		if (cluster == 0)
277 			cluster = hostdomain;
278 
279 		if (afs_timeo <= 0)
280 			afs_timeo = AFS_TIMEO;
281 		if (afs_retrans <= 0)
282 			afs_retrans = AFS_RETRANS;
283 		if (afs_retrans <= 0)
284 			afs_retrans = 3;	/* XXX */
285 		return;
286 	}
287 
288 show_usage:
289 	fprintf(stderr,
290 "Usage: %s [-mnprv] [-a mnt_point] [-c cache_time] [-d domain]\n\
291 \t[-k kernel_arch] [-l logfile|\"syslog\"] [-t afs_timeout]\n\
292 \t[-w wait_timeout] [-C cluster_name]", progname);
293 
294 #if defined(HAS_HOST) && defined(HOST_EXEC)
295 	fputs(" [-h host_helper]\n", stderr);
296 #endif /* defined(HAS_HOST) && defined(HOST_EXEC) */
297 
298 #ifdef HAS_NIS_MAPS
299 	fputs(" [-y nis-domain]\n", stderr);
300 #else
301 	fputc('\n', stderr);
302 #endif /* HAS_NIS_MAPS */
303 
304 	show_opts('x', xlog_opt);
305 #ifdef DEBUG
306 	show_opts('D', dbg_opt);
307 #endif /* DEBUG */
308 	fprintf(stderr, "\t{directory mapname [-map_options]} ...\n");
309 	exit(1);
310 }
311