X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fditto.c;h=34ef6a532e5c1928c67e2f0a6c0e98ddc6671962;hp=8ceafe5de11a05da49d2188c5bb58c71a67a1dcc;hb=396a05943b7da5039dd15d79c4385c7d2a75d6d4;hpb=0eb88fc5281804773e2a0c7a488a4452463535ce diff --git a/test/ditto.c b/test/ditto.c index 8ceafe5d..34ef6a53 100644 --- a/test/ditto.c +++ b/test/ditto.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998 Free Software Foundation, Inc. * + * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -29,7 +29,7 @@ /* * Author: Thomas E. Dickey 1998 * - * $Id: ditto.c,v 1.3 1998/08/15 23:39:34 tom Exp $ + * $Id: ditto.c,v 1.19 2008/04/12 23:37:32 tom Exp $ * * The program illustrates how to set up multiple screens from a single * program. Invoke the program by specifying another terminal on the same @@ -40,109 +40,168 @@ #include #include +#ifdef USE_XTERM_PTY +#include USE_OPENPTY_HEADER +#endif + typedef struct { - FILE *input; - FILE *output; - SCREEN *screen; + FILE *input; + FILE *output; + SCREEN *screen; } DITTO; static void failed(const char *s) { - perror(s); - exit(EXIT_FAILURE); + perror(s); + ExitProgram(EXIT_FAILURE); } static void usage(void) { - fprintf(stderr, "usage: ditto [terminal1 ...]\n"); - exit(EXIT_FAILURE); + fprintf(stderr, "usage: ditto [terminal1 ...]\n"); + ExitProgram(EXIT_FAILURE); } static FILE * open_tty(char *path) { - FILE *fp; - struct stat sb; - - if (stat(path, &sb) < 0) - failed(path); - if ((sb.st_mode & S_IFMT) != S_IFCHR) { - errno = ENOTTY; - failed(path); - } - fp = fopen(path, "a+"); - if (fp == 0) - failed(path); - printf("opened %s\n", path); - return fp; + FILE *fp; +#ifdef USE_XTERM_PTY + int amaster; + int aslave; + char slave_name[1024]; + char s_option[1024]; + char *leaf; + + if (openpty(&amaster, &aslave, slave_name, 0, 0) != 0) + failed("openpty"); + if ((leaf = strrchr(slave_name, '/')) == 0) { + errno = EISDIR; + failed(slave_name); + } + sprintf(s_option, "-S%s/%d", slave_name, aslave); + if (fork()) { + execlp("xterm", "xterm", s_option, "-title", path, (char *) 0); + _exit(0); + } + fp = fdopen(amaster, "r+"); +#else + struct stat sb; + + if (stat(path, &sb) < 0) + failed(path); + if ((sb.st_mode & S_IFMT) != S_IFCHR) { + errno = ENOTTY; + failed(path); + } + fp = fopen(path, "r+"); + if (fp == 0) + failed(path); + printf("opened %s\n", path); +#endif + return fp; +} + +static int +close_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED) +{ + (void) sp; + (void) arg; + return endwin(); +} + +static int +read_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED) +{ + return getch(); +} + +static int +write_screen(SCREEN *sp GCC_UNUSED, void *arg GCC_UNUSED) +{ + addstr((char *) arg); + refresh(); + return OK; +} + +static void +show_ditto(DITTO * data, int count, char *msg) +{ + int n; + + for (n = 0; n < count; n++) { + USING_SCREEN(data[n].screen, write_screen, (void *) msg); + } } int -main( - int argc GCC_UNUSED, - char *argv[] GCC_UNUSED) +main(int argc GCC_UNUSED, + char *argv[]GCC_UNUSED) { - int j; - int active_tty = 0; - DITTO *data; + int j; + int count; + DITTO *data; - if (argc <= 1) - usage(); + if (argc <= 1) + usage(); - if ((data = (DITTO *)calloc(argc, sizeof(DITTO))) == 0) - failed("calloc data"); + if ((data = typeCalloc(DITTO, argc)) == 0) + failed("calloc data"); - data[0].input = stdin; - data[0].output = stdout; - for (j = 1; j < argc; j++) { - data[j].input = - data[j].output = open_tty(argv[j]); - } + data[0].input = stdin; + data[0].output = stdout; + for (j = 1; j < argc; j++) { + data[j].input = + data[j].output = open_tty(argv[j]); + } - /* - * If we got this far, we have open connection(s) to the terminal(s). - * Set up the screens. - */ - for (j = 0; j < argc; j++) { - active_tty++; - data[j].screen = newterm( - (char *)0, /* assume $TERM is the same */ - data[j].output, - data[j].input); - if (data[j].screen == 0) - failed("newterm"); - cbreak(); - noecho(); - scrollok(stdscr, TRUE); - } + /* + * If we got this far, we have open connection(s) to the terminal(s). + * Set up the screens. + */ + for (j = 0; j < argc; j++) { + data[j].screen = newterm((char *) 0, /* assume $TERM is the same */ + data[j].output, + data[j].input); + if (data[j].screen == 0) + failed("newterm"); + cbreak(); + noecho(); + scrollok(stdscr, TRUE); + nodelay(stdscr, TRUE); + } - /* - * Loop, reading characters from any of the inputs and writing to all - * of the screens. - */ - for(;;) { - int ch; - set_term(data[0].screen); - ch = getch(); - if (ch == ERR) - continue; - if (ch == 4) - break; - for (j = 0; j < argc; j++) { - set_term(data[j].screen); - addch(ch); - refresh(); - } - } + /* + * Loop, reading characters from any of the inputs and writing to all + * of the screens. + */ + for (count = 0;; ++count) { + char message[80]; + int ch; + int which = (count % argc); - /* - * Cleanup and exit - */ - for (j = argc-1; j >= 0; j--) { - set_term(data[j].screen); - endwin(); + napms(20); + ch = USING_SCREEN(data[which].screen, read_screen, 0); + if (ch == ERR) { + continue; } - return EXIT_SUCCESS; + if (ch == CTRL('D')) + break; + sprintf(message, "from[%d:%d] '%c' (%#x)\n", count, which, ch, ch); + show_ditto(data, argc, message); + } + + /* + * Cleanup and exit + */ + for (j = argc - 1; j >= 0; j--) { + USING_SCREEN(data[j].screen, close_screen, 0); + fprintf(data[j].output, "**Closed\r\n"); + fflush(data[j].output); + fclose(data[j].output); + delscreen(data[j].screen); + } + ExitProgram(EXIT_SUCCESS); }