summaryrefslogtreecommitdiff
path: root/utils/parse.c
blob: 5334dbdadccd82c21c3ab3226cb0ec02f05e6ad6 (plain)
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
/* Random helpful option parsing functions

   Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.

   Written by Miles Bader <miles@gnu.ai.mit.edu>

   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, 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <error.h>

#include "parse.h"

/* For each string in the comma-separated list in ARG, call ADD_FN; if ARG is
   empty and DEFAULT_ADD_FN isn't NULL, then call DEFAULT_ADD_FN instead. */
error_t
_parse_strlist (char *arg,
		error_t (*add_fn)(const char *str, struct argp_state *state),
		error_t (*default_add_fn)(struct argp_state *state),
		const char *type_name, struct argp_state *state)
{
  if (arg)
    while (isspace(*arg))
      arg++;

  if (arg == NULL || *arg == '\0')
    if (default_add_fn)
      return (*default_add_fn)(state);
    else
      {
	argp_error (state, "Empty %s list", type_name);
	return EINVAL;
      }
  else
    {
      error_t err = 0;
      char *end = arg;

      void mark_end()
	{
	  *end++ = '\0';
	  while (isspace(*end))
	    end++;
	}
      error_t parse_element()
	{
	  char *cur = arg;
	  if (*cur == '\0')
	    {
	      argp_error (state, "Empty element in %s list", type_name);
	      return EINVAL;
	    }
	  arg = end;
	  return (*add_fn)(cur, state);
	}

      while (*end != '\0' && !err)
	switch (*end)
	  {
	  case ' ': case '\t':
	    mark_end();
	    if (*end == ',')
	      mark_end();
	    err = parse_element();
	    break;
	  case ',':
	    mark_end();
	    err = parse_element();
	    break;
	  default:
	    end++;
	  }

      if (! err)
	err = parse_element();

      return err;
    }
}

/* For each string in the comma-separated list in ARG, call ADD_FN; if ARG is
   empty and DEFAULT_FN isn't NULL, then call ADD_FN on the resutl of calling
   DEFAULT_FN instead, otherwise signal an error.  */
error_t
parse_strlist (char *arg,
	       error_t (*add_fn)(const char *str, struct argp_state *state),
	       const char *(*default_fn)(struct argp_state *state),
	       const char *type_name, struct argp_state *state)
{
  error_t default_str_add (struct argp_state *state)
    { return (*add_fn)((*default_fn)(state), state); }
  return _parse_strlist (arg, add_fn, default_str_add, type_name, state);
}

/* For each numeric string in the comma-separated list in ARG, call ADD_FN;
   if ARG is empty and DEFAULT_FN isn't NULL, then call DEF_FN to get a number,
   and call ADD_FN on that, otherwise signal an error.  If any member of the
   list isn't a number, and LOOKUP_FN isn't NULL, then it is called to return
   an integer for the string.  LOOKUP_FN should signal an error itself it
   there's some problem parsing the string.  */
error_t
parse_numlist (char *arg,
	       error_t (*add_fn)(unsigned num, struct argp_state *state),
	       int (*default_fn)(struct argp_state *state),
	       int (*lookup_fn)(const char *str, struct argp_state *state),
	       const char *type_name, struct argp_state *state)
{
  error_t default_num_add() { return (*add_fn)((*default_fn)(state), state); }
  error_t add_num_str(const char *str, struct argp_state *state)
    {
      const char *p;
      for (p = str; *p != '\0'; p++)
	if (!isdigit(*p))
	  {
	    if (lookup_fn)
	      return (*add_fn)((*lookup_fn)(str, state), state);
	    else
	      {
		argp_error (state, "%s: Invalid %s", p, type_name);
		return EINVAL;
	      }
	    return 0;
	  }
      return (*add_fn) (atoi (str), state);
    }
  return _parse_strlist(arg, add_num_str, default_fn ? default_num_add : 0,
			type_name, state);
}

/* Return the index of which of a set of strings ARG matches, including
   non-ambiguous partial matches.  CHOICE_FN should be a function that
   returns the name of the Nth option, or 0 if N is out of range, and KIND
   should be a string that describes what's being matched, for use in error
   messages.  If ALLOW_MISMATCHES is true, then -1 is returned in the event
   that ARG matches no entry , otherwise, an error message is printed and the
   program exits in this event.  If ARG is an ambiguous match, an error
   message is printed and the program exits.  */
int
parse_enum (const char *arg,
	    const char *(*choice_fn)(unsigned n),
            const char *kind, int allow_mismatches,
	    struct argp_state *state)
{
  const char *choice;
  int arglen = strlen (arg);
  int n = 0;
  int partial_match = -1;

  while ((choice = (*choice_fn)(n)) != NULL)
    if (strcasecmp (choice, arg) == 0)
      return n;
    else
      {
	if (strncasecmp (choice, arg, arglen) == 0)
	  {
	    if (partial_match >= 0)
	      {
		argp_error (state, "%s: Ambiguous %s", arg, kind);
		return -1;
	      }
	    else
	      partial_match = n;
	  }
	n++;
      }

  if (partial_match < 0 && !allow_mismatches)
    argp_error (state, "%s: Invalid %s", arg, kind);

  return partial_match;
}