1 /* Copyright 1993-95 by Carl Harris, Jr. Copyright 1996 by Eric S. Raymond
3 * For license terms, see the file COPYING in this directory.
6 /***********************************************************************
9 programmer: Carl Harris, ceharris@mal.com
10 description: command-line option processing
12 ***********************************************************************/
19 #include "fetchmail.h"
32 #define LA_USERNAME 12
33 #define LA_REMOTEFILE 13
35 #define LA_SMTPHOST 15
39 #define LA_NOREWRITE 19
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 }
73 /*********************************************************************
74 function: parsecmdline
75 description: parse/validate the command line options.
78 argv argument strings.
79 queryctl pointer to a struct hostrec to receive the parsed
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
89 globals: writes outlevel, versioninfo, yydebug, logfile,
90 poll_interval, quitmode, rcfile
91 *********************************************************************/
93 int parsecmdline (argc,argv,queryctl)
96 struct hostrec *queryctl;
99 int fflag = 0; /* TRUE when -o or -c has been specified */
100 int errflag = 0; /* TRUE when a syntax error is detected */
102 int got_kill = 0; /* TRUE when --kill is specified */
104 extern int optind, opterr; /* defined in getopt(2) */
105 extern char *optarg; /* defined in getopt(2) */
107 memset(queryctl, '\0', sizeof(struct hostrec)); /* start clean */
110 (c = getopt_long(argc,argv,shortoptions,
111 longoptions,&option_index)) != -1) {
120 queryctl->fetchall = !0;
134 outlevel = O_VERBOSE;
142 queryctl->flush = !0;
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;
158 fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
164 poll_interval = atoi(optarg);
168 rcfile = (char *) xmalloc(strlen(optarg)+1);
169 strcpy(rcfile,optarg);
173 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
177 strncpy(queryctl->mailbox,optarg,sizeof(queryctl->mailbox)-1);
181 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda));
185 queryctl->port = atoi(optarg);
193 strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
206 queryctl->norewrite = 1;
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);