1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1985-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* David Korn <dgk@research.att.com> *
1N/A* Phong Vo <kpv@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A
1N/A#include <ast.h>
1N/A#include <ast_getopt.h>
1N/A
1N/A#undef _BLD_ast /* enable ast imports since we're user static */
1N/A
1N/A#include <error.h>
1N/A#include <option.h>
1N/A#include <getopt.h>
1N/A#include <ctype.h>
1N/A
1N/Astatic const char* lastoptstring;
1N/Astatic const struct option* lastlongopts;
1N/Astatic char* usage;
1N/Astatic Sfio_t* up;
1N/A
1N/Astatic int lastoptind;
1N/A
1N/Astatic int
1N/Agolly(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex, int flags)
1N/A{
1N/A register char* s;
1N/A register const struct option* o;
1N/A register int c;
1N/A char* t;
1N/A
1N/A if (!up || optstring != lastoptstring || longopts != lastlongopts)
1N/A {
1N/A if (!up && !(up = sfstropen()))
1N/A return -1;
1N/A sfprintf(up, "[-1p%d]", flags);
1N/A t = strdup(optstring);
1N/A for (o = longopts; o->name; o++)
1N/A {
1N/A if (o->flag || o->val <= 0 || o->val > UCHAR_MAX || !isalnum(o->val))
1N/A sfprintf(up, "\n[%d:%s]", UCHAR_MAX + 1 + (o - longopts), o->name);
1N/A else
1N/A {
1N/A sfprintf(up, "\n[%c:%s]", o->val, o->name);
1N/A if (s = strchr(t, o->val))
1N/A {
1N/A *s++ = ' ';
1N/A if (*s == ':')
1N/A {
1N/A *s++ = ' ';
1N/A if (*s == ':')
1N/A *s = ' ';
1N/A }
1N/A }
1N/A }
1N/A if (o->has_arg)
1N/A {
1N/A sfputc(up, ':');
1N/A if (o->has_arg == optional_argument)
1N/A sfputc(up, '?');
1N/A sfprintf(up, "[string]");
1N/A }
1N/A }
1N/A s = t;
1N/A while (c = *s++)
1N/A if (c != ' ')
1N/A {
1N/A sfprintf(up, "\n[%c]", c);
1N/A if (*s == ':')
1N/A {
1N/A sfputc(up, *s);
1N/A if (*++s == ':')
1N/A {
1N/A sfputc(up, '?');
1N/A s++;
1N/A }
1N/A sfputc(up, '[');
1N/A sfputc(up, ']');
1N/A }
1N/A }
1N/A sfputc(up, '\n');
1N/A if (!(usage = sfstruse(up)))
1N/A return -1;
1N/A lastoptstring = optstring;
1N/A lastlongopts = longopts;
1N/A }
1N/A opt_info.index = (optind > 1 || optind == lastoptind) ? optind : 0;
1N/A if (opt_info.index >= argc || !(c = optget((char**)argv, usage)))
1N/A {
1N/A sfstrclose(up);
1N/A up = 0;
1N/A c = -1;
1N/A }
1N/A else
1N/A {
1N/A if (c == ':' || c == '?')
1N/A {
1N/A if (opterr && (!optstring || *optstring != ':'))
1N/A {
1N/A if (!error_info.id)
1N/A error_info.id = argv[0];
1N/A errormsg(NiL, c == '?' ? (ERROR_USAGE|4) : 2, "%s", opt_info.arg);
1N/A }
1N/A optopt = opt_info.option[1];
1N/A c = '?';
1N/A }
1N/A optarg = opt_info.arg;
1N/A if (c < 0)
1N/A {
1N/A o = longopts - c - UCHAR_MAX - 1;
1N/A if (o->flag)
1N/A {
1N/A *o->flag = o->val;
1N/A c = 0;
1N/A }
1N/A else
1N/A c = o->val;
1N/A }
1N/A }
1N/A lastoptind = optind = opt_info.index;
1N/A return c;
1N/A}
1N/A
1N/Aextern int
1N/Agetopt_long(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
1N/A{
1N/A return golly(argc, argv, optstring, longopts, longindex, 2);
1N/A}
1N/A
1N/Aextern int
1N/Agetopt_long_only(int argc, char* const* argv, const char* optstring, const struct option* longopts, int* longindex)
1N/A{
1N/A return golly(argc, argv, optstring, longopts, longindex, 1);
1N/A}