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 /* options still left: CDegGhHjJoOqQRTUwWxXYzZ */
54 static const char *shortoptions =
55 "?Vcsvd:NqL:f:i:p:UP:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
57 static const struct option longoptions[] = {
58 /* this can be const because all flag fields are 0 and will never get set */
59 {"help", no_argument, (int *) 0, LA_HELP },
60 {"version", no_argument, (int *) 0, LA_VERSION },
61 {"check", no_argument, (int *) 0, LA_CHECK },
62 {"silent", no_argument, (int *) 0, LA_SILENT },
63 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
64 {"daemon", required_argument, (int *) 0, LA_DAEMON },
65 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
66 {"quit", no_argument, (int *) 0, LA_QUIT },
67 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
68 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
69 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
70 {"idfile", required_argument, (int *) 0, LA_IDFILE },
72 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
73 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
74 {"uidl", no_argument, (int *) 0, LA_UIDL },
75 {"port", required_argument, (int *) 0, LA_PORT },
76 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
77 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
78 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
80 {"user", required_argument, (int *) 0, LA_USERNAME },
81 {"username", required_argument, (int *) 0, LA_USERNAME },
83 {"all", no_argument, (int *) 0, LA_ALL },
84 {"nokeep", no_argument, (int *) 0, LA_NOKEEP },
85 {"keep", no_argument, (int *) 0, LA_KEEP },
86 {"flush", no_argument, (int *) 0, LA_FLUSH },
87 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
88 {"limit", required_argument, (int *) 0, LA_LIMIT },
90 {"folder", required_argument, (int *) 0, LA_FOLDER },
91 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
92 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
93 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
94 {"mda", required_argument, (int *) 0, LA_MDA },
97 {"interface", required_argument, (int *) 0, LA_INTERFACE },
98 {"monitor", required_argument, (int *) 0, LA_MONITOR },
101 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
103 {(char *) 0, no_argument, (int *) 0, 0 }
106 int parsecmdline (argc, argv, ctl)
107 /* parse and validate the command line options */
108 int argc; /* argument count */
109 char **argv; /* argument strings */
110 struct query *ctl; /* option record to be initialized */
113 * return value: if positive, argv index of last parsed option + 1
114 * (presumes one or more server names follows). if zero, the
115 * command line switches are such that no server names are
116 * required (e.g. --version). if negative, the command line is
117 * has one or more syntax errors.
121 int ocount = 0; /* count of destinations specified */
122 int errflag = 0; /* TRUE when a syntax error is detected */
124 char buf[BUFSIZ], *cp;
128 memset(ctl, '\0', sizeof(struct query)); /* start clean */
129 ctl->smtp_socket = -1;
132 (c = getopt_long(argc,argv,shortoptions,
133 longoptions,&option_index)) != -1) {
150 outlevel = O_VERBOSE;
154 cmd_daemon = atoi(optarg);
166 cmd_logfile = optarg;
170 rcfile = (char *) xmalloc(strlen(optarg)+1);
171 strcpy(rcfile,optarg);
175 idfile = (char *) xmalloc(strlen(optarg)+1);
176 strcpy(idfile,optarg);
180 /* XXX -- should probably use a table lookup here */
181 if (strcasecmp(optarg,"pop2") == 0)
182 ctl->server.protocol = P_POP2;
183 else if (strcasecmp(optarg,"pop3") == 0)
184 ctl->server.protocol = P_POP3;
185 else if (strcasecmp(optarg,"apop") == 0)
186 ctl->server.protocol = P_APOP;
187 else if (strcasecmp(optarg,"rpop") == 0)
188 ctl->server.protocol = P_RPOP;
189 else if (strcasecmp(optarg,"kpop") == 0)
191 ctl->server.protocol = P_POP3;
192 ctl->server.port = KPOP_PORT;
193 ctl->server.preauthenticate = A_KERBEROS_V4;
195 else if (strcasecmp(optarg,"imap") == 0)
196 ctl->server.protocol = P_IMAP;
198 else if (strcasecmp(optarg,"imap-k4") == 0)
199 ctl->server.protocol = P_IMAP_K4;
200 #endif /* KERBEROS_V4 */
201 else if (strcasecmp(optarg,"etrn") == 0)
202 ctl->server.protocol = P_ETRN;
204 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
210 ctl->server.uidl = FLAG_TRUE;
214 ctl->server.port = atoi(optarg);
217 case LA_AUTHENTICATE:
218 if (strcmp(optarg, "password") == 0)
219 ctl->server.preauthenticate = A_PASSWORD;
220 else if (strcmp(optarg, "kerberos") == 0)
221 ctl->server.preauthenticate = A_KERBEROS_V4;
222 else if (strcmp(optarg, "kerberos_v4") == 0)
223 ctl->server.preauthenticate = A_KERBEROS_V4;
225 fprintf(stderr,"Invalid preauthentication `%s' specified.\n", optarg);
231 ctl->server.timeout = atoi(optarg);
232 if (ctl->server.timeout == 0)
233 ctl->server.timeout = -1;
237 ctl->server.envelope = xstrdup(optarg);
242 ctl->remotename = xstrdup(optarg);
246 ctl->fetchall = FLAG_TRUE;
250 ctl->keep = FLAG_FALSE;
254 ctl->keep = FLAG_TRUE;
258 ctl->flush = FLAG_TRUE;
262 ctl->rewrite = FLAG_FALSE;
266 ctl->limit = atoi(optarg);
273 cp = strtok(buf, ",");
275 save_str(&ctl->mailboxes, -1, optarg);
277 ((cp = strtok((char *)NULL, ",")));
282 cp = strtok(buf, ",");
284 save_str(&ctl->smtphunt, -1, optarg);
286 ((cp = strtok((char *)NULL, ",")));
291 ctl->batchlimit = atoi(optarg);
292 if (ctl->batchlimit == 0)
293 ctl->batchlimit = -1;
297 ctl->fetchlimit = atoi(optarg);
298 if (ctl->fetchlimit == 0)
299 ctl->fetchlimit = -1;
303 ctl->mda = xstrdup(optarg);
310 interface_parse(optarg, &ctl->server);
314 ctl->server.monitor = xstrdup(optarg);
334 if (errflag || ocount > 1) {
335 /* squawk if syntax errors were detected */
336 fputs("usage: fetchmail [options] [server ...]\n", stderr);
337 fputs(" Options are as follows:\n",stderr);
338 fputs(" -?, --help display this option help\n", stderr);
339 fputs(" -V, --version display version info\n", stderr);
341 fputs(" -c, --check check for messages without fetching\n", stderr);
342 fputs(" -s, --silent work silently\n", stderr);
343 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
344 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
345 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
346 fputs(" -q, --quit kill daemon process\n", stderr);
347 fputs(" -L, --logfile specify logfile name\n", stderr);
348 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
349 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
350 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
352 fputs(" -I, --interface interface required specification\n",stderr);
353 fputs(" -M, --monitor monitor interface for activity\n",stderr);
357 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn, imap-k4\n", stderr);
359 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
360 #endif /* KERBEROS_V4 */
361 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
362 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
363 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
364 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
365 fputs(" -E, --envelope envelope address header\n",stderr);
367 fputs(" -u, --username specify users's login on server\n", stderr);
368 fputs(" -a, --all retrieve old and new messages\n", stderr);
369 fputs(" -K, --nokeep delete new messages after retrieval\n", stderr);
370 fputs(" -k, --keep save new messages after retrieval\n", stderr);
371 fputs(" -F, --flush delete old messages from server\n", stderr);
372 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
373 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
375 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
376 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
377 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
378 fputs(" -r, --folder specify remote folder name\n", stderr);
385 /* options.c ends here */