ncursesw-morphos/test/ditto.c

161 lines
4.5 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
* Copyright (c) 1998-2005,2007 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
1999-10-24 12:32:42 +08:00
/*
* Author: Thomas E. Dickey <dickey@clark.net> 1998
*
* $Id: ditto.c,v 1.8 2007/09/01 21:10:38 tom Exp $
1999-10-24 12:32:42 +08:00
*
* The program illustrates how to set up multiple screens from a single
* program. Invoke the program by specifying another terminal on the same
* machine by specifying its device, e.g.,
* ditto /dev/ttyp1
*/
#include <test.priv.h>
#include <sys/stat.h>
#include <errno.h>
typedef struct {
2002-10-13 11:35:53 +08:00
FILE *input;
FILE *output;
SCREEN *screen;
1999-10-24 12:32:42 +08:00
} DITTO;
static void
failed(const char *s)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
perror(s);
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
}
1999-10-24 12:32:42 +08:00
static void
usage(void)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
fprintf(stderr, "usage: ditto [terminal1 ...]\n");
ExitProgram(EXIT_FAILURE);
1997-05-15 12:00:00 +08:00
}
1999-10-24 12:32:42 +08:00
static FILE *
open_tty(char *path)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
FILE *fp;
struct stat sb;
1999-10-24 12:32:42 +08:00
2002-10-13 11:35:53 +08:00
if (stat(path, &sb) < 0)
failed(path);
if ((sb.st_mode & S_IFMT) != S_IFCHR) {
errno = ENOTTY;
failed(path);
}
fp = fopen(path, "r+");
2002-10-13 11:35:53 +08:00
if (fp == 0)
failed(path);
printf("opened %s\n", path);
return fp;
1997-05-15 12:00:00 +08:00
}
static void
show_ditto(DITTO * data, int count, int which, int ch)
{
int n;
for (n = 0; n < count; n++) {
set_term(data[n].screen);
addch(UChar(ch));
refresh();
}
set_term(data[which].screen);
}
1999-10-24 12:32:42 +08:00
int
2005-10-10 02:41:57 +08:00
main(int argc GCC_UNUSED,
char *argv[]GCC_UNUSED)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
int j;
int count;
2002-10-13 11:35:53 +08:00
DITTO *data;
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
if (argc <= 1)
usage();
1997-05-15 12:00:00 +08:00
2005-10-10 02:41:57 +08:00
if ((data = (DITTO *) calloc((unsigned) argc, sizeof(DITTO))) == 0)
2002-10-13 11:35:53 +08:00
failed("calloc data");
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
data[0].input = stdin;
data[0].output = stdout;
for (j = 1; j < argc; j++) {
data[j].input =
data[j].output = open_tty(argv[j]);
}
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
/*
* If we got this far, we have open connection(s) to the terminal(s).
* Set up the screens.
*/
for (j = 0; j < argc; j++) {
2005-10-10 02:41:57 +08:00
data[j].screen = newterm((char *) 0, /* assume $TERM is the same */
data[j].output,
data[j].input);
2002-10-13 11:35:53 +08:00
if (data[j].screen == 0)
failed("newterm");
cbreak();
noecho();
scrollok(stdscr, TRUE);
nodelay(stdscr, TRUE);
2002-10-13 11:35:53 +08:00
}
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
/*
* Loop, reading characters from any of the inputs and writing to all
* of the screens.
*/
for (count = 0;; ++count) {
2002-10-13 11:35:53 +08:00
int ch;
int which = (count % argc);
set_term(data[which].screen);
napms(20);
2002-10-13 11:35:53 +08:00
ch = getch();
if (ch == ERR) {
/* echochar('.'); */
2002-10-13 11:35:53 +08:00
continue;
1997-05-15 12:00:00 +08:00
}
if (ch == CTRL('D'))
break;
show_ditto(data, argc, which, ch);
2002-10-13 11:35:53 +08:00
}
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
/*
* Cleanup and exit
*/
for (j = argc - 1; j >= 0; j--) {
set_term(data[j].screen);
endwin();
}
ExitProgram(EXIT_SUCCESS);
1997-05-15 12:00:00 +08:00
}