select.c 4.6 KB

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