]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/ditto.c
ncurses 5.6 - patch 20080412
[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 <dickey@clark.net> 1998
31  *
32  * $Id: ditto.c,v 1.19 2008/04/12 23:37:32 tom Exp $
33  *
34  * The program illustrates how to set up multiple screens from a single
35  * program.  Invoke the program by specifying another terminal on the same
36  * machine by specifying its device, e.g.,
37  *      ditto /dev/ttyp1
38  */
39 #include <test.priv.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42
43 #ifdef USE_XTERM_PTY
44 #include USE_OPENPTY_HEADER
45 #endif
46
47 typedef struct {
48     FILE *input;
49     FILE *output;
50     SCREEN *screen;
51 } DITTO;
52
53 static void
54 failed(const char *s)
55 {
56     perror(s);
57     ExitProgram(EXIT_FAILURE);
58 }
59
60 static void
61 usage(void)
62 {
63     fprintf(stderr, "usage: ditto [terminal1 ...]\n");
64     ExitProgram(EXIT_FAILURE);
65 }
66
67 static FILE *
68 open_tty(char *path)
69 {
70     FILE *fp;
71 #ifdef USE_XTERM_PTY
72     int amaster;
73     int aslave;
74     char slave_name[1024];
75     char s_option[1024];
76     char *leaf;
77
78     if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0)
79         failed("openpty");
80     if ((leaf = strrchr(slave_name, '/')) == 0) {
81         errno = EISDIR;
82         failed(slave_name);
83     }
84     sprintf(s_option, "-S%s/%d", slave_name, aslave);
85     if (fork()) {
86         execlp("xterm", "xterm", s_option, "-title", path, (char *) 0);
87         _exit(0);
88     }
89     fp = fdopen(amaster, "r+");
90 #else
91     struct stat sb;
92
93     if (stat(path, &sb) < 0)
94         failed(path);
95     if ((sb.st_mode & S_IFMT) != S_IFCHR) {
96         errno = ENOTTY;
97         failed(path);
98     }
99     fp = fopen(path, "r+");
100     if (fp == 0)
101         failed(path);
102     printf("opened %s\n", path);
103 #endif
104     return fp;
105 }
106
107 static int
108 close_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
109 {
110     (void) sp;
111     (void) arg;
112     return endwin();
113 }
114
115 static int
116 read_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
117 {
118     return getch();
119 }
120
121 static int
122 write_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED)
123 {
124     addstr((char *) arg);
125     refresh();
126     return OK;
127 }
128
129 static void
130 show_ditto(DITTO * data, int count, char *msg)
131 {
132     int n;
133
134     for (n = 0; n < count; n++) {
135         USING_SCREEN(data[n].screen, write_screen, (void *) msg);
136     }
137 }
138
139 int
140 main(int argc GCC_UNUSED,
141      char *argv[]GCC_UNUSED)
142 {
143     int j;
144     int count;
145     DITTO *data;
146
147     if (argc <= 1)
148         usage();
149
150     if ((data = typeCalloc(DITTO, argc)) == 0)
151         failed("calloc data");
152
153     data[0].input = stdin;
154     data[0].output = stdout;
155     for (j = 1; j < argc; j++) {
156         data[j].input =
157             data[j].output = open_tty(argv[j]);
158     }
159
160     /*
161      * If we got this far, we have open connection(s) to the terminal(s).
162      * Set up the screens.
163      */
164     for (j = 0; j < argc; j++) {
165         data[j].screen = newterm((char *) 0,    /* assume $TERM is the same */
166                                  data[j].output,
167                                  data[j].input);
168         if (data[j].screen == 0)
169             failed("newterm");
170         cbreak();
171         noecho();
172         scrollok(stdscr, TRUE);
173         nodelay(stdscr, TRUE);
174     }
175
176     /*
177      * Loop, reading characters from any of the inputs and writing to all
178      * of the screens.
179      */
180     for (count = 0;; ++count) {
181         char message[80];
182         int ch;
183         int which = (count % argc);
184
185         napms(20);
186         ch = USING_SCREEN(data[which].screen, read_screen, 0);
187         if (ch == ERR) {
188             continue;
189         }
190         if (ch == CTRL('D'))
191             break;
192         sprintf(message, "from[%d:%d] '%c' (%#x)\n", count, which, ch, ch);
193         show_ditto(data, argc, message);
194     }
195
196     /*
197      * Cleanup and exit
198      */
199     for (j = argc - 1; j >= 0; j--) {
200         USING_SCREEN(data[j].screen, close_screen, 0);
201         fprintf(data[j].output, "**Closed\r\n");
202         fflush(data[j].output);
203         fclose(data[j].output);
204         delscreen(data[j].screen);
205     }
206     ExitProgram(EXIT_SUCCESS);
207 }