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