File: libyahoo-debug.c

package info (click to toggle)
libyahoo 0.18.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 900 kB
  • ctags: 361
  • sloc: sh: 6,708; ansic: 4,227; makefile: 84; perl: 53
file content (249 lines) | stat: -rw-r--r-- 4,149 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Debugging library routines
 */
#if defined(ENABLE_LIBYAHOO_DEBUG)

#include "config.h"
#include "libyahoo-debug.h"

#include <stdlib.h>
#include <stdio.h>
#if defined(HAVE_STDARG_H)
#include <stdarg.h>
#endif
#include <ctype.h>

#if defined(HAVE_STRING_H)
#include <string.h>
#elif defined(HAVE_STRINGS_H)
#include <strings.h>
#endif

static FILE *debugfile = NULL;
static char *yahoo_dbg_EnabledKeys[128] = { NULL };	/* should be more than enough */
static int yahoo_dbg_MaxEnabledKey = -1;

char *yahoo_unraw_buffer(char *buffer, unsigned int len)
{
	char *un_raw = NULL, *q = un_raw, *p = buffer;
	unsigned int i = 0, n = 1;

	if (!(q = un_raw = malloc(4 * len)))
	  return NULL;

	for (i = 0; i < len; ++i)
	{
		if (isprint((int) *p))
		{
			if ('\\' == *p)
			{
				*q++ = *p++;
				*q++ = *p;
				n += 2;
			}
			else
			{
				*q++ = *p++;
				++n;
			}
		}
		else
		{
			unsigned char c = *p;

			snprintf(q, 5, "\\%03o", c);
			++p;
			q += 4;
			n += 4;
		}
	}

	*q = '\0';

	un_raw = realloc (un_raw, n + 1);

	return un_raw;
}

/* these routines currently do a really inneficient linear search, 
   but it's not like they need to be really fast - they are just for
   debugging purposes */

int yahoo_dbg_Open(const char *file)
{
	if (debugfile && debugfile != stdout)
	{
		fclose(debugfile);
		debugfile = NULL;
	}

	if (!file)
	{
		fprintf(stderr,
			"yahoo_dbg_Open Error: File is null, using stdout.\n");
		debugfile = stdout;
		return 1;
	}

	if ((debugfile = fopen(file, "a")) == NULL)
	{
		fprintf(stderr, "yahoo_dbg_Open Error: Couldn't open output file.\n");
		fprintf(stderr, "                Falling back to stdout.\n");
		debugfile = stdout;
		return 1;
	}

	return 0;
}

int yahoo_dbg_Disable(const char *key)
{
	int i;

	if (!key)
	{
		fprintf(stderr, "yahoo_dbg_Disable Error: key is null.\n");
		return 1;
	}
	for (i = 0; i <= yahoo_dbg_MaxEnabledKey; i++)
	{
		if (yahoo_dbg_EnabledKeys[i]
			&& !strcmp(yahoo_dbg_EnabledKeys[i], key))
		{
			free(yahoo_dbg_EnabledKeys[i]);
			yahoo_dbg_EnabledKeys[i] = NULL;
			return 0;
		}
	}
	return 1;
}

int yahoo_dbg_Enable(const char *key)
{
	int i;

	if (!key)
	{
		fprintf(stderr, "yahoo_dbg_Enable Error: key is null.\n");
		return 1;
	}
	for (i = 0; i <= yahoo_dbg_MaxEnabledKey; i++)
	{
		if (yahoo_dbg_EnabledKeys[i]
			&& !strcmp(yahoo_dbg_EnabledKeys[i], key))
		{
			return 0;			/* already enabled */
		}
	}
	yahoo_dbg_EnabledKeys[yahoo_dbg_MaxEnabledKey + 1] = NULL;
	for (i = 0; i <= yahoo_dbg_MaxEnabledKey + 1; i++)
	{
		if (!yahoo_dbg_EnabledKeys[i])
		{
			yahoo_dbg_EnabledKeys[i] = strdup(key);
			if (i > yahoo_dbg_MaxEnabledKey)
			{
				yahoo_dbg_MaxEnabledKey = i;
			}
			return 0;			/* already enabled */
		}
	}
	return 0;
}

int yahoo_dbg_Print(const char *key, char *format, ...)
{
	va_list args;

	if (!key)
	{
		fprintf(stderr, "yahoo_dbg_Print Error: key is null.\n");
		return 1;
	}
	if (!format)
	{
		fprintf(stderr, "yahoo_dbg_Print Error: format is null.\n");
		return 1;
	}

	if (!debugfile)
	{
		debugfile = stdout;
	}

	if (yahoo_dbg_IsEnabled(key) || yahoo_dbg_IsEnabled("all"))
	{
		va_start(args, format);
		vfprintf(debugfile, format, args);
		va_end(args);

		fflush(debugfile);
	}
	return 0;
}

int yahoo_dbg_Close(void)
{
	if (debugfile && debugfile != stdout)
	{
		fclose(debugfile);
		debugfile = NULL;
	}
	return 0;
}

int yahoo_dbg_IsEnabled(const char *key)
{
	int i;

	for (i = 0; i <= yahoo_dbg_MaxEnabledKey; i++)
	{
		if (!strcmp(yahoo_dbg_EnabledKeys[i], key))
		{
			return 1;			/* is enabled */
		}
	}
	return 0;
}

#else

/* Debugging is not enabled, so do an inline routines that do nothing */
/* Hopefully the compiler will completely optimize these routines away */

inline char *yahoo_unraw_buffer(const char *buffer, unsigned int len)
{
	return (char *) 0;
}

inline int yahoo_dbg_Open(char *file)
{
	return 0;
}

inline int yahoo_dbg_Disable(char *key)
{
	return 0;
}

inline int yahoo_dbg_Enable(char *key)
{
	return 0;
}

inline int yahoo_dbg_Print(char *key, char *format, ...)
{
	return 0;
}

inline int yahoo_dbg_Close(void)
{
	return 0;
}

inline int yahoo_dbg_IsEnabled(char *key)
{
	return 0;
}

#endif