_ERROR MESSAGE MANAGEMENT_ by Rohan Douglas [LISTINÇ ONE] /* Include file for macros replacement of erroò function® */ #define error(m) _error(__FILE__, __LINE__) extern void _error(char *, int); [LISTINÇ TWO] /* Definition of error function® */ #include #include #include #include "error.h" static int get_err_msg(char *, char *, char *); void _error(char *fname, int lno) { char *period; /* Pointer for file extension */ char lstr[6]; /* Temp string for file line */ char key[15]; /* Message key (file:line) */ char msg[75]; /* Error message from file */ /* Strip out file extension from file name */ if ((period = strchr(fname, '.')) != NULL) *period = '\0'; /* Create file:linenumber key */ sprintf(key, "%s:%s", fname, itoa(lno, lstr, 10)); /* Look up error message from file */ if (!get_err_msg(fname, key, msg)) /* Not found in file, just print out * file:lineno. */ printf("Error [%s]", key); else printf("%s [%s]", msg, key); return; } /* error() */ static int get_err_msg(char *fname, char *key, char *msg) { FILE *fp; /* Error message file pointer */ char msg_key[14];/* Key for current line */ /* Open error file */ if (!(fp = fopen("error.dat", "r"))) return FALSE; /* Scan file for message */ while (!feof(fp)) { fscanf(fp, " %s ", msg_key); fgets(msg, 75, fp); if (!strcmpi(key, msg_key)) break; } Š fclose(fp); /* Return false if message not found */ if (feof(fp)) return FALSE; /* Remove CR from message string */ msg[strlen(msg)-1] = '\0'; return TRUE; } /* get_err_msg() */ [LISTINÇ THREE] BEGIN { FS = "\""; printf("") > "error.txt"; printf("") > "error.dat"; } / *error *\(/ { printf("%s :\n", $2) >> "error.txt"; ind = substr(FILENAME, 0, index(FILENAME, ".") - 1); printf("%s:%d\t%s\n", ind, NR, $2) >> "error.dat"; getline(); i = index($0, "/* "); if (i) i++; while (i) { printf("\t%s\n", substr($0, i + 2)) >> "error.txt"; getline(); if (index($0, "*/")) break; i = index($0, "* "); } } Figure 1: Sample source file with embedded error message and description. #include #include "error.h" main(int argc, char *argv[]) { if (argc == 1) error("Missing parameters"); /* No parameters have been passed * to this test program. This error message * will be displayed as an example if no * parameters are passed to this test * program. */ exit(0); } Figure 2: The user manual list resulting from Figure 1. Missing parameters : No parameters have been passed to this test program. This error message will be displayed as an example if no parameters are passed to this test program. Figure 3: The message database resulting from Figure 1. test:8 Missing parameters Figure 4: The results of the sample program. Missing parameters [test:8]