shim.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /* Standard streams as file handles. */
  183. long m2stdout(void) { return (long)stdout; }
  184. long m2stderr(void) { return (long)stderr; }
  185. long m2stdin(void) { return (long)stdin; }
  186. /* Command-line arguments, read from /proc/self/cmdline (Linux). */
  187. static char *m2_args[1024];
  188. static long m2_nargs = -1;
  189. static void m2_argv_init(void)
  190. {
  191. static char cmd[65536];
  192. FILE *f;
  193. long i, n;
  194. int c;
  195. if (m2_nargs >= 0) return;
  196. m2_nargs = 0;
  197. f = fopen("/proc/self/cmdline", "rb");
  198. if (f == NULL) return;
  199. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  200. fclose(f);
  201. if (n <= 0) return;
  202. cmd[n] = 0;
  203. i = 0;
  204. while (i < n && m2_nargs < 1024) {
  205. while (i < n && cmd[i] == 0) i++; /* skip separators */
  206. if (i >= n) break;
  207. m2_args[m2_nargs++] = &cmd[i];
  208. while (i < n && cmd[i] != 0) i++; /* to end of arg */
  209. }
  210. }
  211. long m2argc(void)
  212. {
  213. m2_argv_init();
  214. return m2_nargs;
  215. }
  216. long m2arg(long i, long *desc, long cap)
  217. {
  218. const char *a;
  219. long n;
  220. m2_argv_init();
  221. if (i < 0 || i >= m2_nargs) { ((char *)(desc + 1))[0] = 0; return 0; }
  222. a = m2_args[i];
  223. n = (long)strlen(a);
  224. if (cap > 0 && n >= cap) n = cap - 1;
  225. if (n < 0) n = 0;
  226. memcpy((char *)(desc + 1), a, (size_t)n);
  227. if (n < cap) ((char *)(desc + 1))[n] = 0; /* NUL terminator */
  228. /* desc[0] (capacity) is left intact: length is derived by scanning */
  229. return 1;
  230. }
  231. /* Set a string descriptor's logical length (header). */
  232. long m2setlen(long *desc, long n)
  233. {
  234. desc[0] = n;
  235. return n;
  236. }
  237. /* Write a single character (byte) to fd 1. */
  238. long m2writechar(long c)
  239. {
  240. char ch = (char)c;
  241. write(1, &ch, 1);
  242. return 0;
  243. }
  244. /* Write a signed decimal integer to fd 1. */
  245. long m2writeint(long v)
  246. {
  247. char buf[32];
  248. int i = 0;
  249. int neg = 0;
  250. unsigned long u;
  251. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  252. else u = (unsigned long)v;
  253. do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
  254. if (neg) buf[i++] = '-';
  255. while (i > 0)
  256. write(1, &buf[--i], 1);
  257. return 0;
  258. }
  259. /* Terminate the process. */
  260. long m2exit(long code)
  261. {
  262. exit((int)code);
  263. return 0;
  264. }
  265. /* String content equality: 1 when the NUL-terminated contents of the
  266. two descriptors match. */
  267. long m2streq(long *a, long *b)
  268. {
  269. return (strcmp((char *)(a + 1), (char *)(b + 1)) == 0) ? 1 : 0;
  270. }
  271. /* Number of characters before the NUL. */
  272. long m2strlen(long *s)
  273. {
  274. return (long)strlen((char *)(s + 1));
  275. }
  276. /* Copy a UString descriptor (count + 4-byte codepoints) into the
  277. UCHAR array at dst (capacity dst[0]): min(count, capacity)
  278. codepoints, followed by a 0 codepoint terminator. Returns the
  279. number of codepoints copied. */
  280. long m2uassign(long *dst, long *src)
  281. {
  282. /* Both descriptors are { l count, w codepoints... }: the header is
  283. 64-bit, each codepoint is 32-bit. */
  284. int *d = (int *)(dst + 1);
  285. int *s = (int *)(src + 1);
  286. long cap = dst[0];
  287. long n = src[0];
  288. long i;
  289. if (cap < 0) cap = 0;
  290. if (n > cap) n = cap;
  291. for (i = 0; i < n; i++) d[i] = s[i];
  292. d[n] = 0;
  293. return n;
  294. }
  295. /* Copy the content of src into the CHAR array at dst (capacity
  296. dst[0]); truncated to fit and NUL-terminated. A CHAR array has
  297. `cap` content slots plus a reserved terminator slot at index `cap`,
  298. so up to `cap` characters fit. Returns the length copied. */
  299. long m2strassign(long *dst, long *src)
  300. {
  301. char *d = (char *)(dst + 1);
  302. const char *s = (const char *)(src + 1);
  303. long cap = dst[0];
  304. size_t n;
  305. if (cap < 0) return 0;
  306. n = strlen(s);
  307. if (n > (size_t)cap) n = (size_t)cap;
  308. memmove(d, s, n);
  309. d[n] = 0;
  310. return (long)n;
  311. }
  312. /* Concatenate two string descriptors into a static buffer; returns the
  313. result descriptor address. The value is valid until the next
  314. m2strcat call (so copy it before concatenating again). */
  315. long m2strcat(long *a, long *b)
  316. {
  317. static struct { long count; char data[8192]; } buf;
  318. const char *sa = (const char *)(a + 1);
  319. const char *sb = (const char *)(b + 1);
  320. size_t na = strlen(sa), nb = strlen(sb);
  321. if (na > sizeof buf.data - 1) na = sizeof buf.data - 1;
  322. if (nb > sizeof buf.data - 1 - na)
  323. nb = sizeof buf.data - 1 - na;
  324. memmove(buf.data, sa, na);
  325. memmove(buf.data + na, sb, nb);
  326. buf.data[na + nb] = 0;
  327. buf.count = (long)(na + nb);
  328. return (long)&buf;
  329. }
  330. /* ---------------- reals (Conversions / RealIO) ---------------- */
  331. /* Format a REAL into a descriptor string (6 significant digits,
  332. scientific). desc[0] is the capacity; on return it holds the
  333. length. Returns the number of characters written. */
  334. long m2realstr(double x, long *desc)
  335. {
  336. char buf[64];
  337. char *dst = (char *)(desc + 1);
  338. long cap = desc[0];
  339. int n = snprintf(buf, sizeof buf, "%.6E", x);
  340. if (n < 0) n = 0;
  341. if (cap > 0 && n >= (int)cap) n = (int)cap - 1;
  342. if (n > 0) memcpy(dst, buf, (size_t)n);
  343. if (cap > 0) { dst[n] = 0; desc[0] = n; }
  344. return (long)n;
  345. }
  346. /* Parse a REAL from a descriptor string; *ok := 1 on success. */
  347. double m2strreal(long *desc, long *ok)
  348. {
  349. char buf[4096];
  350. char *end;
  351. double v;
  352. m2cstr(desc, buf, sizeof buf);
  353. v = strtod(buf, &end);
  354. while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++;
  355. *ok = (end != buf && *end == 0) ? 1 : 0;
  356. return v;
  357. }
  358. /* Write a REAL right-justified in a field of width `wid` (0 => one
  359. leading space), matching the integer convention. */
  360. long m2writerealf(double x, long wid)
  361. {
  362. char buf[128];
  363. int n;
  364. if (wid == 0)
  365. n = snprintf(buf, sizeof buf, "%.6E", x);
  366. else
  367. n = snprintf(buf, sizeof buf, "%*.*E", (int)wid, 6, x);
  368. if (n > 0) write(1, buf, (size_t)n);
  369. return 0;
  370. }
  371. long m2writereal(double x)
  372. {
  373. return m2writerealf(x, 0);
  374. }
  375. /* Read a REAL from stdin. */
  376. double m2readreal(void)
  377. {
  378. double v = 0.0;
  379. if (scanf("%lf", &v) != 1) v = 0.0;
  380. return v;
  381. }