1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* my-getopt.cc: Part of GNU CSSC.
*
* Copyright (C) 1997,1998,1999, Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
*
* CSSC was originally Based on MySC, by Ross Ridge, which was
* placed in the Public Domain.
*
*
* Members of the class CSSC_Options.
*
*/
/* SCCS insists that options taking arguments have their arguments
* immediately following the keyletter. Most Unix programs allow the
* argument to appear as the following element in argv[], but SCCS is
* different. We aim for total compatibility with SCCS (except the
* error messages and any size limitations wwe might find). So we
* expect the same. This means that the usual character used in
* getopt() to indicate that the keyletter takes an argument (":") is
* not supported. We use "!" instead, to indicate that this is not
* the traditional Unix behaviour. You can define
* INCOMPATIBLE_OPTIONS if you wish to support a new option that has
* the traditional Unix semantics rather than the normal SCCS
* semantics, but I [jay@gnu.org] do not reccomend this.
*/
#undef INCOMPATIBLE_OPTIONS
/* We do, however, support the POSIX idiom of "--" signifying the end
* of the options and the beginning of the filename list. I don't
* think that this presents a real incompatibility problem.
*/
#ifdef __GNUC__
#pragma implementation "my-getopt.h"
#endif
//#include "cssc.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h> /* for exit() */
#endif
#ifdef HAVE_STRING_H
#include <string.h> /* for strchr() */
#endif
#include "my-getopt.h"
#include "cssc.h" // basically, just for the declaration of quit().
#ifdef CONFIG_SCCS_IDS
static const char rcs_id[] = "CSSC $Id: my-getopt.cc,v 1.12 2004/10/03 10:37:57 james_youngman Exp $";
#endif
int
CSSC_Options::next()
{
if (cindex == 0 || *cindex == '\0')
{
if (index >= argc)
{
return END_OF_ARGUMENTS;
}
cindex = argv[index];
// A dash on its own is not a valid option so we have
// reached the end of the arguments.
if (cindex[0] != '-' || cindex[1] == '\0')
{
return END_OF_ARGUMENTS;
}
index++;
// The argument "--" is the POSIX signal for end-of-args.
if (cindex[1] == '-' && cindex[2] == '\0')
{
return END_OF_ARGUMENTS;
}
cindex++;
}
// Collect the argument character.
char c = *cindex++;
// Look for the argument character in the option list.
const char *match = strchr(opts, c);
if (0 == c || 0 == match)
{
if (opterr) // set opterr for verbose error returns.
{
fprintf(stderr, "Unrecognized option '%c'.\n", c);
usage();
exit(opterr);
}
arg = cindex - 1;
return UNRECOGNIZED_OPTION;
}
// match[1] may contain either a modifier for an option,
// or the next option letter in the option list.
// If an option letter is followed by "!", this indicates
// that any argument must follow the option letter immediately.
int takes_arg;
int arg_catenated;
takes_arg = arg_catenated = 0;
#if INCOMPATIBLE_OPTIONS
if (match[1] == ':') // option takes an argument.
{
takes_arg = 1;
arg_catenated = 0;
}
#else
// Check for and warn about colons in the argument spec!
if (NULL != strchr(opts, ':'))
{
fprintf(stderr,
"Programmer error: option list contains a colon.\n"
"This is illegal for SCCS. Please see "
__FILE__
", line number %d in the source code\n",
__LINE__);
exit(3);
}
#endif
if (match[1] == '!') // option takes a concatenated argument.
{
takes_arg = 1;
arg_catenated = 1;
}
if (takes_arg)
{
if (*cindex == '\0' && (!arg_catenated) )
{
// Option is of the form "-X", the argument is next
// in the list.
if (index >= argc)
{
if (opterr)
{
fprintf(stderr, "Option '%c' requires an argument.\n", c);
usage();
exit(opterr);
}
arg = cindex - 1;
return MISSING_ARGUMENT;
}
arg = argv[index++];
}
else
{
// Option is of the form "-Xmumble", the argument
// is "mumble". Here, *cindex could be '\0', meaning
// that the option is a "catenated arg" option like
// sccs-get's -y option, and there is no actual argument.
arg = cindex;
}
cindex = 0;
}
else
{
arg = 0; // no argument taken by this option.
}
return c;
}
int CSSC_Options::get_index(void) const
{
return index;
}
char *CSSC_Options::getarg(void) const
{
return arg;
}
/*
* reorder: rearrange the argv[] array so that the options
* come before the filenames. The internal ordering
* of the options and of the filenames is unchanged.
*/
void CSSC_Options::reorder(void)
{
int i;
char **files, **options;
files = new char*[argc];
options = new char*[argc];
// separate the options from the files.
// we lease argv[0] alone...
for (i=1; i<argc; ++i)
{
if ('-' == argv[i][0])
{
if ('-' == argv[i][1])
{
// -- signals end of options; stuff the remaining options
// into the files list.
files[i] = 0;
options[i] = 0;
for(++i; i<argc; ++i)
{
files[i] = argv[i];
options[i] = 0;
}
}
else
{
// normal option.
options[i] = argv[i];
files[i] = 0;
}
}
else
{
files[i] = argv[i];
options[i] = 0;
}
}
// now merge them, options first then files.
// we leave argv[0] alone.
int n = 1;
for (i=1; i<argc; ++i)
{
if (options[i])
argv[n++] = options[i];
}
for (i=1; i<argc; ++i)
{
if (files[i])
argv[n++] = files[i];
}
for (i=n; i<argc; ++i)
{
argv[i] = 0;
}
argc = n; // we might have removed some...
#if 0
fprintf(stderr, "** args [%d]: ", argc);
for (i=0; i<argc; i++)
{
fprintf(stderr, "%s ", argv[i]);
}
fprintf(stderr, "\n");
#endif
delete[] files;
delete[] options;
}
CSSC_Options::CSSC_Options(int ac, char **av, const char *s, int err)
: argc(ac),
argv(av),
index(1),
cindex(0),
opts(s),
opterr(err)
{
reorder();
}
int CSSC_Options::get_argc(void) const
{
return argc;
}
char **CSSC_Options::get_argv(void) const
{
return argv;
}
/* Local variables: */
/* mode: c++ */
/* End: */
|