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"
28 #define LA_INVISIBLE 10
32 #define LA_PROTOCOL 14
35 #define LA_AUTHENTICATE 17
37 #define LA_ENVELOPE 19
38 #define LA_QVIRTUAL 20
39 #define LA_USERNAME 21
44 #define LA_NOREWRITE 26
47 #define LA_SMTPHOST 29
48 #define LA_SMTPADDR 30
49 #define LA_BATCHLIMIT 31
50 #define LA_FETCHLIMIT 32
54 #define LA_INTERFACE 36
58 /* options still left: CgGhHjJoORUwWxXYzZ */
59 static const char *shortoptions =
60 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:b:B:e:m:I:M:y";
62 static const struct option longoptions[] = {
63 /* this can be const because all flag fields are 0 and will never get set */
64 {"help", no_argument, (int *) 0, LA_HELP },
65 {"version", no_argument, (int *) 0, LA_VERSION },
66 {"check", no_argument, (int *) 0, LA_CHECK },
67 {"silent", no_argument, (int *) 0, LA_SILENT },
68 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
69 {"daemon", required_argument, (int *) 0, LA_DAEMON },
70 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
71 {"quit", no_argument, (int *) 0, LA_QUIT },
72 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
73 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
74 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
75 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
76 {"idfile", required_argument, (int *) 0, LA_IDFILE },
78 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
79 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
80 {"uidl", no_argument, (int *) 0, LA_UIDL },
81 {"port", required_argument, (int *) 0, LA_PORT },
82 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
83 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
84 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
85 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
87 {"user", required_argument, (int *) 0, LA_USERNAME },
88 {"username", required_argument, (int *) 0, LA_USERNAME },
90 {"all", no_argument, (int *) 0, LA_ALL },
91 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
92 {"keep", no_argument, (int *) 0, LA_KEEP },
93 {"flush", no_argument, (int *) 0, LA_FLUSH },
94 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
95 {"limit", required_argument, (int *) 0, LA_LIMIT },
97 {"folder", required_argument, (int *) 0, LA_FOLDER },
98 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
99 {"smtpaddress", required_argument, (int *) 0, LA_SMTPADDR },
101 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
102 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
103 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
104 {"mda", required_argument, (int *) 0, LA_MDA },
107 {"netsec", required_argument, (int *) 0, LA_NETSEC },
110 #if defined(linux) && !INET6
111 {"interface", required_argument, (int *) 0, LA_INTERFACE },
112 {"monitor", required_argument, (int *) 0, LA_MONITOR },
113 #endif /* defined(linux) && !INET6 */
115 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
117 {(char *) 0, no_argument, (int *) 0, 0 }
120 int parsecmdline (argc, argv, ctl)
121 /* parse and validate the command line options */
122 int argc; /* argument count */
123 char **argv; /* argument strings */
124 struct query *ctl; /* option record to be initialized */
127 * return value: if positive, argv index of last parsed option + 1
128 * (presumes one or more server names follows). if zero, the
129 * command line switches are such that no server names are
130 * required (e.g. --version). if negative, the command line is
131 * has one or more syntax errors.
135 int ocount = 0; /* count of destinations specified */
136 int errflag = 0; /* TRUE when a syntax error is detected */
138 char buf[BUFSIZ], *cp;
142 memset(ctl, '\0', sizeof(struct query)); /* start clean */
143 ctl->smtp_socket = -1;
146 (c = getopt_long(argc,argv,shortoptions,
147 longoptions,&option_index)) != -1) {
164 outlevel = O_VERBOSE;
168 cmd_daemon = atoi(optarg);
180 cmd_logfile = optarg;
183 use_invisible = TRUE;
187 rcfile = (char *) xmalloc(strlen(optarg)+1);
188 strcpy(rcfile,optarg);
192 idfile = (char *) xmalloc(strlen(optarg)+1);
193 strcpy(idfile,optarg);
197 /* XXX -- should probably use a table lookup here */
198 if (strcasecmp(optarg,"pop2") == 0)
199 ctl->server.protocol = P_POP2;
200 else if (strcasecmp(optarg,"pop3") == 0)
201 ctl->server.protocol = P_POP3;
202 else if (strcasecmp(optarg,"apop") == 0)
203 ctl->server.protocol = P_APOP;
204 else if (strcasecmp(optarg,"rpop") == 0)
205 ctl->server.protocol = P_RPOP;
206 else if (strcasecmp(optarg,"kpop") == 0)
208 ctl->server.protocol = P_POP3;
210 ctl->server.service = KPOP_PORT;
212 ctl->server.port = KPOP_PORT;
215 ctl->server.preauthenticate = A_KERBEROS_V5;
217 ctl->server.preauthenticate = A_KERBEROS_V4;
218 #endif /* KERBEROS_V5 */
220 else if (strcasecmp(optarg,"imap") == 0)
221 ctl->server.protocol = P_IMAP;
223 else if (strcasecmp(optarg,"imap-k4") == 0)
224 ctl->server.protocol = P_IMAP_K4;
225 #endif /* KERBEROS_V4 */
226 else if (strcasecmp(optarg,"etrn") == 0)
227 ctl->server.protocol = P_ETRN;
229 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
235 ctl->server.uidl = FLAG_TRUE;
240 ctl->server.service = optarg;
242 ctl->server.port = atoi(optarg);
246 case LA_AUTHENTICATE:
247 if (strcmp(optarg, "password") == 0)
248 ctl->server.preauthenticate = A_PASSWORD;
249 else if (strcmp(optarg, "kerberos") == 0)
251 ctl->server.preauthenticate = A_KERBEROS_V5;
252 else if (strcmp(optarg, "kerberos_v5") == 0)
253 ctl->server.preauthenticate = A_KERBEROS_V5;
255 ctl->server.preauthenticate = A_KERBEROS_V4;
256 else if (strcmp(optarg, "kerberos_v4") == 0)
257 ctl->server.preauthenticate = A_KERBEROS_V4;
258 #endif /* KERBEROS_V5 */
260 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
266 ctl->server.timeout = atoi(optarg);
267 if (ctl->server.timeout == 0)
268 ctl->server.timeout = -1;
272 ctl->server.envelope = xstrdup(optarg);
276 ctl->server.qvirtual = xstrdup(optarg);
281 ctl->remotename = xstrdup(optarg);
285 ctl->fetchall = FLAG_TRUE;
289 ctl->keep = FLAG_FALSE;
293 ctl->keep = FLAG_TRUE;
297 ctl->flush = FLAG_TRUE;
301 ctl->rewrite = FLAG_FALSE;
306 ctl->limit = NUM_VALUE(c);
311 cp = strtok(buf, ",");
313 save_str(&ctl->mailboxes, -1, cp);
315 ((cp = strtok((char *)NULL, ",")));
320 cp = strtok(buf, ",");
322 save_str(&ctl->smtphunt, TRUE, cp);
324 ((cp = strtok((char *)NULL, ",")));
329 ctl->smtpaddress = xstrdup(optarg);
334 ctl->batchlimit = NUM_VALUE(c);
339 ctl->fetchlimit = NUM_VALUE(c);
344 ctl->expunge = NUM_VALUE(c);
348 ctl->mda = xstrdup(optarg);
355 ctl->server.netsec = (void *)optarg;
357 fprintf(stderr, "fetchmail: network security support is disabled\n");
359 #endif /* NET_SECURITY */
362 #if defined(linux) && !INET6
365 interface_parse(optarg, &ctl->server);
369 ctl->server.monitor = xstrdup(optarg);
371 #endif /* defined(linux) && !INET6 */
379 errors_to_syslog = TRUE;
389 if (errflag || ocount > 1) {
390 /* squawk if syntax errors were detected */
391 fputs("usage: fetchmail [options] [server ...]\n", stderr);
392 fputs(" Options are as follows:\n",stderr);
393 fputs(" -?, --help display this option help\n", stderr);
394 fputs(" -V, --version display version info\n", stderr);
396 fputs(" -c, --check check for messages without fetching\n", stderr);
397 fputs(" -s, --silent work silently\n", stderr);
398 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
399 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
400 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
401 fputs(" -q, --quit kill daemon process\n", stderr);
402 fputs(" -L, --logfile specify logfile name\n", stderr);
403 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
404 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
405 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
406 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
407 #if defined(linux) && !INET6
408 fputs(" -I, --interface interface required specification\n",stderr);
409 fputs(" -M, --monitor monitor interface for activity\n",stderr);
413 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
415 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
416 #endif /* KERBEROS_V4 */
417 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
418 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
419 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
420 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
421 fputs(" -E, --envelope envelope address header\n",stderr);
422 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
424 fputs(" -u, --username specify users's login on server\n", stderr);
425 fputs(" -a, --all retrieve old and new messages\n", stderr);
426 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
427 fputs(" -k, --keep save new messages after retrieval\n", stderr);
428 fputs(" -F, --flush delete old messages from server\n", stderr);
429 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
430 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
433 fputs(" -T, --netsec set IP security request\n", stderr);
434 #endif /* NET_SECURITY */
435 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
436 fputs(" -D, --smtpaddress set SMTP delivery domain to use\n", stderr);
437 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
438 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
439 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
440 fputs(" -r, --folder specify remote folder name\n", stderr);
447 /* options.c ends here */