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
48 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:u:akKFnl:r:S:b:B:m:y";
49 static struct option longoptions[] = {
50 {"help", no_argument, (int *) 0, LA_HELP },
51 {"version", no_argument, (int *) 0, LA_VERSION },
52 {"check", no_argument, (int *) 0, LA_CHECK },
53 {"silent", no_argument, (int *) 0, LA_SILENT },
54 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
55 {"daemon", required_argument, (int *) 0, LA_DAEMON },
56 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
57 {"quit", no_argument, (int *) 0, LA_QUIT },
58 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
59 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
60 {"idfile", required_argument, (int *) 0, LA_IDFILE },
62 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
63 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
64 {"port", required_argument, (int *) 0, LA_PORT },
65 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
66 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
68 {"user", required_argument, (int *) 0, LA_USERNAME },
69 {"username", required_argument, (int *) 0, LA_USERNAME },
71 {"all", no_argument, (int *) 0, LA_ALL },
72 {"kill", no_argument, (int *) 0, LA_KILL },
73 {"keep", no_argument, (int *) 0, LA_KEEP },
74 {"flush", no_argument, (int *) 0, LA_FLUSH },
75 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
76 {"limit", required_argument, (int *) 0, LA_LIMIT },
78 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
79 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
80 {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT },
81 {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT },
82 {"mda", required_argument, (int *) 0, LA_MDA },
84 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
86 {(char *) 0, no_argument, (int *) 0, 0 }
89 int parsecmdline (argc, argv, ctl)
90 /* parse and validate the command line options */
91 int argc; /* argument count */
92 char **argv; /* argument strings */
93 struct query *ctl; /* option record to be initialized */
96 * return value: if positive, argv index of last parsed option + 1
97 * (presumes one or more server names follows). if zero, the
98 * command line switches are such that no server names are
99 * required (e.g. --version). if negative, the command line is
100 * has one or more syntax errors.
104 int ocount = 0; /* count of destinations specified */
105 int errflag = 0; /* TRUE when a syntax error is detected */
108 memset(ctl, '\0', sizeof(struct query)); /* start clean */
112 (c = getopt_long(argc,argv,shortoptions,
113 longoptions,&option_index)) != -1) {
130 outlevel = O_VERBOSE;
134 poll_interval = atoi(optarg);
146 cmd_logfile = optarg;
150 rcfile = (char *) xmalloc(strlen(optarg)+1);
151 strcpy(rcfile,optarg);
155 idfile = (char *) xmalloc(strlen(optarg)+1);
156 strcpy(idfile,optarg);
160 /* XXX -- should probably use a table lookup here */
161 if (strcasecmp(optarg,"pop2") == 0)
162 ctl->protocol = P_POP2;
163 else if (strcasecmp(optarg,"pop3") == 0)
164 ctl->protocol = P_POP3;
165 else if (strcasecmp(optarg,"imap") == 0)
166 ctl->protocol = P_IMAP;
167 else if (strcasecmp(optarg,"apop") == 0)
168 ctl->protocol = P_APOP;
169 else if (strcasecmp(optarg,"kpop") == 0)
171 ctl->protocol = P_POP3;
172 ctl->port = KPOP_PORT;
173 ctl->authenticate = A_KERBEROS;
176 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
182 ctl->port = atoi(optarg);
185 case LA_AUTHENTICATE:
186 if (strcmp(optarg, "password") == 0)
187 ctl->authenticate = A_PASSWORD;
188 else if (strcmp(optarg, "kerberos") == 0)
189 ctl->authenticate = A_KERBEROS;
191 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
196 ctl->timeout = atoi(optarg);
200 strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
205 ctl->fetchall = TRUE;
221 ctl->norewrite = TRUE;
225 ctl->limit = atoi(optarg);
229 strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
233 strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
238 cmd_batchlimit = atoi(optarg);
242 ctl->fetchlimit = atoi(optarg);
246 strncpy(ctl->mda,optarg,sizeof(ctl->mda));
261 if (check_only && poll_interval)
263 fputs("The --check and --daemon options aren't compatible.\n", stderr);
267 if (errflag || ocount > 1) {
268 /* squawk if syntax errors were detected */
269 fputs("usage: fetchmail [options] [server ...]\n", stderr);
270 fputs(" Options are as follows:\n",stderr);
271 fputs(" -?, --help display this option help\n", stderr);
272 fputs(" -V, --version display version info\n", stderr);
274 fputs(" -c, --check check for messages without fetching\n", stderr);
275 fputs(" -s, --silent work silently\n", stderr);
276 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
277 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
278 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
279 fputs(" -q, --quit kill daemon process\n", stderr);
280 fputs(" -L, --logfile specify logfile name\n", stderr);
281 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
282 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
284 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
285 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
286 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
287 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
289 fputs(" -u, --username specify users's login on server\n", stderr);
290 fputs(" -a, --all retrieve old and new messages\n", stderr);
291 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
292 fputs(" -k, --keep save new messages after retrieval\n", stderr);
293 fputs(" -F, --flush delete old messages from server\n", stderr);
294 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
295 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
297 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
298 fputs(" -b, --batchlimit set batch limit for SMTP connections\n", stderr);
299 fputs(" -B, --fetchlimit set fetch limit for server connections\n", stderr);
300 fputs(" -r, --remote specify remote folder name\n", stderr);
307 /* options.c ends here */