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