xref: /illumos-gate/usr/src/cmd/msgfmt/option.c (revision 5d9d9091f564c198a760790b0bfa72c44e17912b)
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 (c) 1996-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include "common.h"
28 
29 int
30 parse_option(int *pargc, char ***pargv, struct flags *flag)
31 {
32 	char	c;
33 	char	*arg;
34 	int	argc = *pargc;
35 	char	**argv = *pargv;
36 
37 	argv++;
38 	while (--argc > 1) {
39 		arg = *argv;
40 		if (*arg == '-') {
41 			if (!*(arg + 1)) {
42 				/* not an option */
43 				break;
44 			}
45 loop:
46 			if ((c = *++arg) == '\0') {
47 				/* next argument */
48 				argv++;
49 				continue;
50 			} else if (c != '-') {
51 				/* Sun option */
52 				switch (c) {
53 				case 'D':
54 					/*
55 					 * add directory to list for input
56 					 * files search.
57 					 */
58 					if (*(arg + 1)) {
59 						/*
60 						 * no spaces between -D and
61 						 * optarg
62 						 */
63 						flag->idir = ++arg;
64 						argv++;
65 						continue;
66 					}
67 					if (--argc > 1) {
68 						if (!flag->idir)
69 							flag->idir = *++argv;
70 						else
71 							++argv;
72 						argv++;
73 						continue;
74 					}
75 					/* not enough args */
76 					return (-1);
77 					/* NOTREACHED */
78 				case 'f':
79 					/*
80 					 * Use fuzzy entry
81 					 */
82 					flag->fuzzy = 1;
83 					goto loop;
84 					/* NOTREACHED */
85 				case 'o':
86 					/*
87 					 * Specify output file name
88 					 */
89 					if (*(arg + 1)) {
90 						/*
91 						 * no spaces between -o and
92 						 * optarg
93 						 */
94 						flag->ofile = ++arg;
95 						argv++;
96 						continue;
97 					}
98 					if (--argc > 1) {
99 						flag->ofile = *++argv;
100 						argv++;
101 						continue;
102 					}
103 					/* not enough args */
104 					return (-1);
105 				case 'g':
106 					/*
107 					 * GNU mode
108 					 */
109 					flag->gnu_p = 1;
110 					goto loop;
111 				case 's':
112 					/*
113 					 * Sun mode
114 					 */
115 					flag->sun_p = 1;
116 					goto loop;
117 				case 'v':
118 					/*
119 					 * verbose mode
120 					 */
121 					flag->verbose = 1;
122 					goto loop;
123 				default:
124 					/* illegal option */
125 					return (-1);
126 				}
127 				/* NOTREACHED */
128 			}
129 
130 			if (*(arg + 1) == '\0') {
131 				/* option end */
132 				argv++;
133 				argc--;
134 				break;
135 			}
136 
137 			/* GNU options */
138 			arg++;
139 			if (strncmp(arg, "directory=", 10) == 0) {
140 				/*
141 				 * add directory to list for input
142 				 * files search.
143 				 */
144 				if (flag->idir) {
145 					/*
146 					 * inputdir has already been specified
147 					 */
148 					argv++;
149 					continue;
150 				}
151 				arg += 10;
152 				if (*arg == '\0') {
153 					/* illegal option */
154 					return (-1);
155 				}
156 				flag->idir = arg;
157 				argv++;
158 				continue;
159 			}
160 			if (strcmp(arg, "use-fuzzy") == 0) {
161 				/*
162 				 * Use fuzzy entry
163 				 */
164 				flag->fuzzy = 1;
165 				argv++;
166 				continue;
167 			}
168 			if (strncmp(arg, "output-file=", 12) == 0) {
169 				/*
170 				 * Specify output file name
171 				 */
172 				arg += 12;
173 				if (*arg == '\0') {
174 					/* illegal option */
175 					return (-1);
176 				}
177 				flag->ofile = arg;
178 				argv++;
179 				continue;
180 			}
181 			if (strcmp(arg, "strict") == 0) {
182 				/*
183 				 * strict mode
184 				 */
185 				flag->strict = 1;
186 				argv++;
187 				continue;
188 			}
189 			if (strcmp(arg, "verbose") == 0) {
190 				/*
191 				 * verbose mode
192 				 */
193 				flag->verbose = 1;
194 				argv++;
195 				continue;
196 			}
197 			/* illegal option */
198 			return (-1);
199 		}
200 		break;
201 	}
202 
203 	if (argc == 0)
204 		return (-1);
205 
206 	*pargc = argc;
207 	*pargv = argv;
208 	return (0);
209 }
210