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
33 #define LA_AUTHENTICATE 15
35 #define LA_ENVELOPE 17
36 #define LA_USERNAME 18
41 #define LA_NOREWRITE 23
43 #define LA_REMOTEFILE 25
44 #define LA_SMTPHOST 26
45 #define LA_BATCHLIMIT 27
46 #define LA_FETCHLIMIT 28
48 #define LA_INTERFACE 30
52 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
53 static struct option longoptions[] = {
54 {"help", no_argument, (int *) 0, LA_HELP },
55 {"version", no_argument, (int *) 0, LA_VERSION },
56 {"check", no_argument, (int *) 0, LA_CHECK },
57 {"silent", no_argument, (int *) 0, LA_SILENT },
58 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
59 {"daemon", required_argument, (int *) 0, LA_DAEMON },
60 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
61 {"quit", no_argument, (int *) 0, LA_QUIT },
62 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
63 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
64 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
65 {"idfile", required_argument, (int *) 0, LA_IDFILE },
67 {"interface", required_argument, (int *) 0, LA_INTERFACE },
68 {"monitor", required_argument, (int *) 0, LA_MONITOR },
71 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
72 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
73 {"port", required_argument, (int *) 0, LA_PORT },
74 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
75 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
76 {"envelope", required_argument, (int *) 0, LA_ENVELOPE },
78 {"user", required_argument, (int *) 0, LA_USERNAME },
79 {"username", required_argument, (int *) 0, LA_USERNAME },
81 {"all", no_argument, (int *) 0, LA_ALL },
82 {"kill", no_argument, (int *) 0, LA_KILL },
83 {"keep", no_argument, (int *) 0, LA_KEEP },
84 {"flush", no_argument, (int *) 0, LA_FLUSH },
85 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
86 {"limit", required_argument, (int *) 0, LA_LIMIT },
88 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
89 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
90 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
91 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
92 {"mda", required_argument, (int *) 0, LA_MDA },
94 {"interface", required_argument, (int *) 0, LA_INTERFACE },
95 {"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 */
121 memset(ctl, '\0', sizeof(struct query)); /* start clean */
124 (c = getopt_long(argc,argv,shortoptions,
125 longoptions,&option_index)) != -1) {
142 outlevel = O_VERBOSE;
146 poll_interval = atoi(optarg);
158 cmd_logfile = optarg;
162 rcfile = (char *) xmalloc(strlen(optarg)+1);
163 strcpy(rcfile,optarg);
167 idfile = (char *) xmalloc(strlen(optarg)+1);
168 strcpy(idfile,optarg);
172 /* XXX -- should probably use a table lookup here */
173 if (strcasecmp(optarg,"pop2") == 0)
174 ctl->server.protocol = P_POP2;
175 else if (strcasecmp(optarg,"pop3") == 0)
176 ctl->server.protocol = P_POP3;
177 else if (strcasecmp(optarg,"imap") == 0)
178 ctl->server.protocol = P_IMAP;
179 else if (strcasecmp(optarg,"apop") == 0)
180 ctl->server.protocol = P_APOP;
181 else if (strcasecmp(optarg,"kpop") == 0)
183 ctl->server.protocol = P_POP3;
184 ctl->server.port = KPOP_PORT;
185 ctl->server.authenticate = A_KERBEROS;
188 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
194 ctl->server.port = atoi(optarg);
197 case LA_AUTHENTICATE:
198 if (strcmp(optarg, "password") == 0)
199 ctl->server.authenticate = A_PASSWORD;
200 else if (strcmp(optarg, "kerberos") == 0)
201 ctl->server.authenticate = A_KERBEROS;
203 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
209 ctl->server.timeout = atoi(optarg);
213 ctl->server.envelope = xstrdup(optarg);
218 ctl->remotename = xstrdup(optarg);
222 ctl->fetchall = TRUE;
238 ctl->no_rewrite = TRUE;
242 ctl->limit = atoi(optarg);
246 ctl->mailbox = xstrdup(optarg);
250 ctl->smtphost = xstrdup(optarg);
255 ctl->batchlimit = atoi(optarg);
259 ctl->fetchlimit = atoi(optarg);
263 ctl->mda = xstrdup(optarg);
270 ctl->server.interface = xstrdup(optarg);
274 ctl->server.monitor = xstrdup(optarg);
294 if (check_only && poll_interval)
296 fputs("The --check and --daemon options aren't compatible.\n", stderr);
300 if (poll_interval == 0 && use_syslog)
302 fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
306 if (errflag || ocount > 1) {
307 /* squawk if syntax errors were detected */
308 fputs("usage: fetchmail [options] [server ...]\n", stderr);
309 fputs(" Options are as follows:\n",stderr);
310 fputs(" -?, --help display this option help\n", stderr);
311 fputs(" -V, --version display version info\n", stderr);
313 fputs(" -c, --check check for messages without fetching\n", stderr);
314 fputs(" -s, --silent work silently\n", stderr);
315 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
316 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
317 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
318 fputs(" -q, --quit kill daemon process\n", stderr);
319 fputs(" -L, --logfile specify logfile name\n", stderr);
320 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
321 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
322 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
324 fputs(" -I, --interface interface required specification\n",stderr);
325 fputs(" -M, --monitor monitor interface for activity\n",stderr);
328 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
329 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
330 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
331 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
332 fputs(" -E, --envelope envelope address header\n",stderr);
334 fputs(" -u, --username specify users's login on server\n", stderr);
335 fputs(" -a, --all retrieve old and new messages\n", stderr);
336 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
337 fputs(" -k, --keep save new messages after retrieval\n", stderr);
338 fputs(" -F, --flush delete old messages from server\n", stderr);
339 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
340 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
342 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
343 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
344 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
345 fputs(" -r, --remote specify remote folder name\n", stderr);
352 /* options.c ends here */