source: trunk/cereal/src/cereal @ 1190

Last change on this file since 1190 was 1190, checked in by jrollins, 10 years ago

fixed some lingering bashism that were causing things to fail with
dash/sh.

  • Property svn:executable set to *
File size: 3.6 KB
Line 
1#!/bin/sh
2
3# cereal: command line interface to cereal screen serial loggers
4#
5# The cereal scripts were written by
6# Jameson Graef Rollins <jrollins@finestructure.net>
7# and
8# Daniel Kahn Gillmor <dkg-debian.org@fifthhorseman.net>.
9#
10# They are Copyright 2007, and are all released under the GPL, version 3
11# or later.
12
13##################################################
14SHAREDIR=${SHAREDIR:-"/usr/share/cereal"}
15export SHAREDIR
16. "$SHAREDIR/common"
17##################################################
18
19usage() {
20cat <<EOF
21Usage: cereal <subcommand> [options] [args]
22Cereal client program.
23
24subcommands:
25  attach (a) SESSION                      attach to session
26  follow (f) [options] SESSION            follow session log
27    -c (--cat)                              cat log instead of follow
28    -p (--path)                             output just log file path
29  list (l) [options] [SESSION]...         list session(s)
30    -n (--names)                            list just session names
31  help (h,?)                              this help
32
33EOF
34}
35
36attach() {
37    SESSION="$1"
38
39    if [ -z "$SESSION" ] ; then
40        failure "Not enough input arguments.
41Type 'cereal help' for more info."
42    elif ! is_session "$SESSION" ; then
43        failure "Session '$SESSION' not found.
44Type 'cereal list' to list sessions."
45    elif ! can_attach "$SESSION" "$USER" ; then
46        failure "You do not have permission to attach to session '$SESSION'."
47    fi
48
49    is_running "$SESSION"
50    case $? in
51        0|2) # also attempt to start if session is unknown (return code 2)
52            log_write "$SESSION" "user '$USER' attaching to session from `tty`..."
53            screen -x -S "cereal:$SESSION" || failure "Could not reattach screen."
54            log_write "$SESSION" "user '$USER' on `tty` detached from session."
55            ;;
56        1)
57            failure "Session '$SESSION' not running."
58            ;;
59    esac
60}
61
62log_follow() {
63    tail -n 100 -F "$LOG"
64}
65
66log_cat() {
67    cat "$LOG"
68}
69
70log_pwd() {
71    echo "$LOG"
72}
73
74follow() {
75    local SESSION
76    local TEMP
77    local OUTPUT
78    OUTPUT="log_follow"
79
80    TEMP=$(getopt -o cp --long cat,path -n "cereal follow" -- "$@")
81    if [ $? -ne 0 ] ; then
82        failure "Invalid options."
83    fi
84
85    # Note the quotes around `$TEMP': they are essential!
86    eval set -- "$TEMP"
87
88    while true ; do
89        case "$1" in
90            -c|--cat) OUTPUT="log_cat" ; shift 1 ;;
91            -p|--path) OUTPUT="log_pwd" ; shift 1 ;;
92            --) shift ;;
93            *)
94                SESSION="$1"
95                break
96                ;;
97        esac
98    done
99
100    if [ -z "$SESSION" ] ; then
101        failure "Not enough input arguments.
102Type 'cereal help' for more info."
103    elif ! is_session "$SESSION" ; then
104        failure "Session '$SESSION' not found.
105Type 'cereal list' to list sessions."
106    elif ! can_follow "$SESSION" "$USER" ; then
107        failure "You do not have permission to follow session '$SESSION'."
108    fi
109
110    LOG="$SESSIONDIR/$SESSION/log/main/current"
111    if [ "$OUTPUT" = 'log_follow' -o "$OUTPUT" = 'log_cat' ] ; then
112        echo "############################################################"
113        echo "### cereal log: $SESSION"
114        echo -n "### "
115        list "$SESSION"
116        echo "### log file: $LOG"
117        echo "############################################################"
118    fi
119    $OUTPUT
120}
121
122###############################################################
123### MAIN
124
125handle_command() {
126
127    COMMAND="$1"
128
129    [ "$COMMAND" ] || failure "Type 'cereal help' for usage."
130    shift
131
132    case $COMMAND in
133        'attach'|'a')
134            attach "$@"
135            ;;
136        'follow'|'f')
137            follow "$@"
138            ;;
139        'list'|'l')
140            list "$@" || failure "There are no sessions." 1
141            ;;
142        'help'|'h'|'?')
143            usage
144            ;;
145        *)
146            failure "Unknown command: '$COMMAND'
147Type '$CMD help' for usage."
148            ;;
149    esac
150
151    exit "$ERR"
152}
153
154handle_command "$@"
Note: See TracBrowser for help on using the repository browser.