1 /* Copyright 1993-95 by Carl Harris, Jr. Copyright 1996 by Eric S. Raymond
3 * For license terms, see the file COPYING in this directory.
6 /***********************************************************************
9 programmer: Carl Harris, ceharris@mal.com
10 description: command-line option processing
12 ***********************************************************************/
19 #include "popclient.h"
22 /* XXX -- Would like to use 'enum' here, but it causes type mismatch
23 problems many compilers */
33 #define LA_PROTOCOL 10
37 #define LA_USERNAME 14
38 #define LA_REMOTEFILE 15
39 #define LA_LOCALFILE 16
43 #define LA_NOREWRITE 20
46 static char *shortoptions = "23VaKkvscl:Fd:f:u:r:o:m:L:qN";
47 static struct option longoptions[] = {
48 {"version", no_argument, (int *) 0, LA_VERSION },
49 {"all", no_argument, (int *) 0, LA_ALL },
50 {"kill", no_argument, (int *) 0, LA_KILL },
51 {"keep", no_argument, (int *) 0, LA_KEEP },
52 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
53 {"silent", no_argument, (int *) 0, LA_SILENT },
54 {"stdout", no_argument, (int *) 0, LA_STDOUT },
55 {"limit", required_argument, (int *) 0, LA_LIMIT },
56 {"flush", no_argument, (int *) 0, LA_FLUSH },
57 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
58 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
59 {"daemon", required_argument, (int *) 0, LA_DAEMON },
60 {"poprc", required_argument, (int *) 0, LA_POPRC },
61 {"user", required_argument, (int *) 0, LA_USERNAME },
62 {"username", required_argument, (int *) 0, LA_USERNAME },
63 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
64 {"local", required_argument, (int *) 0, LA_LOCALFILE },
65 {"mda", required_argument, (int *) 0, LA_MDA },
66 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
67 {"idfile", required_argument, (int *) 0, LA_IDFILE },
68 {"quit", no_argument, (int *) 0, LA_QUIT },
69 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
70 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
71 {(char *) 0, no_argument, (int *) 0, 0 }
75 /*********************************************************************
76 function: parsecmdline
77 description: parse/validate the command line options.
80 argv argument strings.
81 queryctl pointer to a struct hostrec to receive the parsed
84 return value: if positive, argv index of last parsed option + 1
85 (presumes one or more server names follows).
86 if zero, the command line switches are such that
87 no server names are required (e.g. --version).
88 if negative, the command line is has one or more
91 globals: writes outlevel, versioninfo, yydebug, logfile,
92 poll_interval, quitmode, poprcfile, idfile, linelimit.
93 *********************************************************************/
95 int parsecmdline (argc,argv,queryctl)
98 struct hostrec *queryctl;
101 int fflag = 0; /* TRUE when -o or -c has been specified */
102 int errflag = 0; /* TRUE when a syntax error is detected */
104 int got_kill = 0; /* TRUE when --kill is specified */
106 extern int optind, opterr; /* defined in getopt(2) */
107 extern char *optarg; /* defined in getopt(2) */
109 bzero(queryctl,sizeof(struct hostrec)); /* start clean */
112 (c = getopt_long(argc,argv,shortoptions,
113 longoptions,&option_index)) != -1) {
117 queryctl->protocol = P_POP2;
120 queryctl->protocol = P_POP3;
128 queryctl->fetchall = !0;
142 outlevel = O_VERBOSE;
154 queryctl->output = TO_STDOUT;
159 linelimit = atoi(optarg);
161 fprintf(stderr,"Line count limit must be non-negative");
167 queryctl->flush = !0;
170 /* XXX -- should probably use a table lookup here */
171 if (strcasecmp(optarg,"pop2") == 0)
172 queryctl->protocol = P_POP2;
173 else if (strcasecmp(optarg,"pop3") == 0)
174 queryctl->protocol = P_POP3;
175 else if (strcasecmp(optarg,"imap") == 0)
176 queryctl->protocol = P_IMAP;
177 else if (strcasecmp(optarg,"apop") == 0)
178 queryctl->protocol = P_APOP;
179 else if (strcasecmp(optarg,"rpop") == 0)
180 queryctl->protocol = P_RPOP;
182 fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
188 poll_interval = atoi(optarg);
192 poprcfile = (char *) xmalloc(strlen(optarg)+1);
193 strcpy(poprcfile,optarg);
197 idfile = (char *) xmalloc(strlen(optarg)+1);
198 strcpy(idfile,optarg);
202 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
210 queryctl->output = TO_FOLDER;
211 strncpy(queryctl->userfolder,optarg,sizeof(queryctl->userfolder)-1);
216 strncpy(queryctl->remotefolder,optarg,sizeof(queryctl->remotefolder)-1);
220 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda)-1);
232 queryctl->rewrite = 0;
243 /* squawk if syntax errors were detected */
244 fputs("usage: popclient [options] [server ...]\n", stderr);
245 fputs(" options\n",stderr);
246 fputs(" -2 use POP2 protocol\n", stderr);
247 fputs(" -3 use POP3 protocol\n", stderr);
248 fputs(" --protocol specify pop2, pop3, imap, apop, or rpop\n",
250 fputs(" -V, --version display version info\n", stderr);
251 fputs(" -a, --all retrieve old and new messages\n", stderr);
252 fputs(" -F, --flush delete old messages from server\n", stderr);
253 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
254 fputs(" -k, --keep save new messages after retrieval\n", stderr);
255 fputs(" -l, --limit retrieve at most n message lines\n", stderr);
256 fputs(" -m, --mda set mail user agent to pass to\n", stderr);
257 fputs(" -q, --quit kill daemon process\n", stderr);
258 fputs(" -s, --silent work silently\n", stderr);
259 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
260 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
261 fputs(" -f, --poprc specify alternate config file\n", stderr);
262 fputs(" -i, --idfile specify alternate ID database\n", stderr);
263 fputs(" -u, --username specify server user ID\n", stderr);
264 fputs(" -c, --stdout write received mail to stdout\n", stderr);
265 fputs(" -o, --local specify filename for received mail\n", stderr);
266 fputs(" -r, --remote specify remote folder name\n", stderr);
267 fputs(" -L, --logfile specify logfile name\n", stderr);
271 if (linelimit && !got_kill)
280 /*********************************************************************
281 function: setdefaults
282 description: set reasonable default values for unspecified options.
284 options option values parsed from the command-line; unspeci-
285 fied options must be filled with zero.
287 return value: zero if defaults were successfully set, else non-zero
288 (indicates a problem reading /etc/passwd).
290 globals: writes outlevel, poprcfile, idfile.
291 *********************************************************************/
293 int setdefaults (queryctl)
294 struct hostrec *queryctl;
300 bzero(queryctl,sizeof(*queryctl));
302 if ((pw = getpwuid(uid = getuid())) == NULL) {
303 fprintf(stderr,"No passwd entry for uid %d\n",uid);
307 queryctl->protocol = DEF_PROTOCOL;
309 #if defined(KEEP_IS_DEFAULT)
314 queryctl->rewrite = 1;
316 strcpy(queryctl->localname,pw->pw_name);
317 strcpy(queryctl->remotename,pw->pw_name);
318 sprintf(queryctl->userfolder, USERFOLDER, pw->pw_name);
319 queryctl->output = TO_MDA;
320 (void) sprintf(queryctl->mda, DEF_MDA, queryctl->localname);
323 (char *) xmalloc(strlen(pw->pw_dir)+strlen(POPRC_NAME)+2);
325 strcpy(poprcfile, pw->pw_dir);
326 strcat(poprcfile, "/");
327 strcat(poprcfile, POPRC_NAME);
330 (char *) xmalloc(strlen(pw->pw_dir)+strlen(IDFILE_NAME)+2);
332 strcpy(idfile, pw->pw_dir);
334 strcat(idfile, IDFILE_NAME);
343 /******************************************************************
344 function: getnextserver
345 description: read next server name from the command line.
349 optind as returned by parsecmdline and this function.
351 ret. value: next server name from command line or NULL if all
352 server names have been retrieved.
355 *****************************************************************/
356 char *getnextserver (argc,argv,optind)
361 if (*optind >= argc) {
362 /* no more servers */
366 return(argv[(*optind)++]);