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