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