2 ** Copyright (C) 1997 Free Software Foundation, Inc.
4 ** This file is part of TACK.
6 ** TACK is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2, or (at your option)
11 ** TACK is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
16 ** You should have received a copy of the GNU General Public License
17 ** along with TACK; see the file COPYING. If not, write to
18 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 ** Boston, MA 02110-1301, USA
26 MODULE_ID("$Id: edit.c,v 1.11 2006/06/24 21:22:42 tom Exp $")
29 * Terminfo edit features
31 static void show_info(struct test_list *, int *, int *);
32 static void show_value(struct test_list *, int *, int *);
33 static void show_untested(struct test_list *, int *, int *);
34 static void show_changed(struct test_list *, int *, int *);
40 struct test_list edit_test_list[] = {
41 {MENU_CLEAR, 0, 0, 0, "i) display current terminfo", show_info, 0},
42 {0, 0, 0, 0, "w) write the current terminfo to a file", save_info, 0},
43 {SHOW_VALUE, 3, 0, 0, "v) show value of a selected cap", show_value, 0},
44 {SHOW_EDIT, 4, 0, 0, "e) edit value of a selected cap", show_value, 0},
45 {SHOW_DELETE, 3, 0, 0, "d) delete string", show_value, 0},
46 {0, 3, 0, 0, "m) show caps that have been modified", show_changed, 0},
47 {MENU_CLEAR + FLAG_CAN_TEST, 0, 0, 0, "c) show caps that can be tested", show_report, 0},
48 {MENU_CLEAR + FLAG_TESTED, 0, 0, 0, "t) show caps that have been tested", show_report, 0},
49 {MENU_CLEAR + FLAG_FUNCTION_KEY, 0, 0, 0, "f) show a list of function keys", show_report, 0},
50 {MENU_CLEAR, 0, 0, 0, "u) show caps defined that can not be tested", show_untested, 0},
51 {MENU_LAST, 0, 0, 0, 0, 0, 0}
54 static char change_pad_text[MAX_CHANGES][80];
55 static struct test_list change_pad_list[MAX_CHANGES] = {
56 {MENU_LAST, 0, 0, 0, 0, 0, 0}
59 static void build_change_menu(struct test_menu *);
60 static void change_one_entry(struct test_list *, int *, int *);
62 struct test_menu change_pad_menu = {
64 "Select cap name", "change", 0,
65 build_change_menu, change_pad_list, 0, 0, 0
68 static TERMTYPE original_term; /* terminal type description */
70 static char flag_boolean[BOOLCOUNT]; /* flags for booleans */
71 static char flag_numerics[NUMCOUNT]; /* flags for numerics */
72 static char *flag_strings; /* flags for strings */
73 static int *label_strings;
74 static int xon_index; /* Subscript for (xon) */
75 static int xon_shadow;
77 static int start_display; /* the display has just started */
78 static int display_lines; /* number of lines displayed */
83 if (flag_strings == 0) {
84 label_strings = (int *)calloc(MAX_STRINGS, sizeof(int));
85 flag_strings = (char *)calloc(MAX_STRINGS, sizeof(char));
90 ** send_info_string(str)
92 ** Return the terminfo string prefixed by the correct separator
101 if (display_lines == -1) {
105 if (len + char_count + 3 >= columns) {
106 if (start_display == 0) {
110 if (++display_lines > lines) {
111 ptext("-- more -- ");
119 if (len >= columns) {
120 /* if the terminal does not (am) then this loses */
122 display_lines += ((strlen(str) + 3) / columns) + 1;
131 if (start_display == 0) {
141 ** show_info(test_list, status, ch)
143 ** Display the current terminfo
147 struct test_list *t GCC_UNUSED,
148 int *state GCC_UNUSED,
156 for (i = 0; i < BOOLCOUNT; i++) {
157 if ((i == xon_index) ? xon_shadow : CUR Booleans[i]) {
158 send_info_string(boolnames[i], ch);
161 for (i = 0; i < NUMCOUNT; i++) {
162 if (CUR Numbers[i] >= 0) {
163 sprintf(buf, "%s#%d", numnames[i], CUR Numbers[i]);
164 send_info_string(buf, ch);
167 for (i = 0; i < MAX_STRINGS; i++) {
168 if (CUR Strings[i]) {
169 sprintf(buf, "%s=%s", STR_NAME(i),
170 print_expand(CUR Strings[i]));
171 send_info_string(buf, ch);
175 *ch = REQUEST_PROMPT;
179 ** save_info_string(str, fp)
181 ** Write the terminfo string prefixed by the correct separator
191 if (len + display_lines >= 77) {
192 if (display_lines > 0) {
193 (void) fprintf(fp, "\n\t");
197 if (display_lines > 0) {
198 (void) fprintf(fp, " ");
201 (void) fprintf(fp, "\t");
204 (void) fprintf(fp, "%s,", str);
205 display_lines += len + 1;
209 ** save_info(test_list, status, ch)
211 ** Write the current terminfo to a file
224 if ((fp = fopen(tty_basename, "w")) == (FILE *) NULL) {
225 (void) sprintf(temp, "can't open: %s", tty_basename);
227 generic_done_message(t, state, ch);
231 /* Note: ctime() returns a newline at the end of the string */
232 (void) fprintf(fp, "# Terminfo created by TACK for TERM=%s on %s",
233 tty_basename, ctime(&now));
234 (void) fprintf(fp, "%s|%s,\n", tty_basename, longname());
237 for (i = 0; i < BOOLCOUNT; i++) {
238 if (i == xon_index ? xon_shadow : CUR Booleans[i]) {
239 save_info_string(boolnames[i], fp);
242 for (i = 0; i < NUMCOUNT; i++) {
243 if (CUR Numbers[i] >= 0) {
244 sprintf(buf, "%s#%d", numnames[i], CUR Numbers[i]);
245 save_info_string(buf, fp);
248 for (i = 0; i < MAX_STRINGS; i++) {
249 if (CUR Strings[i]) {
250 sprintf(buf, "%s=%s", STR_NAME(i),
251 _nc_tic_expand(CUR Strings[i], TRUE, TRUE));
252 save_info_string(buf, fp);
255 (void) fprintf(fp, "\n");
257 sprintf(temp, "Terminfo saved as file: %s", tty_basename);
262 ** show_value(test_list, status, ch)
264 ** Display the value of a selected cap
269 int *state GCC_UNUSED,
272 struct name_table_entry const *nt;
278 ptext("enter name: ");
279 read_string(buf, 80);
280 if (buf[0] == '\0' || buf[1] == '\0') {
284 if (line_count + 2 >= lines) {
288 if ((nt = _nc_find_entry(buf, _nc_info_hash_table))) {
289 switch (nt->nte_type) {
291 if (op == SHOW_DELETE) {
292 if (nt->nte_index == xon_index) {
295 CUR Booleans[nt->nte_index] = 0;
299 b = nt->nte_index == xon_index ? xon_shadow :
300 CUR Booleans[nt->nte_index];
301 sprintf(temp, "boolean %s %s", buf,
302 b ? "True" : "False");
305 if (op == SHOW_DELETE) {
306 CUR Strings[nt->nte_index] = (char *) 0;
309 if (CUR Strings[nt->nte_index]) {
310 sprintf(temp, "string %s %s", buf,
311 expand(CUR Strings[nt->nte_index]));
313 sprintf(temp, "undefined string %s", buf);
317 if (op == SHOW_DELETE) {
318 CUR Numbers[nt->nte_index] = -1;
321 sprintf(temp, "numeric %s %d", buf,
322 CUR Numbers[nt->nte_index]);
325 sprintf(temp, "unknown");
330 sprintf(temp, "Cap not found: %s", buf);
334 if (op != SHOW_EDIT) {
337 if (nt->nte_type == BOOLEAN) {
338 ptextln("Value flipped");
339 if (nt->nte_index == xon_index) {
340 xon_shadow = !xon_shadow;
342 CUR Booleans[nt->nte_index] = !CUR Booleans[nt->nte_index];
346 ptextln("Enter new value");
347 read_string(buf, sizeof(buf));
349 switch (nt->nte_type) {
351 _nc_reset_input((FILE *) 0, buf);
352 _nc_trans_string(tmp, tmp + sizeof(tmp));
353 s = (char *)malloc(strlen(tmp) + 1);
355 CUR Strings[nt->nte_index] = s;
356 sprintf(temp, "new string value %s", nt->nte_name);
358 ptextln(expand(CUR Strings[nt->nte_index]));
361 if (sscanf(buf, "%d", &n) == 1) {
362 CUR Numbers[nt->nte_index] = n;
363 sprintf(temp, "new numeric value %s %d",
367 sprintf(temp, "Illegal number: %s", buf);
377 ** get_string_cap_byname(name, long_name)
379 ** Given a cap name, find the value
380 ** Errors are quietly ignored.
383 get_string_cap_byname(
385 const char **long_name)
387 struct name_table_entry const *nt;
389 if ((nt = _nc_find_entry(name, _nc_info_hash_table))) {
390 if (nt->nte_type == STRING) {
391 *long_name = strfnames[nt->nte_index];
392 return (CUR Strings[nt->nte_index]);
400 ** get_string_cap_byvalue(value)
402 ** Given a capability string, find its position in the data base.
403 ** Return the index or -1 if not found.
406 get_string_cap_byvalue(
412 for (i = 0; i < MAX_STRINGS; i++) {
413 if (CUR Strings[i] == value) {
417 /* search for translated strings */
418 for (i = 0; i < TM_last; i++) {
419 if (TM_string[i].value == value) {
420 return TM_string[i].index;
428 ** show_changed(test_list, status, ch)
430 ** Display a list of caps that have been changed.
434 struct test_list *t GCC_UNUSED,
435 int *state GCC_UNUSED,
438 int i, header = 1, v;
441 static char title[] = " old value cap new value";
444 for (i = 0; i < BOOLCOUNT; i++) {
445 v = (i == xon_index) ? xon_shadow : CUR Booleans[i];
446 if (original_term.Booleans[i] != v) {
451 sprintf(temp, "%30d %6s %d",
452 original_term.Booleans[i], boolnames[i], v);
456 for (i = 0; i < NUMCOUNT; i++) {
457 if (original_term.Numbers[i] != CUR Numbers[i]) {
462 sprintf(temp, "%30d %6s %d",
463 original_term.Numbers[i], numnames[i],
468 for (i = 0; i < MAX_STRINGS; i++) {
469 a = original_term.Strings[i] ? original_term.Strings[i] : "";
470 b = CUR Strings[i] ? CUR Strings[i] : "";
476 strcpy(abuf, _nc_tic_expand(a, TRUE, TRUE));
477 sprintf(temp, "%30s %6s %s", abuf, STR_NAME(i),
478 _nc_tic_expand(b, TRUE, TRUE));
483 ptextln("No changes");
486 *ch = REQUEST_PROMPT;
492 ** Return TRUE if the user has modified the terminfo
500 for (i = 0; i < BOOLCOUNT; i++) {
501 v = (i == xon_index) ? xon_shadow : CUR Booleans[i];
502 if (original_term.Booleans[i] != v) {
506 for (i = 0; i < NUMCOUNT; i++) {
507 if (original_term.Numbers[i] != CUR Numbers[i]) {
511 for (i = 0; i < MAX_STRINGS; i++) {
512 a = original_term.Strings[i] ? original_term.Strings[i] : "";
513 b = CUR Strings[i] ? CUR Strings[i] : "";
521 /*****************************************************************************
523 * Maintain the list of capabilities that can be tested
525 *****************************************************************************/
528 ** mark_cap(name, flag)
530 ** Mark the cap data base with the flag provided.
537 struct name_table_entry const *nt;
540 if ((nt = _nc_find_entry(name, _nc_info_hash_table))) {
541 switch (nt->nte_type) {
543 flag_boolean[nt->nte_index] |= flag;
546 flag_strings[nt->nte_index] |= flag;
549 flag_numerics[nt->nte_index] |= flag;
552 sprintf(temp, "unknown cap type (%s)", name);
557 sprintf(temp, "Cap not found: %s", name);
564 ** can_test(name-list, flags)
566 ** Scan the name list and get the names.
567 ** Enter each name into the can-test data base.
568 ** <space> ( and ) may be used as separators.
579 for (j = 0; (name[j] = ch = *s); s++) {
580 if (ch == ' ' || ch == ')' || ch == '(') {
583 mark_cap(name, flags);
591 mark_cap(name, flags);
597 ** cap_index(name-list, index-list)
599 ** Scan the name list and return a list of indexes.
600 ** <space> ( and ) may be used as separators.
601 ** This list is terminated with -1.
608 struct name_table_entry const *nt;
615 if (ch == ' ' || ch == ')' || ch == '(' || ch == 0) {
618 if ((nt = _nc_find_entry(name,
619 _nc_info_hash_table)) &&
620 (nt->nte_type == STRING)) {
621 *inx++ = nt->nte_index;
637 ** cap_match(name-list, cap)
639 ** Scan the name list and see if the cap is in the list.
640 ** Return TRUE if we find an exact match.
641 ** <space> ( and ) may be used as separators.
653 while ((s = strstr(names, cap))) {
654 c = (names == s) ? 0 : *(s - 1);
656 if ((c == 0 || c == ' ' || c == '(') &&
657 (t == 0 || t == ' ' || t == ')')) {
670 ** show_report(test_list, status, ch)
672 ** Display a list of caps that can be tested
677 int *state GCC_UNUSED,
682 const char **nx = malloc(BOOLCOUNT + NUMCOUNT + MAX_STRINGS);
685 flag = t->flags & 255;
687 for (i = 0; i < BOOLCOUNT; i++) {
688 if (flag_boolean[i] & flag) {
689 nx[nc++] = boolnames[i];
692 for (i = 0; i < NUMCOUNT; i++) {
693 if (flag_numerics[i] & flag) {
694 nx[nc++] = numnames[i];
697 for (i = 0; i < MAX_STRINGS; i++) {
698 if (flag_strings[i] & flag) {
699 nx[nc++] = STR_NAME(i);
703 for (i = 0; i < nc - 1; i++) {
704 for (j = i + 1; j < nc; j++) {
705 if (strcmp(nx[i], nx[j]) > 0) {
712 if (flag & FLAG_FUNCTION_KEY) {
713 ptextln("The following function keys can be tested:");
715 if (flag & FLAG_CAN_TEST) {
716 ptextln("The following capabilities can be tested:");
718 if (flag & FLAG_TESTED) {
719 ptextln("The following capabilities have been tested:");
722 for (i = 0; i < nc; i++) {
723 sprintf(temp, "%s ", nx[i]);
727 *ch = REQUEST_PROMPT;
732 ** show_untested(test_list, status, ch)
734 ** Display a list of caps that are defined but cannot be tested.
735 ** Don't bother to sort this list.
739 struct test_list *t GCC_UNUSED,
740 int *state GCC_UNUSED,
746 ptextln("Caps that are defined but cannot be tested:");
747 for (i = 0; i < BOOLCOUNT; i++) {
748 if (flag_boolean[i] == 0 && CUR Booleans[i]) {
749 sprintf(temp, "%s ", boolnames[i]);
753 for (i = 0; i < NUMCOUNT; i++) {
754 if (flag_numerics[i] == 0 && CUR Numbers[i] >= 0) {
755 sprintf(temp, "%s ", numnames[i]);
759 for (i = 0; i < MAX_STRINGS; i++) {
760 if (flag_strings[i] == 0 && CUR Strings[i]) {
761 sprintf(temp, "%s ", STR_NAME(i));
766 *ch = REQUEST_PROMPT;
772 ** Initialize the function key data base
779 struct name_table_entry const *nt;
783 _nc_copy_termtype(&original_term, &cur_term->type);
784 for (i = 0; i < BOOLCOUNT; i++) {
785 original_term.Booleans[i] = CUR Booleans[i];
787 for (i = 0; i < NUMCOUNT; i++) {
788 original_term.Numbers[i] = CUR Numbers[i];
790 /* scan for labels */
791 for (i = lc = 0; i < MAX_STRINGS; i++) {
792 original_term.Strings[i] = CUR Strings[i];
793 if (strncmp(STR_NAME(i), "lf", 2) == 0) {
794 flag_strings[i] |= FLAG_LABEL;
795 if (CUR Strings[i]) {
796 label_strings[lc++] = i;
800 /* scan for function keys */
801 for (i = 0; i < MAX_STRINGS; i++) {
802 const char *this_name = STR_NAME(i);
803 if ((this_name[0] == 'k') && strcmp(this_name, "kmous")) {
804 flag_strings[i] |= FLAG_FUNCTION_KEY;
806 for (j = 0; j < lc; j++) {
807 if (!strcmp(this_name,
808 STR_NAME(label_strings[j]))) {
809 lab = CUR Strings[label_strings[j]];
813 enter_key(this_name, CUR Strings[i], lab);
816 /* Lookup the translated strings */
817 for (i = 0; i < TM_last; i++) {
818 if ((nt = _nc_find_entry(TM_string[i].name,
819 _nc_info_hash_table)) && (nt->nte_type == STRING)) {
820 TM_string[i].index = nt->nte_index;
822 sprintf(temp, "TM_string lookup failed for: %s",
827 if ((nt = _nc_find_entry("xon", _nc_info_hash_table)) != 0) {
828 xon_index = nt->nte_index;
830 xon_shadow = xon_xoff;
835 ** change_one_entry(test_list, status, ch)
837 ** Change the padding on the selected cap
841 struct test_list *test,
845 struct name_table_entry const *nt;
846 int i, j, x, star, slash, v, dot, ch;
849 const char *current_string;
853 i = test->flags & 255;
855 /* read the cap name from the user */
856 ptext("enter name: ");
857 read_string(pad, 32);
858 if (pad[0] == '\0' || pad[1] == '\0') {
862 if ((nt = _nc_find_entry(pad, _nc_info_hash_table)) &&
863 (nt->nte_type == STRING)) {
865 current_string = CUR Strings[x];
867 sprintf(temp, "%s is not a string capability", pad);
869 generic_done_message(test, state, chp);
874 current_string = tx_cap[i];
875 strcpy(pad, STR_NAME(x));
877 if (!current_string) {
878 ptextln("That string is not currently defined. Please enter a new value, including the padding delay:");
879 read_string(buf, sizeof(buf));
880 _nc_reset_input((FILE *) 0, buf);
881 _nc_trans_string(pad, pad + sizeof(pad));
882 t = (char *)malloc(strlen(pad) + 1);
885 sprintf(temp, "new string value %s", STR_NAME(x));
890 sprintf(buf, "Current value: (%s) %s", pad, _nc_tic_expand(current_string, TRUE, TRUE));
892 ptextln("Enter new pad. 0 for no pad. CR for no change.");
893 read_string(buf, 32);
894 if (buf[0] == '\0' || (buf[1] == '\0' && isalpha(UChar(buf[0])))) {
898 star = slash = FALSE;
899 for (j = v = dot = 0; (ch = buf[j]); j++) {
900 if (ch >= '0' && ch <= '9') {
901 v = ch - '0' + v * 10;
905 } else if (ch == '*') {
907 } else if (ch == '/') {
909 } else if (ch == '.') {
912 sprintf(temp, "Illegal character: %c", ch);
914 ptext("General format: 99.9*/ ");
915 generic_done_message(test, state, chp);
924 sprintf(pad, "%d.%d%s%s", v / 10, v % 10,
925 star ? "*" : "", slash ? "/" : "");
927 sprintf(pad, "%d%s%s",
928 v, star ? "*" : "", slash ? "/" : "");
932 for (v = 0; (ch = *t = *s++); t++) {
933 if (v == '$' && ch == '<') {
934 while ((ch = *s++) && (ch != '>'));
935 for (p = pad; (*++t = *p++); );
937 while ((*t++ = *s++));
944 sprintf(t, "$<%s>", pad);
946 if ((t = (char *)malloc(strlen(buf) + 1))) {
953 generic_done_message(test, state, chp);
957 ** build_change_menu(menu_list)
959 ** Build the change pad menu list
968 for (i = j = 0; i < txp; i++) {
969 if ((k = tx_index[i]) >= 0) {
970 s = _nc_tic_expand(tx_cap[i], TRUE, TRUE);
972 sprintf(change_pad_text[j], "%c) (%s) %s",
973 'a' + j, STR_NAME(k), s);
974 change_pad_list[j].flags = i;
975 change_pad_list[j].lines_needed = 4;
976 change_pad_list[j].menu_entry = change_pad_text[j];
977 change_pad_list[j].test_procedure = change_one_entry;
981 strcpy(change_pad_text[j], "z) enter name");
982 change_pad_list[j].flags = 255;
983 change_pad_list[j].lines_needed = 4;
984 change_pad_list[j].menu_entry = change_pad_text[j];
985 change_pad_list[j].test_procedure = change_one_entry;
987 change_pad_list[j].flags = MENU_LAST;
990 ptextln(m->menu_title);