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.10 2005/09/17 19:49:16 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[STRCOUNT]; /* flags for strings */
73 static int xon_index; /* Subscript for (xon) */
74 static int xon_shadow;
76 static int start_display; /* the display has just started */
77 static int display_lines; /* number of lines displayed */
80 ** send_info_string(str)
82 ** Return the terminfo string prefixed by the correct separator
91 if (display_lines == -1) {
95 if (len + char_count + 3 >= columns) {
96 if (start_display == 0) {
100 if (++display_lines > lines) {
101 ptext("-- more -- ");
109 if (len >= columns) {
110 /* if the terminal does not (am) then this loses */
112 display_lines += ((strlen(str) + 3) / columns) + 1;
121 if (start_display == 0) {
131 ** show_info(test_list, status, ch)
133 ** Display the current terminfo
137 struct test_list *t GCC_UNUSED,
138 int *state GCC_UNUSED,
146 for (i = 0; i < BOOLCOUNT; i++) {
147 if ((i == xon_index) ? xon_shadow : CUR Booleans[i]) {
148 send_info_string(boolnames[i], ch);
151 for (i = 0; i < NUMCOUNT; i++) {
152 if (CUR Numbers[i] >= 0) {
153 sprintf(buf, "%s#%d", numnames[i], CUR Numbers[i]);
154 send_info_string(buf, ch);
157 for (i = 0; i < STRCOUNT; i++) {
158 if (CUR Strings[i]) {
159 sprintf(buf, "%s=%s", strnames[i],
160 print_expand(CUR Strings[i]));
161 send_info_string(buf, ch);
165 *ch = REQUEST_PROMPT;
169 ** save_info_string(str, fp)
171 ** Write the terminfo string prefixed by the correct separator
181 if (len + display_lines >= 77) {
182 if (display_lines > 0) {
183 (void) fprintf(fp, "\n\t");
187 if (display_lines > 0) {
188 (void) fprintf(fp, " ");
191 (void) fprintf(fp, "\t");
194 (void) fprintf(fp, "%s,", str);
195 display_lines += len + 1;
199 ** save_info(test_list, status, ch)
201 ** Write the current terminfo to a file
214 if ((fp = fopen(tty_basename, "w")) == (FILE *) NULL) {
215 (void) sprintf(temp, "can't open: %s", tty_basename);
217 generic_done_message(t, state, ch);
221 /* Note: ctime() returns a newline at the end of the string */
222 (void) fprintf(fp, "# Terminfo created by TACK for TERM=%s on %s",
223 tty_basename, ctime(&now));
224 (void) fprintf(fp, "%s|%s,\n", tty_basename, longname());
227 for (i = 0; i < BOOLCOUNT; i++) {
228 if (i == xon_index ? xon_shadow : CUR Booleans[i]) {
229 save_info_string(boolnames[i], fp);
232 for (i = 0; i < NUMCOUNT; i++) {
233 if (CUR Numbers[i] >= 0) {
234 sprintf(buf, "%s#%d", numnames[i], CUR Numbers[i]);
235 save_info_string(buf, fp);
238 for (i = 0; i < STRCOUNT; i++) {
239 if (CUR Strings[i]) {
240 sprintf(buf, "%s=%s", strnames[i],
241 _nc_tic_expand(CUR Strings[i], TRUE, TRUE));
242 save_info_string(buf, fp);
245 (void) fprintf(fp, "\n");
247 sprintf(temp, "Terminfo saved as file: %s", tty_basename);
252 ** show_value(test_list, status, ch)
254 ** Display the value of a selected cap
259 int *state GCC_UNUSED,
262 struct name_table_entry const *nt;
268 ptext("enter name: ");
269 read_string(buf, 80);
270 if (buf[0] == '\0' || buf[1] == '\0') {
274 if (line_count + 2 >= lines) {
278 if ((nt = _nc_find_entry(buf, _nc_info_hash_table))) {
279 switch (nt->nte_type) {
281 if (op == SHOW_DELETE) {
282 if (nt->nte_index == xon_index) {
285 CUR Booleans[nt->nte_index] = 0;
289 b = nt->nte_index == xon_index ? xon_shadow :
290 CUR Booleans[nt->nte_index];
291 sprintf(temp, "boolean %s %s", buf,
292 b ? "True" : "False");
295 if (op == SHOW_DELETE) {
296 CUR Strings[nt->nte_index] = (char *) 0;
299 if (CUR Strings[nt->nte_index]) {
300 sprintf(temp, "string %s %s", buf,
301 expand(CUR Strings[nt->nte_index]));
303 sprintf(temp, "undefined string %s", buf);
307 if (op == SHOW_DELETE) {
308 CUR Numbers[nt->nte_index] = -1;
311 sprintf(temp, "numeric %s %d", buf,
312 CUR Numbers[nt->nte_index]);
315 sprintf(temp, "unknown");
320 sprintf(temp, "Cap not found: %s", buf);
324 if (op != SHOW_EDIT) {
327 if (nt->nte_type == BOOLEAN) {
328 ptextln("Value flipped");
329 if (nt->nte_index == xon_index) {
330 xon_shadow = !xon_shadow;
332 CUR Booleans[nt->nte_index] = !CUR Booleans[nt->nte_index];
336 ptextln("Enter new value");
337 read_string(buf, sizeof(buf));
339 switch (nt->nte_type) {
341 _nc_reset_input((FILE *) 0, buf);
342 _nc_trans_string(tmp, tmp + sizeof(tmp));
343 s = (char *)malloc(strlen(tmp) + 1);
345 CUR Strings[nt->nte_index] = s;
346 sprintf(temp, "new string value %s", nt->nte_name);
348 ptextln(expand(CUR Strings[nt->nte_index]));
351 if (sscanf(buf, "%d", &n) == 1) {
352 CUR Numbers[nt->nte_index] = n;
353 sprintf(temp, "new numeric value %s %d",
357 sprintf(temp, "Illegal number: %s", buf);
367 ** get_string_cap_byname(name, long_name)
369 ** Given a cap name, find the value
370 ** Errors are quietly ignored.
373 get_string_cap_byname(
375 const char **long_name)
377 struct name_table_entry const *nt;
379 if ((nt = _nc_find_entry(name, _nc_info_hash_table))) {
380 if (nt->nte_type == STRING) {
381 *long_name = strfnames[nt->nte_index];
382 return (CUR Strings[nt->nte_index]);
390 ** get_string_cap_byvalue(value)
392 ** Given a capability string, find its position in the data base.
393 ** Return the index or -1 if not found.
396 get_string_cap_byvalue(
402 for (i = 0; i < STRCOUNT; i++) {
403 if (CUR Strings[i] == value) {
407 /* search for translated strings */
408 for (i = 0; i < TM_last; i++) {
409 if (TM_string[i].value == value) {
410 return TM_string[i].index;
418 ** show_changed(test_list, status, ch)
420 ** Display a list of caps that have been changed.
424 struct test_list *t GCC_UNUSED,
425 int *state GCC_UNUSED,
428 int i, header = 1, v;
431 static char title[] = " old value cap new value";
434 for (i = 0; i < BOOLCOUNT; i++) {
435 v = (i == xon_index) ? xon_shadow : CUR Booleans[i];
436 if (original_term.Booleans[i] != v) {
441 sprintf(temp, "%30d %6s %d",
442 original_term.Booleans[i], boolnames[i], v);
446 for (i = 0; i < NUMCOUNT; i++) {
447 if (original_term.Numbers[i] != CUR Numbers[i]) {
452 sprintf(temp, "%30d %6s %d",
453 original_term.Numbers[i], numnames[i],
458 for (i = 0; i < STRCOUNT; i++) {
459 a = original_term.Strings[i] ? original_term.Strings[i] : "";
460 b = CUR Strings[i] ? CUR Strings[i] : "";
466 strcpy(abuf, _nc_tic_expand(a, TRUE, TRUE));
467 sprintf(temp, "%30s %6s %s", abuf, strnames[i],
468 _nc_tic_expand(b, TRUE, TRUE));
473 ptextln("No changes");
476 *ch = REQUEST_PROMPT;
482 ** Return TRUE if the user has modified the terminfo
490 for (i = 0; i < BOOLCOUNT; i++) {
491 v = (i == xon_index) ? xon_shadow : CUR Booleans[i];
492 if (original_term.Booleans[i] != v) {
496 for (i = 0; i < NUMCOUNT; i++) {
497 if (original_term.Numbers[i] != CUR Numbers[i]) {
501 for (i = 0; i < STRCOUNT; i++) {
502 a = original_term.Strings[i] ? original_term.Strings[i] : "";
503 b = CUR Strings[i] ? CUR Strings[i] : "";
511 /*****************************************************************************
513 * Maintain the list of capabilities that can be tested
515 *****************************************************************************/
518 ** mark_cap(name, flag)
520 ** Mark the cap data base with the flag provided.
527 struct name_table_entry const *nt;
529 if ((nt = _nc_find_entry(name, _nc_info_hash_table))) {
530 switch (nt->nte_type) {
532 flag_boolean[nt->nte_index] |= flag;
535 flag_strings[nt->nte_index] |= flag;
538 flag_numerics[nt->nte_index] |= flag;
541 sprintf(temp, "unknown cap type (%s)", name);
546 sprintf(temp, "Cap not found: %s", name);
553 ** can_test(name-list, flags)
555 ** Scan the name list and get the names.
556 ** Enter each name into the can-test data base.
557 ** <space> ( and ) may be used as separators.
568 for (j = 0; (name[j] = ch = *s); s++) {
569 if (ch == ' ' || ch == ')' || ch == '(') {
572 mark_cap(name, flags);
580 mark_cap(name, flags);
586 ** cap_index(name-list, index-list)
588 ** Scan the name list and return a list of indexes.
589 ** <space> ( and ) may be used as separators.
590 ** This list is terminated with -1.
597 struct name_table_entry const *nt;
604 if (ch == ' ' || ch == ')' || ch == '(' || ch == 0) {
607 if ((nt = _nc_find_entry(name,
608 _nc_info_hash_table)) &&
609 (nt->nte_type == STRING)) {
610 *inx++ = nt->nte_index;
626 ** cap_match(name-list, cap)
628 ** Scan the name list and see if the cap is in the list.
629 ** Return TRUE if we find an exact match.
630 ** <space> ( and ) may be used as separators.
642 while ((s = strstr(names, cap))) {
643 c = (names == s) ? 0 : *(s - 1);
645 if ((c == 0 || c == ' ' || c == '(') &&
646 (t == 0 || t == ' ' || t == ')')) {
659 ** show_report(test_list, status, ch)
661 ** Display a list of caps that can be tested
666 int *state GCC_UNUSED,
671 const char *nx[BOOLCOUNT + NUMCOUNT + STRCOUNT];
673 flag = t->flags & 255;
675 for (i = 0; i < BOOLCOUNT; i++) {
676 if (flag_boolean[i] & flag) {
677 nx[nc++] = boolnames[i];
680 for (i = 0; i < NUMCOUNT; i++) {
681 if (flag_numerics[i] & flag) {
682 nx[nc++] = numnames[i];
685 for (i = 0; i < STRCOUNT; i++) {
686 if (flag_strings[i] & flag) {
687 nx[nc++] = strnames[i];
691 for (i = 0; i < nc - 1; i++) {
692 for (j = i + 1; j < nc; j++) {
693 if (strcmp(nx[i], nx[j]) > 0) {
700 if (flag & FLAG_FUNCTION_KEY) {
701 ptextln("The following function keys can be tested:");
703 if (flag & FLAG_CAN_TEST) {
704 ptextln("The following capabilities can be tested:");
706 if (flag & FLAG_TESTED) {
707 ptextln("The following capabilities have been tested:");
710 for (i = 0; i < nc; i++) {
711 sprintf(temp, "%s ", nx[i]);
715 *ch = REQUEST_PROMPT;
719 ** show_untested(test_list, status, ch)
721 ** Display a list of caps that are defined but cannot be tested.
722 ** Don't bother to sort this list.
726 struct test_list *t GCC_UNUSED,
727 int *state GCC_UNUSED,
732 ptextln("Caps that are defined but cannot be tested:");
733 for (i = 0; i < BOOLCOUNT; i++) {
734 if (flag_boolean[i] == 0 && CUR Booleans[i]) {
735 sprintf(temp, "%s ", boolnames[i]);
739 for (i = 0; i < NUMCOUNT; i++) {
740 if (flag_numerics[i] == 0 && CUR Numbers[i] >= 0) {
741 sprintf(temp, "%s ", numnames[i]);
745 for (i = 0; i < STRCOUNT; i++) {
746 if (flag_strings[i] == 0 && CUR Strings[i]) {
747 sprintf(temp, "%s ", strnames[i]);
752 *ch = REQUEST_PROMPT;
758 ** Initialize the function key data base
765 struct name_table_entry const *nt;
766 int label_strings[STRCOUNT];
768 _nc_copy_termtype(&original_term, &cur_term->type);
769 for (i = 0; i < BOOLCOUNT; i++) {
770 original_term.Booleans[i] = CUR Booleans[i];
772 for (i = 0; i < NUMCOUNT; i++) {
773 original_term.Numbers[i] = CUR Numbers[i];
775 /* scan for labels */
776 for (i = lc = 0; i < STRCOUNT; i++) {
777 original_term.Strings[i] = CUR Strings[i];
778 if (strncmp(strnames[i], "lf", 2) == 0) {
779 flag_strings[i] |= FLAG_LABEL;
780 if (CUR Strings[i]) {
781 label_strings[lc++] = i;
785 /* scan for function keys */
786 for (i = 0; i < STRCOUNT; i++) {
787 if ((strnames[i][0] == 'k') && strcmp(strnames[i], "kmous")) {
788 flag_strings[i] |= FLAG_FUNCTION_KEY;
790 for (j = 0; j < lc; j++) {
791 if (!strcmp(&strnames[i][1],
792 &strnames[label_strings[j]][1])) {
793 lab = CUR Strings[label_strings[j]];
797 enter_key(strnames[i], CUR Strings[i], lab);
800 /* Lookup the translated strings */
801 for (i = 0; i < TM_last; i++) {
802 if ((nt = _nc_find_entry(TM_string[i].name,
803 _nc_info_hash_table)) && (nt->nte_type == STRING)) {
804 TM_string[i].index = nt->nte_index;
806 sprintf(temp, "TM_string lookup failed for: %s",
811 if ((nt = _nc_find_entry("xon", _nc_info_hash_table)) != 0) {
812 xon_index = nt->nte_index;
814 xon_shadow = xon_xoff;
818 ** change_one_entry(test_list, status, ch)
820 ** Change the padding on the selected cap
824 struct test_list *test,
828 struct name_table_entry const *nt;
829 int i, j, x, star, slash, v, dot, ch;
832 const char *current_string;
836 i = test->flags & 255;
838 /* read the cap name from the user */
839 ptext("enter name: ");
840 read_string(pad, 32);
841 if (pad[0] == '\0' || pad[1] == '\0') {
845 if ((nt = _nc_find_entry(pad, _nc_info_hash_table)) &&
846 (nt->nte_type == STRING)) {
848 current_string = CUR Strings[x];
850 sprintf(temp, "%s is not a string capability", pad);
852 generic_done_message(test, state, chp);
857 current_string = tx_cap[i];
858 strcpy(pad, strnames[x]);
860 if (!current_string) {
861 ptextln("That string is not currently defined. Please enter a new value, including the padding delay:");
862 read_string(buf, sizeof(buf));
863 _nc_reset_input((FILE *) 0, buf);
864 _nc_trans_string(pad, pad + sizeof(pad));
865 t = (char *)malloc(strlen(pad) + 1);
868 sprintf(temp, "new string value %s", strnames[x]);
873 sprintf(buf, "Current value: (%s) %s", pad, _nc_tic_expand(current_string, TRUE, TRUE));
875 ptextln("Enter new pad. 0 for no pad. CR for no change.");
876 read_string(buf, 32);
877 if (buf[0] == '\0' || (buf[1] == '\0' && isalpha(UChar(buf[0])))) {
881 star = slash = FALSE;
882 for (j = v = dot = 0; (ch = buf[j]); j++) {
883 if (ch >= '0' && ch <= '9') {
884 v = ch - '0' + v * 10;
888 } else if (ch == '*') {
890 } else if (ch == '/') {
892 } else if (ch == '.') {
895 sprintf(temp, "Illegal character: %c", ch);
897 ptext("General format: 99.9*/ ");
898 generic_done_message(test, state, chp);
907 sprintf(pad, "%d.%d%s%s", v / 10, v % 10,
908 star ? "*" : "", slash ? "/" : "");
910 sprintf(pad, "%d%s%s",
911 v, star ? "*" : "", slash ? "/" : "");
915 for (v = 0; (ch = *t = *s++); t++) {
916 if (v == '$' && ch == '<') {
917 while ((ch = *s++) && (ch != '>'));
918 for (p = pad; (*++t = *p++); );
920 while ((*t++ = *s++));
927 sprintf(t, "$<%s>", pad);
929 if ((t = (char *)malloc(strlen(buf) + 1))) {
936 generic_done_message(test, state, chp);
940 ** build_change_menu(menu_list)
942 ** Build the change pad menu list
951 for (i = j = 0; i < txp; i++) {
952 if ((k = tx_index[i]) >= 0) {
953 s = _nc_tic_expand(tx_cap[i], TRUE, TRUE);
955 sprintf(change_pad_text[j], "%c) (%s) %s",
956 'a' + j, strnames[k], s);
957 change_pad_list[j].flags = i;
958 change_pad_list[j].lines_needed = 4;
959 change_pad_list[j].menu_entry = change_pad_text[j];
960 change_pad_list[j].test_procedure = change_one_entry;
964 strcpy(change_pad_text[j], "z) enter name");
965 change_pad_list[j].flags = 255;
966 change_pad_list[j].lines_needed = 4;
967 change_pad_list[j].menu_entry = change_pad_text[j];
968 change_pad_list[j].test_procedure = change_one_entry;
970 change_pad_list[j].flags = MENU_LAST;
973 ptextln(m->menu_title);