shim.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* m2compiler-V3 runtime shim (C).
  2. *
  3. * Bridges Modula-2 external procedures to the host. Memory layout
  4. * matches the compiler's: a STRING (ARRAY OF CHAR) value is a
  5. * descriptor [0] = LONGCARD element count, then the bytes; the
  6. * compiler passes the descriptor's address as an `l`.
  7. *
  8. * Linked with every V3 program image.
  9. */
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. /* Write the contents of a string descriptor to fd 1. Returns the
  15. number of bytes written. */
  16. long m2write(long *desc)
  17. {
  18. /* desc[0] is the capacity; the string is NUL-terminated. */
  19. char *s = (char *)(desc + 1);
  20. size_t n = strnlen(s, (size_t)desc[0] + 1);
  21. if (n > 0)
  22. write(1, s, n);
  23. return (long)n;
  24. }
  25. /* Write a newline to fd 1. */
  26. long m2writeln(void)
  27. {
  28. write(1, "\n", 1);
  29. return 0;
  30. }
  31. /* Read one character from stdin (0 at EOF). */
  32. long m2readchar(void)
  33. {
  34. int c = getchar();
  35. return (c == EOF) ? 0 : c;
  36. }
  37. /* Read a signed decimal integer from stdin, skipping leading
  38. whitespace. The first non-digit after the number is consumed. */
  39. long m2readint(void)
  40. {
  41. long v = 0;
  42. int c;
  43. int neg = 0;
  44. do { c = getchar(); } while (c == ' ' || c == '\t' ||
  45. c == '\n' || c == '\r');
  46. if (c == '-') { neg = 1; c = getchar(); }
  47. while (c >= '0' && c <= '9') {
  48. v = v * 10 + (c - '0');
  49. c = getchar();
  50. }
  51. return neg ? -v : v;
  52. }
  53. /* ---------------- sequential files (stdio) ---------------- */
  54. /* Copy a descriptor string into a NUL-terminated buffer. */
  55. static void m2cstr(long *desc, char *buf, size_t cap)
  56. {
  57. char *s = (char *)(desc + 1);
  58. size_t n = strnlen(s, (size_t)desc[0] + 1);
  59. if (n >= cap) n = cap - 1;
  60. if (n > 0) memcpy(buf, s, n);
  61. buf[n] = 0;
  62. }
  63. long m2fopenread(long *desc)
  64. {
  65. char buf[4096];
  66. m2cstr(desc, buf, sizeof buf);
  67. return (long)fopen(buf, "r");
  68. }
  69. long m2fopenwrite(long *desc)
  70. {
  71. char buf[4096];
  72. m2cstr(desc, buf, sizeof buf);
  73. return (long)fopen(buf, "w");
  74. }
  75. long m2fclose(long h)
  76. {
  77. if (h != 0) fclose((FILE *)h);
  78. return 0;
  79. }
  80. /* A handle of 0 means the open failed (Modula-2 NIL). Every file
  81. operation degrades to a safe no-op instead of dereferencing NULL. */
  82. long m2fgetc(long h)
  83. {
  84. int c;
  85. if (h == 0) return 0;
  86. c = fgetc((FILE *)h);
  87. return (c == EOF) ? 0 : c;
  88. }
  89. long m2fputc(long h, long c)
  90. {
  91. if (h != 0) fputc((int)c, (FILE *)h);
  92. return 0;
  93. }
  94. long m2fputs(long h, long *desc)
  95. {
  96. char *s = (char *)(desc + 1);
  97. size_t n = strnlen(s, (size_t)desc[0] + 1);
  98. if (h == 0) return 0;
  99. if (n > 0) fwrite(s, 1, n, (FILE *)h);
  100. return (long)n;
  101. }
  102. long m2freadint(long h)
  103. {
  104. long v = 0;
  105. int c, neg = 0;
  106. if (h == 0) return 0;
  107. do { c = fgetc((FILE *)h); } while (c == ' ' || c == '\t' ||
  108. c == '\n' || c == '\r');
  109. if (c == '-') { neg = 1; c = fgetc((FILE *)h); }
  110. while (c >= '0' && c <= '9') {
  111. v = v * 10 + (c - '0');
  112. c = fgetc((FILE *)h);
  113. }
  114. return neg ? -v : v;
  115. }
  116. long m2fwriteint(long h, long v)
  117. {
  118. char buf[32];
  119. int i = 0, neg = 0;
  120. unsigned long u;
  121. if (h == 0) return 0;
  122. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  123. else u = (unsigned long)v;
  124. do { buf[i++] = (char)('0' + u % 10); u /= 10; } while (u != 0);
  125. if (neg) buf[i++] = '-';
  126. while (i > 0) fputc(buf[--i], (FILE *)h);
  127. return 0;
  128. }
  129. long m2fwriteln(long h)
  130. {
  131. if (h != 0) fputc('\n', (FILE *)h);
  132. return 0;
  133. }
  134. /* Read one line (up to and including '\n', or EOF) into the
  135. descriptor's data area, dropping the newline; NUL-terminated.
  136. The descriptor's capacity header is left intact (so a caller may
  137. keep using HIGH), and the number of bytes stored is returned.
  138. `max` is declared `int`: Modula-2 CARDINAL arguments arrive in a
  139. 32-bit register (QBE `w`), so only the low 32 bits are defined. */
  140. long m2freadline(long h, long *desc, int max)
  141. {
  142. char *dst = (char *)(desc + 1);
  143. long n = 0;
  144. int c;
  145. if (max < 0) max = 0;
  146. if (h == 0) { if (max > 0) dst[0] = 0; return 0; }
  147. while (n < max) {
  148. c = fgetc((FILE *)h);
  149. if (c == EOF) break;
  150. if (c == '\n') break;
  151. if (c == '\r') continue;
  152. dst[n++] = (char)c;
  153. }
  154. dst[n] = 0;
  155. return n;
  156. }
  157. /* Bulk read: up to max bytes into the descriptor's data area; the
  158. descriptor's header is set to the number actually read. */
  159. long m2fread(long h, long *desc, long max)
  160. {
  161. long n;
  162. if (max < 0) max = 0;
  163. if (h == 0) { desc[0] = 0; return 0; }
  164. n = (long)fread((char *)(desc + 1), 1, (size_t)max, (FILE *)h);
  165. desc[0] = n;
  166. return n;
  167. }
  168. /* Write a signed integer right-justified in a field of width `wid`.
  169. wid = 0 means exactly one leading space (FileIO convention). */
  170. long m2fwriteintw(long h, long v, long wid)
  171. {
  172. char buf[64];
  173. int n;
  174. if (h == 0) return 0;
  175. if (wid == 0)
  176. n = snprintf(buf, sizeof buf, " %ld", v);
  177. else
  178. n = snprintf(buf, sizeof buf, "%*ld", (int)wid, v);
  179. if (n > 0) fwrite(buf, 1, (size_t)n, (FILE *)h);
  180. return 0;
  181. }
  182. /* Byte offset of the current position in h (-1 on error). */
  183. long m2ftell(long h)
  184. {
  185. if (h == 0) return -1;
  186. return (long)ftell((FILE *)h);
  187. }
  188. /* Seek h to absolute byte offset pos; 1 on success, 0 on failure.
  189. Rewinding also clears the stdio error/EOF state. */
  190. long m2fseek(long h, long pos)
  191. {
  192. if (h == 0) return 0;
  193. if (fseek((FILE *)h, (long)pos, SEEK_SET) != 0) return 0;
  194. clearerr((FILE *)h);
  195. return 1;
  196. }
  197. /* Standard streams as file handles. */
  198. long m2stdout(void) { return (long)stdout; }
  199. long m2stderr(void) { return (long)stderr; }
  200. long m2stdin(void) { return (long)stdin; }
  201. /* Command-line arguments, read from /proc/self/cmdline (Linux). */
  202. static char *m2_args[1024];
  203. static long m2_nargs = -1;
  204. static void m2_argv_init(void)
  205. {
  206. static char cmd[65536];
  207. FILE *f;
  208. long i, n;
  209. int c;
  210. if (m2_nargs >= 0) return;
  211. m2_nargs = 0;
  212. f = fopen("/proc/self/cmdline", "rb");
  213. if (f == NULL) return;
  214. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  215. fclose(f);
  216. if (n <= 0) return;
  217. cmd[n] = 0;
  218. i = 0;
  219. while (i < n && m2_nargs < 1024) {
  220. while (i < n && cmd[i] == 0) i++; /* skip separators */
  221. if (i >= n) break;
  222. m2_args[m2_nargs++] = &cmd[i];
  223. while (i < n && cmd[i] != 0) i++; /* to end of arg */
  224. }
  225. }
  226. long m2argc(void)
  227. {
  228. m2_argv_init();
  229. return m2_nargs;
  230. }
  231. long m2arg(long i, long *desc, long cap)
  232. {
  233. const char *a;
  234. long n;
  235. m2_argv_init();
  236. if (i < 0 || i >= m2_nargs) { ((char *)(desc + 1))[0] = 0; return 0; }
  237. a = m2_args[i];
  238. n = (long)strlen(a);
  239. if (cap > 0 && n >= cap) n = cap - 1;
  240. if (n < 0) n = 0;
  241. memcpy((char *)(desc + 1), a, (size_t)n);
  242. if (n < cap) ((char *)(desc + 1))[n] = 0; /* NUL terminator */
  243. /* desc[0] (capacity) is left intact: length is derived by scanning */
  244. return 1;
  245. }
  246. /* Set a string descriptor's logical length (header). */
  247. long m2setlen(long *desc, long n)
  248. {
  249. desc[0] = n;
  250. return n;
  251. }
  252. /* Write a single character (byte) to fd 1. */
  253. long m2writechar(long c)
  254. {
  255. char ch = (char)c;
  256. write(1, &ch, 1);
  257. return 0;
  258. }
  259. /* Write a signed decimal integer to fd 1. */
  260. long m2writeint(long v)
  261. {
  262. char buf[32];
  263. int i = 0;
  264. int neg = 0;
  265. unsigned long u;
  266. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  267. else u = (unsigned long)v;
  268. do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
  269. if (neg) buf[i++] = '-';
  270. while (i > 0)
  271. write(1, &buf[--i], 1);
  272. return 0;
  273. }
  274. /* Terminate the process. */
  275. long m2exit(long code)
  276. {
  277. exit((int)code);
  278. return 0;
  279. }
  280. /* String content equality: 1 when the NUL-terminated contents of the
  281. two descriptors match. */
  282. long m2streq(long *a, long *b)
  283. {
  284. return (strcmp((char *)(a + 1), (char *)(b + 1)) == 0) ? 1 : 0;
  285. }
  286. /* Number of characters before the NUL. */
  287. long m2strlen(long *s)
  288. {
  289. return (long)strlen((char *)(s + 1));
  290. }
  291. /* UString length: the codepoint count is the descriptor's header. */
  292. long m2ustrlen(long *s)
  293. {
  294. return s[0];
  295. }
  296. /* Build a 1-codepoint UString descriptor from a UCHAR value; returns
  297. its address. A small rotating pool lets an expression supply both
  298. operands to m2ustrcat before it reads them. */
  299. long m2ustrfrom(long cp)
  300. {
  301. static struct { long count; int cp; } pool[16];
  302. static int next = 0;
  303. long *d = (long *)&pool[next];
  304. pool[next].count = 1;
  305. pool[next].cp = (int)cp;
  306. next = (next + 1) & 15;
  307. return (long)d;
  308. }
  309. /* Concatenate two UString descriptors into a static buffer; returns
  310. the result descriptor address. The value is valid until the next
  311. m2ustrcat call. */
  312. long m2ustrcat(long *a, long *b)
  313. {
  314. static struct { long count; int cp[4096]; } buf;
  315. int *pa = (int *)(a + 1);
  316. int *pb = (int *)(b + 1);
  317. long na = a[0], nb = b[0], i;
  318. if (na < 0) na = 0;
  319. if (nb < 0) nb = 0;
  320. if (na > 4096) na = 4096;
  321. if (nb > 4096 - na) nb = 4096 - na;
  322. for (i = 0; i < na; i++) buf.cp[i] = pa[i];
  323. for (i = 0; i < nb; i++) buf.cp[na + i] = pb[i];
  324. buf.count = na + nb;
  325. return (long)&buf;
  326. }
  327. /* Copy a UString descriptor (count + 4-byte codepoints) into the
  328. UCHAR array at dst (capacity dst[0]): min(count, capacity)
  329. codepoints, followed by a 0 codepoint terminator. Returns the
  330. number of codepoints copied. */
  331. long m2uassign(long *dst, long *src)
  332. {
  333. /* Both descriptors are { l count, w codepoints... }: the header is
  334. 64-bit, each codepoint is 32-bit. */
  335. int *d = (int *)(dst + 1);
  336. int *s = (int *)(src + 1);
  337. long cap = dst[0];
  338. long n = src[0];
  339. long i;
  340. if (cap < 0) cap = 0;
  341. if (n > cap) n = cap;
  342. for (i = 0; i < n; i++) d[i] = s[i];
  343. d[n] = 0;
  344. return n;
  345. }
  346. /* Copy the content of src into the CHAR array at dst (capacity
  347. dst[0]); truncated to fit and NUL-terminated. A CHAR array has
  348. `cap` content slots plus a reserved terminator slot at index `cap`,
  349. so up to `cap` characters fit. Returns the length copied. */
  350. long m2strassign(long *dst, long *src)
  351. {
  352. char *d = (char *)(dst + 1);
  353. const char *s = (const char *)(src + 1);
  354. long cap = dst[0];
  355. size_t n;
  356. if (cap < 0) return 0;
  357. n = strlen(s);
  358. if (n > (size_t)cap) n = (size_t)cap;
  359. memmove(d, s, n);
  360. d[n] = 0;
  361. return (long)n;
  362. }
  363. /* Concatenate two string descriptors into a static buffer; returns the
  364. result descriptor address. The value is valid until the next
  365. m2strcat call (so copy it before concatenating again). */
  366. long m2strcat(long *a, long *b)
  367. {
  368. static struct { long count; char data[8192]; } buf;
  369. const char *sa = (const char *)(a + 1);
  370. const char *sb = (const char *)(b + 1);
  371. size_t na = strlen(sa), nb = strlen(sb);
  372. if (na > sizeof buf.data - 1) na = sizeof buf.data - 1;
  373. if (nb > sizeof buf.data - 1 - na)
  374. nb = sizeof buf.data - 1 - na;
  375. memmove(buf.data, sa, na);
  376. memmove(buf.data + na, sb, nb);
  377. buf.data[na + nb] = 0;
  378. buf.count = (long)(na + nb);
  379. return (long)&buf;
  380. }
  381. /* ---------------- reals (Conversions / RealIO) ---------------- */
  382. /* Format a REAL into a descriptor string (6 significant digits,
  383. scientific). desc[0] is the capacity; on return it holds the
  384. length. Returns the number of characters written. */
  385. long m2realstr(double x, long *desc)
  386. {
  387. char buf[64];
  388. char *dst = (char *)(desc + 1);
  389. long cap = desc[0];
  390. int n = snprintf(buf, sizeof buf, "%.6E", x);
  391. if (n < 0) n = 0;
  392. if (cap > 0 && n >= (int)cap) n = (int)cap - 1;
  393. if (n > 0) memcpy(dst, buf, (size_t)n);
  394. if (cap > 0) { dst[n] = 0; desc[0] = n; }
  395. return (long)n;
  396. }
  397. /* Parse a REAL from a descriptor string; *ok := 1 on success. */
  398. double m2strreal(long *desc, long *ok)
  399. {
  400. char buf[4096];
  401. char *end;
  402. double v;
  403. m2cstr(desc, buf, sizeof buf);
  404. v = strtod(buf, &end);
  405. while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++;
  406. *ok = (end != buf && *end == 0) ? 1 : 0;
  407. return v;
  408. }
  409. /* Write a REAL right-justified in a field of width `wid` (0 => one
  410. leading space), matching the integer convention. */
  411. long m2writerealf(double x, long wid)
  412. {
  413. char buf[128];
  414. int n;
  415. if (wid == 0)
  416. n = snprintf(buf, sizeof buf, "%.6E", x);
  417. else
  418. n = snprintf(buf, sizeof buf, "%*.*E", (int)wid, 6, x);
  419. if (n > 0) write(1, buf, (size_t)n);
  420. return 0;
  421. }
  422. long m2writereal(double x)
  423. {
  424. return m2writerealf(x, 0);
  425. }
  426. /* Read a REAL from stdin. */
  427. double m2readreal(void)
  428. {
  429. double v = 0.0;
  430. if (scanf("%lf", &v) != 1) v = 0.0;
  431. return v;
  432. }