The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

headers.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 1993, 2000 Christopher Seiwald.
00003  *
00004  * This file is part of Jam - see jam.c for Copyright information.
00005  */
00006 /*  This file is ALSO:
00007  *  (C) Copyright David Abrahams 2001. Permission to copy, use,
00008  *  modify, sell and distribute this software is granted provided this
00009  *  copyright notice appears in all copies. This software is provided
00010  *  "as is" without express or implied warranty, and with no claim as
00011  *  to its suitability for any purpose.
00012  */
00013 
00014 # include "jam.h"
00015 # include "lists.h"
00016 # include "parse.h"
00017 # include "compile.h"
00018 # include "rules.h"
00019 # include "variable.h"
00020 # include "regexp.h"
00021 # include "headers.h"
00022 # include "hdrmacro.h"
00023 # include "newstr.h"
00024 
00025 #ifdef OPT_HEADER_CACHE_EXT
00026 # include "hcache.h"
00027 #endif
00028 
00029 /*
00030  * headers.c - handle #includes in source files
00031  *
00032  * Using regular expressions provided as the variable $(HDRSCAN), 
00033  * headers() searches a file for #include files and phonies up a
00034  * rule invocation:
00035  * 
00036  *      $(HDRRULE) <target> : <include files> ;
00037  *
00038  * External routines:
00039  *    headers() - scan a target for include files and call HDRRULE
00040  *
00041  * Internal routines:
00042  *    headers1() - using regexp, scan a file and build include LIST
00043  *
00044  * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
00045  * 09/10/00 (seiwald) - replaced call to compile_rule with evaluate_rule,
00046  *              so that headers() doesn't have to mock up a parse structure
00047  *              just to invoke a rule.
00048  */
00049 
00050 #ifndef OPT_HEADER_CACHE_EXT
00051 static LIST *headers1( LIST *l, char *file, int rec, regexp *re[]);
00052 #endif
00053 
00054 /*
00055  * headers() - scan a target for include files and call HDRRULE
00056  */
00057 
00058 # define MAXINC 10
00059 
00060 void
00061 headers( TARGET *t )
00062 {
00063     LIST        *hdrscan;
00064     LIST        *hdrrule;
00065     LIST        *headlist = 0;
00066     regexp      *re[ MAXINC ];
00067     int rec = 0;
00068         
00069     if( !( hdrscan = var_get( "HDRSCAN" ) ) || 
00070         !( hdrrule = var_get( "HDRRULE" ) ) )
00071         return;
00072 
00073     if( DEBUG_HEADER )
00074         printf( "header scan %s\n", t->name );
00075 
00076     /* Compile all regular expressions in HDRSCAN */
00077 
00078     while( rec < MAXINC && hdrscan )
00079     {
00080         re[rec++] = regex_compile( hdrscan->string );
00081         hdrscan = list_next( hdrscan );
00082     }
00083 
00084     /* Doctor up call to HDRRULE rule */
00085     /* Call headers1() to get LIST of included files. */
00086     {
00087         FRAME   frame[1];
00088         frame_init( frame );
00089         lol_add( frame->args, list_new( L0, t->name ) );
00090 #ifdef OPT_HEADER_CACHE_EXT
00091         lol_add( frame->args, hcache( t, rec, re, hdrscan ) );
00092 #else
00093         lol_add( frame->args, headers1( headlist, t->boundname, rec, re ) );
00094 #endif
00095 
00096         if( lol_get( frame->args, 1 ) )
00097             evaluate_rule( hdrrule->string, frame );
00098 
00099         /* Clean up */
00100 
00101         frame_free( frame );
00102     }
00103 }
00104 
00105 /*
00106  * headers1() - using regexp, scan a file and build include LIST
00107  */
00108 
00109 #ifdef OPT_HEADER_CACHE_EXT
00110 LIST *
00111 #else
00112 static LIST *
00113 #endif
00114 headers1( 
00115         LIST    *l,
00116         char    *file,
00117         int     rec,
00118         regexp  *re[] )
00119 {
00120         FILE    *f;
00121         char    buf[ 1024 ];
00122         int             i;
00123         static regexp *re_macros = 0;
00124 
00125         
00126 #ifdef OPT_IMPROVED_PATIENCE_EXT
00127         static int count = 0;
00128         ++count;
00129         if ( ((count == 100) || !( count % 1000 )) && DEBUG_MAKE )
00130             printf("...patience...\n");
00131 #endif
00132         
00133         /* the following regexp is used to detect cases where a  */
00134         /* file is included through a line line "#include MACRO" */
00135         if ( re_macros == 0 )
00136         {
00137             re_macros = regex_compile(
00138                 "^[     ]*#[    ]*include[      ]*([A-Za-z][A-Za-z0-9_]*).*$" );
00139         }
00140 
00141 
00142         if( !( f = fopen( file, "r" ) ) )
00143             return l;
00144 
00145         while( fgets( buf, sizeof( buf ), f ) )
00146         {
00147             for( i = 0; i < rec; i++ )
00148                 if( regexec( re[i], buf ) && re[i]->startp[1] )
00149             {
00150                 re[i]->endp[1][0] = '\0';
00151 
00152                 if( DEBUG_HEADER )
00153                     printf( "header found: %s\n", re[i]->startp[1] );
00154 
00155                 l = list_new( l, newstr( re[i]->startp[1] ) );
00156             }
00157             
00158             /* special treatment for #include MACRO */
00159             if ( regexec( re_macros, buf ) && re_macros->startp[1] )
00160             {
00161               char*  header_filename;
00162               
00163               re_macros->endp[1][0] = '\0';
00164               
00165               if ( DEBUG_HEADER )
00166                 printf( "macro header found: %s", re_macros->startp[1] );
00167                 
00168               header_filename = macro_header_get( re_macros->startp[1] );
00169               if (header_filename)
00170               {
00171                 if ( DEBUG_HEADER )
00172                   printf( " resolved to '%s'\n", header_filename );
00173                 l = list_new( l, newstr( header_filename ) );
00174               }
00175               else
00176               {
00177                 if ( DEBUG_HEADER )
00178                   printf( " ignored !!\n" );
00179               }
00180             }
00181         }
00182 
00183         fclose( f );
00184 
00185         return l;
00186 }
00187 
00188 void
00189 regerror( char *s )
00190 {
00191         printf( "re error %s\n", s );
00192 }

Generated on Mon Nov 8 17:07:35 2004 for MPT by  doxygen 1.3.9.1