1 /*
2  * misc.c
3  *
4  * Copyright (C) 1989, 1991, Craig E. Kolb
5  * All rights reserved.
6  *
7  * This software may be freely copied, modified, and redistributed
8  * provided that this copyright notice is preserved on all copies.
9  *
10  * You may not distribute this software, in whole or in part, as part of
11  * any commercial product without the express consent of the authors.
12  *
13  * There is no warranty or other guarantee of fitness of this software
14  * for any purpose.  It is provided solely "as is".
15  *
16  * $Id: misc.c,v 4.0.1.1 92/01/14 18:29:05 cek Exp Locker: cek $
17  *
18  * $Log:	misc.c,v $
19  * Revision 4.0.1.1  92/01/14  18:29:05  cek
20  * patch3: Added support for switching cpp on/off.
21  *
22  * Revision 4.0  91/07/17  14:46:31  kolb
23  * Initial version.
24  *
25  */
26 #include "rayshade.h"
27 #ifdef RUSAGE
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #else
31 #ifdef TIMES
32 #include <sys/types.h>
33 #include <sys/times.h>
34 #include <sys/param.h>
35 #endif
36 #endif
37 #include "options.h"
38 #include "stats.h"
39 
40 Float RSabstmp;	/* Temporary value used by fabs macro.  Ugly. */
41 static void RSmessage();
42 
43 /*
44  * Open input file and call yyparse().
45  */
46 void
RSReadInputFile()47 RSReadInputFile()
48 {
49 	extern FILE *yyin;	/* lex/yacc file pointer */
50 	extern char yyfilename[];
51 
52 #if defined(CPPSTDIN) && defined(POPEN)
53 	char cmd[BUFSIZ];
54 
55 	if (Options.cppargs != (char *)NULL)
56 		sprintf(cmd, "%s %s ", CPPSTDIN, Options.cppargs);
57 	else
58 		/* fromstdin */
59 		sprintf(cmd, "%s %s ", CPPSTDIN, CPPMINUS);
60 
61 	if (Options.inputname == (char *)NULL) {
62 		(void)strcpy(yyfilename, "stdin");
63 	} else {
64 		(void)strcpy(yyfilename, Options.inputname);
65 		(void)strcat(cmd, Options.inputname);
66 	}
67 
68 	if (Options.cpp) {
69 		yyin = popen(cmd, "r");
70 		if (yyin == (FILE *)NULL)
71 			RLerror(RL_PANIC, "popen of \"%s\" failed!\n", cmd);
72 	} else {
73 #endif
74 	if (Options.inputname == (char *)NULL) {
75 		yyin = stdin;
76 		(void)strcpy(yyfilename, "stdin");
77 	} else {
78 		(void)strcpy(yyfilename, Options.inputname);
79 		yyin = fopen(Options.inputname, "r");
80 		if (yyin == (FILE *)NULL)
81 			RLerror(RL_PANIC,
82 				"Cannot open %s.\n",Options.inputname);
83 	}
84 #if defined(CPPSTDIN) && defined(POPEN)
85 	}
86 #endif
87 	/*
88 	 * Initialize symbol table.
89 	 */
90 	SymtabInit();
91 	(void)yyparse();
92 }
93 
94 void
OpenStatsFile()95 OpenStatsFile()
96 {
97 	if (Options.statsname == (char *)NULL || Stats.fstats != stderr)
98 		return;		/* Not specified or already opened. */
99 
100 	Stats.fstats = fopen(Options.statsname, "w");
101 	if (Stats.fstats == (FILE *)NULL) {
102 		RLerror(RL_PANIC,
103 			"Cannot open stats file %s.\n", Options.statsname);
104 	}
105 }
106 
107 void
RLerror(level,pat,arg1,arg2,arg3)108 RLerror(level, pat, arg1, arg2, arg3)
109 int level;
110 char *pat, *arg1, *arg2, *arg3;
111 {
112 	switch (level) {
113 		case RL_ADVISE:
114 			if (!Options.quiet)
115 				RSmessage("Warning", pat, arg1, arg2, arg3);
116 			break;
117 		case RL_WARN:
118 			RSmessage("Warning", pat, arg1, arg2, arg3);
119 			break;
120 		case RL_ABORT:
121 			RSmessage("Error", pat, arg1, arg2, arg3);
122 			exit(1);
123 			break;
124 		case RL_PANIC:
125 			RSmessage("Fatal error", pat, arg1, arg2, arg3);
126 			exit(2);
127 			break;
128 		default:
129 			RSmessage("Unknown error", pat, arg1, arg2, arg3);
130 			exit(3);
131 	}
132 }
133 
134 static void
RSmessage(type,pat,arg1,arg2,arg3)135 RSmessage(type, pat, arg1, arg2, arg3)
136 char *type, *pat, *arg1, *arg2, *arg3;
137 {
138 	extern FILE *yyin;
139 	extern int yylineno;
140 	extern char yyfilename[];
141 
142 	if (yyin) {
143 		/*
144 		 * cleanup() hasn't nulled yyin, so line #
145 		 * info is valid.
146 		 */
147 		fprintf(stderr,"%s: %s: %s, line %d: ",
148 			Options.progname, type,
149 			yyfilename == (char *)NULL ? "stdin" :
150 				yyfilename, yylineno);
151 	} else {
152 		fprintf(stderr,"%s: %s: ", Options.progname, type);
153 	}
154 	fprintf(stderr, pat, arg1, arg2, arg3);
155 }
156 
157 #ifdef RUSAGE
158 void
RSGetCpuTime(usertime,systime)159 RSGetCpuTime(usertime, systime)
160 Float *usertime, *systime;
161 {
162 	struct rusage usage;
163 
164 	getrusage(RUSAGE_SELF, &usage);
165 
166 	*usertime = (Float)usage.ru_utime.tv_sec +
167 			(Float)usage.ru_utime.tv_usec / 1000000.;
168 	*systime = (Float)usage.ru_stime.tv_sec +
169 			(Float)usage.ru_stime.tv_usec / 1000000.;
170 }
171 
172 #else
173 #ifdef TIMES
174 
175 void
RSGetCpuTime(usertime,systime)176 RSGetCpuTime(usertime, systime)
177 Float *usertime, *systime;
178 {
179 	extern CLOCKTYPE times();
180 	struct tms time;
181 
182 	(void)times(&time);
183 	*usertime = (Float)time.tms_utime / (Float)HZ;
184 	*systime = (Float)time.tms_stime / (Float)HZ;
185 }
186 
187 #else /* !RUSAGE && !TIMES */
188 
189 void
RSGetCpuTime(usertime,systime)190 RSGetCpuTime(usertime, systime)
191 Float *usertime, *systime;
192 {
193 	*usertime = *systime = 0.;
194 }
195 
196 #endif /* TIMES */
197 #endif /* RUSAGE */
198