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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Aug 04, 2016 at 07:17:47PM +0200, Harald van Dijk wrote:

> 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

[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux