xref: /titanic_50/usr/src/cmd/mail/parse.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 2.1	*/
27*7c478bd9Sstevel@tonic-gate #include "mail.h"
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate 	Parse the command line.
30*7c478bd9Sstevel@tonic-gate 	Return index of first non-option field (i.e. user)
31*7c478bd9Sstevel@tonic-gate */
32*7c478bd9Sstevel@tonic-gate parse(argc, argv)
33*7c478bd9Sstevel@tonic-gate int	argc;
34*7c478bd9Sstevel@tonic-gate char	**argv;
35*7c478bd9Sstevel@tonic-gate {
36*7c478bd9Sstevel@tonic-gate 	register int	 	c;
37*7c478bd9Sstevel@tonic-gate 	register char		*tmailsurr;
38*7c478bd9Sstevel@tonic-gate 	static char		pn[] = "parse";
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate 	/*
41*7c478bd9Sstevel@tonic-gate 		"mail +" means to print in reverse order and is
42*7c478bd9Sstevel@tonic-gate 		equivalent to "mail -r"
43*7c478bd9Sstevel@tonic-gate 	*/
44*7c478bd9Sstevel@tonic-gate 	if ((argc > 1) && (argv[1][0] == '+')) {
45*7c478bd9Sstevel@tonic-gate 		if (ismail) {
46*7c478bd9Sstevel@tonic-gate 			argv[1] = "-r";
47*7c478bd9Sstevel@tonic-gate 		} else {
48*7c478bd9Sstevel@tonic-gate 			goerr++;
49*7c478bd9Sstevel@tonic-gate 		}
50*7c478bd9Sstevel@tonic-gate 	}
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "m:f:x:shrpPqeEdtT:w")) != EOF) {
53*7c478bd9Sstevel@tonic-gate 		switch(c) {
54*7c478bd9Sstevel@tonic-gate 		/*
55*7c478bd9Sstevel@tonic-gate 			Set debugging level...
56*7c478bd9Sstevel@tonic-gate 		*/
57*7c478bd9Sstevel@tonic-gate 		case 'x':
58*7c478bd9Sstevel@tonic-gate 			debug = atoi(optarg);
59*7c478bd9Sstevel@tonic-gate 			orig_dbglvl = debug;
60*7c478bd9Sstevel@tonic-gate 			if (debug < 0) {
61*7c478bd9Sstevel@tonic-gate 				/* Keep trace file even if successful */
62*7c478bd9Sstevel@tonic-gate 				keepdbgfile = -1;
63*7c478bd9Sstevel@tonic-gate 				debug = -debug;
64*7c478bd9Sstevel@tonic-gate 			}
65*7c478bd9Sstevel@tonic-gate 			break;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 		/*
68*7c478bd9Sstevel@tonic-gate 			for backwards compatability with mailx...
69*7c478bd9Sstevel@tonic-gate 		*/
70*7c478bd9Sstevel@tonic-gate 		case 's':
71*7c478bd9Sstevel@tonic-gate 			/* ignore this option */
72*7c478bd9Sstevel@tonic-gate 			break;
73*7c478bd9Sstevel@tonic-gate                 /*
74*7c478bd9Sstevel@tonic-gate 		 * Deliver directly to a mailbox. Do Not go to sendmail
75*7c478bd9Sstevel@tonic-gate 		 */
76*7c478bd9Sstevel@tonic-gate 		case 'd':
77*7c478bd9Sstevel@tonic-gate 			deliverflag = TRUE;
78*7c478bd9Sstevel@tonic-gate 			break;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 		/*
81*7c478bd9Sstevel@tonic-gate 			do not print mail
82*7c478bd9Sstevel@tonic-gate  		*/
83*7c478bd9Sstevel@tonic-gate 		case 'e':
84*7c478bd9Sstevel@tonic-gate 			if (ismail) {
85*7c478bd9Sstevel@tonic-gate 				flge = 1;
86*7c478bd9Sstevel@tonic-gate 			} else {
87*7c478bd9Sstevel@tonic-gate 				goerr++;
88*7c478bd9Sstevel@tonic-gate 			}
89*7c478bd9Sstevel@tonic-gate 			optcnt++;
90*7c478bd9Sstevel@tonic-gate 			break;
91*7c478bd9Sstevel@tonic-gate 		/*
92*7c478bd9Sstevel@tonic-gate 			do not print mail
93*7c478bd9Sstevel@tonic-gate  		*/
94*7c478bd9Sstevel@tonic-gate 		case 'E':
95*7c478bd9Sstevel@tonic-gate 			if (ismail) {
96*7c478bd9Sstevel@tonic-gate 				flgE = 1;
97*7c478bd9Sstevel@tonic-gate 			} else {
98*7c478bd9Sstevel@tonic-gate 				goerr++;
99*7c478bd9Sstevel@tonic-gate 			}
100*7c478bd9Sstevel@tonic-gate 			optcnt++;
101*7c478bd9Sstevel@tonic-gate 			break;
102*7c478bd9Sstevel@tonic-gate 		/*
103*7c478bd9Sstevel@tonic-gate 		 *	use alternate file as mailfile, when reading mail
104*7c478bd9Sstevel@tonic-gate 		 *      use this from user when sending mail.
105*7c478bd9Sstevel@tonic-gate 		 */
106*7c478bd9Sstevel@tonic-gate 		case 'f':
107*7c478bd9Sstevel@tonic-gate 			flgf = 1;
108*7c478bd9Sstevel@tonic-gate 			fromflag = TRUE;
109*7c478bd9Sstevel@tonic-gate 			mailfile = optarg;
110*7c478bd9Sstevel@tonic-gate 			strncpy(from_user, optarg, sizeof (from_user));
111*7c478bd9Sstevel@tonic-gate 			from_user[sizeof (from_user) - 1] = '\0';
112*7c478bd9Sstevel@tonic-gate 			optcnt++;
113*7c478bd9Sstevel@tonic-gate 			break;
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate 		/*
116*7c478bd9Sstevel@tonic-gate 			Print headers first
117*7c478bd9Sstevel@tonic-gate 		*/
118*7c478bd9Sstevel@tonic-gate 		case 'h':
119*7c478bd9Sstevel@tonic-gate 			if (ismail) {
120*7c478bd9Sstevel@tonic-gate 				flgh = 1;
121*7c478bd9Sstevel@tonic-gate 			} else {
122*7c478bd9Sstevel@tonic-gate 				goerr++;
123*7c478bd9Sstevel@tonic-gate 			}
124*7c478bd9Sstevel@tonic-gate 			optcnt++;
125*7c478bd9Sstevel@tonic-gate 			break;
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		/*
128*7c478bd9Sstevel@tonic-gate 			print without prompting
129*7c478bd9Sstevel@tonic-gate 		*/
130*7c478bd9Sstevel@tonic-gate 		case 'p':
131*7c478bd9Sstevel@tonic-gate 			if (ismail) {
132*7c478bd9Sstevel@tonic-gate 				flgp++;
133*7c478bd9Sstevel@tonic-gate 			} else {
134*7c478bd9Sstevel@tonic-gate 				goerr++;
135*7c478bd9Sstevel@tonic-gate 			}
136*7c478bd9Sstevel@tonic-gate 			optcnt++;
137*7c478bd9Sstevel@tonic-gate 			break;
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 		/*
140*7c478bd9Sstevel@tonic-gate 			override selective display default setting
141*7c478bd9Sstevel@tonic-gate 			when reading mail...
142*7c478bd9Sstevel@tonic-gate 		*/
143*7c478bd9Sstevel@tonic-gate 		case 'P':
144*7c478bd9Sstevel@tonic-gate 			if (ismail) {
145*7c478bd9Sstevel@tonic-gate 				flgP++;
146*7c478bd9Sstevel@tonic-gate 			}
147*7c478bd9Sstevel@tonic-gate 			optcnt++;
148*7c478bd9Sstevel@tonic-gate 			break;
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 		/*
151*7c478bd9Sstevel@tonic-gate 			terminate on deletes
152*7c478bd9Sstevel@tonic-gate 		*/
153*7c478bd9Sstevel@tonic-gate 		case 'q':
154*7c478bd9Sstevel@tonic-gate 			if (ismail) {
155*7c478bd9Sstevel@tonic-gate 				delflg = 0;
156*7c478bd9Sstevel@tonic-gate 			} else {
157*7c478bd9Sstevel@tonic-gate 				goerr++;
158*7c478bd9Sstevel@tonic-gate 			}
159*7c478bd9Sstevel@tonic-gate 			optcnt++;
160*7c478bd9Sstevel@tonic-gate 			break;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		/*
163*7c478bd9Sstevel@tonic-gate 			print by first in, first out order
164*7c478bd9Sstevel@tonic-gate 		*/
165*7c478bd9Sstevel@tonic-gate 		case 'r':
166*7c478bd9Sstevel@tonic-gate 			if (ismail) {
167*7c478bd9Sstevel@tonic-gate 				flgr = 1;
168*7c478bd9Sstevel@tonic-gate 			} else {
169*7c478bd9Sstevel@tonic-gate 				goerr++;
170*7c478bd9Sstevel@tonic-gate 			}
171*7c478bd9Sstevel@tonic-gate 			optcnt++;
172*7c478bd9Sstevel@tonic-gate 			break;
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 		/*
175*7c478bd9Sstevel@tonic-gate 			add To: line to letters
176*7c478bd9Sstevel@tonic-gate 		*/
177*7c478bd9Sstevel@tonic-gate 		case 't':
178*7c478bd9Sstevel@tonic-gate 			flgt = 1;
179*7c478bd9Sstevel@tonic-gate 			optcnt++;
180*7c478bd9Sstevel@tonic-gate 			break;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 		/*
183*7c478bd9Sstevel@tonic-gate 			don't wait on sends
184*7c478bd9Sstevel@tonic-gate 		*/
185*7c478bd9Sstevel@tonic-gate 		case 'w':
186*7c478bd9Sstevel@tonic-gate 			flgw = 1;
187*7c478bd9Sstevel@tonic-gate 			break;
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 		/*
190*7c478bd9Sstevel@tonic-gate 			set message-type:
191*7c478bd9Sstevel@tonic-gate 		*/
192*7c478bd9Sstevel@tonic-gate 		case 'm':
193*7c478bd9Sstevel@tonic-gate 			msgtype = optarg;
194*7c478bd9Sstevel@tonic-gate 			if (msgtype[0] == '\0' || msgtype[0] == '-') {
195*7c478bd9Sstevel@tonic-gate 				goerr++;
196*7c478bd9Sstevel@tonic-gate 			} else {
197*7c478bd9Sstevel@tonic-gate 				flgm = 1;
198*7c478bd9Sstevel@tonic-gate 			}
199*7c478bd9Sstevel@tonic-gate 			break;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 		/*
202*7c478bd9Sstevel@tonic-gate 			bad option
203*7c478bd9Sstevel@tonic-gate 		*/
204*7c478bd9Sstevel@tonic-gate 		case '?':
205*7c478bd9Sstevel@tonic-gate 			goerr++;
206*7c478bd9Sstevel@tonic-gate 			break;
207*7c478bd9Sstevel@tonic-gate 		}
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	if (argc == optind) {
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	    if (flgm) {
215*7c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
216*7c478bd9Sstevel@tonic-gate 			"-m option used but no recipient(s) specified.");
217*7c478bd9Sstevel@tonic-gate 		goerr++;
218*7c478bd9Sstevel@tonic-gate 	    }
219*7c478bd9Sstevel@tonic-gate 	    if (flgt) {
220*7c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
221*7c478bd9Sstevel@tonic-gate 			"-t option used but no recipient(s) specified.");
222*7c478bd9Sstevel@tonic-gate 		goerr++;
223*7c478bd9Sstevel@tonic-gate 	    }
224*7c478bd9Sstevel@tonic-gate 	    if (flgw) {
225*7c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,
226*7c478bd9Sstevel@tonic-gate 			"-w option used but no recipient(s) specified.");
227*7c478bd9Sstevel@tonic-gate 		goerr++;
228*7c478bd9Sstevel@tonic-gate 	    }
229*7c478bd9Sstevel@tonic-gate 	    if (flgf) {
230*7c478bd9Sstevel@tonic-gate 		    if (mailfile[0] == '-') {
231*7c478bd9Sstevel@tonic-gate 			    errmsg(E_SYNTAX,
232*7c478bd9Sstevel@tonic-gate 				   "Files names must not begin with '-'");
233*7c478bd9Sstevel@tonic-gate 			    done(0);
234*7c478bd9Sstevel@tonic-gate 		    }
235*7c478bd9Sstevel@tonic-gate 		    if (!ismail)
236*7c478bd9Sstevel@tonic-gate 			    goerr++;
237*7c478bd9Sstevel@tonic-gate 	    }
238*7c478bd9Sstevel@tonic-gate 	}
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	if (ismail && (goerr > 0)) {
241*7c478bd9Sstevel@tonic-gate 		errmsg(E_SYNTAX,"Usage: [-ehpPqr] [-f file] [-x debuglevel]");
242*7c478bd9Sstevel@tonic-gate 		(void) fprintf (stderr, "or\t[-tw] [-m message_type] [-T file] [-x debuglevel] persons\n");
243*7c478bd9Sstevel@tonic-gate 		(void) fprintf (stderr, "or\t[-x debuglevel]\n");
244*7c478bd9Sstevel@tonic-gate 		done(0);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	return (optind);
248*7c478bd9Sstevel@tonic-gate }
249