/** * Free find options * * @param __options - options to be freed */ static void free_find_options (action_find_options_t *__options) { int i; SAFE_FREE (__options->file_mask); SAFE_FREE (__options->content); SAFE_FREE (__options->start_at); /* Destroy precompiled options */ for (i = 0; i < __options->re_file_count; ++i) { regexp_free (__options->re_file[i]); } SAFE_FREE (__options->re_file); regexp_free (__options->re_content); SAFE_FREE (__options->mb_content); }
/** * Make sub-string replacing by regular expression matching * * @param __re - compiled regular expression * @param __s - source string * @param __mask - mask of replacement * @return replaced string on success, NULL otherwise * @sideeffect allocate memory for return value */ char* preg_replace (const char *__regexp, const char *__s, const char *__mask) { regexp_t *re; char *result; re = regexp_compile (__regexp); if (!re) { return NULL; } result = regexp_replace (re, __s, __mask); regexp_free (re); return result; }
/** * Check is string matches to regular expression * * @param __regexp - regular expression to use * @param __string - string to check * @return non-zero if string matches to regular expression, zero otherwise */ BOOL preg_match (const char *__regexp, const char *__str) { int dummy; regexp_t *re; /* Compile regexp */ re = regexp_compile (__regexp); if (!re) { return FALSE; } dummy = regexp_match (re, __str); /* Free memory */ regexp_free (re); return dummy; }
void temu_parse_config (terms_t *terms) { struct regexp *bind_action = regexp_new("^bind:[ \t]+([a-zA-Z_]+)[ \t]+([0-9]+)[ \t]+([a-zA-Z0-9_]+)$", 0); struct regexp *bind_switch = regexp_new("^bind:[ \t]+([0-9]+)[ \t]+([0-9]+)[ \t]+([a-zA-Z0-9_]+)(-([a-zA-Z0-9_]+))?([ \t]+(.*?))?$", 0); struct regexp *color = regexp_new("^color:[ \t]+([0-9]+)[ \t]+(.*?)$", 0); struct regexp *font = regexp_new("^font:[ \t]+(.*?)$", 0); struct regexp *size = regexp_new("^size:[ \t]+([0-9]+)x([0-9]+)$", 0); struct regexp_iterator *iterator = NULL; char **subs = NULL; FILE *f; char *file = NULL; long f_len; int j; char *t1, *t2; char conffile[512] = { 0 }; if (!bind_action || !bind_switch || !color || !font || !size) { printf("Unable to compile regexp for config file parsing!\n"); goto done; } snprintf(conffile, sizeof(conffile) - 1, "%s/.temuterm/config", getenv("HOME")); f = fopen(conffile, "r"); if (!f) goto done; fseek(f, 0, SEEK_END); f_len = ftell(f); fseek(f, 0, SEEK_SET); file = calloc (1, f_len + 1); fread (file, f_len, 1, f); fclose(f); t1 = file; while (*t1) { t2 = strchr (t1, '\n'); *t2++ = '\0'; j = t1[0] == '#' ? 1 : 0; if (!j) { j = regexp_find_first_str (t1, &subs, bind_action, &iterator); if (j) temu_parse_bind_action (terms, subs); regexp_find_free (&subs, bind_action, &iterator); } if (!j) { j = regexp_find_first_str (t1, &subs, bind_switch, &iterator); if (j) temu_parse_bind_switch (terms, subs, BIND_ACT_SWITCH); regexp_find_free (&subs, bind_switch, &iterator); } if (!j) { j = regexp_find_first_str (t1, &subs, color, &iterator); if (j) temu_parse_color (terms, subs); regexp_find_free (&subs, color, &iterator); } if (!j) { j = regexp_find_first_str (t1, &subs, font, &iterator); if (j) temu_parse_font (terms, subs); regexp_find_free (&subs, font, &iterator); } if (!j) { j = regexp_find_first_str (t1, &subs, size, &iterator); if (j) temu_parse_size (terms, subs); regexp_find_free (&subs, size, &iterator); } if (!j) fprintf (stderr, "Unable to parse line in config: '%s'\n", t1); t1 = t2; } done: if (bind_action) regexp_free (bind_action); if (bind_switch) regexp_free (bind_switch); if (color) regexp_free (color); if (font) regexp_free (font); if (size) regexp_free (size); if (file) free (file); return; }
/* * IMAPFilter: an IMAP mail filtering utility. */ int main(int argc, char *argv[]) { int c; char *cafile = NULL, *capath = NULL; setlocale(LC_CTYPE, ""); opts.verbose = 0; opts.interactive = 0; opts.log = NULL; opts.config = NULL; opts.oneline = NULL; opts.debug = NULL; opts.truststore = NULL; if (exists_dir("/etc/ssl/certs")) opts.truststore = "/etc/ssl/certs"; else if (exists_file("/etc/ssl/cert.pem")) opts.truststore = "/etc/ssl/cert.pem"; env.home = NULL; env.pathmax = -1; while ((c = getopt(argc, argv, "Vc:d:e:il:t:v?")) != -1) { switch (c) { case 'V': version(); /* NOTREACHED */ break; case 'c': opts.config = optarg; break; case 'd': opts.debug = optarg; break; case 'e': opts.oneline = optarg; break; case 'i': opts.interactive = 1; break; case 'l': opts.log = optarg; break; case 't': opts.truststore = optarg; break; case 'v': opts.verbose = 1; break; case '?': default: usage(); /* NOTREACHED */ break; } } get_pathmax(); open_debug(); create_homedir(); catch_signals(); open_log(); if (opts.config == NULL) opts.config = get_filepath("config.lua"); buffer_init(&ibuf, INPUT_BUF); buffer_init(&obuf, OUTPUT_BUF); buffer_init(&nbuf, NAMESPACE_BUF); buffer_init(&cbuf, CONVERSION_BUF); regexp_compile(responses); SSL_library_init(); SSL_load_error_strings(); ssl3ctx = SSL_CTX_new(SSLv3_client_method()); ssl23ctx = SSL_CTX_new(SSLv23_client_method()); tls1ctx = SSL_CTX_new(TLSv1_client_method()); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL tls11ctx = SSL_CTX_new(TLSv1_1_client_method()); tls12ctx = SSL_CTX_new(TLSv1_2_client_method()); #endif if (exists_dir(opts.truststore)) capath = opts.truststore; else if (exists_file(opts.truststore)) cafile = opts.truststore; SSL_CTX_load_verify_locations(ssl3ctx, cafile, capath); SSL_CTX_load_verify_locations(ssl23ctx, cafile, capath); SSL_CTX_load_verify_locations(tls1ctx, cafile, capath); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL SSL_CTX_load_verify_locations(tls11ctx, cafile, capath); SSL_CTX_load_verify_locations(tls12ctx, cafile, capath); #endif start_lua(); #if LUA_VERSION_NUM < 502 { list *l; session *s; l = sessions; while (l != NULL) { s = l->data; l = l->next; request_logout(s); } } #endif stop_lua(); SSL_CTX_free(ssl3ctx); SSL_CTX_free(ssl23ctx); SSL_CTX_free(tls1ctx); #if OPENSSL_VERSION_NUMBER >= 0x01000100fL SSL_CTX_free(tls11ctx); SSL_CTX_free(tls12ctx); #endif ERR_free_strings(); regexp_free(responses); buffer_free(&ibuf); buffer_free(&obuf); buffer_free(&nbuf); buffer_free(&cbuf); xfree(env.home); close_log(); close_debug(); exit(0); }
/* * IMAPFilter: an IMAP mail filtering utility. */ int main(int argc, char *argv[]) { int c; setlocale(LC_CTYPE, ""); opts.verbose = 0; opts.interactive = 0; opts.log = NULL; opts.config = NULL; opts.oneline = NULL; opts.debug = NULL; env.home = NULL; env.pathmax = -1; while ((c = getopt(argc, argv, "Vc:d:e:il:v?")) != -1) { switch (c) { case 'V': version(); /* NOTREACHED */ break; case 'c': opts.config = optarg; break; case 'd': opts.debug = optarg; break; case 'e': opts.oneline = optarg; break; case 'i': opts.interactive = 1; break; case 'l': opts.log = optarg; break; case 'v': opts.verbose = 1; break; case '?': default: usage(); /* NOTREACHED */ break; } } get_pathmax(); open_debug(); create_homedir(); catch_signals(); open_log(); if (opts.config == NULL) opts.config = get_filepath("config.lua"); buffer_init(&ibuf, INPUT_BUF); buffer_init(&obuf, OUTPUT_BUF); buffer_init(&nbuf, NAMESPACE_BUF); buffer_init(&cbuf, CONVERSION_BUF); regexp_compile(responses); SSL_library_init(); SSL_load_error_strings(); start_lua(); #if LUA_VERSION_NUM < 502 { list *l; session *s; l = sessions; while (l != NULL) { s = l->data; l = l->next; request_logout(s); } } #endif stop_lua(); ERR_free_strings(); regexp_free(responses); buffer_free(&ibuf); buffer_free(&obuf); buffer_free(&nbuf); buffer_free(&cbuf); xfree(env.home); close_log(); close_debug(); exit(0); }