int chars_insert(chars p, const char *str, size_t index) { chars prefix = NULL; chars suffix = NULL; if (index > p->length) { return chars_append(p, str); } else if (index < 0) { fprintf(stderr, "index must be elevatedzero range\n"); return -1; } chars_init(&prefix, p->length + 1); chars_init(&suffix, p->length - index + 1); chars_nappend(prefix, p->s, index); chars_append(prefix, str); chars_cut(p, index, p->length - index); chars_nappend(suffix, p->s, p->length); chars_cpy(p, prefix->s); chars_append(p, suffix->s); chars_free(&prefix); chars_free(&suffix); return 0; }
int dos2unix(const char *ifile, const char * ofile) { FILE * is = NULL; FILE * os = NULL; char * buf = NULL; chars contents = NULL; if ((is = fopen(ifile, "r")) == NULL) { fprintf(stderr, "Open file %s error", ifile); return -1; } buf = (char *)Malloc(MAX_LINE); bzero(buf, MAX_LINE); chars_init(&contents, MAX_LINE); while (!feof(is)) { fgets(buf, MAX_LINE, is); int index = 0; if ((index = index_of(buf, "\r\n")) != -1) { buf[index] = '\n'; buf[index + 1] = '\0'; } chars_append(contents, buf); } free(buf); fclose(is); if ((os = fopen(ofile, "w")) == NULL) { fprintf(stderr, "open output file %s error", ofile); return -1; } fputs(contents->s, os); chars_free(&contents); fclose(os); return 0; }
void terminal_mdoc(void *arg, const struct mdoc *mdoc) { const struct mdoc_node *n; const struct mdoc_meta *m; struct termp *p; p = (struct termp *)arg; p->overstep = 0; p->maxrmargin = p->defrmargin; p->tabwidth = term_len(p, 5); if (NULL == p->symtab) switch (p->enc) { case (TERMENC_ASCII): p->symtab = chars_init(CHARS_ASCII); break; default: abort(); /* NOTREACHED */ } n = mdoc_node(mdoc); m = mdoc_meta(mdoc); term_begin(p, print_mdoc_head, print_mdoc_foot, m); if (n->child) print_mdoc_nodelist(p, NULL, m, n->child); term_end(p); }
void terminal_man(void *arg, const struct man *man) { struct termp *p; const struct man_node *n; const struct man_meta *m; struct mtermp mt; p = (struct termp *)arg; p->overstep = 0; p->maxrmargin = p->defrmargin; p->tabwidth = term_len(p, 5); if (NULL == p->symtab) switch (p->enc) { case (TERMENC_ASCII): p->symtab = chars_init(CHARS_ASCII); break; default: abort(); /* NOTREACHED */ } n = man_node(man); m = man_meta(man); term_begin(p, print_man_head, print_man_foot, m); p->flags |= TERMP_NOSPACE; mt.fl = 0; mt.lmargin = term_len(p, INDENT); mt.offset = term_len(p, INDENT); if (n->child) print_man_nodelist(p, &mt, n->child, m); term_end(p); }
static void * ml_alloc(char *outopts, enum htmltype type) { struct html *h; const char *toks[4]; char *v; toks[0] = "style"; toks[1] = "man"; toks[2] = "includes"; toks[3] = NULL; h = calloc(1, sizeof(struct html)); if (NULL == h) { perror(NULL); exit((int)MANDOCLEVEL_SYSERR); } h->type = type; h->tags.head = NULL; h->symtab = chars_init(CHARS_HTML); while (outopts && *outopts) switch (getsubopt(&outopts, UNCONST(toks), &v)) { case (0): h->style = v; break; case (1): h->base_man = v; break; case (2): h->base_includes = v; break; default: break; } return(h); }