]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/railroad.c
ncurses 5.3
[ncurses.git] / test / railroad.c
1 /****************************************************************************
2  * Copyright (c) 2000-2001,2002 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.10 2002/04/06 20:45:22 tom Exp $
33  *
34  * A simple demo of the termcap interface.
35  */
36 #include <test.priv.h>
37
38 #include <ctype.h>
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     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 world[] = "Hello World";
237         static char *hello[] =
238         {world, 0};
239         railroad(hello);
240     }
241     ExitProgram(EXIT_SUCCESS);
242 }