home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / FUNNEL_S / OS2 / COMMAND.C next >
C/C++ Source or Header  |  1992-08-23  |  72KB  |  2,112 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. /*                                COMMAND.C                                   */
  40. /******************************************************************************/
  41.  
  42. #include <ctype.h>
  43. #include "style.h"
  44.  
  45. #include "analyse.h"
  46. #include "as.h"
  47. #include "command.h"
  48. #include "data.h"
  49. #include "dump.h"
  50. #include "lister.h"
  51. #include "memory.h"
  52. #include "mapper.h"
  53. #include "misc.h"
  54. #include "option.h"
  55. #include "parser.h"
  56. #include "scanner.h"
  57. #include "tangle.h"
  58. #include "weave.h"
  59.  
  60. /******************************************************************************/
  61.  
  62. /* Because FunnelWeb has so many options ,it is convenient to allow the user  */
  63. /* to construct a startup file. This is its name.                             */
  64. #define INITFILE "fwinit.fws"
  65.  
  66. /******************************************************************************/
  67.  
  68. /* Three variables hold options information in FunnelWeb.                     */
  69. /* 'p_comopt' holds the options conveyed by the original raw command line.      */
  70. /* 'p_defopt' holds the options that are default in the FunnelWeb shell.        */
  71. /* 'option' is global and holds the options that have been specified for the  */
  72. /*          current invocation of FunnelWeb proper.                           */
  73. /* 'option' is global so here we see only 'p_comopt' and 'p_defopt'.              */
  74. LOCVAR op_t *p_comopt;  /* Initial command line options.                         */
  75. LOCVAR op_t *p_defopt;  /* Default shell options.                                */
  76.  
  77. /* The FunnelWeb shell interpreter has many different commands and it is      */
  78. /* worth them sharing the same basic command line scanner. These two          */
  79. /* variables hold the numer of arguments and a pointer to strings holding     */
  80. /* copies of each argument.                                                   */
  81. /* Note: The first argument is placed in arr_arg[0].                          */
  82. #define FW_ARG_MAX 5
  83. LOCVAR uword arg_num;
  84. LOCVAR char *arg_arr[FW_ARG_MAX+1]; /* Is this +1 necessary? */
  85.  
  86. /* The FunnelWeb command interpreter allows 10 substitution strings.          */
  87. /* These strings are stored in the following array.                           */
  88. #define NUM_SUBS (10+26)        /* 0..9 and A..Z */
  89. LOCVAR char *subval[NUM_SUBS];
  90.  
  91. LOCVAR ulong old_war;
  92. LOCVAR ulong old_err;
  93. LOCVAR ulong old_sev;
  94.  
  95. /* The FunnelWeb script interpreter can echo (trace) each command before it   */
  96. /* executes it. Whether it does is determined by this variable.               */
  97. LOCVAR bool tracing;
  98.  
  99. /* If this variable is set to true then the interpreter will not abort if the */
  100. /* very next command (and that one only) generates an error or severe error.  */
  101. LOCVAR bool noabort;
  102.  
  103. /* If the following boolean is true, the interpreter skips lines until it     */
  104. /* hits the HERE command.                                                     */
  105. LOCVAR bool skipping;
  106.  
  107. /* The following variables count how many times DIFF is invoked and how many  */
  108. /* times it succeeded. This allows us to output a summary at the end of the   */
  109. /* processing of the test suite.                                              */
  110. LOCVAR ulong difftotl = 0;
  111. LOCVAR ulong diffsucc = 0;
  112.  
  113. /******************************************************************************/
  114.  
  115. /* Here are the prototypes for recursive or out-of-order functions.           */
  116. LOCAL void interstr P_((char *));
  117. LOCAL void do_set P_((char *));
  118.  
  119. /******************************************************************************/
  120.  
  121. LOCAL char *sing P_((ulong,char *,char *));
  122. LOCAL char *sing(num,sinstr,plustr)
  123. /* Return one or other string depending on whether first argument is singular.*/
  124. ulong num;
  125. char *sinstr;
  126. char *plustr;
  127. {
  128.  if (num==1)
  129.     return sinstr;
  130.  else
  131.     return plustr;
  132. }
  133.  
  134. /******************************************************************************/
  135.  
  136. LOCAL uword numpos P_((ulong,ulong,ulong));
  137. LOCAL uword numpos(a,b,c)
  138. /* Returns the number of its arguments that are positive. */
  139. ulong a,b,c;
  140. {
  141.  uword result=0;
  142.  if (a>0) result++;
  143.  if (b>0) result++;
  144.  if (c>0) result++;
  145.  return result;
  146. }
  147.  
  148. /******************************************************************************/
  149.  
  150. LOCAL void errsum P_((ulong,ulong,ulong,ulong));
  151. LOCAL void errsum(fat,sev,err,war)
  152. /* Supply as arguments, the number of various kinds of diagnostics.           */
  153. /* Places in linet1 a string describing the diagnostics.                      */
  154. ulong fat,sev,err,war;
  155. {
  156.  char linet2[100];
  157.  
  158.  if (fat+sev+err+war==0)
  159.     {strcpy(linet1,"SUCCESS: No diagnostics."); return;}
  160.  
  161.  strcpy(linet1,"There ");
  162.  
  163.  /* "Was" or "were" depending on the plurality of highest level diagnostic. */
  164.       if (fat>0) strcat(linet1,sing(fat,"was","were"));
  165.  else if (sev>0) strcat(linet1,sing(sev,"was","were"));
  166.  else if (err>0) strcat(linet1,sing(err,"was","were"));
  167.  else if (war>0) strcat(linet1,sing(war,"was","were"));
  168.  else as_bomb("errsum: Error hierarchy failed!");
  169.  
  170.  strcat(linet1," ");
  171.  
  172.  /* Fatal errors. */
  173.  if (fat>0)
  174.    {
  175.     sprintf(linet2,"%1lu Fatal error",fat); strcat(linet1,linet2);
  176.     strcat(linet1,sing(fat,"","s"));
  177.    }
  178.  
  179.  /* Joiner stuff. */
  180.  if (fat>0 && numpos(sev,err,war)>=2)    strcat(linet1,", ");
  181.  if (fat>0 && sev>0 && err==0 && war==0) strcat(linet1," and ");
  182.  
  183.  /* Severe errors. */
  184.  if (sev>0)
  185.    {
  186.     sprintf(linet2,"%1lu Severe error",sev); strcat(linet1,linet2);
  187.     strcat(linet1,sing(sev,"","s"));
  188.    }
  189.  
  190.  /* Joiner stuff. */
  191.  if (fat+sev>0 && err>0 && war >0) strcat(linet1,", ");
  192.  if (fat+sev>0 && err>0 && war==0) strcat(linet1," and ");
  193.  
  194.  /* Errors. */
  195.  if (err>0)
  196.    {
  197.     sprintf(linet2,"%1lu Error",err); strcat(linet1,linet2);
  198.     strcat(linet1,sing(err,"","s"));
  199.    }
  200.  
  201.  /* Joiner stuff. */
  202.  if (fat+sev+err>0 && war>0) strcat(linet1," and ");
  203.  
  204.  /* Warnings. */
  205.  if (war > 0)
  206.    {
  207.     sprintf(linet2,"%1lu Warning",war); strcat(linet1,linet2);
  208.     strcat(linet1,sing(war,"","s"));
  209.    }
  210.  
  211.  /* The final full stop! */
  212.  strcat(linet1,".");
  213. }
  214.  
  215. /******************************************************************************/
  216.  
  217. LOCAL void allocarg P_((void));
  218. LOCAL void allocarg()
  219. /* Some compilers don't allow much room for statics and so it is necessary to */
  220. /* declare some variables as pointers and allocate them explicitly. This      */
  221. /* function allocates the command line argument array to point to strings.    */
  222. {
  223.  uword i;
  224.  for (i=0;i<=FW_ARG_MAX;i++)
  225.     arg_arr[i]=(char *) mm_perm(sizeof(cl_t));
  226.  
  227.  /* Also initialize the substitution strings. */
  228.  for (i=0;i<NUM_SUBS;i++)
  229.    {
  230.     subval[i]=(char *) mm_perm(sizeof(cl_t));
  231.     subval[i][0]=EOS;
  232.    }
  233.  p_comopt=(p_op_t) mm_perm(sizeof(op_t));
  234.  p_defopt=(p_op_t) mm_perm(sizeof(op_t));
  235. }
  236.  
  237. /******************************************************************************/
  238.  
  239. LOCAL void zerdia P_((void));
  240. LOCAL void zerdia()
  241. {
  242.  old_war=num_war;
  243.  old_err=num_err;
  244.  old_sev=num_sev;
  245.  num_sev=num_err=num_war=0;
  246. }
  247.  
  248. LOCAL void sumdia P_((void));
  249. LOCAL void sumdia()
  250. {
  251.  sum_sev+=num_sev;
  252.  sum_err+=num_err;
  253.  sum_war+=num_war;
  254. }
  255.  
  256. LOCAL void restdia P_((void));
  257. LOCAL void restdia()
  258. {
  259.  num_sev+=old_sev;
  260.  num_err+=old_err;
  261.  num_war+=old_war;
  262. }
  263.  
  264. /******************************************************************************/
  265.  
  266. LOCAL void explode P_((char *));
  267. LOCAL void explode(p)
  268. /* Parses the string p into a set of non-blank sequences. Copies each         */
  269. /* distinct run of non-blanks into successive values of arg_arr. Places the   */
  270. /* number of runs of non-blanks into arg_num. DOESN'T generate an error if    */
  271. /* there are too many arguments.                                              */
  272. char *p;
  273. {
  274.  arg_num=0;
  275.  while (TRUE)
  276.    {
  277.     char *x;
  278.  
  279.     /* Skip to the next argument. */
  280.     while (*p==' ') p++;
  281.  
  282.     /* Exit if we have hit the end of the string. */
  283.     if (*p==EOS) break;
  284.  
  285.     /* Exit if we are already full up with arguments. */
  286.     if (arg_num==FW_ARG_MAX) break;
  287.  
  288.     /* Copy the next argument. */
  289.     x=arg_arr[arg_num];
  290.     while (*p!=' ' && *p!=EOS) *x++ = *p++;
  291.     *x=EOS;
  292.     arg_num++;
  293.    }
  294. }
  295.  
  296. /******************************************************************************/
  297.  
  298. LOCAL void dollsub P_((char *));
  299. LOCAL void dollsub(p_comlin)
  300. /* Assumes that it's string argument is in a command line object. */
  301. /* Performs dollar substitutions. */
  302. char *p_comlin;
  303. {
  304.  char *p=p_comlin;
  305.  cl_t  cl;
  306.  char *t;
  307.  
  308.  t = &cl[0];
  309.  
  310.  /* Copy the unexpanded command line to the temporary from which we expand it */
  311.  /* back from whence it came.                                                 */
  312.  strcpy(t,p);
  313.  
  314.  /* Work through the unexpanded command line expanding. */
  315.  while (*t != EOS)
  316.    {
  317.     /* Complain if there is not room for one more character. */
  318.     if ((p-p_comlin) == COMLINE_MAX) goto toobig;
  319.  
  320.     /* If no dollar sign, no tricks. Just copy the character over. */
  321.     if (*t!='$') {*p++ = *t++; continue;}
  322.  
  323.     /* We have seen a dollar sign. Move onto the next character. */
  324.     t++;
  325.  
  326.     /* The only legal escapes are decimal digits, slash, and the dollar sign. */
  327.     if (!isdigit(*t) && !isupper(*t) && *t!='$' && *t!='/')
  328.       {
  329.        wl_sj("S: Illegal dollar subtitution sequence in command line.");
  330.        wl_sj("Legal sequences are $0..$9, $A..$Z, $/, and $$.");
  331.        num_sev++;
  332.        strcpy(p,t);
  333.        return;
  334.       }
  335.  
  336.     /* A dollar escape is easy to process. */
  337.     if (*t=='$') {*p++ = *t++; continue;}
  338.  
  339.     /* A slash escape is easy to process. */
  340.     if (*t=='/') {*p++ = FN_DELIM; t++; continue;}
  341.  
  342.     /* Substitutions have to be expanded. */
  343.     if (isdigit(*t) || isupper(*t))
  344.       {
  345.        ubyte num;
  346.        char *q;
  347.        if (isdigit(*t))
  348.            num = *t-'0';
  349.        else
  350.            num = 10+(*t-'A');
  351.        as_cold(num<NUM_SUBS,"dollsub: num is too bug!");
  352.        q=subval[num];
  353.        if ((p-p_comlin)+strlen(subval[num]) > COMLINE_MAX) goto toobig;
  354.        while (*q!=EOS) *p++ = *q++;
  355.        t++;
  356.       }
  357.    }
  358.  *p=EOS;
  359.  return;
  360.  
  361.  toobig:
  362.     wl_sj("S: Expanded (i.e. after $1 etc) command line is too long.");
  363.     num_sev++;
  364.     strcpy(p,t);
  365.     return;
  366. }
  367.  
  368. /******************************************************************************/
  369.  
  370. LOCAL void fwonerun P_((void));
  371. LOCAL void fwonerun()
  372. /* Performs a single run of FunnelWeb proper, using the global variable       */
  373. /* 'options' as the input command line.                                       */
  374. {
  375.  fn_t lisnam;
  376.  
  377.  /* The following clocks record the time taken by various parts of FunnelWeb.  */
  378.  ck_t mappck;
  379.  ck_t scanck;
  380.  ck_t parsck;
  381.  ck_t analck;
  382.  ck_t dumpck;
  383.  ck_t lstrck;
  384.  ck_t tangck;
  385.  ck_t weavck;
  386.  ck_t totlck;
  387.  
  388.  /* Intialize/zero all the clocks. */
  389.  ck_ini(&mappck);
  390.  ck_ini(&scanck);
  391.  ck_ini(&parsck);
  392.  ck_ini(&analck);
  393.  ck_ini(&dumpck);
  394.  ck_ini(&lstrck);
  395.  ck_ini(&tangck);
  396.  ck_ini(&weavck);
  397.  ck_ini(&totlck);
  398.  
  399.  /* Start the total time clock ticking. A total time clock is used to gather  */
  400.  /* up the gaps between the invocations of the other clocks.                  */
  401.  ck_start(&totlck);
  402.  
  403.  ck_start(&lstrck);
  404.  
  405.  /* Establish the listing file output stream. */
  406.  strcpy(lisnam,"");             /* Start with an empty string.                */
  407.  fn_ins(lisnam,option.op_f_s);  /* Insert input file name.                    */
  408.  fn_ins(lisnam,".lis");         /* Insert constant extension.                 */
  409.  fn_ins(lisnam,option.op_l_s);  /* Insert command line spec.                  */
  410.  wf_ini(&f_l,option.op_l_b);    /* Initialize the stream.                     */
  411.  wf_ope(&f_l,lisnam);           /* Create the file.                           */
  412.  if (option.op_l_b && wf_err(&f_l))
  413.    {
  414.     sprintf(linet1,"S: Error creating listing file \"%s\".",lisnam);
  415.     wl_sj(linet1);
  416.     wl_sj("Aborting...");
  417.     num_sev++;
  418.     return;
  419.    }
  420.  
  421.  wl_l("FUNNELWEB LISTING FILE");
  422.  wl_l("======================");
  423.  wl_l("");
  424.  
  425.  /* Initialize the lister for this run. */
  426.  lr_ini();
  427.  
  428.  ck_stop(&lstrck);
  429.  
  430.  /* Scanner comes first. */
  431.  ck_start(&scanck);
  432.  scanner(&mappck,&scanck);
  433.  ck_stop(&scanck);
  434.  
  435.  /* Dump the line and token lists if requested. The scanner supplies sensible */
  436.  /* lists even if it encounters errors, so there is no danger here.           */
  437.  ck_start(&dumpck);
  438.  if (option.op_b2_b) dm_lnls(&f_l);
  439.  if (option.op_b3_b) dm_tkls(&f_l);
  440.  ck_stop(&dumpck);
  441.  
  442.  /* Invoke the parser if there were no serious scanner errors. */
  443.  if (num_sev+num_err==0)
  444.    {
  445.     ck_start(&parsck);
  446.     parser();
  447.     ck_stop(&parsck);
  448.     /* Only perform post parser dumps if the parser was run. */
  449.     ck_start(&dumpck);
  450.     if (option.op_b4_b) dm_matb(&f_l);
  451.     if (option.op_b5_b) dm_dcls(&f_l);
  452.     ck_stop(&dumpck);
  453.    }
  454.  else
  455.    {
  456.     if (option.op_b4_b)
  457.        wl_l("Macro table dump skipped (Parser was not invoked).");
  458.     if (option.op_b5_b)
  459.        wl_l("Document list dump skipped (Parser was not invoked).");
  460.    }
  461.  
  462.  /* Invoke the macro structure analyser if still no errors. */
  463.  if (num_sev+num_err==0)
  464.    {
  465.     ck_start(&analck);
  466.     analyse();
  467.     ck_stop(&analck);
  468.    }
  469.  
  470.  /* The scanner, parser, and analyser send errors to the lister package. */
  471.  /* Send sorted listing to the listing file (and screen if desired).     */
  472.  ck_start(&lstrck);
  473.  if (option.op_l_b) lr_gen(&f_l,option.op_c_i);
  474.  if (option.op_s_b) lr_gen(&f_s,option.op_s_i);
  475.  if (option.op_s_b) lr_gen(&f_j,option.op_s_i);
  476.  ck_stop(&lstrck);
  477.  
  478.  /* If the first stages went OK, invoke tangle and weave. */
  479.  if (num_sev+num_err==0)
  480.    {
  481.     if (option.op_o_b)
  482.       {
  483.        ck_start(&tangck);
  484.        tangle();
  485.        ck_stop(&tangck);
  486.       }
  487.     if (option.op_t_b)
  488.       {
  489.        ck_start(&weavck);
  490.        weave();
  491.        ck_stop(&weavck);
  492.       }
  493.     /* Leave output lines from Tangle and Weave joined, but separate them */
  494.     /* from any further output.                                           */
  495.     if (option.op_t_b || option.op_o_b)
  496.        wl_sjl("");
  497.    }
  498.  else
  499.    {
  500.     /* Otherwise tell the user that back-end phases will be skipped. */
  501.     if ( option.op_o_b ||  option.op_t_b)
  502.       {
  503.        if (num_sev+num_err==1)
  504.           wr_sjl("Error caused ");
  505.        else
  506.           wr_sjl("Errors caused ");
  507.       }
  508.     if ( option.op_o_b &&  option.op_t_b) wr_sjl("tangle and weave phases");
  509.     if ( option.op_o_b && !option.op_t_b) wr_sjl("tangle phase");
  510.     if (!option.op_o_b &&  option.op_t_b) wr_sjl("weave phase");
  511.     if ( option.op_o_b ||  option.op_t_b)
  512.        {wl_sjl(" to be skipped."); wl_sjl("");}
  513.    }
  514.  
  515.  ck_stop(&totlck);
  516.  
  517.  /* If requested write out a summary of the time taken. */
  518.  if (option.op_b6_b)
  519.     dm_times(&f_l,
  520.              &mappck,&scanck,&parsck,&analck,
  521.              &dumpck,&lstrck,&tangck,&weavck,&totlck);
  522.  
  523.  /* Write out a line summarizing the diagnostics for this run. */
  524.  errsum(0L,num_sev,num_err,num_war); wl_sjl(linet1);
  525.  
  526.  /* Close the listing file. */
  527.  if (!option.op_l_b) goto finishoff;
  528.  if (wf_err(&f_l))
  529.    {
  530.     wl_sj("S: Error writing to listing file. Aborting...");
  531.     num_sev++;
  532.     goto finishoff;
  533.    }
  534.  wf_clo(&f_l);
  535.  if (wf_err(&f_l))
  536.    {
  537.     wl_sj("S: Error flushing and closing listing file. Aborting...");
  538.     num_sev++;
  539.    }
  540.  
  541.  finishoff:
  542.  /* VERY IMPORTANT: Ask the memory management package to free up all the      */
  543.  /* temporary memory (allocated using mm_temp) that has been allocated. This  */
  544.  /* ensures that the memory allocated for this FunnelWeb run will be recycled.*/
  545.  mm_zapt();
  546.  
  547. } /* End of fwonerun */
  548.  
  549. /******************************************************************************/
  550.  
  551. LOCAL void do_absen P_((void));
  552. LOCAL void do_absen ()
  553. {
  554.  if (arg_num != 2)
  555.    {
  556.     wl_sj("S: The ABSENT command requires exactly one argument.");
  557.     num_sev++;
  558.     return;
  559.    }
  560.  if (fexists(arg_arr[1]))
  561.    {
  562.     sprintf(linet1,"S: ABSENT found \"%s\".",arg_arr[1]);
  563.     wl_sj(linet1);
  564.     num_sev++;
  565.     return;
  566.    }
  567. }
  568.  
  569. /******************************************************************************/
  570.  
  571. LOCAL void do_cody P_((void));
  572. LOCAL void do_cody ()
  573. /* The CODIFY command takes an input text file and generates an output text   */
  574. /* file containing C code to write out the input text file. The need for this */
  575. /* command springs from the weaver. Experience with FunnelWeb showed that use */
  576. /* of a separate header file, to be included, while apparently sensible,      */
  577. /* caused no end of problems. In particular, problems with portably           */
  578. /* specifying where the header file should be found. In the end, it was       */
  579. /* decided that it would be better to write the header file out with the      */
  580. /* weave output. As the header file is quite long, it is best to automate the */
  581. /* process of converting the file from text to C code to write out the text.  */
  582. /* That is what the CODIFY command does.                                      */
  583. {
  584.  FILE *file1;
  585.  FILE *file2;
  586.  
  587. #define MAXHACK 1000
  588.  char hackline[MAXHACK+1];
  589.  uword lineno;
  590.  
  591.  if (arg_num != 3)
  592.    {
  593.     wl_sj("S: The CODIFY command requires exactly two arguments.");
  594.     num_sev++;
  595.     return;
  596.    }
  597.  
  598.  /* Open the input file for text reading. */
  599.  file1=fopen(arg_arr[1],"r");
  600.  if (file1 == FOPEN_F)
  601.    {
  602.     wl_sj("S: Error opening the input file.");
  603.     num_sev++;
  604.     return;
  605.    }
  606.  file2=fopen(arg_arr[2],"w");
  607.  if (file2 == FOPEN_F)
  608.    {
  609.     fclose(file1);
  610.     wl_sj("S: Error creating the output file.");
  611.     num_sev++;
  612.     return;
  613.    }
  614.  
  615.  lineno=0;
  616.  
  617.  /* PROCESS A SINGLE LINE PER ITERATION. */
  618.  while (TRUE)
  619.    {
  620.     uword linelength;
  621.     uword i;
  622.  
  623.     /* Read in a line of input and terminate loop if there are no more lines. */
  624.     fgets(hackline,MAXHACK,file1);
  625.     if (ferror(file1))
  626.        {wl_sj("S: Error reading the input file.");num_sev++;return;}
  627.     if (feof(file1)) break;
  628.     lineno++;
  629.  
  630.     /* Complain if the input line is too long. */
  631.     if (strlen(hackline)>81)
  632.       {
  633.        sprintf(linet1,"Line %lu of input file is too long.",
  634.                (ulong) strlen(hackline));
  635.        wl_sj(linet1);
  636.        wl_sj("The maximum allowable length is 80 characters.");
  637.        num_sev++;
  638.        return;
  639.       }
  640.  
  641.     /* Write the start-of-line string. */
  642.     if (fputs(" WX(\"",file2) == FPUTS_F) goto write_failure;
  643.  
  644.     /* Write out the line in sanitized form. */
  645.     linelength=strlen(hackline);
  646.     for (i=0;i<linelength;i++)
  647.       {
  648.        char ch = hackline[i];
  649.        if (ch==EOL)
  650.           ; /* Ignore this. */
  651.        else
  652.        if (ch=='\"')
  653.           {if (fputs("\\\"",file2) == FPUTS_F) goto write_failure;}
  654.        else
  655.        if (ch=='\\')
  656.           {if (fputs("\\\\",file2) == FPUTS_F) goto write_failure;}
  657.        else
  658.           {if (fputc(ch,file2) == FPUTC_F) goto write_failure;}
  659.       }
  660.  
  661.     /* Write the end of line string. */
  662.     if (fputs("\");\n",file2) == FPUTS_F) goto write_failure;
  663.    }
  664.  
  665.  if (fflush(file2) != FFLUSH_S)
  666.     {wl_sj("S: Error flushing the output file.");num_sev++;return;}
  667.  if (fclose(file1) == FCLOSE_F)
  668.    {wl_sj("S: Error closing the input file.");num_sev++;return;}
  669.  if (fclose(file2) == FCLOSE_F)
  670.    {wl_sj("S: Error closing the output file.");num_sev++;return;}
  671.  return;
  672.  
  673.  write_failure:
  674.     wl_sj("S: Error writing the output file.");num_sev++;return;
  675. }
  676.  
  677. /******************************************************************************/
  678.  
  679. LOCAL void do_comp P_((void));
  680. LOCAL void do_comp ()
  681. /* The compare command should have two arguments being file names. It         */
  682. /* compares the two files and raises a fatal error if they differ.            */
  683. {
  684.  char *errstr;
  685.  bool result;
  686.  if (arg_num != 3)
  687.    {
  688.     wl_sj("S: COMPARE command must be given exactly two arguments.");
  689.     num_sev++;
  690.     return;
  691.    }
  692.  errstr=eq_files(arg_arr[1],arg_arr[2],&result);
  693.  if (errstr!=NULL)
  694.    {
  695.     wl_sj("S: COMPARE command ran into a problem:");
  696.     wl_sj(errstr);
  697.     num_sev++;
  698.     return;
  699.    }
  700.  if (!result)
  701.    {
  702.     wl_sj("S: A comparison FAILED. Two files compared were not identical!");
  703.     sprintf(linet1,"   File1: \"%s\".",arg_arr[1]); wl_sj(linet1);
  704.     sprintf(linet1,"   File2: \"%s\".",arg_arr[2]); wl_sj(linet1);
  705.     wl_sj("   FunnelWeb has just FAILED a regression test.");
  706.     wl_sj("   You should now inspect the two files to see how the result of");
  707.     wl_sj("   this run of FunnelWeb differed from the \"correct answer\" in");
  708.     wl_sj("   the answer directory.");
  709.     num_sev++;
  710.     return;
  711.    }
  712. }
  713.  
  714. /******************************************************************************/
  715.  
  716. LOCAL void do_defin P_((char *));
  717. LOCAL void do_defin (p_comlin)
  718. /* The define command associates a string with a $n. */
  719. char *p_comlin;
  720. {
  721.  char *p;
  722.  ubyte defnum;
  723.  
  724.  /* p is our scanning pointer and starts at the start of the command line. */
  725.  p=p_comlin;
  726.  
  727.  /* Skip past the DEFINE command onto the second argument. */
  728.  while ((*p!=' ') && (*p!=EOS)) p++;
  729.  while (*p==' ') p++;
  730.  
  731.  /* There should be a single digit or upper case letter there. */
  732.  if (!isdigit(*p) && !isupper(*p))
  733.    {wl_sj("S: The first argument to DEFINE must be a decimal digit or");
  734.     wl_sj("   upper case letter.");
  735.     wl_sj("   Example: define 3 \"A Walrus in Spain is a Walrus in Vain.\"");
  736.     num_sev++;return;}
  737.  if (isdigit(*p))
  738.     defnum = *p-'0';
  739.  else
  740.     defnum = 10+(*p-'A');
  741.  as_cold(defnum<NUM_SUBS,"do_defin: num is too bug!");
  742.  
  743.  /* Move past the digit. */
  744.  p++;
  745.  
  746.  /* Skip blanks to get to the next argument. */
  747.  while (*p==' ') p++;
  748.  
  749.  /* Complain if there is no second argument. */
  750.  if (*p==EOS)
  751.    {wl_sj("S: The DEFINE command expected a second argument.");
  752.     num_sev++; return;}
  753.  
  754.  /* Otherwise make sure that we have a double quoted string. */
  755.  if (*p!='"' || *(p+1)==EOS || p[strlen(p)-1]!='"')
  756.    {wl_sj("S: Second argument to DEFINE must be in double quotes.");
  757.     num_sev++;return;}
  758.  
  759.  /* All is checked. Now it is safe to copy over the string. */
  760.  p++;
  761.  strcpy(subval[defnum],p);
  762.  subval[defnum][strlen(subval[defnum])-1]=EOS;
  763.  
  764.  /* TRACE: All the definitions.
  765.  {
  766.   ubyte i;
  767.   for (i=0;i<NUM_SUB;i++)
  768.      printf("$%u=\"%s\"\n",(unsigned) i,subval[i]);
  769.  }
  770.  */
  771. }
  772.  
  773. /******************************************************************************/
  774.  
  775. LOCAL void do_diff P_((void));
  776. /* COMMAND FORMAT: diff file1 file2 logfile [abort]                           */
  777. /* This function/command performs a proper text differences on two files.     */
  778. /* This function is long and messy because of C's lack of nested functions.   */
  779. /* I don't want to create global variables here, and defining subfunctions    */
  780. /* that do not have access to the variables of this function would provoke    */
  781. /* too wide an interface to be worth the trouble. So it's code city.          */
  782. /* How I wish that I had FunnelWeb to help me write this function!            */
  783. LOCAL void do_diff()
  784. {
  785.  bool   diffabort;        /* TRUE iff we should abort if files are different. */
  786.  bool   is_same  = FALSE; /* True iff files are proven to be the same.        */
  787.  FILE  *logfile;          /* File to write result of comparison.              */
  788.  bool   badwrite = FALSE; /* TRUE if we couldn't write to the logfile.        */
  789.  char  *p_file1;          /* Pointer to mapping of first  file to compare.    */
  790.  char  *p_file2;          /* Pointer to mapping of second file to compare.    */
  791.  ulong  len_file1;        /* Number of bytes in mapping of first  file.       */
  792.  ulong  len_file2;        /* Number of bytes in mapping of second file.       */
  793.  char  *mess1;            /* Error message from mapper for first  file.       */
  794.  char  *mess2;            /* Error message from mapper for second file.       */
  795.  bool  is_image;          /* TRUE iff mapped images are identical.            */
  796.  bool   anydiff = FALSE;  /* TRUE iff any differences detected during loop.   */
  797.  
  798.  /* Check that the number of arguments is correct. */
  799.  if (arg_num < 4 || arg_num > 5)
  800.    {
  801.     wl_sj("S: The DIFF command must be given either 3 or 4 arguments.");
  802.     wl_sj("   Usage: diff f1 f2 logfile [abort]");
  803.     num_sev++;
  804.     return;
  805.    }
  806.  
  807.  /* Check that the fourth argument, if present, is legal. */
  808.  diffabort=FALSE;
  809.  if (arg_num == 5)
  810.     if (strcmp(arg_arr[4],"ABORT")==0 || strcmp(arg_arr[4],"abort")==0)
  811.        diffabort=TRUE;
  812.     else
  813.       {
  814.        wl_sj(
  815.           "S: The DIFF command's fourth argument, if present, must be ABORT.");
  816.        wl_sj("   Usage: diff f1 f2 logfile [abort]");
  817.        num_sev++;
  818.        return;
  819.       }
  820.  
  821.  /* Now open the log file to append result of compare. */
  822.  logfile=fopen(arg_arr[3],"a");
  823.  if (logfile == FOPEN_F)
  824.    {
  825.     wl_sj("S: DIFF: Error opening the log file (to append result of compare).");
  826.     num_sev++;
  827.     return;
  828.    }
  829.  
  830.  /* The following define simplifies writing to the log file. */
  831. #define LOGLINE {if (fputs(linet1,logfile) == FPUTS_F) badwrite=TRUE;}
  832. #define LOGSTR(STR) {if (fputs((STR),logfile) == FPUTS_F) badwrite=TRUE;}
  833. #define LOGCHAR(CH) {if (fputc((CH),logfile) == FPUTC_F) badwrite=TRUE;}
  834.  
  835.  /* Write the header for this comparison to the log file. */
  836.  sprintf(linet1,"\n\n"                          ); LOGLINE;
  837.  sprintf(linet1,"Comparing \"%s\"\n" ,arg_arr[1]); LOGLINE;
  838.  sprintf(linet1,"     with \"%s\".\n",arg_arr[2]); LOGLINE;
  839.  
  840.  /* Now map in the two files to be compared.                                  */
  841.  /* Once this is done, we MUST do a mm_zapt later or memory will leak.        */
  842.  /* We attempt to map the second file, even if the first mapping has failed   */
  843.  /* as, if the first file is absent, there is a good chance that the second   */
  844.  /* is absent too, and it is useful to the user to know this.                 */
  845.  mess1=map_file(arg_arr[1],&p_file1,&len_file1);
  846.  mess2=map_file(arg_arr[2],&p_file2,&len_file2);
  847.  if (mess1 != NULL)
  848.    {
  849.     sprintf(linet1,"Error mapping \"%s\".\n",arg_arr[1]); LOGLINE;
  850.     wr_sj("E: DIFF: "); wr_sj(linet1);
  851.     sprintf(linet1,"         %s\n",mess1); LOGLINE; wr_sj(linet1);
  852.     num_err++;
  853.    }
  854.  if (mess2 != NULL)
  855.    {
  856.     sprintf(linet1,"Error mapping \"%s\".\n",arg_arr[2]); LOGLINE;
  857.     wr_sj("E: DIFF: "); wr_sj(linet1);
  858.     sprintf(linet1,"         %s\n",mess2); LOGLINE; wr_sj(linet1);
  859.     num_err++;
  860.    }
  861.  if ((mess1 != NULL) || (mess2 != NULL))
  862.     goto frombadmap;
  863.  
  864.  /* At this point the two files to be compared are sitting in memory and we   */
  865.  /* have a ready-for-writing log file. We are now ready to compare.           */
  866.  
  867.  /* First perform a binary image comparison as a check for later.             */
  868.  /* We could do this later, but it is better to do this now, in case the      */
  869.  /* complicated comparison code somehow corrupts one of the images.           */
  870.  is_image= ((len_file1 == len_file2) &&
  871.             (memcmp(p_file1,p_file2,(size_t) len_file1) == 0));
  872.  
  873.  /* This anonymous block performs the actual comparison. */
  874.  {
  875.   /* The comparison is performed by scrolling the two input files through two */
  876.   /* fixed-length line buffers (buf1 and buf2 - see below). To avoid copying, */
  877.   /* the buffers are made circular. Processing takes place by comparing the   */
  878.   /* first line of each buffer. If the line is the same, the buffers are      */
  879.   /* scrolled by one line. If they are different, then we have encountered a  */
  880.   /* DIFFERENCES SECTION and we have to compare lines near the top of the     */
  881.   /* buffers to find a match. When a match is found, each buffer is scrolled  */
  882.   /* down to its match point and processing continues.                        */
  883.  
  884. /* LBUFSIZ is the number of number of lines that each buffer can hold.        */
  885. /*         Lines are indexed from [0,LBUFSIZ-1].                              */
  886. /*         WARNING: LBUFSIZ must be a power of two corresponding to WRAP(X).  */
  887. /*         WARNING: Totally different input files will provoke O(LBUFSIZ^2)   */
  888. /*                  checksum comparisons per LBUFSIZ input lines.             */
  889. /* WRAP(X) is a macro that performs wraparound of buffer indices.             */
  890. /* GAP     is the number of lines that have to match to end a diff section.   */
  891. /* MAXDIST is the maximum "distance" that is tested when matching.            */
  892. #define LBUFSIZ 64
  893. #define WRAP(X) ((X) & 0x3F)
  894. #define GAP     3
  895. #define MAXDIST (LBUFSIZ-GAP)
  896.  
  897. /* The following macro compares two lines in the buffers.                     */
  898. /* The arguments are absolute buffer indices, not relative ones.              */
  899. /* We assume that checksums will mismatch more often than line lengths.       */
  900. #define COMPLINE(INDA,INDB) \
  901.    ((buf1[INDA].c_line == buf2[INDB].c_line) &&  \
  902.     (buf1[INDA].l_line == buf2[INDB].l_line) &&  \
  903.     (memcmp(buf1[INDA].p_line,buf2[INDB].p_line, \
  904.                 (size_t) buf1[INDA].l_line)==0))
  905.  
  906.   /* The two line buffers buf1 and buf2 (see below) don't actually store      */
  907.   /* lines. Instead they store line structures which store pointers to the    */
  908.   /* lines in the mapped images of the files. They also store the length of   */
  909.   /* each line, and a checksum of the line. The checksum is useful for        */
  910.   /* speeding up the comparisons between lines when processing a differences  */
  911.   /* section.                                                                 */
  912.   typedef
  913.      struct
  914.        {
  915.         char *p_line; /* Pointer to first byte in the line. */
  916.         ulong l_line; /* Number of bytes in the line.       */
  917.         uword c_line; /* Checksum of the line.              */
  918.        } line_t;
  919.  
  920.   char   *p_next1 = p_file1;            /* Points to next line in file1.      */
  921.   char   *p_next2 = p_file2;            /* Points to next line in file2.      */
  922.   char   *p_post1 = p_file1+len_file1;  /* Byte following image of file1.     */
  923.   char   *p_post2 = p_file2+len_file2;  /* Byte following image of file2.     */
  924.   line_t buf1[LBUFSIZ];      /* Comparison buffer for first  file.            */
  925.   line_t buf2[LBUFSIZ];      /* Comparison buffer for second file.            */
  926.   ulong  buf1top = 0;        /* Index of first line in first  buffer.         */
  927.   ulong  buf2top = 0;        /* Index of first line in second buffer.         */
  928.   ulong  buf1fil = 0;        /* Number of lines in first  buffer.             */
  929.   ulong  buf2fil = 0;        /* Number of lines in second buffer.             */
  930.   ulong  topnum1 = 1;        /* Line number of first line of first buffer.    */
  931.   ulong  topnum2 = 1;        /* Line number of first line of second buffer.   */
  932.  
  933.   /* The following loop compares the line(s) at the top of the two buffers    */
  934.   /* and processes (lines1,lines2) lines of each.                             */
  935.   while (TRUE)
  936.     {
  937.      ulong lines1; /* Lines of file1 processed during this loop iteration.    */
  938.      ulong lines2; /* Lines of file2 processed during this loop iteration.    */
  939.      ulong d,g;    /* Used in comparison loops.                               */
  940.  
  941.      /* The first thing we do is to fill each buffer as full as possible. At  */
  942.      /* the end of the next two lumps of code, the only reason that a file's  */
  943.      /* is not full is that we have reached the end of the file.              */
  944.  
  945.      /* Fill the first buffer as full as possible. */
  946.      while ((buf1fil < LBUFSIZ) && (p_next1 != p_post1))
  947.        {
  948.         ulong  ind   = WRAP(buf1top + buf1fil);
  949.         ulong  len   = 0;
  950.         uword  csum  = 0;
  951.         char  *p_lin = p_next1;
  952.         while (TRUE)
  953.           {
  954.            if (p_next1 == p_post1) break;
  955.            len++;
  956.            csum=(csum+*p_next1++) & 0xFFFF;
  957.            if (*(p_next1-1) == EOL) break;
  958.           }
  959.         buf1[ind].p_line = p_lin;
  960.         buf1[ind].l_line = len;
  961.         buf1[ind].c_line = csum;
  962.         buf1fil++;
  963.        }
  964.  
  965.      /* Fill the second buffer as full as possible. */
  966.      while ((buf2fil < LBUFSIZ) && (p_next2 != p_post2))
  967.        {
  968.         ulong  ind   = WRAP(buf2top + buf2fil);
  969.         ulong  len   = 0;
  970.         uword  csum  = 0;
  971.         char  *p_lin = p_next2;
  972.         while (TRUE)
  973.           {
  974.            if (p_next2 == p_post2) break;
  975.            len++;
  976.            csum=(csum+*p_next2++) & 0xFFFF;
  977.            if (*(p_next2-1) == EOL) break;
  978.           }
  979.         buf2[ind].p_line = p_lin;
  980.         buf2[ind].l_line = len;
  981.         buf2[ind].c_line = csum;
  982.         buf2fil++;
  983.        }
  984.  
  985.      /* If the buffers are empty then we must be at the end of each file. */
  986.      if (buf1fil==0 && buf2fil==0)
  987.         break;
  988.  
  989.      /* Try to peel a pair of matching lines off the top of the buffer.       */
  990.      /* If we succeed, zip down to the end of the loop and flush them.        */
  991.      /* We can't integrate this code into the next part because the next part */
  992.      /* requires GAP matches, whereas here we require just one.               */
  993.      if ((buf1fil>0) && (buf2fil>0) && COMPLINE(buf1top,buf2top))
  994.        {lines1=lines2=1; goto flushlines;}
  995.  
  996.      /* At this point, we know we have a differences section. */
  997.      anydiff=TRUE;
  998.  
  999.      /* We now compare the top lines of the two buffers for a match. A match  */
  1000.      /* is only considered to have been found if we match GAP consecutive     */
  1001.      /* lines. The best match minimizes the DISTANCE which is the sum of the  */
  1002.      /* offsets (lines1,lines2) (in lines) from the top of each buffer where  */
  1003.      /* the match starts. Even better matches minimize abs(lines1-lines2) as  */
  1004.      /* well. All these nested loops are to ensure that we search best first. */
  1005.      for (d=1;d<=MAXDIST;d++)
  1006.        {
  1007.         /* Calculate half distance on the high side. */
  1008.         long half = (d/2)+1;
  1009.         long off;
  1010.         long sign_v;
  1011.         /* Explore up and down simultaneously from the halfway mark. */
  1012.         for (off=0;off<=half;off++)
  1013.            for (sign_v= -1;sign_v<2;sign_v+=2)
  1014.              {
  1015.               long x = half + sign_v*off;
  1016.               /* The following test allows the above loops to be sloppy. */
  1017.               if (0<=x && x<=d)
  1018.                 {
  1019.                  lines1=x;
  1020.                  lines2=d-lines1;
  1021.                  /* We now know that we want to test at (lines1,lines2).  */
  1022.                  /* So compare the GAP lines starting at those positions. */
  1023.                  /* Note: lines1 and lines2, as well as being the number  */
  1024.                  /* of lines processed, are also the offset to the first  */
  1025.                  /* match line in our match gap.                          */
  1026.                  for (g=0;g<GAP;g++)
  1027.                    { /* Note: R for relative, A for absolute. */
  1028.                     ulong t1r = lines1 + g;
  1029.                     ulong t2r = lines2 + g;
  1030.                     ulong t1a,t2a;
  1031.  
  1032.                     /* If both files have run out at this point, it's a match!*/
  1033.                     if ((t1r>=buf1fil) && (t2r>=buf2fil)) continue;
  1034.  
  1035.                     /* If just one of the files has run out it's a mismatch.  */
  1036.                     if ((t1r>=buf1fil) || (t2r>=buf2fil)) goto gapfail;
  1037.  
  1038.                     /* We now know that we have two real lines. Compare them. */
  1039.                     /* Variables are to avoid big nested macro expansions.    */
  1040.                     t1a = WRAP(buf1top+t1r);
  1041.                     t2a = WRAP(buf2top+t2r);
  1042.                     if (!COMPLINE(t1a,t2a)) goto gapfail;
  1043.                    }
  1044.                  /* If we dropped out of the gap loop, we must have found     */
  1045.                  /* GAP consecutive matches. So we can run off and write out  */
  1046.                  /* the difference section.                                   */
  1047.                  goto writediff;
  1048.  
  1049.                  /* Here's where we jump if we found a mismatch during gap    */
  1050.                  /* looping. All we do is try next pair of offsets.           */
  1051.                  gapfail:;
  1052.                 } /* End sloppy if. */
  1053.              } /* End for sign_v. */
  1054.        } /* End for distance loop. */
  1055.  
  1056.      /* If we got to here then we must have dropped out of the search loop    */
  1057.      /* which means that there must have been no match at all between the     */
  1058.      /* buffers. The only thing to do is to write out what we have as         */
  1059.      /* a differences section.                                                */
  1060.      lines1=buf1fil;
  1061.      lines2=buf2fil;
  1062.  
  1063.      /* Write out the differences section (lines1,lines2) to the log file. */
  1064.      writediff:
  1065.      { /* Begin writediff */
  1066.       ulong i,j;
  1067.       LOGSTR("\n");
  1068.       LOGSTR("     +-----\n");
  1069.       for (i=0;i<lines1;i++)
  1070.         {
  1071.          ulong nline = WRAP(buf1top+i);
  1072.          sprintf(linet1,"%05lu|",(ulong) topnum1+i); LOGLINE;
  1073.          for (j=0;j<buf1[nline].l_line;j++)
  1074.             LOGCHAR(*(buf1[nline].p_line+j));
  1075.         }
  1076.       LOGSTR("     +-----\n");
  1077.       for (i=0;i<lines2;i++)
  1078.         {
  1079.          ulong nline = WRAP(buf2top+i);
  1080.          sprintf(linet1,"%05lu|",(ulong) topnum2+i); LOGLINE;
  1081.          for (j=0;j<buf2[nline].l_line;j++)
  1082.             LOGCHAR(*(buf2[nline].p_line+j));
  1083.         }
  1084.       LOGSTR("     +-----\n");
  1085.      } /* End writediff. */
  1086.  
  1087.      /* Flush from buffer however many lines we ended up processing. */
  1088.      flushlines:
  1089.      buf1top=WRAP(buf1top+lines1); topnum1+=lines1; buf1fil-=lines1;
  1090.      buf2top=WRAP(buf2top+lines2); topnum2+=lines2; buf2fil-=lines2;
  1091.  
  1092.     } /* End the while loop that runs through the two files. */
  1093.  } /* End of anonymous block for doing actual comparison. */
  1094.  
  1095.  /* The anydiff flag tells us if the loop found any difference sections. */
  1096.  is_same=!anydiff;
  1097.  
  1098.  /* Target position if we couldn't map in the input files earlier. */
  1099.  frombadmap:
  1100.  
  1101.  /* Release the memory allocated by the mapper for the input files. */
  1102.  /* Failure to do this will result in a memory leak!                */
  1103.  mm_zapt();
  1104.  
  1105.  /* If the two files are identical, tell the log file. */
  1106.  if (is_same)
  1107.    LOGSTR("The two files are IDENTICAL.\n");
  1108.  
  1109.  /* Invalidate this test in the log file, if inconsistent (see later). */
  1110.  if (is_same != is_image)
  1111.     LOGSTR("<<CONSISTENCY FAILURE: ABOVE COMPARISON INVALID>>\n");
  1112.  
  1113.  /* If we had problems with the log file at any stage, kick up a fuss now. */
  1114.  if (badwrite)
  1115.    {wl_sj("S: DIFF: Error writing to log file."); num_sev++;}
  1116.  
  1117.  /* Close the log file. */
  1118.  if (fclose(logfile) == FCLOSE_F)
  1119.    {wl_sj("S: DIFF: Error closing the log file."); num_sev++;}
  1120.  
  1121.  /* The above code is quite tricky and there is a chance that it contains     */
  1122.  /* bugs. So, as a safety check we compare the results from the binary memory */
  1123.  /* image comparison performed earlier and the more complicated text          */
  1124.  /* comparison above. If they differ, then it's time to go kaboom.            */
  1125.  if (is_image && !is_same)
  1126.     as_bomb("do_diff: Image comparison succeeded, but text comparison failed.");
  1127.  if (!is_image && is_same)
  1128.     as_bomb("do_diff: Image comparison failed, but text comparison succeeded.");
  1129.  
  1130.  /* If files are non-same and ABORT option is turned on, set severe status. */
  1131.  if (!is_same && diffabort)
  1132.    {
  1133.     wl_sj(
  1134.      "S: DIFF: Files have not been proven identical, and ABORT option is on.");
  1135.     num_sev++;
  1136.    }
  1137.  
  1138.  /* Tell the console whether comparison succeeded. */
  1139.  if (is_same)
  1140.     wl_sj("The two files are IDENTICAL.");
  1141.  else
  1142.     wl_sj("The two files are DIFFERENT (see the differences file for the details).");
  1143.  
  1144.  /* Increment the difference summary counters. */
  1145.  difftotl++;
  1146.  if (is_same)
  1147.     diffsucc++;
  1148. }
  1149.  
  1150. /******************************************************************************/
  1151.  
  1152. LOCAL void do_dsum P_((void));
  1153. LOCAL void do_dsum ()
  1154. {
  1155.  sprintf(linet1,"Summary of Differences"); wl_sj(linet1);
  1156.  sprintf(linet1,"----------------------"); wl_sj(linet1);
  1157.  sprintf(linet1,"Identical = %lu.",(ulong)            diffsucc); wl_sj(linet1);
  1158.  sprintf(linet1,"Different = %lu.",(ulong) (difftotl-diffsucc)); wl_sj(linet1);
  1159.  sprintf(linet1,"Total     = %lu.",(ulong)            difftotl); wl_sj(linet1);
  1160. }
  1161.  
  1162. /******************************************************************************/
  1163.  
  1164. LOCAL void do_dzer P_((void));
  1165. LOCAL void do_dzer ()
  1166. /* Zaps difference counters. */
  1167. {
  1168.  difftotl = 0;
  1169.  diffsucc = 0;
  1170. }
  1171.  
  1172. /******************************************************************************/
  1173.  
  1174. LOCAL void do_eneo P_((void));
  1175. LOCAL void do_eneo ()
  1176. {
  1177.  if (arg_num != 2)
  1178.    {
  1179.     wl_sj("S: The ENEO command must be given exactly one argument.");
  1180.     num_sev++;
  1181.     return;
  1182.    }
  1183.  if (fexists(arg_arr[1]))
  1184.     if (remove(arg_arr[1]) != REMOVE_S)
  1185.       {
  1186.        sprintf(linet1,"S: ENEO failed to delete \"%s\".",arg_arr[1]);
  1187.        wl_sj(linet1);
  1188.        num_sev++;
  1189.        return;
  1190.       }
  1191. }
  1192.  
  1193. /******************************************************************************/
  1194.  
  1195. LOCAL void do_exec P_((void));
  1196. LOCAL void do_exec ()
  1197. {
  1198.  uword i;
  1199.  
  1200.  if (arg_num < 2)
  1201.    {
  1202.     wl_sj("S: The EXECUTE command requires at least one argument.");
  1203.     num_sev++;
  1204.     return;
  1205.    }
  1206.  if (arg_num > 10)
  1207.    {
  1208.     wl_sj("S: The EXECUTE command can have at most nine arguments.");
  1209.     num_sev++;
  1210.     return;
  1211.    }
  1212.  
  1213.  /* Zap all the numeric arguments. */
  1214.  for (i=0; i<10; i++)
  1215.     subval[i][0]=EOS;
  1216.  
  1217.  /* Copy the arguments over to the $1 $2 etc substitution variables. */
  1218.  for (i=1;i<arg_num; i++)
  1219.     strcpy(subval[i-1],arg_arr[i]);
  1220.  
  1221.  /* Run up a new FunnelWeb shell and interpret the file. */
  1222.  interstr(arg_arr[1]);
  1223. }
  1224.  
  1225. /******************************************************************************/
  1226.  
  1227. LOCAL void do_exist P_((void));
  1228. LOCAL void do_exist ()
  1229. {
  1230.  if (arg_num != 2)
  1231.    {
  1232.     wl_sj("S: The EXISTS command requires exactly one argument.");
  1233.     num_sev++;
  1234.     return;
  1235.    }
  1236.  if (!fexists(arg_arr[1]))
  1237.    {
  1238.     sprintf(linet1,"S: EXISTS failed to find \"%s\".",arg_arr[1]);
  1239.     wl_sj(linet1);
  1240.     num_sev++;
  1241.     return;
  1242.    }
  1243. }
  1244.  
  1245. /******************************************************************************/
  1246.  
  1247. LOCAL void do_fix P_((void));
  1248. LOCAL void do_fix ()
  1249. /* When the test suite is moved from one machine to another, it is possible   */
  1250. /* that at some stage it will be moved using a BINARY transfer rather than a  */
  1251. /* text file transfer. The result is that the test files will contain lines   */
  1252. /* terminated with a sequence of control characters that the local buffered   */
  1253. /* IO library will not convert to '\n' upon reading in. The are a few         */
  1254. /* solutions to this problem, but one of the most direct is to have a command */
  1255. /* such as this one that can convert the file over.                           */
  1256. /*                                                                            */
  1257. /* Once we have identified an end of line, it is easy to write it out as we   */
  1258. /* can just send a '\n' and the local buffered IO library will write the      */
  1259. /* right codes for us. The tricky part is deciding what an EOL is in the      */
  1260. /* input stream. Well, I could have made the control characters for the       */
  1261. /* remote EOL a parameter of this command, but instead I decided to use a     */
  1262. /* simple algorithm that should cover nearly all cases...                     */
  1263. /* ALGORITHM: Parse the input into alternating blocks of control characters   */
  1264. /* and non-control characters. Parse each block of control characters into    */
  1265. /* subblocks by parsing it from left to right and starting a new subblock the */
  1266. /* moment a character of the subblock currently being parsed appears again.   */
  1267. /* This covers at least the following cases, and probably many more:          */
  1268. /*    UNIX         LF                                                         */
  1269. /*    MSDOS     CR LF                                                         */
  1270. /*    Macintosh CR                                                            */
  1271. {
  1272.  /* Erk! But I found out the hard way, it doesn't work on a VAX!              */
  1273.  /* I'm making this do nothing on a VAX rather than generate an error as I    */
  1274.  /* want the scripts to work silently on the VAX without modification.        */
  1275. #if !VMS
  1276.  FILE *infile;     /* File variable for input file.                           */
  1277.  FILE *tmpfile;    /* File variable for temporary output file.                */
  1278.  char *p_target;   /* Name of final (target) output file.                     */
  1279.  STAVAR char *p_temp=NULL;   /* Name of temporary output file.                */
  1280.  
  1281.  bool  seen[256];  /* TRUE if char is in current control sequence.            */
  1282.  char  undo[256];  /* undo[0..length-1] contains current control sequence.    */
  1283.  uword length;     /* Number of control bytes in our buffer.                  */
  1284.  uword i;
  1285.  int status;
  1286.  
  1287.  /* Allocate the temporary file name if not already allocated. */
  1288.  if (p_temp==NULL) p_temp=(p_fn_t) mm_perm(sizeof(fn_t));
  1289.  
  1290.  /* We can take one or two arguments. One argument means that we should       */
  1291.  /* fix up the input file, leaving the result in the input file.              */
  1292.  if (arg_num != 2 && arg_num != 3)
  1293.    {
  1294.     wl_sj("S: The FIXEOLS command requires one or two filename arguments.");
  1295.     num_sev++;
  1296.     return;
  1297.    }
  1298.  
  1299.  /* Change to two arguments if the input name is the same as output name. */
  1300.  if (arg_num==3 && strcmp(arg_arr[1],arg_arr[2])==0) arg_num=2;
  1301.  
  1302.  /* Work out what the target name is going to be. */
  1303.  p_target=(arg_num==2) ? arg_arr[1] : arg_arr[2];
  1304.  
  1305.  /* Generate a temporary filename for the output file. This is tricky because */
  1306.  /* on many machines, one cannot rename across devices or directories. This   */
  1307.  /* means that the temporary file has to be created in the same directory as  */
  1308.  /* the file that we are going to rename it to later (the target file).       */
  1309.  strcpy(p_temp,p_target);
  1310.  fn_ins(p_temp,fn_temp());
  1311.  
  1312.  /* Open the input file for BINARY reading. */
  1313.  infile=fopen(arg_arr[1],"rb");
  1314.  if (infile == FOPEN_F)
  1315.    {
  1316.     sprintf(linet1,"S: FIXEOLS: Error opening \"%s\".",arg_arr[1]);
  1317.     wl_sj(linet1);
  1318.     num_sev++;
  1319.     return;
  1320.    }
  1321.  
  1322.  /* Create the output file for TEXT writing. */
  1323.  tmpfile=fopen(p_temp,"w");
  1324.  if (tmpfile == FOPEN_F)
  1325.    {
  1326.     fclose(infile);
  1327.     wl_sj("S: FIXEOLS: Error creating the temporary output file.");
  1328.     num_sev++;
  1329.     return;
  1330.    }
  1331.  
  1332.  /* Initialialize the control character buffer to empty. */
  1333.  for (i=0;i<256;i++) seen[i]=FALSE;
  1334.  length=0;
  1335.  
  1336.  while (TRUE)
  1337.    {
  1338.     /* Read in the next character from the input file. */
  1339.     char ch=getc(infile);
  1340.     if (ferror(infile))
  1341.        {wl_sj("S: FIXEOLS: Error reading the input file.");num_sev++;return;}
  1342.     if (feof(infile)) break;
  1343.  
  1344.     /* Does character terminate a non-empty buffer? If so, flush it. */
  1345.     if (length>0 && (isascprn(ch) || seen[ch]))
  1346.       {
  1347.        if (fputc(EOL,tmpfile) == FPUTC_F) goto write_failure;
  1348.        for (i=0;i<length;i++) seen[undo[i]]=FALSE;
  1349.        length=0;
  1350.       }
  1351.  
  1352.     /* Now we can approach the character cleanly and freshly. */
  1353.     if (isascprn(ch))
  1354.        {if (fputc(ch,tmpfile) == FPUTC_F) goto write_failure;}
  1355.     else
  1356.        {undo[length++]=ch; seen[ch]=TRUE;}
  1357.    }
  1358.  if (length>0)
  1359.    {if (fputc(EOL,tmpfile) == FPUTC_F) goto write_failure;}
  1360.  
  1361.  if (fflush(tmpfile) != FFLUSH_S)
  1362.     {wl_sj("S: FIXEOLS: Error flushing the temporary output file.");num_sev++;return;}
  1363.  if (fclose(infile) == FCLOSE_F)
  1364.    {wl_sj("S: FIXEOLS: Error closing the input file.");num_sev++;return;}
  1365.  if (fclose(tmpfile) == FCLOSE_F)
  1366.    {wl_sj("S: FIXEOLS: Error closing the temporary output file.");num_sev++;return;}
  1367.  
  1368.  /* If renaming to the input file, we have to delete the input file first. */
  1369.  if (arg_num==2)
  1370.    {
  1371.     status=remove(arg_arr[1]);
  1372.     if (status != REMOVE_S)
  1373.       {
  1374.        wl_sj("S: FIXEOLS: Error deleting existing input file to replace it");
  1375.        wl_sj("            with the temporary output file. Deleting temporary and aborting...");
  1376.        remove(p_temp);
  1377.        num_sev++;
  1378.        return;
  1379.       }
  1380.    }
  1381.  
  1382.  /* Rename the temporary file to the target output file. */
  1383.  status=rename(p_temp,p_target);
  1384.  
  1385.  /* Do the error checking. */
  1386.  if (status != RENAME_S)
  1387.    {
  1388.     wl_sjl("S: FIXEOLS: Error renaming temporary output file to output file.");
  1389.     sprintf(linet1,"Temporary file name was \"%s\".",p_temp);
  1390.     wl_sjl(linet1);
  1391.     sprintf(linet1,"Output    file name was \"%s\".",p_target);
  1392.     wl_sjl(linet1);
  1393.     wl_sjl("FunnelWeb will leave both files intact so you can look at them.");
  1394.     num_sev++;
  1395.     return;
  1396.    }
  1397.  
  1398.  return;
  1399.  
  1400.  write_failure:
  1401.     wl_sj("S: FIXEOLS: Error writing the output file.");num_sev++;return;
  1402. #endif
  1403. }
  1404.  
  1405. /******************************************************************************/
  1406.  
  1407. LOCAL void do_fweb P_((char *));
  1408. LOCAL void do_fweb(p_cl)
  1409. /* This function performs a single run of FunnelWeb proper. */
  1410. char *p_cl;
  1411. {
  1412.  op_t saveop;
  1413.  
  1414.  /* Do set can do all the work for us. However, it operates on p_defopt so we */
  1415.  /* have to do some juggling.                                                 */
  1416.  ASSIGN(saveop,*p_defopt);
  1417.  do_set(p_cl);
  1418.  ASSIGN(option,*p_defopt);
  1419.  ASSIGN(*p_defopt,saveop);
  1420.  if (num_sev>0) return;
  1421.  
  1422.  /* do_set ensures that the user hasn't specified any action parameters such  */
  1423.  /* as +X and +K, but it necessarily doesn't check to make sure that the user */
  1424.  /* has actually specified an input file!                                     */
  1425.  if (!option.op_f_b)
  1426.    {
  1427.     wl_sj("S: No input file specified in FW command.");
  1428.     num_sev++;
  1429.     return;
  1430.    }
  1431.  fwonerun();
  1432. }
  1433.  
  1434. /******************************************************************************/
  1435.  
  1436. LOCAL void do_help P_((void));
  1437. LOCAL void do_help()
  1438. {
  1439.  uword messno;
  1440.  
  1441.  if (arg_num == 1)
  1442.    {
  1443.     hel_wri(wr_sj,HL_MEN);
  1444.     return;
  1445.    }
  1446.  if (arg_num > 2)
  1447.    {
  1448.     wl_sj("S: The HELP command takes at most one argument.");
  1449.     num_sev++;
  1450.     return;
  1451.    }
  1452.  
  1453.  /* Translate message name to number. */
  1454.  
  1455.  messno=hel_num(arg_arr[1]);
  1456.  if (messno == HL_ERR)
  1457.    {
  1458.     wl_sj(
  1459.    "S: Unrecognised help message name. Try just \"help\" for a list of names.");
  1460.     num_sev++;
  1461.     return;
  1462.    }
  1463.  
  1464.  hel_wri(wr_sj,messno);
  1465. }
  1466.  
  1467. /******************************************************************************/
  1468.  
  1469. LOCAL void do_set(p_comlin)
  1470. /* The SET command allows the user to specify default FunnelWeb options.      */
  1471. char *p_comlin;
  1472. {
  1473.  op_t tmpopt;
  1474.  
  1475.  /* Experiment with temporary options, not the real thing. */
  1476.  ASSIGN(tmpopt,*p_defopt);
  1477.  
  1478.  /* Now execute the effect of the command line on 'p_defopt'. */
  1479.  if (!op_add(&tmpopt,p_comlin,wr_sj))
  1480.    {
  1481.     wl_s("This is a severe error (S). Aborting to FunnelWeb shell...");
  1482.     num_sev++;
  1483.     return;
  1484.    }
  1485.  
  1486.  /* Now make sure that the user didn't specify an option to do with the       */
  1487.  /* entire FunnelWeb run and not just this invocation of FunnelWeb proper.    */
  1488.  if (tmpopt.op_j_b)
  1489.    {
  1490.     wl_s("S: You cannot invoke FunnelWeb with +J from the FunnelWeb shell.");
  1491.     wl_s("   To create a journal file, exit FunnelWeb and reinvoke with \"fw +j\".");
  1492.     wl_s("This is a severe error. Aborting to FunnelWeb shell...");
  1493.     num_sev++;
  1494.     return;
  1495.    }
  1496.  if (tmpopt.op_x_b)
  1497.    {
  1498.     wl_s("S: You cannot invoke FunnelWeb with +X from the FunnelWeb shell.");
  1499.     wl_s("Use the interactive command EXECUTE instead.");
  1500.     wl_s("This is a severe error. Aborting to FunnelWeb shell...");
  1501.     num_sev++;
  1502.     return;
  1503.    }
  1504.  if (tmpopt.op_k_b)
  1505.    {
  1506.     wl_s("S: You cannot invoke FunnelWeb with +K from the FunnelWeb shell.");
  1507.     wl_s("This is a severe error. Aborting to FunnelWeb shell...");
  1508.     num_sev++;
  1509.     return;
  1510.    }
  1511.  if (tmpopt.op_h_b)
  1512.    {
  1513.     wl_s("S: You cannot invoke FunnelWeb with +H from the FunnelWeb shell.");
  1514.     wl_s("Use the interactive command HELP instead.");
  1515.     wl_s("This is a severe error. Aborting to FunnelWeb shell...");
  1516.     num_sev++;
  1517.     return;
  1518.    }
  1519.  
  1520.  /* If we get to here, the options must be OK so we can set them as default. */
  1521.  ASSIGN(*p_defopt,tmpopt);
  1522. }
  1523.  
  1524. /******************************************************************************/
  1525.  
  1526. LOCAL void do_show P_((void));
  1527. LOCAL void do_show()
  1528. /* The SHOW command writes out the current options. */
  1529. {
  1530.  if (arg_num != 1)
  1531.    {
  1532.     wl_sj("S: The SHOW command does not take arguments.");
  1533.     num_sev++;
  1534.     return;
  1535.    }
  1536.  wl_sj("Here are the FunnelWeb command line options that are");
  1537.  wl_sj("current in this FunnelWeb session:");
  1538.  op_wri(p_defopt,wr_sj);
  1539. }
  1540.  
  1541. /******************************************************************************/
  1542.  
  1543. LOCAL void do_stat P_((void));
  1544. LOCAL void do_stat ()
  1545. /* The status command checks the number of diagnostics generated by the run.  */
  1546. {
  1547.  uword i;
  1548.  char *thing;
  1549.  ulong cnum;
  1550.  
  1551.  if (arg_num<1 || arg_num>4)
  1552.    {
  1553.     wl_sj("S: The STATUS command requires zero to three arguments.");
  1554.     num_sev++;
  1555.     return;
  1556.    }
  1557.  
  1558.  /* Zero arguments just means write out status. */
  1559.  if (arg_num == 1)
  1560.    {
  1561.     sprintf(linet1,"Last command: Severes=%lu, Errors=%lu, Warnings=%lu.",
  1562.             (ulong) old_sev, (ulong) old_err, (ulong) old_war);
  1563.     wl_sj(linet1);
  1564.     sprintf(linet1,"Totals      : Severes=%lu, Errors=%lu, Warnings=%lu.",
  1565.             (ulong) sum_sev, (ulong) sum_err, (ulong) sum_war);
  1566.     wl_sj(linet1);
  1567.     return;
  1568.    }
  1569.  
  1570.  /* More than one argument means CHECK status. */
  1571.  for (i=1;i<arg_num;i++)
  1572.    {
  1573.     char ch=toupper(arg_arr[i][0]);
  1574.     unsigned num;
  1575.     if (ch!='W' && ch!='E' && ch!='S')
  1576.       {
  1577.        sprintf(linet1,
  1578.               "S: Argument %u of STATUS command has bad letter.",
  1579.                (unsigned) i);
  1580.        wl_sj(linet1);
  1581.        wl_sj("Arguments must be of the form ('W'|'E'|'S')<decimalnumber>.");
  1582.        num_sev++;
  1583.        return;
  1584.       }
  1585.     if (sscanf(&arg_arr[i][1],"%u",&num) != 1)
  1586.       {
  1587.        sprintf(linet1,
  1588.       "S: Argument %u of STATUS command has bad number.",
  1589.                (unsigned) i);
  1590.        wl_sj(linet1);
  1591.        wl_sj("   Arguments must be of the form ('W'|'E'|'S')<decimalnumber>.");
  1592.        num_sev++;
  1593.        return;
  1594.       }
  1595.     switch(ch)
  1596.       {
  1597.        case 'W': cnum=old_war; thing="warnings"; break;
  1598.        case 'E': cnum=old_err; thing="errors"  ; break;
  1599.        case 'S': cnum=old_sev; thing="severes" ; break;
  1600.        default : as_bomb("do_stat: case defaulted.");
  1601.       }
  1602.     if (cnum != num)
  1603.       {
  1604.        sprintf(linet1,
  1605.        "S: STATUS command detected wrong number of %s.",thing);
  1606.        wl_sj(linet1);
  1607.        sprintf(linet1, "Specifed %s=%u, Actual %s=%u.",
  1608.                thing,(unsigned) num,thing,(unsigned) cnum);
  1609.        wl_sj(linet1);
  1610.        num_sev++;
  1611.       }
  1612.    }
  1613. }
  1614.  
  1615. /******************************************************************************/
  1616.  
  1617.  
  1618. LOCAL void do_trace P_((void));
  1619. LOCAL void do_trace()
  1620. {
  1621.  if (arg_num != 2) goto help;
  1622.  strupper(arg_arr[1]);
  1623.  if (strcmp(arg_arr[1],"OFF") == 0) {tracing=FALSE; return;}
  1624.  if (strcmp(arg_arr[1], "ON") == 0) {tracing=TRUE ; return;}
  1625.  
  1626.  help:
  1627.     wl_sj("S: The TRACE command has two forms:");
  1628.     wl_sj("      TRACE OFF");
  1629.     wl_sj("      TRACE ON");
  1630.     num_sev++;
  1631.     return;
  1632. }
  1633.  
  1634. /******************************************************************************/
  1635.  
  1636. LOCAL void do_write P_((char *));
  1637. LOCAL void do_write(p)
  1638. char *p;
  1639. {
  1640.  uword len;
  1641.  
  1642.  /* Skip over the main command and the following blanks. */
  1643.  while (*p!=' ' && *p!=EOS) p++;
  1644.  while (*p==' ') p++;
  1645.  
  1646.  /* Now make sure that the remaining string is delimited by double quotes.    */
  1647.  len=strlen(p);
  1648.  if ((*p != '\"') || (p[len-1] != '\"') || len<2)
  1649.    {
  1650.     wl_sj("W: The argument to WRITE should be delimited by double quotes.");
  1651.     wl_sj(p);
  1652.     num_war++;
  1653.     return;
  1654.    }
  1655.  
  1656.  /* Now temporarily hack out the quotes and write out the string. */
  1657.  p[len-1]=EOS;
  1658.  wl_sj(p+1);
  1659.  p[len-1]='\"';
  1660. }
  1661.  
  1662. /******************************************************************************/
  1663.  
  1664. LOCAL void do_writu P_((char *));
  1665. LOCAL void do_writu(p)
  1666. char *p;
  1667. {
  1668.  uword len;
  1669.  
  1670.  /* Skip over the main command and the following blanks. */
  1671.  while (*p!=' ' && *p!=EOS) p++;
  1672.  while (*p==' ') p++;
  1673.  
  1674.  /* Now make sure that the remaining string is delimited by double quotes.    */
  1675.  len=strlen(p);
  1676.  if ((*p != '\"') || (p[len-1] != '\"') || len<2)
  1677.    {
  1678.     wl_sj("W: The argument to WRITEU should be delimited by double quotes.");
  1679.     wl_sj(p);
  1680.     num_war++;
  1681.     return;
  1682.    }
  1683.  
  1684.  /* Now temporarily hack out the quotes and write out the string. */
  1685.  p[len-1]=EOS;
  1686.  wl_sj(p+1);
  1687.  p[len-1]='\"';
  1688.  
  1689.  /* Now write out another line underlining the above. */
  1690.  {uword i; for (i=0;i<len-2;i++) wr_sj("-"); wl_sj("");}
  1691. }
  1692.  
  1693. /******************************************************************************/
  1694.  
  1695. LOCAL bool do_command P_((char *));
  1696. LOCAL bool do_command(p_command)
  1697. /* Execute a single FunnelWeb shell command. */
  1698. char *p_command;
  1699. {
  1700.  char *v;
  1701.  bool result=FALSE;
  1702.  
  1703.  zerdia();
  1704.  
  1705.  /* Check the command for non-printables. */
  1706.  {
  1707.   char *s=p_command;
  1708.   while (*s!=EOS)
  1709.     {
  1710.      if (!isascprn(*s))
  1711.        {
  1712.         sprintf(linet1,
  1713.                 "S: Command line has non-printable at column %u.",
  1714.                 (unsigned) (s-p_command)+1);
  1715.         wl_sj(linet1);
  1716.         num_sev++;
  1717.         goto finished;
  1718.        }
  1719.      s++;
  1720.     }
  1721.  }
  1722.  
  1723.  /* Perform substitutions. */
  1724.  dollsub(p_command); if (num_sev>0) goto finished;
  1725.  
  1726.  /* Ignore commands consisting entirely of blanks (or empty commands). */
  1727.  {
  1728.   char *s=p_command;
  1729.   while (*s==' ') s++;
  1730.   if (*s==EOS) goto finished;
  1731.  }
  1732.  
  1733.  /* Reject command lines beginning with a blank. */
  1734.  if (p_command[0]==' ')
  1735.    {
  1736.     wl_sj("S: Leading blanks are not allowed in command lines.");
  1737.     num_sev++;
  1738.     goto finished;
  1739.    }
  1740.  
  1741.  /* Ignore command lines commencing with the comment character. */
  1742.  if (p_command[0]=='!')
  1743.     {restdia(); goto finished;}
  1744.  
  1745.  /* Parse the command line into arguments. */
  1746.  explode(p_command);
  1747.  
  1748.  /* Complain if there is no command verb. */
  1749.  as_cold(arg_num>0,"do_command: zero arguments!");
  1750.  
  1751.  /* It's convenient to have v pointing to verb. */
  1752.  v=arg_arr[0];
  1753.  
  1754.  /* Convert the verb to upper case. */
  1755.  strupper(v);
  1756.  
  1757.  
  1758.  /* Execute the verb. */
  1759.  
  1760.  if (strcmp(v,"HERE")==0) skipping=FALSE;
  1761.  else if (!skipping)
  1762.       if (strcmp(v,"ABSENT"     )==0) do_absen();
  1763.  else if (strcmp(v,"CODIFY"     )==0) do_cody ();
  1764.  else if (strcmp(v,"COMPARE"    )==0) do_comp ();
  1765.  else if (strcmp(v,"DEFINE"     )==0) do_defin(&p_command[0]);
  1766.  else if (strcmp(v,"DIFF"       )==0) do_diff();
  1767.  else if (strcmp(v,"DIFFSUMMARY")==0) do_dsum();
  1768.  else if (strcmp(v,"DIFFZERO"   )==0) do_dzer();
  1769.  else if (strcmp(v,"ENEO"       )==0) do_eneo ();
  1770.  else if (strcmp(v,"EXECUTE"    )==0) do_exec ();
  1771.  else if (strcmp(v,"EXISTS"     )==0) do_exist();
  1772.  else if (strcmp(v,"FIXEOLS"    )==0) do_fix  ();
  1773.  else if (strcmp(v,"FW"         )==0) do_fweb (&p_command[0]);
  1774.  else if (strcmp(v,"HELP"       )==0) do_help ();
  1775.  else if (strcmp(v,"QUIT"       )==0) result=TRUE;
  1776.  else if (strcmp(v,"SET"        )==0) do_set  (&p_command[0]);
  1777.  else if (strcmp(v,"SHOW"       )==0) do_show ();
  1778.  else if (strcmp(v,"SKIPTO"     )==0) skipping=TRUE;
  1779.  else if (strcmp(v,"STATUS"     )==0) do_stat ();
  1780.  else if (strcmp(v,"TOLERATE"   )==0) noabort=TRUE;
  1781.  else if (strcmp(v,"TRACE"      )==0) do_trace();
  1782.  else if (strcmp(v,"WRITE"      )==0) do_write(&p_command[0]);
  1783.  else if (strcmp(v,"WRITEU"     )==0) do_writu(&p_command[0]);
  1784.  else
  1785.    {
  1786.     /* The following trace is likely to confuse beginners.        */
  1787.     /* sprintf(linet1,"Expanded command line=\"%s\".",p_command); */
  1788.     /* wl_sj(linet1);                                             */
  1789.     wl_sj("S: Unknown command. Type HELP for a list of commands.");
  1790.     num_sev++;
  1791.     goto finished;
  1792.    }
  1793.  
  1794.  finished:
  1795.  sumdia();
  1796.  return result;
  1797. }
  1798.  
  1799. /******************************************************************************/
  1800.  
  1801. LOCAL void interpret P_((FILE *,char *));
  1802. LOCAL void interpret(p_file,filnam)
  1803. /* p_file must be a file opened for reading. The file's name must be supplied */
  1804. /* in filnam for error reporting reasons. The function reads each line from   */
  1805. /* the file and feeds it to the FunnelWeb interpreter command executer.       */
  1806. FILE *p_file;
  1807. char *filnam;
  1808. {
  1809.  ulong lineno=0;
  1810.  char *result;
  1811.  bool b;
  1812.  cl_t  comline;
  1813.  char *p_comline;
  1814.  
  1815.  p_comline = &comline[0];
  1816.  
  1817.  while (TRUE)
  1818.    {
  1819.     bool oldnoabort = noabort;
  1820.     noabort=FALSE;
  1821.  
  1822.     if (p_file == stdin || tracing)
  1823.        wr_sj("FunnelWeb>");
  1824.  
  1825.     result=fgets(p_comline,(int) COMLINE_MAX,p_file);
  1826.     if (feof(p_file))
  1827.       {
  1828.        sprintf(linet1,"<End of Script File \"%s\">",filnam);
  1829.       if (p_file == stdin || tracing)
  1830.           wl_sj(linet1);
  1831.        break;
  1832.       }
  1833.     if (ferror(p_file) || (result == FGETS_FE))
  1834.       {
  1835.        sprintf(linet1,"F: Error reading command file \"%s\".",filnam);
  1836.        wl_sj(linet1);
  1837.        wl_sj("Aborting...");
  1838.        sum_fat++;
  1839.        return;
  1840.       }
  1841.     if (p_file == stdin || tracing) wr_j(p_comline);
  1842.     if (p_file != stdin && tracing) wr_s(p_comline);
  1843.  
  1844.     lineno++;
  1845.     if (strlen(p_comline)==COMLINE_MAX)
  1846.       {
  1847.        sprintf(linet1,"F: Line %lu of command file \"%s\" is too long.",
  1848.                       (unsigned long) lineno,filnam);
  1849.        wl_sj(linet1);
  1850.        wl_sj("Aborting...");
  1851.        sum_fat++;
  1852.        return;
  1853.       }
  1854.     as_cold(p_comline[strlen(p_comline)-1]==EOL,"interpret: NO NEWLINE!");
  1855.     p_comline[strlen(p_comline)-1]=EOS;
  1856.     as_cold(strlen(p_comline)<COMLINE_MAX,"interpret: Filename too long.");
  1857.  
  1858.     b=do_command(p_comline);
  1859.     if (b) break;
  1860.  
  1861.     if (sum_fat>0) break;
  1862.     if ((p_file != stdin) && (num_sev+num_err>0) && !oldnoabort)
  1863.       {
  1864.        wl_sj("Error caused termination of FunnelWeb shellscript.");
  1865.        break;
  1866.       }
  1867.    }
  1868. }
  1869.  
  1870. /******************************************************************************/
  1871.  
  1872. LOCAL void interstr(filnam)
  1873. /* The 'interpret' function (above) interprets each line of a file already    */
  1874. /* opened for reading. This function does a little more, opening and closing  */
  1875. /* the file before and after calling 'interpret'.                             */
  1876. char *filnam;
  1877. {
  1878.  FILE *p_file;
  1879.  fn_t fn;
  1880.  
  1881.  /* Set up a default of ".fws" as a file extension. */
  1882.  strcpy(&fn[0],".fws");
  1883.  
  1884.  /* Inherit the actual filename. */
  1885.  as_cold(strlen(filnam)<=FILENAME_MAX,"interstr: Filename blasted.");
  1886.  fn_ins(&fn[0],filnam);
  1887.  
  1888.  p_file=fopen(fn,"r");
  1889.  if (p_file == FOPEN_F)
  1890.    {
  1891.     sprintf(linet1,"S: Error opening command file \"%s\".",fn);
  1892.     wl_sj(linet1);
  1893.     sum_sev++;
  1894.     return;
  1895.    }
  1896.  
  1897.  interpret(p_file,&fn[0]); if (sum_fat>0) return;
  1898.  
  1899.  if (fclose(p_file) == FCLOSE_F)
  1900.    {
  1901.     sprintf(linet1,"F: Error closing command file \"%s\".",fn);
  1902.     wl_sj(linet1);
  1903.     wl_sj("Aborting...");
  1904.     sum_fat++;
  1905.     return;
  1906.    }
  1907. }
  1908.  
  1909. /******************************************************************************/
  1910.  
  1911. LOCAL void chk_cline P_((void));
  1912. LOCAL void chk_cline()
  1913. /* Checks to make sure that the command line specifies exactly one action.    */
  1914. {
  1915.  uword countopt=0;
  1916.  
  1917.  /* Count the number of active action options are turned on. */
  1918.  if (p_comopt->op_f_b) countopt++;
  1919.  if (p_comopt->op_x_b) countopt++;
  1920.  if (p_comopt->op_k_b) countopt++;
  1921.  if (p_comopt->op_h_b) countopt++;
  1922.  
  1923.  if (countopt == 0)
  1924.    {
  1925.     wl_sj("Your command line does not specify an action.");
  1926.     wl_sj("Here some common ways of invoking FunnelWeb.");
  1927.     wl_sj("");
  1928.     wl_sj("   fw filename          Tangle filename.web.");
  1929.     wl_sj("   fw filename +t       Tangle and weave filename.web.");
  1930.     wl_sj("   fw +k                Enter interactive mode.");
  1931.     wl_sj("   fw +xfilename        Execute FunnelWeb shellscript filename.fws.");
  1932.     wl_sj("   fw +h                Display help information about FunnelWeb.");
  1933.     wl_sj("");
  1934.     if (countopt == 0)
  1935.        wl_sj("F: Aborting because command line does not specify an action.");
  1936.     sum_fat++;
  1937.    }
  1938. }
  1939.  
  1940. /******************************************************************************/
  1941.  
  1942. LOCAL void open_j P_((void));
  1943. LOCAL void open_j()
  1944. /* Creates and opens the journal file. Note that the journal output stream is */
  1945. /* established regardless of whether the user requested a journal file. The   */
  1946. /* only difference is that if the user did not specify a journal file, the    */
  1947. /* stream is created in error mode which means that it never actually writes. */
  1948. {
  1949.  fn_t jname;
  1950.  
  1951.  /* Establish the journal file output stream. */
  1952.  strcpy(jname,"");                 /* Start with an empty string.             */
  1953.  fn_ins(jname,p_comopt->op_f_s);   /* Insert input file name.                 */
  1954.  fn_ins(jname,".jrn");             /* Insert file extension.                  */
  1955.  fn_ins(jname,p_comopt->op_j_s);   /* Insert command line spec.               */
  1956.  wf_ini(&f_j,p_comopt->op_j_b);    /* Initialize the stream.                  */
  1957.  wf_ope(&f_j,jname);            /* Create the file.                           */
  1958.  if (p_comopt->op_j_b && wf_err(&f_j))
  1959.    {
  1960.     sprintf(linet1,"F: Error creating journal file \"%s\".",jname);
  1961.     wl_s(linet1);
  1962.     wl_s("Aborting...");
  1963.     sum_fat++;
  1964.     return;
  1965.    }
  1966. }
  1967.  
  1968. /******************************************************************************/
  1969.  
  1970. LOCAL void close_j P_((void));
  1971. LOCAL void close_j()
  1972. /* Closes the journal file. */
  1973. {
  1974.  if (!p_comopt->op_j_b) return;
  1975.  if (wf_err(&f_j))
  1976.    {
  1977.     wl_s("F: Error writing to journal file. Aborting...");
  1978.     sum_fat++;
  1979.     return;
  1980.    }
  1981.  wf_clo(&f_j);
  1982.  if (wf_err(&f_j))
  1983.    {
  1984.     wl_s("F: Error flushing and closing journal file. Aborting...");
  1985.     sum_fat++;
  1986.     return;
  1987.    }
  1988. }
  1989.  
  1990. /******************************************************************************/
  1991.  
  1992. LOCAL void cl_help P_((void));
  1993. LOCAL void cl_help ()
  1994. {
  1995.  uword messno;
  1996.  
  1997.  /* Translate message name to number. This ought to work, as the options      */
  1998.  /* package is already have supposed to have cleared this argument as OK.     */
  1999.  messno=hel_num(p_comopt->op_h_s);
  2000.  as_cold(messno!=HL_ERR,"cl_help: Unknown help argument.");
  2001.  
  2002.  /* Write out the message. */
  2003.  hel_wri(wr_sj,messno);
  2004. }
  2005.  
  2006. /******************************************************************************/
  2007.  
  2008. EXPORT void command(p_comline)
  2009. /* Execute the top level command line. This is the place where we do all the  */
  2010. /* "once per shell" things as opposed to the "once per run" things.           */
  2011. /* If a fatal error occurs, the correct course of action here is to increment */
  2012. /* sum_fat and return immediately. The main() function deals with delivering  */
  2013. /* the correct return status to the operating system.                         */
  2014. char *p_comline;
  2015. {
  2016.  old_war=old_err=old_sev=0;
  2017.  
  2018.  tracing=FALSE;
  2019.  noabort=FALSE;
  2020.  skipping=FALSE;
  2021.  
  2022.  /* Allocate space for command line arguments. */
  2023.  allocarg();
  2024.  
  2025.  /* Set up the standard output (stdout) screen (console) output stream. */
  2026.  wf_ini(&f_s,TRUE);
  2027.  wf_att(&f_s,stdout);
  2028.  
  2029.  /* Parse the command line and place the information in p_comopt-> */
  2030.  op_ini(p_comopt);
  2031.  if (!op_add(p_comopt,p_comline,wr_s))
  2032.    {wr_s("F: Command line error. Aborting..."); sum_fat++; goto windup;}
  2033.  
  2034.  /* If the user asked for some peace and quiet by setting +Q, disable the     */
  2035.  /* console stream. Note: FunnelWeb main() always issues a message using a    */
  2036.  /* printf if any diagnostics have been generated.                            */
  2037.  if (p_comopt->op_q_b) wf_ini(&f_s,FALSE);
  2038.  
  2039.  /* Create and open the journal file. */
  2040.  open_j(); if (sum_fat>0) goto windup;
  2041.  
  2042.  wl_sj("FunnelWeb Version 3.0 (May 1992)");
  2043.  wl_sj("--------------------------------");
  2044.  wl_sj("Copyright (C) Ross Williams 1992. There is ABSOLUTELY NO WARRANTY.");
  2045.  wl_sj("You are welcome to distribute this software under certain conditions.");
  2046.  if (p_comopt->op_k_b)
  2047.  wl_sj("For more information, type HELP.");
  2048.  else
  2049.  wl_sj("For more information, use the +h (help) option (e.g. \"fw +h\").");
  2050.  wl_sj("");
  2051.  
  2052.  /* Ensure that the user has specified at least one action. */
  2053.  chk_cline(); if (sum_fat>0) goto windup;
  2054.  
  2055.  /* Establish the default options for the shell run (if any).                 */
  2056.  /* Get rid of any options not to do with a single run of FunnelWeb proper.   */
  2057.  ASSIGN(*p_defopt,*p_comopt);
  2058.  p_defopt->op_j_b=FALSE;
  2059.  p_defopt->op_x_b=FALSE;
  2060.  p_defopt->op_k_b=FALSE;
  2061.  
  2062.  /* In the absence of everything else, command line options are run options. */
  2063.  ASSIGN(option,*p_comopt);
  2064.  
  2065.  /* Execute initialization file if any. */
  2066.  if (fexists(INITFILE))
  2067.     interstr(INITFILE);
  2068.  
  2069.  /* Execute the specified actions. */
  2070.  if (p_comopt->op_x_b) interstr(p_comopt->op_x_s);
  2071.  if (p_comopt->op_f_b) {zerdia(); fwonerun(); sumdia();}
  2072.  if (p_comopt->op_h_b) cl_help();
  2073.  if (p_comopt->op_k_b) interpret(stdin,"standard_input");
  2074.  
  2075.  /* If we weren't in onerun mode, give a grand summary of errors. */
  2076.  if (p_comopt->op_k_b || p_comopt->op_x_b)
  2077.     {
  2078.      wr_sj("Final diagnostics totals: ");
  2079.      errsum(sum_fat,sum_sev,sum_err,sum_war);
  2080.      wl_sj(linet1);
  2081.     }
  2082.  
  2083.  /* Close the journal file. */
  2084.  close_j(); if (sum_fat>0) goto windup;
  2085.  
  2086.  /* Check for errors on the screen stream (standard output). */
  2087.  if (p_comopt->op_s_b && wf_err(&f_s))
  2088.    {
  2089.     /* No point in trying to write a message to the screen! */
  2090.     /* But we can at least register a fatal error.          */
  2091.     sum_fat++;
  2092.    }
  2093.  
  2094.  windup:
  2095.  
  2096.  /* If the user has set +Q to turn off screen output and one or more          */
  2097.  /* diagnostics have been generated, we need to break through to warn the     */
  2098.  /* user.                                                                     */
  2099.  {
  2100.   ulong sum_all=sum_fat+sum_sev+sum_err+sum_war;
  2101.   if (p_comopt->op_q_b && sum_all>0)
  2102.      {
  2103.       errsum(sum_fat,sum_sev,sum_err,sum_war);
  2104.       fprintf(stderr,"%s\n",linet1);
  2105.      }
  2106.  }
  2107. }
  2108.  
  2109. /******************************************************************************/
  2110. /*                             End of COMMAND.C                               */
  2111. /******************************************************************************/
  2112.