/* file: standalone.c G. Moody 22 February 2012 Last revised: 3 March 2012 main() and other functions for standalone PhysioBank Simple Query Server */ #include "pbsqs.h" #include #include static int maxlines, maxcols; /* Read a string from stdin, and return a pointer to it. Returns NULL on EOF. */ char *rl_gets () { static char *line_read = (char *)NULL; if (line_read) { free(line_read); line_read = (char *)NULL; } line_read = readline("Look for: "); if (line_read && *line_read) add_history(line_read); return (line_read); } void newline() { int r; fprintf(ofile, "\n"); if (maxlines == 0) { rl_initialize(); rl_get_screen_size(&maxlines, &maxcols); } if (++lines >= maxlines) { fprintf(ofile, " for more, Q to return to query prompt: "); rl_prep_terminal(0); r = rl_getc(stdin); rl_reset_screen_size(); rl_get_screen_size(&maxlines, &maxcols); rl_deprep_terminal(); if (r == EOF || r == 'q' || r == 'x' || r == 'Q' || r == 'X') { verbose = 0; fprintf(ofile, "\r [Output interrupted!] \n"); } else fprintf(ofile, "\r \r"); lines = 2; } } main(int argc, char **argv) { ifile = stdin; ofile = stdout; efile = stderr; fprintf(ofile, "Loading PhysioBank Index from %s ...\n Read %d bytes\n", PBI_URL, geturl(PBI_URL, &indeximg)); if (parsepbi()) { int matches, qc = 0, status = 1; fprintf(ofile, " Parsing complete (%d records, %d lines)\n", nrec, nie); fprintf(ofile, " Enter a query, or ? for help, or type EXIT to quit\n\n"); while (status) { static char *p; Query q; while ((p = rl_gets()) == NULL) ; lines = 1; matches = -1; verbose = 1; q = parsequery(p); switch (q.type) { case qt_count: showlist = 0; matches = search(q); qc++; break; case qt_list: showlist = 1; matches = search(q); qc++; break; case qt_long: showlist = 2; matches = search(q); qc++; break; case qt_help: help(q); fprintf(ofile, "You may use the arrow keys and Emacs-style control " "characters"); newline(); fprintf(ofile, "to recall and edit previously entered queries."); newline(); break; case qt_exit: status = 0; break; case qt_null: default: break; } if (matches >= 0) { newline(); fprintf(ofile, "%d match%s for query: %s %s %s", matches, (matches == 1 ? "" : "es"), q.iname, rn[q.comp].name, q.sval); lines = 2; newline(); } } fprintf(ofile, "\nExit requested, %d quer%s processed\nBye!\n", qc, (qc == 1 ? "y" : "ies")); } else { fprintf(efile, "Index was not loaded from %s, exiting!\n", PBI_URL); exit(1); } cleanup(); exit(0); }