aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gm2/pimlib/run/pass/testgetopt.mod
blob: 3e322da1c2b0f03b7846c7526afb61b27bfc4a20 (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
MODULE testgetopt ;

FROM libc IMPORT printf, exit ;
FROM GetOpt IMPORT InitLongOptions, KillLongOptions, AddLongOption,
                   GetOptLong, PtrToInteger, LongOptions ;
FROM DynamicStrings IMPORT String, InitString, string ;
IMPORT UnixArgs ;
FROM Storage IMPORT ALLOCATE ;
FROM SYSTEM IMPORT ADR ;


(*
   Assert -
*)

PROCEDURE Assert (condition: BOOLEAN) ;
BEGIN
   IF NOT condition
   THEN
      printf ("assert failed, condition is false\n") ;
      exit (1)
   END
END Assert ;



(*
   test -
*)

PROCEDURE test ;
VAR
   result   : INTEGER ;
   optstring: String ;
   i, val   : INTEGER ;
   ch       : CHAR ;
BEGIN
   longopts := AddLongOption (longopts, 0, InitString ('help'), 0, result, 0) ;
   longopts := AddLongOption (longopts, 1, InitString ('dir'), 1, result, 0) ;
   longopts := AddLongOption (longopts, 2, NIL, 0, result, 0) ;
   optstring := InitString ('hd:') ;
   i := 1 ;
   REPEAT
      val := GetOptLong (UnixArgs.GetArgC (), UnixArgs.GetArgV (),
                         optstring, longopts, i) ;
      IF val = 0
      THEN
         printf ("long option detected, result = %d, val = %d, index i = %d, optstring = %s\n",
                 result, val, i, string (optstring))
      ELSIF val > 0
      THEN
         ch := VAL (CHAR, val) ;
         CASE ch OF

         'h': printf ("short option 'h' seen\n")

         ELSE
            printf ("unknown short option '%c' seen\n", ch)
         END
      ELSE
         printf ("unknown long option\n")
      END ;
      INC (i)
   UNTIL val <= 0
END test ;


VAR
   longopts: LongOptions ;
BEGIN
   longopts := InitLongOptions () ;
   test ;
   longopts := KillLongOptions (longopts)
END testgetopt.