| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <unistd.h> |
| #include <libgen.h> |
|
|
| #include "libtoken.h" |
|
|
| int main(int argc, char *argv[]) |
| { |
| extern char *optarg; |
| extern int opterr; |
| extern int optind; |
| int option; |
| char const *opt_str = "1acdhjkl:m:nNo:rsvwW"; |
| char usage_str[80]; |
|
|
| const char *token; |
| enum TokenClass type; |
| unsigned line; |
| unsigned col; |
| unsigned pos; |
| unsigned token_len; |
| unsigned num_files = 0; |
| int start_token = 0; |
| int continuous_files = 0; |
|
|
| char *outfile = 0; |
| enum { PLAIN, CSV, JSON, JSONL, XML, RAW } mode = PLAIN; |
| int first_time = 1; |
| Language source; |
| int explicit_source = 0; |
| int append = 0; |
| int suppress_newline = 0; |
|
|
| sprintf(usage_str, "usage: %%s [ -%s ] [ FILES ]\n", opt_str); |
|
|
| |
| while ((option = getopt(argc, argv, opt_str)) != EOF) { |
| switch (option) { |
|
|
| case '1': |
| continuous_files = 1; |
| break; |
|
|
| case 'a': |
| append = 1; |
| break; |
|
|
| case 'c': |
| hash_as_comment = 1; |
| break; |
|
|
| case 'd': |
| debug = verbose = 1; |
| break; |
|
|
| case 'h': |
| fputs( |
| "A tokenizer for C/C++ (and Java) source code with output in 6 formats.\n" |
| "Recognizes the following token classes: keyword, identifier, integer,\n" |
| "floating, string, character, operator, and preprocessor.\n\n", stderr); |
| fprintf(stderr, usage_str, basename(argv[0])); |
| fputs( |
| "\nCommand line options are:\n" |
| "-a : append to output file instead of create or overwrite.\n" |
| "-c : treat a # character as the start of a line comment.\n" |
| "-d : print debug info to stderr; implies -v.\n" |
| "-h : print just this text to stderr and stop.\n" |
| "-j : assume input is Java (deprecated: use -l Java or .java).\n" |
| "-k : output line and block comments as tokens.\n" |
| "-l<lang> : specify language explicitly (C, C++, Java).\n" |
| "-m<mode> : output mode either plain (default), csv, json, jsonl, xml, or raw.\n" |
| "-n : output newlines as a special pseudo token.\n" |
| "-N : output line continuations as a special pseudo token.\n" |
| "-o<file> : write output to this file (instead of stdout).\n" |
| "-r : suppress newline after each token in raw mode.\n" |
| "-s : enable a special start token specifying the filename.\n" |
| "-1 : treat all filename arguments as a continuous single input.\n" |
| "-v : print action summary to stderr.\n" |
| "-w : suppress all warning messages.\n" |
| "-W : output adjacent white-space as a token.\n", |
| stderr); |
| return 0; |
|
|
| case 'j': |
| source = set_or_detect_lang("Java"); |
| explicit_source = 1; |
| break; |
|
|
| case 'k': |
| comment_token = 1; |
| break; |
|
|
| case 'l': |
| source = set_or_detect_lang(optarg); |
| explicit_source = 1; |
| break; |
|
|
| case 'm': |
| if (!strcmp(optarg, "plain")) |
| mode = PLAIN; |
| else if (!strcmp(optarg, "csv")) |
| mode = CSV; |
| else if (!strcmp(optarg, "json")) |
| mode = JSON; |
| else if (!strcmp(optarg, "jsonl")) |
| mode = JSONL; |
| else if (!strcmp(optarg, "xml")) |
| mode = XML; |
| else if (!strcmp(optarg, "raw")) |
| mode = RAW; |
| else { |
| if (!nowarn) |
| fprintf(stderr, "(W): Invalid mode %s (using plain).\n", optarg); |
| mode = PLAIN; |
| } |
| break; |
|
|
| case 'n': |
| newline_token = 1; |
| break; |
|
|
| case 'N': |
| continuation_token = 1; |
| break; |
|
|
| case 'o': |
| outfile = optarg; |
| break; |
|
|
| case 'r': |
| suppress_newline = 1; |
| break; |
|
|
| case 's': |
| start_token = 1; |
| break; |
|
|
| case 'v': |
| verbose = 1; |
| break; |
|
|
| case 'w': |
| nowarn = 1; |
| break; |
|
|
| case 'W': |
| whitespace_token = 1; |
| break; |
|
|
| case '?': |
| default: |
| fputs("(F): unknown option. Stop.\n", stderr); |
| fprintf(stderr, usage_str, argv[0]); |
| return 1; |
| } |
| } |
|
|
| if (outfile && outfile[0]) { |
| if (!freopen(outfile, append ? "a" : "w", stdout)) { |
| fprintf(stderr, "(F): cannot open %s for writing.\n", outfile); |
| exit(3); |
| } |
| } |
|
|
| if (optind == argc) |
| goto doit; |
|
|
| do { |
| filename = argv[optind]; |
| if (!freopen(filename, "r", stdin)) { |
| if (!nowarn) |
| fprintf(stderr, "(W): Cannot read file %s.\n", filename); |
| continue; |
| } |
|
|
| if (!explicit_source) |
| source = set_or_detect_lang(0); |
|
|
| doit: |
| if (verbose) fprintf(stderr, "(I): Processing file %s...\n", filename); |
| num_files++; |
|
|
| |
| switch (mode) { |
| case RAW: |
| |
| break; |
| case PLAIN: |
| if (start_token) |
| fprintf(stdout, "( 0, 0) filename: %s\n", filename); |
| break; |
| case CSV: |
| if (!continuous_files || num_files == 1) |
| fputs("line,column,class,token\n", stdout); |
| if (start_token) |
| fprintf(stdout, "0,0,filename,\"%s\"\n", filename); |
| break; |
| case JSON: |
| case JSONL: |
| if (!continuous_files || num_files == 1) { |
| if (mode == JSON) fputs("[\n", stdout); |
| } |
| else { |
| if (mode == JSON) fputc(',', stdout); |
| fputc('\n', stdout); |
| first_time = 1; |
| } |
| if (start_token) { |
| fprintf(stdout, |
| "{ \"line\": 0, \"column\": 0, " |
| "\"class\": \"filename\", \"length\": %d, \"token\": \"%s\" }", |
| strlen(filename), filename); |
| first_time = 0; |
| } |
| break; |
| case XML: |
| if (!continuous_files || num_files == 1) { |
| fputs("<?xml version='1.0' encoding='UTF-8'?>\n", stdout); |
| |
| fputs("<tokens>\n", stdout); |
| } |
| if (start_token) { |
| fprintf(stdout, |
| "<token line='0' column='0' class='filename' length='%d'>", |
| strlen(filename)); |
| XML_escape(stdout, filename); |
| fputs("</token>\n", stdout); |
| } |
| break; |
| } |
|
|
| while ((token_len = C_tokenize_int(&token, &type, &line, &col, &pos))) { |
| switch (mode) { |
| case RAW: |
| fputs(token, stdout); |
| if (!suppress_newline) fputc('\n', stdout); |
| break; |
| case PLAIN: |
| fprintf(stdout, "(%4u,%3u;%6u:%3u) %s: %s\n", |
| line, col, pos, token_len, token_class[type], token); |
| break; |
| case CSV: |
| |
| |
| fprintf(stdout, "%u,%u,%s,", line, col, token_class[type]); |
| if (type == STRING || |
| |
| type == CHARACTER && (strchr(token, '"') || strchr(token, ',')) || |
| type == WHITESPACE && strchr(token, '\n') || |
| type == NEWLINE || |
| type == CONTINUATION || |
| comment_token && (type == LINE_COMMENT || type == BLOCK_COMMENT)) |
| CSV_escape(stdout, token); |
| else if (!strcmp(token, ",")) |
| fputs("\",\"", stdout); |
| else |
| fputs(token, stdout); |
| fputc('\n', stdout); |
| break; |
| case JSON: |
| case JSONL: |
| if (first_time) |
| first_time = 0; |
| else { |
| if (mode == JSON) fputc(',', stdout); |
| fputc('\n', stdout); |
| } |
| fprintf(stdout, |
| "{ \"line\": %u, \"column\": %u, " |
| "\"class\": \"%s\", \"length\": %u, \"token\": \"", |
| line, col, token_class[type], token_len); |
| |
| if (type == STRING || type == CHARACTER || |
| type == NEWLINE || type == WHITESPACE || |
| type == CONTINUATION) |
| JSON_escape(stdout, token); |
| else |
| fputs(token, stdout); |
| fputs("\" }", stdout); |
| break; |
| case XML: |
| fprintf(stdout, "<token line='%u' column='%u' class='%s' length='%u'>", |
| line, col, token_class[type], token_len); |
| if (type == STRING || |
| type == CHARACTER || |
| type == OPERATOR || |
| comment_token && (type == LINE_COMMENT || |
| type == BLOCK_COMMENT)) |
| XML_escape(stdout, token); |
| else |
| fputs(token, stdout); |
| fputs("</token>\n", stdout); |
| break; |
| } |
| } |
|
|
| if (!continuous_files) { |
| |
| switch (mode) { |
| case RAW: |
| break; |
| case PLAIN: |
| break; |
| case CSV: |
| break; |
| case JSON: |
| fputs("\n]", stdout); |
| |
| case JSONL: |
| fputc('\n', stdout); |
| break; |
| case XML: |
| fputs("</tokens>\n", stdout); |
| break; |
| } |
|
|
| if (verbose) |
| fprintf (stderr, "(I): %u bytes, %u UTF-8 encoded chars.\n", |
| char_count, utf8_count); |
|
|
| |
| char_count = 0; |
| utf8_count = 0; |
| linenr = 1; |
| column = 0; |
| buffered = 0; |
| saved_col = 0; |
| first_time = 1; |
| } |
| } while (++optind < argc); |
|
|
| if (continuous_files) { |
| |
| switch (mode) { |
| case RAW: |
| break; |
| case PLAIN: |
| break; |
| case CSV: |
| break; |
| case JSON: |
| fputs("\n]", stdout); |
| |
| case JSONL: |
| fputc('\n', stdout); |
| break; |
| case XML: |
| fputs("</tokens>\n", stdout); |
| break; |
| } |
|
|
| if (verbose) |
| fprintf(stderr, "(I): %u bytes, %u (UTF-8 encoded) unicode characters.\n", |
| char_count, utf8_count); |
| } |
|
|
| if (num_files > 1 && verbose) |
| fprintf(stderr, "(I): Total number of files processed: %u\n", num_files); |
|
|
| return (illegals || unexpect_eof) ? 1 : 0; |
| } |
|
|