According to Geoff Hutchison:
> On Fri, 18 Aug 2000, Gilles Detillieux wrote:
> > I don't think YY_CURRENT_BUFFER has the file name.  Only the FILE pointer,
> > a buffer and some state info.  You'd need to have a separate name stack,
> > using the same index, to save and pop off any file names.  I think the
> > initial file name, for the top level config file, get saved and it may
> > be accessible by a config.getFileName(), so the conf_lexer.yxx code
> > will need to use that for level 0, and put the name on the stack for
> > subsequent includes.  There isn't a method for setting the file name
> > outside the HtConfiguration class, nor do I think there should be (but
> > I may be wrong on that point).  I'll code it if you'll test it.  :-)
> 
> Yes, you're right. You don't want to put the names on the stack as it
> stands now because then you'll have to "remeber" where you were. So it
> puts the current state of the buffer.
> 
> I'm always willing to test. But I think it'd be better to put the
> *directory* and not the filename into the new stack. It's easy enough to
> test if the include filename is relative and adjust the current directory
> accordingly. The less directory-path munging we have to do, the better.
> (Easier to read, faster and less bug-prone, IMHO.)

I think you'd still have to do the same directory path munging in
either case.  The only difference I see is whether you do it before
or after the name is pushed onto the stack.  For the initial state,
you need to get the filename elsewhere anyway, so you need to strip the
directory name at that point.  It makes sense to always do the munging
just before opening the file.

> So if you write up something you think will work, I'll test it tonight. If
> not, I'll try this approach as well.

Here you go.  Note that I also changed the rather severe exit calls to
return(T_NEWLINE) instead.  I'm not completely certain that's the right
action to do in this case, but it seems more resonable than abruptly
dying.

Index: htcommon/conf_lexer.lxx
===================================================================
RCS file: /opt/htdig/cvs/htdig3/htcommon/conf_lexer.lxx,v
retrieving revision 1.1.2.9
diff -u -p -r1.1.2.9 conf_lexer.lxx
--- htcommon/conf_lexer.lxx     2000/08/18 05:24:13     1.1.2.9
+++ htcommon/conf_lexer.lxx     2000/08/18 20:20:51
@@ -36,6 +36,7 @@
 #include "conf_parser.h"
 #define MAX_INCLUDE_DEPTH 10
 YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
+String *name_stack[MAX_INCLUDE_DEPTH];
 int include_stack_ptr = 0;
 extern HtConfiguration config;
 %}
@@ -110,21 +111,44 @@ include[ \t]*:            BEGIN(incl);
                        if ( include_stack_ptr >= MAX_INCLUDE_DEPTH )
                            {
                            fprintf(stderr,"Includes nested too deeply\n");
-                           exit(1);
+                           // exit(1); // Seems too harsh!
+                           return(T_NEWLINE);
                            }
                        include_stack[include_stack_ptr++] =
                            YY_CURRENT_BUFFER;
 
                        // handle ${var} in file name
-                       String ParsedFilename = \
-                       config.ParseString(yytext);
+                       String ParsedFilename =
+                               config.ParseString(yytext);
 
+                       if (ParsedFilename[0] != '/')
+                           {   // Given file name not fully qualified
+                               // so strip dir. name from current one
+                           String str;
+                           if (include_stack_ptr > 1)
+                               str = *name_stack[include_stack_ptr-2];
+                           else        // still at top level config
+                               str = config.getFileName();
+                           int len = str.lastIndexOf('/') + 1;
+                           if (len > 0)
+                               {       // Current name has directory path
+                                       // component, so use it for new name
+                               str.chop(str.length() - len);
+                               str << ParsedFilename;
+                               ParsedFilename = str;
+                               }
+                           }
+
                        yyin = fopen( ParsedFilename.get(), "r" );
 
                        if ( ! yyin ) {
                            fprintf(stderr,"can't find file: %s\n",yytext);
-                           exit(1);
+                           // exit(1); // Seems too harsh!
+                           include_stack_ptr--;
+                           return(T_NEWLINE);
                        }
+                       name_stack[include_stack_ptr-1] =
+                                       new String(ParsedFilename.get());
                        yy_switch_to_buffer( yy_create_buffer( yyin, YY_BUF_SIZE ) );
 
                        BEGIN(INITIAL);
@@ -135,6 +159,7 @@ include[ \t]*:              BEGIN(incl);
                            yyterminate();
                        else
                            {
+                           delete name_stack[include_stack_ptr-1];
                            yy_delete_buffer( YY_CURRENT_BUFFER );
                            yy_switch_to_buffer(
                                 include_stack[--include_stack_ptr] );

-- 
Gilles R. Detillieux              E-mail: <[EMAIL PROTECTED]>
Spinal Cord Research Centre       WWW:    http://www.scrc.umanitoba.ca/~grdetil
Dept. Physiology, U. of Manitoba  Phone:  (204)789-3766
Winnipeg, MB  R3E 3J7  (Canada)   Fax:    (204)789-3930

------------------------------------
To unsubscribe from the htdig3-dev mailing list, send a message to
[EMAIL PROTECTED] 
You will receive a message to confirm this. 


Reply via email to