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