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_USERNAME 20
43 #define LA_NOREWRITE 25
46 #define LA_SMTPHOST 28
47 #define LA_BATCHLIMIT 29
48 #define LA_FETCHLIMIT 30
51 #define LA_INTERFACE 33
54 #define LA_QVIRTUAL 36
56 /* options still left: CDgGhHjJoORTUwWxXYzZ */
57 static const char *shortoptions =
58 "?Vcsvd:NqL:f:i:p:UP:A:t:E:Q:u:akKFnl:r:S:b:B:e:m:I:M:y";
60 static const struct option longoptions[] = {
61 /* this can be const because all flag fields are 0 and will never get set */
62 {"help", no_argument, (int *) 0, LA_HELP },
63 {"version", no_argument, (int *) 0, LA_VERSION },
64 {"check", no_argument, (int *) 0, LA_CHECK },
65 {"silent", no_argument, (int *) 0, LA_SILENT },
66 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
67 {"daemon", required_argument, (int *) 0, LA_DAEMON },
68 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
69 {"quit", no_argument, (int *) 0, LA_QUIT },
70 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
71 {"invisible", no_argument, (int *) 0, LA_INVISIBLE },
72 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
73 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
74 {"idfile", required_argument, (int *) 0, LA_IDFILE },
76 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
77 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
78 {"uidl", no_argument, (int *) 0, LA_UIDL },
79 {"port", required_argument, (int *) 0, LA_PORT },
80 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
81 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
82 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
83 {"qvirtual", required_argument, (int *) 0, LA_QVIRTUAL },
85 {"user", required_argument, (int *) 0, LA_USERNAME },
86 {"username", required_argument, (int *) 0, LA_USERNAME },
88 {"all", no_argument, (int *) 0, LA_ALL },
89 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
90 {"keep", no_argument, (int *) 0, LA_KEEP },
91 {"flush", no_argument, (int *) 0, LA_FLUSH },
92 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
93 {"limit", required_argument, (int *) 0, LA_LIMIT },
95 {"folder", required_argument, (int *) 0, LA_FOLDER },
96 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
97 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
98 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
99 {"expunge", required_argument, (int *) 0, LA_EXPUNGE },
100 {"mda", required_argument, (int *) 0, LA_MDA },
103 {"interface", required_argument, (int *) 0, LA_INTERFACE },
104 {"monitor", required_argument, (int *) 0, LA_MONITOR },
107 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
109 {(char *) 0, no_argument, (int *) 0, 0 }
112 int parsecmdline (argc, argv, ctl)
113 /* parse and validate the command line options */
114 int argc; /* argument count */
115 char **argv; /* argument strings */
116 struct query *ctl; /* option record to be initialized */
119 * return value: if positive, argv index of last parsed option + 1
120 * (presumes one or more server names follows). if zero, the
121 * command line switches are such that no server names are
122 * required (e.g. --version). if negative, the command line is
123 * has one or more syntax errors.
127 int ocount = 0; /* count of destinations specified */
128 int errflag = 0; /* TRUE when a syntax error is detected */
130 char buf[BUFSIZ], *cp;
134 memset(ctl, '\0', sizeof(struct query)); /* start clean */
135 ctl->smtp_socket = -1;
138 (c = getopt_long(argc,argv,shortoptions,
139 longoptions,&option_index)) != -1) {
156 outlevel = O_VERBOSE;
160 cmd_daemon = atoi(optarg);
172 cmd_logfile = optarg;
175 use_invisible = TRUE;
179 rcfile = (char *) xmalloc(strlen(optarg)+1);
180 strcpy(rcfile,optarg);
184 idfile = (char *) xmalloc(strlen(optarg)+1);
185 strcpy(idfile,optarg);
189 /* XXX -- should probably use a table lookup here */
190 if (strcasecmp(optarg,"pop2") == 0)
191 ctl->server.protocol = P_POP2;
192 else if (strcasecmp(optarg,"pop3") == 0)
193 ctl->server.protocol = P_POP3;
194 else if (strcasecmp(optarg,"apop") == 0)
195 ctl->server.protocol = P_APOP;
196 else if (strcasecmp(optarg,"rpop") == 0)
197 ctl->server.protocol = P_RPOP;
198 else if (strcasecmp(optarg,"kpop") == 0)
200 ctl->server.protocol = P_POP3;
201 ctl->server.port = KPOP_PORT;
202 ctl->server.preauthenticate = A_KERBEROS_V4;
204 else if (strcasecmp(optarg,"imap") == 0)
205 ctl->server.protocol = P_IMAP;
207 else if (strcasecmp(optarg,"imap-k4") == 0)
208 ctl->server.protocol = P_IMAP_K4;
209 #endif /* KERBEROS_V4 */
210 else if (strcasecmp(optarg,"etrn") == 0)
211 ctl->server.protocol = P_ETRN;
213 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
219 ctl->server.uidl = FLAG_TRUE;
223 ctl->server.port = atoi(optarg);
226 case LA_AUTHENTICATE:
227 if (strcmp(optarg, "password") == 0)
228 ctl->server.preauthenticate = A_PASSWORD;
229 else if (strcmp(optarg, "kerberos") == 0)
230 ctl->server.preauthenticate = A_KERBEROS_V4;
231 else if (strcmp(optarg, "kerberos_v4") == 0)
232 ctl->server.preauthenticate = A_KERBEROS_V4;
234 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
240 ctl->server.timeout = atoi(optarg);
241 if (ctl->server.timeout == 0)
242 ctl->server.timeout = -1;
246 ctl->server.envelope = xstrdup(optarg);
250 ctl->server.qvirtual = xstrdup(optarg);
255 ctl->remotename = xstrdup(optarg);
259 ctl->fetchall = FLAG_TRUE;
263 ctl->keep = FLAG_FALSE;
267 ctl->keep = FLAG_TRUE;
271 ctl->flush = FLAG_TRUE;
275 ctl->rewrite = FLAG_FALSE;
279 ctl->limit = atoi(optarg);
286 cp = strtok(buf, ",");
288 save_str(&ctl->mailboxes, -1, cp);
290 ((cp = strtok((char *)NULL, ",")));
295 cp = strtok(buf, ",");
297 save_str(&ctl->smtphunt, TRUE, cp);
299 ((cp = strtok((char *)NULL, ",")));
304 ctl->batchlimit = atoi(optarg);
305 if (ctl->batchlimit == 0)
306 ctl->batchlimit = -1;
310 ctl->fetchlimit = atoi(optarg);
311 if (ctl->fetchlimit == 0)
312 ctl->fetchlimit = -1;
316 ctl->expunge = atoi(optarg);
317 if (ctl->expunge == 0)
322 ctl->mda = xstrdup(optarg);
329 interface_parse(optarg, &ctl->server);
333 ctl->server.monitor = xstrdup(optarg);
353 if (errflag || ocount > 1) {
354 /* squawk if syntax errors were detected */
355 fputs("usage: fetchmail [options] [server ...]\n", stderr);
356 fputs(" Options are as follows:\n",stderr);
357 fputs(" -?, --help display this option help\n", stderr);
358 fputs(" -V, --version display version info\n", stderr);
360 fputs(" -c, --check check for messages without fetching\n", stderr);
361 fputs(" -s, --silent work silently\n", stderr);
362 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
363 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
364 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
365 fputs(" -q, --quit kill daemon process\n", stderr);
366 fputs(" -L, --logfile specify logfile name\n", stderr);
367 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
368 fputs(" --invisible suppress Received line & enable host spoofing\n", stderr);
369 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
370 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
372 fputs(" -I, --interface interface required specification\n",stderr);
373 fputs(" -M, --monitor monitor interface for activity\n",stderr);
377 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
379 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
380 #endif /* KERBEROS_V4 */
381 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
382 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
383 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
384 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
385 fputs(" -E, --envelope envelope address header\n",stderr);
386 fputs(" -Q, --qvirtual prefix to remove from local user id\n",stderr);
388 fputs(" -u, --username specify users's login on server\n", stderr);
389 fputs(" -a, --all retrieve old and new messages\n", stderr);
390 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
391 fputs(" -k, --keep save new messages after retrieval\n", stderr);
392 fputs(" -F, --flush delete old messages from server\n", stderr);
393 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
394 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
396 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
397 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
398 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
399 fputs(" -e, --expunge set max deletions between expunges\n", stderr);
400 fputs(" -r, --folder specify remote folder name\n", stderr);
407 /* options.c ends here */