Skip to content

Commit 3992a5a

Browse files
committed
Run help50 COMMAND as though COMMAND were typed directly
Students used to run `help50 make foo`. Help now arrives automatically after any failed command, so run COMMAND with the same exit status and let the prompt hook handle the rest: help50 make foo behaves exactly like make foo. Builtins go through bash -c with the shell's error wording preserved; an unknown command fails with the shell's own "command not found" message so helpers match it. Bare help50 prints usage (exit 0) explaining the new behaviour. The subcommands (start/stop/...) are unchanged; only they require non-root.
1 parent dd74ee9 commit 3992a5a

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

‎opt/cs50/bin/help50‎

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
#!/bin/bash
22

3-
# If root
4-
if [[ `id -u` -eq 0 ]]; then
5-
exit 1
6-
fi
7-
83
function _disable() {
94
touch /tmp/help50.lock
105
}
@@ -80,28 +75,43 @@ function _stop() {
8075
kill -SIGTERM $ggppid
8176
}
8277

78+
# Run a command as though typed directly. Students used to run `help50 make foo`; help now
79+
# arrives automatically after any failed command, so just run the command, with the same
80+
# exit status, and let the shell's prompt hook handle the rest.
81+
function _run() {
82+
if [[ -n "$(type -P "$1")" ]]; then
83+
exec "$@"
84+
elif [[ -n "$(type -t "$1")" ]]; then # Builtin or keyword, e.g., cd
85+
exec bash -c "$(printf '%q ' "$@")" 2> >(sed 's/^bash: line [0-9]*: /bash: /' >&2)
86+
else # Fail the way the shell would, so that helpers recognize it
87+
echo "bash: $1: command not found" >&2
88+
exit 127
89+
fi
90+
}
91+
92+
function _usage() {
93+
echo "Usage: help50 COMMAND [ARGS...]"
94+
echo
95+
echo "Runs COMMAND. If it fails, help appears automatically, as it now does for any"
96+
echo "command you run, so you can also just run COMMAND directly."
97+
echo
98+
echo "Other usage: help50 [disable|enable|is-enabled|start|status|stop]"
99+
}
100+
83101
# Parse argument
84102
case "$1" in
85-
disable)
86-
_disable
87-
;;
88-
enable)
89-
_enable
90-
;;
91-
is-enabled)
92-
_is-enabled
93-
;;
94-
start)
95-
_start
96-
;;
97-
status)
98-
_status
103+
disable|enable|is-enabled|start|status|stop)
104+
105+
# Not for root
106+
if [[ `id -u` -eq 0 ]]; then
107+
exit 1
108+
fi
109+
_${1}
99110
;;
100-
stop)
101-
_stop
111+
""|-h|--help)
112+
_usage
102113
;;
103114
*)
104-
echo "Usage: $0 [disable|enable|is-enabled|start|status|stop]"
105-
exit 1
115+
_run "$@"
106116
;;
107117
esac

0 commit comments

Comments
 (0)