Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 2000-2011,2013 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 - 2000 |
| 31 | * |
| 32 | * $Id: railroad.c,v 1.21 2013/09/28 22:02:17 tom Exp $ |
| 33 | * |
| 34 | * A simple demo of the termcap interface. |
| 35 | */ |
| 36 | #define USE_TINFO |
| 37 | #include <test.priv.h> |
| 38 | |
| 39 | #if HAVE_TGETENT |
| 40 | |
| 41 | static char *wipeit; |
| 42 | static char *moveit; |
| 43 | static int length; |
| 44 | static int height; |
| 45 | |
| 46 | static char *finisC; |
| 47 | static char *finisS; |
| 48 | static char *finisU; |
| 49 | |
| 50 | static char *startC; |
| 51 | static char *startS; |
| 52 | static char *startU; |
| 53 | |
| 54 | static char *backup; |
| 55 | |
| 56 | static bool interrupted = FALSE; |
| 57 | |
| 58 | static |
| 59 | TPUTS_PROTO(outc, c) |
| 60 | { |
| 61 | int rc = OK; |
| 62 | |
| 63 | if (interrupted) { |
| 64 | char tmp = (char) c; |
| 65 | if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1) |
| 66 | rc = ERR; |
| 67 | } else { |
| 68 | if (putc(c, stdout) == EOF) |
| 69 | rc = ERR; |
| 70 | } |
| 71 | TPUTS_RETURN(rc); |
| 72 | } |
| 73 | |
| 74 | static void |
| 75 | PutChar(int ch) |
| 76 | { |
| 77 | putchar(ch); |
| 78 | fflush(stdout); |
| 79 | napms(moveit ? 10 : 50); /* not really termcap... */ |
| 80 | } |
| 81 | |
| 82 | static void |
| 83 | Backup(void) |
| 84 | { |
| 85 | tputs(backup != 0 ? backup : "\b", 1, outc); |
| 86 | } |
| 87 | |
| 88 | static void |
| 89 | MyShowCursor(int flag) |
| 90 | { |
| 91 | if (startC != 0 && finisC != 0) { |
| 92 | tputs(flag ? startC : finisC, 1, outc); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | StandOut(int flag) |
| 98 | { |
| 99 | if (startS != 0 && finisS != 0) { |
| 100 | tputs(flag ? startS : finisS, 1, outc); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | Underline(int flag) |
| 106 | { |
| 107 | if (startU != 0 && finisU != 0) { |
| 108 | tputs(flag ? startU : finisU, 1, outc); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | ShowSign(char *string) |
| 114 | { |
| 115 | char *base = string; |
| 116 | int ch, first, last; |
| 117 | |
| 118 | if (moveit != 0) { |
| 119 | tputs(tgoto(moveit, 0, height - 1), 1, outc); |
| 120 | tputs(wipeit, 1, outc); |
| 121 | } |
| 122 | |
| 123 | while (*string != 0) { |
| 124 | ch = *string; |
| 125 | if (ch != ' ') { |
| 126 | if (moveit != 0) { |
| 127 | for (first = length - 2; first >= (string - base); first--) { |
| 128 | if (first < length - 1) { |
| 129 | tputs(tgoto(moveit, first + 1, height - 1), 1, outc); |
| 130 | PutChar(' '); |
| 131 | } |
| 132 | tputs(tgoto(moveit, first, height - 1), 1, outc); |
| 133 | PutChar(ch); |
| 134 | } |
| 135 | } else { |
| 136 | last = ch; |
| 137 | if (isalpha(ch)) { |
| 138 | first = isupper(ch) ? 'A' : 'a'; |
| 139 | } else if (isdigit(ch)) { |
| 140 | first = '0'; |
| 141 | } else { |
| 142 | first = ch; |
| 143 | } |
| 144 | if (first < last) { |
| 145 | Underline(1); |
| 146 | while (first < last) { |
| 147 | PutChar(first); |
| 148 | Backup(); |
| 149 | first++; |
| 150 | } |
| 151 | Underline(0); |
| 152 | } |
| 153 | } |
| 154 | if (moveit != 0) |
| 155 | Backup(); |
| 156 | } |
| 157 | StandOut(1); |
| 158 | PutChar(ch); |
| 159 | StandOut(0); |
| 160 | fflush(stdout); |
| 161 | string++; |
| 162 | } |
| 163 | if (moveit != 0) |
| 164 | tputs(wipeit, 1, outc); |
| 165 | putchar('\n'); |
| 166 | } |
| 167 | |
| 168 | static void |
| 169 | cleanup(void) |
| 170 | { |
| 171 | Underline(0); |
| 172 | StandOut(0); |
| 173 | MyShowCursor(1); |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | onsig(int n GCC_UNUSED) |
| 178 | { |
| 179 | interrupted = TRUE; |
| 180 | cleanup(); |
| 181 | ExitProgram(EXIT_FAILURE); |
| 182 | } |
| 183 | |
| 184 | static void |
| 185 | railroad(char **args) |
| 186 | { |
| 187 | NCURSES_CONST char *name = getenv("TERM"); |
| 188 | char buffer[1024]; |
| 189 | char area[1024], *ap = area; |
| 190 | |
| 191 | if (name == 0) |
| 192 | name = "dumb"; |
| 193 | if (tgetent(buffer, name) >= 0) { |
| 194 | |
| 195 | wipeit = tgetstr("ce", &ap); |
| 196 | height = tgetnum("li"); |
| 197 | length = tgetnum("co"); |
| 198 | moveit = tgetstr("cm", &ap); |
| 199 | |
| 200 | if (wipeit == 0 |
| 201 | || moveit == 0 |
| 202 | || height <= 0 |
| 203 | || length <= 0) { |
| 204 | wipeit = 0; |
| 205 | moveit = 0; |
| 206 | height = 0; |
| 207 | length = 0; |
| 208 | } |
| 209 | |
| 210 | startS = tgetstr("so", &ap); |
| 211 | finisS = tgetstr("se", &ap); |
| 212 | |
| 213 | startU = tgetstr("us", &ap); |
| 214 | finisU = tgetstr("ue", &ap); |
| 215 | |
| 216 | backup = tgetstr("le", &ap); |
| 217 | |
| 218 | startC = tgetstr("ve", &ap); |
| 219 | finisC = tgetstr("vi", &ap); |
| 220 | |
| 221 | MyShowCursor(0); |
| 222 | |
| 223 | CATCHALL(onsig); |
| 224 | |
| 225 | while (*args) { |
| 226 | ShowSign(*args++); |
| 227 | } |
| 228 | MyShowCursor(1); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int |
| 233 | main(int argc, char *argv[]) |
| 234 | { |
| 235 | if (argc > 1) { |
| 236 | railroad(argv + 1); |
| 237 | } else { |
| 238 | static char world[] = "Hello World"; |
| 239 | static char *hello[] = |
| 240 | {world, 0}; |
| 241 | railroad(hello); |
| 242 | } |
| 243 | ExitProgram(EXIT_SUCCESS); |
| 244 | } |
| 245 | |
| 246 | #else |
| 247 | int |
| 248 | main(int argc GCC_UNUSED, |
| 249 | char *argv[]GCC_UNUSED) |
| 250 | { |
| 251 | printf("This program requires termcap\n"); |
| 252 | exit(EXIT_FAILURE); |
| 253 | } |
| 254 | #endif |