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