source: branches/cereal/0.22/fs/usr/bin/cereal @ 1205

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

cereal: i got way overzealous and did not pay attention, so i am reverting the uid changes i just made outside of the debian/ directory. i am also reverting some other the changes that Daniel made that i thought were ultimately not necessary, so as to keep the total number of diffs to an absolute minimum. i will now completely stop mucking with things, and will accept whatever changes Daniel thinks are best for the backport. all appologies.

  • Property svn:executable set to *
File size: 3.5 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 Rollins <jrollins@fifthhorseman.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 ! is_running "$SESSION" ; then
46        failure "Session '$SESSION' not running."
47    elif ! can_attach "$SESSION" "$USER" ; then
48        failure "You do not have permission to attach to session '$SESSION'."
49    fi
50
51    log_write "$SESSION" "user '$USER' attaching to session from `tty`..."
52    screen -x -S "cereal:$SESSION" || failure "Could not reattach screen."
53    log_write "$SESSION" "user '$USER' on `tty` detached from session."
54}
55
56log_follow() {
57    tail -n 100 -F "$LOG"
58}
59
60log_cat() {
61    cat "$LOG"
62}
63
64log_pwd() {
65    echo "$LOG"
66}
67
68follow() {
69    local SESSION
70    local TEMP
71    local OUTPUT
72    OUTPUT="log_follow"
73
74    TEMP=$(getopt -o cp --long cat,path -n "cereal follow" -- "$@")
75    if [ $? -ne 0 ] ; then
76        failure "Invalid options."
77    fi
78
79    # Note the quotes around `$TEMP': they are essential!
80    eval set -- "$TEMP"
81
82    while true ; do
83        case "$1" in
84            -c|--cat) OUTPUT="log_cat" ; shift 1 ;;
85            -p|--path) OUTPUT="log_pwd" ; shift 1 ;;
86            --) shift ;;
87            *)
88                SESSION="$1"
89                break
90                ;;
91        esac
92    done
93
94    if [ -z "$SESSION" ] ; then
95        failure "Not enough input arguments.
96Type 'cereal help' for more info."
97    elif ! is_session "$SESSION" ; then
98        failure "Session '$SESSION' not found.
99Type 'cereal list' to list sessions."
100    elif ! can_follow "$SESSION" "$USER" ; then
101        failure "You do not have permission to follow session '$SESSION'."
102    fi
103
104    LOG="$SESSIONDIR/$SESSION/log/main/current"
105    if [ "$OUTPUT" = 'log_follow' -o "$OUTPUT" = 'log_cat' ] ; then
106        echo "############################################################"
107        echo "### cereal log: $SESSION"
108        echo -n "### "
109        list "$SESSION"
110        echo "### log file: $LOG"
111        echo "############################################################"
112    fi
113    $OUTPUT
114}
115
116###############################################################
117### MAIN
118
119handle_command() {
120
121    COMMAND="$1"
122
123    [ "$COMMAND" ] || failure "Type 'cereal help' for usage."
124    shift
125
126    case $COMMAND in
127        'attach'|'a')
128            attach "$@"
129            ;;
130        'follow'|'f')
131            follow "$@"
132            ;;
133        'list'|'l')
134            list "$@" || failure "There are no sessions." 1
135            ;;
136        'help'|'h'|'?')
137            usage
138            ;;
139        *)
140            failure "Unknown command: '$COMMAND'
141Type '$CMD help' for usage."
142            ;;
143    esac
144
145    exit "$ERR"
146}
147
148handle_command "$@"
Note: See TracBrowser for help on using the repository browser.