xref: /illumos-gate/usr/src/cmd/mail/parse.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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 #include "mail.h"
31 /*
32 	Parse the command line.
33 	Return index of first non-option field (i.e. user)
34 */
35 int
36 parse(int argc, char **argv)
37 {
38 	int	 		c;
39 	char			*tmailsurr;
40 	static char		pn[] = "parse";
41 
42 	/*
43 		"mail +" means to print in reverse order and is
44 		equivalent to "mail -r"
45 	*/
46 	if ((argc > 1) && (argv[1][0] == '+')) {
47 		if (ismail) {
48 			argv[1] = "-r";
49 		} else {
50 			goerr++;
51 		}
52 	}
53 
54 	while ((c = getopt(argc, argv, "m:f:x:shrpPqeEdtT:w")) != EOF) {
55 		switch(c) {
56 		/*
57 			Set debugging level...
58 		*/
59 		case 'x':
60 			debug = atoi(optarg);
61 			orig_dbglvl = debug;
62 			if (debug < 0) {
63 				/* Keep trace file even if successful */
64 				keepdbgfile = -1;
65 				debug = -debug;
66 			}
67 			break;
68 
69 		/*
70 			for backwards compatability with mailx...
71 		*/
72 		case 's':
73 			/* ignore this option */
74 			break;
75                 /*
76 		 * Deliver directly to a mailbox. Do Not go to sendmail
77 		 */
78 		case 'd':
79 			deliverflag = TRUE;
80 			break;
81 
82 		/*
83 			do not print mail
84  		*/
85 		case 'e':
86 			if (ismail) {
87 				flge = 1;
88 			} else {
89 				goerr++;
90 			}
91 			optcnt++;
92 			break;
93 		/*
94 			do not print mail
95  		*/
96 		case 'E':
97 			if (ismail) {
98 				flgE = 1;
99 			} else {
100 				goerr++;
101 			}
102 			optcnt++;
103 			break;
104 		/*
105 		 *	use alternate file as mailfile, when reading mail
106 		 *      use this from user when sending mail.
107 		 */
108 		case 'f':
109 			flgf = 1;
110 			fromflag = TRUE;
111 			mailfile = optarg;
112 			strncpy(from_user, optarg, sizeof (from_user));
113 			from_user[sizeof (from_user) - 1] = '\0';
114 			optcnt++;
115 			break;
116 
117 		/*
118 			Print headers first
119 		*/
120 		case 'h':
121 			if (ismail) {
122 				flgh = 1;
123 			} else {
124 				goerr++;
125 			}
126 			optcnt++;
127 			break;
128 
129 		/*
130 			print without prompting
131 		*/
132 		case 'p':
133 			if (ismail) {
134 				flgp++;
135 			} else {
136 				goerr++;
137 			}
138 			optcnt++;
139 			break;
140 
141 		/*
142 			override selective display default setting
143 			when reading mail...
144 		*/
145 		case 'P':
146 			if (ismail) {
147 				flgP++;
148 			}
149 			optcnt++;
150 			break;
151 
152 		/*
153 			terminate on deletes
154 		*/
155 		case 'q':
156 			if (ismail) {
157 				delflg = 0;
158 			} else {
159 				goerr++;
160 			}
161 			optcnt++;
162 			break;
163 
164 		/*
165 			print by first in, first out order
166 		*/
167 		case 'r':
168 			if (ismail) {
169 				flgr = 1;
170 			} else {
171 				goerr++;
172 			}
173 			optcnt++;
174 			break;
175 
176 		/*
177 			add To: line to letters
178 		*/
179 		case 't':
180 			flgt = 1;
181 			optcnt++;
182 			break;
183 
184 		/*
185 			don't wait on sends
186 		*/
187 		case 'w':
188 			flgw = 1;
189 			break;
190 
191 		/*
192 			set message-type:
193 		*/
194 		case 'm':
195 			msgtype = optarg;
196 			if (msgtype[0] == '\0' || msgtype[0] == '-') {
197 				goerr++;
198 			} else {
199 				flgm = 1;
200 			}
201 			break;
202 
203 		/*
204 			bad option
205 		*/
206 		case '?':
207 			goerr++;
208 			break;
209 		}
210 	}
211 
212 
213 
214 	if (argc == optind) {
215 
216 	    if (flgm) {
217 		errmsg(E_SYNTAX,
218 			"-m option used but no recipient(s) specified.");
219 		goerr++;
220 	    }
221 	    if (flgt) {
222 		errmsg(E_SYNTAX,
223 			"-t option used but no recipient(s) specified.");
224 		goerr++;
225 	    }
226 	    if (flgw) {
227 		errmsg(E_SYNTAX,
228 			"-w option used but no recipient(s) specified.");
229 		goerr++;
230 	    }
231 	    if (flgf) {
232 		    if (mailfile[0] == '-') {
233 			    errmsg(E_SYNTAX,
234 				   "Files names must not begin with '-'");
235 			    done(0);
236 		    }
237 		    if (!ismail)
238 			    goerr++;
239 	    }
240 	}
241 
242 	if (ismail && (goerr > 0)) {
243 		errmsg(E_SYNTAX,"Usage: [-ehpPqr] [-f file] [-x debuglevel]");
244 		(void) fprintf (stderr, "or\t[-tw] [-m message_type] [-T file] [-x debuglevel] persons\n");
245 		(void) fprintf (stderr, "or\t[-x debuglevel]\n");
246 		done(0);
247 	}
248 
249 	return (optind);
250 }
251