home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z01.c < prev    next >
C/C++ Source or Header  |  1994-01-23  |  17KB  |  400 lines

  1. /*@z01.c:Supervise:StartSym, AllowCrossDb, Encapsulated, etc.@****************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z01.c                                                      */
  26. /*  MODULE:       Supervise                                                  */
  27. /*  EXTERNS:      main(), StartSym, GalleySym, InputSym, PrintSym,           */
  28. /*                AllowCrossDb, Encapsulated                                 */
  29. /*                                                                           */
  30. /*****************************************************************************/
  31. #include "externs"
  32.  
  33.  
  34. /*****************************************************************************/
  35. /*                                                                           */
  36. /*  StartSym      the symbol table entry for \Start (overall scope)          */
  37. /*  GalleySym     the symbol table entry for @Galley                         */
  38. /*  InputSym      the symbol table entry for @LInput                         */
  39. /*  PrintSym      the symbol table entry for \Print (root target)            */
  40. /*                                                                           */
  41. /*****************************************************************************/
  42.  
  43. OBJECT StartSym, GalleySym, InputSym, PrintSym;
  44.  
  45. /*****************************************************************************/
  46. /*                                                                           */
  47. /*  AllowCrossDb        Allow references to OldCrossDb and NewCrossDb        */
  48. /*  Encapsulated        Produce a one-page encapsulated PostScript file      */
  49. /*                                                                           */
  50. /*****************************************************************************/
  51.  
  52. BOOLEAN AllowCrossDb;
  53. BOOLEAN Encapsulated;
  54.  
  55.  
  56. /*****************************************************************************/
  57. /*                                                                           */
  58. /*  static OBJECT load(xstr, xpredefined, xleft, xright, xindef, xprec)      */
  59. /*                                                                           */
  60. /*  Load a predefined operator with these attributes into the symbol table.  */
  61. /*  If the operator has parameters, load symbols for those also.             */
  62. /*                                                                           */
  63. /*****************************************************************************/
  64.  
  65. static OBJECT load(xstr, xpre, xleft, xright, xindef, xprec)
  66. FULL_CHAR *xstr;  unsigned  xpre;  BOOLEAN xleft, xright, xindef;
  67. unsigned char xprec;
  68. { OBJECT s;
  69.   s = InsertSym(xstr, LOCAL, no_fpos, xprec, xindef, FALSE, xpre, StartSym,nil);
  70.   if( xleft )  InsertSym( AsciiToFull("pa"), LPAR, no_fpos, DEFAULT_PREC,
  71.     FALSE, FALSE, 0, s, nil);
  72.   if( xright )  InsertSym( AsciiToFull("pb"), RPAR, no_fpos, DEFAULT_PREC,
  73.     FALSE, FALSE, 0, s, nil);
  74.   if( xleft && xright )  right_assoc(s) = TRUE;
  75.   return s;
  76. } /* end load */
  77.  
  78.  
  79. /*@::GetArg(), main()@********************************************************/
  80. /*                                                                           */
  81. /*  GetArg(arg, message)                                                     */
  82. /*                                                                           */
  83. /*  Get the next argument from the command line and store it in arg.         */
  84. /*  Print message as a fatal error if it isn't there.                        */
  85. /*                                                                           */
  86. /*****************************************************************************/
  87.  
  88. #define GetArg(arg, message)                        \
  89. { if( !StringEqual(AsciiToFull(argv[i]+2), STR_EMPTY) )            \
  90.     arg = AsciiToFull(argv[i]+2);                    \
  91.   else if( i < argc-1 && *argv[i+1] != CH_HYPHEN )            \
  92.     arg = AsciiToFull(argv[i++ +1]);                    \
  93.   else                                    \
  94.     Error(FATAL, no_fpos, message);                    \
  95. } /* end GetArg */
  96.  
  97.  
  98. /*****************************************************************************/
  99. /*                                                                           */
  100. /*  main(argc, argv)                                                         */
  101. /*                                                                           */
  102. /*  Read command line, initialise everything, read definitions, read         */
  103. /*  galleys, clean up and exit.                                              */
  104. /*                                                                           */
  105. /*****************************************************************************/
  106.  
  107. main(argc, argv)
  108. int argc; char *argv[];
  109. { int i, len;  FULL_CHAR *arg;
  110.   OBJECT t, res, s;            /* current token, parser output      */
  111.   BOOLEAN stdin_seen;            /* TRUE when stdin file seen         */
  112.   FULL_CHAR *cross_db;            /* name of cross reference database  */
  113.   FULL_CHAR *outfile;            /* name of output file               */
  114.   FILE *out_fp;
  115.  
  116.   /* initialise various modules, add current directory to search paths */
  117.   AllowCrossDb = TRUE;
  118.   Encapsulated = FALSE;
  119.   InitSym();
  120.   LexInit();
  121.   MemInit();
  122.   InitFiles();
  123.   AddToPath(SOURCE_PATH,   STR_EMPTY);
  124.   AddToPath(DATABASE_PATH, STR_EMPTY);
  125.   AddToPath(INCLUDE_PATH,  STR_EMPTY);
  126.  
  127.   /* read command line */
  128.   stdin_seen = FALSE;
  129.   cross_db = CROSS_DB;
  130.   outfile = STR_STDOUT;
  131.   for( i = 1;  i < argc;  i++ )
  132.   { if( *argv[i] == CH_HYPHEN ) switch( *(argv[i]+1) )
  133.     {
  134.       case CH_FLAG_OUTFILE:
  135.      
  136.     /* read name of output file */
  137.     GetArg(outfile, "usage: -o<filename>");
  138.     break;
  139.  
  140.  
  141.       case CH_FLAG_SUPPRESS:
  142.      
  143.     /* suppress references to OldCrossDb and NewCrossDb */
  144.     AllowCrossDb = FALSE;
  145.     break;
  146.  
  147.  
  148.       case CH_FLAG_CROSS:
  149.      
  150.     /* read name of cross reference database */
  151.     GetArg(cross_db, "usage: -c<filename>");
  152.     break;
  153.  
  154.  
  155.       case CH_FLAG_ERRFILE:
  156.      
  157.     /* read log file name */
  158.     GetArg(arg, "usage: -e<filename>");
  159.     ErrorInit(arg);
  160.     break;
  161.  
  162.  
  163.       case CH_FLAG_EPSFIRST:
  164.      
  165.     /* -EPS produces encapsulated PostScript output */
  166.     if( !StringEqual(AsciiToFull(argv[i]+1), STR_EPS) )
  167.       Error(FATAL, no_fpos, "usage: -EPS");
  168.     Encapsulated = TRUE;
  169.     break;
  170.  
  171.  
  172.       case CH_FLAG_DIRPATH:
  173.      
  174.     /* add directory to database and sysdatabase paths */
  175.     GetArg(arg, "usage: -D<dirname>");
  176.     AddToPath(DATABASE_PATH, arg);
  177.     AddToPath(SYSDATABASE_PATH, arg);
  178.     break;
  179.  
  180.  
  181.       case CH_FLAG_ENCPATH:
  182.      
  183.     /* add directory to encoding path */
  184.     GetArg(arg, "usage: -C<dirname>");
  185.     AddToPath(ENCODING_PATH, arg);
  186.     break;
  187.  
  188.  
  189.       case CH_FLAG_FNTPATH:
  190.      
  191.     /* add directory to font path */
  192.     GetArg(arg, "usage: -F<dirname>");
  193.     AddToPath(FONT_PATH, arg);
  194.     break;
  195.  
  196.  
  197.       case CH_FLAG_INCPATH:
  198.      
  199.     /* add directory to include and sysinclude paths */
  200.     GetArg(arg, "usage: -I<dirname>");
  201.     AddToPath(INCLUDE_PATH, arg);
  202.     AddToPath(SYSINCLUDE_PATH, arg);
  203.     break;
  204.  
  205.  
  206.       case CH_FLAG_INCLUDE:
  207.      
  208.     /* read sysinclude file and strip any .lt suffix */
  209.     GetArg(arg, "usage: -i<filename>");
  210.     len = StringLength(arg) - StringLength(SOURCE_SUFFIX);
  211.     if( len >= 0 && StringEqual(&arg[len], SOURCE_SUFFIX) )
  212.       StringCopy(&arg[len], STR_EMPTY);
  213.     DefineFile(arg, STR_EMPTY, no_fpos,
  214.       SOURCE_FILE, SYSINCLUDE_PATH);
  215.     break;
  216.  
  217.  
  218.       case CH_FLAG_HYPHEN:
  219.      
  220.     /* declare hyphenation file */
  221.     if( FirstFile(HYPH_FILE) != NO_FILE )
  222.       Error(FATAL, no_fpos, "two -h options illegal");
  223.     GetArg(arg, "usage: -h<filename>");
  224.     DefineFile(arg, STR_EMPTY, no_fpos,
  225.       HYPH_FILE, INCLUDE_PATH);
  226.     DefineFile(arg, HYPH_SUFFIX, no_fpos,
  227.       HYPH_PACKED_FILE, INCLUDE_PATH);
  228.     break;
  229.  
  230.  
  231.       case CH_FLAG_VERSION:
  232.      
  233.     fprintf(stderr, "%s\n", LOUT_VERSION);
  234.     break;
  235.  
  236.  
  237.       case CH_FLAG_USAGE:
  238.      
  239.     fprintf(stderr, "usage: lout [ -i<filename> ] files\n");
  240.     exit(0);
  241.     break;
  242.  
  243.  
  244.       case CH_FLAG_DEBUG:
  245.      
  246.     debug_init(argv[i]);
  247.     break;
  248.  
  249.  
  250.       case '\0':
  251.      
  252.     /* read stdin as file name */
  253.     if( stdin_seen )  Error(FATAL, no_fpos, "stdin read twice!");
  254.     stdin_seen = TRUE;
  255.     DefineFile(STR_STDIN, STR_EMPTY, no_fpos, SOURCE_FILE, SOURCE_PATH);
  256.     break;
  257.  
  258.  
  259.       default:
  260.      
  261.     Error(FATAL, no_fpos, "unknown command line flag %s", argv[i]);
  262.     break;
  263.  
  264.     }
  265.     else
  266.     {   /* argument is source file, strip any .lout suffix and define it */
  267.     arg = argv[i];
  268.     len = StringLength(arg) - StringLength(SOURCE_SUFFIX);
  269.     if( len >= 0 && StringEqual(&arg[len], SOURCE_SUFFIX) )
  270.       StringCopy(&arg[len], STR_EMPTY);
  271.     DefineFile(AsciiToFull(argv[i]), STR_EMPTY, no_fpos,
  272.         SOURCE_FILE, SOURCE_PATH);
  273.     }
  274.   } /* for */
  275.  
  276.   /* define hyphenation file if not done already by -h flag */
  277.   if( FirstFile(HYPH_FILE) == NO_FILE )
  278.   { DefineFile(HYPH_FILENAME, STR_EMPTY, no_fpos, HYPH_FILE, SYSINCLUDE_PATH);
  279.     DefineFile(HYPH_FILENAME, HYPH_SUFFIX, no_fpos,
  280.       HYPH_PACKED_FILE, SYSINCLUDE_PATH);
  281.   }
  282.  
  283.   /* start timing if required */
  284.   ifdebug(DPP, D, ProfileOn("main"));
  285.  
  286.   /* open output file, or stdout if none specified, and initialize printer */
  287.   if( StringEqual(outfile, STR_STDOUT) )  out_fp = stdout;
  288.   else if( (out_fp = StringFOpen(outfile, "w")) == null )
  289.     Error(FATAL, no_fpos, "cannot open output file %s", outfile);
  290.   FontInit();
  291.   PrintInit(out_fp);
  292.  
  293.   /* append default directories to file search paths */
  294.   AddToPath(FONT_PATH,         AsciiToFull(FONT_DIR));
  295.   AddToPath(ENCODING_PATH,     AsciiToFull(EVEC_DIR));
  296.   AddToPath(SYSDATABASE_PATH,  AsciiToFull(DATA_DIR));
  297.   AddToPath(DATABASE_PATH,     AsciiToFull(DATA_DIR));
  298.   AddToPath(SYSINCLUDE_PATH,   AsciiToFull(INCL_DIR));
  299.   AddToPath(INCLUDE_PATH,      AsciiToFull(INCL_DIR));
  300.  
  301.   /* use stdin if no source files were mentioned */
  302.   if( FirstFile(SOURCE_FILE) == NO_FILE )
  303.     DefineFile(STR_STDIN, STR_EMPTY, no_fpos, SOURCE_FILE, SOURCE_PATH);
  304.  
  305.   /* load predefined symbols into symbol table */
  306.   StartSym = nil;  /* Not a mistake */
  307.   StartSym  = load(KW_START,    0,     FALSE,  FALSE,  TRUE,  NO_PREC     );
  308.   GalleySym = load(KW_GALLEY,   0,     FALSE,  FALSE,  TRUE,  NO_PREC     );
  309.   InputSym  = load(KW_INPUT,    0,     FALSE,  FALSE,  TRUE,  NO_PREC     );
  310.   PrintSym  = load(KW_PRINT,    0,     FALSE,  FALSE,  TRUE,  NO_PREC     );
  311.  
  312.   load(KW_BEGIN,       BEGIN,          FALSE,  FALSE,  FALSE, BEGIN_PREC  );
  313.   load(KW_END,         END,            FALSE,  FALSE,  FALSE, END_PREC    );
  314.   load(KW_ENV,         ENV,            FALSE,  FALSE,  FALSE, NO_PREC     );
  315.   load(KW_CLOS,        CLOS,           FALSE,  FALSE,  FALSE, NO_PREC     );
  316.   load(KW_LVIS,        LVIS,           FALSE,  FALSE,  FALSE, NO_PREC     );
  317.   load(KW_LBR,         LBR,            FALSE,  FALSE,  FALSE, LBR_PREC    );
  318.   load(KW_RBR,         RBR,            FALSE,  FALSE,  FALSE, RBR_PREC    );
  319.   load(KW_INCLUDE,     INCLUDE,        FALSE,  FALSE,  FALSE, NO_PREC     );
  320.   load(KW_SYSINCLUDE,  SYS_INCLUDE,    FALSE,  FALSE,  FALSE, NO_PREC     );
  321.   load(KW_PREPEND,     PREPEND,        FALSE,  FALSE,  FALSE, NO_PREC     );
  322.   load(KW_SYSPREPEND,  SYS_PREPEND,    FALSE,  FALSE,  FALSE, NO_PREC     );
  323.   load(KW_DATABASE,    DATABASE,       FALSE,  FALSE,  FALSE, NO_PREC     );
  324.   load(KW_SYSDATABASE, SYS_DATABASE,   FALSE,  FALSE,  FALSE, NO_PREC     );
  325.   load(KW_USE,         USE,            FALSE,  FALSE,  FALSE, NO_PREC     );
  326.   load(KW_CASE,        CASE,           TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  327.   load(KW_YIELD,       YIELD,          TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  328.   load(KW_XCHAR,       XCHAR,          FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  329.   load(KW_FONT,        FONT,           TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  330.   load(KW_SPACE,       SPACE,          TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  331.   load(KW_BREAK,       BREAK,          TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  332.   load(KW_NEXT,        NEXT,           FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  333.   load(KW_OPEN,        OPEN,           TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  334.   load(KW_TAGGED,      TAGGED,         TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  335.   load(KW_HIGH,        HIGH,           TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  336.   load(KW_WIDE,        WIDE,           TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  337.   load(KW_ONE_COL,     ONE_COL,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  338.   load(KW_ONE_ROW,     ONE_ROW,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  339.   load(KW_HSCALE,      HSCALE,         FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  340.   load(KW_VSCALE,      VSCALE,         FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  341.   load(KW_SCALE,       SCALE,          TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  342.   load(KW_HCONTRACT,   HCONTRACT,      FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  343.   load(KW_VCONTRACT,   VCONTRACT,      FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  344.   load(KW_HEXPAND,     HEXPAND,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  345.   load(KW_VEXPAND,     VEXPAND,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  346.   load(KW_PADJUST,     PADJUST,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  347.   load(KW_HADJUST,     HADJUST,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  348.   load(KW_VADJUST,     VADJUST,        FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  349.   load(KW_ROTATE,      ROTATE,         TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  350.   load(KW_INCGRAPHIC,  INCGRAPHIC,     FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  351.   load(KW_SINCGRAPHIC, SINCGRAPHIC,    FALSE,  TRUE,   FALSE, DEFAULT_PREC);
  352.   load(KW_GRAPHIC,     GRAPHIC,        TRUE,   TRUE,   FALSE, DEFAULT_PREC);
  353.   load(KW_CROSS,       CROSS,          TRUE,   TRUE,   FALSE, CROSSOP_PREC);
  354.   load(KW_NULL,        NULL_CLOS,      FALSE,  FALSE,  TRUE,  NO_PREC     );
  355.  
  356. #define setcat(s, mk, jn)  has_mark(s)=mk, has_join(s)=jn
  357.  
  358.   s=load(KW_VCAT_NN, VCAT, TRUE, TRUE, FALSE, VCAT_PREC); setcat(s,FALSE,FALSE);
  359.   s=load(KW_VCAT_MN, VCAT, TRUE, TRUE, FALSE, VCAT_PREC); setcat(s,TRUE, FALSE);
  360.   s=load(KW_VCAT_NJ, VCAT, TRUE, TRUE, FALSE, VCAT_PREC); setcat(s,FALSE,TRUE);
  361.   s=load(KW_VCAT_MJ, VCAT, TRUE, TRUE, FALSE, VCAT_PREC); setcat(s,TRUE, TRUE);
  362.   s=load(KW_HCAT_NN, HCAT, TRUE, TRUE, FALSE, HCAT_PREC); setcat(s,FALSE,FALSE);
  363.   s=load(KW_HCAT_MN, HCAT, TRUE, TRUE, FALSE, HCAT_PREC); setcat(s,TRUE, FALSE);
  364.   s=load(KW_HCAT_NJ, HCAT, TRUE, TRUE, FALSE, HCAT_PREC); setcat(s,FALSE,TRUE);
  365.   s=load(KW_HCAT_MJ, HCAT, TRUE, TRUE, FALSE, HCAT_PREC); setcat(s,TRUE, TRUE);
  366.   s=load(KW_ACAT_NJ, ACAT, TRUE, TRUE, FALSE, ACAT_PREC); setcat(s,FALSE,TRUE);
  367.   s=load(KW_ACAT_MJ, ACAT, TRUE, TRUE, FALSE, ACAT_PREC); setcat(s,TRUE, TRUE);
  368.  
  369.   /* intialize current time and load @Moment symbol */
  370.   InitTime();
  371.  
  372.   /* initialise scope chain to <StartSym> */
  373.   PushScope(StartSym, FALSE, FALSE);
  374.  
  375.   /* initialise lexical analyser */
  376.   LexPush(FirstFile(SOURCE_FILE), 0, SOURCE_FILE);
  377.  
  378.   /* process input files */
  379.   InitParser(cross_db);
  380.   t = NewToken(BEGIN, no_fpos, 0, 0, BEGIN_PREC, StartSym);
  381.   res = Parse(&t, StartSym, TRUE, TRUE);
  382.   TransferEnd(res);
  383.   TransferClose();
  384.  
  385.   /* close various  modules */
  386.   PrintClose();
  387.   CrossClose();
  388.   CloseFiles();
  389.  
  390.   /* wrapup */
  391.   ifdebug(DST, D, CheckSymSpread() );
  392.   debug0(ANY, D, "commencing deletes");
  393.   ifdebug(ANY, D, DeleteEverySym() );
  394.   ifdebug(DMA, D, DebugMemory() );
  395.   ifdebug(DPP, D, ProfileOff("main"));
  396.   ifdebug(DPP, D, ProfilePrint());
  397.   exit(0);
  398.   return 0;
  399. } /* end main */
  400.