shim.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. long n = desc[0];
  19. if (n > 0)
  20. write(1, (char *)(desc + 1), (size_t)n);
  21. return n;
  22. }
  23. /* Write a newline to fd 1. */
  24. long m2writeln(void)
  25. {
  26. write(1, "\n", 1);
  27. return 0;
  28. }
  29. /* Read one character from stdin (0 at EOF). */
  30. long m2readchar(void)
  31. {
  32. int c = getchar();
  33. return (c == EOF) ? 0 : c;
  34. }
  35. /* Read a signed decimal integer from stdin, skipping leading
  36. whitespace. The first non-digit after the number is consumed. */
  37. long m2readint(void)
  38. {
  39. long v = 0;
  40. int c;
  41. int neg = 0;
  42. do { c = getchar(); } while (c == ' ' || c == '\t' ||
  43. c == '\n' || c == '\r');
  44. if (c == '-') { neg = 1; c = getchar(); }
  45. while (c >= '0' && c <= '9') {
  46. v = v * 10 + (c - '0');
  47. c = getchar();
  48. }
  49. return neg ? -v : v;
  50. }
  51. /* ---------------- sequential files (stdio) ---------------- */
  52. /* Copy a descriptor string into a NUL-terminated buffer. */
  53. static void m2cstr(long *desc, char *buf, size_t cap)
  54. {
  55. long n = desc[0];
  56. if ((size_t)n >= cap) n = (long)(cap - 1);
  57. if (n > 0) memcpy(buf, (char *)(desc + 1), (size_t)n);
  58. buf[n] = 0;
  59. }
  60. long m2fopenread(long *desc)
  61. {
  62. char buf[4096];
  63. m2cstr(desc, buf, sizeof buf);
  64. return (long)fopen(buf, "r");
  65. }
  66. long m2fopenwrite(long *desc)
  67. {
  68. char buf[4096];
  69. m2cstr(desc, buf, sizeof buf);
  70. return (long)fopen(buf, "w");
  71. }
  72. long m2fclose(long h)
  73. {
  74. if (h != 0) fclose((FILE *)h);
  75. return 0;
  76. }
  77. /* A handle of 0 means the open failed (Modula-2 NIL). Every file
  78. operation degrades to a safe no-op instead of dereferencing NULL. */
  79. long m2fgetc(long h)
  80. {
  81. int c;
  82. if (h == 0) return 0;
  83. c = fgetc((FILE *)h);
  84. return (c == EOF) ? 0 : c;
  85. }
  86. long m2fputc(long h, long c)
  87. {
  88. if (h != 0) fputc((int)c, (FILE *)h);
  89. return 0;
  90. }
  91. long m2fputs(long h, long *desc)
  92. {
  93. long n = desc[0];
  94. if (h == 0) return 0;
  95. if (n > 0) fwrite((char *)(desc + 1), 1, (size_t)n, (FILE *)h);
  96. return n;
  97. }
  98. long m2freadint(long h)
  99. {
  100. long v = 0;
  101. int c, neg = 0;
  102. if (h == 0) return 0;
  103. do { c = fgetc((FILE *)h); } while (c == ' ' || c == '\t' ||
  104. c == '\n' || c == '\r');
  105. if (c == '-') { neg = 1; c = fgetc((FILE *)h); }
  106. while (c >= '0' && c <= '9') {
  107. v = v * 10 + (c - '0');
  108. c = fgetc((FILE *)h);
  109. }
  110. return neg ? -v : v;
  111. }
  112. long m2fwriteint(long h, long v)
  113. {
  114. char buf[32];
  115. int i = 0, neg = 0;
  116. unsigned long u;
  117. if (h == 0) return 0;
  118. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  119. else u = (unsigned long)v;
  120. do { buf[i++] = (char)('0' + u % 10); u /= 10; } while (u != 0);
  121. if (neg) buf[i++] = '-';
  122. while (i > 0) fputc(buf[--i], (FILE *)h);
  123. return 0;
  124. }
  125. long m2fwriteln(long h)
  126. {
  127. if (h != 0) fputc('\n', (FILE *)h);
  128. return 0;
  129. }
  130. /* Bulk read: up to max bytes into the descriptor's data area; the
  131. descriptor's header is set to the number actually read. */
  132. long m2fread(long h, long *desc, long max)
  133. {
  134. long n;
  135. if (max < 0) max = 0;
  136. if (h == 0) { desc[0] = 0; return 0; }
  137. n = (long)fread((char *)(desc + 1), 1, (size_t)max, (FILE *)h);
  138. desc[0] = n;
  139. return n;
  140. }
  141. /* Write a signed integer right-justified in a field of width `wid`.
  142. wid = 0 means exactly one leading space (FileIO convention). */
  143. long m2fwriteintw(long h, long v, long wid)
  144. {
  145. char buf[64];
  146. int n;
  147. if (h == 0) return 0;
  148. if (wid == 0)
  149. n = snprintf(buf, sizeof buf, " %ld", v);
  150. else
  151. n = snprintf(buf, sizeof buf, "%*ld", (int)wid, v);
  152. if (n > 0) fwrite(buf, 1, (size_t)n, (FILE *)h);
  153. return 0;
  154. }
  155. /* Standard streams as file handles. */
  156. long m2stdout(void) { return (long)stdout; }
  157. long m2stderr(void) { return (long)stderr; }
  158. /* Command-line arguments, read from /proc/self/cmdline (Linux). */
  159. static char *m2_args[1024];
  160. static long m2_nargs = -1;
  161. static void m2_argv_init(void)
  162. {
  163. static char cmd[65536];
  164. FILE *f;
  165. long i, n;
  166. int c;
  167. if (m2_nargs >= 0) return;
  168. m2_nargs = 0;
  169. f = fopen("/proc/self/cmdline", "rb");
  170. if (f == NULL) return;
  171. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  172. fclose(f);
  173. if (n <= 0) return;
  174. cmd[n] = 0;
  175. i = 0;
  176. while (i < n && m2_nargs < 1024) {
  177. while (i < n && cmd[i] == 0) i++; /* skip separators */
  178. if (i >= n) break;
  179. m2_args[m2_nargs++] = &cmd[i];
  180. while (i < n && cmd[i] != 0) i++; /* to end of arg */
  181. }
  182. }
  183. long m2argc(void)
  184. {
  185. m2_argv_init();
  186. return m2_nargs;
  187. }
  188. long m2arg(long i, long *desc, long cap)
  189. {
  190. const char *a;
  191. long n;
  192. m2_argv_init();
  193. if (i < 0 || i >= m2_nargs) { desc[0] = 0; return 0; }
  194. a = m2_args[i];
  195. n = (long)strlen(a);
  196. if (cap > 0 && n >= cap) n = cap - 1;
  197. if (n < 0) n = 0;
  198. memcpy((char *)(desc + 1), a, (size_t)n);
  199. desc[0] = n;
  200. return 1;
  201. }
  202. /* Set a string descriptor's logical length (header). */
  203. long m2setlen(long *desc, long n)
  204. {
  205. desc[0] = n;
  206. return n;
  207. }
  208. /* Write a single character (byte) to fd 1. */
  209. long m2writechar(long c)
  210. {
  211. char ch = (char)c;
  212. write(1, &ch, 1);
  213. return 0;
  214. }
  215. /* Write a signed decimal integer to fd 1. */
  216. long m2writeint(long v)
  217. {
  218. char buf[32];
  219. int i = 0;
  220. int neg = 0;
  221. unsigned long u;
  222. if (v < 0) { neg = 1; u = (unsigned long)(-v); }
  223. else u = (unsigned long)v;
  224. do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
  225. if (neg) buf[i++] = '-';
  226. while (i > 0)
  227. write(1, &buf[--i], 1);
  228. return 0;
  229. }
  230. /* Terminate the process. */
  231. long m2exit(long code)
  232. {
  233. exit((int)code);
  234. return 0;
  235. }