| 1 | /**************************************************************************** | 
|---|
| 2 | * Copyright (c) 2005 Free Software Foundation, Inc.                        * | 
|---|
| 3 | *                                                                          * | 
|---|
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a  * | 
|---|
| 5 | * copy of this software and associated documentation files (the            * | 
|---|
| 6 | * "Software"), to deal in the Software without restriction, including      * | 
|---|
| 7 | * without limitation the rights to use, copy, modify, merge, publish,      * | 
|---|
| 8 | * distribute, distribute with modifications, sublicense, and/or sell       * | 
|---|
| 9 | * copies of the Software, and to permit persons to whom the Software is    * | 
|---|
| 10 | * furnished to do so, subject to the following conditions:                 * | 
|---|
| 11 | *                                                                          * | 
|---|
| 12 | * The above copyright notice and this permission notice shall be included  * | 
|---|
| 13 | * in all copies or substantial portions of the Software.                   * | 
|---|
| 14 | *                                                                          * | 
|---|
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  * | 
|---|
| 16 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               * | 
|---|
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   * | 
|---|
| 18 | * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   * | 
|---|
| 19 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    * | 
|---|
| 20 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    * | 
|---|
| 21 | * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               * | 
|---|
| 22 | *                                                                          * | 
|---|
| 23 | * Except as contained in this notice, the name(s) of the above copyright   * | 
|---|
| 24 | * holders shall not be used in advertising or otherwise to promote the     * | 
|---|
| 25 | * sale, use or other dealings in this Software without prior written       * | 
|---|
| 26 | * authorization.                                                           * | 
|---|
| 27 | ****************************************************************************/ | 
|---|
| 28 |  | 
|---|
| 29 | /* | 
|---|
| 30 | * Author: Thomas E. Dickey | 
|---|
| 31 | * | 
|---|
| 32 | * $Id: demo_termcap.c,v 1.2 2005/04/30 14:57:54 tom Exp $ | 
|---|
| 33 | * | 
|---|
| 34 | * A simple demo of the termcap interface. | 
|---|
| 35 | */ | 
|---|
| 36 | #include <test.priv.h> | 
|---|
| 37 |  | 
|---|
| 38 | #define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0) | 
|---|
| 39 |  | 
|---|
| 40 | static void | 
|---|
| 41 | dumpit(char *cap) | 
|---|
| 42 | { | 
|---|
| 43 | /* | 
|---|
| 44 | * One of the limitations of the termcap interface is that the library | 
|---|
| 45 | * cannot determine the size of the buffer passed via tgetstr(), nor the | 
|---|
| 46 | * amount of space remaining.  This demo simply reuses the whole buffer | 
|---|
| 47 | * for each call; a normal termcap application would try to use the buffer | 
|---|
| 48 | * to hold all of the strings extracted from the terminal entry. | 
|---|
| 49 | */ | 
|---|
| 50 | char area[1024], *ap = area; | 
|---|
| 51 | char *str; | 
|---|
| 52 | int num; | 
|---|
| 53 |  | 
|---|
| 54 | if ((str = tgetstr(cap, &ap)) != 0) { | 
|---|
| 55 | /* | 
|---|
| 56 | * Note that the strings returned are mostly terminfo format, since | 
|---|
| 57 | * ncurses does not convert except for a handful of special cases. | 
|---|
| 58 | */ | 
|---|
| 59 | printf("str %s = ", cap); | 
|---|
| 60 | while (*str != 0) { | 
|---|
| 61 | int ch = UChar(*str++); | 
|---|
| 62 | switch (ch) { | 
|---|
| 63 | case '\177': | 
|---|
| 64 | fputs("^?", stdout); | 
|---|
| 65 | break; | 
|---|
| 66 | case '\033': | 
|---|
| 67 | fputs("\\E", stdout); | 
|---|
| 68 | break; | 
|---|
| 69 | case '\b': | 
|---|
| 70 | fputs("\\b", stdout); | 
|---|
| 71 | break; | 
|---|
| 72 | case '\f': | 
|---|
| 73 | fputs("\\f", stdout); | 
|---|
| 74 | break; | 
|---|
| 75 | case '\n': | 
|---|
| 76 | fputs("\\n", stdout); | 
|---|
| 77 | break; | 
|---|
| 78 | case '\r': | 
|---|
| 79 | fputs("\\r", stdout); | 
|---|
| 80 | break; | 
|---|
| 81 | case ' ': | 
|---|
| 82 | fputs("\\s", stdout); | 
|---|
| 83 | break; | 
|---|
| 84 | case '\t': | 
|---|
| 85 | fputs("\\t", stdout); | 
|---|
| 86 | break; | 
|---|
| 87 | case '^': | 
|---|
| 88 | fputs("\\^", stdout); | 
|---|
| 89 | break; | 
|---|
| 90 | case ':': | 
|---|
| 91 | fputs("\\072", stdout); | 
|---|
| 92 | break; | 
|---|
| 93 | case '\\': | 
|---|
| 94 | fputs("\\\\", stdout); | 
|---|
| 95 | break; | 
|---|
| 96 | default: | 
|---|
| 97 | if (isgraph(ch)) | 
|---|
| 98 | fputc(ch, stdout); | 
|---|
| 99 | else if (ch < 32) | 
|---|
| 100 | printf("^%c", ch + '@'); | 
|---|
| 101 | else | 
|---|
| 102 | printf("\\%03o", ch); | 
|---|
| 103 | break; | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 | printf("\n"); | 
|---|
| 107 | } else if ((num = tgetnum(cap)) >= 0) { | 
|---|
| 108 | printf("num %s = %d\n", cap, num); | 
|---|
| 109 | } else if ((num = tgetflag(cap)) != 0) { | 
|---|
| 110 | printf("flg %s\n", cap); | 
|---|
| 111 | } | 
|---|
| 112 | fflush(stdout); | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | static void | 
|---|
| 116 | demo_termcap(char *name) | 
|---|
| 117 | { | 
|---|
| 118 | char buffer[1024]; | 
|---|
| 119 |  | 
|---|
| 120 | printf("Terminal type %s\n", name); | 
|---|
| 121 | if (tgetent(buffer, name)) { | 
|---|
| 122 | char cap[3]; | 
|---|
| 123 | int c1, c2; | 
|---|
| 124 |  | 
|---|
| 125 | cap[2] = 0; | 
|---|
| 126 | for (c1 = 0; c1 < 256; ++c1) { | 
|---|
| 127 | cap[0] = c1; | 
|---|
| 128 | if (isCapName(c1)) { | 
|---|
| 129 | for (c2 = 0; c2 < 256; ++c2) { | 
|---|
| 130 | cap[1] = c2; | 
|---|
| 131 | if (isCapName(c2)) { | 
|---|
| 132 | dumpit(cap); | 
|---|
| 133 | } | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | } | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | int | 
|---|
| 141 | main(int argc, char *argv[]) | 
|---|
| 142 | { | 
|---|
| 143 | int n; | 
|---|
| 144 | char *name; | 
|---|
| 145 |  | 
|---|
| 146 | if (argc > 1) { | 
|---|
| 147 | for (n = 1; n < argc; ++n) { | 
|---|
| 148 | demo_termcap(argv[n]); | 
|---|
| 149 | } | 
|---|
| 150 | } else if ((name = getenv("TERM")) != 0) { | 
|---|
| 151 | demo_termcap(name); | 
|---|
| 152 | } else { | 
|---|
| 153 | demo_termcap("dumb"); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | ExitProgram(EXIT_SUCCESS); | 
|---|
| 157 | } | 
|---|