home *** CD-ROM | disk | FTP | other *** search
- /* file makeutil.c */
- /* utilities for "make" for ms-dos */
- /* K. Mitchum 7/84 */
- /* R. Gregg 5/85 */
-
- #include <stdio.h>
- #include "make.h"
-
- #if MSC
- #include <ctype.h>
- #endif
-
- #if LC
- extern int _stack;
- #endif
-
- /* external definitions */
- extern struct dta dta;
- extern int execute;
- extern int firstcc;
- char *get_mem(),*mov_in();
-
- int longflag;
- char buf[INMAX];
-
-
- exec_how(cmd)
- char *cmd;
- {
- int pos,this_echo,this_ign,x,i,ccode,no_more_flags;
- int long_name;
- char cmdname[INMAXSH];
-
- i = pos = 0;
- this_echo = TRUE;
- this_ign = FALSE;
- long_name = FALSE;
- no_more_flags = FALSE;
- while ( TRUE ) {
- while ( isspace(cmd[pos]) )
- pos++;
- switch ( cmd[pos] ) {
- case '@':
- this_echo = FALSE;
- break;
- case '-':
- this_ign = TRUE;
- break;
- case BKSLSH:
- long_name = no_more_flags = TRUE;
- break;
- default:
- no_more_flags = TRUE;
- break;
- }
- if (no_more_flags)
- break;
- else
- pos++;
- }
- for (x=pos; !isspace(cmd[x]) ; x++)
- cmdname[i++] = cmd[x];
- cmdname[i] = NUL;
- if ( this_echo )
- fprintf(stdout,"\n%s\n",cmd+pos);
- if ( !execute && !this_echo) {
- fprintf(stdout,"\n%s\n",cmd+pos);
- fprintf(stdout,"Make: \(above line will not echo when tracing off\)\n");
- return(0);
- }
- if ( !execute )
- return(0);
- if ( (strcmp(cmdname,"cc") == 0) || (strcmp(cmdname,"CC") == 0) ){
- if ( (ccode = do_cc(cmd+x)) != 0 ) {
- if (longflag)
- baddo_sys(buf,ccode);
- else
- badsys(buf,ccode);
- }
- if (this_ign)
- return (0);
- else
- return(err_code());
- }
- else {
- #if MSC
- ccode = do_sys(cmdname,cmd+x,long_name);
- if ( ccode != 0 )
- baddo_sys(cmdname,ccode);
- #else
- if ( long_name ) {
- ccode = do_sys(cmdname,cmd+x);
- if ( ccode != 0 ) baddo_sys(cmdname,ccode);
- }
- else {
- ccode = system(cmd+pos);
- if ( ccode != 0 ) badsys(cmdname,ccode);
- }
- #endif
- if (this_ign)
- return (0);
- else
- return(err_code());
- }
- }
-
- char sh_arg[INMAX];
-
- do_cc(arglist)
- char *arglist;
- {
-
- extern struct macrec *maclist;
- static struct macrec *ccptr;
- char c_ext[INMAXSH],temp[INMAX];
- static char compilstr[INMAXSH];
- static int numpass;
- int i,j,k,n,x,stop,extseen;
- long getmodified();
-
-
-
-
- if (!firstcc){ /* if not first pass through*/
-
- if (numpass > 1){ /* if there is more than 1 pass*/
- strcpy(sh_arg,arglist);
- for (i=1;i<numpass +1;i++){
- sprintf(buf,compilstr,i);
- #if MSC
- if (x = do_sys(buf,sh_arg,longflag) != 0) return(x);
- #else
- if ( longflag ) {
- if (x = do_sys(buf,sh_arg) != 0) return(x);
- }
- else {
- strcat(buf,sh_arg);
- if ( x = system(buf) != 0) return(x);
- }
- #endif
- if (x = err_code() != 0) return(x);
- if (i==1)stripflag(); /* operates on sh_arg */
- }
- return(0);
- }
- else{
- strcpy(buf,compilstr);
- #if MSC
- if (x = do_sys(buf,arglist,longflag) != 0) return(x);
- #else
- if ( longflag ) {
- if (x = do_sys(buf,arglist) != 0) return(x);
- }
- else if ( !longflag ) {
- strcat(buf,arglist);
- if ( x = system(buf) != 0) return(0);
- }
- else return(0);
- #endif
- }
- }
- /*first pass thru -- assemble the static strings*/
- else{
- /*is cc a defined macro?*/
- ccptr = maclist;
- if (ccptr != NULL){
- while (TRUE){
- if (strcmp(ccptr->name,"CC") == 0) break;
- else if (ccptr->nextmac == NULL) {
- ccptr = NULL; /* found no match*/
- break;
- }
- else ccptr = ccptr->nextmac;
- }
- }
-
- if (ccptr == NULL) {
- /*use the default strings*/
- strcpy(compilstr,DEF_CC);
- numpass = DEF_PASS;
- longflag = (compilstr[0] == BKSLSH) ? TRUE : FALSE;
-
- }
- else{
- /*use the defined cc macro*/
- /*do this by
- 1.adding extension if required,checking for existence, then using the extension supplied by find_first
- 2.adding "%d" to the last printing char before ';' (if needed)
- 3.reading the integer between ';' and EOL (if needed)
- */
- k=i=0;
- stop = extseen = FALSE;
- expand(ccptr->mexpand,temp,"",NO_TARG);
- /* strcpy(compilstr,"C:");k=2; *//* always looks in the default drive*/
- while (temp[i]){
- switch (temp[i]){
- case ' ':
- case '\t':
- i++;break;
- case BKSLSH:
- if ( temp[i+1] == ';' ) {
- i++;
- stop = TRUE;
- break;
- }
- else {
- compilstr[k++] = temp[i++];
- break;
- }
- case NUL:
- stop = TRUE;break;
- case '.':
- extseen = stop = TRUE;break;
- default:
- compilstr[k++] = temp[i++];
- break;
- }
- if (stop) break;
- }
- /*if we have gotten this far, we are pointing to '.',';' or EOL*/
- compilstr[k] = NUL;
- /* assemble the extension */
- if (extseen){
- n = 0;
- while (notnull(temp[i]) && temp[i] != ';')
- c_ext[n++] = temp[i++];
- c_ext[n] = NUL;
- }
- if (temp[i] == ';')
- /*saw escaped ';', expecting integer number of passes, assume for now it's coming*/
- strcat(compilstr,"1"); /*looking to see if pass 1 exists*/
- if (!extseen)
- /*no extension seen, add generic*/
- strcat(compilstr,".*");
- else strcat(compilstr,c_ext);
-
- /* if this is a long name, use do_sys to execute it */
- longflag = ( compilstr[0] == BKSLSH ) ? TRUE : FALSE ;
-
- if (find_comp(compilstr,longflag) == FALSE)
- /* searches PATH if req */
- error(" cc: Can't find '%s'",compilstr);
- /*found the compiler. fix the extensions*/
- for (n=0;compilstr[n] != '.';n++); /*n points to '.' in string*/
- for(j=0;dta.dta_name[j] != '.';j++); /*count up to the dot*/
- if (temp[i] == ';'){
- compilstr[n-1] = NUL; /*blank out the '1' (first pass)*/
- strcat(compilstr,"%d");
- strcat(compilstr,dta.dta_name + j );/* add .ext */
- sscanf(temp +i +1,"%d",&numpass);
- if (numpass < 1 || numpass > 10)
- error("odd value \( %s \) for number of passes in definition of 'cc'",temp + i + 1);
- }
- else{
- /*easier case. with only one pass, copy the ext directly*/
- compilstr[n] = NUL; /*blank out at the dot */
- strcat(compilstr,dta.dta_name + j); /*add extension*/
- numpass = 0;
- }
- }/*else*/
- /*whew*/
- firstcc = FALSE;
- return(do_cc(arglist));
- }/*end case first pass through*/
- /* NOTREACHED */
- }
-
- stripflag()
- {
- int i,k;
- /*
- This is what to do with the arguement list, which is in sh_arg.
- It will look something like ' -a -v test.c -Daardvark '
- */
- /* for this version, delete words beginning with '-' */
- i=k=0;
- while ( TRUE ) {
- while (isspace(sh_arg[i])) i++;
- if (isnull(sh_arg[i])) break;
- else if (sh_arg[i] == '-')
- while (notnull(sh_arg[i]) && !isspace(sh_arg[i])) i++;
- else
- while (notnull(sh_arg[i]) && !isspace(sh_arg[i]))
- sh_arg[k++] = sh_arg[i++];
- }
- sh_arg[k] = NUL;
- }
-
- uppercase(s) char *s;
- {
- for( ; notnull(*s) ; s++)
- *s = toupper(*s);
- }
-
- char *get_mem(size)
- unsigned int size;
- {
- char *p,*malloc();
-
- if ((p = malloc(size)) == 0)
- panic("Ran out of memory.");
- return(p);
- }
-
-
- long getmodified(name,which)
- char *name;
- int which;
- {
- long datetime;
-
- datetime = 0; /* as old as possible if does not exist */
- if (find_first (name) == 0) { /* at least one matching file exists */
- datetime = dta.dttime;
- /* now loop through all the rest of the matching files */
- while (find_next() == 0) {
- if (which == DEPENDANT)
- datetime = max(datetime, dta.dttime);
- else /* this is DEFINED */
- datetime = min(datetime, dta.dttime);
- }
- } else /* doesn't exist */
- datetime = 0; /* as old as possible */
-
- return(datetime);
- }
-
- char *mov_in(string) /* return pointer to fresh memory with copy of string*/
- char *string;
- {
- char *ptr;
-
- ptr = get_mem((unsigned)(strlen(string) + 1));
- strcpy(ptr,string);
- return(ptr);
- }
-
- #if LC | MSC
- #define envfind getenv
- #endif
-
- #if UNIX
- #define envfind retnull
- char *retnull(){ return( NULL );}
- #endif
-
-
- find_comp(n,flag) /* does a file exist? (uses PATH if req ) */
- char *n;
- int flag;
- {
- long getmodified();
- int i,k;
- char temp[INMAX],*path;
- char *envfind();
-
- if ( getmodified(n,DEFINED) != 0L ) return(TRUE);
-
- if ( flag ) return(FALSE); /* if ( full_name_specified, better have found it */
-
- /* search the path and load DTA */
- if ( (path = envfind("PATH")) == NULL ) return(FALSE);
-
- k = i = 0;
- while ( TRUE ) {
- while (notnull(path[k]) && path[k] != ';')
- temp[i++] = path[k++];
- if ( temp[i-1] != BKSLSH ) temp[i++] = BKSLSH;
- temp[i] = NUL;
- strcat(temp,n);
-
- if ( getmodified(temp,DEFINED) != 0L ) return(TRUE);
- i = 0;
- if ( isnull(path[k]) ) {
- free(path);
- return(FALSE); /* nothing to prepend, admit defeat */
- }
- k++;
- }
- }