~ubuntu-branches/ubuntu/raring/findutils/raring

« back to all changes in this revision

Viewing changes to gnulib/lib/argmatch.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2005-07-04 11:37:37 UTC
  • mto: (11.1.1 lenny) (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050704113737-oxfumqxsqgfz5gay
Tags: upstream-4.2.22
ImportĀ upstreamĀ versionĀ 4.2.22

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* argmatch.c -- find a match for a string in an array
2
2
 
3
 
   Copyright (C) 1990, 1998, 1999, 2001, 2002, 2003 Free Software
4
 
   Foundation, Inc.
 
3
   Copyright (C) 1990, 1998, 1999, 2001, 2002, 2003, 2004, 2005 Free
 
4
   Software Foundation, Inc.
5
5
 
6
6
   This program is free software; you can redistribute it and/or modify
7
7
   it under the terms of the GNU General Public License as published by
15
15
 
16
16
   You should have received a copy of the GNU General Public License
17
17
   along with this program; if not, write to the Free Software Foundation,
18
 
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
19
 
20
20
/* Written by David MacKenzie <djm@ai.mit.edu>
21
21
   Modified by Akim Demaille <demaille@inf.enst.fr> */
27
27
/* Specification.  */
28
28
#include "argmatch.h"
29
29
 
 
30
#include <stdbool.h>
30
31
#include <stdio.h>
31
32
#include <stdlib.h>
32
33
#include <string.h>
35
36
#define _(msgid) gettext (msgid)
36
37
 
37
38
#include "error.h"
 
39
#include "exit.h"
38
40
#include "quotearg.h"
39
41
#include "quote.h"
40
 
#include "unlocked-io.h"
 
42
 
 
43
#if USE_UNLOCKED_IO
 
44
# include "unlocked-io.h"
 
45
#endif
41
46
 
42
47
/* When reporting an invalid argument, show nonprinting characters
43
48
   by using the quoting style ARGMATCH_QUOTING_STYLE.  Do not use
46
51
# define ARGMATCH_QUOTING_STYLE locale_quoting_style
47
52
#endif
48
53
 
49
 
#ifndef EXIT_FAILURE
50
 
# define EXIT_FAILURE 1
51
 
#endif
52
 
 
53
54
/* Non failing version of argmatch call this function after failing. */
54
55
#ifndef ARGMATCH_DIE
55
 
# define ARGMATCH_DIE exit (EXIT_FAILURE)
 
56
# include "exitfail.h"
 
57
# define ARGMATCH_DIE exit (exit_failure)
56
58
#endif
57
59
 
58
60
#ifdef ARGMATCH_DIE_DECL
71
73
 
72
74
 
73
75
/* If ARG is an unambiguous match for an element of the
74
 
   null-terminated array ARGLIST, return the index in ARGLIST
 
76
   NULL-terminated array ARGLIST, return the index in ARGLIST
75
77
   of the matched element, else -1 if it does not match any element
76
78
   or -2 if it is ambiguous (is a prefix of more than one element).
77
79
 
81
83
     "no", "nope" -> 1
82
84
   "y" is a valid argument, for `0', and "n" for `1'.  */
83
85
 
84
 
int
 
86
ptrdiff_t
85
87
argmatch (const char *arg, const char *const *arglist,
86
88
          const char *vallist, size_t valsize)
87
89
{
88
 
  int i;                        /* Temporary index in ARGLIST.  */
 
90
  size_t i;                     /* Temporary index in ARGLIST.  */
89
91
  size_t arglen;                /* Length of ARG.  */
90
 
  int matchind = -1;            /* Index of first nonexact match.  */
91
 
  int ambiguous = 0;            /* If nonzero, multiple nonexact match(es).  */
 
92
  ptrdiff_t matchind = -1;      /* Index of first nonexact match.  */
 
93
  bool ambiguous = false;       /* If true, multiple nonexact match(es).  */
92
94
 
93
95
  arglen = strlen (arg);
94
96
 
112
114
                {
113
115
                  /* There is a real ambiguity, or we could not
114
116
                     disambiguate. */
115
 
                  ambiguous = 1;
 
117
                  ambiguous = true;
116
118
                }
117
119
            }
118
120
        }
129
131
   PROBLEM is the return value from argmatch.  */
130
132
 
131
133
void
132
 
argmatch_invalid (const char *context, const char *value, int problem)
 
134
argmatch_invalid (const char *context, const char *value, ptrdiff_t problem)
133
135
{
134
136
  char const *format = (problem == -1
135
137
                        ? _("invalid argument %s for %s")
147
149
argmatch_valid (const char *const *arglist,
148
150
                const char *vallist, size_t valsize)
149
151
{
150
 
  int i;
 
152
  size_t i;
151
153
  const char *last_val = NULL;
152
154
 
153
155
  /* We try to put synonyms on the same line.  The assumption is that
173
175
   "--version-control", or "$VERSION_CONTROL" etc.).  Upon failure,
174
176
   calls the (supposed never to return) function EXIT_FN. */
175
177
 
176
 
int
 
178
ptrdiff_t
177
179
__xargmatch_internal (const char *context,
178
180
                      const char *arg, const char *const *arglist,
179
181
                      const char *vallist, size_t valsize,
180
182
                      argmatch_exit_fn exit_fn)
181
183
{
182
 
  int res = argmatch (arg, arglist, vallist, valsize);
 
184
  ptrdiff_t res = argmatch (arg, arglist, vallist, valsize);
183
185
  if (res >= 0)
184
186
    /* Success. */
185
187
    return res;
199
201
                      const char *const *arglist,
200
202
                      const char *vallist, size_t valsize)
201
203
{
202
 
  int i;
 
204
  size_t i;
203
205
 
204
206
  for (i = 0; arglist[i]; i++)
205
207
    if (!memcmp (value, vallist + valsize * i, valsize))
212
214
 * Based on "getversion.c" by David MacKenzie <djm@gnu.ai.mit.edu>
213
215
 */
214
216
char *program_name;
215
 
extern const char *getenv ();
216
217
 
217
218
/* When to make backup files.  */
218
219
enum backup_type
219
220
{
220
221
  /* Never make backups.  */
221
 
  none,
 
222
  no_backups,
222
223
 
223
224
  /* Make simple backups of every file.  */
224
 
  simple,
 
225
  simple_backups,
225
226
 
226
227
  /* Make numbered backups of files that already have numbered backups,
227
228
     and simple backups of the others.  */
228
 
  numbered_existing,
 
229
  numbered_existing_backups,
229
230
 
230
231
  /* Make numbered backups of every file.  */
231
 
  numbered
 
232
  numbered_backups
232
233
};
233
234
 
234
235
/* Two tables describing arguments (keys) and their corresponding
244
245
 
245
246
static const enum backup_type backup_vals[] =
246
247
{
247
 
  none, none, none,
248
 
  simple, simple,
249
 
  numbered_existing, numbered_existing,
250
 
  numbered, numbered
 
248
  no_backups, no_backups, no_backups,
 
249
  simple_backups, simple_backups,
 
250
  numbered_existing_backups, numbered_existing_backups,
 
251
  numbered_backups, numbered_backups
251
252
};
252
253
 
253
254
int
254
255
main (int argc, const char *const *argv)
255
256
{
256
257
  const char *cp;
257
 
  enum backup_type backup_type = none;
 
258
  enum backup_type backup_type = no_backups;
258
259
 
259
260
  program_name = (char *) argv[0];
260
261