Separate main function, parse command-line options

This commit is contained in:
David Baer
2015-08-05 23:27:39 -04:00
parent e9c9d0bda3
commit 3c2f50078e
4 changed files with 66 additions and 31 deletions

5
.gitignore vendored
View File

@@ -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

View File

@@ -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)

63
src/main.c Normal file
View File

@@ -0,0 +1,63 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}
}

View File

@@ -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);