Debian Codegraph @

demo/gci-try.rexx

raw
1 #!/usr/bin/env regina
2 /*
3 * This is an example for using GCI. Enjoy, copy and paste!
4 * We have three different execution paths for Unix, Win32 and OS/2.
5 * For some interpreters, most notable the builtin interpreter of OS/2, you
6 * have to rename this file to gci-try.cmd
7 */
8 parse version version
9 say "Your REXX interpreter is" version
10
11 /******************************************************************************
12 * Try to detect the system to show several things.
13 */
14 parse var version "REXX-"ipret"_".
15 IsRegina = 0
16 if ipret = "Regina" then do
17 IsRegina = 1
18 uname = uname('S')
19 options NOEXT_COMMANDS_AS_FUNCS
20 end
21
22 parse source system . source
23
24 system = translate(system)
25 if left(system, 3) = "WIN" then do
26 library = "GCI.DLL"
27 CLib = "MSVCRT"
28 system = "WIN"
29 end
30 else if system = "OS/2" then do
31 library = "GCI.DLL"
32 system = "OS/2"
33 end
34 else do
35 library = "libgci.so"
36 tr = trace()
37 trace o
38 "test `uname -s` = 'SunOS'"
39 if RC = 0 then do
40 CLib = "libc.so.1"
41 MathLib = "m"
42 end
43 else do
44 if IsRegina & Left(uname, 6) = 'CYGWIN' then do
45 CLib = "cygwin1.dll"
46 MathLib = "cygwin1.dll"
47 end
48 else do
49 CLib = "libc.so.6"
50 MathLib = "m"
51 end
52 end
53 trace value tr
54 end
55
56 /*
57 * Check for a builtin RxFuncDefine. Call it with errorneous arguments and
58 * expect a simple error if installed and a syntax signal if unknown.
59 */
60 InternalGCI = 0
61 signal on syntax name NotInstalled
62 x = "X"
63 x. = "X"
64 h = RxFuncDefine(x,x,x,x)
65 if h \= 0 & h \= 10005 & DataType(h, "NUM") then
66 InternalGCI = 1
67 NotInstalled:
68 drop GCI_RC
69
70 signal on syntax
71 if \InternalGCI then do
72 /*
73 * The DLL may have been loaded already. Prevent bugs at this stage.
74 */
75 call RxFuncDrop RxFuncDefine
76 call RxFuncDrop GciPrefixChar
77 call RxFuncDrop GciFuncDrop
78 if RxFuncadd(RxFuncDefine, "gci", "RxFuncDefine") \= 0 then do
79 msg = "Can't load RxFuncDefine of" library
80 if IsRegina then
81 msg = msg || ":" RxFuncErrMsg()
82 say msg
83 return 1
84 end
85 if RxFuncadd(GciFuncDrop, "gci", "GciFuncDrop") \= 0 then do
86 msg = "Can't load GciFuncDrop of" library
87 if IsRegina then
88 msg = msg || ":" RxFuncErrMsg()
89 say msg
90 return 1
91 end
92 call RxFuncadd GciPrefixChar, "gci", "GciPrefixChar"
93 say "Your interpreter has no internal support of GCI"
94 end
95 else
96 say "Your interpreter has internal support of GCI"
97
98 say ""
99
100 /******************************************************************************
101 * After the prelimnaries try to use the GCI.
102 */
103 if system = "OS/2" then signal useOS2
104
105 say "Trying to copy string 'hello' to a string 'world' using the C library"
106 stem.calltype = cdecl
107 stem.0 = 2
108 stem.1.name = "Target"
109 stem.1.type = indirect string80
110 stem.2.name = "Source"
111 stem.2.type = indirect string80
112 stem.return.type = "" /* We are not interested in the return value */
113
114 call funcDefine strcpy, CLib, "strcpy", stem
115
116 stem.1.name = "Target"
117 stem.1.value = "world"
118 stem.2.name = "Source"
119 stem.2.value = "hello"
120 call strcpy stem
121 say stem.1.name"="stem.1.value stem.2.name"="stem.2.value
122
123 call funcDrop strcpy
124 say ""
125 /******************************************************************************
126 * Check if v1.1 GciPrefixChar works.
127 */
128
129 version = 1.0
130 signal on syntax name v1.0
131 say "Trying to detect and use v1.1 features (same result as above expected)"
132 oldChar = GciPrefixChar('!')
133 signal on syntax
134 stem.!calltype = cdecl
135 stem.0 = 2
136 stem.1.!name = "Target"
137 stem.1.!type = indirect string80
138 stem.2.!name = "Source"
139 stem.2.!type = indirect string80
140 stem.!return.!type = ""
141
142 call funcDefine strcpy, CLib, "strcpy", stem
143
144 stem.1.!name = "Target"
145 stem.1.!value = "world"
146 stem.2.!name = "Source"
147 stem.2.!value = "hello"
148 call strcpy stem
149 say stem.1.!name"="stem.1.!value stem.2.name"="stem.2.!value
150
151 call funcDrop strcpy
152 say ""
153 call GciPrefixChar oldChar
154 drop oldChar c1 c2
155 version = 1.1
156 v1.0:
157 /******************************************************************************
158 * Use the "as function" feature
159 */
160 say "Trying to find the last occurcance of '.' in 'James F. Cooper' using the C"
161 say "library using the 'as function' feature"
162 stem.calltype = cdecl as function
163 stem.0 = 2
164 stem.1.name = "String"
165 stem.1.type = indirect string80
166 stem.2.name = "Character"
167 stem.2.type = char
168 stem.return.type = indirect string80
169
170 call funcDefine strrchr, CLib, "strrchr", stem
171
172 stem.1.name = "Target"
173 stem.1.value = "James F. Cooper"
174 stem.2.name = "Character"
175 stem.2.value = .
176 say "The last dot starts at '" || strrchr( stem ) || "'"
177 say ""
178 say "Trying to find the last occurcance of '.' in 'James Cooper' using the C lib"
179 say "We expect a NULL pointer which leads to a dropped value which becomes ''"
180 say "for a return value."
181
182 stem.1.name = "Target"
183 stem.1.value = "James Cooper"
184 say "The last dot starts at '" || strrchr( stem ) || "'"
185
186 call funcDrop strrchr
187 say ""
188 /******************************************************************************
189 * Use the "with parameters as function" feature.
190 * Note that you must omit "as function" if the return value doesn't exist.
191 * We use separate functions for Windows and unix.
192 */
193 if system = "WIN" then signal useWindows
194
195 say "Trying to use the math library to compute some natural logarithms"
196 stem.calltype = cdecl with parameters as function
197 stem.0 = 1
198 stem.1.name = "X"
199 stem.1.type = float128
200 stem.return.type = float128
201 call RxFuncDefine logl, MathLib, "logl", stem
202 if RESULT \= 0 then do
203 stem.1.type = float96
204 stem.return.type = float96
205 call RxFuncDefine logl, MathLib, "logl", stem
206 end
207 if RESULT \= 0 then do
208 stem.1.type = float64
209 stem.return.type = float64
210 call RxFuncDefine logl, MathLib, "log", stem
211 end
212 if RESULT \= 0 then do
213 if IsRegina & InternalGCI then
214 say "Error, code" RESULT || ":" RxFuncErrMsg()
215 else
216 say "Error, code" RESULT || ":" GCI_RC
217 end
218 else do
219 say "some logarithms"
220 do i = 1 to 5
221 say "log("i")="logl(i)
222 end
223
224 call funcDrop logl
225 end
226 say ""
227 /*******************************/
228 say "Using a structure and checking the file system's size."
229 say "You may look into the source."
230 /*
231 * This examples has removed all unnecessary stuff.
232 */
233 stem.calltype = cdecl as function
234 stem.0 = 2
235 stem.1.type = indirect string256
236 stem.2.type = indirect container
237 stem.2.0 = 10 /* statvfs64 */
238 stem.2.1.type = unsigned /* bsize */
239 stem.2.2.type = unsigned /* frsize */
240 stem.2.3.type = unsigned64 /* blocks */
241 stem.2.4.type = unsigned64 /* bfree */
242 stem.2.5.type = unsigned64 /* bavail */
243 stem.2.6.type = unsigned64 /* files */
244 stem.2.7.type = unsigned64 /* ffree */
245 stem.2.8.type = unsigned64 /* favail */
246 stem.2.9.type = unsigned /* fsid */
247 stem.2.10.type = string256 /* indifferent between unices */
248 stem.return.type = integer
249
250 call funcDefine statvfs, CLib, "statvfs64", stem
251
252 args. = 0
253 args.1.value = source
254 args.2.value = 10 /* otherwise the argument becomes NULL */
255 if statvfs( args ) \= -1 then do
256 say "statvfs-info of" source
257 say "block size =" args.2.1.value "byte"
258 size = trunc(args.2.3.value * args.2.1.value / (1024 * 1024))
259 avail = trunc(args.2.5.value * args.2.1.value / (1024 * 1024))
260 say "file system size =" size"MB, available =" avail"MB"
261 say "file nodes =" args.2.6.value "available =" args.2.8.value
262 say "sid =" args.2.9.value
263 end
264 else
265 say "Sorry, '"source"' not found."
266
267 call funcDrop statvfs
268 say ""
269 /*******************************/
270
271 say "We use qsort of the C library for sorting some strings using arrays."
272 /*
273 * This examples has removed all unnecessary stuff.
274 * We need a sorting routine. Without callbacks we have to use one of a
275 * library. "strcmp" is a good example. We have to play with the dynamic
276 * link loader.
277 * The strategy is:
278 * Load the loader functions (dlopen, dlsym, dlclose)
279 * Load the compare routine (strcmp) using the loader functions
280 * Load the sorting routine (qsort) and do the sort
281 * Additional sort operations may have to redefine qsort only.
282 */
283 stem.calltype = cdecl with parameters as function
284 stem.0 = 2
285 stem.1.type = indirect string256
286 stem.2.type = integer
287 stem.return.type = integer /* handle, but who cares? */
288 call funcDefine dlopen, "dl", "dlopen", stem
289
290 stem.calltype = cdecl with parameters as function
291 stem.0 = 2
292 stem.1.type = integer /* handle */
293 stem.2.type = indirect string256
294 stem.return.type = integer /* entry point address, but who cares? */
295 call funcDefine dlsym, "dl", "dlsym", stem
296
297 stem.calltype = cdecl with parameters as function
298 stem.0 = 1
299 stem.1.type = integer /* handle */
300 stem.return.type = integer
301 call funcDefine dlclose, "dl", "dlclose", stem
302
303 CLibHandle = dlopen( CLib, 1 /* RTLD_LAZY */ )
304 if CLibHandle = 0 then do
305 say "dlopen() can't load" CLib
306 return 1
307 end
308
309 strcmp = dlsym( CLibHandle, "strcmp" )
310 if strcmp = 0 then do
311 say "dlsym() can't relocate strcmp()"
312 return 1
313 end
314
315 stem.calltype = cdecl
316 stem.0 = 4
317 stem.1.type = indirect array
318 stem.1.0 = 3
319 stem.1.1.type = string95
320 stem.2.type = integer
321 stem.3.type = integer
322 stem.4.type = integer
323 stem.return.type = ""
324 call funcDefine qsort10, CLib, "qsort", stem
325
326 args.0 = 4
327 args.1.value = 3
328 args.1.1.value = "Ann"
329 args.1.2.value = "Charles"
330 args.1.3.value = "Betty"
331 args.2.value = 3
332 args.3.value = 96
333 args.4.value = strcmp
334 say "Sorting (" args.1.1.value args.1.2.value args.1.3.value ") ..."
335 call qsort10 args
336 say "Sorted values are (" args.1.1.value args.1.2.value args.1.3.value ")"
337
338 call dlclose CLibHandle
339 call funcDrop qsort
340 call funcDrop dlclose
341 call funcDrop dlsym
342 call funcDrop dlopen
343 say ""
344 call accessStructTm
345 return 0
346
347
348 /***************************************************/
349 useWindows:
350 stem.calltype = stdcall with parameters as function
351 stem.0 = 4
352 stem.1.name = "HWND"
353 stem.1.type = unsigned
354 stem.2.name = "Text"
355 stem.2.type = indirect string1024
356 stem.3.name = "Caption"
357 stem.3.type = indirect string1024
358 stem.4.name = "Type"
359 stem.4.type = unsigned
360 stem.return.type = integer
361
362 call funcDefine messagebox, "user32", "MessageBoxA", stem
363
364 MB_YESNO_INFO = x2d(44)
365 if messagebox( 0, "Do you love this rocking GCI?", "GCI", MB_YESNO_INFO ) = 6 then
366 say "Yes, you're right, GCI is cool."
367 else
368 say "No, you're kidding! GCI is cool."
369
370 call funcDrop messagebox
371 say ""
372 /*******************************/
373 say "We operate on containers and check this file's date."
374 say "You may look into the source."
375 /*
376 * This examples has removed all unnecessary stuff.
377 */
378 stem.calltype = stdcall as function
379 stem.0 = 2
380 stem.1.type = indirect string256
381 stem.2.type = indirect container
382 stem.2.0 = 8 /* WIN32_FIND_DATA */
383 stem.2.1.type = unsigned /* FileAttributes */
384 stem.2.2.type = unsigned64 /* Creation */
385 stem.2.3.type = unsigned64 /* Access */
386 stem.2.4.type = unsigned64 /* Write */
387 stem.2.5.type = unsigned64 /* Size */
388 stem.2.6.type = unsigned64 /* Reserved */
389 stem.2.7.type = string259 /* FileName */
390 stem.2.8.type = string13 /* AlternateFileName */
391 stem.return.type = integer
392
393 stem2.calltype = stdcall with parameters
394 stem2.0 = 1
395 stem2.1.type = integer
396 stem2.return.type = ""
397
398 call funcDefine findfirstfile, "kernel32", "FindFirstFileA", stem
399
400 call funcDefine findclose, "kernel32", "FindClose", stem2
401
402 args. = 0
403 args.1.value = source
404 args.2.value = 8 /* otherwise the argument becomes NULL */
405 handle = findfirstfile( args )
406 if handle \= -1 then do
407 say "argument's name="source
408 say "filename="args.2.7.value
409 say "8.3-name="args.2.8.value
410 numeric digits 40
411 filetime = args.2.4.value
412 d = /*second*/ 1000*1000*10 * /*seconds per day*/ 60*60*24
413 daypart = trunc(filetime / d)
414 date = date( 'N', daypart + date('B', 16010101, 'S'), 'B')
415 ns = filetime - daypart * d
416 secs = ns % (10*1000*1000)
417 fract = ns // (10*1000*1000)
418 time = time('N', secs, 'S') || "." || right(fract, 7, '0')
419 say "ns from 1.1.1601="filetime "= GMT," date || "," time
420 numeric digits 9
421 call findclose handle
422 end
423 else
424 say "Sorry, '"source"' not found."
425
426 call funcDrop findfirstfile
427 call funcDrop findclose
428 say ""
429 call accessStructTm
430
431 return 0
432
433 /***************************************************/
434 accessStructTm: procedure expose IsRegina InternalGCI CLib version
435 if version < 1.1 then
436 return
437 say "Finally, we use the LIKE keyword and check the number of the week."
438 say "You may look into the source, we use a PROCEDURE and v1.1 specific code."
439 /*
440 * This examples has removed all unnecessary stuff.
441 */
442 tm.0 = 10
443 tm.1.type = integer /* tm_sec */
444 tm.2.type = integer /* tm_min */
445 tm.3.type = integer /* tm_hour */
446 tm.4.type = integer /* tm_mday */
447 tm.5.type = integer /* tm_mon */
448 tm.6.type = integer /* tm_year */
449 tm.7.type = integer /* tm_wday */
450 tm.8.type = integer /* tm_yday */
451 tm.9.type = integer /* tm_isdst */
452 tm.10.type = string 32 /* reserved stuff sometimes used by the OS */
453
454 time_t.0 = 1
455 time_t.1.type = integer64 /* SURPRISE! some systems may use 64 bit data types
456 * already. We don't have problems with this,
457 * because we use double buffering.
458 */
459
460 stem.calltype = cdecl
461 stem.0 = 1
462 stem.1.type = indirect container like time_t
463 stem.return.type = ""
464 call funcDefine _time, CLib, "time", stem
465
466 stem.calltype = cdecl
467 stem.0 = 1
468 stem.1.type = indirect container like time_t
469 stem.return.type = indirect container like tm
470 call funcDefine localtime, CLib, "localtime", stem
471
472 stem.calltype = cdecl
473 stem.0 = 4
474 stem.1.type = indirect string256 /* dest */
475 stem.2.type = unsigned /* size(dest) */
476 stem.3.type = indirect string256 /* template */
477 stem.4.type = indirect container like tm
478 stem.return.type = unsigned
479 call funcDefine strftime, CLib, "strftime", stem
480
481 time_val.1.value = 1
482 time_val.1.1.value = 1
483 call _time time_val
484
485 lct.1.value = 1
486 lct.1.1.value = time_val.1.1.value
487 call localtime lct
488
489 strf.1.value = ""
490 strf.2.value = 256
491 strf.3.value = "%A"
492 strf.4.value = lct.return.value
493 do i = 1 to 10
494 strf.4.i.value = lct.return.i.value
495 end
496 call strftime strf
497 dayname = strf.1.value
498
499 strf.3.value = "%B"
500 call strftime strf
501 monthname = strf.1.value
502
503 strf.3.value = "%U"
504 call strftime strf
505 week!Sun = strf.1.value
506
507 strf.3.value = "%W"
508 call strftime strf
509 week!Mon = strf.1.value
510
511 if week!Mon = week!Sun then
512 add = "."
513 else
514 add = " if you count Monday as the first day of the week. Otherwise it " ||,
515 "is the" week!Sun || ". week."
516 say "Today is a" dayname "in" monthname || ". We have the" week!Mon || ". week" || add
517 say
518 call funcDrop strftime
519 call funcDrop localtime
520 call funcDrop _time
521
522 return
523
524 /***************************************************/
525 useOS2:
526 say "Checking the high precision system timer."
527 stem.calltype = stdcall
528 stem.0 = 1
529 stem.1.name = "Frequency"
530 stem.1.type = indirect unsigned
531 stem.return.type = "unsigned"
532
533 call funcDefine DosTmrQueryFreq, "doscalls", "#362", stem
534
535 stem.1.name = "Frequency"
536 stem.1.value = 0 /* don't raise NOVALUE */
537
538 call DosTmrQueryFreq stem
539 if stem.return.value \= 0 then
540 say "Error" stem.return.value "while using DosTmrQueryFreq."
541 else
542 say "The timer has a frequency of" stem.1.value "Hz"
543
544 call funcDrop DosTmrQueryFreq
545 say ""
546 /*******************************/
547 say "You should hear your beeper."
548 /*
549 * Use the "with parameters" feature.
550 */
551 stem.calltype = stdcall with parameters
552 stem.0 = 2
553 stem.1.name = "Frequency"
554 stem.1.type = unsigned
555 stem.2.name = "Duration"
556 stem.2.type = unsigned
557 stem.return.type = "" /* We are not interested in the return value */
558
559 call funcDefine DosBeep, "doscalls", "#286", stem
560
561 do i = 500 to 3000 by 100
562 call DosBeep i, 10
563 end
564
565 call funcDrop DosBeep
566 say ""
567 /*******************************/
568 say "Checking the installed codepages."
569 /*
570 * Use the "as function" feature.
571 */
572 stem.calltype = stdcall as function
573 stem.0 = 3
574 stem.1.name = "cb"
575 stem.1.type = unsigned
576 stem.2.name = "arCP"
577 stem.2.type = indirect array
578 stem.2.0 = 25
579 stem.2.1.type = unsigned
580 stem.3.name = "pcCP"
581 stem.3.type = indirect unsigned
582 stem.return.type = "unsigned"
583
584 call funcDefine DosQueryCp, "doscalls", "#291", stem
585
586 drop stem.
587 stem. = 0 /* NOVALUE should not happen */
588 stem.0 = 3
589 stem.1.name = "cb"
590 stem.1.value = 100
591 stem.2.name = "arCP"
592 stem.2.value = 25
593 stem.3.name = "pcCP"
594 if DosQueryCp( stem ) = 0 then do
595 say "current codepage:" stem.2.1.value
596 do i = 2 to stem.3.value / 4
597 say "prepared codepage:" stem.2.i.value
598 end
599 end
600 else
601 say "Error calling DosQueryCp."
602
603 call funcDrop DosQueryCp
604 say ""
605 /*******************************/
606 say "Examining the file system on" left( source, 2 )
607 /*
608 * Use the "as function" feature.
609 */
610 stem.calltype = stdcall as function
611 stem.0 = 4
612 stem.1.name = "disknum"
613 stem.1.type = unsigned
614 stem.2.name = "infolevel"
615 stem.2.type = unsigned
616 stem.3.name = "pBuf"
617 stem.3.type = indirect container
618 stem.3.0 = 5
619 stem.3.1.name = "idFileSystem"
620 stem.3.1.type = unsigned
621 stem.3.2.name = "cSectorUnit"
622 stem.3.2.type = unsigned
623 stem.3.3.name = "cUnit"
624 stem.3.3.type = unsigned
625 stem.3.4.name = "cUnitAvail"
626 stem.3.4.type = unsigned
627 stem.3.5.name = "cbSector"
628 stem.3.5.type = unsigned16
629 stem.4.name = "cbBuf"
630 stem.4.type = unsigned
631 stem.return.type = "unsigned"
632
633 call funcDefine DosQueryFSInfo, "doscalls", "#278", stem
634
635 drop stem.
636 stem. = 0 /* NOVALUE should not happen */
637 stem.0 = 3
638 stem.1.name = "disknum"
639 stem.1.value = c2d( translate( left( source, 1 ) ) ) - c2d( 'A' ) + 1
640 stem.2.name = "infolevel"
641 stem.2.value = 1
642 stem.3.name = "pBuf"
643 stem.3.value = 5
644 stem.4.name = "cbBuf"
645 stem.4.value = 18
646 if DosQueryFSInfo( stem ) = 0 then do
647 cluster = stem.3.2.value * stem.3.5.value
648 say "Total size:" showFileSize( cluster*stem.3.3.value )
649 say "Free size:" showFileSize( cluster*stem.3.4.value )
650 end
651 else
652 say "Error calling DosQueryFSInfo."
653
654 call funcDrop DosQueryFSInfo
655 say ""
656 return 0
657
658 /*****/
659 showFileSize: procedure
660 suffix = "byte"
661 size = arg(1)
662 suffixes = "KB MB GB TB"
663 do i = 1 to words( suffixes )
664 if size < 1024 then
665 leave
666 suffix = word( suffixes, i )
667 size = size / 1024
668 end
669 if size >= 100 then
670 size = format( size, , 0 )
671 else if size >= 10 then
672 size = format( size, , 1 )
673 else
674 size = format( size, , 2 )
675 return size suffix
676
677 /*****************************************************************************/
678 syntax:
679 /*
680 * Not all interpreters are ANSI compatible.
681 */
682 code = .MN
683 if code = '.MN' then
684 code = RC
685 if datatype( SIGL_FUNCDEFINE, "W" ) then
686 SIGL = SIGL_FUNCDEFINE
687 say "Error" code "in line" SIGL || ":" condition('D')
688 say "GCI_RC=" || GCI_RC
689 exit 0
690
691 /*****************************************************************************/
692 funcDrop:
693 /*
694 * Drops one defined function depending on whether is is defined in the
695 * lightweight library or in the interpreter's kernel.
696 */
697 if InternalGCI then
698 call RxFuncDrop arg(1)
699 else
700 call GciFuncDrop arg(1)
701 return
702
703 /*****************************************************************************/
704 funcDefine:
705 /*
706 * Defines a new subroutine as RxFuncDefine does, additionally it undefines
707 * (drops) the subroutine in front and it shows the error messages.
708 * Finally it terminates the process is an error occurs.
709 */
710 _SIGL_FUNCDEFINE = SIGL
711 call funcDrop arg(1)
712 drop GCI_RC
713 SIGL_FUNCDEFINE = _SIGL_FUNCDEFINE
714 call RxFuncDefine arg(1), arg(2), arg(3), arg(4)
715 drop SIGL_FUNCDEFINE _SIGL_FUNCDEFINE
716 if RESULT = 0 then
717 return
718 if IsRegina & InternalGCI then
719 errAdd = ":" RxFuncErrMsg()
720 else do
721 if GCI_RC \= "GCI_RC" then
722 errAdd = ":" GCI_RC
723 else
724 errAdd = ""
725 end
726 say "Error defining '" || arg(1) || "', code" RESULT || errAdd
727
728 exit 1
729