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_USERNAME 17
40 #define LA_NOREWRITE 22
42 #define LA_REMOTEFILE 24
43 #define LA_SMTPHOST 25
44 #define LA_BATCHLIMIT 26
45 #define LA_FETCHLIMIT 27
47 #define LA_INTERFACE 29
51 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:u:akKFnl:r:S:b:B:m:I:M:y";
52 static struct option longoptions[] = {
53 {"help", no_argument, (int *) 0, LA_HELP },
54 {"version", no_argument, (int *) 0, LA_VERSION },
55 {"check", no_argument, (int *) 0, LA_CHECK },
56 {"silent", no_argument, (int *) 0, LA_SILENT },
57 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
58 {"daemon", required_argument, (int *) 0, LA_DAEMON },
59 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
60 {"quit", no_argument, (int *) 0, LA_QUIT },
61 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
62 {"syslog", no_argument, (int *) 0, LA_SYSLOG },
63 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
64 {"idfile", required_argument, (int *) 0, LA_IDFILE },
66 {"interface", required_argument, (int *) 0, LA_INTERFACE },
67 {"monitor", required_argument, (int *) 0, LA_MONITOR },
70 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
71 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
72 {"port", required_argument, (int *) 0, LA_PORT },
73 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
74 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
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 },
92 {"interface", required_argument, (int *) 0, LA_INTERFACE },
93 {"monitor", required_argument, (int *) 0, LA_MONITOR },
95 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
97 {(char *) 0, no_argument, (int *) 0, 0 }
100 int parsecmdline (argc, argv, ctl)
101 /* parse and validate the command line options */
102 int argc; /* argument count */
103 char **argv; /* argument strings */
104 struct query *ctl; /* option record to be initialized */
107 * return value: if positive, argv index of last parsed option + 1
108 * (presumes one or more server names follows). if zero, the
109 * command line switches are such that no server names are
110 * required (e.g. --version). if negative, the command line is
111 * has one or more syntax errors.
115 int ocount = 0; /* count of destinations specified */
116 int errflag = 0; /* TRUE when a syntax error is detected */
119 memset(ctl, '\0', sizeof(struct query)); /* start clean */
123 (c = getopt_long(argc,argv,shortoptions,
124 longoptions,&option_index)) != -1) {
141 outlevel = O_VERBOSE;
145 poll_interval = atoi(optarg);
157 cmd_logfile = optarg;
161 rcfile = (char *) xmalloc(strlen(optarg)+1);
162 strcpy(rcfile,optarg);
166 idfile = (char *) xmalloc(strlen(optarg)+1);
167 strcpy(idfile,optarg);
171 /* XXX -- should probably use a table lookup here */
172 if (strcasecmp(optarg,"pop2") == 0)
173 ctl->protocol = P_POP2;
174 else if (strcasecmp(optarg,"pop3") == 0)
175 ctl->protocol = P_POP3;
176 else if (strcasecmp(optarg,"imap") == 0)
177 ctl->protocol = P_IMAP;
178 else if (strcasecmp(optarg,"apop") == 0)
179 ctl->protocol = P_APOP;
180 else if (strcasecmp(optarg,"kpop") == 0)
182 ctl->protocol = P_POP3;
183 ctl->port = KPOP_PORT;
184 ctl->authenticate = A_KERBEROS;
187 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
193 ctl->port = atoi(optarg);
196 case LA_AUTHENTICATE:
197 if (strcmp(optarg, "password") == 0)
198 ctl->authenticate = A_PASSWORD;
199 else if (strcmp(optarg, "kerberos") == 0)
200 ctl->authenticate = A_KERBEROS;
202 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
207 ctl->timeout = atoi(optarg);
211 strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
216 ctl->fetchall = TRUE;
232 ctl->norewrite = TRUE;
236 ctl->limit = atoi(optarg);
240 strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
244 strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
249 cmd_batchlimit = atoi(optarg);
253 ctl->fetchlimit = atoi(optarg);
257 strncpy(ctl->mda,optarg,sizeof(ctl->mda));
264 cmd_interface = optarg;
268 cmd_monitor = optarg;
288 if (check_only && poll_interval)
290 fputs("The --check and --daemon options aren't compatible.\n", stderr);
294 if (poll_interval == 0 && use_syslog)
296 fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
300 if (errflag || ocount > 1) {
301 /* squawk if syntax errors were detected */
302 fputs("usage: fetchmail [options] [server ...]\n", stderr);
303 fputs(" Options are as follows:\n",stderr);
304 fputs(" -?, --help display this option help\n", stderr);
305 fputs(" -V, --version display version info\n", stderr);
307 fputs(" -c, --check check for messages without fetching\n", stderr);
308 fputs(" -s, --silent work silently\n", stderr);
309 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
310 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
311 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
312 fputs(" -q, --quit kill daemon process\n", stderr);
313 fputs(" -L, --logfile specify logfile name\n", stderr);
314 fputs(" --syslog use syslog(3) for most messages when running as a daemon\n", stderr);
315 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
316 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
318 fputs(" -I, --interface interface required specification\n",stderr);
319 fputs(" -M, --monitor monitor interface for activity\n",stderr);
322 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
323 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
324 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
325 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
327 fputs(" -u, --username specify users's login on server\n", stderr);
328 fputs(" -a, --all retrieve old and new messages\n", stderr);
329 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
330 fputs(" -k, --keep save new messages after retrieval\n", stderr);
331 fputs(" -F, --flush delete old messages from server\n", stderr);
332 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
333 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
335 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
336 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
337 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
338 fputs(" -r, --remote specify remote folder name\n", stderr);
345 /* options.c ends here */