File: rline.cpp

package info (click to toggle)
cgdb 0.8.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,468 kB
  • sloc: cpp: 12,169; ansic: 10,042; sh: 4,383; exp: 640; makefile: 197
file content (497 lines) | stat: -rw-r--r-- 12,278 bytes parent folder | download | duplicates (2)
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
#include <stretchy.h>
#include <sys_util.h>
#include "rline.h"
#include "fork_util.h"

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

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

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

#if HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif /* HAVE_SYS_IOCTL_H */

#if HAVE_TERMIOS_H
#include <termios.h>
#endif /* HAVE_TERMIOS_H */

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

/* includes {{{*/

#ifdef HAVE_LIBREADLINE

#if defined(HAVE_READLINE_READLINE_H)
#include <readline/readline.h>
#elif defined(HAVE_READLINE_H)
#include <readline.h>
#endif /* !defined(HAVE_READLINE_H) */

#if defined(HAVE_READLINE_HISTORY_H)
#include <readline/history.h>
#elif defined(HAVE_HISTORY_H)
#include <history.h>
#endif /* defined(HAVE_READLINE_HISTORY_H) */

#endif /* HAVE_LIBREADLINE */

/* }}}*/

/* Our array of completion strings and current index */
static int completions_index = 0;
static char **completions_array = NULL;
static int completions_array_size = 0;

struct rline {
    /* The input to readline. Writing to this, writes to readline. */
    FILE *input;

    /* The output of readline. When readline writes data, it goes to this
     * descritpor. Thus, reading from this, get's readline's output. */
    FILE *output;

    /** Master/Slave PTY used to keep readline off of stdin/stdout. */
    pty_pair_ptr pty_pair;

    /* The user defined tab completion function */
    completion_cb *tab_completion;

    /* The user defined tab completion function */
    rl_command_func_t *rline_rl_last_func;

    /* The value of rl_completion_query_items before its set to -1.
     * It is set to -1 so that readline will not attempt to ask "y or no",
     * since that particular functionality of readline does not work with
     * the alternative interface. */
    int rline_rl_completion_query_items;

};

static void custom_deprep_term_function()
{
}

/* Createing and Destroying a librline context. {{{*/
struct rline *rline_initialize(command_cb * command,
        completion_cb * completion, const char *TERM)
{
    struct rline *rline = (struct rline *) malloc(sizeof (struct rline));
    static char word_break_chars[] = " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";

    if (!rline)
        return NULL;

    /* Initialize each member variable */
    rline->input = NULL;
    rline->output = NULL;

    rline->pty_pair = pty_pair_create();
    if (!rline->pty_pair) {
        return NULL;
    }

    int slavefd = pty_pair_get_masterfd(rline->pty_pair);

    rline->input = fdopen(slavefd, "r");
    if (!rline->input) {
        rline_shutdown(rline);
        return NULL;
    }

    rline->output = fdopen(slavefd, "w");
    if (!rline->output) {
        rline_shutdown(rline);
        return NULL;
    }

    rline->tab_completion = completion;
    rline->rline_rl_last_func = NULL;
    rline->rline_rl_completion_query_items = rl_completion_query_items;

    rl_readline_name = "cgdb";
    rl_instream = rline->input;
    rl_outstream = rline->output;

    /* Tell readline not to put the initial prompt */
    rl_already_prompted = 1;

    /* Tell readline not to catch signals */
    rl_catch_signals = 0;
    rl_catch_sigwinch = 0;

    /* Tell readline what the prompt is if it needs to put it back */
    rl_callback_handler_install("(gdb) ", command);
    rl_bind_key('\t', completion);

    /* Set the terminal type to dumb so the output of readline can be
     * understood by tgdb */
    if (rl_reset_terminal(TERM) == -1) {
        rline_shutdown(rline);
        return NULL;
    }

    /* For some reason, readline can not deprep the terminal.
     * However, it doesn't matter because no other application is working on
     * the terminal besides readline */
    rl_deprep_term_function = custom_deprep_term_function;

    /* These variables are here to make sure readline doesn't 
     * attempt to query the user to determine if it wants more input.
     */
    rl_completion_query_items = -1;
    rl_variable_bind("page-completions", "0");
    rl_completer_word_break_characters = word_break_chars;
    rl_completer_quote_characters = "'";

    return rline;
}

static void rline_free_completions()
{
    /* Set and index to 0. */
    delete [] completions_array;
    completions_array = NULL;
    completions_index = 0;
    completions_array_size = 0;
}

int rline_shutdown(struct rline *rline)
{
    if (!rline)
        return -1;              /* Should this be OK? */

    rline_free_completions();

    if (rline->input)
        fclose(rline->input);

    if (rline->output)
        fclose(rline->output);

    pty_pair_destroy(rline->pty_pair);

    free(rline);
    rline = NULL;

    return 0;
}

/*@}*/
/* }}}*/

/* Reading and Writing the librline context. {{{*/
int rline_read_history(struct rline *rline, const char *file)
{
    if (!rline)
        return -1;

    using_history();
    read_history(file);
    history_set_pos(history_length);

    return 0;
}

int rline_write_history(struct rline *rline, const char *file)
{
    if (!rline)
        return -1;

    write_history(file);

    return 0;
}

/*@}*/
/* }}}*/

/* Functional commands {{{*/

int rline_set_prompt(struct rline *rline, const char *prompt)
{
    if (!rline)
        return -1;

    rl_set_prompt(prompt);

    return 0;
}

int rline_clear(struct rline *rline)
{
    if (!rline)
        return -1;

    /* Clear whatever readline has in it's buffer. */
    rl_point = 0;
    rl_end = 0;
    rl_mark = 0;
    rl_delete_text(0, rl_end);

    return 0;
}

int rline_add_history(struct rline *rline, const char *line)
{
    if (!rline)
        return -1;

    if (!line)
        return -1;

    add_history(line);

    return 0;
}

int rline_get_prompt(struct rline *rline, char **prompt)
{
    if (!rline)
        return -1;

    if (!prompt)
        return -1;

    *prompt = rl_prompt;

    return 0;
}

int rline_get_current_line(struct rline *rline, char **current_line)
{
    if (!rline)
        return -1;

    if (!current_line)
        return -1;

    *current_line = rl_line_buffer;

    return 0;
}

int rline_rl_forced_update_display(struct rline *rline)
{
    if (!rline)
        return -1;

    rl_forced_update_display();

    return 0;
}

int rline_rl_callback_read_char(struct rline *rline)
{
    if (!rline)
        return -1;

    /* Capture the last function used here.  */
    rline->rline_rl_last_func = rl_last_func;

    rl_callback_read_char();

    return 0;
}

/**
 * Return to readline a possible completion.
 *
 * \param text
 * The text that is being completed. It can be a subset of the full current 
 * line (rl_line_buffer) at the prompt.
 *
 * \param matches
 * The current number of matches so far
 *
 * \return
 * A possible match, or NULL if none left.
 */
char *rline_rl_completion_entry_function(const char *text, int matches)
{
    if (completions_index < completions_array_size)
    {
        /**
         * 'local' is a possible completion. 'text' is the data to be completed.
         * 'word' is the current possible match started off at the same point
         * in local, that text is started in rl_line_buffer.
         *
         * In C++ if you do "b 'classname::functionam<Tab>". This will complete
         * the line like "b 'classname::functioname'".
         */
        char *local = completions_array[completions_index++];
        char *word = local + rl_point - strlen(text);

        return strdup(word);
    }

    return NULL;
}

/* This is necessary because we want to make what the user has typed
 * against a list of possibilities. For example, if the user has typed
 * 'b ma', the completion possibilities should at least have 'b main'.
 * The default readline word break includes a ' ', which would not
 * result in 'b main' as the completion result.
 */
static char *rline_rl_cpvfunc_t(void)
{
    static char buf[] = "";
    return buf;
}

int rline_rl_complete(struct rline *rline, char **completions,
        display_callback display_cb)
{
    int size;
    int key;
    rl_command_func_t *compare_func = NULL;

    if (!rline)
        return -1;

    /* Currently, if readline output's the tab completion to rl_outstream,
     * it will fill the pty between it and CGDB and will cause CGDB to hang. */
    if (!display_cb)
        return -1;

    size = sbcount(completions);
    if (size == 0) {
        rl_completion_word_break_hook = NULL;
        rl_completion_entry_function = NULL;
    } else {
        completions_index = 0;
        completions_array_size = size;
        completions_array = new char*[size];

        for (int i = 0; i < size; i++)
            completions_array[i] = completions[i];

        rl_completion_word_break_hook = rline_rl_cpvfunc_t;
        rl_completion_entry_function = rline_rl_completion_entry_function;
    }

    rl_completion_display_matches_hook = display_cb;

    /* This is probably a hack, however it works for now.
     *
     * Basically, rl_complete is working fine. After the call to rl_complete,
     * rl_line_buffer contains the proper data. However, the CGDB main loop
     * always call rline_rl_forced_update_display, which in the case of tab 
     * completion does this, (gdb) b ma(gdb) b main
     *
     * Normally, this works fine because the user hits '\n', which puts the prompt
     * on the next line. In this case, the user hit's \t.
     *
     * In order work around this problem, simply putting the \r should work 
     * for now.
     *
     * This obviously shouldn't be done when readline is doing 
     *    `?' means list the possible completions.
     * style completion. Because that actuall does list all of the values on
     * a different line. In this situation the \r goes after the completion
     * is done, since only the current prompt is on that line.
     */

    /* Another confusing comparison. This checks to see if the last
     * readline function and the current readline function and the 
     * tab completion callback are all the same. This ensures that this
     * is the second time the user hit \t in a row. Instead of simply
     * calling rl_complete_internal, it's better to call, rl_completion_mode
     * because this checks to see what kind of completion should be done.
     */
    if (rline->rline_rl_last_func == rline->tab_completion &&
            rline->rline_rl_last_func == rl_last_func)
        compare_func = rline->tab_completion;

    key = rl_completion_mode(compare_func);

    if (key == TAB)
        fprintf(rline->output, "\r");

    rl_complete_internal(key);

    if (key != TAB)
        fprintf(rline->output, "\r");

    /* Free the current completion array entries */
    rline_free_completions();

    return 0;
}

int rline_resize_terminal_and_redisplay(struct rline *rline, int rows, int cols)
{
    struct winsize size;

    if (!rline)
        return -1;

    size.ws_row = rows;
    size.ws_col = cols;
    size.ws_xpixel = 0;
    size.ws_ypixel = 0;
    ioctl(fileno(rline->input), TIOCSWINSZ, &size);

    rl_resize_terminal();
    return 0;
}

int rline_get_rl_completion_query_items(struct rline *rline)
{
    if (!rline)
        return -1;

    return rline->rline_rl_completion_query_items;
}

int
rline_get_keyseq(struct rline *rline, const char *named_function,
        std::list<std::string> &keyseq)
{
    rl_command_func_t *func;
    char **invoking_keyseqs = NULL;
    char **invoking_keyseqs_cur = NULL;
    char *new_keyseq = NULL;
    int len;

    func = rl_named_function(named_function);
    if (func == 0)
        return 0;

    invoking_keyseqs = rl_invoking_keyseqs(func);
    invoking_keyseqs_cur = invoking_keyseqs;

    while (invoking_keyseqs_cur && (*invoking_keyseqs_cur)) {

        new_keyseq =
                (char *) cgdb_malloc((2 * strlen(*invoking_keyseqs_cur)) + 1);
        if (rl_translate_keyseq(*invoking_keyseqs_cur, new_keyseq, &len)) {
            free(new_keyseq);
            free(*invoking_keyseqs_cur);
            /* Can't do much about readline failing, just continue on. */
            continue;
        }

        keyseq.push_back(new_keyseq);
        free(new_keyseq);

        free(*invoking_keyseqs_cur);
        invoking_keyseqs_cur++;
    }
    free(invoking_keyseqs);

    return 0;
}

/*@}*/
/* }}}*/