From owner-freebsd-arch Tue May 7 20: 4:35 2002 Delivered-To: freebsd-arch@freebsd.org Received: from green.bikeshed.org (freefall.FreeBSD.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id A953F37B401; Tue, 7 May 2002 20:04:12 -0700 (PDT) Received: from localhost (green@localhost) by green.bikeshed.org (8.11.6/8.11.6) with ESMTP id g4834BL42647; Tue, 7 May 2002 23:04:12 -0400 (EDT) (envelope-from green@green.bikeshed.org) Message-Id: <200205080304.g4834BL42647@green.bikeshed.org> X-Mailer: exmh version 2.5 07/13/2001 with nmh-1.0.4 To: "J. Mallett" Cc: "David O'Brien" , Garance A Drosihn , arch@FreeBSD.ORG, cvs-all@FreeBSD.ORG, cvs-committers@FreeBSD.ORG Subject: Re: cvs commit: src/usr.bin/sed main.c sed.1 In-Reply-To: Your message of "Wed, 08 May 2002 01:36:53 -0000." <20020508013651.GA28536@FreeBSD.ORG> From: "Brian F. Feldman" Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Date: Tue, 07 May 2002 23:04:11 -0400 Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG "J. Mallett" wrote: > On Tue, May 07, 2002 at 06:27:50PM -0700, David O'Brien wrote: > > On Wed, May 08, 2002 at 12:59:58AM +0000, J. Mallett wrote: > > > > If a user can not be bothered to use '-I .blah' instead of > > > > '-i.blah', then they can continue to use perl for all I care. > > ... > > > I like your logic better than the logic I've been using. > > > If one or two more people will agree to this, I'll happily make > > > the necessary changes. > > > > I do not agree, I WILL not agree. I will commit -i[ext] later if anyone > > takes it away (or fails to add it). The ENTIRE PURPOSE HERE is to add > > "perl -i" functionality. NOT find how many different ways things can be > > proposed. > > But unless people will shut the hell up about allowing -i with no argument, > we cannot achieve that without adding another option one way or the other. > > [...] Trying to move this to arch: Another option would be to extend getopt(3) in what could simply be a very reasonable way. Here's my proposition for making a "clean" version that acts like Perl: Index: Makefile =================================================================== RCS file: /usr2/ncvs/src/usr.bin/sed/Makefile,v retrieving revision 1.4 diff -u -r1.4 Makefile --- Makefile 8 Feb 2002 23:07:35 -0000 1.4 +++ Makefile 8 May 2002 02:49:35 -0000 @@ -3,6 +3,7 @@ PROG= sed SRCS= compile.c main.c misc.c process.c +SRCS+= getopt.c .include Index: main.c =================================================================== RCS file: /usr2/ncvs/src/usr.bin/sed/main.c,v retrieving revision 1.19 diff -u -r1.19 main.c --- main.c 7 May 2002 23:33:44 -0000 1.19 +++ main.c 8 May 2002 02:58:58 -0000 @@ -124,7 +124,7 @@ fflag = 0; inplace = NULL; - while ((c = getopt(argc, argv, "Eae:f:i:n")) != -1) + while ((c = getopt(argc, argv, "Eae:f:i;n")) != -1) switch (c) { case 'E': rflags = REG_EXTENDED; @@ -146,6 +146,8 @@ break; case 'i': inplace = optarg; + if (inplace == NULL) + inplace = ""; break; case 'n': nflag = 1; @@ -182,8 +184,8 @@ usage() { (void)fprintf(stderr, "%s\n%s\n", - "usage: sed script [-Ean] [-i extension] [file ...]", - " sed [-an] [-i extension] [-e script] ... [-f script_file] ... [file ...]"); + "usage: sed script [-Ean] [-i[extension]] [file ...]", + " sed [-an] [-i[extension]] [-e script] ... [-f script_file] ... [file ...]"); exit(1); } @@ -434,20 +436,21 @@ if (*inplace == '\0') { char template[] = "/tmp/sed.XXXXXXXXXX"; - if (mktemp(template) == NULL) - err(1, "mktemp"); + output = mkstemp(template); + if (output == -1) + err(1, "mkstemp"); strlcpy(backup, template, MAXPATHLEN); } else { strlcpy(backup, *filename, MAXPATHLEN); strlcat(backup, inplace, MAXPATHLEN); + output = open(backup, O_WRONLY|O_CREAT); + if (output == -1) + err(1, "open(%s)", backup); } input = open(*filename, O_RDONLY); if (input == -1) err(1, "open(%s)", *filename); - output = open(backup, O_WRONLY|O_CREAT); - if (output == -1) - err(1, "open(%s)", backup); if (fchmod(output, orig.st_mode & ~S_IFMT) == -1) err(1, "chmod"); buffer = malloc(orig.st_size); --- ../../lib/libc/stdlib/getopt.c Thu Sep 6 02:47:20 2001 +++ getopt.c Tue May 7 22:49:22 2002 @@ -93,12 +93,8 @@ "%s: illegal option -- %c\n", __progname, optopt); return (BADCH); } - if (*++oli != ':') { /* don't need argument */ - optarg = NULL; - if (!*place) - ++optind; - } - else { /* need an argument */ + switch (*++oli) { + case ':': /* need an argument */ if (*place) /* no white space */ optarg = place; else if (nargc <= ++optind) { /* no arg */ @@ -115,6 +111,19 @@ optarg = nargv[optind]; place = EMSG; ++optind; + break; + case ';': /* argument optional */ + if (*place) + optarg = place; + else + optarg = NULL; + place = EMSG; + optind++; + break; + default: /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; } return (optopt); /* dump back option letter */ } -- Brian Fundakowski Feldman \'[ FreeBSD ]''''''''''\ <> green@FreeBSD.org <> bfeldman@tislabs.com \ The Power to Serve! \ Opinions expressed are my own. \,,,,,,,,,,,,,,,,,,,,,,\ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message