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

hdrmacro.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 
00007 /*  This file is ALSO:
00008  *  (C) Copyright David Abrahams 2001. Permission to copy, use,
00009  *  modify, sell and distribute this software is granted provided this
00010  *  copyright notice appears in all copies. This software is provided
00011  *  "as is" without express or implied warranty, and with no claim as
00012  *  to its suitability for any purpose.
00013  */
00014 
00015 # include "jam.h"
00016 # include "lists.h"
00017 # include "parse.h"
00018 # include "compile.h"
00019 # include "rules.h"
00020 # include "variable.h"
00021 # include "regexp.h"
00022 # include "hdrmacro.h"
00023 # include "hash.h"
00024 # include "newstr.h"
00025 # include "strings.h"
00026 
00027 /*
00028  * hdrmacro.c - handle header files that define macros used in
00029  *              #include statements.
00030  *
00031  *  we look for lines like "#define MACRO  <....>" or '#define MACRO  "    "'
00032  *  in the target file. When found, we 
00033  *
00034  *  we then phony up a rule invocation like:
00035  *
00036  *      $(HDRRULE) <target> : <resolved included files> ;
00037  *
00038  * External routines:
00039  *    headers1() - scan a target for "#include MACRO" lines and try
00040  *                 to resolve them when needed
00041  *
00042  * Internal routines:
00043  *    headers1() - using regexp, scan a file and build include LIST
00044  *
00045  * 04/13/94 (seiwald) - added shorthand L0 for null list pointer
00046  * 09/10/00 (seiwald) - replaced call to compile_rule with evaluate_rule,
00047  *              so that headers() doesn't have to mock up a parse structure
00048  *              just to invoke a rule.
00049  */
00050 
00051 static LIST *header_macros1( LIST *l, char *file, int rec, regexp *re[] );
00052 
00053 /* this type is used to store a dictionary of file header macros */
00054 typedef struct header_macro
00055 {
00056   char*  symbol;
00057   char*  filename;  /* we could maybe use a LIST here ?? */
00058   
00059 } HEADER_MACRO;
00060  
00061 static struct hash*   header_macros_hash = 0;
00062 
00063 /*
00064  * headers() - scan a target for include files and call HDRRULE
00065  */
00066 
00067 # define MAXINC 10
00068 
00069 void
00070 macro_headers( TARGET *t )
00071 {
00072     static regexp *re = 0;
00073     FILE        *f;
00074     char        buf[ 1024 ];
00075     
00076     if ( DEBUG_HEADER )
00077         printf( "macro header scan for %s\n", t->name );
00078 
00079     /* this regexp is used to detect lines of the form       */
00080     /* "#define  MACRO  <....>" or "#define  MACRO  "....."  */
00081     /* in the header macro files..                           */
00082     if ( re == 0 )
00083     {
00084         re = regex_compile(
00085             "^[         ]*#[    ]*define[       ]*([A-Za-z][A-Za-z0-9_]*)[      ]*"
00086             "[<\"]([^\">]*)[\">].*$" );
00087     }
00088     
00089     if( !( f = fopen( t->boundname, "r" ) ) )
00090         return;
00091 
00092     while( fgets( buf, sizeof( buf ), f ) )
00093     {
00094         HEADER_MACRO  var, *v = &var;
00095 
00096         if ( regexec( re, buf ) && re->startp[1] )
00097         {
00098             /* we detected a line that looks like "#define  MACRO  filename */
00099             re->endp[1][0] = '\0';
00100             re->endp[2][0] = '\0';
00101         
00102             if ( DEBUG_HEADER )
00103                 printf( "macro '%s' used to define filename '%s' in '%s'\n",
00104                         re->startp[1], re->startp[2], t->boundname );
00105 
00106             /* add macro definition to hash table */
00107             if ( !header_macros_hash )
00108                 header_macros_hash = hashinit( sizeof( HEADER_MACRO ), "hdrmacros" );
00109 
00110             v->symbol   = re->startp[1];
00111             v->filename = 0;
00112             if ( hashenter( header_macros_hash, (HASHDATA **)&v ) )
00113             {
00114                 v->symbol   = newstr( re->startp[1] );  /* never freed */
00115                 v->filename = newstr( re->startp[2] );  /* never freed */
00116             }
00117             /* XXXX: FOR NOW, WE IGNORE MULTIPLE MACRO DEFINITIONS !! */
00118             /*       WE MIGHT AS WELL USE A LIST TO STORE THEM..      */
00119         }
00120     }
00121 
00122     fclose( f );
00123 }
00124 
00125 
00126 char*
00127 macro_header_get( const char*  macro_name )
00128 {
00129   HEADER_MACRO  var, *v = &var;
00130 
00131   v->symbol = (char*)macro_name;
00132 
00133   if( header_macros_hash && hashcheck( header_macros_hash, (HASHDATA **)&v ) )
00134   {
00135     if ( DEBUG_HEADER )
00136       printf( "### macro '%s' evaluated to '%s'\n", macro_name, v->filename );
00137     return v->filename;
00138   }
00139   return 0;  
00140 }
00141 

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