]> Pileus Git - ~andy/fetchmail/blob - options.c
4818e9ff692c7f8c5de1ed6f3bf1da79e4917543
[~andy/fetchmail] / options.c
1 /* Copyright 1993-95 by Carl Harris, Jr. Copyright 1996 by Eric S. Raymond
2  * All rights reserved.
3  * For license terms, see the file COPYING in this directory.
4  */
5
6 /***********************************************************************
7   module:       options.c
8   project:      fetchmail
9   programmer:   Carl Harris, ceharris@mal.com
10   description:  command-line option processing
11
12  ***********************************************************************/
13
14 #include <config.h>
15 #include <stdio.h>
16
17 #include <pwd.h>
18 #include "getopt.h"
19 #include "fetchmail.h"
20
21 #define LA_VERSION      1 
22 #define LA_ALL          2
23 #define LA_KILL         3
24 #define LA_KEEP         4 
25 #define LA_VERBOSE      5 
26 #define LA_SILENT       6 
27 #define LA_STDOUT       7
28 #define LA_FLUSH        8
29 #define LA_PROTOCOL     9
30 #define LA_DAEMON       10
31 #define LA_RCFILE       11
32 #define LA_USERNAME     12
33 #define LA_REMOTEFILE   13
34 #define LA_PORT         14
35 #define LA_SMTPHOST     15
36 #define LA_MDA          16
37 #define LA_LOGFILE      17
38 #define LA_QUIT         18
39 #define LA_NOREWRITE    19
40 #define LA_CHECK        20
41 #define LA_HELP         21
42 #define LA_YYDEBUG      22
43
44 static char *shortoptions = "P:p:VaKkvS:m:sFd:f:u:r:L:qN?";
45 static struct option longoptions[] = {
46   {"version",   no_argument,       (int *) 0, LA_VERSION    },
47   {"all",       no_argument,       (int *) 0, LA_ALL        },
48   {"kill",      no_argument,       (int *) 0, LA_KILL       },
49   {"keep",      no_argument,       (int *) 0, LA_KEEP       },
50   {"verbose",   no_argument,       (int *) 0, LA_VERBOSE    },
51   {"silent",    no_argument,       (int *) 0, LA_SILENT     },
52   {"flush",     no_argument,       (int *) 0, LA_FLUSH      },
53   {"protocol",  required_argument, (int *) 0, LA_PROTOCOL   },
54   {"proto",     required_argument, (int *) 0, LA_PROTOCOL   },
55   {"daemon",    required_argument, (int *) 0, LA_DAEMON     },
56   {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE     },
57   {"user",      required_argument, (int *) 0, LA_USERNAME   },
58   {"username",  required_argument, (int *) 0, LA_USERNAME   },
59   {"remote",    required_argument, (int *) 0, LA_REMOTEFILE },
60   {"port",      required_argument, (int *) 0, LA_PORT       },
61   {"smtphost",  required_argument, (int *) 0, LA_SMTPHOST   },
62   {"mda",       required_argument, (int *) 0, LA_MDA        },
63   {"logfile",   required_argument, (int *) 0, LA_LOGFILE    },
64   {"quit",      no_argument,       (int *) 0, LA_QUIT       },
65   {"norewrite", no_argument,       (int *) 0, LA_NOREWRITE  },
66   {"check",     no_argument,       (int *) 0, LA_CHECK      },
67   {"help",      no_argument,       (int *) 0, LA_HELP       },
68   {"yydebug",   no_argument,       (int *) 0, LA_YYDEBUG    },
69   {(char *) 0,  no_argument,       (int *) 0, 0             }
70 };
71
72
73 /*********************************************************************
74   function:      parsecmdline
75   description:   parse/validate the command line options.
76   arguments:
77     argc         argument count.
78     argv         argument strings.
79     queryctl     pointer to a struct hostrec to receive the parsed 
80                  options.
81
82   return value:  if positive, argv index of last parsed option + 1
83                  (presumes one or more server names follows).
84                  if zero, the command line switches are such that
85                  no server names are required (e.g. --version).
86                  if negative, the command line is has one or more
87                  syntax errors.
88   calls:         none.  
89   globals:       writes outlevel, versioninfo, yydebug, logfile, 
90                  poll_interval, quitmode, rcfile
91  *********************************************************************/
92
93 int parsecmdline (argc,argv,queryctl)
94 int argc;
95 char **argv;
96 struct hostrec *queryctl;
97 {
98   int c,i;
99   int fflag = 0;     /* TRUE when -o or -c has been specified */
100   int errflag = 0;   /* TRUE when a syntax error is detected */
101   int option_index;
102   int got_kill = 0;  /* TRUE when --kill is specified */
103
104   extern int optind, opterr;     /* defined in getopt(2) */
105   extern char *optarg;          /* defined in getopt(2) */
106
107   memset(queryctl, '\0', sizeof(struct hostrec));    /* start clean */
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 = !0;
117         break;
118       case 'a':
119       case LA_ALL:
120         queryctl->fetchall = !0;
121         break;
122       case 'K':
123       case LA_KILL:
124         queryctl->keep = 0;
125         got_kill = 1;
126         break;
127       case 'k':
128       case LA_KEEP:
129         queryctl->keep = !0;
130         got_kill = 0;
131         break;
132       case 'v':
133       case LA_VERBOSE:
134         outlevel = O_VERBOSE;
135         break;
136       case 's':
137       case LA_SILENT:
138         outlevel = O_SILENT;
139         break;
140       case 'F':
141       case LA_FLUSH:
142         queryctl->flush = !0;
143         break;
144       case 'p':
145       case LA_PROTOCOL:
146         /* XXX -- should probably use a table lookup here */
147         if (strcasecmp(optarg,"pop2") == 0)
148           queryctl->protocol = P_POP2;
149         else if (strcasecmp(optarg,"pop3") == 0)
150           queryctl->protocol = P_POP3;
151         else if (strcasecmp(optarg,"imap") == 0)
152           queryctl->protocol = P_IMAP;
153         else if (strcasecmp(optarg,"apop") == 0)
154           queryctl->protocol = P_APOP;
155         else if (strcasecmp(optarg,"kpop") == 0)
156             queryctl->protocol = P_KPOP;
157         else {
158           fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
159           errflag++;
160         }
161         break;
162       case 'd':
163       case LA_DAEMON:
164         poll_interval = atoi(optarg);
165         break;
166       case 'f':
167       case LA_RCFILE:
168         rcfile = (char *) xmalloc(strlen(optarg)+1);
169         strcpy(rcfile,optarg);
170         break;
171       case 'u':
172       case LA_USERNAME:
173         strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
174         break;
175       case 'r':
176       case LA_REMOTEFILE:
177         strncpy(queryctl->mailbox,optarg,sizeof(queryctl->mailbox)-1);
178         break;
179       case 'm':
180       case LA_MDA:
181         strncpy(queryctl->mda,optarg,sizeof(queryctl->mda));
182         break;
183       case 'P':
184       case LA_PORT:
185         queryctl->port = atoi(optarg);
186         break;
187       case 'S':
188       case LA_SMTPHOST:
189         if (fflag) 
190           errflag++;
191         else {
192           fflag++;
193           strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
194         }
195         break;
196       case 'L':
197       case LA_LOGFILE:
198         logfile = optarg;
199         break;
200       case 'q':
201       case LA_QUIT:
202         quitmode = 1;
203         break;
204       case 'N':
205       case LA_NOREWRITE:
206         queryctl->norewrite = 1;
207         break;
208       case 'c':
209       case LA_CHECK:
210         check_only = 1;
211         break;
212       case LA_YYDEBUG:
213         yydebug = 1;
214         break;
215       case '?':
216       case LA_HELP:
217       default:
218         errflag++;
219     }
220   }
221
222   if (errflag) {
223     /* squawk if syntax errors were detected */
224     fputs("usage:  fetchmail [options] [server ...]\n", stderr);
225     fputs("  Options are as follows:\n",stderr);
226     fputs("  -?, --help        display this option help\n", stderr);
227     fputs("  -p, --protocol    specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
228     fputs("  -V, --version     display version info\n", stderr);
229     fputs("  -a, --all         retrieve old and new messages\n", stderr);
230     fputs("  -F, --flush       delete old messages from server\n", stderr);
231     fputs("  -K, --kill        delete new messages after retrieval\n", stderr);
232     fputs("  -k, --keep        save new messages after retrieval\n", stderr);
233     fputs("  -S, --smtphost    set SMTP forwarding host\n", stderr);
234     fputs("  -q, --quit        kill daemon process\n", stderr);
235     fputs("  -s, --silent      work silently\n", stderr);
236     fputs("  -v, --verbose     work noisily (diagnostic output)\n", stderr);
237     fputs("  -d, --daemon      run as a daemon once per n seconds\n", stderr);
238     fputs("  -f, --fetchmailrc specify alternate run control file\n", stderr);
239     fputs("  -u, --username    specify users's login on server\n", stderr);
240     fputs("  -r, --remote      specify remote folder name\n", stderr);
241     fputs("  -L, --logfile     specify logfile name\n", stderr);
242     fputs("  -c, --check       check for messages without retrieving\n", stderr);
243     return(-1);
244   }
245
246   return(optind);
247 }
248