Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 279 lines (236 sloc) 6.266 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2012, The MacRuby Team. All rights reserved.
* Copyright (C) 2007-2011, Apple Inc. All rights reserved.
* Copyright (C) 1993-2007 Yukihiro Matsumoto
*/

#include "macruby_internal.h"
#include "ruby/node.h"
#include "vm.h"
#include "dln.h"

#include <stdlib.h>
#include <alloca.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <unistd.h>

#define FUNCNAME_PATTERN "Init_%s"

// In file.c
int eaccess(const char *path, int mode);

static size_t
init_funcname_len(char **buf, const char *file)
{
    char *p;
    const char *slash;
    size_t len;

    /* Load the file as an object one */
    for (slash = file-1; *file; file++) /* Find position of last '/' */
if (*file == '/') slash = file;

    len = strlen(FUNCNAME_PATTERN) + strlen(slash + 1);
    *buf = xmalloc(len);
    snprintf(*buf, len, FUNCNAME_PATTERN, slash + 1);
    for (p = *buf; *p; p++) { /* Delete suffix if it exists */
if (*p == '-') {
*p = '_';
}
    }
    for (p = *buf; *p; p++) { /* Delete suffix if it exists */
if (*p == '.') {
*p = '\0';
break;
}
    }
    return p - *buf;
}

#define init_funcname(buf, file) do {\
size_t len = init_funcname_len(buf, file);\
char *tmp = ALLOCA_N(char, len+1);\
if (tmp == NULL) {\
xfree(*buf);\
rb_memerror();\
}\
strlcpy(tmp, *buf, len + 1);\
xfree(*buf);\
*buf = tmp;\
} while (0)

#include <dlfcn.h>
#include <mach-o/dyld.h>

static const char *
dln_strerror(void)
{
    return (char*)dlerror();
}

bool ruby_is_miniruby = false;

// This function is called back from .rbo files' gcc constructors, passing a
// pointer to their entry point function, during dlopen(). The entry point
// function is called right after dlopen() directly. This is because C++
// exceptions raised within a gcc constructor are not properly propagated.
static void *__mrep__ = NULL;
void
rb_mrep_register(void *imp)
{
    __mrep__ = imp;
}

void*
dln_load(const char *file, bool call_init)
{
    if (ruby_is_miniruby) {
rb_raise(rb_eLoadError,
"miniruby can't load C extension bundles due to technical problems");
    }

    const char *error = 0;
#define DLN_ERROR() (error = dln_strerror(), strcpy(ALLOCA_N(char, strlen(error) + 1), error))

    char *buf;
    /* Load the file as an object one */
    init_funcname(&buf, file);

    {
void *handle;

/* Load file */
__mrep__ = NULL;
if ((handle = (void*)dlopen(file, RTLD_LAZY|RTLD_GLOBAL)) == NULL) {
error = dln_strerror();
goto failed;
}

if (call_init) {
void (*init_fct)();
init_fct = (void(*)())dlsym(handle, buf);
if (init_fct == NULL) {
error = DLN_ERROR();
dlclose(handle);
goto failed;
}
/* Call the init code */
rb_vm_dln_load(init_fct, NULL);
}
else {
if (__mrep__ == NULL) {
rb_raise(rb_eLoadError, "Can't load %s: entry point function not located (this can happen when you load twice the same .rbo file with a different case on a case-insensitive filesystem)", file);
}
rb_vm_dln_load(NULL, (IMP)__mrep__);
}

return handle;
    }

  failed:
    rb_loaderror("%s - %s", error, file);

    return 0; /* dummy return */
}

static char *dln_find_1(const char *fname, const char *path, char *buf, int size, int exe_flag);

char *
dln_find_exe_r(const char *fname, const char *path, char *buf, int size)
{
    if (path == NULL) {
path = getenv(PATH_ENV);
    }

    if (path == NULL) {
path = "/usr/local/bin:/usr/bin:/bin:.";
    }
    return dln_find_1(fname, path, buf, size, 1);
}

char *
dln_find_file_r(const char *fname, const char *path, char *buf, int size)
{
    if (path == NULL) {
path = ".";
    }
    return dln_find_1(fname, path, buf, size, 0);
}

static char fbuf[MAXPATHLEN];

char *
dln_find_exe(const char *fname, const char *path)
{
    return dln_find_exe_r(fname, path, fbuf, sizeof(fbuf));
}

char *
dln_find_file(const char *fname, const char *path)
{
    return dln_find_file_r(fname, path, fbuf, sizeof(fbuf));
}

static char *
dln_find_1(const char *fname, const char *path, char *fbuf, int size,
int exe_flag /* non 0 if looking for executable. */)
{
    register const char *dp;
    register const char *ep;
    register char *bp;
    struct stat st;

#define RETURN_IF(expr) if (expr) return (char *)fname;

    RETURN_IF(!fname);
    RETURN_IF(fname[0] == '/');
    RETURN_IF(strncmp("./", fname, 2) == 0 || strncmp("../", fname, 3) == 0);
    RETURN_IF(exe_flag && strchr(fname, '/'));

#undef RETURN_IF

    for (dp = path;; dp = ++ep) {
register int l;
int i;
int fspace;

/* extract a component */
ep = strchr(dp, PATH_SEP[0]);
if (ep == NULL) {
ep = dp+strlen(dp);
}

/* find the length of that component */
l = ep - dp;
bp = fbuf;
fspace = size - 2;
if (l > 0) {
/*
** If the length of the component is zero length,
** start from the current directory. If the
** component begins with "~", start from the
** user's $HOME environment variable. Otherwise
** take the path literally.
*/

if (*dp == '~' && (l == 1 || dp[1] == '/')) {
char *home;

home = getenv("HOME");
if (home != NULL) {
i = strlen(home);
if ((fspace -= i) < 0) {
goto toolong;
}
memcpy(bp, home, i);
bp += i;
}
dp++;
l--;
}
if (l > 0) {
if ((fspace -= l) < 0) {
goto toolong;
}
memcpy(bp, dp, l);
bp += l;
}

/* add a "/" between directory and filename */
if (ep[-1] != '/') {
*bp++ = '/';
}
}

/* now append the file name */
i = strlen(fname);
if ((fspace -= i) < 0) {
toolong:
fprintf(stderr, "openpath: pathname too long (ignored)\n");
*bp = '\0';
fprintf(stderr, "\tDirectory \"%s\"\n", fbuf);
fprintf(stderr, "\tFile \"%s\"\n", fname);
goto next;
}
memcpy(bp, fname, i + 1);

if (stat(fbuf, &st) == 0) {
if (!S_ISDIR(st.st_mode)
&& (exe_flag == 0 || eaccess(fbuf, X_OK) == 0)) {
return fbuf;
}
}

      next:
/* if not, and no other alternatives, life is bleak */
if (*ep == '\0') {
return NULL;
}

/* otherwise try the next component in the search path */
    }
}
Something went wrong with that request. Please try again.