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"
30 #define LA_PROTOCOL 10
34 #define LA_USERNAME 14
35 #define LA_REMOTEFILE 15
36 #define LA_LOCALFILE 16
39 #define LA_SMTPHOST 19
42 #define LA_NOREWRITE 22
45 static char *shortoptions = "23PVaKkvS:scl:Fd:f:u:r:o:m:L:qN";
46 static struct option longoptions[] = {
47 {"version", no_argument, (int *) 0, LA_VERSION },
48 {"all", no_argument, (int *) 0, LA_ALL },
49 {"kill", no_argument, (int *) 0, LA_KILL },
50 {"keep", no_argument, (int *) 0, LA_KEEP },
51 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
52 {"silent", no_argument, (int *) 0, LA_SILENT },
53 {"stdout", no_argument, (int *) 0, LA_STDOUT },
54 {"limit", required_argument, (int *) 0, LA_LIMIT },
55 {"flush", no_argument, (int *) 0, LA_FLUSH },
56 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
57 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
58 {"daemon", required_argument, (int *) 0, LA_DAEMON },
59 {"fetchrc", required_argument, (int *) 0, LA_FETCHRC },
60 {"user", required_argument, (int *) 0, LA_USERNAME },
61 {"username", required_argument, (int *) 0, LA_USERNAME },
62 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
63 {"local", required_argument, (int *) 0, LA_LOCALFILE },
64 {"mda", required_argument, (int *) 0, LA_MDA },
65 {"port", required_argument, (int *) 0, LA_PORT },
66 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
67 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
68 {"idfile", required_argument, (int *) 0, LA_IDFILE },
69 {"quit", no_argument, (int *) 0, LA_QUIT },
70 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
71 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
72 {(char *) 0, no_argument, (int *) 0, 0 }
76 /*********************************************************************
77 function: parsecmdline
78 description: parse/validate the command line options.
81 argv argument strings.
82 queryctl pointer to a struct hostrec to receive the parsed
85 return value: if positive, argv index of last parsed option + 1
86 (presumes one or more server names follows).
87 if zero, the command line switches are such that
88 no server names are required (e.g. --version).
89 if negative, the command line is has one or more
92 globals: writes outlevel, versioninfo, yydebug, logfile,
93 poll_interval, quitmode, rcfile, idfile, linelimit.
94 *********************************************************************/
96 int parsecmdline (argc,argv,queryctl)
99 struct hostrec *queryctl;
102 int fflag = 0; /* TRUE when -o or -c has been specified */
103 int errflag = 0; /* TRUE when a syntax error is detected */
105 int got_kill = 0; /* TRUE when --kill is specified */
107 extern int optind, opterr; /* defined in getopt(2) */
108 extern char *optarg; /* defined in getopt(2) */
110 memset(queryctl, '\0', sizeof(struct hostrec)); /* start clean */
113 (c = getopt_long(argc,argv,shortoptions,
114 longoptions,&option_index)) != -1) {
118 queryctl->protocol = P_POP2;
121 queryctl->protocol = P_POP3;
129 queryctl->fetchall = !0;
143 outlevel = O_VERBOSE;
155 queryctl->output = TO_STDOUT;
160 linelimit = atoi(optarg);
162 fprintf(stderr,"Line count limit must be non-negative");
168 queryctl->flush = !0;
172 /* XXX -- should probably use a table lookup here */
173 if (strcasecmp(optarg,"pop2") == 0)
174 queryctl->protocol = P_POP2;
175 else if (strcasecmp(optarg,"pop3") == 0)
176 queryctl->protocol = P_POP3;
177 else if (strcasecmp(optarg,"imap") == 0)
178 queryctl->protocol = P_IMAP;
179 else if (strcasecmp(optarg,"apop") == 0)
180 queryctl->protocol = P_APOP;
181 else if (strcasecmp(optarg,"rpop") == 0)
182 queryctl->protocol = P_RPOP;
184 fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
190 poll_interval = atoi(optarg);
194 rcfile = (char *) xmalloc(strlen(optarg)+1);
195 strcpy(rcfile,optarg);
199 idfile = (char *) xmalloc(strlen(optarg)+1);
200 strcpy(idfile,optarg);
204 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
212 queryctl->output = TO_FOLDER;
213 strncpy(queryctl->userfolder,optarg,sizeof(queryctl->userfolder)-1);
218 strncpy(queryctl->remotefolder,optarg,sizeof(queryctl->remotefolder)-1);
222 queryctl->output = TO_MDA;
223 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda)-1);
227 queryctl->port = atoi(optarg);
235 queryctl->output = TO_SMTP;
236 strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
249 queryctl->norewrite = 1;
260 /* squawk if syntax errors were detected */
261 fputs("usage: fetchmail [options] [server ...]\n", stderr);
262 fputs(" options\n",stderr);
263 fputs(" -2 use POP2 protocol\n", stderr);
264 fputs(" -3 use POP3 protocol\n", stderr);
265 fputs(" -p, --protocol specify pop2, pop3, imap, apop, or rpop\n",
267 fputs(" -V, --version display version info\n", stderr);
268 fputs(" -a, --all retrieve old and new messages\n", stderr);
269 fputs(" -F, --flush delete old messages from server\n", stderr);
270 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
271 fputs(" -k, --keep save new messages after retrieval\n", stderr);
272 fputs(" -l, --limit retrieve at most n message lines\n", stderr);
273 fputs(" -m, --mda set mail user agent to pass to\n", stderr);
274 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
275 fputs(" -q, --quit kill daemon process\n", stderr);
276 fputs(" -s, --silent work silently\n", stderr);
277 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
278 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
279 fputs(" -f, --fetchrc specify alternate config file\n", stderr);
280 fputs(" -i, --idfile specify alternate ID database\n", stderr);
281 fputs(" -u, --username specify server user ID\n", stderr);
282 fputs(" -c, --stdout write received mail to stdout\n", stderr);
283 fputs(" -o, --local specify filename for received mail\n", stderr);
284 fputs(" -r, --remote specify remote folder name\n", stderr);
285 fputs(" -L, --logfile specify logfile name\n", stderr);
289 if (linelimit && !got_kill)
298 /*********************************************************************
299 function: setdefaults
300 description: set reasonable default values for unspecified options.
302 options option values parsed from the command-line; unspeci-
303 fied options must be filled with zero.
305 return value: zero if defaults were successfully set, else non-zero
306 (indicates a problem reading /etc/passwd).
308 globals: writes outlevel, rcfile, idfile.
309 *********************************************************************/
312 int setdefaults (queryctl)
313 struct hostrec *queryctl;
317 memset(queryctl, '\0', sizeof(*queryctl));
319 if ((user = getenv("USER")) == (char *)NULL
320 || (home = getenv("HOME")) == (char *)NULL)
324 if ((pw = getpwuid(getuid())) != NULL)
331 fprintf(stderr,"I can't find your name and home directory!\n");
336 queryctl->protocol = DEF_PROTOCOL;
338 #if defined(KEEP_IS_DEFAULT)
343 queryctl->norewrite = 0;
345 strcpy(queryctl->localname, user);
346 strcpy(queryctl->remotename, user);
347 sprintf(queryctl->userfolder, USERFOLDER, user);
348 strcpy(queryctl->smtphost, "localhost");
349 queryctl->output = TO_SMTP;
350 (void) sprintf(queryctl->mda, DEF_MDA, queryctl->localname);
353 (char *) xmalloc(strlen(home)+strlen(FETCHRC_NAME)+2);
355 strcpy(rcfile, home);
357 strcat(rcfile, FETCHRC_NAME);
360 (char *) xmalloc(strlen(home)+strlen(IDFILE_NAME)+2);
362 strcpy(idfile, home);
364 strcat(idfile, IDFILE_NAME);
373 /******************************************************************
374 function: getnextserver
375 description: read next server name from the command line.
379 optind as returned by parsecmdline and this function.
381 ret. value: next server name from command line or NULL if all
382 server names have been retrieved.
385 *****************************************************************/
386 char *getnextserver (argc,argv,optind)
391 if (*optind >= argc) {
392 /* no more servers */
396 return(argv[(*optind)++]);