aboutsummaryrefslogtreecommitdiff
path: root/src/debug.cin
blob: 34f06a038c9419feaae259cceb6e80c34a241783 (plain)
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
/* This file is part of mailfromd.             -*- c -*-
   Copyright (C) 2007 Sergey Poznyakoff

   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, 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., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include "mailfromd.h"

struct modinfo {
	char *name;
	unsigned code;
};

static char *modname[] = {
	"all", 
MAKEDEBUGTAB(SRCLIST)
};

#define NMODULES (sizeof modname / sizeof modname[0])

static int modlevel[NMODULES];         /* Per-module levels */


/* Implementation */

int
debug_level_p(int modn, int level)
{
	return modn < NMODULES
		&& (modlevel[modn] ? (modlevel[modn] >= level)
		    : modlevel[0] >= level);
}

static int
find_module(const char *name)
{
	size_t len, i;
	const char *q;
	const char *p;

	p = strrchr(name, '/');                     
	if (p)                                   
		p++;                             
	else                                     
		p = name;                           
	q = strrchr(p, '.');                     
	if (q)                                   
		len = q - name;                       
	else                                     
		len = strlen(p);

	for (i = 0; i < NMODULES; i++) {
		if (strlen(modname[i]) == len
		    && memcmp(modname[i], p, len) == 0)
			return i;
	}
	return -1;
}

void
debug_enable_module(const char *file, int level)
{
	int modn = find_module(file);
	if (modn < 0) {
		mu_error(_("unknown module: %s"), file);
		return;
	}
	modlevel[modn] = level;
}

int
debug_module_level(const char *modname, int *plev)
{
	int modn;

	if (modname) {
		modn = find_module(modname);
		if (modn < 0) 
			return 1;
	} else
		modn = 0;
	*plev = modlevel[modn];
	return 0;
}

static void
parse_spec(char *spec)
{
	char *p, *q;
	int level;
	
	if (isdigit(*spec)) {
		modlevel[0] = strtoul(spec, &q, 0);
		if (*q) 
			mu_error(_("%s: wrong debug spec near %s"), spec, q);
		if (modlevel[0] >= 100)
 			enable_prog_trace("all");
		return;
	}

	p = strchr(spec, '=');
	if (p) {
		*p++ = 0;
		level = strtol(p, &q, 0);
		if (*q || level < 0) { 
			mu_error(_("%s: wrong debug spec near %s"), spec, q);
			return;
		}
	} else
		level = 100;
	debug_enable_module(spec, level);
}

void
debug_parse_spec(const char *spec)
{
	char *copy = xstrdup(spec);
	char *s;
	
	for (s = strtok(copy, ","); s; s = strtok(NULL, ","))
		parse_spec(s);
	free(copy);
}

int
debug_spec_string(const char *spec, char **pbuf)
{
	char mods[NMODULES];
	size_t len = 0, i;
	char nbuf[NUMERIC_BUFSIZE_BOUND];
	char *buf, *ptr;
	
	if (!spec) {
		memset(mods, 0xff, sizeof mods);
	} else {
		char *copy = xstrdup(spec);
		char *s;
	
		memset(mods, 0, sizeof mods);
		for (s = strtok(copy, ","); s; s = strtok(NULL, ",")) {
			int n = find_module(s);
			if (n < 0) {
				free(copy);
				return EINVAL;
			}
			mods[n] = 1;
		}
	}

	if (mods[0]) {
		snprintf(nbuf, sizeof nbuf, "%d", modlevel[0]);
		len += strlen(nbuf) + 1;
	}
	for (i = 1; i < NMODULES; i++) {
		if (mods[i]) {
			snprintf(nbuf, sizeof nbuf, "%d", modlevel[i]);
			len += strlen(modname[i]) + 1 + strlen(nbuf) + 1;
		}
	}		

	buf = malloc(len + 1);
	if (!buf)
		return ENOMEM;

	ptr = buf;
	if (mods[0]) {
		sprintf(ptr, "%d", modlevel[0]);
		ptr += strlen(ptr);
	}
	for (i = 1; i < NMODULES; i++) {
		if (mods[i]) {
			if (ptr > buf)
				*ptr++ = ',';
			sprintf(ptr, "%s=%d", modname[i], modlevel[i]);
			ptr += strlen(ptr);
		}
	}
	*ptr = 0;
	*pbuf = buf;
	return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.