--- ray/src/rt/devmain.c 1989/10/25 16:49:45 1.2 +++ ray/src/rt/devmain.c 2023/02/09 21:54:11 2.6 @@ -1,42 +1,32 @@ -/* Copyright (c) 1989 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: devmain.c,v 2.6 2023/02/09 21:54:11 greg Exp $"; #endif - /* * devmain.c - main for independent drivers. * * Redefine your initialization routine to dinit. - * - * 10/25/89 */ -#include +#include "copyright.h" -#include +#include "standard.h" #include "color.h" #include "driver.h" -int (*wrnvec)(), (*errvec)(), (*cmdvec)(); /* error vectors, unused */ - -long nrays = 0; /* fake it */ - struct driver *dev = NULL; /* output device */ FILE *devin, *devout; /* communications channels */ -int notified = 0; /* notified parent of input? */ - char *progname; /* driver name */ -int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(); +int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush(); int (*dev_func[NREQUESTS])() = { /* request handlers */ r_clear, r_paintr, - r_getcur, r_comout, r_comin + r_getcur, r_comout, + r_comin, r_flush }; @@ -49,26 +39,25 @@ char *argv[]; /* set up I/O */ progname = argv[0]; if (argc < 3) { - stderr_v("arg count\n"); + eputs("arg count\n"); quit(1); } devin = fdopen(atoi(argv[1]), "r"); devout = fdopen(atoi(argv[2]), "w"); if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) { - stderr_v("connection failure\n"); + eputs("connection failure\n"); quit(1); } /* open device */ if ((dev = dinit(argv[0], argv[3])) == NULL) quit(1); putw(COM_RECVM, devout); /* verify initialization */ - putw(dev->xsiz, devout); - putw(dev->ysiz, devout); + sendstate(); fflush(devout); /* loop on requests */ while ((com = getc(devin)) != EOF) { if (com >= NREQUESTS || dev_func[com] == NULL) { - stderr_v("invalid request\n"); + eputs("invalid request\n"); quit(1); } (*dev_func[com])(); /* process request */ @@ -77,6 +66,7 @@ char *argv[]; } +void quit(code) /* close device and exit */ int code; { @@ -101,19 +91,23 @@ r_paintr() /* paint a rectangle */ COLOR col; int xmin, ymin, xmax, ymax; - nrays += 5; /* pretend */ - fread(col, sizeof(COLOR), 1, devin); + getbinary((char *)col, sizeof(COLOR), 1, devin); xmin = getw(devin); ymin = getw(devin); xmax = getw(devin); ymax = getw(devin); (*dev->paintr)(col, xmin, ymin, xmax, ymax); - /* check for input */ - if (dev->inpready > notified) { - kill(getppid(), SIGIO); - notified = dev->inpready; - } } +r_flush() /* flush output */ +{ + if (dev->flush != NULL) + (*dev->flush)(); + putc(COM_FLUSH, devout); + sendstate(); + fflush(devout); +} + + r_getcur() /* get and return cursor position */ { int c; @@ -133,35 +127,11 @@ r_getcur() /* get and return cursor position */ } -r_comout() /* print string to command line */ +void +mygets(char *s, FILE *fp) /* get string from file (with nul) */ { - char str[256]; + int c; - mygets(str, devin); - (*dev->comout)(str); -} - - -r_comin() /* read string from command line */ -{ - char buf[256]; - /* get string */ - (*dev->comin)(buf); - /* reply */ - putc(COM_COMIN, devout); - myputs(buf, devout); - fflush(devout); - /* reset notify */ - notified = 0; -} - - -mygets(s, fp) /* get string from file (with nul) */ -register char *s; -register FILE *fp; -{ - register int c; - while ((c = getc(fp)) != EOF) if ((*s++ = c) == '\0') return; @@ -169,9 +139,8 @@ register FILE *fp; } -myputs(s, fp) /* put string to file (with nul) */ -register char *s; -register FILE *fp; +void +myputs(const char *s, FILE *fp) /* put string to file (with nul) */ { do putc(*s, fp); @@ -179,25 +148,57 @@ register FILE *fp; } -repaint(xmin, ymin, xmax, ymax) /* repaint section of display */ -int xmin, ymin, xmax, ymax; +r_comout() /* print string to command line */ { - /* no can do! */ + char str[256]; + + mygets(str, devin); + (*dev->comout)(str); } -stderr_v(s) /* put string to stderr */ -register char *s; +r_comin() /* read string from command line */ { - static int inline = 0; + char buf[256], *prompt; + /* get prompt */ + if (getc(devin)) { + mygets(buf, devin); + prompt = buf; + } else + prompt = NULL; + /* get string */ + (*dev->comin)(buf, prompt); + /* reply */ + putc(COM_COMIN, devout); + myputs(buf, devout); + sendstate(); + fflush(devout); +} - if (!inline++) { + +void +eputs(const char *s) /* put string to stderr */ +{ + static int midline = 0; + + if (!*s) + return; + if (!midline++) { fputs(progname, stderr); fputs(": ", stderr); } fputs(s, stderr); - if (*s && s[strlen(s)-1] == '\n') { + if (s[strlen(s)-1] == '\n') { fflush(stderr); - inline = 0; + midline = 0; } +} + + +sendstate() /* send driver state variables */ +{ + putbinary(&dev->pixaspect, sizeof(dev->pixaspect), 1, devout); + putw(dev->xsiz, devout); + putw(dev->ysiz, devout); + putw(dev->inpready, devout); }