1 | COMMENT - 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 | |
---|
17 | switch \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 | } |
---|
26 | echo |
---|
27 | echo Defining macros: |
---|
28 | |
---|
29 | define ERRMSG if def \%1 echo \%1, end 1 |
---|
30 | |
---|
31 | COMMENT - COPY macro. Only works for single files. |
---|
32 | ; |
---|
33 | echo { COPY} |
---|
34 | define 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 | |
---|
56 | COMMENT - SPELLNUM macro. |
---|
57 | ; |
---|
58 | echo { SPELLNUM} |
---|
59 | define 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 | |
---|
82 | COMMENT - CALC macro. "Pocket calculator". No arguments. |
---|
83 | ; |
---|
84 | echo { CALC} |
---|
85 | define 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 | |
---|
95 | echo { ADDINGMACHINE} |
---|
96 | define 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 | |
---|
109 | COMMENT - SMALLEST macro, recursive. Arguments: |
---|
110 | ; 1 = a number |
---|
111 | ; 2 = a number |
---|
112 | ; 3 = a number |
---|
113 | ; Prints the smallest of the three. |
---|
114 | ; |
---|
115 | echo { SMALLEST} |
---|
116 | def 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 | |
---|
140 | ec Spelling some numbers... |
---|
141 | for \%i -5 9 1 { spellnum \%i } |
---|
142 | |
---|
143 | echo Calculator demo... |
---|
144 | calc |
---|
145 | |
---|
146 | echo Adding machine demo - Enter an empty line to quit... |
---|
147 | addingmachine |
---|
148 | |
---|
149 | if eq {\v(program)} {MS-DOS_KERMIT} forward smallest |
---|
150 | |
---|
151 | ; No \fexec() in MS-DOS Kermit. |
---|
152 | |
---|
153 | COMMENT - SUM macro, recursive. Argument: |
---|
154 | ; 1 = limit of sum, a positive number. |
---|
155 | ; Returns sum of 1 through the number. |
---|
156 | ; |
---|
157 | echo { SUM} |
---|
158 | def 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 | |
---|
166 | COMMENT - ADDEMUP macro, for calling SUM. |
---|
167 | ; |
---|
168 | echo { ADDEMUP} |
---|
169 | def 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 | |
---|
176 | addemup 1 |
---|
177 | addemup 2 |
---|
178 | addemup 3 |
---|
179 | addemup 4 |
---|
180 | addemup 5 |
---|
181 | addemup 10 |
---|
182 | addemup 20 |
---|
183 | |
---|
184 | :SMALLEST |
---|
185 | |
---|
186 | while 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 | |
---|
192 | echo WHILE-LOOP TEST... |
---|
193 | echo You should see: |
---|
194 | echo { 0 1 2 3 4} |
---|
195 | def \%a 0 |
---|
196 | while < \%a 5 { write scr { \%a}, incr \%a } |
---|
197 | echo |
---|
198 | |
---|
199 | echo NESTED WHILE-LOOP TEST... |
---|
200 | echo You should see: |
---|
201 | echo { 0:0 0:1 0:2 1:0 1:1 1:2 2:0 2:1 2:2} |
---|
202 | def \%a 0 |
---|
203 | while < \%a 3 { |
---|
204 | def \%b 0 |
---|
205 | while < \%b 3 { |
---|
206 | write scr { \%a:\%b} |
---|
207 | incr \%b |
---|
208 | } |
---|
209 | incr \%a |
---|
210 | } |
---|
211 | echo |
---|
212 | |
---|
213 | echo FOR-LOOP INSIDE WHILE-LOOP |
---|
214 | echo You should see: |
---|
215 | echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3} |
---|
216 | def \%a 1 |
---|
217 | while < \%a 4 { |
---|
218 | for \%i 1 3 1 { write scr { \%a:\%i} } |
---|
219 | inc \%a |
---|
220 | } |
---|
221 | echo |
---|
222 | |
---|
223 | echo WHILE-LOOP INSIDE FOR-LOOP |
---|
224 | echo You should see: |
---|
225 | echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1 3:2 3:3} |
---|
226 | for \%i 1 3 1 { |
---|
227 | def \%a 1 |
---|
228 | while < \%a 4 { |
---|
229 | writ scr { \%i:\%a} |
---|
230 | incr \%a |
---|
231 | } |
---|
232 | } |
---|
233 | echo |
---|
234 | |
---|
235 | echo NESTED FOR LOOP TEST |
---|
236 | echo You should see: |
---|
237 | echo { 1:1 1:2 1:3 2:2 2:3 3:3} |
---|
238 | for \%i 1 3 1 { |
---|
239 | for \%j \%i 3 1 { |
---|
240 | write scr { \%i:\%j} |
---|
241 | } |
---|
242 | } |
---|
243 | echo |
---|
244 | |
---|
245 | echo NESTED FOR/WHILE/BREAK/CONTINUE TEST |
---|
246 | echo You should see: |
---|
247 | echo { 1:1 1:3 3:1 3:3} |
---|
248 | for \%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 | } |
---|
259 | echo |
---|
260 | |
---|
261 | echo END from inside nested FOR loops |
---|
262 | echo You should see: |
---|
263 | echo { 1:1 1:2 1:3 2:1 2:2 2:3 3:1} |
---|
264 | define 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 | } |
---|
272 | do xx |
---|
273 | echo |
---|
274 | |
---|
275 | if not eq {\v(program)} {C-Kermit} forward xifendtest |
---|
276 | |
---|
277 | echo RETURN from inside nested FOR loops |
---|
278 | echo You should see "IT WORKS": |
---|
279 | define 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 | } |
---|
288 | echo "\fexec(xx WORKS)" |
---|
289 | |
---|
290 | :XIFENDTEST |
---|
291 | echo END message from inside XIF |
---|
292 | echo You should see "IT WORKS" |
---|
293 | def xx xif = 1 1 { end 0 "IT \%1"} |
---|
294 | xx WORKS |
---|
295 | |
---|
296 | echo Grouping of words in IF EQUAL |
---|
297 | echo You should see "IT WORKS": |
---|
298 | def \%a one two three |
---|
299 | if equal {\%a} {one two three} echo "IT WORKS" |
---|
300 | else echo It doesn't work, foo. |
---|
301 | ec |
---|
302 | |
---|
303 | echo Use of expressions and braces in FOR-loop variables |
---|
304 | echo You should see "1 2 3": |
---|
305 | def \%a 2 |
---|
306 | for \%i 1 { 1 + \%a } 1 { write screen {\%i } } |
---|
307 | echo |
---|
308 | |
---|
309 | echo A macro that echoes its arguments |
---|
310 | def XX { |
---|
311 | local \%i |
---|
312 | for \%i 1 { \v(argc) - 1 } 1 { |
---|
313 | echo \%i. "\&_[\%i]" |
---|
314 | } |
---|
315 | } |
---|
316 | while true { |
---|
317 | ask \%a {Type some words (or just carriage return to quit): } |
---|
318 | if not def \%a break |
---|
319 | xx \%a |
---|
320 | } |
---|
321 | echo |
---|
322 | |
---|
323 | if eq {\v(program)} {MS-DOS_KERMIT} forward arrays |
---|
324 | if not eq {\v(connection)} {\v(remote)} forward arrays |
---|
325 | ec MINPUT test... |
---|
326 | ec Please type one of the following (without the number): |
---|
327 | ec 1. ab cd |
---|
328 | ec 2. abcd |
---|
329 | ec 3. xyz |
---|
330 | ec You have 20 seconds... |
---|
331 | minput 20 {ab cd} abcd xyz |
---|
332 | ec |
---|
333 | if success echo You typed Number \v(minput). |
---|
334 | else echo You did not type any of them within the time limit. |
---|
335 | echo |
---|
336 | |
---|
337 | :ARRAYS |
---|
338 | getc \%c {ARRAY TEST -- type a char to continue: } |
---|
339 | |
---|
340 | declare \&a[26] |
---|
341 | local \%i \%j \%t ; Local variables |
---|
342 | assign \%i 1 |
---|
343 | asg \&a[\%i] zebra |
---|
344 | incr \%i |
---|
345 | asg \&a[\%i] x-ray |
---|
346 | incr \%i 1 |
---|
347 | asg \&a[\%i] baker |
---|
348 | incr \%i 3-2 |
---|
349 | asg \&a[\%i] able |
---|
350 | decr \%i -1 |
---|
351 | asg \&a[\%i] charlie |
---|
352 | asg \&a[\%i+1] easy |
---|
353 | asg \&a[\%i+2] george |
---|
354 | asg \&a[\%i+3] dog |
---|
355 | asg \%n \%i+2+8/4 |
---|
356 | asg \&a[\%n] fox |
---|
357 | echo ARRAY TEST - Sorting ... |
---|
358 | getc \%c {Type a char to continue: } |
---|
359 | for \%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 | } |
---|
368 | echo You should see 9 words in alphabetical order: |
---|
369 | getc \%c {Type a char to continue: } |
---|
370 | for \%i 1 \%n 1 { echo \&a[\%i] } ; All sorted - print them |
---|
371 | |
---|
372 | echo End of \v(cmdfile) |
---|
373 | echo |
---|