/** @license 2022 Neil Edelman, distributed under the terms of the
 [MIT License](https://opensource.org/licenses/MIT).

 Scan journal. */

#include "../src/journal.h"
#include "../src/scan.h"
#include <stdio.h>
#include <assert.h>
#include <inttypes.h>

/* Tree mapping from date-line in the journal to indices in whatever array. */
static void linemap_to_string(const union line64 line, const size_t *const u,
	char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int linemap_compare(const union line64 a, const union line64 b)
	{ return a.u64 > b.u64; }
#define TREE_NAME linemap
#define TREE_KEY union line64
#define TREE_VALUE size_t
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_DEFAULT 0
#define TREE_BODY
#include "../src/tree.h"

/** `p` to `z`. */
static void pair_to_string(const struct pair *const p,
	char (*const z)[12]) {
	const char *a = p->a, *b;
	char *y = *z;
	b = p->b <= a + 11 ? p->b : a + 11;
	while(a < b) *(y++) = *(a++);
	*y = '\0';
}

/* Array of sources. */
static void source_to_string(const struct source *const s,
	char (*const z)[12]) { pair_to_string(&s->name, z); }
#define ARRAY_NAME source
#define ARRAY_TYPE struct source
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"

/* Array of places. */
static void place_to_string(const struct place *const p,
	char (*const z)[12]) { pair_to_string(&p->name, z); }
#define ARRAY_NAME place
#define ARRAY_TYPE struct place
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"

/* Array of scores. */
static void score_to_string(const struct score *const s,
	char (*const z)[12]) { pair_to_string(&s->name, z); }
#define ARRAY_NAME score
#define ARRAY_TYPE struct score
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"

/* Array of edits. */
static void edit_to_string(const struct edit *const e,
	char (*const z)[12]) { pair_to_string(&e->desc, z); }
#define ARRAY_NAME edit
#define ARRAY_TYPE struct edit
#define ARRAY_TO_STRING
#define ARRAY_BODY
#include "../src/array.h"

/* Glider tree. */
static void glider_to_string(const union line64 line, const struct glider *g,
	char (*const a)[12]) { (void)g; date32_to_string(line.date, a); }
static int glider_compare(const union line64 a, const union line64 b)
	{ return a.u64 > b.u64; }
#define TREE_NAME glider
#define TREE_KEY union line64
#define TREE_VALUE struct glider
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"

/* Flight tree. */
static void flight_to_string(const union line64 line, const struct flight *f,
	char (*const a)[12]) { (void)f; date32_to_string(line.date, a); }
static int flight_compare(const union line64 a, const union line64 b)
	{ return a.u64 > b.u64; }
#define TREE_NAME flight
#define TREE_KEY union line64
#define TREE_VALUE struct flight
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"

/* KJV tree. */
static void kjv_to_string(const union line64 line, const struct kjvrange *u,
	char (*const a)[12]) { (void)u; date32_to_string(line.date, a); }
static int kjv_compare(const union line64 a, const union line64 b)
	{ return a.u64 > b.u64; }
#define TREE_NAME kjv
#define TREE_KEY union line64
#define TREE_VALUE struct kjvrange
#define TREE_COMPARE
#define TREE_TO_STRING
#define TREE_BODY
#include "../src/tree.h"


/*!conditions:re2c*/

static int scan_day(struct scan *const scan, union date32 date,
	const char *const buffer) {
	const char *YYCURSOR, *YYMARKER, *yyt1, *yyt2, *yyt3, *s0, *s1, *t0, *t1;
	enum YYCONDTYPE condition = yycline;
	size_t line = 1;
	char datestr[12] = {0};
	const char *fail = "perhaps a bat?";
	struct pair *future = 0;
	struct score *new_score = 0;
	struct glider *new_glider = 0;
	struct flight *new_flight = 0;
	enum kjv_book book = Revelation;
	uint32_t chapter = 0, verse = 0, verse_end = 0;

	assert(scan && date.u32 && buffer);
	YYCURSOR = YYMARKER = yyt1 = buffer;
	date32_to_string(date, &datestr);

	/*!re2c /**/
	re2c:define:YYCTYPE = char;
	re2c:yyfill:enable = 0;
	re2c:define:YYGETCONDITION = "condition";
	re2c:define:YYSETCONDITION = "condition = @@;";
	re2c:define:YYGETCONDITION:naked = 1;
	re2c:define:YYSETCONDITION:naked = 1;

	ws = [ \t];
	glyph = [^\x00-\x20\x7f] | [\x80-\xff]; // [^\x00\n\t ] + all weird
	semichar = glyph \ ";";
	brachar = semichar \ [[\]];
	parchar = glyph \ [()];

	// label: possibly separated by spaces; used in freestanding last
	anylabel = glyph+ (ws+ glyph+)*;
	// label except ";"; used in freestanding
	semilabel = semichar+ (ws+ semichar+)*;
	// label except ";[]"; used in []
	bralabel = brachar+ (ws+ brachar+)*;
	// label except start "()"; used in location
	parlabel = (parchar glyph*) (ws+ glyph+)*;

	keyword = [A-Za-z0-9][A-Za-z0-9_-]*;

	natural = [1-9][0-9]*;
	zero_natural = [0-9]+;
	decimal = "-"? zero_natural ("." zero_natural?)?;
	uint = [0-9]+;
	date = natural "-" [0-1][0-9] "-" [0-3][0-9];
	minutes = [0-5][0-9];
	airport = [A-Z0-9]{4,4};

	kjvlookat = ws* natural ":" natural [ab]? ("-" natural [ab]?)? ws+ "--" ws+;
	first = ("I" | "1") " "?;
	second = ("II" | "2") " "?;
	third = ("III" | "3") " "?;
	*/
	for( ; ; ) {
		/*!re2c /**/
		<skip> [^\n\x00] { continue; } /* Default ignore. */
		<skip> "\x00" { fail = "no newline at end of file"; goto catch; }
		<line> "\x00" { return 1; } /* End of day. */
		<line, skip> "\n" => line { line++; continue; }
		<line> * :=> skip
		<line> "->" :=> place
		<line> "--" / [^-] :=> source
		<line> "::" / [^:] :=> score
		<line> "[glider]" :=> glider_type
		<line> "[flight]" :=> flight_type
		<line> "[" :=> bracket

		/* ^"[" ... */
		<bracket> * { fail = "bracket unrecognized"; goto catch; }
		<bracket> "document: " :=> document
		<bracket> "ed: " :=> edit //change to "rem"

		<document> * { fail = "document title"; goto catch; }
		<document> @s0 bralabel @s1 "]" => future {
			const union line64 key = { { (uint32_t)line, date } };
			size_t *pi;
			struct source *doc;
			if(!(doc = source_array_new(&scan->documents.array))) goto catch;
			doc->name.a = s0, doc->name.b = s1;
			doc->desc.a = 0, doc->desc.b = 0;
			assert(!future), future = &doc->desc;
			switch(linemap_tree_bulk_assign(&scan->documents.dates, key, &pi)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT:
				*pi = (size_t)(doc - scan->documents.array.data); break;
			}
			fprintf(stderr, "%s[%zu]: new document <<%.*s>> stored at %zu.\n",
				datestr, line, (int)(s1 - s0), s0, *pi);
			continue;
		}

		<edit> * { fail = "edit"; goto catch; }
		<edit> @s0 date "]" => future {
			const union line64 key = { { (uint32_t)line, date } };
			size_t *pi;
			struct edit *ed;
			if(!(ed = edit_array_new(&scan->edits.array))) goto catch;
			ed->edit.u32 = 0;
			ed->desc.a = 0, ed->desc.b = 0;
			assert(!future), future = &ed->desc;
			if(!pair_to_date(s0, &ed->edit)) goto catch;
			switch(linemap_tree_bulk_assign(&scan->edits.dates, key, &pi)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT:
				*pi = (size_t)(ed - scan->edits.array.data); break;
			}
			fprintf(stderr, "%s[%zu]: new edit from %"
				PRIu32 "-%" PRIu32 "-%" PRIu32 " stored at %zu.\n",
				datestr, line, ed->edit.year, ed->edit.month, ed->edit.day, *pi);
			continue;
		}

		<place> * { fail = "place unrecognized"; goto catch; }
		<place> @s0 parlabel @s1 / "\n" => skip { also_place: {
			const struct pair keyword = pair(s0, s1);
			const union line64 key = { { (uint32_t)line, date } };
			size_t i, *pi;
			if(line > UINT32_MAX)
				{ errno = ERANGE; fail = "too many lines of text"; goto catch; }
			/*fprintf(stderr, "map: <<%.*s>> out of %s\n", (int)(s1 - s0), s0,
					pair_map_table_to_string(&scan->places.map));*/
			if(!(i = pair_map_table_get(&scan->places.map, keyword)))
				{ fail = "keyword not introduced"; goto catch; }
			switch(linemap_tree_bulk_assign(&scan->places.dates, key, &pi)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT: *pi = i; break;
			}
			fprintf(stderr, "%s[%zu]: place <<%.*s>>\n",
				datestr, line, (int)(s1 - s0), s0);
		} continue; }
		<place> "(" @t0 decimal "," @t1 decimal ")"
			ws+ @s0 parlabel @s1 / "\n" => skip {
			const struct pair keyword = pair(s0, s1);
			const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */
			size_t *idx;
			struct place *place;
			switch(pair_map_table_assign(&scan->places.map, keyword, &idx)) {
			case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
			case TABLE_ERROR: goto catch; /* /\ _Sic_. */
			case TABLE_ABSENT: *idx = 0; break; /* Good. */
			}
			if(!(place = place_array_new(&scan->places.array))) goto catch;
			*idx = (size_t)(place - scan->places.array.data);
			place->name.a = s0, place->name.b = s1;
			place->x = x, place->y = y;
			fprintf(stderr,
				"%s[%zu]: new place: <<%.*s>> at (%f,%f) stored at %zu.\n",
				datestr, line, (int)(s1 - s0), s0, x, y, *idx);
			goto also_place;
		}
		// anonymous place: bypass map and have a 1:1 correspondence
		<place> "(" @t0 decimal "," @t1 decimal ")" / "\n" => skip {
			size_t *idx;
			struct place *place;
			const double x = strtod(t0, 0), y = strtod(t1, 0); /* Safe? */
			const union line64 key = { { (uint32_t)line, date } };
			if(!(place = place_array_new(&scan->places.array))) goto catch;
			place->name.a = place->name.b = 0;
			place->x = x, place->y = y;
			switch(linemap_tree_bulk_assign(&scan->places.dates, key, &idx)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT: break;
			}
			*idx = (size_t)(place - scan->places.array.data);
			continue;
		}


		<source> * { fail = "source unrecognized"; goto catch; }
		<source> @s0 keyword @s1 / "\n" => skip { also_source: {
			const struct pair keyword = pair(s0, s1);
			const union line64 key = { { (uint32_t)line, date } };
			size_t i, *pi;
			//fixme: verify way before
			if(line > UINT32_MAX)
				{ errno = ERANGE; fail = "too many lines of text"; goto catch; }
			if(!(i = pair_map_table_get(&scan->sources.map, keyword)))
				{ fail = "keyword not introduced"; goto catch; }
			switch(linemap_tree_bulk_assign(&scan->sources.dates, key, &pi)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT: *pi = i; break;
			}
			fprintf(stderr, "%s[%zu]: source <<%.*s>>\n",
				datestr, line, (int)(s1 - s0), s0);
		} continue; }
		/* New source. */
		<source> @s0 keyword @s1 ":" => future {
			struct pair keyword = pair(s0, s1);
			size_t *idx;
			struct source *source;
			switch(pair_map_table_assign(&scan->sources.map, keyword, &idx)) {
			case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
			case TABLE_ERROR: goto catch; /* /\ _Sic_. */
			case TABLE_ABSENT: *idx = 0; break; /* Good. */
			}
			if(!(source = source_array_new(&scan->sources.array))) goto catch;
			*idx = (size_t)(source - scan->sources.array.data);
			source->name.a = s0, source->name.b = s1;
			source->desc.a = 0, source->desc.b = 0;
			assert(!future), future = &source->desc;
			fprintf(stderr, "%s[%zu]: new source <<%.*s>> stored at %zu.\n",
				datestr, line, (int)(s1 - s0), s0, *idx);
			goto also_source;
		}


		<score> * { fail = "score unrecognized"; goto catch; }
		/* Already there. Use the map to get the index from the keyword and
		 then stick a marker in the tree with that index. */
		<score> @s0 keyword @s1 / "\n" => skip { new_score: {
			const struct pair keyword = pair(s0, s1);
			const union line64 key = { { (uint32_t)line, date } };
			size_t idx, *pidx;
			if(line > UINT32_MAX)
				{ errno = ERANGE; fail = "too many lines of text"; goto catch; }
			if(!(idx = pair_map_table_get(&scan->scores.map, keyword)))
				{ fail = "keyword not introduced"; goto catch; }
			if(scan->scores.array.data[idx].last.u32 >= date.u32)
				{ fail = "duplicate key in same day"; goto catch; }
			scan->scores.array.data[idx].last.u32 = date.u32;
			switch(linemap_tree_bulk_assign(&scan->scores.dates, key, &pidx)) {
			case TREE_PRESENT: assert(0); fail = "duplicate key"; /* _Sic_. */
			case TREE_ERROR: goto catch;
			case TREE_ABSENT: *pidx = idx; break;
			}
			fprintf(stderr, "%s[%zu]: score <%.*s>\n",
				datestr, line, (int)(s1 - s0), s0);
		} continue; }
		/* New score. */
		<score> @s0 keyword @s1 ":" => score_name {
			size_t *idx;
			assert(!new_score);
			/* Create a new mapping from dateline to scores array. */
			switch(pair_map_table_assign(&scan->scores.map,
				pair(s0, s1), &idx)) {
			case TABLE_PRESENT: errno = EDOM; fail = "new keyword already used";
			case TABLE_ERROR: goto catch; /* _Sic_. */
			case TABLE_ABSENT: *idx = 0; break;
			}
			/* New entry in the scores array for this map to point to. */
			if(!(new_score = score_array_new(&scan->scores.array))) goto catch;
			*idx = (size_t)(new_score - scan->scores.array.data); /* Offset. */
			/*struct pair key, name; union date32 date, last; unsigned edges;*/
			new_score->key.a = s0, new_score->key.b = s1;
			new_score->name.a = 0, new_score->name.b = 0;
			new_score->date.u32 = new_score->last.u32 = 0;
			new_score->edges = 0, new_score->score = 0;
			fprintf(stderr, "%s[%zu]: new score <%.*s> stored at %zu.\n",
				datestr, line, (int)(s1 - s0), s0, *idx);
			goto new_score;
		}
		<score_name> * { fail = "name unrecognized"; goto catch; }
		<score_name> ws* @s0 semilabel @s1 ";" => score_date {
			assert(new_score);
			new_score->name.a = s0, new_score->name.b = s1;
			continue;
		}
		<score_date> * { fail = "date unrecognized"; goto catch; }
		<score_date> ws* "~"? @s0 date ";" => score_edges {
			assert(new_score);
			if(!pair_to_date(s0, &new_score->date)) goto catch;
			continue;
		}
		<score_edges> * { fail = "edges unrecognized"; goto catch; }
		<score_edges> ws* "~"? @s0 uint @s1 / "\n" => skip {
			assert(new_score);
			if(!pair_to_natural(s0, s1, &new_score->edges)) goto catch;
			new_score = 0; /* Done. */
			continue;
		}


		/* type, reg, launch, how, height, landing, pilot, dual, instr, remarks
		 eg, [glider] 2-33A; C-GCLK; CYQQ; A; 2000'; CYQQ; ;:13;; Peters D1 */
		<glider_type> * { fail = "glider type"; goto catch; }
		<glider_type> ws* @s0 semilabel @s1 ";" => glider_reg {
			const union line64 key = {{ (uint32_t)line, date }};
			assert(!new_glider);
			if(line > UINT32_MAX) { fail = "line overflow"; goto catch; }
			switch(glider_tree_bulk_assign(&scan->gliders, key, &new_glider)) {
			case TREE_PRESENT: fail = "duplicate"; case TREE_ERROR: goto catch;
			case TREE_ABSENT: break;
			}
			new_glider->type.a = s0, new_glider->type.b = s1;
			continue;
		}
		<glider_reg> * { fail = "glider reg"; goto catch; }
		<glider_reg> ws* @s0 semilabel @s1 ";" => glider_launch
			{ new_glider->reg.a = s0, new_glider->reg.b = s1; continue; }
		<glider_launch> * { fail = "glider launch"; goto catch; }
		<glider_launch> ws* @s0 airport @s1 ";" => glider_how {
			new_glider->launch.a = s0, new_glider->launch.b = s1;
			fprintf(stderr, "%s[%zu]: glider <<%.*s>> at <<%.*s>>\n",
				datestr, line, (int)(new_glider->reg.b - new_glider->reg.a),
				new_glider->reg.a, (int)(s1 - s0), s0);
			continue;
		}
		<glider_how> * { fail = "glider how"; goto catch; }
		<glider_how> ws* @s0 [MWA] ";" => glider_height {
			switch(*s0) {
			case 'M': new_glider->how = MotorCarTow; break;
			case 'W': new_glider->how = Winch; break;
			case 'A': new_glider->how = AeroTow; break;
			}
			continue;
		}
		<glider_height> * { fail = "glider height"; goto catch; }
		<glider_height> ws* @s0 natural @s1 "';" => glider_landing
			{ if(!pair_to_natural(s0, s1, &new_glider->height_ft)); continue; }
		<glider_landing> * { fail = "glider landing"; goto catch; }
		<glider_landing> ws* @s0 airport @s1 ";" => glider_pilot
			{ new_glider->landing.a = s0, new_glider->landing.b = s1; continue;}
		<glider_pilot> * { fail = "glider pilot time"; goto catch; }
		<glider_pilot> ws* ";" => glider_dual /* not PIC */
			{ new_glider->pilot_min = 0; continue; }
		<glider_pilot> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
			=> glider_dual { if(!pair_colon_to_minutes(s0, s1, t0, t1,
			&new_glider->pilot_min)) { fail = "glider pilot time"; goto catch; }
			continue; }
		<glider_dual> * { fail = "glider dual time"; goto catch; }
		<glider_dual> ws* ";" => glider_instr
			{ new_glider->dual_min = 0; continue; }
		<glider_dual> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
			=> glider_instr { if(!pair_colon_to_minutes(s0, s1, t0, t1,
			&new_glider->dual_min)) { fail = "glider dual time"; goto catch; }
			continue; }
		<glider_instr> * { fail = "glider instr time"; goto catch; }
		<glider_instr> ws* ";" => glider_remarks
			{ new_glider->instr_min = 0; continue; }
		<glider_instr> ws* @s0 natural? @s1 ":" @t0 minutes @t1 ws* ";"
			=> glider_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1,
			&new_glider->instr_min)) { fail = "glider instr time"; goto catch; }
			continue; }
		<glider_remarks> * { fail = "glider remarks"; goto catch; }
		<glider_remarks> "\n" => line
			{ new_glider->remarks.a = new_glider->remarks.b = 0;
			new_glider = 0; line++; continue; }
		<glider_remarks> ws* @s0 anylabel @s1 "\n" => line
			{ new_glider->remarks.a = s0, new_glider->remarks.b = s1;
			new_glider = 0; line++; continue; }


		/* type; registration; launch -- landing; pic; sic;
		 single engine day dual; pilot; instrument simulated; actual; remarks */
		<flight_type> * { fail = "flight type"; goto catch; }
		<flight_type> ws* @s0 semilabel @s1 ";" => flight_reg {
			const union line64 key = {{ (uint32_t)line, date }};
			assert(!new_flight);
			if(line > UINT32_MAX) { fail = "line overflow"; goto catch; }
			switch(flight_tree_bulk_assign(&scan->flights, key, &new_flight)) {
			case TREE_PRESENT: fail = "duplicate key";
			case TREE_ERROR: goto catch;
			case TREE_ABSENT: break;
			}
			new_flight->type.a = s0, new_flight->type.b = s1;
			continue;
		}
		<flight_reg> * { fail = "flight reg"; goto catch; }
		<flight_reg> ws* @s0 semilabel @s1 ";" => flight_airports
			{ new_flight->reg.a = s0, new_flight->reg.b = s1; continue; }
		<flight_airports> * { fail = "flight airports"; goto catch; }
		<flight_airports> ws* @s0 airport @s1 ws* "--"
			ws* @t0 airport @t1 ws* ";" => flight_pic {
			new_flight->launch.a = s0, new_flight->launch.b = s1;
			new_flight->landing.a = t0, new_flight->landing.b = t1;
			fprintf(stderr, "%s[%zu]: flight <<%.*s>> at <<%.*s>>\n",
				datestr, line, (int)(new_flight->reg.b - new_flight->reg.a),
				new_flight->reg.a, (int)(s1 - s0), s0);
			continue;
		}
		<flight_pic> * { fail = "flight pic"; goto catch; }
		<flight_pic> ws* @s0 semilabel @s1 ";" => flight_sic
			{ new_flight->pilot.a = s0, new_flight->pilot.b = s1; continue; }
		<flight_sic> * { fail = "flight sic"; goto catch; }
		<flight_sic> ws* ";" => flight_dual
			{ new_flight->copilot.a = new_flight->copilot.b = 0; continue; }
		<flight_sic> ws* @s0 semilabel @s1 ";" => flight_dual
			{ new_flight->copilot.a = s0, new_flight->copilot.b = s1; continue; }
		<flight_dual> * { fail = "flight dual time"; goto catch; }
		<flight_dual> ws* ";" => flight_pilot
			{ new_flight->dual_min = 0; continue; }
		<flight_dual> ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";"
			=> flight_pilot { if(!pair_hours_to_minutes(s0, s1, t0, t1,
			&new_flight->dual_min)) { fail = "flight dual time"; goto catch; }
			continue; }
		<flight_pilot> * { fail = "flight pilot time"; goto catch; }
		<flight_pilot> ws* ";" => flight_ifrsim
			{ new_flight->pilot_min = 0; continue; }
		<flight_pilot> ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";"
			=> flight_ifrsim { if(!pair_hours_to_minutes(s0, s1, t0, t1,
			&new_flight->pilot_min)) { fail = "flight pilot time";
			goto catch; } continue; }
		<flight_ifrsim> * { fail = "flight simulated ifr time"; goto catch; }
		<flight_ifrsim> ws* ";" => flight_ifr
			{ new_flight->ifrsim_min = 0; continue; }
		<flight_ifrsim> ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";"
			=> flight_ifr { if(!pair_hours_to_minutes(s0, s1, t0, t1,
			&new_flight->ifrsim_min)) { fail = "flight simulated ifr time";
			goto catch; } continue; }
		<flight_ifr> * { fail = "flight ifr time"; goto catch; }
		<flight_ifr> ws* ";" => flight_remarks
			{ new_flight->ifr_min = 0; continue; }
		<flight_ifr> ws* @s0 zero_natural? @s1 "." @t0 [0-9] @t1 "h" ws* ";"
			=> flight_remarks { if(!pair_hours_to_minutes(s0, s1, t0, t1,
			&new_flight->ifr_min)) { fail = "flight ifr time"; goto catch; }
			continue; }
		<flight_remarks> * { fail = "flight remarks"; goto catch; }
		<flight_remarks> "\n" => line
			{ new_flight->remarks.a = new_flight->remarks.b = 0;
			new_flight = 0; line++; continue; }
		<flight_remarks> ws* @s0 anylabel @s1 "\n" => line
			{ new_flight->remarks.a = s0, new_flight->remarks.b = s1;
			new_flight = 0; line++; continue; }


		/* Books in KJV. */
		<line> "Genesis" / kjvlookat => book { book = Genesis; continue; }
		<line> "Exodus" / kjvlookat => book { book = Exodus; continue; }
		<line> "Leviticus" / kjvlookat => book { book = Leviticus; continue; }
		<line> "Numbers" / kjvlookat => book { book = Numbers; continue; }
		<line> "Deuteronomy" / kjvlookat => book
			{ book = Deuteronomy; continue; }
		<line> "Joshua" / kjvlookat => book { book = Joshua; continue; }
		<line> "Judges" / kjvlookat => book { book = Judges; continue; }
		<line> "Ruth" / kjvlookat => book { book = Ruth; continue; }
		<line> first "Samuel" / kjvlookat => book { book = ISamuel; continue; }
		<line> second "Samuel" / kjvlookat => book
			{ book = IISamuel; continue; }
		<line> first "Kings" / kjvlookat => book { book = IKings; continue; }
		<line> second "Kings" / kjvlookat => book { book = IIKings; continue; }
		<line> first "Chronicles" / kjvlookat
			=> book { book = IChronicles; continue; }
		<line> second "Chronicles" / kjvlookat
			=> book { book = IIChronicles; continue; }
		<line> "Ezra" / kjvlookat => book { book = Ezra; continue; }
		<line> "Nehemiah" / kjvlookat => book { book = Nehemiah; continue; }
		<line> "Esther" / kjvlookat => book { book = Esther; continue; }
		<line> "Job" / kjvlookat => book { book = Job; continue; }
		<line> "Psalms" / kjvlookat => book { book = Psalms; continue; }
		<line> "Proverbs" / kjvlookat => book { book = Proverbs; continue; }
		<line> "Ecclesiastes" / kjvlookat
			=> book { book = Ecclesiastes; continue; }
		<line> "Song of Solomon" / kjvlookat
			=> book { book = Song_of_Solomon; continue; }
		<line> "Isaiah" / kjvlookat => book { book = Isaiah; continue; }
		<line> "Jeremiah" / kjvlookat => book { book = Jeremiah; continue; }
		<line> "Lamentations" / kjvlookat
			=> book { book = Lamentations; continue; }
		<line> "Ezekiel" / kjvlookat => book { book = Ezekiel; continue; }
		<line> "Daniel" / kjvlookat => book { book = Daniel; continue; }
		<line> "Hosea" / kjvlookat => book { book = Hosea; continue; }
		<line> "Joel" / kjvlookat => book { book = Joel; continue; }
		<line> "Amos" / kjvlookat => book { book = Amos; continue; }
		<line> "Obadiah" / kjvlookat => book { book = Obadiah; continue; }
		<line> "Jonah" / kjvlookat => book { book = Jonah; continue; }
		<line> "Micah" / kjvlookat => book { book = Micah; continue; }
		<line> "Nahum" / kjvlookat => book { book = Nahum; continue; }
		<line> "Habakkuk" / kjvlookat => book { book = Habakkuk; continue; }
		<line> "Zephaniah" / kjvlookat => book { book = Zephaniah; continue; }
		<line> "Haggai" / kjvlookat => book { book = Haggai; continue; }
		<line> "Zechariah" / kjvlookat => book { book = Zechariah; continue; }
		<line> "Malachi" / kjvlookat => book { book = Malachi; continue; }
		<line> "Matthew" / kjvlookat => book { book = Matthew; continue; }
		<line> "Mark" / kjvlookat => book { book = Mark; continue; }
		<line> "Luke" / kjvlookat => book { book = Luke; continue; }
		<line> "John" / kjvlookat => book { book = John; continue; }
		<line> "Acts" / kjvlookat => book { book = Acts; continue; }
		<line> "Romans" / kjvlookat => book { book = Romans; continue; }
		<line> first "Corinthians" / kjvlookat
			=> book { book = ICorinthians; continue; }
		<line> second "Corinthians" / kjvlookat
			=> book { book = IICorinthians; continue; }
		<line> "Galatians" / kjvlookat => book { book = Galatians; continue; }
		<line> "Ephesians" / kjvlookat => book { book = Ephesians; continue; }
		<line> "Philippians" / kjvlookat => book
			{ book = Philippians; continue; }
		<line> "Colossians" / kjvlookat => book { book = Colossians; continue; }
		<line> first "Thessalonians" / kjvlookat
			=> book { book = IThessalonians; continue; }
		<line> second "Thessalonians" / kjvlookat
			=> book { book = IIThessalonians; continue; }
		<line> first "Timothy" / kjvlookat => book
			{ book = ITimothy; continue; }
		<line> second "Timothy" / kjvlookat => book
			{ book = IITimothy; continue; }
		<line> "Titus" / kjvlookat => book { book = Titus; continue; }
		<line> "Philemon" / kjvlookat => book { book = Philemon; continue; }
		<line> "Hebrews" / kjvlookat => book { book = Hebrews; continue; }
		<line> "James" / kjvlookat => book { book = James; continue; }
		<line> first "Peter" / kjvlookat => book { book = IPeter; continue; }
		<line> second "Peter" / kjvlookat => book { book = IIPeter; continue; }
		<line> first "John" / kjvlookat => book { book = IJohn; continue; }
		<line> second "John" / kjvlookat => book { book = IIJohn; continue; }
		<line> third "John" / kjvlookat => book { book = IIIJohn; continue; }
		<line> "Jude" / kjvlookat => book { book = Jude; continue; }
		<line> "Revelation" / kjvlookat => book { book = Revelation; continue; }
		<book> * { fail = "kjv unrecognized"; goto catch; }
		/* 19:15a, just ignore the a. */
		<book> " " @s0 natural @s1 ":" @t0 natural @t1 [ab]? {
			if(chapter || verse || verse_end)
				{ fail = "kjv reference"; goto catch; }
			if(!pair_to_natural(s0, s1, &chapter)
				|| !pair_to_natural(t0, t1, &verse))
				{ fail = "kjv reference numerical error"; goto catch; }
			continue;
		}
		<book> "-" @s0 natural @s1 [ab]? { /* Verse range. */
			if(!chapter || !verse || verse_end)
				{ fail = "kjv range unrecognized"; goto catch; }
			if(!pair_to_natural(s0, s1, &verse_end))
				{ fail = "kjv range numerical error"; goto catch; }
			continue;
		}
		<book> " -- " => skip {
			if(!chapter || !verse)
				{ fail = "kjv missing information"; goto catch; }
			if(verse_end && verse_end <= verse)
				{ fail = "kjv interval error"; goto catch; }
			const union line64 key = {{ (uint32_t)line, date }};
			struct kjvrange *value;
			switch(kjv_tree_bulk_assign(&scan->kjvs, key, &value)) {
			case TREE_PRESENT: fail = "kjv duplicate key";
			case TREE_ERROR: goto catch;
			case TREE_ABSENT:
				value->start.book = book;
				value->start.chapter = chapter;
				value->start.verse = verse;
				value->verse_end = verse_end;
				break;
			}
			fprintf(stderr, "%s[%zu]: KJV %s %" PRIu32 ":%" PRIu32,
				datestr, line, kjv_book_string[book], chapter, verse);
			if(verse_end) fprintf(stderr, "-%u", verse_end);
			fprintf(stderr, "\n");
			book = Revelation, chapter = 0, verse = 0, verse_end = 0;
			continue;
		}


		/* fixme: This is a cool way of doing things. Avoid repetition, make
		 all the things this way. */
		/* "<<\ntext\n>>" or "text\n" used by several. Must have pointers to
		 future input. */
		<future, future_multi> * { fail = "text input"; goto catch; }
		<future> ws+ { continue; }
		<future> "\n" => line { // empty is okay
			line++;
			assert(future);
			future->a = future->b = 0, future = 0;
			continue;
		}
		<future> "<<\n" @s0 => future_multi { // multi-line
			line++;
			assert(future);
			future->a = s0;
			continue;
		}
		<future> @s0 anylabel @s1 "\n" => line { // one line
			line++;
			fprintf(stderr, "text: <<%.*s>>\n", (int)(s1 - s0), s0);
			assert(future);
			future->a = s0, future->b = s1, future = 0;
			continue;
		}
		<future_multi> [^\x00\n] { continue; }
		<future_multi> [\x00] { fail = "missing closing \">>\""; goto catch; }
		<future_multi> "\n" { line++; continue; }
		/* Restricts this to be the last one; you could imagine that it would
		 be more flexible, "<<\n>>; <<\n>>\n". */
		<future_multi> @s1 ">>\n" => line {
			line++;
			assert(future && future->a);
			future->b = s1, future = 0;
			continue;
		}

	*/ }
	assert(0); /* Never gets here. */
catch:
	if(!errno) errno = EILSEQ;
	date32_to_string(date, &datestr);
	fprintf(stderr, "%s line %zu: %s" /*" condition %d"*/ ".\n",
		datestr, line, fail/*, condition*/);
	return 0;
}

void scan_(struct scan *const scan) {
	if(!scan) return;
	kjv_tree_(&scan->kjvs);
	flight_tree_(&scan->flights);
	glider_tree_(&scan->gliders);

	linemap_tree_(&scan->scores.dates);
	pair_map_table_(&scan->scores.map);
	score_array_(&scan->scores.array);

	linemap_tree_(&scan->places.dates);
	pair_map_table_(&scan->places.map);
	place_array_(&scan->places.array);

	linemap_tree_(&scan->edits.dates);
	pair_map_table_(&scan->edits.map);
	edit_array_(&scan->edits.array);

	linemap_tree_(&scan->documents.dates);
	pair_map_table_(&scan->documents.map);
	source_array_(&scan->documents.array);

	linemap_tree_(&scan->sources.dates);
	pair_map_table_(&scan->sources.map);
	source_array_(&scan->sources.array);
}

/** @param[jrnl] Must be constant throughout the use of the returned value. */
struct scan scan(struct journal *const jrnl) {
	struct scan scan = {0};
	struct journal_iterator it;
	union date32 date;
	const char *text;
	assert(jrnl);

	/* Null is the first item for convenience, (TABLE_DEFAULT). */
	{
		struct source *nul;
		if(!(nul = source_array_new(&scan.sources.array))) goto catch;
		*nul = (struct source){0};
		if(!(nul = source_array_new(&scan.documents.array))) goto catch;
		*nul = (struct source){0};
	}
	{
		struct edit *nul;
		if(!(nul = edit_array_new(&scan.edits.array))) goto catch;
		*nul = (struct edit){0};
	}
	{
		struct place *nul;
		if(!(nul = place_array_new(&scan.places.array))) goto catch;
		*nul = (struct place){0};
	}
	{
		struct score *nul;
		if(!(nul = score_array_new(&scan.scores.array))) goto catch;
		*nul = (struct score){0};
	}

	/* Scan all. */
	it = journal_iterator(jrnl);
	while(journal_next(&it, &date, &text))
		if(!scan_day(&scan, date, text)) goto catch;

	/* Scans make trees bulk-loaded; fix to real tree. */
	if(!linemap_tree_bulk_finish(&scan.sources.dates)
		|| !linemap_tree_bulk_finish(&scan.places.dates)
		|| !linemap_tree_bulk_finish(&scan.scores.dates)
		|| !glider_tree_bulk_finish(&scan.gliders)
		|| !flight_tree_bulk_finish(&scan.flights)
		|| !kjv_tree_bulk_finish(&scan.kjvs)) goto catch;

	goto finally;
catch:
	scan_(&scan);
finally:
	return scan;
}

/** Lookup the source of `x` in `scan`, if any. */
static const struct source *source_lookup(struct scan *const scan,
	const union line64 x) {
	struct linemap_tree_iterator it;
	assert(scan);
	it = linemap_tree_less(&scan->sources.dates, x);
	/* If it's before all elements of the journal or is not on the same date as
	 the source, this has no source, which is `array[0]`. */
	return scan->sources.array.data + (linemap_tree_has_element(&it)
		&& x.date.u32 == linemap_tree_key(&it).date.u32
		? *linemap_tree_value(&it) : 0);
}

/** Lookup the source of `x` in `scan`, if any; public function for testing. */
const struct source *scan_source_lookup(struct scan *const scan,
	const union line64 x) { return source_lookup(scan, x); }

void scan_score_graph(struct scan *const scan) {
	struct linemap_tree_iterator it
		= linemap_tree_iterator(&scan->scores.dates);
	union line64 line;
	struct score *score;
	assert(scan);

	/* Set score to zero to verify count with paper journal. */
	for(struct score *i = scan->scores.array.data,
		*const z = i + scan->scores.array.size; i < z; i++) i->score = 0;

	/* 840 with legend; only useful to me. */
	printf("set terminal pngcairo dashed transparent truecolor"
		" size 840, 480 fontscale 1\n"
		"set output \"score.png\"\n");
	printf("$Data <<EOD\n"
		"# date, key, key score\n");
	while(linemap_tree_next(&it)) {
		line = linemap_tree_key(&it);
		assert(*linemap_tree_value(&it) < scan->scores.array.size);
		score = scan->scores.array.data + *linemap_tree_value(&it);
		char datestr[12];
		date32_to_string(line.date, &datestr);
		score->score++;
		printf("%s, %.*s, %u\n", datestr,
			(int)(score->key.b - score->key.a), score->key.a, score->score);
	}
	printf("EOD\n"
		"# theozh https://stackoverflow.com/a/75466214/2472827\n"
		"# get a unique list from datablock\n"
		"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
		" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
		"Uniqs = ''\n"
		"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
		"Uniq(i) = word(Uniqs,i)\n"
		"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
		"\n"
		/*"stats $Data u 3 nooutput\n"
		"cumsum = STATS_sum\n"
		"stats $Data u 4 nooutput\n"
		"setsum = STATS_sum\n"
		"\n"*/
		"myTimeFmt = \"%%Y-%%m-%%d\"\n"
		"set format x myTimeFmt timedate\n"
		"set xtics format myTimeFmt rotate by -30\n"
		"set ylabel \"happiness and satisfaction CDF (days)\"\n"
		"set grid\n"
		"set key out reverse Left noautotitle\n"
		"set style fill solid 0.5\n"
		"unset border\n"
		"unset key\n"
		"set autoscale xfix # max? hack: can't get x to extend further\n"
		"\n"
		/*"set label sprintf(\"%%u cumulative words (duplicate verses counted)\","
		" cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n"
		"set label sprintf(\"%%u unique KJV verse words memorized\", setsum) "
		"center at graph 0.5, first setsum*100/%zu offset 0,0.5\n"
		"\n"*/
		"plot \\\n"
		" total=0 $Data u"
		" (timecolumn(1,myTimeFmt)):(total=total+1) \\\n"
		" w steps lc \"black\" dt 1 lw 1, \\\n"
		" total=0 '' u \\\n"
		" (timecolumn(1,myTimeFmt)): \\\n"
		" (total=total+1,total/2.): \\\n"
		" (43200): \\\n"
		" (total/2.): \\\n"
		" (getIndex(strcol(2))) w boxxy lc var lw 1, \\\n"
		" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n");
}

#include <inttypes.h>

void scan_glider_graph(struct scan *const scan) {
	assert(scan);
	fprintf(stderr, "Glider: %s.\n", glider_tree_to_string(&scan->gliders));
	printf("set terminal pngcairo dashed transparent truecolor"
		" size 840, 480 fontscale 1\n"
		"set output \"glider.png\"\n");
	/*printf("set terminal cairolatex standalone pdf size 16cm,10.5cm"
		" dashed transparent\n"
		"set output \"flight.tex\"\n");*/
	/*printf("set term postscript eps enhanced\n"
		"set output \"flighthours.eps\"\n");*/
	printf("$Data <<EOD\n"
		"# date, reg, sic, pic, source\n");
	struct glider_tree_iterator it = glider_tree_iterator(&scan->gliders);
	while(glider_tree_next(&it)) {
		const union line64 line = glider_tree_key(&it);
		const struct glider *glider = glider_tree_value(&it);
		char datestr[12];
		date32_to_string(line.date, &datestr);
		const struct source *src = source_lookup(scan, line);
		assert(src);
		if(!src->name.a) { fprintf(stderr,
			"Glider has no source at %s; ignoring.\n", datestr); continue; }
		printf("%s, ", datestr);
		printf("%.*s, %" PRIu32 ", %" PRIu32,
			(int)(glider->reg.b - glider->reg.a), glider->reg.a,
			glider->dual_min, glider->pilot_min + glider->instr_min);
		/* case POWER:
			printf("%.*s, %" PRIu32 ", %" PRIu32,
				(int)(flight->power.reg.b - flight->power.reg.a),
				flight->power.reg.a,
				flight->power.dual_min,
				flight->power.pilot_min);
			break; */
		printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a);
	}
	printf("EOD\n"
		"# theozh https://stackoverflow.com/a/75466214/2472827\n"
		"# get a unique list from datablock\n"
		"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
		" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
		"Uniqs = ''\n"
		"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
		"Uniq(i) = word(Uniqs,i)\n"
		"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
		"\n"
		"stats $Data u 3 nooutput\n"
		"sicsum = STATS_sum\n"
		"stats $Data u 4 nooutput\n"
		"picsum = STATS_sum\n"
		"\n"
		"myTimeFmt = \"%%Y-%%m-%%d\"\n"
		"set format x myTimeFmt timedate\n"
		"set xtics format myTimeFmt rotate by -30\n"
		"set format y \"%%tH:%%tM\" timedate\n"
		"set grid\n"
		"set key out reverse Left noautotitle\n"
		"set style fill solid 0.5\n"
		"unset border\n"
		"plot total=0 $Data u"
		" (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)"
		" w steps lc \"black\" dt 3, \\\n"
		" total=0 '' u (timecolumn(1,myTimeFmt)):"
		"(dy=($3+$4)*60,total=total+dy,total/2.): \\\n"
		" (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n"
		" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n"
		/*"set xrange [*:'2001-09-11']\n"*/
		/*"#set style fill solid 0.1 #pattern 5 (better, but restarts)\n"
		"plot $Data using 1:($6/60) with fillsteps lw 2\n"*/);
}

void scan_flight_graph(struct scan *const scan) {
	assert(scan);
	fprintf(stderr, "Flight: %s.\n", flight_tree_to_string(&scan->flights));
	printf("set terminal pngcairo dashed transparent truecolor"
		" size 840, 480 fontscale 1\n"
		"set output \"glider.png\"\n");
	printf("$Data <<EOD\n"
		"# date, reg, sic, pic, source\n");
	struct flight_tree_iterator it = flight_tree_iterator(&scan->flights);
	while(flight_tree_next(&it)) {
		const union line64 line = flight_tree_key(&it);
		const struct flight *flight = flight_tree_value(&it);
		char datestr[12];
		date32_to_string(line.date, &datestr);
		const struct source *src = source_lookup(scan, line);
		assert(src);
		if(!src->name.a) { fprintf(stderr,
			"Source has no source at %s; ignoring.\n", datestr); continue; }
		printf("%s, ", datestr);
		printf("%.*s, %" PRIu32 ", %" PRIu32,
			(int)(flight->reg.b - flight->reg.a), flight->reg.a,
			flight->dual_min, flight->pilot_min);
		printf(", %.*s\n", (int)(src->name.b - src->name.a), src->name.a);
	}
	printf("EOD\n"
		"# theozh https://stackoverflow.com/a/75466214/2472827\n"
		"# get a unique list from datablock\n"
		"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
		" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
		"Uniqs = ''\n"
		"stats $Data u (Uniqs=addToList(Uniqs,2)) nooutput\n"
		"Uniq(i) = word(Uniqs,i)\n"
		"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
		"\n"
		"stats $Data u 3 nooutput\n"
		"sicsum = STATS_sum\n"
		"stats $Data u 4 nooutput\n"
		"picsum = STATS_sum\n"
		"\n"
		"myTimeFmt = \"%%Y-%%m-%%d\"\n"
		"set format x myTimeFmt timedate\n"
		"set xtics format myTimeFmt rotate by -30\n"
		"set format y \"%%tH:%%tM\" timedate\n"
		"set grid\n"
		"set key out reverse Left noautotitle\n"
		"set style fill solid 0.5\n"
		"unset border\n"
		"plot total=0 $Data u"
		" (timecolumn(1,myTimeFmt)):(dy=($3+$4)*60,total=total+dy)"
		" w steps lc \"black\" dt 3, \\\n"
		" total=0 '' u (timecolumn(1,myTimeFmt)):"
		"(dy=($3+$4)*60,total=total+dy,total/2.): \\\n"
		" (43200):(total/2.):(getIndex(strcol(2))) w boxxy lc var, \\\n"
		" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n");
}

void scan_kjv_graph(struct scan *const scan) {
	struct kjvcount_table count = {0};
	struct kjvset_table set = kjv_set();
	size_t no_total;
	const char *reason = 0;

	count = kjv_count(&no_total);
	fprintf(stderr, "KJV count: %s.\n", kjv_count_to_string(&count));
	if(!no_total) { reason = "kjv failed to load"; goto catch; }

	fprintf(stderr, "KJV lines: %s.\n", kjv_tree_to_string(&scan->kjvs));

	struct kjv_tree_iterator it = kjv_tree_iterator(&scan->kjvs);

	/* https://stackoverflow.com/a/12601553 */
	printf("set terminal pngcairo dashed transparent truecolor"
		" size 840, 480 fontscale 1\n"
		"set output \"kjv.png\"\n");
	printf("$Data <<EOD\n"
		"# date, verse, words, set / %zu, source\n", no_total);
	while(kjv_tree_next(&it)) {
		char citestr[12], datestr[12];
		const union line64 line = kjv_tree_key(&it);
		const struct kjvrange *const range = kjv_tree_value(&it);
		size_t words = 0, newwords = 0;
		const struct source *src = source_lookup(scan, line);
		if(!src || !src->name.a) { errno = EDOM; goto catch; }
		date32_to_string(line.date, &datestr); /* Date. */
		kjvcite_to_string(range->start, &citestr); /* KJV cite. */
		for(union kjvcite c = range->start; ; c.verse++) {
			size_t w = kjv_count_get(&count, c);
			words += w;
			switch (kjv_set_add(&set, c)) {
			case TABLE_ERROR: goto catch;
			case TABLE_ABSENT: newwords += w;
			case TABLE_PRESENT: break;
			} /* while(); */
			if(!range->verse_end || range->verse_end <= c.verse) break;
		}
		printf("%s, %s", datestr, citestr);
		if(range->verse_end) printf("-%" PRIu32, range->verse_end);
		printf(", %zu, %zu, %.*s\n",
			words, newwords, (int)(src->name.b - src->name.a), src->name.a);
	}
	printf("EOD\n"
		"# theozh https://stackoverflow.com/a/75466214/2472827\n"
		"# get a unique list from datablock\n"
		"addToList(list,col) = list.( strstrt(list,'\"'.strcol(col).'\"') \\\n"
		" > 0 ? '' : ' \"'.strcol(col).'\"')\n"
		"Uniqs = ''\n"
		"stats $Data u (Uniqs=addToList(Uniqs,5)) nooutput\n"
		"Uniq(i) = word(Uniqs,i)\n"
		"getIndex(s) = sum [_i=1:words(Uniqs)] s eq word(Uniqs,_i) ? _i : 0\n"
		"\n"
		"stats $Data u 3 nooutput\n"
		"cumsum = STATS_sum\n"
		"stats $Data u 4 nooutput\n"
		"setsum = STATS_sum\n"
		"\n"
		"myTimeFmt = \"%%Y-%%m-%%d\"\n"
		"set format x myTimeFmt timedate\n"
		"set xtics format myTimeFmt rotate by -30\n"
		"set format y \"%%g%%%%\"\n"
		"set grid\n"
		"set key out reverse Left noautotitle\n"
		"set style fill solid 0.5\n"
		"unset border\n"
		"set autoscale xfix # max? hack: can't get x to extend further\n"
		"\n"
		"set label sprintf(\"%%u cumulative words (duplicate verses counted)\","
		" cumsum) center at graph 0.5, first cumsum*100/%zu offset 0,0.5\n"
		"set label sprintf(\"%%u unique KJV verse words memorized\", setsum) "
		"center at graph 0.5, first setsum*100/%zu offset 0,0.5\n"
		"\n"
		"plot \\\n"
		" cumsum*100/%zu w l lc \"grey\" dt 2 lw 1, \\\n"
		" total=0 $Data u"
		" (timecolumn(1,myTimeFmt)):(dy=$3*100/%zu,total=total+dy) \\\n"
		" w steps lc \"grey\" dt 2 lw 1, \\\n" /* `pngcairo` has trouble :[. */
		" total=0 $Data u"
		" (timecolumn(1,myTimeFmt)):(dy=$4*100/%zu,total=total+dy) \\\n"
		" w steps lc \"black\" dt 1 lw 1, \\\n"
		" setsum*100/%zu w l lc \"black\" dt 1 lw 1, \\\n"
		" total=0 '' u \\\n"
		" (timecolumn(1,myTimeFmt)): \\\n"
		" (dy=$4*100/%zu,total=total+dy,total/2.): \\\n"
		" (43200): \\\n"
		" (total/2.): \\\n"
		" (getIndex(strcol(5))) w boxxy lc var lw 1, \\\n"
		" for [i=1:words(Uniqs)] keyentry w boxxy lc i ti Uniq(i)\n",
		no_total, no_total, no_total, no_total, no_total, no_total, no_total);
	goto finally;
catch:
	perror(reason);
finally:
	kjv_count_(&count);
}
