home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tools / make / nmake / makeutil.c < prev    next >
Text File  |  1987-07-21  |  12KB  |  381 lines

  1. /* file makeutil.c */
  2. /* utilities for "make" for ms-dos */
  3. /* K. Mitchum 7/84 */
  4. /* R. Gregg   5/85 */
  5.  
  6. #include <stdio.h>
  7. #include "make.h"
  8.  
  9. #if MSC
  10. #include <ctype.h>
  11. #endif
  12.  
  13. #if LC
  14. extern int _stack;
  15. #endif
  16.  
  17. /* external definitions */
  18. extern struct dta dta;
  19. extern int execute;
  20. extern int firstcc;
  21. char *get_mem(),*mov_in();
  22.  
  23. int longflag;
  24. char buf[INMAX];
  25.  
  26.  
  27. exec_how(cmd)
  28. char *cmd;
  29. {
  30. int pos,this_echo,this_ign,x,i,ccode,no_more_flags;
  31. int long_name;
  32. char cmdname[INMAXSH];
  33.  
  34.     i = pos = 0;
  35.     this_echo = TRUE;
  36.     this_ign = FALSE;
  37.     long_name = FALSE;
  38.     no_more_flags = FALSE;
  39.     while ( TRUE ) {
  40.         while ( isspace(cmd[pos]) )
  41.             pos++;
  42.         switch ( cmd[pos] ) {
  43.         case '@':
  44.             this_echo = FALSE;
  45.             break;
  46.         case '-':
  47.             this_ign = TRUE;
  48.             break;
  49.         case BKSLSH:
  50.             long_name = no_more_flags = TRUE;
  51.             break;
  52.         default: 
  53.             no_more_flags = TRUE;
  54.             break;
  55.         }
  56.         if (no_more_flags) 
  57.             break;
  58.         else 
  59.             pos++;
  60.     }
  61.     for (x=pos; !isspace(cmd[x]) ; x++) 
  62.         cmdname[i++] = cmd[x];
  63.     cmdname[i] = NUL;
  64.     if ( this_echo )
  65.         fprintf(stdout,"\n%s\n",cmd+pos);
  66.     if ( !execute  && !this_echo) {
  67.         fprintf(stdout,"\n%s\n",cmd+pos);
  68.         fprintf(stdout,"Make: \(above line will not echo when tracing off\)\n");
  69.         return(0);
  70.     }
  71.     if ( !execute ) 
  72.         return(0);
  73.     if ( (strcmp(cmdname,"cc") == 0) || (strcmp(cmdname,"CC") == 0) ){
  74.         if ( (ccode = do_cc(cmd+x)) != 0 ) {
  75.             if (longflag) 
  76.                 baddo_sys(buf,ccode);
  77.             else 
  78.                 badsys(buf,ccode);
  79.         }
  80.         if (this_ign) 
  81.             return (0);
  82.         else 
  83.             return(err_code());
  84.     }
  85.     else {
  86. #if MSC
  87.         ccode = do_sys(cmdname,cmd+x,long_name);
  88.         if ( ccode != 0 ) 
  89.             baddo_sys(cmdname,ccode);
  90. #else
  91.         if ( long_name ) {
  92.             ccode = do_sys(cmdname,cmd+x);
  93.             if ( ccode != 0 ) baddo_sys(cmdname,ccode);
  94.         }
  95.         else {
  96.             ccode = system(cmd+pos);
  97.             if ( ccode != 0 ) badsys(cmdname,ccode);
  98.         }
  99. #endif
  100.         if (this_ign) 
  101.             return (0);
  102.         else 
  103.             return(err_code());
  104.      }
  105. }
  106.  
  107. char sh_arg[INMAX];
  108.  
  109. do_cc(arglist)
  110. char *arglist;
  111. {
  112.  
  113. extern struct macrec      *maclist;
  114. static struct macrec      *ccptr;
  115. char                      c_ext[INMAXSH],temp[INMAX];
  116. static char               compilstr[INMAXSH];
  117. static int                numpass;
  118. int                       i,j,k,n,x,stop,extseen;
  119. long                      getmodified();
  120.  
  121.  
  122.  
  123.  
  124.         if (!firstcc){    /* if not first pass through*/
  125.  
  126.             if (numpass > 1){   /* if there is more than 1 pass*/
  127.                 strcpy(sh_arg,arglist);
  128.                 for (i=1;i<numpass +1;i++){
  129.                     sprintf(buf,compilstr,i);
  130. #if MSC
  131.                     if (x = do_sys(buf,sh_arg,longflag) != 0) return(x);
  132. #else
  133.                     if ( longflag ) {
  134.                         if (x = do_sys(buf,sh_arg) != 0) return(x);
  135.                     }
  136.                     else {
  137.                         strcat(buf,sh_arg);
  138.                         if ( x = system(buf) != 0) return(x);
  139.                     }
  140. #endif
  141.                     if (x = err_code() != 0) return(x);
  142.                     if (i==1)stripflag(); /* operates on sh_arg */
  143.                 }
  144.                 return(0);
  145.             }
  146.             else{
  147.                 strcpy(buf,compilstr);
  148. #if MSC
  149.                 if (x = do_sys(buf,arglist,longflag) != 0) return(x);
  150. #else
  151.                 if ( longflag ) {
  152.                     if (x = do_sys(buf,arglist) != 0) return(x);
  153.                 }
  154.                 else if ( !longflag ) {
  155.                     strcat(buf,arglist);
  156.                     if ( x = system(buf) != 0) return(0);
  157.                 }
  158.                 else return(0);
  159. #endif
  160.             }
  161.         }
  162.         /*first pass thru -- assemble the static strings*/
  163.         else{
  164.             /*is cc a defined macro?*/
  165.             ccptr = maclist;
  166.             if (ccptr != NULL){
  167.                 while (TRUE){
  168.                     if (strcmp(ccptr->name,"CC") == 0) break;
  169.                     else if (ccptr->nextmac == NULL) {
  170.                         ccptr = NULL;    /* found no match*/
  171.                         break;
  172.                     }
  173.                     else ccptr = ccptr->nextmac;
  174.                 }
  175.             }
  176.  
  177.             if (ccptr == NULL) {
  178.                 /*use the default strings*/
  179.                 strcpy(compilstr,DEF_CC);
  180.                 numpass = DEF_PASS;
  181.                 longflag = (compilstr[0] == BKSLSH) ? TRUE : FALSE;
  182.  
  183.             }
  184.             else{
  185.                 /*use the defined cc macro*/
  186.                 /*do this by
  187.                     1.adding extension if required,checking for existence, then using the extension supplied by find_first
  188.                     2.adding "%d" to the last printing char before ';' (if needed)
  189.                     3.reading the integer between ';' and EOL (if needed)
  190.                 */
  191.                 k=i=0;
  192.                 stop = extseen = FALSE;
  193.                 expand(ccptr->mexpand,temp,"",NO_TARG);
  194. /*              strcpy(compilstr,"C:");k=2; *//* always looks in the default drive*/
  195.                 while (temp[i]){
  196.                     switch (temp[i]){
  197.                         case ' ':
  198.                         case '\t':
  199.                             i++;break;
  200.                         case BKSLSH:
  201.                             if ( temp[i+1] == ';' ) {
  202.                                 i++;
  203.                                 stop = TRUE;
  204.                                 break;
  205.                             }
  206.                             else {
  207.                                 compilstr[k++] = temp[i++];
  208.                                 break;
  209.                             }
  210.                         case NUL:
  211.                             stop = TRUE;break;                      
  212.                         case '.':
  213.                             extseen = stop = TRUE;break;
  214.                         default:
  215.                             compilstr[k++] = temp[i++];
  216.                             break;
  217.                     }
  218.                     if (stop) break;
  219.                 }
  220.                 /*if we have gotten this far, we are pointing to '.',';' or EOL*/
  221.                 compilstr[k] = NUL;
  222.                 /* assemble the extension */
  223.                 if (extseen){
  224.                     n = 0;
  225.                     while (notnull(temp[i]) && temp[i] != ';')
  226.                         c_ext[n++] = temp[i++];
  227.                     c_ext[n] = NUL;
  228.                 }
  229.                 if (temp[i] == ';')
  230.                     /*saw escaped ';', expecting integer number of passes, assume for now it's coming*/
  231.                     strcat(compilstr,"1"); /*looking to see if pass 1 exists*/
  232.                 if (!extseen)
  233.                     /*no extension seen, add generic*/
  234.                     strcat(compilstr,".*");
  235.                 else strcat(compilstr,c_ext);
  236.  
  237.                 /* if this is a long name, use do_sys to execute it */
  238.                 longflag =  ( compilstr[0] == BKSLSH ) ? TRUE : FALSE ;
  239.  
  240.                 if (find_comp(compilstr,longflag) == FALSE)
  241.                     /* searches PATH if req */
  242.                     error(" cc: Can't find '%s'",compilstr);
  243.                 /*found the compiler. fix the extensions*/
  244.                 for (n=0;compilstr[n] != '.';n++); /*n points to '.' in string*/
  245.                 for(j=0;dta.dta_name[j] != '.';j++); /*count up to the dot*/
  246.                 if (temp[i] == ';'){
  247.                     compilstr[n-1] = NUL;  /*blank out the '1' (first pass)*/
  248.                     strcat(compilstr,"%d");
  249.                     strcat(compilstr,dta.dta_name + j );/* add .ext */
  250.                     sscanf(temp +i +1,"%d",&numpass);
  251.                     if (numpass < 1 || numpass > 10)
  252.                         error("odd value \( %s \) for number of passes in definition of 'cc'",temp + i + 1);
  253.                 }
  254.                 else{
  255.                     /*easier case. with only one pass, copy the ext directly*/
  256.                     compilstr[n] = NUL;  /*blank out at the dot */
  257.                     strcat(compilstr,dta.dta_name + j); /*add extension*/
  258.                     numpass = 0;
  259.                 }
  260.             }/*else*/
  261.             /*whew*/
  262.             firstcc = FALSE;
  263.             return(do_cc(arglist));
  264.         }/*end case first pass through*/
  265.     /* NOTREACHED */
  266. }
  267.  
  268. stripflag()
  269. {
  270. int i,k;
  271.     /*
  272.     This is what to do with the arguement list, which is in sh_arg.
  273.     It will look something like ' -a -v test.c -Daardvark '
  274.     */
  275.     /* for this version, delete words beginning with '-' */
  276.     i=k=0;
  277.     while ( TRUE ) {
  278.         while (isspace(sh_arg[i])) i++;
  279.         if (isnull(sh_arg[i])) break;
  280.         else if (sh_arg[i] == '-') 
  281.             while (notnull(sh_arg[i]) && !isspace(sh_arg[i])) i++;
  282.         else 
  283.             while (notnull(sh_arg[i]) && !isspace(sh_arg[i]))
  284.                  sh_arg[k++] = sh_arg[i++];
  285.     }
  286.     sh_arg[k] = NUL;
  287. }
  288.  
  289. uppercase(s)   char *s;
  290. {
  291.     for( ; notnull(*s) ; s++)
  292.         *s = toupper(*s);
  293. }
  294.  
  295. char *get_mem(size)
  296. unsigned int size;
  297. {
  298. char    *p,*malloc();
  299.  
  300.     if ((p = malloc(size)) == 0)
  301.         panic("Ran out of memory.");
  302.     return(p);
  303. }
  304.  
  305.  
  306. long getmodified(name,which)
  307. char *name;
  308. int which;
  309. {
  310. long        datetime;
  311.  
  312.     datetime = 0;   /* as old as possible if does not exist */
  313.     if (find_first (name) == 0) {   /* at least one matching file exists */
  314.         datetime = dta.dttime;
  315.         /* now loop through all the rest of the matching files */
  316.         while (find_next() == 0) {
  317.             if (which == DEPENDANT)
  318.                 datetime = max(datetime, dta.dttime);
  319.             else    /* this is DEFINED */
  320.                 datetime = min(datetime, dta.dttime);
  321.         }
  322.     } else  /* doesn't exist */
  323.             datetime = 0;   /* as old as possible */
  324.             
  325.     return(datetime);
  326. }
  327.  
  328. char *mov_in(string)    /* return pointer to fresh memory with copy of string*/
  329. char *string;
  330. {
  331. char   *ptr;
  332.     
  333.        ptr = get_mem((unsigned)(strlen(string) + 1));
  334.        strcpy(ptr,string);
  335.        return(ptr);
  336. }
  337.  
  338. #if LC | MSC
  339. #define envfind getenv
  340. #endif
  341.  
  342. #if UNIX
  343. #define envfind retnull
  344. char *retnull(){ return( NULL );}
  345. #endif
  346.     
  347.  
  348. find_comp(n,flag)   /* does a file exist? (uses PATH if req ) */
  349. char *n;
  350. int flag;
  351. {
  352.     long getmodified();
  353.     int i,k;
  354.     char temp[INMAX],*path;
  355.     char *envfind();
  356.     
  357.     if ( getmodified(n,DEFINED) != 0L ) return(TRUE);
  358.     
  359.     if ( flag ) return(FALSE);   /* if ( full_name_specified, better have found it */
  360.  
  361.     /* search the path and load DTA */
  362.     if ( (path = envfind("PATH")) == NULL ) return(FALSE);
  363.  
  364.     k = i = 0;
  365.     while ( TRUE ) {
  366.         while (notnull(path[k]) && path[k] != ';') 
  367.             temp[i++] = path[k++];
  368.         if ( temp[i-1] != BKSLSH ) temp[i++] = BKSLSH;
  369.         temp[i] = NUL;
  370.         strcat(temp,n);
  371.  
  372.         if ( getmodified(temp,DEFINED) != 0L ) return(TRUE);
  373.         i = 0;
  374.         if ( isnull(path[k]) ) {
  375.             free(path);
  376.             return(FALSE);     /* nothing to prepend, admit defeat */
  377.         }
  378.         k++;
  379.     }
  380. }
  381.