Watt-32 tcp/ip  2.2 dev-rel.10
getopt.c
Go to the documentation of this file.
1 
5 /* (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes
6  * Adapted for Watt-32 TCP/IP by G. Vanem Nov-96
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 
13 #include "wattcp.h"
14 #include "language.h"
15 #include "strings.h"
16 
17 char *watt_optarg = NULL;
18 int watt_optopt = 0;
19 int watt_optind = 0; /* Default: first call */
20 int watt_opterr = 1; /* Default: error messages enabled */
21 char *watt_optswchar = "-/"; /* '-' or '/' starts options */
22 enum _watt_optmode watt_optmode = GETOPT_UNIX;
23 
24 static char *next_opt; /* Next character in cluster of options */
25 static char *empty = ""; /* Empty string */
26 
27 static BOOL done;
28 static char sw_char;
29 
30 static char **options; /* List of entries which are options */
31 static char **non_options; /* List of entries which are not options */
32 static int options_count;
33 static int non_options_count;
34 
35 #define PUT(dst) do { \
36  if (watt_optmode == GETOPT_ANY) \
37  dst[dst##_count++] = argv[watt_optind]; \
38  } while (0)
39 
40 #undef ERROR
41 
42 #if defined(USE_DEBUG)
43  #define ERROR(str,fmt,ch) printf (str), printf (fmt, ch), puts ("")
44 #else /* avoid pulling in printf() */
45  #define ERROR(str,fmt,ch) outsnl (str)
46 #endif
47 
48 
49 int W32_CALL watt_getopt (int argc, char *const *_argv, const char *opt_str)
50 {
51  char c;
52  char *q;
53  int i, j;
54  char **argv = (char **) _argv;
55 
56  if (watt_optind == 0)
57  {
58  watt_optind = 1;
59  done = FALSE;
60  next_opt = empty;
61  if (watt_optmode == GETOPT_ANY)
62  {
63  options = malloc (argc * sizeof(char*));
64  non_options = malloc (argc * sizeof(char*));
65  if (!options || !non_options)
66  {
67  outsnl ("out of memory (getopt)");
68  exit (255);
69  }
70  options_count = 0;
71  non_options_count = 0;
72  }
73  }
74  if (done)
75  return (EOF);
76 
77 restart:
78  watt_optarg = NULL;
79  if (*next_opt == 0)
80  {
81  if (watt_optind >= argc)
82  {
83  if (watt_optmode == GETOPT_ANY)
84  {
85  j = 1;
86  for (i = 0; i < options_count; ++i)
87  argv[j++] = options[i];
88  for (i = 0; i < non_options_count; ++i)
89  argv[j++] = non_options[i];
90  watt_optind = options_count+1;
91  free (options);
92  free (non_options);
93  }
94  done = TRUE;
95  return (EOF);
96  }
97  else if (!strchr (watt_optswchar, argv[watt_optind][0]) || argv[watt_optind][1] == 0)
98  {
99  if (watt_optmode == GETOPT_UNIX)
100  {
101  done = TRUE;
102  return (EOF);
103  }
104  PUT (non_options);
105  watt_optarg = argv[watt_optind++];
106  if (watt_optmode == GETOPT_ANY)
107  goto restart;
108  /* watt_optmode==GETOPT_KEEP */
109  return (0);
110  }
111  else if (argv[watt_optind][0] == argv[watt_optind][1] && argv[watt_optind][2] == 0)
112  {
113  if (watt_optmode == GETOPT_ANY)
114  {
115  j = 1;
116  for (i = 0; i < options_count; ++i)
117  argv[j++] = options[i];
118  argv[j++] = argv[watt_optind];
119  for (i = 0; i < non_options_count; ++i)
120  argv[j++] = non_options[i];
121  for (i = watt_optind+1; i < argc; ++i)
122  argv[j++] = argv[i];
123  watt_optind = options_count + 2;
124  free (options);
125  free (non_options);
126  }
127  ++watt_optind;
128  done = TRUE;
129  return (EOF);
130  }
131  else
132  {
133  PUT (options);
134  sw_char = argv[watt_optind][0];
135  next_opt = argv[watt_optind] + 1;
136  }
137  }
138  c = *next_opt++;
139  if (*next_opt == 0) /* Move to next argument if end of argument reached */
140  watt_optind++;
141  if (c == ':' || (q = strchr (opt_str, c)) == NULL)
142  {
143  if (watt_opterr)
144  {
145  if (c < ' ' || c >= 127)
146  ERROR ("Invalid option", "; character code=0x%.2x", c);
147  else ERROR ("Invalid option", " -%c", c);
148  }
149  watt_optopt = '?';
150  return ('?');
151  }
152  if (q[1] == ':')
153  {
154  if (*next_opt != 0) /* Argument given */
155  {
156  watt_optarg = next_opt;
157  next_opt = empty;
158  ++watt_optind;
159  }
160  else if (q[2] == ':')
161  watt_optarg = NULL; /* Optional argument missing */
162  else if (watt_optind >= argc)
163  { /* Required argument missing */
164  if (watt_opterr)
165  ERROR ("No argument for option", " `-%c'", c);
166  c = '?';
167  }
168  else
169  {
170  PUT (options);
171  watt_optarg = argv[watt_optind++];
172  }
173  }
174  watt_optopt = c;
175  return (c);
176 }
177 
178 
179 #if defined(WATT32_ON_WINDOWS)
180 
182 W32_DATA wchar_t *_w_watt_optarg;
183 W32_DATA wchar_t *_w_watt_optswchar;
184 
185 static wchar_t *_w_next_opt; /* Next character in cluster of options */
186 static wchar_t *_w_empty = L""; /* Empty string */
187 static wchar_t _w_sw_char;
188 
189 static wchar_t **_w_options; /* List of entries which are options */
190 static wchar_t **_w_non_options; /* List of entries which are not options */
191 
192 int W32_CALL _w_watt_getopt (int argc, wchar_t *const *argv, const wchar_t *opt_str)
193 {
194  return (0);
195 }
196 #endif
197 
198 
Core definitions.
W32_DATA wchar_t * _w_watt_optarg
Definition: getopt.c:182