Updated WHILE opcode documentation.
[mpsl.git] / mpsl_d.c
blobf6896de63d99aeb3fca1de428ed9602e19eb9c7e
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 <string.h>
30 #include <wchar.h>
31 #include <malloc.h>
33 #include "mpdm.h"
34 #include "mpsl.h"
36 /** code **/
38 static wchar_t *dump_string(const mpdm_t v, wchar_t * ptr, int *size)
39 /* dumps a string, escaping special chars */
41 wchar_t *iptr = mpdm_string(v);
43 ptr = mpdm_pokews(ptr, size, L"\"");
45 while (*iptr != L'\0') {
46 switch (*iptr) {
47 case '"':
48 ptr = mpdm_pokews(ptr, size, L"\\\"");
49 break;
51 case '\'':
52 ptr = mpdm_pokews(ptr, size, L"\\'");
53 break;
55 case '\r':
56 ptr = mpdm_pokews(ptr, size, L"\\r");
57 break;
59 case '\n':
60 ptr = mpdm_pokews(ptr, size, L"\\n");
61 break;
63 case '\t':
64 ptr = mpdm_pokews(ptr, size, L"\\t");
65 break;
67 case '\\':
68 ptr = mpdm_pokews(ptr, size, L"\\\\");
69 break;
71 default:
72 if (*iptr > 127) {
73 char tmp[16];
74 wchar_t *wptr;
76 sprintf(tmp, "\\x{%04x}", *iptr);
77 wptr = mpdm_mbstowcs(tmp, NULL, -1);
78 ptr = mpdm_pokews(ptr, size, wptr);
79 free(wptr);
81 else
82 ptr = mpdm_poke(ptr, size, iptr, 1, sizeof(wchar_t));
84 break;
86 iptr++;
89 ptr = mpdm_pokews(ptr, size, L"\"");
91 return ptr;
95 wchar_t *mpsl_dump_1(const mpdm_t v, int l, wchar_t * ptr, int *size)
96 /* dump plugin for mpdm_dump() */
98 int n;
99 char tmp[256];
100 wchar_t *wptr;
103 /* indent (if negative, don't prepend indentation) */
104 if (l < 0)
105 l = -l;
106 else
107 for (n = 0; n < l; n++)
108 ptr = mpdm_pokews(ptr, size, L" ");
110 if (v == NULL)
111 ptr = mpdm_pokews(ptr, size, L"NULL");
112 else
113 if (MPDM_IS_HASH(v)) {
114 mpdm_t k;
115 mpdm_t w;
116 int c;
118 c = n = 0;
120 ptr = mpdm_pokews(ptr, size, L"{\n");
122 while (mpdm_iterator(v, &c, &k, &w)) {
123 if (n++)
124 ptr = mpdm_pokews(ptr, size, L",\n");
126 ptr = mpsl_dump_1(k, l + 1, ptr, size);
127 ptr = mpdm_pokews(ptr, size, L" => ");
128 ptr = mpsl_dump_1(w, -(l + 1), ptr, size);
131 ptr = mpdm_pokews(ptr, size, L"\n");
133 /* re-indent */
134 for (n = 0; n < l; n++)
135 ptr = mpdm_pokews(ptr, size, L" ");
137 ptr = mpdm_pokews(ptr, size, L"}");
139 else
140 if (MPDM_IS_ARRAY(v)) {
141 ptr = mpdm_pokews(ptr, size, L"[\n");
143 for (n = 0; n < mpdm_size(v); n++) {
144 ptr = mpsl_dump_1(mpdm_aget(v, n), l + 1, ptr, size);
146 if (n < mpdm_size(v) - 1)
147 ptr = mpdm_pokews(ptr, size, L",");
149 ptr = mpdm_pokews(ptr, size, L"\n");
152 /* re-indent */
153 for (n = 0; n < l; n++)
154 ptr = mpdm_pokews(ptr, size, L" ");
156 ptr = mpdm_pokews(ptr, size, L"]");
158 else
159 if (MPDM_IS_EXEC(v)) {
160 snprintf(tmp, sizeof(tmp), "bincall(%p)", v->data);
161 wptr = mpdm_mbstowcs(tmp, NULL, -1);
162 ptr = mpdm_pokews(ptr, size, wptr);
163 free(wptr);
165 else
166 if (MPDM_IS_STRING(v))
167 ptr = dump_string(v, ptr, size);
168 else
169 if (v->flags & MPDM_FILE) {
170 FILE *f;
172 f = mpdm_get_filehandle(v);
174 if (f != NULL)
175 snprintf(tmp, sizeof(tmp), "NULL /* file descriptor: %d */",
176 fileno(f));
177 else
178 strcpy(tmp, "NULL /* file descriptor (unknown handle) */");
180 wptr = mpdm_mbstowcs(tmp, NULL, -1);
181 ptr = mpdm_pokews(ptr, size, wptr);
182 free(wptr);
184 else
185 if (v->flags & MPDM_REGEX)
186 ptr =
187 mpdm_pokews(ptr, size,
188 L"NULL /* compiled regular expression */");
189 else
190 if (v->flags & MPDM_MUTEX)
191 ptr = mpdm_pokews(ptr, size, L"NULL /* mutex */");
192 else
193 if (v->flags & MPDM_SEMAPHORE)
194 ptr = mpdm_pokews(ptr, size, L"NULL /* semaphore */");
195 else
196 if (v->flags & MPDM_THREAD)
197 ptr = mpdm_pokews(ptr, size, L"NULL /* thread handle */");
198 else {
199 snprintf(tmp, sizeof(tmp),
200 "NULL /* non-printable value (flags: 0x%04x) */",
201 v->flags);
202 wptr = mpdm_mbstowcs(tmp, NULL, -1);
203 ptr = mpdm_pokews(ptr, size, wptr);
204 free(wptr);
207 if (l == 0)
208 ptr = mpdm_pokews(ptr, size, L";\n");
210 return ptr;