Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / amd / amd / get_args.c
1 /*
2  * Copyright (c) 1997-1999 Erez Zadok
3  * Copyright (c) 1990 Jan-Simon Pendry
4  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Jan-Simon Pendry at Imperial College, London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgment:
21  *      This product includes software developed by the University of
22  *      California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: get_args.c,v 1.4 1999/09/30 21:01:31 ezk Exp $
42  * $FreeBSD: src/contrib/amd/amd/get_args.c,v 1.5 1999/11/05 11:58:05 obrien Exp $
43  *
44  */
45
46 /*
47  * Argument decode
48  */
49
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif /* HAVE_CONFIG_H */
53 #include <am_defs.h>
54 #include <amd.h>
55
56 /* include auto-generated version file */
57 #include <build_version.h>
58
59 char *conf_file = "/etc/amd.conf"; /* default amd configuration file */
60 char *conf_tag = NULL;          /* default conf file tags to use */
61 int usage = 0;
62 int use_conf_file = 0;          /* default don't use amd.conf file */
63 char *mnttab_file_name = NULL;  /* symbol must be available always */
64 #if 0
65 #ifdef DEBUG
66 int debug_flags = D_AMQ         /* Register AMQ */
67                 | D_DAEMON;     /* Enter daemon mode */
68 #endif /* DEBUG */
69 #endif
70
71 /*
72  * Return the version string (dynamic buffer)
73  */
74 char *
75 get_version_string(void)
76 {
77   static char *vers = NULL;
78   char tmpbuf[1024];
79   char *wire_buf;
80   int wire_buf_len = 0;
81
82   /* first get dynamic string listing all known networks */
83   wire_buf = print_wires();
84   if (wire_buf)
85     wire_buf_len = strlen(wire_buf);
86
87   vers = xmalloc(2048 + wire_buf_len);
88   sprintf(vers, "%s\n%s\n%s\n%s\n",
89           "Copyright (c) 1997-1999 Erez Zadok",
90           "Copyright (c) 1990 Jan-Simon Pendry",
91           "Copyright (c) 1990 Imperial College of Science, Technology & Medicine",
92           "Copyright (c) 1990 The Regents of the University of California.");
93   sprintf(tmpbuf, "%s version %s (build %d).\n",
94           PACKAGE, VERSION, AMU_BUILD_VERSION);
95   strcat(vers, tmpbuf);
96   sprintf(tmpbuf, "Built by %s@%s on date %s.\n",
97           USER_NAME, HOST_NAME, CONFIG_DATE);
98   strcat(vers, tmpbuf);
99   sprintf(tmpbuf, "cpu=%s (%s-endian), arch=%s, karch=%s.\n",
100           cpu, endian, gopt.arch, gopt.karch);
101   strcat(vers, tmpbuf);
102   sprintf(tmpbuf, "full_os=%s, os=%s, osver=%s, vendor=%s.\n",
103           gopt.op_sys_full, gopt.op_sys, gopt.op_sys_ver, gopt.op_sys_vendor);
104   strcat(vers, tmpbuf);
105
106   strcat(vers, "Map support for: ");
107   mapc_showtypes(tmpbuf);
108   strcat(vers, tmpbuf);
109   strcat(vers, ".\nAMFS: ");
110   ops_showamfstypes(tmpbuf);
111   strcat(vers, tmpbuf);
112   strcat(vers, ".\nFS: ");
113   ops_showfstypes(tmpbuf);
114   strcat(vers, tmpbuf);
115
116   /* append list of networks if available */
117   if (wire_buf) {
118     strcat(vers, wire_buf);
119     XFREE(wire_buf);
120   }
121
122   return vers;
123 }
124
125
126 void
127 get_args(int argc, char *argv[])
128 {
129   int opt_ch;
130   FILE *fp = stdin;
131
132   /* if no arguments were passed, try to use /etc/amd.conf file */
133   if (argc <= 1)
134     use_conf_file = 1;
135
136   while ((opt_ch = getopt(argc, argv, "nprvSa:c:d:k:l:o:t:w:x:y:C:D:F:T:O:H")) != -1)
137     switch (opt_ch) {
138
139     case 'a':
140       if (*optarg != '/') {
141         fprintf(stderr, "%s: -a option must begin with a '/'\n",
142                 am_get_progname());
143         exit(1);
144       }
145       gopt.auto_dir = optarg;
146       break;
147
148     case 'c':
149       gopt.am_timeo = atoi(optarg);
150       if (gopt.am_timeo <= 0)
151         gopt.am_timeo = AM_TTL;
152       break;
153
154     case 'd':
155       gopt.sub_domain = optarg;
156       break;
157
158     case 'k':
159       gopt.karch = optarg;
160       break;
161
162     case 'l':
163       gopt.logfile = optarg;
164       break;
165
166     case 'n':
167       gopt.flags |= CFM_NORMALIZE_HOSTNAMES;
168       break;
169
170     case 'o':
171       gopt.op_sys_ver = optarg;
172       break;
173
174     case 'p':
175      gopt.flags |= CFM_PRINT_PID;
176       break;
177
178     case 'r':
179       gopt.flags |= CFM_RESTART_EXISTING_MOUNTS;
180       break;
181
182     case 't':
183       /* timeo.retrans */
184       {
185         char *dot = strchr(optarg, '.');
186         if (dot)
187           *dot = '\0';
188         if (*optarg) {
189           gopt.amfs_auto_timeo = atoi(optarg);
190         }
191         if (dot) {
192           gopt.amfs_auto_retrans = atoi(dot + 1);
193           *dot = '.';
194         }
195       }
196       break;
197
198     case 'v':
199       fputs(get_version_string(), stderr);
200       exit(0);
201       break;
202
203     case 'w':
204       gopt.am_timeo_w = atoi(optarg);
205       if (gopt.am_timeo_w <= 0)
206         gopt.am_timeo_w = AM_TTL_W;
207       break;
208
209     case 'x':
210       usage += switch_option(optarg);
211       break;
212
213     case 'y':
214 #ifdef HAVE_MAP_NIS
215       gopt.nis_domain = optarg;
216 #else /* not HAVE_MAP_NIS */
217       plog(XLOG_USER, "-y: option ignored.  No NIS support available.");
218 #endif /* not HAVE_MAP_NIS */
219       break;
220
221     case 'C':
222       gopt.cluster = optarg;
223       break;
224
225     case 'D':
226 #ifdef DEBUG
227       usage += debug_option(optarg);
228 #else /* not DEBUG */
229       fprintf(stderr, "%s: not compiled with DEBUG option -- sorry.\n",
230               am_get_progname());
231 #endif /* not DEBUG */
232       break;
233
234     case 'F':
235       conf_file = optarg;
236       use_conf_file = 1;
237       break;
238
239     case 'H':
240       goto show_usage;
241       break;
242
243     case 'O':
244       gopt.op_sys = optarg;
245       break;
246
247     case 'S':
248       gopt.flags &= ~CFM_PROCESS_LOCK; /* turn process locking off */
249       break;
250
251     case 'T':
252       conf_tag = optarg;
253       break;
254
255     default:
256       usage = 1;
257       break;
258     }
259
260   /*
261    * amd.conf file: if not command-line arguments were used, or if -F was
262    * specified, then use that amd.conf file.  If the file cannot be opened,
263    * abort amd.  If it can be found, open it, parse it, and then close it.
264    */
265   if (use_conf_file && conf_file) {
266     fp = fopen(conf_file, "r");
267     if (!fp) {
268       char buf[128];
269       sprintf(buf, "Amd configuration file (%s)", conf_file);
270       perror(buf);
271       exit(1);
272     }
273     yyin = fp;
274     yyparse();
275     fclose(fp);
276     if (process_last_regular_map() != 0)
277       exit(1);
278   }
279
280   /* make sure there are some default options defined */
281   if (xlog_level_init == ~0) {
282     switch_option("");
283   }
284 #ifdef DEBUG
285   usage += switch_option("debug");
286 #endif /* DEBUG */
287
288   /* log information regarding amd.conf file */
289   if (use_conf_file && conf_file)
290     plog(XLOG_INFO, "using configuration file %s", conf_file);
291
292 #ifdef HAVE_MAP_LDAP
293   /* ensure that if ldap_base is specified, that also ldap_hostports is */
294   if (gopt.ldap_hostports && !gopt.ldap_base) {
295     fprintf(stderr, "must specify both ldap_hostports and ldap_base\n");
296     exit(1);
297   }
298 #endif /* HAVE_MAP_LDAP */
299
300   if (usage)
301     goto show_usage;
302
303   while (optind <= argc - 2) {
304     char *dir = argv[optind++];
305     char *map = argv[optind++];
306     char *opts = "";
307     if (argv[optind] && *argv[optind] == '-')
308       opts = &argv[optind++][1];
309
310     root_newmap(dir, opts, map, NULL);
311   }
312
313   if (optind == argc) {
314     /*
315      * Append domain name to hostname.
316      * sub_domain overrides hostdomain
317      * if given.
318      */
319     if (gopt.sub_domain)
320       hostdomain = gopt.sub_domain;
321     if (*hostdomain == '.')
322       hostdomain++;
323     strcat(hostd, ".");
324     strcat(hostd, hostdomain);
325
326 #ifdef MOUNT_TABLE_ON_FILE
327 # ifdef DEBUG
328     if (debug_flags & D_MTAB)
329       mnttab_file_name = DEBUG_MNTTAB;
330     else
331 # endif /* DEBUG */
332       mnttab_file_name = MNTTAB_FILE_NAME;
333 #else /* not MOUNT_TABLE_ON_FILE */
334 # ifdef DEBUG
335     if (debug_flags & D_MTAB)
336       dlog("-D mtab option ignored");
337 # endif /* DEBUG */
338 #endif /* not MOUNT_TABLE_ON_FILE */
339
340     if (switch_to_logfile(gopt.logfile, orig_umask) != 0)
341       plog(XLOG_USER, "Cannot switch logfile");
342
343     /*
344      * If the kernel architecture was not specified
345      * then use the machine architecture.
346      */
347     if (gopt.karch == 0)
348       gopt.karch = gopt.arch;
349
350     if (gopt.cluster == 0)
351       gopt.cluster = hostdomain;
352
353     if (gopt.amfs_auto_timeo <= 0)
354       gopt.amfs_auto_timeo = AMFS_AUTO_TIMEO;
355     if (gopt.amfs_auto_retrans <= 0)
356       gopt.amfs_auto_retrans = AMFS_AUTO_RETRANS;
357     if (gopt.amfs_auto_retrans <= 0)
358       gopt.amfs_auto_retrans = 3;       /* XXX */
359     return;
360   }
361
362 show_usage:
363   fprintf(stderr,
364           "Usage: %s [-nprvHS] [-a mount_point] [-c cache_time] [-d domain]\n\
365 \t[-k kernel_arch] [-l logfile%s\n\
366 \t[-t timeout.retrans] [-w wait_timeout] [-C cluster_name]\n\
367 \t[-o op_sys_ver] [-O op_sys_name]\n\
368 \t[-F conf_file] [-T conf_tag]", am_get_progname(),
369 #ifdef HAVE_SYSLOG
370 # ifdef LOG_DAEMON
371           "|\"syslog[:facility]\"]"
372 # else /* not LOG_DAEMON */
373           "|\"syslog\"]"
374 # endif /* not LOG_DAEMON */
375 #else /* not HAVE_SYSLOG */
376           "]"
377 #endif /* not HAVE_SYSLOG */
378           );
379
380 #ifdef HAVE_MAP_NIS
381   fputs(" [-y nis-domain]\n", stderr);
382 #else /* not HAVE_MAP_NIS */
383   fputc('\n', stderr);
384 #endif /* HAVE_MAP_NIS */
385
386   show_opts('x', xlog_opt);
387 #ifdef DEBUG
388   show_opts('D', dbg_opt);
389 #endif /* DEBUG */
390   fprintf(stderr, "\t[directory mapname [-map_options]] ...\n");
391   exit(1);
392 }