OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [completer.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* Line completion stuff for GDB, the GNU debugger.
2
   Copyright 2000, 2001 Free Software Foundation, Inc.
3
 
4
   This file is part of GDB.
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 59 Temple Place - Suite 330,
19
   Boston, MA 02111-1307, USA.  */
20
 
21
#include "defs.h"
22
#include "symtab.h"
23
#include "gdbtypes.h"
24
#include "expression.h"
25
#include "filenames.h"          /* for DOSish file names */
26
 
27
/* FIXME: This is needed because of lookup_cmd_1().
28
   We should be calling a hook instead so we eliminate the CLI dependency. */
29
#include "gdbcmd.h"
30
 
31
/* Needed for rl_completer_word_break_characters() and for
32
   filename_completion_function.  */
33
#include <readline/readline.h>
34
 
35
/* readline defines this.  */
36
#undef savestring
37
 
38
#include "completer.h"
39
 
40
/* Prototypes for local functions */
41
 
42
/* readline uses the word breaks for two things:
43
   (1) In figuring out where to point the TEXT parameter to the
44
   rl_completion_entry_function.  Since we don't use TEXT for much,
45
   it doesn't matter a lot what the word breaks are for this purpose, but
46
   it does affect how much stuff M-? lists.
47
   (2) If one of the matches contains a word break character, readline
48
   will quote it.  That's why we switch between
49
   gdb_completer_word_break_characters and
50
   gdb_completer_command_word_break_characters.  I'm not sure when
51
   we need this behavior (perhaps for funky characters in C++ symbols?).  */
52
 
53
/* Variables which are necessary for fancy command line editing.  */
54
static char *gdb_completer_word_break_characters =
55
" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-";
56
 
57
/* When completing on command names, we remove '-' from the list of
58
   word break characters, since we use it in command names.  If the
59
   readline library sees one in any of the current completion strings,
60
   it thinks that the string needs to be quoted and automatically supplies
61
   a leading quote. */
62
static char *gdb_completer_command_word_break_characters =
63
" \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,";
64
 
65
/* When completing on file names, we remove from the list of word
66
   break characters any characters that are commonly used in file
67
   names, such as '-', '+', '~', etc.  Otherwise, readline displays
68
   incorrect completion candidates.  */
69
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
70
/* MS-DOS and MS-Windows use colon as part of the drive spec, and most
71
   programs support @foo style response files.  */
72
static char *gdb_completer_file_name_break_characters = " \t\n*|\"';?><@";
73
#else
74
static char *gdb_completer_file_name_break_characters = " \t\n*|\"';:?><";
75
#endif
76
 
77
/* These are used when completing on locations, which can mix file
78
   names and symbol names separated by a colon.  */
79
static char *gdb_completer_loc_break_characters = " \t\n*|\"';:?><,";
80
 
81
/* Characters that can be used to quote completion strings.  Note that we
82
   can't include '"' because the gdb C parser treats such quoted sequences
83
   as strings. */
84
static char *gdb_completer_quote_characters = "'";
85
 
86
/* Accessor for some completer data that may interest other files. */
87
 
88
char *
89
get_gdb_completer_word_break_characters (void)
90
{
91
  return gdb_completer_word_break_characters;
92
}
93
 
94
char *
95
get_gdb_completer_quote_characters (void)
96
{
97
  return gdb_completer_quote_characters;
98
}
99
 
100
/* Complete on filenames.  */
101
char **
102
filename_completer (char *text, char *word)
103
{
104
  int subsequent_name;
105
  char **return_val;
106
  int return_val_used;
107
  int return_val_alloced;
108
 
109
  return_val_used = 0;
110
  /* Small for testing.  */
111
  return_val_alloced = 1;
112
  return_val = (char **) xmalloc (return_val_alloced * sizeof (char *));
113
 
114
  subsequent_name = 0;
115
  while (1)
116
    {
117
      char *p;
118
      p = filename_completion_function (text, subsequent_name);
119
      if (return_val_used >= return_val_alloced)
120
        {
121
          return_val_alloced *= 2;
122
          return_val =
123
            (char **) xrealloc (return_val,
124
                                return_val_alloced * sizeof (char *));
125
        }
126
      if (p == NULL)
127
        {
128
          return_val[return_val_used++] = p;
129
          break;
130
        }
131
      /* We need to set subsequent_name to a non-zero value before the
132
         continue line below, because otherwise, if the first file seen
133
         by GDB is a backup file whose name ends in a `~', we will loop
134
         indefinitely.  */
135
      subsequent_name = 1;
136
      /* Like emacs, don't complete on old versions.  Especially useful
137
         in the "source" command.  */
138
      if (p[strlen (p) - 1] == '~')
139
        continue;
140
 
141
      {
142
        char *q;
143
        if (word == text)
144
          /* Return exactly p.  */
145
          return_val[return_val_used++] = p;
146
        else if (word > text)
147
          {
148
            /* Return some portion of p.  */
149
            q = xmalloc (strlen (p) + 5);
150
            strcpy (q, p + (word - text));
151
            return_val[return_val_used++] = q;
152
            xfree (p);
153
          }
154
        else
155
          {
156
            /* Return some of TEXT plus p.  */
157
            q = xmalloc (strlen (p) + (text - word) + 5);
158
            strncpy (q, word, text - word);
159
            q[text - word] = '\0';
160
            strcat (q, p);
161
            return_val[return_val_used++] = q;
162
            xfree (p);
163
          }
164
      }
165
    }
166
#if 0
167
  /* There is no way to do this just long enough to affect quote inserting
168
     without also affecting the next completion.  This should be fixed in
169
     readline.  FIXME.  */
170
  /* Insure that readline does the right thing
171
     with respect to inserting quotes.  */
172
  rl_completer_word_break_characters = "";
173
#endif
174
  return return_val;
175
}
176
 
177
/* Complete on locations, which might be of two possible forms:
178
 
179
       file:line
180
   or
181
       symbol+offset
182
 
183
   This is intended to be used in commands that set breakpoints etc.  */
184
char **
185
location_completer (char *text, char *word)
186
{
187
  int n_syms = 0, n_files = 0;
188
  char ** fn_list = NULL;
189
  char ** list = NULL;
190
  char *p;
191
  int quote_found = 0;
192
  int quoted = *text == '\'' || *text == '"';
193
  int quote_char = '\0';
194
  char *colon = NULL;
195
  char *file_to_match = NULL;
196
  char *symbol_start = text;
197
  char *orig_text = text;
198
  size_t text_len;
199
 
200
  /* Do we have an unquoted colon, as in "break foo.c::bar"?  */
201
  for (p = text; *p != '\0'; ++p)
202
    {
203
      if (*p == '\\' && p[1] == '\'')
204
        p++;
205
      else if (*p == '\'' || *p == '"')
206
        {
207
          quote_found = *p;
208
          quote_char = *p++;
209
          while (*p != '\0' && *p != quote_found)
210
            {
211
              if (*p == '\\' && p[1] == quote_found)
212
                p++;
213
              p++;
214
            }
215
 
216
          if (*p == quote_found)
217
            quote_found = 0;
218
          else
219
            break;              /* hit the end of text */
220
        }
221
#if HAVE_DOS_BASED_FILE_SYSTEM
222
      /* If we have a DOS-style absolute file name at the beginning of
223
         TEXT, and the colon after the drive letter is the only colon
224
         we found, pretend the colon is not there.  */
225
      else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
226
        ;
227
#endif
228
      else if (*p == ':' && !colon)
229
        {
230
          colon = p;
231
          symbol_start = p + 1;
232
        }
233
      else if (strchr (gdb_completer_word_break_characters, *p))
234
        symbol_start = p + 1;
235
    }
236
 
237
  if (quoted)
238
    text++;
239
  text_len = strlen (text);
240
 
241
  /* Where is the file name?  */
242
  if (colon)
243
    {
244
      char *s;
245
 
246
      file_to_match = (char *) xmalloc (colon - text + 1);
247
      strncpy (file_to_match, text, colon - text + 1);
248
      /* Remove trailing colons and quotes from the file name.  */
249
      for (s = file_to_match + (colon - text);
250
           s > file_to_match;
251
           s--)
252
        if (*s == ':' || *s == quote_char)
253
          *s = '\0';
254
    }
255
  /* If the text includes a colon, they want completion only on a
256
     symbol name after the colon.  Otherwise, we need to complete on
257
     symbols as well as on files.  */
258
  if (colon)
259
    {
260
      list = make_file_symbol_completion_list (symbol_start, word,
261
                                               file_to_match);
262
      xfree (file_to_match);
263
    }
264
  else
265
    {
266
      list = make_symbol_completion_list (symbol_start, word);
267
      /* If text includes characters which cannot appear in a file
268
         name, they cannot be asking for completion on files.  */
269
      if (strcspn (text, gdb_completer_file_name_break_characters) == text_len)
270
        fn_list = make_source_files_completion_list (text, text);
271
    }
272
 
273
  /* How many completions do we have in both lists?  */
274
  if (fn_list)
275
    for ( ; fn_list[n_files]; n_files++)
276
      ;
277
  if (list)
278
    for ( ; list[n_syms]; n_syms++)
279
      ;
280
 
281
  /* Make list[] large enough to hold both lists, then catenate
282
     fn_list[] onto the end of list[].  */
283
  if (n_syms && n_files)
284
    {
285
      list = xrealloc (list, (n_syms + n_files + 1) * sizeof (char *));
286
      memcpy (list + n_syms, fn_list, (n_files + 1) * sizeof (char *));
287
      xfree (fn_list);
288
    }
289
  else if (n_files)
290
    {
291
      /* If we only have file names as possible completion, we should
292
         bring them in sync with what rl_complete expects.  The
293
         problem is that if the user types "break /foo/b TAB", and the
294
         possible completions are "/foo/bar" and "/foo/baz"
295
         rl_complete expects us to return "bar" and "baz", without the
296
         leading directories, as possible completions, because `word'
297
         starts at the "b".  But we ignore the value of `word' when we
298
         call make_source_files_completion_list above (because that
299
         would not DTRT when the completion results in both symbols
300
         and file names), so make_source_files_completion_list returns
301
         the full "/foo/bar" and "/foo/baz" strings.  This produces
302
         wrong results when, e.g., there's only one possible
303
         completion, because rl_complete will prepend "/foo/" to each
304
         candidate completion.  The loop below removes that leading
305
         part.  */
306
      for (n_files = 0; fn_list[n_files]; n_files++)
307
        {
308
          memmove (fn_list[n_files], fn_list[n_files] + (word - text),
309
                   strlen (fn_list[n_files]) + 1 - (word - text));
310
        }
311
      /* Return just the file-name list as the result.  */
312
      list = fn_list;
313
    }
314
  else if (!n_syms)
315
    {
316
      /* No completions at all.  As the final resort, try completing
317
         on the entire text as a symbol.  */
318
      list = make_symbol_completion_list (orig_text, word);
319
    }
320
 
321
  return list;
322
}
323
 
324
/* Here are some useful test cases for completion.  FIXME: These should
325
   be put in the test suite.  They should be tested with both M-? and TAB.
326
 
327
   "show output-" "radix"
328
   "show output" "-radix"
329
   "p" ambiguous (commands starting with p--path, print, printf, etc.)
330
   "p "  ambiguous (all symbols)
331
   "info t foo" no completions
332
   "info t " no completions
333
   "info t" ambiguous ("info target", "info terminal", etc.)
334
   "info ajksdlfk" no completions
335
   "info ajksdlfk " no completions
336
   "info" " "
337
   "info " ambiguous (all info commands)
338
   "p \"a" no completions (string constant)
339
   "p 'a" ambiguous (all symbols starting with a)
340
   "p b-a" ambiguous (all symbols starting with a)
341
   "p b-" ambiguous (all symbols)
342
   "file Make" "file" (word break hard to screw up here)
343
   "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
344
 */
345
 
346
/* Generate completions one by one for the completer.  Each time we are
347
   called return another potential completion to the caller.
348
   line_completion just completes on commands or passes the buck to the
349
   command's completer function, the stuff specific to symbol completion
350
   is in make_symbol_completion_list.
351
 
352
   TEXT is the caller's idea of the "word" we are looking at.
353
 
354
   MATCHES is the number of matches that have currently been collected from
355
   calling this completion function.  When zero, then we need to initialize,
356
   otherwise the initialization has already taken place and we can just
357
   return the next potential completion string.
358
 
359
   LINE_BUFFER is available to be looked at; it contains the entire text
360
   of the line.  POINT is the offset in that line of the cursor.  You
361
   should pretend that the line ends at POINT.
362
 
363
   Returns NULL if there are no more completions, else a pointer to a string
364
   which is a possible completion, it is the caller's responsibility to
365
   free the string.  */
366
 
367
char *
368
line_completion_function (char *text, int matches, char *line_buffer, int point)
369
{
370
  static char **list = (char **) NULL;  /* Cache of completions */
371
  static int index;             /* Next cached completion */
372
  char *output = NULL;
373
  char *tmp_command, *p;
374
  /* Pointer within tmp_command which corresponds to text.  */
375
  char *word;
376
  struct cmd_list_element *c, *result_list;
377
 
378
  if (matches == 0)
379
    {
380
      /* The caller is beginning to accumulate a new set of completions, so
381
         we need to find all of them now, and cache them for returning one at
382
         a time on future calls. */
383
 
384
      if (list)
385
        {
386
          /* Free the storage used by LIST, but not by the strings inside.
387
             This is because rl_complete_internal () frees the strings. */
388
          xfree (list);
389
        }
390
      list = 0;
391
      index = 0;
392
 
393
      /* Choose the default set of word break characters to break completions.
394
         If we later find out that we are doing completions on command strings
395
         (as opposed to strings supplied by the individual command completer
396
         functions, which can be any string) then we will switch to the
397
         special word break set for command strings, which leaves out the
398
         '-' character used in some commands.  */
399
 
400
      rl_completer_word_break_characters =
401
        gdb_completer_word_break_characters;
402
 
403
      /* Decide whether to complete on a list of gdb commands or on symbols. */
404
      tmp_command = (char *) alloca (point + 1);
405
      p = tmp_command;
406
 
407
      strncpy (tmp_command, line_buffer, point);
408
      tmp_command[point] = '\0';
409
      /* Since text always contains some number of characters leading up
410
         to point, we can find the equivalent position in tmp_command
411
         by subtracting that many characters from the end of tmp_command.  */
412
      word = tmp_command + point - strlen (text);
413
 
414
      if (point == 0)
415
        {
416
          /* An empty line we want to consider ambiguous; that is, it
417
             could be any command.  */
418
          c = (struct cmd_list_element *) -1;
419
          result_list = 0;
420
        }
421
      else
422
        {
423
          c = lookup_cmd_1 (&p, cmdlist, &result_list, 1);
424
        }
425
 
426
      /* Move p up to the next interesting thing.  */
427
      while (*p == ' ' || *p == '\t')
428
        {
429
          p++;
430
        }
431
 
432
      if (!c)
433
        {
434
          /* It is an unrecognized command.  So there are no
435
             possible completions.  */
436
          list = NULL;
437
        }
438
      else if (c == (struct cmd_list_element *) -1)
439
        {
440
          char *q;
441
 
442
          /* lookup_cmd_1 advances p up to the first ambiguous thing, but
443
             doesn't advance over that thing itself.  Do so now.  */
444
          q = p;
445
          while (*q && (isalnum (*q) || *q == '-' || *q == '_'))
446
            ++q;
447
          if (q != tmp_command + point)
448
            {
449
              /* There is something beyond the ambiguous
450
                 command, so there are no possible completions.  For
451
                 example, "info t " or "info t foo" does not complete
452
                 to anything, because "info t" can be "info target" or
453
                 "info terminal".  */
454
              list = NULL;
455
            }
456
          else
457
            {
458
              /* We're trying to complete on the command which was ambiguous.
459
                 This we can deal with.  */
460
              if (result_list)
461
                {
462
                  list = complete_on_cmdlist (*result_list->prefixlist, p,
463
                                              word);
464
                }
465
              else
466
                {
467
                  list = complete_on_cmdlist (cmdlist, p, word);
468
                }
469
              /* Insure that readline does the right thing with respect to
470
                 inserting quotes.  */
471
              rl_completer_word_break_characters =
472
                gdb_completer_command_word_break_characters;
473
            }
474
        }
475
      else
476
        {
477
          /* We've recognized a full command.  */
478
 
479
          if (p == tmp_command + point)
480
            {
481
              /* There is no non-whitespace in the line beyond the command.  */
482
 
483
              if (p[-1] == ' ' || p[-1] == '\t')
484
                {
485
                  /* The command is followed by whitespace; we need to complete
486
                     on whatever comes after command.  */
487
                  if (c->prefixlist)
488
                    {
489
                      /* It is a prefix command; what comes after it is
490
                         a subcommand (e.g. "info ").  */
491
                      list = complete_on_cmdlist (*c->prefixlist, p, word);
492
 
493
                      /* Insure that readline does the right thing
494
                         with respect to inserting quotes.  */
495
                      rl_completer_word_break_characters =
496
                        gdb_completer_command_word_break_characters;
497
                    }
498
                  else if (c->enums)
499
                    {
500
                      list = complete_on_enum (c->enums, p, word);
501
                      rl_completer_word_break_characters =
502
                        gdb_completer_command_word_break_characters;
503
                    }
504
                  else
505
                    {
506
                      /* It is a normal command; what comes after it is
507
                         completed by the command's completer function.  */
508
                      if (c->completer == filename_completer)
509
                        {
510
                          /* Many commands which want to complete on
511
                             file names accept several file names, as
512
                             in "run foo bar >>baz".  So we don't want
513
                             to complete the entire text after the
514
                             command, just the last word.  To this
515
                             end, we need to find the beginning of the
516
                             file name by starting at `word' and going
517
                             backwards.  */
518
                          for (p = word;
519
                               p > tmp_command
520
                                 && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
521
                               p--)
522
                            ;
523
                          rl_completer_word_break_characters =
524
                            gdb_completer_file_name_break_characters;
525
                        }
526
                      else if (c->completer == location_completer)
527
                        {
528
                          /* Commands which complete on locations want to
529
                             see the entire argument.  */
530
                          for (p = word;
531
                               p > tmp_command
532
                                 && p[-1] != ' ' && p[-1] != '\t';
533
                               p--)
534
                            ;
535
                        }
536
                      list = (*c->completer) (p, word);
537
                    }
538
                }
539
              else
540
                {
541
                  /* The command is not followed by whitespace; we need to
542
                     complete on the command itself.  e.g. "p" which is a
543
                     command itself but also can complete to "print", "ptype"
544
                     etc.  */
545
                  char *q;
546
 
547
                  /* Find the command we are completing on.  */
548
                  q = p;
549
                  while (q > tmp_command)
550
                    {
551
                      if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_')
552
                        --q;
553
                      else
554
                        break;
555
                    }
556
 
557
                  list = complete_on_cmdlist (result_list, q, word);
558
 
559
                  /* Insure that readline does the right thing
560
                     with respect to inserting quotes.  */
561
                  rl_completer_word_break_characters =
562
                    gdb_completer_command_word_break_characters;
563
                }
564
            }
565
          else
566
            {
567
              /* There is non-whitespace beyond the command.  */
568
 
569
              if (c->prefixlist && !c->allow_unknown)
570
                {
571
                  /* It is an unrecognized subcommand of a prefix command,
572
                     e.g. "info adsfkdj".  */
573
                  list = NULL;
574
                }
575
              else if (c->enums)
576
                {
577
                  list = complete_on_enum (c->enums, p, word);
578
                }
579
              else
580
                {
581
                  /* It is a normal command.  */
582
                  if (c->completer == filename_completer)
583
                    {
584
                      /* See the commentary above about the specifics
585
                         of file-name completion.  */
586
                      for (p = word;
587
                           p > tmp_command
588
                             && strchr (gdb_completer_file_name_break_characters, p[-1]) == NULL;
589
                           p--)
590
                        ;
591
                      rl_completer_word_break_characters =
592
                        gdb_completer_file_name_break_characters;
593
                    }
594
                  else if (c->completer == location_completer)
595
                    {
596
                      for (p = word;
597
                           p > tmp_command
598
                             && p[-1] != ' ' && p[-1] != '\t';
599
                           p--)
600
                        ;
601
                    }
602
                  list = (*c->completer) (p, word);
603
                }
604
            }
605
        }
606
    }
607
 
608
  /* If we found a list of potential completions during initialization then
609
     dole them out one at a time.  The vector of completions is NULL
610
     terminated, so after returning the last one, return NULL (and continue
611
     to do so) each time we are called after that, until a new list is
612
     available. */
613
 
614
  if (list)
615
    {
616
      output = list[index];
617
      if (output)
618
        {
619
          index++;
620
        }
621
    }
622
 
623
#if 0
624
  /* Can't do this because readline hasn't yet checked the word breaks
625
     for figuring out whether to insert a quote.  */
626
  if (output == NULL)
627
    /* Make sure the word break characters are set back to normal for the
628
       next time that readline tries to complete something.  */
629
    rl_completer_word_break_characters =
630
      gdb_completer_word_break_characters;
631
#endif
632
 
633
  return (output);
634
}
635
/* Skip over a possibly quoted word (as defined by the quote characters
636
   and word break characters the completer uses).  Returns pointer to the
637
   location after the "word". */
638
 
639
char *
640
skip_quoted (char *str)
641
{
642
  char quote_char = '\0';
643
  char *scan;
644
 
645
  for (scan = str; *scan != '\0'; scan++)
646
    {
647
      if (quote_char != '\0')
648
        {
649
          /* Ignore everything until the matching close quote char */
650
          if (*scan == quote_char)
651
            {
652
              /* Found matching close quote. */
653
              scan++;
654
              break;
655
            }
656
        }
657
      else if (strchr (gdb_completer_quote_characters, *scan))
658
        {
659
          /* Found start of a quoted string. */
660
          quote_char = *scan;
661
        }
662
      else if (strchr (gdb_completer_word_break_characters, *scan))
663
        {
664
          break;
665
        }
666
    }
667
  return (scan);
668
}
669
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.