Esempio n. 1
0
File: edjsdb.c Progetto: mankyd/edjs
static JSBool edjsdb_Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
    JSBool ok = JS_TRUE;
    JSObject *proto_obj = NULL;
    JSObject *ret_obj = NULL;
    edjsdb_private *p = NULL;
    JSString *type = NULL;
    char *mod_name = NULL;
    char *mod_path = NULL;
    struct stat *file_stat = NULL;
    JSBool (*db_driver_init)(JSContext *, JSObject *) = NULL;
    edjs_private *edjs_p = NULL;
    jsval path_val = JSVAL_VOID;
    JSObject *path_obj = NULL;


    JSFunctionSpec my_methods[] = {
        {"connect", edjsdb_connect, 1, JSPROP_ENUMERATE, 0},
        {"close", edjsdb_close, 0, 0, 0},
        {"query", edjsdb_query, 1, 0, 0},
        {"exec", edjsdb_exec, 1, 0, 0},
        {"createSequence", edjsdb_create_sequence, 1, 0, 0},
        {"dropSequence", edjsdb_drop_sequence, 1, 0, 0}, 
        {"listSequences", edjsdb_list_sequences, 0, 0, 0}, 
        {"nextID", edjsdb_next_id, 1, 0, 0},
        {"currentID", edjsdb_current_id, 1, 0, 0},
        {"quote", edjsdb_quote, 1, 0, 0},
        {0, 0, 0, 0, 0}
    };

    if (argc != 1) {
        //report error
        goto error;
    }

    JS_AddRoot(cx, &type);

    proto_obj = JS_NewObject(cx, NULL, NULL, NULL);
    if (NULL == proto_obj) {
        //throw error
        goto error;
    }
    *rval = OBJECT_TO_JSVAL(proto_obj); //root proto_obj

    if (JS_FALSE == JS_DefineFunctions(cx, proto_obj, my_methods)) {
        //report error
        goto error;
    }

    ret_obj = JS_NewObject(cx, &edjsdb_class, proto_obj, NULL);//JS_ConstructObjectWithArguments(cx, &edjsdb_class, NULL, NULL, argc, argv);

    if (NULL == ret_obj) {
        //throw error
        goto error;
    }
    *rval = OBJECT_TO_JSVAL(ret_obj); //root ret_obj

    p = (edjsdb_private *)EDJS_malloc(cx, sizeof(edjsdb_private));
    if (NULL == p) {
        goto error;
    }

    p->db_connection = NULL;
    p->lib_handle = NULL;
    p->finalize_func = edjsdb_finalize_stub;
    p->connect_func = edjsdb_connect_stub;
    p->close_func = edjsdb_close_stub;
    p->query_func = edjsdb_func_stub;
    p->exec_func = edjsdb_func_stub;
    p->create_sequence_func = edjsdb_func_stub;
    p->drop_sequence_func = edjsdb_func_stub;
    p->list_sequences_func = edjsdb_func_stub;
    p->next_id_func = edjsdb_func_stub;
    p->current_id_func = edjsdb_func_stub;
    p->quote_func = edjsdb_func_stub;


    if (JS_FALSE == JS_SetPrivate(cx, ret_obj, p)) {
        //report error
        goto error;
    }

    if (JS_FALSE == JS_DefineProperty(cx, ret_obj, "type", argv[0], NULL, NULL, JSPROP_ENUMERATE | JSPROP_READONLY)) {
        //report error
        goto error;
    }

    type = JS_ValueToString(cx, *argv);
    if (NULL == type) {
        //report error
        goto error;
    }

    mod_name = (char *)EDJS_malloc(cx, (7 + strlen(JS_GetStringBytes(type))) * sizeof(char)); //7 for db_.so\0
    strcpy(mod_name, "db_");
    strcat(mod_name, JS_GetStringBytes(type));
    strcat(mod_name, ".so");

    edjs_p = (edjs_private *)JS_GetContextPrivate(cx);

    if (JS_FALSE == JS_GetProperty(cx, edjs_p->settings_obj, "include_path", &path_val)) {
        //EDJS_ERR(cx, EDJSERR_GET_PROPERTY, "EDJS", "include_path");
        goto error;
    }

    path_obj = JSVAL_TO_OBJECT(path_val);

    if (JS_FALSE == EDJS_ResolveFile(cx, path_obj, NULL, mod_name, &mod_path, &file_stat)) {
        goto error;
    }

    if (NULL == mod_path) {
        //EDJS_ERR(cx, EDJSERR_FILENAME_RESOLVE, JS_GetStringBytes(include_str));
        goto error;
    }

    p->lib_handle = dlopen(mod_path, RTLD_NOW);
    char *dl_error = NULL;

    //if (NULL == p->lib_handle) {
    if ((dl_error = dlerror()) != NULL) {
        p->lib_handle = NULL;
        JS_ReportError(cx, dl_error);
        goto error;
    }


    db_driver_init = dlsym(p->lib_handle, "edjsdb_driver_instance_init");

    //    if (NULL == db_driver_init) {
    if ((dl_error = dlerror()) != NULL) {
        JS_ReportError(cx, dl_error);
        //JS_ReportError(cx, "failed to load edjsdb_driver_instance_init");
        goto error;
    }

    ok = db_driver_init(cx, ret_obj);
    if (JS_FALSE == ok) {
        JS_ReportError(cx, "driver failed its init method");
        goto error;
    }


    goto finish;
 error:
    ok = JS_FALSE;
    *rval = JSVAL_VOID;

 finish:
    JS_RemoveRoot(cx, &type);
    return ok;
}
Esempio n. 2
0
/*!
  \brief Get driver (?)

  \param argc, argv arguments

  \return 0 on success
  \return 1 on failure
 */
int db_driver(int argc, char *argv[])
{
    int stat;
    int procnum;
    int i;
    int rfd, wfd;
    FILE *send, *recv;
    char *modestr;

    /* Read and set environment variables, see dbmi_client/start.c */
    if ((modestr = getenv("GRASS_DB_DRIVER_GISRC_MODE"))) {
	int mode;

	mode = atoi(modestr);

	if (mode == G_GISRC_MODE_MEMORY) {
	    G_set_gisrc_mode(G_GISRC_MODE_MEMORY);
	    G__setenv("DEBUG", getenv("DEBUG"));
	    G__setenv("GISDBASE", getenv("GISDBASE"));
	    G__setenv("LOCATION_NAME", getenv("LOCATION_NAME"));
	    G__setenv("MAPSET", getenv("MAPSET"));
	    G_debug(3, "Driver GISDBASE set to '%s'", G_getenv("GISDBASE"));
	}
    }

#ifdef __MINGW32__
    /* TODO: */
    /* We should close everything except stdin, stdout but _fcloseall()
     * closes open streams not file descriptors. _getmaxstdio too big number.
     * 
     * Because the pipes were created just before this driver was started 
     * the file descriptors should not be above a closed descriptor
     * until it was run from a multithread application and some descriptors 
     * were closed in the mean time. 
     * Also Windows documentation does not say that new file descriptor is 
     * the lowest available.
     */

    {
	int err_count = 0;
	int cfd = 3;

	while (1) {
	    if (close(cfd) == -1)
		err_count++;

	    /* no good reason for 10 */
	    if (err_count > 10)
		break;

	    cfd++;
	}
    }
#endif

    send = stdout;
    recv = stdin;

    /* THIS CODE IS FOR DEBUGGING WITH CODECENTER */

/**********************************************/
    if (argc == 3) {
	rfd = wfd = -1;
	sscanf(argv[1], "%d", &rfd);
	sscanf(argv[2], "%d", &wfd);
	send = fdopen(wfd, "w");
	if (send == NULL) {
	    db_syserror(argv[1]);
	    exit(1);
	}
	recv = fdopen(rfd, "r");
	if (recv == NULL) {
	    db_syserror(argv[2]);
	    exit(1);
	}
    }

/**********************************************/

    db_clear_error();
    db_auto_print_errors(0);
    db_auto_print_protocol_errors(1);
    db__init_driver_state();

#ifndef USE_BUFFERED_IO
    setbuf(recv, NULL);
    setbuf(send, NULL);
#endif
    db__set_protocol_fds(send, recv);

    if (db_driver_init(argc, argv) == DB_OK)
	db__send_success();
    else {
	db__send_failure();
	exit(1);
    }

    stat = DB_OK;
    /* get the procedure number */
    while (db__recv_procnum(&procnum) == DB_OK) {
#ifdef __MINGW32__
	if (procnum == DB_PROC_SHUTDOWN_DRIVER) {
	    db__send_procedure_ok(procnum);
	    break;
	}
#endif
	db_clear_error();

	/* find this procedure */
	for (i = 0; procedure[i].routine; i++)
	    if (procedure[i].procnum == procnum)
		break;

	/* if found, call it */
	if (procedure[i].routine) {
	    if ((stat = db__send_procedure_ok(procnum)) != DB_OK)
		break;		/* while loop */
	    if ((stat = (*procedure[i].routine) ()) != DB_OK)
		break;
	}
	else if ((stat =
		  db__send_procedure_not_implemented(procnum)) != DB_OK)
	    break;
    }

    db_driver_finish();

    exit(stat == DB_OK ? 0 : 1);
}