]> pd.if.org Git - zpackage/blobdiff - t/ctap/prove.c
initialize variable
[zpackage] / t / ctap / prove.c
index 8ff8a6410ded3fb02a77bee17e1ad5aa3d30c853..7d1b8e2eaa37b172bf95240e26b3869017e3db0d 100644 (file)
@@ -11,6 +11,7 @@
 #include <regex.h>
 
 #define GREATER(x,y) ( ((x) > (y)) ? (x) : (y) )
+#define WARN(x) fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, x)
 
 struct result {
        int test; /* i.e. the number of the test */
@@ -21,6 +22,7 @@ struct result {
 
 struct testrun {
        char *name;
+       int readstdin;
        int version;
        int plan;
        int ran;
@@ -55,43 +57,51 @@ int runone(struct testrun *run) {
        int pipefd[2];
        pid_t cpid;
        FILE *tap;
-       char *line;
+       char *line = 0;
        ssize_t nread;
        size_t len = 0;
        int written = 0;
        
-       if (pipe(pipefd) == -1) {
-               perror("pipe");
-               exit(EXIT_FAILURE);
-               }
-
-       cpid = fork();
-       if (cpid == -1) {
-               perror("fork");
-               exit(EXIT_FAILURE);
-       }
+       if (!run->readstdin) {
+               if (pipe(pipefd) == -1) {
+                       perror("pipe");
+                       exit(EXIT_FAILURE);
+               }
 
-       if (cpid == 0) {
-               /* child */
-               close(pipefd[0]); /* close read end */
-               close(1); /* close stdout */
-               dup(pipefd[1]); /* set stdout to the write end of the pipe */
-               execlp(run->name, run->name, NULL);
-               /* only gets here if exec failed */
-               printf("Bail Out! exec %s failed: %s\n", run->name, strerror(errno));
-               exit(EXIT_FAILURE);
-       }
+               cpid = fork();
+               if (cpid == -1) {
+                       perror("fork");
+                       exit(EXIT_FAILURE);
+               }
+
+               if (cpid == 0) {
+                       /* child */
+                       close(pipefd[0]); /* close read end */
+                       close(1); /* close stdout */
+                       dup(pipefd[1]); /* set stdout to the write end of the pipe */
+                       execlp(run->name, run->name, NULL);
+                       /* only gets here if exec failed */
+                       printf("Bail Out! exec %s failed: %s\n", run->name, strerror(errno));
+                       exit(EXIT_FAILURE);
+               }
 
-       close(pipefd[1]);
-       /* parent continue on */
-       tap = fdopen(pipefd[0], "r");
+               close(pipefd[1]);
+               /* parent continue on */
+               tap = fdopen(pipefd[0], "r");
+               if (tap == NULL) {
+                       perror("tap is null:");
+                       exit(EXIT_FAILURE);
+               }
+       } else {
+               tap = stdin;
+       }
 
        regex_t plan, diagnostic, bail, test, directive, version;
        regmatch_t match[4];
 
        regcomp(&plan, "^[[:digit:]]+\\.\\.([[:digit:]]+)", REG_EXTENDED);
        regcomp(&diagnostic, "#[[:space:]]*(.+)", REG_EXTENDED);
-       regcomp(&bail, "^Bail Out![[:space:]]*(.+)", REG_EXTENDED);
+       regcomp(&bail, "^Bail Out![[:space:]]*(.+)", REG_EXTENDED|REG_ICASE);
        regcomp(&version, "^TAP Version ([[:digit:]]+)", REG_EXTENDED);
 
        regcomp(&test, "^(not )?ok([[:space:]]*([[:digit:]]+))?", REG_EXTENDED);
@@ -218,7 +228,9 @@ int runone(struct testrun *run) {
        regfree(&test);
 
        free(line);
-       fclose(tap);
+       if (!run->readstdin) {
+               fclose(tap);
+       }
        wait(NULL);
 
        if (run->ran < run->plan) {
@@ -251,6 +263,8 @@ int main(int ac, char *av[]) {
        struct testrun clear = { 0 };
        struct testrun *runs;
        int opt;
+       char *stdinname = "<stdin>";
+       int stdinnamelen = 0;
 
        /*
         * -j json output
@@ -266,7 +280,7 @@ int main(int ac, char *av[]) {
 
        /* TODO -jj turn off everything else? */
        /* TODO -cC color escapes */
-       while ((opt = getopt(ac, av, "jJpPiIsSdD")) != -1) {
+       while ((opt = getopt(ac, av, "jJpPiIsSdDn:")) != -1) {
                switch (opt) {
                        case 'j': json = 1; break;
                        case 'J': json = 0; break;
@@ -278,25 +292,39 @@ int main(int ac, char *av[]) {
                        case 'S': summary = 0; break;
                        case 'd': detail = 1; break;
                        case 'D': detail = 0; break;
+                       case 'n': stdinname = optarg;
+                                 break;
                        default: /* '?' */
                                fprintf(stderr, "Usage: %s [-jpisd] file...\n",
                                                av[0]);
                                exit(EXIT_FAILURE);
                }
        }
+       stdinnamelen = strlen(stdinname);
 
        runs = malloc(ac * sizeof *runs);
 
        for (i=optind; i<ac; i++) {
-               len = strlen(av[i]);
-               maxnamelen = GREATER(len, maxnamelen);
+               if (!strcmp(av[i], "-")) {
+                       maxnamelen = GREATER(stdinnamelen, maxnamelen);
+               } else {
+                       len = strlen(av[i]);
+                       maxnamelen = GREATER(len, maxnamelen);
+               }
        }
 
        for (i=optind; i<ac; i++) {
                run = clear;
-               run.name = av[i];
                run.interactive = interactive;
 
+               if (!strcmp(av[i], "-")) {
+                       run.readstdin = 1;
+                       run.name = stdinname;
+               } else {
+                       run.readstdin = 0;
+                       run.name = av[i];
+               }
+
                if (progress) {
                        printf("%s ", run.name);
                        len = strlen(run.name);