blob: 4d7c0700de5f1401fbe296e8eb3abfbc32841c40 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2022 Thomas E. Dickey *
3 * Copyright 2000-2013,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30/*
31 * Author: Thomas E. Dickey - 2000
32 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: railroad.c,v 1.26 2022/12/11 00:12:13 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
35 * A simple demo of the termcap interface.
36 */
37#define USE_TINFO
38#include <test.priv.h>
39
40#if HAVE_TGETENT
41
42static char *wipeit;
43static char *moveit;
44static int length;
45static int height;
46
47static char *finisC;
48static char *finisS;
49static char *finisU;
50
51static char *startC;
52static char *startS;
53static char *startU;
54
55static char *backup;
56
57static bool interrupted = FALSE;
58
59static
60TPUTS_PROTO(outc, c)
61{
62 int rc = OK;
63
64 if (interrupted) {
65 char tmp = (char) c;
66 if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
67 rc = ERR;
68 } else {
69 if (putc(c, stdout) == EOF)
70 rc = ERR;
71 }
72 TPUTS_RETURN(rc);
73}
74
75static void
76PutChar(int ch)
77{
78 putchar(ch);
79 fflush(stdout);
80 napms(moveit ? 10 : 50); /* not really termcap... */
81}
82
83static void
84Backup(void)
85{
86 tputs(backup != 0 ? backup : "\b", 1, outc);
87}
88
89static void
90MyShowCursor(int flag)
91{
92 if (startC != 0 && finisC != 0) {
93 tputs(flag ? startC : finisC, 1, outc);
94 }
95}
96
97static void
98StandOut(int flag)
99{
100 if (startS != 0 && finisS != 0) {
101 tputs(flag ? startS : finisS, 1, outc);
102 }
103}
104
105static void
106Underline(int flag)
107{
108 if (startU != 0 && finisU != 0) {
109 tputs(flag ? startU : finisU, 1, outc);
110 }
111}
112
113static void
114ShowSign(char *string)
115{
116 char *base = string;
micky3879b9f5e72025-07-08 18:04:53 -0400117 int first, last;
Steve Kondikae271bc2015-11-15 02:50:53 +0100118
119 if (moveit != 0) {
120 tputs(tgoto(moveit, 0, height - 1), 1, outc);
121 tputs(wipeit, 1, outc);
122 }
123
124 while (*string != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400125 int ch = *string;
Steve Kondikae271bc2015-11-15 02:50:53 +0100126 if (ch != ' ') {
127 if (moveit != 0) {
128 for (first = length - 2; first >= (string - base); first--) {
129 if (first < length - 1) {
130 tputs(tgoto(moveit, first + 1, height - 1), 1, outc);
131 PutChar(' ');
132 }
133 tputs(tgoto(moveit, first, height - 1), 1, outc);
134 PutChar(ch);
135 }
136 } else {
137 last = ch;
138 if (isalpha(ch)) {
139 first = isupper(ch) ? 'A' : 'a';
140 } else if (isdigit(ch)) {
141 first = '0';
142 } else {
143 first = ch;
144 }
145 if (first < last) {
146 Underline(1);
147 while (first < last) {
148 PutChar(first);
149 Backup();
150 first++;
151 }
152 Underline(0);
153 }
154 }
155 if (moveit != 0)
156 Backup();
157 }
158 StandOut(1);
159 PutChar(ch);
160 StandOut(0);
161 fflush(stdout);
162 string++;
163 }
164 if (moveit != 0)
165 tputs(wipeit, 1, outc);
166 putchar('\n');
167}
168
169static void
170cleanup(void)
171{
172 Underline(0);
173 StandOut(0);
174 MyShowCursor(1);
175}
176
177static void
178onsig(int n GCC_UNUSED)
179{
180 interrupted = TRUE;
181 cleanup();
182 ExitProgram(EXIT_FAILURE);
183}
184
185static void
186railroad(char **args)
187{
188 NCURSES_CONST char *name = getenv("TERM");
189 char buffer[1024];
190 char area[1024], *ap = area;
micky3879b9f5e72025-07-08 18:04:53 -0400191 int z;
Steve Kondikae271bc2015-11-15 02:50:53 +0100192
193 if (name == 0)
micky3879b9f5e72025-07-08 18:04:53 -0400194#ifdef EXP_WIN32_DRIVER
195 name = "ms-terminal";
196#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100197 name = "dumb";
micky3879b9f5e72025-07-08 18:04:53 -0400198#endif
199
200 InitAndCatch(z = tgetent(buffer, name), onsig);
201 if (z >= 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100202
203 wipeit = tgetstr("ce", &ap);
204 height = tgetnum("li");
205 length = tgetnum("co");
206 moveit = tgetstr("cm", &ap);
207
208 if (wipeit == 0
209 || moveit == 0
210 || height <= 0
211 || length <= 0) {
212 wipeit = 0;
213 moveit = 0;
214 height = 0;
215 length = 0;
216 }
217
218 startS = tgetstr("so", &ap);
219 finisS = tgetstr("se", &ap);
220
221 startU = tgetstr("us", &ap);
222 finisU = tgetstr("ue", &ap);
223
224 backup = tgetstr("le", &ap);
225
226 startC = tgetstr("ve", &ap);
227 finisC = tgetstr("vi", &ap);
228
229 MyShowCursor(0);
230
Steve Kondikae271bc2015-11-15 02:50:53 +0100231 while (*args) {
232 ShowSign(*args++);
233 }
234 MyShowCursor(1);
235 }
236}
237
micky3879b9f5e72025-07-08 18:04:53 -0400238static void
239usage(int ok)
240{
241 static const char *msg[] =
242 {
243 "Usage: railroad [options]"
244 ,""
245 ,USAGE_COMMON
246 };
247 size_t n;
248
249 for (n = 0; n < SIZEOF(msg); n++)
250 fprintf(stderr, "%s\n", msg[n]);
251
252 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
253}
254/* *INDENT-OFF* */
255VERSION_COMMON()
256/* *INDENT-ON* */
257
Steve Kondikae271bc2015-11-15 02:50:53 +0100258int
259main(int argc, char *argv[])
260{
micky3879b9f5e72025-07-08 18:04:53 -0400261 int ch;
262
263 while ((ch = getopt(argc, argv, OPTS_COMMON)) != -1) {
264 switch (ch) {
265 case OPTS_VERSION:
266 show_version(argv);
267 ExitProgram(EXIT_SUCCESS);
268 default:
269 usage(ch == OPTS_USAGE);
270 /* NOTREACHED */
271 }
272 }
273
274 if (optind < argc) {
275 railroad(argv + optind);
Steve Kondikae271bc2015-11-15 02:50:53 +0100276 } else {
277 static char world[] = "Hello World";
278 static char *hello[] =
279 {world, 0};
280 railroad(hello);
281 }
282 ExitProgram(EXIT_SUCCESS);
283}
284
285#else
286int
micky3879b9f5e72025-07-08 18:04:53 -0400287main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100288{
289 printf("This program requires termcap\n");
290 exit(EXIT_FAILURE);
291}
292#endif