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
46 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:u:akKFnl:r:S:m:y";
47 static struct option longoptions[] = {
48 {"help", no_argument, (int *) 0, LA_HELP },
49 {"version", no_argument, (int *) 0, LA_VERSION },
50 {"check", no_argument, (int *) 0, LA_CHECK },
51 {"silent", no_argument, (int *) 0, LA_SILENT },
52 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
53 {"daemon", required_argument, (int *) 0, LA_DAEMON },
54 {"nodetach", no_argument, (int *) 0, LA_NODETACH },
55 {"quit", no_argument, (int *) 0, LA_QUIT },
56 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
57 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
58 {"idfile", required_argument, (int *) 0, LA_IDFILE },
60 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
61 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
62 {"port", required_argument, (int *) 0, LA_PORT },
63 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
64 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
66 {"user", required_argument, (int *) 0, LA_USERNAME },
67 {"username", required_argument, (int *) 0, LA_USERNAME },
69 {"all", no_argument, (int *) 0, LA_ALL },
70 {"kill", no_argument, (int *) 0, LA_KILL },
71 {"keep", no_argument, (int *) 0, LA_KEEP },
72 {"flush", no_argument, (int *) 0, LA_FLUSH },
73 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
74 {"limit", required_argument, (int *) 0, LA_LIMIT },
76 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
77 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
78 {"mda", required_argument, (int *) 0, LA_MDA },
80 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
82 {(char *) 0, no_argument, (int *) 0, 0 }
85 int parsecmdline (argc,argv,ctl)
86 /* parse and validate the command line options */
87 int argc; /* argument count */
88 char **argv; /* argument strings */
89 struct query *ctl; /* option record to be initialized */
92 * return value: if positive, argv index of last parsed option + 1
93 * (presumes one or more server names follows). if zero, the
94 * command line switches are such that no server names are
95 * required (e.g. --version). if negative, the command line is
96 * has one or more syntax errors.
100 int ocount = 0; /* count of destinations specified */
101 int errflag = 0; /* TRUE when a syntax error is detected */
104 memset(ctl, '\0', sizeof(struct query)); /* start clean */
107 (c = getopt_long(argc,argv,shortoptions,
108 longoptions,&option_index)) != -1) {
125 outlevel = O_VERBOSE;
129 poll_interval = atoi(optarg);
130 ctl->fetchall = TRUE;
146 rcfile = (char *) xmalloc(strlen(optarg)+1);
147 strcpy(rcfile,optarg);
151 idfile = (char *) xmalloc(strlen(optarg)+1);
152 strcpy(idfile,optarg);
156 /* XXX -- should probably use a table lookup here */
157 if (strcasecmp(optarg,"pop2") == 0)
158 ctl->protocol = P_POP2;
159 else if (strcasecmp(optarg,"pop3") == 0)
160 ctl->protocol = P_POP3;
161 else if (strcasecmp(optarg,"imap") == 0)
162 ctl->protocol = P_IMAP;
163 else if (strcasecmp(optarg,"apop") == 0)
164 ctl->protocol = P_APOP;
165 else if (strcasecmp(optarg,"kpop") == 0)
167 ctl->protocol = P_POP3;
168 ctl->port = KPOP_PORT;
169 ctl->authenticate = A_KERBEROS;
172 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
178 ctl->port = atoi(optarg);
181 case LA_AUTHENTICATE:
182 if (strcmp(optarg, "password") == 0)
183 ctl->authenticate = A_PASSWORD;
184 else if (strcmp(optarg, "kerberos") == 0)
185 ctl->authenticate = A_KERBEROS;
187 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
192 ctl->timeout = atoi(optarg);
196 strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
201 ctl->fetchall = TRUE;
217 ctl->norewrite = TRUE;
221 ctl->limit = atoi(optarg);
225 strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
229 strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
234 strncpy(ctl->mda,optarg,sizeof(ctl->mda));
249 if (check_only && poll_interval)
251 fputs("The --check and --daemon options aren't compatible.\n", stderr);
255 if (errflag || ocount > 1) {
256 /* squawk if syntax errors were detected */
257 fputs("usage: fetchmail [options] [server ...]\n", stderr);
258 fputs(" Options are as follows:\n",stderr);
259 fputs(" -?, --help display this option help\n", stderr);
260 fputs(" -V, --version display version info\n", stderr);
262 fputs(" -c, --check check for messages without fetching\n", stderr);
263 fputs(" -s, --silent work silently\n", stderr);
264 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
265 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
266 fputs(" -N, --nodetach don't detach daemon process\n", stderr);
267 fputs(" -q, --quit kill daemon process\n", stderr);
268 fputs(" -L, --logfile specify logfile name\n", stderr);
269 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
270 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
272 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
273 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
274 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
275 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
277 fputs(" -u, --username specify users's login on server\n", stderr);
278 fputs(" -a, --all retrieve old and new messages\n", stderr);
279 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
280 fputs(" -k, --keep save new messages after retrieval\n", stderr);
281 fputs(" -F, --flush delete old messages from server\n", stderr);
282 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
283 fputs(" -l, --limit don't fetch messages over given size\n", stderr);
285 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
286 fputs(" -r, --remote specify remote folder name\n", stderr);
293 /* options.c ends here */