Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 Feb 2003 19:04:09 +0100 (CET)
From:      Stefan Farfeleder <stefan@fafoe.dyndns.org>
To:        FreeBSD-gnats-submit@FreeBSD.org
Cc:        stefan@fafoe.dyndns.org
Subject:   bin/48313: [patch] make yacc(1) use getopt(3)
Message-ID:  <20030215180409.12C386C0@frog.fafoe>

next in thread | raw e-mail | index | archive | help

>Number:         48313
>Category:       bin
>Synopsis:       [patch] make yacc(1) use getopt(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Sat Feb 15 10:10:13 PST 2003
>Closed-Date:
>Last-Modified:
>Originator:     Stefan Farfeleder
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
>Environment:
System: FreeBSD frog.fafoe 5.0-CURRENT FreeBSD 5.0-CURRENT #16: Thu Feb 13 22:23:09 CET 2003 freebsd@frog.fafoe:/freebsd/current/obj/freebsd/current/src/sys/FROG i386


	
>Description:
Instead of using getopt(3) yacc parses the command lines options on its
own.  By using getopt(3) code duplication in the for loop can be
eliminated leading to cleaner and shorter code.
	
>How-To-Repeat:
n/a
	
>Fix:

	

--- yacc.diff begins here ---
Index: main.c
===================================================================
RCS file: /usr/home/ncvs/src/usr.bin/yacc/main.c,v
retrieving revision 1.20
diff -c -u -r1.20 main.c
--- main.c	9 Apr 2002 11:39:05 -0000	1.20
+++ main.c	15 Feb 2003 16:17:55 -0000
@@ -168,32 +168,15 @@
 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 @@
 	    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 @@
 	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];
 }
 
 
--- yacc.diff ends here ---


>Release-Note:
>Audit-Trail:
>Unformatted:

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030215180409.12C386C0>