summary refs log tree commit diff
path: root/Functions/TCP/tcp_log
diff options
context:
space:
mode:
authorPeter Stephenson <pws@users.sourceforge.net>2003-02-06 12:21:49 +0000
committerPeter Stephenson <pws@users.sourceforge.net>2003-02-06 12:21:49 +0000
commit5c1f3b65a6f5abeae8459f41adb8fd2316971515 (patch)
tree21a82daa1abab96c967d731c7afe2a3a2bd07fff /Functions/TCP/tcp_log
parent809ab19dff75185a805b4cbb31a6b89f225167f4 (diff)
downloadzsh-5c1f3b65a6f5abeae8459f41adb8fd2316971515.tar.gz
zsh-5c1f3b65a6f5abeae8459f41adb8fd2316971515.tar.xz
zsh-5c1f3b65a6f5abeae8459f41adb8fd2316971515.zip
18202: New TCP function system plus small error message change in ztcp.
Diffstat (limited to 'Functions/TCP/tcp_log')
-rw-r--r--Functions/TCP/tcp_log94
1 files changed, 94 insertions, 0 deletions
diff --git a/Functions/TCP/tcp_log b/Functions/TCP/tcp_log
new file mode 100644
index 000000000..e8ebaca23
--- /dev/null
+++ b/Functions/TCP/tcp_log
@@ -0,0 +1,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