[PATCH:xscope 23/24] Refactor error printing to use common functions

Alan Coopersmith alan.coopersmith at oracle.com
Fri Aug 31 22:18:05 PDT 2012


All errors currently recognized by xscope fall into two forms,
with or without a 32-bit value to print as a bad value, so use
common implementations for those two forms so we can stop
duplicating that code for every new extension-defined error.

Signed-off-by: Alan Coopersmith <alan.coopersmith at oracle.com>
---
 print11.c      |  150 +++++++++++++++-----------------------------------------
 print_render.c |   40 ++-------------
 x11.h          |    5 ++
 3 files changed, 49 insertions(+), 146 deletions(-)

diff --git a/print11.c b/print11.c
index 7935b84..cf477e2 100644
--- a/print11.c
+++ b/print11.c
@@ -193,213 +193,141 @@ const char *REPLYHEADER   = "..............REPLY";
 
 /* Error Printing procedures */
 
+/* generic routine for printing common format error messages with a
+   labeled bad value in bytes 4-7. */
 void
-RequestError(const unsigned char *buf)
+printErrorWithValue(const unsigned char *buf,
+                    short FieldType, const char *desc)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
+    PrintField(buf, 1, 1, ERROR, ERRORHEADER);
     if (Verbose < 1)
         return;
     printfield(buf, 2, 2, CARD16, "sequence number");
+    PrintField(buf, 4, 4, FieldType, desc);
     PrintField(buf, 8, 2, CARD16, "minor opcode");
     PrintField(buf, 10, 1, CARD8, "major opcode");
 }
 
+/* generic routine for printing common format error messages with
+   no additional information provided. */
 void
-ValueError(const unsigned char *buf)
+printErrorNoValue(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Value */
+    PrintField(buf, 1, 1, ERROR, ERRORHEADER);
     if (Verbose < 1)
         return;
     printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, INT32, "bad value");
+    /* 4 bytes unused */
     PrintField(buf, 8, 2, CARD16, "minor opcode");
     PrintField(buf, 10, 1, CARD8, "major opcode");
 }
 
 void
+RequestError(const unsigned char *buf)
+{
+    printErrorNoValue(buf);
+}
+
+void
+ValueError(const unsigned char *buf)
+{
+    printErrorWithValue(buf, INT32, "bad value");
+}
+
+void
 WindowError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Window */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 PixmapError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Pixmap */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 AtomError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Atom */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad atom id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad atom id");
 }
 
 void
 CursorError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Cursor */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 FontError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Font */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 MatchError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Match */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 DrawableError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Drawable */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 AccessError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Access */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 AllocError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Alloc */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 ColormapError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Colormap */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 GContextError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* GContext */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 IDChoiceError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* IDChoice */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 void
 NameError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Name */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 LengthError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Length */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 ImplementationError(const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Implementation */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorNoValue(buf);
 }
 
 void
 UnknownError(const unsigned char *buf)
 {
-    PrintField(RBf, 1, 1, ERROR, ERRORHEADER);
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "bad resource id");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "bad resource id");
 }
 
 /* ************************************************************ */
diff --git a/print_render.c b/print_render.c
index 092fc9c..67f3a34 100644
--- a/print_render.c
+++ b/print_render.c
@@ -723,59 +723,29 @@ RenderCreateConicalGradient(FD fd, const unsigned char *buf)
 void
 RenderPictFormatError(FD fd, const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, PICTFORMAT, "format");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, PICTFORMAT, "format");
 }
 
 void
 RenderPictureError(FD fd, const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, PICTURE, "picture");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, PICTURE, "picture");
 }
 
 void
 RenderPictOpError(FD fd, const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, PICTOP, "pictop");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, PICTOP, "pictop");
 }
 
 void
 RenderGlyphSetError(FD fd, const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, GLYPHSET, "glyphset");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, GLYPHSET, "glyphset");
 }
 
 void
 RenderGlyphError(FD fd, const unsigned char *buf)
 {
-    PrintField(buf, 1, 1, ERROR, ERRORHEADER); /* Request */
-    if (Verbose < 1)
-        return;
-    printfield(buf, 2, 2, CARD16, "sequence number");
-    PrintField(buf, 4, 4, CARD32, "glyph");
-    PrintField(buf, 8, 2, CARD16, "minor opcode");
-    PrintField(buf, 10, 1, CARD8, "major opcode");
+    printErrorWithValue(buf, CARD32, "glyph");
 }
diff --git a/x11.h b/x11.h
index 6cdf270..fa6cfef 100644
--- a/x11.h
+++ b/x11.h
@@ -656,4 +656,9 @@ uint32_t getreqlen(FD fd, const unsigned char *buf) {
 /* Constant defined in Generic Event Protocol 1.0 for event type */
 #define Event_Type_Generic	35
 
+/* Routines for printing the two common forms of error responses */
+extern void printErrorWithValue(const unsigned char *buf,
+                                short FieldType, const char *desc);
+extern void printErrorNoValue(const unsigned char *buf);
+
 #endif                          /* XSCOPE_X11_H */
-- 
1.7.9.2



More information about the xorg-devel mailing list