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
44 #define LA_REMOTEFILE 26
45 #define LA_SMTPHOST 27
46 #define LA_BATCHLIMIT 28
47 #define LA_FETCHLIMIT 29
49 #define LA_INTERFACE 31
53 static char *shortoptions = "?Vcsvd:NqL:f:i:p:UP:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
54 static struct option longoptions[] = {
55 {"help", no_argument, (int *) 0, LA_HELP },
56 {"version", no_argument, (int *) 0, LA_VERSION },
57 {"check", no_argument, (int *) 0, LA_CHECK },
58 {"silent", no_argument, (int *) 0, LA_SILENT },
59 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
60 {"daemon", required_argument, (int *) 0, LA_DAEMON },
61 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
62 {"quit", no_argument, (int *) 0, LA_QUIT },
63 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
64 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
65 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
66 {"idfile", required_argument, (int *) 0, LA_IDFILE },
68 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
69 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
70 {"uidl", no_argument, (int *) 0, LA_UIDL },
71 {"port", required_argument, (int *) 0, LA_PORT },
72 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
73 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
74 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
76 {"user", required_argument, (int *) 0, LA_USERNAME },
77 {"username", required_argument, (int *) 0, LA_USERNAME },
79 {"all", no_argument, (int *) 0, LA_ALL },
80 {"kill", no_argument, (int *) 0, LA_KILL },
81 {"keep", no_argument, (int *) 0, LA_KEEP },
82 {"flush", no_argument, (int *) 0, LA_FLUSH },
83 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
84 {"limit", required_argument, (int *) 0, LA_LIMIT },
86 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
87 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
88 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
89 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
90 {"mda", required_argument, (int *) 0, LA_MDA },
93 {"interface", required_argument, (int *) 0, LA_INTERFACE },
94 {"monitor", required_argument, (int *) 0, LA_MONITOR },
97 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
99 {(char *) 0, no_argument, (int *) 0, 0 }
102 int parsecmdline (argc, argv, ctl)
103 /* parse and validate the command line options */
104 int argc; /* argument count */
105 char **argv; /* argument strings */
106 struct query *ctl; /* option record to be initialized */
109 * return value: if positive, argv index of last parsed option + 1
110 * (presumes one or more server names follows). if zero, the
111 * command line switches are such that no server names are
112 * required (e.g. --version). if negative, the command line is
113 * has one or more syntax errors.
117 int ocount = 0; /* count of destinations specified */
118 int errflag = 0; /* TRUE when a syntax error is detected */
123 memset(ctl, '\0', sizeof(struct query)); /* start clean */
126 (c = getopt_long(argc,argv,shortoptions,
127 longoptions,&option_index)) != -1) {
144 outlevel = O_VERBOSE;
148 cmd_daemon = atoi(optarg);
160 cmd_logfile = optarg;
164 rcfile = (char *) xmalloc(strlen(optarg)+1);
165 strcpy(rcfile,optarg);
169 idfile = (char *) xmalloc(strlen(optarg)+1);
170 strcpy(idfile,optarg);
174 /* XXX -- should probably use a table lookup here */
175 if (strcasecmp(optarg,"pop2") == 0)
176 ctl->server.protocol = P_POP2;
177 else if (strcasecmp(optarg,"pop3") == 0)
178 ctl->server.protocol = P_POP3;
179 else if (strcasecmp(optarg,"imap") == 0)
180 ctl->server.protocol = P_IMAP;
181 else if (strcasecmp(optarg,"apop") == 0)
182 ctl->server.protocol = P_APOP;
183 else if (strcasecmp(optarg,"rpop") == 0)
184 ctl->server.protocol = P_RPOP;
185 else if (strcasecmp(optarg,"kpop") == 0)
187 ctl->server.protocol = P_POP3;
188 ctl->server.port = KPOP_PORT;
189 ctl->server.authenticate = A_KERBEROS;
191 else if (strcasecmp(optarg,"etrn") == 0)
192 ctl->server.protocol = P_ETRN;
194 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
200 ctl->server.uidl = FLAG_TRUE;
204 ctl->server.port = atoi(optarg);
207 case LA_AUTHENTICATE:
208 if (strcmp(optarg, "password") == 0)
209 ctl->server.authenticate = A_PASSWORD;
210 else if (strcmp(optarg, "kerberos") == 0)
211 ctl->server.authenticate = A_KERBEROS;
213 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
219 ctl->server.timeout = atoi(optarg);
223 ctl->server.envelope = xstrdup(optarg);
228 ctl->remotename = xstrdup(optarg);
232 ctl->fetchall = FLAG_TRUE;
236 ctl->keep = FLAG_FALSE;
240 ctl->keep = FLAG_TRUE;
244 ctl->flush = FLAG_TRUE;
248 ctl->rewrite = FLAG_FALSE;
252 ctl->limit = atoi(optarg);
256 ctl->mailbox = xstrdup(optarg);
260 save_str(&ctl->smtphunt, -1, optarg);
265 ctl->batchlimit = atoi(optarg);
269 ctl->fetchlimit = atoi(optarg);
273 ctl->mda = xstrdup(optarg);
280 interface_parse(optarg, &ctl->server);
284 ctl->server.monitor = xstrdup(optarg);
304 if (errflag || ocount > 1) {
305 /* squawk if syntax errors were detected */
306 fputs("usage: fetchmail [options] [server ...]\n", stderr);
307 fputs(" Options are as follows:\n",stderr);
308 fputs(" -?, --help display this option help\n", stderr);
309 fputs(" -V, --version display version info\n", stderr);
311 fputs(" -c, --check check for messages without fetching\n", stderr);
312 fputs(" -s, --silent work silently\n", stderr);
313 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
314 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
315 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
316 fputs(" -q, --quit kill daemon process\n", stderr);
317 fputs(" -L, --logfile specify logfile name\n", stderr);
318 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
319 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
320 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
322 fputs(" -I, --interface interface required specification\n",stderr);
323 fputs(" -M, --monitor monitor interface for activity\n",stderr);
326 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop, etrn\n", stderr);
327 fputs(" -U, --uidl force the use of UIDLs (pop3 only)\n", stderr);
328 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
329 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
330 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
331 fputs(" -E, --envelope envelope address header\n",stderr);
333 fputs(" -u, --username specify users's login on server\n", stderr);
334 fputs(" -a, --all retrieve old and new messages\n", stderr);
335 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
336 fputs(" -k, --keep save new messages after retrieval\n", stderr);
337 fputs(" -F, --flush delete old messages from server\n", stderr);
338 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
339 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
341 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
342 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
343 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
344 fputs(" -r, --remote specify remote folder name\n", stderr);
351 /* options.c ends here */