From 0d1bf853016b80e3a071b075d6bf4bcc1ebef76e Mon Sep 17 00:00:00 2001 From: Wes Peters Date: Fri, 5 Mar 2004 01:52:09 +0000 Subject: [PATCH] Use getopt instead of hand-rolled argument parsing. Usage remains the same, no man page changes required. PR: bin/48313 Submitted by: Stefan Farfeleder Reviewed by: joe@ --- usr.bin/yacc/main.c | 87 ++++++++------------------------------------- 1 file changed, 15 insertions(+), 72 deletions(-) diff --git a/usr.bin/yacc/main.c b/usr.bin/yacc/main.c index fdfe11bbed36..474de0b19157 100644 --- a/usr.bin/yacc/main.c +++ b/usr.bin/yacc/main.c @@ -168,32 +168,15 @@ getargs(argc, argv) int argc; char *argv[]; { - int i; - char *s; + int ch; - for (i = 1; i < argc; ++i) + while ((ch = getopt(argc, argv, "b:dlo:p:rtv")) != -1) { - s = argv[i]; - if (*s != '-') break; - switch (*++s) + switch (ch) { - case '\0': - input_file = stdin; - if (i + 1 < argc) usage(); - return; - - case '-': - ++i; - goto no_more_options; - case 'b': - if (*++s) - file_prefix = s; - else if (++i < argc) - file_prefix = argv[i]; - else - usage(); - continue; + file_prefix = optarg; + break; case 'd': dflag = 1; @@ -204,22 +187,12 @@ char *argv[]; break; case 'o': - if (*++s) - output_file_name = s; - else if (++i < argc) - output_file_name = argv[i]; - else - usage(); - continue; + output_file_name = optarg; + break; case 'p': - if (*++s) - symbol_prefix = s; - else if (++i < argc) - symbol_prefix = argv[i]; - else - usage(); - continue; + symbol_prefix = optarg; + break; case 'r': rflag = 1; @@ -236,44 +209,14 @@ char *argv[]; default: usage(); } - - for (;;) - { - switch (*++s) - { - case '\0': - goto end_of_option; - - case 'd': - dflag = 1; - break; - - case 'l': - lflag = 1; - break; - - case 'r': - rflag = 1; - break; - - case 't': - tflag = 1; - break; - - case 'v': - vflag = 1; - break; - - default: - usage(); - } - } -end_of_option:; } -no_more_options:; - if (i + 1 != argc) usage(); - input_file_name = argv[i]; + if (optind + 1 != argc) + usage(); + if (strcmp(argv[optind], "-") == 0) + input_file = stdin; + else + input_file_name = argv[optind]; }