⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standard.shar

📁 The art and science of c_source code!
💻 SHAR
📖 第 1 页 / 共 5 页
字号:
#! /bin/sh# This is a shell archive.  Remove anything before this line, then unpack# it by saving it into a file and typing "sh file".  To overwrite existing# files, type "sh file -c".  You can also feed this as standard input via# unshar, or by typing "sh <file", e.g..  If this archive is complete, you# will see the following message at the end:#		"End of shell archive."# Contents:  cslib cslib/Makefile cslib/exception.c cslib/exception.h#   cslib/gcalloc.h cslib/genlib.c cslib/genlib.h cslib/graphics.c#   cslib/graphics.h cslib/random.c cslib/random.h cslib/simpio.c#   cslib/simpio.h cslib/strlib.c cslib/strlib.h# Wrapped by eroberts@Eeyore.Stanford.EDU on Fri Feb 24 19:44:56 1995PATH=/bin:/usr/bin:/usr/ucb ; export PATHif test ! -d 'cslib' ; then    echo shar: Creating directory \"'cslib'\"    mkdir 'cslib'fiif test -f 'cslib/Makefile' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/Makefile'\"elseecho shar: Extracting \"'cslib/Makefile'\" \(2146 characters\)sed "s/^X//" >'cslib/Makefile' <<'END_OF_FILE'X# Makefile for cslib/standard directoryX# Last modified on Thu Oct 20 13:49:04 1994 by erobertsX#****************************************************************XXOBJECTS = \X    genlib.o \X    exception.o \X    strlib.o \X    simpio.o \X    random.o \X    graphics.oXXCSLIB = cslib.aXXCC = gccXCFLAGS = -g -I. $(CCFLAGS)XX# ***************************************************************X# Entry to bring the package up to dateX#    The "make all" entry should be the first real entryXXall: $(CSLIB) gccxXX# ***************************************************************X# Standard entries to remove files from the directoriesX#    tidy    -- eliminate unwanted filesX#    clean   -- delete derived files in preparation for rebuildX#    scratch -- synonym for cleanXXtidy:X	rm -f ,* .,* *~ core a.out *.errXXclean scratch: tidyX	rm -f *.o *.a gccxXX# ***************************************************************X# C compilationsXXgenlib.o: genlib.c genlib.h exception.h gcalloc.hX	$(CC) $(CFLAGS) -c genlib.cXXexception.o: exception.c exception.h genlib.hX	$(CC) $(CFLAGS) -c exception.cXXstrlib.o: strlib.c strlib.h genlib.hX	$(CC) $(CFLAGS) -c strlib.cXXsimpio.o: simpio.c simpio.h strlib.h genlib.hX	$(CC) $(CFLAGS) -c simpio.cXXgraphics.o: graphics.c graphics.h genlib.hX	$(CC) $(CFLAGS) -c graphics.cXXrandom.o: random.c random.h genlib.hX	$(CC) $(CFLAGS) -c random.cXX# ***************************************************************X# Entry to reconstruct the library archiveXX$(CSLIB): $(OBJECTS)X	-rm -f $(CSLIB)X	ar cr $(CSLIB) $(OBJECTS)X	ranlib $(CSLIB)XX# ***************************************************************X# Entry to reconstruct the gccx scriptXXgccx: MakefileX	@echo '#! /bin/csh -f' > gccxX	@echo 'set INCLUDE =' `pwd` >> gccxX	@echo 'set CSLIB = $$INCLUDE/cslib.a' >> gccxX	@echo 'set LIBRARIES = ($$CSLIB -lm)' >> gccxX	@echo 'foreach x ($$*)' >> gccxX	@echo '  if ("x$$x" == "x-c") then' >> gccxX	@echo '    set LIBRARIES = ""' >> gccxX	@echo '    break' >> gccxX	@echo '  endif' >> gccxX	@echo 'end' >> gccxX	@echo 'gcc -g -I$$INCLUDE $$* $$LIBRARIES' >> gccxX	@chmod a+x gccxX	@echo '[gccx script created]'END_OF_FILEif test 2146 -ne `wc -c <'cslib/Makefile'`; then    echo shar: \"'cslib/Makefile'\" unpacked with wrong size!fi# end of 'cslib/Makefile'fiif test -f 'cslib/exception.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/exception.c'\"elseecho shar: Extracting \"'cslib/exception.c'\" \(3276 characters\)sed "s/^X//" >'cslib/exception.c' <<'END_OF_FILE'X/*X * File: exception.cX * Version: 1.0X * Last modified on Sun Jul 24 10:28:11 1994 by erobertsX * -----------------------------------------------------X * This file implements the C exception handler.  Much of theX * real work is done in the exception.h header file.X */XX#include <stdio.h>X#include <stdarg.h>XX#include "genlib.h"X#include "gcalloc.h"X#include "exception.h"XX/*X * Constant: MaxUnhandledMessageX * -----------------------------X * This constant should be large enough to accommodate theX * unhandled exception message, including the exception name.X */XX#define MaxUnhandledMessage 100XX/* Publically accessible exceptions */XXexception ANY = { "ANY" };Xexception ErrorException = { "ErrorException" };XX/*X * Global variable: exceptionStackX * -------------------------------X * This variable is the head pointer to a linked list ofX * context blocks that act as the exception stack.  The chainX * pointer is referenced by the macros in exception.h and mustX * therefore be exported, but clients should not reference itX * directly.X */XXcontext_block *exceptionStack = NULL;XX/* Private function prototypes */XXstatic context_block *FindHandler(exception *e);XX/* Public entries */XX/*X * Function: RaiseExceptionX * ------------------------X * This function operates by finding an appropriate handlerX * and then using longjmp to return to the context storedX * there after resetting the exception stack.  If no handlerX * exists, the function notes an unhandled exception.  MuchX * of the complexity comes from the fact that allocationX * within the exception handler may fail.X */XXvoid RaiseException(exception *e, string name, void *value)X{X    context_block *cb;X    char errbuf[MaxUnhandledMessage + 1];X    string errmsg;X    int errlen;XX    cb = FindHandler(e);X    if (cb == NULL) {X        sprintf(errbuf, "Unhandled exception (%.30s)", name);X        errlen = strlen(errbuf);X        if (_acb == NULL) {X            errmsg = malloc(errlen + 1);X        } else {X            errmsg = _acb->allocMethod(errlen + 1);X        }X        if (errmsg == NULL) {X            errmsg = "Unhandled exception: unknown";X        } else {X            strcpy(errmsg, errbuf);X        }X        Error(errmsg);X    }X    exceptionStack = cb;X    cb->id = e;X    cb->value = value;X    cb->name = name;X    longjmp(cb->jmp, ES_Exception);X}XX/*X * Function: HandlerExistsX * -----------------------X * This public entry is used primarily by the Error functionX * to determine if ErrorException has been trapped, althoughX * it is available to other clients as well.X */XXbool HandlerExists(exception *e)X{X    return (FindHandler(e) != NULL);X}XX/* Private functions */XX/*X * Function: FindHandlerX * ---------------------X * This function searches the exception stack to find theX * first active handler for the indicated exception.  If aX * match is found, the context block pointer is returned.X * If not, FindHandler returns NULL.X */XXstatic context_block *FindHandler(exception *e)X{X    context_block *cb;X    exception *t;X    int i;XX    for (cb = exceptionStack; cb != NULL; cb = cb->link) {X        for (i = 0; i < cb->nx; i++) {X            t = cb->array[i];X            if (t == e || t == &ANY) return (cb);X        }X    }X    return (NULL);X}END_OF_FILEif test 3276 -ne `wc -c <'cslib/exception.c'`; then    echo shar: \"'cslib/exception.c'\" unpacked with wrong size!fi# end of 'cslib/exception.c'fiif test -f 'cslib/exception.h' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/exception.h'\"elseecho shar: Extracting \"'cslib/exception.h'\" \(7974 characters\)sed "s/^X//" >'cslib/exception.h' <<'END_OF_FILE'X/*X * File: exception.hX * Version: 1.0X * Last modified on Sat Nov 19 16:36:26 1994 by erobertsX * -----------------------------------------------------X * The exception package provides a general exceptionX * handling mechanism for use with C that is portableX * across a variety of compilers and operating systems.X */XX#ifndef _exception_hX#define _exception_hXX/*X * Overview:X * --------X * The exception package makes it possible for clients toX * specify a handler for an exceptional conditions in aX * syntactically readable way.  As a client, your first stepX * is to declare an exception condition name by declaringX * a variable of type exception, as inX *X *       exception MyException;X *X * Normal visibility rules apply, so that you should declareX * the exception variable at the appropriate level.  ForX * example, if an exception is local to an implementation,X * it should be declared statically within that module.  IfX * an exception condition is shared by many modules, theX * exception variable should be declared in an interfaceX * and exported to all clients that need it.  This packageX * defines and exports the exception ErrorException, whichX * is likely to be sufficient for many clients.X *X * The basic functionality of exceptions is that one pieceX * of code can "raise" an exception so that it can then beX * "handled" by special code in a dynamically enclosingX * section of the program.  Exceptions are raised by callingX * the pseudo-function raise with the exception name, as inX *X *     raise(MyException);X *X * Exceptions are handled using the "try" statementX * (actually implemented using macros), which has the form:X *X *     try {X *        . . . statements in the body of the block . . .X *     except(exception1)X *        . . . statements to handle exception 1 . . .X *     except(exception2)X *        . . . statements to handle exception 2 . . .X *     except(ANY)X *        . . . statements to handle any exception . . .X *     } endtryX *X * Any number of except clauses may appear (up to aX * maximum defined by the constant MaxExceptionsPerScope),X * and the ANY clause is optional.X *X * When the program encounters the "try" statement, theX * statements in the body are executed.  If no exceptionX * conditions are raised during that execution, eitherX * in this block or by a function call nested insideX * this block, control passes to the end of the "try"X * statement when the last statement in the block isX * executed.  If an exception is raised during theX * dynamic execution of the block, control immediatelyX * passes to the statements in the appropriate exceptX * clause.  Only the statements in that clause areX * executed; no break statement is required to exitX * the block.  If no handler for the raised exceptionX * appears anywhere in the control history, the programX * exits with an error.X *X * Examples of use:X *X * 1.  Catching errors.X *X * The following code fragment traps calls to Error, soX * that the program does not quit but instead returnsX * to the top-level read-and-execute loop.X *X *     while (TRUE) {X *         try {X *             printf("> ");X *             cmd = ReadCommand();X *             ExecuteCommand(cmd);X *         except(ErrorException)X *             printf("Error: %s\n", (string) GetExceptionValue());X *             -- additional handling code, if any --X *         } endtryX *     }X *X * If either ReadCommand or ExecuteCommand calls Error,X * control will be passed back to the main loop, afterX * executing any additional handler code.  The errorX * message is passed as the exception value and can beX * printed as shown in the example.X *X * 2.  Handling control-CX *X * The following code extends the example above so thatX * typing ^C also returns to top-level.X *X *     #include <signal.h>X *X *     static exception ControlCException;X *     static int errorCount = 0;X *     static int ControlCHandler();X *X *     main()X *     {X *         string cmd;X *X *         signal(SIGINT, ControlCHandler);X *         while (TRUE) {X *             try {X *                 printf("> ");X *                 cmd = ReadCommand();X *                 ExecuteCommand(cmd);X *             except(ControlCException);X *                 printf("^C\n");X *                 signal(SIGINT, ControlCHandler);X *             except(ErrorException)X *                 errorCount++;X *             } endtryX *         }X *     }X *X *     static int ControlCHandler()X *     {X *         raise(ControlCException);X *     }X */XX/*X * Actual interface specificationX * ------------------------------X * Most of the implementation of the exception mechanism isX * actually done in the macros defined by this file.X * Clients should ordinarily be able to read the descriptionX * above and ignore the detailed code below.X */XX#include <setjmp.h>X#include <string.h>X#include "genlib.h"XX/* Define parameters and error status indicators */XX#define MaxExceptionsPerScope 10X#define ETooManyExceptClauses 101X#define EUnhandledException 102XX/* Codes to keep track of the state of the try handler */XX#define ES_Initialize 0X#define ES_EvalBody 1X#define ES_Exception 2XX/*X * Type: exceptionX * ---------------X * Exceptions are specified by their address, so that theX * actual structure does not matter.  Strings are used hereX * so that exporters of exceptions can store the exceptionX * name for the use of debuggers and other tools.X */XXtypedef struct { string name; } exception;XX/*X * Type: context_blockX * -------------------X * This structure is used internally to maintain a chain ofX * exception scopes on the control stack.X */XXtypedef struct ctx_block {X    jmp_buf jmp;X    int nx;X    exception *array[MaxExceptionsPerScope];X    exception *id;X    void *value;X    string name;X    struct ctx_block *link;X} context_block;XX/* Declare the built-in exceptions */XXextern exception ErrorException;Xextern exception ANY;XX/* Declare a global pointer to the context stack */XXextern context_block *exceptionStack;XX/*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -