]> git.draconx.ca Git - cdecl99.git/blobdiff - test/crossparse.c
Port random crossparse test to Autotest.
[cdecl99.git] / test / crossparse.c
index 05c3095a2d1953220e9860b934a1e2f5962b8c51..210714a3a5dada4fcfdaf9ca88a95387f45f6935 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <errno.h>
 #include <stdbool.h>
 #include <getopt.h>
 #include <cdecl.h>
 
 #define PROGNAME "crossparse"
 static const char *progname = PROGNAME;
-static const char sopts[] = "ECVH";
+static const char sopts[] = "f:ECVH";
 static const struct option lopts[] = {
        { "cdecl",   0, NULL, 'C' },
        { "english", 0, NULL, 'E' },
+       { "file",    2, NULL, 'f' },
        { "version", 0, NULL, 'V' },
        { "help",    0, NULL, 'H' },
        { 0 }
@@ -131,13 +133,22 @@ out:
        free(buf1);
        free(buf2);
        free(buf3);
+
+       if (!ret) {
+               fprintf(stderr, "%s: failed cross-parse check of: %s\n",
+                               progname, str);
+       }
+
        return ret;
 }
 
 int main(int argc, char **argv)
 {
        int opt, mode = MODE_CDECL;
-       int ret = EXIT_FAILURE;
+       int ret = EXIT_SUCCESS;
+
+       const char *filename = NULL;
+       FILE *infile = NULL;
 
        if (argc > 0)
                progname = argv[0];
@@ -150,6 +161,10 @@ int main(int argc, char **argv)
                case 'E':
                        mode = MODE_ENGLISH;
                        break;
+               case 'f':
+                       infile = stdin;
+                       filename = optarg;
+                       break;
                case 'V':
                        test_print_version(PROGNAME);
                        return EXIT_SUCCESS;
@@ -162,18 +177,35 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!argv[optind]) {
-               print_usage(stderr);
-               return EXIT_FAILURE;
-       }
+       if (infile) {
+               char *line = NULL;
+               size_t n;
+
+               if (filename) {
+                       infile = fopen(filename, "r");
+                       if (!infile) {
+                               fprintf(stderr, "%s: %s: %s\n", progname,
+                                               filename, strerror(errno));
+                               return EXIT_FAILURE;
+                       }
+               }
 
-       for (int i = optind; i < argc; i++) {
-               if (!test_crossparse(argv[i], mode)) {
-                       fprintf(stderr, "%s: failed cross-parse check of: %s\n",
-                                       progname, argv[i]);
-                       return EXIT_FAILURE;
+               while (getline(&line, &n, infile) >= 0) {
+                       char *c = strchr(line, '\n');
+                       if (c)
+                               *c = '\0';
+                       if (!test_crossparse(line, mode))
+                               ret = EXIT_FAILURE;
                }
+       } else if (argv[optind]) {
+               for (int i = optind; i < argc; i++) {
+                       if (!test_crossparse(argv[i], mode))
+                               ret = EXIT_FAILURE;
+               }
+       } else {
+               print_usage(stderr);
+               return EXIT_FAILURE;
        }
 
-       return EXIT_SUCCESS;
+       return ret;
 }