]> Pileus Git - ~andy/fetchmail/blob - options.c
Dave Bodenstab's error changes.
[~andy/fetchmail] / options.c
1 /*
2  * options.c -- command-line option processing
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include <config.h>
8
9 #include <stdio.h>
10 #include <pwd.h>
11 #include <string.h>
12 #if defined(STDC_HEADERS)
13 #include  <stdlib.h>
14 #endif
15
16 #include "getopt.h"
17 #include "fetchmail.h"
18
19 #define LA_HELP         1
20 #define LA_VERSION      2 
21 #define LA_CHECK        3
22 #define LA_SILENT       4 
23 #define LA_VERBOSE      5 
24 #define LA_DAEMON       6
25 #define LA_NODETACH     7
26 #define LA_QUIT         8
27 #define LA_LOGFILE      9
28 #define LA_SYSLOG       10
29 #define LA_RCFILE       11
30 #define LA_IDFILE       12
31 #define LA_PROTOCOL     13
32 #define LA_PORT         14
33 #define LA_AUTHENTICATE 15
34 #define LA_TIMEOUT      16
35 #define LA_USERNAME     17
36 #define LA_ALL          18
37 #define LA_KILL         19
38 #define LA_KEEP         20
39 #define LA_FLUSH        21
40 #define LA_NOREWRITE    22
41 #define LA_LIMIT        23
42 #define LA_REMOTEFILE   24
43 #define LA_SMTPHOST     25
44 #define LA_BATCHLIMIT   26
45 #define LA_FETCHLIMIT   27
46 #define LA_MDA          28
47 #define LA_INTERFACE    29
48 #define LA_MONITOR      30
49 #define LA_YYDEBUG      31
50
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      },
65 #ifdef  linux
66   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
67   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
68 #endif
69
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     },
75
76   {"user",      required_argument, (int *) 0, LA_USERNAME    },
77   {"username",  required_argument, (int *) 0, LA_USERNAME    },
78
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       },
85
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         },
91
92   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
93   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
94
95   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
96
97   {(char *) 0,  no_argument,       (int *) 0, 0              }
98 };
99
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 */
105 {
106     /*
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.
112      */
113
114     int c;
115     int ocount = 0;     /* count of destinations specified */
116     int errflag = 0;   /* TRUE when a syntax error is detected */
117     int option_index;
118
119     memset(ctl, '\0', sizeof(struct query));    /* start clean */
120     cmd_batchlimit = -1;
121
122     while (!errflag && 
123            (c = getopt_long(argc,argv,shortoptions,
124                             longoptions,&option_index)) != -1) {
125
126         switch (c) {
127         case 'V':
128         case LA_VERSION:
129             versioninfo = TRUE;
130             break;
131         case 'c':
132         case LA_CHECK:
133             check_only = TRUE;
134             break;
135         case 's':
136         case LA_SILENT:
137             outlevel = O_SILENT;
138             break;
139         case 'v':
140         case LA_VERBOSE:
141             outlevel = O_VERBOSE;
142             break;
143         case 'd':
144         case LA_DAEMON:
145             poll_interval = atoi(optarg);
146             break;
147         case 'N':
148         case LA_NODETACH:
149             nodetach = TRUE;
150             break;
151         case 'q':
152         case LA_QUIT:
153             quitmode = TRUE;
154             break;
155         case 'L':
156         case LA_LOGFILE:
157             cmd_logfile = optarg;
158             break;
159         case 'f':
160         case LA_RCFILE:
161             rcfile = (char *) xmalloc(strlen(optarg)+1);
162             strcpy(rcfile,optarg);
163             break;
164         case 'i':
165         case LA_IDFILE:
166             idfile = (char *) xmalloc(strlen(optarg)+1);
167             strcpy(idfile,optarg);
168             break;
169         case 'p':
170         case LA_PROTOCOL:
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)
181             {
182                 ctl->protocol = P_POP3;
183                 ctl->port = KPOP_PORT;
184                 ctl->authenticate =  A_KERBEROS;
185             }
186             else {
187                 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
188                 errflag++;
189             }
190             break;
191         case 'P':
192         case LA_PORT:
193             ctl->port = atoi(optarg);
194             break;
195         case 'A':
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;
201             else {
202                 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
203                 errflag++;
204             }
205             break;
206         case 't':
207             ctl->timeout = atoi(optarg);
208             break;
209         case 'u':
210         case LA_USERNAME:
211             strncpy(ctl->remotename,optarg,sizeof(ctl->remotename)-1);
212             break;
213
214         case 'a':
215         case LA_ALL:
216             ctl->fetchall = TRUE;
217             break;
218         case 'K':
219         case LA_KILL:
220             ctl->keep = FALSE;
221             break;
222         case 'k':
223         case LA_KEEP:
224             ctl->keep = TRUE;
225             break;
226         case 'F':
227         case LA_FLUSH:
228             ctl->flush = TRUE;
229             break;
230         case 'n':
231         case LA_NOREWRITE:
232             ctl->norewrite = TRUE;
233             break;
234         case 'l':
235         case LA_LIMIT:
236             ctl->limit = atoi(optarg);
237             break;
238         case 'r':
239         case LA_REMOTEFILE:
240             strncpy(ctl->mailbox,optarg,sizeof(ctl->mailbox)-1);
241             break;
242         case 'S':
243         case LA_SMTPHOST:
244             strncpy(ctl->smtphost,optarg,sizeof(ctl->smtphost)-1);
245             ocount++;
246             break;
247         case 'b':
248         case LA_BATCHLIMIT:
249             cmd_batchlimit = atoi(optarg);
250             break;
251         case 'B':
252         case LA_FETCHLIMIT:
253             ctl->fetchlimit = atoi(optarg);
254             break;
255         case 'm':
256         case LA_MDA:
257             strncpy(ctl->mda,optarg,sizeof(ctl->mda));
258             ocount++;
259             break;
260
261 #ifdef  linux
262         case 'I':
263         case LA_INTERFACE:
264             cmd_interface = optarg;
265             break;
266         case 'M':
267         case LA_MONITOR:
268             cmd_monitor = optarg;
269             break;
270 #endif
271
272         case 'y':
273         case LA_YYDEBUG:
274             yydebug = TRUE;
275             break;
276
277         case LA_SYSLOG:
278             use_syslog = TRUE;
279             break;
280
281         case '?':
282         case LA_HELP:
283         default:
284             errflag++;
285         }
286     }
287
288     if (check_only && poll_interval)
289     {
290         fputs("The --check and --daemon options aren't compatible.\n", stderr);
291         return(-1);
292     }
293
294     if (poll_interval == 0 && use_syslog)
295     {
296         fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
297         return(-1);
298     }
299
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);
306
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);
317 #ifdef  linux
318         fputs("  -I, --interface   interface required specification\n",stderr);
319         fputs("  -M, --monitor     monitor interface for activity\n",stderr);
320 #endif
321
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);
326
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);
334
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);
339         return(-1);
340     }
341
342     return(optind);
343 }
344
345 /* options.c ends here */