chiark / gitweb /
@@@ tvec error return
[mLib] / test / tvec-main.c
index ea597adf093239c18f4ccabce0b8644d38e95db1..aa361807e9ee7e51ad309f9dbfe5e77d6db66def 100644 (file)
@@ -172,52 +172,68 @@ void tvec_parseargs(int argc, char *argv[], struct tvec_state *tv_out,
   tvec_begin(tv_out, info, o); *argpos_out = optind;
 }
 
-void tvec_readstdin(struct tvec_state *tv)
-  { tvec_read(tv, "<stdin>", stdin); }
+int tvec_readstdin(struct tvec_state *tv)
+  { return (tvec_read(tv, "<stdin>", stdin)); }
 
-void tvec_readfile(struct tvec_state *tv, const char *file)
+int tvec_readfile(struct tvec_state *tv, const char *file)
 {
-  FILE *fp;
+  FILE *fp = 0;
+  int rc;
 
   fp = fopen(file, "r");
-  if (!fp)
+  if (!fp) {
     tvec_error(tv, "failed to open `%s' for reading: %s",
               file, strerror(errno));
-  tvec_read(tv, file, fp);
-  fclose(fp);
+    rc = -1; goto end;
+  }
+  if (tvec_read(tv, file, fp)) { rc = -1; goto end; }
+  rc = 0;
+end:
+  if (fp) fclose(fp);
+  return (rc);
 }
 
-void tvec_readdflt(struct tvec_state *tv, const char *file)
+int tvec_readdflt(struct tvec_state *tv, const char *file)
 {
   dstr d = DSTR_INIT;
   const char *p;
+  int rc;
 
   if (file) {
     p = getenv("srcdir");
-    if (p)
-      { dstr_putf(&d, "%s/%s", p, file); file = d.buf; }
-    tvec_readfile(tv, file);
+    if (p) { dstr_putf(&d, "%s/%s", p, file); file = d.buf; }
+    rc = tvec_readfile(tv, file);
   } else if (isatty(0))
-    tvec_error(tv, "use `-' to force reading from interactive stdin");
+    rc = tvec_error(tv, "use `-' to force reading from interactive stdin");
   else
-    tvec_readstdin(tv);
+    rc = tvec_readstdin(tv);
+  dstr_destroy(&d);
+  return (rc);
 }
 
-void tvec_readarg(struct tvec_state *tv, const char *arg)
+int tvec_readarg(struct tvec_state *tv, const char *arg)
 {
-  if (STRCMP(arg, ==, "-")) tvec_readstdin(tv);
-  else tvec_readfile(tv, arg);
+  int rc;
+
+  if (STRCMP(arg, ==, "-")) rc = tvec_readstdin(tv);
+  else rc = tvec_readfile(tv, arg);
+  return (rc);
 }
 
-void tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
+int tvec_readargs(int argc, char *argv[], struct tvec_state *tv,
                   int *argpos_inout, const char *dflt)
 {
   int i = *argpos_inout;
+  int rc;
 
-
-  if (i == argc) tvec_readdflt(tv, dflt);
-  else while (i < argc) tvec_readarg(tv, argv[i++]);
+  if (i == argc) rc = tvec_readdflt(tv, dflt);
+  else {
+    rc = 0;
+    while (i < argc)
+      if (tvec_readarg(tv, argv[i++])) rc = -1;
+  }
   *argpos_inout = i;
+  return (rc);
 }
 
 int tvec_main(int argc, char *argv[],