char *subst_get(register Subst *sp) { register char *cp; sp->d_current_state_ptr = sp->d_start_state_ptr; cp = string_release(&sp->d_buffer); string_construct(&sp->d_buffer, 0); return cp; }
static string_t *irc_color_parse_code(const char *source) { const char *p1 = NULL; const char *p2 = NULL; const char *cur = source; string_t *string = string_construct(); while ((p1 = strchr(cur, '['))) { const char *replaced = NULL; /* Suitable tag found */ if (p1[1] != '\0' && (p2 = strchr(p1, ']')) && (p2 - p1) > 1 && (p2 - p1) < 31) { /* Extract the tag */ size_t taglen = p2 - p1 - 1; char tagdata[32]; memcpy(tagdata, p1 + 1, taglen); tagdata[taglen] = '\0'; /* Termination */ if (!strcmp(tagdata, "/COLOR")) replaced = "\x0F"; else if (strstr(tagdata, "COLOR=") == tagdata) { /* Parse the color line */ int fgc; int bgc = -2; /* Background color */ char *find = strchr(tagdata + 6, '/'); if (find) { *find++ = '\0'; bgc = irc_color_lookup(find); } /* Foreground color */ fgc = irc_color_lookup(tagdata + 6); if (fgc != -1 && bgc == -2) { snprintf(tagdata, sizeof(tagdata), "\x03%02d", fgc); replaced = tagdata; } else if (fgc != -1 && bgc >= 0) { snprintf(tagdata, sizeof(tagdata), "\x03%02d,%02d", fgc, bgc); replaced = tagdata; } } /* Deal with the other tags */ else if (!strcmp(tagdata, "B") || !strcmp(tagdata, "/B")) replaced = "\x02"; else if (!strcmp(tagdata, "U") || !strcmp(tagdata, "/U")) replaced = "\x1F"; else if (!strcmp(tagdata, "I") || !strcmp(tagdata, "/I")) replaced = "\x16"; } if (replaced) { string_catf(string, "%.*s%s", (int)(p1 - cur), cur, replaced); cur = p2 + 1; } else { if (!p2) p2 = cur + strlen(cur); string_catf(string, "%.*s", (int)(p2 - cur + 1), cur); cur = p2 + 1; } } string_catf(string, "%s", cur); return string; }
char *file_fullpath(HashMap *symtab, char const *filename) { String fname; char const *includePath; if (message_show(MSG_NOTICE)) message("Looking for file `%s'", filename); string_construct(&fname, filename); includePath = file_includePath(symtab); return *filename == '/' || !includePath ? /* absolute path, or no */ f_as_is(filename, &fname) /* include path */ : f_search_path(filename, &fname, /* or search the path */ includePath); }
void module_enter(irc_t *irc, const char *channel, const char *user, const char *message) { if (!message) message = user; const char *digit = message; while (*digit && isspace(*digit)) digit++; size_t x = isdigit(*digit) ? (size_t)atoi(message) : (urand() % 36); size_t y = 0; while (*digit && isdigit(*digit)) digit++; while (*digit && isspace(*digit)) digit++; string_t *string = string_construct(); while (x >= y+1) { if (gibber(string, (isdigit(*digit) ? atoi(digit) + 1 : 16)) && y+1 != x) string_catf(string, " "); y++; } irc_write(irc, channel, "%s: %s", user, string_contents(string)); }
module_t *module_open(const char *file, module_manager_t *manager, string_t **error) { char *function = NULL; if (!module_allow(file, &function)) { *error = string_construct(); string_catf(*error, "%s blacklisted", function); free(function); return NULL; } module_t *module = malloc(sizeof(*module)); if (!(module->handle = dlopen(file, RTLD_LAZY))) { free(module); *error = string_create(dlerror()); return NULL; } module->file = strdup(file); module->instance = manager->instance; if (!module_load(module)) { if (!module) return NULL; if (module->handle) dlclose(module->handle); free(module->file); free(module); return NULL; } module->random = mt_create(); list_push(manager->modules, module); return module; }
char *string_str_replace(char const *left, char const *search, char const *replace) { size_t searchlen = search ? strlen(search) : 0; register char const *right; String out; if (!searchlen) /* search should have some length */ return new_str(left); { string_construct(&out, 0); while ((right = strstr(left, search))) { string_additerators(&out, left, right); string_addstr(&out, replace); left = right + searchlen; } string_addstr(&out, left); /* append the final tail */ return string_release(&out); } }
void args_construct(int argc, char **argv, char *options, LongOption const *longOption) { char *cp; size_t nopt; struct option *long_option; size_t nlong = 0; if (!longOption) long_option = new_calloc(1, sizeof(struct option)); else { while (longOption[nlong++].d_name) ; long_option = new_memory(nlong, sizeof(struct option)); for (nopt = 0; nopt < nlong; nopt++) { long_option[nopt].name = longOption[nopt].d_name; long_option[nopt].has_arg = longOption[nopt].d_type; long_option[nopt].flag = 0; long_option[nopt].val = longOption[nopt].d_value; } } memset(&args, 0, sizeof(Args)); string_construct(&args.d_option, 0); args.d_argv = argv; args.d_argc = argc; if ((cp = getenv("home")) || (cp = getenv("HOME"))) /* set home */ args.d_home = new_str(cp); args.d_programName = basename(argv[0]); /* set programname */ args.d_initial_dir = new_getcwd(); /* set initial dir. */ args.d_ok = true; nopt = 0; while (true) { int optchar = getopt_long(args.d_argc, args.d_argv, options, long_option, NULL); switch (optchar) { default: string_addchar(&args.d_option, optchar); new_size(&args.d_optarg, nopt + 1, nopt, sizeof(char *)); args.d_optarg[nopt++] = optarg ? new_str(optarg) : 0; // FALLING THROUGH case 'l': continue; /* at `while(true)' */ case '?': args.d_ok = false; /* stop processing at failure */ break; case EOF: args.d_optind = optind; break; } break; /* leave `while(true)' */ } free(long_option); }
int main (int argc, char **argv) { char const *doctype; char const **ptr; args_data = postqueue_data = message_data; message_construct(argv[0]); args_construct(argc, argv, "?x:l:", 0); hashmap_construct(&symtab); lines_construct(&global.d_toc); lines_construct(&global.d_section); lines_add(&global.d_section, ""); hashmap_constructText(&global.d_symbol, default_symbols); if (!args_ok() || args_nArgs() < 2) /* check arguments */ usage(); if (args_nArgs() == 2) /* file name specified */ { global.d_out = stdout; global.d_noext = 0; } else { global.d_noext = file_rmExtension(args_arg(2)); global.d_ext = file_extension(args_arg(2)); if (!global.d_ext) { global.d_ext = new_str(args_optarg('x')); if (!global.d_ext) global.d_ext = "ypp"; /* Yodl Post Processor */ } } string_construct(&global.d_outName, 0); postqueue_construct(task); if (global.d_noext) { string_format(&global.d_outName, "%s.%s", global.d_noext, global.d_ext); global.d_out = file_open(string_str(&global.d_outName), "w"); } doctypes[sizeofDocType - 1] = doctype = hashmap_textOf(&global.d_symbol, "documenttype"); for ( ptr = doctypes, global.d_doctype = 1; strcmp(doctype, *ptr); ptr++, global.d_doctype <<= 1 ) ; postqueue_process(); fclose(global.d_out); return 0; }