interpret/src/driver.c

107 lines
2.6 KiB
C

/** @license 2023 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
Scan source files for meanings.
@std C99 */
#include "journal.h"
#include "scan.h"
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h> /* getcwd, chdir */
int main(void) {
const char *intent = "start";
struct journal jrnl = {0};
struct scan scn = {0};
int showhelp = 1;
FILE *fpwhere = 0;
char cwd[PATH_MAX], jdir[PATH_MAX];
errno = 0;
intent = "current directory";
if(!getcwd(cwd, sizeof(cwd))) goto catch;
intent = "interpret";
if(!(fpwhere = fopen(intent, "r"))) goto catch;
if(!fgets(jdir, sizeof jdir, fpwhere)) {
if(!errno) errno = EDOM;
fprintf(stderr, "first line error\n");
goto catch;
}
if(fgetc(fpwhere) != EOF) {
fprintf(stderr, "expected eof\n");
errno = EDOM; goto catch;
}
/* Could be something other than EOF. */
if(errno || (fclose(fpwhere) == EOF && (fpwhere = 0, 1))) goto catch;
fpwhere = 0;
jdir[strcspn(jdir, "\n")] = '\0'; /* Strip. */
if(chdir(jdir)) {
fprintf(stderr, "while switching to directory: %s\n", jdir);
goto catch;
}
showhelp = 0;
intent = "journal";
jrnl = journal();
fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl));
if(errno) goto catch;
intent = cwd;
if(chdir(cwd)) goto catch;
intent = "parse";
scn = scan(&jrnl);
//fprintf(stderr, "Scan: %s.\n", scan_to_string(&scrs));
// <- Not sure what that would do.
if(errno) goto catch;
intent = "score.gnu";
if(!freopen(intent, "w", stdout)) goto catch;
scan_score_graph(&scn);
intent = "labs.csv";
if(!freopen(intent, "w", stdout)) goto catch;
scan_labs_graph(&scn);
intent = "glider.gnu";
if(!freopen(intent, "w", stdout)) goto catch;
scan_glider_graph(&scn);
intent = "flight.gnu";
if(!freopen(intent, "w", stdout)) goto catch;
scan_flight_graph(&scn);
intent = "kjv.gnu";
if(!freopen(intent, "w", stdout)) goto catch;
scan_kjv_graph(&scn);
intent = "dream.gnu";
if(!freopen(intent, "w", stdout)) goto catch;
scan_dream_graph(&scn);
intent = 0;
goto finally;
catch:
perror(intent);
finally:
/* fixme: ~scan should be idempotent but it's not on disabling ASLR, which
debug mode is in. */
if(fpwhere && fclose(fpwhere) == EOF)
intent = "interpret", perror(intent);
scan_(&scn);
journal_(&jrnl);
if(intent && showhelp)
fprintf(stderr, "\nMeant to be run in a directory with a file called "
"\"interpret\".\n"
"The contents of that file shall be a directory wherein\n"
"<year>/<month>/<day>.txt, eg 2000/12/01.txt.\n"
"Loads all journal entries and parses them, outputting to the "
"current directory.\n\n");
return intent ? EXIT_FAILURE : EXIT_SUCCESS;
}