/* * See LICENSE for license details. */ #include #include #include #include #include #include char buf[PATH_MAX]; /* from git://bitreich.org/utf8expr */ size_t utf8strlen(char *s) { size_t i; i = 0; for (; s[0]; s++) { if ((s[0] & 0xc0) != 0x80) i++; } return i; } void fprintunderline(FILE *fp, char *str, size_t linelen) { size_t i; fprintf(fp, "\n %s\n ", str); for (i=0; i<=utf8strlen(str); ++i) fputs("=", fp); fputs("\n\n", fp); } void escapechars(char *s, size_t linelen) { size_t i; for (i=0; i %s", src, dst); while ((c = fgetc(fsrc)) != EOF) fputc(c, fdst); fclose(fsrc); fclose(fdst); } int main(int argc, char* argv[]) { int i; static char *line; static size_t linesize; ssize_t linelen; char title[PATH_MAX], fname[PATH_MAX], fname_old[PATH_MAX]; FILE *fp; fp = NULL; title[0] = fname[0] = fname_old[0] = '\0'; i = 0; while ((linelen = getline(&line, &linesize, stdin)) > 0) { if (line[linelen-1] == '\n') line[--linelen] = '\0'; if (linelen > 1 && line[0] == '#' && line[1] == '#') { if (fp) fclose(fp); strlcpy(title, line+2, PATH_MAX); mkfilename(fname, title, PATH_MAX, i++); if (!(fp = fopen(fname, "w"))) err(1, "fopen: %s", fname); if (linelen == 2) fputs("\n", fp); else fprintunderline(fp, title, linelen); } else if (linelen > 0 && line[0] == '%') { continue; } else if (linelen > 5 && !strncmp(line, "#pause", linelen)) { if (fp) fclose(fp); strlcpy(fname_old, fname, PATH_MAX); mkfilename(fname, title, PATH_MAX, i++); copyfile(fname, fname_old); if (strlen(fname) > 0 && !(fp = fopen(fname, "a"))) err(1, "fopen: %s", fname); } else { /* ignore text before first header */ if (fp) fprintesc(fp, line, linelen); } } free(line); return 0; }