2 * For license terms, see the file COPYING in this directory.
5 /***********************************************************************
8 programmer: Eric S. Raymond, <esr@thyrsus.com>
9 description: command-line option processing
11 ***********************************************************************/
18 #include "fetchmail.h"
30 #define LA_PROTOCOL 11
32 #define LA_AUTHENTICATE 13
34 #define LA_USERNAME 15
39 #define LA_NOREWRITE 20
40 #define LA_REMOTEFILE 21
41 #define LA_SMTPHOST 22
45 static char *shortoptions = "?Vcsvd:qL:f:i:p:P:A:t:u:akKFnr:S:m:y";
46 static struct option longoptions[] = {
47 {"help", no_argument, (int *) 0, LA_HELP },
48 {"version", no_argument, (int *) 0, LA_VERSION },
49 {"check", no_argument, (int *) 0, LA_CHECK },
50 {"silent", no_argument, (int *) 0, LA_SILENT },
51 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
52 {"daemon", required_argument, (int *) 0, LA_DAEMON },
53 {"quit", no_argument, (int *) 0, LA_QUIT },
54 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
55 {"fetchmailrc",required_argument,(int *) 0, LA_RCFILE },
56 {"idfile", required_argument, (int *) 0, LA_IDFILE },
58 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
59 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
60 {"port", required_argument, (int *) 0, LA_PORT },
61 {"auth", required_argument, (int *) 0, LA_AUTHENTICATE},
62 {"timeout", required_argument, (int *) 0, LA_TIMEOUT },
64 {"user", required_argument, (int *) 0, LA_USERNAME },
65 {"username", required_argument, (int *) 0, LA_USERNAME },
67 {"all", no_argument, (int *) 0, LA_ALL },
68 {"kill", no_argument, (int *) 0, LA_KILL },
69 {"keep", no_argument, (int *) 0, LA_KEEP },
70 {"flush", no_argument, (int *) 0, LA_FLUSH },
71 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
73 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
74 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
75 {"mda", required_argument, (int *) 0, LA_MDA },
77 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
79 {(char *) 0, no_argument, (int *) 0, 0 }
83 /*********************************************************************
84 function: parsecmdline
85 description: parse/validate the command line options.
88 argv argument strings.
89 queryctl pointer to a struct hostrec to receive the parsed
92 return value: if positive, argv index of last parsed option + 1
93 (presumes one or more server names follows).
94 if zero, the command line switches are such that
95 no server names are required (e.g. --version).
96 if negative, the command line is has one or more
99 globals: writes outlevel, versioninfo, yydebug, logfile,
100 poll_interval, quitmode, rcfile
101 *********************************************************************/
103 int parsecmdline (argc,argv,queryctl)
106 struct hostrec *queryctl;
109 int ocount = 0; /* count of destinations specified */
110 int errflag = 0; /* TRUE when a syntax error is detected */
113 memset(queryctl, '\0', sizeof(struct hostrec)); /* start clean */
116 (c = getopt_long(argc,argv,shortoptions,
117 longoptions,&option_index)) != -1) {
134 outlevel = O_VERBOSE;
138 poll_interval = atoi(optarg);
150 rcfile = (char *) xmalloc(strlen(optarg)+1);
151 strcpy(rcfile,optarg);
155 idfile = (char *) xmalloc(strlen(optarg)+1);
156 strcpy(idfile,optarg);
160 /* XXX -- should probably use a table lookup here */
161 if (strcasecmp(optarg,"pop2") == 0)
162 queryctl->protocol = P_POP2;
163 else if (strcasecmp(optarg,"pop3") == 0)
164 queryctl->protocol = P_POP3;
165 else if (strcasecmp(optarg,"imap") == 0)
166 queryctl->protocol = P_IMAP;
167 else if (strcasecmp(optarg,"apop") == 0)
168 queryctl->protocol = P_APOP;
169 else if (strcasecmp(optarg,"kpop") == 0)
171 queryctl->protocol = P_POP3;
172 queryctl->port = KPOP_PORT;
173 queryctl->authenticate = A_KERBEROS;
176 fprintf(stderr,"Invalid protocol `%s' specified.\n", optarg);
182 queryctl->port = atoi(optarg);
185 case LA_AUTHENTICATE:
186 if (strcmp(optarg, "password") == 0)
187 queryctl->authenticate = A_PASSWORD;
188 else if (strcmp(optarg, "kerberos") == 0)
189 queryctl->authenticate = A_KERBEROS;
191 fprintf(stderr,"Invalid authentication `%s' specified.\n", optarg);
196 queryctl->timeout = atoi(optarg);
200 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
205 queryctl->fetchall = TRUE;
209 queryctl->keep = FALSE;
213 queryctl->keep = TRUE;
217 queryctl->flush = TRUE;
221 queryctl->norewrite = TRUE;
225 strncpy(queryctl->mailbox,optarg,sizeof(queryctl->mailbox)-1);
229 strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
234 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda));
249 if (errflag || ocount > 1) {
250 /* squawk if syntax errors were detected */
251 fputs("usage: fetchmail [options] [server ...]\n", stderr);
252 fputs(" Options are as follows:\n",stderr);
253 fputs(" -?, --help display this option help\n", stderr);
254 fputs(" -V, --version display version info\n", stderr);
256 fputs(" -c, --check check for messages without fetching\n", stderr);
257 fputs(" -s, --silent work silently\n", stderr);
258 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
259 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
260 fputs(" -q, --quit kill daemon process\n", stderr);
261 fputs(" -L, --logfile specify logfile name\n", stderr);
262 fputs(" -f, --fetchmailrc specify alternate run control file\n", stderr);
263 fputs(" -i, --idfile specify alternate UIDs file\n", stderr);
265 fputs(" -p, --protocol specify pop2, pop3, imap, apop, rpop, kpop\n", stderr);
266 fputs(" -P, --port TCP/IP service port to connect to\n",stderr);
267 fputs(" -A, --auth authentication type (password or kerberos)\n",stderr);
268 fputs(" -t, --timeout server nonresponse timeout\n",stderr);
270 fputs(" -u, --username specify users's login on server\n", stderr);
271 fputs(" -a, --all retrieve old and new messages\n", stderr);
272 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
273 fputs(" -k, --keep save new messages after retrieval\n", stderr);
274 fputs(" -F, --flush delete old messages from server\n", stderr);
275 fputs(" -n, --norewrite don't rewrite header addresses\n", stderr);
277 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
278 fputs(" -r, --remote specify remote folder name\n", stderr);