1 /* -------------------------------------------------------------------- */
2 /* 									*/
3 /*									*/
4 /*									*/
5 /* Copyright (C) 2000 Angelo Masci					*/
6 /*       								*/
7 /* This program is free software; you can redistribute it and/or modify	*/
8 /* it under the terms of the GNU General Public License as published by	*/
9 /* the Free Software Foundation; either version 2 of the License, or	*/
10 /* (at your option) any later version.					*/
11 /*									*/
12 /* This program is distributed in the hope that it will be useful,	*/
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of	*/
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the	*/
15 /* GNU General Public License for more details.				*/
16 /*									*/
17 /* You should have received a copy of the GNU General Public License	*/
18 /* along with this program; if not, write to the Free Software		*/
19 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.		*/
20 /* 									*/
21 /*  You can contact the author at this e-mail address:			*/
22 /*									*/
23 /*  angelo@styx.demon.co.uk						*/
24 /*									*/
25 /* --------------------------------------------------------------------
26    $Id$
27    -------------------------------------------------------------------- */
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 
34 #include "format.h"
35 #include "status.h"
36 #include "common.h"
37 
38 /* -------------------------------------------------------------------- */
39 
40 static STATUS emalloc =
41 {
42 	"malloc() failed\n",
43 	NULL
44 };
45 
46 static STATUS estatus =
47 {
48 	"status_stack_message() failed\n",
49 	&emalloc
50 };
51 
52 /* -------------------------------------------------------------------- */
53 /* -------------------------------------------------------------------- */
54 
55 static STATUS *status_stack_message(STATUS *status, char *msg);
56 
57 /* -------------------------------------------------------------------- */
58 /* -------------------------------------------------------------------- */
status_stack_message(STATUS * status,char * msg)59 static STATUS *status_stack_message(STATUS *status, char *msg)
60 {
61 	STATUS *item;
62 
63 	item = (STATUS *)malloc(sizeof(STATUS));
64 	if (item == NULL)
65 	{	return &estatus;
66 	}
67 
68 	item->msg  = msg;
69 	item->next = status;
70 
71 	return item;
72 }
73 
74 /* -------------------------------------------------------------------- */
75 /* -------------------------------------------------------------------- */
status_stack_errno(STATUS * status,int error)76 STATUS *status_stack_errno(STATUS *status, int error)
77 {
78 	return status_stack(status, "%s\n", strerror(error));
79 }
80 
81 /* -------------------------------------------------------------------- */
82 /* -------------------------------------------------------------------- */
status_stack(STATUS * status,char * fmt,...)83 STATUS *status_stack(STATUS *status, char *fmt, ...)
84 {
85 	va_list args;
86 	char	*buf;
87 
88 
89 	va_start(args, fmt);
90 	libcommon_vasprintf(&buf, fmt, args);
91 	va_end(args);
92 
93 	return status_stack_message(status, buf);
94 }
95 
96 /* -------------------------------------------------------------------- */
97 /* -------------------------------------------------------------------- */
status_output(STATUS * status,void (* status_output_message)(char *))98 void status_output(STATUS *status, void (*status_output_message)(char *))
99 {
100 	STATUS *item;
101 
102 	item = status;
103 	while (item != NULL)
104 	{
105 		(*status_output_message)(item->msg);
106 		item = item->next;
107 	}
108 }
109 
110 /* -------------------------------------------------------------------- */
111 /* -------------------------------------------------------------------- */
status_free(STATUS * status)112 void status_free(STATUS *status)
113 {
114 	if (status->next != NULL)
115 	{	status_free(status->next);
116 	}
117 
118 	free(status->msg);
119 	free(status);
120 }
121 
122 /* -------------------------------------------------------------------- */
123 /* -------------------------------------------------------------------- */
status_default_output(char * str)124 void status_default_output(char *str)
125 {
126 	libcommon_fprintf(stderr, str);
127 }
128 
129 
130