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