Refactor sed(1) a bit

Well, isspacerune() has been fixed and some other FIXME's were also easy
to do.
There are some places where maybe some util-functions could be helpful.
In some cases, like for instance in regard to escape-sequences, I'm all
for consistency rather than adhering to the POSIX-standard too much.
Relying on centralized util-functions also makes it possible to keep
this consistency across the board.
This commit is contained in:
FRIGN 2015-02-18 10:51:39 +01:00
parent 6b719faade
commit a98405d277

25
sed.c
View File

@ -4,8 +4,6 @@
* nul bytes cause explosions due to use of libc string functions. thoughts? * nul bytes cause explosions due to use of libc string functions. thoughts?
* lack of newline at end of file, currently we add one. what should we do? * lack of newline at end of file, currently we add one. what should we do?
* allow "\\t" for "\t" etc. in regex? in replacement text? * allow "\\t" for "\t" etc. in regex? in replacement text?
* do I need to isdigit() -> isdigitrune()?
* fix libutf isspacerune() (isspacerune('\t') returns 0)
* POSIX says don't flush on N when out of input, but GNU and busybox do. * POSIX says don't flush on N when out of input, but GNU and busybox do.
* currently call fatal() when compiling and when running, do we need to fclose * currently call fatal() when compiling and when running, do we need to fclose
* wfiles when compiling and nothing has been written to them? if not don't * wfiles when compiling and nothing has been written to them? if not don't
@ -130,7 +128,6 @@ void stracpy(String *dst, char *src);
void strnacpy(String *dst, char *src, size_t n); void strnacpy(String *dst, char *src, size_t n);
/* Cleanup and errors */ /* Cleanup and errors */
void swarn(char *s);
void fatal(char *s); void fatal(char *s);
void usage(void); void usage(void);
@ -363,13 +360,6 @@ strnacpy(String *dst, char *src, size_t n)
strlcpy(dst->str, src, len); strlcpy(dst->str, src, len);
} }
/* FIXME: swarn -> weprintf everywhere */
void
swarn(char *s)
{
fprintf(stderr, "%zu: %s\n", lineno, s);
}
/* FIXME: more info, better error messages /* FIXME: more info, better error messages
* this currently exists as a place to close all wfiles when not doing a full * this currently exists as a place to close all wfiles when not doing a full
* cleanup() on fatal errors. also means I don't use all the ealloc etc. libutil * cleanup() on fatal errors. also means I don't use all the ealloc etc. libutil
@ -381,7 +371,7 @@ fatal(char *s)
while (wfiles.size) { while (wfiles.size) {
Wfile *wp = (Wfile *)pop(&wfiles); Wfile *wp = (Wfile *)pop(&wfiles);
if (wp->file && fclose(wp->file)) if (wp->file && fclose(wp->file))
swarn("fclose failed"); weprintf("fclose failed\n");
} }
eprintf("%zu: %s: %s\n", lineno, s, strerror(errno)); eprintf("%zu: %s: %s\n", lineno, s, strerror(errno));
} }
@ -457,7 +447,7 @@ compile(char *s, int isfile)
} }
if (fclose(f)) if (fclose(f))
swarn("fclose failed"); weprintf("fclose failed\n");
} }
/* FIXME: if we decide to honor lack of trailing newline, set/clear a global /* FIXME: if we decide to honor lack of trailing newline, set/clear a global
@ -609,8 +599,7 @@ chompr(char *s, Rune rune)
size_t rlen; size_t rlen;
char *end = s + strlen(s); char *end = s + strlen(s);
/* FIXME: fix libutf's isspacerune(), for now manually check '\t' I know there are more */ while (*s && (rlen = echarntorune(&r, s, end - s)) && (isspacerune(r) || r == rune))
while (*s && (rlen = echarntorune(&r, s, end - s)) && (isspacerune(r) || r == rune || r == '\t'))
s += rlen; s += rlen;
return s; return s;
} }
@ -1155,7 +1144,7 @@ next_file(void)
if (file == stdin) if (file == stdin)
clearerr(file); clearerr(file);
else if (file && fclose(file)) else if (file && fclose(file))
swarn("fclose failed"); weprintf("fclose failed\n");
file = NULL; file = NULL;
do { do {
@ -1167,8 +1156,8 @@ next_file(void)
file = stdin; file = stdin;
files++; files++;
} else if (!(file = fopen(*files++, "r"))) { } else if (!(file = fopen(*files++, "r"))) {
/* swarn this file didn't open, but move on to next */ /* warn this file didn't open, but move on to next */
swarn("fopen failed"); weprintf("fopen failed\n");
} }
} while (!file && *files); } while (!file && *files);
first = 0; first = 0;
@ -1224,7 +1213,7 @@ write_file(char *path, FILE *out)
check_puts(genbuf.str, out); check_puts(genbuf.str, out);
if (fclose(in)) if (fclose(in))
swarn("fclose failed"); weprintf("fclose failed\n");
} }
void void