char *asn_replace(const char *regex, const char *rep, const char *str, mmatic *mm) { int cv[CVS], cvn, rc, br, offset = 0, len = strlen(str); xstr *xs = MMXSTR_CREATE(""); char *mem = malloc(strlen(rep) + 1), *p, *bs; while ((rc = _regex_match(regex, str, len, offset, cv, &cvn)) == 1 && cv[0] >= 0 && cv[1] >= cv[0]) { if (cv[0] >= len) break; dbg(8, "asn_replace(): matched at %d-%d (rc=%d, offset=%d)\n", cv[0], cv[1], rc, offset); /* copy text up to the first match */ xstr_append_size(xs, str+offset, cv[0]-offset); /* replace, handling backreferences */ strcpy(mem, rep); for (bs = p = mem; (bs = strchr(bs, '\\'));) { if (!bs[1] || !isdigit(bs[1])) continue; /* append everything up to \, position bs on the number */ *bs++ = '\0'; xstr_append(xs, p); /* position p on the end of the number + 1 */ for (p = bs; *p && isdigit(*p); p++); /* substitute */ br = atoi(bs); if (br++ > 0 && br <= cvn) { # define IB (2*br - 2) # define IT (2*br - 1) dbg(9, "asn_replace(): appending backreference %d between %d and %d\n", br-1, cv[IB], cv[IT]-1); xstr_append_size(xs, str + cv[IB], cv[IT] - cv[IB]); } else { dbg(1, "asn_replace(): invalid backreference: %d\n", br-1); } bs = p; } /* in no backreferences case, this appends the whole "rep" string */ xstr_append(xs, p); /* start next match after * XXX: pcreapi(3) manual page says "The first element of a pair is set to the offset of the first character in * a substring, and the second is set to the offset of the first character *after* the end of a substring.", but * does not mention that e.g. a pattern of just /$/m will return cv[1] == cv[0]! */ offset = cv[1] + (cv[1] == cv[0]); if (offset >= len) break; } if (offset <= len); xstr_append(xs, str + offset); /* may be just "" */ free(mem); return xs->s; }
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; }
int asn_match(const char *regex, const char *str) { int cv[CVS]; return _regex_match(regex, str, strlen(str), 0, cv, NULL); }