home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8712 / mkmf / 2 / src / macro.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  4.6 KB  |  203 lines

  1. /* $Header: macro.c,v 1.3 85/05/02 07:53:52 nicklin Exp $ */
  2.  
  3. /*
  4.  * Author: Peter J. Nicklin
  5.  */
  6. #include <ctype.h>
  7. #include <stdio.h>
  8. #include "Mkmf.h"
  9. #include "hash.h"
  10. #include "macro.h"
  11. #include "null.h"
  12. #include "slist.h"
  13. #include "yesno.h"
  14.  
  15. extern char IOBUF[];            /* I/O buffer line */
  16.  
  17. /*
  18.  * findmacro() searchs a line for a macro definition. Returns the name,
  19.  * or null if not found.
  20.  */
  21. char *
  22. findmacro(macroname, bp)
  23.     char *macroname;        /* macro name receiving buffer */
  24.     register char *bp;        /* buffer pointer */
  25. {
  26.     register char *mp;        /* macro name pointer */
  27.  
  28.     mp = macroname;
  29.     while(isalnum(*bp))
  30.         *mp++ = *bp++;
  31.     *mp = '\0';
  32.     while(*bp == ' ' || *bp == '\t')
  33.         bp++;
  34.     if (*bp != '=')
  35.         return(NULL);
  36.     return(macroname);
  37. }
  38.     
  39.  
  40.  
  41. /*
  42.  * getmacro() loads the body of a macro definition into mdefbuf and returns
  43.  * a pointer to mdefbuf. If the macro definition continues on more than
  44.  * one line further lines are fetched from the input stream.
  45.  */
  46. char *
  47. getmacro(mdefbuf, stream)
  48.     char *mdefbuf;            /* receiving macro definition buffer */
  49.     FILE *stream;            /* input stream */
  50. {
  51.     extern short CONTINUE;        /* does the line continue? */
  52.     char *getlin();            /* get a line from input stream */
  53.     register char *bp;        /* buffer pointer */
  54.     register char *mp;        /* macro definition buffer pointer */
  55.  
  56.     bp = IOBUF;
  57.     mp = mdefbuf;
  58.     while (*bp++ != '=')
  59.         continue;
  60.     if (WHITESPACE(*bp))
  61.         bp++;
  62.     while (*bp != '\0')
  63.         *mp++ = *bp++;
  64.     while (CONTINUE == YES)
  65.         {
  66.         *mp++ = ' ';
  67.         if (getlin(stream) == NULL)
  68.             break;
  69.         bp = IOBUF;
  70.         while (*bp != '\0')
  71.             *mp++ = *bp++;
  72.         }
  73.     *mp = '\0';
  74.     return(mdefbuf);
  75. }
  76.  
  77.  
  78.  
  79. /*
  80.  * putmacro() prints a macro definition from the macro definition table.
  81.  */
  82. void
  83. putmacro(macrovalue, stream)
  84.     char *macrovalue;        /* value of macro definition */
  85.     register FILE *stream;        /* output stream */
  86. {
  87.     register char *iop;        /* IOBUF pointer */
  88.     register int c;            /* current character */
  89.  
  90.     iop = IOBUF;
  91.     while ((c = *iop++) != '=')
  92.         putc(c, stream);
  93.     fprintf(stream, "= %s\n", macrovalue);
  94. }
  95.  
  96.  
  97.  
  98. /*
  99.  * putobjmacro() derives and prints object file names from the SRCLIST list.
  100.  */
  101. void
  102. putobjmacro(stream)
  103.     register FILE *stream;        /* output stream */
  104. {
  105.     extern SLIST *SRCLIST;        /* source file name list */
  106.     register char *iop;        /* IOBUF pointer */
  107.     register int c;            /* current character */
  108.     char *rindex();            /* find last occurrence of character */
  109.     char *suffix;            /* suffix pointer */
  110.     HASHBLK *lookupinclude();    /* look up include name in hash table */
  111.     int cnt = 0;            /* number of object filenames printed */
  112.     int lookuptypeofinclude();    /* look up the brand of include */
  113.     int type;            /* file type */
  114.     SLBLK *lbp;            /* list block pointer */
  115.     void putobj();            /* print object file name */
  116.  
  117.     iop = IOBUF;
  118.     while ((c = *iop++) != '=')
  119.         putc(c, stream);
  120.     putc('=', stream);
  121.     for (lbp = SRCLIST->head; lbp != NULL; lbp = lbp->next)
  122.         {
  123.         suffix = rindex(lbp->key, '.');
  124.         type = lookuptypeofinclude(++suffix);
  125.         if (lookupinclude(lbp->key, type) == NULL)
  126.             {
  127.             cnt += 1;
  128.             if (cnt == 1)
  129.                 {
  130.                 putc(' ', stream);
  131.                 putobj(lbp->key, stream);
  132.                 }
  133.             else    {
  134.                 fprintf(stream, " \\\n\t\t");
  135.                 putobj(lbp->key, stream);
  136.                 }
  137.             }
  138.         }
  139.     putc('\n', stream);
  140. }
  141.  
  142.  
  143.  
  144. /*
  145.  * putslmacro() copies a macro definition from a list.
  146.  */
  147. void
  148. putslmacro(slist, stream)
  149.     SLIST *slist;            /* singly-linked macro def list */
  150.     register FILE *stream;        /* output stream */
  151. {
  152.     register char *iop;        /* IOBUF pointer */
  153.     register int c;            /* current character */
  154.     SLBLK *lbp;            /* list block pointer */
  155.  
  156.     iop = IOBUF;
  157.     while ((c = *iop++) != '=')
  158.         putc(c, stream);
  159.     putc('=', stream);
  160.     if (SLNUM(slist) > 0)
  161.         {
  162.         lbp = slist->head;
  163.         fprintf(stream, " %s", lbp->key);
  164.         }
  165.     if (SLNUM(slist) > 1)
  166.         for (lbp = lbp->next; lbp != NULL; lbp = lbp->next)
  167.             fprintf(stream, " \\\n\t\t%s", lbp->key);
  168.     putc('\n', stream);
  169. }
  170.  
  171.  
  172.  
  173. /*
  174.  * storemacro() stores a macro definition in the macro definition table.
  175.  * Returns integer YES if a macro definition (macro=definition), otherwise
  176.  * NO. exit(1) is called if out of memory.
  177.  */
  178. storemacro(macdef)
  179.     char *macdef;            /* macro definition string */
  180. {
  181.     extern HASH *MDEFTABLE;        /* macro definition table */
  182.     register int i;            /* macro value index */
  183.     register int j;            /* macro name index */
  184.     HASHBLK *htinstall();        /* install hash table entry */
  185.  
  186.     for (i = 0; macdef[i] != '='; i++)
  187.         if (macdef[i] == '\0')
  188.             return(NO);
  189.     
  190.     /* removing trailing blanks and tabs from end of macro name */
  191.     for (j = i; j > 0; j--)
  192.         if (!WHITESPACE(macdef[j-1]))
  193.             break;
  194.     macdef[j] = '\0';
  195.  
  196.     /* remove leading blanks and tabs from macro value */
  197.     for (i++; WHITESPACE(macdef[i]); i++)
  198.         continue;
  199.     if (htinstall(macdef, macdef+i, VREADWRITE, MDEFTABLE) == NULL)
  200.         exit(1);
  201.     return(YES);
  202. }
  203.