More useful errors.

This commit is contained in:
Neil 2022-07-05 20:48:40 -07:00
parent debffaf7a6
commit 2b320eb040

View File

@ -128,6 +128,7 @@ struct source { char *key, *desc; };
int main(int argc, char **argv) {
int success = EXIT_SUCCESS;
char *intent = 0;
/* For reading in files, overwritten. */
DIR *dir = 0;
@ -139,8 +140,8 @@ int main(int argc, char **argv) {
struct page_tree journal = page_tree();
errno = 0;
if(argc != 2) { fprintf(stderr, "Needs journal location.\n"
"(should contain <year>/<month>/<day>.txt)\n"); goto finally; }
if(argc != 2) { intent = "needs journal location, which should"
" contain <year>/<month>/<day>.txt"; goto catch; }
/* Get the years list as directories matching a year. */
if(chdir(argv[1]) == -1 || !(dir = opendir("."))) goto catch;
@ -156,7 +157,7 @@ int main(int argc, char **argv) {
closedir(dir), dir = 0;
/* Sort the years for sensible ordering of parsing. */
qsort(years.data, years.size, sizeof *years.data, &void_int_cmp);
fprintf(stderr, "Files in %s: %s.\n", argv[1], int_array_to_string(&years));
fprintf(stderr, "(Files in %s: %s.)\n", argv[1], int_array_to_string(&years));
/* Go though each year. */
for(y = years.data, y_end = y + years.size; y < y_end; y++) {
@ -177,14 +178,13 @@ int main(int argc, char **argv) {
}
closedir(dir), dir = 0;
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
fprintf(stderr, "Files in %s: %s.\n",
fprintf(stderr, "(Files in %s: %s.)\n",
fn, int_array_to_string(&months));
/* Go though each month. */
for(m = months.data, m_end = m + months.size; m < m_end; m++) {
int *d, *d_end;
sprintf(fn, "%.2d", *m);
printf("month %s\n", fn);
/* Get the days as files. */
if(chdir(fn) == -1 || !(dir = opendir("."))) goto catch;
@ -200,48 +200,53 @@ int main(int argc, char **argv) {
}
closedir(dir), dir = 0;
qsort(days.data, days.size, sizeof *days.data, &void_int_cmp);
fprintf(stderr, "Files in %s: %s.\n",
fprintf(stderr, "(Files in %s: %s.)\n",
fn, int_array_to_string(&days));
for(d = days.data, d_end = d + days.size; d < d_end; d++) {
union date32 d32;
struct lex *lex = 0;
struct page *page = 0;
printf("Date: %d-%.2d-%.2d\n", *y, *m, *d);
if(!(d32 = date_to_32(*y, *m, *d)).year)
{ errno = EILSEQ; goto catch; }
union date32 d32;
if(!(d32 = date_to_32(*y, *m, *d)).year) { errno = EILSEQ;
intent = "date parse error"; goto syntax; }
sprintf(fn, "%.2d.txt", *d);
if(page_tree_bulk_add(&journal, d32, &page) != TREE_UNIQUE) {
if(!errno) fprintf(stderr, "Not unique?\n"), errno = EDOM;
goto catch;
if(!errno) intent = "not unique", errno = EDOM;
goto syntax;
}
page->entry = char_array();
page->lexx = lex_array();
if(!append_file(&page->entry, fn)) goto catch;
printf("%s", page->entry.data);
printf("Lexing:\n");
if(!append_file(&page->entry, fn)) goto syntax;
for(lex_reset(page->entry.data); ; ) {
struct lex *lex;
if(!(lex = lex_array_new(&page->lexx))) goto catch;
if(!(lex = lex_array_new(&page->lexx))) goto syntax;
if(!lex_next(lex)) {
if(lex->symbol != END) { errno = EILSEQ; goto catch; }
printf("Lexing finished: %s on %lu.\n",
lex_symbols[lex->symbol], lex->line);
if(lex->symbol != END) { errno = EILSEQ; goto syntax; }
/*printf("Lexing finished: %s on %lu.\n",
lex_symbols[lex->symbol], lex->line);*/
break;
}
printf("%lu: %s",
(unsigned long)lex->line, lex_symbols[lex->symbol]);
if(lex->symbol == TEXT || lex->symbol == ARG_KEYWORD
|| lex->symbol == ARG_DATE
|| lex->symbol == ARG_FREEFORM
|| lex->symbol == CAPTION) {
if(lex->s0 + INT_MAX < lex->s1)
{ errno = EILSEQ; goto catch; }
printf(" <<%.*s>>", (int)(lex->s1 - lex->s0), lex->s0);
}
printf(".\n");
}
/* Transfer the temporary page to the journal. */
page = 0;
continue;
syntax:
fprintf(stderr, "On date: %d-%.2d-%.2d.\n", *y, *m, *d);
if(!page) goto catch;
printf("While parsing <<<\n%s>>>\n", page->entry.data);
if(!lex) goto catch;
printf("On line %lu: %s",
(unsigned long)lex->line, lex_symbols[lex->symbol]);
if(lex->symbol == TEXT || lex->symbol == ARG_KEYWORD
|| lex->symbol == ARG_DATE
|| lex->symbol == ARG_FREEFORM
|| lex->symbol == CAPTION
|| lex->symbol == BIBLE_TEXT) {
if(lex->s0 + INT_MAX < lex->s1)
{ intent = "line too long"; errno = EILSEQ; }
printf(" <<%.*s>>", (int)(lex->s1 - lex->s0), lex->s0);
}
printf(".\n");
goto catch;
}
int_array_clear(&days);
@ -250,19 +255,19 @@ int main(int argc, char **argv) {
int_array_clear(&months);
if(chdir("..") == -1) goto catch;
break; /* fixme */
//break; /* fixme */
}
page_tree_bulk_finish(&journal);
int_array_(&years), int_array_(&months), int_array_(&days);
printf("Journal has entries: %s\n", page_tree_to_string(&journal));
goto finally;
catch:
success = EXIT_FAILURE;
perror("interpret");
if(intent) fprintf(stderr, "Further explanation: %s.\n", intent);
finally:
if(dir && closedir(dir)) success = EXIT_FAILURE, perror("dir");
int_array_(&years);
int_array_(&months);
int_array_(&days);
int_array_(&years), int_array_(&months), int_array_(&days);
struct page_tree_entry entry;
for(struct page_tree_iterator it = page_tree_begin(&journal);
(entry = page_tree_next(&it)).key; ) {