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