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