Skip to content

remove as many non POSIX idioms #27

@phyver

Description

@phyver

There are several bashisms in GameShell code.
I am trying to remove some of them, more as a learning experience than as a real goal. (Even though it would make it easier to port GameShell to zsh or some other shell.)

All of this concerns primarily GameShell's code, but missions' code could also benefit from them where appropriate.

  • local variables do not exist in POSIX sh! The function mission_source relies on them to be re-entrant. There might be some ideas here: htps://stackoverflow.com/questions/18597697/posix-compliant-way-to-scope-variables-to-a-function-in-a-shell-script

  • compgen is used in save_environment.sh. This is used to compare the before / after environments when sourcing mission files. To get variables, set can be used, but I'm not sure there is a way to get a list of functions in POSIX sh

  • find ... -print0 and xargs -0 are not in POSIX but exist in freebsd, openbsd and macos

  • sleep 0.1 isn't in POSIX but is supported in freebsd, openbsd and macos

  • readlink -f is not in POSIX and doesn't work on macos. I wrote a small script, but it doesn't mix well when intermediate directories don't have the execute bit.

  • mktemp isn't in POSIX, but seems to be available in freebsd, openbsd and macos
    At the moment, I only use it bare mktemp, so it would be easy to write script to do something reliable enough.

  • when not available, gettext and eval_gettext could be redefined using printf and eval+printf. (That would of course remove localization.)

  • BASHPID is used to detect subshell, but I don't know if there is a POSIX equivalent
    apparently, the POSIX way of doing it is to use sh -c 'echo $PPID', see https://unix.stackexchange.com/questions/484442/how-can-i-get-the-pid-of-a-subshell

  • $RANDOM variable : there now is a RANDOM function (using awk) to do the same

  • source FILE : can be replaced by . FILE

  • echo -n : can mostly be replaced by printf

  • read -p : can be replaced by printf ... ; read

  • read -s : was replaced by stty -echo; read; stty echo

  • $'STR': were replaced

  • the variable $OSTYPE isn't necessary anymore since almost every problematic function has been re-implemented in a POSIX compliant way

  • export -f: it can mostly be replaced by writing an external script in $GSH_ROOT/bin or $MISSION_DIR/bin

  • head -c and tail -c: they were replaced by instances of dd

  • the command seq is not in POSIX! (It is not present on openbsd.) It was replaced by a small script (no option are available)

Metadata

Metadata

Assignees

No one assigned

    Labels

    GameShellrelative to GameShell codemissionsrelative to particular missions

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions