Rune - Content-locking work 1/2
[rune.git] / tests / varargs.d
1 #!/usr/local/bin/rune -x
2 #
3
4 limport "sys";
5 import "stdio" as self;
6
7 typedef const char *string_p;
8
9 int
10 main(int ac, char **av)
11 {
12         volatile int a = 4;
13
14         show("how are you doing", 1,
15              "oclock", 2UB, 3, 4, 5,
16              NULL, (const char *).NULL, a, 0);
17         show("should be:        1 oclock (unknown-int) 3 4 5 (vnull) (null) 4 0");
18
19         show("");
20         stdout->show("how are you doing ", 1,
21              "oclock ", 2UB, 3, 4, 5,
22              (const void *).NULL, (const char *).NULL, " ", a, 0);
23         show("should be:        1 oclock 2 3 4 5 ? (null) 4 0");
24
25         #show2("should be: 1 2 3", 1, 2, 3);
26 }
27
28 void
29 show(...)
30 {
31         Fs fd;
32         int i;
33
34         fd.setfd(1);
35
36         for (i = 0; i < self.__varcount; ++i) {
37                 if (i)
38                         fd.write(" ", 1);
39
40                 switch(typeof(self.__vardata[i])) {
41                 case typeof(int):
42                         char buf[32];
43                         int v = self.__vardata[i];
44                         int j = arysize(buf);
45
46                         if (v == 0) {
47                                 buf[--j] = '0';
48                         } else {
49                                 uint vv;
50                                 int negative = 0;
51
52                                 if (v < 0) {
53                                         v = -v;
54                                         negative = 1;
55                                 }
56                                 vv = (uint)v;
57                                 while (vv != 0U) {
58                                         buf[--j] = (char)(vv % 10U) + '0';
59                                         vv = vv / 10U;
60                                 }
61                                 if (negative != 0)
62                                         buf[--j] = '-';
63                         }
64                         fd.write(&buf[j], arysize(buf) - (size_t)j);
65                         break;
66                 case typeof(char *):
67                         const char *str = self.__vardata[i];
68                         if (str == NULL)
69                                 fd.write("(null)", 6);
70                         else
71                                 fd.write(str, Str.strlen(str));
72                         break;
73                 case typeof(void *):
74                         const void *data = self.__vardata[i];
75                         if (data == NULL)
76                                 fd.write("(vnull)", 7);
77                         else
78                                 fd.write("(not-null)", 10);
79                         break;
80                 case typeof(SInteger):
81                         /*
82                          * Sweep
83                          */
84                         fd.write("(unknown-int)", 13);
85                         break;
86                 default:
87                         char c = '?';
88                         fd.write(&c, 1);
89                         break;
90                 }
91         }
92         fd.write("\n", 1);
93 }
94
95 #void
96 #show2(...)
97 #{
98 #       stdout->format("show2: varcount %d\n", self.__varcount);
99 #       show3(self);
100 #}
101 #
102 #void
103 #show3(...)
104 #{
105 #       stdout->format("show3: varcount %d\n", self.__varcount);
106 #}