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"
30 #define LA_PROTOCOL 12
32 #define LA_AUTHENTICATE 14
34 #define LA_USERNAME 16
39 #define LA_NOREWRITE 21
41 #define LA_REMOTEFILE 23
42 #define LA_SMTPHOST 24
43 #define LA_BATCHLIMIT 25
44 #define LA_FETCHLIMIT 26
46 #define LA_INTERFACE 28
50 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:u:akKFnl:r:S:b:B:m:I:M:y";
51 static struct option longoptions[] = {
52 {"help", no_argument, (int *) 0, LA_HELP },
53 {"version", no_argument, (int *) 0, LA_VERSION },
54 {"check", no_argument, (int *) 0, LA_CHECK },
55 {"silent", no_argument, (int *) 0, LA_SILENT },
56 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
57 {"daemon", required_argument, (int *) 0, LA_DAEMON },
58 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
59 {"quit", no_argument, (int *) 0, LA_QUIT },
60 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
61 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
62 {"idfile", required_argument, (int *) 0, LA_IDFILE },
64 {"interface", required_argument, (int *) 0, LA_INTERFACE },
65 {"monitor", required_argument, (int *) 0, LA_MONITOR },
68 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
69 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
70 {"port", required_argument, (int *) 0, LA_PORT },
71 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
72 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
74 {"user", required_argument, (int *) 0, LA_USERNAME },
75 {"username", required_argument, (int *) 0, LA_USERNAME },
77 {"all", no_argument, (int *) 0, LA_ALL },
78 {"kill", no_argument, (int *) 0, LA_KILL },
79 {"keep", no_argument, (int *) 0, LA_KEEP },
80 {"flush", no_argument, (int *) 0, LA_FLUSH },
81 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
82 {"limit", required_argument, (int *) 0, LA_LIMIT },
84 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
85 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
86 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
87 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
88 {"mda", required_argument, (int *) 0, LA_MDA },
90 {"interface", required_argument, (int *) 0, LA_INTERFACE },
91 {"monitor", required_argument, (int *) 0, LA_MONITOR },
93 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
95 {(char *) 0, no_argument, (int *) 0, 0 }
98 int parsecmdline (argc, argv, ctl)
99 /* parse and validate the command line options */
100 int argc; /* argument count */
101 char **argv; /* argument strings */
102 struct query *ctl; /* option record to be initialized */
105 * return value: if positive, argv index of last parsed option + 1
106 * (presumes one or more server names follows). if zero, the
107 * command line switches are such that no server names are
108 * required (e.g. --version). if negative, the command line is
109 * has one or more syntax errors.
113 int ocount = 0; /* count of destinations specified */
114 int errflag = 0; /* TRUE when a syntax error is detected */
117 memset(ctl, '\0', sizeof(struct query)); /* start clean */
121 (c = getopt_long(argc,argv,shortoptions,
122 longoptions,&option_index)) != -1) {
139 outlevel = O_VERBOSE;
143 poll_interval = atoi(optarg);
155 cmd_logfile = optarg;
159 rcfile = (char *) xmalloc(strlen(optarg)+1);
160 strcpy(rcfile,optarg);
164 idfile = (char *) xmalloc(strlen(optarg)+1);
165 strcpy(idfile,optarg);
169 /* XXX -- should probably use a table lookup here */
170 if (strcasecmp(optarg,"pop2") == 0)
171 ctl->protocol = P_POP2;
172 else if (strcasecmp(optarg,"pop3") == 0)
173 ctl->protocol = P_POP3;
174 else if (strcasecmp(optarg,"imap") == 0)
175 ctl->protocol = P_IMAP;
176 else if (strcasecmp(optarg,"apop") == 0)
177 ctl->protocol = P_APOP;
178 else if (strcasecmp(optarg,"kpop") == 0)
180 ctl->protocol = P_POP3;
181 ctl->port = KPOP_PORT;
182 ctl->authenticate = A_KERBEROS;
185 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
191 ctl->port = atoi(optarg);
194 case LA_AUTHENTICATE:
195 if (strcmp(optarg, "password") == 0)
196 ctl->authenticate = A_PASSWORD;
197 else if (strcmp(optarg, "kerberos") == 0)
198 ctl->authenticate = A_KERBEROS;
200 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
205 ctl->timeout = atoi(optarg);
209 strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
214 ctl->fetchall = TRUE;
230 ctl->norewrite = TRUE;
234 ctl->limit = atoi(optarg);
238 strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
242 strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
247 cmd_batchlimit = atoi(optarg);
251 ctl->fetchlimit = atoi(optarg);
255 strncpy(ctl->mda,optarg,sizeof(ctl->mda));
262 cmd_interface = optarg;
266 cmd_monitor = optarg;
282 if (check_only && poll_interval)
284 fputs("The --check and --daemon options aren't compatible.\n", stderr);
288 if (errflag || ocount > 1) {
289 /* squawk if syntax errors were detected */
290 fputs("usage: fetchmail [options] [server ...]\n", stderr);
291 fputs(" Options are as follows:\n",stderr);
292 fputs(" -?, --help display this option help\n", stderr);
293 fputs(" -V, --version display version info\n", stderr);
295 fputs(" -c, --check check for messages without fetching\n", stderr);
296 fputs(" -s, --silent work silently\n", stderr);
297 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
298 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
299 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
300 fputs(" -q, --quit kill daemon process\n", stderr);
301 fputs(" -L, --logfile specify logfile name\n", stderr);
302 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
303 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
305 fputs(" -I, --interface interface required specification\n",stderr);
306 fputs(" -M, --monitor monitor interface for activity\n",stderr);
309 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
310 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
311 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
312 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
314 fputs(" -u, --username specify users's login on server\n", stderr);
315 fputs(" -a, --all retrieve old and new messages\n", stderr);
316 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
317 fputs(" -k, --keep save new messages after retrieval\n", stderr);
318 fputs(" -F, --flush delete old messages from server\n", stderr);
319 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
320 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
322 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
323 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
324 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
325 fputs(" -r, --remote specify remote folder name\n", stderr);
332 /* options.c ends here */