interpret/src/driver.c

103 lines
2.5 KiB
C
Raw Normal View History

2023-04-22 03:11:57 +00:00
/** @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"
2023-04-23 07:20:50 +00:00
#include "scan.h"
2023-04-22 03:11:57 +00:00
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h> /* getcwd, chdir */
2023-04-22 03:11:57 +00:00
int main(void) {
2023-04-22 03:49:36 +00:00
const char *intent = "start";
2023-04-22 03:11:57 +00:00
struct journal jrnl = {0};
2023-04-23 07:20:50 +00:00
struct scan scn = {0};
int showhelp = 1;
FILE *fpwhere = 0;
char cwd[PATH_MAX], jdir[PATH_MAX];
2023-04-22 03:11:57 +00:00
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();
2023-04-22 03:11:57 +00:00
fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl));
if(errno) goto catch;
intent = cwd;
if(chdir(cwd)) goto catch;
intent = "parse";
2023-04-23 07:20:50 +00:00
scn = scan(&jrnl);
2023-04-26 07:59:14 +00:00
//fprintf(stderr, "Scan: %s.\n", scan_to_string(&scrs));
// <- Not sure what that would do.
2023-04-23 07:20:50 +00:00
if(errno) goto catch;
intent = "score.gnu";
2023-04-26 07:59:14 +00:00
if(!freopen(intent, "w", stdout)) goto catch;
scan_score_graph(&scn);
intent = "glider.gnu";
2023-04-26 07:59:14 +00:00
if(!freopen(intent, "w", stdout)) goto catch;
scan_glider_graph(&scn);
intent = "flight.gnu";
2023-04-28 20:40:11 +00:00
if(!freopen(intent, "w", stdout)) goto catch;
scan_flight_graph(&scn);
intent = "kjv.gnu";
2023-04-28 23:18:58 +00:00
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;
2023-04-22 03:11:57 +00:00
goto finally;
catch:
2023-04-22 03:49:36 +00:00
perror(intent);
2023-04-22 03:11:57 +00:00
finally:
2023-05-08 04:44:14 +00:00
/* 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);
2023-04-23 07:20:50 +00:00
scan_(&scn);
2023-04-22 03:11:57 +00:00
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");
2023-04-22 03:49:36 +00:00
return intent ? EXIT_FAILURE : EXIT_SUCCESS;
2023-04-22 03:11:57 +00:00
}