2022-02-10 20:30:11 -08:00
|
|
|
/** @license 2022 Neil Edelman, distributed under the terms of the
|
|
|
|
[MIT License](https://opensource.org/licenses/MIT).
|
|
|
|
|
|
|
|
Lexer for journal entries.
|
|
|
|
|
|
|
|
@std C89/90 */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2022-02-12 22:17:45 -08:00
|
|
|
/*!re2c
|
|
|
|
re2c:yyfill:enable = 0;
|
|
|
|
re2c:define:YYCTYPE = char;
|
|
|
|
*/
|
|
|
|
|
2022-02-11 01:46:41 -08:00
|
|
|
static int looks_like_year(const char *const a, int *const year) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
assert(a && year);
|
|
|
|
/*!re2c
|
|
|
|
@s0 ("-"? [1-9][0-9]* | "0") "\x00" {
|
|
|
|
int sign = 1, mag;
|
|
|
|
if(*s0 == '-') { sign = -1; s0++; }
|
|
|
|
for(mag = 0; *s0 != '\0'; s0++) {
|
|
|
|
int d = *s0 - '0';
|
2022-02-12 22:17:45 -08:00
|
|
|
if((INT_MAX - d) / 10 < mag) return 0;
|
2022-02-11 01:46:41 -08:00
|
|
|
mag = mag * 10 + d;
|
|
|
|
}
|
|
|
|
*year = sign * mag;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-02-12 22:17:45 -08:00
|
|
|
static int looks_like_month(const char *const a) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
assert(a);
|
|
|
|
/*!re2c
|
|
|
|
@s0 [0-1][0-9] "\x00" {
|
|
|
|
int val = 10 * (s0[0] - '0') + (s0[1] - '0');
|
|
|
|
return val < 1 || val > 12 ? 0 : val;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static int looks_like_day(const char *const a) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
assert(a);
|
|
|
|
/*!re2c
|
|
|
|
@s0 [0-3][0-9] ".txt\x00" {
|
|
|
|
int val = 10 * (s0[0] - '0') + (s0[1] - '0');
|
|
|
|
return val < 1 || val > 31 ? 0 : val;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:30:11 -08:00
|
|
|
/* This defines `enum condition`. */
|
|
|
|
/*!types:re2c*/
|
2022-02-11 02:00:14 -08:00
|
|
|
enum symbol { END, TEXT, BANG, BRACKET, WHITE, MAP };
|
2022-02-10 20:30:11 -08:00
|
|
|
|
|
|
|
/** scanner reads a file and extracts semantic information. Valid to access
|
|
|
|
only while underlying pointers do not change. */
|
|
|
|
struct scanner {
|
|
|
|
/* `re2c` variables; these point directly into `buffer`. */
|
|
|
|
const char *marker, *ctx_marker, *from, *cursor;
|
|
|
|
/* Weird `c2re` stuff: these fields have to come after when >5? */
|
|
|
|
const char *label, *buffer, *s0, *s1;
|
|
|
|
enum condition condition;
|
|
|
|
enum symbol symbol;
|
|
|
|
size_t line;
|
|
|
|
int ws_before;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*!re2c
|
2022-02-12 22:17:45 -08:00
|
|
|
re2c:flags:tags = 1;
|
2022-02-10 20:30:11 -08:00
|
|
|
re2c:define:YYCURSOR = s->cursor;
|
|
|
|
re2c:define:YYMARKER = s->marker;
|
|
|
|
re2c:define:YYCTXMARKER = s->ctx_marker;
|
|
|
|
re2c:define:YYCONDTYPE = 'condition';
|
|
|
|
re2c:define:YYGETCONDITION = 's->condition';
|
|
|
|
re2c:define:YYGETCONDITION:naked = 1;
|
|
|
|
re2c:define:YYSETCONDITION = 's->condition = @@;';
|
|
|
|
re2c:define:YYSETCONDITION:naked = 1;
|
|
|
|
|
|
|
|
// Eof is marked by null when preparing files for lexing.
|
|
|
|
// Mutually exclusive; only !, [, are not covered.
|
|
|
|
end = "\x00";
|
|
|
|
newline = "\n" | "\r" "\n"?;
|
|
|
|
ws = [ \t\v\f];
|
|
|
|
glyph = [^ \t\n\r\v\f![\x00];
|
|
|
|
glyphs = glyph+;
|
|
|
|
|
|
|
|
// inside the block
|
|
|
|
decimal = [1-9][0-9]*;
|
|
|
|
number = ([1-9][0-9]* | [0])? "." [0-9]+ | [1-9][0-9]* | [0];
|
|
|
|
id = [a-zA-Z_][a-zA-Z_\-0-9]{0,63};
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int lex(struct scanner *const s) {
|
|
|
|
const char *s0, *s1;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
s->ws_before = 0;
|
|
|
|
scan:
|
|
|
|
/*!re2c
|
|
|
|
<text> end { return s->symbol = END, 1; }
|
|
|
|
// fixme: paragraphs.
|
|
|
|
<text> newline { s->line++; s->ws_before = 1; goto scan; }
|
|
|
|
<text> ws+ { s->ws_before = 1; goto scan; }
|
|
|
|
<text> @s0 glyph+ @s1 { s->s0 = s0, s->s1 = s1;
|
|
|
|
return s->symbol = TEXT, 1; }
|
|
|
|
<text> @s0 "!" @s1 { s->s0 = s0, s->s1 = s1;
|
|
|
|
return s->symbol = BANG, 1; }
|
2022-02-11 02:00:14 -08:00
|
|
|
<text> "\\" @s0 "[" @s1 { s->s0 = s0, s->s1 = s1;
|
|
|
|
return s->symbol = BRACKET, 1; }
|
2022-02-10 20:30:11 -08:00
|
|
|
<text> "" {
|
|
|
|
s->condition = yyctext;
|
|
|
|
s->s0 = s0, s->s1 = s1;
|
|
|
|
printf("Got a map.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
<command> * { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-02-11 01:46:41 -08:00
|
|
|
#if INT_MAX >= 100000000000
|
|
|
|
#error int_to_string requires truncation on this compiler.
|
|
|
|
#endif
|
|
|
|
static void int_to_string(const int *const n,
|
|
|
|
char (*const a)[12]) { sprintf(*a, "%d", *n); }
|
|
|
|
#define ARRAY_NAME int
|
|
|
|
#define ARRAY_TYPE int
|
|
|
|
#define ARRAY_EXPECT_TRAIT
|
|
|
|
#include "../src/array.h"
|
|
|
|
#define ARRAY_TO_STRING &int_to_string
|
|
|
|
#include "../src/array.h"
|
|
|
|
static int int_cmp(const int *const a, const int *const b)
|
|
|
|
{ return (*b < *a) - (*a < *b); }
|
|
|
|
static int void_int_cmp(const void *const a, const void *const b)
|
|
|
|
{ return int_cmp(a, b); }
|
2022-02-10 20:30:11 -08:00
|
|
|
|
2022-02-11 00:28:28 -08:00
|
|
|
#include <unistd.h> /* chdir (POSIX) */
|
|
|
|
#include <sys/types.h> /* mode_t (POSIX) */
|
|
|
|
#include <sys/stat.h> /* umask (POSIX) */
|
|
|
|
#include <dirent.h> /* opendir readdir closedir */
|
|
|
|
#include <limits.h>
|
2022-02-10 20:30:11 -08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
int success = EXIT_FAILURE;
|
2022-02-12 22:17:45 -08:00
|
|
|
DIR *dir = 0;
|
|
|
|
struct dirent *de;
|
|
|
|
struct int_array years = ARRAY_IDLE, months = ARRAY_IDLE, days = ARRAY_IDLE;
|
|
|
|
int *y, *y_end;
|
2022-02-11 00:28:28 -08:00
|
|
|
|
2022-02-11 01:46:41 -08:00
|
|
|
/* Get the years list as directories matching a year in order. */
|
2022-02-11 00:28:28 -08:00
|
|
|
errno = 0;
|
2022-02-10 20:30:11 -08:00
|
|
|
if(argc != 2) { fprintf(stderr, "Needs journal location.\n"
|
|
|
|
"(should contain <year>/<month>/<day>.txt)\n"); goto finally; }
|
2022-02-12 22:17:45 -08:00
|
|
|
if(chdir(argv[1]) == -1 || !(dir = opendir("."))) goto catch;
|
|
|
|
while((de = readdir(dir))) {
|
2022-02-11 00:28:28 -08:00
|
|
|
struct stat st;
|
2022-02-11 01:46:41 -08:00
|
|
|
int year, *p;
|
2022-02-12 22:17:45 -08:00
|
|
|
if(!looks_like_year(de->d_name, &year)) continue;
|
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
2022-02-11 01:46:41 -08:00
|
|
|
if(!S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&years))) goto catch;
|
|
|
|
*p = year;
|
2022-02-11 00:28:28 -08:00
|
|
|
}
|
2022-02-12 22:17:45 -08:00
|
|
|
closedir(dir), dir = 0;
|
2022-02-11 01:46:41 -08:00
|
|
|
qsort(years.data, years.size, sizeof *years.data, &void_int_cmp);
|
2022-02-12 22:17:45 -08:00
|
|
|
fprintf(stderr, "%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++) {
|
|
|
|
char temp[64];
|
|
|
|
int *m, *m_end;
|
|
|
|
sprintf(temp, "%d", *y);
|
|
|
|
|
|
|
|
/* Get the months as directories. */
|
|
|
|
if(chdir(temp) == -1 || !(dir = opendir("."))) goto catch;
|
|
|
|
while((de = readdir(dir))) {
|
|
|
|
struct stat st;
|
|
|
|
int month, *p;
|
|
|
|
if(!(month = looks_like_month(de->d_name))) continue;
|
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
|
|
|
if(!S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&months))) goto catch;
|
|
|
|
*p = month;
|
|
|
|
}
|
|
|
|
closedir(dir), dir = 0;
|
|
|
|
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
|
|
|
fprintf(stderr, "%s: %s.\n", temp, 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(temp, "%.2d", *m);
|
|
|
|
|
|
|
|
/* Get the days as files. */
|
|
|
|
if(chdir(temp) == -1 || !(dir = opendir("."))) goto catch;
|
|
|
|
while((de = readdir(dir))) {
|
|
|
|
struct stat st;
|
|
|
|
int day, *p;
|
|
|
|
/* fixme: Have yyyy-mm-dd to figure out how many days. */
|
|
|
|
if(!(day = looks_like_day(de->d_name))) continue;
|
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
|
|
|
if(S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&days))) goto catch;
|
|
|
|
*p = day;
|
|
|
|
}
|
|
|
|
closedir(dir), dir = 0;
|
|
|
|
qsort(days.data, days.size, sizeof *days.data, &void_int_cmp);
|
|
|
|
fprintf(stderr, "%s: %s.\n", temp, int_array_to_string(&days));
|
|
|
|
|
|
|
|
for(d = days.data, d_end = d + days.size; d < d_end; d++) {
|
|
|
|
printf("%d-%.2d-%.2d\n", *y, *m, *d);
|
|
|
|
}
|
2022-02-10 20:30:11 -08:00
|
|
|
|
2022-02-12 22:17:45 -08:00
|
|
|
int_array_clear(&days);
|
|
|
|
if(chdir("..") == -1) goto catch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int_array_clear(&months);
|
|
|
|
if(chdir("..") == -1) goto catch;
|
|
|
|
}
|
2022-02-10 20:30:11 -08:00
|
|
|
{ success = EXIT_SUCCESS; goto finally; }
|
|
|
|
catch:
|
|
|
|
perror("interpret");
|
|
|
|
finally:
|
2022-02-12 22:17:45 -08:00
|
|
|
if(dir) closedir(dir);
|
2022-02-11 01:46:41 -08:00
|
|
|
int_array_(&years);
|
2022-02-12 22:17:45 -08:00
|
|
|
int_array_(&months);
|
2022-02-10 20:30:11 -08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|