| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- /* m2compiler-V3 runtime shim (C).
- *
- * Bridges Modula-2 external procedures to the host. Memory layout
- * matches the compiler's: a STRING (ARRAY OF CHAR) value is a
- * descriptor [0] = LONGCARD element count, then the bytes; the
- * compiler passes the descriptor's address as an `l`.
- *
- * Linked with every V3 program image.
- */
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- /* Write the contents of a string descriptor to fd 1. Returns the
- number of bytes written. */
- long m2write(long *desc)
- {
- /* desc[0] is the capacity; the string is NUL-terminated. */
- char *s = (char *)(desc + 1);
- size_t n = strnlen(s, (size_t)desc[0] + 1);
- if (n > 0)
- write(1, s, n);
- return (long)n;
- }
- /* Write a newline to fd 1. */
- long m2writeln(void)
- {
- write(1, "\n", 1);
- return 0;
- }
- /* Read one character from stdin (0 at EOF). */
- long m2readchar(void)
- {
- int c = getchar();
- return (c == EOF) ? 0 : c;
- }
- /* Read a signed decimal integer from stdin, skipping leading
- whitespace. The first non-digit after the number is consumed. */
- long m2readint(void)
- {
- long v = 0;
- int c;
- int neg = 0;
- do { c = getchar(); } while (c == ' ' || c == '\t' ||
- c == '\n' || c == '\r');
- if (c == '-') { neg = 1; c = getchar(); }
- while (c >= '0' && c <= '9') {
- v = v * 10 + (c - '0');
- c = getchar();
- }
- return neg ? -v : v;
- }
- /* ---------------- sequential files (stdio) ---------------- */
- /* Copy a descriptor string into a NUL-terminated buffer. */
- static void m2cstr(long *desc, char *buf, size_t cap)
- {
- char *s = (char *)(desc + 1);
- size_t n = strnlen(s, (size_t)desc[0] + 1);
- if (n >= cap) n = cap - 1;
- if (n > 0) memcpy(buf, s, n);
- buf[n] = 0;
- }
- long m2fopenread(long *desc)
- {
- char buf[4096];
- m2cstr(desc, buf, sizeof buf);
- return (long)fopen(buf, "r");
- }
- long m2fopenwrite(long *desc)
- {
- char buf[4096];
- m2cstr(desc, buf, sizeof buf);
- return (long)fopen(buf, "w");
- }
- long m2fclose(long h)
- {
- if (h != 0) fclose((FILE *)h);
- return 0;
- }
- /* A handle of 0 means the open failed (Modula-2 NIL). Every file
- operation degrades to a safe no-op instead of dereferencing NULL. */
- long m2fgetc(long h)
- {
- int c;
- if (h == 0) return 0;
- c = fgetc((FILE *)h);
- return (c == EOF) ? 0 : c;
- }
- long m2fputc(long h, long c)
- {
- if (h != 0) fputc((int)c, (FILE *)h);
- return 0;
- }
- long m2fputs(long h, long *desc)
- {
- char *s = (char *)(desc + 1);
- size_t n = strnlen(s, (size_t)desc[0] + 1);
- if (h == 0) return 0;
- if (n > 0) fwrite(s, 1, n, (FILE *)h);
- return (long)n;
- }
- long m2freadint(long h)
- {
- long v = 0;
- int c, neg = 0;
- if (h == 0) return 0;
- do { c = fgetc((FILE *)h); } while (c == ' ' || c == '\t' ||
- c == '\n' || c == '\r');
- if (c == '-') { neg = 1; c = fgetc((FILE *)h); }
- while (c >= '0' && c <= '9') {
- v = v * 10 + (c - '0');
- c = fgetc((FILE *)h);
- }
- return neg ? -v : v;
- }
- long m2fwriteint(long h, long v)
- {
- char buf[32];
- int i = 0, neg = 0;
- unsigned long u;
- if (h == 0) return 0;
- if (v < 0) { neg = 1; u = (unsigned long)(-v); }
- else u = (unsigned long)v;
- do { buf[i++] = (char)('0' + u % 10); u /= 10; } while (u != 0);
- if (neg) buf[i++] = '-';
- while (i > 0) fputc(buf[--i], (FILE *)h);
- return 0;
- }
- long m2fwriteln(long h)
- {
- if (h != 0) fputc('\n', (FILE *)h);
- return 0;
- }
- /* Bulk read: up to max bytes into the descriptor's data area; the
- descriptor's header is set to the number actually read. */
- long m2fread(long h, long *desc, long max)
- {
- long n;
- if (max < 0) max = 0;
- if (h == 0) { desc[0] = 0; return 0; }
- n = (long)fread((char *)(desc + 1), 1, (size_t)max, (FILE *)h);
- desc[0] = n;
- return n;
- }
- /* Write a signed integer right-justified in a field of width `wid`.
- wid = 0 means exactly one leading space (FileIO convention). */
- long m2fwriteintw(long h, long v, long wid)
- {
- char buf[64];
- int n;
- if (h == 0) return 0;
- if (wid == 0)
- n = snprintf(buf, sizeof buf, " %ld", v);
- else
- n = snprintf(buf, sizeof buf, "%*ld", (int)wid, v);
- if (n > 0) fwrite(buf, 1, (size_t)n, (FILE *)h);
- return 0;
- }
- /* Standard streams as file handles. */
- long m2stdout(void) { return (long)stdout; }
- long m2stderr(void) { return (long)stderr; }
- /* Command-line arguments, read from /proc/self/cmdline (Linux). */
- static char *m2_args[1024];
- static long m2_nargs = -1;
- static void m2_argv_init(void)
- {
- static char cmd[65536];
- FILE *f;
- long i, n;
- int c;
- if (m2_nargs >= 0) return;
- m2_nargs = 0;
- f = fopen("/proc/self/cmdline", "rb");
- if (f == NULL) return;
- n = (long)fread(cmd, 1, sizeof cmd - 1, f);
- fclose(f);
- if (n <= 0) return;
- cmd[n] = 0;
- i = 0;
- while (i < n && m2_nargs < 1024) {
- while (i < n && cmd[i] == 0) i++; /* skip separators */
- if (i >= n) break;
- m2_args[m2_nargs++] = &cmd[i];
- while (i < n && cmd[i] != 0) i++; /* to end of arg */
- }
- }
- long m2argc(void)
- {
- m2_argv_init();
- return m2_nargs;
- }
- long m2arg(long i, long *desc, long cap)
- {
- const char *a;
- long n;
- m2_argv_init();
- if (i < 0 || i >= m2_nargs) { ((char *)(desc + 1))[0] = 0; return 0; }
- a = m2_args[i];
- n = (long)strlen(a);
- if (cap > 0 && n >= cap) n = cap - 1;
- if (n < 0) n = 0;
- memcpy((char *)(desc + 1), a, (size_t)n);
- if (n < cap) ((char *)(desc + 1))[n] = 0; /* NUL terminator */
- /* desc[0] (capacity) is left intact: length is derived by scanning */
- return 1;
- }
- /* Set a string descriptor's logical length (header). */
- long m2setlen(long *desc, long n)
- {
- desc[0] = n;
- return n;
- }
- /* Write a single character (byte) to fd 1. */
- long m2writechar(long c)
- {
- char ch = (char)c;
- write(1, &ch, 1);
- return 0;
- }
- /* Write a signed decimal integer to fd 1. */
- long m2writeint(long v)
- {
- char buf[32];
- int i = 0;
- int neg = 0;
- unsigned long u;
- if (v < 0) { neg = 1; u = (unsigned long)(-v); }
- else u = (unsigned long)v;
- do { buf[i++] = (char)('0' + (u % 10)); u /= 10; } while (u != 0);
- if (neg) buf[i++] = '-';
- while (i > 0)
- write(1, &buf[--i], 1);
- return 0;
- }
- /* Terminate the process. */
- long m2exit(long code)
- {
- exit((int)code);
- return 0;
- }
- /* String content equality: 1 when the NUL-terminated contents of the
- two descriptors match. */
- long m2streq(long *a, long *b)
- {
- return (strcmp((char *)(a + 1), (char *)(b + 1)) == 0) ? 1 : 0;
- }
- /* ---------------- reals (Conversions / RealIO) ---------------- */
- /* Format a REAL into a descriptor string (6 significant digits,
- scientific). desc[0] is the capacity; on return it holds the
- length. Returns the number of characters written. */
- long m2realstr(double x, long *desc)
- {
- char buf[64];
- char *dst = (char *)(desc + 1);
- long cap = desc[0];
- int n = snprintf(buf, sizeof buf, "%.6E", x);
- if (n < 0) n = 0;
- if (cap > 0 && n >= (int)cap) n = (int)cap - 1;
- if (n > 0) memcpy(dst, buf, (size_t)n);
- if (cap > 0) { dst[n] = 0; desc[0] = n; }
- return (long)n;
- }
- /* Parse a REAL from a descriptor string; *ok := 1 on success. */
- double m2strreal(long *desc, long *ok)
- {
- char buf[4096];
- char *end;
- double v;
- m2cstr(desc, buf, sizeof buf);
- v = strtod(buf, &end);
- while (*end == ' ' || *end == '\t' || *end == '\n' || *end == '\r') end++;
- *ok = (end != buf && *end == 0) ? 1 : 0;
- return v;
- }
- /* Write a REAL right-justified in a field of width `wid` (0 => one
- leading space), matching the integer convention. */
- long m2writerealf(double x, long wid)
- {
- char buf[128];
- int n;
- if (wid == 0)
- n = snprintf(buf, sizeof buf, "%.6E", x);
- else
- n = snprintf(buf, sizeof buf, "%*.*E", (int)wid, 6, x);
- if (n > 0) write(1, buf, (size_t)n);
- return 0;
- }
- long m2writereal(double x)
- {
- return m2writerealf(x, 0);
- }
- /* Read a REAL from stdin. */
- double m2readreal(void)
- {
- double v = 0.0;
- if (scanf("%lf", &v) != 1) v = 0.0;
- return v;
- }
|