| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- /* 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>
- /* Write the contents of a string descriptor to fd 1. Returns the
- number of bytes written. */
- long m2write(long *desc)
- {
- long n = desc[0];
- if (n > 0)
- write(1, (char *)(desc + 1), (size_t)n);
- return n;
- }
- /* Write a newline to fd 1. */
- long m2writeln(void)
- {
- write(1, "\n", 1);
- return 0;
- }
- /* 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;
- }
|