static ChimpRef * _chimp_os_getenv (ChimpRef *self, ChimpRef *args) { const char *value = getenv(CHIMP_STR_DATA(CHIMP_ARRAY_ITEM(args, 0))); if (value == NULL) { return chimp_nil; } else { return chimp_str_new (value, strlen(value)); } }
static ChimpRef * _chimp_io_readline (ChimpRef *self, ChimpRef *args) { char buf[1024]; size_t len; if (fgets (buf, sizeof(buf), stdin) == NULL) { return chimp_nil; } len = strlen(buf); if (len > 0 && buf[len-1] == '\n') { buf[--len] = '\0'; } return chimp_str_new (buf, len); }
static ChimpRef * _chimp_os_dirname (ChimpRef *self, ChimpRef *args) { ChimpRef *path = CHIMP_ARRAY_ITEM(args, 0); size_t i; const char *begin = CHIMP_STR_DATA(path); const char *end = begin + CHIMP_STR_SIZE(path); for (i = 0; i < CHIMP_STR_SIZE(path); i++) { end--; if (*end == '/') { return chimp_str_new (begin, (size_t) (end - begin)); } } return CHIMP_STR_NEW (""); }
static ChimpRef * _chimp_os_basename (ChimpRef *self, ChimpRef *args) { ChimpRef *path = CHIMP_ARRAY_ITEM(args, 0); size_t len = CHIMP_STR_SIZE(path); const char *s = CHIMP_STR_DATA(path) + len; if (len > 0) len--; while (len > 0) { if (*s == '/') { s++; break; } s--; len--; } return chimp_str_new (s, CHIMP_STR_SIZE(path) - len); }
static chimp_bool_t _parse_name (const char **ptr, ChimpRef **op) { const char *begin; const char *p = *ptr; size_t len = 0; _skip_whitespace (&p); begin = p; if (*p != '_' && !isalpha(*p)) { return CHIMP_FALSE; } while (*p && (*p == '_' || isalnum(*p))) { len++; p++; } if (len == 0) return CHIMP_FALSE; *op = chimp_str_new (begin, len); if (*op == NULL) { return CHIMP_FALSE; } *ptr = p; return CHIMP_TRUE; }
static ChimpRef * parse_args (int argc, char **argv) { int i; ChimpRef *args; ChimpRef *argv_ = chimp_array_new (); for (i = 1; i < argc; i++) { ChimpRef *arg = chimp_str_new (argv[i], strlen(argv[i])); if (arg == NULL) { return NULL; } if (!chimp_array_push (argv_, arg)) { return NULL; } } args = chimp_array_new (); if (args == NULL) { return NULL; } if (!chimp_array_push (args, argv_)) { return NULL; } return args; }
static chimp_bool_t _parse_const_str (const char **ptr, ChimpRef **value) { const char *begin; const char *p = *ptr; size_t len = 0; _skip_whitespace (&p); p++; begin = p; while (*p && *p != '"') { len++; p++; } if (!*p) { return CHIMP_FALSE; } p++; *value = chimp_str_new (begin, len); if (!*value) { return CHIMP_FALSE; } *ptr = p; return CHIMP_TRUE; }
chimp_bool_t chimp_hash_put_str (ChimpRef *self, const char *key, ChimpRef *value) { return chimp_hash_put (self, chimp_str_new (key, strlen(key)), value); }