sh: Add a function to print warnings (with command name and newline).

This is like error() but without raising an exception.
It is particularly useful as a replacement for the warnx macro in
bltin/bltin.h.
This commit is contained in:
Jilles Tjoelker 2010-12-21 20:47:06 +00:00
parent b5224580a4
commit 5fe9123ff5
6 changed files with 27 additions and 14 deletions

View File

@ -246,7 +246,7 @@ aliascmd(int argc, char **argv)
while ((n = *++argv) != NULL) {
if ((v = strchr(n+1, '=')) == NULL) /* n+1: funny ksh stuff */
if ((ap = lookupalias(n, 0)) == NULL) {
outfmt(out2, "alias: %s not found\n", n);
warning("%s not found", n);
ret = 1;
} else
printalias(ap);

View File

@ -57,11 +57,7 @@
#define fwrite(ptr, size, nmemb, file) outbin(ptr, (size) * (nmemb), file)
#define fflush flushout
#define INITARGS(argv)
#define warnx(...) do { \
out2fmt_flush("%s: ", commandname); \
out2fmt_flush(__VA_ARGS__); \
out2fmt_flush("\n"); \
} while (0)
#define warnx warning
#define errx(exitstatus, ...) error(__VA_ARGS__)
#else

View File

@ -224,7 +224,7 @@ cdphysical(char *dest)
}
p = findcwd(NULL);
if (p == NULL)
out2fmt_flush("cd: warning: failed to get name of current directory\n");
warning("warning: failed to get name of current directory");
updatepwd(p);
INTON;
return (0);

View File

@ -134,6 +134,26 @@ onint(void)
}
static void
vwarning(const char *msg, va_list ap)
{
if (commandname)
outfmt(out2, "%s: ", commandname);
doformat(out2, msg, ap);
out2fmt_flush("\n");
}
void
warning(const char *msg, ...)
{
va_list ap;
va_start(ap, msg);
vwarning(msg, ap);
va_end(ap);
}
/*
* Exverror is called to raise the error exception. If the first argument
* is not NULL then error prints an error message using printf style
@ -158,12 +178,8 @@ exverror(int cond, const char *msg, va_list ap)
else
TRACE(("exverror(%d, NULL) pid=%d\n", cond, getpid()));
#endif
if (msg) {
if (commandname)
outfmt(out2, "%s: ", commandname);
doformat(out2, msg, ap);
out2c('\n');
}
if (msg)
vwarning(msg, ap);
flushall();
exraise(cond);
}

View File

@ -80,6 +80,7 @@ extern volatile sig_atomic_t intpending;
void exraise(int) __dead2;
void onint(void);
void warning(const char *, ...) __printflike(1, 2);
void error(const char *, ...) __printf0like(1, 2) __dead2;
void exerror(int, const char *, ...) __printf0like(2, 3) __dead2;

View File

@ -185,7 +185,7 @@ trapcmd(int argc, char **argv)
}
while (*argv) {
if ((signo = sigstring_to_signum(*argv)) == -1) {
out2fmt_flush("trap: bad signal %s\n", *argv);
warning("bad signal %s", *argv);
errors = 1;
}
INTOFF;