1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey (1998-on)
32 * $Id: ditto.c,v 1.34 2009/07/17 00:07:25 tom Exp $
34 * The program illustrates how to set up multiple screens from a single
37 * If openpty() is supported, the command line parameters are titles for
38 * the windows showing each screen's data.
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.,
44 #include <test.priv.h>
53 #include USE_OPENPTY_HEADER
58 #define THIS_FIFO(n) ((n) % MAX_FIFO)
59 #define NEXT_FIFO(n) THIS_FIFO((n) + 1)
62 unsigned long sequence;
69 unsigned long sequence;
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.
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 */
93 * Structure used to pass multiple parameters via the use_screen()
94 * single-parameter interface.
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 */
103 failed(const char *s)
106 ExitProgram(EXIT_FAILURE);
112 fprintf(stderr, "usage: ditto [terminal1 ...]\n");
113 ExitProgram(EXIT_FAILURE);
116 /* Add to the head of the fifo, checking for overflow. */
118 put_fifo(FIFO * fifo, int value)
120 int next = NEXT_FIFO(fifo->head);
121 if (next == fifo->tail)
122 fifo->tail = NEXT_FIFO(fifo->tail);
123 fifo->data[next] = value;
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.
132 * FIXME - test/workaround for case where fifo gets more than a buffer
136 peek_fifo(FIFO * fifo, PEEK * peek)
139 if (peek->sequence < fifo->sequence) {
140 result = fifo->data[THIS_FIFO(peek->sequence)];
153 char slave_name[1024];
154 char s_option[sizeof(slave_name) + 80];
157 if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0
158 || strlen(slave_name) > sizeof(slave_name) - 1)
160 if ((leaf = strrchr(slave_name, '/')) == 0) {
164 sprintf(s_option, "-S%s/%d", slave_name, aslave);
166 execlp("xterm", "xterm", s_option, "-title", path, (char *) 0);
169 fp = fdopen(amaster, "r+");
175 if (stat(path, &sb) < 0)
177 if ((sb.st_mode & S_IFMT) != S_IFCHR) {
181 fp = fopen(path, "r+");
184 printf("opened %s\n", path);
193 SCREEN *sp GCC_UNUSED,
197 DITTO *target = (DITTO *) arg;
203 scrollok(stdscr, TRUE);
206 target->windows = typeCalloc(WINDOW *, (size_t) target->length);
207 target->peeks = typeCalloc(PEEK, (size_t) target->length);
209 high = (LINES - 2) / target->length;
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);
216 mvwaddstr(outer, 0, 2, target->titles[k]);
219 scrollok(inner, TRUE);
222 nodelay(inner, TRUE);
225 target->windows[k] = inner;
231 open_screen(DITTO * target, char **source, int length, int which1)
235 target->output = open_tty(source[which1]);
237 target->input = stdin;
238 target->output = stdout;
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 */
249 if (target->screen == 0)
252 (void) USING_SCREEN(target->screen, init_screen, target);
258 SCREEN *sp GCC_UNUSED,
260 void *arg GCC_UNUSED)
270 * Read data from the 'source' screen.
275 SCREEN *sp GCC_UNUSED,
279 DDATA *data = (DDATA *) arg;
280 DITTO *ditto = &(data->ditto[data->source]);
281 WINDOW *win = ditto->windows[data->source];
282 int ch = wgetch(win);
284 if (ch > 0 && ch < 256)
285 put_fifo(&(ditto->fifo), ch);
293 * Write all of the data that's in fifos for the 'target' screen.
298 SCREEN *sp GCC_UNUSED,
300 void *arg GCC_UNUSED)
302 DDATA *data = (DDATA *) arg;
303 DITTO *ditto = &(data->ditto[data->target]);
304 bool changed = FALSE;
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]);
313 while ((ch = peek_fifo(fifo, peek)) > 0) {
316 waddch(win, (chtype) ch);
327 show_ditto(DITTO * data, int count, DDATA * ddata)
331 for (n = 0; n < count; n++) {
333 USING_SCREEN(data[n].screen, write_screen, (void *) ddata);
339 handle_screen(void *arg)
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 */
350 ch = read_screen(ddata.ditto->screen, &ddata);
351 if (ch == CTRL('D')) {
352 int later = (ddata.source ? ddata.source : -1);
355 for (j = ddata.ditto->length - 1; j > 0; --j) {
357 pthread_cancel(ddata.ditto[j].thread);
361 pthread_cancel(ddata.ditto[later].thread);
365 show_ditto(ddata.ditto, ddata.ditto->length, &ddata);
372 main(int argc, char *argv[])
383 if ((data = typeCalloc(DITTO, (size_t) argc)) == 0)
384 failed("calloc data");
386 for (j = 0; j < argc; j++) {
387 open_screen(&data[j], argv, argc, j);
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.
396 for (j = 0; j < argc; j++) {
397 (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]);
399 pthread_join(data[1].thread, NULL);
402 * Loop, reading characters from any of the inputs and writing to all
405 for (count = 0;; ++count) {
408 int which = (count % argc);
412 ddata.source = which;
415 ch = USING_SCREEN(data[which].screen, read_screen, &ddata);
416 if (ch == CTRL('D')) {
418 } else if (ch != ERR) {
419 show_ditto(data, argc, &ddata);
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");
432 * Closing before a delscreen() helps ncurses determine that there
433 * is no valid output buffer, and can remove the setbuf() data.
435 fflush(data[j].output);
436 fclose(data[j].output);
437 delscreen(data[j].screen);
439 ExitProgram(EXIT_SUCCESS);