]> Pileus Git - ~andy/fetchmail/blob - options.c
Added dns/nodns.
[~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_ENVELOPE     17
36 #define LA_USERNAME     18
37 #define LA_ALL          19
38 #define LA_KILL         20
39 #define LA_KEEP         21
40 #define LA_FLUSH        22
41 #define LA_NOREWRITE    23
42 #define LA_LIMIT        24
43 #define LA_REMOTEFILE   25
44 #define LA_SMTPHOST     26
45 #define LA_BATCHLIMIT   27
46 #define LA_FETCHLIMIT   28
47 #define LA_MDA          29
48 #define LA_INTERFACE    30
49 #define LA_MONITOR      31
50 #define LA_YYDEBUG      32
51
52 static char *shortoptions = "?Vcsvd:NqL:f:i:p:P:A:t:E:u:akKFnl:r:S:b:B:m:I:M:y";
53 static struct option longoptions[] = {
54   {"help",      no_argument,       (int *) 0, LA_HELP        },
55   {"version",   no_argument,       (int *) 0, LA_VERSION     },
56   {"check",     no_argument,       (int *) 0, LA_CHECK       },
57   {"silent",    no_argument,       (int *) 0, LA_SILENT      },
58   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE     },
59   {"daemon",    required_argument, (int *) 0, LA_DAEMON      },
60   {"nodetach",  no_argument,       (int *) 0, LA_NODETACH    },
61   {"quit",      no_argument,       (int *) 0, LA_QUIT        },
62   {"logfile",   required_argument, (int *) 0, LA_LOGFILE     },
63   {"syslog",    no_argument,       (int *) 0, LA_SYSLOG      },
64   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE      },
65   {"idfile",    required_argument, (int *) 0, LA_IDFILE      },
66 #ifdef  linux
67   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
68   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
69 #endif
70
71   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL    },
72   {"proto",     required_argument, (int *) 0, LA_PROTOCOL    },
73   {"port",      required_argument, (int *) 0, LA_PORT        },
74   {"auth",      required_argument, (int *) 0, LA_AUTHENTICATE},
75   {"timeout",   required_argument, (int *) 0, LA_TIMEOUT     },
76   {"envelope",  required_argument, (int *) 0, LA_ENVELOPE    },
77
78   {"user",      required_argument, (int *) 0, LA_USERNAME    },
79   {"username",  required_argument, (int *) 0, LA_USERNAME    },
80
81   {"all",       no_argument,       (int *) 0, LA_ALL         },
82   {"kill",      no_argument,       (int *) 0, LA_KILL        },
83   {"keep",      no_argument,       (int *) 0, LA_KEEP        },
84   {"flush",     no_argument,       (int *) 0, LA_FLUSH       },
85   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE   },
86   {"limit",     required_argument, (int *) 0, LA_LIMIT       },
87
88   {"remote",    required_argument, (int *) 0, LA_REMOTEFILE  },
89   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST    },
90   {"batchlimit",required_argument, (int *) 0, LA_BATCHLIMIT  },
91   {"fetchlimit",required_argument, (int *) 0, LA_FETCHLIMIT  },
92   {"mda",       required_argument, (int *) 0, LA_MDA         },
93
94   {"interface", required_argument, (int *) 0, LA_INTERFACE   },
95   {"monitor",   required_argument, (int *) 0, LA_MONITOR     },
96
97   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG     },
98
99   {(char *) 0,  no_argument,       (int *) 0, 0              }
100 };
101
102 int parsecmdline (argc, argv, ctl)
103 /* parse and validate the command line options */
104 int argc;                       /* argument count */
105 char **argv;                    /* argument strings */
106 struct query *ctl;      /* option record to be initialized */
107 {
108     /*
109      * return value: if positive, argv index of last parsed option + 1
110      * (presumes one or more server names follows).  if zero, the
111      * command line switches are such that no server names are
112      * required (e.g. --version).  if negative, the command line is
113      * has one or more syntax errors.
114      */
115
116     int c;
117     int ocount = 0;     /* count of destinations specified */
118     int errflag = 0;   /* TRUE when a syntax error is detected */
119     int option_index;
120
121     memset(ctl, '\0', sizeof(struct query));    /* start clean */
122
123     while (!errflag && 
124            (c = getopt_long(argc,argv,shortoptions,
125                             longoptions,&option_index)) != -1) {
126
127         switch (c) {
128         case 'V':
129         case LA_VERSION:
130             versioninfo = TRUE;
131             break;
132         case 'c':
133         case LA_CHECK:
134             check_only = TRUE;
135             break;
136         case 's':
137         case LA_SILENT:
138             outlevel = O_SILENT;
139             break;
140         case 'v':
141         case LA_VERBOSE:
142             outlevel = O_VERBOSE;
143             break;
144         case 'd':
145         case LA_DAEMON:
146             poll_interval = atoi(optarg);
147             break;
148         case 'N':
149         case LA_NODETACH:
150             nodetach = TRUE;
151             break;
152         case 'q':
153         case LA_QUIT:
154             quitmode = TRUE;
155             break;
156         case 'L':
157         case LA_LOGFILE:
158             cmd_logfile = optarg;
159             break;
160         case 'f':
161         case LA_RCFILE:
162             rcfile = (char *) xmalloc(strlen(optarg)+1);
163             strcpy(rcfile,optarg);
164             break;
165         case 'i':
166         case LA_IDFILE:
167             idfile = (char *) xmalloc(strlen(optarg)+1);
168             strcpy(idfile,optarg);
169             break;
170         case 'p':
171         case LA_PROTOCOL:
172             /* XXX -- should probably use a table lookup here */
173             if (strcasecmp(optarg,"pop2") == 0)
174                 ctl->server.protocol = P_POP2;
175             else if (strcasecmp(optarg,"pop3") == 0)
176                 ctl->server.protocol = P_POP3;
177             else if (strcasecmp(optarg,"imap") == 0)
178                 ctl->server.protocol = P_IMAP;
179             else if (strcasecmp(optarg,"apop") == 0)
180                 ctl->server.protocol = P_APOP;
181             else if (strcasecmp(optarg,"kpop") == 0)
182             {
183                 ctl->server.protocol = P_POP3;
184                 ctl->server.port = KPOP_PORT;
185                 ctl->server.authenticate =  A_KERBEROS;
186             }
187             else {
188                 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
189                 errflag++;
190             }
191             break;
192         case 'P':
193         case LA_PORT:
194             ctl->server.port = atoi(optarg);
195             break;
196         case 'A':
197         case LA_AUTHENTICATE:
198             if (strcmp(optarg, "password") == 0)
199                 ctl->server.authenticate = A_PASSWORD;
200             else if (strcmp(optarg, "kerberos") == 0)
201                 ctl->server.authenticate = A_KERBEROS;
202             else {
203                 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
204                 errflag++;
205             }
206             break;
207         case 't':
208         case LA_TIMEOUT:
209             ctl->server.timeout = atoi(optarg);
210             break;
211         case 'E':
212         case LA_ENVELOPE:
213             ctl->server.envelope = xstrdup(optarg);
214             break;
215
216         case 'u':
217         case LA_USERNAME:
218             ctl->remotename = xstrdup(optarg);
219             break;
220         case 'a':
221         case LA_ALL:
222             ctl->fetchall = TRUE;
223             break;
224         case 'K':
225         case LA_KILL:
226             ctl->keep = FALSE;
227             break;
228         case 'k':
229         case LA_KEEP:
230             ctl->keep = TRUE;
231             break;
232         case 'F':
233         case LA_FLUSH:
234             ctl->flush = TRUE;
235             break;
236         case 'n':
237         case LA_NOREWRITE:
238             ctl->no_rewrite = TRUE;
239             break;
240         case 'l':
241         case LA_LIMIT:
242             ctl->limit = atoi(optarg);
243             break;
244         case 'r':
245         case LA_REMOTEFILE:
246             ctl->mailbox = xstrdup(optarg);
247             break;
248         case 'S':
249         case LA_SMTPHOST:
250             ctl->smtphost = xstrdup(optarg);
251             ocount++;
252             break;
253         case 'b':
254         case LA_BATCHLIMIT:
255             ctl->batchlimit = atoi(optarg);
256             break;
257         case 'B':
258         case LA_FETCHLIMIT:
259             ctl->fetchlimit = atoi(optarg);
260             break;
261         case 'm':
262         case LA_MDA:
263             ctl->mda = xstrdup(optarg);
264             ocount++;
265             break;
266
267 #ifdef  linux
268         case 'I':
269         case LA_INTERFACE:
270             ctl->server.interface = xstrdup(optarg);
271             break;
272         case 'M':
273         case LA_MONITOR:
274             ctl->server.monitor = xstrdup(optarg);
275             break;
276 #endif
277
278         case 'y':
279         case LA_YYDEBUG:
280             yydebug = TRUE;
281             break;
282
283         case LA_SYSLOG:
284             use_syslog = TRUE;
285             break;
286
287         case '?':
288         case LA_HELP:
289         default:
290             errflag++;
291         }
292     }
293
294     if (check_only && poll_interval)
295     {
296         fputs("The --check and --daemon options aren't compatible.\n", stderr);
297         return(-1);
298     }
299
300     if (poll_interval == 0 && use_syslog)
301     {
302         fputs("The --syslog option is only valid with the --daemon option.\n", stderr);
303         return(-1);
304     }
305
306     if (errflag || ocount > 1) {
307         /* squawk if syntax errors were detected */
308         fputs("usage:  fetchmail [options] [server ...]\n", stderr);
309         fputs("  Options are as follows:\n",stderr);
310         fputs("  -?, --help        display this option help\n", stderr);
311         fputs("  -V, --version     display version info\n", stderr);
312
313         fputs("  -c, --check       check for messages without fetching\n", stderr);
314         fputs("  -s, --silent      work silently\n", stderr);
315         fputs("  -v, --verbose     work noisily (diagnostic output)\n", stderr);
316         fputs("  -d, --daemon      run as a daemon once per n seconds\n", stderr);
317         fputs("  -N, --nodetach    don't detach daemon process\n", stderr);
318         fputs("  -q, --quit        kill daemon process\n", stderr);
319         fputs("  -L, --logfile     specify logfile name\n", stderr);
320         fputs("      --syslog      use syslog(3) for most messages when running as a daemon\n", stderr);
321         fputs("  -f, --fetchmailrc specify alternate run control file\n", stderr);
322         fputs("  -i, --idfile      specify alternate UIDs file\n", stderr);
323 #ifdef  linux
324         fputs("  -I, --interface   interface required specification\n",stderr);
325         fputs("  -M, --monitor     monitor interface for activity\n",stderr);
326 #endif
327
328         fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
329         fputs("  -P, --port        TCP/IP service port to connect to\n",stderr);
330         fputs("  -A, --auth        authentication type (password or kerberos)\n",stderr);
331         fputs("  -t, --timeout     server nonresponse timeout\n",stderr);
332         fputs("  -E, --envelope    envelope address header\n",stderr);
333
334         fputs("  -u, --username    specify users's login on server\n", stderr);
335         fputs("  -a, --all         retrieve old and new messages\n", stderr);
336         fputs("  -K, --kill        delete new messages after retrieval\n", stderr);
337         fputs("  -k, --keep        save new messages after retrieval\n", stderr);
338         fputs("  -F, --flush       delete old messages from server\n", stderr);
339         fputs("  -n, --norewrite   don't rewrite header addresses\n", stderr);
340         fputs("  -l, --limit       don't fetch messages over given size\n", stderr);
341
342         fputs("  -S, --smtphost    set SMTP forwarding host\n", stderr);
343         fputs("  -b, --batchlimit  set batch limit for SMTP connections\n", stderr);
344         fputs("  -B, --fetchlimit  set fetch limit for server connections\n", stderr);
345         fputs("  -r, --remote      specify remote folder name\n", stderr);
346         return(-1);
347     }
348
349     return(optind);
350 }
351
352 /* options.c ends here */