-/* $OpenBSD: compile.c,v 1.52 2024/06/18 00:32:22 millert Exp $ */
+/* $OpenBSD: compile.c,v 1.53 2024/07/17 20:57:15 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
#include <sys/stat.h>
#include <ctype.h>
-#include <errno.h>
+#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
for (;;) {
if ((p = cu_getline(&lbuf, &bufsize)) == NULL) {
if (stack != 0)
- error(COMPILE, "unexpected EOF (pending }'s)");
+ error("unexpected EOF (pending }'s)");
return (link);
}
nonsel: /* Now parse the command */
if (!*p)
- error(COMPILE, "command expected");
+ error("command expected");
cmd->code = *p;
for (fp = cmd_fmts; fp->code; fp++)
if (fp->code == *p)
break;
if (!fp->code)
- error(COMPILE, "invalid command code %c", *p);
+ error("invalid command code %c", *p);
if (naddr > fp->naddr)
- error(COMPILE,
- "command %c expects up to %d address(es), found %d",
- *p, fp->naddr, naddr);
+ error("command %c expects up to %d address(es),"
+ " found %d", *p, fp->naddr, naddr);
switch (fp->args) {
case NONSEL: /* ! */
p++;
*/
cmd->nonsel = 1;
if (stack == 0)
- error(COMPILE, "unexpected }");
+ error("unexpected }");
cmd2 = stack;
stack = cmd2->next;
cmd2->next = cmd;
goto semicolon;
}
if (*p)
- error(COMPILE,
-"extra characters at the end of %c command", cmd->code);
+ error("extra characters at the end of %c"
+ " command", cmd->code);
break;
case TEXT: /* a c i */
p++;
EATSPACE();
if (*p != '\\')
- error(COMPILE, "command %c expects \\ followed by"
- " text", cmd->code);
+ error("command %c expects \\ followed by text",
+ cmd->code);
p++;
EATSPACE();
if (*p)
- error(COMPILE, "extra characters after \\ at the"
+ error("extra characters after \\ at the"
" end of %c command", cmd->code);
cmd->t = compile_text();
break;
p++;
EATSPACE();
if (*p == '\0')
- error(COMPILE, "filename expected");
+ error("filename expected");
cmd->t = duptoeol(p, "w command", NULL);
if (aflag) {
cmd->u.fd = -1;
else if ((cmd->u.fd = open(p,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
DEFFILEMODE)) == -1)
- error(FATAL, "%s: %s", p, strerror(errno));
+ err(1, "%s", p);
break;
case RFILE: /* r */
pledge_rpath = 1;
p++;
EATSPACE();
if (*p == '\0')
- error(COMPILE, "filename expected");
+ error("filename expected");
cmd->t = duptoeol(p, "read command", NULL);
break;
case BRANCH: /* b t */
EATSPACE();
cmd->t = duptoeol(p, "label", &p);
if (strlen(cmd->t) == 0)
- error(COMPILE, "empty label");
+ error("empty label");
enterlabel(cmd);
if (*p == ';') {
p++;
case SUBST: /* s */
p++;
if (*p == '\0' || *p == '\\')
- error(COMPILE, "substitute pattern can not be"
+ error("substitute pattern can not be"
" delimited by newline or backslash");
cmd->u.s = xmalloc(sizeof(struct s_subst));
p = compile_re(p, &cmd->u.s->re);
if (p == NULL)
- error(COMPILE, "unterminated substitute pattern");
+ error("unterminated substitute pattern");
--p;
p = compile_subst(p, cmd->u.s);
p = compile_flags(p, cmd->u.s);
goto semicolon;
}
if (*p)
- error(COMPILE, "extra text at the end of a"
+ error("extra text at the end of a"
" transform command");
break;
}
if (c == '\0')
return (NULL);
else if (c == '\\')
- error(COMPILE, "\\ can not be used as a string delimiter");
+ error("\\ can not be used as a string delimiter");
else if (c == '\n')
- error(COMPILE, "newline can not be used as a string delimiter");
+ error("newline can not be used as a string delimiter");
while (p[0]) {
/* Unescaped delimiter: We are done. */
* It may contain the delimiter without escaping.
*/
else if ((d = compile_ccl(&p, d)) == NULL)
- error(COMPILE, "unbalanced brackets ([])");
+ error("unbalanced brackets ([])");
}
return NULL;
}
}
*repp = xmalloc(sizeof(regex_t));
if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
- error(COMPILE, "RE error: %s", strregerror(eval, *repp));
+ error("RE error: %s", strregerror(eval, *repp));
if (maxnsub < (*repp)->re_nsub)
maxnsub = (*repp)->re_nsub;
free(re);
ref = *p - '0';
if (s->re != NULL &&
ref > s->re->re_nsub)
- error(COMPILE,
-"\\%c not defined in the RE", *p);
+ error("\\%c not defined in the"
+ " RE", *p);
if (s->maxbref < ref)
s->maxbref = ref;
} else if (*p == '&' || *p == '\\')
s->new = xrealloc(text, size);
return (p);
} else if (*p == '\n') {
- error(COMPILE,
-"unescaped newline inside substitute pattern");
+ error("unescaped newline inside substitute"
+ " pattern");
}
*sp++ = *p;
}
size += sp - op;
} while ((p = cu_getline(&lbuf, &bufsize)));
- error(COMPILE, "unterminated substitute in regular expression");
+ error("unterminated substitute in regular expression");
}
/*
switch (*p) {
case 'g':
if (gn)
- error(COMPILE, "more than one number or 'g' in"
+ error("more than one number or 'g' in"
" substitute flags");
gn = 1;
s->n = 0;
case '4': case '5': case '6':
case '7': case '8': case '9':
if (gn)
- error(COMPILE, "more than one number or 'g' in"
+ error("more than one number or 'g' in"
" substitute flags");
gn = 1;
l = strtol(p, &p, 10);
if (l <= 0 || l >= INT_MAX)
- error(COMPILE,
- "number in substitute flags out of range");
+ error("number in substitute flags out of"
+ " range");
s->n = (int)l;
continue;
case 'w':
p++;
EATSPACE();
if (*p == '\0')
- error(COMPILE, "filename expected");
+ error("filename expected");
s->wfile = duptoeol(p, "s command w flag", NULL);
*p = '\0';
if (aflag)
else if ((s->wfd = open(s->wfile,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
DEFFILEMODE)) == -1)
- error(FATAL, "%s: %s", s->wfile, strerror(errno));
+ err(1, "%s", s->wfile);
return (p);
default:
- error(COMPILE,
- "bad flag in substitute command: '%c'", *p);
+ error("bad flag in substitute command: '%c'", *p);
break;
}
p++;
memset(check, 0, sizeof(check));
delimiter = *old;
if (delimiter == '\\')
- error(COMPILE, "\\ can not be used as a string delimiter");
+ error("\\ can not be used as a string delimiter");
else if (delimiter == '\n' || delimiter == '\0')
- error(COMPILE, "newline can not be used as a string delimiter");
+ error("newline can not be used as a string delimiter");
new = old++;
do {
if ((new = strchr(new + 1, delimiter)) == NULL)
- error(COMPILE, "unterminated transform source string");
+ error("unterminated transform source string");
} while (*(new - 1) == '\\' && *(new -2) != '\\');
*new = '\0';
end = new++;
do {
if ((end = strchr(end + 1, delimiter)) == NULL)
- error(COMPILE, "unterminated transform target string");
+ error("unterminated transform target string");
} while (*(end -1) == '\\' && *(end -2) != '\\');
*end = '\0';
if (*old == 'n')
*old = '\n';
else if (*old != delimiter && *old != '\\')
- error(COMPILE, "Unexpected character after "
- "backslash");
+ error("Unexpected character after backslash");
}
if (*new == '\\') {
new++;
if (*new == 'n')
*new = '\n';
else if (*new != delimiter && *new != '\\')
- error(COMPILE, "Unexpected character after "
- "backslash");
+ error("Unexpected character after backslash");
}
if (check[(u_char) *old] == 1)
- error(COMPILE, "Repeated character in source string");
+ error("Repeated character in source string");
check[(u_char) *old] = 1;
(*transtab)[(u_char) *old++] = *new++;
}
if (*old != '\0' || *new != '\0')
- error(COMPILE, "transform strings are not the same length");
+ error("transform strings are not the same length");
return end + 1;
}
case '/': /* Context address */
p = compile_re(p, &a->u.r);
if (p == NULL)
- error(COMPILE, "unterminated regular expression");
+ error("unterminated regular expression");
a->type = AT_RE;
return (p);
a->u.l = strtoul(p, &end, 10);
return (end);
default:
- error(COMPILE, "expected context address");
+ error("expected context address");
return (NULL);
}
}
break;
}
if ((cp->u.c = findlabel(cp->t)) == NULL)
- error(COMPILE, "undefined label '%s'", cp->t);
+ error("undefined label '%s'", cp->t);
free(cp->t);
break;
case '{':
lhp = &labels[h & LHMASK];
for (lh = *lhp; lh != NULL; lh = lh->lh_next)
if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
- error(COMPILE, "duplicate label '%s'", cp->t);
+ error("duplicate label '%s'", cp->t);
lh = xmalloc(sizeof *lh);
lh->lh_next = *lhp;
lh->lh_hash = h;
-/* $OpenBSD: defs.h,v 1.10 2022/12/26 19:16:02 jmc Exp $ */
+/* $OpenBSD: defs.h,v 1.11 2024/07/17 20:57:15 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
size_t blen; /* Backing memory length. */
} SPACE;
-/*
- * Error severity codes:
- */
-#define FATAL 1 /* Exit immediately with 1 */
-#define COMPILE 2 /* Print error, count and finish script */
-
/*
* Round up to the nearest multiple of _POSIX2_LINE_MAX
*/
-/* $OpenBSD: extern.h,v 1.15 2024/06/18 00:32:22 millert Exp $ */
+/* $OpenBSD: extern.h,v 1.16 2024/07/17 20:57:16 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
void compile(void);
void cspace(SPACE *, const char *, size_t, enum e_spflag);
char *cu_getline(char **, size_t *);
-__dead void error(int, const char *, ...);
-void warning(const char *, ...);
+__dead void error(const char *, ...) __attribute__((__format__ (printf, 1, 2)));
+void warning(const char *, ...) __attribute__((__format__ (printf, 1, 2)));
int mf_getline(SPACE *, enum e_spflag);
int lastline(void);
void finish_file(void);
-/* $OpenBSD: main.c,v 1.46 2024/07/17 03:05:19 millert Exp $ */
+/* $OpenBSD: main.c,v 1.47 2024/07/17 20:57:16 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
#include <sys/stat.h>
#include <ctype.h>
+#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
if (inplace != NULL) {
if (pledge("stdio rpath wpath cpath fattr chown", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
} else {
if (pledge("stdio rpath wpath cpath", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
}
/* First usage case; script is the first arg */
if (*argv) {
if (!pledge_wpath && inplace == NULL) {
if (pledge("stdio rpath", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
}
for (; *argv; argv++)
add_file(*argv);
} else {
if (!pledge_wpath && !pledge_rpath) {
if (pledge("stdio", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
} else if (pledge_rpath) {
if (pledge("stdio rpath", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
} else if (pledge_wpath) {
if (pledge("stdio wpath cpath", NULL) == -1)
- error(FATAL, "pledge: %s", strerror(errno));
+ err(1, "pledge");
}
add_file(NULL);
}
process();
cfclose(prog, NULL);
if (fclose(stdout))
- error(FATAL, "stdout: %s", strerror(errno));
+ err(1, "stdout");
exit (rval);
}
switch (script->type) {
case CU_FILE:
if ((f = fopen(script->s, "r")) == NULL)
- error(FATAL,
- "%s: %s", script->s, strerror(errno));
+ err(1, "%s", script->s);
fname = script->s;
state = ST_FILE;
goto again;
fclose(infile);
if (*oldfname != '\0') {
if (rename(fname, oldfname) != 0) {
- warning("rename(): %s", strerror(errno));
+ warn("rename %s to %s", fname, oldfname);
unlink(tmpfname);
exit(1);
}
fclose(outfile);
outfile = NULL;
if (rename(tmpfname, fname) != 0) {
- warning("rename(): %s", strerror(errno));
+ warn("rename %s to %s", tmpfname, fname);
unlink(tmpfname);
exit(1);
}
/* stdin? */
if (files->fname == NULL) {
if (inplace != NULL)
- error(FATAL, "-i may not be used with stdin");
+ errx(1, "-i may not be used with stdin");
infile = stdin;
fname = "stdin";
outfile = stdout;
fname = files->fname;
if (inplace != NULL) {
if (stat(fname, &sb) != 0)
- error(FATAL, "%s: %s", fname,
- strerror(errno ? errno : EIO));
+ err(1, "%s", fname);
if (!S_ISREG(sb.st_mode))
- error(FATAL, "%s: %s %s", fname,
+ errx(1, "%s: %s %s", fname,
"in-place editing only",
"works for regular files");
if (*inplace != '\0') {
- strlcpy(oldfname, fname,
+ (void)strlcpy(oldfname, fname,
sizeof(oldfname));
len = strlcat(oldfname, inplace,
sizeof(oldfname));
- if (len > sizeof(oldfname))
- error(FATAL, "%s: name too long", fname);
+ if (len >= sizeof(oldfname))
+ errc(1, ENAMETOOLONG, "%s", fname);
}
- strlcpy(dirbuf, fname, sizeof(dirbuf));
+ len = strlcpy(dirbuf, fname, sizeof(dirbuf));
+ if (len >= sizeof(dirbuf))
+ errc(1, ENAMETOOLONG, "%s", fname);
len = snprintf(tmpfname, sizeof(tmpfname),
"%s/sedXXXXXXXXXX", dirname(dirbuf));
if (len >= sizeof(tmpfname))
- error(FATAL, "%s: name too long", fname);
+ errc(1, ENAMETOOLONG, "%s", fname);
if ((fd = mkstemp(tmpfname)) == -1)
- error(FATAL, "%s: %s", fname, strerror(errno));
+ err(1, "%s", fname);
(void)fchown(fd, sb.st_uid, sb.st_gid);
(void)fchmod(fd, sb.st_mode & ALLPERMS);
if ((outfile = fdopen(fd, "w")) == NULL) {
+ warn("%s", fname);
unlink(tmpfname);
- error(FATAL, "%s", fname);
+ exit(1);
}
outfname = tmpfname;
linenum = 0;
outfname = "stdout";
}
if ((infile = fopen(fname, "r")) == NULL) {
- warning("%s", strerror(errno));
+ warn("%s", fname);
rval = 1;
continue;
}
*/
len = getline(&p, &psize, infile);
if ((ssize_t)len == -1)
- error(FATAL, "%s: %s", fname, strerror(errno));
+ err(1, "%s", fname);
if (len != 0 && p[len - 1] == '\n') {
sp->append_newline = 1;
len--;
-/* $OpenBSD: misc.c,v 1.12 2017/01/20 10:26:16 krw Exp $ */
+/* $OpenBSD: misc.c,v 1.13 2024/07/17 20:57:16 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
#include <sys/types.h>
-#include <errno.h>
+#include <err.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
void *p;
if ((p = malloc(size)) == NULL)
- error(FATAL, "%s", strerror(errno));
+ err(1, NULL);
return (p);
}
void *p;
if ((p = reallocarray(o, nmemb, size)) == NULL)
- error(FATAL, "%s", strerror(errno));
+ err(1, NULL);
return (p);
}
{
if ((p = realloc(p, size)) == NULL)
- error(FATAL, "%s", strerror(errno));
+ err(1, NULL);
return (p);
}
* Error reporting function
*/
__dead void
-error(int severity, const char *fmt, ...)
+error(const char *fmt, ...)
{
va_list ap;
+ (void)fprintf(stderr, "sed: %lu: %s: ", linenum, fname);
va_start(ap, fmt);
- (void)fprintf(stderr, "sed: ");
- switch (severity) {
- case COMPILE:
- (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
- }
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
{
va_list ap;
+ (void)fprintf(stderr, "sed: %lu: %s: ", linenum, fname);
va_start(ap, fmt);
- (void)fprintf(stderr, "sed: ");
- (void)fprintf(stderr, "%lu: %s: ", linenum, fname);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
(void)fprintf(stderr, "\n");
-/* $OpenBSD: process.c,v 1.36 2024/06/18 00:32:22 millert Exp $ */
+/* $OpenBSD: process.c,v 1.37 2024/07/17 20:57:16 millert Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
#include <sys/uio.h>
#include <ctype.h>
-#include <errno.h>
+#include <err.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
DEFFILEMODE)) == -1)
- error(FATAL, "%s: %s",
- cp->t, strerror(errno));
+ err(1, "%s", cp->t);
if ((size_t)write(cp->u.fd, ps, psl) != psl ||
write(cp->u.fd, "\n", 1) != 1)
- error(FATAL, "%s: %s",
- cp->t, strerror(errno));
+ err(1, "%s", cp->t);
break;
case 'x':
if (hs == NULL)
if (re == NULL) {
if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
linenum = cp->u.s->linenum;
- error(COMPILE, "\\%d not defined in the RE",
- cp->u.s->maxbref);
+ error("\\%d not defined in the RE", cp->u.s->maxbref);
}
}
if (!regexec_e(re, ps, 0, 0, 0, psl))
if (cp->u.s->wfile && !pd) {
if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
- error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
+ err(1, "%s", cp->u.s->wfile);
if ((size_t)write(cp->u.s->wfd, ps, psl) != psl ||
write(cp->u.s->wfd, "\n", 1) != 1)
- error(FATAL, "%s: %s", cp->u.s->wfile, strerror(errno));
+ err(1, "%s", cp->u.s->wfile);
}
return (1);
}
break;
}
if (ferror(outfile))
- error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
+ err(1, "%s", outfname);
appendx = sdone = 0;
}
(void)fputc('$', outfile);
(void)fputc('\n', outfile);
if (ferror(outfile))
- error(FATAL, "%s: %s", outfname, strerror(errno ? errno : EIO));
+ err(1, "%s", outfname);
}
static inline int
if (preg == NULL) {
if (defpreg == NULL)
- error(FATAL, "first RE may not be empty");
+ errx(1, "first RE may not be empty");
} else
defpreg = preg;
case REG_NOMATCH:
return (0);
}
- error(FATAL, "RE error: %s", strregerror(eval, defpreg));
+ errx(1, "RE error: %s", strregerror(eval, defpreg));
}
/*
switch (cp->code) {
case 's':
if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
- error(FATAL,
- "%s: %s", cp->u.s->wfile, strerror(errno));
+ err(1, "%s", cp->u.s->wfile);
cp->u.s->wfd = -1;
break;
case 'w':
if (cp->u.fd != -1 && close(cp->u.fd))
- error(FATAL, "%s: %s", cp->t, strerror(errno));
+ err(1, "%s", cp->t);
cp->u.fd = -1;
break;
case '{':