From 11a23e22fd9b87344dd1e2b37ce6898b8225d5b3 Mon Sep 17 00:00:00 2001 From: Heikki Levanto Date: Tue, 11 Feb 2003 14:01:39 +0000 Subject: [PATCH] Minor cleaning, removed a crash from error reporting routines if not initialized correctly, and tests for those --- index/zebraapi.c | 14 +++++++--- index/zebraapi.h | 75 ++++++++++++++++++++++++++++++++++++++-------------- index/zebrash.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 139 insertions(+), 27 deletions(-) diff --git a/index/zebraapi.c b/index/zebraapi.c index 4529294..711dbc5 100644 --- a/index/zebraapi.c +++ b/index/zebraapi.c @@ -1,4 +1,4 @@ -/* $Id: zebraapi.c,v 1.81 2003-01-15 07:26:40 oleg Exp $ +/* $Id: zebraapi.c,v 1.82 2003-02-11 14:01:39 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -834,17 +834,23 @@ int zebra_deleleResultSet(ZebraHandle zh, int function, int zebra_errCode (ZebraHandle zh) { - return zh->errCode; + if (zh) + return zh->errCode; + return 0; /* is this the right thing to return ?*/ } const char *zebra_errString (ZebraHandle zh) { - return diagbib1_str (zh->errCode); + if (zh) + return diagbib1_str (zh->errCode); + return ""; } char *zebra_errAdd (ZebraHandle zh) { - return zh->errString; + if (zh) + return zh->errString; + return ""; } int zebra_auth (ZebraHandle zh, const char *user, const char *pass) diff --git a/index/zebraapi.h b/index/zebraapi.h index f34c52a..0768c12 100644 --- a/index/zebraapi.h +++ b/index/zebraapi.h @@ -1,4 +1,4 @@ -/* $Id: zebraapi.h,v 1.23 2003-01-15 07:26:40 oleg Exp $ +/* $Id: zebraapi.h,v 1.24 2003-02-11 14:01:39 heikki Exp $ Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002 Index Data Aps @@ -74,9 +74,45 @@ typedef struct { typedef struct zebra_session *ZebraHandle; typedef struct zebra_service *ZebraService; -/* Open Zebra using file 'configName' (usually zebra.cfg) */ + +/****** + * Starting and stopping + */ + +/* Start Zebra using file 'configName' (usually zebra.cfg) */ +/* There should be exactly one ZebraService */ +YAZ_EXPORT ZebraService zebra_start (const char *configName); + +/* Close the whole Zebra */ +YAZ_EXPORT void zebra_stop (ZebraService zs); + + +/* Open a ZebraHandle */ +/* There should be one handle for each thred doing something */ +/* with zebra, be that searching or indexing. In simple apps */ +/* one handle is sufficient */ YAZ_EXPORT ZebraHandle zebra_open (ZebraService zs); +/* Close handle */ +YAZ_EXPORT void zebra_close (ZebraHandle zh); + +/********* + * Error handling + */ + +/* last error code */ +YAZ_EXPORT int zebra_errCode (ZebraHandle zh); + +/* string representatio of above */ +YAZ_EXPORT const char *zebra_errString (ZebraHandle zh); + +/* extra information associated with error */ +YAZ_EXPORT char *zebra_errAdd (ZebraHandle zh); + +/************** + * Searching + */ + /* Search using RPN-Query */ YAZ_EXPORT void zebra_search_rpn (ZebraHandle zh, ODR input, ODR output, Z_RPNQuery *query, @@ -88,6 +124,12 @@ YAZ_EXPORT void zebra_records_retrieve (ZebraHandle zh, ODR stream, oid_value input_format, int num_recs, ZebraRetrievalRecord *recs); +/* Delete Result Set(s) */ +YAZ_EXPORT int zebra_deleleResultSet(ZebraHandle zh, int function, + int num_setnames, char **setnames, + int *statuses); + + /* Browse */ YAZ_EXPORT void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, @@ -95,23 +137,13 @@ YAZ_EXPORT void zebra_scan (ZebraHandle zh, ODR stream, int *position, int *num_entries, ZebraScanEntry **list, int *is_partial); - -/* Delete Result Set(s) */ -YAZ_EXPORT int zebra_deleleResultSet(ZebraHandle zh, int function, - int num_setnames, char **setnames, - int *statuses); - -/* Close zebra and destroy handle */ -YAZ_EXPORT void zebra_close (ZebraHandle zh); - -/* last error code */ -YAZ_EXPORT int zebra_errCode (ZebraHandle zh); -/* string representatio of above */ -YAZ_EXPORT const char *zebra_errString (ZebraHandle zh); - -/* extra information associated with error */ -YAZ_EXPORT char *zebra_errAdd (ZebraHandle zh); + + +/********* + * Other + */ + /* do authentication */ YAZ_EXPORT int zebra_auth (ZebraHandle zh, const char *user, const char *pass); @@ -121,10 +153,13 @@ YAZ_EXPORT int zebra_string_norm (ZebraHandle zh, unsigned reg_id, const char *input_str, int input_len, char *output_str, int output_len); + +/****** + * Admin + */ + YAZ_EXPORT void zebra_admin_create (ZebraHandle zh, const char *db); -YAZ_EXPORT ZebraService zebra_start (const char *configName); -YAZ_EXPORT void zebra_stop (ZebraService zs); YAZ_EXPORT void zebra_admin_shutdown (ZebraHandle zh); YAZ_EXPORT void zebra_admin_start (ZebraHandle zh); diff --git a/index/zebrash.c b/index/zebrash.c index 37dbdb1..1b1f355 100644 --- a/index/zebrash.c +++ b/index/zebrash.c @@ -7,7 +7,7 @@ #include #include -#include +#include #include #if HAVE_READLINE_READLINE_H @@ -212,6 +212,44 @@ static int cmd_logf( char *args[], char *outbuff) return 0; /* ok */ } +/**************** + * Error handling + */ +static int cmd_err ( char *args[], char *outbuff) +{ + char tmp[MAX_OUT_BUFF]; + sprintf(tmp, "errCode: %d \nerrStr: %s\nerrAdd: %s \n", + zebra_errCode (zh), + zebra_errString (zh), + zebra_errAdd (zh) ); + strcat(outbuff, tmp); + return 0; /* ok */ +} +static int cmd_errcode ( char *args[], char *outbuff) +{ + char tmp[MAX_OUT_BUFF]; + sprintf(tmp, "errCode: %d ", + zebra_errCode (zh)); + strcat(outbuff, tmp); + return 0; /* ok */ +} +static int cmd_errstr ( char *args[], char *outbuff) +{ + char tmp[MAX_OUT_BUFF]; + sprintf(tmp, "errStr: %s", + zebra_errString (zh)); + strcat(outbuff, tmp); + return 0; /* ok */ +} +static int cmd_erradd ( char *args[], char *outbuff) +{ + char tmp[MAX_OUT_BUFF]; + sprintf(tmp, "errAdd: %s \n", + zebra_errAdd (zh) ); + strcat(outbuff, tmp); + return 0; /* ok */ +} + /************************************** * Command table, parser, and help @@ -268,6 +306,20 @@ struct cmdstruct cmds[] = { "[level] text...", "writes an entry in the log", cmd_logf}, + + { "", "Error handling:","", 0}, + { "err", "", + "Displays zebra's error status (code, str, add)", + cmd_err}, + { "errcode", "", + "Displays zebra's error code", + cmd_errcode}, + { "errstr", "", + "Displays zebra's error string", + cmd_errstr}, + { "erradd", "", + "Displays zebra's additional error message", + cmd_erradd}, { "", "Misc:","", 0}, { "echo", "string", @@ -290,6 +342,7 @@ int onecommand( char *line, char *outbuff) char *args[MAX_NO_ARGS]; int n; char argbuf[MAX_ARG_LEN]; + int rc; strncpy(argbuf,line, MAX_ARG_LEN-1); argbuf[MAX_ARG_LEN-1]='\0'; /* just to be sure */ n=split_args(argbuf, args); @@ -360,9 +413,26 @@ int onecommand( char *line, char *outbuff) strcat(outbuff,tmp); } return 0; - } - +} +/* If Zebra reports an error after an operation, + * append it to the outbuff */ +static void Zerrors ( char *outbuff) +{ + int ec; + char tmp[MAX_OUT_BUFF]; + if (!zh) + return ; + ec=zebra_errCode (zh); + if (ec) + { + sprintf(tmp, "Zebra error %d: %s, (%s) \n", + ec, zebra_errString (zh), + zebra_errAdd (zh) ); + strcat(outbuff, tmp); + } +} + /************************************** * The shell */ @@ -397,6 +467,7 @@ void shell() #endif outbuff[0]='\0'; rc=onecommand(buf, outbuff); + Zerrors(outbuff); printf("%s\n", outbuff); } -- 1.7.10.4