/** * Returns the connection string for the current database, or nil when * there is none. If there are multiple connections defined, only the * first is returned. */ str SABAOTHgetLocalConnection(str *ret) { char data[8096]; sabdb *stats; str err; err = msab_getMyStatus(&stats); if (err != NULL) excFromMem(MAL, "sabaoth.getlocalconnection", err); if (stats == NULL || stats->conns == NULL || stats->conns->val == NULL) { *ret = GDKstrdup(str_nil); } else { if (stats->conns->val[15] == '/') { snprintf(data, sizeof(data), "%s?database=%s", stats->conns->val, stats->dbname); } else { snprintf(data, sizeof(data), "%s%s", stats->conns->val, stats->dbname); } *ret = GDKstrdup(data); } if (stats != NULL) SABAOTHfreeStatus(&stats); return(MAL_SUCCEED); }
str BKCgetAccess(str *res, const bat *bid) { BAT *b; if ((b = BATdescriptor(*bid)) == NULL) throw(MAL, "bat.getAccess", RUNTIME_OBJECT_MISSING); switch (BATgetaccess(b)) { case BAT_READ: *res = GDKstrdup("read"); break; case BAT_APPEND: *res = GDKstrdup("append"); break; case BAT_WRITE: *res = GDKstrdup("write"); break; default: /* cannot happen, just here to help analysis tools */ *res = GDKstrdup(str_nil); break; } BBPunfix(b->batCacheid); return MAL_SUCCEED; }
str JSONkeyArray(json *ret, json *js) { char *result = NULL; str r; int i; JSON *jt; jt = JSONparse(*js, FALSE); // already validated CHECK_JSON(jt); if (jt->elm[0].kind == JSON_OBJECT) for (i = jt->elm[0].next; i; i = jt->elm[i].next) { r = GDKzalloc(jt->elm[i].valuelen + 3); if (jt->elm[i].valuelen) strncpy(r, jt->elm[i].value - 1, jt->elm[i].valuelen + 2); result = JSONglue(result, r, ','); } else throw(MAL, "json.keyarray", "Object expected"); r = (char *) GDKstrdup("["); result = JSONglue(r, result, 0); r = (char *) GDKstrdup("]"); *ret = JSONglue(result, r, 0); return MAL_SUCCEED; }
str MRgetCloud(int *ret, str *mrcluster) { str msg; BAT *cloud; BUN p, q; BATiter bi; char nodes[BUFSIZ]; char *n = nodes; int mapcount = 0; snprintf(nodes, sizeof(nodes), "*/%s/node/*", *mrcluster); if ((msg = RMTresolve(ret, &n)) != MAL_SUCCEED) return msg; MT_lock_set(&mal_contextLock, "mapreduce"); cloud = BATdescriptor(*ret); /* should succeed */ mapnodes = (mapnode*)GDKzalloc(sizeof(mapnode) * (BATcount(cloud) + 1)); if (mapnodes == NULL) { BBPreleaseref(*ret); throw(MAL, "mapreduce.getCloud", MAL_MALLOC_FAIL); } bi = bat_iterator(cloud); BATloop(cloud, p, q) { str t = (str)BUNtail(bi, p); mapnodes[mapcount].uri = GDKstrdup(t); mapnodes[mapcount].user = GDKstrdup("monetdb"); mapnodes[mapcount].pass = GDKstrdup("monetdb"); mapcount++; }
int JSONfromString(str src, int *len, json *j) { ssize_t slen = (ssize_t) strlen(src); JSON *jt = JSONparse(src, FALSE); if (*j) GDKfree(*j); if (!jt || jt->error) { *j = GDKstrdup(str_nil); if (jt) JSONfree(jt); return 0; } JSONfree(jt); *j = GDKstrdup(src); *len = (int) slen; if (GDKstrFromStr((unsigned char *) *j, (const unsigned char *) src, slen) < 0) { GDKfree(*j); *j = GDKstrdup(str_nil); *len = 2; return 0; } return *len; }
/* * At any point we should be able to construct an ascii representation of * the type descriptor. Including the variable references. */ str getTypeName(malType tpe) { char buf[PATHLENGTH], *s; size_t l = PATHLENGTH; int k; if (tpe == TYPE_any) return GDKstrdup("any"); if (isaBatType(tpe)) { snprintf(buf, l, "bat["); l -= strlen(buf); s = buf + strlen(buf); k = getColumnIndex(tpe); if (k) snprintf(s, l, ":any%c%d]",TMPMARKER, k); else if (getColumnType(tpe) == TYPE_any) snprintf(s, l, ":any]"); else snprintf(s, l, ":%s]", ATOMname(getColumnType(tpe))); return GDKstrdup(buf); } if (isAnyExpression(tpe)) { strncpy(buf, "any", 4); if (isAnyExpression(tpe)) snprintf(buf + 3, PATHLENGTH - 3, "%c%d", TMPMARKER, getColumnIndex(tpe)); return GDKstrdup(buf); } return GDKstrdup(ATOMname(tpe)); }
/* Search for occurrence of the function in the library identified by the filename. */ MALfcn getAddress(str fcnname) { void *dl; MALfcn adr; int idx=0; static int prev= -1; /* First try the last module loaded */ if( prev >= 0){ adr = (MALfcn) dlsym(filesLoaded[prev].handle, fcnname); if( adr != NULL) return adr; /* found it */ } /* * Search for occurrence of the function in any library already loaded. * This deals with the case that files are linked together to reduce * the loading time, while the signatures of the functions are still * obtained from the source-file MAL script. */ for (idx =0; idx < lastfile; idx++) if (idx != prev && /* skip already searched module */ filesLoaded[idx].handle && (idx == 0 || filesLoaded[idx].handle != filesLoaded[0].handle)) { adr = (MALfcn) dlsym(filesLoaded[idx].handle, fcnname); if (adr != NULL) { prev = idx; return adr; /* found it */ } } if (lastfile) return NULL; /* * Try the program libraries at large or run through all * loaded files and try to resolve the functionname again. * * the first argument must be the same as the base name of the * library that is created in src/tools */ dl = mdlopen("libmonetdb5", RTLD_NOW | RTLD_GLOBAL); if (dl == NULL) return NULL; adr = (MALfcn) dlsym(dl, fcnname); filesLoaded[lastfile].modname = GDKstrdup("libmonetdb5"); if(filesLoaded[lastfile].modname == NULL) { dlclose(dl); return NULL; } filesLoaded[lastfile].fullname = GDKstrdup("libmonetdb5"); if(filesLoaded[lastfile].fullname == NULL) { dlclose(dl); GDKfree(filesLoaded[lastfile].modname); return NULL; } filesLoaded[lastfile].handle = dl; lastfile ++; return adr; }
static int unix_getenv(str *res, str varname) { char *p = getenv(varname); if (p) *res = GDKstrdup(p); else *res = GDKstrdup(""); return GDK_SUCCEED; }
static str JSONgetValue(JSON *jt, int idx) { str s; if (jt->elm[idx].valuelen == 0) return GDKstrdup(str_nil); if (strncmp(jt->elm[idx].value, "null", 4) == 0) return GDKstrdup(str_nil); s = GDKzalloc(jt->elm[idx].valuelen + 1); if (s) strncpy(s, jt->elm[idx].value, jt->elm[idx].valuelen); return s; }
/* COMMAND "getPort": Extract the port id from the URL * SIGNATURE: getPort(str) : str; */ str URLgetPort(str *retval, url *val) { const char *s; const char *p = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getPort", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, &p)) == NULL) throw(ILLARG, "url.getPort", "bad url"); if (p == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l = s - p; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, p, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getPort", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getDomain": Extract the Internet domain from the URL * SIGNATURE: getDomain(str) : str; */ str URLgetDomain(str *retval, url *val) { const char *s; const char *h = NULL; const char *p = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getDomain", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, &h, &p)) == NULL) throw(ILLARG, "url.getDomain", "bad url"); if (h == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l; if (p != NULL) p--; else p = s; l = 0; while (p > h && p[-1] != '.') { p--; l++; } if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, p, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getDomain", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getExtension": Extract the file extension of the URL * SIGNATURE: getExtension(str) : str; */ str URLgetExtension(str *retval, url *val) { const char *s; const char *e = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getExtension", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(s, NULL, &e)) == NULL) throw(ILLARG, "url.getExtension", "bad url"); if (e == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l = s - e; assert(*e == '.'); if ((*retval = GDKmalloc(l)) != NULL) { strncpy(*retval, e + 1, l - 1); (*retval)[l - 1] = 0; } } if (*retval == NULL) throw(MAL, "url.getExtension", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getBasename": Extract the base of the last file name of the URL, * thus, excluding the file extension. * SIGNATURE: getBasename(str) : str; */ str URLgetBasename(str *retval, url *val) { const char *s; const char *b = NULL; const char *e = NULL; if (val == NULL || *val == NULL) throw(ILLARG, "url.getBasename", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(s, &b, &e)) == NULL) throw(ILLARG, "url.getBasename", "bad url"); if (b == NULL) { *retval = GDKstrdup(str_nil); } else { size_t l; if (e != NULL) { l = e - b; } else { l = s - b; } if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, b, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getBasename", "Allocation failed"); return MAL_SUCCEED; }
static int BATlocation(str *fnme, int *bid) { /* this function was formerly ATTlocation in removed file * monetdb5/modules/mal/attach.c */ BAT *b = BBPquickdesc(*bid, FALSE); char path[BUFSIZ], *s; *fnme = NULL; if (b == NULL || (!b->T->heap.filename && !b->H->heap.filename)) return 0; s = GDKfilepath(b->T->heap.farmid, BATDIR, (b->T->heap.filename ? b->T->heap.filename : b->H->heap.filename), 0); if (!MT_path_absolute(s)) { snprintf(path, BUFSIZ, "%s%c%s", GDKgetenv("gdk_dbpath"), DIR_SEP, s); } else { snprintf(path, sizeof(path), "%s", s); } GDKfree(s); s = strrchr(path, '.'); if (s) *s = 0; *fnme = GDKstrdup(path); return 1; }
inline static char * fromMallocToGDK(char *val) { char *ret = GDKstrdup(val); free(val); return(ret); }
/* COMMAND "getUser": Extract the user identity from the URL * SIGNATURE: getUser(str) : str; */ str URLgetUser(str *retval, url *val) { const char *s; const char *p; const char *u; if (val == NULL || *val == NULL) throw(ILLARG, "url.getUser", "url missing"); if ((s = skip_scheme(*val)) == NULL || (p = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (s = skip_path(p, NULL, NULL)) == NULL) throw(ILLARG, "url.getUser", "bad url"); if (p == s || *p != '/' || p[1] != '~') { *retval = GDKstrdup(str_nil); } else { size_t l; u = p + 2; for (p = u; p < s && *p != '/'; p++) ; l = p - u; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, u, l); (*retval)[l] = 0; } } if (*retval == NULL) throw(MAL, "url.getUser", "Allocation failed"); return MAL_SUCCEED; }
/* COMMAND "getQuery": Extract the query part from the URL * SIGNATURE: getQuery(str) : str; */ str URLgetQuery(str *retval, url *val) { const char *s; const char *q; if (val == NULL || *val == NULL) throw(ILLARG, "url.getQuery", "url missing"); if ((s = skip_scheme(*val)) == NULL || (s = skip_authority(s, NULL, NULL, NULL, NULL)) == NULL || (q = skip_path(s, NULL, NULL)) == NULL || (s = skip_search(q)) == NULL) throw(ILLARG, "url.getQuery", "bad url"); if (*q == '?') { size_t l; q++; l = s - q; if ((*retval = GDKmalloc(l + 1)) != NULL) { strncpy(*retval, q, l); (*retval)[l] = 0; } } else { *retval = GDKstrdup(str_nil); } if (*retval == NULL) throw(MAL, "url.getQuery", "Allocation failed"); return MAL_SUCCEED; }
/* all non-exported functions must be declared static */ static char * UDFreverse_(char **ret, const char *src) { size_t len = 0; char *dst = NULL; /* assert calling sanity */ assert(ret != NULL); /* handle NULL pointer and NULL value */ if (src == NULL || strcmp(src, str_nil) == 0) { *ret = GDKstrdup(str_nil); if (*ret == NULL) throw(MAL, "udf.reverse", "failed to create copy of str_nil"); return MAL_SUCCEED; } /* allocate result string */ len = strlen(src); *ret = dst = GDKmalloc(len + 1); if (dst == NULL) throw(MAL, "udf.reverse", "failed to allocate string of length " SZFMT, len + 1); /* copy characters from src to dst in reverse order */ dst[len] = 0; while (len > 0) *dst++ = src[--len]; return MAL_SUCCEED; }
/* * Forking is a relatively cheap way to create a new client. The new * client record shares the IO descriptors. To avoid interference, we * limit children to only produce output by closing the input-side. * * If the father itself is a temporary client, let the new child depend * on the grandfather. */ Client MCforkClient(Client father) { Client son = NULL; if (father == NULL) return NULL; if (father->father != NULL) father = father->father; if ((son = MCinitClient(father->user, father->fdin, father->fdout))) { son->fdin = NULL; son->fdout = father->fdout; son->bak = NULL; son->yycur = 0; son->father = father; son->scenario = father->scenario; if (son->prompt) GDKfree(son->prompt); son->prompt = GDKstrdup(father->prompt); son->promptlength = strlen(father->prompt); /* reuse the scopes wherever possible */ if (son->nspace == 0) son->nspace = newModule(NULL, putName("child", 5)); son->nspace->outer = father->nspace->outer; } return son; }
/** * Returns the location the exception was raised, if known. It * depends on how the exception was created, what the location looks * like. The returned string is mallocced with GDKmalloc, and hence * needs to be GDKfreed. */ str getExceptionPlace(const char *exception) { str ret; const char *s, *t; enum malexception i; size_t l; for (i = MAL; exceptionNames[i] != NULL; i++) { l = strlen(exceptionNames[i]); if (strncmp(exceptionNames[i], exception, l) == 0 && exception[l] == ':') { s = exception + l + 1; if ((t = strchr(s, ':')) != NULL) { if ((ret = GDKmalloc(t - s + 1)) == NULL) return NULL; strncpy(ret, s, t - s); ret[t - s] = 0; return ret; } break; } } return GDKstrdup("(unknown)"); }
ValPtr VALcopy(ValPtr d, const ValRecord *s) { if (!ATOMextern(s->vtype)) { *d = *s; } else if (s->val.pval == 0) { d->val.pval = ATOMnil(s->vtype); d->vtype = s->vtype; } else if (s->vtype == TYPE_str) { d->vtype = TYPE_str; d->val.sval = GDKstrdup(s->val.sval); d->len = strLen(d->val.sval); } else if (s->vtype == TYPE_bit) { d->vtype = s->vtype; d->len = 1; d->val.btval = s->val.btval; } else { ptr p = s->val.pval; d->vtype = s->vtype; d->len = ATOMlen(d->vtype, p); d->val.pval = GDKmalloc(d->len); memcpy(d->val.pval, p, d->len); } return d; }
int ATOMunknown_find(const char *nme) { int i, j = 0; /* first try to find the atom */ MT_lock_set(&GDKthreadLock); for (i = 1; i < MAXATOMS; i++) { if (unknown[i]) { if (strcmp(unknown[i], nme) == 0) { MT_lock_unset(&GDKthreadLock); return -i; } } else if (j == 0) j = i; } if (j == 0) { /* no space for new atom (shouldn't happen) */ MT_lock_unset(&GDKthreadLock); return 0; } if ((unknown[j] = GDKstrdup(nme)) == NULL) { MT_lock_unset(&GDKthreadLock); return 0; } MT_lock_unset(&GDKthreadLock); return -j; }
str ALARMctime(str *res) { time_t t = time(0); char *base; #ifdef HAVE_CTIME_R3 char buf[26]; base = ctime_r(&t, buf, sizeof(buf)); #else #ifdef HAVE_CTIME_R char buf[26]; base = ctime_r(&t, buf); #else base = ctime(&t); #endif #endif if (base == NULL) /* very unlikely to happen... */ throw(MAL, "alarm.ctime", "failed to format time"); base[24] = 0; /* squash final newline */ *res = GDKstrdup(base); if (*res == NULL) throw(MAL, "alarm.ctime", SQLSTATE(HY001) MAL_MALLOC_FAIL); return MAL_SUCCEED; }
sht PropertyIndex(str name) { int i=0; for (i=0; i<nr_properties; i++) { if (strcmp(properties[i], name) == 0) return i; } MT_lock_set(&mal_contextLock, "propertyIndex"); /* small change it's already added */ for (i=0; i<nr_properties; i++) { if (strcmp(properties[i], name) == 0) { MT_lock_unset(&mal_contextLock, "propertyIndex"); return i; } } if (i >= max_properties) { max_properties += 256; properties = GDKrealloc(properties, max_properties * sizeof(str)); if( properties == NULL){ GDKerror("PropertyIndex" MAL_MALLOC_FAIL); MT_lock_unset(&mal_contextLock, "propertyIndex"); return nr_properties; } } properties[nr_properties] = GDKstrdup(name); MT_lock_unset(&mal_contextLock, "propertyIndex"); return nr_properties++; }
static str JSONglue(str res, str r, char sep) { size_t len, l; str n; if (r == 0 || *r == 0) return res; len = strlen(r); if (res == 0) res = GDKstrdup(r); else { l = strlen(res); n = GDKzalloc(l + len + 3); strcpy(n, res); if (sep) { n[l] = ','; strncpy(n + l + 1, r, len); n[l + 1 + len] = 0; } else { strncpy(n + l, r, len); n[l + len] = 0; } GDKfree(res); res = n; } if (r) GDKfree(r); return res; }
str newTmpName(char tag, int i) { char buf[PATHLENGTH]; snprintf(buf, PATHLENGTH, "%c%d", tag, i); return GDKstrdup(buf); }
static Actuator ACnew(str nme) { Actuator ac; ac = (Actuator) GDKzalloc(sizeof(ACrecord)); ac->name = GDKstrdup(nme); ac->nxt = acAnchor; acAnchor = ac; return ac; }
int MCpushClientInput(Client c, bstream *new_input, int listing, char *prompt) { ClientInput *x = (ClientInput *) GDKmalloc(sizeof(ClientInput)); if (x == 0) return -1; x->fdin = c->fdin; x->yycur = c->yycur; x->listing = c->listing; x->prompt = c->prompt; x->next = c->bak; c->bak = x; c->fdin = new_input; c->listing = listing; c->prompt = prompt ? GDKstrdup(prompt) : GDKstrdup(""); c->promptlength = strlen(c->prompt); c->yycur = 0; return 0; }
static Sensor SEnew(str nme) { Sensor se; se = (Sensor) GDKzalloc(sizeof(SErecord)); se->name = GDKstrdup(nme); se->nxt = seAnchor; seAnchor = se; return se; }
Client MCinitClientRecord(Client c, oid user, bstream *fin, stream *fout) { str prompt; c->user = user; c->username = 0; c->scenario = NULL; c->oldscenario = NULL; c->srcFile = NULL; c->blkmode = 0; c->fdin = fin ? fin : bstream_create(GDKin, 0); c->yycur = 0; c->bak = NULL; c->listing = 0; c->fdout = fout ? fout : GDKstdout; c->mdb = 0; c->history = 0; c->curprg = c->backup = 0; c->glb = 0; /* remove garbage from previous connection */ if (c->nspace) { freeModule(c->nspace); c->nspace = 0; } c->father = NULL; c->login = c->lastcmd = time(0); //c->active = 0; c->session = GDKusec(); c->qtimeout = 0; c->stimeout = 0; c->stage = 0; c->itrace = 0; c->debugOptimizer = c->debugScheduler = 0; c->flags = MCdefault; c->errbuf = 0; prompt = !fin ? GDKgetenv("monet_prompt") : PROMPT1; c->prompt = GDKstrdup(prompt); c->promptlength = strlen(prompt); c->actions = 0; c->totaltime = 0; /* create a recycler cache */ c->exception_buf_initialized = 0; c->error_row = c->error_fld = c->error_msg = c->error_input = NULL; (void) AUTHgetUsername(&c->username, c); MT_sema_init(&c->s, 0, "Client->s"); return c; }