Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/preferences/src/lib/argp/argp-test.c @ 6403

Last change on this file since 6403 was 6393, checked in by rennerc, 19 years ago

added libargp to orxonox

File size: 6.3 KB
Line 
1/* Test program for argp argument parser
2   Copyright (C) 1997 Free Software Foundation, Inc.
3   This file is part of the GNU C Library.
4   Written by Miles Bader <miles@gnu.ai.mit.edu>.
5
6   The GNU C Library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Library General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10
11   The GNU C Library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   Library General Public License for more details.
15
16   You should have received a copy of the GNU Library General Public
17   License along with the GNU C Library; see the file COPYING.LIB.  If not,
18   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   Boston, MA 02111-1307, USA.  */
20
21#ifndef _GNU_SOURCE
22# define _GNU_SOURCE  1
23#endif
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28
29#include <stdlib.h>
30#include <time.h>
31#include <string.h>
32#include <stdio.h>
33
34#include "lib/argp/argp.h"
35
36#if !HAVE_ASPRINTF
37#include <stdarg.h>
38
39static int
40asprintf (char **result, const char *format, ...)
41{
42  size_t size;
43  char *p;
44
45  for (size = 200, p = NULL;; size *= 2)
46  {
47    va_list args;
48    int written;
49
50    p = realloc(p, size + 1);
51    if (!p)
52    {
53      fprintf(stderr, "Virtual memory exhausted.\n");
54      abort();
55    }
56
57    p[size] = '\0';
58
59    va_start(args, format);
60    written = vsnprintf(p, size, format, args);
61    va_end(args);
62
63    if (written >= 0)
64    {
65      *result = p;
66      return written;
67    }
68  }
69}
70#endif /* !HAVE_ASPRINTF */
71
72const char *argp_program_version = "argp-test 1.0";
73
74struct argp_option sub_options[] =
75{
76  {"subopt1",       's',     0,  0, "Nested option 1", 0},
77  {"subopt2",       'S',     0,  0, "Nested option 2", 0},
78
79  { 0, 0, 0, 0, "Some more nested options:", 10},
80  {"subopt3",       'p',     0,  0, "Nested option 3", 0},
81
82  {"subopt4",       'q',     0,  0, "Nested option 4", 1},
83
84  {0, 0, 0, 0, 0, 0}
85};
86
87static const char sub_args_doc[] = "STRING...\n-";
88static const char sub_doc[] = "\vThis is the doc string from the sub-arg-parser.";
89
90static error_t
91sub_parse_opt (int key, char *arg, struct argp_state *state UNUSED)
92{
93  switch (key)
94    {
95    case ARGP_KEY_NO_ARGS:
96      printf ("NO SUB ARGS\n");
97      break;
98    case ARGP_KEY_ARG:
99      printf ("SUB ARG: %s\n", arg);
100      break;
101
102    case 's' : case 'S': case 'p': case 'q':
103      printf ("SUB KEY %c\n", key);
104      break;
105
106    default:
107      return ARGP_ERR_UNKNOWN;
108    }
109  return 0;
110}
111
112static char *
113sub_help_filter (int key, const char *text, void *input UNUSED)
114{
115  if (key == ARGP_KEY_HELP_EXTRA)
116    return strdup ("This is some extra text from the sub parser (note that it \
117is preceded by a blank line).");
118  else
119    return (char *)text;
120}
121
122static struct argp sub_argp = {
123  sub_options, sub_parse_opt, sub_args_doc, sub_doc, 0, sub_help_filter, 0
124};
125
126/* Structure used to communicate with the parsing functions.  */
127struct params
128{
129  unsigned foonly;    /* Value parsed for foonly.  */
130  unsigned foonly_default;  /* Default value for it.  */
131};
132
133#define OPT_PGRP 1
134#define OPT_SESS 2
135
136struct argp_option options[] =
137{
138  {"pid",       'p',     "PID", 0, "List the process PID", 0},
139  {"pgrp",      OPT_PGRP,"PGRP",0, "List processes in the process group PGRP", 0},
140  {"no-parent", 'P',   0,     0, "Include processes without parents", 0},
141  {0,           'x',     0,     OPTION_ALIAS, NULL, 0},
142  {"all-fields",'Q',     0,     0, "Don't elide unusable fields (normally"
143           " if there's some reason ps can't"
144           " print a field for any process, it's"
145           " removed from the output entirely)", 0},
146  {"reverse",   'r',    0,      0, "Reverse the order of any sort", 0},
147  {"gratuitously-long-reverse-option", 0, 0, OPTION_ALIAS, NULL, 0},
148  {"session",  OPT_SESS,"SID",  OPTION_ARG_OPTIONAL,
149           "Add the processes from the session"
150           " SID (which defaults to the sid of"
151           " the current process)", 0},
152
153  {0,0,0,0, "Here are some more options:", 0},
154  {"foonly", 'f', "ZOT", OPTION_ARG_OPTIONAL, "Glork a foonly", 0},
155  {"zaza", 'z', 0, 0, "Snit a zar", 0},
156
157  {0, 0, 0, 0, 0, 0}
158};
159
160static const char args_doc[] = "STRING";
161static const char doc[] = "Test program for argp."
162 "\vThis doc string comes after the options."
163 "\nHey!  Some manual formatting!"
164 "\nThe current time is: %s";
165
166static void
167popt (int key, char *arg)
168{
169  char buf[10];
170  if (isprint (key))
171    sprintf (buf, "%c", key);
172  else
173    sprintf (buf, "%d", key);
174  if (arg)
175    printf ("KEY %s: %s\n", buf, arg);
176  else
177    printf ("KEY %s\n", buf);
178}
179
180static error_t
181parse_opt (int key, char *arg, struct argp_state *state)
182{
183  struct params *params = state->input;
184
185  switch (key)
186    {
187    case ARGP_KEY_NO_ARGS:
188      printf ("NO ARGS\n");
189      break;
190
191    case ARGP_KEY_ARG:
192      if (state->arg_num > 0)
193  return ARGP_ERR_UNKNOWN; /* Leave it for the sub-arg parser.  */
194      printf ("ARG: %s\n", arg);
195      break;
196
197    case 'f':
198      if (arg)
199  params->foonly = atoi (arg);
200      else
201  params->foonly = params->foonly_default;
202      popt (key, arg);
203      break;
204
205    case 'p': case 'P': case OPT_PGRP: case 'x': case 'Q':
206    case 'r': case OPT_SESS: case 'z':
207      popt (key, arg);
208      break;
209
210    default:
211      return ARGP_ERR_UNKNOWN;
212    }
213  return 0;
214}
215
216static char *
217help_filter (int key, const char *text, void *input)
218{
219  char *new_text;
220  struct params *params = input;
221
222  if (key == ARGP_KEY_HELP_POST_DOC && text)
223    {
224      time_t now = time (0);
225      asprintf (&new_text, text, ctime (&now));
226    }
227  else if (key == 'f')
228    /* Show the default for the --foonly option.  */
229    asprintf (&new_text, "%s (ZOT defaults to %x)",
230        text, params->foonly_default);
231  else
232    new_text = (char *)text;
233
234  return new_text;
235}
236
237static struct argp_child argp_children[] = {
238  { &sub_argp, 0, 0, 0 }, { 0, 0, 0, 0 }
239};
240
241static struct argp argp = {
242  options, parse_opt, args_doc, doc, argp_children, help_filter, 0
243};
244
245int
246main (int argc, char **argv)
247{
248  struct params params;
249  params.foonly = 0;
250  params.foonly_default = random ();
251  argp_parse (&argp, argc, argv, 0, 0, &params);
252  printf ("After parsing: foonly = %x\n", params.foonly);
253  return 0;
254}
Note: See TracBrowser for help on using the repository browser.