missing ncurses sources
[tomato.git] / release / src / router / libncurses / test / railroad.c
blob973ec46a38de5879730ac805247e7999148d46f8
1 /****************************************************************************
2 * Copyright (c) 2000-2008,2009 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 ****************************************************************************/
30 * Author: Thomas E. Dickey - 2000
32 * $Id: railroad.c,v 1.19 2009/10/24 21:37:56 tom Exp $
34 * A simple demo of the termcap interface.
36 #define USE_TINFO
37 #include <test.priv.h>
39 #if HAVE_TGETENT
41 static char *wipeit;
42 static char *moveit;
43 static int length;
44 static int height;
46 static char *finisC;
47 static char *finisS;
48 static char *finisU;
50 static char *startC;
51 static char *startS;
52 static char *startU;
54 static char *backup;
56 static bool interrupted = FALSE;
58 static int
59 outc(TPUTS_ARG c)
61 int rc = OK;
63 if (interrupted) {
64 char tmp = (char) c;
65 if (write(STDOUT_FILENO, &tmp, 1) == -1)
66 rc = ERR;
67 } else {
68 if (putc(c, stdout) == EOF)
69 rc = ERR;
71 return rc;
74 static void
75 PutChar(int ch)
77 putchar(ch);
78 fflush(stdout);
79 napms(moveit ? 10 : 50); /* not really termcap... */
82 static void
83 Backup(void)
85 tputs(backup != 0 ? backup : "\b", 1, outc);
88 static void
89 MyShowCursor(int flag)
91 if (startC != 0 && finisC != 0) {
92 tputs(flag ? startC : finisC, 1, outc);
96 static void
97 StandOut(int flag)
99 if (startS != 0 && finisS != 0) {
100 tputs(flag ? startS : finisS, 1, outc);
104 static void
105 Underline(int flag)
107 if (startU != 0 && finisU != 0) {
108 tputs(flag ? startU : finisU, 1, outc);
112 static void
113 ShowSign(char *string)
115 char *base = string;
116 int ch, first, last;
118 if (moveit != 0) {
119 tputs(tgoto(moveit, 0, height - 1), 1, outc);
120 tputs(wipeit, 1, outc);
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(' ');
132 tputs(tgoto(moveit, first, height - 1), 1, outc);
133 PutChar(ch);
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;
144 if (first < last) {
145 Underline(1);
146 while (first < last) {
147 PutChar(first);
148 Backup();
149 first++;
151 Underline(0);
154 if (moveit != 0)
155 Backup();
157 StandOut(1);
158 PutChar(ch);
159 StandOut(0);
160 fflush(stdout);
161 string++;
163 if (moveit != 0)
164 tputs(wipeit, 1, outc);
165 putchar('\n');
168 static void
169 cleanup(void)
171 Underline(0);
172 StandOut(0);
173 MyShowCursor(1);
176 static void
177 onsig(int n GCC_UNUSED)
179 interrupted = TRUE;
180 cleanup();
181 ExitProgram(EXIT_FAILURE);
184 static void
185 railroad(char **args)
187 NCURSES_CONST char *name = getenv("TERM");
188 char buffer[1024];
189 char area[1024], *ap = area;
191 if (name == 0)
192 name = "dumb";
193 if (tgetent(buffer, name) >= 0) {
195 wipeit = tgetstr("ce", &ap);
196 height = tgetnum("li");
197 length = tgetnum("co");
198 moveit = tgetstr("cm", &ap);
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;
210 startS = tgetstr("so", &ap);
211 finisS = tgetstr("se", &ap);
213 startU = tgetstr("us", &ap);
214 finisU = tgetstr("ue", &ap);
216 backup = tgetstr("le", &ap);
218 startC = tgetstr("ve", &ap);
219 finisC = tgetstr("vi", &ap);
221 MyShowCursor(0);
223 CATCHALL(onsig);
225 while (*args) {
226 ShowSign(*args++);
228 MyShowCursor(1);
233 main(int argc, char *argv[])
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);
243 ExitProgram(EXIT_SUCCESS);
246 #else
248 main(int argc GCC_UNUSED,
249 char *argv[]GCC_UNUSED)
251 printf("This program requires termcap\n");
252 exit(EXIT_FAILURE);
254 #endif