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 "fetchmail.h"
31 #define LA_PROTOCOL 10
35 #define LA_USERNAME 14
36 #define LA_REMOTEFILE 15
37 #define LA_LOCALFILE 16
40 #define LA_SMTPHOST 19
43 #define LA_NOREWRITE 22
46 static char *shortoptions = "23PVaKkvS:scl: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 {"fetchrc", required_argument, (int *) 0, LA_FETCHRC },
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 {"port", required_argument, (int *) 0, LA_PORT },
67 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
68 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
69 {"idfile", required_argument, (int *) 0, LA_IDFILE },
70 {"quit", no_argument, (int *) 0, LA_QUIT },
71 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
72 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
73 {(char *) 0, no_argument, (int *) 0, 0 }
77 /*********************************************************************
78 function: parsecmdline
79 description: parse/validate the command line options.
82 argv argument strings.
83 queryctl pointer to a struct hostrec to receive the parsed
86 return value: if positive, argv index of last parsed option + 1
87 (presumes one or more server names follows).
88 if zero, the command line switches are such that
89 no server names are required (e.g. --version).
90 if negative, the command line is has one or more
93 globals: writes outlevel, versioninfo, yydebug, logfile,
94 poll_interval, quitmode, rcfile, idfile, linelimit.
95 *********************************************************************/
97 int parsecmdline (argc,argv,queryctl)
100 struct hostrec *queryctl;
103 int fflag = 0; /* TRUE when -o or -c has been specified */
104 int errflag = 0; /* TRUE when a syntax error is detected */
106 int got_kill = 0; /* TRUE when --kill is specified */
108 extern int optind, opterr; /* defined in getopt(2) */
109 extern char *optarg; /* defined in getopt(2) */
111 bzero(queryctl,sizeof(struct hostrec)); /* start clean */
114 (c = getopt_long(argc,argv,shortoptions,
115 longoptions,&option_index)) != -1) {
119 queryctl->protocol = P_POP2;
122 queryctl->protocol = P_POP3;
130 queryctl->fetchall = !0;
144 outlevel = O_VERBOSE;
156 queryctl->output = TO_STDOUT;
161 linelimit = atoi(optarg);
163 fprintf(stderr,"Line count limit must be non-negative");
169 queryctl->flush = !0;
173 /* XXX -- should probably use a table lookup here */
174 if (strcasecmp(optarg,"pop2") == 0)
175 queryctl->protocol = P_POP2;
176 else if (strcasecmp(optarg,"pop3") == 0)
177 queryctl->protocol = P_POP3;
178 else if (strcasecmp(optarg,"imap") == 0)
179 queryctl->protocol = P_IMAP;
180 else if (strcasecmp(optarg,"apop") == 0)
181 queryctl->protocol = P_APOP;
182 else if (strcasecmp(optarg,"rpop") == 0)
183 queryctl->protocol = P_RPOP;
185 fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
191 poll_interval = atoi(optarg);
195 rcfile = (char *) xmalloc(strlen(optarg)+1);
196 strcpy(rcfile,optarg);
200 idfile = (char *) xmalloc(strlen(optarg)+1);
201 strcpy(idfile,optarg);
205 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
213 queryctl->output = TO_FOLDER;
214 strncpy(queryctl->userfolder,optarg,sizeof(queryctl->userfolder)-1);
219 strncpy(queryctl->remotefolder,optarg,sizeof(queryctl->remotefolder)-1);
223 queryctl->output = TO_MDA;
224 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda)-1);
228 queryctl->port = atoi(optarg);
236 queryctl->output = TO_SMTP;
237 strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
250 queryctl->norewrite = 1;
261 /* squawk if syntax errors were detected */
262 fputs("usage: fetchmail [options] [server ...]\n", stderr);
263 fputs(" options\n",stderr);
264 fputs(" -2 use POP2 protocol\n", stderr);
265 fputs(" -3 use POP3 protocol\n", stderr);
266 fputs(" -p, --protocol specify pop2, pop3, imap, apop, or rpop\n",
268 fputs(" -V, --version display version info\n", stderr);
269 fputs(" -a, --all retrieve old and new messages\n", stderr);
270 fputs(" -F, --flush delete old messages from server\n", stderr);
271 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
272 fputs(" -k, --keep save new messages after retrieval\n", stderr);
273 fputs(" -l, --limit retrieve at most n message lines\n", stderr);
274 fputs(" -m, --mda set mail user agent to pass to\n", stderr);
275 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
276 fputs(" -q, --quit kill daemon process\n", stderr);
277 fputs(" -s, --silent work silently\n", stderr);
278 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
279 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
280 fputs(" -f, --fetchrc specify alternate config file\n", stderr);
281 fputs(" -i, --idfile specify alternate ID database\n", stderr);
282 fputs(" -u, --username specify server user ID\n", stderr);
283 fputs(" -c, --stdout write received mail to stdout\n", stderr);
284 fputs(" -o, --local specify filename for received mail\n", stderr);
285 fputs(" -r, --remote specify remote folder name\n", stderr);
286 fputs(" -L, --logfile specify logfile name\n", stderr);
290 if (linelimit && !got_kill)
299 /*********************************************************************
300 function: setdefaults
301 description: set reasonable default values for unspecified options.
303 options option values parsed from the command-line; unspeci-
304 fied options must be filled with zero.
306 return value: zero if defaults were successfully set, else non-zero
307 (indicates a problem reading /etc/passwd).
309 globals: writes outlevel, rcfile, idfile.
310 *********************************************************************/
313 int setdefaults (queryctl)
314 struct hostrec *queryctl;
318 bzero(queryctl,sizeof(*queryctl));
320 if ((user = getenv("USER")) == (char *)NULL
321 || (home = getenv("HOME")) == (char *)NULL)
325 if ((pw = getpwuid(getuid())) != NULL)
332 fprintf(stderr,"I can't find your name and home directory!\n");
337 queryctl->protocol = DEF_PROTOCOL;
339 #if defined(KEEP_IS_DEFAULT)
344 queryctl->norewrite = 0;
346 strcpy(queryctl->localname, user);
347 strcpy(queryctl->remotename, user);
348 sprintf(queryctl->userfolder, USERFOLDER, user);
349 strcpy(queryctl->smtphost, "localhost");
350 queryctl->output = TO_SMTP;
351 (void) sprintf(queryctl->mda, DEF_MDA, queryctl->localname);
354 (char *) xmalloc(strlen(home)+strlen(FETCHRC_NAME)+2);
356 strcpy(rcfile, home);
358 strcat(rcfile, FETCHRC_NAME);
361 (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
363 strcpy(idfile, home);
365 strcat(idfile, IDFILE_NAME);
374 /******************************************************************
375 function: getnextserver
376 description: read next server name from the command line.
380 optind as returned by parsecmdline and this function.
382 ret. value: next server name from command line or NULL if all
383 server names have been retrieved.
386 *****************************************************************/
387 char *getnextserver (argc,argv,optind)
392 if (*optind >= argc) {
393 /* no more servers */
397 return(argv[(*optind)++]);