NULL);
break;
/* ... */
}
As you can see, unless you are implementing the internals of an XView package,
xv_error() may be of limited use. However, this example demonstrates how
xv_error() can be called internally by XView.
24.4.1.1 Error severity
In the above example, the reason to call xv_error() is not a serious one, at least not one
that should terminate the program. By default, calling xv_error() is a recoverable error,
so in order to specify a non-recoverable error, the call to xv_error() should pass the attri-
bute, ERROR_SEVERITY, and the value ERROR_NON_RECOVERABLE. Unrecoverable errors
generally terminate using exit() with a non-zero exit status. However, abort() may be
used instead to generate a core image used for debugging.
In any event, if the error handler is called, it should print a warning message and either con-
tinue or exit accordingly. If you install your own error routine, the choice is yours. Example
24-1 in the next section, shows how the error severity can be evaluated and acted upon
accordingly.
24.4.2 Revisiting the Error Handler
Advanced usage of the ERROR package provides us with the ability to scan the attribute list in
search of the causes of the error. With this information, you can print out more useful error
messages or display them in a manner other than printing to stderr.
Recall that there are two parameters passed to the function:
error_proc(object, avlist)
Xv_object object;
Attr_avlist avlist;
object is the object in which the xv_* call failed to operate. You can get the type of ob-
ject by calling:
Xv_pkg *pkg = (Xv_pkg *)xv_get(object, XV_TYPE);
If the error itself pertains to the object, then getting the type of the object may generate
another error. You should not attempt to get the package until you test the error code in the
Attr_avlist to be sure the error is not with the object parameter. If the error is not due
to the object itself, the package returned by xv_get() indicates to which XView package
the object belongs. This value matches the same argument as the second parameter to
xv_create(). For example, the package returned may be
MENU, CANVAS, PANEL_
BUTTON
, SERVER, etc.
572 XView Programming Manual
The Attr_avlist may be scanned for the ERROR attributes. Example 24-1 shows how
this is done. This is a very simplistic example and is for demonstration purposes only. A
more complete example may be found in the function xv_error_format() in the XView
source code.
Example 24-1. Example error parsing function
int
my_error_handler(object, avlist)
Xv_object object;
Attr_avlist avlist;
{
Attr_avlist attrs;
Error_layer layer;
Error_severity severity = ERROR_RECOVERABLE;
int n = 0;
char strs[64][7];
for (attrs = avlist; *attrs && n < 5; attrs = attr_next(attrs)) {
switch ((int) attrs[0]) {
case ERROR_BAD_ATTR:
sprintf(strs[n++], "bad attribute %s",
attr_name(attr[1]));
break;
case ERROR_BAD_VALUE:
sprintf(strs[n++],
"bad value (0x%x) for attribute %s", attrs[1],
attr_name(attrs[2]));
break;
case ERROR_INVALID_OBJECT:
sprintf(strs[n++], "invalid object (%s)",
(char *) attrs[1]);
break;
case ERROR_STRING:
sprintf(strs[n++], "%s", (char *) attrs[1]);
break;
case ERROR_PKG:
sprintf(strs[n++], "Package: %s",
((Xv_pkg *)attrs[1])->name);
break;
case ERROR_SEVERITY:
severity = attrs[1];
}
}
strcpy(strs[n++], "Core dump?");
strs[n] = 0;
if (notice_prompt(base_frame, (Event *)NULL,
NOTICE_MESSAGE_STRINGS_ARRAY_PTR, strs,
NOTICE_BUTTON_YES, "Yes",
NOTICE_BUTTON_NO, "No",
NULL) == NOTICE_YES)
abort();
if (severity == ERROR_NON_RECOVERABLE)
exit(1);
return XV_OK;
}
Error Recovery
Error Recovery 573
This error handling routine sets a set of error messages in an array of buffers. A notice is
used to display the messages and prompt the user to generate a core image of the program for
debugging. Selecting “Yes” causes abort() to be called. The program exits if the severity
is non-recoverable, and continues otherwise.
One particular note of interest is the use of the routine attr_name(). This is a hypotheti-
cal routine that you would have to write to convert the actual enumerated attribute-values
into strings that make sense to read. If you write your own XView package with new attri-
butes, you will have to write a routine equivalent to attr_name(). The routine should
return a static char * describing the attribute. If the attribute has no corresponding string
(e.g., it is an unknown attribute), it should return a string indicating the integer or hexade-
cimal value.
574 XView Programming Manual
This page intentionally left blank
to preserve original page counts.
This page intentionally left blank
to preserve original page counts.

Get Volume 7A: XView Programming Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.