From 908c671d6b3807854d41b4b51c584d16d7c2694b Mon Sep 17 00:00:00 2001 From: "graham.gower" Date: Mon, 2 Nov 2009 03:39:12 +0000 Subject: [PATCH] Cleanup error_list stuff a bit more. - Remove reverse_error_list. - Push messages on to the tail of the list. - Move the print function in with the other error list functions. - Indentation and variable name cleanups. git-svn-id: http://opkg.googlecode.com/svn/trunk@238 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358 --- libopkg/libopkg.c | 5 ++- libopkg/opkg.c | 2 - libopkg/opkg_cmd.c | 27 +----------- libopkg/opkg_cmd.h | 2 - libopkg/opkg_error.h | 2 - libopkg/opkg_utils.c | 100 ++++++++++++++++++++++--------------------- libopkg/opkg_utils.h | 6 +-- 7 files changed, 60 insertions(+), 84 deletions(-) diff --git a/libopkg/libopkg.c b/libopkg/libopkg.c index 418a324..787a510 100644 --- a/libopkg/libopkg.c +++ b/libopkg/libopkg.c @@ -36,7 +36,7 @@ int default_opkg_message_callback(opkg_conf_t *conf, message_level_t level, return 0; } else { if ( level == OPKG_ERROR ){ - push_error_list(&error_list, msg); + push_error_list(msg); } else printf("%s",msg); } @@ -131,7 +131,8 @@ opkg_op (int argc, char *argv[]) args_deinit (&args); if (err) { - opkg_print_error_list (&opkg_conf); + print_error_list(); + free_error_list(); return err; } diff --git a/libopkg/opkg.c b/libopkg/opkg.c index 5ff0eb5..d6b6658 100644 --- a/libopkg/opkg.c +++ b/libopkg/opkg.c @@ -34,8 +34,6 @@ #include -struct errlist* error_list; - struct _opkg_t { args_t *args; diff --git a/libopkg/opkg_cmd.c b/libopkg/opkg_cmd.c index e7038c5..99ee64d 100644 --- a/libopkg/opkg_cmd.c +++ b/libopkg/opkg_cmd.c @@ -141,26 +141,6 @@ opkg_cmd_t *opkg_cmd_find(const char *name) return NULL; } -void opkg_print_error_list (opkg_conf_t *conf) -{ - struct errlist *err = error_list; - - if (err) { - reverse_error_list(&err); - - printf ("Collected errors:\n"); - /* Here we print the errors collected and free the list */ - while (err != NULL) { - printf (" * %s", err->errmsg); - err = err->next; - - } - - free_error_list(&error_list); - } - -} - int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata) { int result; @@ -169,11 +149,8 @@ int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **arg result = (cmd->fun)(conf, argc, argv); - if ( result != 0 && !error_list) { - opkg_message(conf, OPKG_NOTICE, "An error occurred, return value: %d.\n", result); - } - - opkg_print_error_list (conf); + print_error_list(); + free_error_list(); p_userdata = NULL; return result; diff --git a/libopkg/opkg_cmd.h b/libopkg/opkg_cmd.h index c597555..f512488 100644 --- a/libopkg/opkg_cmd.h +++ b/libopkg/opkg_cmd.h @@ -31,6 +31,4 @@ typedef struct opkg_cmd opkg_cmd_t; opkg_cmd_t *opkg_cmd_find(const char *name); int opkg_cmd_exec(opkg_cmd_t *cmd, opkg_conf_t *conf, int argc, const char **argv, void *userdata); -void opkg_print_error_list (opkg_conf_t *conf); - #endif diff --git a/libopkg/opkg_error.h b/libopkg/opkg_error.h index 43a5507..ec0d1c2 100644 --- a/libopkg/opkg_error.h +++ b/libopkg/opkg_error.h @@ -52,6 +52,4 @@ struct errlist { struct errlist * next; } ; -extern struct errlist* error_list; - #endif /* OPKG_ERROR_H */ diff --git a/libopkg/opkg_utils.c b/libopkg/opkg_utils.c index f0ef051..92291c4 100644 --- a/libopkg/opkg_utils.c +++ b/libopkg/opkg_utils.c @@ -146,60 +146,64 @@ int line_is_blank(const char *line) return 1; } +static struct errlist *error_list_head, *error_list_tail; + /* * XXX: this function should not allocate memory as it may be called to * print an error because we are out of memory. */ -void push_error_list(struct errlist ** errors, char * msg){ - struct errlist *err_lst_tmp; - - err_lst_tmp = calloc (1, sizeof (struct errlist) ); - if (err_lst_tmp == NULL) { - fprintf(stderr, "%s: calloc: %s\n", __FUNCTION__, strerror(errno)); - return; - } - - err_lst_tmp->errmsg = strdup(msg); - if (err_lst_tmp->errmsg == NULL) { - fprintf(stderr, "%s: strdup: %s\n", __FUNCTION__, strerror(errno)); - free(err_lst_tmp); - return; - } - - err_lst_tmp->next = *errors; - *errors = err_lst_tmp; +void push_error_list(char * msg) +{ + struct errlist *e; + + e = calloc(1, sizeof(struct errlist)); + if (e == NULL) { + fprintf(stderr, "%s: calloc: %s\n", + __FUNCTION__, strerror(errno)); + return; + } + + e->errmsg = strdup(msg); + if (e->errmsg == NULL) { + fprintf(stderr, "%s: strdup: %s\n", + __FUNCTION__, strerror(errno)); + free(e); + return; + } + + e->next = NULL; + + if (error_list_head) { + error_list_tail->next = e; + error_list_tail = e; + } else { + error_list_head = error_list_tail = e; + } } - -void reverse_error_list(struct errlist **errors){ - struct errlist *result=NULL; - struct errlist *current= *errors; - struct errlist *next; - - while ( current != NULL ) { - next = current->next; - current->next=result; - result=current; - current=next; - } - *errors=result; - +void free_error_list(void) +{ + struct errlist *err, *err_tmp; + + err = error_list_head; + while (err != NULL) { + free(err->errmsg); + err_tmp = err; + err = err->next; + free(err_tmp); + } } - -void free_error_list(struct errlist **errors){ -struct errlist *err_tmp_lst; - - err_tmp_lst = *errors; - - while (err_tmp_lst != NULL) { - free(err_tmp_lst->errmsg); - err_tmp_lst = error_list->next; - free(*errors); - *errors = err_tmp_lst; - } - - +void print_error_list (void) +{ + struct errlist *err = error_list_head; + + if (err) { + printf ("Collected errors:\n"); + /* Here we print the errors collected and free the list */ + while (err != NULL) { + printf (" * %s", err->errmsg); + err = err->next; + } + } } - - diff --git a/libopkg/opkg_utils.h b/libopkg/opkg_utils.h index ed9215b..a6c9eb0 100644 --- a/libopkg/opkg_utils.h +++ b/libopkg/opkg_utils.h @@ -21,9 +21,9 @@ #include "pkg.h" #include "opkg_error.h" -void push_error_list(struct errlist **errors,char * msg); -void reverse_error_list(struct errlist **errors); -void free_error_list(struct errlist **errors); +void push_error_list(char * msg); +void free_error_list(void); +void print_error_list(void); long unsigned int get_available_blocks(char * filesystem); char **read_raw_pkgs_from_file(const char *file_name); -- 2.25.1