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