]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ditto.c
ncurses 5.6 - patch 20080607
[ncurses.git] / test / ditto.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2008 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 (1998-on)
31  *
32  * $Id: ditto.c,v 1.28 2008/06/07 22:35:58 tom Exp $
33  *
34  * The program illustrates how to set up multiple screens from a single
35  * program.
36  *
37  * If openpty() is supported, the command line parameters are titles for
38  * the windows showing each screen's data.
39  *
40  * If openpty() is not supported, you must invoke the program by specifying
41  * another terminal on the same machine by specifying its device, e.g.,
42  *      ditto /dev/ttyp1
43  */
44 #include <test.priv.h>
45 #include <sys/stat.h>
46 #include <errno.h>
47 #undef USE_PTHREADS
48
49 #ifdef USE_PTHREADS
50 #include <pthread.h>
51 #endif
52
53 #ifdef USE_XTERM_PTY
54 #include USE_OPENPTY_HEADER
55 #endif
56
57 #define MAX_FIFO 256
58
59 #define THIS_FIFO(n) ((n) % MAX_FIFO)
60 #define NEXT_FIFO(n) THIS_FIFO((n) + 1)
61
62 typedef struct {
63     unsigned long sequence;
64     int head;
65     int tail;
66     int data[MAX_FIFO];
67 } FIFO;
68
69 typedef struct {
70     unsigned long sequence;
71 } PEEK;
72
73 /*
74  * Data "owned" for a single screen.  Each screen is divided into windows that
75  * show the text read from each terminal.  Input from a given screen will also
76  * be read into one window per screen.
77  */
78 typedef struct {
79     FILE *input;
80     FILE *output;
81     SCREEN *screen;             /* this screen - curses internal data */
82     int which1;                 /* this screen's index in DITTO[] array */
83     int length;                 /* length of windows[] and peeks[] */
84     char **titles;              /* per-window titles */
85     WINDOW **windows;           /* display data from each screen */
86     PEEK *peeks;                /* indices for each screen's fifo */
87     FIFO fifo;                  /* fifo for this screen */
88 #ifdef USE_PTHREADS
89     pthread_t thread;
90 #endif
91 } DITTO;
92
93 /*
94  * Structure used to pass multiple parameters via the use_screen()
95  * single-parameter interface.
96  */
97 typedef struct {
98     int source;                 /* which screen did character come from */
99     int target;                 /* which screen is character going to */
100     DITTO *ditto;               /* data for all screens */
101 } DDATA;
102
103 static void
104 failed(const char *s)
105 {
106     perror(s);
107     ExitProgram(EXIT_FAILURE);
108 }
109
110 static void
111 usage(void)
112 {
113     fprintf(stderr, "usage: ditto [terminal1 ...]\n");
114     ExitProgram(EXIT_FAILURE);
115 }
116
117 /* Add to the head of the fifo, checking for overflow. */
118 static void
119 put_fifo(FIFO * fifo, int value)
120 {
121     int next = NEXT_FIFO(fifo->head);
122     if (next == fifo->tail)
123         fifo->tail = NEXT_FIFO(fifo->tail);
124     fifo->data[next] = value;
125     fifo->head = next;
126     fifo->sequence += 1;
127 }
128
129 /* Get data from the tail (oldest part) of the fifo, returning -1 if no data.
130  * Since each screen can peek into the fifo, we do not update the tail index,
131  * but modify the peek-index.
132  *
133  * FIXME - test/workaround for case where fifo gets more than a buffer
134  * ahead of peek.
135  */
136 static int
137 peek_fifo(FIFO * fifo, PEEK * peek)
138 {
139     int result = -1;
140     if (peek->sequence < fifo->sequence) {
141         peek->sequence += 1;
142         result = fifo->data[THIS_FIFO(peek->sequence)];
143     }
144     return result;
145 }
146
147 static FILE *
148 open_tty(char *path)
149 {
150     FILE *fp;
151 #ifdef USE_XTERM_PTY
152     int amaster;
153     int aslave;
154     char slave_name[1024];
155     char s_option[1024];
156     char *leaf;
157
158     if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0)
159         failed("openpty");
160     if ((leaf = strrchr(slave_name, '/')) == 0) {
161         errno = EISDIR;
162         failed(slave_name);
163     }
164     sprintf(s_option, "-S%s/%d", slave_name, aslave);
165     if (fork()) {
166         execlp("xterm", "xterm", s_option, "-title", path, (char *) 0);
167         _exit(0);
168     }
169     fp = fdopen(amaster, "r+");
170 #else
171     struct stat sb;
172
173     if (stat(path, &sb) < 0)
174         failed(path);
175     if ((sb.st_mode & S_IFMT) != S_IFCHR) {
176         errno = ENOTTY;
177         failed(path);
178     }
179     fp = fopen(path, "r+");
180     if (fp == 0)
181         failed(path);
182     printf("opened %s\n", path);
183 #endif
184     return fp;
185 }
186
187 static void
188 init_screen(SCREEN *sp GCC_UNUSED, void *arg)
189 {
190     DITTO *target = (DITTO *) arg;
191     int high, wide;
192     int k;
193
194     cbreak();
195     noecho();
196     scrollok(stdscr, TRUE);
197     box(stdscr, 0, 0);
198
199     target->windows = typeCalloc(WINDOW *, target->length);
200     target->peeks = typeCalloc(PEEK, target->length);
201
202     high = (LINES - 2) / target->length;
203     wide = (COLS - 2);
204     for (k = 0; k < target->length; ++k) {
205         WINDOW *outer = newwin(high, wide, 1 + (high * k), 1);
206         WINDOW *inner = derwin(outer, high - 2, wide - 2, 1, 1);
207
208         box(outer, 0, 0);
209         mvwaddstr(outer, 0, 2, target->titles[k]);
210         wnoutrefresh(outer);
211
212         scrollok(inner, TRUE);
213         keypad(inner, TRUE);
214 #ifndef USE_PTHREADS
215         nodelay(inner, TRUE);
216 #endif
217
218         target->windows[k] = inner;
219     }
220     doupdate();
221 }
222
223 static void
224 open_screen(DITTO * target, char **source, int length, int which1)
225 {
226     if (which1 != 0) {
227         target->input =
228             target->output = open_tty(source[which1]);
229     } else {
230         target->input = stdin;
231         target->output = stdout;
232     }
233
234     target->which1 = which1;
235     target->titles = source;
236     target->length = length;
237     target->screen = newterm((char *) 0,        /* assume $TERM is the same */
238                              target->output,
239                              target->input);
240
241     if (target->screen == 0)
242         failed("newterm");
243
244     (void) USING_SCREEN(target->screen, init_screen, target);
245 }
246
247 static int
248 close_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
249 {
250     (void) sp;
251     (void) arg;
252     return endwin();
253 }
254
255 /*
256  * Read data from the 'source' screen.
257  */
258 static int
259 read_screen(SCREEN *sp GCC_UNUSED, void *arg)
260 {
261     DDATA *data = (DDATA *) arg;
262     DITTO *ditto = &(data->ditto[data->source]);
263     WINDOW *win = ditto->windows[data->source];
264     int ch = wgetch(win);
265
266     if (ch > 0 && ch < 256)
267         put_fifo(&(ditto->fifo), ch);
268     else
269         ch = ERR;
270
271     return ch;
272 }
273
274 /*
275  * Write all of the data that's in fifos for the 'target' screen.
276  */
277 static int
278 write_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
279 {
280     DDATA *data = (DDATA *) arg;
281     DITTO *ditto = &(data->ditto[data->target]);
282     bool changed = FALSE;
283     int which;
284
285     for (which = 0; which < ditto->length; ++which) {
286         WINDOW *win = ditto->windows[which];
287         FIFO *fifo = &(data->ditto[which].fifo);
288         PEEK *peek = &(ditto->peeks[which]);
289         int ch;
290
291         while ((ch = peek_fifo(fifo, peek)) > 0) {
292             changed = TRUE;
293
294             waddch(win, ch);
295             wnoutrefresh(win);
296         }
297     }
298
299     if (changed)
300         doupdate();
301     return OK;
302 }
303
304 static void
305 show_ditto(DITTO * data, int count, DDATA * ddata)
306 {
307     int n;
308
309     for (n = 0; n < count; n++) {
310         ddata->target = n;
311         USING_SCREEN(data[n].screen, write_screen, (void *) ddata);
312     }
313 }
314
315 #ifdef USE_PTHREADS
316 static void *
317 handle_screen(void *arg)
318 {
319     DDATA ddata;
320     int ch;
321
322     ddata.ditto = (DITTO *) arg;
323     ddata.source = ddata.ditto->which1;
324     for (;;) {
325         ch = read_screen(ddata.ditto->screen, &ddata);
326         if (ch == CTRL('D'))
327             break;
328         show_ditto(ddata.ditto, ddata.ditto->length, &ddata);
329     }
330     return NULL;
331 }
332 #endif
333
334 int
335 main(int argc, char *argv[])
336 {
337     int j;
338     DITTO *data;
339 #ifndef USE_PTHREADS
340     int count;
341 #endif
342
343     if (argc <= 1)
344         usage();
345
346     if ((data = typeCalloc(DITTO, argc)) == 0)
347         failed("calloc data");
348
349     for (j = 0; j < argc; j++) {
350         open_screen(&data[j], argv, argc, j);
351     }
352
353 #ifdef USE_PTHREADS
354     /*
355      * For multi-threaded operation, set up a reader for each of the screens.
356      * That uses blocking I/O rather than polling for input, so no calls to
357      * napms() are needed.
358      */
359     for (j = 0; j < argc; j++) {
360         (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]);
361     }
362 #else
363     /*
364      * Loop, reading characters from any of the inputs and writing to all
365      * of the screens.
366      */
367     for (count = 0;; ++count) {
368         DDATA ddata;
369         int ch;
370         int which = (count % argc);
371
372         napms(20);
373
374         ddata.source = which;
375         ddata.ditto = data;
376
377         ch = USING_SCREEN(data[which].screen, read_screen, &ddata);
378         if (ch == CTRL('D')) {
379             break;
380         } else if (ch != ERR) {
381             show_ditto(data, argc, &ddata);
382         }
383     }
384 #endif
385
386     /*
387      * Cleanup and exit
388      */
389     for (j = argc - 1; j >= 0; j--) {
390         USING_SCREEN(data[j].screen, close_screen, 0);
391         fprintf(data[j].output, "**Closed\r\n");
392
393         /*
394          * Closing before a delscreen() helps ncurses determine that there
395          * is no valid output buffer, and can remove the setbuf() data.
396          */
397         fflush(data[j].output);
398         fclose(data[j].output);
399         delscreen(data[j].screen);
400     }
401     ExitProgram(EXIT_SUCCESS);
402 }