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