home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-01-29 | 39.9 KB | 1,482 lines |
- Newsgroups: comp.sources.misc
- From: dvadura@plg.waterloo.edu (Dennis Vadura)
- Subject: v27i109: dmake - dmake Version 3.8, Part08/41
- Message-ID: <1992Jan28.031422.7223@sparky.imd.sterling.com>
- X-Md4-Signature: 2a00c738fc4bef1e39ec5c5d81319c5c
- Date: Tue, 28 Jan 1992 03:14:22 GMT
- Approved: kent@sparky.imd.sterling.com
-
- Submitted-by: dvadura@plg.waterloo.edu (Dennis Vadura)
- Posting-number: Volume 27, Issue 109
- Archive-name: dmake/part08
- Environment: Atari-ST, Coherent, Mac, MSDOS, OS/2, UNIX
- Supersedes: dmake: Volume 19, Issue 22-58
-
- ---- Cut Here and feed the following to sh ----
- # this is dmake.shar.08 (part 8 of a multipart archive)
- # do not concatenate these parts, unpack them in order with /bin/sh
- # file dmake/dmdump.c continued
- #
- if test ! -r _shar_seq_.tmp; then
- echo 'Please unpack part 1 first!'
- exit 1
- fi
- (read Scheck
- if test "$Scheck" != 8; then
- echo Please unpack part "$Scheck" next!
- exit 1
- else
- exit 0
- fi
- ) < _shar_seq_.tmp || exit 1
- if test -f _shar_wnt_.tmp; then
- sed 's/^X//' << 'SHAR_EOF' >> 'dmake/dmdump.c' &&
- -- SYNOPSIS -- dump the internal dag to stdout.
- --
- -- DESCRIPTION
- -- This file contains the routine that is called to dump a version of
- -- the digested makefile to the standard output. May be useful perhaps
- -- to the ordinary user, and invaluable for debugging make.
- --
- -- AUTHOR
- -- Dennis Vadura, dvadura@watdragon.uwaterloo.ca
- -- CS DEPT, University of Waterloo, Waterloo, Ont., Canada
- --
- -- COPYRIGHT
- -- Copyright (c) 1990 by Dennis Vadura. All rights reserved.
- --
- -- This program is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU General Public License
- -- (version 1), as published by the Free Software Foundation, and
- -- found in the file 'LICENSE' included with this distribution.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warrant of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- --
- -- LOG
- -- $Log: dmdump.c,v $
- X * Revision 1.1 1992/01/24 03:28:51 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- */
- X
- #include "extern.h"
- X
- #define M_TEST (M_PRECIOUS | M_VAR_MASK)
- X
- static void dump_name ANSI((HASHPTR, int));
- static void dump_normal_target ANSI((CELLPTR, int));
- static void dump_prerequisites ANSI((LINKPTR, int, int, int));
- X
- X
- PUBLIC void
- Dump()/*
- ======== Dump onto standard output the digested makefile. Note that
- X the form of the dump is not representative of the contents
- X of the original makefile contents at all */
- {
- X HASHPTR hp;
- X int i;
- X
- X DB_ENTER( "Dump" );
- X
- X puts( "# Dump of dmake macro variables:" );
- X for( i=0; i<HASH_TABLE_SIZE; i++)
- X for( hp=Macs[i]; hp != NIL(HASH); hp = hp->ht_next ) {
- X int flag = hp->ht_flag;
- X
- X printf( "%s ", hp->ht_name );
- X if( flag & M_EXPANDED ) putchar( ':' );
- X printf( "= " );
- X if( hp->ht_value != NIL(char) ) printf( "%s",hp->ht_value );
- X if( flag & M_PRECIOUS )
- X printf( "\t # PRECIOUS " );
- X putchar( '\n' );
- X }
- X
- X puts( "\n#====================================" );
- X puts( "# Dump of targets:\n" );
- X
- X for( i=0; i<HASH_TABLE_SIZE; i++ )
- X for( hp = Defs[i]; hp != NIL(HASH); hp = hp->ht_next )
- X if( !(hp->CP_OWNR->ce_flag & F_PERCENT) ) {
- X if( Root->ce_prq && hp->CP_OWNR == Root->ce_prq->cl_prq )
- X puts( "# ******* FIRST TARGET ********" );
- X dump_normal_target( hp->CP_OWNR, hp->CP_OWNR->ce_flag );
- X }
- X
- X puts( "\n#====================================" );
- X puts( "# Dump of inference graph\n" );
- X
- X for( i=0; i<HASH_TABLE_SIZE; i++ )
- X for( hp = Defs[i]; hp != NIL(HASH); hp = hp->ht_next )
- X if( (hp->CP_OWNR->ce_flag & F_PERCENT) &&
- X !(hp->CP_OWNR->ce_flag & F_MAGIC) )
- X dump_normal_target( hp->CP_OWNR, hp->CP_OWNR->ce_flag );
- X
- X DB_VOID_RETURN;
- }
- X
- X
- X
- PUBLIC void
- Dump_recipe( sp )/*
- ===================
- X Given a string pointer print the recipe line out */
- STRINGPTR sp;
- {
- X char *st;
- X char *nl;
- X
- X if( sp == NIL(STRING) ) return;
- X
- X putchar( '\t' );
- X if( sp->st_attr & A_SILENT ) putchar( '@' );
- X if( sp->st_attr & A_IGNORE ) putchar( '-' );
- X if( sp->st_attr & A_SHELL ) putchar( '+' );
- X if( sp->st_attr & A_SWAP ) putchar( '%' );
- X
- X st = sp->st_string;
- X for( nl=strchr(st,'\n'); nl != NIL( char); nl=strchr(st,'\n') ) {
- X *nl = '\0';
- X printf( "%s\\\n", st );
- X *nl = '\n';
- X st = nl+1;
- X }
- X printf( "%s\n", st );
- }
- X
- X
- static char *_attrs[] = { ".PRECIOUS", ".SILENT", ".LIBRARY",
- X ".EPILOG", ".PROLOG", ".IGNORE", ".SYMBOL", ".NOINFER",
- X ".UPDATEALL", ".SEQUENTIAL", ".SETDIR=", ".USESHELL", ".SWAP", ".MKSARGS",
- X ".PHONY", ".NOSTATE" };
- X
- static void
- dump_normal_target( cp, flag )/*
- ================================
- X Dump in makefile like format the dag information */
- CELLPTR cp;
- int flag;
- {
- X register LINKPTR lp;
- X register STRINGPTR sp;
- X t_attr attr;
- X unsigned int k;
- X
- X DB_ENTER( "dump_normal_target" );
- X
- X if( !(cp->ce_flag & F_TARGET) && !cp->ce_attr ) { DB_VOID_RETURN; }
- X
- X if( cp->ce_flag & F_MULTI ) {
- X int tflag = cp->ce_prq->cl_prq->ce_flag;
- X if( !(cp->ce_flag & F_PERCENT) ) tflag |= F_MULTI;
- X dump_prerequisites(cp->ce_prq, FALSE, TRUE, tflag);
- X }
- X else {
- X dump_name( cp->ce_name, FALSE );
- X
- X for( k=0, attr=1; attr <= MAX_ATTR; attr <<= 1, k++ )
- X if( cp->ce_attr & attr ) {
- X printf( "%s%s ", _attrs[k],
- X (attr != A_SETDIR) ? "" : (cp->ce_dir?cp->ce_dir:"") );
- X }
- X
- X putchar( ':' );
- X
- X if( flag & F_MULTI ) putchar( ':' );
- X if( flag & F_SINGLE ) putchar( '!' );
- X putchar( ' ' );
- X
- X dump_prerequisites( cp->ce_prq, FALSE, FALSE, F_DEFAULT);
- X dump_prerequisites( cp->ce_indprq, TRUE, FALSE, F_DEFAULT);
- X
- X putchar( '\n' );
- X if( cp->ce_flag & F_GROUP ) puts( "[" );
- X for( sp = cp->ce_recipe; sp != NIL(STRING); sp = sp->st_next )
- X Dump_recipe( sp );
- X if( cp->ce_flag & F_GROUP ) puts( "]" );
- X
- X putchar( '\n' );
- X }
- X
- X DB_VOID_RETURN;
- }
- X
- X
- static void
- dump_prerequisites( lp, quote, recurse, flag )
- LINKPTR lp;
- int quote;
- int recurse;
- int flag;
- {
- X for( ; lp; lp=lp->cl_next )
- X if( recurse )
- X dump_normal_target(lp->cl_prq, flag);
- X else if( lp->cl_prq )
- X dump_name(lp->cl_prq->ce_name, quote);
- }
- X
- X
- static void
- dump_name( hp, quote )/*
- ========================
- X print out a name */
- HASHPTR hp;
- int quote;
- {
- X if( quote ) putchar('\'');
- X printf( "%s", hp->ht_name );
- X if( quote ) putchar('\'');
- X putchar(' ');
- }
- SHAR_EOF
- chmod 0640 dmake/dmdump.c ||
- echo 'restore of dmake/dmdump.c failed'
- Wc_c="`wc -c < 'dmake/dmdump.c'`"
- test 5875 -eq "$Wc_c" ||
- echo 'dmake/dmdump.c: original size 5875, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/dmstring.c ==============
- if test -f 'dmake/dmstring.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/dmstring.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/dmstring.c' &&
- /* RCS -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/dmstring.c,v 1.1 1992/01/24 03:29:38 dvadura Exp $
- -- SYNOPSIS -- string handling code
- --
- -- DESCRIPTION
- -- Routines to handle string manipulation. This code is not specific
- -- to dmake and has/and will be used in other programs. The string
- -- "" is considered the NULL string, if NIL(char) is received instead
- -- undefined results may occurr. (In reality NIL(char) is checked for
- -- but in general it is not safe to assume NIL(char) == NULL)
- --
- -- AUTHOR
- -- Dennis Vadura, dvadura@watdragon.uwaterloo.ca
- -- CS DEPT, University of Waterloo, Waterloo, Ont., Canada
- --
- -- COPYRIGHT
- -- Copyright (c) 1990 by Dennis Vadura. All rights reserved.
- --
- -- This program is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU General Public License
- -- (version 1), as published by the Free Software Foundation, and
- -- found in the file 'LICENSE' included with this distribution.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warrant of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- --
- -- LOG
- -- $Log: dmstring.c,v $
- X * Revision 1.1 1992/01/24 03:29:38 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- */
- X
- #include "extern.h"
- X
- PUBLIC char *
- _strjoin( src, data, n, fr )/*
- ==============================
- X Join data to src according to value of n.
- X
- X n = -1 - return strcat( src, data )
- X n >= 0 - return strncat( src, data, n )
- X
- X FREE original src if fr == TRUE, else leave it alone */
- X
- char *src;
- char *data;
- int n;
- int fr;
- {
- X char *t;
- X int l;
- X int flag = FALSE;
- X
- X DB_ENTER( "_strjoin" );
- X
- X if( src == NIL(char) ) { src = ""; flag = TRUE; }
- X if( data == NIL(char) ) data = "";
- X DB_PRINT( "str", ("Joining [%s] [%s] %d", src, data, n) );
- X
- X if( n == -1 ) n = strlen( data );
- X
- X l = strlen( src ) + n + 1;
- X if( (t = MALLOC( l, char )) == NIL(char) ) No_ram();
- X
- X strcpy( t, src );
- X if (n) strncat( t, data, n );
- X t[ l-1 ] = '\0';
- X
- X if( !flag && fr ) FREE( src );
- X
- X DB_PRINT( "str", ("Result [%s]", t) );
- X DB_RETURN( t );
- }
- X
- X
- X
- X
- PUBLIC char *
- _stradd( src, data, fr )/*
- ==========================
- X append data to src with space in between if src is not NIL(char) or ""
- X and free both src and data if fr == TRUE, otherwise leave them be */
- X
- char *src;
- char *data;
- int fr;
- {
- X char *t;
- X int l;
- X int sflag;
- X int dflag;
- X
- X DB_ENTER( "_stradd" );
- X
- X sflag = dflag = fr;
- X
- X if( src == NIL(char) ) { src = ""; sflag = FALSE; }
- X if( data == NIL(char) ) { data = ""; dflag = FALSE; }
- X DB_PRINT( "str", ("Adding [%s] [%s] %d", src, data, fr) );
- X
- X l = strlen(src) + strlen(data) + 1;
- X if( *src ) l++;
- X
- X if( (t = MALLOC( l, char )) == NIL(char) ) No_ram();
- X
- X strcpy( t, src );
- X
- X if( *data )
- X {
- X if( *src ) strcat( t, " " );
- X strcat( t, data );
- X }
- X
- X if( sflag ) FREE( src );
- X if( dflag ) FREE( data );
- X
- X DB_PRINT( "str", ("Result [%s]", t) );
- X DB_RETURN( t );
- }
- X
- X
- X
- PUBLIC char *
- _strapp( src1, src2 )/*
- =======================
- X Append two strings together, and return the result with a space between
- X the two strings. FREE the first string if it is not NIL and always
- X leave the second string be. */
- char *src1;
- char *src2;
- {
- X src2 = _stradd( src1, src2, FALSE );
- X if( src1 != NIL(char) ) FREE( src1 );
- X return( src2 );
- }
- X
- X
- X
- #ifdef DBUG
- #ifdef _strdup
- #undef _strdup
- #endif
- #endif
- PUBLIC char *
- _strdup( str )/*
- ================ Duplicate the contents of a string, by using malloc */
- char *str;
- {
- X char *t;
- X
- X if( str == NIL(char) ) return( NIL(char) );
- X
- X if( (t = MALLOC( strlen( str )+1, char )) == NIL(char) ) No_ram();
- X strcpy( t, str );
- X
- X return( t );
- }
- X
- X
- X
- PUBLIC char *
- _strdup2( str )/*
- =================
- X This function is used solely to properly quote command line arguments when
- X they are reinserted int MAKEMACROS so that they can be used further in
- X a processing line. */
- char *str;
- {
- X char *t;
- X size_t size;
- X size_t alloced;
- X char *tmp;
- X char *dest;
- X int seen_equal = 0;
- X
- X if(str == NIL(char)) return(NIL(char));
- X size = strlen(str) + 1;
- X alloced = size + 2; /* for two quotes */
- X
- X for(tmp = str; *tmp; tmp++)
- X if(*tmp == '"')
- X alloced++;
- X
- X if((t = MALLOC(alloced, char)) == NIL(char)) No_ram();
- X
- X for(tmp = str, dest = t; *tmp; tmp++, dest++) {
- X if(*tmp == '=' && !seen_equal) {
- X seen_equal = 1;
- X *dest++ = *tmp;
- X *dest = '"';
- X continue;
- X }
- X if(*tmp == '"')
- X *dest++ = '\\';
- X *dest = *tmp;
- X }
- X
- X if(!seen_equal)
- X Fatal("_strdup2 invoked without argument of form x=y\n");
- X
- X *dest++ = '"';
- X *dest = 0;
- X
- X return t;
- }
- X
- X
- X
- PUBLIC char *
- _strpbrk( s1, s2 )/*
- ====================
- X find first occurence of char in s2 in string s1.
- X Returns a pointer to the first occurrence. NOTE '\0' is considered part
- X of s2 and a pointer to it is returned if no other chars match. */
- X
- char *s1;
- char *s2;
- {
- X register char *t;
- X
- X if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
- X
- X for( t=s1; *t && (strchr( s2, *t ) == NIL(char)); t++ );
- X return( t );
- }
- X
- X
- X
- X
- PUBLIC char *
- _strspn( s1, s2 )/*
- ===================
- X return pointer to first char in s1 that does not belong to s2.
- X Returns the pointer if match found, else returns pointer to null char
- X in s1. (ie. "" ) */
- X
- char *s1;
- char *s2;
- {
- X register char *t;
- X
- X if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
- X
- X for( t=s1; *t && (strchr( s2, *t ) != NIL(char)); t++ );
- X return( t );
- }
- X
- X
- X
- X
- PUBLIC char *
- _strstr( s1, s2 )/*
- ================== find first occurrence in s1 of s2 */
- char *s1;
- char *s2;
- {
- X register char *s;
- X register char *p;
- X register char *r;
- X
- X if( s1 != NIL(char) && s2 != NIL(char) )
- X for( s=s1; *s; s++ )
- X if( *s == *s2 )
- X {
- X for( r=s+1, p = s2+1; *p && (*r == *p); r++, p++ );
- X if( !*p ) return( s );
- X }
- X
- X return( NIL(char) );
- }
- X
- X
- X
- PUBLIC char *
- _substr( s, e )/*
- =================
- X Return the string between the two pointers s and e, not including the
- X char that e points to. NOTE: This routine assumes that s and e point
- X into the same string. */
- X
- char *s;
- char *e;
- {
- X char save;
- X int len = e-s;
- X
- X if( len < 0 || len > strlen(s) )
- X Fatal( "Internal Error: _substr fails consistency test" );
- X
- X save = *e;
- X *e = '\0';
- X s = _strdup( s );
- X *e = save;
- X
- X return( s );
- }
- SHAR_EOF
- chmod 0640 dmake/dmstring.c ||
- echo 'restore of dmake/dmstring.c failed'
- Wc_c="`wc -c < 'dmake/dmstring.c'`"
- test 6772 -eq "$Wc_c" ||
- echo 'dmake/dmstring.c: original size 6772, current size' "$Wc_c"
- rm -f _shar_wnt_.tmp
- fi
- # ============= dmake/expand.c ==============
- if test -f 'dmake/expand.c' -a X"$1" != X"-c"; then
- echo 'x - skipping dmake/expand.c (File already exists)'
- rm -f _shar_wnt_.tmp
- else
- > _shar_wnt_.tmp
- sed 's/^X//' << 'SHAR_EOF' > 'dmake/expand.c' &&
- /* RCS -- $Header: /u2/dvadura/src/generic/dmake/src/RCS/expand.c,v 1.1 1992/01/24 03:27:03 dvadura Exp $
- -- SYNOPSIS -- macro expansion code.
- --
- -- DESCRIPTION
- --
- -- This routine handles all the necessary junk that deals with macro
- -- expansion. It understands the following syntax. If a macro is
- -- not defined it expands to NULL, and {} are synonyms for ().
- --
- -- $$ - expands to $
- -- {{ - expands to {
- -- }} - expands to }
- -- $A - expands to whatever the macro A is defined as
- -- $(AA) - expands to whatever the macro AA is defined as
- -- $($(A)) - represents macro indirection
- -- <+...+> - get mapped to $(mktmp ...)
- --
- -- following macro is recognized
- --
- -- string1{ token_list }string2
- --
- -- and expands to string1 prepended to each element of token_list and
- -- string2 appended to each of the resulting tokens from the first
- -- operation. If string2 is of the form above then the result is
- -- the cross product of the specified (possibly modified) token_lists.
- --
- -- The folowing macro modifiers are defined and expanded:
- --
- -- $(macro:modifier_list:modifier_list:...)
- --
- -- where modifier_list a combination of:
- --
- -- D or d - Directory portion of token including separator
- -- F or f - File portion of token including suffix
- -- B or b - basename portion of token not including suffix
- -- T or t - for tokenization
- --
- -- or a single
- -- S or s - pattern substitution (simple)
- --
- -- NOTE: Modifiers are applied once the macro value has been found.
- -- Thus the construct $($(test):s/joe/mary/) is defined and
- -- modifies the value of $($(test))
- --
- -- Also the construct $(m:d:f) is not the same as $(m:df)
- -- the first applies d to the value of $(m) and then
- -- applies f to the value of that whereas the second form
- -- applies df to the value of $(m).
- --
- -- AUTHOR
- -- Dennis Vadura, dvadura@watdragon.uwaterloo.ca
- -- CS DEPT, University of Waterloo, Waterloo, Ont., Canada
- --
- -- COPYRIGHT
- -- Copyright (c) 1990 by Dennis Vadura. All rights reserved.
- --
- -- This program is free software; you can redistribute it and/or
- -- modify it under the terms of the GNU General Public License
- -- (version 1), as published by the Free Software Foundation, and
- -- found in the file 'LICENSE' included with this distribution.
- --
- -- This program is distributed in the hope that it will be useful,
- -- but WITHOUT ANY WARRANTY; without even the implied warrant of
- -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- -- GNU General Public License for more details.
- --
- -- You should have received a copy of the GNU General Public License
- -- along with this program; if not, write to the Free Software
- -- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- --
- -- LOG
- -- $Log: expand.c,v $
- X * Revision 1.1 1992/01/24 03:27:03 dvadura
- X * dmake Version 3.8, Initial revision
- X *
- */
- X
- #include "extern.h"
- X
- /* Microsoft BRAINDAMAGE ALERT!!!!
- X * This #ifdef is here only to satisfy stupid bugs in MSC5.0 and MSC5.1
- X * it isn't needed for anything else. It turns loop optimization off. */
- #if defined(_MSC_VER)
- #include "optoff.h"
- #endif
- X
- static char* _scan_token ANSI((char*, char**));
- static char* _scan_macro ANSI((char*, char**));
- static char* _scan_brace ANSI((char*, char**, int*));
- static char* _cross_prod ANSI((char*, char*));
- X
- X
- PUBLIC char *
- Expand( src )/*
- ===============
- X This is the driver routine for the expansion, it identifies non-white
- X space tokens and gets the _scan_token routine to figure out if they should
- X be treated in a special way. */
- X
- char *src; /* pointer to source string */
- {
- X char *tmp; /* pointer to temporary str */
- X char *res; /* pointer to result string */
- X char *start; /* pointer to start of token */
- X
- X DB_ENTER( "Expand" );
- X DB_PRINT( "exp", ("Expanding [%s]", src) );
- X
- X res = _strdup( "" );
- X if( src == NIL(char) ) DB_RETURN( res );
- X
- X while( *src ) {
- X char *ks, *ke;
- X
- X /* Here we find the next non white space token in the string
- X * and find it's end, with respect to non-significant white space. */
- X
- X start = _strspn( src, " \t\n" );
- X res = _strjoin( res, src, start-src, TRUE );
- X if( !(*start) ) break;
- X
- X /* START <+...+> KLUDGE */
- X if( (ks=_strstr(start,"<+")) && (ke=_strstr(ks,"+>")) ){
- X char *t1, *t2;
- X
- X res = _strjoin( res, t2=Expand(t1=_substr(start,ks)), -1, TRUE);
- X FREE(t1); FREE(t2);
- X
- X t1 = _substr(ks+2, ke+1); t1[ke-ks-2] = ')';
- X t2 = _strjoin( "$(mktmp ", t1, -1,FALSE);
- X FREE(t1);
- X res = _strjoin( res, t2=Expand(t2), -1, TRUE);
- X FREE(t2);
- X src = ke+2;
- X }
- X /* END <+...+> KLUDGE */
- X else {
- X res = _strjoin( res, tmp = _scan_token( start, &src ), -1, TRUE );
- X FREE( tmp );
- X }
- X }
- X
- X DB_PRINT( "exp", ("Returning [%s]", res) );
- X DB_RETURN( res );
- }
- X
- X
- PUBLIC char *
- Apply_edit( src, pat, subst, fr, anchor )/*
- ===========================================
- X Take the src string and apply the pattern substitution. ie. look for
- X occurrences of pat in src and replace each occurrence with subst. This is
- X NOT a regular expressions pattern substitution, it's just not worth it.
- X
- X if anchor == TRUE then the src pattern match must be at the end of a token.
- X ie. this is for SYSV compatibility and is only used for substitutions of
- X the caused by $(macro:pat=sub). So if src = "fre.o.k june.o" then
- X $(src:.o=.a) results in "fre.o.k june.a", and $(src:s/.o/.a) results in
- X "fre.a.k june.a" */
- X
- char *src; /* the source string */
- char *pat; /* pattern to find */
- char *subst; /* substitute string */
- int fr; /* if TRUE free src */
- int anchor; /* if TRUE anchor */
- {
- X char *res;
- X char *p;
- X char *s;
- X int l;
- X
- X DB_ENTER( "Apply_edit" );
- X
- X if( !*pat ) DB_RETURN( src ); /* do nothing if pat is NULL */
- X
- X DB_PRINT( "mod", ("Source str: [%s]", src) );
- X DB_PRINT( "mod", ("Replacing [%s], with [%s]", pat, subst) );
- X
- X s = src;
- X l = strlen( pat );
- X if( (p = _strstr( s, pat )) != NIL(char) ) {
- X res = _strdup( "" );
- X do {
- X if( anchor )
- X if( !*(p+l) || (strchr(" \t", *(p+l)) != NIL(char)) )
- X res = _strjoin( _strjoin( res, s, p-s, TRUE ), subst, -1, TRUE );
- X else
- X res = _strjoin( res, s, p+l-s, TRUE );
- X else
- X res = _strjoin( _strjoin( res, s, p-s, TRUE ), subst, -1, TRUE );
- X
- X s = p + l;
- X }
- X while( (p = _strstr( s, pat )) != NIL(char) );
- X
- X res = _strjoin( res, s, -1, TRUE );
- X if( fr ) FREE( src );
- X }
- X else
- X res = src;
- X
- X
- X DB_PRINT( "mod", ("Result [%s]", res) );
- X DB_RETURN( res );
- }
- X
- X
- PUBLIC void
- Map_esc( tok )/*
- ================
- X Map an escape sequence and replace it by it's corresponding character
- X value. It is assumed that tok points at the initial \, the esc
- X sequence in the original string is replaced and the value of tok
- X is not modified. */
- char *tok;
- {
- X if( strchr( "\"\\vantbrf01234567", tok[1] ) ) {
- X switch( tok[1] ) {
- X case 'a' : *tok = 0x07; break;
- X case 'b' : *tok = '\b'; break;
- X case 'f' : *tok = '\f'; break;
- X case 'n' : *tok = '\n'; break;
- X case 'r' : *tok = '\r'; break;
- X case 't' : *tok = '\t'; break;
- X case 'v' : *tok = 0x0b; break;
- X case '\\': *tok = '\\'; break;
- X case '\"': *tok = '\"'; break;
- X
- X default: {
- X register int i = 0;
- X register int j = 0;
- X for( ; i<2 && isdigit(tok[2]); i++ ) {
- X j = (j << 3) + (tok[1] - '0');
- X strcpy( tok+1, tok+2 );
- X }
- X j = (j << 3) + (tok[1] - '0');
- X *tok = j;
- X }
- X }
- X strcpy( tok+1, tok+2 );
- X }
- }
- X
- X
- PUBLIC char*
- Apply_modifiers( mod, src )/*
- =============================
- X This routine applies the appropriate modifiers to the string src
- X and returns the proper result string */
- X
- int mod;
- char *src;
- {
- X char *s;
- X char *e;
- X TKSTR str;
- X
- X DB_ENTER( "Apply_modifiers" );
- X
- X if( mod == (SUFFIX_FLAG | DIRECTORY_FLAG | FILE_FLAG) )
- X DB_RETURN( src );
- X
- X SET_TOKEN( &str, src );
- X DB_PRINT( "mod", ("Source string [%s]", src) );
- X
- X while( *(s = Get_token( &str, "", FALSE )) != '\0' ) {
- X /* search for the directory portion of the filename. If the
- X * DIRECTORY_FLAG is set, then we want to keep the directory portion
- X * othewise throw it away and blank out to the end of the token */
- X
- X if( (e = basename(s)) != s)
- X if( !(mod & DIRECTORY_FLAG) ) {
- X strcpy(s, e);
- X e = s+(str.tk_str-e);
- X for(; e != str.tk_str; e++)
- X *e = ' ';
- X }
- X else
- X s = e;
- X
- X /* search for the suffix, if there is none, treat it as a NULL suffix.
- X * if no file name treat it as a NULL file name. same copy op as
- X * for directory case above */
- X
- X e = strrchr( s, '.' ); /* NULL suffix if e=0 */
- X if( e == NIL(char) ) e = s+strlen(s);
- X
- X if( !(mod & FILE_FLAG) ) {
- X strcpy( s, e );
- X e = s+(str.tk_str-e);
- X for( ; e != str.tk_str; e++ ) *e = ' ';
- X }
- X else
- X s = e;
- X
- X /* The last and final part. This is the suffix case, if we don't want
- X * it then just erase to the end of the token. */
- X
- X if( s != NIL(char) )
- X if( !(mod & SUFFIX_FLAG) )
- X for( ; s != str.tk_str; s++ ) *s = ' ';
- X }
- X
- X /* delete the extra white space, it looks ugly */
- X for( s = src, e = NIL(char); *s; s++ )
- X if( *s == ' ' || *s == '\t' || *s == '\n' ) {
- X if( e == NIL(char) )
- X e = s;
- X }
- X else {
- X if( e != NIL(char) ) {
- X if( e+1 < s ) {
- X strcpy( e+1, s );
- X s = e+1;
- X *e = ' ';
- X }
- X e = NIL(char);
- X }
- X }
- X
- X if( e != NIL(char) )
- X if( e < s )
- X strcpy( e, s );
- X
- X DB_PRINT( "mod", ("Result string [%s]", src) );
- X DB_RETURN( src );
- }
- X
- X
- PUBLIC char*
- Tokenize( src, separator )/*
- ============================
- X Tokenize the input of src and join each token found together with
- X the next token separated by the separator string.
- X
- X When doing the tokenization, <sp>, <tab>, <nl>, and \<nl> all
- X constitute white space. */
- X
- char *src;
- char *separator;
- {
- X TKSTR tokens;
- X char *tok;
- X char *res;
- X int first = TRUE;
- X
- X DB_ENTER( "Tokenize" );
- X
- X SET_TOKEN( &tokens, src );
- X
- X
- X /* map the escape codes in the separator string first */
- X
- X for(tok=separator; (tok = strchr(tok,ESCAPE_CHAR)) != NIL(char); tok++)
- X Map_esc( tok );
- X
- X DB_PRINT( "exp", ("Separator [%s]", separator) );
- X
- X /* Build the token list */
- X res = _strdup( "" );
- X while( *(tok = Get_token( &tokens, "", FALSE )) != '\0' ) {
- X DB_PRINT( "exp", ("Tokenizing [%s]", tok) );
- X
- X if( first ) {
- X FREE( res );
- X res = _strdup( tok );
- X first = FALSE;
- X }
- X else {
- X char *x;
- X res = _strjoin(res, x =_strjoin(separator, tok, -1, FALSE), -1, TRUE);
- X FREE( x );
- X }
- X }
- X
- X FREE( src );
- X DB_RETURN( res );
- }
- X
- X
- static char*
- _scan_token( s, ps )/*
- ======================
- X This routine scans the token characters one at a time and identifies
- X macros starting with $( and ${ and calls _scan_macro to expand their
- X value. the string1{ token_list }string2 expansion is also handled.
- X In this case a temporary result is maintained so that we can take it's
- X cross product with any other token_lists that may possibly appear. */
- X
- char *s; /* pointer to start of src string */
- char **ps; /* pointer to start pointer */
- {
- X char *res; /* pointer to result */
- X char *start; /* pointer to start of prefix */
- X int crossproduct = 0; /* if 1 then computing X-prod */
- X
- X start = s;
- X res = _strdup( "" );
- X while( 1 )
- X switch( *s ) {
- X /* Termination, We halt at seeing a space or a tab or end of string.
- X * We return the value of the result with any new macro's we scanned
- X * or if we were computing cross_products then we return the new
- X * cross_product.
- X * NOTE: Once we start computing cross products it is impossible to
- X * stop. ie. the semantics are such that once a {} pair is
- X * seen we compute cross products until termination. */
- X
- X case ' ':
- X case '\t':
- X case '\n':
- X case '\0':
- X {
- X char *tmp;
- X
- X *ps = s;
- X if( !crossproduct )
- X tmp = _strjoin( res, start, (s-start), TRUE );
- X else
- X {
- X tmp = _substr( start, s );
- X tmp = _cross_prod( res, tmp );
- X }
- X return( tmp );
- X }
- X
- X case '$':
- X case '{':
- X {
- X /* Handle if it's a macro or if it's a {} construct.
- X * The results of a macro expansion are handled differently based
- X * on whether we have seen a {} beforehand. */
- X
- X char *tmp;
- X tmp = _substr( start, s ); /* save the prefix */
- X
- X if( *s == '$' ) {
- X start = _scan_macro( s+1, &s );
- X
- X if( crossproduct )
- X res = _cross_prod( res, _strjoin( tmp, start, -1, TRUE ) );
- X else {
- X res = _strjoin(res,tmp = _strjoin(tmp,start,-1,TRUE),-1,TRUE);
- X FREE( tmp );
- X }
- X FREE( start );
- X }
- X else if( strchr("{ \t",s[1]) == NIL(char) ){
- X int ok;
- X start = _scan_brace( s+1, &s, &ok );
- X
- X if( ok ) {
- X res = _cross_prod( res, _cross_prod(tmp, start) );
- X crossproduct = TRUE;
- X }
- X else {
- X res =_strjoin(res,tmp=_strjoin(tmp,start,-1,TRUE),-1,TRUE);
- X FREE( start );
- X FREE( tmp );
- X }
- X }
- X else { /* handle the {{ case */
- X res = _strjoin( res, start, (s-start+1), TRUE );
- X s += (s[1]=='{')?2:1;
- X FREE( tmp );
- X }
- X
- X start = s;
- X }
- X break;
- X
- X case '}':
- X if( s[1] != '}' ) {
- X /* error malformed macro expansion */
- X s++;
- X }
- X else { /* handle the }} case */
- X res = _strjoin( res, start, (s-start+1), TRUE );
- X s += 2;
- X start = s;
- X }
- X break;
- X
- X default: s++;
- X }
- }
- X
- X
- static char*
- _scan_macro( s, ps )/*
- ======================
- X This routine scans a macro use and expands it to the value. It
- X returns the macro's expanded value and modifies the pointer into the
- X src string to point at the first character after the macro use.
- X The types of uses recognized are:
- X
- X $$ - expands to $
- X $(name) - expands to value of name
- X ${name} - same as above
- X $($(name)) - recurses on macro names (any level)
- X and
- X $(func[,args ...] [data])
- X and
- X $(name:modifier_list:modifier_list:...)
- X
- X see comment for Expand for description of valid modifiers.
- X
- X NOTE that once a macro name bounded by ( or { is found only
- X the appropriate terminator (ie. ( or } is searched for. */
- X
- char *s; /* pointer to start of src string */
- char **ps; /* pointer to start pointer */
- {
- X char sdelim; /* start of macro delimiter */
- X char edelim; /* corresponding end macro delim */
- X char *start; /* start of prefix */
- X char *macro_name; /* temporary macro name */
- X char *recurse_name; /* recursive macro name */
- X char *result; /* result for macro expansion */
- X int bflag = 0; /* brace flag, ==0 => $A type macro */
- X int done = 0; /* != 0 => done macro search */
- X int lev = 0; /* brace level */
- X int mflag = 0; /* != 0 => modifiers present in mac */
- X int fflag = 0; /* != 0 => GNU style function */
- X HASHPTR hp; /* hash table pointer for macros */
- X
- X DB_ENTER( "_scan_macro" );
- X
- X /* Check for the simple $ at end of line case */
- X if( !*s ) {
- X *ps = s;
- X DB_RETURN( _strdup("") );
- X }
- X
- X if( *s == '$' ) { /* Take care of the simple $$ case. */
- X *ps = s+1;
- X DB_RETURN( _strdup("$") );
- X }
- X
- X sdelim = *s; /* set and remember start/end delim */
- X if( sdelim == '(' )
- X edelim = ')';
- X else
- X edelim = '}';
- X
- X start = s; /* build up macro name, find its end*/
- X while( !done ) {
- X switch( *s ) {
- X case '(': /* open macro brace */
- X case '{':
- X if( *s == sdelim ) {
- X lev++;
- X bflag++;
- X }
- X break;
- X
- X case ':': /* halt at modifier */
- X if( lev == 1 ) {
- X done = TRUE;
- X mflag = 1;
- X }
- X break;
- X
- X case ' ':
- X case '\t':
- X case '\n':
- X fflag = 1;
- X break;
- X
- X case '\0': /* check for null */
- X *ps = s;
- X if( lev ) {
- X done = TRUE;
- X bflag = 0;
- X s = start;
- X }
- X break;
- X
- X case ')': /* close macro brace */
- X case '}':
- X if( *s == edelim && lev ) --lev;
- X /*FALLTHROUGH*/
- X
- X default:
- X done = !lev;
- X }
- X s++;
- X }
- X
- X /* Check if this is a $A type macro. If so then we have to
- X * handle it a little differently. */
- X if( bflag )
- X macro_name = _substr( start+1, s-1 );
- X else
- X macro_name = _substr( start, s );
- X
- X /* Check to see if the macro name contains spaces, if so then treat it
- X * as a GNU style function invocation and call the function mapper to
- X * deal with it. */
- X if( fflag )
- X result = Exec_function(macro_name);
- X else {
- X /* Check if the macro is a recursive macro name, if so then
- X * EXPAND the name before expanding the value */
- X if( strchr( macro_name, '$' ) != NIL(char) ) {
- X recurse_name = Expand( macro_name );
- X FREE( macro_name );
- X macro_name = recurse_name;
- X }
- X
- X /* Code to do value expansion goes here, NOTE: macros whose assign bit
- X is one have been evaluated and assigned, they contain no further
- X expansions and thus do not need their values expanded again. */
- X
- X if( (hp = GET_MACRO( macro_name )) != NIL(HASH) ) {
- X if( hp->ht_flag & M_MARK )
- X Fatal( "Detected circular macro [%s]", hp->ht_name );
- X
- X /* for M_MULTI macro variable assignments */
- X If_multi = hp->ht_flag & M_MULTI;
- X
- X if( !(hp->ht_flag & M_EXPANDED) ) {
- X hp->ht_flag |= M_MARK;
- X result = Expand( hp->ht_value );
- X hp->ht_flag ^= M_MARK;
- X }
- X else if( hp->ht_value != NIL(char) )
- X result = _strdup( hp->ht_value );
- X else
- X result = _strdup( "" );
- X
- X /*
- X * Mark macros as used only if we are not expanding them for
- X * the purpose of a .IF test, so we can warn about redef after use*/
- X
- X if( !If_expand ) hp->ht_flag |= M_USED;
- X }
- X else
- X result = _strdup( "" );
- X }
- X
- X if( mflag ) {
- X char separator;
- X int modifier_list = 0;
- X int aug_mod = FALSE;
- X char *pat1;
- X char *pat2;
- X char *p;
- X
- X /* Yet another brain damaged AUGMAKE kludge. We should accept the
- X * AUGMAKE bullshit of $(f:pat=sub) form of macro expansion. In
- X * order to do this we will forgo the normal processing if the
- X * AUGMAKE solution pans out, otherwise we will try to process the
- X * modifiers ala dmake.
- X *
- X * So we look for = in modifier string.
- X * If found we process it and not do the normal stuff */
- X
- X for( p=s; *p && *p != '=' && *p != edelim; p++ );
- X
- X if( *p == '=' ) {
- X pat1 = _substr( s, p );
- X for( s=p=p+1; (*p != edelim); p++ );
- X
- X pat2 = _substr( s, p );
- X
- X if( !Augmake ) {
- X char *tmp = pat2;
- X pat2 = Expand(pat2);
- X FREE(tmp);
- X }
- X
- X result = Apply_edit( result, pat1, pat2, TRUE, TRUE );
- X FREE( pat1 );
- X FREE( pat2 );
- X s = p;
- X aug_mod = TRUE;
- X }
- X
- X if( !aug_mod )
- X while( *s && *s != edelim ) { /* while not at end of macro */
- X switch( *s++ ) {
- X case 'b':
- X case 'B': modifier_list |= FILE_FLAG; break;
- X
- X case 'd':
- X case 'D': modifier_list |= DIRECTORY_FLAG; break;
- X
- X case 'f':
- X case 'F': modifier_list |= FILE_FLAG | SUFFIX_FLAG; break;
- X
- X case 'S':
- X case 's':
- X if( modifier_list ) {
- X Warning( "Edit modifier must appear alone, ignored");
- X modifier_list = 0;
- X }
- X else {
- X separator = *s++;
- X for( p=s; *p != separator && *p != edelim; p++ );
- X
- X if( *p == edelim )
- X Warning("Syntax error in edit pattern, ignored");
- X else {
- X char *t1, *t2;
- X pat1 = _substr( s, p );
- X for(s=p=p+1; (*p != separator) && (*p != edelim); p++ );
- X pat2 = _substr( s, p );
- X t1 = Expand(pat1); FREE(pat1);
- X t2 = Expand(pat2); FREE(pat2);
- X result = Apply_edit( result, t1, t2, TRUE, FALSE );
- X FREE( t1 );
- X FREE( t2 );
- X }
- X s = p;
- X }
- X /* find the end of the macro spec, or the start of a new
- X * modifier list for further processing of the result */
- X
- X for( ; (*s != edelim) && (*s != ':'); s++ );
- X if( *s == ':' ) s++;
- X break;
- X
- X case 'T':
- X case 't':
- X if( modifier_list ) {
- X Warning( "Tokenize modifier must appear alone, ignored");
- X modifier_list = 0;
- X }
- X else {
- X char *msg = "Separator string must be quoted";
- X
- X separator = *s++;
- X
- X if( separator != '\"' )
- X Warning( msg );
- X else {
- X /* we change the semantics to allow $(v:t")") */
- X for (p = s; *p && *p != separator; p++)
- X if (*p == '\\')
- X if (p[1] == '\\' || p[1] == '"')
- X p++;
- X if( *p == 0 )
- X Fatal( "Unterminated separator string" );
- X else {
- X pat1 = _substr( s, p );
- X result = Tokenize( result, pat1 );
- X FREE( pat1 );
- X }
- X s = p;
- X }
- X
- X /* find the end of the macro spec, or the start of a new
- X * modifier list for further processing of the result */
- X
- X for( ; (*s != edelim) && (*s != ':'); s++ );
- X if( *s == ':' ) s++;
- X }
- X break;
- X
- X case ':':
- X if( modifier_list ) {
- X result = Apply_modifiers( modifier_list, result );
- X modifier_list = 0;
- X }
- X break;
- X
- X default:
- X Warning( "Illegal modifier in macro, ignored" );
- X break;
- X }
- X }
- X
- X if( modifier_list ) /* apply modifier */
- X result = Apply_modifiers( modifier_list, result );
- X
- X s++;
- X }
- X
- X *ps = s;
- X FREE( macro_name );
- X DB_RETURN( result );
- }
- X
- X
- static char*
- _scan_brace( s, ps, flag )/*
- ============================
- X This routine scans for { token_list } pairs. It expands the value of
- X token_list by calling Expand on it. Token_list may be anything at all.
- X Note that the routine count's ballanced parentheses. This means you
- X cannot have something like { fred { joe }, if that is what you really
- X need the write it as { fred {{ joe }, flag is set to 1 if all ok
- X and to 0 if the braces were unballanced. */
- X
- char *s;
- char **ps;
- int *flag;
- {
- X char *t;
- X char *start;
- X char *res;
- X int lev = 1;
- X int done = 0;
- X
- X DB_ENTER( "_scan_brace" );
- X
- X start = s;
- X while( !done )
- X switch( *s++ ) {
- X case '{':
- X if( *s == '{' ) break; /* ignore {{ */
- X lev++;
- X break;
- X
- X case '}':
- X if( *s == '}' ) break; /* ignore }} */
- X if( lev )
- X if( --lev == 0 ) done = TRUE;
- X break;
- X
- X case '$':
- X if( *s == '{' || *s == '}' ) {
- X if( (t = strchr(s,'}')) != NIL(char) )
- X s = t;
- X s++;
- X }
- X break;
- X
- X case '\0':
- X if( lev ) {
- X done = TRUE;
- X s--;
- X /* error malformed macro expansion */
- X }
- X break;
- X }
- X
- X start = _substr( start, (lev) ? s : s-1 );
- X
- X if( lev ) {
- X /* Braces were not ballanced so just return the string.
- X * Do not expand it. */
- X
- X res = _strjoin( "{", start, -1, FALSE );
- X *flag = 0;
- X }
- X else {
- X *flag = 1;
- X res = Expand( start );
- X
- X if( (t = _strspn( res, " \t" )) != res ) strcpy( res, t );
- X }
- X
- X FREE( start ); /* this is ok! start is assigned a _substr above */
- X *ps = s;
- X
- X DB_RETURN( res );
- }
- X
- X
- static char*
- _cross_prod( x, y )/*
- =====================
- X Given two strings x and y compute the cross-product of the tokens found
- X in each string. ie. if x = "a b" and y = "c d" return "ac ad bc bd".
- X
- X NOTE: buf will continue to grow until it is big enough to handle
- X all cross product requests. It is never freed! (maybe I
- X will fix this someday) */
- X
- char *x;
- char *y;
- {
- X static char *buf;
- X static int buf_siz = 0;
- X char *brkx;
- X char *brky;
- X char *cy;
- X char *cx;
- X char *res;
- X int i;
- X
- X if( *x && *y ) {
- X res = _strdup( "" ); cx = x;
- X while( *cx ) {
- X cy = y;
- X brkx = _strpbrk( cx, " \t\n" );
- X if( (brkx-cx == 2) && *cx == '\"' && *(cx+1) == '\"' ) cx = brkx;
- X
- X while( *cy ) {
- X brky = _strpbrk( cy, " \t\n" );
- X if( (brky-cy == 2) && *cy == '\"' && *(cy+1) == '\"' ) cy = brky;
- X i = brkx-cx + brky-cy + 2;
- X
- X if( i > buf_siz ) { /* grow buf to the correct size */
- X if( buf != NIL(char) ) FREE( buf );
- X if( (buf = MALLOC( i, char )) == NIL(char)) No_ram();
- X buf_siz = i;
- X }
- X
- X strncpy( buf, cx, (i = brkx-cx) );
- X buf[i] = '\0';
- SHAR_EOF
- true || echo 'restore of dmake/expand.c failed'
- fi
- echo 'End of part 8, continue with part 9'
- echo 9 > _shar_seq_.tmp
- exit 0
- exit 0 # Just in case...
-