shhmsg-1.4.2/000755 000765 000024 00000000000 12266760574 014034 5ustar00sverrehustaff000000 000000 shhmsg-1.4.2/ChangeLog000644 000765 000024 00000005562 12266760247 015613 0ustar00sverrehustaff000000 000000 2014-01-19 Sverre H. Huseby * Release 1.4.2 2014-01-19 Emanuele Rocca * Avoid FTBFS when building with hardened security flags When building with -Werror=format-security, compilation fails if printf and scanf are called without format arguments. 2002-03-02 Sverre H. Huseby * Release 1.4.1 * Updated comment style to match the one I currently use. * errno.c (msgPerror, msgFatalPerror): Using strerror() instead of sys_errlist[]. Patch from Oohara Yuuma . * internal.h (GET_ERROR_STREAM): The macro would return the message stream instead of the error stream if not set by the programmer. Patch from Oohara Yuuma. * Makefile: Cleaned up some whitespace mess. Patch from Oohara Yuuma. 1998-10-13 Sverre H. Huseby * Release 1.4.0 * Changed internal stream variables to not be initialised to stdio etc, as this would not compile with glibc2 ("initializer element is not constant"). 1998-09-04 Sverre H. Huseby * errno.c: Redefined sys_errlist to match GNU libc 2. 1998-07-05 Sverre H. Huseby * Release 1.3.4 * Updated author address. Tue May 27 23:09:35 1997 Sverre H. Huseby * Release: 1.3.3 * shhmsg.h: Added GNU C specials for checking format parameters to functions. Sun Mar 23 09:34:43 1997 Sverre H. Huseby * Release: 1.3.2 * Makefile: Creating lib*.so in addition to lib*.so.* Tue Nov 12 09:01:40 1996 Sverre H. Huseby * Version: 1.3.1 * Minor code restructuring and documentation updates. Mon Aug 14 16:50:04 1995 Sverre H. Huseby * New version: 1.3 * Incorporated Louis' changes in the distribution, with some rewriting to match my coding style and preference. Any bugs are mine. Sat Aug 13 21:46:00 1995 Louis W. Erickson * Added the msgSetShowNameAlways function, allowing a forcing of the display of the app's name. * Added the msgSetXxxStream functions, allowing streams to be redirected to other things, besides the defaults. * Added some MSG_ constants. * Added code to allow use of an environmental variable as the verbosity level for msgVerbose. Setting a verbosity of MSG_VERBOSE_DEFAULT enables this. * Wrote a rudimentary quick reference. Fri Aug 11 00:25:34 1995 Sverre H. Huseby * Version 1.2 * Updated README and INSTALL a bit. Sun Jul 16 17:49:52 1995 Sverre H. Huseby * Flushing stdout before writing anything, to avoid order corruption if stderr equals stdout. The idea is taken from W. Richard Stevens. * verbose.c/shhmsg.h: Added a verbosity level instead of just on/off. * Makefile: using -f when symlinking shared libraries. Changed version number to 1.1. Added ChangeLog to the DISTFILES. shhmsg-1.4.2/CREDITS000644 000765 000024 00000000414 12266760122 015040 0ustar00sverrehustaff000000 000000 Louis W. Erickson added both nice functionality and documentation. See the ChangeLog file for details. Oohara Yuuma fixed a couple of bugs. Emanuele Rocca fixed usage of fprintf without format string. shhmsg-1.4.2/errno.c000644 000765 000024 00000006471 12266757672 015342 0ustar00sverrehustaff000000 000000 /* $Id: errno.c,v 1.6 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE errno.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION The functions in this file, are for displaying errno- | dependant error messages. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include #include #include #include "internal.h" #include "shhmsg.h" #ifndef HAVE_STRERROR extern const char *const sys_errlist[]; #define strerror(x) sys_errlist[x] #endif /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgPerror | | FUNCTION Show an errno-dependant error message. | | SYNOPSIS #include "shhmsg.h" | void msgPerror(const char *format, ...); | | INPUT format, ... | Arguments used as with printf(). If NULL, | the name of the program is printed. | | DESCRIPTION Prints the given text followed by ": " and the | correct errormessage on the _msgErrorStream. | May add the program name before anything is printed. | | If errno indicates "no error", nothing is printed. */ void msgPerror(const char *format, ...) { va_list ap; int en; if (!(en = errno)) return; fflush(stdout); if (format && *format) { if (_msgShowNameAlways) fprintf(GET_ERROR_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_ERROR_STREAM, format, ap); va_end(ap); } else fputs(msgGetName(), GET_ERROR_STREAM); fprintf(GET_ERROR_STREAM, ": %s\n", strerror(en)); } /*------------------------------------------------------------------------ | | NAME msgFatalPerror | | FUNCTION Show an errno-dependant error message and abort the program. | | SYNOPSIS #include "shhmsg.h" | void msgFatalPerror(const char *format, ...); | | INPUT format, ... | Arguments used as with printf(). If NULL, | the name of the program is printed. | | RETURNS Nothing, never returns. | | DESCRIPTION Prints the given text followed by ": " and the | correct errormessage on the _msgErrorStream, and then | aborts the program. | May add the program name before anything is printed. | | If errno indicates "no error", nothing is printed, | and the program keeps running. */ void msgFatalPerror(const char *format, ...) { va_list ap; int en; if (!(en = errno)) return; fflush(stdout); if (format && *format) { if (_msgShowNameAlways) fprintf(GET_ERROR_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_ERROR_STREAM, format, ap); va_end(ap); } else fputs(msgGetName(), GET_ERROR_STREAM); fprintf(GET_ERROR_STREAM, ": %s\n", strerror(en)); exit(99); } shhmsg-1.4.2/error.c000644 000765 000024 00000002661 07440225000 015310 0ustar00sverrehustaff000000 000000 /* $Id: error.c,v 1.6 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE error.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Function for displaying a simple error message. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgError | | FUNCTION Show given message on the _msgErrorStream. | | SYNOPSIS #include "shhmsg.h" | void msgError(const char *format, ...); | | INPUT format, ... | Arguments used as with printf(). | | DESCRIPTION Prints the name of this program followed by ": " and the | given message on the _msgErrorStream. */ void msgError(const char *format, ...) { va_list ap; fflush(stdout); fprintf(GET_ERROR_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_ERROR_STREAM, format, ap); va_end(ap); } shhmsg-1.4.2/fatal.c000644 000765 000024 00000003135 07440225000 015243 0ustar00sverrehustaff000000 000000 /* $Id: fatal.c,v 1.6 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE fatal.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Function for displaying an error message and | abort the program. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgFatal | | FUNCTION Show given message and abort the program. | | SYNOPSIS #include "shhmsg.h" | void msgFatal(const char *format, ...); | | INPUT format, ... | Arguments used as with printf(). | | RETURNS Never returns. The program is aborted. | | DESCRIPTION Prints the name of this program followed by ": " and | the given message on the _msgErrorStream, then aborts | the program. */ void msgFatal(const char *format, ...) { va_list ap; fflush(stdout); fprintf(GET_ERROR_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_ERROR_STREAM, format, ap); va_end(ap); exit(99); } shhmsg-1.4.2/INSTALL000644 000765 000024 00000001245 07440225027 015052 0ustar00sverrehustaff000000 000000 Installing shhmsg ================= If you don't have gcc, you'll have to edit the Makefile. If your compiler is not ANSI, you'll have to change all files. 1. Check the Makefile. In particular, if you run Linux with ELF, you may want to make a shared library (search for SHARED). 2. Build the library make dep make 3. Install library and header file make install By default, the library will be installed in /usr/local/lib, and the header file in /usr/local/include. If you happen to know how to make one of those fancy configure files, and feel like making one for this library, please do so and send the files to me (contact info in the README file). shhmsg-1.4.2/internal.h000644 000765 000024 00000001546 07440225000 016001 0ustar00sverrehustaff000000 000000 /* $Id: internal.h,v 1.4 2002/03/02 19:37:36 sverrehu Exp $ */ #ifndef INTERNAL_SHHMSG_H #define INTERNAL_SHHMSG_H #include /* FILE */ /* streams for error messages, verbose messages, and general messages. */ #define MSG_DEFAULT_ERROR_STREAM stderr #define MSG_DEFAULT_VERBOSE_STREAM stderr #define MSG_DEFAULT_MESSAGE_STREAM stdout extern FILE *_msgErrorStream; extern FILE *_msgVerboseStream; extern FILE *_msgMessageStream; #define GET_ERROR_STREAM \ ((_msgErrorStream == NULL) \ ? MSG_DEFAULT_ERROR_STREAM \ : _msgErrorStream) #define GET_VERBOSE_STREAM \ ((_msgVerboseStream == NULL) \ ? MSG_DEFAULT_VERBOSE_STREAM \ : _msgVerboseStream) #define GET_MESSAGE_STREAM \ ((_msgMessageStream == NULL) \ ? MSG_DEFAULT_MESSAGE_STREAM \ : _msgMessageStream) extern int _msgShowNameAlways; #endif shhmsg-1.4.2/Makefile000644 000765 000024 00000004421 12266760463 015472 0ustar00sverrehustaff000000 000000 DIST = shhmsg VERMAJ = 1 VERMIN = 4 VERPAT = 2 VERSION = $(VERMAJ).$(VERMIN).$(VERPAT) # Define SHARED as 1 for Linux shared ELF library SHARED = 0 ifeq ($(SHARED),1) LIBTARGET = lib$(DIST).so.$(VERSION) LIBTARGETSO = lib$(DIST).so LIBTARGETSOMAJ = $(LIBTARGETSO).$(VERMAJ) CCSHRD = -fPIC else LIBTARGET = lib$(DIST).a endif LIBHEAD = $(DIST).h TARGETS = $(LIBTARGET) INSTBASEDIR = /usr/local INSTLIBDIR = $(INSTBASEDIR)/lib INSTINCDIR = $(INSTBASEDIR)/include INSTALL = install -m 644 INSTALLPROG = install -m 755 MKDIRP = install -d -m 755 CC = gcc OPTIM = -O2 INCDIR = -I. CCOPT = -s -Wall -Werror=format-security $(OPTIM) $(INCDIR) -DHAVE_STRERROR # Object files to store in the library LIBOBJS = vars.o streams.o progname.o \ errno.o error.o fatal.o message.o verbose.o all: $(TARGETS) $(LIBTARGET): $(LIBOBJS) ifeq ($(SHARED),1) $(CC) -shared -Wl,-soname,$(LIBTARGETSOMAJ) -o $(LIBTARGET) $(LIBOBJS) else ar rcs $(LIBTARGET) $(LIBOBJS) endif .c.o: $(CC) $(CCSHRD) -o $@ -c $(CCOPT) $< depend dep: $(CC) $(INCDIR) -MM *.c >depend install: $(LIBTARGET) $(MKDIRP) $(INSTLIBDIR) $(INSTINCDIR) $(INSTALL) $(LIBTARGET) $(INSTLIBDIR) $(INSTALL) $(LIBHEAD) $(INSTINCDIR) ifeq ($(SHARED),1) ln -sf $(LIBTARGET) $(INSTLIBDIR)/$(LIBTARGETSOMAJ) ln -sf $(LIBTARGETSOMAJ) $(INSTLIBDIR)/$(LIBTARGETSO) echo "Now run ldconfig if necessary." endif clean: rm -f *.o core *~ depend chmod: chmod a+r * # To let the author make a distribution. The rest of the Makefile # should be used by the author only. LSMFILE = $(DIST)-$(VERSION).lsm DISTDIR = $(DIST)-$(VERSION) DISTFILE = $(DIST)-$(VERSION).tar.gz DISTFILES = README INSTALL shhmsg.txt CREDITS ChangeLog \ Makefile $(LSMFILE) \ $(LIBHEAD) \ internal.h \ errno.c \ error.c \ fatal.c \ message.c \ progname.c \ streams.c \ vars.c \ verbose.c $(LSMFILE): $(DIST).lsm.in VER=$(VERSION); \ DATE=`date "+%d%b%y"|tr '[a-z]' '[A-Z]'`; \ sed -e "s/VER/$$VER/g;s/DATE/$$DATE/g" $(DIST).lsm.in > $(LSMFILE) veryclean: clean rm -f $(TARGETS) $(DISTFILE) $(LSMFILE) dist: $(LSMFILE) chmod mkdir $(DISTDIR) chmod a+rx $(DISTDIR) ln $(DISTFILES) $(DISTDIR) tar -cvzf $(DISTFILE) $(DISTDIR) chmod a+r $(DISTFILE) rm -rf $(DISTDIR) ifeq (depend,$(wildcard depend)) include depend endif shhmsg-1.4.2/message.c000644 000765 000024 00000004734 07440225000 015606 0ustar00sverrehustaff000000 000000 /* $Id: message.c,v 1.6 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE message.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Routines for displaying messages unless "quiet mode" | is on. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC DATA | +-----------------------------------------------------------------------*/ int msgBeQuiet = 0; /* supress everything from msgMessage() ? */ /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgSetQuiet | | FUNCTION Decide whether msgMessage() should display anything. | | SYNOPSIS #include "shhmsg.h" | void msgSetQuiet(int onoff); | | INPUT onoff 1 to disable all messages, 0 to enable. | | DESCRIPTION Used in conjunction with the msgMessage() function. | Note that changing the msgBeQuiet-variable directly | is fully legal. */ void msgSetQuiet(int onoff) { msgBeQuiet = onoff; } /*------------------------------------------------------------------------ | | NAME msgMessage | | FUNCTION Show given message unless quiet mode is on. | | SYNOPSIS #include "shhmsg.h" | void msgMessage(const char *format, ...); | | INPUT format, ... | Arguments used as with printf(). | | DESCRIPTION If msgSetQuiet(0) has been called, the given message is | displayed on the _msgMessageStream. If msgSetQuiet(1) | has been called, this function just returns. */ void msgMessage(const char *format, ...) { va_list ap; if (msgBeQuiet) return; fflush(stdout); if (_msgShowNameAlways) fprintf(GET_MESSAGE_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_MESSAGE_STREAM, format, ap); va_end(ap); } shhmsg-1.4.2/progname.c000644 000765 000024 00000006606 07440225000 015772 0ustar00sverrehustaff000000 000000 /* $Id: progname.c,v 1.5 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE progname.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Routines for handling the name of the program. | This is used when displaying error messages, | according to the Unix tradition. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PRIVATE DATA | +-----------------------------------------------------------------------*/ static char *Argv0 = NULL; /* the name of the program. */ /*-----------------------------------------------------------------------+ | PUBLIC DATA | +-----------------------------------------------------------------------*/ int _msgShowNameAlways = 0; /* show program name always */ /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgSetName | | FUNCTION Specify the name of this program (from argv[0]). | | SYNOPSIS #include "shhmsg.h" | void msgSetName(const char *name); | | INPUT name Name of this program, possibly including path. | | DESCRIPTION The program name specified is used by the errorfunctions | to identify this program. */ void msgSetName(const char *name) { Argv0 = (char *) name; } /*------------------------------------------------------------------------ | | NAME msgGetName | | FUNCTION Return the name of this program. | | SYNOPSIS #include "shhmsg.h" | char *msgGetName(void); | | RETURNS Pointer to the name of this program, without any path. | | DESCRIPTION The name depends on the string passed to msgSetName(). */ char * msgGetName(void) { char *ret; #ifdef __MSDOS__ static char name[15]; #define PATHSEP '\\' #else #define PATHSEP '/' #endif if (!Argv0) return("!!!"); #ifdef __MSDOS__ if ((ret = strrchr(Argv0, PATHSEP)) == NULL) ret = Argv0; else ++ret; strcpy(name, ret); if ((ret = strchr(name, '.')) != NULL) *ret = '\0'; strlwr(name); ret = name; #else if ((ret = strrchr(Argv0, PATHSEP)) == NULL) ret = Argv0; else ++ret; #endif return ret; } /*------------------------------------------------------------------------ | | NAME msgSetShowNameAlways | | FUNCTION Enable or disable the name of the program in all output. | | SYNOPSIS #include "shhmsg.h" | void msgSetShowNameAlways(int onoff); | | INPUT onoff 1 or 0, 1 to show name always. | | DESCRIPTION This enables or disables the forcing of the name to show | in all output. */ void msgSetShowNameAlways(int onoff) { _msgShowNameAlways = onoff; } shhmsg-1.4.2/README000644 000765 000024 00000004603 12266760316 014711 0ustar00sverrehustaff000000 000000 shhmsg - library for displaying messages. ========================================= This is a set of functions for very simple message handling in terminal based programs. To use these in your own programs, include shhmsg.h, and link with the library. You should also call the function msgSetName at startup, to indicate what program identification should be used in the error messages. Most functions accept printf-like variable arguments. Overview of files ----------------- What follows is a brief overview. Please refer to the C source files for detailed descriptions. There are no manual pages yet, and probably will never be, unless someone writes them for me. errno.c Functions for displaying messages depending on the current errno-value. error.c Non-fatal (non-aborting) error message function. fatal.c Fatal error message function. Aborts the program. message.c The function in this file works quite like printf, but it is possible to make this function silent by setting a flag. progname.c For setting the name of the program. This name is used in error messages, in the traditional Unix-ish way. streams.c Handles the reassignment of streams, so that the functions can output to any user defined file stream. verbose.c printf-like function that takes an additional parmeter specifying verbosity level of the message. This lets the user filter less interresting messages. Documentation ------------- The file shhmsg.txt (contributed by Louis W. Erickson) describes the use of the library. Portability ----------- The library has (more or less recently) been compiled and `tested' on the following systems: IRIX Release 5.2 IP22 GNU/Linux 2.4.17 with glibc 2.2.5 SunOS Release 4.1.3_U1 ULTRIX V4.4 (Rev. 69) All compilations were done using GNU's gcc, and GNU's make. Author ------ The program is written by Sverre H. Huseby shh@thathost.com Lindealleen 18 A http://shh.thathost.com/ N-0487 Oslo Norway License ------- This program is released under the Artistic License: http://www.opensource.org/licenses/artistic-license.html Comments (even as simple as "I use your program") are very welcome. If you insist on paying something, please donate some money to an organization that strives to make the world a better place for everyone. I don't like bugs, so please help me removing them by reporting whatever you find! shhmsg-1.4.2/shhmsg-1.4.2.lsm000644 000765 000024 00000000763 12266760574 016510 0ustar00sverrehustaff000000 000000 Begin3 Title: shhmsg - library for displaying messages. Version: 1.4.2 Entered-date: 19JAN14 Description: C-functions for error messages, verbose messages and `normal' messages in terminalbased programs. Keywords: programming, library, lib, message, error Author: shh@thathost.com (Sverre H. Huseby) Primary-site: http://shh.thathost.com/pub-unix/ Platforms: Requires ANSI C-compiler. Copying-policy: Artistic License http://www.opensource.org/licenses/artistic-license.html End shhmsg-1.4.2/shhmsg.h000644 000765 000024 00000003006 07440166636 015472 0ustar00sverrehustaff000000 000000 /* $Id: shhmsg.h,v 1.5 1997/05/27 21:11:06 sverrehu Exp $ */ #ifndef SHHMSG_H #define SHHMSG_H #include /* FILE */ #ifdef __cplusplus extern "C" { #endif #ifdef __GNUC__ #define MSG_NORET __attribute__ ((noreturn)) #define MSG_FORMAT_1 __attribute__ ((format (printf, 1, 2))) #define MSG_FORMAT_2 __attribute__ ((format (printf, 2, 3))) #else #define MSG_NORET #define MSG_FORMAT_1 #define MSG_FORMAT_2 #endif /* program name functions */ void msgSetName(const char *s); char *msgGetName(void); /* error functions */ void msgError(const char *format, ...) MSG_FORMAT_1 ; void msgFatal(const char *format, ...) MSG_FORMAT_1 MSG_NORET ; void msgPerror(const char *format, ...) MSG_FORMAT_1 ; void msgFatalPerror(const char *format, ...) MSG_FORMAT_1 ; /* message output functions */ void msgMessage(const char *format, ...) MSG_FORMAT_1 ; void msgVerbose(int level, const char *format, ...) MSG_FORMAT_2 ; /* output control functions */ void msgSetQuiet(int onoff); void msgSetVerbose(int level); void msgSetShowNameAlways(int onoff); FILE *msgSetErrorStream(FILE *f); FILE *msgSetVerboseStream(FILE *f); FILE *msgSetMessageStream(FILE *f); /* constants */ #define MSG_VERBOSE_DEFAULT -1 #define MSG_VERBOSE_NONE 0 #define MSG_ON 1 #define MSG_OFF 0 #define MSG_QUIET 1 #define MSG_OUTPUT 0 /* variables that may be changed */ extern int msgVerboseLevel; extern int msgBeQuiet; #undef MSG_NORET #undef MSG_FORMAT_1 #undef MSG_FORMAT_2 #ifdef __cplusplus } #endif #endif shhmsg-1.4.2/shhmsg.txt000644 000765 000024 00000022474 07440224763 016070 0ustar00sverrehustaff000000 000000 [Thanks to Louis W. Erickson for contributing this file] ------------------------------------------------------------------------------ INTRODUCTION: This is a quick overview of all the functions included in this library, with all their inputs and return values listed and defined in one place, so you don't need to refer to the header and/or the sources as much. ------------------------------------------------------------------------------ USING THE LIBRARY: Before calling the library, make and install it. Include shhmsg.h in every module that will make calls to the library. You probably also want to set the application name before using the output functions. See msgSetName, below. link with the library like so: cc foo.c -o foo -lshhmsg or, include it directly, if it's not in the system path: cc bar.c -o bar libshhmsg.a ------------------------------------------------------------------------------ A word about streams: This library uses three file streams. One is used for error messages, one for informational messages, and one for verbose messages. These normally default to stderr, stdout, and stderr, respectively. They can, however be easily changed, by calling the stream management functions, and set to any stream the caller likes, including stdout, stderr, opened pipes, open files, an open to /dev/console, whatever. The documents refer to them as the "error stream", the "message stream", and the "verbose stream". These names have nothing to do with what stream they may be actually pointing to. ------------------------------------------------------------------------------ Controlling the library: There is one initialization function, and several control functions. ------------------------------------------------------------------------------ FUNCTION: void msgSetName(const char *s); DESCRIPTION: Call msgSetName with the name of the application as the parameter. Note that argv[0] is a perfectly valid (perhaps encouraged) place to get this, and all preceding path information to the program name will be stripped off before it is displayed. SAMPLE: msgSetName(argv[0]); ========================== FUNCTION: void msgSetQuiet(int onoff); DESCRIPTION: This function sets an internal flag with whatever you pass in. It controls the behavior of msgMessage (see below). A non-zero will silence msgMessage, and a zero value will make it produce output. You can use the constants MSG_QUIET and MSG_OUTPUT, defined in the header file, if you want, or 0 or 1, or whatever. SAMPLE: msgSetQuiet(MSG_OFF); ========================== FUNCTION: void msgSetVerbose(int level); DESCRIPTION: This function controls what calls to the msgVerbose function occur. If the first parameter to that function is greater than the currently set level, the function produces output. If you call this function from code, the value of what you pass becomes the currently set level. If you pass MSG_VERBOSE_NONE, all calls to msgVerbose will produce no output, quieting your program a great deal. The default value is MSG_VERBOSE_DEFAULT. It has special behavior. If you do not call msgSetVerbose, or you call it with MSG_VERBOSE_DEFAULT, then the current verbosity level is read from an environmental variable. The variable is based on the name set with msgSetName. The program takes the first ten bytes, forces everything to upper case, and appends "_LEVEL" to it to make the name. You can set this variable (export it if you're using bash or sh) and then the program will produce that much output. Notice that this is a numeric value of -1, so all prompts will be suppressed if there is none set. Spaces in the name are NOT affected, and must be part of the environmental variable's name. NOTE: Because the special values used to control the extended behavior are negative numbers, the user of negative levels is not recommended. Levels from 0 to MAX_INT should be ok. SAMPLE: msgSetVerbose(2); -or- msgSetVerbose(MSG_VERBOSE_DEFAULT); ========================== FUNCTION: void msgSetShowNameAlways(int onoff) DESCRIPTION: Some of the functions in the library prepend the name of the application set with msgSetName to the strings they output. Some functions do this SOMETIMES. Calling this with a non-zero parameter will make ALL of the functions in the library do this. Calling it with a 0 will return the default behavior. When using this as a debug engine, having the name prepended is useful. The default value is "off". There are predefined constants, MSG_ON and MSG_OFF in the header file, if you want to use them. SAMPLE: msgSetShowNameAlways(MSG_ON); ========================== FUNCTION: FILE *msgSetErrorStream(FILE *f) DESCRIPTION: This function sets the file stream that the error functions (msgError, msgFatal, msgPerror, and msgFatalPerror) output to. The return value is the old stream that was in use (to be closed, if needed). No opening or closing of these streams is performed by the library. Passing in NULL will return the library to it's default value (stderr) and return you the steam currently in use. SAMPLE: FILE *fl; fl = fopen("/dev/console", "w"); msgSetErrorStream(fl); ... fclose(msgSetErrorStream(NULL)); ========================== FUNCTION: FILE *msgSetMessageStream(FILE *f); DESCRIPTION: This function controls the stream that msgMessage outputs to. It normally uses stdout, but this can be changed to anything you want. The library does no opening or closing of this stream, the application must do this. Passing in a NULL returns the library to the default of stdout, and returns the current stream in use. SAMPLE: FILE *fl; fl = fopen("output", "w"); msgSetMessageStream(fl); ... fclose(msgSetMessageStream(NULL)); ========================== FUNCTION: FILE *msgSetVerboseStream(FILE *f); DESCRIPTION: This function controls what stream the msgVerbose function outputs to. The default value is stderr. The library does no opening or closing of this file. Passing in a NULL restores the library to the default of stderr, and returns the value currently in use. SAMPLE: FILE *fl; fl = fopen("output", "w"); msgSetVerboseStream(fl); ... fclose(msgSetVerboseStream(NULL)); ------------------------------------------------------------------------------ Using the library to report errors: FUNCTION: void msgError(const char *format, ...) DESCRIPTION: Pass msgError printf-like arguments, and it will print the name of the program, followed by ": ", then your passed in arguments to whatever the error stream is set to. SAMPLE: msgError("you need to have more than %d things!\n", iThings); ========================== FUNCTION: void msgFatal(const char *fmt, ...) DESCRIPTION: This function, called with printf-like arguments, will print the name of the program, a ": ", then your output, to whatever the error stream is set to. It will then exit the running program, with an exit code of 99. SAMPLE: msgFatal("cannot open: %s\n", szFileName); ========================== FUNCTION: void msgPerror(const char *fmt, ...) DESCRIPTION: msgPerror, named for the C library call perror, will print out your passed in, printf-like arguments, then a ": ", then the textual error code represented by the system global errno. All output goes to the stream set as the current error stream. If errno represents "no error", then the function returns, doing nothing. The program name is not output, unless the msgSetShowNameAlways function has forced it to do so. SAMPLE: msgPerror("can't open %s", filename); ========================== FUNCTION: void msgFatalPerror(const char *fmt, ...) DESCRIPTION: Like msgPerror, this function accepts printf-like arguments, and displays those to the stream set as the error stream, followed by a ": ", then the textual error string that the global errno indicates. If errno is not reporting an error, the call returns, having done nothing. The call normally does not output the program name, but a call to msgSetShowNameAlways can force this. If an error is set, the function will not return, exiting with a return code of 99. SAMPLE: msgFatalPerror("can't continue: %s file missing!", filename); ========================== FUNCTION: void msgMessage(const char *format, ...) DESCRIPTION: msgMessage will take your output, format is as printf would, and output it to the message stream. If quiet mode has been set with msgSetQuiet, nothing is output. Normally, the program name is not output, but if msgSetShowNameAlways has been called, the name of the program, then a ": " will be prepended to the message. SAMPLE: msgMessage("something went wrong!\n"); ========================== FUNCTION: msgVerbose(int level, const char *fmt, ...) DESCRIPTION: msgVerbose accepts a "verbosity level", followed by printf-like arguments. If the current verbosity level is greater than or equal to the passed in value, the output will be formatted, and sent to the verbose stream. See above, in the msgSetVerbose description, for the different fixed values, and methods of getting them. The program name is not automatically output, unless msgSetShowNameAlways has been called with a non-zero parameter. Note that this can be quite useful to track program flow, if these are used liberally at function entry and exit points, with a high verbosity level. SAMPLE: msgVerbose(5, "entering the CalcFooBar function!\n"); shhmsg-1.4.2/streams.c000644 000765 000024 00000005532 07440225000 015635 0ustar00sverrehustaff000000 000000 /* $Id: streams.c,v 1.4 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE streams.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Routines for handling the stream that output goes to. | This allows using streams other than stderr and stdout. | | WRITTEN BY Sverre H. Huseby | This file is mainly contributed by | Louis W. Erickson +----------------------------------------------------------------------*/ #include #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgSetErrorStream | | FUNCTION Specify the file stream error values should go to. | | SYNOPSIS #include "shhmsg.h" | FILE *msgSetErrorStream(FILE *f); | | INPUT f pointer to a FILE that holds stream to write to. | | RETURNS Pointer to the old stream used for output. | | DESCRIPTION The stream passed is used to write all errors to. */ FILE * msgSetErrorStream(FILE *f) { FILE *ret = GET_ERROR_STREAM; _msgErrorStream = f ? f : MSG_DEFAULT_ERROR_STREAM; return ret; } /*------------------------------------------------------------------------ | | NAME msgSetVerboseStream | | FUNCTION Specify the file stream verbose output should go to. | | SYNOPSIS #include "shhmsg.h" | FILE *msgSetVerboseStream(FILE *f); | | INPUT f pointer to a FILE that holds stream to write to. | | RETURNS Pointer to the old stream used for output. | | DESCRIPTION The stream passed is used to write all verbose output to. */ FILE * msgSetVerboseStream(FILE *f) { FILE *ret = GET_VERBOSE_STREAM; _msgVerboseStream = f ? f : MSG_DEFAULT_VERBOSE_STREAM; return ret; } /*------------------------------------------------------------------------ | | NAME msgSetMessageStream | | FUNCTION Specify the file stream message output should go to. | | SYNOPSIS #include "shhmsg.h" | FILE *msgSetMessageStream(FILE *f); | | INPUT f pointer to a FILE that holds stream to write to. | | RETURNS Pointer to the old stream used for output. | | DESCRIPTION The stream passed is used to write all "messages" to. */ FILE * msgSetMessageStream(FILE *f) { FILE *ret = GET_MESSAGE_STREAM; _msgMessageStream = f ? f : MSG_DEFAULT_MESSAGE_STREAM; return ret; } shhmsg-1.4.2/vars.c000644 000765 000024 00000002017 07440225000 015125 0ustar00sverrehustaff000000 000000 /* $Id: vars.c,v 1.4 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE vars.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Variables common to most of the source files. These | are collected in a file of their own, to avoid linking | in functions not used. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC DATA | +-----------------------------------------------------------------------*/ /* Actually, these are not public, as they're supposed to be used * internally by this library only. */ FILE *_msgErrorStream = NULL; FILE *_msgVerboseStream = NULL; FILE *_msgMessageStream = NULL; shhmsg-1.4.2/verbose.c000644 000765 000024 00000010500 07440225000 015613 0ustar00sverrehustaff000000 000000 /* $Id: verbose.c,v 1.7 2002/03/02 19:37:36 sverrehu Exp $ */ /*------------------------------------------------------------------------ | FILE verbose.c | MODULE OF shhmsg - library for displaying messages. | | DESCRIPTION Routines for displaying text according to a verbosity | level. All messages with a level less than or equal | to the current level are displayed. | | WRITTEN BY Sverre H. Huseby +----------------------------------------------------------------------*/ #include #include #include #include #include #include "internal.h" #include "shhmsg.h" /*-----------------------------------------------------------------------+ | PUBLIC DATA | +-----------------------------------------------------------------------*/ int msgVerboseLevel = 0; /* current verbosity level */ /*-----------------------------------------------------------------------+ | PRIVATE FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgLookupVerboseLevel | | FUNCTION Get a default verbose level from the environment. | | RETURNS Default verbosity level. | | DESCRIPTION The environment variable "APPNAME_LEVEL" is checked | for a numeric value, and that becomes the verbosity | level, if none has been set in code, or the code | has not set "default". The APPNAME is truncated to | 10 bytes, and converted to all upper case. */ static int msgLookupVerboseLevel(void) { char envName[20]; char *envVal; strncpy(envName, msgGetName(), 10); envName[10] = '\0'; /* force the string to upper case */ for (envVal = envName; *envVal; envVal++) if (islower(*envVal)) *envVal = toupper(*envVal); strcat(envName, "_LEVEL"); envVal = getenv(envName); if (envVal == NULL || *envVal == '\0') return MSG_VERBOSE_DEFAULT; return atoi(envVal); } /*-----------------------------------------------------------------------+ | PUBLIC FUNCTIONS | +-----------------------------------------------------------------------*/ /*------------------------------------------------------------------------ | | NAME msgSetVerbose | | FUNCTION Set verbosity level. | | SYNOPSIS #include "shhmsg.h" | void msgSetVerbose(int level); | | INPUT level The new verbosity level. Note that a negative | value will make the system print nothing, assuming | all comparisons passed in by the caller to | msgVerbose are positive. A negative number will | revert the code to the "Default" state of checking | the environmental variable for it's verbosity level, | allowing changes at run time, instead of compile | time. | | DESCRIPTION Used in conjunction with the msgVerbose() function. | Note that changing the msgVerboseLevel-variable directly | is fully legal. */ void msgSetVerbose(int level) { msgVerboseLevel = level; } /*------------------------------------------------------------------------ | | NAME msgVerbose | | FUNCTION Show given message if allowed by the current verb. level. | | SYNOPSIS #include "shhmsg.h" | void msgVerbose(int level, const char *format, ...); | | INPUT level The verbosity level of the message to display. | format, ... | Arguments used as with printf(). | | DESCRIPTION The given message is displayed on the _msgVerboseStream if | it's level is less than or equal to the current verbosity | level. */ void msgVerbose(int level, const char *format, ...) { int lev; va_list ap; if ((lev = msgVerboseLevel) == MSG_VERBOSE_DEFAULT) lev = msgLookupVerboseLevel(); if (level > lev) return; fflush(stdout); if (_msgShowNameAlways) fprintf(GET_VERBOSE_STREAM, "%s: ", msgGetName()); va_start(ap, format); vfprintf(GET_VERBOSE_STREAM, format, ap); va_end(ap); }