m2make_os.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* M2make OS helpers (C).
  2. *
  3. * Dual-ABI file/time/process primitives for the M2make front-end.
  4. *
  5. * Two string ABIs exist and both are served here:
  6. * - V3 (m2compiler-V3): ARRAY OF CHAR is a descriptor (LONGCARD
  7. * count + bytes (see runtime/syslib/shim.c). V3 buffers handed
  8. * to M2make are NUL-padded with count == capacity (verified by
  9. * probe), so the _d variants scan for NUL bounded by the count.
  10. * - GNU Modula-2: ARRAY OF CHAR is a NUL-terminated C buffer;
  11. * the _s variants use it directly.
  12. *
  13. * Handle-only calls (close) are ABI-neutral and shared.
  14. * Only the string-taking calls need _d/_s pairs.
  15. * Readline never overruns: Modula-2 passes cap (HIGH(buf)+1) and at
  16. * most cap-1 bytes plus NUL are stored. Physical lines longer than
  17. * the C temp (2048) continue on the next call; the Modula-2 scanner
  18. * keeps comment state across lines, so this only splits very long
  19. * lines (documented in the README).
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/stat.h>
  25. #define M2OS_TMP 2048
  26. /* Copy a V3 descriptor to a NUL-terminated buffer (NUL scan bounded
  27. by the descriptor count, then by cap). */
  28. static void desc2c(long *desc, char *buf, unsigned long cap)
  29. {
  30. long count = desc[0];
  31. unsigned long bound;
  32. unsigned long i = 0;
  33. if (count < 0)
  34. count = 0;
  35. bound = (unsigned long)count;
  36. if (bound >= cap)
  37. bound = cap - 1;
  38. while (i < bound && ((char *)(desc + 1))[i] != 0) {
  39. buf[i] = ((char *)(desc + 1))[i];
  40. i++;
  41. }
  42. buf[i] = 0;
  43. }
  44. /* ---------------- existence ---------------- */
  45. long m2osExistD(long *desc)
  46. {
  47. char buf[4096];
  48. struct stat st;
  49. desc2c(desc, buf, sizeof buf);
  50. return (stat(buf, &st) == 0);
  51. }
  52. long m2osExistS(char *s)
  53. {
  54. struct stat st;
  55. return (stat(s, &st) == 0);
  56. }
  57. /* ---------------- modification time ----------------
  58. * 1 with *t set on success, 0 when the file cannot be statted. */
  59. long m2osMtimeD(long *desc, long long *t)
  60. {
  61. char buf[4096];
  62. struct stat st;
  63. desc2c(desc, buf, sizeof buf);
  64. if (stat(buf, &st) != 0)
  65. return 0;
  66. *t = (long long)st.st_mtime;
  67. return 1;
  68. }
  69. long m2osMtimeS(char *s, long long *t)
  70. {
  71. struct stat st;
  72. if (stat(s, &st) != 0)
  73. return 0;
  74. *t = (long long)st.st_mtime;
  75. return 1;
  76. }
  77. /* ---------------- process execution ----------------
  78. * Runs cmd via /bin/sh, returns the raw system() code. */
  79. long m2osSystemD(long *desc)
  80. {
  81. char buf[4096];
  82. desc2c(desc, buf, sizeof buf);
  83. return (long)system(buf);
  84. }
  85. long m2osSystemS(char *s)
  86. {
  87. return (long)system(s);
  88. }
  89. /* ---------------- process exit (ABI-neutral) ---------------- */
  90. void m2os_flush(void)
  91. {
  92. fflush(NULL);
  93. }
  94. void m2osBye(long code)
  95. {
  96. fflush(NULL);
  97. exit((int)code);
  98. }
  99. /* ---------------- command-line arguments (V3 side) ----------------
  100. * Skips argv[0] (the program name), unlike the raw SysShim.arg. */
  101. static char *m2os_args[1024];
  102. static long m2os_nargs = -1;
  103. static void m2os_argv_init(void)
  104. {
  105. static char cmd[65536];
  106. FILE *f;
  107. long i, n;
  108. if (m2os_nargs >= 0)
  109. return;
  110. m2os_nargs = 0;
  111. f = fopen("/proc/self/cmdline", "rb");
  112. if (f == NULL)
  113. return;
  114. n = (long)fread(cmd, 1, sizeof cmd - 1, f);
  115. fclose(f);
  116. if (n <= 0)
  117. return;
  118. cmd[n] = 0;
  119. i = 0;
  120. while (i < n && m2os_nargs < 1024) {
  121. while (i < n && cmd[i] == 0)
  122. i++;
  123. if (i >= n)
  124. break;
  125. m2os_args[m2os_nargs++] = &cmd[i];
  126. while (i < n && cmd[i] != 0)
  127. i++;
  128. }
  129. }
  130. long m2osArgcD(void)
  131. {
  132. m2os_argv_init();
  133. if (m2os_nargs <= 0)
  134. return 0;
  135. return m2os_nargs - 1;
  136. }
  137. long m2osArgD(long i, long *desc, unsigned long cap)
  138. {
  139. const char *a;
  140. unsigned long n, k;
  141. char *out;
  142. m2os_argv_init();
  143. if (cap == 0)
  144. return 0;
  145. if (i < 0 || i + 1 >= m2os_nargs)
  146. return 0;
  147. a = m2os_args[i + 1];
  148. n = (unsigned long)strlen(a);
  149. if (n >= cap)
  150. n = cap - 1;
  151. out = (char *)(desc + 1);
  152. for (k = 0; k < n; k++)
  153. out[k] = a[k];
  154. out[n] = 0;
  155. return 1;
  156. }
  157. /* ---------------- sequential text input ---------------- */
  158. long m2osOpenreadD(long *desc)
  159. {
  160. char buf[4096];
  161. desc2c(desc, buf, sizeof buf);
  162. return (long)fopen(buf, "r");
  163. }
  164. long m2osOpenreadS(char *s)
  165. {
  166. return (long)fopen(s, "r");
  167. }
  168. /* Shared readline core: 1 with a line stored, 0 at end of file.
  169. * The trailing newline (and a preceding carriage return) is
  170. * stripped. A final line without a newline still returns 1. */
  171. static long readline_core(FILE *fp, char *out, unsigned long cap)
  172. {
  173. static char tmp[M2OS_TMP];
  174. unsigned long n, i;
  175. if (cap == 0)
  176. return 0;
  177. if (fgets(tmp, sizeof tmp, fp) == NULL)
  178. return 0;
  179. n = (unsigned long)strlen(tmp);
  180. while (n > 0 && (tmp[n - 1] == 10 || tmp[n - 1] == 13))
  181. n--;
  182. if (n >= cap)
  183. n = cap - 1;
  184. for (i = 0; i < n; i++)
  185. out[i] = tmp[i];
  186. out[n] = 0;
  187. return 1;
  188. }
  189. long m2osReadlineD(long h, long *desc, unsigned long cap)
  190. {
  191. char *out;
  192. if (h == 0 || cap == 0)
  193. return 0;
  194. out = (char *)(desc + 1);
  195. return readline_core((FILE *)h, out, cap);
  196. }
  197. long m2osReadlineS(long h, char *buf, unsigned long cap)
  198. {
  199. if (h == 0)
  200. return 0;
  201. return readline_core((FILE *)h, buf, cap);
  202. }
  203. long m2osClose(long h)
  204. {
  205. if (h != 0)
  206. fclose((FILE *)h);
  207. return 0;
  208. }