home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / FUNNEL_S / SOURCES / MACHIN.C < prev    next >
C/C++ Source or Header  |  1992-05-27  |  12KB  |  346 lines

  1. /*##############################################################################
  2.  
  3. FUNNNELWEB COPYRIGHT
  4. ====================
  5. FunnelWeb is a literate-programming macro preprocessor.
  6.  
  7. Copyright (C) 1992 Ross N. Williams.
  8.  
  9.    Ross N. Williams
  10.    ross@spam.adelaide.edu.au
  11.    16 Lerwick Avenue, Hazelwood Park 5066, Australia.
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of Version 2 of the GNU General Public License as
  15. published by the Free Software Foundation.
  16.  
  17. This program is distributed WITHOUT ANY WARRANTY; without even the implied
  18. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. See Version 2 of the GNU General Public License for more details.
  20.  
  21. You should have received a copy of Version 2 of the GNU General Public
  22. License along with this program. If not, you can FTP the license from
  23. prep.ai.mit.edu/pub/gnu/COPYING-2 or write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26. Section 2a of the license requires that all changes to this file be
  27. recorded prominently in this file. Please record all changes here.
  28.  
  29. Programmers:
  30.    RNW  Ross N. Williams  ross@spam.adelaide.edu.au
  31.  
  32. Changes:
  33.    07-May-1992  RNW  Program prepared for release under GNU GPL V2.
  34.  
  35. ##############################################################################*/
  36.  
  37.  
  38. /******************************************************************************/
  39. /*                                    MACHIN.C                                */
  40. /******************************************************************************/
  41. /*                                                                            */
  42. /* WARNING: DO NOT ADD ANY PROGRAM DEPENDENT DEFINITIONS.                     */
  43. /*                                                                            */
  44. /* This file contains machine-dependent, program-independent stuff that       */
  45. /* implements the functionality promised in MACHIN.H.                         */
  46. /*                                                                            */
  47. /* Note: As usual, the full definitions of functions in the .H file are not   */
  48. /*       repeated here.                                                       */
  49. /*                                                                            */
  50. /******************************************************************************/
  51.  
  52. #include <time.h>
  53. #include "style.h"
  54.  
  55. #include "as.h"
  56. #include "machin.h"
  57.  
  58. /* The Macintosh's doesn't have a command language, and so we have to prompt  */
  59. /* for a command line. This requires a special package.                       */
  60. #if MAC
  61. #include <console.h>
  62. #endif
  63.  
  64. /* Under VMS, we need RMS to get at stuff to do with filenames.               */
  65. #if VMS
  66. #include RMS
  67. #endif
  68.  
  69. /******************************************************************************/
  70. /*                           LOCAL FUNCTIONS                                  */
  71. /******************************************************************************/
  72.  
  73. LOCAL void mconcat P_((int,char **,char *));
  74. LOCAL void mconcat(argc,argv,result)
  75. /* Given argc and argv, as passed to main(), this function appends all the    */
  76. /* arguments together (separated by spaces) into the string "result".         */
  77. /* This function assists in reconstructing the command line.                  */
  78. /* This function is actually portable, so you shouldn't have to change it.    */
  79. /* Note: "mconcat" stands for Multiple CONCATenation.                         */
  80. int    argc;
  81. char **argv;
  82. char *result;
  83. {
  84.  uword i;
  85.  strcpy(&result[0],argv[0]);
  86.  for (i=1;i<argc;i++)
  87.    {
  88.     strcat(&result[0]," "    );
  89.     strcat(&result[0],argv[i]);
  90.    }
  91. }
  92.  
  93. /******************************************************************************/
  94.  
  95. #if MAC | SUN | PC
  96. LOCAL void fn_split P_((p_fn_t,p_fn_t,p_fn_t,p_fn_t));
  97. LOCAL void fn_split(p_fn,p_path,p_name,p_ext)
  98. /* Accepts a filename in p_fn and writes the path, name, and extension        */
  99. /* components of the filename to the strings p_path, p_name, and p_ext.       */
  100. p_fn_t p_fn;
  101. p_fn_t p_path;
  102. p_fn_t p_name;
  103. p_fn_t p_ext;
  104. {
  105.  char *p,*q;
  106.  
  107.  /* Make sure that the input filename is a sensible length. */
  108.  as_cold(strlen(p_fn)<=FILENAME_MAX,"fn_split: Filename is too long.");
  109.  
  110.  /* Extract the path from the input string by searching for the last */
  111.  /* occurrance of the special character used for directories.        */
  112.  
  113.  /* Find the last directory/filename separator. */
  114.  p=strrchr(p_fn,FN_DELIM);
  115.  if (p==NULL)
  116.    {strcpy(p_path,""); p=p_fn;}
  117.  else
  118.    {
  119.     memcpy(p_path,p_fn,(size_t) (p-p_fn+1));
  120.     p_path[(size_t) (p-p_fn+1)]=EOS;
  121.     p++;
  122.    }
  123.  
  124.  /* Assert: p now points to the first character of the name field. */
  125.  
  126.  /* Now locate the file extension. */
  127.  q=strrchr(p_fn,'.');
  128.  if (q==NULL)
  129.    {strcpy(p_ext,""); q = &p_fn[strlen(p_fn)];}
  130.  else
  131.    {strcpy(p_ext,q);}
  132.  
  133.  /* Assert: q now points to the '.' preceding the extension field. */
  134.  
  135.  /* Now anything between p and q must be the filename proper. */
  136.  for (;p<q;p++)
  137.     *p_name++ = *p;
  138.  *p_name=EOS;
  139. }
  140. #endif
  141.  
  142. /******************************************************************************/
  143.  
  144. #if MAC | SUN | PC
  145. LOCAL void fn_join P_((p_fn_t,p_fn_t,p_fn_t,p_fn_t));
  146. LOCAL void fn_join(p_fn,p_path,p_name,p_ext)
  147. /* This function sets p_fn to the empty string and then successively appends  */
  148. /* p_path, p_name, and p_ext. Before doing this, it checks to make sure that  */
  149. /* there will be enough room and bombs the program if there isn't.            */
  150. p_fn_t p_fn;
  151. p_fn_t p_path;
  152. p_fn_t p_name;
  153. p_fn_t p_ext;
  154. {
  155.  as_cold(strlen(p_path)+strlen(p_name)+strlen(p_ext) <= FILENAME_MAX,
  156.          "fn_join: Constructed filename is too long.");
  157.  strcpy(p_fn,p_path);
  158.  strcat(p_fn,p_name);
  159.  strcat(p_fn,p_ext);
  160. }
  161. #endif
  162.  
  163. /******************************************************************************/
  164. /*                              EXPORTED FUNCTIONS                            */
  165. /******************************************************************************/
  166.  
  167. #if MAC | SUN | PC
  168. EXPORT void fn_ins(p_cur,p_add)
  169. p_fn_t p_cur;
  170. p_fn_t p_add;
  171. {
  172.  fn_t cur_path, add_path;
  173.  fn_t cur_name, add_name;
  174.  fn_t cur_extn, add_extn;
  175.  
  176.  /* Split the argument file names into their component parts. */
  177.  fn_split(p_cur,cur_path,cur_name,cur_extn);
  178.  fn_split(p_add,add_path,add_name,add_extn);
  179.  
  180.  /* Perform the inheriting on a field by field basis. */
  181.  if (strlen(add_path) > 0) strcpy(cur_path,add_path);
  182.  if (strlen(add_name) > 0) strcpy(cur_name,add_name);
  183.  if (strlen(add_extn) > 0) strcpy(cur_extn,add_extn);
  184.  
  185.  /* Put the fields back together again to yield the final result. */
  186.  fn_join(p_cur,cur_path,cur_name,cur_extn);
  187. }
  188. #endif
  189.  
  190. #if VMS
  191. EXPORT void fn_ins(p_cur,p_add)
  192. /* This VMS version of fn_ins was written with the assistance of:             */
  193. /*    Jeremy Begg (jeremy@vsm.com.au) of VSM Software Services. Thanks!       */
  194. /* The VMS version is messy because VMS has highly structured filenames and   */
  195. /* highly ingrained rituals for manipulating them.                            */
  196. p_fn_t p_cur;
  197. p_fn_t p_add;
  198. {
  199.  struct FAB cur_fab;   /* FAB    for SYS$PARSE */
  200.  struct NAM cur_nam;   /* NAM    for SYS$PARSE */
  201.  fn_t expanded;        /* Result of  SYS$PARSE */
  202.  long rms_status;      /* Status of  SYS$PARSE */
  203.  
  204.  /*
  205.  printf("\nTRACE: Call of fn_ins(...).\n");
  206.  printf("   Current spec = \"%s\".\n",p_cur);
  207.  printf("   Add     spec = \"%s\".\n",p_add);
  208.  */
  209.  
  210.  /* Initialize the FAB and NAM block to something sensible. */
  211.  cur_fab = cc$rms_fab;
  212.  cur_nam = cc$rms_nam;
  213.  
  214.  /* (fna,fns) is address and size of input file spec in FAB block. */
  215.  cur_fab.fab$l_fna = p_add;
  216.  cur_fab.fab$b_fns = strlen(p_add);
  217.  
  218.  /* (dna,dns) is address and size of default file spec in FAB block. */
  219.  cur_fab.fab$l_dna = p_cur;
  220.  cur_fab.fab$b_dns = strlen(p_cur);
  221.  
  222.  /* Connect the NAM block to the FAB block. */
  223.  cur_fab.fab$l_nam = &cur_nam;
  224.  
  225.  /* Point the NAM block's target name fields to the target namechar array. */
  226.  cur_nam.nam$l_esa = &expanded;
  227.  cur_nam.nam$b_ess = FILENAME_MAX;  /* Reserve last char for NULL terminator */
  228.  
  229.  /* PWD => put the password from a DECnet Access Control String in the filespec
  230.   * SYNCHK => check syntax only, don't search for the file on disk
  231.   */
  232.  cur_nam.nam$b_nop = NAM$M_PWD + NAM$M_SYNCHK;
  233.  
  234.  /* Perform the parse. */
  235.  rms_status = sys$parse(&cur_fab);
  236.  if (rms_status & 1)
  237.    {
  238.     expanded[cur_nam.nam$b_esl]=EOS; /* Terminate VMS string. */
  239.     strcpy(p_cur,expanded);
  240.    }
  241.  else
  242.    {
  243.     printf("Note: RMS parse failed. Could be a syntax error, could be a bug!\n");
  244.    }
  245.  
  246.  /* TRACE printf("   Result  spec = \"%s\".",p_cur); */
  247. }
  248. #endif
  249.  
  250. /******************************************************************************/
  251.  
  252. EXPORT void getcline(argc,argv,p_comline)
  253. /* Given, argc and argv, writes a command line string in p_comline.           */
  254. /* See machin.h for a thorough definition of this function.                   */
  255. int   argc;
  256. char **argv;
  257. char *p_comline;
  258. {
  259.  int    argc2;
  260.  char **argv2;
  261.  
  262. #if MAC
  263.  /* On the Macintosh there is no command language and therefore no command    */
  264.  /* line. Therefore we cannot trust the argc and argv handed to us by main()  */
  265.  /* and have to obtain a command line from other sources.                     */
  266.  /* The "ccommand" function comes from <console.h> of the THINK C libraries.  */
  267.  argc2=ccommand(&argv2);
  268. #endif
  269.  
  270. /* The other systems work like Unix. */
  271. #if SUN | VMS | PC
  272. argc2=argc;
  273. argv2=argv;
  274. #endif
  275.  
  276.  /* The command line is currently in pieces. Reassemble it. That's all!       */
  277.  mconcat(argc2,argv2,p_comline);
  278. }
  279.  
  280. /******************************************************************************/
  281.  
  282. EXPORT float tim_real()
  283. {
  284.  STAVAR bool   init=FALSE;
  285.  STAVAR time_t base;
  286.  
  287.  /* The first time this routine is called, we establish a base real time      */
  288.  /* from which real time differences are calculated. There are two reasons    */
  289.  /* for doing this. The first is that difftime seems to be the only way to    */
  290.  /* get ANSI C to hand over a calibrated arithmetic type holding the real     */
  291.  /* time. The second is that we don't want to have to deal with large         */
  292.  /* absolute times anyway.                                                    */
  293.  if (!init)
  294.    {
  295.     /* Returns -1 if the time is not available (ANSI 7.12.2.4). */
  296.     base=time(NULL);
  297.     init=TRUE;
  298.    }
  299.  
  300.  /* Return the elapsed time since the base time. */
  301.  
  302. /* Macintosh, VMS, and PC have difftime. */
  303. #if MAC | VMS | PC
  304.  if (base == -1)
  305.     return 0.0;
  306.  else
  307.     return (float) difftime(time(NULL),base);
  308. #endif
  309.  
  310. /* Sun does not have difftime. */
  311. #if SUN
  312.  return 0.0;
  313. #endif
  314.  
  315. /* The timing functions are only used to generate performance statistics and  */
  316. /* are not critical to FunnelWeb. There is not much harm in returning 0.0.    */
  317. }
  318.  
  319. /******************************************************************************/
  320.  
  321. EXPORT float tim_cpu()
  322. {
  323.  clock_t t=clock();
  324.  
  325. /* Make sure that we have a definition for CLOCKS_PER_SEC. */
  326. #ifndef CLOCKS_PER_SEC
  327. #ifdef CLK_TCK
  328. #define CLOCKS_PER_SEC CLK_TCK
  329. #else
  330. /* Assume one million ticks per second. */
  331. #define CLOCKS_PER_SEC (1000000L)
  332. #endif
  333. #endif
  334.  
  335.  /* The clock() function returns -1 if the CPU time is not available. */
  336.  /* Otherwise it returns the number of "clocks" since power-up.       */
  337.  if (t == -1)
  338.     return 0.0;
  339.  else
  340.     return ((float) t) / ((float) CLOCKS_PER_SEC);
  341. }
  342.  
  343. /******************************************************************************/
  344. /*                              End of MACHIN.C                               */
  345. /******************************************************************************/
  346.