xref: /onnv-gate/usr/src/lib/libast/common/misc/optjoin.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #pragma prototyped
23*4887Schin /*
24*4887Schin  * Glenn Fowler
25*4887Schin  * AT&T Research
26*4887Schin  *
27*4887Schin  * multi-pass commmand line option parse assist
28*4887Schin  *
29*4887Schin  *	int fun(char** argv, int last)
30*4887Schin  *
31*4887Schin  * each fun() argument parses as much of argv as
32*4887Schin  * possible starting at (opt_info.index,opt_info.offset) using
33*4887Schin  * optget()
34*4887Schin  *
35*4887Schin  * if last!=0 then fun is the last pass to view
36*4887Schin  * the current arg, otherwise fun sets opt_info.again=1
37*4887Schin  * and another pass will get a crack at it
38*4887Schin  *
39*4887Schin  * 0 fun() return causes immediate optjoin() 0 return
40*4887Schin  *
41*4887Schin  * optjoin() returns non-zero if more args remain
42*4887Schin  * to be parsed at opt_info.index
43*4887Schin  */
44*4887Schin 
45*4887Schin #include <optlib.h>
46*4887Schin 
47*4887Schin typedef int (*Optpass_f)(char**, int);
48*4887Schin 
49*4887Schin int
50*4887Schin optjoin(char** argv, ...)
51*4887Schin {
52*4887Schin 	va_list			ap;
53*4887Schin 	register Optpass_f	fun;
54*4887Schin 	register Optpass_f	rep;
55*4887Schin 	Optpass_f		err;
56*4887Schin 	int			more;
57*4887Schin 	int			user;
58*4887Schin 	int			last_index;
59*4887Schin 	int			last_offset;
60*4887Schin 	int			err_index;
61*4887Schin 	int			err_offset;
62*4887Schin 
63*4887Schin 	if (!opt_info.state)
64*4887Schin 		optget(NiL, NiL);
65*4887Schin 	err = rep = 0;
66*4887Schin 	for (;;)
67*4887Schin 	{
68*4887Schin 		va_start(ap, argv);
69*4887Schin 		opt_info.state->join = 0;
70*4887Schin 		while (fun = va_arg(ap, Optpass_f))
71*4887Schin 		{
72*4887Schin 			last_index = opt_info.index;
73*4887Schin 			last_offset = opt_info.offset;
74*4887Schin 			opt_info.state->join++;
75*4887Schin 			user = (*fun)(argv, 0);
76*4887Schin 			more = argv[opt_info.index] != 0;
77*4887Schin 			if (!opt_info.again)
78*4887Schin 			{
79*4887Schin 				if (!more)
80*4887Schin 				{
81*4887Schin 					opt_info.state->join = 0;
82*4887Schin 					return 0;
83*4887Schin 				}
84*4887Schin 				if (!user)
85*4887Schin 				{
86*4887Schin 					if (*argv[opt_info.index] != '+')
87*4887Schin 					{
88*4887Schin 						opt_info.state->join = 0;
89*4887Schin 						return 1;
90*4887Schin 					}
91*4887Schin 					opt_info.again = -1;
92*4887Schin 				}
93*4887Schin 				else
94*4887Schin 					err = 0;
95*4887Schin 			}
96*4887Schin 			if (opt_info.again)
97*4887Schin 			{
98*4887Schin 				if (opt_info.again > 0 && (!err || err_index < opt_info.index || err_index == opt_info.index && err_offset < opt_info.offset))
99*4887Schin 				{
100*4887Schin 					err = fun;
101*4887Schin 					err_index = opt_info.index;
102*4887Schin 					err_offset = opt_info.offset;
103*4887Schin 				}
104*4887Schin 				opt_info.again = 0;
105*4887Schin 				opt_info.index = opt_info.state->pindex ? opt_info.state->pindex : 1;
106*4887Schin 				opt_info.offset = opt_info.state->poffset;
107*4887Schin 			}
108*4887Schin 			if (!rep || opt_info.index != last_index || opt_info.offset != last_offset)
109*4887Schin 				rep = fun;
110*4887Schin 			else if (fun == rep)
111*4887Schin 			{
112*4887Schin 				if (!err)
113*4887Schin 				{
114*4887Schin 					opt_info.state->join = 0;
115*4887Schin 					return 1;
116*4887Schin 				}
117*4887Schin 				(*err)(argv, 1);
118*4887Schin 				opt_info.offset = 0;
119*4887Schin 			}
120*4887Schin 		}
121*4887Schin 		va_end(ap);
122*4887Schin 	}
123*4887Schin }
124