2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
27 #include <sys/types.h>
35 #include <readline/readline.h>
36 #include <readline/history.h>
38 #define ASSERT_NOT_REACHED \
40 static const int NOT_REACHED = 0; \
41 assert (NOT_REACHED); \
46 typedef struct _item {
60 CATEGORY_ORDER_RANDOM,
61 CATEGORY_ORDER_SEQUENTIAL
71 typedef struct _category {
77 /* Support sequential introduction of items from bin 0 */
78 category_order_t order;
79 /* Support categories where responses are timed (0.0 == disable). */
82 /* Support challenges of non-text types (image, audio, etc.) */
83 challenge_type_t challenge_type;
84 /* Whether to repeat afterwards (for a little extra reinforcement) */
88 typedef struct _mnemon {
93 category_t *categories;
106 xmalloc (size_t size)
112 fprintf (stderr, "Error: out of memory\n");
120 xrealloc (void *ptr, size_t size)
124 ret = realloc (ptr, size);
126 fprintf (stderr, "Error: out of memory\n");
134 xstrdup (const char *s)
140 fprintf (stderr, "Error: out of memory\n");
148 xstrndup (const char *s, size_t n)
152 ret = strndup (s, n);
154 fprintf (stderr, "Error: out of memory\n");
162 xasprintf (char **strp, const char *fmt, ...)
168 ret = vasprintf (strp, fmt, ap);
172 fprintf (stderr, "Error: out of memory\n");
178 item_init (item_t *item,
180 const char *challenge,
181 const char *response)
185 item->challenge = xmalloc (strlen (challenge) + 1 +
186 strlen (response) + 1);
187 item->response = item->challenge + strlen (challenge) + 1;
189 strcpy (item->challenge, challenge);
190 strcpy (item->response, response);
194 item_fini (item_t *item)
196 /* item->response shares allocation with item->challenge, so
197 * doesn't require a separate call to free */
198 free (item->challenge);
202 category_init (category_t *category,
205 category->name = xstrdup (name);
207 category->items_size = 0;
208 category->num_items = 0;
209 category->items = NULL;
210 category->order = CATEGORY_ORDER_RANDOM;
211 category->time_limit = 0.0;
212 category->bin_zero_head = 0;
213 category->challenge_type = CHALLENGE_TYPE_TEXT;
214 category->repeat = 0;
218 category_fini (category_t *category)
222 for (i = 0; i < category->num_items; i++)
223 item_fini (&category->items[i]);
225 free (category->items);
227 free (category->name);
231 category_grow (category_t *category)
233 if (category->items_size)
234 category->items_size *= 2;
236 category->items_size = 1;
238 category->items = xrealloc (category->items,
239 category->items_size * sizeof (item_t));
243 category_add_item (category_t *category,
245 const char *challenge,
246 const char *response)
250 if (category->num_items == category->items_size)
251 category_grow (category);
253 item = &category->items[category->num_items++];
255 item_init (item, score, challenge, response);
261 category_next_bin_zero_item (category_t *category)
263 int *i = &category->bin_zero_head;
265 for ( ; *i < category->num_items; *i = *i + 1)
266 if (category->items[*i].score == 0)
267 return &category->items[*i];
273 category_print (category_t *category,
279 fprintf (file, "order = %s\n\n",
280 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
281 fprintf (file, "time = %f\n\n",
282 category->time_limit);
284 fprintf (file, "challenge = ");
285 switch (category->challenge_type) {
286 case CHALLENGE_TYPE_TEXT:
287 fprintf (file, "text");
289 case CHALLENGE_TYPE_IMAGE:
290 fprintf (file, "image");
292 case CHALLENGE_TYPE_AUDIO:
293 fprintf (file, "audio");
295 case CHALLENGE_TYPE_MIDI:
296 fprintf (file, "midi");
299 fprintf (file, "\n\n");
301 fprintf (file, "repeat = %d\n\n", category->repeat);
303 for (i = 0; i < category->num_items; i++) {
304 item = &category->items[i];
306 fprintf (file, "\n");
307 fprintf (file, "%d\n%s\n%s\n",
315 bin_init (bin_t *bin,
326 bin_fini (bin_t *bin)
332 bin_grow (bin_t *bin)
335 bin->items_size *= 2;
339 bin->items = xrealloc (bin->items,
340 bin->items_size * sizeof (item_t*));
344 bin_add_item (bin_t *bin,
347 assert (item->score == bin->score);
349 if (bin->num_items == bin->items_size)
352 bin->items[bin->num_items++] = item;
356 bin_remove_item (bin_t *bin,
359 /* Replace the current item with the last item, (no need to shift
360 * any more than that since we don't care about the order of the
361 * items within a bin). */
364 bin->items[item_index] = bin->items[bin->num_items];
367 /* Find the index for an item within a bin.
369 * XXX: This is currently a linear search, so is a potential
370 * performance problem.
373 bin_item_index (bin_t *bin,
378 for (i = 0; i < bin->num_items; i++)
379 if (bin->items[i] == item)
385 typedef int (item_match_predicate_t) (void *closure, item_t *item);
387 /* Return the number of items in the bin from the given category (or
388 * from all categories if category == NULL) */
390 bin_num_items_matching (bin_t *bin,
391 item_match_predicate_t *predicate,
394 int i, num_items = 0;
396 if (predicate == NULL)
397 return bin->num_items;
399 for (i = 0; i < bin->num_items; i++)
400 if ((predicate) (closure, bin->items[i]))
407 mnemon_init (mnemon_t *mnemon)
411 home = getenv ("HOME");
415 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
417 mnemon->categories_size = 0;
418 mnemon->num_categories = 0;
419 mnemon->categories = NULL;
421 mnemon->bins_size = 0;
422 mnemon->num_bins = 0;
425 mnemon->to_introduce = 10;
426 mnemon->to_master = 10;
427 mnemon->unlearned = 0;
428 mnemon->mastered = -1;
432 mnemon_fini (mnemon_t *mnemon)
436 for (i = 0; i < mnemon->num_bins; i++)
437 bin_fini (&mnemon->bins[i]);
440 for (i = 0; i < mnemon->num_categories; i++)
441 category_fini (&mnemon->categories[i]);
442 free (mnemon->categories);
444 free (mnemon->dir_name);
448 mnemon_categories_grow (mnemon_t *mnemon)
450 if (mnemon->categories_size)
451 mnemon->categories_size *= 2;
453 mnemon->categories_size = 1;
455 mnemon->categories = xrealloc (mnemon->categories,
456 mnemon->categories_size * sizeof (category_t));
459 /* Get a category by name if it exists */
461 mnemon_get_category_if_exists (mnemon_t *mnemon,
466 for (i = 0; i < mnemon->num_categories; i++)
467 if (strcmp (mnemon->categories[i].name, name) == 0)
468 return &mnemon->categories[i];
473 /* Get a category by name, creating new one if necessary. */
475 mnemon_get_category (mnemon_t *mnemon,
478 category_t *category;
480 category = mnemon_get_category_if_exists (mnemon, name);
484 mnemon_categories_grow (mnemon);
486 category = &mnemon->categories[mnemon->num_categories++];
488 category_init (category, name);
494 mnemon_bins_grow (mnemon_t *mnemon)
496 if (mnemon->bins_size)
497 mnemon->bins_size *= 2;
499 mnemon->bins_size = 1;
501 mnemon->bins = xrealloc (mnemon->bins,
502 mnemon->bins_size * sizeof (bin_t));
506 mnemon_get_bin (mnemon_t *mnemon,
512 for (i = 0; i < mnemon->num_bins; i++)
513 if (mnemon->bins[i].score == score)
514 return &mnemon->bins[i];
515 else if (mnemon->bins[i].score > score)
518 if (mnemon->num_bins == mnemon->bins_size)
519 mnemon_bins_grow (mnemon);
521 bin = &mnemon->bins[i];
523 /* Make room to insert new bin at its sorted location. */
524 if (i < mnemon->num_bins)
525 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
528 bin_init (bin, score);
534 mnemon_remove_bin (mnemon_t *mnemon,
537 int i = bin - mnemon->bins;
541 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
548 int len = strlen (s);
551 if (s[len - 1] == '\n')
556 trim_space (char *string)
561 while (*s && isspace (*s))
566 s = string + strlen (string) - 1;
567 while (s > string && isspace (*s)) {
576 mnemon_load_category (mnemon_t *mnemon,
580 char *line = NULL, *end;
581 size_t line_size = 0;
585 category_t *category;
588 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
589 sprintf (path, "%s/%s", mnemon->dir_name, name);
591 file = fopen (path, "r");
593 fprintf (stderr, "Error: Failed to open %s: %s\n",
594 path, strerror (errno));
598 category = mnemon_get_category (mnemon, name);
600 #define READ_LINE do { \
601 bytes_read = getline (&line, &line_size, file); \
602 if (bytes_read == -1) \
610 char *name, *equal, *value;
612 /* Ignore blank lines */
617 /* An initial digit means we hit an item. Trigger the
618 * spaghetti machine. */
619 if ((*line >= '0' && *line <= '9') || *line == '-')
622 equal = strchr (line, '=');
624 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
625 line, path, line_count);
633 name = trim_space (name);
634 value = trim_space (value);
636 if (strcmp (name, "order") == 0) {
637 if (strcmp (value, "sequential") == 0) {
638 category->order = CATEGORY_ORDER_SEQUENTIAL;
639 } else if (strcmp (value, "random") == 0) {
640 category->order = CATEGORY_ORDER_RANDOM;
642 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
643 value, path, line_count);
646 } else if (strcmp (name, "time") == 0) {
649 limit = strtod (value, &end);
650 while (isspace (*end))
653 category->time_limit = limit;
655 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
656 value, path, line_count);
659 } else if (strcmp (name, "challenge") == 0) {
660 if (strcmp (value, "text") == 0) {
661 category->challenge_type = CHALLENGE_TYPE_TEXT;
662 } else if (strcmp (value, "image") == 0) {
663 category->challenge_type = CHALLENGE_TYPE_IMAGE;
664 } else if (strcmp (value, "audio") == 0) {
665 category->challenge_type = CHALLENGE_TYPE_AUDIO;
666 } else if (strcmp (value, "midi") == 0) {
667 category->challenge_type = CHALLENGE_TYPE_MIDI;
669 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
670 value, path, line_count);
673 } else if (strcmp (name, "repeat") == 0) {
674 if (strcmp (value, "0") == 0)
675 category->repeat = 0;
677 category->repeat = 1;
679 fprintf (stderr, "Unknown option %s at %s:%d\n",
680 name, path, line_count);
688 char *challenge, *response;
690 /* Ignore blank lines */
695 /* Read bin number */
697 score = strtol (line, &end, 10);
699 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
700 line, path, line_count);
706 challenge = strdup (line);
712 category_add_item (category, score, challenge, response);
722 /* Resize category items to fit exactly. */
723 category->items_size = category->num_items;
724 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
726 /* Now that the category is completely loaded, with stable
727 * pointers to every item, we can add each item to its appropriate
729 for (i = 0; i < category->num_items; i++) {
730 item_t *item = &category->items[i];
731 bin_t *bin = mnemon_get_bin (mnemon, item->score);
733 bin_add_item (bin, item);
738 mnemon_load (mnemon_t *mnemon)
741 struct dirent *dirent;
743 dir = opendir (mnemon->dir_name);
745 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
746 mnemon->dir_name, strerror (errno));
751 dirent = readdir (dir);
755 if (dirent->d_type == DT_REG) {
756 /* Ignore files matching *~, (yes, this shouldn't be
757 * hard-coded in such an ad-hoc way, but there you go. */
758 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
759 mnemon_load_category (mnemon, dirent->d_name);
767 mnemon_save (mnemon_t *mnemon)
770 char *filename, *lock_filename;
772 category_t *category;
774 for (i = 0; i < mnemon->num_categories; i++) {
775 category = &mnemon->categories[i];
777 xasprintf (&filename, "%s/%s",
778 mnemon->dir_name, category->name);
779 xasprintf (&lock_filename, "%s/.#%s",
780 mnemon->dir_name, category->name);
782 file = fopen (lock_filename, "w");
784 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
785 lock_filename, strerror (errno));
789 category_print (category, file);
791 fsync (fileno (file));
794 err = rename (lock_filename, filename);
796 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
797 lock_filename, filename, strerror (errno));
802 free (lock_filename);
806 /* Return a uniformly-distributed pseudo-random integer within the
809 * 0 <= result < num_values
812 rand_within (int num_values)
814 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
817 /* Return an exponentially-distributed pseudo-random integer within
820 * 0 <= result < num_values
822 * The distribution is such that each successively larger value will
823 * occur with a probability of half of the previous value.
826 rand_within_exponential (int num_values)
829 static uint32_t mask = 0;
833 /* Optimize the constant case. */
843 while (mask > RAND_MAX)
850 if (ones == num_values)
858 /* Find the category to which an item belongs. */
860 mnemon_item_category (mnemon_t *mnemon,
863 category_t *category;
866 for (i = 0; i < mnemon->num_categories; i++) {
867 category = &mnemon->categories[i];
868 item_index = item - category->items;
869 if (item_index >= 0 && item_index < category->num_items)
876 typedef struct _item_in_category_closure
879 category_t *category;
880 } item_in_category_closure_t;
883 mnemon_item_in_category (void *closure, item_t *item)
885 item_in_category_closure_t *iicc = closure;
886 mnemon_t *mnemon = iicc->mnemon;
887 category_t *category = iicc->category;
889 return (mnemon_item_category (mnemon, item) == category);
892 typedef struct _item_in_category_of_length_closure
895 category_t *category;
897 } item_in_category_of_length_closure_t;
900 mnemon_item_in_category_of_length (void *closure, item_t *item)
902 item_in_category_of_length_closure_t *iicolc = closure;
903 mnemon_t *mnemon = iicolc->mnemon;
904 category_t *category = iicolc->category;
905 int length = iicolc->length;
907 if (mnemon_item_category (mnemon, item) != category)
910 return strlen (item->challenge) == length;
914 mnemon_select_item (mnemon_t *mnemon,
917 category_t **category_ret)
919 int bin_index, item_index;
922 category_t *category;
924 bin_index = rand_within_exponential (mnemon->num_bins);
925 bin = &mnemon->bins[bin_index];
927 /* The most intuitive understanding of the to_introduce counter is
928 * that it's tracking never-before-learned items as they are
929 * pulled from the bin with score 0. But that bin can become
930 * empty. So the refined rule is that we decrement to_introduce
931 * whenever we pull from the lowest-indexed bin with a
932 * non-negative score. */
933 if (mnemon->to_introduce && bin->score >=0 &&
934 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
936 mnemon->to_introduce--;
939 item_index = rand_within (bin->num_items);
941 item = bin->items[item_index];
942 category = mnemon_item_category (mnemon, item);
944 if (bin->score == 0) {
945 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
946 item = category_next_bin_zero_item (category);
948 item_index = bin_item_index (bin, item);
953 *item_index_ret = item_index;
954 *category_ret = category;
958 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
959 #define HISTOGRAM_BAR_WIDTH 63
962 print_histogram_bar (double size,
965 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
966 static char const *boxes[8] = {
971 while (size > units_per_cell) {
973 size -= units_per_cell;
976 size /= units_per_cell;
980 else if (size > 6.5/8.0)
982 else if (size > 5.5/8.0)
984 else if (size > 4.5/8.0)
986 else if (size > 3.5/8.0)
988 else if (size > 2.5/8.0)
990 else if (size > 1.5/8.0)
992 else if (size > 0.5/8.0)
999 mnemon_print_histogram (mnemon_t *mnemon,
1000 const char *category_name,
1003 int i, last_score, max;
1004 category_t *category = NULL;
1007 item_match_predicate_t *predicate = NULL;
1008 void *closure = NULL;
1009 item_in_category_closure_t item_in_category;
1010 item_in_category_of_length_closure_t item_in_category_of_length;
1012 if (mnemon->num_bins == 0)
1015 if (category_name) {
1016 category = mnemon_get_category_if_exists (mnemon, category_name);
1019 predicate = mnemon_item_in_category_of_length;
1020 item_in_category_of_length.mnemon = mnemon;
1021 item_in_category_of_length.category = category;
1022 item_in_category_of_length.length = length;
1023 closure = &item_in_category_of_length;
1025 predicate = mnemon_item_in_category;
1026 item_in_category.mnemon = mnemon;
1027 item_in_category.category = category;
1028 closure = &item_in_category;
1033 for (i = 0; i < mnemon->num_bins; i++) {
1034 num_items = bin_num_items_matching (&mnemon->bins[i],
1035 predicate, closure);
1036 if (i == 0 || num_items > max)
1040 for (i = 0; i < mnemon->num_bins; i++) {
1041 bin = &mnemon->bins[i];
1043 while (bin->score - last_score > 1)
1044 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1045 num_items = bin_num_items_matching (bin,
1046 predicate, closure);
1047 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1048 print_histogram_bar (num_items, max);
1049 last_score = bin->score;
1054 mnemon_handle_command (mnemon_t *mnemon,
1055 const char *command)
1059 switch (command[0]) {
1060 /* 'h' for histogram */
1063 char *category = NULL;
1067 arg += strspn (arg, " \t");
1068 len = strcspn (arg, " \t");
1070 category = xstrndup (arg, len);
1072 arg += strspn (arg, " \t");
1074 length = atoi (arg);
1076 mnemon_print_histogram (mnemon, category, length);
1079 /* 'r' for repeat */
1082 /* Nothing necessary for repeating. */
1086 printf ("Unknown command: %s\n", command);
1092 mnemon_handle_response (mnemon_t *mnemon,
1096 const char *response,
1097 double response_time,
1102 correct = (strcmp (response, item->response) == 0);
1104 bin_remove_item (bin, item_index);
1106 /* If the bin is now empty, we must remove it. Also if we just
1107 * picked the last word we'll ever pick from the bin with
1108 * score 0, then we can remove that as well. */
1109 if (bin->num_items == 0 ||
1110 (bin->score == 0 && mnemon->to_introduce == 0))
1112 mnemon_remove_bin (mnemon, bin);
1116 (time_limit == 0.0 || response_time < time_limit))
1119 /* We reserve an item score of 0 for an item that has
1120 * never been asked. */
1121 if (item->score == 0) {
1123 mnemon->unlearned--;
1124 printf ("You got it!");
1125 } else if (item->score < 0) {
1126 printf ("Yes---just give me %d more.",
1128 } else if (item->score == 1) {
1129 printf ("On your first try, no less!");
1131 printf ("Masterful (%dx).", item->score);
1132 if (mnemon->to_master)
1133 mnemon->to_master--;
1137 printf (" %s is the correct answer.",
1140 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1141 response_time, time_limit);
1142 /* Penalize an incorrect response by forcing the score
1144 if (item->score >= 0) {
1145 if (item->score > 0)
1146 printf (" Oops, you knew that, right? (%dx)\n ",
1148 mnemon->unlearned++;
1149 /* We increase to_master here as an extra penalty. If the
1150 * user is forgetting stuff learned previously, then more
1151 * time should be spent on mastering than learning new
1152 * items. Note that we only do this during the initial
1153 * phase while new items are still being introduced. */
1154 if (mnemon->to_introduce)
1155 mnemon->to_master++;
1156 /* We go to -2 to force a little extra reinforcement
1157 * when re-learning an item, (otherwise, it will often
1158 * get asked again immediately where it is easy to get
1159 * a correct response without any learning). */
1167 if (mnemon->to_introduce)
1168 printf ("%d to come. ", mnemon->to_introduce);
1169 if (mnemon->unlearned)
1170 printf ("%d still unlearned. ", mnemon->unlearned);
1171 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1172 printf ("%d items to master", mnemon->to_master);
1175 bin = mnemon_get_bin (mnemon, item->score);
1177 bin_add_item (bin, item);
1181 mnemon_show_challenge (mnemon_t *mnemon,
1182 challenge_type_t challenge_type,
1183 const char *challenge)
1185 const char *program;
1188 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1189 printf ("%s\n", challenge);
1193 /* XXX: Yes, shelling out to system is total cheese. The planned
1194 * fix here is to bring graphical display in process, (or at least
1195 * have a custom external program that accepts image filenames on
1198 switch (challenge_type) {
1199 case CHALLENGE_TYPE_TEXT:
1202 case CHALLENGE_TYPE_IMAGE:
1203 program = "xli -gamma 2.2";
1205 case CHALLENGE_TYPE_AUDIO:
1208 case CHALLENGE_TYPE_MIDI:
1209 program = "timidity -Os";
1213 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1222 mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
1226 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1229 /* XXX: And this is just embarrassing (obviously wrong in several
1230 * ways). Hopefully I'll amend away any commit that includes this.
1232 xasprintf (&command, "killall xli");
1238 mnemon_do_challenges (mnemon_t *mnemon)
1243 category_t *category;
1247 /* Count the number of items with negative scores. */
1248 mnemon->unlearned = 0;
1249 for (i = 0; i < mnemon->num_bins; i++) {
1250 bin = &mnemon->bins[i];
1251 if (bin->score >= 0)
1253 mnemon->unlearned += bin->num_items;
1256 mnemon->to_introduce -= mnemon->unlearned;
1257 if (mnemon->to_introduce < 0)
1258 mnemon->to_introduce = 0;
1260 /* Get rid of bin with score of 0 if we aren't going to be
1261 * introducing anything from it. */
1262 if (mnemon->to_introduce == 0) {
1263 bin = mnemon_get_bin (mnemon, 0);
1264 mnemon_remove_bin (mnemon, bin);
1267 if (mnemon->unlearned) {
1268 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1269 if (mnemon->to_introduce)
1270 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1273 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1278 struct timeval start, end;
1280 mnemon_select_item (mnemon, &bin, &item_index, &category);
1281 item = bin->items[item_index];
1284 if (category->time_limit > 0.0) {
1285 response = readline ("The next one is timed. Press enter when ready:");
1289 mnemon_show_challenge (mnemon, category->challenge_type,
1292 gettimeofday (&start, NULL);
1293 response = readline ("> ");
1294 gettimeofday (&end, NULL);
1296 mnemon_hide_challenge (mnemon, category->challenge_type);
1298 /* Terminate on EOF */
1299 if (response == NULL) {
1304 if (response[0] == '/') {
1305 mnemon_handle_command (mnemon, response + 1);
1312 mnemon_handle_response (mnemon, bin, item_index,
1314 (end.tv_sec + end.tv_usec / 1e6) -
1315 (start.tv_sec + start.tv_usec / 1e6),
1316 category->time_limit);
1319 /* Replay audio challenges for reinforcement. */
1320 if (category->repeat)
1322 mnemon_show_challenge (mnemon, category->challenge_type,
1324 printf ("%s\n", item->challenge);
1327 } while (mnemon->to_introduce ||
1328 mnemon->unlearned ||
1329 mnemon->to_master > 0);
1333 main (int argc, char *argv[])
1338 srand (time (NULL));
1340 mnemon_init (&mnemon);
1342 mnemon_load (&mnemon);
1344 mnemon_do_challenges (&mnemon);
1346 mnemon_save (&mnemon);
1348 mnemon_fini (&mnemon);
1350 mnemon_init (&mnemon);
1351 mnemon_load (&mnemon);
1353 printf ("Great job.\nHere are your current results:\n");
1354 mnemon_print_histogram (&mnemon, NULL, 0);
1355 response = readline ("Press enter to quit.\n");
1358 mnemon_fini (&mnemon);