home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk440.lzh / DMake / var.c < prev    next >
C/C++ Source or Header  |  1991-01-19  |  7KB  |  323 lines

  1.  
  2. /*
  3.  *  VAR.C
  4.  *
  5.  *  Variables and Macros
  6.  */
  7.  
  8. #include "defs.h"
  9.  
  10. #define MACNODE struct _MACNODE
  11.  
  12. MACNODE {
  13.     MNODE Node;
  14.     char *Name;     /*    variable name    */
  15.     char *MacName;  /*    replace with..    */
  16.     short MacLen;
  17.     short NameLen;
  18.     short NormalEntry;
  19. };
  20.  
  21.  
  22. MLIST    MacList = { (MNODE *)&MacList.mlh_Tail, NULL, (MNODE *)&MacList.mlh_Head };
  23. MLIST    ColList = { (MNODE *)&ColList.mlh_Tail, NULL, (MNODE *)&ColList.mlh_Head };
  24.  
  25. /*
  26.  *  var points to begining, str points to the '='
  27.  */
  28.  
  29. void
  30. MacroAssign(var, str)
  31. char *var, *str;
  32. {
  33.     MACNODE *node = malloc(sizeof(MACNODE));
  34.  
  35.     while (str != var && (*str == '=' || *str == ' ' || *str == '\t'))
  36.     --str;
  37.     ++str;
  38.     node->NameLen = str - var;
  39.     node->Name = malloc(node->NameLen + 1);
  40.     movmem(var, node->Name, node->NameLen);
  41.     node->Name[node->NameLen] = 0;
  42.     while (*str && *str != '=')
  43.     ++str;
  44.     if (*str == '=')
  45.     ++str;
  46.     while (*str && (*str == ' ' || *str == '\t'))
  47.     ++str;
  48.     node->MacLen = strlen(str);
  49.     node->MacName = malloc(node->MacLen + 1);
  50.     strcpy(node->MacName, str);
  51.     AddHead(&MacList, node);
  52. }
  53.  
  54. MacroReplace(spec, buf)
  55. register char *buf;
  56. {
  57.     char *base = buf;
  58.     char cext;
  59.     short error = 8192;
  60.  
  61.     while (*buf && --error) {
  62.     register short i;
  63.     char *found = NULL;
  64.     short falloced = 0;
  65.  
  66.     if ((buf[0] != '$' && buf[0] != '%') || buf[1] != '(') {
  67.         ++buf;
  68.         continue;
  69.     }
  70.     for (i = 2; buf[i] && buf[i] != ')' && buf[i] != ':'; ++i) {
  71.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  72.         MacroReplace(spec, buf + i);
  73.     }
  74.     if (buf[i] != ')' && buf[i] != ':') {
  75.         ++buf;
  76.         continue;
  77.     }
  78.     cext = (buf[i] == ':');
  79.     {
  80.         register MACNODE *node;
  81.         register short len = i - 2;
  82.         char *search = buf + 2;
  83.  
  84.         /*
  85.          *    Check normal macro variables
  86.          */
  87.  
  88.         for (node = GetHead(&MacList); node; node = GetSucc(node)) {
  89.         if (node->NameLen == len && strncmp(node->Name, search, len) == 0) {
  90.             found = node->MacName;
  91.             break;
  92.         }
  93.         }
  94. percind:
  95.         if (found && buf[0] == '%') {
  96.         search = found;
  97.         len = strlen(found);
  98.         if (falloced) {
  99.             free(found);
  100.             falloced = 0;
  101.         }
  102.         node = NULL;
  103.         found = NULL;
  104.         }
  105.  
  106.         /*
  107.          *    Check collections
  108.          */
  109.  
  110.         if (!found && spec) {
  111.         for (node = GetHead(&ColList); node; node = GetSucc(node)) {
  112.             if (node->NameLen == len && strncmp(node->Name, search, len) == 0) {
  113.             found = node->MacName;
  114.             break;
  115.             }
  116.         }
  117.         }
  118.  
  119.         /*
  120.          *    Still nothing!    If it is a wildcard, try to find a match
  121.          *    with item(s) on the collection list.  Else check the
  122.          *    enviroment.
  123.          */
  124.  
  125.         if (!found) {
  126.         char c = search[len];
  127.         search[len] = 0;
  128.         if (IsWildCard(search)) {
  129.             for (node = GetHead(&ColList); node; node = GetSucc(node)) {
  130.             if (node->NormalEntry && WildCmp(search, node->MacName)) {
  131.                 if (found) {
  132.                 char *new = malloc(strlen(found) + strlen(node->MacName) + 2);
  133.                 strcpy(new, found);
  134.                 strcat(new, " ");
  135.                 strcat(new, node->MacName);
  136.                 if (falloced)
  137.                     free(found);
  138.                 found = new;
  139.                 falloced = 1;
  140.                 } else {
  141.                 found = node->MacName;
  142.                 }
  143.             }
  144.             }
  145.         } else {
  146.             found = NULL;
  147.             /*
  148.             mountrequest(0);
  149.             if (found = GetDEnv(search))
  150.             falloced = 1;
  151.             mountrequest(1);
  152.             */
  153.         }
  154.         search[len] = c;
  155.         if (found)
  156.             goto percind;
  157.         }
  158.     }
  159.     if (found && cext) {    /*  found the string & extension    */
  160.         char *w1, *w2;
  161.         char *xbuf = malloc(256);
  162.         char *res;
  163.         char *rstr = malloc(1);
  164.         register char *ptr;
  165.         register short len;
  166.  
  167.         rstr[0] = 0;
  168.         ++i;
  169.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  170.         MacroReplace(spec, buf + i);
  171.         if (buf[i] != '\"') {
  172.         printf("Quote missing: %s\n", buf);
  173.         xexit(30);
  174.         }
  175.         ++i;
  176.         w1 = buf + i;
  177.         while (buf[i] && buf[i] != '\"') {
  178.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  179.             MacroReplace(spec, buf + i);
  180.         else
  181.             ++i;
  182.         }
  183.         if (buf[i] != '\"') {
  184.         printf("Quote missing: %s\n", buf);
  185.         xexit(30);
  186.         }
  187.         buf[i++] = 0;
  188.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  189.         MacroReplace(spec, buf + i);
  190.         if (buf[i++] != ':') {
  191.         printf("Colon missing: %s\n", buf);
  192.         xexit(30);
  193.         }
  194.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  195.         MacroReplace(spec, buf + i);
  196.         if (buf[i++] != '\"') {
  197.         puts("QM");
  198.         xexit(30);
  199.         }
  200.         w2 = buf + i;
  201.         while (buf[i] && buf[i] != '\"') {
  202.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  203.             MacroReplace(spec, buf + i);
  204.         else
  205.             ++i;
  206.         }
  207.         if (buf[i] != '\"') {
  208.         puts("QM2");
  209.         xexit(30);
  210.         }
  211.         buf[i++] = 0;
  212.         if ((buf[i] == '$' || buf[i] == '%') && buf[i+1] == '(')
  213.         MacroReplace(spec, buf + i);
  214.         if (buf[i] != ')') {
  215.         puts("QM3");
  216.         xexit(30);
  217.         }
  218.         /*
  219.          *    Now parse found string and call VirtuoExpand(w2, w1, file)
  220.          */
  221.  
  222.         for (ptr = found; *ptr;) {
  223.         while (*ptr == ' ' || *ptr == '\t')
  224.             ++ptr;
  225.         if (!ptr[0])
  226.             break;
  227.         for (len = 0; ptr[len] && ptr[len] != ' ' && ptr[len] != '\t'; ++len);
  228.         movmem(ptr, xbuf, len);
  229.         ptr += len;
  230.         xbuf[len] = 0;
  231.         /*
  232.         printf("VEXT '%s' '%s' '%s'\n", w2, w1, xbuf);
  233.         */
  234.         if (res = VirtuoExpand(w2, w1, xbuf)) {
  235.             char *tmp = malloc(strlen(rstr) + strlen(res) + 2);
  236.             strcpy(tmp, rstr);
  237.             if (tmp[0])
  238.             strcat(tmp, " ");
  239.             strcat(tmp, res);
  240.             free(rstr);
  241.             rstr = tmp;
  242.         }
  243.         }
  244.         if (falloced)
  245.         free(found);
  246.         found = rstr;
  247.         falloced = 1;
  248.         free(xbuf);
  249.     }
  250.  
  251.     /*
  252.      *  replace the area buf + 0; < buf + i + 1    (len i + 1)
  253.      *  with 'found'
  254.      */
  255.  
  256.     if (found) {
  257.         register short flen = strlen(found);
  258.         register short len = i + 1;
  259.  
  260.         movmem(buf + len, buf + flen, strlen(buf + len) + 1);
  261.         movmem(found, buf, flen);
  262.     } else {
  263.         buf += i + 1;
  264.     }
  265.     if (falloced)
  266.         free(found);
  267.     }
  268.     return(buf - base);
  269. }
  270.  
  271. ResetVarCollector()
  272. {
  273.     register MACNODE *node;
  274.  
  275. #ifdef DEBUG2
  276.     puts("RESETVARCOL");
  277. #endif
  278.     while (node = RemHead(&ColList)) {
  279.     free(node->Name);
  280.     free(node->MacName);
  281.     free(node);
  282.     }
  283. }
  284.  
  285. AddVarCollector(str, basestr, normal)
  286. char *str, *basestr;
  287. {
  288.     register MACNODE *node;
  289.     register char *ptr;
  290.     short newlen = strlen(str);
  291.  
  292. #ifdef DEBUG2
  293.     printf("ADDVARCOLLECTOR: %s += %s\n", basestr, str);
  294. #endif
  295.  
  296.     for (node = GetHead(&ColList); node; node = GetSucc(node)) {
  297.     if (strcmp(node->Name, basestr) == 0)
  298.         break;
  299.     }
  300.     if (!node) {
  301.     node = malloc(sizeof(MACNODE));
  302.     node->NameLen = strlen(basestr);
  303.     node->Name = malloc(node->NameLen+1);
  304.     strcpy(node->Name, basestr);
  305.     node->MacLen = 0;
  306.     node->MacName = malloc(1);
  307.     node->MacName[0] = 0;
  308.     AddTail(&ColList, node);
  309.     }
  310.     node->NormalEntry = normal;
  311.     ptr = malloc(node->MacLen + newlen + 2);
  312.     strcpy(ptr, node->MacName);
  313.     if (node->MacLen)
  314.     strcat(ptr, " ");
  315.     strcat(ptr, str);
  316.     free(node->MacName);
  317.     node->MacName = ptr;
  318.     node->MacLen = strlen(ptr);
  319. }
  320.  
  321.  
  322.  
  323.