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