/***********************************************************************1* *2* This software is part of the ast package *3* Copyright (c) 1985-2011 AT&T Intellectual Property *4* and is licensed under the *5* Eclipse Public License, Version 1.0 *6* by AT&T Intellectual Property *7* *8* A copy of the License is available at *9* http://www.eclipse.org/org/documents/epl-v10.html *10* (with md5 checksum b35adb5213ca9657e911e9befb180842) *11* *12* Information and Software Systems Research *13* AT&T Research *14* Florham Park NJ *15* *16* Glenn Fowler <[email protected]> *17* David Korn <[email protected]> *18* Phong Vo <[email protected]> *19* *20***********************************************************************/21#pragma prototyped22/*23* Glenn Fowler24* AT&T Research25*26* multi-pass commmand line option parse assist27*28* int fun(char** argv, int last)29*30* each fun() argument parses as much of argv as31* possible starting at (opt_info.index,opt_info.offset) using32* optget()33*34* if last!=0 then fun is the last pass to view35* the current arg, otherwise fun sets opt_info.again=136* and another pass will get a crack at it37*38* 0 fun() return causes immediate optjoin() 0 return39*40* optjoin() returns non-zero if more args remain41* to be parsed at opt_info.index42*/4344#include <optlib.h>4546typedef int (*Optpass_f)(char**, int);4748int49optjoin(char** argv, ...)50{51va_list ap;52register Optpass_f fun;53register Optpass_f rep;54Optpass_f err;55Optstate_t* state;56int r;57int more;58int user;59int last_index;60int last_offset;61int err_index;62int err_offset;6364state = optstate(&opt_info);65err = rep = 0;66r = -1;67while (r < 0)68{69va_start(ap, argv);70state->join = 0;71while (fun = va_arg(ap, Optpass_f))72{73last_index = opt_info.index;74last_offset = opt_info.offset;75state->join++;76user = (*fun)(argv, 0);77more = argv[opt_info.index] != 0;78if (!opt_info.again)79{80if (!more)81{82state->join = 0;83r = 0;84break;85}86if (!user)87{88if (*argv[opt_info.index] != '+')89{90state->join = 0;91r = 1;92break;93}94opt_info.again = -1;95}96else97err = 0;98}99if (opt_info.again)100{101if (opt_info.again > 0 && (!err || err_index < opt_info.index || err_index == opt_info.index && err_offset < opt_info.offset))102{103err = fun;104err_index = opt_info.index;105err_offset = opt_info.offset;106}107opt_info.again = 0;108opt_info.index = state->pindex ? state->pindex : 1;109opt_info.offset = state->poffset;110}111if (!rep || opt_info.index != last_index || opt_info.offset != last_offset)112rep = fun;113else if (fun == rep)114{115if (!err)116{117state->join = 0;118r = 1;119break;120}121(*err)(argv, 1);122opt_info.offset = 0;123}124}125va_end(ap);126}127return r;128}129130131