示例#1
0
文件: ifj12.c 项目: honza889/IFJ12
int main(int argc, char**argv)
{

    /****************************** Inicializace ******************************/

    exceptions_init();
    if(argc!=2) {
        fprintf( stderr, "Chybny zpusob volani interpretru!\nPouziti: %s program.ifj\n\n", argv[0] );
        exit( 99 );
    }

    FILE* f=fopen(argv[1],"r");
    if(f==NULL) {
        fprintf( stderr, "Program k interpretaci (%s) se nepodarilo otevrit!\n\n", argv[1] );
        exit( 99 );
    }

    Scanner s;
    SyntaxContext syntaxcontext;
    Function mainFunction;

    int exitVal = 0;

    try {
        try {

            initScanner(&s,f);
            initDefaultSyntaxContext(&syntaxcontext);

            /***************************** Překlad do AST *****************************/
            parseProgram(&s, &syntaxcontext, &mainFunction);
        }
        catch {
            on(ScannerError, e) {
                scannerErrorPrint(*e);
                fclose( f );
                exitVal = 1;
                rethrow();
            }
            on(SyntaxError, e) {
                syntaxErrorPrint(*e);
                fclose( f );
                exitVal = 2;
                rethrow();
            }
            on(UnexpectedToken, e) {
                UnexpectedTokenPrint(*e);
                fclose( f );
                exitVal = 2;
                rethrow();
            }
示例#2
0
inline void exceptions_endCatchBlock()
{
  if( !currentExceptionHandled )
  {
    rethrow();
  }
}
示例#3
0
static void _write_item(apr_file_t *file, nx_cc_item_t *item)
{
    nx_value_t key;
    nx_string_t str;
    nx_exception_t e;

    ASSERT(file != NULL);
    ASSERT(item != NULL);
    ASSERT(item->key != NULL);
    ASSERT(item->value != NULL);

    nx_value_init(&key);
    key.type = NX_VALUE_TYPE_STRING;
    nx_string_init_const(&str, item->key);
    key.string = &str;

    try
    {
	CHECKERR_MSG(nx_value_to_file(&key, file), "couldn't write key to cache file");
	if ( item->value->type == NX_VALUE_TYPE_INTEGER )
	{ // get offset for integer values so that updates can be done efficiently	    
	    item->offs = 0;
	    CHECKERR_MSG(apr_file_seek(file, APR_CUR, &(item->offs)), "failed to seek in cache file");
	}
	CHECKERR_MSG(nx_value_to_file(item->value, file), "couldn't write value to cache file");
    }
    catch(e)
    {
	nx_value_kill(&key);
	rethrow(e);
    }
}
示例#4
0
bool SessionImpl::nextReceiver(qpid::messaging::Receiver& receiver, qpid::messaging::Duration timeout)
{
    while (true) {
        txError.raise();
        try {
            std::string destination;
            if (incoming.getNextDestination(destination, adjust(timeout))) {
                qpid::sys::Mutex::ScopedLock l(lock);
                Receivers::const_iterator i = receivers.find(destination);
                if (i == receivers.end()) {
                    throw qpid::messaging::ReceiverError(QPID_MSG("Received message for unknown destination " << destination));
                } else {
                    receiver = i->second;
                }
                return true;
            } else {
                return false;
            }
        } catch (TransportFailure&) {
            reconnect();
        } catch (const qpid::framing::ResourceLimitExceededException& e) {
            if (backoff()) return false;
            else throw qpid::messaging::TargetCapacityExceeded(e.what());
        } catch (const qpid::SessionException& e) {
            rethrow(e);
        } catch (const qpid::ClosedException&) {
            throw qpid::messaging::SessionClosed();
        } catch (const qpid::ConnectionException& e) {
            throw qpid::messaging::ConnectionError(e.what());
        } catch (const qpid::ChannelException& e) {
            throw qpid::messaging::MessagingException(e.what());
        }
    }
}
示例#5
0
static void nx_win32_svc_start(DWORD argc, LPCTSTR *argv)
{
    SC_HANDLE service_manager = NULL;
    SC_HANDLE service_handle = NULL;
    SERVICE_STATUS status;
    nx_exception_t e;

    // Connect to service manager
    service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( service_manager == NULL )
    {
        nx_win32_error("Cannot initialize access to the service manager");
    }
    try
    {
        service_handle = OpenService(service_manager, "nxlog", SERVICE_ALL_ACCESS);
        if ( service_handle == NULL )
        {
            nx_win32_error("Couldn't open nxlog service");
        }
        else
        {
            if ( QueryServiceStatus(service_handle, &status) )
            {
                if ( status.dwCurrentState != SERVICE_RUNNING )
                {
                    log_info("Service not running - starting service...");
                    if ( StartService(service_handle, argc, argv) == 0 )
                    {
                        nx_win32_error("Failed to start nxlog service");
                    }
                }
                else
                {
                    log_error("Service is already running");
                }
            }

            // Close connection to service
            CloseServiceHandle(service_handle);
        }
        // Close connection to service manager
        CloseServiceHandle(service_manager);
    }
    catch(e)
    {
        if ( service_handle != NULL )
        {
            CloseServiceHandle(service_handle);

        }
        if ( service_manager != NULL )
        {
            CloseServiceHandle(service_manager);

        }
        rethrow(e);
    }
}
示例#6
0
文件: ifj12.c 项目: honza889/IFJ12
 on(MultipleFunctionDefinitions, value) {
     fprintf( stderr, "Nejednou definovana funkce \"");
     RCStringPrint(value, stderr);
     fprintf( stderr, "\"!\n");
     fclose( f );
     exitVal = 5;
     rethrow();
 }
示例#7
0
文件: context.c 项目: WareX97/aPlus
void java_context_run(java_context_t* j) {
    do {
        register u1 opcode = PC8;


        LOGF("[%d] (%2X) %s", PC, (int) opcode, j_opcodes[opcode].name);


        PB = PC;
        PC += 1;
        j_opcodes[opcode].handler (j);

    } while(j->flags == JAVACTX_FLAG_CONTINUE);


    if(unlikely(j->flags == JAVACTX_FLAG_EXCEPTION)) {

        register long i;
        for(i = 0; i < j->frame.method->code->code.exception_table_length; i++) {

            int spc = j->frame.method->code->code.exception_table[i].start_pc;
            int epc = j->frame.method->code->code.exception_table[i].end_pc;
            const char* exname = (const char*) j->assembly->java_this.jc_cp[j->assembly->java_this.jc_cp[j->frame.method->code->code.exception_table[i].catch_type].class_info.name_index].utf8_info.bytes;


            if(j->frame.regs.pb >= spc && j->frame.regs.pb <= epc) {
                if((strcmp(j->exception.name, exname) == 0) || (__check_exception(j->exception.name, exname) == J_OK)) {

                    java_object_t* exobj = NULL;
                    java_object_new(&exobj, j->exception.name);

                    JPUSH(ptr, (void*) exobj);



                    PC = PB = j->frame.method->code->code.exception_table[i].handler_pc;

                    j->exception.name = NULL;
                    j->exception.message = NULL;
                    j->exception.owner = NULL;
                    j->flags = JAVACTX_FLAG_CONTINUE;

                    
                    return java_context_run(j);
                }
            }
        }

        rethrow(j, j->parent);
        return;
    }

#if HAVE_GC_H
    //GC_gcollect();
#endif

    j->flags = JAVACTX_FLAG_SUCCESS;
}
static void io_err_handler(nx_module_t *module, nx_exception_t *e)
{
    ASSERT(e != NULL);
    ASSERT(module != NULL);

    nx_module_stop_self(module);
    om_kafka_stop(module);
    rethrow(*e);
}
示例#9
0
str SABgetLocalConnectionURI(str *ret) {
	str tmp, con;

	rethrow("sabaoth.getLocalConnectionURI", tmp,
			SABAOTHgetLocalConnection(&con));

	*ret = con;
	return(MAL_SUCCEED);
}
示例#10
0
static void im_ssl_listen(nx_module_t *module)
{
    nx_im_ssl_conf_t *imconf;
    nx_exception_t e;

    imconf = (nx_im_ssl_conf_t *) module->config;

    try
    {
	if ( imconf->listensock == NULL )
	{
	    apr_sockaddr_t *sa;

	    CHECKERR_MSG(apr_socket_create(&(imconf->listensock), APR_INET, SOCK_STREAM,
					   APR_PROTO_TCP, module->input.pool),
			 "couldn't create tcp socket");

	    CHECKERR_MSG(apr_sockaddr_info_get(&sa, imconf->host, APR_INET, imconf->port, 
					       0, module->input.pool),
			 "apr_sockaddr_info failed for %s:%d", imconf->host, imconf->port);
	
	    CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_SO_NONBLOCK, 1),
			 "couldn't set SO_NONBLOCK on listen socket");
	    CHECKERR_MSG(apr_socket_timeout_set(imconf->listensock, 0),
			 "couldn't set socket timeout on listen socket");
	    CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_SO_REUSEADDR, 1),
			 "couldn't set SO_REUSEADDR on listen socket");
	    CHECKERR_MSG(apr_socket_opt_set(imconf->listensock, APR_TCP_NODELAY, 1),
			 "couldn't set TCP_NODELAY on listen socket");
	    CHECKERR_MSG(apr_socket_bind(imconf->listensock, sa),
			 "couldn't bind ssl socket to %s:%d", imconf->host, imconf->port);
	}
        else
	{
	    log_debug("ssl socket already initialized");
	}

	CHECKERR_MSG(apr_socket_listen(imconf->listensock, SOMAXCONN),
		     "couldn't listen to ssl socket on %s:%d",
		     imconf->host, imconf->port);
    
	nx_module_pollset_add_socket(module, imconf->listensock, APR_POLLIN | APR_POLLHUP);
    }
    catch(e)
    {
	if ( imconf->listensock != NULL )
	{
	    apr_socket_close(imconf->listensock);
	    imconf->listensock = NULL;
	}
	apr_pool_clear(module->input.pool);
	rethrow(e);
    }
}
static void funcMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    if (UNLIKELY(info.Length() < 1)) {
        throwMinimumArityTypeErrorForMethod("func", "TestInterfaceGarbageCollected", 1, info.Length(), info.GetIsolate());
        return;
    }
    TestInterfaceGarbageCollected* impl = V8TestInterfaceGarbageCollected::toNative(info.Holder());
    TestInterfaceGarbageCollected* arg;
    {
        v8::TryCatch block;
        V8RethrowTryCatchScope rethrow(block);
        TONATIVE_VOID_INTERNAL(arg, V8TestInterfaceGarbageCollected::toNativeWithTypeCheck(info.GetIsolate(), info[0]));
    }
    impl->func(arg);
}
static void voidMethodArrayOfLongsArgMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    TestTypedefs* impl = V8TestTypedefs::toNative(info.Holder());
    Vector<int> arrayOfLongsArg;
    {
        v8::TryCatch block;
        V8RethrowTryCatchScope rethrow(block);
        if (UNLIKELY(info.Length() <= 0)) {
            impl->voidMethodArrayOfLongsArg();
            return;
        }
        TONATIVE_VOID_INTERNAL(arrayOfLongsArg, toNativeArray<int>(info[0], 1, info.GetIsolate()));
    }
    impl->voidMethodArrayOfLongsArg(arrayOfLongsArg);
}
static void stringArrayMethodStringArrayArgMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    if (UNLIKELY(info.Length() < 1)) {
        throwMinimumArityTypeErrorForMethod("stringArrayMethodStringArrayArg", "TestTypedefs", 1, info.Length(), info.GetIsolate());
        return;
    }
    TestTypedefs* impl = V8TestTypedefs::toNative(info.Holder());
    Vector<String> stringArrayArg;
    {
        v8::TryCatch block;
        V8RethrowTryCatchScope rethrow(block);
        TONATIVE_VOID_INTERNAL(stringArrayArg, toNativeArray<String>(info[0], 1, info.GetIsolate()));
    }
    v8SetReturnValue(info, v8Array(impl->stringArrayMethodStringArrayArg(stringArrayArg), info.Holder(), info.GetIsolate()));
}
static void uLongLongMethodTestInterfaceEmptyTypeSequenceArgMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    if (UNLIKELY(info.Length() < 1)) {
        throwMinimumArityTypeErrorForMethod("uLongLongMethodTestInterfaceEmptyTypeSequenceArg", "TestTypedefs", 1, info.Length(), info.GetIsolate());
        return;
    }
    TestTypedefs* impl = V8TestTypedefs::toNative(info.Holder());
    Vector<RefPtr<TestInterfaceEmpty> > testInterfaceEmptyTypeSequenceArg;
    {
        v8::TryCatch block;
        V8RethrowTryCatchScope rethrow(block);
        TONATIVE_VOID_INTERNAL(testInterfaceEmptyTypeSequenceArg, (toRefPtrNativeArray<TestInterfaceEmpty, V8TestInterfaceEmpty>(info[0], 1, info.GetIsolate())));
    }
    v8SetReturnValue(info, static_cast<double>(impl->uLongLongMethodTestInterfaceEmptyTypeSequenceArg(testInterfaceEmptyTypeSequenceArg)));
}
示例#15
0
str SABgetLocalConnectionPort(int *ret) {
	str tmp, con, p;

	rethrow("sabaoth.getLocalConnectionHost", tmp,
			SABAOTHgetLocalConnection(&con));

	/* this happens if no connection is available */
	if (strcmp(con, (str)str_nil) == 0) {
		*ret = 0;
		GDKfree(con);
		return(MAL_SUCCEED);
	}

	/* con looks like mapi:monetdb://hostname:port */
	/* do some poor man's parsing */
	tmp = con;
	if ((p = strchr(con, ':')) == NULL) {
		p = createException(MAL, "sabaoth.getLocalConnectionHost",
				"invalid local connection string: %s", tmp);
		GDKfree(tmp);
		return(p);
	}
	if ((p = strchr(p + 1, ':')) == NULL) {
		p = createException(MAL, "sabaoth.getLocalConnectionHost",
				"invalid local connection string: %s", tmp);
		GDKfree(tmp);
		return(p);
	}
	if ((con = strchr(p + 1, ':')) == NULL) {
		p = createException(MAL, "sabaoth.getLocalConnectionHost",
				"invalid local connection string: %s", tmp);
		GDKfree(tmp);
		return(p);
	}
	if ((p = strchr(con + 1, '/')) == NULL) {
		p = createException(MAL, "sabaoth.getLocalConnectionHost",
				"invalid local connection string: %s", tmp);
		GDKfree(tmp);
		return(p);
	}
	*p = '\0';

	*ret = atoi(con + 1);
	GDKfree(tmp);
	return(MAL_SUCCEED);
}
static void voidMethodFloatArgStringArgMethod(const v8::FunctionCallbackInfo<v8::Value>& info)
{
    if (UNLIKELY(info.Length() < 2)) {
        throwMinimumArityTypeErrorForMethod("voidMethodFloatArgStringArg", "TestTypedefs", 2, info.Length(), info.GetIsolate());
        return;
    }
    TestTypedefs* impl = V8TestTypedefs::toNative(info.Holder());
    float floatArg;
    V8StringResource<> stringArg;
    {
        v8::TryCatch block;
        V8RethrowTryCatchScope rethrow(block);
        TONATIVE_VOID_INTERNAL(floatArg, static_cast<float>(info[0]->NumberValue()));
        TOSTRING_VOID_INTERNAL(stringArg, info[1]);
    }
    impl->voidMethodFloatArgStringArg(floatArg, stringArg);
}
示例#17
0
static void _update_item(nx_cc_item_t *item)
{
    nx_ctx_t *ctx;
    apr_off_t offs;
    nx_exception_t e;

    ASSERT(item->value != NULL);

    ctx = nx_ctx_get();

    CHECKERR(apr_thread_mutex_lock(ctx->config_cache_mutex));
    try
    {
	if ( ctx->ccfile == NULL )
	{
	    CHECKERR_MSG(apr_file_open(&(ctx->ccfile), ctx->ccfilename,
				       APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE,
				       APR_OS_DEFAULT, ctx->pool),
			 "couldn't open config cache '%s' for writing", ctx->ccfilename);
	}
	// only integers are supported for now, string modifications trigger a whole rewrite
	if ( (item->offs > 0) && (item->value->type == NX_VALUE_TYPE_INTEGER) )

	{
	    offs = item->offs;
	    CHECKERR_MSG(apr_file_seek(ctx->ccfile, APR_SET, &offs),
			 "failed to seek in %s file", ctx->ccfilename);
	    CHECKERR_MSG(nx_value_to_file(item->value, ctx->ccfile), "failed to update config cache item");
	}
	else
	{ // write the whole file, not very efficient
	    nx_config_cache_write();
	}
	item->needflush = FALSE;
    }
    catch(e)
    {
	CHECKERR(apr_thread_mutex_unlock(ctx->config_cache_mutex));
	rethrow(e);
    }
    CHECKERR(apr_thread_mutex_unlock(ctx->config_cache_mutex));
}
示例#18
0
static void parse_kvp(nx_logdata_t *logdata,
		      nx_kvp_ctx_t *ctx,
		      const char *src,
		      size_t len)
{
    nx_string_t *valuestr = NULL;
    nx_exception_t e;
    nx_kvp_state_t state = NX_KVP_STATE_KEY_START;
    char keyname[256];
    int keylen = 0;
    volatile boolean got_keyquote = FALSE;

    try
    {
	int pos = 0;
	boolean got_valquote = FALSE;

	for ( pos = 0; pos < (int) len; pos++ )
	{
//	    log_info("parse [%c] state: %d", src[pos], state);
	    
	    switch ( state )
	    {
		case NX_KVP_STATE_KEY:
		    if ( src[pos] == ctx->keyquotechar )
		    {
			if ( got_keyquote == TRUE )
			{
			    state = NX_KVP_STATE_KV_DELIMITER;
			}
			else
			{
			    throw_msg("invalid key quotation in key-value pair");
			}
		    }
		    else if ( (ctx->kvdelimiter == '\0') &&
			      IS_KVDELIMITERCHAR(src[pos]) )
		    {
			ctx->kvdelimiter = src[pos];
			state = NX_KVP_STATE_VALUE_START;
		    }
		    else if ( src[pos] == ctx->kvdelimiter )
		    {
			state = NX_KVP_STATE_VALUE_START;
		    }
		    else if ( src[pos] == ctx->escapechar )
		    {
			state = NX_KVP_STATE_KEY_ESCAPE;
		    }
/* Unquoted keys containing space don't work with this, so instead we trim the space at the end
		    else if ( (src[pos] == ' ') && (got_keyquote != TRUE) )
		    {
			state = NX_KVP_STATE_KV_DELIMITER;
		    }
*/
		    else
		    {
			if ( keylen < (int) sizeof(keyname) - 1 )
			{
			    keyname[keylen] = src[pos];
			    keylen++;
			}
		    }
		    break;

		case NX_KVP_STATE_VALUE_START:
		    if ( src[pos] == ' ' )
		    { // skip leading space
			break;
		    }
		    got_valquote = FALSE;
		    ASSERT(valuestr == NULL);
		    valuestr = nx_string_new();

		    if ( (ctx->valquotechar == '\0') && 
			 IS_QUOTECHAR(src[pos]) )
		    { // auto-detected quotechar
			ctx->valquotechar = src[pos];
			got_valquote = TRUE;
		    }		    
		    else if ( src[pos] == ctx->valquotechar )
		    {
			got_valquote = TRUE;
		    }
		    else
		    {
			pos--; // handle character in STATE_VALUE
		    }
		    state = NX_KVP_STATE_VALUE;
		    break;

		case NX_KVP_STATE_VALUE:
		    if ( src[pos] == ctx->valquotechar )
		    {
			state = NX_KVP_STATE_KVP_DELIMITER;
			if ( got_valquote == TRUE )
			{
			}
			else
			{
			    if ( valuestr->len > 0)
			    {
				throw_msg("invalid value quotation in key-value pair: %s", valuestr->buf);
			    }
			}
		    }
		    else if ( (ctx->kvpdelimiter == '\0') &&
			      IS_KVPDELIMITERCHAR(src[pos]) &&
			      (got_valquote == FALSE) )
		    {
			ctx->kvpdelimiter = src[pos];
			// add field-value
			add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr);
			valuestr = NULL;
			state = NX_KVP_STATE_KEY_START;
		    }
		    else if ( (src[pos] == ctx->kvpdelimiter) && (got_valquote == FALSE) )
		    {
			// add field-value
			add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr);
			valuestr = NULL;
			state = NX_KVP_STATE_KEY_START;
		    }
		    else if ( src[pos] == ctx->escapechar )
		    {
			state = NX_KVP_STATE_VALUE_ESCAPE;
		    }
		    else
		    { // append character to value
			nx_string_append(valuestr, src + pos, 1);
		    }
		    break;

		case NX_KVP_STATE_VALUE_ESCAPE:
		    unescape_value(ctx, src[pos], valuestr, got_valquote);
		    state = NX_KVP_STATE_VALUE;
		    break;

		case NX_KVP_STATE_KV_DELIMITER:
		    if ( src[pos] == ' ' ) 
		    { // skip space
			break;
		    }

		    if ( (ctx->kvdelimiter == '\0') &&
			 IS_KVDELIMITERCHAR(src[pos]) )
		    {
			ctx->kvdelimiter = src[pos];
			state = NX_KVP_STATE_VALUE_START;
		    }
		    else if ( src[pos] == ctx->kvdelimiter )
		    {
			state = NX_KVP_STATE_VALUE_START;
		    }
		    break;
		    
		case NX_KVP_STATE_KVP_DELIMITER:
		    if ( (ctx->kvpdelimiter == '\0') &&
			 IS_KVPDELIMITERCHAR(src[pos]) )
		    {
			ctx->kvpdelimiter = src[pos];
			state = NX_KVP_STATE_KEY_START;
			add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr);
			valuestr = NULL;
		    }
		    else if ( src[pos] == ctx->kvpdelimiter )
		    {
			state = NX_KVP_STATE_KEY_START;
			add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr);
			valuestr = NULL;
		    }
		    break;
		    
		case NX_KVP_STATE_KEY_START:
		    keylen = 0;
		    got_keyquote = FALSE;
		    if ( src[pos] == ' ' )
		    { // skip space
			break;
		    }
		    else if ( (ctx->keyquotechar == '\0') && 
			      IS_QUOTECHAR(src[pos]) )
		    { // auto-detected quotechar
			ctx->keyquotechar = src[pos];
			got_keyquote = TRUE;
			state = NX_KVP_STATE_KEY;
		    }
		    else if ( src[pos] == ctx->keyquotechar )
		    {
			got_keyquote = TRUE;
			state = NX_KVP_STATE_KEY;
		    }
		    else if ( src[pos] == ctx->escapechar )
		    {
			state = NX_KVP_STATE_KEY_ESCAPE;
		    }
		    else if ( src[pos] == ctx->kvpdelimiter )
		    { // double delimiter, no value for kvp, skip
		    }
		    else
		    { // first character of unquoted key
			state = NX_KVP_STATE_KEY;
			if ( keylen < (int) sizeof(keyname) - 1 )
			{
			    keyname[keylen] = src[pos];
			    keylen++;
			}
		    }
		    break;
		case NX_KVP_STATE_KEY_ESCAPE:
		    if ( (src[pos] == ctx->escapechar) ||
			 (src[pos] == ctx->keyquotechar) )
		    {
			if ( keylen < (int) sizeof(keyname) - 1 )
			{
			    keyname[keylen] = src[pos];
			    keylen++;
			}
		    }
		    else
		    {
			if ( keylen < (int) sizeof(keyname) - 2 )
			{
			    keyname[keylen] = ctx->escapechar;
			    keylen++;

			    keyname[keylen] = src[pos];
			    keylen++;
			}
		    }
		    state = NX_KVP_STATE_KEY;
		    break;
		default:
		    nx_panic("invalid state %d", state);
	    }
	}
    }
    catch(e)
    {
	if ( valuestr != NULL )
	{
	    nx_string_free(valuestr);
	}
	rethrow(e);
    }

    switch ( state )
    {
	case NX_KVP_STATE_VALUE:
	case NX_KVP_STATE_KVP_DELIMITER:
	    add_logdata_field(logdata, keyname, keylen, got_keyquote, valuestr);
	    break;
	case NX_KVP_STATE_KEY_START:
	    ASSERT(valuestr == NULL);
	    break;
	case NX_KVP_STATE_KV_DELIMITER:
	case NX_KVP_STATE_KEY:
	case NX_KVP_STATE_KEY_ESCAPE:
	case NX_KVP_STATE_VALUE_START:
	case NX_KVP_STATE_VALUE_ESCAPE:
	    if ( valuestr != NULL )
	    {
		nx_string_free(valuestr);
	    }
	    throw_msg("invalid KVP input: '%s' [state: %d]", src, state);
	    break;
	default:
	    nx_panic("invalid state %d", state);
    }
}
示例#19
0
nx_string_t *nx_logdata_to_kvp(nx_kvp_ctx_t *ctx, nx_logdata_t *logdata)
{
    nx_string_t *retval = NULL;
    char *string = NULL;
    nx_string_t *tmp = NULL;
    nx_exception_t e;
    nx_logdata_field_t *field = NULL;
    char kvpdelimiter = ';';
    char kvdelimiter = '=';

    ASSERT(ctx != NULL);
    ASSERT(logdata != NULL);

    if ( ctx->kvdelimiter != '\0' )
    {
	kvdelimiter = ctx->kvdelimiter;
    }
    if ( ctx->kvpdelimiter != '\0' )
    {
	kvpdelimiter = ctx->kvpdelimiter;
    }

    retval = nx_string_new_size(150);

    try
    {
	for ( field = NX_DLIST_FIRST(&(logdata->fields));
	      field != NULL;
	      field = NX_DLIST_NEXT(field, link) )
	{
	    if ( strcmp(field->key, "raw_event") == 0 )
	    { // don't write raw event
		continue;
	    }

	    if ( (field->key[0] == '.') || (field->key[0] == '_') )
	    {
		continue;
	    }

	    nx_string_append(retval, field->key, -1);
	    nx_string_append(retval, &kvdelimiter, 1);

	    ASSERT(field->value != NULL);
	    if ( field->value->defined == FALSE )
	    { // don't write undef
	    }
	    else
	    {
		if ( field->value->type == NX_VALUE_TYPE_STRING )
		{
		    if ( field->value->string->len > 0 )
		    {
			tmp = nx_string_clone(field->value->string);
			escape_value(tmp, ctx);
			nx_string_append(retval, tmp->buf, (int) tmp->len);
			nx_string_free(tmp);
		    }
		}
		else
		{
		    string = nx_value_to_string(field->value);
		    if ( string != NULL )
		    {
			nx_string_append(retval, string, -1);
		    }
		}
	    }
	    nx_string_append(retval, &kvpdelimiter, 1);
	}
    }
    catch(e)
    {
	if ( string != NULL )
	{
	    free(string);
	}
	if ( tmp != NULL )
	{
	    nx_string_free(tmp);
	}
	if ( retval != NULL )
	{
	    nx_string_free(retval);
	}
	rethrow(e);
    }

    return ( retval );
}
void nx_expr_proc__xm_fileop_file_truncate(nx_expr_eval_ctx_t *eval_ctx,
					   nx_module_t *module,
					   nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    nx_expr_arg_t *offset;
    nx_value_t offsetval;
    nx_exception_t e;
    apr_off_t offs = 0;
    apr_status_t rv;
    apr_pool_t *pool;
    apr_file_t *fd;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }
    if ( file.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	throw_msg("string type required for 'file'");
    }

    if ( (offset = NX_DLIST_NEXT(arg, link)) != NULL )
    {
	ASSERT(offset->expr != NULL);
	try
	{
	    nx_expr_evaluate(eval_ctx, &offsetval, offset->expr);
	}
	catch(e)
	{
	    nx_value_kill(&file);
	    rethrow(e);
	}

	if ( offsetval.defined != TRUE )
	{
	    nx_value_kill(&file);
	    throw_msg("'offset' is undef");
	}
	if ( offsetval.type != NX_VALUE_TYPE_INTEGER )
	{
	    nx_value_kill(&offsetval);
	    nx_value_kill(&file);
	    throw_msg("integer type required for 'offset'");
	}
	offs = (apr_off_t) offsetval.integer;
    }

    pool = nx_pool_create_core();

    if ( (rv = apr_file_open(&fd, file.string->buf, APR_WRITE | APR_CREATE, APR_OS_DEFAULT,
			     pool)) != APR_SUCCESS )
    {
	log_aprerror(rv, "failed to open file '%s' when trying to truncate",
		     file.string->buf);
    }
    if ( rv == APR_SUCCESS )
    {
	if ( (rv = apr_file_trunc(fd, offs)) != APR_SUCCESS )
	{
	    log_aprerror(rv, "failed to truncate file '%s' to length %lu",
			 file.string->buf, (long unsigned int) offs);
	}
	apr_file_close(fd);
    }
    apr_pool_destroy(pool);
    nx_value_kill(&file);
}
void nx_expr_proc__xm_fileop_file_chmod(nx_expr_eval_ctx_t *eval_ctx,
					nx_module_t *module,
					nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    nx_expr_arg_t *mode;
    nx_value_t modeval;
    nx_exception_t e;
    int rv;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);

    mode = NX_DLIST_NEXT(arg, link);
    ASSERT(mode != NULL);
    ASSERT(mode->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }
    if ( file.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	throw_msg("string type required for 'file'");
    }

    try
    {
	nx_expr_evaluate(eval_ctx, &modeval, mode->expr);
    }
    catch(e)
    {
	nx_value_kill(&file);
	rethrow(e);
    }
    if ( modeval.defined != TRUE )
    {
	nx_value_kill(&file);
	throw_msg("'mode' is undef");
    }
    if ( modeval.type != NX_VALUE_TYPE_INTEGER )
    {
	nx_value_kill(&file);
	throw_msg("integer type required for 'mode'");
    }

#ifdef HAVE_CHMOD
    if ( (rv = chmod(file.string->buf, (mode_t) modeval.integer)) != 0 )
    {
	log_aprerror(rv, "failed to change file ownership on '%s'", file.string->buf);
    }
#else
    log_error("This platform does not support the file_chown() function");
#endif    
    nx_value_kill(&file);
}
void nx_expr_proc__xm_fileop_file_append(nx_expr_eval_ctx_t *eval_ctx,
					 nx_module_t *module,
					 nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *src, *dst;
    nx_value_t srcval, dstval;
    nx_exception_t e;
    apr_status_t rv;
    apr_pool_t *pool;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    src = NX_DLIST_FIRST(args);
    ASSERT(src != NULL);
    ASSERT(src->expr != NULL);
    dst = NX_DLIST_NEXT(src, link);
    ASSERT(dst != NULL);
    ASSERT(dst->expr != NULL);

    nx_expr_evaluate(eval_ctx, &srcval, src->expr);

    if ( srcval.defined != TRUE )
    {
	throw_msg("'src' is undef");
    }
    if ( srcval.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&srcval);
	throw_msg("string type required for 'src'");
    }

    try
    {
	nx_expr_evaluate(eval_ctx, &dstval, dst->expr);
    }
    catch(e)
    {
	nx_value_kill(&srcval);
	rethrow(e);
    }
    if ( dstval.defined != TRUE )
    {
	nx_value_kill(&srcval);
	throw_msg("'dst' is undef");
    }
    if ( dstval.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&dstval);
	nx_value_kill(&srcval);
	throw_msg("string type required for 'dst'");
    }

    pool = nx_pool_create_core();
    if ( (rv = apr_file_append(srcval.string->buf, dstval.string->buf, APR_FILE_SOURCE_PERMS,
			       pool)) != APR_SUCCESS )
    {
	log_aprerror(rv, "failed to append file from '%s' to '%s'", 
		     srcval.string->buf, dstval.string->buf);
    }
    apr_pool_destroy(pool);
    nx_value_kill(&srcval);
    nx_value_kill(&dstval);
}
void nx_expr_proc__xm_fileop_file_write(nx_expr_eval_ctx_t *eval_ctx,
					nx_module_t *module,
					nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    nx_expr_arg_t *val;
    nx_value_t value;
    nx_exception_t e;
    apr_status_t rv;
    apr_pool_t *pool;
    apr_file_t *fd;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);
    val = NX_DLIST_NEXT(arg, link);
    ASSERT(val != NULL);
    ASSERT(val->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }
    if ( file.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	throw_msg("string type required for 'file'");
    }

    try
    {
	nx_expr_evaluate(eval_ctx, &value, val->expr);
    }
    catch(e)
    {
	nx_value_kill(&file);
	rethrow(e);
    }
    if ( value.defined != TRUE )
    {
	nx_value_kill(&file);
	// do not write anything
	return;
    }
    if ( value.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	nx_value_kill(&value);
	throw_msg("string type required for 'value'");
    }

    pool = nx_pool_create_core();

    if ( (rv = apr_file_open(&fd, file.string->buf, APR_WRITE | APR_CREATE | APR_APPEND,
			     APR_OS_DEFAULT, pool)) != APR_SUCCESS )
    {
	log_aprerror(rv, "failed to open file '%s' when trying to write",
		     file.string->buf);
    }
    if ( rv == APR_SUCCESS )
    {
	if ( (rv = apr_file_write_full(fd, value.string->buf, value.string->len, NULL)) != APR_SUCCESS )
	{
	    log_aprerror(rv, "failed to write value to file '%s'", file.string->buf);
	}
	apr_file_close(fd);
    }
    apr_pool_destroy(pool);
    nx_value_kill(&file);
    nx_value_kill(&value);
}
示例#24
0
文件: ifj12.c 项目: honza889/IFJ12
 on(UnexpectedKeyWord, e) {
     UnexpectedKeyWordPrint(*e);
     fclose( f );
     exitVal = 2;
     rethrow();
 }
void nx_expr_proc__xm_fileop_file_link(nx_expr_eval_ctx_t *eval_ctx,
				       nx_module_t *module,
				       nx_expr_arg_list_t *args)
{
#ifdef HAVE_APR_FILE_LINK
    nx_expr_arg_t *src, *dst;
    nx_value_t srcval, dstval;
    nx_exception_t e;
    apr_status_t rv;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    src = NX_DLIST_FIRST(args);
    ASSERT(src != NULL);
    ASSERT(src->expr != NULL);
    dst = NX_DLIST_NEXT(src, link);
    ASSERT(dst != NULL);
    ASSERT(dst->expr != NULL);

    nx_expr_evaluate(eval_ctx, &srcval, src->expr);

    if ( srcval.defined != TRUE )
    {
	throw_msg("'src' is undef");
    }
    if ( srcval.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&srcval);
	throw_msg("string type required for 'src'");
    }

    try
    {
	nx_expr_evaluate(eval_ctx, &dstval, dst->expr);
    }
    catch(e)
    {
	nx_value_kill(&srcval);
	rethrow(e);
    }
    if ( dstval.defined != TRUE )
    {
	nx_value_kill(&srcval);
	throw_msg("'dst' is undef");
    }
    if ( dstval.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&dstval);
	nx_value_kill(&srcval);
	throw_msg("string type required for 'dst'");
    }

    if ( (rv = apr_file_link(srcval.string->buf, dstval.string->buf)) != APR_SUCCESS )
    {
	log_aprerror(rv, "failed to link file from '%s' to '%s'", 
		     srcval.string->buf, dstval.string->buf);
    }
    nx_value_kill(&srcval);
    nx_value_kill(&dstval);
#else
    throw_msg("file_link() is not available because apr_file_link is not provided by the linked apr library. Recompile with a newer apr version");
#endif
}
示例#26
0
static void im_ssl_accept(nx_module_t *module)
{
    nx_im_ssl_conf_t *imconf;
    apr_socket_t *sock;
    apr_sockaddr_t *sa;
    char *ipstr;
    nx_module_input_t *input;
    SSL *ssl;
    apr_pool_t *pool = NULL;
    apr_status_t rv;
    nx_exception_t e;

    log_debug("im_ssl_accept");
    
    imconf = (nx_im_ssl_conf_t *) module->config;

    pool = nx_pool_create_child(module->pool);
    try
    {
	if ( (rv = apr_socket_accept(&sock, imconf->listensock, pool)) != APR_SUCCESS )
	{
	    if ( APR_STATUS_IS_EAGAIN(rv) )
	    {
		nx_module_add_poll_event(module);
		apr_pool_destroy(pool);
	    }
	    else
	    {
		throw(rv, "couldn't accept connection on %s:%u (statuscode: %d)",
		      imconf->host, imconf->port, rv);
	    }
	}

	if ( rv == APR_SUCCESS )
	{
	    CHECKERR_MSG(apr_socket_opt_set(sock, APR_SO_NONBLOCK, 1),
			 "couldn't set SO_NONBLOCK on accepted socket");
	    CHECKERR_MSG(apr_socket_timeout_set(sock, 0),
			 "couldn't set socket timeout on accepted socket");
	    CHECKERR_MSG(apr_socket_addr_get(&sa, APR_REMOTE, sock),
			 "couldn't get info on accepted socket");
	    CHECKERR_MSG(apr_sockaddr_ip_get(&ipstr, sa),
			 "couldn't get IP of accepted socket");

	    nx_module_pollset_add_socket(module, imconf->listensock, APR_POLLIN | APR_POLLHUP);
	    
	    ssl = nx_ssl_from_socket(&(imconf->ssl_ctx), sock);
	    ASSERT(ssl != NULL);
	    SSL_set_accept_state(ssl);
	    //SSL_accept(ssl);
    
	    CHECKERR_MSG(apr_socket_opt_set(sock, APR_SO_NONBLOCK, 1),
			 "couldn't set SO_NONBLOCK on accepted socket");
	    CHECKERR_MSG(apr_socket_timeout_set(sock, 0),
			 "couldn't set socket timeout on accepted socket");

	    input = nx_module_input_new(module, pool);
	    input->desc_type = APR_POLL_SOCKET;
	    input->desc.s = sock;
	    input->inputfunc = imconf->inputfunc;
	    ASSERT(input->inputfunc != NULL);
    
	    nx_module_input_data_set(input, "ssl", ssl);
		
	    CHECKERR_MSG(apr_socket_data_set(sock, input, "input", NULL),
			 "couldn't set data on socket");
	    NX_DLIST_INSERT_TAIL(imconf->connections, input, link);
	    nx_module_input_data_set(input, "recv_from_str", ipstr);
	    
	    nx_module_pollset_add_socket(module, sock, APR_POLLIN | APR_POLLHUP);
	    log_info("SSL connection accepted from %s:%u", ipstr, sa->port);
	}
    }
    catch(e)
    {
	apr_pool_destroy(pool);
	rethrow(e);
    }
}
示例#27
0
static void nx_win32_svc_uninstall()
{
    SC_HANDLE service_manager = NULL;
    SC_HANDLE service_handle = NULL;
    SERVICE_STATUS query_status;
    nx_exception_t e;

    // Connect to service manager
    service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( service_manager == NULL )
    {
        nx_win32_error("Cannot initialize access to the service manager");
    }

    try
    {
        // Connect to service
        service_handle = OpenService(service_manager, "nxlog", SERVICE_ALL_ACCESS | DELETE);
        if ( service_handle == NULL )
        {
            nx_win32_error("Couldn't open nxlog service");
        }
        else
        {
            // Check that the service is stopped
            if ( QueryServiceStatus(service_handle, &query_status) &&
                    (query_status.dwCurrentState == SERVICE_RUNNING) )
            {
                throw_msg("Service is running, please stop it first.");
            }
            else
            {   // we can remove
                if ( DeleteService(service_handle) == FALSE )
                {
                    nx_win32_error("Couldn't delete service");
                }
                else
                {
                    log_info("Service successfully uninstalled");
                }
            }
            // Close connection to service
            CloseServiceHandle(service_handle);
        }
        // Close connection to service manager
        CloseServiceHandle(service_manager);
    }
    catch(e)
    {
        if ( service_handle != NULL )
        {
            CloseServiceHandle(service_handle);

        }
        if ( service_manager != NULL )
        {
            CloseServiceHandle(service_manager);

        }
        rethrow(e);
    }
}
示例#28
0
static void nx_win32_svc_stop()
{
    SC_HANDLE service_manager = NULL;
    SC_HANDLE service_handle = NULL;
    SERVICE_STATUS status;
    nx_exception_t e;

    // Connect to service manager
    service_manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( service_manager == NULL )
    {
        nx_win32_error("Cannot initialize access to the service manager");
    }
    try
    {
        service_handle = OpenService(service_manager, "nxlog", SERVICE_ALL_ACCESS);
        if ( service_handle == NULL )
        {
            nx_win32_error("Couldn't open nxlog service");
        }
        else
        {
            if ( QueryServiceStatus(service_handle, &status) )
            {
                if ( status.dwCurrentState != SERVICE_STOPPED )
                {
                    log_info("Service currenty active - stopping service...");
                    if ( !ControlService(service_handle, SERVICE_CONTROL_STOP, &status) )
                    {
                        nx_win32_error("Couldn't stop service");
                    }
                    else
                    {
                        Sleep(500);
                    }
                }
                else
                {
                    log_error("Service is already stopped");
                }
            }
            // Close connection to service
            CloseServiceHandle(service_handle);
        }
        // Close connection to service manager
        CloseServiceHandle(service_manager);
    }
    catch(e)
    {
        if ( service_handle != NULL )
        {
            CloseServiceHandle(service_handle);

        }
        if ( service_manager != NULL )
        {
            CloseServiceHandle(service_manager);

        }
        rethrow(e);
    }
}
示例#29
0
static void _read_item(char * volatile buf,
		       apr_size_t volatile bufsize,
		       apr_hash_t *cache,
		       apr_size_t *bytesread)
{
    nx_cc_item_t *item = NULL;
    nx_cc_item_t *tmpitem = NULL;
    nx_value_t * volatile key = NULL;
    nx_value_t *value = NULL;
    apr_size_t readsize;
    nx_exception_t e;

    ASSERT(buf != NULL);
    ASSERT(cache != NULL);

    item = malloc(sizeof(nx_cc_item_t));
    memset(item, 0, sizeof(nx_cc_item_t));

    try
    {
	key = nx_value_from_membuf(buf, bufsize, &readsize);
	if ( (key == NULL) || (key->type != NX_VALUE_TYPE_STRING) )
	{
	    throw_msg("failed to read key from config cache");
	}
	buf = buf + readsize;
	*bytesread = readsize;
	bufsize -= readsize;
	item->key = strdup(key->string->buf);
	nx_value_free(key);
	key = NULL;

	value = nx_value_from_membuf(buf, bufsize, &readsize);
	if ( value == NULL )
	{
	    throw_msg("failed to read value from config cache");
	}
	item->value = value;

	if ( item->value->type == NX_VALUE_TYPE_INTEGER )
	{
	    log_debug("read config cache item: %s=%ld", item->key, item->value->integer);
	}
	else if ( item->value->type == NX_VALUE_TYPE_STRING )
	{
	    log_debug("read config cache item: %s=%s", item->key, item->value->string->buf);
	}
	else
	{
	    log_debug("read config cache item: %s", item->key);
	}

	*bytesread += readsize;
    }
    catch(e)
    {
	if ( item != NULL )
	{
	    _free_item(item);
	}
	if ( key != NULL )
	{
	    nx_value_free(key);
	}
	if ( value != NULL )
	{
	    nx_value_free(value);
	}
	rethrow(e);
    }

    tmpitem = (nx_cc_item_t *) apr_hash_get(cache, item->key, APR_HASH_KEY_STRING);
    ASSERT(tmpitem == NULL);
    apr_hash_set(cache, item->key, APR_HASH_KEY_STRING, (void *) item);
}
示例#30
0
/**
 * This is not thread safe, must ensure that modules are not running,
 * otherwise lock config_cache_mutex
 */
void nx_config_cache_write()
{
    nx_ctx_t *ctx;
    nx_cc_item_t *item;
    nx_value_t *version = NULL;
    nx_exception_t e;
    apr_hash_index_t *idx;
    const char *key;
    apr_ssize_t keylen;

    ctx = nx_ctx_get();

    ASSERT(ctx != NULL);
    if ( ctx->nocache == TRUE )
    {
	log_debug("NoCache is TRUE, not writing config cache");
	return;
    }

    if ( apr_hash_count(ctx->config_cache) == 0 )
    { // no items to write
	log_debug("no entries found, not writing configcache.dat");
	return;
    }
    log_debug("nx_config_cache_write()");
    
    if ( ctx->ccfile == NULL )
    {
	CHECKERR_MSG(apr_file_open(&(ctx->ccfile), ctx->ccfilename,
				   APR_READ | APR_WRITE | APR_CREATE | APR_TRUNCATE,
				   APR_OS_DEFAULT, ctx->pool),
		     "couldn't open config cache '%s' for writing", ctx->ccfilename);
    }
    else
    {
	CHECKERR_MSG(apr_file_trunc(ctx->ccfile, 0), "failed to truncate '%s'",
		     ctx->ccfilename);
    }
    version = nx_value_new_string(NX_CONFIG_CACHE_VERSION);
    try
    {
	CHECKERR_MSG(nx_value_to_file(version, ctx->ccfile),
		     "couldn't write version to cache file");

	idx = apr_hash_first(NULL, ctx->config_cache);

	while ( idx != NULL )
	{
	    apr_hash_this(idx, (const void **) &key, &keylen, (void **) &item);
	    idx = apr_hash_next(idx);
	    if ( item->used == FALSE )
	    { // only write items which have been read or written
		continue;
	    }
	    _write_item(ctx->ccfile, item);
	    item->needflush = FALSE;
	}
    }
    catch(e)
    {
	nx_value_free(version);
	rethrow(e);
    }

    log_debug("config cache written to %s", ctx->ccfilename);
}