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