static void prettyPrint(ATerm t, FILE *fp) { ATermList ambs; int count; int i; if (ATmatch(t, "ambiguities(<int>,[<list>])",&count,&ambs)) { if (count == 0) { ATfprintf(fp,"No ambiguities\n"); } else { ATfprintf(fp, "%d ambiguity cluster%s:\n\n",count,count > 1 ? "s" : ""); for(i = 1;!ATisEmpty(ambs); ambs = ATgetNext(ambs), i++) { ATerm amb = ATgetFirst(ambs); ATermList productions; ATerm line, col, offset; if(ATmatch(amb,"ambiguity(" " position(character(0)," " line(<term>)," " col(<term>)," " char(<term>))," " productions([<list>]))", &line, &col, &offset, &productions)) { ATfprintf(fp,"[%d/%d] at (%t:%t):\n", i, count, line, col); for(;!ATisEmpty(productions); productions = ATgetNext(productions)) { char *str = deslash(ATgetFirst(productions)); ATfprintf(fp," %s\n", str); free(str); } ATfprintf(fp,"\n"); } else { ATerror("%s: Unexpected term: %t\n",myname,t); } } } } else { ATerror("%s: Unexpected term: %t\n", myname,t); return; } }
static int main0(int argc, char *argv[]) { int i, j; struct rlimit rlb; char *arg; pthread_t tid; pthread_attr_t pab; argv0 = argv[0]; setlocale(LC_CTYPE, ""); getrlimit(RLIMIT_NOFILE, &rlb); rlb.rlim_cur = rlb.rlim_max; setrlimit(RLIMIT_NOFILE, &rlb); signal(SIGPIPE, SIG_IGN); nworkers = 2; pthread_mutex_init(&print_lock, NULL); pthread_mutex_init(&aworker_lock, NULL); pthread_mutex_init(&matches_lock, NULL); for (i = 1; i < argc && argv[i][0] == '-'; i++) for (j = 1; j > 0 && argv[i][j]; ++j) switch (argv[i][j]) { case '-': ++i; goto EndOptions; case 'V': print_version(stdout); break; case 'd': ++debug; break; case 'i': ignore_case = 1; break; case 'v': ++verbose; break; case 'h': usage(stdout); exit(0); case 'l': ++line_f; break; case 'L': if (argv[i][2]) arg = argv[i]+2; else arg = argv[++i]; if (!arg || sscanf(arg, "%u", &maxlen) != 1) { fprintf(stderr, "%s: Invalid length specification: %s\n", argv[0], arg ? arg : "<null>"); exit(1); } j = -2; break; case 'n': if (argv[i][2]) arg = argv[i]+2; else arg = argv[++i]; if (!arg || sscanf(arg, "%u", &nworkers) != 1) { fprintf(stderr, "%s: Invalid workers specification: %s\n", argv[0], arg ? arg : "<null>"); exit(1); } j = -2; break; default: fprintf(stderr, "%s: unknown command line switch: -%c\n", argv[0], argv[i][j]); exit(1); } EndOptions: rstr = (unsigned char *) strdup(argv[i++]); rlen = deslash(rstr); if (bm_init(&bmb, rstr, rlen, ignore_case) < 0) { fprintf(stderr, "%s: Failed search string setup: %s\n", argv[0], rstr); exit(1); } max_depth = rlb.rlim_max - nworkers - 16; if (debug) fprintf(stderr, "max_depth = %d, nworkers = %d\n", max_depth, nworkers); pqueue_init(&pqb, nworkers + 8); pthread_attr_init(&pab); pthread_attr_setscope(&pab, PTHREAD_SCOPE_SYSTEM); aworkers = nworkers; for (j = 0; j < nworkers; ++j) if (pthread_create(&tid, &pab, worker, NULL) != 0) { fprintf(stderr, "%s: pthread_create: failed to create worker thread\n", argv[0]); exit(1); } while (i < argc && do_ftw(argv[i++]) == 0) ; pqueue_close(&pqb); if (debug) fprintf(stderr, "Waiting for workers to finish...\n"); pthread_mutex_lock(&aworker_lock); while (aworkers > 0) pthread_cond_wait(&aworker_cv, &aworker_lock); pthread_mutex_unlock(&aworker_lock); if (debug) fprintf(stderr, "n_files = %d, n_matches = %d, n_workers = %d, n_Mbytes = %d\n", n_files, n_matches, nworkers, (int) (n_bytes / 1000000)); return n_matches; }