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