]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/ditto.c
ncurses 5.0
[ncurses.git] / test / ditto.c
similarity index 50%
rename from form/frm_adabind.c
rename to test/ditto.c
index 53d1ca2a7afd22ae96e022350adec23b1020eb2a..8ceafe5de11a05da49d2188c5bb58c71a67a1dcc 100644 (file)
  * authorization.                                                           *
  ****************************************************************************/
 
-/****************************************************************************
- *   Author: Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> 1995,1997        *
- ****************************************************************************/
-
-/***************************************************************************
-* Module frm_adabind.c                                                     *
-* Helper routines to ease the implementation of an Ada95 binding to        *
-* ncurses. For details and copyright of the binding see the ../Ada95       *
-* subdirectory.                                                            *
-***************************************************************************/
-#include "form.priv.h"
-
-MODULE_ID("$Id: frm_adabind.c,v 1.5 1998/02/11 12:13:43 tom Exp $")
+/*
+ * Author: Thomas E. Dickey <dickey@clark.net> 1998
+ *
+ * $Id: ditto.c,v 1.3 1998/08/15 23:39:34 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
+ * machine by specifying its device, e.g.,
+ *     ditto /dev/ttyp1
+ */
+#include <test.priv.h>
+#include <sys/stat.h>
+#include <errno.h>
 
-/* Prototypes for the functions in this module */
-void   _nc_ada_normalize_field_opts (int *opt);
-void   _nc_ada_normalize_form_opts (int *opt);
-void*  _nc_ada_getvarg(va_list *);
-FIELD* _nc_get_field(const FORM*, int);
+typedef struct {
+       FILE *input;
+       FILE *output;
+       SCREEN *screen;
+} DITTO;
 
-
-void _nc_ada_normalize_field_opts (int *opt)
+static void
+failed(const char *s)
 {
-  *opt = ALL_FIELD_OPTS & (*opt);
+       perror(s);
+       exit(EXIT_FAILURE);
 }
 
-void _nc_ada_normalize_form_opts (int *opt)
+static void
+usage(void)
 {
-  *opt = ALL_FORM_OPTS & (*opt);
+       fprintf(stderr, "usage: ditto [terminal1 ...]\n");
+       exit(EXIT_FAILURE);
 }
 
-
-/*  This tiny stub helps us to get a void pointer from an argument list.
-//  The mechanism for libform to handle arguments to field types uses
-//  unfortunately functions with variable argument lists. In the Ada95
-//  binding we replace this by a mechanism that only uses one argument
-//  that is a pointer to a record describing all the specifics of an
-//  user defined field type. So we need only this simple generic
-//  procedure to get the pointer from the arglist.
-*/
-void *_nc_ada_getvarg(va_list *ap)
+static FILE *
+open_tty(char *path)
 {
-  return va_arg(*ap,void*);
+       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;
 }
 
-FIELD* _nc_get_field(const FORM* frm, int idx) {
-  if (frm && frm->field && idx>=0 && (idx<frm->maxfield))
-    {
-      return frm->field[idx];
-    }
-  else
-    return (FIELD*)0;
+int
+main(
+       int argc GCC_UNUSED,
+       char *argv[] GCC_UNUSED)
+{
+       int j;
+       int active_tty = 0;
+       DITTO *data;
+
+       if (argc <= 1)
+               usage();
+
+       if ((data = (DITTO *)calloc(argc, sizeof(DITTO))) == 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]);
+       }
+
+       /*
+        * 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);
+       }
+
+       /*
+        * 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();
+               }
+       }
+
+       /*
+        * Cleanup and exit
+        */
+       for (j = argc-1; j >= 0; j--) {
+               set_term(data[j].screen);
+               endwin();
+       }
+       return EXIT_SUCCESS;
 }