From: Erik M. <er...@us...> - 2001-10-03 16:02:46
|
Update of /cvsroot/blob/blob/src In directory usw-pr-cvs1:/tmp/cvs-serv4842/src Modified Files: error.c Makefile.am Log Message: Generalised error handling functions Index: error.c =================================================================== RCS file: /cvsroot/blob/blob/src/error.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- error.c 2001/10/03 16:01:25 1.1 +++ error.c 2001/10/03 16:02:43 1.2 @@ -0,0 +1,95 @@ +/* + * errno.c: error handling functions + * + * Copyright (C) 2001 Erik Mouw (J.A...@it...) + * + * $Id$ + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ident "$Id$" + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "errno.h" +#include "error.h" +#include "serial.h" +#include "types.h" + + +typedef struct { + int errno; + char *string; +} error_t; + + +static error_t errors[] = { + { ENOERROR, "no error" }, + { EINVAL, "invalid argument" }, + { ENOPARAMS, "not enough parameters" }, + { EMAGIC, "magic value failed" }, + { ECOMMAND, "invalid command" }, + { ENAN, "not a number" }, + { EALIGN, "alignment error" }, + { ERANGE, "out of range" }, + { ETIMEOUT, "timeout exceeded" }, + { ETOOSHORT, "short file" }, + { ETOOLONG, "long file" }, +}; + + +static char *unknown = "unknown error"; +static char *errprefix = "*** "; + + +char *strerror(int errnum) +{ + int i; + int num = sizeof(errors) / sizeof(error_t); + + /* make positive if it is negative */ + if(errnum < 0) + errnum = -errnum; + + for(i = 0; i < num; i++) + if(errors[i].errno == errnum) + return errors[i].string; + + return unknown; +} + + +void printerrprefix(void) +{ + SerialOutputString(errprefix); +} + + +void printerror(int errnum, char *s) +{ + printerrprefix(); + SerialOutputString(strerror(errnum)); + + if(s != NULL) { + SerialOutputString(": "); + SerialOutputString(s); + } + + SerialOutputByte('\n'); +} Index: Makefile.am =================================================================== RCS file: /cvsroot/blob/blob/src/Makefile.am,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- Makefile.am 2001/10/02 21:39:54 1.7 +++ Makefile.am 2001/10/03 16:02:43 1.8 @@ -59,6 +59,7 @@ flashasm.S \ testmem2.S \ command.c \ + error.c \ flash.c \ init.c \ led.c \ |