home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / msdos / raytrace / rayshade / src / misc.c < prev    next >
Text File  |  1992-05-04  |  6KB  |  263 lines

  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. #include "common.h"
  28. #ifdef RUSAGE
  29. #include <sys/time.h>
  30. #include <sys/resource.h>
  31. #else
  32. #ifdef TIMES
  33. #include <sys/types.h>
  34. #if defined(__BORLANDC__) || defined(__WATCOMC__)
  35. #include <time.h>
  36. #else
  37. #include <sys/times.h>
  38. #include <sys/param.h>
  39. #endif
  40. #endif
  41. #endif
  42. #include "options.h"
  43. #include "stats.h"
  44.  
  45. #ifdef __WATCOMC__
  46. #define    POPEN
  47. #include <process.h>
  48. #endif
  49.  
  50. Float RSabstmp;    /* Temporary value used by fabs macro.  Ugly. */
  51. static void RSmessage();
  52.  
  53. void RLerror(int level, char *pat, char *arg1, char *arg2, char *arg3);
  54.  
  55. /*
  56.  * Open input file and call yyparse().
  57.  */
  58. void
  59. RSReadInputFile()
  60. {
  61.     extern FILE *yyin;    /* lex/yacc file pointer */
  62.     extern char yyfilename[];
  63.  
  64. #if defined(CPPSTDIN) && defined(POPEN)
  65. #ifdef __WATCOMC__
  66.     char cmd2[BUFSIZ];
  67.     char cppcmd[10][BUFSIZ];
  68. #endif
  69.     char cmd[BUFSIZ];
  70.  
  71.     if (Options.cppargs != (char *)NULL)
  72.         sprintf(cmd, "%s %s ", CPPSTDIN, Options.cppargs);
  73.     else
  74.         /* fromstdin */
  75.         sprintf(cmd, "%s %s ", CPPSTDIN, CPPMINUS);
  76.  
  77.     if (Options.inputname == (char *)NULL) {
  78.         (void)strcpy(yyfilename, "stdin");
  79. #ifdef __WATCOMC__
  80.         if (Options.cpp)
  81.             RLerror(RL_PANIC, "Can't run CPP from stdin.\n","","","");
  82. #endif
  83.     } else {
  84.         (void)strcpy(yyfilename, Options.inputname);
  85.         (void)strcat(cmd, Options.inputname);
  86.     }
  87.  
  88.     if (Options.cpp) {
  89. #ifdef __WATCOMC__
  90.         strcpy(cmd,"-o");
  91.         tmpnam(cmd2);
  92.         strcat(cmd,cmd2);
  93.         sscanf(Options.cppargs,"%s %s %s %s %s %s %s %s %s %s",
  94.             cppcmd[0],cppcmd[1],cppcmd[2],cppcmd[3],cppcmd[4],
  95.             cppcmd[5],cppcmd[6],cppcmd[7],cppcmd[8],cppcmd[9]);
  96.         if (spawnlp(P_WAIT,"cpp","cpp","-P-",
  97.             cppcmd[0],cppcmd[1],cppcmd[2],cppcmd[3],cppcmd[4],
  98.             cppcmd[5],cppcmd[6],cppcmd[7],cppcmd[8],cppcmd[9],
  99.             cmd,Options.inputname,NULL)!=0)
  100.             RLerror(RL_PANIC, "Couldn't run CPP!\n","","","");
  101.         strcpy(cmd,cmd+2);
  102.         yyin = fopen(cmd,"r");
  103.         if (yyin == (FILE *)NULL)
  104.             RLerror(RL_PANIC, "Couldn't open output from CPP!\n","","","");
  105. #else
  106.         yyin = popen(cmd, "r");
  107.         if (yyin == (FILE *)NULL)
  108.             RLerror(RL_PANIC, "popen of \"%s\" failed!\n", cmd,"","");
  109. #endif
  110.     } else {
  111. #endif
  112.     if (Options.inputname == (char *)NULL) {
  113.         yyin = stdin;
  114.         (void)strcpy(yyfilename, "stdin");
  115.     } else {
  116.         (void)strcpy(yyfilename, Options.inputname);
  117.         yyin = fopen(Options.inputname, "r");
  118.         if (yyin == (FILE *)NULL)
  119.             RLerror(RL_PANIC,
  120.                 "Cannot open %s.\n",Options.inputname,"","");
  121.     }
  122. #if defined(CPPSTDIN) && defined(POPEN)
  123.     }
  124. #endif
  125.     /*
  126.      * Initialize symbol table.
  127.      */
  128.     SymtabInit();
  129.     (void)yyparse();
  130. #if defined(CPPSTDIN) && defined(POPEN)
  131. #ifdef __WATCOMC__
  132.     fclose(yyin);
  133.     unlink(cmd);
  134. #endif
  135. #endif
  136. }
  137.  
  138. void
  139. OpenStatsFile()
  140. {
  141.     if (Options.statsname == (char *)NULL || Stats.fstats != stderr)
  142.         return;        /* Not specified or already opened. */
  143.  
  144.     Stats.fstats = fopen(Options.statsname, "w");
  145.     if (Stats.fstats == (FILE *)NULL) {
  146.         RLerror(RL_PANIC,
  147.             "Cannot open stats file %s.\n", Options.statsname,"","");
  148.     }
  149. }
  150.  
  151. #if defined(__WATCOMC__) || defined(__BORLANDC__)
  152. void RLerror(int level, char *pat, char *arg1, char *arg2, char *arg3)
  153. #else
  154. void
  155. RLerror(level, pat, arg1, arg2, arg3)
  156. int level;
  157. char *pat, *arg1, *arg2, *arg3;
  158. #endif
  159. {
  160.     switch (level) {
  161.         case RL_ADVISE:
  162.             if (!Options.quiet)
  163.                 RSmessage("Warning", pat, arg1, arg2, arg3);
  164.             break;
  165.         case RL_WARN:
  166.             RSmessage("Warning", pat, arg1, arg2, arg3);
  167.             break;
  168.         case RL_ABORT:
  169.             RSmessage("Error", pat, arg1, arg2, arg3);
  170.             exit(1);
  171.             break;
  172.         case RL_PANIC:
  173.             RSmessage("Fatal error", pat, arg1, arg2, arg3);
  174.             exit(2);
  175.             break;
  176.         default:
  177.             RSmessage("Unknown error", pat, arg1, arg2, arg3);
  178.             exit(3);
  179.     }
  180. }
  181.  
  182. static void
  183. RSmessage(type, pat, arg1, arg2, arg3)
  184. char *type, *pat, *arg1, *arg2, *arg3;
  185. {
  186.     extern FILE *yyin;
  187.     extern int yylineno;
  188.     extern char yyfilename[];
  189.  
  190.     if (yyin) {
  191.         /*
  192.          * cleanup() hasn't nulled yyin, so line #
  193.          * info is valid.
  194.          */
  195.         fprintf(stderr,"%s: %s: %s, line %d: ",
  196.             Options.progname, type,
  197.             yyfilename == (char *)NULL ? "stdin" :
  198.                 yyfilename, yylineno);
  199.     } else {
  200.         fprintf(stderr,"%s: %s: ", Options.progname, type);
  201.     }
  202.     fprintf(stderr, pat, arg1, arg2, arg3);
  203. }
  204.         
  205. #ifdef RUSAGE
  206. void
  207. RSGetCpuTime(usertime, systime)
  208. Float *usertime, *systime;
  209. {
  210.     struct rusage usage;
  211.  
  212.     getrusage(RUSAGE_SELF, &usage);
  213.  
  214.     *usertime = (Float)usage.ru_utime.tv_sec +
  215.             (Float)usage.ru_utime.tv_usec / 1000000.;
  216.     *systime = (Float)usage.ru_stime.tv_sec +
  217.             (Float)usage.ru_stime.tv_usec / 1000000.;
  218. }
  219.  
  220. #else
  221. #ifdef TIMES
  222.  
  223. #if defined(__BORLANDC__) || defined(__WATCOMC__)
  224.  
  225. extern time_t starttime;
  226.  
  227. void
  228. RSGetCpuTime(usertime, systime)
  229. Float *usertime, *systime;
  230. {
  231.     extern CLOCKTYPE times();
  232.     time_t timeval;
  233.  
  234.     timeval=time(&timeval);
  235.     *usertime = (Float)0;
  236.     *systime = (Float)timeval-(Float)starttime;
  237. }
  238. #else
  239. void
  240. RSGetCpuTime(usertime, systime)
  241. Float *usertime, *systime;
  242. {
  243.     extern CLOCKTYPE times();
  244.     struct tms time;
  245.  
  246.     (void)times(&time);
  247.     *usertime = (Float)time.tms_utime / (Float)HZ;
  248.     *systime = (Float)time.tms_stime / (Float)HZ;
  249. }
  250. #endif
  251.  
  252. #else /* !RUSAGE && !TIMES */
  253.  
  254. void
  255. RSGetCpuTime(usertime, systime)
  256. Float *usertime, *systime;
  257. {
  258.     *usertime = *systime = 0.;
  259. }
  260.  
  261. #endif /* TIMES */
  262. #endif /* RUSAGE */
  263.