1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/*
------------------------------------------------------------------------------
A license is hereby granted to reproduce this software source code and
to create executable versions from this source code for personal,
non-commercial use. The copyright notice included with the software
must be maintained in all copies produced.
THIS PROGRAM IS PROVIDED "AS IS". THE AUTHOR PROVIDES NO WARRANTIES
WHATSOEVER, EXPRESSED OR IMPLIED, INCLUDING WARRANTIES OF
MERCHANTABILITY, TITLE, OR FITNESS FOR ANY PARTICULAR PURPOSE. THE
AUTHOR DOES NOT WARRANT THAT USE OF THIS PROGRAM DOES NOT INFRINGE THE
INTELLECTUAL PROPERTY RIGHTS OF ANY THIRD PARTY IN ANY COUNTRY.
Copyright (c) 1995, John Conover, All Rights Reserved.
Comments and/or bug reports should be addressed to:
john@johncon.com (John Conover)
------------------------------------------------------------------------------
message.c, print an error message
void message (int fil, char *ancillary);
prints message indicated by the error code, fil, called to lookup
the message to be printed, print it to stderr, and print any
ancillary message, perhaps a file name, referenced by the
character reference, ancillary
the array, message_array[], is an array of type MESSAGE_STRUCT
elements, each element containing a mnemonic error type, the error
number of the message (in ascending order,) both of which must be
defined and match an entry in message.h-in addition to a character
string that will be printed to stderr
the first 50 error messages, numbered 0 to 49, are system related
messages, and may be omitted if desired, or customized to a
particular system by looking for the errors in signal.h, etc.
The algorithm is as follows:
if fil is zero, just return
if fil is greater than the the size of the message array, assume
an unknown error
print the message array element implicitly addressed by fil
if the ancillary message reference is not null, print the message
referenced by the ancillary message reference
Usage is a call with an error code, and an optional ancillary message,
for example:
char *filename;
message (URFIL_ERR,filename);
would print the message for a file that failed to open, or
message (URMEM_ERR, (char *) 0);
would print the message for a memory allocation error
The argument, fill, represents the error code that corresponds to the
message to be printed, and ancillary references an additional string
that will be appended to the error code
There is no return value from this function
To test this module, compile the module source with -DTEST_MESSAGE
$Revision: 1.1 $
$Date: 1995/12/23 23:14:31 $
$Id: message.c,v 1.1 1995/12/23 23:14:31 john Exp $
$Log: message.c,v $
Revision 1.1 1995/12/23 23:14:31 john
Changed mnemonic and message elements of MESSAGE_STRUCT structure to const char to comply with formal ANSI C requirements for statically declared character strings.
Changes to files: Makefile, searchpath.c, searchfile.c, message.c, version.c-specifically to control program behavior under certain file system exceptions; specifics for the GNU gcc compiler, and ANSI C cosmetic formalities.
* Revision 1.0 1995/04/22 05:13:18 john
* Initial revision
*
*/
#include "rel.h"
#ifndef LINT /* include rcsid only if not running lint */
static char rcsid[] = "$Id: message.c,v 1.1 1995/12/23 23:14:31 john Exp john $"; /* module version */
static char rcsid_h[] = MESSAGE_H_ID; /* module include version */
#endif
#ifdef __STDC__
typedef struct message_struct /* message structure, one for each error message */
{
const char *mnemonic; /* error type */
int number; /* error number */
int arguments; /* ancillary arguments to be printed */
const char *message; /* message to be printed on exit with error */
} MESSAGE_STRUCT;
#else
typedef struct message_struct /* message structure, one for each error message */
{
char *mnemonic; /* error type */
int number; /* error number */
int arguments; /* ancillary arguments to be printed */
char *message; /* message to be printed on exit with error */
} MESSAGE_STRUCT;
#endif
int max_interrupts = 49; /* maximum value of system interrupts */
static char unknown[] = "unknown error"; /* unknown error message */
static MESSAGE_STRUCT message_array[] = /* system interrupts, 0 through 49, system dependent messages, and found in signal.h */
{
{"NO_ERROR", 0, 0, "No errors"},
{"SIGHUP", 1, 0, "hangup"},
{"SIGINT", 2, 0, "interrupt"},
{"SIGQUIT", 3, 0, "quit"},
{"SIGILL", 4, 0, "illegal instruction"},
{"SIGTRAP", 5, 0, "trace trap"},
{"SIGIOT", 6, 0, "IOT instruction"},
{"SIGEMT", 7, 0, "EMT instruction"},
{"SIGFPE", 8, 0, "floating point exception"},
{"SIGKILL", 9, 0, "kill"},
{"SIGBUS", 10, 0, "bus error"},
{"SIGSEGV", 11, 0, "segmentation violation"},
{"SIGSYS", 12, 0, "bad argument to system call"},
{"SIGPIPE", 13, 0, "write on a pipe with no one to read it"},
{"SIGALRM", 14, 0, "alarm clock"},
{"SIGTERM", 15, 0, "software termination signal from kill"},
{"SIGUSR1", 16, 0, "user defined signal 1"},
{"SIGUSR2", 17, 0, "user defined signal 2"},
{"SIGCLD", 18, 0, "child status change"},
{"SIGPWR", 19, 0, "power-fail restart"},
{"SIGWINCH", 20, 0, "window size change"},
{"SIGURG", 21, 0, "urgent socket condition"},
{"SIGPOLL", 22, 0, "pollable event occured"},
{"SIGSTOP", 23, 0, "stop"},
{"SIGTSTP", 24, 0, "user stop requested from tty"},
{"SIGCONT", 25, 0, "stopped process has been continued"},
{"SIGTTIN", 26, 0, "background tty read attempted"},
{"SIGTTOU", 27, 0, "background tty write attempted"},
{"SIGVTALRM", 28, 0, "virtual timer expired"},
{"SIGPROF", 29, 0, "profiling timer expired"},
{"SIGXCPU", 30, 0, "exceeded cpu limit"},
{"SIGXFSZ", 31, 0, "exceeded file size limit"},
{"UNKNOWN", 32, 0, unknown},
{"UNKNOWN", 33, 0, unknown},
{"UNKNOWN", 34, 0, unknown},
{"UNKNOWN", 35, 0, unknown},
{"UNKNOWN", 36, 0, unknown},
{"UNKNOWN", 37, 0, unknown},
{"UNKNOWN", 38, 0, unknown},
{"UNKNOWN", 39, 0, unknown},
{"UNKNOWN", 40, 0, unknown},
{"UNKNOWN", 41, 0, unknown},
{"UNKNOWN", 42, 0, unknown},
{"UNKNOWN", 43, 0, unknown},
{"UNKNOWN", 44, 0, unknown},
{"UNKNOWN", 45, 0, unknown},
{"UNKNOWN", 46, 0, unknown},
{"UNKNOWN", 47, 0, unknown},
{"UNKNOWN", 48, 0, unknown},
{"UNKNOWN", 49, 0, unknown},
/* program specific errors */
{"URISG_ERR", 50, 0, "error installing signal handler"},
{"URIGN_ERR", 51, 0, "error disabling interrupts"},
{"URRSG_ERR", 52, 0, "error restoring default interrupts"},
{"URMEM_ERR", 53, 0, "error allocating memory"},
{"URPAR_ERR", 54, 0, "error in grouping operators"},
{"URSYN_ERR", 55, 0, "error in syntax"},
{"URFIL_ERR", 56, 1, "error opening file, %s"}, /* requires ancillary filename argument */
{"URLCK_ERR", 57, 1, "error locking file, %s"}, /* requires ancillary filename argument */
{"URRED_ERR", 58, 1, "error reading from file, %s"}, /* requires ancillary filename argument */
{"URUCK_ERR", 59, 1, "error unlocking file, %s"}, /* requires ancillary filename argument */
{"URCLS_ERR", 60, 1, "error closing file, %s"}, /* requires ancillary filename argument */
{"URSTA_ERR", 61, 1, "error stating file, %s"}, /* requires ancillary filename argument */
{"URODR_ERR", 62, 1, "error opening directory, %s"}, /* requires ancillary filename argument */
{"URCDR_ERR", 63, 1, "error closing directory, %s"}, /*requires ancillary filename argument */
{"URARG_ERR", 64, 1, "usage: %s [-v] pattern filename(s)"} /* requires ancillary program name argument */
};
static size_t array_size = sizeof (message_array) / sizeof (MESSAGE_STRUCT); /* size of message structure array */
#ifdef __STDC__
void message (int fil, char *ancillary)
#else
void message (fil, ancillary)
int fil;
char *ancillary;
#endif
{
MESSAGE_STRUCT *message_ref; /* message array element reference */
if (fil != 0) /* requested message signify no error? */
{
if (fil < 0) /* fil negative? */
{
fil = -fil; /* yes, use the absolute value */
}
if (fil >= (int) array_size) /* requested message in bounds of array? */
{
fil = 49; /* assume unknown error */
}
message_ref = &message_array[fil]; /* yes, reference the message */
if (message_ref->arguments == 0) /* any ancillary reference to additional error message? */
{
(void) fprintf (stderr, message_ref->message); /* print the message */
}
else
{
if (ancillary == (char *) 0) /* protect from programming errors */
{
ancillary = unknown;
}
(void) fprintf (stderr, message_ref->message, ancillary); /* print the message with the ancillary message */
}
(void) fprintf (stderr, "\n"); /* terminate the message */
}
}
#ifdef TEST_MESSAGE
/*
simple exerciser for testing message (); get a number from stdin, and
print the corresponding error message-ignore the:
declared global, could be static
message message.c(xxx)
from lint
*/
#include <stdio.h>
#include <stdlib.h>
#include "message.h"
#ifdef __STDC__
int main (int argc, char *argv[])
#else
int main (argc, argv)
int argc;
char *argv[];
#endif
{
char buffer[BUFSIZ], /* input buffer */
*ancillary = (char *) 0; /* ancillary message reference */
if (argc > 1) /* any argument? */
{
ancillary = argv[1]; /* assume argument is ancillary message */
}
while (gets (buffer) != 0) /* input the number */
{
message (atoi (buffer), ancillary); /* convert the input buffer to a number, and print the corresponding error message */
}
exit (0); /* always return 0 */
#ifdef LINT /* include only if running lint */
return (0); /* for LINT formality */
#endif
}
#endif
|