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