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