home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / DMAKE38A.ZIP / FUNCTION.C < prev    next >
C/C++ Source or Header  |  1992-01-23  |  9KB  |  357 lines

  1. /* RCS      -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/function.c,v 1.1 1992/01/24 03:27:00 dvadura Exp $
  2. -- SYNOPSIS -- GNU style functions for dmake.
  3. -- 
  4. -- DESCRIPTION
  5. --     All GNU stule functions understood by dmake are implemented in this
  6. --    file.  Currently the only such function is $(mktmp ...) which is
  7. --    not part of GNU-make is an extension provided by dmake.
  8. --
  9. -- AUTHOR
  10. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  11. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  12. --
  13. -- COPYRIGHT
  14. --      Copyright (c) 1990 by Dennis Vadura.  All rights reserved.
  15. -- 
  16. --      This program is free software; you can redistribute it and/or
  17. --      modify it under the terms of the GNU General Public License
  18. --      (version 1), as published by the Free Software Foundation, and
  19. --      found in the file 'LICENSE' included with this distribution.
  20. -- 
  21. --      This program is distributed in the hope that it will be useful,
  22. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  23. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. --      GNU General Public License for more details.
  25. -- 
  26. --      You should have received a copy of the GNU General Public License
  27. --      along with this program;  if not, write to the Free Software
  28. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. --
  30. -- LOG
  31. --     $Log: function.c,v $
  32.  * Revision 1.1  1992/01/24  03:27:00  dvadura
  33.  * dmake Version 3.8, Initial revision
  34.  *
  35. */
  36.  
  37. #include "extern.h"
  38.  
  39. static char *_exec_mktmp  ANSI((char *, char *, char *));
  40. static char *_exec_subst  ANSI((char *, char *, char *));
  41. static char *_exec_iseq   ANSI((char *, char *, char *, int));
  42. static char *_exec_sort   ANSI((char *));
  43. static char *_exec_shell  ANSI((char *));
  44. static int   _mystrcmp    ANSI((CONST PVOID, CONST PVOID));
  45.  
  46.  
  47. PUBLIC char *
  48. Exec_function(buf)/*
  49. ====================
  50.    Execute the function given by the value of args.
  51.  
  52.    So far mktmp is the only valid function, anything else elicits and error
  53.    message.  It is my hope to support the GNU style functions in this portion
  54.    of the code at some time in the future. */
  55. char *buf;
  56. {
  57.    char *fname;
  58.    char *args;
  59.    char *mod1;
  60.    char *mod2 = NIL(char);
  61.    char *res  = NIL(char);
  62.  
  63.    /* This must succeed since the presence of ' ', \t or \n is what
  64.     * determines if this functions is called in the first place. */
  65.    fname = _substr(buf, args=_strpbrk(buf," \t\n"));
  66.  
  67.    if( (mod1 = strchr(fname,',')) != NIL(char) ){
  68.       *mod1 = '\0';
  69.       mod1++;
  70.  
  71.       if( (mod2 = strchr(mod1,',')) != NIL(char) ){
  72.      *mod2 = '\0';
  73.      mod2++;
  74.       }
  75.    }
  76.  
  77.    switch( *fname ) {
  78.       case 'e':
  79.      if(strncmp(fname,"eq",2) == 0) res = _exec_iseq(mod1,mod2,args,TRUE);
  80.      break;
  81.  
  82.       case 'm':
  83.      if( strncmp(fname,"mktmp", 5) == 0 ) res = _exec_mktmp(mod1,mod2,args);
  84.      break;
  85.  
  86.       case 'n':
  87.      if( strncmp(fname,"null", 4) == 0 )
  88.         res = _exec_iseq(mod1,NIL(char),args,TRUE);
  89.      break;
  90.  
  91.       case '!':
  92.      if(strncmp(fname,"!null",5) == 0)
  93.         res = _exec_iseq(mod1,NIL(char),args,FALSE);
  94.      if(strncmp(fname,"!eq",3) == 0) res = _exec_iseq(mod1,mod2,args,FALSE);
  95.      break;
  96.  
  97.       case 's':
  98.      if(strncmp(fname,"sort",4) == 0) res = _exec_sort(args);
  99.      else if(strncmp(fname,"shell",5)==0) res = _exec_shell(args);
  100.      else if(strncmp(fname,"strip",5)==0) res = Tokenize(Expand(args)," ");
  101.      else if(strncmp(fname,"subst",5)==0) res = _exec_subst(mod1,mod2,args);
  102.      break;
  103.  
  104.       default:
  105.      Warning( "Function '%s' not implemented at this time", fname );
  106.    }
  107.  
  108.    if( res == NIL(char) ) res = _strdup("");
  109.  
  110.    FREE(fname);
  111.    return(res);
  112. }
  113.  
  114.  
  115. static char *
  116. _exec_mktmp( file, text, data )
  117. char *file;
  118. char *text;
  119. char *data;
  120. {
  121.    register char *p;
  122.    char *tmpname;
  123.    char *name;
  124.    FILE *tmpfile = NIL(FILE);
  125.  
  126.    /* This is only a test of the recipe line so prevent the tempfile side
  127.     * effects. */
  128.    if( Suppress_temp_file ) return(NIL(char));
  129.  
  130.    name = Current_target ? Current_target->CE_NAME:"makefile text";
  131.  
  132.    if( file && *file ) {
  133.       char *newtmp;
  134.  
  135.       /* This call to Get_temp sets TMPFILE for subsequent expansion of file.
  136.        * DO NOT DELETE IT! */
  137.       Get_temp( &newtmp, "", FALSE ); FREE(newtmp);
  138.       tmpname = Expand(file);
  139.  
  140.       if( *tmpname ) {
  141.      if( (tmpfile = fopen(tmpname, "w")) == NIL(FILE) )
  142.         Open_temp_error( tmpname, name );
  143.  
  144.      Def_macro("TMPFILE", tmpname, M_EXPANDED|M_MULTI);
  145.      Link_temp( Current_target, tmpfile, tmpname );
  146.       }
  147.       else
  148.      FREE(tmpname);
  149.    }
  150.  
  151.    if( !tmpfile )
  152.       tmpfile = Start_temp( "", Current_target, &tmpname );
  153.  
  154.    if( !text || !*text ) text = tmpname;
  155.    data = Expand(_strspn(data, " \t\n"));
  156.  
  157.    for(p=strchr(data,'\n'); p; p=strchr(p,'\n')) {
  158.       char *q = _strspn(++p," \t");
  159.       strcpy(p,q);
  160.    }
  161.  
  162.    Append_line( data, FALSE, tmpfile, name, FALSE, TRUE );
  163.    Close_temp( Current_target, tmpfile );
  164.    FREE(data);
  165.  
  166.    return( Expand(text) );
  167. }
  168.  
  169.  
  170. static char *
  171. _exec_iseq( lhs, rhs, data, eq )
  172. char *lhs;
  173. char *rhs;
  174. char *data;
  175. int  eq;
  176. {
  177.    char *l = Expand(lhs);
  178.    char *r = Expand(rhs);
  179.    char *i = _strspn(data, " \t\n");
  180.    char *e = strchr(i, ' ');
  181.    char *res = NIL(char);
  182.    int  val = strcmp(l,r);
  183.  
  184.    if( (!val && eq) || (val && !eq) ) {
  185.       if( e != NIL(char) ) *e = '\0';
  186.       res = Expand(i);
  187.    }
  188.    else if( e != NIL(char) ) {
  189.       e = _strspn(e," \t\n");
  190.       if( *e ) res = Expand(e);
  191.    }
  192.  
  193.    FREE(l);
  194.    FREE(r);
  195.    return(res);
  196. }
  197.  
  198.  
  199. static char *
  200. _exec_sort( args )
  201. char *args;
  202. {
  203.    char *res  = NIL(char);
  204.    char *data = Expand(args);
  205.    char **tokens = NIL(char *);
  206.    char *p;
  207.    char *white = " \t\n";
  208.    int  j;
  209.    int  i = 0;
  210.  
  211.    for( i=0,p=_strspn(data,white); *p; p=_strspn(_strpbrk(p,white),white),i++); 
  212.  
  213.    if( i != 0 ) {
  214.       TALLOC(tokens, i, char *);
  215.  
  216.       for( i=0,p=_strspn(data,white); *p; p=_strspn(p,white),i++){
  217.      tokens[i] = p;
  218.      p = _strpbrk(p,white);
  219.      if( *p ) *p++ = '\0';
  220.       }
  221.  
  222.       qsort( tokens, i, sizeof(char *), _mystrcmp );
  223.  
  224.       for( j=0; j<i; j++ ) res = _strapp(res, tokens[j]);
  225.       FREE(data);
  226.       FREE(tokens);
  227.    }
  228.  
  229.    return(res);
  230. }
  231.  
  232.  
  233. static int
  234. _mystrcmp( p, q )
  235. CONST PVOID p;
  236. CONST PVOID q;
  237. {
  238.    return(strcmp(*((CONST char **)p),*((CONST char **)q)));
  239. }
  240.  
  241.  
  242. static char *
  243. _exec_subst( pat, subst, data )
  244. char *pat;
  245. char *subst;
  246. char *data;
  247. {
  248.    char *res;
  249.  
  250.    pat = Expand(pat);
  251.    subst = Expand(subst);
  252.    res = Apply_edit( Expand(data), pat, subst, TRUE, FALSE );
  253.    FREE(pat);
  254.    FREE(subst);
  255.  
  256.    return(res);
  257. }
  258.  
  259.  
  260. static char *
  261. _exec_shell( data )
  262. char *data;
  263. {
  264.    extern char *tempnam();
  265.    static int  nestlevel = 0;
  266.    static int  org_out;
  267.    static int  bsize;
  268.    static char *buffer;
  269.    static char *tmpnm;
  270.    static FILE *tmp;
  271.  
  272.    int wait     = Wait_for_completion;
  273.    uint16 vflag = Verbose;
  274.    int tflag    = Trace;
  275.    char *res    = NIL(char);
  276.    CELL cell;
  277.    STRING rcp;
  278.    HASH   cname;
  279.  
  280.    if( Suppress_temp_file ) return(NIL(char));
  281.  
  282.    /* Set the temp CELL used for building prerequisite candidates to
  283.     * all zero so that we don't have to keep initializing all the
  284.     * fields. */
  285.    {
  286.       register char *s = (char *) &cell;
  287.       register int   n = sizeof(CELL);
  288.       while( n ) { *s++ = '\0'; n--; }
  289.    }
  290.    rcp.st_string  = _strspn(data, " \t+-%@");
  291.    rcp.st_attr    = Rcp_attribute( data );
  292.    rcp.st_next    = NIL(STRING);
  293.    cname.ht_name  = "Shell escape";
  294.    cell.ce_name   = &cname;
  295.    cell.ce_fname  = cname.ht_name;
  296.    cell.ce_recipe = &rcp;
  297.    cell.ce_flag   = F_TARGET|F_RULES;
  298.    cell.ce_attr   = A_PHONY|A_SILENT;
  299.  
  300.    if( nestlevel == 0 ) {
  301.       tmpnm   = tempnam(NIL(char),"mk");
  302.       org_out = dup(1);
  303.  
  304.       if( (tmp = fopen(tmpnm, "w+")) == NIL(FILE) )
  305.      Open_temp_error( tmpnm, cname.ht_name );
  306.  
  307.       close(1);
  308.       dup( fileno(tmp) );
  309.  
  310.       bsize  = (Buffer_size < BUFSIZ)?BUFSIZ:Buffer_size;
  311.       buffer = MALLOC(bsize,char);
  312.    }
  313.  
  314.    Wait_for_completion = TRUE;
  315.    Verbose = V_NONE;
  316.    Trace   = FALSE;
  317.    nestlevel++;
  318.    Exec_commands( &cell );
  319.    nestlevel--;
  320.    Trace   = tflag;
  321.    Verbose = vflag;
  322.    Wait_for_completion = wait;
  323.  
  324.    /* Now we have to read the temporary file, get the tokens and return them
  325.     * as a string. */
  326.    rewind(tmp);
  327.    while( fgets(buffer, bsize, tmp) ) {
  328.       char *p = strchr(buffer, '\n');
  329.  
  330.       if( p == NIL(char) )
  331.      res = _strjoin(res,buffer,-1,TRUE);
  332.       else {
  333.      *p = '\0';
  334.          res = _strapp(res,buffer);
  335.       }
  336.    }
  337.  
  338.    fclose(tmp);
  339.    if( nestlevel == 0 ) {
  340.       Remove_file(tmpnm);
  341.       close(1);
  342.       dup(org_out);
  343.       close(org_out);
  344.       FREE(tmpnm);
  345.       FREE(buffer);
  346.    }
  347.    else {
  348.       if( (tmp = fopen(tmpnm, "w+")) == NIL(FILE) )
  349.      Open_temp_error( tmpnm, cname.ht_name );
  350.  
  351.       close(1);
  352.       dup( fileno(tmp) );
  353.    }
  354.  
  355.    return(res);
  356. }
  357.