--- ray/src/rt/devcomm.c 1989/11/01 17:33:03 1.7 +++ ray/src/rt/devcomm.c 2003/11/10 16:52:25 2.10 @@ -1,66 +1,84 @@ -/* Copyright (c) 1988 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: devcomm.c,v 2.10 2003/11/10 16:52:25 greg Exp $"; #endif - /* * devcomm.c - communication routines for separate drivers. * - * 10/5/88 + * External symbols declared in driver.h */ -#include +#include "copyright.h" -#include +#include "platform.h" -#include "color.h" +#include "standard.h" +#include "paths.h" + #include "driver.h" #ifndef DEVPATH #define DEVPATH getenv("PATH") /* device search path */ #endif -#ifndef BSD -#define vfork fork -#endif +static int comm_getcur(); +static void comm_close(), comm_clear(), comm_paintr(), + comm_comin(), comm_comout(), comm_flush(); -#ifndef WFLUSH -#define WFLUSH 30 /* flush after this many rays */ -#endif - -extern char *getpath(), *getenv(); - -int onsigio(); - -int comm_close(), comm_clear(), comm_paintr(), comm_errout(), - comm_getcur(), comm_comout(), comm_comin(); - -struct driver comm_driver, comm_default = { +struct driver comm_driver = { comm_close, comm_clear, comm_paintr, comm_getcur, - comm_comout, comm_comin, - MAXRES, MAXRES, 0 + comm_comout, comm_comin, comm_flush }; +static void mygets(), myputs(), reply_error(), getstate(); + FILE *devin, *devout; int devchild; +static struct driver * +final_connect() /* verify and initialize connection */ +{ + putw(COM_SENDM, devout); + fflush(devout); + if (getw(devin) != COM_RECVM) + return(NULL); + /* get driver parameters */ + getstate(); + /* set error vectors */ + erract[COMMAND].pf = comm_comout; + if (erract[WARNING].pf != NULL) + erract[WARNING].pf = comm_comout; + return(&comm_driver); +} + + struct driver * +slave_init(dname, id) /* run rview in slave mode */ +char *dname, *id; +{ + devchild = -1; /* we're the slave here */ + devout = stdout; /* use standard input */ + devin = stdin; /* and standard output */ + return(final_connect()); /* verify initialization */ +} + + +struct driver * comm_init(dname, id) /* set up and execute driver */ char *dname, *id; { - char *devname; + char *dvcname; int p1[2], p2[2]; char pin[16], pout[16]; /* find driver program */ - if ((devname = getpath(dname, DEVPATH, 1)) == NULL) { - stderr_v(dname); - stderr_v(": not found\n"); + if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) { + eputs(dname); + eputs(": not found\n"); return(NULL); } +#ifdef RHAS_FORK_EXEC /* open communication pipes */ if (pipe(p1) == -1 || pipe(p2) == -1) goto syserr; @@ -69,8 +87,8 @@ char *dname, *id; close(p2[0]); sprintf(pin, "%d", p1[0]); sprintf(pout, "%d", p2[1]); - execl(devname, dname, pin, pout, id, 0); - perror(devname); + execl(dvcname, dname, pin, pout, id, 0); + perror(dvcname); _exit(127); } if (devchild == -1) @@ -81,75 +99,74 @@ char *dname, *id; goto syserr; if ((devin = fdopen(p2[0], "r")) == NULL) goto syserr; - bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); - /* verify & get resolution */ - putw(COM_SENDM, devout); - fflush(devout); - if (getw(devin) != COM_RECVM) - return(NULL); - comm_driver.xsiz = getw(devin); - comm_driver.ysiz = getw(devin); - /* input handling */ - signal(SIGIO, onsigio); - /* set error vectors */ - cmdvec = comm_comout; - if (wrnvec != NULL) - wrnvec = comm_comout; - return(&comm_driver); + return(final_connect()); /* verify initialization */ syserr: perror(dname); return(NULL); + +#else /* ! RHAS_FORK_EXEC */ + + eputs(dname); + eputs(": no fork/exec\n"); + return(NULL); + +#endif /* ! RHAS_FORK_EXEC */ } -static +static void comm_close() /* done with driver */ { int pid; - cmdvec = NULL; /* reset error vectors */ - if (wrnvec != NULL) - wrnvec = stderr_v; - signal(SIGIO, SIG_DFL); + erract[COMMAND].pf = NULL; /* reset error vectors */ + if (erract[WARNING].pf != NULL) + erract[WARNING].pf = wputs; fclose(devout); fclose(devin); + if (devchild < 0) + return; while ((pid = wait(0)) != -1 && pid != devchild) ; } -static +static void comm_clear(xres, yres) /* clear screen */ int xres, yres; { putc(COM_CLEAR, devout); - fwrite(&xres, sizeof(xres), 1, devout); - fwrite(&yres, sizeof(yres), 1, devout); + putw(xres, devout); + putw(yres, devout); fflush(devout); } -static +static void comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ COLOR col; int xmin, ymin, xmax, ymax; { - extern long nrays; /* number of rays traced */ - static long lastflush = 0; /* ray count at last flush */ - putc(COM_PAINTR, devout); - fwrite(col, sizeof(COLOR), 1, devout); - fwrite(&xmin, sizeof(xmin), 1, devout); - fwrite(&ymin, sizeof(ymin), 1, devout); - fwrite(&xmax, sizeof(xmax), 1, devout); - fwrite(&ymax, sizeof(ymax), 1, devout); - if (nrays - lastflush >= WFLUSH) { - fflush(devout); - lastflush = nrays; - } + fwrite((char *)col, sizeof(COLOR), 1, devout); + putw(xmin, devout); + putw(ymin, devout); + putw(xmax, devout); + putw(ymax, devout); } +static void +comm_flush() /* flush output to driver */ +{ + putc(COM_FLUSH, devout); + fflush(devout); + if (getc(devin) != COM_FLUSH) + reply_error("flush"); + getstate(); +} + + static int comm_getcur(xp, yp) /* get and return cursor position */ int *xp, *yp; @@ -161,46 +178,44 @@ int *xp, *yp; if (getc(devin) != COM_GETCUR) reply_error("getcur"); c = getc(devin); - fread(xp, sizeof(*xp), 1, devin); - fread(yp, sizeof(*yp), 1, devin); + *xp = getw(devin); + *yp = getw(devin); return(c); } -static +static void comm_comout(str) /* print string to command line */ char *str; { putc(COM_COMOUT, devout); myputs(str, devout); - fflush(devout); + if (str[strlen(str)-1] == '\n') + fflush(devout); } -static -comm_comin(buf) /* read string from command line */ +static void +comm_comin(buf, prompt) /* read string from command line */ char *buf; +char *prompt; { putc(COM_COMIN, devout); + if (prompt == NULL) + putc(0, devout); + else { + putc(1, devout); + myputs(prompt, devout); + } fflush(devout); if (getc(devin) != COM_COMIN) reply_error("comin"); mygets(buf, devin); - if (comm_driver.inpready > 0) - comm_driver.inpready--; + getstate(); } -static -comm_errout(str) /* display an error message */ -char *str; -{ - comm_comout(str); - stderr_v(str); /* send to standard error also */ -} - - -static +static void mygets(s, fp) /* get string from file (with nul) */ register char *s; register FILE *fp; @@ -214,7 +229,7 @@ register FILE *fp; } -static +static void myputs(s, fp) /* put string to file (with nul) */ register char *s; register FILE *fp; @@ -225,18 +240,22 @@ register FILE *fp; } -static +static void reply_error(routine) /* what should we do here? */ char *routine; { - stderr_v(routine); - stderr_v(": driver reply error\n"); + eputs(routine); + eputs(": driver reply error\n"); quit(1); } -static -onsigio() /* input ready */ +static void +getstate() /* get driver state variables */ { - comm_driver.inpready++; + fread((char *)&comm_driver.pixaspect, + sizeof(comm_driver.pixaspect), 1, devin); + comm_driver.xsiz = getw(devin); + comm_driver.ysiz = getw(devin); + comm_driver.inpready = getw(devin); }