Added mpdm_dump_unref() at the end of stress.
[mpsl.git] / mpsl_d.c
bloba40a6be2f2333c0a8042da903e8f1d49e036cf53
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2009 Angel Ortega <angel@triptico.com>
6 mpsl_d.c - Minimum Profit Scripting Language debugging functions
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include "config.h"
28 #include <stdio.h>
29 #include <wchar.h>
30 #include <malloc.h>
32 #include "mpdm.h"
33 #include "mpsl.h"
35 /** code **/
37 static wchar_t *dump_string(const mpdm_t v, wchar_t *ptr, int *size)
38 /* dumps a string, escaping special chars */
40 wchar_t *iptr = mpdm_string(v);
42 ptr = mpdm_pokews(ptr, size, L"\"");
44 while (*iptr != L'\0') {
45 switch (*iptr) {
46 case '"':
47 ptr = mpdm_pokews(ptr, size, L"\\\"");
48 break;
50 case '\'':
51 ptr = mpdm_pokews(ptr, size, L"\\'");
52 break;
54 case '\r':
55 ptr = mpdm_pokews(ptr, size, L"\\r");
56 break;
58 case '\n':
59 ptr = mpdm_pokews(ptr, size, L"\\n");
60 break;
62 case '\t':
63 ptr = mpdm_pokews(ptr, size, L"\\t");
64 break;
66 case '\\':
67 ptr = mpdm_pokews(ptr, size, L"\\\\");
68 break;
70 default:
71 if (*iptr > 127) {
72 char tmp[16];
73 wchar_t *wptr;
75 sprintf(tmp, "\\x{%04x}", *iptr);
76 wptr = mpdm_mbstowcs(tmp, NULL, -1);
77 ptr = mpdm_pokews(ptr, size, wptr);
78 free(wptr);
80 else
81 ptr = mpdm_poke(ptr, size, iptr, 1, sizeof(wchar_t));
83 break;
85 iptr++;
88 ptr = mpdm_pokews(ptr, size, L"\"");
90 return ptr;
94 wchar_t *mpsl_dump_1(const mpdm_t v, int l, wchar_t *ptr, int *size)
95 /* dump plugin for mpdm_dump() */
97 int n;
99 /* indent (if negative, don't prepend indentation) */
100 if (l < 0)
101 l = -l;
102 else
103 for (n = 0; n < l; n++)
104 ptr = mpdm_pokews(ptr, size, L" ");
106 if (v == NULL)
107 ptr = mpdm_pokews(ptr, size, L"NULL");
108 else
109 if (MPDM_IS_HASH(v)) {
110 mpdm_t k;
111 mpdm_t w;
112 int c;
114 c = n = 0;
116 ptr = mpdm_pokews(ptr, size, L"{\n");
118 while (mpdm_iterator(v, &c, &k, &w)) {
119 if (n++)
120 ptr = mpdm_pokews(ptr, size, L",\n");
122 ptr = mpsl_dump_1(k, l + 1, ptr, size);
123 ptr = mpdm_pokews(ptr, size, L" => ");
124 ptr = mpsl_dump_1(w, -(l + 1), ptr, size);
127 ptr = mpdm_pokews(ptr, size, L"\n");
129 /* re-indent */
130 for (n = 0; n < l; n++)
131 ptr = mpdm_pokews(ptr, size, L" ");
133 ptr = mpdm_pokews(ptr, size, L"}");
135 else
136 if (MPDM_IS_ARRAY(v)) {
137 ptr = mpdm_pokews(ptr, size, L"[\n");
139 for (n = 0; n < mpdm_size(v); n++) {
140 ptr = mpsl_dump_1(mpdm_aget(v, n), l + 1, ptr, size);
142 if (n < mpdm_size(v) - 1)
143 ptr = mpdm_pokews(ptr, size, L",");
145 ptr = mpdm_pokews(ptr, size, L"\n");
148 /* re-indent */
149 for (n = 0; n < l; n++)
150 ptr = mpdm_pokews(ptr, size, L" ");
152 ptr = mpdm_pokews(ptr, size, L"]");
154 else
155 if (MPDM_IS_EXEC(v)) {
156 char tmp[256];
157 wchar_t *wptr;
159 snprintf(tmp, sizeof(tmp), "bincall(%p)", v->data);
160 wptr = mpdm_mbstowcs(tmp, NULL, -1);
161 ptr = mpdm_pokews(ptr, size, wptr);
162 free(wptr);
164 else
165 if (MPDM_IS_STRING(v))
166 ptr = dump_string(v, ptr, size);
167 else
168 ptr = mpdm_pokews(ptr, size, L"NULL /* non-printable value */");
170 if (l == 0)
171 ptr = mpdm_pokews(ptr, size, L";\n");
173 return ptr;