From 700ac112a3dd2041de5fc2a6abd3781103c704a8 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sat, 17 Dec 2016 17:46:15 +1300 Subject: Rename all pdksh stuff to ksh As part of a foray into more active use of ksh and derivatives. --- Makefile | 24 +++--- README.markdown | 12 ++- check/ksh | 6 ++ check/pdksh | 6 -- ksh/kshrc | 13 +++ ksh/kshrc.d/prompt.ksh | 201 +++++++++++++++++++++++++++++++++++++++++++ lint/ksh | 2 + lint/pdksh | 2 - pdksh/pdkshrc | 13 --- pdksh/pdkshrc.d/prompt.pdksh | 193 ----------------------------------------- sh/shrc.d/ksh.sh | 10 +-- vim/after/ftdetect/sh.vim | 2 +- 12 files changed, 244 insertions(+), 240 deletions(-) create mode 100755 check/ksh delete mode 100755 check/pdksh create mode 100644 ksh/kshrc create mode 100644 ksh/kshrc.d/prompt.ksh create mode 100755 lint/ksh delete mode 100755 lint/pdksh delete mode 100644 pdksh/pdkshrc delete mode 100644 pdksh/pdkshrc.d/prompt.pdksh diff --git a/Makefile b/Makefile index 595a600b..f2e9e278 100644 --- a/Makefile +++ b/Makefile @@ -23,7 +23,7 @@ install-ncmcpp \ install-newsbeuter \ install-mysql \ - install-pdksh \ + install-ksh \ install-perlcritic \ install-perltidy \ install-psql \ @@ -46,7 +46,7 @@ check-bash \ check-bin \ check-games \ - check-pdksh \ + check-ksh \ check-sh \ check-urxvt \ check-yash \ @@ -54,7 +54,7 @@ lint-bash \ lint-bin \ lint-games \ - lint-pdksh \ + lint-ksh \ lint-yash \ lint-sh \ lint-urxvt @@ -290,11 +290,11 @@ install-newsbeuter : install-mysql : install -pm 0644 -- mysql/my.cnf "$(HOME)"/.my.cnf -install-pdksh : check-pdksh install-sh +install-ksh : check-ksh install-sh install -m 0755 -d -- \ - "$(HOME)"/.pdkshrc.d - install -pm 0644 -- pdksh/pdkshrc "$(HOME)"/.pdkshrc - install -pm 0644 -- pdksh/pdkshrc.d/* "$(HOME)"/.pdkshrc.d + "$(HOME)"/.kshrc.d + install -pm 0644 -- ksh/kshrc "$(HOME)"/.kshrc + install -pm 0644 -- ksh/kshrc.d/* "$(HOME)"/.kshrc.d install-perlcritic : install -pm 0644 -- perlcritic/perlcriticrc "$(HOME)"/.perlcriticrc @@ -409,8 +409,8 @@ check-games : check-man : check/man -check-pdksh : - check/pdksh +check-ksh : + check/ksh check-sh : check/sh @@ -425,7 +425,7 @@ lint : check \ lint-bash \ lint-bin \ lint-games \ - lint-pdksh \ + lint-ksh \ lint-sh \ lint-urxvt \ lint-yash @@ -439,8 +439,8 @@ lint-bin : lint-games : lint/games -lint-pdksh : - lint/pdksh +lint-ksh : + lint/ksh lint-sh : lint/sh diff --git a/README.markdown b/README.markdown index f4c73817..aa5ce453 100644 --- a/README.markdown +++ b/README.markdown @@ -53,13 +53,12 @@ Configuration is included for: * [GTK+](http://www.gtk.org/) -- GIMP Toolkit, for graphical user interface elements * [i3](https://i3wm.org/) -- Tiling window manager +* [Korn shell](http://www.kornshell.com/) -- Korn shell and its derivatives * [less](https://www.gnu.org/software/less/) -- Terminal pager * [Mutt](http://www.mutt.org/) -- Terminal mail user agent * [`mysql(1)`](http://linux.die.net/man/1/mysql) -- Command-line MySQL client * [Ncmpcpp](https://rybczak.net/ncmpcpp/) -- ncurses music player client * [Newsbeuter](https://www.newsbeuter.org/) -- Terminal RSS/Atom feed reader -* [`pdksh(1)`](http://www.cs.mun.ca/~michael/pdksh/) -- public domain fork - of the Korn shell * [`psql(1)`](http://linux.die.net/man/1/psql) -- Command-line PostgreSQL client * [Perl::Critic](http://perlcritic.com/) -- static source code analysis @@ -254,12 +253,11 @@ I also add completions for my own scripts and functions where useful. The completions are dynamically loaded if Bash is version 4.0 or greater. Otherwise, they're all loaded on startup. -#### pdksh +#### Korn shell -The pdksh configuration files and functions are not nearly as featureful as the -Bash ones. They're tested on OpenBSD and FreeBSD pdksh implementations, but the -former is the primary system for which I'm maintaining them, and there are some -feature differences. +The `ksh` configuration files and functions are not nearly as featureful as the +Bash ones. At the moment these are mainly being maintained for use on OpenBSD +`pdksh`. #### Yash diff --git a/check/ksh b/check/ksh new file mode 100755 index 00000000..3136c413 --- /dev/null +++ b/check/ksh @@ -0,0 +1,6 @@ +#!/bin/sh +for ksh in ksh/* ksh/kshrc.d/* ; do + [ -f "$ksh" ] || continue + ksh -n "$ksh" || exit +done +printf 'All ksh scripts parsed successfully.\n' diff --git a/check/pdksh b/check/pdksh deleted file mode 100755 index fd1d55b7..00000000 --- a/check/pdksh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh -for pdksh in pdksh/* pdksh/pdkshrc.d/* ; do - [ -f "$pdksh" ] || continue - ksh -n "$pdksh" || exit -done -printf 'All pdksh scripts parsed successfully.\n' diff --git a/ksh/kshrc b/ksh/kshrc new file mode 100644 index 00000000..cf7812d6 --- /dev/null +++ b/ksh/kshrc @@ -0,0 +1,13 @@ +# Emacs-style key bindings +set -o braceexpand +set -o emacs + +# Save history +HISTFILE=$HOME/.ksh_history +HISTSIZE=$((1 << 10)) + +# Load any supplementary scripts +for kshrc in "$HOME"/.kshrc.d/*.ksh ; do + [[ -e $kshrc ]] && . "$kshrc" +done +unset -v kshrc diff --git a/ksh/kshrc.d/prompt.ksh b/ksh/kshrc.d/prompt.ksh new file mode 100644 index 00000000..90e813f0 --- /dev/null +++ b/ksh/kshrc.d/prompt.ksh @@ -0,0 +1,201 @@ +# All of this is only known to work on OpenBSD's fork of pdksh +case $KSH_VERSION in + *'PD KSH'*) + case $(uname -s) in + OpenBSD) ;; + *) return ;; + esac + ;; + *) return ;; +esac + +# Frontend to controlling prompt +prompt() { + + # If no arguments, print the prompt strings as they are + if ! (($#)) ; then + printf '%s\n' PS1="$PS1" PS2="$PS2" PS3="$PS3" PS4="$PS4" + return + fi + + # What's done next depends on the first argument to the function + case $1 in + + # Turn complex, colored PS1 and debugging PS4 prompts on + on) + # Basic prompt shape depends on whether we're in SSH or not + PS1= + if [[ -n $SSH_CLIENT ]] || [[ -n $SSH_CONNECTION ]] ; then + PS1=$PS1'\u@\h:' + fi + PS1=$PS1'\w' + + # Add sub-commands; VCS, job, and return status checks + PS1=$PS1'$(prompt vcs)$(prompt job)$(prompt ret "$?")' + + # Add prefix and suffix + PS1='${PROMPT_PREFIX}'$PS1'${PROMPT_SUFFIX}' + + # Add terminating "$" or "#" sign + PS1=$PS1'\$' + + # Declare variables to contain terminal control strings + typeset format reset + + # Disregard output and error from these tput(1) calls + { + # Count available colors + typeset -i colors + colors=$(tput colors || tput Co) + + # Prepare reset code + reset=$(tput sgr0 || tput me) + + # Check if we have non-bold bright yellow available + if ((colors >= 16)) ; then + format=$( + : "${PROMPT_COLOR:=11}" + tput setaf "$PROMPT_COLOR" || + tput setaf "$PROMPT_COLOR" 0 0 || + tput AF "$PROMPT_COLOR" || + tput AF "$PROMPT_COLOR" 0 0 + ) + + # If we have only eight colors, use bold yellow + elif ((colors >= 8)) ; then + format=$( + : "${PROMPT_COLOR:=3}" + tput setaf "$PROMPT_COLOR" || + tput AF "$PROMPT_COLOR" + tput bold || tput md + ) + + # Otherwise, we just try bold + else + format=$(tput bold || tput md) + fi + + } >/dev/null 2>&1 + + # String it all together + PS1='\['"$format"'\]'"$PS1"'\['"$reset"'\] ' + PS2='> ' + PS3='? ' + PS4='+<$?> $LINENO:' + ;; + + # Git prompt function + git) + + # Wrap as compound command; we don't want to see output from any of + # these git(1) calls + { + # Bail if we're not in a work tree--or, implicitly, if we don't + # have git(1). + [[ -n $(git rev-parse --is-inside-work-tree) ]] || + return + + # Refresh index so e.g. git-diff-files(1) is accurate + git update-index --refresh + + # Find a local branch, remote branch, or tag (annotated or + # not), or failing all of that just show the short commit ID, + # in that order of preference; if none of that works, bail out + typeset name + name=$( + git symbolic-ref --quiet HEAD || + git describe --tags --exact-match HEAD || + git rev-parse --short HEAD + ) || return + name=${name##*/} + [[ -n $name ]] || return + + # Check various files in .git to flag processes + typeset proc + [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && + proc=${proc:+"$proc",}'REBASE' + [[ -f .git/MERGE_HEAD ]] && + proc=${proc:+"$proc",}'MERGE' + [[ -f .git/CHERRY_PICK_HEAD ]] && + proc=${proc:+"$proc",}'PICK' + [[ -f .git/REVERT_HEAD ]] && + proc=${proc:+"$proc",}'REVERT' + [[ -f .git/BISECT_LOG ]] && + proc=${proc:+"$proc",}'BISECT' + + # Collect symbols representing repository state + typeset state + + # Upstream HEAD has commits after local HEAD; we're "behind" + (($(git rev-list --count 'HEAD..@{u}'))) && + state=${state}'<' + + # Local HEAD has commits after upstream HEAD; we're "ahead" + (($(git rev-list --count '@{u}..HEAD'))) && + state=${state}'>' + + # Tracked files are modified + git diff-files --no-ext-diff --quiet || + state=${state}'!!' + + # Changes are staged + git diff-index --cached --no-ext-diff --quiet HEAD || + state=${state}'+' + + # There are some untracked and unignored files + git ls-files --directory --error-unmatch --exclude-standard \ + --no-empty-directory --others -- ':/*' && + state=${state}'?' + + # There are stashed changes + git rev-parse --quiet --verify refs/stash && + state=${state}'^' + + } >/dev/null 2>&1 + + # Print the status in brackets; add a git: prefix only if there + # might be another VCS prompt (because PROMPT_VCS is set) + printf '(%s%s%s%s)' \ + "${PROMPT_VCS:+git:}" "$name" "${proc:+:"$proc"}" "$state" + ;; + + # Revert to simple inexpensive prompts + off) + PS1='\$ ' + PS2='> ' + PS3='? ' + PS4='+ ' + ;; + + # VCS wrapper prompt function; print the first relevant prompt, if any + vcs) + typeset vcs + for vcs in "${PROMPT_VCS[@]:-git}" ; do + prompt "$vcs" && return + done + ;; + + # Show return status of previous command in angle brackets, if not zero + ret) + local ret=$2 + ((ret)) && printf '<%u>' "$ret" + ;; + + # Show the count of background jobs in curly brackets, if not zero + job) + typeset -i jobc + jobc=$(jobs -p | sed -n '$=') + ((jobc)) && printf '{%u}' "$jobc" + ;; + + # Print error + *) + printf 'prompt: Unknown command %s\n' "$1" >&2 + return 2 + ;; + + esac +} + +# Start with full-fledged prompt +prompt on diff --git a/lint/ksh b/lint/ksh new file mode 100755 index 00000000..5c5445fc --- /dev/null +++ b/lint/ksh @@ -0,0 +1,2 @@ +#!/bin/sh +find ksh -type f -print -exec shellcheck -e SC1090 -s ksh -- {} \; diff --git a/lint/pdksh b/lint/pdksh deleted file mode 100755 index 03745b3d..00000000 --- a/lint/pdksh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -find pdksh -type f -print -exec shellcheck -e SC1090 -s ksh -- {} \; diff --git a/pdksh/pdkshrc b/pdksh/pdkshrc deleted file mode 100644 index 48799c2e..00000000 --- a/pdksh/pdkshrc +++ /dev/null @@ -1,13 +0,0 @@ -# Emacs-style key bindings -set -o braceexpand -set -o emacs - -# Save history -HISTFILE=$HOME/.pdksh_history -HISTSIZE=$((1 << 10)) - -# Load any supplementary scripts -for pdkshrc in "$HOME"/.pdkshrc.d/*.pdksh ; do - [[ -e $pdkshrc ]] && . "$pdkshrc" -done -unset -v pdkshrc diff --git a/pdksh/pdkshrc.d/prompt.pdksh b/pdksh/pdkshrc.d/prompt.pdksh deleted file mode 100644 index 63e965f9..00000000 --- a/pdksh/pdkshrc.d/prompt.pdksh +++ /dev/null @@ -1,193 +0,0 @@ -# All of this is only known to work on OpenBSD's fork of pdksh -[[ $(uname -s) == OpenBSD ]] || return - -# Frontend to controlling prompt -prompt() { - - # If no arguments, print the prompt strings as they are - if ! (($#)) ; then - printf '%s\n' PS1="$PS1" PS2="$PS2" PS3="$PS3" PS4="$PS4" - return - fi - - # What's done next depends on the first argument to the function - case $1 in - - # Turn complex, colored PS1 and debugging PS4 prompts on - on) - # Basic prompt shape depends on whether we're in SSH or not - PS1= - if [[ -n $SSH_CLIENT ]] || [[ -n $SSH_CONNECTION ]] ; then - PS1=$PS1'\u@\h:' - fi - PS1=$PS1'\w' - - # Add sub-commands; VCS, job, and return status checks - PS1=$PS1'$(prompt vcs)$(prompt job)$(prompt ret "$?")' - - # Add prefix and suffix - PS1='${PROMPT_PREFIX}'$PS1'${PROMPT_SUFFIX}' - - # Add terminating "$" or "#" sign - PS1=$PS1'\$' - - # Declare variables to contain terminal control strings - typeset format reset - - # Disregard output and error from these tput(1) calls - { - # Count available colors - typeset -i colors - colors=$(tput colors || tput Co) - - # Prepare reset code - reset=$(tput sgr0 || tput me) - - # Check if we have non-bold bright yellow available - if ((colors >= 16)) ; then - format=$( - : "${PROMPT_COLOR:=11}" - tput setaf "$PROMPT_COLOR" || - tput setaf "$PROMPT_COLOR" 0 0 || - tput AF "$PROMPT_COLOR" || - tput AF "$PROMPT_COLOR" 0 0 - ) - - # If we have only eight colors, use bold yellow - elif ((colors >= 8)) ; then - format=$( - : "${PROMPT_COLOR:=3}" - tput setaf "$PROMPT_COLOR" || - tput AF "$PROMPT_COLOR" - tput bold || tput md - ) - - # Otherwise, we just try bold - else - format=$(tput bold || tput md) - fi - - } >/dev/null 2>&1 - - # String it all together - PS1='\['"$format"'\]'"$PS1"'\['"$reset"'\] ' - PS2='> ' - PS3='? ' - PS4='+<$?> $LINENO:' - ;; - - # Git prompt function - git) - - # Wrap as compound command; we don't want to see output from any of - # these git(1) calls - { - # Bail if we're not in a work tree--or, implicitly, if we don't - # have git(1). - [[ -n $(git rev-parse --is-inside-work-tree) ]] || - return - - # Refresh index so e.g. git-diff-files(1) is accurate - git update-index --refresh - - # Find a local branch, remote branch, or tag (annotated or - # not), or failing all of that just show the short commit ID, - # in that order of preference; if none of that works, bail out - typeset name - name=$( - git symbolic-ref --quiet HEAD || - git describe --tags --exact-match HEAD || - git rev-parse --short HEAD - ) || return - name=${name##*/} - [[ -n $name ]] || return - - # Check various files in .git to flag processes - typeset proc - [[ -d .git/rebase-merge || -d .git/rebase-apply ]] && - proc=${proc:+"$proc",}'REBASE' - [[ -f .git/MERGE_HEAD ]] && - proc=${proc:+"$proc",}'MERGE' - [[ -f .git/CHERRY_PICK_HEAD ]] && - proc=${proc:+"$proc",}'PICK' - [[ -f .git/REVERT_HEAD ]] && - proc=${proc:+"$proc",}'REVERT' - [[ -f .git/BISECT_LOG ]] && - proc=${proc:+"$proc",}'BISECT' - - # Collect symbols representing repository state - typeset state - - # Upstream HEAD has commits after local HEAD; we're "behind" - (($(git rev-list --count 'HEAD..@{u}'))) && - state=${state}'<' - - # Local HEAD has commits after upstream HEAD; we're "ahead" - (($(git rev-list --count '@{u}..HEAD'))) && - state=${state}'>' - - # Tracked files are modified - git diff-files --no-ext-diff --quiet || - state=${state}'!!' - - # Changes are staged - git diff-index --cached --no-ext-diff --quiet HEAD || - state=${state}'+' - - # There are some untracked and unignored files - git ls-files --directory --error-unmatch --exclude-standard \ - --no-empty-directory --others -- ':/*' && - state=${state}'?' - - # There are stashed changes - git rev-parse --quiet --verify refs/stash && - state=${state}'^' - - } >/dev/null 2>&1 - - # Print the status in brackets; add a git: prefix only if there - # might be another VCS prompt (because PROMPT_VCS is set) - printf '(%s%s%s%s)' \ - "${PROMPT_VCS:+git:}" "$name" "${proc:+:"$proc"}" "$state" - ;; - - # Revert to simple inexpensive prompts - off) - PS1='\$ ' - PS2='> ' - PS3='? ' - PS4='+ ' - ;; - - # VCS wrapper prompt function; print the first relevant prompt, if any - vcs) - typeset vcs - for vcs in "${PROMPT_VCS[@]:-git}" ; do - prompt "$vcs" && return - done - ;; - - # Show return status of previous command in angle brackets, if not zero - ret) - local ret=$2 - ((ret)) && printf '<%u>' "$ret" - ;; - - # Show the count of background jobs in curly brackets, if not zero - job) - typeset -i jobc - jobc=$(jobs -p | sed -n '$=') - ((jobc)) && printf '{%u}' "$jobc" - ;; - - # Print error - *) - printf 'prompt: Unknown command %s\n' "$1" >&2 - return 2 - ;; - - esac -} - -# Start with full-fledged prompt -prompt on diff --git a/sh/shrc.d/ksh.sh b/sh/shrc.d/ksh.sh index 046cd6c4..9cb72e8e 100644 --- a/sh/shrc.d/ksh.sh +++ b/sh/shrc.d/ksh.sh @@ -2,9 +2,7 @@ # configuration if it was defined or if we can find it. Bash and Zsh invoke # their own rc files first, which I've written to then look for ~/.shrc; ksh # does it the other way around. -case $KSH_VERSION in - *'PD KSH '*|*'MIRBSD KSH '*) - [ -f "${KSH_ENV:="$HOME"/.pdkshrc}" ] || return - . "$KSH_ENV" - ;; -esac +[ -n "$KSH_VERSION" ] || return +[ -n "$KSH_ENV" ] || KSH_ENV=$HOME/.kshrc +[ -f "$KSH_ENV" ] || return +. "$KSH_ENV" diff --git a/vim/after/ftdetect/sh.vim b/vim/after/ftdetect/sh.vim index 3bc10ba7..c974e293 100644 --- a/vim/after/ftdetect/sh.vim +++ b/vim/after/ftdetect/sh.vim @@ -9,7 +9,7 @@ augroup dfsh " Names/paths of things that are Korn shell script autocmd BufNewFile,BufRead - \ **/.dotfiles/pdksh/**,.pdkshrc,*.pdksh + \ **/.dotfiles/ksh/**,.kshrc,*.ksh \ let b:is_kornshell = 1 | \ setlocal filetype=sh -- cgit v1.2.3