source: trunk/third/kermit/ckedemo.ksc @ 10780

Revision 10780, 9.4 KB checked in by brlewis, 27 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r10779, which included commits to RCS files with non-trunk default branches.
Line 
1COMMENT - File CKEDEMO.KSC
2;
3; Exercises Kermit's programming constructs.
4; Converted to block-structured format, March 1996.
5;
6; Usage: tell Kermit to "take ckedemo.ksc"
7;
8; echo If you don't see the message "Proceeding..."
9; echo on the next line, C-Kermit was not configured for script programming.
10; check if
11; echo Proceeding...
12; sleep 2
13; echo
14
15; forward arrays
16
17switch \v(program) {
18  :MS-DOS_KERMIT,
19    if < \v(version) 315 stop 1 Version 3.15 or later required...
20    echo MS-DOS Kermit Programming-Constructs Test
21    break
22  :C-Kermit,
23    if < \v(version) 600192 stop 1 Version 6.0 or later required...
24    echo C-Kermit Programming-Constructs Test
25}
26echo
27echo Defining macros:
28
29define ERRMSG if def \%1 echo \%1, end 1
30
31COMMENT - COPY macro.  Only works for single files.
32;
33echo { COPY}
34define COPY {
35  if > \v(argc) 3 -                     ; Too many arguments given?
36    end 1 \%0: too many arguments       ;  Too many, fail.
37  if not def \%1 -                      ; Was a source file given?
38    end 1 copy what?                    ;  No.
39  if not = \ffiles(\%1) 1 -             ; Yes, but is it "wild?"?
40    end 1 wildcards not allowed         ;  Sorry, no wildcards.
41  if not exist \%1 -                    ; Does source file exist?
42    end 1 file \%1 doesn't exist        ;  No, so it can't be copied.
43  if not def \%2 -                      ; Destination file specified?
44    end 1 copy \%1 to what?             ;  No, so it can't be copied to.
45  if not = \ffiles(\%2) 0 -             ; Does it already exist?
46    end 1 file \%2 already exists       ;  Yes, so don't write over it.
47  if equal "\v(system)" "UNIX" -        ; COPY command for UNIX:
48    run cp \%1 \%2                      ;   cp source destination
49  else if equal "\v(system)" "AOS/VS" - ; For AOS/VS:
50    run COPY \%2 \%1                    ;   COPY destination source
51  else run COPY \%1 \%2                 ; Others: COPY source destination
52  if exist \%2 end 0                    ; Check our work and return SUCCESS
53  else end 1 COPY failed.               ;   or FAILURE as appropriate.
54}
55
56COMMENT - SPELLNUM macro.
57;
58echo { SPELLNUM}
59define SPELLNUM {
60  local \%x
61  if not def \%1 end 1
62  if not numeric \%1 end 1 { Sorry, not a number}
63  xif < \%1 0 {
64     asg \%x { minus}
65     asg \%1 \feval(0-\%1)
66  }
67  asg \%1 \feval(\%1)  ; This takes care of "09" etc
68  if > \%1 9 end 1 { Sorry, too hard}
69  else forward \%1
70  :0,end 0 \%x zero
71  :1,end 0 \%x one
72  :2,end 0 \%x two
73  :3,end 0 \%x three
74  :4,end 0 \%x four
75  :5,end 0 \%x five
76  :6,end 0 \%x six
77  :7,end 0 \%x seven
78  :8,end 0 \%x eight
79  :9,end 0 \%x nine
80}
81
82COMMENT - CALC macro.  "Pocket calculator".  No arguments.
83;
84echo { CALC}
85define CALC {
86  echo Press Return to exit       ; Say how to exit.
87  def \%1 1                       ; Initial condition for loop
88  while def \%1 {                 ; Loop until they want to exit
89    ask \%1 { expression: }       ; Ask for an expression
90    echo \flpad(\feval(\%1),10)   ; Evaluate and print answer
91  }
92  echo Back to...                 ; All done
93}
94
95echo { ADDINGMACHINE}
96define ADDINGMACHINE {
97  local total \%s
98  echo Type numbers (one per line) or press Return to quit...
99  assign total 0                          ; Initialize the sum
100  while true {                            ; Loop till done
101    askq \%s                              ; Wait for a number
102    if not def \%s break                  ; Return quits loop
103    increment total \%s                   ; Add it to the sum
104    write screen \flpad(\%s,10)\flpad(\m(total),10) ; Print number and subtotal
105  }
106  echo Total\flpad(\m(total),15,.)
107}
108
109COMMENT - SMALLEST macro, recursive.  Arguments:
110; 1 = a number
111; 2 = a number
112; 3 = a number
113; Prints the smallest of the three.
114;
115echo { SMALLEST}
116def SMALLEST {
117  if < \v(argc) 4 end 1 { Sorry - three numbers required.}
118  xif < \%1 \%2 {                   ; Compare first two arguments
119    echo \%1 is less than \%2       ; The first one is smaller
120    xif < \%1 \%3 {                 ; Compare it with the third
121      echo \%1 is less than \%3     ; The first one is smaller
122      def \%a \%1                   ; Copy it to \%a
123    } else {                        ; The third is smaller
124      echo \%1 is not less than \%3
125      def \%a \%3                   ; Copy it to \%a
126    }
127  } else {                          ; Otherwise
128    echo \%1 is not less than \%2   ; The second is smaller
129    xif < \%2 \%3 {                 ; Compare it with the third
130      echo \%2 is less than \%3     ; The second is smaller
131      def \%a \%2                   ; Copy it to \%a
132    } else {                        ; The third is smaller
133      echo \%2 is not less than \%3
134      def \%a \%3                   ; Copy it to \%a
135    }
136  }
137  echo So the smallest is \%a.      ; Announce the winner
138}
139
140ec Spelling some numbers...
141for \%i -5 9 1 { spellnum \%i }
142
143echo Calculator demo...
144calc
145
146echo Adding machine demo - Enter an empty line to quit...
147addingmachine
148
149if eq {\v(program)} {MS-DOS_KERMIT} forward smallest
150
151; No \fexec() in MS-DOS Kermit.
152
153COMMENT - SUM macro, recursive.  Argument:
154; 1 = limit of sum, a positive number.
155; Returns sum of 1 through the number.
156;
157echo { SUM}
158def SUM {
159  if not def \%1 return        ; Make sure there is an argument
160  if not numeric \%1 return    ; Make sure argument is numeric
161  if not > \%1 0 return        ; Make sure argument is positive
162  if = \%1 1 return 1          ; If argument is 1, the sum is 1
163  else return \feval(\%1+\fexecute(sum,\feval(\%1-1)))
164}
165
166COMMENT - ADDEMUP macro, for calling SUM.
167;
168echo { ADDEMUP}
169def ADDEMUP {
170  local total
171  assign total \fexec(sum,\%1)
172  if def total echo SUM = \m(total)
173  else echo SUM doesn't work for \%1
174}
175
176addemup 1
177addemup 2
178addemup 3
179addemup 4
180addemup 5
181addemup 10
182addemup 20
183
184:SMALLEST
185
186while true {
187  ask \%x { Type 3 numbers separated by spaces or an empty line to quit:  }
188  if not def \%x break
189  smallest \%x
190}
191
192echo WHILE-LOOP TEST...
193echo You should see:
194echo { 0 1 2 3 4}
195def \%a 0
196while < \%a 5 { write scr { \%a}, incr \%a }
197echo
198
199echo NESTED WHILE-LOOP TEST...
200echo You should see:
201echo { 0:0 0:1 0:2 1:0 1:1 1:2 2:0 2:1 2:2}
202def \%a 0
203while < \%a 3 {
204  def \%b 0
205  while < \%b 3 {
206    write scr { \%a:\%b}
207    incr \%b
208  }
209  incr \%a
210}
211echo
212
213echo FOR-LOOP INSIDE WHILE-LOOP
214echo You should see:
215echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}
216def \%a 1
217while < \%a 4 {
218  for \%i 1 3 1 { write scr { \%a:\%i} }
219  inc \%a
220}
221echo
222
223echo WHILE-LOOP INSIDE FOR-LOOP
224echo You should see:
225echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3}
226for \%i 1 3 1 {
227  def \%a 1
228  while < \%a 4 {
229    writ scr { \%i:\%a}
230    incr \%a
231  }
232}
233echo
234
235echo NESTED FOR LOOP TEST
236echo You should see:
237echo { 1:1 1:2 1:3 2:2 2:3 3:3}
238for \%i 1 3 1 {
239  for \%j \%i 3 1 {
240    write scr { \%i:\%j}
241  }
242}
243echo
244
245echo NESTED FOR/WHILE/BREAK/CONTINUE TEST
246echo You should see:
247echo { 1:1 1:3 3:1 3:3}
248for \%i 1 4 1 {
249  if = \%i 2 continue
250  else if = \%i 4 break
251  asg \%j 0
252  while < \%j 4 {
253    incr \%j
254    if = \%j 2 continue
255    else if = \%j 4 break
256    write screen { \%i:\%j}
257  }
258}
259echo
260
261echo END from inside nested FOR loops
262echo You should see:
263echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1}
264define xx {
265  for \%i 1 3 1 {
266    for \%j 1 3 1 {
267      write scr { \%i:\%j}
268      if = \%i 3 if = \%j 1 end
269    }
270  }
271}
272do xx
273echo
274
275if not eq {\v(program)} {C-Kermit} forward xifendtest
276
277echo RETURN from inside nested FOR loops
278echo You should see "IT WORKS":
279define xx {
280  local \%i \%j
281  for \%i 1 3 1 {
282    for \%j 1 3 1 {
283      if = \%i 3 if = \%j 1 return IT \%1
284    }
285  }
286  echo YOU SHOULD NOT SEE THIS
287}
288echo "\fexec(xx WORKS)"
289
290:XIFENDTEST
291echo END message from inside XIF
292echo You should see "IT WORKS"
293def xx xif = 1 1 { end 0 "IT \%1"}
294xx WORKS
295
296echo Grouping of words in IF EQUAL
297echo You should see "IT WORKS":
298def \%a one two three
299if equal {\%a} {one two three} echo "IT WORKS"
300else echo It doesn't work, foo.
301ec
302
303echo Use of expressions and braces in FOR-loop variables
304echo You should see "1 2 3":
305def \%a 2
306for \%i 1 { 1 + \%a } 1 { write screen {\%i } }
307echo
308
309echo A macro that echoes its arguments
310def XX {
311  local \%i
312  for \%i 1 { \v(argc) - 1 } 1 {
313    echo \%i. "\&_[\%i]"
314  }
315}
316while true {
317  ask \%a {Type some words (or just carriage return to quit): }
318  if not def \%a break
319  xx \%a
320}
321echo
322
323if eq {\v(program)} {MS-DOS_KERMIT} forward arrays
324if not eq {\v(connection)} {\v(remote)} forward arrays
325ec MINPUT test...
326ec Please type one of the following (without the number):
327ec 1. ab cd
328ec 2. abcd
329ec 3. xyz
330ec You have 20 seconds...
331minput 20 {ab cd} abcd xyz
332ec
333if success echo You typed Number \v(minput).
334else echo You did not type any of them within the time limit.
335echo
336
337:ARRAYS
338getc \%c {ARRAY TEST -- type a char to continue: }
339
340declare \&a[26]
341local \%i \%j \%t                    ; Local variables
342assign \%i 1
343asg \&a[\%i] zebra
344incr \%i
345asg \&a[\%i] x-ray
346incr \%i 1
347asg \&a[\%i] baker
348incr \%i 3-2
349asg \&a[\%i] able
350decr \%i -1
351asg \&a[\%i] charlie
352asg \&a[\%i+1] easy
353asg \&a[\%i+2] george
354asg \&a[\%i+3] dog
355asg \%n \%i+2+8/4
356asg \&a[\%n] fox
357echo ARRAY TEST - Sorting ...
358getc \%c {Type a char to continue: }
359for \%i 1 \%n-1 1 {                  ; Outer loop: i from 1 to n-1
360    for \%j \%i \%n 1 {               ; Inner loop: j from i to n
361        xif lgt \&a[\%i] \&a[\%j] {  ; Compare array elements
362            asg \%t \&a[\%i]         ; If out of order,
363            asg \&a[\%i] \&a[\%j]    ; exchange them
364            asg \&a[\%j] \%t
365        }
366    }
367}
368echo You should see 9 words in alphabetical order:
369getc \%c {Type a char to continue: }
370for \%i 1 \%n 1 { echo \&a[\%i] }    ; All sorted - print them
371
372echo End of \v(cmdfile)
373echo
Note: See TracBrowser for help on using the repository browser.