]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_termcap.c
ncurses 5.9 - patch 20130608
[ncurses.git] / test / dots_termcap.c
1 /****************************************************************************
2  * Copyright (c) 2013 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
31  *
32  * $Id: dots_termcap.c,v 1.5 2013/06/09 00:09:46 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 #include <time.h>
42
43 #define valid(s) ((s != 0) && s != (char *)-1)
44
45 static bool interrupted = FALSE;
46 static long total_chars = 0;
47 static time_t started;
48
49 static char *t_AB;
50 static char *t_AF;
51 static char *t_cl;
52 static char *t_cm;
53 static char *t_me;
54 static char *t_mr;
55 static char *t_oc;
56 static char *t_op;
57 static char *t_ve;
58 static char *t_vi;
59
60 static struct {
61     const char *name;
62     char **value;
63 } my_caps[] = {
64
65     {
66         "AB", &t_AB
67     },
68     {
69         "AF", &t_AF
70     },
71     {
72         "cl", &t_cl
73     },
74     {
75         "cm", &t_cm
76     },
77     {
78         "me", &t_me
79     },
80     {
81         "mr", &t_mr
82     },
83     {
84         "oc", &t_oc
85     },
86     {
87         "op", &t_op
88     },
89     {
90         "ve", &t_ve
91     },
92     {
93         "vi", &t_vi
94     },
95 };
96
97 static
98 TPUTS_PROTO(outc, c)
99 {
100     int rc = c;
101
102     if (interrupted) {
103         char tmp = (char) c;
104         if (write(STDOUT_FILENO, &tmp, 1) == -1)
105             rc = EOF;
106     } else {
107         rc = putc(c, stdout);
108     }
109     TPUTS_RETURN(rc);
110 }
111
112 static bool
113 outs(char *s)
114 {
115     if (valid(s)) {
116         tputs(s, 1, outc);
117         return TRUE;
118     }
119     return FALSE;
120 }
121
122 static void
123 cleanup(void)
124 {
125     outs(t_me);
126     if (!outs(t_oc))
127         outs(t_op);
128     outs(t_cl);
129     outs(t_ve);
130
131     printf("\n\n%ld total chars, rate %.2f/sec\n",
132            total_chars,
133            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
134 }
135
136 static void
137 onsig(int n GCC_UNUSED)
138 {
139     interrupted = TRUE;
140 }
141
142 static double
143 ranf(void)
144 {
145     long r = (rand() & 077777);
146     return ((double) r / 32768.);
147 }
148
149 static void
150 my_napms(int ms)
151 {
152 #if defined(__MINGW32__)
153     Sleep(ms);
154 #else
155     struct timeval data;
156     data.tv_sec = 0;
157     data.tv_usec = ms * 1000;
158     select(0, NULL, NULL, NULL, &data);
159 #endif
160 }
161
162 int
163 main(int argc GCC_UNUSED,
164      char *argv[]GCC_UNUSED)
165 {
166     int x, y, z, p;
167     int num_colors;
168     int num_lines;
169     int num_columns;
170     double r;
171     double c;
172     char buffer[1024];
173     char area[1024];
174     char *name;
175
176     CATCHALL(onsig);
177
178     srand((unsigned) time(0));
179
180     if ((name = getenv("TERM")) == 0) {
181         fprintf(stderr, "TERM is not set\n");
182         ExitProgram(EXIT_FAILURE);
183     } else if (tgetent(buffer, name) < 0) {
184         fprintf(stderr, "terminal description not found\n");
185         ExitProgram(EXIT_FAILURE);
186     } else {
187         size_t t;
188         char *ap = area;
189         for (t = 0; t < SIZEOF(my_caps); ++t) {
190             *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
191                                           my_caps[t].name, &ap);
192         }
193     }
194
195     num_colors = tgetnum("Co");
196     num_lines = tgetnum("li");
197     num_columns = tgetnum("co");
198
199     outs(t_cl);
200     outs(t_vi);
201     if (num_colors > 1) {
202         if (!valid(t_AF)
203             || !valid(t_AB)
204             || (!valid(t_oc) && !valid(t_op)))
205             num_colors = -1;
206     }
207
208     r = (double) (num_lines - 4);
209     c = (double) (num_columns - 4);
210     started = time((time_t *) 0);
211
212     while (!interrupted) {
213         x = (int) (c * ranf()) + 2;
214         y = (int) (r * ranf()) + 2;
215         p = (ranf() > 0.9) ? '*' : ' ';
216
217         tputs(tgoto(t_cm, x, y), 1, outc);
218         if (num_colors > 0) {
219             z = (int) (ranf() * num_colors);
220             if (ranf() > 0.01) {
221                 tputs(tgoto(t_AF, 0, z), 1, outc);
222             } else {
223                 tputs(tgoto(t_AB, 0, z), 1, outc);
224                 my_napms(1);
225             }
226         } else if (valid(t_me)
227                    && valid(t_mr)) {
228             if (ranf() <= 0.01) {
229                 outs((ranf() > 0.6)
230                      ? t_mr
231                      : t_me);
232                 my_napms(1);
233             }
234         }
235         outc(p);
236         fflush(stdout);
237         ++total_chars;
238     }
239     cleanup();
240     ExitProgram(EXIT_SUCCESS);
241 }
242 #else
243 int
244 main(int argc GCC_UNUSED,
245      char *argv[]GCC_UNUSED)
246 {
247     fprintf(stderr, "This program requires termcap\n");
248     exit(EXIT_FAILURE);
249 }
250 #endif