SourceForge has been redesigned. Learn more.
Close

Diff of /libutil/op_popt.c [6bc578] .. [6893b5]  Maximize  Restore

Switch to unified view

a/libutil/op_popt.c b/libutil/op_popt.c
...
...
9
 * @author John Levon
9
 * @author John Levon
10
 * @author Philippe Elie
10
 * @author Philippe Elie
11
 */
11
 */
12
12
13
#include <stdlib.h>
13
#include <stdlib.h>
14
#include <string.h>
14
#include "op_libiberty.h"
15
#include "op_libiberty.h"
15
#include "op_popt.h"
16
#include "op_popt.h"
16
17
17
poptContext op_poptGetContext(char const * name,
18
poptContext op_poptGetContext(char const * name,
18
        int argc, char const ** argv,
19
        int argc, char const ** argv,
...
...
39
        exit(EXIT_FAILURE);
40
        exit(EXIT_FAILURE);
40
    }
41
    }
41
42
42
    return optcon;
43
    return optcon;
43
}
44
}
45
46
poptContext op_poptGetOptions_getApp(char const * name, int argc,
47
                                     char const ** argv,
48
                                     struct poptOption const * options,
49
                                     char **app_options, int flags)
50
{
51
  poptContext optcon;
52
  const char * leftover = NULL;
53
  int c;
54
55
  xmalloc_set_program_name(argv[0]);
56
57
#ifdef CONST_POPT
58
  optcon = poptGetContext(name, argc, argv, options, flags);
59
#else
60
  optcon = poptGetContext((char *)name, argc, (char **)argv, options, flags);
61
#endif
62
63
  c = poptGetNextOpt(optcon);
64
  if (c < 0) {
65
      leftover = poptGetArg(optcon);
66
      if (leftover) {
67
          int arg_idx, app_name_found, length, num_app_args;
68
          char * app_name = (char *)xcalloc(strlen(leftover) + 1, 1);
69
          strncpy(app_name, leftover, strlen(leftover) + 1);
70
          app_options[0] = app_name;
71
          for (arg_idx = 1, app_name_found = 0, length = 0, num_app_args = 0;
72
                  arg_idx < argc;
73
                  arg_idx++) {
74
              if (app_name_found) {
75
                  num_app_args++;
76
                  length += strlen(argv[arg_idx]) + 1;
77
              }
78
              if (!strcmp(argv[arg_idx], app_name)) {
79
                  app_name_found = 1;
80
              }
81
          }
82
          if (num_app_args)
83
              app_options[1] = (char *)xcalloc(length, 1);
84
          else
85
              app_options[1] = "";
86
          for (arg_idx = argc - num_app_args; arg_idx < argc; arg_idx++) {
87
              if (arg_idx > (argc - num_app_args))
88
                  app_options[1] = strcat(app_options[1], " ");
89
              app_options[1] = strcat(app_options[1], argv[arg_idx]);
90
          }
91
      } else if (c < -1) {
92
          fprintf(stderr, "%s: %s: %s\n", argv[0],
93
                  poptBadOption(optcon, POPT_BADOPTION_NOALIAS),
94
                  poptStrerror(c));
95
          poptPrintHelp(optcon, stderr, 0);
96
          exit(EXIT_FAILURE);
97
      }
98
  }
99
100
  return optcon;
101
102
}