]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/railroad.c
90eac981941161d0a298f3b209e02fbe5d208026
[ncurses.git] / test / railroad.c
1 /****************************************************************************
2  * Copyright (c) 2000 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 <dickey@clark.net> 2000
31  *
32  * $Id: railroad.c,v 1.4 2000/10/15 00:21:33 tom Exp $
33  *
34  * A simple demo of the termcap interface.
35  */
36 #include <test.priv.h>
37
38 #include <termcap.h>
39 #include <ctype.h>
40 #include <signal.h>
41
42 static char *wipeit;
43 static char *moveit;
44 static int length;
45 static int height;
46
47 static char *finisC;
48 static char *finisS;
49 static char *finisU;
50
51 static char *startC;
52 static char *startS;
53 static char *startU;
54
55 static char *backup;
56
57 static bool interrupted = FALSE;
58
59 static int
60 outc(int c)
61 {
62     if (interrupted) {
63         char tmp = c;
64         write(STDOUT_FILENO, &tmp, 1);
65     } else {
66         putc(c, stdout);
67     }
68     return 0;
69 }
70
71 static void
72 PutChar(int ch)
73 {
74     putchar(ch);
75     fflush(stdout);
76     napms(moveit ? 10 : 50);    /* not really termcap... */
77 }
78
79 static void
80 Backup(void)
81 {
82     tputs(backup != 0 ? backup : "\b", 1, outc);
83 }
84
85 static void
86 ShowCursor(int flag)
87 {
88     if (startC != 0 && finisC != 0) {
89         tputs(flag ? startC : finisC, 1, outc);
90     }
91 }
92
93 static void
94 StandOut(int flag)
95 {
96     if (startS != 0 && finisS != 0) {
97         tputs(flag ? startS : finisS, 1, outc);
98     }
99 }
100
101 static void
102 Underline(int flag)
103 {
104     if (startU != 0 && finisU != 0) {
105         tputs(flag ? startU : finisU, 1, outc);
106     }
107 }
108
109 static void
110 ShowSign(char *string)
111 {
112     char *base = string;
113     int ch, first, last;
114
115     if (moveit != 0) {
116         tputs(tgoto(moveit, 0, height - 1), 1, outc);
117         tputs(wipeit, 1, outc);
118     }
119
120     while (*string != 0) {
121         ch = *string;
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         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     exit(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     int j;
186
187     if (name == 0)
188         name = "dumb";
189     if (tgetent(buffer, name)) {
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         for (j = SIGHUP; j <= SIGTERM; j++)
220             if (signal(j, SIG_IGN) != SIG_IGN)
221                 signal(j, onsig);
222
223         while (*args) {
224             ShowSign(*args++);
225         }
226         ShowCursor(1);
227     }
228 }
229
230 int
231 main(int argc, char *argv[])
232 {
233     if (argc > 1) {
234         railroad(argv + 1);
235     } else {
236         static char *hello[] =
237         {"Hello World", 0};
238         railroad(hello);
239     }
240     return EXIT_SUCCESS;
241 }