]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ditto.c
ncurses 5.7 - patch 20090718
[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.34 2009/07/17 00:07:25 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
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 #if HAVE_USE_WINDOW
263     (void) sp;
264 #endif
265     (void) arg;
266     return endwin();
267 }
268
269 /*
270  * Read data from the 'source' screen.
271  */
272 static int
273 read_screen(
274 #if HAVE_USE_WINDOW
275                SCREEN *sp GCC_UNUSED,
276 #endif
277                void *arg)
278 {
279     DDATA *data = (DDATA *) arg;
280     DITTO *ditto = &(data->ditto[data->source]);
281     WINDOW *win = ditto->windows[data->source];
282     int ch = wgetch(win);
283
284     if (ch > 0 && ch < 256)
285         put_fifo(&(ditto->fifo), ch);
286     else
287         ch = ERR;
288
289     return ch;
290 }
291
292 /*
293  * Write all of the data that's in fifos for the 'target' screen.
294  */
295 static int
296 write_screen(
297 #if HAVE_USE_WINDOW
298                 SCREEN *sp GCC_UNUSED,
299 #endif
300                 void *arg GCC_UNUSED)
301 {
302     DDATA *data = (DDATA *) arg;
303     DITTO *ditto = &(data->ditto[data->target]);
304     bool changed = FALSE;
305     int which;
306
307     for (which = 0; which < ditto->length; ++which) {
308         WINDOW *win = ditto->windows[which];
309         FIFO *fifo = &(data->ditto[which].fifo);
310         PEEK *peek = &(ditto->peeks[which]);
311         int ch;
312
313         while ((ch = peek_fifo(fifo, peek)) > 0) {
314             changed = TRUE;
315
316             waddch(win, (chtype) ch);
317             wnoutrefresh(win);
318         }
319     }
320
321     if (changed)
322         doupdate();
323     return OK;
324 }
325
326 static void
327 show_ditto(DITTO * data, int count, DDATA * ddata)
328 {
329     int n;
330
331     for (n = 0; n < count; n++) {
332         ddata->target = n;
333         USING_SCREEN(data[n].screen, write_screen, (void *) ddata);
334     }
335 }
336
337 #ifdef USE_PTHREADS
338 static void *
339 handle_screen(void *arg)
340 {
341     DDATA ddata;
342     int ch;
343
344     memset(&ddata, 0, sizeof(ddata));
345     ddata.ditto = (DITTO *) arg;
346     ddata.source = ddata.ditto->which1;
347     ddata.ditto -= ddata.source;        /* -> base of array */
348
349     for (;;) {
350         ch = read_screen(ddata.ditto->screen, &ddata);
351         if (ch == CTRL('D')) {
352             int later = (ddata.source ? ddata.source : -1);
353             int j;
354
355             for (j = ddata.ditto->length - 1; j > 0; --j) {
356                 if (j != later) {
357                     pthread_cancel(ddata.ditto[j].thread);
358                 }
359             }
360             if (later > 0) {
361                 pthread_cancel(ddata.ditto[later].thread);
362             }
363             break;
364         }
365         show_ditto(ddata.ditto, ddata.ditto->length, &ddata);
366     }
367     return NULL;
368 }
369 #endif
370
371 int
372 main(int argc, char *argv[])
373 {
374     int j;
375     DITTO *data;
376 #ifndef USE_PTHREADS
377     int count;
378 #endif
379
380     if (argc <= 1)
381         usage();
382     
383     if ((data = typeCalloc(DITTO, (size_t) argc)) == 0)
384         failed("calloc data");
385
386     for (j = 0; j < argc; j++) {
387         open_screen(&data[j], argv, argc, j);
388     }
389
390 #ifdef USE_PTHREADS
391     /*
392      * For multi-threaded operation, set up a reader for each of the screens.
393      * That uses blocking I/O rather than polling for input, so no calls to
394      * napms() are needed.
395      */
396     for (j = 0; j < argc; j++) {
397         (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]);
398     }
399     pthread_join(data[1].thread, NULL);
400 #else
401     /*
402      * Loop, reading characters from any of the inputs and writing to all
403      * of the screens.
404      */
405     for (count = 0;; ++count) {
406         DDATA ddata;
407         int ch;
408         int which = (count % argc);
409
410         napms(20);
411
412         ddata.source = which;
413         ddata.ditto = data;
414
415         ch = USING_SCREEN(data[which].screen, read_screen, &ddata);
416         if (ch == CTRL('D')) {
417             break;
418         } else if (ch != ERR) {
419             show_ditto(data, argc, &ddata);
420         }
421     }
422 #endif
423
424     /*
425      * Cleanup and exit
426      */
427     for (j = argc - 1; j >= 0; j--) {
428         USING_SCREEN(data[j].screen, close_screen, 0);
429         fprintf(data[j].output, "**Closed\r\n");
430
431         /*
432          * Closing before a delscreen() helps ncurses determine that there
433          * is no valid output buffer, and can remove the setbuf() data.
434          */
435         fflush(data[j].output);
436         fclose(data[j].output);
437         delscreen(data[j].screen);
438     }
439     ExitProgram(EXIT_SUCCESS);
440 }