about summary refs log tree commit diff
path: root/Functions/TCP/tcp_log
blob: e8ebaca23862ba062f55d144474b482ed139202e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Log TCP output.
#
# Argument:  Output filename.
#
# Options:
#   -a    Append.  Otherwise the existing file is truncated without warning.
#	  (N.B.: even if logging was already active to it!)
#   -s    Per-session logs.  Output to <filename>1, <filename>2, etc.
#   -c    Close logging.
#   -n/-N Turn off or on normal output; output only goes to the logfile, if
#         any.  Otherwise, output also appears interactively.  This
#         can be given with -c (or any other option), then no output
#         goes anywhere.  However, input is still handled by the usual
#         mechanisms --- $tcp_lines and $TCP_LINE are still set, hence
#         tcp_expect still works.  Equivalent to (un)setting TCP_SILENT.
#
# With no options and no arguments, print the current configuration.
#
# Per-session logs are raw output, otherwise $TCP_PROMPT is prepended
# to each line.

emulate -L zsh
setopt cbases extendedglob

local opt append sess close
integer activity
while getopts "ascnN" opt; do
  (( activity++ ))
  case $opt in
    # append to existing file
    a) append=1
       ;;
    # per-session
    s) sess=1
       ;;
    # close
    c) close=1
       ;;
    # turn off interactive output
    n) TCP_SILENT=1
       ;;
    # turn on interactive output
    N) unset TCP_SILENT
       ;;
    # incorrect option
    \?) return 1
	;;
    # correct option I forgot about
    *) print "$0: option -$opt not handled, oops." >&2
       return 1
       ;;
  esac
done
(( OPTIND > 1 )) && shift $(( OPTIND - 1)) 

if [[ -n $close ]]; then
  if (( $# )); then
    print "$0: too many arguments for -c" >&2
    return 1
  fi
  unset TCP_LOG TCP_LOG_SESS
  return 0
fi

if (( $# == 0 && ! activity )); then
  print "\
Per-session log: ${TCP_LOG_SESS:-<none>}
Overall log:     ${TCP_LOG:-<none>}
Silent?          ${${TCP_SILENT:+yes}:-no}"
  return 0
fi

if (( $# != 1 )); then
  print "$0: wrong number of arguments" >&2
  return 1
fi

if [[ -n $sess ]]; then
  TCP_LOG_SESS=$1
  if [[ -z $append ]]; then
    local sesslogs
    integer i
    sesslogs=(${TCP_LOG_SESS}*(N))
    # yes, i know i can do this with multios
    for (( i = 1; i <= $#sesslogs; i++ )); do
      : >$sesslogs[$i]
    done
  fi
else
  TCP_LOG=$1
  [[ -z $append ]] && : >$TCP_LOG
fi

return 0