This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH] rpcgen: make error() helper into a varargs func
- From: Mike Frysinger <vapier at gentoo dot org>
- To: libc-alpha at sourceware dot org
- Date: Tue, 28 Feb 2017 13:20:07 -0700
- Subject: [PATCH] rpcgen: make error() helper into a varargs func
- Authentication-results: sourceware.org; auth=none
A minor clean up to the code to avoid fixed buffers for output messages
and to make error() more useful internally.
---
sunrpc/proto.h | 2 +-
sunrpc/rpc_parse.c | 13 ++-----------
sunrpc/rpc_scan.c | 13 +++----------
sunrpc/rpc_util.c | 28 ++++++++++++----------------
sunrpc/rpc_util.h | 2 +-
5 files changed, 19 insertions(+), 39 deletions(-)
diff --git a/sunrpc/proto.h b/sunrpc/proto.h
index ea28565b1e6e..ff94fe214a2b 100644
--- a/sunrpc/proto.h
+++ b/sunrpc/proto.h
@@ -45,7 +45,7 @@ void write_tables(void);
/****** rpc_util.c ******/
void reinitialize(void);
int streq(const char *a, const char *b);
-void error(const char *msg) __attribute__ ((noreturn));
+void error(const char *msg, ...) __attribute__ ((noreturn, __format__ (__printf__, 1, 2)));
void crash(void) __attribute__ ((noreturn));
void tabify(FILE *f, int tab);
char *make_argname(const char *pname, const char *vname);
diff --git a/sunrpc/rpc_parse.c b/sunrpc/rpc_parse.c
index 505a6554cfa3..be2eb9bd2b4e 100644
--- a/sunrpc/rpc_parse.c
+++ b/sunrpc/rpc_parse.c
@@ -409,27 +409,18 @@ static void
check_type_name (const char *name, int new_type)
{
int i;
- char tmp[100];
for (i = 0; reserved_words[i] != NULL; i++)
{
if (strcmp (name, reserved_words[i]) == 0)
- {
- sprintf (tmp,
- "illegal (reserved) name :\'%s\' in type definition", name);
- error (tmp);
- }
+ error ("illegal (reserved) name :\'%s\' in type definition", name);
}
if (new_type)
{
for (i = 0; reserved_types[i] != NULL; i++)
{
if (strcmp (name, reserved_types[i]) == 0)
- {
- sprintf (tmp,
- "illegal (reserved) name :\'%s\' in type definition", name);
- error (tmp);
- }
+ error ("illegal (reserved) name :\'%s\' in type definition", name);
}
}
}
diff --git a/sunrpc/rpc_scan.c b/sunrpc/rpc_scan.c
index a230aa396b37..95405687f30f 100644
--- a/sunrpc/rpc_scan.c
+++ b/sunrpc/rpc_scan.c
@@ -303,19 +303,12 @@ get_token (token *tokp)
if (!(isalpha (*where) || *where == '_'))
{
char buf[100];
- char *p;
- s_print (buf, _("illegal character in file: "));
- p = buf + strlen (buf);
if (isprint (*where))
- {
- s_print (p, "%c", *where);
- }
+ s_print (buf, "%c", *where);
else
- {
- s_print (p, "%d", *where);
- }
- error (buf);
+ s_print (buf, "%d", *where);
+ error (_("illegal character in file: %s"), buf);
}
findkind (&where, tokp);
break;
diff --git a/sunrpc/rpc_util.c b/sunrpc/rpc_util.c
index 52aa69757b30..7e62f95f2ac6 100644
--- a/sunrpc/rpc_util.c
+++ b/sunrpc/rpc_util.c
@@ -35,6 +35,7 @@
*/
#include <stdio.h>
#include <ctype.h>
+#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "rpc_scan.h"
@@ -270,11 +271,16 @@ pvname (const char *pname, const char *vnum)
* print a useful (?) error message, and then die
*/
void
-error (const char *msg)
+error (const char *msg, ...)
{
+ va_list arg;
+
printwhere ();
f_print (stderr, "%s, line %d: ", infilename, linenum);
- f_print (stderr, "%s\n", msg);
+ va_start (arg, msg);
+ vfprintf (stderr, msg, arg);
+ f_print (stderr, "\n");
+ va_end (arg);
crash ();
}
@@ -308,17 +314,13 @@ record_open (const char *file)
}
}
-static char expectbuf[100];
-
/*
* error, token encountered was not the expected one
*/
void
expected1 (tok_kind exp1)
{
- s_print (expectbuf, "expected '%s'",
- toktostr (exp1));
- error (expectbuf);
+ error ("expected '%s'", toktostr (exp1));
}
/*
@@ -327,10 +329,7 @@ expected1 (tok_kind exp1)
void
expected2 (tok_kind exp1, tok_kind exp2)
{
- s_print (expectbuf, "expected '%s' or '%s'",
- toktostr (exp1),
- toktostr (exp2));
- error (expectbuf);
+ error ("expected '%s' or '%s'", toktostr (exp1), toktostr (exp2));
}
/*
@@ -339,11 +338,8 @@ expected2 (tok_kind exp1, tok_kind exp2)
void
expected3 (tok_kind exp1, tok_kind exp2, tok_kind exp3)
{
- s_print (expectbuf, "expected '%s', '%s' or '%s'",
- toktostr (exp1),
- toktostr (exp2),
- toktostr (exp3));
- error (expectbuf);
+ error ("expected '%s', '%s' or '%s'", toktostr (exp1), toktostr (exp2),
+ toktostr (exp3));
}
void
diff --git a/sunrpc/rpc_util.h b/sunrpc/rpc_util.h
index 53316d9516db..e46a5d2a605a 100644
--- a/sunrpc/rpc_util.h
+++ b/sunrpc/rpc_util.h
@@ -115,7 +115,7 @@ void pvname(const char *pname, const char *vnum);
void ptype(const char *prefix, const char *type, int follow);
int isvectordef(const char *type, relation rel);
int streq(const char *a, const char *b);
-void error(const char *msg);
+void error(const char *msg, ...);
void tabify(FILE *f, int tab);
void record_open(const char *file);
bas_type *find_type(const char *type);
--
2.11.1