--- ray/src/util/rfluxmtx.c 2015/05/21 05:54:54 2.29 +++ ray/src/util/rfluxmtx.c 2016/08/19 18:31:26 2.39 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rfluxmtx.c,v 2.29 2015/05/21 05:54:54 greg Exp $"; +static const char RCSid[] = "$Id: rfluxmtx.c,v 2.39 2016/08/19 18:31:26 greg Exp $"; #endif /* * Calculate flux transfer matrix or matrices using rcontrib @@ -11,27 +11,13 @@ static const char RCSid[] = "$Id: rfluxmtx.c,v 2.29 20 #include #include "rtio.h" #include "rtmath.h" -#include "rtprocess.h" +#include "paths.h" #include "bsdf.h" #include "bsdf_m.h" #include "random.h" #include "triangulate.h" #include "platform.h" -#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */ -#undef getc -#define getc getc_unlocked -#endif - -#ifdef _WIN32 -#define SPECIALS " \t\"$*?" -#define QUOTCHAR '"' -#else -#define SPECIALS " \t\n'\"()${}*?[];|&" -#define QUOTCHAR '\'' -#define ALTQUOT '"' -#endif - #define MAXRCARG 512 char *progname; /* global argv[0] */ @@ -156,66 +142,28 @@ oconv_command(int ac, char *av[]) strcpy(cp, *av++); while (*cp) cp++; *cp++ = ' '; - if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) { - fputs(progname, stderr); - fputs(": too many file arguments!\n", stderr); - exit(1); - } + if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) + goto overrun; } - strcpy(cp, recv); /* receiver goes last */ + /* receiver goes last */ + if (matchany(recv, SPECIALS)) { + *cp++ = QUOTCHAR; + while (*recv) { + if (cp >= oconvbuf+(sizeof(oconvbuf)-3)) + goto overrun; + *cp++ = *recv++; + } + *cp++ = QUOTCHAR; + *cp = '\0'; + } else + strcpy(cp, recv); return(oconvbuf); +overrun: + fputs(progname, stderr); + fputs(": too many file arguments!\n", stderr); + exit(1); } -/* Check if any of the characters in str2 are found in str1 */ -static int -matchany(const char *str1, const char *str2) -{ - while (*str1) { - const char *cp = str2; - while (*cp) - if (*cp++ == *str1) - return(*str1); - ++str1; - } - return(0); -} - - -/* Convert a set of arguments into a command line for pipe() or system() */ -static char * -convert_commandline(char *cmd, const int len, char *av[]) -{ - int match; - char *cp; - - for (cp = cmd; *av != NULL; av++) { - const int n = strlen(*av); - if (cp+n >= cmd+(len-3)) { - fputs(progname, stderr); - return(NULL); - } - if ((match = matchany(*av, SPECIALS))) { - const int quote = -#ifdef ALTQUOT - (match == QUOTCHAR) ? ALTQUOT : -#endif - QUOTCHAR; - *cp++ = quote; - strcpy(cp, *av); - cp += n; - *cp++ = quote; - } else { - strcpy(cp, *av); - cp += n; - } - *cp++ = ' '; - } - if (cp <= cmd) - return(NULL); - *--cp = '\0'; - return(cmd); -} - /* Open a pipe to/from a command given as an argument list */ static FILE * popen_arglist(char *av[], char *mode) @@ -233,7 +181,7 @@ popen_arglist(char *av[], char *mode) return(popen(cmd, mode)); } -#ifdef _WIN32 +#if defined(_WIN32) || defined(_WIN64) /* Execute system command (Windows version) */ static int my_exec(char *av[]) @@ -318,6 +266,7 @@ parse_params(PARAMS *p, char *pargs) { char *cp = pargs; int nparams = 0; + int quot; int i; for ( ; ; ) { @@ -354,14 +303,23 @@ parse_params(PARAMS *p, char *pargs) case 'o': if (*cp++ != '=') break; + quot = 0; + if ((*cp == '"') | (*cp == '\'')) + quot = *cp++; i = 0; - while (*cp && !isspace(*cp++)) - i++; + while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) { + i++; cp++; + } if (!i) break; - *--cp = '\0'; + if (!*cp) { + if (quot) + break; + cp[1] = '\0'; + } + *cp = '\0'; p->outfn = savqstr(cp-i); - *cp++ = ' '; + *cp++ = quot ? quot : ' '; ++nparams; continue; case ' ': @@ -600,7 +558,7 @@ ssamp_poly(FVECT orig, SURF *sp, double x) if (ptp == NULL) { /* need to triangulate */ ptp = (POLYTRIS *)malloc(sizeof(POLYTRIS) + - sizeof(struct ptri)*(sp->nfargs/3 - 3)); + sizeof(struct ptri)*(sp->nfargs/3 - 1)); if (ptp == NULL) goto memerr; if (sp->nfargs == 3) { /* simple case */ @@ -674,7 +632,7 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, /* special case for lone surface */ if (p->nsurfs == 1) { sp = p->slist; - if (DOT(sp->snrm, rdir) >= -FTINY) { + if (DOT(sp->snrm, rdir) >= FTINY) { fprintf(stderr, "%s: internal - sample behind sender '%s'\n", progname, sp->sname); @@ -685,7 +643,11 @@ sample_origin(PARAMS *p, FVECT orig, const FVECT rdir, if (p->nsurfs > nall) { /* (re)allocate surface area cache */ if (projsa) free(projsa); projsa = (double *)malloc(sizeof(double)*p->nsurfs); - if (!projsa) return(0); + if (projsa == NULL) { + fputs(progname, stderr); + fputs(": out of memory in sample_origin!\n", stderr); + exit(1); + } nall = p->nsurfs; } /* compute projected areas */ @@ -728,7 +690,7 @@ sample_uniform(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -758,7 +720,7 @@ sample_shirchiu(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -806,7 +768,7 @@ sample_reinhart(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -857,7 +819,7 @@ sample_klems(PARAMS *p, int b, FILE *fp) duvw[2]*p->nrm[i] ; if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0])) return(0); - if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2) + if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2) return(0); } return(1); @@ -984,7 +946,7 @@ add_surface(int st, const char *oname, FILE *fp) snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1)); if (snew == NULL) { fputs(progname, stderr); - fputs(": out of memory!\n", stderr); + fputs(": out of memory in add_surface!\n", stderr); exit(1); } strncpy(snew->sname, oname, sizeof(snew->sname)-1);