mostly style fixes

Signed-off-by: Christoph Lohmann <20h@r-36.net>
master
Hiltjo Posthuma 2020-04-29 18:59:53 +02:00 committed by Christoph Lohmann
parent 652effdccb
commit dfe08ef1dd
1 changed files with 86 additions and 88 deletions

View File

@ -1,24 +1,21 @@
/* /*
* See LICENSE for license details. * See LICENSE for license details.
*/ */
#include <unistd.h> #include <err.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <err.h>
#include <string.h> #include <string.h>
#include <unistd.h>
char buf[PATH_MAX];
/* from git://bitreich.org/utf8expr */ /* from git://bitreich.org/utf8expr */
size_t size_t
utf8strlen(char *s) utf8strlen(const char *s)
{ {
size_t i; size_t i;
i = 0; for (i = 0; *s; s++) {
for (; s[0]; s++) { if ((*s & 0xc0) != 0x80)
if ((s[0] & 0xc0) != 0x80)
i++; i++;
} }
@ -26,9 +23,10 @@ utf8strlen(char *s)
} }
void void
fprintunderline(FILE *fp, char *str, size_t linelen) fprintunderline(FILE *fp, const char *str)
{ {
size_t i; size_t i;
fprintf(fp, "\n %s\n ", str); fprintf(fp, "\n %s\n ", str);
for (i = 0; i <= utf8strlen(str); ++i) for (i = 0; i <= utf8strlen(str); ++i)
fputs("=", fp); fputs("=", fp);
@ -36,14 +34,13 @@ fprintunderline(FILE *fp, char *str, size_t linelen)
} }
void void
escapechars(char *s, size_t linelen) escapechars(char *s)
{ {
size_t i; for (; *s; s++) {
for (i=0; i<linelen && *s != '\0'; (void)*s++, i++)
switch (*s) { switch (*s) {
case '#': case '#':
case ' ': case ' ':
case ' ': case '\t':
case ':': case ':':
case '.': case '.':
case '(': case '(':
@ -58,17 +55,16 @@ escapechars(char *s, size_t linelen)
break; break;
} }
} }
}
void void
fprintesc(FILE *fp, char *s, ssize_t len) fprintesc(FILE *fp, const char *s)
{ {
ssize_t i; int intext = 0;
int intext;
intext = 0;
fputs(" ", fp); fputs(" ", fp);
for (i=0; i<len && s[i] != '\0'; i++) for (; *s; s++) {
switch (s[i]) { switch (*s) {
case ' ': case ' ':
fputc(' ', fp); fputc(' ', fp);
break; break;
@ -77,7 +73,7 @@ fprintesc(FILE *fp, char *s, ssize_t len)
break; break;
case '*': case '*':
if (intext) { if (intext) {
fputc(s[i], fp); fputc(*s, fp);
} else { } else {
fputc('o', fp); fputc('o', fp);
intext = 1; intext = 1;
@ -85,18 +81,21 @@ fprintesc(FILE *fp, char *s, ssize_t len)
break; break;
default: default:
intext = 1; intext = 1;
fputc(s[i], fp); fputc(*s, fp);
break; break;
} }
}
fputs("\n", fp); fputs("\n", fp);
} }
void void
mkfilename(char *fname, char *str, size_t len, int i) mkfilename(char *fname, char *str, size_t bufsiz, int i)
{ {
strlcpy(buf, str, len); char buf[PATH_MAX];
escapechars(buf, len);
snprintf(fname, len, "%.4d-%s.txt", i, buf); strlcpy(buf, str, sizeof(buf));
escapechars(buf);
snprintf(fname, bufsiz, "%.4d-%s.txt", i, buf);
} }
void void
@ -105,7 +104,7 @@ copyfile(char *dst, char *src)
int c; int c;
FILE *fsrc, *fdst; FILE *fsrc, *fdst;
if (strlen(src) < 1 || strlen(dst) < 1 || if (src[0] == '\0' || dst[0] == '\0' ||
!(fsrc = fopen(src, "r")) || !(fdst = fopen(dst, "w"))) !(fsrc = fopen(src, "r")) || !(fdst = fopen(dst, "w")))
err(1, "copyfile: %s -> %s", src, dst); err(1, "copyfile: %s -> %s", src, dst);
@ -117,54 +116,53 @@ copyfile(char *dst, char *src)
} }
int int
main(int argc, char* argv[]) main(void)
{ {
int i; size_t i = 0;
static char *line; char *line = NULL;
static size_t linesize; size_t linesize = 0;
ssize_t linelen; ssize_t linelen;
char title[PATH_MAX], fname[PATH_MAX], fname_old[PATH_MAX]; char title[PATH_MAX] = "", fname[PATH_MAX] = "", fname_old[PATH_MAX] = "";
FILE *fp; FILE *fp = NULL;
fp = NULL;
title[0] = fname[0] = fname_old[0] = '\0';
i = 0;
while ((linelen = getline(&line, &linesize, stdin)) > 0) { while ((linelen = getline(&line, &linesize, stdin)) > 0) {
if (line[linelen - 1] == '\n') if (line[linelen - 1] == '\n')
line[--linelen] = '\0'; line[--linelen] = '\0';
if (linelen > 1 && line[0] == '#' && line[1] == '#') { if (line[0] == '%')
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; continue;
} else if (linelen > 5 && !strncmp(line, "#pause", linelen)) { if (line[0] == '#' && line[1] == '#') {
if (fp) if (fp) {
fclose(fp); fclose(fp);
strlcpy(fname_old, fname, PATH_MAX); fp = NULL;
mkfilename(fname, title, PATH_MAX, i++); }
copyfile(fname, fname_old); strlcpy(title, line + 2, sizeof(title));
if (strlen(fname) > 0 && !(fp = fopen(fname, "a"))) mkfilename(fname, title, sizeof(fname), i++);
if (!(fp = fopen(fname, "w")))
err(1, "fopen: %s", fname);
if (line[2] == '\0')
fputs("\n", fp);
else
fprintunderline(fp, title);
} else if (linelen > 5 && !strncmp(line, "#pause", linelen)) {
if (fp) {
fclose(fp);
fp = NULL;
}
strlcpy(fname_old, fname, sizeof(fname_old));
mkfilename(fname, title, sizeof(fname), i++);
copyfile(fname, fname_old);
if (fname[0] != '\0' && !(fp = fopen(fname, "a")))
err(1, "fopen: %s", fname); err(1, "fopen: %s", fname);
} else { } else {
/* ignore text before first header */ /* ignore text before first header */
if (fp) if (fp)
fprintesc(fp, line, linelen); fprintesc(fp, line);
} }
} }
free(line); free(line);
return 0; return 0;
} }