2 * options.c -- command-line option processing
4 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
17 #include "fetchmail.h"
31 #define LA_PROTOCOL 13
34 #define LA_AUTHENTICATE 16
36 #define LA_ENVELOPE 18
37 #define LA_USERNAME 19
42 #define LA_NOREWRITE 24
45 #define LA_SMTPHOST 27
46 #define LA_BATCHLIMIT 28
47 #define LA_FETCHLIMIT 29
49 #define LA_INTERFACE 31
53 static const char *shortoptions =
54 "?Vcsvd:NqL:f:i:p:UP:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
56 static const struct option longoptions[] = {
57 /* this can be const because all flag fields are 0 and will never get set */
58 {"help", no_argument, (int *) 0, LA_HELP },
59 {"version", no_argument, (int *) 0, LA_VERSION },
60 {"check", no_argument, (int *) 0, LA_CHECK },
61 {"silent", no_argument, (int *) 0, LA_SILENT },
62 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
63 {"daemon", required_argument, (int *) 0, LA_DAEMON },
64 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
65 {"quit", no_argument, (int *) 0, LA_QUIT },
66 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
67 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
68 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
69 {"idfile", required_argument, (int *) 0, LA_IDFILE },
71 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
72 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
73 {"uidl", no_argument, (int *) 0, LA_UIDL },
74 {"port", required_argument, (int *) 0, LA_PORT },
75 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
76 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
77 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
79 {"user", required_argument, (int *) 0, LA_USERNAME },
80 {"username", required_argument, (int *) 0, LA_USERNAME },
82 {"all", no_argument, (int *) 0, LA_ALL },
83 {"kill", no_argument, (int *) 0, LA_KILL },
84 {"keep", no_argument, (int *) 0, LA_KEEP },
85 {"flush", no_argument, (int *) 0, LA_FLUSH },
86 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
87 {"limit", required_argument, (int *) 0, LA_LIMIT },
89 {"folder", required_argument, (int *) 0, LA_FOLDER },
90 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
91 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
92 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
93 {"mda", required_argument, (int *) 0, LA_MDA },
96 {"interface", required_argument, (int *) 0, LA_INTERFACE },
97 {"monitor", required_argument, (int *) 0, LA_MONITOR },
100 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
102 {(char *) 0, no_argument, (int *) 0, 0 }
105 int parsecmdline (argc, argv, ctl)
106 /* parse and validate the command line options */
107 int argc; /* argument count */
108 char **argv; /* argument strings */
109 struct query *ctl; /* option record to be initialized */
112 * return value: if positive, argv index of last parsed option + 1
113 * (presumes one or more server names follows). if zero, the
114 * command line switches are such that no server names are
115 * required (e.g. --version). if negative, the command line is
116 * has one or more syntax errors.
120 int ocount = 0; /* count of destinations specified */
121 int errflag = 0; /* TRUE when a syntax error is detected */
123 char buf[BUFSIZ], *cp;
127 memset(ctl, '\0', sizeof(struct query)); /* start clean */
128 ctl->smtp_socket = -1;
131 (c = getopt_long(argc,argv,shortoptions,
132 longoptions,&option_index)) != -1) {
149 outlevel = O_VERBOSE;
153 cmd_daemon = atoi(optarg);
165 cmd_logfile = optarg;
169 rcfile = (char *) xmalloc(strlen(optarg)+1);
170 strcpy(rcfile,optarg);
174 idfile = (char *) xmalloc(strlen(optarg)+1);
175 strcpy(idfile,optarg);
179 /* XXX -- should probably use a table lookup here */
180 if (strcasecmp(optarg,"pop2") == 0)
181 ctl->server.protocol = P_POP2;
182 else if (strcasecmp(optarg,"pop3") == 0)
183 ctl->server.protocol = P_POP3;
184 else if (strcasecmp(optarg,"apop") == 0)
185 ctl->server.protocol = P_APOP;
186 else if (strcasecmp(optarg,"rpop") == 0)
187 ctl->server.protocol = P_RPOP;
188 else if (strcasecmp(optarg,"kpop") == 0)
190 ctl->server.protocol = P_POP3;
191 ctl->server.port = KPOP_PORT;
192 ctl->server.preauthenticate = A_KERBEROS_V4;
194 else if (strcasecmp(optarg,"imap") == 0)
195 ctl->server.protocol = P_IMAP;
197 else if (strcasecmp(optarg,"imap-k4") == 0)
198 ctl->server.protocol = P_IMAP_K4;
199 #endif /* KERBEROS_V4 */
200 else if (strcasecmp(optarg,"etrn") == 0)
201 ctl->server.protocol = P_ETRN;
203 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
209 ctl->server.uidl = FLAG_TRUE;
213 ctl->server.port = atoi(optarg);
216 case LA_AUTHENTICATE:
217 if (strcmp(optarg, "password") == 0)
218 ctl->server.preauthenticate = A_PASSWORD;
219 else if (strcmp(optarg, "kerberos") == 0)
220 ctl->server.preauthenticate = A_KERBEROS_V4;
221 else if (strcmp(optarg, "kerberos_v4") == 0)
222 ctl->server.preauthenticate = A_KERBEROS_V4;
224 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
230 ctl->server.timeout = atoi(optarg);
231 if (ctl->server.timeout == 0)
232 ctl->server.timeout = -1;
236 ctl->server.envelope = xstrdup(optarg);
241 ctl->remotename = xstrdup(optarg);
245 ctl->fetchall = FLAG_TRUE;
249 ctl->keep = FLAG_FALSE;
253 ctl->keep = FLAG_TRUE;
257 ctl->flush = FLAG_TRUE;
261 ctl->rewrite = FLAG_FALSE;
265 ctl->limit = atoi(optarg);
272 cp = strtok(buf, ",");
274 save_str(&ctl->mailboxes, -1, optarg);
276 ((cp = strtok((char *)NULL, ",")));
281 cp = strtok(buf, ",");
283 save_str(&ctl->smtphunt, -1, optarg);
285 ((cp = strtok((char *)NULL, ",")));
290 ctl->batchlimit = atoi(optarg);
291 if (ctl->batchlimit == 0)
292 ctl->batchlimit = -1;
296 ctl->fetchlimit = atoi(optarg);
297 if (ctl->fetchlimit == 0)
298 ctl->fetchlimit = -1;
302 ctl->mda = xstrdup(optarg);
309 interface_parse(optarg, &ctl->server);
313 ctl->server.monitor = xstrdup(optarg);
333 if (errflag || ocount > 1) {
334 /* squawk if syntax errors were detected */
335 fputs("usage: fetchmail [options] [server ...]\n", stderr);
336 fputs(" Options are as follows:\n",stderr);
337 fputs(" -?, --help display this option help\n", stderr);
338 fputs(" -V, --version display version info\n", stderr);
340 fputs(" -c, --check check for messages without fetching\n", stderr);
341 fputs(" -s, --silent work silently\n", stderr);
342 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
343 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
344 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
345 fputs(" -q, --quit kill daemon process\n", stderr);
346 fputs(" -L, --logfile specify logfile name\n", stderr);
347 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
348 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
349 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
351 fputs(" -I, --interface interface required specification\n",stderr);
352 fputs(" -M, --monitor monitor interface for activity\n",stderr);
356 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
358 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
359 #endif /* KERBEROS_V4 */
360 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
361 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
362 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
363 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
364 fputs(" -E, --envelope envelope address header\n",stderr);
366 fputs(" -u, --username specify users's login on server\n", stderr);
367 fputs(" -a, --all retrieve old and new messages\n", stderr);
368 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
369 fputs(" -k, --keep save new messages after retrieval\n", stderr);
370 fputs(" -F, --flush delete old messages from server\n", stderr);
371 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
372 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
374 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
375 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
376 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
377 fputs(" -r, --folder specify remote folder name\n", stderr);
384 /* options.c ends here */