File: sources.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 (945 lines) | stat: -rw-r--r-- 26,778 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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/* sources.c:
 * ----------
 * 
 * Source file management routines for the GUI.  Provides the ability to
 * add files to the list, load files, and display within a curses window.
 * Files are buffered in memory when they are displayed, and held in
 * memory for the duration of execution.  If memory consumption becomes a
 * problem, this can be optimized to unload files which have not been
 * displayed recently, or only load portions of large files at once. (May
 * affect syntax hilighting.)
 *
 */

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

/* System Includes */
#if HAVE_MATH_H
#include <math.h>
#endif

#if HAVE_STDIO_H
#include <stdio.h>
#endif /* HAVE_STDIO_H */

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

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

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif

#if HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif

#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#if HAVE_CTYPE_H
#include <ctype.h>
#endif

/* Local Includes */
#include "highlight.h"
#include "sources.h"
#include "cgdb.h"
#include "logo.h"
#include "sys_util.h"
#include "cgdbrc.h"
#include "highlight_groups.h"

int sources_syntax_on = 1;

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

/* verify_file_exists: Checks to see if a file exists
 * -------------------
 *
 * Return Value: 0 if does not exist, 1 if exists 
 */
static int verify_file_exists(const char *path)
{
    struct stat st;

    /* Check for read permission of file, already exists */
    if (stat(path, &st) == -1)
        return 0;

    return 1;
}

/* get_relative_node:  Returns a pointer to the node that matches the 
 * ------------------  given relative path.
 
 *   lpath:  Full path to source file
 *
 * Return Value:  Pointer to the matching node, or NULL if not found.
 */
static struct list_node *get_relative_node(struct sviewer *sview,
        const char *lpath)
{

    struct list_node *cur;

    for (cur = sview->list_head; cur != NULL; cur = cur->next)
        if (cur->lpath)
            if (strcmp(lpath, cur->lpath) == 0)
                return cur;

    return NULL;
}

/* get_node:  Returns a pointer to the node that matches the given path.
 * ---------
 
 *   path:  Full path to source file
 *
 * Return Value:  Pointer to the matching node, or NULL if not found.
 */
static struct list_node *get_node(struct sviewer *sview, const char *path)
{
    struct list_node *cur;

    for (cur = sview->list_head; cur != NULL; cur = cur->next)
        if (cur->path)
            if (strcmp(path, cur->path) == 0)
                return cur;

    return NULL;
}

/**
 * Get's the timestamp of a particular file.
 *
 * \param path
 * The path to the file to get the timestamp of
 *
 * \param timestamp
 * The timestamp of the file, or 0 on error.
 * 
 * \return
 * 0 on success, -1 on error.
 */
static int get_timestamp(const char *path, time_t * timestamp)
{
    struct stat s;
    int val;

    if (!path)
        return -1;

    if (!timestamp)
        return -1;

    *timestamp = 0;

    val = stat(path, &s);

    if (val)                    /* Error on timestamp */
        return -1;

    *timestamp = s.st_mtime;

    return 0;
}

static int release_file_buffer(struct buffer *buf)
{
    int i;

    /* Nothing to free */
    if (!buf)
        return 0;

    for (i = 0; i < buf->length; ++i) {
        free(buf->tlines[i]);
        buf->tlines[i] = NULL;
    }

    free(buf->tlines);
    buf->tlines = NULL;
    buf->length = 0;
    buf->cur_line = NULL;
    buf->max_width = 0;
    free(buf->breakpts);
    buf->breakpts = NULL;

    return 0;
}

/** 
 * Remove's the memory related to a file.
 *
 * \param node
 * The node who's file buffer data needs to be freed.
 *
 * \return
 * 0 on success, or -1 on error.
 */
static int release_file_memory(struct list_node *node)
{
    if (!node)
        return -1;

    /* Free the buffer */
    if (release_file_buffer(&node->buf) == -1)
        return -1;

    if (release_file_buffer(&node->orig_buf) == -1)
        return -1;

    return 0;
}

/* load_file:  Loads the file in the list_node into its memory buffer.
 * ----------
 *
 *   node:  The list node to work on
 *
 * Return Value:  Zero on success, non-zero on error.
 */
static int load_file(struct list_node *node)
{
    FILE *file;
    char line[MAX_LINE];
    int i;

    node->buf.length = 0;
    node->buf.tlines = NULL;
    node->buf.breakpts = NULL;
    node->buf.cur_line = NULL;
    node->buf.max_width = 0;

    /* Open file and save in original buffer.
     * I am not sure if this should be done this way in the future.
     * Maybe this data should be recieved from flex.
     */
    node->orig_buf.length = 0;
    node->orig_buf.tlines = NULL;
    node->orig_buf.breakpts = NULL;
    node->orig_buf.cur_line = NULL;
    node->orig_buf.max_width = 0;

    /* Stat the file to get the timestamp */
    if (get_timestamp(node->path, &(node->last_modification)) == -1)
        return 2;

    if (!(file = fopen(node->path, "r")))
        return 1;

    while (!feof(file)) {
        if (fgets(line, MAX_LINE, file)) {
            int length = strlen(line);

            if (length > 0) {
                if (line[length - 1] == '\n')
                    line[length - 1] = 0;
                if (line[length - 1] == '\r')
                    line[length - 1] = 0;
            }
            if (strlen(line) > node->orig_buf.max_width)
                node->orig_buf.max_width = strlen(line);

            /* Inefficient - Reallocates memory at each line */
            node->orig_buf.length++;
            node->orig_buf.tlines = realloc(node->orig_buf.tlines,
                    sizeof (char *) * node->orig_buf.length);
            node->orig_buf.tlines[node->orig_buf.length - 1] = strdup(line);
        }
    }

    fclose(file);

    node->language = tokenizer_get_default_file_type(strrchr(node->path, '.'));

    /* Add the highlighted lines */
    if (has_colors()) {
        highlight(node);
    } else {
        /* 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]);
    }

    /* Allocate the breakpoints array */
    node->buf.breakpts = malloc(sizeof (char) * node->buf.length);
    for (i = 0; i < node->buf.length; i++)
        node->buf.breakpts[i] = 0;

    return 0;
}

/* draw_current_line:  Draws the currently executing source line on the screen
 * ------------------  including the user-selected marker (arrow, highlight,
 *                     etc) indicating this is the executing line.
 *
 *   sview:  The current source viewer
 *   line:   The line number
 *   lwidth: The width of the line number, used to limit printing to the width
 *           of the screen.  Kinda ugly.
 */
static void draw_current_line(struct sviewer *sview, int line, int lwidth)
{

    int height = 0;             /* Height of curses window */
    int width = 0;              /* Width of curses window */
    int i = 0, j = 0;           /* Iterators */
    struct buffer *buf = NULL;  /* Pointer to the source buffer */
    char *text = NULL;          /* The current line (highlighted) */
    char *otext = NULL;         /* The current line (unhighlighted) */
    unsigned int length = 0;    /* Length of the line */
    int column_offset = 0;      /* Text to skip due to arrow */
    int arrow_attr;
    int highlight_attr;
    enum ArrowStyle config_arrowstyle =
            cgdbrc_get(CGDBRC_ARROWSTYLE)->variant.arrow_style;
    int highlight_tabstop = cgdbrc_get(CGDBRC_TABSTOP)->variant.int_val;

    if (hl_groups_get_attr(hl_groups_instance, HLG_ARROW, &arrow_attr) == -1)
        return;

    if (hl_groups_get_attr(hl_groups_instance, HLG_LINE_HIGHLIGHT,
                    &highlight_attr) == -1)
        return;

    /* Initialize height and width */
    getmaxyx(sview->win, height, width);

    /* If syntax highlighting is on, point to the colored buffer. */
    if (sources_syntax_on) {
        buf = &(sview->cur->buf);
    } else {
        buf = &(sview->cur->orig_buf);
    }

    /* If the current selected line is the line executing, use cur_line */
    if (line == sview->cur->sel_line && buf->cur_line != NULL) {
        text = buf->cur_line;
    } else {
        text = buf->tlines[line];
    }
    otext = sview->cur->orig_buf.tlines[line];
    length = strlen(otext);

    /* Draw the appropriate arrow, if applicable */
    switch (config_arrowstyle) {

        case ARROWSTYLE_SHORT:

            wattron(sview->win, arrow_attr);
            waddch(sview->win, ACS_LTEE);
            waddch(sview->win, '>');
            wattroff(sview->win, arrow_attr);
            break;

        case ARROWSTYLE_LONG:

            wattron(sview->win, arrow_attr);
            waddch(sview->win, ACS_LTEE);

            /* Compute the length of the arrow, respecting tab stops, etc. */
            for (i = 0; i < length - 1 && isspace(otext[i]); i++) {

                /* Oh so cryptic */
                int offset = otext[i] != '\t' ? 1 :
                        highlight_tabstop - (column_offset % highlight_tabstop);

                if (!isspace(otext[i + 1])) {
                    offset--;
                }

                column_offset += offset;
            }
            column_offset -= sview->cur->sel_col;
            if (column_offset < 0) {
                column_offset = 0;
            }

            /* Now actually draw the arrow */
            for (j = 0; j < column_offset; j++) {
                waddch(sview->win, ACS_HLINE);
            }

            waddch(sview->win, '>');
            wattroff(sview->win, arrow_attr);
            break;

        case ARROWSTYLE_HIGHLIGHT:
            waddch(sview->win, VERT_LINE);
            waddch(sview->win, ' ');

            wattron(sview->win, highlight_attr);
            for (i = 0; i < width - lwidth - 2; i++) {
                if (i < length) {
                    waddch(sview->win, otext[i]);
                } else {
                    waddch(sview->win, ' ');
                }
            }
            wattroff(sview->win, highlight_attr);

            return;
    }

    /* Finally, print the source line */
    hl_wprintw(sview->win, text, width - lwidth - 2,
            sview->cur->sel_col + column_offset);
}

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

/* Descriptive comments found in header file: sources.h */

struct sviewer *source_new(int pos_r, int pos_c, int height, int width)
{
    struct sviewer *rv;

    /* Allocate a new structure */
    if ((rv = malloc(sizeof (struct sviewer))) == NULL)
        return NULL;

    /* Initialize the structure */
    rv->win = newwin(height, width, pos_r, pos_c);
    rv->cur = NULL;
    rv->list_head = NULL;

    return rv;
}

int source_add(struct sviewer *sview, const char *path)
{
    struct list_node *new_node;

    new_node = malloc(sizeof (struct list_node));
    new_node->path = strdup(path);
    new_node->lpath = NULL;
    new_node->buf.length = 0;
    new_node->buf.tlines = NULL;    /* This signals an empty buffer */
    new_node->sel_line = 0;
    new_node->sel_col = 0;
    new_node->sel_col_rbeg = 0;
    new_node->sel_col_rend = 0;
    new_node->sel_rline = 0;
    new_node->exe_line = 0;
    new_node->last_modification = 0;    /* No timestamp yet */

    if (sview->list_head == NULL) {
        /* List is empty, this is the first node */
        new_node->next = NULL;
        sview->list_head = new_node;
    } else {
        /* Insert at the front of the list (easy) */
        new_node->next = sview->list_head;
        sview->list_head = new_node;
    }

    return 0;
}

int source_set_relative_path(struct sviewer *sview,
        const char *path, const char *lpath)
{

    struct list_node *node = sview->list_head;

    while (node != NULL) {
        if (strcmp(node->path, path) == 0) {
            node->lpath = strdup(lpath);
            return 0;
        }

        node = node->next;
    }

    return -1;
}

int source_del(struct sviewer *sview, const char *path)
{
    struct list_node *cur;
    struct list_node *prev = NULL;
    int i;

    /* Find the target node */
    for (cur = sview->list_head; cur != NULL; cur = cur->next) {
        if (strcmp(path, cur->path) == 0)
            break;
        prev = cur;
    }

    if (cur == NULL)
        return 1;               /* Node not found */

    /* Release file buffer, if one is in memory */
    if (cur->buf.tlines) {
        for (i = 0; i < cur->buf.length; i++) {
            free(cur->buf.tlines[i]);
            if (cur->buf.cur_line) {
                free(cur->buf.cur_line);
                cur->buf.cur_line = NULL;
            }
            free(cur->orig_buf.tlines[i]);
        }
    }
    free(cur->buf.tlines);
    free(cur->orig_buf.tlines);

    /* Release the breakpoints */
    if (cur->buf.breakpts) {
        free(cur->buf.breakpts);
        cur->buf.breakpts = NULL;
    }

    /* Release file name */
    free(cur->path);
    cur->path = NULL;

    /* Release local file name */
    if (cur->lpath) {
        free(cur->lpath);
        cur->lpath = NULL;
    }

    /* Remove link from list */
    if (cur == sview->list_head)
        sview->list_head = sview->list_head->next;
    else
        prev->next = cur->next;

    /* Free the node */
    free(cur);

    return 0;
}

int source_length(struct sviewer *sview, const char *path)
{
    struct list_node *cur = get_node(sview, path);

    if (!cur) {
        /* Load the file if it's not already */
        if (!cur->buf.tlines && load_file(cur))
            return -1;
    }

    return cur->buf.length;
}

char *source_current_file(struct sviewer *sview, char *path)
{
    if (sview == NULL || sview->cur == NULL)
        return NULL;

    strcpy(path, sview->cur->path);
    return path;
}

int source_display(struct sviewer *sview, int focus)
{
    char fmt[5];
    int width, height;
    int lwidth;
    int line;
    int i;
    int attr = 0, sellineno;

    if (hl_groups_get_attr(hl_groups_instance, HLG_SELECTED_LINE_NUMBER,
                    &sellineno) == -1)
        return -1;

    /* Check that a file is loaded */
    if (sview->cur == NULL || sview->cur->buf.tlines == NULL) {
        logo_display(sview->win);
        wrefresh(sview->win);
        return 0;
    }

    /* Make sure cursor is visible */
    if (focus)
        curs_set(1);
    else
        curs_set(0);

    /* Initialize variables */
    getmaxyx(sview->win, height, width);

    /* Set starting line number (center source file if it's small enough) */
    if (sview->cur->buf.length < height)
        line = (sview->cur->buf.length - height) / 2;
    else {
        line = sview->cur->sel_line - height / 2;
        if (line > sview->cur->buf.length - height)
            line = sview->cur->buf.length - height;
        else if (line < 0)
            line = 0;
    }

    /* Print 'height' lines of the file, starting at 'line' */
    lwidth = (int) log10(sview->cur->buf.length) + 1;
    sprintf(fmt, "%%%dd", lwidth);

    for (i = 0; i < height; i++, line++) {
        wmove(sview->win, i, 0);
        if (has_colors()) {
            /* Outside of file, just finish drawing the vertical line */
            if (line < 0 || line >= sview->cur->buf.length) {
                int j;

                for (j = 1; j < lwidth; j++)
                    waddch(sview->win, ' ');
                waddch(sview->win, '~');
                if (focus)
                    wattron(sview->win, A_BOLD);
                waddch(sview->win, VERT_LINE);
                if (focus)
                    wattroff(sview->win, A_BOLD);
                for (j = 2 + lwidth; j < width; j++)
                    waddch(sview->win, ' ');

                /* Mark the current line with an arrow */
            } else if (line == sview->cur->exe_line) {
                switch (sview->cur->buf.breakpts[line]) {
                    case 0:
                        if (hl_groups_get_attr(hl_groups_instance, HLG_ARROW,
                                        &attr) == -1)
                            return -1;
                        break;
                    case 1:
                        if (hl_groups_get_attr(hl_groups_instance,
                                        HLG_ENABLED_BREAKPOINT, &attr) == -1)
                            return -1;
                        break;
                    case 2:
                        if (hl_groups_get_attr(hl_groups_instance,
                                        HLG_DISABLED_BREAKPOINT, &attr) == -1)
                            return -1;
                        break;
                }
                wattron(sview->win, attr);
                wprintw(sview->win, fmt, line + 1);
                wattroff(sview->win, attr);

                draw_current_line(sview, line, lwidth);

                /* Look for breakpoints */
            } else if (sview->cur->buf.breakpts[line]) {
                if (sview->cur->buf.breakpts[line] == 1) {
                    if (hl_groups_get_attr(hl_groups_instance,
                                    HLG_ENABLED_BREAKPOINT, &attr) == -1)
                        return -1;
                } else {
                    if (hl_groups_get_attr(hl_groups_instance,
                                    HLG_DISABLED_BREAKPOINT, &attr) == -1)
                        return -1;
                }
                wattron(sview->win, attr);
                wprintw(sview->win, fmt, line + 1);
                wattroff(sview->win, attr);
                if (focus)
                    wattron(sview->win, A_BOLD);
                waddch(sview->win, VERT_LINE);
                if (focus)
                    wattroff(sview->win, A_BOLD);
                waddch(sview->win, ' ');

                /* I know this is rediculous, it needs to be reimplemented */
                if (sources_syntax_on) {
                    if (line == sview->cur->sel_line &&
                            sview->cur->buf.cur_line != NULL) {
                        hl_wprintw(sview->win, sview->cur->buf.cur_line,
                                width - lwidth - 2, sview->cur->sel_col);

                    } else {
                        hl_wprintw(sview->win, sview->cur->buf.tlines[line],
                                width - lwidth - 2, sview->cur->sel_col);
                    }
                } else {
                    if (line == sview->cur->sel_line &&
                            sview->cur->buf.cur_line != NULL) {
                        hl_wprintw(sview->win, sview->cur->orig_buf.cur_line,
                                width - lwidth - 2, sview->cur->sel_col);

                    } else {
                        hl_wprintw(sview->win,
                                sview->cur->orig_buf.tlines[line],
                                width - lwidth - 2, sview->cur->sel_col);
                    }
                }
            }
            /* Ordinary lines */
            else {
                if (focus && sview->cur->sel_line == line)
                    wattron(sview->win, sellineno);

                wprintw(sview->win, fmt, line + 1);

                if (focus && sview->cur->sel_line == line)
                    wattroff(sview->win, sellineno);

                if (focus)
                    wattron(sview->win, A_BOLD);
                waddch(sview->win, VERT_LINE);
                if (focus)
                    wattroff(sview->win, A_BOLD);
                waddch(sview->win, ' ');

                /* I know this is rediculous, it needs to be reimplemented */
                if (sources_syntax_on) {
                    /* No special line information */
                    if (line == sview->cur->sel_line &&
                            sview->cur->buf.cur_line != NULL) {
                        hl_wprintw(sview->win, sview->cur->buf.cur_line,
                                width - lwidth - 2, sview->cur->sel_col);

                    } else {
                        hl_wprintw(sview->win, sview->cur->buf.tlines[line],
                                width - lwidth - 2, sview->cur->sel_col);
                    }
                } else {
                    /* No special line information */
                    if (line == sview->cur->sel_line &&
                            sview->cur->buf.cur_line != NULL) {
                        hl_wprintw(sview->win, sview->cur->orig_buf.cur_line,
                                width - lwidth - 2, sview->cur->sel_col);

                    } else {
                        hl_wprintw(sview->win,
                                sview->cur->orig_buf.tlines[line],
                                width - lwidth - 2, sview->cur->sel_col);
                    }
                }
            }
        } else {
            wprintw(sview->win, "%s\n", sview->cur->buf.tlines[line]);
        }
    }

    wmove(sview->win, height - (line - sview->cur->sel_line), lwidth + 2);
    wrefresh(sview->win);

    return 0;
}

void source_move(struct sviewer *sview,
        int pos_r, int pos_c, int height, int width)
{
    delwin(sview->win);
    sview->win = newwin(height, width, pos_r, pos_c);
    wclear(sview->win);
}

void source_vscroll(struct sviewer *sview, int offset)
{
    if (sview->cur) {
        sview->cur->sel_line += offset;
        if (sview->cur->sel_line < 0)
            sview->cur->sel_line = 0;
        if (sview->cur->sel_line >= sview->cur->buf.length)
            sview->cur->sel_line = sview->cur->buf.length - 1;

        sview->cur->sel_rline = sview->cur->sel_line;
    }
}

void source_hscroll(struct sviewer *sview, int offset)
{
    int lwidth;
    int max_width;

    if (sview->cur) {
        lwidth = (int) log10(sview->cur->buf.length) + 1;
        max_width = sview->cur->buf.max_width - COLS + lwidth + 6;

        sview->cur->sel_col += offset;
        if (sview->cur->sel_col > max_width)
            sview->cur->sel_col = max_width;
        if (sview->cur->sel_col < 0)
            sview->cur->sel_col = 0;
    }
}

void source_set_sel_line(struct sviewer *sview, int line)
{
    if (sview->cur) {
        /* Set line (note correction for 0-based line counting) */
        sview->cur->sel_line = line - 1;
        if (sview->cur->sel_line < 0)
            sview->cur->sel_line = 0;
        if (sview->cur->sel_line >= sview->cur->buf.length)
            sview->cur->sel_line = sview->cur->buf.length - 1;

        sview->cur->sel_rline = sview->cur->sel_line;
    }
}

int source_set_exec_line(struct sviewer *sview, const char *path, int line)
{
    if (path && !verify_file_exists(path))
        return 5;

    /* Locate node, if path has changed */
    if (path != NULL && !(sview->cur = get_node(sview, path))) {
        /* Not found -- attempt to add */
        if (source_add(sview, path))
            return 1;
        else if (!(sview->cur = get_node(sview, path)))
            return 2;
    } else if (path == NULL && sview->cur == NULL)
        return 3;

    /* Buffer the file if it's not already */
    if (!sview->cur->buf.tlines && load_file(sview->cur))
        return 4;

    /* Update line, if set */
    if (line--) {
        /* Check bounds of line */
        if (line < 0)
            line = 0;
        if (line >= sview->cur->buf.length)
            line = sview->cur->buf.length - 1;
        sview->cur->sel_line = sview->cur->exe_line = line;
    }

    return 0;
}

void source_free(struct sviewer *sview)
{
    /* Free all file buffers */
    while (sview->list_head != NULL)
        source_del(sview, sview->list_head->path);

    delwin(sview->win);
}

void source_search_regex_init(struct sviewer *sview)
{
    if (sview == NULL || sview->cur == NULL)
        return;

    /* Start from beginning of line if not at same line */
    if (sview->cur->sel_rline != sview->cur->sel_line) {
        sview->cur->sel_col_rend = 0;
        sview->cur->sel_col_rbeg = 0;
    }

    /* Start searching at the beginning of the selected line */
    sview->cur->sel_rline = sview->cur->sel_line;
}

int source_search_regex(struct sviewer *sview,
        const char *regex, int opt, int direction, int icase)
{

    if (sview == NULL || sview->cur == NULL || regex == NULL ||
            strlen(regex) == 0) {

        if (sview && sview->cur)
            sview->cur->buf.cur_line = NULL;
        return -1;
    }

    return hl_regex(regex,
            (const char **) sview->cur->buf.tlines,
            (const char **) sview->cur->orig_buf.tlines,
            sview->cur->orig_buf.length,
            &sview->cur->buf.cur_line, &sview->cur->sel_line,
            &sview->cur->sel_rline, &sview->cur->sel_col_rbeg,
            &sview->cur->sel_col_rend, opt, direction, icase);
}

void source_disable_break(struct sviewer *sview, const char *path, int line)
{
    struct list_node *node;

    if ((node = get_relative_node(sview, path)) == NULL)
        return;

    if (node->buf.tlines == NULL)
        if (load_file(node))
            return;

    if (line > 0 && line <= node->buf.length)
        node->buf.breakpts[line - 1] = 2;
}

void source_enable_break(struct sviewer *sview, const char *path, int line)
{
    struct list_node *node;

    if ((node = get_relative_node(sview, path)) == NULL)
        return;

    if (node->buf.tlines == NULL)
        if (load_file(node))
            return;

    if (line > 0 && line <= node->buf.length) {
        node->buf.breakpts[line - 1] = 1;
    }
}

void source_clear_breaks(struct sviewer *sview)
{
    struct list_node *node;

    for (node = sview->list_head; node != NULL; node = node->next)
        memset(node->buf.breakpts, 0, node->buf.length);

}

int source_reload(struct sviewer *sview, const char *path, int force)
{
    time_t timestamp;
    struct list_node *cur;
    struct list_node *prev = NULL;
    int auto_source_reload =
            cgdbrc_get(CGDBRC_AUTOSOURCERELOAD)->variant.int_val;

    if (!path)
        return -1;

    if (get_timestamp(path, &timestamp) == -1)
        return -1;

    /* Find the target node */
    for (cur = sview->list_head; cur != NULL; cur = cur->next) {
        if (strcmp(path, cur->path) == 0)
            break;
        prev = cur;
    }

    if (cur == NULL)
        return 1;               /* Node not found */

    if ((auto_source_reload || force) && cur->last_modification < timestamp) {

        if (release_file_memory(sview->cur) == -1)
            return -1;

        if (load_file(cur))
            return -1;
    }

    return 0;
}