shim.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /* Bulk read: up to max bytes into the descriptor's data area; the
  135. descriptor's header is set to the number actually read. */
  136. long m2fread(long h, long *desc, long max)
  137. {
  138. long n;
  139. if (max < 0) max = 0;
  140. if (h == 0) { desc[0] = 0; return 0; }
  141. n = (long)fread((char *)(desc + 1), 1, (size_t)max, (FILE *)h);
  142. desc[0] = n;
  143. return n;
  144. }
  145. /* Write a signed integer right-justified in a field of width `wid`.
  146. wid = 0 means exactly one leading space (FileIO convention). */
  147. long m2fwriteintw(long h, long v, long wid)
  148. {
  149. char buf[64];
  150. int n;
  151. if (h == 0) return 0;
  152. if (wid == 0)
  153. n = snprintf(buf, sizeof buf, " %ld", v);
  154. else
  155. n = snprintf(buf, sizeof buf, "%*ld", (int)wid, v);
  156. if (n > 0) fwrite(buf, 1, (size_t)n, (FILE *)h);
  157. return 0;
  158. }
  159. /* Standard streams as file handles. */
  160. long m2stdout(void) { return (long)stdout; }
  161. long m2stderr(void) { return (long)stderr; }
  162. /* Command-line arguments, read from /proc/self/cmdline (Linux). */
  163. static char *m2_args[1024];
  164. static long m2_nargs = -1;
  165. static void m2_argv_init(void)
  166. {
  167. static char cmd[65536];
  168. FILE *f;
  169. long i, n;
  170. int c;
  171. if (m2_nargs >= 0) return;
  172. m2_nargs = 0;
  173. f = fopen("/proc/self/cmdline", "rb");
  174. if (f == NULL) return;
  175. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  176. fclose(f);
  177. if (n <= 0) return;
  178. cmd[n] = 0;
  179. i = 0;
  180. while (i < n && m2_nargs < 1024) {
  181. while (i < n && cmd[i] == 0) i++; /* skip separators */
  182. if (i >= n) break;
  183. m2_args[m2_nargs++] = &cmd[i];
  184. while (i < n && cmd[i] != 0) i++; /* to end of arg */
  185. }
  186. }
  187. long m2argc(void)
  188. {
  189. m2_argv_init();
  190. return m2_nargs;
  191. }
  192. long m2arg(long i, long *desc, long cap)
  193. {
  194. const char *a;
  195. long n;
  196. m2_argv_init();
  197. if (i < 0 || i >= m2_nargs) { ((char *)(desc + 1))[0] = 0; return 0; }
  198. a = m2_args[i];
  199. n = (long)strlen(a);
  200. if (cap > 0 && n >= cap) n = cap - 1;
  201. if (n < 0) n = 0;
  202. memcpy((char *)(desc + 1), a, (size_t)n);
  203. if (n < cap) ((char *)(desc + 1))[n] = 0; /* NUL terminator */
  204. /* desc[0] (capacity) is left intact: length is derived by scanning */
  205. return 1;
  206. }
  207. /* Set a string descriptor's logical length (header). */
  208. long m2setlen(long *desc, long n)
  209. {
  210. desc[0] = n;
  211. return n;
  212. }
  213. /* Write a single character (byte) to fd 1. */
  214. long m2writechar(long c)
  215. {
  216. char ch = (char)c;
  217. write(1, &ch, 1);
  218. return 0;
  219. }
  220. /* Write a signed decimal integer to fd 1. */
  221. long m2writeint(long v)
  222. {
  223. char buf[32];
  224. int i = 0;
  225. int neg = 0;
  226. unsigned long u;
  227. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  228. else u = (unsigned long)v;
  229. do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
  230. if (neg) buf[i++] = '-';
  231. while (i > 0)
  232. write(1, &buf[--i], 1);
  233. return 0;
  234. }
  235. /* Terminate the process. */
  236. long m2exit(long code)
  237. {
  238. exit((int)code);
  239. return 0;
  240. }
  241. /* String content equality: 1 when the NUL-terminated contents of the
  242. two descriptors match. */
  243. long m2streq(long *a, long *b)
  244. {
  245. return (strcmp((char *)(a + 1), (char *)(b + 1)) == 0) ? 1 : 0;
  246. }
  247. /* ---------------- reals (Conversions / RealIO) ---------------- */
  248. /* Format a REAL into a descriptor string (6 significant digits,
  249. scientific). desc[0] is the capacity; on return it holds the
  250. length. Returns the number of characters written. */
  251. long m2realstr(double x, long *desc)
  252. {
  253. char buf[64];
  254. char *dst = (char *)(desc + 1);
  255. long cap = desc[0];
  256. int n = snprintf(buf, sizeof buf, "%.6E", x);
  257. if (n < 0) n = 0;
  258. if (cap > 0 && n >= (int)cap) n = (int)cap - 1;
  259. if (n > 0) memcpy(dst, buf, (size_t)n);
  260. if (cap > 0) { dst[n] = 0; desc[0] = n; }
  261. return (long)n;
  262. }
  263. /* Parse a REAL from a descriptor string; *ok := 1 on success. */
  264. double m2strreal(long *desc, long *ok)
  265. {
  266. char buf[4096];
  267. char *end;
  268. double v;
  269. m2cstr(desc, buf, sizeof buf);
  270. v = strtod(buf, &end);
  271. while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++;
  272. *ok = (end != buf && *end == 0) ? 1 : 0;
  273. return v;
  274. }
  275. /* Write a REAL right-justified in a field of width `wid` (0 => one
  276. leading space), matching the integer convention. */
  277. long m2writerealf(double x, long wid)
  278. {
  279. char buf[128];
  280. int n;
  281. if (wid == 0)
  282. n = snprintf(buf, sizeof buf, "%.6E", x);
  283. else
  284. n = snprintf(buf, sizeof buf, "%*.*E", (int)wid, 6, x);
  285. if (n > 0) write(1, buf, (size_t)n);
  286. return 0;
  287. }
  288. long m2writereal(double x)
  289. {
  290. return m2writerealf(x, 0);
  291. }
  292. /* Read a REAL from stdin. */
  293. double m2readreal(void)
  294. {
  295. double v = 0.0;
  296. if (scanf("%lf", &v) != 1) v = 0.0;
  297. return v;
  298. }