xref: /illumos-gate/usr/src/cmd/mail/parse.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*b7d62af5Sceastha /*
23*b7d62af5Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*b7d62af5Sceastha  * Use is subject to license terms.
25*b7d62af5Sceastha  */
26*b7d62af5Sceastha 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include "mail.h"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate 	Parse the command line.
337c478bd9Sstevel@tonic-gate 	Return index of first non-option field (i.e. user)
347c478bd9Sstevel@tonic-gate */
35*b7d62af5Sceastha int
parse(int argc,char ** argv)36*b7d62af5Sceastha parse(int argc, char **argv)
377c478bd9Sstevel@tonic-gate {
38*b7d62af5Sceastha 	int	 		c;
39*b7d62af5Sceastha 	char			*tmailsurr;
407c478bd9Sstevel@tonic-gate 	static char		pn[] = "parse";
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	/*
437c478bd9Sstevel@tonic-gate 		"mail +" means to print in reverse order and is
447c478bd9Sstevel@tonic-gate 		equivalent to "mail -r"
457c478bd9Sstevel@tonic-gate 	*/
467c478bd9Sstevel@tonic-gate 	if ((argc > 1) && (argv[1][0] == '+')) {
477c478bd9Sstevel@tonic-gate 		if (ismail) {
487c478bd9Sstevel@tonic-gate 			argv[1] = "-r";
497c478bd9Sstevel@tonic-gate 		} else {
507c478bd9Sstevel@tonic-gate 			goerr++;
517c478bd9Sstevel@tonic-gate 		}
527c478bd9Sstevel@tonic-gate 	}
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "m:f:x:shrpPqeEdtT:w")) != EOF) {
557c478bd9Sstevel@tonic-gate 		switch(c) {
567c478bd9Sstevel@tonic-gate 		/*
577c478bd9Sstevel@tonic-gate 			Set debugging level...
587c478bd9Sstevel@tonic-gate 		*/
597c478bd9Sstevel@tonic-gate 		case 'x':
607c478bd9Sstevel@tonic-gate 			debug = atoi(optarg);
617c478bd9Sstevel@tonic-gate 			orig_dbglvl = debug;
627c478bd9Sstevel@tonic-gate 			if (debug < 0) {
637c478bd9Sstevel@tonic-gate 				/* Keep trace file even if successful */
647c478bd9Sstevel@tonic-gate 				keepdbgfile = -1;
657c478bd9Sstevel@tonic-gate 				debug = -debug;
667c478bd9Sstevel@tonic-gate 			}
677c478bd9Sstevel@tonic-gate 			break;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		/*
707c478bd9Sstevel@tonic-gate 			for backwards compatability with mailx...
717c478bd9Sstevel@tonic-gate 		*/
727c478bd9Sstevel@tonic-gate 		case 's':
737c478bd9Sstevel@tonic-gate 			/* ignore this option */
747c478bd9Sstevel@tonic-gate 			break;
757c478bd9Sstevel@tonic-gate                 /*
767c478bd9Sstevel@tonic-gate 		 * Deliver directly to a mailbox. Do Not go to sendmail
777c478bd9Sstevel@tonic-gate 		 */
787c478bd9Sstevel@tonic-gate 		case 'd':
797c478bd9Sstevel@tonic-gate 			deliverflag = TRUE;
807c478bd9Sstevel@tonic-gate 			break;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 		/*
837c478bd9Sstevel@tonic-gate 			do not print mail
847c478bd9Sstevel@tonic-gate  		*/
857c478bd9Sstevel@tonic-gate 		case 'e':
867c478bd9Sstevel@tonic-gate 			if (ismail) {
877c478bd9Sstevel@tonic-gate 				flge = 1;
887c478bd9Sstevel@tonic-gate 			} else {
897c478bd9Sstevel@tonic-gate 				goerr++;
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			optcnt++;
927c478bd9Sstevel@tonic-gate 			break;
937c478bd9Sstevel@tonic-gate 		/*
947c478bd9Sstevel@tonic-gate 			do not print mail
957c478bd9Sstevel@tonic-gate  		*/
967c478bd9Sstevel@tonic-gate 		case 'E':
977c478bd9Sstevel@tonic-gate 			if (ismail) {
987c478bd9Sstevel@tonic-gate 				flgE = 1;
997c478bd9Sstevel@tonic-gate 			} else {
1007c478bd9Sstevel@tonic-gate 				goerr++;
1017c478bd9Sstevel@tonic-gate 			}
1027c478bd9Sstevel@tonic-gate 			optcnt++;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		/*
1057c478bd9Sstevel@tonic-gate 		 *	use alternate file as mailfile, when reading mail
1067c478bd9Sstevel@tonic-gate 		 *      use this from user when sending mail.
1077c478bd9Sstevel@tonic-gate 		 */
1087c478bd9Sstevel@tonic-gate 		case 'f':
1097c478bd9Sstevel@tonic-gate 			flgf = 1;
1107c478bd9Sstevel@tonic-gate 			fromflag = TRUE;
1117c478bd9Sstevel@tonic-gate 			mailfile = optarg;
1127c478bd9Sstevel@tonic-gate 			strncpy(from_user, optarg, sizeof (from_user));
1137c478bd9Sstevel@tonic-gate 			from_user[sizeof (from_user) - 1] = '\0';
1147c478bd9Sstevel@tonic-gate 			optcnt++;
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		/*
1187c478bd9Sstevel@tonic-gate 			Print headers first
1197c478bd9Sstevel@tonic-gate 		*/
1207c478bd9Sstevel@tonic-gate 		case 'h':
1217c478bd9Sstevel@tonic-gate 			if (ismail) {
1227c478bd9Sstevel@tonic-gate 				flgh = 1;
1237c478bd9Sstevel@tonic-gate 			} else {
1247c478bd9Sstevel@tonic-gate 				goerr++;
1257c478bd9Sstevel@tonic-gate 			}
1267c478bd9Sstevel@tonic-gate 			optcnt++;
1277c478bd9Sstevel@tonic-gate 			break;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		/*
1307c478bd9Sstevel@tonic-gate 			print without prompting
1317c478bd9Sstevel@tonic-gate 		*/
1327c478bd9Sstevel@tonic-gate 		case 'p':
1337c478bd9Sstevel@tonic-gate 			if (ismail) {
1347c478bd9Sstevel@tonic-gate 				flgp++;
1357c478bd9Sstevel@tonic-gate 			} else {
1367c478bd9Sstevel@tonic-gate 				goerr++;
1377c478bd9Sstevel@tonic-gate 			}
1387c478bd9Sstevel@tonic-gate 			optcnt++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		/*
1427c478bd9Sstevel@tonic-gate 			override selective display default setting
1437c478bd9Sstevel@tonic-gate 			when reading mail...
1447c478bd9Sstevel@tonic-gate 		*/
1457c478bd9Sstevel@tonic-gate 		case 'P':
1467c478bd9Sstevel@tonic-gate 			if (ismail) {
1477c478bd9Sstevel@tonic-gate 				flgP++;
1487c478bd9Sstevel@tonic-gate 			}
1497c478bd9Sstevel@tonic-gate 			optcnt++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 			terminate on deletes
1547c478bd9Sstevel@tonic-gate 		*/
1557c478bd9Sstevel@tonic-gate 		case 'q':
1567c478bd9Sstevel@tonic-gate 			if (ismail) {
1577c478bd9Sstevel@tonic-gate 				delflg = 0;
1587c478bd9Sstevel@tonic-gate 			} else {
1597c478bd9Sstevel@tonic-gate 				goerr++;
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 			optcnt++;
1627c478bd9Sstevel@tonic-gate 			break;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		/*
1657c478bd9Sstevel@tonic-gate 			print by first in, first out order
1667c478bd9Sstevel@tonic-gate 		*/
1677c478bd9Sstevel@tonic-gate 		case 'r':
1687c478bd9Sstevel@tonic-gate 			if (ismail) {
1697c478bd9Sstevel@tonic-gate 				flgr = 1;
1707c478bd9Sstevel@tonic-gate 			} else {
1717c478bd9Sstevel@tonic-gate 				goerr++;
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 			optcnt++;
1747c478bd9Sstevel@tonic-gate 			break;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		/*
1777c478bd9Sstevel@tonic-gate 			add To: line to letters
1787c478bd9Sstevel@tonic-gate 		*/
1797c478bd9Sstevel@tonic-gate 		case 't':
1807c478bd9Sstevel@tonic-gate 			flgt = 1;
1817c478bd9Sstevel@tonic-gate 			optcnt++;
1827c478bd9Sstevel@tonic-gate 			break;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 		/*
1857c478bd9Sstevel@tonic-gate 			don't wait on sends
1867c478bd9Sstevel@tonic-gate 		*/
1877c478bd9Sstevel@tonic-gate 		case 'w':
1887c478bd9Sstevel@tonic-gate 			flgw = 1;
1897c478bd9Sstevel@tonic-gate 			break;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 			set message-type:
1937c478bd9Sstevel@tonic-gate 		*/
1947c478bd9Sstevel@tonic-gate 		case 'm':
1957c478bd9Sstevel@tonic-gate 			msgtype = optarg;
1967c478bd9Sstevel@tonic-gate 			if (msgtype[0] == '\0' || msgtype[0] == '-') {
1977c478bd9Sstevel@tonic-gate 				goerr++;
1987c478bd9Sstevel@tonic-gate 			} else {
1997c478bd9Sstevel@tonic-gate 				flgm = 1;
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		/*
2047c478bd9Sstevel@tonic-gate 			bad option
2057c478bd9Sstevel@tonic-gate 		*/
2067c478bd9Sstevel@tonic-gate 		case '?':
2077c478bd9Sstevel@tonic-gate 			goerr++;
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 		}
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	if (argc == optind) {
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	    if (flgm) {
2177c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
2187c478bd9Sstevel@tonic-gate 			"-m option used but no recipient(s) specified.");
2197c478bd9Sstevel@tonic-gate 		goerr++;
2207c478bd9Sstevel@tonic-gate 	    }
2217c478bd9Sstevel@tonic-gate 	    if (flgt) {
2227c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
2237c478bd9Sstevel@tonic-gate 			"-t option used but no recipient(s) specified.");
2247c478bd9Sstevel@tonic-gate 		goerr++;
2257c478bd9Sstevel@tonic-gate 	    }
2267c478bd9Sstevel@tonic-gate 	    if (flgw) {
2277c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
2287c478bd9Sstevel@tonic-gate 			"-w option used but no recipient(s) specified.");
2297c478bd9Sstevel@tonic-gate 		goerr++;
2307c478bd9Sstevel@tonic-gate 	    }
2317c478bd9Sstevel@tonic-gate 	    if (flgf) {
2327c478bd9Sstevel@tonic-gate 		    if (mailfile[0] == '-') {
2337c478bd9Sstevel@tonic-gate 			    errmsg(E_SYNTAX,
2347c478bd9Sstevel@tonic-gate 				   "Files names must not begin with '-'");
2357c478bd9Sstevel@tonic-gate 			    done(0);
2367c478bd9Sstevel@tonic-gate 		    }
2377c478bd9Sstevel@tonic-gate 		    if (!ismail)
2387c478bd9Sstevel@tonic-gate 			    goerr++;
2397c478bd9Sstevel@tonic-gate 	    }
2407c478bd9Sstevel@tonic-gate 	}
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (ismail && (goerr > 0)) {
2437c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,"Usage: [-ehpPqr] [-f file] [-x debuglevel]");
2447c478bd9Sstevel@tonic-gate 		(void) fprintf (stderr, "or\t[-tw] [-m message_type] [-T file] [-x debuglevel] persons\n");
2457c478bd9Sstevel@tonic-gate 		(void) fprintf (stderr, "or\t[-x debuglevel]\n");
2467c478bd9Sstevel@tonic-gate 		done(0);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	return (optind);
2507c478bd9Sstevel@tonic-gate }
251