+extern uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
+{
+ uint16_t i = 0;
+ while (1)
+ {
+ if (0 == kb_p)
+ {
+ break;
+ }
+ i++;
+ kb_p = kb_p->next;
+ }
+ return i;
+}
+
+
+
+extern struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
+{
+ uint16_t i = 0;
+ while (1)
+ {
+ if (n == i)
+ {
+ break;
+ }
+ i++;
+ kb_p = kb_p->next;
+ }
+ return kb_p;
+}
+
+
+
+extern void init_keybindings(struct World * world, char * path,
+ struct KeyBiData * kbd)
+{
+ char * f_name = "init_keybindings()";
+
+ FILE * file = try_fopen(path, "r", world, f_name);
+ uint16_t lines, linemax;
+ char * err = "textfile_sizes() in init_keybindings() returns error.";
+ exit_err(textfile_sizes(file, &linemax, &lines), world, err);
+
+ char command[linemax + 1];
+ char * cmdptr;
+ struct KeyBinding ** loc_last_ptr = &kbd->kbs;
+ * loc_last_ptr = 0;
+ while (fgets(command, linemax + 1, file))
+ {
+ if ('\n' == command[0] || 0 == command[0])
+ {
+ break;
+ }
+ * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), world, f_name);
+ struct KeyBinding * kb_p = * loc_last_ptr;
+ kb_p->next = 0;
+ kb_p->key = atoi(command);
+ cmdptr = strchr(command, ' ') + 1;
+ kb_p->name = try_malloc(strlen(cmdptr), world, f_name);
+ memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
+ kb_p->name[strlen(cmdptr) - 1] = '\0';
+ loc_last_ptr = & kb_p->next;
+ }
+
+ try_fclose(file, world, f_name);
+
+ kbd->edit = 0;
+ kbd->select = 0;
+}
+
+
+
+extern void save_keybindings(struct World * world, char * path,
+ struct KeyBiData * kbd)
+{
+ char * f_name = "save_keybindings()";
+
+ char path_tmp[strlen(path) + 4 + 1];
+ sprintf(path_tmp, "%s_tmp", path);
+ FILE * file = try_fopen(path_tmp, "w", world, f_name);
+
+ uint16_t linemax = 0;
+ struct KeyBinding * kb_p = kbd->kbs;
+ while (0 != kb_p)
+ {
+ if (strlen(kb_p->name) > linemax)
+ {
+ linemax = strlen(kb_p->name);
+ }
+ kb_p = kb_p->next;
+ }
+ linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
+
+ char line[linemax];
+ kb_p = kbd->kbs;
+ while (0 != kb_p)
+ {
+ snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
+ try_fwrite(line, sizeof(char), strlen(line), file, world, f_name);
+ kb_p = kb_p->next;
+ }
+
+ try_fclose_unlink_rename(file, path_tmp, path, world, f_name);
+}
+
+
+
+extern void free_keybindings(struct KeyBinding * kb_start)
+{
+ if (0 == kb_start)
+ {
+ return;
+ }
+ struct KeyBinding * kb_p = kb_start->next;
+ if (0 != kb_p)
+ {
+ free_keybindings(kb_p);
+ }
+ free(kb_start->name);
+ free(kb_start);
+}
+
+
+
+extern void mod_selected_keyb(struct World * world, struct KeyBiData * kbd)