home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / scpp / part2 / scpp.h < prev   
Encoding:
C/C++ Source or Header  |  1986-11-30  |  10.4 KB  |  371 lines

  1.  
  2. /*
  3.  * scpp.h - common declarations for the selective C preprocessor, scpp.
  4.  *
  5.  * Copyright (c) 1985 by
  6.  * Tektronix, Incorporated Beaverton, Oregon 97077
  7.  * All rights reserved.
  8.  *
  9.  * Permission is hereby granted for personal, non-commercial
  10.  * reproduction and use of this program, provided that this
  11.  * notice and all copyright notices are included in any copy.
  12.  */
  13.  
  14. # define TRUE    1
  15. # define FALSE    0
  16.  
  17. /*
  18.  * sawerror - "some error was processed" If true, scpp exits non-zero.
  19.  * Set by the error printout routines, examined when exiting.
  20.  */
  21. #ifdef VARS
  22. int sawerror;
  23. #else
  24. extern int sawerror;
  25. #endif
  26.  
  27. # define BSIZE    512    /*
  28.              * # of bytes per read -- controls how quickly
  29.              * istk[] is consumed.
  30.              */
  31.  
  32. /*
  33.  * PENDSIZ is a tunable parameter -- it is the largest number of characters
  34.  *  which can be waiting to be output.  This number sets a limit on:
  35.  *  1) the longest comment;
  36.  *  2) the largest invocation of a macro with parameters, i.e. the number
  37.  *    of characters between the '(' and the ')'.
  38.  *  3) the longest preprocessor control line, e.g. #define....
  39.  * PENDSIZ also controls the input stack size, ISTK.
  40.  *
  41.  * Pend[] is the pending output buffer.
  42.  *
  43.  * Nxtout points to where within pend[] to put the next token scanned.
  44.  * Nxtout is advanced by questr() and quec(),
  45.  *  the primitives for putting stuff in pend[],
  46.  * and is moved backward by dispose() and outpend(),
  47.  *  the primitives for getting stuff out of pend[].
  48.  *
  49.  * Curtext points to the start of the text within pend[] of
  50.  * the current token.  Set by gtok() and gintok().
  51.  * For anyone who uses gtok() or gintok() to get
  52.  *  a token, the limits of the text of the resultant
  53.  *  token are curtext and nxtout. (be aware that the
  54.  *  text of anything in pend[] may contain imbedded
  55.  *  ATTN bytes.)
  56.  */
  57.  
  58. # define PENDSIZ 8000
  59. # define PENDHIGH 512    /* highwater mark for flushing pend[]    */
  60. #ifdef VARS
  61. char pend[PENDSIZ];
  62. char *nxtout;
  63. char *curtext;
  64. #else
  65. extern char pend[];
  66. extern char *nxtout;
  67. char *curtext;
  68. #endif
  69. extern char *dispose();
  70. #define outpend() (nxtout < &pend[PENDHIGH] ? 0 : writepend())
  71.  
  72. /*
  73.  * filestk - the stack containing the state of the current file
  74.  */
  75.  
  76. struct afile {
  77.     int    af_fd;        /* the open file's file-descriptor    */
  78.     char    *af_name;    /* the name of the file (dynamic alloc)    */
  79.     int    af_line;    /* the current line in the file        */
  80.     int    af_raw;        /*
  81.                  * "scanning unprocessed data rather than
  82.                  *  pushed-back data".
  83.                  * Used to count input lines.
  84.                  * Also used to prevent
  85.                  *  interpretation of "#if" expressions whose
  86.                  *  truth or falsehood does not depend on
  87.                  *  interpreting macros (e.g. #if '\377' > 0). 
  88.                  */
  89.     int    af_hide;    /*
  90.                  * "do not output anything for this file."
  91.                  * This file is the result of an uninterpreted
  92.                  * "# include".
  93.                  */
  94. };
  95.  
  96. #define FILESIZ 11    /* max # of include files + 1 (the original file) */
  97. #ifdef VARS
  98. struct afile filestk[FILESIZ];
  99. struct afile *curfile;    /* the current file.  Initially = &filestk[-1]    */
  100. #else
  101. extern struct afile filestk[];
  102. extern struct afile *curfile;
  103. #endif
  104.  
  105. /*
  106.  * ISTKSIZ is the size of the input/pushback stack.
  107.  *  It contains up to one block of data for each pending file plus
  108.  *  one pending token.
  109.  * The input stack grows down from istk[ISTKSIZ - 1].
  110.  *
  111.  * Nxtin points to the next char to read from istk[].
  112.  * Characters are popped from the stack by nxtc()
  113.  * and are pushed back on the stack by unc() and
  114.  * pushmac().
  115.  */
  116.  
  117. # define ISTKSIZ (FILESIZ * BSIZE + PENDSIZ)
  118. #ifdef VARS
  119. char istk[ISTKSIZ];
  120. char *nxtin;
  121. #else
  122. extern char istk[];
  123. extern char *nxtin;
  124. #endif
  125. extern char nxtc();
  126. extern char *pushmac();
  127. #define unc(c) (nxtin-- < &istk[0] ? over() : (*nxtin = c))
  128.  
  129. /*
  130.  * ATTN appears in the input stack to notify nxtc() of some condition,
  131.  *  in the output queue to notify dispose() or outpend() of some condition,
  132.  *  or in the value of a macro to notify gintok() of some condition.
  133.  * ATTN means that the next byte contains a control code.
  134.  * Input control codes are:
  135.  *  AT_EPUSH    - end of pushed-back data.  what follows has not been
  136.  *         scanned before.
  137.  *  AT_EBLK    - end of block.  read another block from the current file.
  138.  * Output control codes are:
  139.  *  AT_OUTOFF    - disable further output.
  140.  *  AT_OUTON    - enable output.
  141.  * Macro value control codes are formal parameter numbers and are not defined.
  142.  *
  143.  * note: to avoid breaking string operations and newline recognition,
  144.  *  do not add an ATTN control code which has a value of '\0', '\\', or '\n'.
  145.  */
  146.  
  147. #define ATTN        '\376'    /* this char must not appear in any file */
  148. #define AT_EPUSH    '\001'
  149. #define AT_EBLK        '\002'
  150.  
  151. #define AT_OUTOFF    '\006'
  152. #define AT_OUTON    '\007'
  153.  
  154. /*
  155.  * Ninterp - number of interpretations.  Incremented each time
  156.  *  gintok() interprets a macro.  Since there is no
  157.  *  overflow detection, ninterp can be used only to
  158.  *  see if some interpretation took place -- not to
  159.  *  count the interpretations (e.g. "oldnint != ninterp"
  160.  *  works, but "cnt = ninterp - oldnint" may fail).
  161.  * Used in conjunction with af_raw to prevent
  162.  *  interpretation of  #if's which are always true
  163.  *  or false without any macro interpretation (e.g.
  164.  *  "#if '\377' > 0").
  165.  */
  166.  
  167. #ifdef VARS
  168. int ninterp;
  169. #else
  170. extern int ninterp;
  171. #endif
  172.  
  173. /*
  174.  * Falsecnt - number of currently false #if's;
  175.  * Hidecnt  - current number of uninterpreted #include's.
  176.  * Collectively, these variables are used to determine when
  177.  *  to enable or disable output.
  178.  */
  179.  
  180. #ifdef VARS
  181. int falsecnt;
  182. int hidecnt;
  183. #else
  184. extern int falsecnt;
  185. extern int hidecnt;
  186. #endif
  187.  
  188. /*
  189.  * ifstk[] contains flags describing the state of all currently active #if's.
  190.  * curif points to the currently active #if within the stack.
  191.  * The stack grows upward, starting at ifstk[-1].
  192.  */
  193.  
  194. #define IF_INIF        '\001'    /* "in the 'if' clause rather than 'else'" */
  195. #define IF_TRUE        '\002'    /* "this if is currently true"           */
  196. #define IF_FALSE    '\004'    /* "this if is currently false"           */
  197.     /* uninterpreted #if statements are neither true nor false.       */
  198. #define IFSIZ    100        /* maximum number of nested #if's       */
  199. #ifdef VARS
  200. char ifstk[IFSIZ];
  201. char *curif;
  202. #else
  203. extern char ifstk[];
  204. extern char *curif;
  205. #endif
  206.  
  207. /*
  208.  * expparse - "currently parsing a #if expression".
  209.  *  Used to prevent interpretation of the macro "defined()" outside
  210.  *  #if expressions.
  211.  */
  212.  
  213. #ifdef VARS
  214. int expparse;
  215. #else
  216. extern int expparse;
  217. #endif
  218.  
  219. /*
  220.  * the next set of definitions are values of parameters to pushfile().
  221.  *  PF_NOLOOK    - the filename was given on the command line.  Don't
  222.  *         search any directories for it.
  223.  *  PF_NODOT    - the include filename was enclosed in '<' and '>'.
  224.  *         Do not search the current directory (dot) for the it.
  225.  *  PF_DOT    - the include filename was enclosed in double-quotes.
  226.  *
  227.  *  PF_HIDE    - the file is not to be interpreted (I.e. is an include file).
  228.  *         Do not output anything while processing this file.
  229.  *  PF_NOHIDE    - the file is to be interpreted.
  230.  */
  231.  
  232. # define PF_NOLOOK    (-1)
  233. # define PF_NODOT    0
  234. # define PF_DOT        1
  235.  
  236. # define PF_HIDE    TRUE
  237. # define PF_NOHIDE    FALSE
  238.  
  239. /*
  240.  * savcom - "save comments and whitespace"
  241.  *  If false, comments and leading and trailing whitespace are removed
  242.  *   from interpreted macro definitions.
  243.  */
  244.  
  245. #ifdef VARS
  246. int savcom;
  247. #else
  248. extern int savcom;
  249. #endif
  250.  
  251. /*
  252.  * catlist - the list of files to process; I.E. the filenames from
  253.  *  the command line.  A zero pointer marks the end of the list.
  254.  * nxtfile - points to the next element of catlist[] to be processed.
  255.  */
  256.  
  257. # define CLSIZ        100
  258. #ifdef VARS
  259. char *catlist[CLSIZ];
  260. char **nxtfile;
  261. #else
  262. extern char *catlist[];
  263. extern char **nxtfile;
  264. #endif
  265.  
  266. /*
  267.  * dirlist - the list of directories to search for an include file.
  268.  *  I.E. all the -I directories from the command line + /usr/include.
  269.  *  (the search of the current directory of the file is handled separately.)
  270.  *  A zero pointer marks the end of the list.
  271.  */
  272.  
  273. #define DLSIZ        100
  274. #ifdef VARS
  275. char *dirlist[DLSIZ];
  276. #else
  277. extern char *dirlist[];
  278. #endif
  279.  
  280. /*
  281.  * The symbol table.  All macros are stored in this table.
  282.  */
  283.  
  284. struct amacro {
  285.     char *am_name;    /*
  286.              * the name of this macro (dynamically allocated).
  287.              * An am_name value of 0 means this slot is empty.
  288.              * All macros to be interpreted are allocated slots
  289.              * before any files are scanned.  #define and #undef
  290.              * do not allocate or free symbol-table slots.
  291.              */
  292.     int am_npar;    /* number of parameters.  -1 == no parameters.    */
  293.     char *am_val;    /*
  294.              * the value (replacement text) of the macro.
  295.              * (dynamically allocated.)
  296.              * An am_val value of 0 means that this macro is not
  297.              * currently defined.
  298.              *
  299.              * am_val points to the null-terminator of the
  300.              * replacement text.  The replacement text is to be
  301.              * read backwards from (am_val - 1) until a null-
  302.              * terminator is found at the other end.
  303.              * An ATTN byte followed (well, preceeded if scanning
  304.              *  forward) by a one-byte integer parameter number
  305.              *  is replaced when expanding this macro by the
  306.              *  corresponding actual parameter.
  307.              * To avoid breaking string operations on val strings,
  308.              * parameter numbers begin at 1 rather than 0
  309.              *
  310.              * A visual example may help:
  311.              *   #define goop(name) hello there name people
  312.              *  results in a sym[] slot containing:
  313.              *
  314.              *  am_name:-------------|
  315.              *             V
  316.              *             goop\0
  317.              *  am_npar: 1
  318.              *  am_val:----------------------------|
  319.              *                       V
  320.              *    \0hello there <1><ATTN> people\0
  321.              */
  322. };
  323.  
  324. #define SYMSIZ    1001
  325. #ifdef VARS
  326. struct amacro sym[SYMSIZ];
  327. #else
  328. extern struct amacro sym[];
  329. #endif
  330.  
  331. extern struct amacro *findmac();
  332. extern char *savtok();
  333. extern int gintok();
  334. extern int gtok();
  335.  
  336. /*
  337.  * magicval - This (uninitialized) character is used to
  338.  *  recognize special macro's (e.g. "defined()").
  339.  * An am_val field of &magicval marks a macro
  340.  *  as special -- it cannot be undef'ed or redefined,
  341.  *  and macro expansion in gintok() recognizes it.
  342.  */
  343.  
  344. #ifdef VARS
  345. char magicval;
  346. #else
  347. extern char magicval;
  348. #endif
  349.  
  350. #define MAXPARMS 40    /* max number of formal parameters to a macro    */
  351.  
  352. /*
  353.  * the keyword structure - one of these describes each preprocessor keyword.
  354.  * see ctrl.c for the keyword array, key[].
  355.  */
  356.  
  357. struct akeyword {
  358.     char *ak_name;        /* name of this keyword (used to set ak_sym) */
  359.     int (*ak_proc)();    /* procedure to interpret this directive     */
  360.     struct amacro *ak_sym;    /*
  361.                  * pointer to the symbol table slot for this
  362.                  * keyword.  Used to recognise the keyword.
  363.                  * All keywords in this list are effectively
  364.                  * "-M"ed when scpp is invoked.  They are
  365.                  * never defined.
  366.                  *   This field is initialized at runtime.
  367.                  */
  368. };
  369. extern struct akeyword *findkey();
  370. extern char *strcpy();
  371.