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 */
125 (c = getopt_long(argc,argv,shortoptions,
126 longoptions,&option_index)) != -1) {
143 outlevel = O_VERBOSE;
147 poll_interval = atoi(optarg);
159 cmd_logfile = optarg;
163 rcfile = (char *) xmalloc(strlen(optarg)+1);
164 strcpy(rcfile,optarg);
168 idfile = (char *) xmalloc(strlen(optarg)+1);
169 strcpy(idfile,optarg);
173 /* XXX -- should probably use a table lookup here */
174 if (strcasecmp(optarg,"pop2") == 0)
175 ctl->protocol = P_POP2;
176 else if (strcasecmp(optarg,"pop3") == 0)
177 ctl->protocol = P_POP3;
178 else if (strcasecmp(optarg,"imap") == 0)
179 ctl->protocol = P_IMAP;
180 else if (strcasecmp(optarg,"apop") == 0)
181 ctl->protocol = P_APOP;
182 else if (strcasecmp(optarg,"kpop") == 0)
184 ctl->protocol = P_POP3;
185 ctl->port = KPOP_PORT;
186 ctl->authenticate = A_KERBEROS;
189 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
195 ctl->port = atoi(optarg);
198 case LA_AUTHENTICATE:
199 if (strcmp(optarg, "password") == 0)
200 ctl->authenticate = A_PASSWORD;
201 else if (strcmp(optarg, "kerberos") == 0)
202 ctl->authenticate = A_KERBEROS;
204 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
210 ctl->timeout = atoi(optarg);
214 ctl->envelope = xstrdup(optarg);
219 strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
223 ctl->fetchall = TRUE;
239 ctl->norewrite = TRUE;
243 ctl->limit = atoi(optarg);
247 strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
251 strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
256 cmd_batchlimit = atoi(optarg);
260 ctl->fetchlimit = atoi(optarg);
264 strncpy(ctl->mda,optarg,sizeof(ctl->mda));
271 cmd_interface = optarg;
275 cmd_monitor = optarg;
295 if (check_only && poll_interval)
297 fputs("The --check and --daemon options aren't compatible.\n", stderr);
301 if (poll_interval == 0 && use_syslog)
303 fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
307 if (errflag || ocount > 1) {
308 /* squawk if syntax errors were detected */
309 fputs("usage: fetchmail [options] [server ...]\n", stderr);
310 fputs(" Options are as follows:\n",stderr);
311 fputs(" -?, --help display this option help\n", stderr);
312 fputs(" -V, --version display version info\n", stderr);
314 fputs(" -c, --check check for messages without fetching\n", stderr);
315 fputs(" -s, --silent work silently\n", stderr);
316 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
317 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
318 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
319 fputs(" -q, --quit kill daemon process\n", stderr);
320 fputs(" -L, --logfile specify logfile name\n", stderr);
321 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
322 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
323 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
325 fputs(" -I, --interface interface required specification\n",stderr);
326 fputs(" -M, --monitor monitor interface for activity\n",stderr);
329 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
330 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
331 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
332 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
333 fputs(" -E, --envelope envelope address header\n",stderr);
335 fputs(" -u, --username specify users's login on server\n", stderr);
336 fputs(" -a, --all retrieve old and new messages\n", stderr);
337 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
338 fputs(" -k, --keep save new messages after retrieval\n", stderr);
339 fputs(" -F, --flush delete old messages from server\n", stderr);
340 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
341 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
343 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
344 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
345 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
346 fputs(" -r, --remote specify remote folder name\n", stderr);
353 /* options.c ends here */