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