File: indent.c

package info (click to toggle)
xmlindent 0.2.18-0.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: ansic: 625; yacc: 63; makefile: 29; xml: 22
file content (512 lines) | stat: -rw-r--r-- 12,424 bytes parent folder | download
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * Copyright (C) 2002-2004  Pekka Enberg <penberg@iki.fi>
 *
 * 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "buffer.h"
#include "indent.h"

/*
 * Functions the generated scanner uses. 
 */
static void xml_declaration(void);
static void cdata_section(void);
static void doctype_declaration(void);
static void start_tag(void);
static void end_tag(void);
static void empty_element_tag(void);
static void comment(void);
static void content(void);
static void processing_insn(void);
static void do_newline(struct buffer * buffer, const char * text);

#include "lex.yy.c"

/*
 * Flex unput() wrapper
 */
extern void do_unput(int);

/*
 * Functions we use.
 */
static void newline(void);

/*
 * We have three buffers: primary, secondary, and tag buffer. The first one
 * has all data before previous start-tag, second one has data encountered
 * after previous start-tag, and the last one _has_ the start-tag.
 */
struct buffer primary_buf;
struct buffer secondary_buf;
struct buffer tag_buf;
struct buffer * current_buf;

/*
 * Indent options. To be modified only trough set_options.
 */
static char indent_char;
static int  num_indent_chars;
static bool force_newline_after_start_tag;
static bool force_newline_after_end_tag;
static bool force_newline_before_start_tag;
static bool force_newline_before_end_tag;
static bool force_always;
static int  max_columns;
static bool wrap_long_lines;
static FILE * indent_output;

/*
 * Default options.
 */
#define DEFAULT_INDENT_CHAR ' '
#define DEFAULT_NUM_INDENT_CHARS 4

/* Set default indent options.  */
void indent_options_set_defaults(struct indent_options * opts)
{
    opts->indent_char      = DEFAULT_INDENT_CHAR;
    opts->num_indent_chars = DEFAULT_NUM_INDENT_CHARS;
    opts->max_columns      = -1;
    opts->wrap_long_lines  = false;
    opts->force_newline_after_start_tag  = true;
    opts->force_newline_after_end_tag    = true;
    opts->force_newline_before_start_tag = true;
    opts->force_newline_before_end_tag   = true;
    opts->force_always                   = false;
}

/* Set indent options.  */
static void set_options(struct indent_options * opts)
{
    indent_char      = opts->indent_char;
    num_indent_chars = opts->num_indent_chars;
    max_columns      = opts->max_columns;
    wrap_long_lines  = opts->wrap_long_lines;
    force_newline_after_start_tag  = opts->force_newline_after_start_tag;
    force_newline_after_end_tag    = opts->force_newline_after_end_tag;
    force_newline_before_start_tag = opts->force_newline_before_start_tag;
    force_newline_before_end_tag   = opts->force_newline_before_end_tag;
    force_always                   = opts->force_always;
}

#define BUFFER_INITIAL_CAPACITY 1024

void indent(FILE * input, FILE * output, struct indent_options * opts)
{
    set_options(opts);

    buffer_init(&primary_buf,   BUFFER_INITIAL_CAPACITY);
    buffer_init(&secondary_buf, BUFFER_INITIAL_CAPACITY);
    buffer_init(&tag_buf,       BUFFER_INITIAL_CAPACITY);

    current_buf   = &primary_buf;
    yyin          = input;
    indent_output = output;
    yylex();

    /*
     * There might not have been a newline before EOF.
     */
    buffer_flush(current_buf, indent_output);

    buffer_release(&primary_buf);
    buffer_release(&secondary_buf);
    buffer_release(&tag_buf);
}

/* Print indent characters.  */
static void print_indent(FILE * output, int indent_level)
{
    int i;

    for (i = 0; i < (num_indent_chars * indent_level); i++) {
	fputc(indent_char, output);
    }
}

static void xml_declaration(void)
{
    buffer_push_str(current_buf, yytext);
}

static void cdata_section(void)
{
    buffer_push_str(current_buf, yytext);
}

static void doctype_declaration(void)
{
    buffer_push_str(current_buf, yytext);
}

/* XML end of line characters.  */
#define CARRIAGE_RETURN 0x0D
#define LINE_FEED       0x0A
#define NEL             0x85

static inline bool is_newline(int current)
{
    if ((CARRIAGE_RETURN == current)
	|| (LINE_FEED    == current)
	|| (NEL          == current))
	return true;

    return false;
}

/* Check for whitespace character.  */
static inline bool is_whitespace(int c)
{
    return ((c == ' ') || (c == '\f') || (c == '\t') || (c == '\v'));
}

/* Eat whitespace from stream.  */
static void eat_whitespace(void)
{
    for (;;) {
	int current = input();
	if (!is_whitespace(current)) {
	    do_unput(current);
	    break;
	}
    }
}

static int indent_level = 0;
static int indent_delta = 0;

/* Force newline before tag. Use buffer for getting current character.  */
static void force_newline_before_tag(struct buffer * buffer)
{
    int current;

    if (buffer_size(buffer) == 0) {
	/*
	 * We just did a newline, no need to force it.
	 */
	return;
    }

    current = buffer_pop_char(buffer);
    buffer_push_char(buffer, current);

    if (!is_newline(current)) {
        do_newline(buffer, "\n");
        if (is_whitespace(current)) {
            eat_whitespace();
            return;
        }
    }
}

/* Force newline after tag. Use lexer to get current character.  */
static void force_newline_after_tag(struct buffer * buffer)
{
    int current = input();

    do_unput(current);

    if (!is_newline(current)) {
        do_newline(buffer, "\n");
        eat_whitespace();
    }
}

static bool using_primary_buf(void)
{
    return current_buf == &primary_buf;
}

/* Merge tag and secondary buffer to primary buffer. Force newlines if
   necessary. This routine is used with start-tags.  */
static void merge_buffers_start_tag(void)
{
    if (force_newline_before_start_tag) {
        force_newline_before_tag(&primary_buf);
    }
    buffer_copy(&primary_buf, &tag_buf);
    indent_delta++;
    if (force_newline_after_start_tag) {
        force_newline_after_tag(&primary_buf);
    }
    buffer_copy(&primary_buf, &secondary_buf);

    buffer_clear(&tag_buf);
    buffer_clear(&secondary_buf);

    current_buf = &primary_buf;
}

/* Merge tag and secondary buffer back to primary buffer.  */
static void merge_buffers(void)
{
    buffer_copy(&primary_buf, &tag_buf);
    buffer_copy(&primary_buf, &secondary_buf);

    buffer_clear(&tag_buf);
    buffer_clear(&secondary_buf);

    current_buf = &primary_buf;
    /* We just processed a start-tag so bump up indent_delta.  */
    indent_delta++;
}

/* Force newline for wrapping line. Use lexer to get current character and
   do not eat whitespace from next line.  */
static void force_newline_for_wrap(struct buffer * buffer)
{
    int current = input();

    /*
     * Flush all pending stuff before doing the newline.
     */
    if (!using_primary_buf()) {
	merge_buffers();
    }
    do_newline(current_buf, "\n");

    if (!is_newline(current))
	do_unput(current);
}

static void start_tag(void)
{
    char * tmp;
    /*
     * Save text because merge_buffers_start_tag may trash it.
     */
    tmp = strdup(yytext);

    if (!using_primary_buf()) {
	/*
	 * This is second start-tag. Thus first one has children. We can force
	 * newline here if we want.
	 */
        merge_buffers_start_tag();
    }
    buffer_push_str(&tag_buf, tmp);
    current_buf = &secondary_buf;
    free(tmp);
}

static void end_tag(void)
{
    bool can_force_newline;
    char * tmp;
    /*
     * Save text because force_newline_before_tag can trash it.
     */
    tmp = strdup(yytext);

    if (using_primary_buf()) {
        can_force_newline = true;
    } else {
        /* The element didn't have any children - force newline only if user
           explicity requested that.  */
	merge_buffers();
	can_force_newline = force_always;
    }
    if (force_newline_before_end_tag && can_force_newline) {
        force_newline_before_tag(current_buf);
    }
    buffer_push_str(current_buf, tmp);
    indent_delta--;

    if (force_newline_after_end_tag && can_force_newline) {
        force_newline_after_tag(current_buf);
    }
    free(tmp);
}

static void empty_element_tag(void)
{
    bool can_force_newline;
    char * tmp;
    /*
     * Save text because force_newline_before_tag can trash it.
     */
    tmp = strdup(yytext);

    /* We treat empty element tag as a "merged" start-tag and end-tag.
       Therefore we use start-tag options before the tag and end-tag
       options after the tag.  */

    if (!using_primary_buf()) {
	/*
	 * This is second start-tag. Thus first one has children. We can force
	 * newline here if we want.
	 */
        merge_buffers_start_tag();
        can_force_newline = force_always;
    } else {
        can_force_newline = true;
    }
    buffer_push_str(current_buf, tmp);

    if (force_newline_after_end_tag && can_force_newline) {
        /* Empty element never has any children. Force newline only if user
           explicitly requested it.  */
        force_newline_after_tag(current_buf);
    }
    free(tmp);
}

static int input_and_push(void)
{
    int ret = input();
    if (ret != EOF) buffer_push_char(current_buf, ret);
    return ret;
}

static void comment(void)
{
    int c;

    buffer_push_str(current_buf, yytext);
    for (;;) {
	while ((c = input_and_push()) != '-' &&
	       c != EOF)
	    ;
	if ((c = input_and_push()) != '-') {
	    continue;
	}
	if ((c = input_and_push()) == '>') {
	    break;
	}
    }
}

static void do_newline(struct buffer * buffer, const char * text)
{
    buffer_push_str(buffer, text);

    if (indent_delta < 0) indent_level += indent_delta;
    print_indent(indent_output, indent_level);
    if (indent_delta > 0) indent_level += indent_delta;
    indent_delta = 0;

    buffer_flush(buffer, indent_output);
}

static void newline(void)
{
    /*
     * Flush all pending stuff before doing the newline.
     */
    if (!using_primary_buf()) {
	merge_buffers();
    }
    do_newline(current_buf, "\n");
    eat_whitespace();
}

/*
 * We assume tab is equal to 8 spaces.
 */
#define TAB_SIZE 8

static unsigned long indent_size(void)
{
    return (indent_char == '\t'
	    ? indent_level * TAB_SIZE
	    : indent_level * num_indent_chars);
}

static bool need_wrap(struct buffer * buffer)
{
    if (buffer == &primary_buf)
	return buffer_size(buffer) + indent_size() == max_columns;
    else
	return (buffer_size(&primary_buf) + buffer_size(&tag_buf)
		+ buffer_size(buffer) + indent_size()) >= max_columns;
}

/*
 * We detect EOF by getting a call to yywrap() when the only input file
 * is completely read.
 */
static bool is_at_eof;

int yywrap() {
    is_at_eof = 1;
    return 1; /* 1=nothing more to read */
}

static void content(void)
{
    char current;

    /*
     * We should get one character at a time.
     */
    assert(yyleng == 1); /* strlen(yytext) fails at NUL or EOF */

    current = yytext[0];
    if (is_at_eof)
	return;
    
    if (is_newline(current)) {
	 newline();
	 return;
    }
    buffer_push_char(current_buf, current);

    /*
     * Forcing newline changes 'text' so lets do it after we've pushed
     * it to the buffer.
     */
    if (wrap_long_lines && need_wrap(current_buf)) {
	struct buffer tmp;
	buffer_init(&tmp, buffer_size(current_buf));
	/*
	 * Find last character that was not whitespace
	 */
	for (;;) {
	    int c;
	    if (buffer_size(current_buf) == 0)
		break;

	    c = buffer_pop_char(current_buf);
	    if (is_whitespace(c)) {
		/*
		 * Do not push whitespace because it would appear
		 * after the newline.
		 */
		break;
	    }
	    /*
	     * Characters are put in tmp buffer in reverse order.
	     */
	    buffer_push_char(&tmp, c);
	}
	force_newline_for_wrap(current_buf);
	/*
	 * Restore non-wrapped text into buffer.
	 */
	while (buffer_size(&tmp) > 0) {
	    buffer_push_char(current_buf, buffer_pop_char(&tmp));
	}
	buffer_release(&tmp);
    }
}

static void processing_insn(void)
{
    buffer_push_str(current_buf, yytext);
}