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."
26 #include <sys/types.h>
32 #include <readline/readline.h>
33 #include <readline/history.h>
37 typedef struct _item {
50 typedef struct _category {
57 typedef struct _mnemon {
62 category_t *categories;
76 fprintf (stderr, "Error: out of memory\n");
84 xrealloc (void *ptr, size_t size)
88 ret = realloc (ptr, size);
90 fprintf (stderr, "Error: out of memory\n");
98 xstrdup (const char *s)
104 fprintf (stderr, "Error: out of memory\n");
112 xasprintf (char **strp, const char *fmt, ...)
118 ret = vasprintf (strp, fmt, ap);
122 fprintf (stderr, "Error: out of memory\n");
128 item_init (item_t *item,
130 const char *challenge,
131 const char *response)
135 item->challenge = xmalloc (strlen (challenge) + 1 +
136 strlen (response) + 1);
137 item->response = item->challenge + strlen (challenge) + 1;
139 strcpy (item->challenge, challenge);
140 strcpy (item->response, response);
144 item_fini (item_t *item)
146 /* item->response shares allocation with item->challenge, so
147 * doesn't require a separate call to free */
148 free (item->challenge);
152 category_init (category_t *category,
155 category->name = xstrdup (name);
157 category->items_size = 0;
158 category->num_items = 0;
159 category->items = NULL;
163 category_fini (category_t *category)
167 for (i = 0; i < category->num_items; i++)
168 item_fini (&category->items[i]);
170 free (category->items);
172 free (category->name);
176 category_grow (category_t *category)
178 if (category->items_size)
179 category->items_size *= 2;
181 category->items_size = 1;
183 category->items = xrealloc (category->items,
184 category->items_size * sizeof (item_t));
188 category_add_item (category_t *category,
190 const char *challenge,
191 const char *response)
195 if (category->num_items == category->items_size)
196 category_grow (category);
198 item = &category->items[category->num_items++];
200 item_init (item, count, challenge, response);
206 category_print (category_t *category,
212 for (i = 0; i < category->num_items; i++) {
213 item = &category->items[i];
215 fprintf (file, "\n");
216 fprintf (file, "%d\n%s\n%s\n",
224 bin_init (bin_t *bin,
235 bin_fini (bin_t *bin)
241 bin_grow (bin_t *bin)
244 bin->items_size *= 2;
248 bin->items = xrealloc (bin->items,
249 bin->items_size * sizeof (item_t*));
253 bin_add_item (bin_t *bin,
256 assert (item->count == bin->count);
258 if (bin->num_items == bin->items_size)
261 bin->items[bin->num_items++] = item;
265 bin_remove_item (bin_t *bin,
268 /* Replace the current item with the last item, (no need to shift
269 * any more than that since we don't care about the order of the
270 * items within a bin). */
273 bin->items[item_index] = bin->items[bin->num_items];
277 mnemon_init (mnemon_t *mnemon)
281 home = getenv ("HOME");
285 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
287 mnemon->categories_size = 0;
288 mnemon->num_categories = 0;
289 mnemon->categories = NULL;
291 mnemon->bins_size = 0;
292 mnemon->num_bins = 0;
297 mnemon_fini (mnemon_t *mnemon)
301 for (i = 0; i < mnemon->num_bins; i++)
302 bin_fini (&mnemon->bins[i]);
305 for (i = 0; i < mnemon->num_categories; i++)
306 category_fini (&mnemon->categories[i]);
307 free (mnemon->categories);
309 free (mnemon->dir_name);
313 mnemon_categories_grow (mnemon_t *mnemon)
315 if (mnemon->categories_size)
316 mnemon->categories_size *= 2;
318 mnemon->categories_size = 1;
320 mnemon->categories = xrealloc (mnemon->categories,
321 mnemon->categories_size * sizeof (category_t));
325 mnemon_get_category (mnemon_t *mnemon,
329 category_t *category;
331 for (i = 0; i < mnemon->num_categories; i++)
332 if (strcmp (mnemon->categories[i].name, name) == 0)
333 return &mnemon->categories[i];
335 mnemon_categories_grow (mnemon);
337 category = &mnemon->categories[mnemon->num_categories++];
339 category_init (category, name);
345 mnemon_bins_grow (mnemon_t *mnemon)
347 if (mnemon->bins_size)
348 mnemon->bins_size *= 2;
350 mnemon->bins_size = 1;
352 mnemon->bins = xrealloc (mnemon->bins,
353 mnemon->bins_size * sizeof (bin_t));
357 mnemon_get_bin (mnemon_t *mnemon,
363 for (i = 0; i < mnemon->num_bins; i++)
364 if (mnemon->bins[i].count == count)
365 return &mnemon->bins[i];
366 else if (mnemon->bins[i].count > count)
369 mnemon_bins_grow (mnemon);
371 bin = &mnemon->bins[i];
373 /* Make room to insert new bin at its sorted location. */
374 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
377 bin_init (bin, count);
385 int len = strlen (s);
388 if (s[len - 1] == '\n')
393 mnemon_load_category (mnemon_t *mnemon,
397 char *line = NULL, *end;
398 size_t line_size = 0;
402 category_t *category;
405 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
406 sprintf (path, "%s/%s", mnemon->dir_name, name);
408 file = fopen (path, "r");
410 fprintf (stderr, "Error: Failed to open %s: %s\n",
411 path, strerror (errno));
415 category = mnemon_get_category (mnemon, name);
419 char *challenge, *response;
421 /* Read bin number (ignoring blank separator lines) */
423 bytes_read = getline (&line, &line_size, file);
424 if (bytes_read == -1)
428 } while (*line == '\0');
430 count = strtol (line, &end, 10);
432 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
433 line, path, line_count);
438 bytes_read = getline (&line, &line_size, file);
439 if (bytes_read == -1)
443 challenge = strdup (line);
446 bytes_read = getline (&line, &line_size, file);
447 if (bytes_read == -1)
453 category_add_item (category, count, challenge, response);
463 /* Resize category items to fit exactly. */
464 category->items_size = category->num_items;
465 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
467 /* Now that the category is completely loaded, with stable
468 * pointers to every item, we can add each item to its appropriate
470 for (i = 0; i < category->num_items; i++) {
471 item_t *item = &category->items[i];
472 bin_t *bin = mnemon_get_bin (mnemon, item->count);
474 bin_add_item (bin, item);
479 mnemon_load (mnemon_t *mnemon)
482 struct dirent *dirent;
484 dir = opendir (mnemon->dir_name);
486 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
487 mnemon->dir_name, strerror (errno));
492 dirent = readdir (dir);
496 if (dirent->d_type == DT_REG) {
497 /* Ignore files matching *~, (yes, this shouldn't be
498 * hard-coded in such an ad-hoc way, but there you go. */
499 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
500 mnemon_load_category (mnemon, dirent->d_name);
508 mnemon_save (mnemon_t *mnemon)
511 char *filename, *lock_filename;
513 category_t *category;
515 for (i = 0; i < mnemon->num_categories; i++) {
516 category = &mnemon->categories[i];
518 xasprintf (&filename, "%s/%s",
519 mnemon->dir_name, category->name);
520 xasprintf (&lock_filename, "%s/.#%s",
521 mnemon->dir_name, category->name);
523 file = fopen (lock_filename, "w");
525 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
526 lock_filename, strerror (errno));
530 category_print (category, file);
534 err = rename (lock_filename, filename);
536 fprintf (stderr, "Error: Failes to rename %s to %s: %s\n",
537 lock_filename, filename, strerror (errno));
542 free (lock_filename);
546 /* Return a uniformly-distributed pseudo-random integer within the
549 * 0 <= result < num_values
552 rand_within (int num_values)
554 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
557 /* Return an exponentially-distributed pseudo-random integer within
560 * 0 <= result < num_values
562 * The distribution is such that each successively larger value will
563 * occur with a probability of half of the previous value.
566 rand_within_exponential (int num_values)
569 static uint32_t mask = 0;
573 /* Optimize the constant case. */
583 while (mask > RAND_MAX)
590 if (ones == num_values)
599 mnemon_select_item (mnemon_t *mnemon,
606 bin_index = rand_within_exponential (mnemon->num_bins);
608 bin = &mnemon->bins[bin_index];
611 *item_index_ret = rand_within (bin->num_items);
615 mnemon_do_challenges (mnemon_t *mnemon)
624 mnemon_select_item (mnemon, &bin, &item_index);
625 item = bin->items[item_index];
627 printf ("%s\n", item->challenge);
629 response = readline ("> ");
630 if (response == NULL)
633 correct = (strcmp (response, item->response) == 0);
635 bin_remove_item (bin, item_index);
638 printf ("Correct!\n\n");
644 printf (" %s is the correct answer.\n\n",
652 bin = mnemon_get_bin (mnemon, item->count);
654 bin_add_item (bin, item);
659 main (int argc, char *argv[])
665 mnemon_init (&mnemon);
667 mnemon_load (&mnemon);
669 mnemon_do_challenges (&mnemon);
671 mnemon_save (&mnemon);
673 mnemon_fini (&mnemon);