about summary refs log tree commit diff
path: root/Test/C04funcdef.ztst
blob: d297ea418b6832f70a0351e7049fd5e499b00acd (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
95
96
97
98
99
100
101
%test

  function f$$ () {
    print regress expansion of function names
  }
  f$$
0:Regression test: `function f$$ () { ... }'
>regress expansion of function names

  function foo () print bar
  foo
0:Function definition without braces
>bar

  functions -M m1
  m1() { (( $# )) }
  print $(( m1() ))
  print $(( m1(1) ))
  print $(( m1(1,2) ))
0:User-defined math functions, argument handling
>0
>1
>2

  functions -M m2
  m2() {
    integer sum
    local val
    for val in $*; do
      (( sum += $val ))
    done
  }
  print $(( m2(1) ))
  print $(( m2(1,3+3,4**2) ))
0:User-defined math functions, complex argument handling
>1
>23

  functions -M m3 1 2
  m3() { (( 1 )) }
  print zero
  (print $(( m3() )))
  print one
  print $(( m3(1) ))
  print two
  print $(( m3(1,2) ))
  print three
  (print $(( m3(1,2,3) )))
1:User-defined math functions, argument checking
>zero
>one
>1
>two
>1
>three
?(eval):4: wrong number of arguments: m3()
?(eval):10: wrong number of arguments: m3(1,2,3)

  functions -M m4 0 0 testmathfunc
  functions -M m5 0 0 testmathfunc
  testmathfunc() {
    if [[ $0 = m4 ]]; then
      (( 4 ))
    else
      (( 5 ))
    fi
  }
  print $(( m4() ))
  print $(( m5() ))
0:User-defined math functions, multiple interfaces
>4
>5

  command_not_found_handler() {
    print "Great News!  I've handled the command:"
    print "$1"
    print "with arguments:"
    print -l ${argv[2,-1]}
  }
  ACommandWhichHadBetterNotExistOnTheSystem and some "really useful" args
0:Command not found handler, success
>Great News!  I've handled the command:
>ACommandWhichHadBetterNotExistOnTheSystem
>with arguments:
>and
>some
>really useful
>args

  command_not_found_handler() {
     print "Your command:" >&2
     print "$1" >&2
     print "has gone down the tubes.  Sorry." >&2
     return 1
  }
  ThisCommandDoesNotExistEither
127:Command not found handler, failure
?Your command:
?ThisCommandDoesNotExistEither
?has gone down the tubes.  Sorry.
?(eval):7: command not found: ThisCommandDoesNotExistEither