Commit ab5c2555

ab5c2555097bMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 13 files · +6 added · ~7 modified

Description

runtime: implement ARGF (read ARGV files or stdin)

ARGF was an undefined constant, so it resolved to Integer and every method on it emitted the int-fallback 0 (ARGF.class printed Integer).

Add a real ARGF: the constant is a TY_ARGF singleton (a marker pointer to a global state), and a runtime that reads the files named in ARGV in sequence -- or stdin when ARGV is empty (a - filename also means stdin). Supported methods: read, gets / readline, readlines / to_a, each_line / each (block), filename / path, eof? / eof, to_s, and class (its singleton class name "ARGF.class", matching CRuby). The dispatch mirrors the existing TY_IO handling; the type plumbing follows the TY_RANDOM template (ctype, default, scalar-ret, class-name).

Tests: reading a file passed in ARGV (filename + each_line), and the no-args class/filename shape (stdin "-").

Fixes #1157

Details

Commit
ab5c2555097b
Parent
9d9a70880703

lib/sp_runtime.hmodifiedView file

@@ -2266,6 +2266,56 @@
22662266 return sp_argv_array_cache;
22672267 }
22682268
2269+/* ARGF: a pseudo-IO that reads the files named in ARGV in sequence, or stdin
2270+ when ARGV is empty (a `-` filename also means stdin). The state is a single
2271+ global; the ARGF constant is a marker pointer to it. */
2272+typedef struct { mrb_int idx; FILE *cur; int started; const char *fname; } sp_Argf;
2273+static sp_Argf sp_argf_obj = {0, NULL, 0, NULL};
2274+/* Ensure a current readable stream, or return 0 at total end of input. */
2275+static int sp_argf_ensure(void) {
2276+ if (sp_argf_obj.cur) return 1;
2277+ if (sp_argv.len == 0) {
2278+ if (sp_argf_obj.started) return 0;
2279+ sp_argf_obj.started = 1; sp_argf_obj.cur = stdin; sp_argf_obj.fname = "-"; return 1;
2280+ }
2281+ while (sp_argf_obj.idx < sp_argv.len) {
2282+ const char *fn = sp_argv.data[sp_argf_obj.idx++];
2283+ sp_argf_obj.started = 1;
2284+ if (fn && fn[0] == '-' && fn[1] == 0) { sp_argf_obj.cur = stdin; sp_argf_obj.fname = "-"; return 1; }
2285+ FILE *f = fn ? fopen(fn, "r") : NULL;
2286+ if (f) { sp_argf_obj.cur = f; sp_argf_obj.fname = fn; return 1; }
2287+ }
2288+ return 0;
2289+}
2290+static const char *sp_argf_gets(void) {
2291+ for (;;) {
2292+ if (!sp_argf_ensure()) return NULL;
2293+ char buf[8192];
2294+ if (fgets(buf, sizeof buf, sp_argf_obj.cur)) {
2295+ size_t l = strlen(buf); char *r = sp_str_alloc_raw(l + 1); memcpy(r, buf, l + 1); return r;
2296+ }
2297+ if (sp_argf_obj.cur && sp_argf_obj.cur != stdin) fclose(sp_argf_obj.cur);
2298+ sp_argf_obj.cur = NULL; /* EOF on this stream; advance on next ensure */
2299+ }
2300+}
2301+static const char *sp_argf_read(void) {
2302+ sp_String *s = sp_String_new(""); SP_GC_ROOT(s);
2303+ const char *line;
2304+ while ((line = sp_argf_gets())) sp_String_append(s, line);
2305+ return s->data;
2306+}
2307+static sp_StrArray *sp_argf_readlines(void) {
2308+ sp_StrArray *a = sp_StrArray_new(); SP_GC_ROOT(a);
2309+ const char *line;
2310+ while ((line = sp_argf_gets())) sp_StrArray_push(a, line);
2311+ return a;
2312+}
2313+static const char *sp_argf_filename(void) {
2314+ if (sp_argf_obj.fname) return sp_argf_obj.fname;
2315+ return sp_argv.len > 0 ? sp_argv.data[0] : "-";
2316+}
2317+static mrb_bool sp_argf_eof(void) { return !sp_argf_ensure(); }
2318+
22692319 /* Mark active in-flight exception messages. Most raises pass string
22702320 literals (rodata, marker byte ≠ 0xfe → no-op for sp_mark_string),
22712321 but raises that build messages dynamically via sp_sprintf (e.g.