--- ray/src/rt/devcomm.c 2003/02/22 02:07:28 2.6 +++ ray/src/rt/devcomm.c 2014/07/08 18:25:00 2.17 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: devcomm.c,v 2.6 2003/02/22 02:07:28 greg Exp $"; +static const char RCSid[] = "$Id: devcomm.c,v 2.17 2014/07/08 18:25:00 greg Exp $"; #endif /* * devcomm.c - communication routines for separate drivers. @@ -7,93 +7,47 @@ static const char RCSid[] = "$Id: devcomm.c,v 2.6 2003 * External symbols declared in driver.h */ -/* ==================================================================== - * The Radiance Software License, Version 1.0 - * - * Copyright (c) 1990 - 2002 The Regents of the University of California, - * through Lawrence Berkeley National Laboratory. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes Radiance software - * (http://radsite.lbl.gov/) - * developed by the Lawrence Berkeley National Laboratory - * (http://www.lbl.gov/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Radiance," "Lawrence Berkeley National Laboratory" - * and "The Regents of the University of California" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact radiance@radsite.lbl.gov. - * - * 5. Products derived from this software may not be called "Radiance", - * nor may "Radiance" appear in their name, without prior written - * permission of Lawrence Berkeley National Laboratory. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of Lawrence Berkeley National Laboratory. For more - * information on Lawrence Berkeley National Laboratory, please see - * . - */ +#include "copyright.h" -#include "standard.h" +#include +#ifndef _WIN32 +#include +#endif -#include "color.h" - +#include "paths.h" +#include "platform.h" +#include "standard.h" #include "driver.h" -#include "vfork.h" - #ifndef DEVPATH #define DEVPATH getenv("PATH") /* device search path */ #endif -static int comm_getcur(); -static void comm_close(), comm_clear(), comm_paintr(), - comm_comin(), comm_comout(), comm_flush(); +FILE *devin, *devout; +int devchild; +static struct driver * final_connect(void); +static void mygets(char *s, FILE *fp); +static void myputs(char *s, FILE *fp); +static void reply_error(char *routine); +static void getstate(void); + +static dr_closef_t comm_close; +static dr_clearf_t comm_clear; +static dr_paintrf_t comm_paintr; +static dr_getcurf_t comm_getcur; +static dr_comoutf_t comm_comout; +static dr_cominf_t comm_comin; +static dr_flushf_t comm_flush; + struct driver comm_driver = { comm_close, comm_clear, comm_paintr, comm_getcur, 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 */ +final_connect(void) /* verify and initialize connection */ { putw(COM_SENDM, devout); fflush(devout); @@ -103,15 +57,19 @@ final_connect() /* verify and initialize connection getstate(); /* set error vectors */ erract[COMMAND].pf = comm_comout; + /* doesn't work with raypcalls.c 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; +slave_init( /* run rview in slave mode */ + char *dname, + char *id +) { devchild = -1; /* we're the slave here */ devout = stdout; /* use standard input */ @@ -121,8 +79,10 @@ char *dname, *id; struct driver * -comm_init(dname, id) /* set up and execute driver */ -char *dname, *id; +comm_init( /* set up and execute driver */ + char *dname, + char *id +) { char *dvcname; int p1[2], p2[2]; @@ -133,15 +93,16 @@ char *dname, *id; eputs(": not found\n"); return(NULL); } +#ifdef RHAS_FORK_EXEC /* open communication pipes */ if (pipe(p1) == -1 || pipe(p2) == -1) goto syserr; - if ((devchild = vfork()) == 0) { /* fork driver process */ + if ((devchild = fork()) == 0) { /* fork driver process */ close(p1[1]); close(p2[0]); sprintf(pin, "%d", p1[0]); sprintf(pout, "%d", p2[1]); - execl(dvcname, dname, pin, pout, id, 0); + execl(dvcname, dname, pin, pout, id, NULL); perror(dvcname); _exit(127); } @@ -149,6 +110,12 @@ char *dname, *id; goto syserr; close(p1[0]); close(p2[1]); + /* + * Close write stream on exec to avoid multiprocessing deadlock. + * No use in read stream without it, so set flag there as well. + */ + fcntl(p1[1], F_SETFD, FD_CLOEXEC); + fcntl(p2[0], F_SETFD, FD_CLOEXEC); if ((devout = fdopen(p1[1], "w")) == NULL) goto syserr; if ((devin = fdopen(p2[0], "r")) == NULL) @@ -157,11 +124,19 @@ char *dname, *id; syserr: perror(dname); return(NULL); + +#else /* ! RHAS_FORK_EXEC */ + + eputs(dname); + eputs(": no fork/exec\n"); + return(NULL); + +#endif /* ! RHAS_FORK_EXEC */ } static void -comm_close() /* done with driver */ +comm_close(void) /* done with driver */ { int pid; @@ -172,14 +147,18 @@ comm_close() /* done with driver */ fclose(devin); if (devchild < 0) return; +#ifndef _WIN32 while ((pid = wait(0)) != -1 && pid != devchild) ; +#endif } static void -comm_clear(xres, yres) /* clear screen */ -int xres, yres; +comm_clear( /* clear screen */ + int xres, + int yres +) { putc(COM_CLEAR, devout); putw(xres, devout); @@ -189,9 +168,13 @@ int xres, yres; static void -comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ -COLOR col; -int xmin, ymin, xmax, ymax; +comm_paintr( /* paint a rectangle */ + COLOR col, + int xmin, + int ymin, + int xmax, + int ymax +) { putc(COM_PAINTR, devout); fwrite((char *)col, sizeof(COLOR), 1, devout); @@ -203,7 +186,7 @@ int xmin, ymin, xmax, ymax; static void -comm_flush() /* flush output to driver */ +comm_flush(void) /* flush output to driver */ { putc(COM_FLUSH, devout); fflush(devout); @@ -214,8 +197,10 @@ comm_flush() /* flush output to driver */ static int -comm_getcur(xp, yp) /* get and return cursor position */ -int *xp, *yp; +comm_getcur( /* get and return cursor position */ + int *xp, + int *yp +) { int c; @@ -231,8 +216,9 @@ int *xp, *yp; static void -comm_comout(str) /* print string to command line */ -char *str; +comm_comout( /* print string to command line */ + char *str +) { putc(COM_COMOUT, devout); myputs(str, devout); @@ -242,9 +228,10 @@ char *str; static void -comm_comin(buf, prompt) /* read string from command line */ -char *buf; -char *prompt; +comm_comin( /* read string from command line */ + char *buf, + char *prompt +) { putc(COM_COMIN, devout); if (prompt == NULL) @@ -262,11 +249,12 @@ char *prompt; static void -mygets(s, fp) /* get string from file (with nul) */ -register char *s; -register FILE *fp; +mygets( /* get string from file (with nul) */ + char *s, + FILE *fp +) { - register int c; + int c; while ((c = getc(fp)) != EOF) if ((*s++ = c) == '\0') @@ -276,9 +264,10 @@ register FILE *fp; static void -myputs(s, fp) /* put string to file (with nul) */ -register char *s; -register FILE *fp; +myputs( /* put string to file (with nul) */ + char *s, + FILE *fp +) { do putc(*s, fp); @@ -287,8 +276,9 @@ register FILE *fp; static void -reply_error(routine) /* what should we do here? */ -char *routine; +reply_error( /* what should we do here? */ + char *routine +) { eputs(routine); eputs(": driver reply error\n"); @@ -297,7 +287,7 @@ char *routine; static void -getstate() /* get driver state variables */ +getstate(void) /* get driver state variables */ { fread((char *)&comm_driver.pixaspect, sizeof(comm_driver.pixaspect), 1, devin);