]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/railroad.c
ncurses 5.1
[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.1 2000/01/15 02:41:27 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 *finisC;
43 static char *finisS;
44 static char *finisU;
45
46 static char *startC;
47 static char *startS;
48 static char *startU;
49
50 static char *backup;
51
52 static bool interrupted = FALSE;
53
54 static int
55 outc(int c)
56 {
57     if (interrupted) {
58         char tmp = c;
59         write(STDOUT_FILENO, &tmp, 1);
60     } else {
61         putc(c, stdout);
62     }
63     return 0;
64 }
65
66 static void
67 PutChar(int ch)
68 {
69     putchar(ch);
70     fflush(stdout);
71     napms(50);                  /* not really termcap... */
72 }
73
74 static void
75 Backup(void)
76 {
77     tputs(backup != 0 ? backup : "\b", 1, outc);
78 }
79
80 static void
81 ShowCursor(int flag)
82 {
83     if (startC != 0 && finisC != 0) {
84         tputs(flag ? startC : finisC, 1, outc);
85     }
86 }
87
88 static void
89 StandOut(int flag)
90 {
91     if (startS != 0 && finisS != 0) {
92         tputs(flag ? startS : finisS, 1, outc);
93     }
94 }
95
96 static void
97 Underline(int flag)
98 {
99     if (startU != 0 && finisU != 0) {
100         tputs(flag ? startU : finisU, 1, outc);
101     }
102 }
103
104 static void
105 ShowSign(char *string)
106 {
107     int ch, first, last;
108
109     while (*string != 0) {
110         ch = *string;
111         last = ch;
112         if (isalpha(ch)) {
113             first = isupper(ch) ? 'A' : 'a';
114         } else if (isdigit(ch)) {
115             first = '0';
116         } else {
117             first = ch;
118         }
119         if (first < last) {
120             Underline(1);
121             while (first < last) {
122                 PutChar(first);
123                 Backup();
124                 first++;
125             }
126             Underline(0);
127         }
128         StandOut(1);
129         PutChar(ch);
130         StandOut(0);
131         fflush(stdout);
132         string++;
133     }
134     putchar('\n');
135 }
136
137 static void
138 cleanup(void)
139 {
140     Underline(0);
141     StandOut(0);
142     ShowCursor(1);
143 }
144
145 static void
146 onsig(int n GCC_UNUSED)
147 {
148     interrupted = TRUE;
149     cleanup();
150     exit(EXIT_FAILURE);
151 }
152
153 static void
154 railroad(char **args)
155 {
156     char *name = getenv("TERM");
157     char buffer[1024];
158     char area[1024], *ap = area;
159     int j;
160
161     if (name == 0)
162         name = "dumb";
163     if (tgetent(buffer, name)) {
164         startS = tgetstr("so", &ap);
165         finisS = tgetstr("se", &ap);
166
167         startU = tgetstr("us", &ap);
168         finisU = tgetstr("ue", &ap);
169
170         backup = tgetstr("le", &ap);
171
172         startC = tgetstr("ve", &ap);
173         finisC = tgetstr("vi", &ap);
174
175         ShowCursor(0);
176
177         for (j = SIGHUP; j <= SIGTERM; j++)
178             if (signal(j, SIG_IGN) != SIG_IGN)
179                 signal(j, onsig);
180
181         while (*args) {
182             ShowSign(*args++);
183         }
184         ShowCursor(1);
185     }
186 }
187
188 int
189 main(
190     int argc,
191     char *argv[])
192 {
193     if (argc > 1) {
194         railroad(argv + 1);
195     } else {
196         static char *hello[] =
197         {"Hello World", 0};
198         railroad(hello);
199     }
200     return EXIT_SUCCESS;
201 }