Beispiel #1
0
variable_t * variable_cast_string (variable_t * castee) {
    char buf[24];
    switch (castee->type) {
        case TYPE_NUMBER :
            snprintf(buf, 24, "%d", castee->number);
            return variable_create(buf, TYPE_STRING);
        case TYPE_NONE :
            snprintf(buf, 24, "NONE");
            return variable_create(buf, TYPE_STRING);
        case TYPE_POINTER :
            snprintf(buf, 24, "%p", castee->pointer);
            return variable_create(buf, TYPE_STRING);
        case TYPE_STRING :
            return variable_create(castee->string, TYPE_STRING);
    }
    return NULL;
}
Beispiel #2
0
int script_process_line(char *buf)
{
	int ret = -ENOTSUP;
	struct timespec ts = { .tv_sec = 0, .tv_nsec = 0 };
	struct timespec tse;

	if (_perf_measure)
		ts = utils_get_time( TIME_CURRENT );

	/* Skip if it's a one line comment (more to be implemented) */
	if (is_comment(buf) == 1) {
		DPRINTF("%s: Found comment, skipping\n", __FUNCTION__);
		ret = 0;
		goto cleanup;
	}
	else
	if (strcmp(buf, "else {") == 0) {
		/* Reverse the condition */

		if ((_script_in_condition_and_met == 1)
			|| (_script_in_condition_and_met == -10))
			_script_in_condition_and_met = 0;
		else
		if ((_script_in_condition_and_met == 0)
			|| (_script_in_condition_and_met == -20))
			_script_in_condition_and_met = 1;
		else
		if (_script_in_condition_and_met != -1) {
			DPRINTF("%s: Invalid state for else statement\n", __FUNCTION__);

			ret = -EINVAL;
			goto cleanup;
		}

		ret = 0;
		goto cleanup;
	}
	else
	if (strcmp(buf, "}") == 0) {
		_script_in_condition_and_met = (_script_in_condition_and_met == 1) ? -10 : -20;
		ret = 0;
		goto cleanup;
	}

	if (_script_in_condition_and_met < -1)
		_script_in_condition_and_met = 1;

	if (_script_in_condition_and_met == 0) {
		ret = 0;
		goto cleanup;
	}

	/* Comparison with no ternary operator support... yet */
	if (regex_match("if \\(([^(]*)([^)]*)\\)", buf)) {
		if (regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf)) {
			char **matches = NULL;
			int i, num_matches;

			matches = (char **)utils_alloc( "scripting.script_process_line.matches", sizeof(char *) );
			_regex_match("if \\(([^(]*) == ([^)]*)\\) {", buf, matches, &num_matches);

			if (num_matches >= 2)
				_script_in_condition_and_met = (valcmp(matches[0], matches[1]) == 0) ? 1 : 0;

			for (i = 0; i < num_matches; i++)
				matches[i] = utils_free("scripting.condition.matches[]", matches[i]);
			matches = utils_free("scripting.condition.matches", matches);
		}
	}
	else
	/* Definition */
	if (strncmp(buf, "define ", 7) == 0) {
		tTokenizer t;

		t = tokenize(buf + 7, " ");
		if (t.numTokens != 2) {
			ret = -EINVAL;
			goto cleanup;
		}
		if (variable_create(trim(t.tokens[0]), trim(t.tokens[1])) != 1) {
			ret = -EIO;
			goto cleanup;
		}
	}
	else
	/* Operators */
	if ((strstr(buf, "+=") != NULL) || (strstr(buf, "-=") != NULL) ||
		(strstr(buf, "%=") != NULL) || (strstr(buf, "*=") != NULL) ||
		(strstr(buf, "/=") != NULL)) {
		tTokenizer t;
		char *var;
		char *val;
		int op, vtype;

		t = tokenize(buf, "=");
		if (t.numTokens != 2) {
			ret = -EINVAL;
			goto cleanup;
		}

		var = trim(strdup(t.tokens[0]));
		val = trim(strdup(t.tokens[1]));

		op = var[ strlen(var) - 1];
		var[ strlen(var) - 1] = 0;

		var = trim(var);
		if (val[strlen(val) - 1] == ';')
			val[strlen(val) - 1] = 0;
		val = trim(val);

		vtype = variable_get_type(var, NULL);
		if ((vtype == TYPE_INT) || (vtype == TYPE_LONG)) {
			char tmp[32] = { 0 };
			long value = atol(variable_get_element_as_string(var, NULL));

			if (op == '+')
				value += atol(val);
			else
			if (op == '-')
				value -= atol(val);
			else
			if (op == '*')
				value *= atol(val);
			else
			if (op == '%')
				value %= atol(val);
			else
			if (op == '/')
				value /= atol(val);

			snprintf(tmp, sizeof(tmp), "%ld", value);
			variable_set_deleted(var, 1);
			variable_add(var, tmp, TYPE_QSCRIPT, -1, vtype);
			ret = 0;
		}
		else
			ret = -EINVAL;

		var = utils_free("scripting.operators.var", var);
		val = utils_free("scripting.operators.val", val);
		free_tokens(t);
		return ret;
	}
	else
	/* Assignment */
	if (is_assignment(buf)) {
		tTokenizer t;

		t = tokenize(buf, "=");
		char *val = strdup( trim(t.tokens[1]) );
		if (val[strlen(val) - 1] == ';') {
			val[strlen(val) - 1] = 0;

			if (is_numeric(val) || is_string(val)) {
				if (is_string(val)) {
					*val++;
					val[strlen(val) - 1] = 0;
				}
				if (variable_add(trim(t.tokens[0]), val, TYPE_QSCRIPT, -1, gettype(val)) < 0) {
					desc_printf(gIO, gFd, "Cannot set new value to variable %s\n", trim(t.tokens[0]));
					ret = -EEXIST;
				}
				else
					ret = 0;
			}
			else
			if (regex_match("([^(]*)([^)]*)", val)) {
				tTokenizer t2;
				char *args = NULL;
				char *fn;

				t2 = tokenize(val, "(");
				if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ')') {
					ret = -EINVAL;
					goto cleanup;
				}

				t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0;
				fn = strdup(t2.tokens[0]);

				/* We need to make sure parenthesis won't break script line */
				if (t2.numTokens > 1) {
					int i;
					char argstmp[8192] = { 0 };

					for (i = 1; i < t2.numTokens; i++) {
						strcat(argstmp, t2.tokens[i]);

						if (i < t2.numTokens - 1)
							strcat(argstmp, "(");
					}

					args = strdup(argstmp);
				}

				if (args != NULL) {
					char args2[1024] = { 0 };

					snprintf(args2, sizeof(args2), "%s", args + 1);
					args2[ strlen(args2) - 1] = 0;
					args = utils_free("scripting.function-call.args", args);

					args = strdup( args2 );
				}
				free_tokens(t2);

				if (_script_builtin_function(trim(t.tokens[0]), fn, args) != 0)
					DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn);

				DPRINTF("%s: Should be a function with return value\n", __FUNCTION__);
				args = utils_free("scripting.function-call.args", args);
				fn = utils_free("scripting.function-call.fn", fn);

				ret = 0;
			}
			else
				ret = -EINVAL;
		}

		free_tokens(t);
	}
	else
	if (regex_match("([^(]*)([^)]*)", buf)) {
		tTokenizer t2;
		char *args = NULL;
		char *fn;

		t2 = tokenize(buf, "(");
		if (t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] != ';') {
			ret = -EINVAL;
			goto cleanup;
		}

		t2.tokens[t2.numTokens - 1][strlen(t2.tokens[t2.numTokens - 1]) - 1] = 0;
		fn = strdup(t2.tokens[0]);

		/* We need to make sure parenthesis won't break script line */
		if (t2.numTokens > 1) {
			int i;
			char argstmp[8192] = { 0 };

			for (i = 1; i < t2.numTokens; i++) {
				strcat(argstmp, t2.tokens[i]);

				if (i < t2.numTokens - 1)
					strcat(argstmp, "(");
			}

			args = strdup(argstmp);
		}

		if (args != NULL) {
			if (args[strlen(args) - 1] == ')')
				args[strlen(args) - 1] = 0;
		}
		free_tokens(t2);

		if (_script_builtin_function(NULL, fn, args) != 0)
			DPRINTF("Function %s doesn't seem to be builtin, we should try user-defined function\n", fn);

		args = utils_free("scripting.function-call.args", args);
		fn = utils_free("scripting.function-call.fn", fn);

		ret = 0;
	}
	else
		DPRINTF("%s: Not implemented yet\n", __FUNCTION__);

cleanup:
	if ((_perf_measure) && ((ts.tv_nsec > 0) && (ts.tv_sec > 0)) && (_script_in_condition_and_met > 0)) {
		tse = utils_get_time( TIME_CURRENT );
		desc_printf(gIO, gFd, "PERF: Line \"%s\" was being processed for %.3f microseconds (%.3f ms)\n\n",
			buf, get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.);
	}

	return ret;
}

int run_script(char *filename)
{
	FILE *fp;
	int opened = 0;
	char buf[4096] = { 0 };
	struct timespec ts = utils_get_time( TIME_CURRENT );
	struct timespec tse;

	if (access(filename, R_OK) != 0)
		return -ENOENT;

	_script_in_condition_and_met = -1;

	fp = fopen(filename, "r");
	if (fp == NULL)
		return -EPERM;

	if (gHttpHandler)
		http_host_header(gIO, gFd, HTTP_CODE_OK, gHost, "text/html", NULL, myRealm, 0);

	while (!feof(fp)) {
		memset(buf, 0, sizeof(buf));
		fgets(buf, sizeof(buf), fp);

		if ((strlen(buf) > 1) && (buf[strlen(buf) - 1] == '\n'))
			buf[strlen(buf) - 1] = 0;

		if ((strlen(buf) > 1) && (buf[0] != '\n')) {
			if (strcmp(buf, "<$") == 0)
				opened = 1;
			if (strcmp(buf, "$>") == 0)
				opened = 0;

			if ((opened) && (strcmp(buf, "<$") != 0))
				script_process_line(trim(buf));
		}
	}

	fclose(fp);

	idb_free_last_select_data();

	if (_perf_measure) {
		tse = utils_get_time( TIME_CURRENT );
		desc_printf(gIO, gFd, "PERF: File \"%s\" has been processed in %.3f microseconds (%.3f ms)\n\n",
			basename(filename), get_time_float_us( tse, ts ), get_time_float_us(tse, ts) / 1000.);
	}

	variable_dump();
	variable_free_all();
	return 0;
}
Beispiel #3
0
void _DbgWatchVariable(int l, const char* name, variable_type type, int size, void* data)
{
	variable* var=variable_create(l, name, type, size, data);
	
	frame_add_variable((frame*)linked_list_first_element(frames)->data, var);
}