PreviousUpNext

15.4.75  src/app/makelib/main/lib-load-path.pkg

## lib-load-path.pkg -- MYTHRYL_LIB_LOAD_PATH handling

# Compiled by:
#     src/app/makelib/makelib.sublib




stipulate
    package wnx =  winix__premicrothread;                                                       # winix__premicrothread                         is from   src/lib/std/winix--premicrothread.pkg
    package rex =  regex;                                                                       # regex                                         is from   src/lib/regex/regex.pkg

    getenv      =  wnx::process::get_env:   String -> Null_Or(String);
herein

    package lib_load_path
    :       Lib_Load_Path
    {
        default_lib_load_path
            =
            ".:$HOME/.mythryl/lib:/usr/lib/mythryl:/usr/local/lib/mythryl";


        fun split_string_pathlist_into_component_pathstrings  string_pathlist                   # ".:$HOME:/usr/lib/mythryl"  ->  [ ".", "$HOME", "/usr/lib/mythryl" ]
            =
            rex::find_all_matches_to_regex  ./[^:]+/  string_pathlist;                          # All maximal-length substrings not containing a ':'.


        fun normalize_dirname  dirname                                                          # Map "." to `pwd`, $HOME/foo to getenv(HOME) + "/foo" etc.
            =
            if (dirname == ".")
                wnx::file::current_directory ();
            else
                case (rex::find_first_match_to_regex_and_return_all_groups                      # See if dirname starts with "$HOME/..." or such.
                         ./^\$([A-Z0-9_]+)(.*)$/
                        dirname
                     )
                    #
                    THE [ envvar, rest ] => case (getenv envvar)
                                                #
                                                THE path => path + rest;
                                                NULL     => {   msg =  sprintf "environment var $%s in mythryl lib load path not defined!" envvar; 
                                                                log::fatal msg;
                                                                raise exception DIE msg;
                                                            };
                                            esac;
                    NULL                 => dirname;
                    _                    => raise exception DIE "impossible -- normalize_dirname in lib-load-path.pkg";
                esac;
            fi;

        fun get_lib_load_path ()
            =
            {   string_pathlist                                                                 # Say, ".:$HOME/.mythryl/lib:/usr/lib/mythryl:/usr/local/lib/mythryl";
                    =
                    case (getenv "MYTHRYL_LIB_LOAD_PATH")
                        #
                        THE path_as_string      =>  path_as_string;
                        NULL                    =>  default_lib_load_path;
                    esac;

                pathstrings                                                                     # Say, [ ".", "$HOME/.mythryl/lib", "/usr/lib/mythryl", "/usr/local/lib/mythryl" ]
                    =
                    split_string_pathlist_into_component_pathstrings  string_pathlist;

                pathstrings =  map  normalize_dirname  pathstrings;                             # Say, [ "/usr/src/mythryl", "/home/cynbe/.mythryl/lib", "/usr/lib/mythryl", "/usr/local/lib/mythryl" ]

                pathstrings;
            };

        fun search_lib_load_path_for_file                                                       # PUBLIC.
                (libname: String)                                                               # Say, "foo.lib"
            =
            {   directories =  get_lib_load_path ();                                            # Say, [ "/usr/src/mythryl", "/home/cynbe/.mythryl/lib", "/usr/lib/mythryl", "/usr/local/lib/mythryl" ]
                #
                if   (string::length_in_bytes libname     == 0  )   NULL;                       # Posix probably doesn't allow zero-length filenames.
                elif (string::get_byte_as_char (libname, 0) == '/')
                    #                                                                           # Filename starts with "/" so it is absolute -- skip directory search.
                    if (file::exists libname)   THE libname;
                    else                        NULL;
                    fi;
                else
                    dirname =   list::find  try_dir  directories                                # Over all directories in lib load path, if libname exists in that directory return it.
                                where
                                    fun try_dir dirname                                         # Return TRUE iff file 'libname' exists in directory 'dirname'.
                                        =
                                        file::exists (dirname + "/" + libname);
                                end;

                    case dirname
                        #
                        THE dirname     =>  THE (dirname + "/" + libname);
                        NULL            =>  NULL;
                    esac;
                fi; 
            };  
    };

end;




Comments and suggestions to: bugs@mythryl.org

PreviousUpNext