Pristine Ack-5.5
[Ack-5.5.git] / emtest / select.c
1 /* $Id: select.c,v 1.7 1994/06/24 10:06:45 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  *
6  */
7
8 #include        <stdio.h>
9 #include        <assert.h>
10 #include        <signal.h>
11
12 #define LINSIZ  100
13
14 int     sigs[] = {
15         SIGHUP,
16         SIGINT,
17         SIGQUIT,
18         SIGTERM,
19         0
20 };
21
22 char    *prog;
23 char    line[LINSIZ];
24 int     nlocals = 0;
25 int     nhol    = 0;
26 int     nerrors = 0;
27 int     oknum   = 2;
28 int     fflag   = 1;
29 int     low     = 0;
30 int     high    = 999;
31
32 FILE    *file1;
33 FILE    *file2;
34 FILE    *file3;
35 char    name1[] = "/usr/tmp/f1XXXXXX";
36 char    name2[] = "/usr/tmp/f2XXXXXX";
37 char    name3[] = "/usr/tmp/f3XXXXXX";
38
39 char *to3dig();
40
41 stop() {
42         unlink(name1);
43         unlink(name2);
44         unlink(name3);
45         exit(nerrors);
46 }
47
48 main(argc,argv) char **argv; {
49         register *p;
50         register char *s;
51
52         prog = *argv++; --argc;
53         mktemp(name1);
54         mktemp(name2);
55         mktemp(name3);
56         for (p = sigs; *p; p++)
57                 if (signal(*p, stop) == SIG_IGN)
58                         signal(*p, SIG_IGN);
59         while (argc > 0 && argv[0][0] == '-') {
60                 switch (argv[0][1]) {
61                 case 'f':
62                         fflag ^= 1;
63                         break;
64                 case '0': case '1': case '2': case '3': case '4':
65                 case '5': case '6': case '7': case '8': case '9':
66                         high = atoi(&argv[0][1]);
67                         break;
68                 default:
69                         usage();
70                         break;
71                 }
72                 argc--;
73                 argv++;
74         }
75         if (argc > 0 && argv[0][0] >= '0' && argv[0][0] <= '9') {
76                 s = argv[0];
77                 do
78                         low = low*10 + *s++ - '0';
79                 while (*s >= '0' && *s <= '9');
80                 if (*s == 0)
81                         high = low;
82                 else if (*s++ == '-') {
83                         high = atoi(s);
84                         if (high == 0)
85                                 high = 999;
86                 } else
87                         fatal("bad range %s", argv[0]);
88                 argc--;
89                 argv++;
90         }
91         if (argc > 1)
92                 usage();
93         if (argc == 1 && freopen(argv[0], "r", stdin) == NULL)
94                 fatal("cannot open %s", argv[0]);
95         if ((file1 = fopen(name1, "w")) == NULL)
96                 fatal("cannot create %s", name1);
97         if ((file2 = fopen(name2, "w")) == NULL)
98                 fatal("cannot create %s", name2);
99         if ((file3 = fopen(name3, "w")) == NULL)
100                 fatal("cannot create %s", name3);
101         if (getline())
102                 while (select())
103                         ;
104         fclose(file1);
105         fclose(file2);
106         fclose(file3);
107         combine();
108         stop();
109 }
110
111 select() {
112         register FILE *f;
113         int i;
114
115         if (sscanf(line, "TEST %d", &i) != 1)
116                 fatal("bad test identification(%s)", line);
117         if (i < low || i > high) {
118                 while (getline())
119                         if (line[0] == 'T')
120                                 return(1);
121                 return(0);
122         }
123         fprintf(file2, "; %s\n", line);
124         if (fflag) {
125                 char *s = to3dig(i);
126                 fprintf(file1, ".%s\n", s);
127                 fprintf(file1, " con \"tst%s\"\n", s);
128                 fprintf(file2, " fil .%s\n", s);
129         }
130         f = file1;
131         while (getline()) {
132                 switch (line[0]) {
133                 case 'T':
134                         return(1);
135                 case 'M':
136                         if (sscanf(line, "MAIN%d", &i) != 1 || i%4 != 0)
137                                 break;
138                         if (i > nlocals)
139                                 nlocals = i;
140                         f = file2;
141                         continue;
142                 case 'P':
143                         if (strcmp(line, "PROC") != 0)
144                                 break;
145                         f = file3;
146                         continue;
147                 case 'H':
148                         if (f != file1 ||
149                             sscanf(line, "HOL%d", &i) != 1 ||
150                             i%4 != 0)
151                                 break;
152                         if (i > nhol)
153                                 nhol = i;
154                         continue;
155                 case 'O':
156                         if (strcmp(line, "OK") != 0)
157                                 break;
158                         fprintf(f, " lin %d\n nop\n", oknum++);
159                         continue;
160                 case 'E':
161                         if (f != file3 || strcmp(line, "ERRLAB") != 0)
162                                 break;
163                         fprintf(f, "1\n lin 1\n nop\n loc 1\n loc 1\n mon\n");
164                         continue;
165                 default:
166                         putline(f);
167                         continue;
168                 }
169                 fatal("bad line (%s)", line);
170         }
171         return(0);
172 }
173
174 combine() {
175
176         printf("#define WS EM_WSIZE\n");
177         printf("#define PS EM_PSIZE\n");
178         printf("#include \"test.h\"\n");
179         printf(" mes 2,WS,PS\n");
180         printf(" mes 1\n");
181         printf(" mes 4,300\n");
182         if (nhol)
183                 printf(" hol %d,0,0\n", nhol);
184         copy(name1);
185         printf(" exp $_m_a_i_n\n");
186         printf(" pro $_m_a_i_n,%d\n", nlocals);
187         printf(" loc 123\n");
188         printf(" loc -98\n");
189         copy(name2);
190         printf(" loc -98\n");
191         printf(" bne *1\n");
192         printf(" loc 123\n");
193         printf(" bne *1\n");
194         printf(" lin 0\n");
195         printf(" nop\n");
196         printf(" loc 0\n");
197         printf(" ret WS\n");
198         printf("1\n");
199         printf(" lin 1\n");
200         printf(" nop\n");
201         printf(" loc 1\n");
202         printf(" ret WS\n");
203         printf(" end\n");
204         copy(name3);
205 }
206
207 copy(s) char *s; {
208         if (freopen(s, "r", stdin) == NULL)
209                 fatal("cannot reopen %s", s);
210         while (getline())
211                 putline(stdout);
212 }
213
214 getline() {
215         register len;
216
217         if (fgets(line, LINSIZ, stdin) == NULL)
218                 return(0);
219         len = strlen(line);
220         if (line[len-1] != '\n')
221                 fatal("line too long(%s)", line);
222         line[len-1] = 0;
223         return(1);
224 }
225
226 putline(f) FILE *f; {
227         fprintf(f, "%s\n", line);
228 }
229
230 fatal(s, a1, a2, a3, a4) char *s; {
231         fprintf(stderr, "%s: ", prog);
232         fprintf(stderr, s, a1, a2, a3, a4);
233         fprintf(stderr, " (fatal)\n");
234         nerrors++;
235         stop();
236 }
237
238 usage() {
239         fprintf(stderr, "usage: %s -f [[low]-[high]] [testcollection]\n", prog);
240         nerrors++;
241         stop();
242 }
243
244 char *
245 to3dig(i)
246         register int i;
247 {
248         static char buf[4];
249         register char *s = buf;
250
251         *s++ = (i % 1000) / 100 + '0';
252         *s++ = (i % 100) / 10 + '0';
253         *s++ = (i % 10) + '0';
254         *s = '\0';
255         return buf;
256 }