• C preprocessor wrapper around awk.

    From Kaz Kylheku@480-992-1380@kylheku.com to comp.lang.awk on Thu Mar 17 21:41:36 2022
    From Newsgroup: comp.lang.awk

    Don't want @include? Use #include!

    Quick demo:

    $ cppawk '#define FORTY_TWO 42
    BEGIN { print FORTY_TWO }'
    42

    $ cppawk -DFORTY_TWO=42 'BEGIN { print FORTY_TWO }'
    42

    $ cppawk -f cppawk.test
    42

    $ cat cppawk.test
    #define FORTY_TWO 42
    BEGIN { print FORTY_TWO }

    $ cppawk -f cppawk.test2
    42

    $ cat cppawk.test2
    #include "cppawk.test"

    $ cppawk '#include "cppawk.test2"'
    42

    Code:

    #!/bin/sh

    #
    # cppawk: C preprocessor wrapper around awk
    # Kaz Kylheku <kaz@kylheku.com>, March 2022.
    #
    # Redistribution and use in source and binary forms, with or without
    # modification, are permitted provided that the following conditions are met:
    #
    # 1. Redistributions of source code must retain the above copyright notice,
    # this list of conditions and the following disclaimer.
    #
    # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.
    #
    # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    # POSSIBILITY OF SUCH DAMAGE.
    #

    # site configuration
    prepro=/usr/bin/cpp
    incopt=-iquote # GNU cpp feature: use -I if unavailable awk=/usr/bin/gawk

    # globals
    awk_file=
    awk_opts=
    prepro_opts=
    tmp_file=

    # functions
    shell_escape()
    {
    case $1 in
    *"'"* )
    case $1 in
    *'"'* | *'$'* )
    printf "%s" "'$(printf "%s" "$1" | sed -e "s/'/'\\\\''/g")'"
    ;;
    * )
    printf "%s" "\"$1\""
    ;;
    esac
    ;;
    *'"'* | *['$*?[(){};&|<>#']* | '~'* )
    printf "%s" "'$1'"
    ;;
    *' '* | *' '* )
    printf "%s" "\"$1\""
    ;;
    * )
    printf "%s" "$1"
    ;;
    esac
    }

    die()
    {
    fmt="$0: $1\n" ; shift
    printf "$fmt" "$@"
    exit 1
    }

    while [ $# -gt 0 ] ; do
    case $1 in
    -M* )
    die "-M family of cpp options not supported"
    ;;
    -U* | -D* | -iquote* )
    prepro_opts="$prepro_opts $(shell_escape "$1")"
    ;;
    -f )
    [ $# -gt 1 ] || die "-f requires argument"
    [ -z "$awk_file" ] || die "-f can be only given once"
    awk_file=$2
    shift
    ;;
    -F | -v | -E | -i | -l | -L )
    [ $# -gt 1 ] || die "%s requires argument" $1
    awk_opts="$awk_opts $1 $(shell_escape "$2")"
    shift
    ;;
    -- )
    break
    ;;
    -* )
    awk_opts="$awk_opts $(shell_escape "$1")"
    ;;
    * )
    break
    ;;
    esac
    shift
    done

    trap 'rm -f $tmp_file' EXIT INT TERM

    if [ -n "$awk_file" ] ; then
    tmp_file=$(mktemp)
    $prepro $prepro_opts "$awk_file" > $tmp_file
    $awk $awk_opts -f $tmp_file "$@"
    elif [ $# -gt 0 ] ; then
    tmp_file=$(mktemp)
    printf "%s" "$1" | $prepro $incopt"$(pwd)" $prepro_opts - > $tmp_file; shift
    $awk $awk_opts -f $tmp_file "$@"
    else
    die "awk code must be specified"
    fi
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.lang.awk on Fri Mar 18 00:55:13 2022
    From Newsgroup: comp.lang.awk

    On 17.03.2022 22:41, Kaz Kylheku wrote:
    Don't want @include? Use #include!

    Doesn't @include do more than just lexical text replacement?

    Janis (seriously asking, since I rarely use the @... syntax)


    [...]


    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Kaz Kylheku@480-992-1380@kylheku.com to comp.lang.awk on Fri Mar 18 03:27:10 2022
    From Newsgroup: comp.lang.awk

    On 2022-03-17, Kaz Kylheku <480-992-1380@kylheku.com> wrote:
    Don't want @include? Use #include!

    Quick demo:

    $ cppawk '#define FORTY_TWO 42
    BEGIN { print FORTY_TWO }'
    42

    Needs to recognize and skip #! syntax; the preprocessor
    currently chokes on that as an invalid directive.
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Kaz Kylheku@480-992-1380@kylheku.com to comp.lang.awk on Fri Mar 18 15:25:07 2022
    From Newsgroup: comp.lang.awk

    On 2022-03-17, Kaz Kylheku <480-992-1380@kylheku.com> wrote:
    Don't want @include? Use #include!

    Quick demo:

    $ cppawk '#define FORTY_TWO 42
    BEGIN { print FORTY_TWO }'
    42

    Git repo with test cases.

    https://www.kylheku.com/cgit/cppawk
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Tue Mar 22 20:11:41 2022
    From Newsgroup: comp.lang.awk

    In article <t10hp1$ttj$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    On 17.03.2022 22:41, Kaz Kylheku wrote:
    Don't want @include? Use #include!

    Doesn't @include do more than just lexical text replacement?

    It is hard to tell what exactly is the point of this "cppawk" thing.
    As usual, OP is long on "where" and "how", but not so hot on "why".

    That said, I do kinda see how it (using the C preprocessor to do the heavy lifting) is more powerful than the (rather basic) @include facility built
    into GAWK. But of course, is it more powerful enough to justify anyone
    other than OP adopting it ("it" being "cppawk") into their workflow?
    Unclear.

    But to answer your question: No, I don't really think GAWK's @include does anything more than "lexical text replacement". What more would you want it
    to do?

    P.S. I'm surprised to hear that you don't use @include much. It seems
    pretty much essential to do anything beyond simple command line AWK
    programs. Also, I use @load a lot as well (and I don't see any other replacement for that).

    Do you use the "inplace" editing facility much? That uses the concept of @include (one way or the other).
    --
    People often ask what is the difference between liberals and conservatives.
    It is this. Libs see the government helping them and are OK with the government also helping other people. Cons see the government screwing them and are OK with
    that as long as the government is also screwing other people.
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Kaz Kylheku@480-992-1380@kylheku.com to comp.lang.awk on Tue Mar 22 21:55:01 2022
    From Newsgroup: comp.lang.awk

    On 2022-03-22, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    In article <t10hp1$ttj$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    On 17.03.2022 22:41, Kaz Kylheku wrote:
    Don't want @include? Use #include!

    Doesn't @include do more than just lexical text replacement?

    It is hard to tell what exactly is the point of this "cppawk" thing.
    As usual, OP is long on "where" and "how", but not so hot on "why".

    The "why" is very open ended. An aspect of it is "why not", as well
    as exploration: discover the "why".

    That said, I do kinda see how it (using the C preprocessor to do the heavy lifting) is more powerful than the (rather basic) @include facility built into GAWK. But of course, is it more powerful enough to justify anyone
    other than OP adopting it ("it" being "cppawk") into their workflow?
    Unclear.

    Depends on dependencies too.

    Imagine I say to you, I have this nice cppawk program. Oh, but it's
    written in Rust, just install Cargo and pick up the cppawk crate ...
    Oh good grief.

    But a tiny shell script; what the heck?

    Possible reasons for using it:

    1. You know how to use that preprocessor, and are doing work on a system where
    you can count on it being installed. Use what you know.

    (I will likely be using it in situations when Awk gets used as part of build
    tooling. Reason being: cpp being installed is a given if you're compiling
    stuff with GCC or Clang. Also, in that environment, C preprocessing is much
    more familiar to devs than Awk; I don't have to worry about anyone not
    understanding the preprocessing bits of a piece of code. The cpp dependency
    is not too bad, except in embedded systems: but see point 8).

    2. It makes some things easy, like making a program out of multiple
    files, which easily find each other in the same directory or relative
    path. Or macros, for some syntactic sugars.

    3. Potentially works with different Awks. There is the possibility of using
    #ifdef to make code work on different Awks from one file. (The default
    cppawk installation uses gawk, and also defines __gawk__ 1 that you can
    test for.)

    4. Comments. Awk has no comments that don't end at the end of
    the line; cppawk gives you /*...*/.

    5. Temporarily disabling code with #if 0 ... #endif rather than
    fiddling with hash marks.

    6. Exploration: Awk is syntactically C like, but not C: what
    implications does that have for writing macros? You can discover some
    new-ish techniques, though it won't be earth-shattering.

    7. Weird access to some host attributes intended for C:

    $ cppawk '#include <limits.h>
    BEGIN { print PATH_MAX, ULONG_MAX }'
    4096 214748364701

    this sort of thing looks mildly useful in devops land.

    8. cppawk --prepro-only will generate the preprocessed output for
    you, which can run on a system that has no preprocessor, such
    as an embedded board that has only BusyBox Awk.

    Using -Dfoo=bar macro definitions with --prepro-only and
    preprocessing conditionals/macros lets you generate different
    versions of the program.

    for conf in this that other ; do
    cppawk --prepro-only -Dconfiguration=$conf > $conf.awk
    done

    now I have a this.awk that.awk and other.awk based on the
    same source, but somehow usefully different.
    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From gazelle@gazelle@shell.xmission.com (Kenny McCormack) to comp.lang.awk on Wed Mar 23 01:58:13 2022
    From Newsgroup: comp.lang.awk

    In article <20220322135730.675@kylheku.com>,
    Kaz Kylheku <480-992-1380@kylheku.com> wrote:
    On 2022-03-22, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    In article <t10hp1$ttj$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    On 17.03.2022 22:41, Kaz Kylheku wrote:
    Don't want @include? Use #include!

    Doesn't @include do more than just lexical text replacement?

    It is hard to tell what exactly is the point of this "cppawk" thing.
    As usual, OP is long on "where" and "how", but not so hot on "why".

    The "why" is very open ended. An aspect of it is "why not", as well
    as exploration: discover the "why".

    These are all good reasons. I may indeed check it out!
    --
    Alice was something of a handful to her father, Theodore Roosevelt. He was once asked by a visiting dignitary about parenting his spitfire of a daughter and he replied, "I can be President of the United States, or I can control Alice. I cannot possibly do both."
    --- Synchronet 3.19c-Linux NewsLink 1.113
  • From Janis Papanagnou@janis_papanagnou@hotmail.com to comp.lang.awk on Wed Mar 23 12:37:30 2022
    From Newsgroup: comp.lang.awk

    On 22.03.2022 21:11, Kenny McCormack wrote:
    In article <t10hp1$ttj$1@dont-email.me>,
    Janis Papanagnou <janis_papanagnou@hotmail.com> wrote:
    [...]

    But to answer your question: No, I don't really think GAWK's @include does anything more than "lexical text replacement". What more would you want it to do?

    It's not that I "want" it to do more, it's just that as a built-in I'd
    expected it to do more (without being able to name it, since I don't
    know the internals and consequences of that mechanism).

    (Actually if it's just doing the same as cpp with #include then I'd
    more have expected the separation of duties and use cpp instead of re-implementing that feature. I don't want to discuss that decision,
    though, because I'm not concerned, neither the one way nor the other.)


    P.S. I'm surprised to hear that you don't use @include much. It seems pretty much essential to do anything beyond simple command line AWK
    programs. Also, I use @load a lot as well (and I don't see any other replacement for that).

    With (GNU) awk I indeed don't use it much, rather closer to not at all.
    I recall for some multi-precision arithmetic I had used it in the past
    (loading a module) but I think that's not necessary any more - I recall
    my last use was simpler (just an option?). And maybe it has also been
    necessary for XML-processing (but my use of that feature may even have
    been before it was accessible as module and I might have used the xgawk
    variant of GNU awk).

    Not sure we have the same definition for "essential", and probably also
    a different set of tools and languages typically used. The availability
    of that feature was certainly not a primary concern for me w.r.t. gawk.


    Do you use the "inplace" editing facility much? That uses the concept of @include (one way or the other).

    I seem to recall to have used it once (maybe twice, but not more often).

    Anyway, to each his own. Awk is - for me - not the tool to create large
    or complex software systems.

    Janis

    --- Synchronet 3.19c-Linux NewsLink 1.113