Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (c) 1998-2011,2012 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.42 2012/11/24 20:16:18 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 | #define MAX_FIFO 256 |
| 56 | |
| 57 | #define THIS_FIFO(n) ((n) % MAX_FIFO) |
| 58 | #define NEXT_FIFO(n) THIS_FIFO((n) + 1) |
| 59 | |
| 60 | typedef struct { |
| 61 | unsigned long sequence; |
| 62 | int head; |
| 63 | int tail; |
| 64 | int data[MAX_FIFO]; |
| 65 | } FIFO; |
| 66 | |
| 67 | typedef struct { |
| 68 | unsigned long sequence; |
| 69 | } PEEK; |
| 70 | |
| 71 | /* |
| 72 | * Data "owned" for a single screen. Each screen is divided into windows that |
| 73 | * show the text read from each terminal. Input from a given screen will also |
| 74 | * be read into one window per screen. |
| 75 | */ |
| 76 | typedef struct { |
| 77 | FILE *input; |
| 78 | FILE *output; |
| 79 | SCREEN *screen; /* this screen - curses internal data */ |
| 80 | int which1; /* this screen's index in DITTO[] array */ |
| 81 | int length; /* length of windows[] and peeks[] */ |
| 82 | char **titles; /* per-window titles */ |
| 83 | WINDOW **parents; /* display boxes around each screen's data */ |
| 84 | WINDOW **windows; /* display data from each screen */ |
| 85 | PEEK *peeks; /* indices for each screen's fifo */ |
| 86 | FIFO fifo; /* fifo for this screen */ |
| 87 | #ifdef USE_PTHREADS |
| 88 | pthread_t thread; |
| 89 | #endif |
| 90 | } DITTO; |
| 91 | |
| 92 | /* |
| 93 | * Structure used to pass multiple parameters via the use_screen() |
| 94 | * single-parameter interface. |
| 95 | */ |
| 96 | typedef struct { |
| 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 */ |
| 100 | } DDATA; |
| 101 | |
| 102 | static void failed(const char *) GCC_NORETURN; |
| 103 | static void usage(void) GCC_NORETURN; |
| 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 | |
| 159 | if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0 |
| 160 | || strlen(slave_name) > sizeof(slave_name) - 1) |
| 161 | failed("openpty"); |
| 162 | if (strrchr(slave_name, '/') == 0) { |
| 163 | errno = EISDIR; |
| 164 | failed(slave_name); |
| 165 | } |
| 166 | sprintf(s_option, "-S%s/%d", slave_name, aslave); |
| 167 | if (fork()) { |
| 168 | execlp("xterm", "xterm", s_option, "-title", path, (char *) 0); |
| 169 | _exit(0); |
| 170 | } |
| 171 | fp = fdopen(amaster, "r+"); |
| 172 | if (fp == 0) |
| 173 | failed(path); |
| 174 | #else |
| 175 | struct stat sb; |
| 176 | |
| 177 | if (stat(path, &sb) < 0) |
| 178 | failed(path); |
| 179 | if ((sb.st_mode & S_IFMT) != S_IFCHR) { |
| 180 | errno = ENOTTY; |
| 181 | failed(path); |
| 182 | } |
| 183 | fp = fopen(path, "r+"); |
| 184 | if (fp == 0) |
| 185 | failed(path); |
| 186 | printf("opened %s\n", path); |
| 187 | #endif |
| 188 | assert(fp != 0); |
| 189 | return fp; |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | init_screen( |
| 194 | #if HAVE_USE_WINDOW |
| 195 | SCREEN *sp GCC_UNUSED, |
| 196 | #endif |
| 197 | void *arg) |
| 198 | { |
| 199 | DITTO *target = (DITTO *) arg; |
| 200 | int high, wide; |
| 201 | int k; |
| 202 | |
| 203 | cbreak(); |
| 204 | noecho(); |
| 205 | scrollok(stdscr, TRUE); |
| 206 | box(stdscr, 0, 0); |
| 207 | |
| 208 | target->parents = typeCalloc(WINDOW *, (size_t) target->length); |
| 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->parents[k] = outer; |
| 229 | target->windows[k] = inner; |
| 230 | } |
| 231 | doupdate(); |
| 232 | } |
| 233 | |
| 234 | static void |
| 235 | open_screen(DITTO * target, char **source, int length, int which1) |
| 236 | { |
| 237 | if (which1 != 0) { |
| 238 | target->input = |
| 239 | target->output = open_tty(source[which1]); |
| 240 | } else { |
| 241 | target->input = stdin; |
| 242 | target->output = stdout; |
| 243 | } |
| 244 | |
| 245 | target->which1 = which1; |
| 246 | target->titles = source; |
| 247 | target->length = length; |
| 248 | target->fifo.head = -1; |
| 249 | target->screen = newterm((char *) 0, /* assume $TERM is the same */ |
| 250 | target->output, |
| 251 | target->input); |
| 252 | |
| 253 | if (target->screen == 0) |
| 254 | failed("newterm"); |
| 255 | |
| 256 | (void) USING_SCREEN(target->screen, init_screen, target); |
| 257 | } |
| 258 | |
| 259 | static int |
| 260 | close_screen( |
| 261 | #if HAVE_USE_WINDOW |
| 262 | SCREEN *sp GCC_UNUSED, |
| 263 | #endif |
| 264 | void *arg GCC_UNUSED) |
| 265 | { |
| 266 | #if HAVE_USE_WINDOW |
| 267 | (void) sp; |
| 268 | #endif |
| 269 | (void) arg; |
| 270 | return endwin(); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Read data from the 'source' screen. |
| 275 | */ |
| 276 | static int |
| 277 | read_screen( |
| 278 | #if HAVE_USE_WINDOW |
| 279 | SCREEN *sp GCC_UNUSED, |
| 280 | #endif |
| 281 | void *arg) |
| 282 | { |
| 283 | DDATA *data = (DDATA *) arg; |
| 284 | DITTO *ditto = &(data->ditto[data->source]); |
| 285 | WINDOW *win = ditto->windows[data->source]; |
| 286 | int ch = wgetch(win); |
| 287 | |
| 288 | if (ch > 0 && ch < 256) |
| 289 | put_fifo(&(ditto->fifo), ch); |
| 290 | else |
| 291 | ch = ERR; |
| 292 | |
| 293 | return ch; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Write all of the data that's in fifos for the 'target' screen. |
| 298 | */ |
| 299 | static int |
| 300 | write_screen( |
| 301 | #if HAVE_USE_WINDOW |
| 302 | SCREEN *sp GCC_UNUSED, |
| 303 | #endif |
| 304 | void *arg GCC_UNUSED) |
| 305 | { |
| 306 | DDATA *data = (DDATA *) arg; |
| 307 | DITTO *ditto = &(data->ditto[data->target]); |
| 308 | bool changed = FALSE; |
| 309 | int which; |
| 310 | |
| 311 | for (which = 0; which < ditto->length; ++which) { |
| 312 | WINDOW *win = ditto->windows[which]; |
| 313 | FIFO *fifo = &(data->ditto[which].fifo); |
| 314 | PEEK *peek = &(ditto->peeks[which]); |
| 315 | int ch; |
| 316 | |
| 317 | while ((ch = peek_fifo(fifo, peek)) > 0) { |
| 318 | changed = TRUE; |
| 319 | |
| 320 | waddch(win, (chtype) ch); |
| 321 | wnoutrefresh(win); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (changed) |
| 326 | doupdate(); |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | static void |
| 331 | show_ditto(DITTO * data, int count, DDATA * ddata) |
| 332 | { |
| 333 | int n; |
| 334 | |
| 335 | (void) data; |
| 336 | for (n = 0; n < count; n++) { |
| 337 | ddata->target = n; |
| 338 | USING_SCREEN(data[n].screen, write_screen, (void *) ddata); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | #ifdef USE_PTHREADS |
| 343 | static void * |
| 344 | handle_screen(void *arg) |
| 345 | { |
| 346 | DDATA ddata; |
| 347 | int ch; |
| 348 | |
| 349 | memset(&ddata, 0, sizeof(ddata)); |
| 350 | ddata.ditto = (DITTO *) arg; |
| 351 | ddata.source = ddata.ditto->which1; |
| 352 | ddata.ditto -= ddata.source; /* -> base of array */ |
| 353 | |
| 354 | for (;;) { |
| 355 | ch = read_screen(ddata.ditto->screen, &ddata); |
| 356 | if (ch == CTRL('D')) { |
| 357 | int later = (ddata.source ? ddata.source : -1); |
| 358 | int j; |
| 359 | |
| 360 | for (j = ddata.ditto->length - 1; j > 0; --j) { |
| 361 | if (j != later) { |
| 362 | pthread_cancel(ddata.ditto[j].thread); |
| 363 | } |
| 364 | } |
| 365 | if (later > 0) { |
| 366 | pthread_cancel(ddata.ditto[later].thread); |
| 367 | } |
| 368 | break; |
| 369 | } |
| 370 | show_ditto(ddata.ditto, ddata.ditto->length, &ddata); |
| 371 | } |
| 372 | return NULL; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | int |
| 377 | main(int argc, char *argv[]) |
| 378 | { |
| 379 | int j; |
| 380 | DITTO *data; |
| 381 | #ifndef USE_PTHREADS |
| 382 | int count; |
| 383 | #endif |
| 384 | |
| 385 | if (argc <= 1) |
| 386 | usage(); |
| 387 | |
| 388 | if ((data = typeCalloc(DITTO, (size_t) argc)) == 0) |
| 389 | failed("calloc data"); |
| 390 | |
| 391 | assert(data != 0); |
| 392 | |
| 393 | for (j = 0; j < argc; j++) { |
| 394 | open_screen(&data[j], argv, argc, j); |
| 395 | } |
| 396 | |
| 397 | #ifdef USE_PTHREADS |
| 398 | /* |
| 399 | * For multi-threaded operation, set up a reader for each of the screens. |
| 400 | * That uses blocking I/O rather than polling for input, so no calls to |
| 401 | * napms() are needed. |
| 402 | */ |
| 403 | for (j = 0; j < argc; j++) { |
| 404 | (void) pthread_create(&(data[j].thread), NULL, handle_screen, &data[j]); |
| 405 | } |
| 406 | pthread_join(data[1].thread, NULL); |
| 407 | #else |
| 408 | /* |
| 409 | * Loop, reading characters from any of the inputs and writing to all |
| 410 | * of the screens. |
| 411 | */ |
| 412 | for (count = 0;; ++count) { |
| 413 | DDATA ddata; |
| 414 | int ch; |
| 415 | int which = (count % argc); |
| 416 | |
| 417 | napms(20); |
| 418 | |
| 419 | ddata.source = which; |
| 420 | ddata.ditto = data; |
| 421 | |
| 422 | ch = USING_SCREEN(data[which].screen, read_screen, &ddata); |
| 423 | if (ch == CTRL('D')) { |
| 424 | break; |
| 425 | } else if (ch != ERR) { |
| 426 | show_ditto(data, argc, &ddata); |
| 427 | } |
| 428 | } |
| 429 | #endif |
| 430 | |
| 431 | /* |
| 432 | * Cleanup and exit |
| 433 | */ |
| 434 | for (j = argc - 1; j >= 0; j--) { |
| 435 | USING_SCREEN(data[j].screen, close_screen, 0); |
| 436 | fprintf(data[j].output, "**Closed\r\n"); |
| 437 | |
| 438 | /* |
| 439 | * Closing before a delscreen() helps ncurses determine that there |
| 440 | * is no valid output buffer, and can remove the setbuf() data. |
| 441 | */ |
| 442 | fflush(data[j].output); |
| 443 | fclose(data[j].output); |
| 444 | delscreen(data[j].screen); |
| 445 | } |
| 446 | ExitProgram(EXIT_SUCCESS); |
| 447 | } |