summaryrefslogtreecommitdiff
path: root/examples/functions/coshell.bash
blob: dc177b30329144a9c8a7aa78e5ad441cc9cd1afa (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# vi:set sts=2 sw=2 ai:
#
# coshell.bash - Control shell coprocesses (see coprocess.bash).
#

function coshell ()
{
  while (( $# > 0 )) ; do
    case "$1" in
      #
      # coshell open
      #
      o|op|ope|open)
	shift
	coprocess open "$@"
	local ret=$?

	# This should eat any ssh error messages or what not.
	coshell eval : >/dev/null 2>&1
	return $ret
	;;

      #
      # coshell close
      #
      c|cl|clo|close)
	shift
	coprocess close "$@"
	return $?
	;;

      #
      # coshell eval
      #
      e|ev|eva|eval)
	shift
	local cookie=$RANDOM
	if (( $# == 0 )) ; then
	  echo "coshell eval: no argumentsl" >&2
	  return 1
	fi
	if [ x$coprocess_pid = x ] ; then
	  echo "coshell eval: no active coshell" >&2
	  return 1
	fi

	coprocess print "$@" 
	coprocess print "coprocess_rc=\$?"
	coprocess print "printf 'coprocess-$cookie----\n%d\n' \$coprocess_rc"
	if [ x$coprocess_pid = x ] ; then
	  return 0
	fi

	local ol
	while coprocess read ol ; do
	  case "$ol" in
	    *coprocess-$cookie----*)
	      ol="${ol%coprocess-$cookie----}"
	      echo -n "$ol"
	      break
	      ;;
	  esac
	  echo "$ol"
	done
	coprocess read ol
	return $ol
	;;

      #
      # coshell sendfile
      #
      s|se|sen|send|sendf|sendfi|sendfil|sendfile)
	shift
	if (( $# != 2 )) ; then
	  echo "coshell sendfile: syntax is 'coshell sendfile SRC TARGET'" >&2
	  return 1
	fi
	if [ x$coprocess_pid = x ] ; then
	  echo "coshell sendfile: no active coshell" >&2
	  return 1
	fi

	local target=$2
	if coshell test -d "$target" ; then
	  target="$target/${1##*/}" 
	fi

	coprocess print "uudecode <<END_OF_FILE"
	uuencode -m "$target" <$1 |coprocess print --stdin
	coshell eval "END_OF_FILE"
	return $?
	;;

      #
      # coshell getfile
      #
      g|ge|get|getf|getfi|getfil|getfile)
	shift
	if (( $# != 2 )) ; then
	  echo "coshell getfile: syntax is 'coshell getfile SRC TARGET'" >&2
	  return 1
	fi
	if [ x$coprocess_pid = x ] ; then
	  echo "coshell getfile: no active coshell" >&2
	  return 1
	fi

	local target=$2
	if test -d "$target" ; then
	  target="$target/${1##*/}" 
	fi

	coshell eval uuencode -m "$target" "<" "$1" |uudecode
	return $?
	;;

      *)
	coshell eval "$@"
	return $?
	;;
    esac
    shift
  done
  coprocess status
  return $?
}