File: highlight.c

package info (click to toggle)
cgdb 0.6.7-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch
  • size: 3,276 kB
  • sloc: ansic: 17,828; sh: 5,176; exp: 1,019; lex: 611; makefile: 287; yacc: 255; cpp: 10
file content (493 lines) | stat: -rw-r--r-- 15,206 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
/* highlight.c:
 * ------------
 * 
 * Syntax highlighting routines.
 *
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

/* System Includes */
#if HAVE_CTYPE_H
#include <ctype.h>
#endif /* HAVE_CTYPE_H */

#if HAVE_LIMITS_H
#include <limits.h>             /* CHAR_MAX */
#endif /* HAVE_LIMITS_H */

#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */

#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif /* HAVE_SYS_TYPES_H */

#if HAVE_REGEX_H
#include <regex.h>
#endif /* HAVE_REGEX_H */

/* Local Includes */
#include "highlight.h"
#include "highlight_groups.h"
#include "sources.h"
#include "cgdb.h"
#include "tokenizer.h"
#include "interface.h"
#include "sys_util.h"
#include "logger.h"

/* ----------- */
/* Definitions */
/* ----------- */

#define HL_CHAR CHAR_MAX        /* Special marker character */

/* --------------- */
/* Local Variables */
/* --------------- */

static int highlight_node(struct list_node *node)
{
    struct tokenizer *t = tokenizer_init();
    int ret;
    struct ibuf *ibuf = ibuf_init();

    ibuf_addchar(ibuf, HL_CHAR);
    ibuf_addchar(ibuf, HLG_TEXT);

    /* Initialize */
    node->buf.length = 0;
    node->buf.tlines = NULL;
    node->buf.max_width = 0;

    if (tokenizer_set_file(t, node->path, node->language) == -1) {
        if_print_message("%s:%d tokenizer_set_file error", __FILE__, __LINE__);
        return -1;
    }

    while ((ret = tokenizer_get_token(t)) > 0) {
        enum tokenizer_type e = tokenizer_get_packet_type(t);

        /*if_print_message  ( "TOKEN(%d:%s)\n", e, tokenizer_get_printable_enum ( e ) ); */

        switch (e) {
            case TOKENIZER_KEYWORD:
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_KEYWORD);
                ibuf_add(ibuf, tokenizer_get_data(t));
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_TYPE:
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TYPE);
                ibuf_add(ibuf, tokenizer_get_data(t));
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_LITERAL:
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_LITERAL);
                ibuf_add(ibuf, tokenizer_get_data(t));
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_NUMBER:
                ibuf_add(ibuf, tokenizer_get_data(t));
                break;
            case TOKENIZER_COMMENT:
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_COMMENT);
                ibuf_add(ibuf, tokenizer_get_data(t));
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_DIRECTIVE:
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_DIRECTIVE);
                ibuf_add(ibuf, tokenizer_get_data(t));
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_TEXT:
                ibuf_add(ibuf, tokenizer_get_data(t));
                break;
            case TOKENIZER_NEWLINE:
                node->buf.length++;
                node->buf.tlines =
                        realloc(node->buf.tlines,
                        sizeof (char *) * node->buf.length);
                node->buf.tlines[node->buf.length - 1] = strdup(ibuf_get(ibuf));

                if (ibuf_length(ibuf) > node->buf.max_width)
                    node->buf.max_width = ibuf_length(ibuf);

                ibuf_clear(ibuf);
                ibuf_addchar(ibuf, HL_CHAR);
                ibuf_addchar(ibuf, HLG_TEXT);
                break;
            case TOKENIZER_ERROR:
                ibuf_add(ibuf, tokenizer_get_data(t));
                break;
            default:
                return -1;
                break;
        }
    }

    return 0;
}

/* --------- */
/* Functions */
/* --------- */

/* See comments in highlight.h for function descriptions. */

void highlight(struct list_node *node)
{
    if (node->language == TOKENIZER_LANGUAGE_UNKNOWN) {
        /* Just copy the lines from the original buffer if no highlighting 
         * is possible */
        int i;

        node->buf.length = node->orig_buf.length;
        node->buf.max_width = node->orig_buf.max_width;
        node->buf.tlines = cgdb_malloc(sizeof (char *) * node->orig_buf.length);
        for (i = 0; i < node->orig_buf.length; i++)
            node->buf.tlines[i] = cgdb_strdup(node->orig_buf.tlines[i]);
    } else
        highlight_node(node);
}

/* highlight_line_segment: Creates a new line that is hightlighted.
 * ------------------------
 *
 *  orig:   The line that needs to be highlighted.
 *  start:  The desired starting position of the highlighted portion.
 *          The start index *is* included in the highlighted segment.
 *  end:    The desired ending position of the highlighted portion.
 *          The end index *is not* include in the highlighted segment.
 *
 *  Return Value: Null on error. Or a pointer to a new line that
 *  has highlighting. The new line MUST BE FREED.
 */
/*#define HIGHLIGHT_DEBUG*/
static char *highlight_line_segment(const char *orig, int start, int end)
{
    char *new_line = NULL;
    int length = strlen(orig), j = 0, pos = 0;
    int syn_search_recieved = 0, cur_color = HLG_TEXT;

    /* Cases not possible */
    if (start > end || orig == NULL ||
            start > length || start < 0 || end > length || end < 0)
        return NULL;

    /* The 5 is: color start (2), color end (2) and EOL */
    if ((new_line = (char *) malloc(sizeof (char) * (length + 5))) == NULL)
        return NULL;

#ifdef HIGHLIGHT_DEBUG
    /*
       for ( j = 0; j < strlen(orig); j++ ) {
       char temp[100];
       sprintf(temp, "(%d:%c)", orig[j], orig[j]);
       scr_add(gdb_win, temp);
       }
       scr_add(gdb_win, "\r\n");
       scr_refresh(gdb_win, 1);
     */
#endif

    /* This traverses the input line. It creates a new line with the section
     *     given highlighted.
     * If a highlight symbol is encountered, the end and/or start position 
     *     is incremented because the original match was against only the text,
     *     not against the color embedded text :)
     */
    for (j = 0; j < length; j++) {
        if (orig[j] == HL_CHAR) {
            if (j <= start)
                start += 2;

            if (j <= end)
                end += 2;

            cur_color = orig[j + 1];
        }

        /* Mark when the search is started and when it ends */
        if (j == start) {
            syn_search_recieved = 1;
            new_line[pos++] = HL_CHAR;
            new_line[pos++] = HLG_SEARCH;
        } else if (j == end) {
            syn_search_recieved = 0;
            new_line[pos++] = HL_CHAR;
            new_line[pos++] = cur_color;
        }

        new_line[pos++] = orig[j];

        /* If the search has started, then make all the colors the 
         * highlighted colors  */
        if (syn_search_recieved && orig[j] == HL_CHAR) {
            ++j;
            new_line[pos++] = HLG_SEARCH;
        }
    }

    new_line[pos] = '\0';

#ifdef HIGHLIGHT_DEBUG
    for (j = 0; j < strlen(new_line); j++) {
        char temp[100];

        fprintf(stderr, "(%d:%c)\r\n", new_line[j], new_line[j]);
    }
#endif

    /* Now the line has new information in it.
     * Lets traverse it again and correct the highlighting to be the 
     * reverse.
     */

    return new_line;
}

void hl_wprintw(WINDOW * win, const char *line, int width, int offset)
{
    int length;                 /* Length of the line passed in */
    enum hl_group_kind color;   /* Color used to print current char */
    int i;                      /* Loops through the line char by char */
    int j;                      /* General iterator */
    int p;                      /* Count of chars printed to screen */
    int pad;                    /* Used to pad partial tabs */
    int attr;                   /* A temp variable used for attributes */
    int highlight_tabstop = cgdbrc_get(CGDBRC_TABSTOP)->variant.int_val;

    /* Jump ahead to the character at offset (process color commands too) */
    length = strlen(line);
    color = HLG_TEXT;

    for (i = 0, j = 0; i < length && j < offset; i++) {
        if (line[i] == HL_CHAR && i + 1 < length) {
            /* Even though we're not printing anything in this loop,
             * the color attribute needs to be maintained for when we
             * start printing in the loop below.  This way the text
             * printed will be done in the correct color. */
            color = (int) line[++i];
        } else if (line[i] == '\t') {
            /* Tab character, expand to size set by user */
            j += highlight_tabstop - (j % highlight_tabstop);
        } else {
            /* Normal character, just increment counter by one */
            j++;
        }
    }
    pad = j - offset;

    /* Pad tab spaces if offset is less than the size of a tab */
    for (j = 0, p = 0; j < pad && p < width; j++, p++)
        wprintw(win, " ");

    /* Set the color appropriately */
    if (hl_groups_get_attr(hl_groups_instance, color, &attr) == -1) {
        logger_write_pos(logger, __FILE__, __LINE__,
                "hl_groups_get_attr error");
        return;
    }

    wattron(win, attr);

    /* Print string 1 char at a time */
    for (; i < length && p < width; i++) {
        if (line[i] == HL_CHAR) {
            if (++i < length) {
                wattroff(win, attr);
                color = (int) line[i];

                if (hl_groups_get_attr(hl_groups_instance, color, &attr) == -1) {
                    logger_write_pos(logger, __FILE__, __LINE__,
                            "hl_groups_get_attr error");
                    return;
                }

                wattron(win, attr);
            }
        } else {
            switch (line[i]) {
                case '\t':
                    do {
                        wprintw(win, " ");
                        p++;
                    } while ((p + offset) % highlight_tabstop > 0 && p < width);
                    break;
                default:
                    wprintw(win, "%c", line[i]);
                    p++;
            }
        }
    }

    /* Shut off color attribute */
    wattroff(win, attr);

    for (; p < width; p++)
        wprintw(win, " ");
}

int hl_regex(const char *regex, const char **hl_lines, const char **tlines,
        const int length, char **cur_line, int *sel_line,
        int *sel_rline, int *sel_col_rbeg, int *sel_col_rend,
        int opt, int direction, int icase)
{
    regex_t t;                  /* Regular expression */
    regmatch_t pmatch[1];       /* Indexes of matches */
    int i = 0, result = 0;
    char *local_cur_line;
    int success = 0;
    int offset = 0;
    int config_wrapscan = cgdbrc_get(CGDBRC_WRAPSCAN)->variant.int_val;

    if (tlines == NULL || tlines[0] == NULL ||
            cur_line == NULL || sel_line == NULL ||
            sel_rline == NULL || sel_col_rbeg == NULL || sel_col_rend == NULL)
        return -1;

    /* Clear last highlighted line */
    if (*cur_line != NULL) {
        free(*cur_line);
        *cur_line = NULL;
    }

    /* If regex is empty, set current line to original line */
    if (regex == NULL || *regex == '\0') {
        *sel_line = *sel_rline;
        return -2;
    }

    /* Compile the regular expression */
    if (regcomp(&t, regex, REG_EXTENDED & (icase) ? REG_ICASE : 0) != 0) {
        regfree(&t);
        return -3;
    }

    /* Forward search */
    if (direction) {
        int start = *sel_rline;
        int end = length;

        offset = *sel_col_rend;
        while (!success) {
            for (i = start; i < end; i++) {
                int local_cur_line_length;

                local_cur_line = (char *) tlines[i];
                local_cur_line_length = strlen(local_cur_line);

                /* Add the position of the current line's last match */
                if (i == *sel_rline) {
                    if (offset >= local_cur_line_length)
                        continue;
                    local_cur_line += offset;
                }

                /* Found a match */
                if ((result = regexec(&t, local_cur_line, 1, pmatch, 0)) == 0) {
                    success = 1;
                    break;
                }
            }

            if (success || start == 0 || !config_wrapscan) {
                break;
            } else {
                end = start;
                start = 0;
            }
        }

    } else {                    /* Reverse search */
        int j, pos;
        int start = *sel_rline;
        int end = 0;

        offset = *sel_col_rbeg;

        /* Try each line */
        while (!success) {
            for (i = start; i >= end; i--) {
                local_cur_line = (char *) tlines[i];
                pos = strlen(local_cur_line) - 1;
                if (pos < 0)
                    continue;

                if (i == *sel_rline)
                    pos = offset - 1;

                /* Try each line, char by char starting from the end */
                for (j = pos; j >= 0; j--) {
                    if ((result = regexec(&t, local_cur_line + j, 1, pmatch,
                                            0)) == 0) {
                        if (i == *sel_rline && pmatch[0].rm_so > pos - j)
                            continue;
                        /* Found a match */
                        success = 1;
                        offset = j;
                        break;
                    }
                }

                if (success)
                    break;
            }

            if (success || start == length - 1 || !config_wrapscan) {
                break;
            } else {
                end = start;
                start = length - 1;
            }
        }

    }

    if (success) {
        /* The offset is 0 if the line was not on the original line */
        if (direction && *sel_rline != i)
            offset = 0;

        /* If final match ( user hit enter ) make position perminant */
        if (opt == 2) {
            *sel_col_rbeg = pmatch[0].rm_so + offset;
            *sel_col_rend = pmatch[0].rm_eo + offset;
            *sel_rline = i;
        }

        /* Keep the new line as the selected line */
        *sel_line = i;

        /* If the match is not perminant then give cur_line highlighting */
        if (opt != 2 && pmatch[0].rm_so != -1 && pmatch[0].rm_eo != -1)
            *cur_line =
                    highlight_line_segment(hl_lines[i],
                    pmatch[0].rm_so + offset, pmatch[0].rm_eo + offset);
    } else {
        /* On failure, the current line goes to the original line */
        *sel_line = *sel_rline;
    }

    regfree(&t);

    return success;
}