]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ditto.c
ncurses 5.6 - patch 20080705
[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.30 2008/06/14 23:00:26 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         peek->sequence += 1;
141         result = fifo->data[THIS_FIFO(peek->sequence)];
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[1024];
155     char *leaf;
156
157     if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0)
158         failed("openpty");
159     if ((leaf = strrchr(slave_name, '/')) == 0) {
160         errno = EISDIR;
161         failed(slave_name);
162     }
163     sprintf(s_option, "-S%s/%d", slave_name, aslave);
164     if (fork()) {
165         execlp("xterm", "xterm", s_option, "-title", path, (char *) 0);
166         _exit(0);
167     }
168     fp = fdopen(amaster, "r+");
169 #else
170     struct stat sb;
171
172     if (stat(path, &sb) < 0)
173         failed(path);
174     if ((sb.st_mode & S_IFMT) != S_IFCHR) {
175         errno = ENOTTY;
176         failed(path);
177     }
178     fp = fopen(path, "r+");
179     if (fp == 0)
180         failed(path);
181     printf("opened %s\n", path);
182 #endif
183     return fp;
184 }
185
186 static void
187 init_screen(SCREEN *sp GCC_UNUSED, void *arg)
188 {
189     DITTO *target = (DITTO *) arg;
190     int high, wide;
191     int k;
192
193     cbreak();
194     noecho();
195     scrollok(stdscr, TRUE);
196     box(stdscr, 0, 0);
197
198     target->windows = typeCalloc(WINDOW *, target->length);
199     target->peeks = typeCalloc(PEEK, target->length);
200
201     high = (LINES - 2) / target->length;
202     wide = (COLS - 2);
203     for (k = 0; k < target->length; ++k) {
204         WINDOW *outer = newwin(high, wide, 1 + (high * k), 1);
205         WINDOW *inner = derwin(outer, high - 2, wide - 2, 1, 1);
206
207         box(outer, 0, 0);
208         mvwaddstr(outer, 0, 2, target->titles[k]);
209         wnoutrefresh(outer);
210
211         scrollok(inner, TRUE);
212         keypad(inner, TRUE);
213 #ifndef USE_PTHREADS
214         nodelay(inner, TRUE);
215 #endif
216
217         target->windows[k] = inner;
218     }
219     doupdate();
220 }
221
222 static void
223 open_screen(DITTO * target, char **source, int length, int which1)
224 {
225     if (which1 != 0) {
226         target->input =
227             target->output = open_tty(source[which1]);
228     } else {
229         target->input = stdin;
230         target->output = stdout;
231     }
232
233     target->which1 = which1;
234     target->titles = source;
235     target->length = length;
236     target->screen = newterm((char *) 0,        /* assume $TERM is the same */
237                              target->output,
238                              target->input);
239
240     if (target->screen == 0)
241         failed("newterm");
242
243     (void) USING_SCREEN(target->screen, init_screen, target);
244 }
245
246 static int
247 close_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
248 {
249     (void) sp;
250     (void) arg;
251     return endwin();
252 }
253
254 /*
255  * Read data from the 'source' screen.
256  */
257 static int
258 read_screen(SCREEN *sp GCC_UNUSED, void *arg)
259 {
260     DDATA *data = (DDATA *) arg;
261     DITTO *ditto = &(data->ditto[data->source]);
262     WINDOW *win = ditto->windows[data->source];
263     int ch = wgetch(win);
264
265     if (ch > 0 && ch < 256)
266         put_fifo(&(ditto->fifo), ch);
267     else
268         ch = ERR;
269
270     return ch;
271 }
272
273 /*
274  * Write all of the data that's in fifos for the 'target' screen.
275  */
276 static int
277 write_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
278 {
279     DDATA *data = (DDATA *) arg;
280     DITTO *ditto = &(data->ditto[data->target]);
281     bool changed = FALSE;
282     int which;
283
284     for (which = 0; which < ditto->length; ++which) {
285         WINDOW *win = ditto->windows[which];
286         FIFO *fifo = &(data->ditto[which].fifo);
287         PEEK *peek = &(ditto->peeks[which]);
288         int ch;
289
290         while ((ch = peek_fifo(fifo, peek)) > 0) {
291             changed = TRUE;
292
293             waddch(win, ch);
294             wnoutrefresh(win);
295         }
296     }
297
298     if (changed)
299         doupdate();
300     return OK;
301 }
302
303 static void
304 show_ditto(DITTO * data, int count, DDATA * ddata)
305 {
306     int n;
307
308     for (n = 0; n < count; n++) {
309         ddata->target = n;
310         USING_SCREEN(data[n].screen, write_screen, (void *) ddata);
311     }
312 }
313
314 #ifdef USE_PTHREADS
315 static void *
316 handle_screen(void *arg)
317 {
318     DDATA ddata;
319     int ch;
320
321     memset(&ddata, 0, sizeof(ddata));
322     ddata.ditto = (DITTO *) arg;
323     ddata.source = ddata.ditto->which1;
324     ddata.ditto -= ddata.source;        /* -> base of array */
325
326     for (;;) {
327         ch = read_screen(ddata.ditto->screen, &ddata);
328         if (ch == CTRL('D')) {
329             int later = (ddata.source ? ddata.source : -1);
330             int j;
331
332             for (j = ddata.ditto->length - 1; j > 0; --j) {
333                 if (j != later) {
334                     pthread_cancel(ddata.ditto[j].thread);
335                 }
336             }
337             if (later > 0) {
338                 pthread_cancel(ddata.ditto[later].thread);
339             }
340             break;
341         }
342         show_ditto(ddata.ditto, ddata.ditto->length, &ddata);
343     }
344     return NULL;
345 }
346 #endif
347
348 int
349 main(int argc, char *argv[])
350 {
351     int j;
352     DITTO *data;
353 #ifndef USE_PTHREADS
354     int count;
355 #endif
356
357     if (argc <= 1)
358         usage();
359
360     if ((data = typeCalloc(DITTO, argc)) == 0)
361         failed("calloc data");
362
363     for (j = 0; j < argc; j++) {
364         open_screen(&data[j], argv, argc, j);
365     }
366
367 #ifdef USE_PTHREADS
368     /*
369      * For multi-threaded operation, set up a reader for each of the screens.
370      * That uses blocking I/O rather than polling for input, so no calls to
371      * napms() are needed.
372      */
373     for (j = 0; j < argc; j++) {
374         (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]);
375     }
376     pthread_join(data[1].thread, NULL);
377 #else
378     /*
379      * Loop, reading characters from any of the inputs and writing to all
380      * of the screens.
381      */
382     for (count = 0;; ++count) {
383         DDATA ddata;
384         int ch;
385         int which = (count % argc);
386
387         napms(20);
388
389         ddata.source = which;
390         ddata.ditto = data;
391
392         ch = USING_SCREEN(data[which].screen, read_screen, &ddata);
393         if (ch == CTRL('D')) {
394             break;
395         } else if (ch != ERR) {
396             show_ditto(data, argc, &ddata);
397         }
398     }
399 #endif
400
401     /*
402      * Cleanup and exit
403      */
404     for (j = argc - 1; j >= 0; j--) {
405         USING_SCREEN(data[j].screen, close_screen, 0);
406         fprintf(data[j].output, "**Closed\r\n");
407
408         /*
409          * Closing before a delscreen() helps ncurses determine that there
410          * is no valid output buffer, and can remove the setbuf() data.
411          */
412         fflush(data[j].output);
413         fclose(data[j].output);
414         delscreen(data[j].screen);
415     }
416     ExitProgram(EXIT_SUCCESS);
417 }