Skip to contentSkip to conversation

mkbuiltins: Use a `while` loop rather than `nl`

PATCH In review Kylie McClain 5 participants 9 messages Aug 4, 2016 – Aug 8, 2016

lore
Share & tools
lore
#1 lore raw

PATCH 2/2 histedit: Remove non-glibc fallback code

In reply to Kylie McClain

From: Kylie McClain <[email protected]>

This fallback code seems to go back the import of the repository back in 2005, it fails on musl libc, and there aren't any comments explaining why this difference is needed. Regardless, any compatibility ifdefs should probably be checking macros defined on systems that need a different code path, rather than just having fallback code for non-glibc.

---
 src/histedit.c | 4 ----
 1 file changed, 4 deletions(-)
src/histedit.c +0 -4
index 94465d7..4498cf4 100644
@@ -214,11 +214,7 @@ histcmd(int argc, char **argv)
 	if (argc == 1)
 		sh_error("missing history argument");
 
-#ifdef __GLIBC__
 	optind = 0;
-#else
-	optreset = 1; optind = 1; /* initialize getopt */
-#endif
 	while (not_fcnumber(argv[optind]) &&
 	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
 		switch ((char)ch) {
Signature · 1 line
2.9.0
#0 lore raw

PATCH 1/2 mkbuiltins: Use a `while` loop rather than `nl`

From: Kylie McClain <[email protected]>

nl, while specified in POSIX, is rather obscure and isn't provided by small coreutils implementations such as busybox. This while loop works just as well for our purposes.

---
 src/mkbuiltins | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/mkbuiltins +2 -1
index b4d6f4e..a47bce8 100644
@@ -101,7 +101,8 @@ cat <<\!
  */
 
 !
-sed 's/	-[a-z]*//' $temp2 | nl -ba -v0 |
+sed 's/	-[a-z]*//' $temp2 | while read line;do \
+	i=$(( ${i:--1} + 1 )); printf '%s %s\n' "${i}" "${line}";done |
 	LC_ALL= LC_COLLATE=C sort -u -k 3,3 |
 	tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ |
 	awk '{	printf "#define %s (builtincmd + %d)\n", $3, $1}'
Signature · 1 line
2.9.0
#2 lore raw

Re: [PATCH 2/2] histedit: Remove non-glibc fallback code

In reply to Kylie McClain
Quoted 8 lines · Kylie McClain

From: Kylie McClain <[email protected]>

This fallback code seems to go back the import of the repository back in 2005, it fails on musl libc, and there aren't any comments explaining why this difference is needed. Regardless, any compatibility ifdefs should probably be checking macros defined on systems that need a different code path, rather than just having fallback code for non-glibc.

Setting optreset is required on FreeBSD and NetBSD which do not recognize setting optind to 0. POSIX specifies neither of these methods to reset getopt().

However, removing the dependency on this feature is simple. The shell already features a function nextopt() that can be used to parse options and is automatically set up for builtins.

From 23da600dcff616662a93f307420d9142598e2cae Mon Sep 17 00:00:00 2001
From: Jilles Tjoelker <[email protected]>
Date: Thu, 4 Aug 2016 17:51:12 +0200
Subject: [PATCH 1/2] [HISTEDIT] Stop depending on getopt reset feature.

Instead, use our own nextopt() function.

---
 src/histedit.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
src/histedit.c +1 -6
index 94465d7..ec45065 100644
@@ -214,13 +214,8 @@ histcmd(int argc, char **argv)
 	if (argc == 1)
 		sh_error("missing history argument");
 
-#ifdef __GLIBC__
-	optind = 0;
-#else
-	optreset = 1; optind = 1; /* initialize getopt */
-#endif
 	while (not_fcnumber(argv[optind]) &&
-	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
+	      (ch = nextopt(":e:lnrs")) != '\0')
 		switch ((char)ch) {
 		case 'e':
 			editor = optionarg;
Signature · 4 lines
2.9.2

-- 
Jilles Tjoelker
#4 lore raw
In reply to Kylie McClain
Quoted 5 lines · Kylie McClain

From: Kylie McClain <[email protected]>

nl, while specified in POSIX, is rather obscure and isn't provided by small coreutils implementations such as busybox. This while loop works just as well for our purposes.

...

src/mkbuiltins cat <<\! · line 105

Quoted 3 lines
src/mkbuiltins +2 -1
-sed 's/	-[a-z]*//' $temp2 | nl -ba -v0 |
+sed 's/	-[a-z]*//' $temp2 | while read line;do \
+	i=$(( ${i:--1} + 1 )); printf '%s %s\n' "${i}" "${line}";done |

$(( ... )) is mentioned in <https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Shell-Substitutions.html> as not universally supported, notably Solaris 10 /bin/sh does not have it. Given that dash was fairly recently changed to make it build on Solaris 9, it seems like a mistake to break that again.

Aside from that, i is such a common variable name that it seems risky to assume it is unset. I know I've set it myself in shell sessions that I ended up using for building dash. I never exported it, so it wouldn't break here, but it doesn't seem like a stretch that someone else does export it.

Cheers,
Harald van Dijk

#5 lore raw
Quoted 2 lines · Harald van Dijk

Given that dash was fairly recently changed to make it build on Solaris 9, it seems like a mistake to break that again.

Hello,

This is an attempt to simplify the current implementation. This one does not require any temporary file anymore and relies only on sort and (a rather basic usage of) awk.

I've checked the the Opensolaris' awk manual, trying to not introduce some unsupported syntax. I still wonder about the escaped newlines and the (currently commented) close() statements, but both can be easily addressed. So, if someone thinks it's worth testing...

++
Seb.

#!/bin/sh

LC_ALL=C # Force the collate order of the builtins.
export LC_ALL # some shells may not support the "export FOO=bar" form.

awk '

(NF && ($1 !~ /^#/)) {
    # command [options] alias1 [[options] alias2] ...
    for (i = 2; i <= NF; i++) {
        mask = 0
        cmd = $1
        if ($i ~ /^-/) {
            if ($i ~ /n/)
                cmd = "NULL"
            if ($i ~ /s/)
                mask += 1
            if ($i ~ /[su]/)
                mask += 2
            if ($i ~ /a/)
                mask += 4
            i++
        }
        print $i, cmd, mask, $1
    }
}' $1 | sort -k1,1 | awk '
BEGIN {
    BUILTINS_H = "./builtins.h"
    BUILTINS_C = "./builtins.c"
    warn = "/*\n * This file was generated by the mkbuiltins program.\n */\n"
    print  warn >BUILTINS_H
    print  warn "\n#include \"shell.h\"" \
                "\n#include \"builtins.h\"\n" >BUILTINS_C
}
(!($NF in DEFINE)) {
    up = $NF # /bin/awk has no toupper() on Solaris.
    gsub(/a/, "A", up); gsub(/j/, "J", up); gsub(/s/, "S", up)
    gsub(/b/, "B", up); gsub(/k/, "K", up); gsub(/t/, "T", up)
    gsub(/c/, "C", up); gsub(/l/, "L", up); gsub(/u/, "U", up)
    gsub(/d/, "D", up); gsub(/m/, "M", up); gsub(/v/, "V", up)
    gsub(/e/, "E", up); gsub(/n/, "N", up); gsub(/w/, "W", up)
    gsub(/f/, "F", up); gsub(/o/, "O", up); gsub(/x/, "X", up)
    gsub(/g/, "G", up); gsub(/p/, "P", up); gsub(/y/, "Y", up)
    gsub(/h/, "H", up); gsub(/q/, "Q", up); gsub(/z/, "Z", up)
    gsub(/i/, "I", up); gsub(/r/, "R", up)


    print "#define "up" (builtincmd + "(NR-1)")" >BUILTINS_H
    print "int "$NF"(int, char **);" >BUILTINS_C
    DEFINE[$NF]
}
{
    CMD[NR] = "\""$1"\", "$2", "$3
}
END {
    print "\n#define NUMBUILTINS "NR"\n" \
          "\n#define BUILTIN_SPECIAL 0x1" \
          "\n#define BUILTIN_REGULAR 0x2" \
          "\n#define BUILTIN_ASSIGN  0x4\n" \
          "\nstruct builtincmd {" \
          "\n    const char *name;" \
          "\n    int (*builtin)(int, char **);" \
          "\n    unsigned flags;" \
          "\n};" \
          "\n\nextern const struct builtincmd builtincmd[];" >BUILTINS_H
    # close(BUILTINS_H) # not supported on Solaris ? 

    print "\nconst struct builtincmd builtincmd[] = {" >BUILTINS_C
    for (i = 1; i <= NR; i++)
        print "\t{ "CMD[i]" }," >BUILTINS_C
    print "};" >BUILTINS_C
    # close(BUILTINS_C)
}'

# EoF

#7 lore raw
In reply to Harald van Dijk
Quoted 9 lines · Harald van Dijk
Quoted 7 lines · Kylie McClain

From: Kylie McClain <[email protected]>

nl, while specified in POSIX, is rather obscure and isn't provided by small
coreutils implementations such as busybox. This while loop works just as
well for our purposes.

...

Quoted 9 lines

src/mkbuiltins cat <<\! · line 105

Quoted 3 lines
src/mkbuiltins +2 -1
-sed 's/    -[a-z]*//' $temp2 | nl -ba -v0 |
+sed 's/    -[a-z]*//' $temp2 | while read line;do \
+    i=$(( ${i:--1} + 1 )); printf '%s %s\n' "${i}" "${line}";done |

$(( ... )) is mentioned in
<https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Shell-Substitutions.html>
as not universally supported, notably Solaris 10 /bin/sh does not have it. Given that dash was fairly recently changed to make it build on Solaris 9, it seems like a mistake to break that again.

I just realised that that particular change to make it build on Solaris 9 involved avoiding running mkbuiltins using sh, choosing to use $SHELL instead, as $SHELL should already have been picked by configure as something more modern. So the use of $(( ... )) should probably not be a problem after all...

Quoted 5 lines

Aside from that, i is such a common variable name that it seems risky to assume it is unset. I know I've set it myself in shell sessions that I ended up using for building dash. I never exported it, so it wouldn't break here, but it doesn't seem like a stretch that someone else does export it.

...as long as i is simply initialised before the loop.

Cheers,
Harald van Dijk

#6 lore raw
In reply to Seb
Quoted 1 line · Seb

This is an attempt to simplify the current implementation [...]

BTW, I wonder if there is not a (virtual) bug with the current mkbuiltins. Look at the TRUECMD macro in the generated builtins.h:

#define TRUECMD (builtincmd + 1)

It doesn't point on "true" (mask=2, "special builtin") but on ":" (mask=3, "standard builtin"), whatever the order of the aliases you set in builtins.def.in. Maybe it's a mere problem with the macro name ("TRUECMD" should rather be "COLONCMD"), but I don't know if it is really an intended behaviour as the result is finally a matter of sorting.

++
Seb.

#8 lore raw
In reply to Harald van Dijk
Quoted 17 lines · Harald van Dijk
Quoted 5 lines · Kylie McClain

From: Kylie McClain <[email protected]>

nl, while specified in POSIX, is rather obscure and isn't provided by small coreutils implementations such as busybox. This while loop works just as well for our purposes.

...

src/mkbuiltins cat <<\! · line 105

Quoted 3 lines
src/mkbuiltins +2 -1
-sed 's/      -[a-z]*//' $temp2 | nl -ba -v0 |
+sed 's/      -[a-z]*//' $temp2 | while read line;do \
+     i=$(( ${i:--1} + 1 )); printf '%s %s\n' "${i}" "${line}";done |

$(( ... )) is mentioned in
<https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Shell-Substitutions.html>
as not universally supported, notably Solaris 10 /bin/sh does not have it. Given that dash was fairly recently changed to make it build on Solaris 9, it seems like a mistake to break that again.

Quoted 5 lines

Aside from that, i is such a common variable name that it seems risky to assume it is unset. I know I've set it myself in shell sessions that I ended up using for building dash. I never exported it, so it wouldn't break here, but it doesn't seem like a stretch that someone else does export it.

nl has been around forever. I'm not taking this patch.

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#3 lore raw

Re: [PATCH 2/2] histedit: Remove non-glibc fallback code

In reply to Jilles Tjoelker
Quoted 19 lines · Jilles Tjoelker
Quoted 1 line

From 23da600dcff616662a93f307420d9142598e2cae Mon Sep 17 00:00:00 2001

From: Jilles Tjoelker <[email protected]>
Date: Thu, 4 Aug 2016 17:51:12 +0200
Subject: [PATCH 1/2] [HISTEDIT] Stop depending on getopt reset feature.

Instead, use our own nextopt() function.

---
 src/histedit.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
src/histedit.c +0 -1
index 94465d7..ec45065 100644
@@ -214,13 +214,8 @@ histcmd(int argc, char **argv)
 	if (argc == 1)
 		sh_error("missing history argument");
 
-#ifdef __GLIBC__

src/histedit.c histcmd(int argc, char **argv) · line 221

Quoted 10 lines
src/histedit.c +1 -5
-	optind = 0;
-#else
-	optreset = 1; optind = 1; /* initialize getopt */
-#endif
 	while (not_fcnumber(argv[optind]) &&
-	      (ch = getopt(argc, argv, ":e:lnrs")) != -1)
+	      (ch = nextopt(":e:lnrs")) != '\0')
 		switch ((char)ch) {
 		case 'e':
 			editor = optionarg;

This is clearly wrong; not_fcnumber() should be passed *argptr instead of something bogus depending on optind.

The fixed version is what FreeBSD sh has as of SVN r240541 but I have not tested it in dash.

In any case, a side effect of this change is a small code size reduction.

Signature · 1 line
Jilles Tjoelker