1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 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.48 2018/05/20 19:19:10 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>
54 #include USE_OPENPTY_HEADER
59 #define THIS_FIFO(n) ((n) % MAX_FIFO)
60 #define NEXT_FIFO(n) THIS_FIFO((n) + 1)
63 unsigned long sequence;
70 unsigned long sequence;
74 * Data "owned" for a single screen. Each screen is divided into windows that
75 * show the text read from each terminal. Input from a given screen will also
76 * be read into one window per screen.
81 SCREEN *screen; /* this screen - curses internal data */
82 int which1; /* this screen's index in DITTO[] array */
83 int length; /* length of windows[] and peeks[] */
84 char **titles; /* per-window titles */
85 WINDOW **parents; /* display boxes around each screen's data */
86 WINDOW **windows; /* display data from each screen */
87 PEEK *peeks; /* indices for each screen's fifo */
88 FIFO fifo; /* fifo for this screen */
95 * Structure used to pass multiple parameters via the use_screen()
96 * single-parameter interface.
99 int source; /* which screen did character come from */
100 int target; /* which screen is character going to */
101 DITTO *ditto; /* data for all screens */
104 static void failed(const char *) GCC_NORETURN;
105 static void usage(void) GCC_NORETURN;
108 failed(const char *s)
111 ExitProgram(EXIT_FAILURE);
117 fprintf(stderr, "Usage: ditto [terminal1 ...]\n");
118 ExitProgram(EXIT_FAILURE);
121 /* Add to the head of the fifo, checking for overflow. */
123 put_fifo(FIFO * fifo, int value)
125 int next = NEXT_FIFO(fifo->head);
126 if (next == fifo->tail)
127 fifo->tail = NEXT_FIFO(fifo->tail);
128 fifo->data[next] = value;
133 /* Get data from the tail (oldest part) of the fifo, returning -1 if no data.
134 * Since each screen can peek into the fifo, we do not update the tail index,
135 * but modify the peek-index.
137 * FIXME - test/workaround for case where fifo gets more than a buffer
141 peek_fifo(FIFO * fifo, PEEK * peek)
144 if (peek->sequence < fifo->sequence) {
145 result = fifo->data[THIS_FIFO(peek->sequence)];
158 char slave_name[1024];
159 char s_option[sizeof(slave_name) + 80];
160 const char *xterm_prog = 0;
162 if ((xterm_prog = getenv("XTERM_PROG")) == 0)
163 xterm_prog = "xterm";
165 if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0
166 || strlen(slave_name) > sizeof(slave_name) - 1)
168 if (strrchr(slave_name, '/') == 0) {
172 _nc_SPRINTF(s_option, _nc_SLIMIT(sizeof(s_option))
173 "-S%s/%d", slave_name, aslave);
175 execlp(xterm_prog, xterm_prog, s_option, "-title", path, (char *) 0);
178 fp = fdopen(amaster, "r+");
184 if (stat(path, &sb) < 0)
186 if ((sb.st_mode & S_IFMT) != S_IFCHR) {
190 fp = fopen(path, "r+");
193 printf("opened %s\n", path);
202 SCREEN *sp GCC_UNUSED,
206 DITTO *target = (DITTO *) arg;
212 scrollok(stdscr, TRUE);
215 target->parents = typeCalloc(WINDOW *, (size_t) target->length);
216 target->windows = typeCalloc(WINDOW *, (size_t) target->length);
217 target->peeks = typeCalloc(PEEK, (size_t) target->length);
219 high = (LINES - 2) / target->length;
221 for (k = 0; k < target->length; ++k) {
222 WINDOW *outer = newwin(high, wide, 1 + (high * k), 1);
223 WINDOW *inner = derwin(outer, high - 2, wide - 2, 1, 1);
226 MvWAddStr(outer, 0, 2, target->titles[k]);
229 scrollok(inner, TRUE);
232 nodelay(inner, TRUE);
235 target->parents[k] = outer;
236 target->windows[k] = inner;
243 open_screen(DITTO * target, char **source, int length, int which1)
247 target->output = open_tty(source[which1]);
249 target->input = stdin;
250 target->output = stdout;
253 target->which1 = which1;
254 target->titles = source;
255 target->length = length;
256 target->fifo.head = -1;
257 target->screen = newterm((char *) 0, /* assume $TERM is the same */
261 if (target->screen == 0)
264 (void) USING_SCREEN(target->screen, init_screen, target);
270 SCREEN *sp GCC_UNUSED,
272 void *arg GCC_UNUSED)
282 * Read data from the 'source' screen.
287 SCREEN *sp GCC_UNUSED,
291 DDATA *data = (DDATA *) arg;
292 DITTO *ditto = &(data->ditto[data->source]);
293 WINDOW *win = ditto->windows[data->source];
294 int ch = wgetch(win);
296 if (ch > 0 && ch < 256)
297 put_fifo(&(ditto->fifo), ch);
305 * Write all of the data that's in fifos for the 'target' screen.
310 SCREEN *sp GCC_UNUSED,
312 void *arg GCC_UNUSED)
314 DDATA *data = (DDATA *) arg;
315 DITTO *ditto = &(data->ditto[data->target]);
316 bool changed = FALSE;
319 for (which = 0; which < ditto->length; ++which) {
320 WINDOW *win = ditto->windows[which];
321 FIFO *fifo = &(data->ditto[which].fifo);
322 PEEK *peek = &(ditto->peeks[which]);
325 while ((ch = peek_fifo(fifo, peek)) > 0) {
328 waddch(win, (chtype) ch);
339 show_ditto(DITTO * data, int count, DDATA * ddata)
344 for (n = 0; n < count; n++) {
346 USING_SCREEN(data[n].screen, write_screen, (void *) ddata);
352 handle_screen(void *arg)
357 memset(&ddata, 0, sizeof(ddata));
358 ddata.ditto = (DITTO *) arg;
359 ddata.source = ddata.ditto->which1;
360 ddata.ditto -= ddata.source; /* -> base of array */
363 ch = read_screen(ddata.ditto->screen, &ddata);
364 if (ch == CTRL('D')) {
365 int later = (ddata.source ? ddata.source : -1);
368 for (j = ddata.ditto->length - 1; j > 0; --j) {
370 pthread_cancel(ddata.ditto[j].thread);
374 pthread_cancel(ddata.ditto[later].thread);
378 show_ditto(ddata.ditto, ddata.ditto->length, &ddata);
385 main(int argc, char *argv[])
396 if ((data = typeCalloc(DITTO, (size_t) argc)) == 0)
397 failed("calloc data");
401 for (j = 0; j < argc; j++) {
402 open_screen(&data[j], argv, argc, j);
407 * For multi-threaded operation, set up a reader for each of the screens.
408 * That uses blocking I/O rather than polling for input, so no calls to
409 * napms() are needed.
411 for (j = 0; j < argc; j++) {
412 (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]);
414 pthread_join(data[1].thread, NULL);
417 * Loop, reading characters from any of the inputs and writing to all
420 for (count = 0;; ++count) {
423 int which = (count % argc);
427 ddata.source = which;
430 ch = USING_SCREEN(data[which].screen, read_screen, &ddata);
431 if (ch == CTRL('D')) {
433 } else if (ch != ERR) {
434 show_ditto(data, argc, &ddata);
442 for (j = argc - 1; j >= 0; j--) {
443 USING_SCREEN(data[j].screen, close_screen, 0);
444 fprintf(data[j].output, "**Closed\r\n");
447 * Closing before a delscreen() helps ncurses determine that there
448 * is no valid output buffer, and can remove the setbuf() data.
450 fflush(data[j].output);
451 fclose(data[j].output);
452 delscreen(data[j].screen);
454 ExitProgram(EXIT_SUCCESS);
460 printf("This program requires the curses delscreen function\n");
461 ExitProgram(EXIT_FAILURE);