cvsuser     04/05/26 12:14:34

  Modified:    imcc     imcc.l imclexer.c parser_util.c
               include/parrot library.h packfile.h
               ops      core.ops
               src      library.c packfile.c
  Log:
  - refactored the include file locating C code
  - added Parrot_library_fallback_locate
    it locates a file with a given name in a list of directories
  - the C code path now correctly locates include files in runtime/parrot/library
  - all tests are passing even if the old library directory is removed
  - added Parrot_imcc_include_paths, an array of strings holding the default include 
directories to imcc/parser_util.c
  - removed Parrot_load_bytecode_direct, Parrot_load_bytecode now takes a const char* 
file name
  - Parrot_load_bytecode does not use parrotlib code to locate the specified file; 
this has to be done by the bytecode loader or user supplied compiler
  - small fix: PackFile_append_pbc has now const char *filenamed
  
  Revision  Changes    Path
  1.105     +14 -50    parrot/imcc/imcc.l
  
  Index: imcc.l
  ===================================================================
  RCS file: /cvs/public/parrot/imcc/imcc.l,v
  retrieving revision 1.104
  retrieving revision 1.105
  diff -u -w -r1.104 -r1.105
  --- imcc.l    26 May 2004 12:51:11 -0000      1.104
  +++ imcc.l    26 May 2004 19:14:21 -0000      1.105
  @@ -814,63 +814,27 @@
   /* _PARROTLIB is now the default; XXX: "make testr" is not working yet */
   /*#define _PARROTLIB*/
   
  -/* XXX: use this code for miniparrot */
  -#if !defined(_PARROTLIB)
  -static FILE*
  -open_file (char *file_name, const char **incl)
  -{
  -    FILE* file = 0;
  -    char *s;
  -    const char** ptr;
  -    int length = 0;
  -    int i;
  -
  -    /* calculate the length of the largest include directory */
  -    for( ptr = incl; *ptr != 0; ++ptr ) {
  -     i = strlen(*ptr);
  -     length = (i > length) ? i : length;
  -    }
  -
  -    s = malloc(strlen(file_name) + length + 1);
  -
  -    for( ptr = incl; (file == 0) && (*ptr != 0); ++ptr ) {
  -     strcpy(s, *ptr);
  -     strcat(s, file_name);
  -
  -#ifdef WIN32
  -        {
  -            char *p;
  -            while ( (p = strchr(s, '/')) )
  -                *p = '\\';
  -        }
  -#endif
  -     file = fopen(s, "r");
  -    }
  -
  -    if (file)
  -        sourcefile = strdup(s); /* FIXME: leak */
  -
  -    free(s);
  -
  -    return file;
  -}
  -#endif
  -
   static void
   include_file (void* interp, char *file_name)
   {
       struct macro_frame_t *frame;
       FILE *file = 0;
       char *ext;
  +
  +/* XXX: use this code for miniparrot */
   #if !defined(_PARROTLIB)
  -    const char *incl_paths[] = {
  -     "./",
  -     "runtime/parrot/include/",
  -     "runtime/parrot/",
  -     0,
  -    };
  +    extern const char* Parrot_imcc_include_paths[];
  +    STRING *str = Parrot_library_fallback_locate(interp, file_name, 
Parrot_imcc_include_paths);
   
  -    file = open_file(file_name, incl_paths);
  +    if (str) {
  +     char* s = string_to_cstring(interp, str);
  +     
  +     file = fopen(s, "r");
  +     if (file)
  +         sourcefile = s;
  +     else
  +         string_cstring_free(s);
  +    }
   #else
       STRING* name = string_from_cstring(interp, file_name, strlen(file_name));
       STRING* result = Parrot_library_query(interp, "include_file_location", name);
  
  
  
  1.109     +14 -50    parrot/imcc/imclexer.c
  
  Index: imclexer.c
  ===================================================================
  RCS file: /cvs/public/parrot/imcc/imclexer.c,v
  retrieving revision 1.108
  retrieving revision 1.109
  diff -u -w -r1.108 -r1.109
  --- imclexer.c        26 May 2004 12:51:11 -0000      1.108
  +++ imclexer.c        26 May 2004 19:14:21 -0000      1.109
  @@ -2,7 +2,7 @@
   /* A lexical scanner generated by flex */
   
   /* Scanner skeleton version:
  - * $Header: /cvs/public/parrot/imcc/imclexer.c,v 1.108 2004/05/26 12:51:11 jrieks 
Exp $
  + * $Header: /cvs/public/parrot/imcc/imclexer.c,v 1.109 2004/05/26 19:14:21 jrieks 
Exp $
    */
   
   #define FLEX_SCANNER
  @@ -3599,63 +3599,27 @@
   /* _PARROTLIB is now the default; XXX: "make testr" is not working yet */
   /*#define _PARROTLIB*/
   
  -/* XXX: use this code for miniparrot */
  -#if !defined(_PARROTLIB)
  -static FILE*
  -open_file (char *file_name, const char **incl)
  -{
  -    FILE* file = 0;
  -    char *s;
  -    const char** ptr;
  -    int length = 0;
  -    int i;
  -
  -    /* calculate the length of the largest include directory */
  -    for( ptr = incl; *ptr != 0; ++ptr ) {
  -     i = strlen(*ptr);
  -     length = (i > length) ? i : length;
  -    }
  -
  -    s = malloc(strlen(file_name) + length + 1);
  -
  -    for( ptr = incl; (file == 0) && (*ptr != 0); ++ptr ) {
  -     strcpy(s, *ptr);
  -     strcat(s, file_name);
  -
  -#ifdef WIN32
  -        {
  -            char *p;
  -            while ( (p = strchr(s, '/')) )
  -                *p = '\\';
  -        }
  -#endif
  -     file = fopen(s, "r");
  -    }
  -
  -    if (file)
  -        sourcefile = strdup(s); /* FIXME: leak */
  -
  -    free(s);
  -
  -    return file;
  -}
  -#endif
  -
   static void
   include_file (void* interp, char *file_name)
   {
       struct macro_frame_t *frame;
       FILE *file = 0;
       char *ext;
  +
  +/* XXX: use this code for miniparrot */
   #if !defined(_PARROTLIB)
  -    const char *incl_paths[] = {
  -     "./",
  -     "runtime/parrot/include/",
  -     "runtime/parrot/",
  -     0,
  -    };
  +    extern const char* Parrot_imcc_include_paths[];
  +    STRING *str = Parrot_library_fallback_locate(interp, file_name, 
Parrot_imcc_include_paths);
   
  -    file = open_file(file_name, incl_paths);
  +    if (str) {
  +     char* s = string_to_cstring(interp, str);
  +     
  +     file = fopen(s, "r");
  +     if (file)
  +         sourcefile = s;
  +     else
  +         string_cstring_free(s);
  +    }
   #else
       STRING* name = string_from_cstring(interp, file_name, strlen(file_name));
       STRING* result = Parrot_library_query(interp, "include_file_location", name);
  
  
  
  1.68      +24 -5     parrot/imcc/parser_util.c
  
  Index: parser_util.c
  ===================================================================
  RCS file: /cvs/public/parrot/imcc/parser_util.c,v
  retrieving revision 1.67
  retrieving revision 1.68
  diff -u -w -r1.67 -r1.68
  --- parser_util.c     25 May 2004 08:34:15 -0000      1.67
  +++ parser_util.c     26 May 2004 19:14:21 -0000      1.68
  @@ -29,6 +29,14 @@
    */
   void imcc_init(Parrot_Interp interpreter);
   
  +/* includes to use if parrotlib is not available */
  +const char *Parrot_imcc_include_paths[] = {
  +    "./",
  +    "runtime/parrot/include/",
  +    "runtime/parrot/",
  +    0,
  +};
  +
   /*
    * P = new type, [init]
    * PASM like:
  @@ -508,7 +516,7 @@
       struct PackFile *pf_save = interp->code;
       struct PackFile *pf;
       const char *source = sourcefile;
  -    char *ext;
  +    char *ext, *fullname;
       int pasm = pasm_file;
       FILE *new;
       union {
  @@ -516,13 +524,23 @@
           void * __ptr;
       } __ptr_u;
   
  -    if (!(new = fopen(s, "r"))) {
  -        fatal(1, "imcc_compile_file", "couldn't open '%s'\n", s);
  +#if defined(_PARROTLIB)
  +    STRING *str = string_from_cstring(interp, s, strlen(s));
  +    STRING *path = Parrot_library_query(interp, "imcc_compile_file_location", str);
  +#else
  +    STRING *str = Parrot_library_fallback_locate(interp, s, 
Parrot_imcc_include_paths);
  +#endif
  +    if (str) {
  +     fullname = string_to_cstring(interp, str);
  +    }
  +    if (!str || !(new = fopen(fullname, "r"))) {
  +        fatal(1, "imcc_compile_file", "couldn't open '%s'\n", fullname);
  +     string_cstring_free(fullname);
           return NULL;
       }
   
   #if IMC_TRACE
  -    fprintf(stderr, "parser_util.c: imcc_compile_file '%s'\n", s);
  +    fprintf(stderr, "parser_util.c: imcc_compile_file '%s'\n", fullname);
   #endif
   
       cur_namespace = NULL;
  @@ -530,7 +548,7 @@
       pf = PackFile_new(0);
       interp->code = pf;  /* put new packfile in place */
       sourcefile = const_cast(s);
  -    ext = strrchr(s, '.');
  +    ext = strrchr(fullname, '.');
       if (ext && strcmp (ext, ".pasm") == 0) {
           pasm_file = 1;
       }
  @@ -547,6 +565,7 @@
       sourcefile = source;
       pasm_file = pasm;
       fclose(new);
  +    string_cstring_free(fullname);
       return pf;
   }
   
  
  
  
  1.2       +2 -1      parrot/include/parrot/library.h
  
  Index: library.h
  ===================================================================
  RCS file: /cvs/public/parrot/include/parrot/library.h,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -w -r1.1 -r1.2
  --- library.h 19 May 2004 09:32:16 -0000      1.1
  +++ library.h 26 May 2004 19:14:28 -0000      1.2
  @@ -1,7 +1,7 @@
   /* library.h
    *  Copyright: 2004 The Perl Foundation.  All Rights Reserved.
    *  CVS Info
  - *     $Id: library.h,v 1.1 2004/05/19 09:32:16 jrieks Exp $
  + *     $Id: library.h,v 1.2 2004/05/26 19:14:28 jrieks Exp $
    *  Overview:
    *      Contains accessor functions for the _parrotlib bytecode interface
    *  Data Structure and Algorithms:
  @@ -14,6 +14,7 @@
   #define PARROT_LIBRARY_H_GUARD
   
   void* Parrot_library_query(Parrot_Interp, const char *func_name, ...);
  +STRING* Parrot_library_fallback_locate(Parrot_Interp interp, const char *file_name, 
const char **incl);
   
   #endif /* PARROT_LIBRARY_H_GUARD */
   
  
  
  
  1.62      +2 -3      parrot/include/parrot/packfile.h
  
  Index: packfile.h
  ===================================================================
  RCS file: /cvs/public/parrot/include/parrot/packfile.h,v
  retrieving revision 1.61
  retrieving revision 1.62
  diff -u -w -r1.61 -r1.62
  --- packfile.h        19 May 2004 21:08:59 -0000      1.61
  +++ packfile.h        26 May 2004 19:14:28 -0000      1.62
  @@ -1,6 +1,6 @@
   /* packfile.h
   *
  -* $Id: packfile.h,v 1.61 2004/05/19 21:08:59 jrieks Exp $
  +* $Id: packfile.h,v 1.62 2004/05/26 19:14:28 jrieks Exp $
   *
   * History:
   *  Rework by Melvin; new bytecode format, make bytecode portable.
  @@ -246,8 +246,7 @@
   struct PackFile_Segment * PackFile_Segment_new_seg(struct PackFile_Directory *,
           UINTVAL type, const char *name, int add);
   
  -void Parrot_load_bytecode(struct Parrot_Interp *, STRING *filename);
  -void Parrot_load_bytecode_direct(struct Parrot_Interp *, char *filename);
  +void Parrot_load_bytecode(struct Parrot_Interp *, const char *filename);
   /*
   ** PackFile_Segment Functions:
   */
  
  
  
  1.363     +4 -2      parrot/ops/core.ops
  
  Index: core.ops
  ===================================================================
  RCS file: /cvs/public/parrot/ops/core.ops,v
  retrieving revision 1.362
  retrieving revision 1.363
  diff -u -w -r1.362 -r1.363
  --- core.ops  24 May 2004 15:21:13 -0000      1.362
  +++ core.ops  26 May 2004 19:14:31 -0000      1.363
  @@ -141,7 +141,9 @@
   }
   
   inline op load_bytecode(in STR) :load_file {
  -  Parrot_load_bytecode(interpreter, $1);
  +    char * file = string_to_cstring(interpreter, $1);
  +    Parrot_load_bytecode(interpreter, file);
  +    string_cstring_free(file);
     goto NEXT();
   }
   
  
  
  
  1.5       +40 -2     parrot/src/library.c
  
  Index: library.c
  ===================================================================
  RCS file: /cvs/public/parrot/src/library.c,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -w -r1.4 -r1.5
  --- library.c 25 May 2004 13:50:18 -0000      1.4
  +++ library.c 26 May 2004 19:14:34 -0000      1.5
  @@ -1,6 +1,6 @@
   /*
   Copyright: 2004 The Perl Foundation.  All Rights Reserved.
  -$Id: library.c,v 1.4 2004/05/25 13:50:18 jrieks Exp $
  +$Id: library.c,v 1.5 2004/05/26 19:14:34 jrieks Exp $
   
   =head1 NAME
   
  @@ -37,7 +37,7 @@
   {
       /* XXX TODO: file location not known at runtime, should
          be linked with parrot (or use the upcoming config system) */
  -    Parrot_load_bytecode_direct(interpreter, 
"runtime/parrot/include/parrotlib.pbc");
  +    Parrot_load_bytecode(interpreter, "runtime/parrot/include/parrotlib.pbc");
   }
   
   /*
  @@ -101,6 +101,44 @@
       return ret;
   }
   
  +STRING*
  +Parrot_library_fallback_locate(Parrot_Interp interp, const char *file_name, const 
char **incl)
  +{
  +    char *s;
  +    const char** ptr;
  +    int length = 0;
  +    int i, ok = 0;
  +    STRING *str;
  +    
  +    /* calculate the length of the largest include directory */
  +    for( ptr = incl; *ptr != 0; ++ptr ) {
  +        i = strlen(*ptr);
  +        length = (i > length) ? i : length;
  +    }
  +
  +    s = malloc(strlen(file_name) + length + 1);
  +
  +    for( ptr = incl; (!ok) && (*ptr != 0); ++ptr ) {
  +        strcpy(s, *ptr);
  +        strcat(s, file_name);
  +
  +#ifdef WIN32
  +        {
  +            char *p;
  +            while ( (p = strchr(s, '/')) )
  +                *p = '\\';
  +        }
  +#endif
  +     str = string_from_cstring(interp, s, strlen(s));
  +     ok = Parrot_stat_info_intval(interp, str, STAT_EXISTS);
  +    }
  +    if (!ok) {
  +     str = NULL;
  +    }
  +    free( s );
  +    return str;
  +}
  +
   /*
   
   =back
  
  
  
  1.163     +5 -22     parrot/src/packfile.c
  
  Index: packfile.c
  ===================================================================
  RCS file: /cvs/public/parrot/src/packfile.c,v
  retrieving revision 1.162
  retrieving revision 1.163
  diff -u -w -r1.162 -r1.163
  --- packfile.c        26 May 2004 09:36:39 -0000      1.162
  +++ packfile.c        26 May 2004 19:14:34 -0000      1.163
  @@ -2,7 +2,7 @@
   Copyright (C) 2001-2002 Gregor N. Purdy. All rights reserved.
   This program is free software. It is subject to the same license as
   Parrot itself.
  -$Id: packfile.c,v 1.162 2004/05/26 09:36:39 leo Exp $
  +$Id: packfile.c,v 1.163 2004/05/26 19:14:34 jrieks Exp $
   
   =head1 NAME
   
  @@ -3024,7 +3024,7 @@
   /*
   
   =item C<static struct PackFile *
  -PackFile_append_pbc(Interp *interpreter, char *filename)>
  +PackFile_append_pbc(Interp *interpreter, const char *filename)>
   
   Read a PBC and append it to the current directory
   Fixup local label and sub addresses in newly loaded bytecode.
  @@ -3034,7 +3034,7 @@
   */
   
   static struct PackFile *
  -PackFile_append_pbc(Interp *interpreter, char *filename)
  +PackFile_append_pbc(Interp *interpreter, const char *filename)
   {
       struct PackFile * pf = Parrot_readbc(interpreter, filename);
       if (!pf)
  @@ -3059,26 +3059,9 @@
   */
   
   void
  -Parrot_load_bytecode(Interp *interpreter, STRING *name)
  +Parrot_load_bytecode(Interp *interpreter, const char *filename)
   {
  -    char *ext;
  -    char* filename;
  -
  -#if TRACE_PACKFILE
  -    fprintf(stderr, "packfile.c: parrot_load_bytecode()\n");
  -#endif
  -
  -#if defined(_PARROTLIB)
  -    name = Parrot_library_query(interpreter, "bytecode_location", name );
  -#endif
  -    filename = string_to_cstring(interpreter, name);
  -    Parrot_load_bytecode_direct(interpreter, filename);
  -}
  -
  -void
  -Parrot_load_bytecode_direct(Interp *interpreter, char *filename)
  -{
  -    char *ext;
  +    const char *ext;
   
   #if TRACE_PACKFILE
       fprintf(stderr, "packfile.c: parrot_load_bytecode()\n");
  
  
  

Reply via email to