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 "popclient.h"
31 #define LA_PROTOCOL 10
35 #define LA_USERNAME 14
36 #define LA_REMOTEFILE 15
37 #define LA_LOCALFILE 16
39 #define LA_SMTPHOST 18
42 #define LA_NOREWRITE 21
45 static char *shortoptions = "23PVaKkvS:scl:Fd:f:u:r:o:m:L:qN";
46 static struct option longoptions[] = {
47 {"version", no_argument, (int *) 0, LA_VERSION },
48 {"all", no_argument, (int *) 0, LA_ALL },
49 {"kill", no_argument, (int *) 0, LA_KILL },
50 {"keep", no_argument, (int *) 0, LA_KEEP },
51 {"verbose", no_argument, (int *) 0, LA_VERBOSE },
52 {"silent", no_argument, (int *) 0, LA_SILENT },
53 {"stdout", no_argument, (int *) 0, LA_STDOUT },
54 {"limit", required_argument, (int *) 0, LA_LIMIT },
55 {"flush", no_argument, (int *) 0, LA_FLUSH },
56 {"protocol", required_argument, (int *) 0, LA_PROTOCOL },
57 {"proto", required_argument, (int *) 0, LA_PROTOCOL },
58 {"daemon", required_argument, (int *) 0, LA_DAEMON },
59 {"poprc", required_argument, (int *) 0, LA_POPRC },
60 {"user", required_argument, (int *) 0, LA_USERNAME },
61 {"username", required_argument, (int *) 0, LA_USERNAME },
62 {"remote", required_argument, (int *) 0, LA_REMOTEFILE },
63 {"local", required_argument, (int *) 0, LA_LOCALFILE },
64 {"mda", required_argument, (int *) 0, LA_MDA },
65 {"smtphost", required_argument, (int *) 0, LA_SMTPHOST },
66 {"logfile", required_argument, (int *) 0, LA_LOGFILE },
67 {"idfile", required_argument, (int *) 0, LA_IDFILE },
68 {"quit", no_argument, (int *) 0, LA_QUIT },
69 {"norewrite", no_argument, (int *) 0, LA_NOREWRITE },
70 {"yydebug", no_argument, (int *) 0, LA_YYDEBUG },
71 {(char *) 0, no_argument, (int *) 0, 0 }
75 /*********************************************************************
76 function: parsecmdline
77 description: parse/validate the command line options.
80 argv argument strings.
81 queryctl pointer to a struct hostrec to receive the parsed
84 return value: if positive, argv index of last parsed option + 1
85 (presumes one or more server names follows).
86 if zero, the command line switches are such that
87 no server names are required (e.g. --version).
88 if negative, the command line is has one or more
91 globals: writes outlevel, versioninfo, yydebug, logfile,
92 poll_interval, quitmode, poprcfile, idfile, linelimit.
93 *********************************************************************/
95 int parsecmdline (argc,argv,queryctl)
98 struct hostrec *queryctl;
101 int fflag = 0; /* TRUE when -o or -c has been specified */
102 int errflag = 0; /* TRUE when a syntax error is detected */
104 int got_kill = 0; /* TRUE when --kill is specified */
106 extern int optind, opterr; /* defined in getopt(2) */
107 extern char *optarg; /* defined in getopt(2) */
109 bzero(queryctl,sizeof(struct hostrec)); /* start clean */
112 (c = getopt_long(argc,argv,shortoptions,
113 longoptions,&option_index)) != -1) {
117 queryctl->protocol = P_POP2;
120 queryctl->protocol = P_POP3;
128 queryctl->fetchall = !0;
142 outlevel = O_VERBOSE;
154 queryctl->output = TO_STDOUT;
159 linelimit = atoi(optarg);
161 fprintf(stderr,"Line count limit must be non-negative");
167 queryctl->flush = !0;
171 /* XXX -- should probably use a table lookup here */
172 if (strcasecmp(optarg,"pop2") == 0)
173 queryctl->protocol = P_POP2;
174 else if (strcasecmp(optarg,"pop3") == 0)
175 queryctl->protocol = P_POP3;
176 else if (strcasecmp(optarg,"imap") == 0)
177 queryctl->protocol = P_IMAP;
178 else if (strcasecmp(optarg,"apop") == 0)
179 queryctl->protocol = P_APOP;
180 else if (strcasecmp(optarg,"rpop") == 0)
181 queryctl->protocol = P_RPOP;
183 fprintf(stderr,"Invalid protocol '%s'\n specified.\n", optarg);
189 poll_interval = atoi(optarg);
193 poprcfile = (char *) xmalloc(strlen(optarg)+1);
194 strcpy(poprcfile,optarg);
198 idfile = (char *) xmalloc(strlen(optarg)+1);
199 strcpy(idfile,optarg);
203 strncpy(queryctl->remotename,optarg,sizeof(queryctl->remotename)-1);
211 queryctl->output = TO_FOLDER;
212 strncpy(queryctl->userfolder,optarg,sizeof(queryctl->userfolder)-1);
217 strncpy(queryctl->remotefolder,optarg,sizeof(queryctl->remotefolder)-1);
221 strncpy(queryctl->mda,optarg,sizeof(queryctl->mda)-1);
225 strncpy(queryctl->smtphost,optarg,sizeof(queryctl->smtphost)-1);
237 queryctl->norewrite = 1;
248 /* squawk if syntax errors were detected */
249 fputs("usage: popclient [options] [server ...]\n", stderr);
250 fputs(" options\n",stderr);
251 fputs(" -2 use POP2 protocol\n", stderr);
252 fputs(" -3 use POP3 protocol\n", stderr);
253 fputs(" -p, --protocol specify pop2, pop3, imap, apop, or rpop\n",
255 fputs(" -V, --version display version info\n", stderr);
256 fputs(" -a, --all retrieve old and new messages\n", stderr);
257 fputs(" -F, --flush delete old messages from server\n", stderr);
258 fputs(" -K, --kill delete new messages after retrieval\n", stderr);
259 fputs(" -k, --keep save new messages after retrieval\n", stderr);
260 fputs(" -l, --limit retrieve at most n message lines\n", stderr);
261 fputs(" -m, --mda set mail user agent to pass to\n", stderr);
262 fputs(" -S, --smtphost set SMTP forwarding host\n", stderr);
263 fputs(" -q, --quit kill daemon process\n", stderr);
264 fputs(" -s, --silent work silently\n", stderr);
265 fputs(" -v, --verbose work noisily (diagnostic output)\n", stderr);
266 fputs(" -d, --daemon run as a daemon once per n seconds\n", stderr);
267 fputs(" -f, --poprc specify alternate config file\n", stderr);
268 fputs(" -i, --idfile specify alternate ID database\n", stderr);
269 fputs(" -u, --username specify server user ID\n", stderr);
270 fputs(" -c, --stdout write received mail to stdout\n", stderr);
271 fputs(" -o, --local specify filename for received mail\n", stderr);
272 fputs(" -r, --remote specify remote folder name\n", stderr);
273 fputs(" -L, --logfile specify logfile name\n", stderr);
277 if (linelimit && !got_kill)
286 /*********************************************************************
287 function: setdefaults
288 description: set reasonable default values for unspecified options.
290 options option values parsed from the command-line; unspeci-
291 fied options must be filled with zero.
293 return value: zero if defaults were successfully set, else non-zero
294 (indicates a problem reading /etc/passwd).
296 globals: writes outlevel, poprcfile, idfile.
297 *********************************************************************/
299 int setdefaults (queryctl)
300 struct hostrec *queryctl;
306 bzero(queryctl,sizeof(*queryctl));
308 if ((pw = getpwuid(uid = getuid())) == NULL) {
309 fprintf(stderr,"No passwd entry for uid %d\n",uid);
313 queryctl->protocol = DEF_PROTOCOL;
315 #if defined(KEEP_IS_DEFAULT)
320 queryctl->norewrite = 0;
322 strcpy(queryctl->localname,pw->pw_name);
323 strcpy(queryctl->remotename,pw->pw_name);
324 sprintf(queryctl->userfolder, USERFOLDER, pw->pw_name);
325 queryctl->output = TO_MDA;
326 (void) sprintf(queryctl->mda, DEF_MDA, queryctl->localname);
329 (char *) xmalloc(strlen(pw->pw_dir)+strlen(POPRC_NAME)+2);
331 strcpy(poprcfile, pw->pw_dir);
332 strcat(poprcfile, "/");
333 strcat(poprcfile, POPRC_NAME);
336 (char *) xmalloc(strlen(pw->pw_dir)+strlen(IDFILE_NAME)+2);
338 strcpy(idfile, pw->pw_dir);
340 strcat(idfile, IDFILE_NAME);
349 /******************************************************************
350 function: getnextserver
351 description: read next server name from the command line.
355 optind as returned by parsecmdline and this function.
357 ret. value: next server name from command line or NULL if all
358 server names have been retrieved.
361 *****************************************************************/
362 char *getnextserver (argc,argv,optind)
367 if (*optind >= argc) {
368 /* no more servers */
372 return(argv[(*optind)++]);