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