diff --git a/.gitignore b/.gitignore index b00763b..399b094 100644 --- a/.gitignore +++ b/.gitignore @@ -17,10 +17,9 @@ src/Makefile src/Makefile.in src/sermon src/sermon_lexer.c -src/sermon_lexer.o src/sermon_parser.c src/sermon_parser.h -src/sermon_parser.o -src/sermon_util.o stamp-h1 ylwrap +*.o +*.core diff --git a/src/Makefile.am b/src/Makefile.am index 4e1fdb1..21c4114 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,6 @@ bin_PROGRAMS = sermon BUILT_SOURCES = sermon_lexer.c sermon_parser.c sermon_parser.h AM_YFLAGS = -d --location -sermon_SOURCES = sermon_lexer.l sermon_parser.y sermon_util.c +sermon_SOURCES = sermon_lexer.l sermon_parser.y sermon_util.c main.c CLEANFILES = sermon_lexer.c sermon_parser.c sermon_parser.h LIBS = $(LEXLIB) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..7a49045 --- /dev/null +++ b/src/main.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include "sermon.h" + +extern int yyparse(Sermon *); +extern FILE* yyin; + +void usage(const char* progname) { + fprintf(stderr, "Usage: %s [-h] FILE\n" + "\n" + " -h Display help message\n" + "\n" + " FILE sermon file to scan\n", progname); +} + +int main(int argc, char* argv[]) { + Sermon sermon; + int i = 0, block = 0, normal = 0; + const char* progname = argv[0], *filename = NULL; + while (++i < argc) { + if (strcmp(argv[i], "-h") == 0) { usage(progname); exit(0); } + else if ((argv[i][0] == '-') && (argv[i][1] != '\0')) { + fprintf(stderr, "Unknown option: %s\n", argv[i]); + } + else { + filename = argv[i]; + } + } + if (!filename) { + usage(progname); + exit(1); + } + InitSermon(&sermon); + if (strcmp(filename, "-") == 0) { + yyin = stdin; + } else { + yyin = fopen(argv[1], "rt"); + } + yyparse(&sermon); + printf("Parsed sermon.\n"); + printf("TITLE=%s\n", sermon.sermonTitle ? sermon.sermonTitle : "none"); + printf("AUTHOR=%s\n", sermon.sermonAuthor ? sermon.sermonAuthor : "none"); + printf("DATE=%s\n", sermon.sermonDate ? sermon.sermonDate : "none"); + printf("OCCASION=%s\n", sermon.sermonOccasion ? sermon.sermonOccasion : "none"); + printf("TEXT=%s\n", sermon.sermonText ? sermon.sermonText : "none"); + printf("\nThere are %d paragraphs", sermon.numParagraphs); + for (i = 0; i < sermon.numParagraphs; i++) { + if (sermon.sermonParagraphs[i].paraType == PARA_DEFAULT) normal++; + else if (sermon.sermonParagraphs[i].paraType == PARA_BLOCKQUOTE) block++; + } + printf(" (%d regular, %d blockquote)\n", normal, block); + printf("\nThere are %d references.\n", sermon.numReferences); + for (i = 0; i < sermon.numReferences; i++) { + printf(" - %s: %s\n", sermon.sermonReferences[i].refId, sermon.sermonReferences[i].refText); + } + printf("\n"); + FreeSermon(&sermon); + if (strcmp(filename, "-") != 0) { + fclose(yyin); + } +} + diff --git a/src/sermon_parser.y b/src/sermon_parser.y index 325143f..3585c7e 100644 --- a/src/sermon_parser.y +++ b/src/sermon_parser.y @@ -130,33 +130,6 @@ reference: ; %% -int main(int argc, char* argv[]) { - Sermon sermon; - int i = 0, block = 0, normal = 0; - InitSermon(&sermon); - yyin = fopen(argv[1], "rt"); - yyparse(&sermon); - printf("Parsed sermon.\n"); - printf("TITLE=%s\n", sermon.sermonTitle ? sermon.sermonTitle : "none"); - printf("AUTHOR=%s\n", sermon.sermonAuthor ? sermon.sermonAuthor : "none"); - printf("DATE=%s\n", sermon.sermonDate ? sermon.sermonDate : "none"); - printf("OCCASION=%s\n", sermon.sermonOccasion ? sermon.sermonOccasion : "none"); - printf("TEXT=%s\n", sermon.sermonText ? sermon.sermonText : "none"); - printf("\nThere are %d paragraphs", sermon.numParagraphs); - for (i = 0; i < sermon.numParagraphs; i++) { - if (sermon.sermonParagraphs[i].paraType == PARA_DEFAULT) normal++; - else if (sermon.sermonParagraphs[i].paraType == PARA_BLOCKQUOTE) block++; - } - printf(" (%d regular, %d blockquote)\n", normal, block); - printf("\nThere are %d references.\n", sermon.numReferences); - for (i = 0; i < sermon.numReferences; i++) { - printf(" - %s: %s\n", sermon.sermonReferences[i].refId, sermon.sermonReferences[i].refText); - } - printf("\n"); - FreeSermon(&sermon); - fclose(yyin); -} - void yyerror(Sermon* s, const char* msg) { fprintf(stderr, "Parse error (%d:%d): %s - \"%s\"\n", yylloc.first_line, yylloc.first_column, msg, yytext); exit(1);