Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make JSONPath.sh and test scripts robust #16

Merged
merged 8 commits into from
Aug 11, 2024

Conversation

lcn2
Copy link
Contributor

@lcn2 lcn2 commented Jul 26, 2024

egrep

While we disagree with the notion that egrep is obsolete, tools such as gnu egrep whine when used with the warning:

egrep: warning: egrep is obsolescent; using ggrep -E

Sadly this pull request changes use of egrep to grep -E in order to stop the warnings that gnu egrep now issues.

Add a Makefile so that make install will work.

Updated README.md to mention the make install alternative.

Removed trailing whitespace from README.md.

new command line options

We add a -S option to print spaces around :s'.

The default (w/o -S):

    "submit_slot":2,

With -S:

    "submit_slot" : 2,

We add an -A option start a JSON array on line as JSON member.

The default (w/o -A):

    "authors":
    [

With -A:

    "authors":[

With -S -A:

    "authors" : [

We add -T to indent with tabs.

The default (w/o -T):

    "submit_slot":2,
    "author_count":3,

With -T:

        "submit_slot":2,
        "author_count":3,

The IOCCC plans to use -S -A -T as this is identical to what jparse(1) (and related friends) produce.

Miscellanious

We also removed a few trailing whitespace from the shell script.

For macOS (Darwin) we use use the correct paths for homebrew
in ensure_deps.sh If there is an error under macOS, we
also remind the user what to put in their $PATH.

command line parsing

fix and improve command line parsing and

The command line parsing now uses the bash builtin getopts. Now, and
invalid option is flagged as an error. Improper arguments to command
line options are flagged as errors. The optional pattern is also obtained
after parsing command line options.

The usage message has been updated to include command line options that
were implemented but not documented in the usage message.

Updated the Invocation section of README.md to reflect the current
command line, including command line options that were documented in
the usage message, but not in README.md.

fix test command lines

The original usage message and README.md invocation has the optional
pattern at the end of the command line . We fix 3 tests to place the
pattern at the end of the command line. Moreover, we put -- between
command line options and a pattern to prevent the pattern from being
interpreted as a command line option. And while unlikely to happen,
this would allow pattern that begins with a dash to be correctly parsed.

Ran all-tests.sh to test the above.

improve test scripts

We ran into some issues with the test scripts that were fixed by some
shell script hardening and using some bash best practices. What follows
are fixes and improvements to the test scripts.

We make the tests scripts more robust:

  • detect when the cd fails
  • prevent unquoted shell variable expansion from corrupting execution

We improve the use of bash:

  • use env(1) in the initial #! line to find bash(1) on $PATH

NOTE: macOS has deprecated the distributed bash(1) in favor of zsh(1).
Not all systems have bash(1) as /bin/sh.
We allow for bash(1) to come from home brew or mac ports, or ...

We use bash constructs for slight speed gain and best practices:

  • increment using ((++i)), in some cases avoiding a sub-shell launch
  • bypass use of cat(1) by using $(< "$argpfile")
  • use find(1) to select test files instead of the slower ls(1)
  • avoid using shell glob problems by using find(1) to select test files
  • use $(...) instead of \cmd`` to help avoid data corrupting execution

Use python3(1) instead of just python(1).

NOTE: Not all systems that have python3(1) have python(1).
Some system admins have yet to select the default version of python.
The macOS platform, while they have python3(1) to not have python(1).

The test scripts now pass the shellcheck script analysis tool.
See www.shellcheck.net for details.

Fixed a bug in all-docker-tests.sh where Centos tests were re-displayed
a 2nd time as if they were Debian tests.

Ran ./all-tests.sh to verify the above under macOS and RHEL 9.

improve JSONPath.sh

We make JSONPath.sh much more robust and use bash constructs for slight
speed gain and best practices:

  • prevent unquoted shell variable expansion from corrupting execution
  • compute numerical values using ((var=equation)) instead of let
  • declare local variable before assigning a value
  • fixed how options to grep are accumulated
  • fixed how main is called
  • fixed how an unexpected EOF with token is thrown
  • fixed how array elements are unset
  • fixed how code determines of an array contains only empty values
  • changed cases where cat(1) of a file is piped into feeding the file on stdin
  • use $(...) instead of \cmd`` to help avoid data corrupting execution
  • replaced use of [ ... ] with [[ ... ]]
  • removed shell variables that were set but never unused
  • fixed cases where a case statement lacked a default case
  • fixed how the literal string '"*' is used
  • fixed cases where a scalar is followed by a literal string that starts with [
  • fixed cases where $@ is confused with $*

All errors, warnings and issues reported by shellcheck
have been addressed / fix. Now JSONPath.sh is error, warning and issue free
under shellcheck!

Ran ./all-tests.sh to verify the above under macOS and RHEL 9 Linux.

lcn2 added 3 commits July 25, 2024 21:40
While we disagree with the notion that `egrep` is obsolete,
tools such as gnu egrep whine when used with the warning:

```
egrep: warning: egrep is obsolescent; using ggrep -E
```

Sadly this pull request changes use of `egrep` to `grep -E`
in order to stop the warnings that gnu egrep now issues.
Add a Makefile so that `make install` will work.

Updated `README.md` to mention the `make install` alternative.
Removed trailing whitespace from `README.md`.
We add a `-S` option to print spaces around :s'.

The default (w/o -S):

```json
    "submit_slot":2,
```

With `-S`:

```json
    "submit_slot" : 2,
```

We add an `-A` option start a JSON array on line as JSON member.

The default (w/o -A):

```json
    "authors":
    [
```

With `-A`:

```json
    "authors":[
```

With `-S -A`:

```json
    "authors" : [
```

We add `-T` to indent with tabs.

The default (w/o -T):

```json
    "submit_slot":2,
    "author_count":3,
```

With `-T`:

```json
        "submit_slot":2,
        "author_count":3,
```

We also removed a few trailing whitespace from the shell script.
@lcn2 lcn2 changed the title change use of egrep to grep -E use grep -E, add Makefile. add -S, -A, -T options Jul 26, 2024
@lcn2 lcn2 changed the title use grep -E, add Makefile. add -S, -A, -T options use grep -E, add Makefile, add -S, -A, -T options Jul 26, 2024
For macOS (Darwin) we use use the correct paths for homebrew
in `ensure_deps.sh`  If there is an error under macOS, we
also remind the user what to put in their $PATH.
@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS Jul 26, 2024
Copy link
Member

@mclarkson mclarkson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to default to homebrew on mac.

Copy link
Member

@mclarkson mclarkson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output looks nice. Not sure why it's necessary though.

Copy link
Member

@mclarkson mclarkson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handy, and thanks for removing the trailing ws

Copy link
Member

@mclarkson mclarkson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for this. LGTM

@lcn2
Copy link
Contributor Author

lcn2 commented Jul 26, 2024

Output looks nice.

Thank you @mclarkson.

Not sure why it's necessary though.

It matches what jparse(1) produces.

We (the 9International Obfuscated C Code Content - IOCCC](https://ioccc-src.github.io/temp-test-ioccc/) plan to use your tool in our processing of submissions (submissions come with 2 JSON files that need to be manipulated and transformed / merged into other JSON files). We plan to use -S -A -T in that regard.

The command line parsing now uses the bash builtin `getopts`.  Now, and
invalid option is flagged as an error.  Improper arguments to command
line options are flagged as errors.  The optional pattern is also obtained
after parsing command line options.

The usage message has been updated to include command line options that
were implemented but not documented in the usage message.

Updated the Invocation section of `README.md` to reflect the current
command line, including command line options that were documented in
the usage message, but not in `README.md`.
@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing Jul 26, 2024
@lcn2
Copy link
Contributor Author

lcn2 commented Jul 26, 2024

We updated this pull request to include a fix of the command line parsing, and updated the usage message and invocation text of README.md.

UPDATE 0

We also fix the command line usage of the tests to match the original README.md invocation section and the original usage message.

lcn2 added 2 commits July 26, 2024 13:32
The original usage message and `README.md` invocation has the optional
pattern at the end of the command line .  We fix 3 tests to place the
pattern at the end of the command line.  Moreover, we put `--` between
command line options and a pattern to prevent the pattern from being
interpreted as a command line option.  And while unlikely to happen,
this would allow pattern that begins with a dash to be correctly parsed.

Ran `all-tests.sh` to test the above.
We ran into some issues with the test scripts that were fixed by some
shell script hardening and using some bash best practices.  What follows
are fixes and improvements to the test scripts.

We make the tests scripts more robust:

* detect when the cd fails
* prevent unquoted shell variable expansion from corrupting execution

We improve the use of bash:

* use `env(1)` in the initial `#!` line to find `bash(1)` on `$PATH`

NOTE: macOS has deprecated the distributed `bash(1)` in favor of `zsh(1)`.
      Not all systems have `bash(1)` as `/bin/sh`.
      We allow for `bash(1)` to come from home brew or mac ports, or ...

We use bash constructs for slight speed gain and best practices:

* increment using ((++i)), in some cases avoiding a sub-shell launch
* bypass use of `cat(1)` by using `$(< "$argpfile")`
* use `find(1)` to select test files instead of the slower `ls(1)`
* avoid using shell glob problems by using `find(1)` to select test files
* use `$(...)` instead of `\`cmd\`` to help avoid data corrupting execution

Use `python3(1)` instead of just `python(1)`.

NOTE: Not all systems that have `python3(1)` have `python(1)`.
      Some system admins have yet to select the default version of python.
      The macOS platform, while they have `python3(1)` to not have `python(1)`.

The test scripts now pass the shellcheck script analysis tool.
See [www.shellcheck.net](https://www.shellcheck.net) for details.

Fixed a bug in `all-docker-tests.sh` where Centos tests were re-displayed
a 2nd time as if they were Debian tests.

Ran `./all-tests.sh` to verify the above under macOS and RHEL 9.

TODO: Fix a number of shell script problems with `JSONPath.sh` along
      similar lines as above: especially where shell variables containing
      JSON and other test data can take on values that can
      corrupt the shell script execution.  We wanted to make the
      other test scripts more robust before taking on `JSONPath.sh`
      so that we may use such tests.
@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make text scripts robust Jul 27, 2024
@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make text scripts robust use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make test scripts robust Jul 27, 2024
@lcn2
Copy link
Contributor Author

lcn2 commented Jul 27, 2024

Hello @mclarkson and other maintainers,

Thank you

We the IOCCC judges have found the JSONPath.sh script to be extremely useful. We THANK YOU for writing and for making JSONPath.sh available for others to use.

Making JSONPath.sh bullet proof

People will see, when we public our tools, that we will be using your JSONPath.sh tool. Therefore, we need to prepared for the to attempt to break our use of the JSONPath.sh script.

The IOCCC encourages people to submit code that is fairly twisted :-) and in some cases to bend the content rules to the breaking point. This will likely include people messing with the JSON data that a submission needs to describe itself. So we need to be prepared. 🤓

For the International Obfuscated C Code Contest - IOCCC we need the JSONPath.sh script to be as "bullet proof" as we can get. We believe that the JSONPath.sh script can reach this level of robustness.

Test script hardened

We have discovered cases where pathologic shell variable values could corrupt the execution of the script. We have begun to harden JSONPath.sh, using the shellcheck tool as a guide (along with 50+ years of shell scripting experience and as a co-author of the POSIX 1003-2 shell standard on which bash was based :) ). As we do so, we intend to use the test scripts to verify that we have not impacted the function of the JSONPath.sh script.

We discovered some issues with the test scripts where shell script hardening as well as applying other best bash practices helped, along with addressing some macOS and other probability issues. As a result we added commit c4c8ec2 to this PR.

Hardening JSONPath.sh next

We plan to fix a number of shell script concerns with JSONPath.sh along similar lines as above: especially where shell variables containing JSON and other test data can take on values that could corrupt the shell script execution. We wanted to make the other test scripts more robust before taking on JSONPath.sh so that we may better use such tests during the hardening process.

Please stay tuned for the next commit (in a day or so).

@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make test scripts robust use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make JSONPat.sh and test scripts robust Jul 28, 2024
@lcn2 lcn2 changed the title use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make JSONPat.sh and test scripts robust use grep -E, add Makefile, add -S, -A, -T options, fix ensure_deps.sh for macOS, fix command line parsing, make JSONPath.sh and test scripts robust Jul 28, 2024
We make `JSONPath.sh` much more robust and use bash constructs for slight
speed gain and best practices:

* prevent unquoted shell variable expansion from corrupting execution
* compute numerical values using ((var=equation)) instead of let
* declare local variable before assigning a value
* fixed how options to grep are accumulated
* fixed how main is called
* fixed how an unexpected EOF with token is thrown
* fixed how array elements are unset
* fixed how code determines of an array contains only empty values
* changed cases where `cat(1)` of a file is piped into feeding the file on stdin
* use `$(...)` instead of `\`cmd\`` to help avoid data corrupting execution
* replaced use of `[ ... ]` with `[[ ... ]]`
* removed shell variables that were set but never unused
* fixed cases where a case statement lacked a default case
* fixed how the literal string `'"*'` is used
* fixed cases where a scalar is followed by a literal string that starts with `[`
* fixed cases where `$@` is confused with `$*`

All errors, warnings and issues reported by [shellcheck](https://www.shellcheck.net)
have been addressed / fix.  Now `JSONPath.sh` is error, warning and issue free
under [shellcheck](https://www.shellcheck.net)!

Ran `./all-tests.sh` to verify the above under macOS and RHEL 9 Linux.
@lcn2
Copy link
Contributor Author

lcn2 commented Jul 28, 2024

With commit d7660c7 we have made JSONPath.sh more robust:

We make JSONPath.sh much more robust and use bash constructs for slight
speed gain and best practices:

  • prevent unquoted shell variable expansion from corrupting execution
  • compute numerical values using ((var=equation)) instead of let
  • declare local variable before assigning a value
  • fixed how options to grep are accumulated
  • fixed how main is called
  • fixed how an unexpected EOF with token is thrown
  • fixed how array elements are unset
  • fixed how code determines of an array contains only empty values
  • changed cases where cat(1) of a file is piped into feeding the file on stdin
  • use $(...) instead of \cmd`` to help avoid data corrupting execution
  • replaced use of [ ... ] with [[ ... ]]
  • removed shell variables that were set but never unused
  • fixed cases where a case statement lacked a default case
  • fixed how the literal string '"*' is used
  • fixed cases where a scalar is followed by a literal string that starts with [
  • fixed cases where $@ is confused with $*

All errors, warnings and issues reported by shellcheck
have been addressed / fix. Now JSONPath.sh is error, warning and issue free
under shellcheck!

Ran ./all-tests.sh to verify the above under macOS and RHEL 9 Linux.

@lcn2
Copy link
Contributor Author

lcn2 commented Aug 5, 2024

Any change that this pull request could be processed?

@mclarkson
Copy link
Member

Any change that this pull request could be processed?

Hi there! Yes, in the next few days I should think. I'm very happy you made this PR, the work was long over due. Thank you!

@lcn2
Copy link
Contributor Author

lcn2 commented Aug 7, 2024

Any change that this pull request could be processed?

Hi there! Yes, in the next few days I should think. I'm very happy you made this PR, the work was long over due. Thank you!

You are most welcome.

@mclarkson mclarkson merged commit 273ad63 into bashtools:master Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants