/[public]/getopt/trunk/getopt.c
ViewVC logotype

Diff of /getopt/trunk/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 322 Revision 359
35 * Version 1.1.0: Tue Jun 30 2000 35 * Version 1.1.0: Tue Jun 30 2000
36 * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz 36 * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz
37 * <misiek@pld.org.pl>) 37 * <misiek@pld.org.pl>)
38 * Version 1.1.4: Mon Nov 7 2005 38 * Version 1.1.4: Mon Nov 7 2005
39 * Fixed a few type's in the manpage 39 * Fixed a few type's in the manpage
40 * Version 1.1.5: Mon Nov 7 2005
41 * Sync with util-linux-2.21, fixed build problems, many new translations
40 */ 42 */
41 43
42/* Exit codes: 44/* Exit codes:
43 * 0) No errors, succesful operation. 45 * 0) No errors, succesful operation.
44 * 1) getopt(3) returned an error. 46 * 1) getopt(3) returned an error.
115 free(BUFFER); 117 free(BUFFER);
116 118
117 if (!quote) { 119 if (!quote) {
118 /* Just copy arg */ 120 /* Just copy arg */
119 BUFFER = xmalloc(strlen(arg) + 1); 121 BUFFER = xmalloc(strlen(arg) + 1);
120
121 strcpy(BUFFER, arg); 122 strcpy(BUFFER, arg);
122 return BUFFER; 123 return BUFFER;
123 } 124 }
124 125
125 /* 126 /*
196 if (opt == LONG_OPT) { 197 if (opt == LONG_OPT) {
197 printf(" --%s", longopts[longindex].name); 198 printf(" --%s", longopts[longindex].name);
198 if (longopts[longindex].has_arg) 199 if (longopts[longindex].has_arg)
199 printf(" %s", normalize(optarg ? optarg : "")); 200 printf(" %s", normalize(optarg ? optarg : ""));
200 } else if (opt == NON_OPT) 201 } else if (opt == NON_OPT)
201 printf(" %s", normalize(optarg)); 202 printf(" %s", normalize(optarg ? optarg : ""));
202 else { 203 else {
203 printf(" -%c", opt); 204 printf(" -%c", opt);
204 charptr = strchr(optstr, opt); 205 charptr = strchr(optstr, opt);
205 if (charptr != NULL && *++charptr == ':') 206 if (charptr != NULL && *++charptr == ':')
206 printf(" %s", normalize(optarg ? optarg : "")); 207 printf(" %s", normalize(optarg ? optarg : ""));
221 * we already sent a message, we just exit with a helpful hint. 222 * we already sent a message, we just exit with a helpful hint.
222 */ 223 */
223static void __attribute__ ((__noreturn__)) parse_error(const char *message) 224static void __attribute__ ((__noreturn__)) parse_error(const char *message)
224{ 225{
225 if (message) 226 if (message)
226 fprintf(stderr, "getopt: %s\n", message); 227 fprintf(stderr, "%s: %s\n", program_invocation_short_name, message);
227 fputs(_("Try `getopt --help' for more information.\n"), stderr); 228 fprintf(stderr, _("Try `%s --help' for more information.\n"),
229 program_invocation_short_name);
228 exit(PARAMETER_EXIT_CODE); 230 exit(PARAMETER_EXIT_CODE);
229} 231}
230 232
231static struct option *long_options = NULL; 233static struct option *long_options = NULL;
232static int long_options_length = 0; /* Length of array */ 234static int long_options_length = 0; /* Length of array */
256 long_options[long_options_nr].name = NULL; 258 long_options[long_options_nr].name = NULL;
257 long_options[long_options_nr].has_arg = 0; 259 long_options[long_options_nr].has_arg = 0;
258 long_options[long_options_nr].flag = NULL; 260 long_options[long_options_nr].flag = NULL;
259 long_options[long_options_nr].val = 0; 261 long_options[long_options_nr].val = 0;
260 262
261 if (long_options_nr) { 263 if (long_options_nr && name) {
262 /* Not for init! */ 264 /* Not for init! */
263 long_options[long_options_nr - 1].has_arg = has_arg; 265 long_options[long_options_nr - 1].has_arg = has_arg;
264 long_options[long_options_nr - 1].flag = NULL; 266 long_options[long_options_nr - 1].flag = NULL;
265 long_options[long_options_nr - 1].val = LONG_OPT; 267 long_options[long_options_nr - 1].val = LONG_OPT;
266 tmp = xmalloc(strlen(name) + 1); 268 tmp = xmalloc(strlen(name) + 1);
320{ 322{
321 fputs(_("Usage: getopt optstring parameters\n"), stderr); 323 fputs(_("Usage: getopt optstring parameters\n"), stderr);
322 fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); 324 fputs(_(" getopt [options] [--] optstring parameters\n"), stderr);
323 fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); 325 fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr);
324 fputs(_(" parameters\n"), stderr); 326 fputs(_(" parameters\n"), stderr);
327 fputs(_("\nOptions:\n"), stderr);
325 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); 328 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr);
326 fputs(_(" -h, --help This small usage guide\n"), stderr); 329 fputs(_(" -h, --help This small usage guide\n"), stderr);
327 fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); 330 fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr);
328 fputs(_(" -n, --name=progname The name under which errors are reported\n"), stderr); 331 fputs(_(" -n, --name <progname> The name under which errors are reported\n"), stderr);
329 fputs(_(" -o, --options=optstring Short options to be recognized\n"), stderr); 332 fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr);
330 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); 333 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr);
331 fputs(_(" -Q, --quiet-output No normal output\n"), stderr); 334 fputs(_(" -Q, --quiet-output No normal output\n"), stderr);
332 fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); 335 fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr);
333 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); 336 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr);
334 fputs(_(" -u, --unqote Do not quote the output\n"), stderr); 337 fputs(_(" -u, --unquote Do not quote the output\n"), stderr);
335 fputs(_(" -V, --version Output version information\n"), stderr); 338 fputs(_(" -V, --version Output version information\n"), stderr);
339 fputc('\n', stderr);
340
336 exit(PARAMETER_EXIT_CODE); 341 exit(PARAMETER_EXIT_CODE);
337} 342}
338 343
339int main(int argc, char *argv[]) 344int main(int argc, char *argv[])
340{ 345{
357 {"alternative", no_argument, NULL, 'a'}, 362 {"alternative", no_argument, NULL, 'a'},
358 {"name", required_argument, NULL, 'n'}, 363 {"name", required_argument, NULL, 'n'},
359 {"version", no_argument, NULL, 'V'}, 364 {"version", no_argument, NULL, 'V'},
360 {NULL, 0, NULL, 0} 365 {NULL, 0, NULL, 0}
361 }; 366 };
362#if WITHOUT_GETTEXT
363#else
364 setlocale(LC_ALL, ""); 367 setlocale(LC_ALL, "");
365 bindtextdomain(PACKAGE, LOCALEDIR); 368 bindtextdomain(PACKAGE, LOCALEDIR);
366 textdomain(PACKAGE); 369 textdomain(PACKAGE);
367#endif
368 370
369 init_longopt(); 371 init_longopt();
370 getopt_long_fp = getopt_long; 372 getopt_long_fp = getopt_long;
371 373
372 if (getenv("GETOPT_COMPATIBLE")) 374 if (getenv("GETOPT_COMPATIBLE"))
428 return TEST_EXIT_CODE; 430 return TEST_EXIT_CODE;
429 case 'u': 431 case 'u':
430 quote = 0; 432 quote = 0;
431 break; 433 break;
432 case 'V': 434 case 'V':
433 printf(_("getopt (enhanced) 1.1.4\n")); 435 printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version);
434 return EXIT_SUCCESS; 436 return EXIT_SUCCESS;
435 case '?': 437 case '?':
436 case ':': 438 case ':':
437 parse_error(NULL); 439 parse_error(NULL);
438 default: 440 default:

Legend:
Removed from v.322  
changed lines
  Added in v.359

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26