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