summaryrefslogtreecommitdiffstats
path: root/lib/skin_parser/skin_debug.c
blob: a09cd673c7a4d484599e6f1992e58863ecd53a29 (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Robert Bieber
 *
 * 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
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "skin_parser.h"
#include "skin_debug.h"
#include "tag_table.h"

/* Global variables for debug output */
int debug_indent_level = 0;
extern int skin_line;
extern char* skin_start;

/* Global error variables */
int error_line;
int error_col;
const char *error_line_start;
char* error_message;

/* Debugging functions */
void skin_error(enum skin_errorcode error, const char* cursor)
{

    error_col = 0;

    while(cursor > skin_start && *cursor != '\n')
    {
        cursor--;
        error_col++;
    }
    error_line_start = cursor+1;

    error_line = skin_line;

    switch(error)
    {
    case MEMORY_LIMIT_EXCEEDED:
        error_message = "Memory limit exceeded";
        break;
    case NEWLINE_EXPECTED:
        error_message = "Newline expected";
        break;
    case ILLEGAL_TAG:
        error_message = "Illegal tag";
        break;
    case ARGLIST_EXPECTED:
        error_message = "Argument list expected";
        break;
    case TOO_MANY_ARGS:
        error_message =  "Too many arguments given";
        break;
    case DEFAULT_NOT_ALLOWED:
        error_message = "Argument can not be set to default";
        break;
    case UNEXPECTED_NEWLINE:
        error_message  = "Unexpected newline";
        break;
    case INSUFFICIENT_ARGS:
        error_message = "Not enough arguments";
        break;
    case INT_EXPECTED:
        error_message =  "Expected integer";
        break;
    case DECIMAL_EXPECTED:
        error_message =  "Expected decimal";
        break;
    case SEPARATOR_EXPECTED:
        error_message = "Expected argument separator";
        break;
    case CLOSE_EXPECTED:
        error_message = "Expected list close";
        break;
    case MULTILINE_EXPECTED:
        error_message = "Expected subline separator";
        break;
    };

}

int skin_error_line()
{
    return error_line;
}

int skin_error_col()
{
    return error_col;
}

char* skin_error_message()
{
    return error_message;
}

void skin_clear_errors()
{
    error_line = 0;
    error_col = 0;
    error_message = NULL;
}

#if !defined(ROCKBOX) || defined(__PCTOOL__)
void skin_debug_tree(struct skin_element* root)
{
    int i;
    char *text;

    struct skin_element* current = root;

    while(current)
    {
        skin_debug_indent();

        switch(current->type)
        {
        case UNKNOWN:
            printf("[ Unknown element.. error\n]");
            break;

        case VIEWPORT:
            printf("[ Viewport \n");

            debug_indent_level++;
            skin_debug_tree(current->children[0]);
            debug_indent_level--;

            printf("]");
            break;

        case TEXT:
            text = current->data;
            printf("[ Plain text on line %d : %s ]\n", current->line, text);
            break;

        case COMMENT:
            text = current->data;
            printf("[ Comment on line %d: ", current->line);
            for(i = 0; i < (int)strlen(text); i++)
            {
                if(text[i] == '\n')
                    printf("\\n");
                else
                    printf("%c", text[i]);
            }
            printf(" ]\n");
            break;

        case TAG:
            printf("[ %s tag on line %d with %d arguments\n",
                   current->tag->name,
                   current->line, current->params_count);
            debug_indent_level++;
            skin_debug_params(current->params_count, current->params);
            debug_indent_level--;
            skin_debug_indent();
            printf("]\n");

            break;

        case LINE_ALTERNATOR:
            printf("[ Alternator on line %d with %d sublines \n", current->line,
                   current->children_count);
            debug_indent_level++;
            for(i = 0; i < current->children_count; i++)
            {
                skin_debug_tree(current->children[i]);
            }
            debug_indent_level--;

            skin_debug_indent();
            printf("]\n");
            break;

        case CONDITIONAL:
            printf("[ Conditional tag on line %d with %d enumerations \n",
                   current->line, current->children_count - 1);
            debug_indent_level++;

            skin_debug_indent();
            printf("[ Condition tag \n");
            debug_indent_level++;
            skin_debug_tree(current->children[0]);
            debug_indent_level--;
            skin_debug_indent();
            printf("]\n");

            for(i = 1; i < current->children_count; i++)
            {
                skin_debug_indent();
                printf("[ Enumeration %d\n", i - 1);
                debug_indent_level++;
                skin_debug_tree(current->children[i]);
                debug_indent_level--;
                skin_debug_indent();
                printf("]\n");
            }

            debug_indent_level--;
            skin_debug_indent();
            printf("]\n");


            break;

        case LINE:
            printf("[ Logical line on line %d\n", current->line);

            debug_indent_level++;
            if (current->children)
                skin_debug_tree(current->children[0]);
            debug_indent_level--;

            skin_debug_indent();
            printf("]\n");
            break;
        }

        current = current->next;
    }

}

void skin_debug_params(int count, struct skin_tag_parameter params[])
{
    int i;
    for(i = 0; i < count; i++)
    {

        skin_debug_indent();
        switch(params[i].type)
        {
        case DEFAULT:
            printf("[-]");
            break;

        case STRING:
            printf("[%s]", params[i].data.text);
            break;

        case INTEGER:
            printf("[%d]", params[i].data.number);
            break;
            
        case DECIMAL:
            printf("[%d.%d]", params[i].data.number/10,
                              params[i].data.number%10);
            break;

        case CODE:
            printf("[ WPS Code: \n");
            debug_indent_level++;
            skin_debug_tree(params[i].data.code);
            debug_indent_level--;
            skin_debug_indent();
            printf("]");
            break;
        }

        printf("\n");

    }
}

void skin_debug_indent()
{
    int i;
    for(i = 0; i < debug_indent_level; i++)
        printf("    ");
}

#endif

#define MIN(a,b) ((a<b)?(a):(b))
void skin_error_format_message()
{
    int i;
    char text[128];
    char* line_end = strchr(error_line_start, '\n');
    int len = MIN(line_end - error_line_start, 80);
    if (!line_end)
        len = strlen(error_line_start);
    printf("Error on line %d.\n", error_line);
    error_col--;
    if (error_col <= 10)
    {
        strncpy(text, error_line_start, len);
        text[len] = '\0';
    }
    else
    {
        int j;
        /* make it fit nicely.. "<start few chars>...<10 chars><error>" */
        strncpy(text, error_line_start, 6);
        i = 5;
        text[i++] = '.';
        text[i++] = '.';
        text[i++] = '.';
        for (j=error_col-10; error_line_start[j] && error_line_start[j] != '\n'; j++)
            text[i++] = error_line_start[j];
        text[i] = '\0';
        error_col = 18;
    }
    printf("%s\n", text);
    for (i=0; i<error_col; i++)
        text[i] = ' ';
    snprintf(&text[i],64, "^ \'%s\' Here", error_message);
    printf("%s\n", text);
}