From: Martin Mares Date: Sat, 17 Nov 2007 22:02:44 +0000 (+0100) Subject: Improved parsing of switches. X-Git-Tag: python-dummy-working~282 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=2ac38d23f88e706088d2218c1bc6959c5bed1e8a;p=moe.git Improved parsing of switches. --- diff --git a/judge/judge-tok.c b/judge/judge-tok.c index bb4e95f..017f608 100644 --- a/judge/judge-tok.c +++ b/judge/judge-tok.c @@ -6,33 +6,57 @@ */ #include +#include #include +#include #include "judge.h" +static int ignore_nl, ignore_trailing_nl; + static int trailing_nl(struct tokenizer *t) { // Ignore empty lines at the end of file - if (t->token[0]) + if (t->token[0] || !ignore_trailing_nl) return 0; t->flags &= ~TF_REPORT_LINES; return !get_token(t); } +static void usage(void) +{ + fprintf(stderr, "Usage: judge-tok [] \n\ +\n\ +Options:\n\ +-n\t\tIgnore newlines\n\ +-t\t\tIgnore newlines at the end of file\n\ +"); + exit(2); +} + int main(int argc, char **argv) { struct tokenizer t1, t2; - int report_lines = 1; - - if (argc != 3 && argc != 4) - die("Usage: judge-tok [-n] "); + int opt; - // Check for -n parameter - report_lines = !(argc == 4 && !strcmp(argv[1], "-n")); + while ((opt = getopt(argc, argv, "nt")) >= 0) + switch (opt) + { + case 'n': + ignore_nl++; + break; + case 't': + ignore_trailing_nl++; + break; + default: + usage(); + } + if (optind + 2 != argc) + usage(); - tok_init(&t1, sopen_read(argv[argc-2])); - tok_init(&t2, sopen_read(argv[argc-1])); - if (report_lines) + tok_init(&t1, sopen_read(argv[optind])); + tok_init(&t2, sopen_read(argv[optind+1])); + if (!ignore_nl) t1.flags = t2.flags = TF_REPORT_LINES; for (;;)