completer.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007
  1. /* Line completion stuff for GDB, the GNU debugger.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "filenames.h" /* For DOSish file names. */
  19. #include "language.h"
  20. #include "gdbsupport/gdb_signals.h"
  21. #include "target.h"
  22. #include "reggroups.h"
  23. #include "user-regs.h"
  24. #include "arch-utils.h"
  25. #include "location.h"
  26. #include <algorithm>
  27. #include "linespec.h"
  28. #include "cli/cli-decode.h"
  29. /* FIXME: This is needed because of lookup_cmd_1 (). We should be
  30. calling a hook instead so we eliminate the CLI dependency. */
  31. #include "gdbcmd.h"
  32. /* Needed for rl_completer_word_break_characters and for
  33. rl_filename_completion_function. */
  34. #include "readline/readline.h"
  35. /* readline defines this. */
  36. #undef savestring
  37. #include "completer.h"
  38. /* See completer.h. */
  39. class completion_tracker::completion_hash_entry
  40. {
  41. public:
  42. /* Constructor. */
  43. completion_hash_entry (gdb::unique_xmalloc_ptr<char> name,
  44. gdb::unique_xmalloc_ptr<char> lcd)
  45. : m_name (std::move (name)),
  46. m_lcd (std::move (lcd))
  47. {
  48. /* Nothing. */
  49. }
  50. /* Returns a pointer to the lowest common denominator string. This
  51. string will only be valid while this hash entry is still valid as the
  52. string continues to be owned by this hash entry and will be released
  53. when this entry is deleted. */
  54. char *get_lcd () const
  55. {
  56. return m_lcd.get ();
  57. }
  58. /* Get, and release the name field from this hash entry. This can only
  59. be called once, after which the name field is no longer valid. This
  60. should be used to pass ownership of the name to someone else. */
  61. char *release_name ()
  62. {
  63. return m_name.release ();
  64. }
  65. /* Return true of the name in this hash entry is STR. */
  66. bool is_name_eq (const char *str) const
  67. {
  68. return strcmp (m_name.get (), str) == 0;
  69. }
  70. /* Return the hash value based on the name of the entry. */
  71. hashval_t hash_name () const
  72. {
  73. return htab_hash_string (m_name.get ());
  74. }
  75. private:
  76. /* The symbol name stored in this hash entry. */
  77. gdb::unique_xmalloc_ptr<char> m_name;
  78. /* The lowest common denominator string computed for this hash entry. */
  79. gdb::unique_xmalloc_ptr<char> m_lcd;
  80. };
  81. /* Misc state that needs to be tracked across several different
  82. readline completer entry point calls, all related to a single
  83. completion invocation. */
  84. struct gdb_completer_state
  85. {
  86. /* The current completion's completion tracker. This is a global
  87. because a tracker can be shared between the handle_brkchars and
  88. handle_completion phases, which involves different readline
  89. callbacks. */
  90. completion_tracker *tracker = NULL;
  91. /* Whether the current completion was aborted. */
  92. bool aborted = false;
  93. };
  94. /* The current completion state. */
  95. static gdb_completer_state current_completion;
  96. /* An enumeration of the various things a user might attempt to
  97. complete for a location. If you change this, remember to update
  98. the explicit_options array below too. */
  99. enum explicit_location_match_type
  100. {
  101. /* The filename of a source file. */
  102. MATCH_SOURCE,
  103. /* The name of a function or method. */
  104. MATCH_FUNCTION,
  105. /* The fully-qualified name of a function or method. */
  106. MATCH_QUALIFIED,
  107. /* A line number. */
  108. MATCH_LINE,
  109. /* The name of a label. */
  110. MATCH_LABEL
  111. };
  112. /* Prototypes for local functions. */
  113. /* readline uses the word breaks for two things:
  114. (1) In figuring out where to point the TEXT parameter to the
  115. rl_completion_entry_function. Since we don't use TEXT for much,
  116. it doesn't matter a lot what the word breaks are for this purpose,
  117. but it does affect how much stuff M-? lists.
  118. (2) If one of the matches contains a word break character, readline
  119. will quote it. That's why we switch between
  120. current_language->word_break_characters () and
  121. gdb_completer_command_word_break_characters. I'm not sure when
  122. we need this behavior (perhaps for funky characters in C++
  123. symbols?). */
  124. /* Variables which are necessary for fancy command line editing. */
  125. /* When completing on command names, we remove '-' and '.' from the list of
  126. word break characters, since we use it in command names. If the
  127. readline library sees one in any of the current completion strings,
  128. it thinks that the string needs to be quoted and automatically
  129. supplies a leading quote. */
  130. static const char gdb_completer_command_word_break_characters[] =
  131. " \t\n!@#$%^&*()+=|~`}{[]\"';:?/><,";
  132. /* When completing on file names, we remove from the list of word
  133. break characters any characters that are commonly used in file
  134. names, such as '-', '+', '~', etc. Otherwise, readline displays
  135. incorrect completion candidates. */
  136. /* MS-DOS and MS-Windows use colon as part of the drive spec, and most
  137. programs support @foo style response files. */
  138. static const char gdb_completer_file_name_break_characters[] =
  139. #ifdef HAVE_DOS_BASED_FILE_SYSTEM
  140. " \t\n*|\"';?><@";
  141. #else
  142. " \t\n*|\"';:?><";
  143. #endif
  144. /* Characters that can be used to quote completion strings. Note that
  145. we can't include '"' because the gdb C parser treats such quoted
  146. sequences as strings. */
  147. static const char gdb_completer_quote_characters[] = "'";
  148. /* Accessor for some completer data that may interest other files. */
  149. const char *
  150. get_gdb_completer_quote_characters (void)
  151. {
  152. return gdb_completer_quote_characters;
  153. }
  154. /* This can be used for functions which don't want to complete on
  155. symbols but don't want to complete on anything else either. */
  156. void
  157. noop_completer (struct cmd_list_element *ignore,
  158. completion_tracker &tracker,
  159. const char *text, const char *prefix)
  160. {
  161. }
  162. /* Complete on filenames. */
  163. void
  164. filename_completer (struct cmd_list_element *ignore,
  165. completion_tracker &tracker,
  166. const char *text, const char *word)
  167. {
  168. int subsequent_name;
  169. subsequent_name = 0;
  170. while (1)
  171. {
  172. gdb::unique_xmalloc_ptr<char> p_rl
  173. (rl_filename_completion_function (text, subsequent_name));
  174. if (p_rl == NULL)
  175. break;
  176. /* We need to set subsequent_name to a non-zero value before the
  177. continue line below, because otherwise, if the first file
  178. seen by GDB is a backup file whose name ends in a `~', we
  179. will loop indefinitely. */
  180. subsequent_name = 1;
  181. /* Like emacs, don't complete on old versions. Especially
  182. useful in the "source" command. */
  183. const char *p = p_rl.get ();
  184. if (p[strlen (p) - 1] == '~')
  185. continue;
  186. tracker.add_completion
  187. (make_completion_match_str (std::move (p_rl), text, word));
  188. }
  189. #if 0
  190. /* There is no way to do this just long enough to affect quote
  191. inserting without also affecting the next completion. This
  192. should be fixed in readline. FIXME. */
  193. /* Ensure that readline does the right thing
  194. with respect to inserting quotes. */
  195. rl_completer_word_break_characters = "";
  196. #endif
  197. }
  198. /* The corresponding completer_handle_brkchars
  199. implementation. */
  200. static void
  201. filename_completer_handle_brkchars (struct cmd_list_element *ignore,
  202. completion_tracker &tracker,
  203. const char *text, const char *word)
  204. {
  205. set_rl_completer_word_break_characters
  206. (gdb_completer_file_name_break_characters);
  207. }
  208. /* Find the bounds of the current word for completion purposes, and
  209. return a pointer to the end of the word. This mimics (and is a
  210. modified version of) readline's _rl_find_completion_word internal
  211. function.
  212. This function skips quoted substrings (characters between matched
  213. pairs of characters in rl_completer_quote_characters). We try to
  214. find an unclosed quoted substring on which to do matching. If one
  215. is not found, we use the word break characters to find the
  216. boundaries of the current word. QC, if non-null, is set to the
  217. opening quote character if we found an unclosed quoted substring,
  218. '\0' otherwise. DP, if non-null, is set to the value of the
  219. delimiter character that caused a word break. */
  220. struct gdb_rl_completion_word_info
  221. {
  222. const char *word_break_characters;
  223. const char *quote_characters;
  224. const char *basic_quote_characters;
  225. };
  226. static const char *
  227. gdb_rl_find_completion_word (struct gdb_rl_completion_word_info *info,
  228. int *qc, int *dp,
  229. const char *line_buffer)
  230. {
  231. int scan, end, delimiter, pass_next, isbrk;
  232. char quote_char;
  233. const char *brkchars;
  234. int point = strlen (line_buffer);
  235. /* The algorithm below does '--point'. Avoid buffer underflow with
  236. the empty string. */
  237. if (point == 0)
  238. {
  239. if (qc != NULL)
  240. *qc = '\0';
  241. if (dp != NULL)
  242. *dp = '\0';
  243. return line_buffer;
  244. }
  245. end = point;
  246. delimiter = 0;
  247. quote_char = '\0';
  248. brkchars = info->word_break_characters;
  249. if (info->quote_characters != NULL)
  250. {
  251. /* We have a list of characters which can be used in pairs to
  252. quote substrings for the completer. Try to find the start of
  253. an unclosed quoted substring. */
  254. for (scan = pass_next = 0;
  255. scan < end;
  256. scan++)
  257. {
  258. if (pass_next)
  259. {
  260. pass_next = 0;
  261. continue;
  262. }
  263. /* Shell-like semantics for single quotes -- don't allow
  264. backslash to quote anything in single quotes, especially
  265. not the closing quote. If you don't like this, take out
  266. the check on the value of quote_char. */
  267. if (quote_char != '\'' && line_buffer[scan] == '\\')
  268. {
  269. pass_next = 1;
  270. continue;
  271. }
  272. if (quote_char != '\0')
  273. {
  274. /* Ignore everything until the matching close quote
  275. char. */
  276. if (line_buffer[scan] == quote_char)
  277. {
  278. /* Found matching close. Abandon this
  279. substring. */
  280. quote_char = '\0';
  281. point = end;
  282. }
  283. }
  284. else if (strchr (info->quote_characters, line_buffer[scan]))
  285. {
  286. /* Found start of a quoted substring. */
  287. quote_char = line_buffer[scan];
  288. point = scan + 1;
  289. }
  290. }
  291. }
  292. if (point == end && quote_char == '\0')
  293. {
  294. /* We didn't find an unclosed quoted substring upon which to do
  295. completion, so use the word break characters to find the
  296. substring on which to complete. */
  297. while (--point)
  298. {
  299. scan = line_buffer[point];
  300. if (strchr (brkchars, scan) != 0)
  301. break;
  302. }
  303. }
  304. /* If we are at an unquoted word break, then advance past it. */
  305. scan = line_buffer[point];
  306. if (scan)
  307. {
  308. isbrk = strchr (brkchars, scan) != 0;
  309. if (isbrk)
  310. {
  311. /* If the character that caused the word break was a quoting
  312. character, then remember it as the delimiter. */
  313. if (info->basic_quote_characters
  314. && strchr (info->basic_quote_characters, scan)
  315. && (end - point) > 1)
  316. delimiter = scan;
  317. point++;
  318. }
  319. }
  320. if (qc != NULL)
  321. *qc = quote_char;
  322. if (dp != NULL)
  323. *dp = delimiter;
  324. return line_buffer + point;
  325. }
  326. /* Find the completion word point for TEXT, emulating the algorithm
  327. readline uses to find the word point, using WORD_BREAK_CHARACTERS
  328. as word break characters. */
  329. static const char *
  330. advance_to_completion_word (completion_tracker &tracker,
  331. const char *word_break_characters,
  332. const char *text)
  333. {
  334. gdb_rl_completion_word_info info;
  335. info.word_break_characters = word_break_characters;
  336. info.quote_characters = gdb_completer_quote_characters;
  337. info.basic_quote_characters = rl_basic_quote_characters;
  338. int delimiter;
  339. const char *start
  340. = gdb_rl_find_completion_word (&info, NULL, &delimiter, text);
  341. tracker.advance_custom_word_point_by (start - text);
  342. if (delimiter)
  343. {
  344. tracker.set_quote_char (delimiter);
  345. tracker.set_suppress_append_ws (true);
  346. }
  347. return start;
  348. }
  349. /* See completer.h. */
  350. const char *
  351. advance_to_expression_complete_word_point (completion_tracker &tracker,
  352. const char *text)
  353. {
  354. const char *brk_chars = current_language->word_break_characters ();
  355. return advance_to_completion_word (tracker, brk_chars, text);
  356. }
  357. /* See completer.h. */
  358. const char *
  359. advance_to_filename_complete_word_point (completion_tracker &tracker,
  360. const char *text)
  361. {
  362. const char *brk_chars = gdb_completer_file_name_break_characters;
  363. return advance_to_completion_word (tracker, brk_chars, text);
  364. }
  365. /* See completer.h. */
  366. bool
  367. completion_tracker::completes_to_completion_word (const char *word)
  368. {
  369. recompute_lowest_common_denominator ();
  370. if (m_lowest_common_denominator_unique)
  371. {
  372. const char *lcd = m_lowest_common_denominator;
  373. if (strncmp_iw (word, lcd, strlen (lcd)) == 0)
  374. {
  375. /* Maybe skip the function and complete on keywords. */
  376. size_t wordlen = strlen (word);
  377. if (word[wordlen - 1] == ' ')
  378. return true;
  379. }
  380. }
  381. return false;
  382. }
  383. /* See completer.h. */
  384. void
  385. complete_nested_command_line (completion_tracker &tracker, const char *text)
  386. {
  387. /* Must be called from a custom-word-point completer. */
  388. gdb_assert (tracker.use_custom_word_point ());
  389. /* Disable the custom word point temporarily, because we want to
  390. probe whether the command we're completing itself uses a custom
  391. word point. */
  392. tracker.set_use_custom_word_point (false);
  393. size_t save_custom_word_point = tracker.custom_word_point ();
  394. int quote_char = '\0';
  395. const char *word = completion_find_completion_word (tracker, text,
  396. &quote_char);
  397. if (tracker.use_custom_word_point ())
  398. {
  399. /* The command we're completing uses a custom word point, so the
  400. tracker already contains the matches. We're done. */
  401. return;
  402. }
  403. /* Restore the custom word point settings. */
  404. tracker.set_custom_word_point (save_custom_word_point);
  405. tracker.set_use_custom_word_point (true);
  406. /* Run the handle_completions completer phase. */
  407. complete_line (tracker, word, text, strlen (text));
  408. }
  409. /* Complete on linespecs, which might be of two possible forms:
  410. file:line
  411. or
  412. symbol+offset
  413. This is intended to be used in commands that set breakpoints
  414. etc. */
  415. static void
  416. complete_files_symbols (completion_tracker &tracker,
  417. const char *text, const char *word)
  418. {
  419. completion_list fn_list;
  420. const char *p;
  421. int quote_found = 0;
  422. int quoted = *text == '\'' || *text == '"';
  423. int quote_char = '\0';
  424. const char *colon = NULL;
  425. char *file_to_match = NULL;
  426. const char *symbol_start = text;
  427. const char *orig_text = text;
  428. /* Do we have an unquoted colon, as in "break foo.c:bar"? */
  429. for (p = text; *p != '\0'; ++p)
  430. {
  431. if (*p == '\\' && p[1] == '\'')
  432. p++;
  433. else if (*p == '\'' || *p == '"')
  434. {
  435. quote_found = *p;
  436. quote_char = *p++;
  437. while (*p != '\0' && *p != quote_found)
  438. {
  439. if (*p == '\\' && p[1] == quote_found)
  440. p++;
  441. p++;
  442. }
  443. if (*p == quote_found)
  444. quote_found = 0;
  445. else
  446. break; /* Hit the end of text. */
  447. }
  448. #if HAVE_DOS_BASED_FILE_SYSTEM
  449. /* If we have a DOS-style absolute file name at the beginning of
  450. TEXT, and the colon after the drive letter is the only colon
  451. we found, pretend the colon is not there. */
  452. else if (p < text + 3 && *p == ':' && p == text + 1 + quoted)
  453. ;
  454. #endif
  455. else if (*p == ':' && !colon)
  456. {
  457. colon = p;
  458. symbol_start = p + 1;
  459. }
  460. else if (strchr (current_language->word_break_characters (), *p))
  461. symbol_start = p + 1;
  462. }
  463. if (quoted)
  464. text++;
  465. /* Where is the file name? */
  466. if (colon)
  467. {
  468. char *s;
  469. file_to_match = (char *) xmalloc (colon - text + 1);
  470. strncpy (file_to_match, text, colon - text);
  471. file_to_match[colon - text] = '\0';
  472. /* Remove trailing colons and quotes from the file name. */
  473. for (s = file_to_match + (colon - text);
  474. s > file_to_match;
  475. s--)
  476. if (*s == ':' || *s == quote_char)
  477. *s = '\0';
  478. }
  479. /* If the text includes a colon, they want completion only on a
  480. symbol name after the colon. Otherwise, we need to complete on
  481. symbols as well as on files. */
  482. if (colon)
  483. {
  484. collect_file_symbol_completion_matches (tracker,
  485. complete_symbol_mode::EXPRESSION,
  486. symbol_name_match_type::EXPRESSION,
  487. symbol_start, word,
  488. file_to_match);
  489. xfree (file_to_match);
  490. }
  491. else
  492. {
  493. size_t text_len = strlen (text);
  494. collect_symbol_completion_matches (tracker,
  495. complete_symbol_mode::EXPRESSION,
  496. symbol_name_match_type::EXPRESSION,
  497. symbol_start, word);
  498. /* If text includes characters which cannot appear in a file
  499. name, they cannot be asking for completion on files. */
  500. if (strcspn (text,
  501. gdb_completer_file_name_break_characters) == text_len)
  502. fn_list = make_source_files_completion_list (text, text);
  503. }
  504. if (!fn_list.empty () && !tracker.have_completions ())
  505. {
  506. /* If we only have file names as possible completion, we should
  507. bring them in sync with what rl_complete expects. The
  508. problem is that if the user types "break /foo/b TAB", and the
  509. possible completions are "/foo/bar" and "/foo/baz"
  510. rl_complete expects us to return "bar" and "baz", without the
  511. leading directories, as possible completions, because `word'
  512. starts at the "b". But we ignore the value of `word' when we
  513. call make_source_files_completion_list above (because that
  514. would not DTRT when the completion results in both symbols
  515. and file names), so make_source_files_completion_list returns
  516. the full "/foo/bar" and "/foo/baz" strings. This produces
  517. wrong results when, e.g., there's only one possible
  518. completion, because rl_complete will prepend "/foo/" to each
  519. candidate completion. The loop below removes that leading
  520. part. */
  521. for (const auto &fn_up: fn_list)
  522. {
  523. char *fn = fn_up.get ();
  524. memmove (fn, fn + (word - text), strlen (fn) + 1 - (word - text));
  525. }
  526. }
  527. tracker.add_completions (std::move (fn_list));
  528. if (!tracker.have_completions ())
  529. {
  530. /* No completions at all. As the final resort, try completing
  531. on the entire text as a symbol. */
  532. collect_symbol_completion_matches (tracker,
  533. complete_symbol_mode::EXPRESSION,
  534. symbol_name_match_type::EXPRESSION,
  535. orig_text, word);
  536. }
  537. }
  538. /* See completer.h. */
  539. completion_list
  540. complete_source_filenames (const char *text)
  541. {
  542. size_t text_len = strlen (text);
  543. /* If text includes characters which cannot appear in a file name,
  544. the user cannot be asking for completion on files. */
  545. if (strcspn (text,
  546. gdb_completer_file_name_break_characters)
  547. == text_len)
  548. return make_source_files_completion_list (text, text);
  549. return {};
  550. }
  551. /* Complete address and linespec locations. */
  552. static void
  553. complete_address_and_linespec_locations (completion_tracker &tracker,
  554. const char *text,
  555. symbol_name_match_type match_type)
  556. {
  557. if (*text == '*')
  558. {
  559. tracker.advance_custom_word_point_by (1);
  560. text++;
  561. const char *word
  562. = advance_to_expression_complete_word_point (tracker, text);
  563. complete_expression (tracker, text, word);
  564. }
  565. else
  566. {
  567. linespec_complete (tracker, text, match_type);
  568. }
  569. }
  570. /* The explicit location options. Note that indexes into this array
  571. must match the explicit_location_match_type enumerators. */
  572. static const char *const explicit_options[] =
  573. {
  574. "-source",
  575. "-function",
  576. "-qualified",
  577. "-line",
  578. "-label",
  579. NULL
  580. };
  581. /* The probe modifier options. These can appear before a location in
  582. breakpoint commands. */
  583. static const char *const probe_options[] =
  584. {
  585. "-probe",
  586. "-probe-stap",
  587. "-probe-dtrace",
  588. NULL
  589. };
  590. /* Returns STRING if not NULL, the empty string otherwise. */
  591. static const char *
  592. string_or_empty (const char *string)
  593. {
  594. return string != NULL ? string : "";
  595. }
  596. /* A helper function to collect explicit location matches for the given
  597. LOCATION, which is attempting to match on WORD. */
  598. static void
  599. collect_explicit_location_matches (completion_tracker &tracker,
  600. struct event_location *location,
  601. enum explicit_location_match_type what,
  602. const char *word,
  603. const struct language_defn *language)
  604. {
  605. const struct explicit_location *explicit_loc
  606. = get_explicit_location (location);
  607. /* True if the option expects an argument. */
  608. bool needs_arg = true;
  609. /* Note, in the various MATCH_* below, we complete on
  610. explicit_loc->foo instead of WORD, because only the former will
  611. have already skipped past any quote char. */
  612. switch (what)
  613. {
  614. case MATCH_SOURCE:
  615. {
  616. const char *source = string_or_empty (explicit_loc->source_filename);
  617. completion_list matches
  618. = make_source_files_completion_list (source, source);
  619. tracker.add_completions (std::move (matches));
  620. }
  621. break;
  622. case MATCH_FUNCTION:
  623. {
  624. const char *function = string_or_empty (explicit_loc->function_name);
  625. linespec_complete_function (tracker, function,
  626. explicit_loc->func_name_match_type,
  627. explicit_loc->source_filename);
  628. }
  629. break;
  630. case MATCH_QUALIFIED:
  631. needs_arg = false;
  632. break;
  633. case MATCH_LINE:
  634. /* Nothing to offer. */
  635. break;
  636. case MATCH_LABEL:
  637. {
  638. const char *label = string_or_empty (explicit_loc->label_name);
  639. linespec_complete_label (tracker, language,
  640. explicit_loc->source_filename,
  641. explicit_loc->function_name,
  642. explicit_loc->func_name_match_type,
  643. label);
  644. }
  645. break;
  646. default:
  647. gdb_assert_not_reached ("unhandled explicit_location_match_type");
  648. }
  649. if (!needs_arg || tracker.completes_to_completion_word (word))
  650. {
  651. tracker.discard_completions ();
  652. tracker.advance_custom_word_point_by (strlen (word));
  653. complete_on_enum (tracker, explicit_options, "", "");
  654. complete_on_enum (tracker, linespec_keywords, "", "");
  655. }
  656. else if (!tracker.have_completions ())
  657. {
  658. /* Maybe we have an unterminated linespec keyword at the tail of
  659. the string. Try completing on that. */
  660. size_t wordlen = strlen (word);
  661. const char *keyword = word + wordlen;
  662. if (wordlen > 0 && keyword[-1] != ' ')
  663. {
  664. while (keyword > word && *keyword != ' ')
  665. keyword--;
  666. /* Don't complete on keywords if we'd be completing on the
  667. whole explicit linespec option. E.g., "b -function
  668. thr<tab>" should not complete to the "thread"
  669. keyword. */
  670. if (keyword != word)
  671. {
  672. keyword = skip_spaces (keyword);
  673. tracker.advance_custom_word_point_by (keyword - word);
  674. complete_on_enum (tracker, linespec_keywords, keyword, keyword);
  675. }
  676. }
  677. else if (wordlen > 0 && keyword[-1] == ' ')
  678. {
  679. /* Assume that we're maybe past the explicit location
  680. argument, and we didn't manage to find any match because
  681. the user wants to create a pending breakpoint. Offer the
  682. keyword and explicit location options as possible
  683. completions. */
  684. tracker.advance_custom_word_point_by (keyword - word);
  685. complete_on_enum (tracker, linespec_keywords, keyword, keyword);
  686. complete_on_enum (tracker, explicit_options, keyword, keyword);
  687. }
  688. }
  689. }
  690. /* If the next word in *TEXT_P is any of the keywords in KEYWORDS,
  691. then advance both TEXT_P and the word point in the tracker past the
  692. keyword and return the (0-based) index in the KEYWORDS array that
  693. matched. Otherwise, return -1. */
  694. static int
  695. skip_keyword (completion_tracker &tracker,
  696. const char * const *keywords, const char **text_p)
  697. {
  698. const char *text = *text_p;
  699. const char *after = skip_to_space (text);
  700. size_t len = after - text;
  701. if (text[len] != ' ')
  702. return -1;
  703. int found = -1;
  704. for (int i = 0; keywords[i] != NULL; i++)
  705. {
  706. if (strncmp (keywords[i], text, len) == 0)
  707. {
  708. if (found == -1)
  709. found = i;
  710. else
  711. return -1;
  712. }
  713. }
  714. if (found != -1)
  715. {
  716. tracker.advance_custom_word_point_by (len + 1);
  717. text += len + 1;
  718. *text_p = text;
  719. return found;
  720. }
  721. return -1;
  722. }
  723. /* A completer function for explicit locations. This function
  724. completes both options ("-source", "-line", etc) and values. If
  725. completing a quoted string, then QUOTED_ARG_START and
  726. QUOTED_ARG_END point to the quote characters. LANGUAGE is the
  727. current language. */
  728. static void
  729. complete_explicit_location (completion_tracker &tracker,
  730. struct event_location *location,
  731. const char *text,
  732. const language_defn *language,
  733. const char *quoted_arg_start,
  734. const char *quoted_arg_end)
  735. {
  736. if (*text != '-')
  737. return;
  738. int keyword = skip_keyword (tracker, explicit_options, &text);
  739. if (keyword == -1)
  740. {
  741. complete_on_enum (tracker, explicit_options, text, text);
  742. /* There are keywords that start with "-". Include them, too. */
  743. complete_on_enum (tracker, linespec_keywords, text, text);
  744. }
  745. else
  746. {
  747. /* Completing on value. */
  748. enum explicit_location_match_type what
  749. = (explicit_location_match_type) keyword;
  750. if (quoted_arg_start != NULL && quoted_arg_end != NULL)
  751. {
  752. if (quoted_arg_end[1] == '\0')
  753. {
  754. /* If completing a quoted string with the cursor right
  755. at the terminating quote char, complete the
  756. completion word without interpretation, so that
  757. readline advances the cursor one whitespace past the
  758. quote, even if there's no match. This makes these
  759. cases behave the same:
  760. before: "b -function function()"
  761. after: "b -function function() "
  762. before: "b -function 'function()'"
  763. after: "b -function 'function()' "
  764. and trusts the user in this case:
  765. before: "b -function 'not_loaded_function_yet()'"
  766. after: "b -function 'not_loaded_function_yet()' "
  767. */
  768. tracker.add_completion (make_unique_xstrdup (text));
  769. }
  770. else if (quoted_arg_end[1] == ' ')
  771. {
  772. /* We're maybe past the explicit location argument.
  773. Skip the argument without interpretation, assuming the
  774. user may want to create pending breakpoint. Offer
  775. the keyword and explicit location options as possible
  776. completions. */
  777. tracker.advance_custom_word_point_by (strlen (text));
  778. complete_on_enum (tracker, linespec_keywords, "", "");
  779. complete_on_enum (tracker, explicit_options, "", "");
  780. }
  781. return;
  782. }
  783. /* Now gather matches */
  784. collect_explicit_location_matches (tracker, location, what, text,
  785. language);
  786. }
  787. }
  788. /* A completer for locations. */
  789. void
  790. location_completer (struct cmd_list_element *ignore,
  791. completion_tracker &tracker,
  792. const char *text, const char * /* word */)
  793. {
  794. int found_probe_option = -1;
  795. /* If we have a probe modifier, skip it. This can only appear as
  796. first argument. Until we have a specific completer for probes,
  797. falling back to the linespec completer for the remainder of the
  798. line is better than nothing. */
  799. if (text[0] == '-' && text[1] == 'p')
  800. found_probe_option = skip_keyword (tracker, probe_options, &text);
  801. const char *option_text = text;
  802. int saved_word_point = tracker.custom_word_point ();
  803. const char *copy = text;
  804. explicit_completion_info completion_info;
  805. event_location_up location
  806. = string_to_explicit_location (&copy, current_language,
  807. &completion_info);
  808. if (completion_info.quoted_arg_start != NULL
  809. && completion_info.quoted_arg_end == NULL)
  810. {
  811. /* Found an unbalanced quote. */
  812. tracker.set_quote_char (*completion_info.quoted_arg_start);
  813. tracker.advance_custom_word_point_by (1);
  814. }
  815. if (completion_info.saw_explicit_location_option)
  816. {
  817. if (*copy != '\0')
  818. {
  819. tracker.advance_custom_word_point_by (copy - text);
  820. text = copy;
  821. /* We found a terminator at the tail end of the string,
  822. which means we're past the explicit location options. We
  823. may have a keyword to complete on. If we have a whole
  824. keyword, then complete whatever comes after as an
  825. expression. This is mainly for the "if" keyword. If the
  826. "thread" and "task" keywords gain their own completers,
  827. they should be used here. */
  828. int keyword = skip_keyword (tracker, linespec_keywords, &text);
  829. if (keyword == -1)
  830. {
  831. complete_on_enum (tracker, linespec_keywords, text, text);
  832. }
  833. else
  834. {
  835. const char *word
  836. = advance_to_expression_complete_word_point (tracker, text);
  837. complete_expression (tracker, text, word);
  838. }
  839. }
  840. else
  841. {
  842. tracker.advance_custom_word_point_by (completion_info.last_option
  843. - text);
  844. text = completion_info.last_option;
  845. complete_explicit_location (tracker, location.get (), text,
  846. current_language,
  847. completion_info.quoted_arg_start,
  848. completion_info.quoted_arg_end);
  849. }
  850. }
  851. /* This is an address or linespec location. */
  852. else if (location != NULL)
  853. {
  854. /* Handle non-explicit location options. */
  855. int keyword = skip_keyword (tracker, explicit_options, &text);
  856. if (keyword == -1)
  857. complete_on_enum (tracker, explicit_options, text, text);
  858. else
  859. {
  860. tracker.advance_custom_word_point_by (copy - text);
  861. text = copy;
  862. symbol_name_match_type match_type
  863. = get_explicit_location (location.get ())->func_name_match_type;
  864. complete_address_and_linespec_locations (tracker, text, match_type);
  865. }
  866. }
  867. else
  868. {
  869. /* No options. */
  870. complete_address_and_linespec_locations (tracker, text,
  871. symbol_name_match_type::WILD);
  872. }
  873. /* Add matches for option names, if either:
  874. - Some completer above found some matches, but the word point did
  875. not advance (e.g., "b <tab>" finds all functions, or "b -<tab>"
  876. matches all objc selectors), or;
  877. - Some completer above advanced the word point, but found no
  878. matches.
  879. */
  880. if ((text[0] == '-' || text[0] == '\0')
  881. && (!tracker.have_completions ()
  882. || tracker.custom_word_point () == saved_word_point))
  883. {
  884. tracker.set_custom_word_point (saved_word_point);
  885. text = option_text;
  886. if (found_probe_option == -1)
  887. complete_on_enum (tracker, probe_options, text, text);
  888. complete_on_enum (tracker, explicit_options, text, text);
  889. }
  890. }
  891. /* The corresponding completer_handle_brkchars
  892. implementation. */
  893. static void
  894. location_completer_handle_brkchars (struct cmd_list_element *ignore,
  895. completion_tracker &tracker,
  896. const char *text,
  897. const char *word_ignored)
  898. {
  899. tracker.set_use_custom_word_point (true);
  900. location_completer (ignore, tracker, text, NULL);
  901. }
  902. /* See completer.h. */
  903. void
  904. complete_expression (completion_tracker &tracker,
  905. const char *text, const char *word)
  906. {
  907. expression_up exp;
  908. std::unique_ptr<expr_completion_base> expr_completer;
  909. /* Perform a tentative parse of the expression, to see whether a
  910. field completion is required. */
  911. try
  912. {
  913. exp = parse_expression_for_completion (text, &expr_completer);
  914. }
  915. catch (const gdb_exception_error &except)
  916. {
  917. return;
  918. }
  919. /* Part of the parse_expression_for_completion contract. */
  920. gdb_assert ((exp == nullptr) == (expr_completer == nullptr));
  921. if (expr_completer != nullptr
  922. && expr_completer->complete (exp.get (), tracker))
  923. return;
  924. complete_files_symbols (tracker, text, word);
  925. }
  926. /* Complete on expressions. Often this means completing on symbol
  927. names, but some language parsers also have support for completing
  928. field names. */
  929. void
  930. expression_completer (struct cmd_list_element *ignore,
  931. completion_tracker &tracker,
  932. const char *text, const char *word)
  933. {
  934. complete_expression (tracker, text, word);
  935. }
  936. /* See definition in completer.h. */
  937. void
  938. set_rl_completer_word_break_characters (const char *break_chars)
  939. {
  940. rl_completer_word_break_characters = (char *) break_chars;
  941. }
  942. /* Complete on symbols. */
  943. void
  944. symbol_completer (struct cmd_list_element *ignore,
  945. completion_tracker &tracker,
  946. const char *text, const char *word)
  947. {
  948. collect_symbol_completion_matches (tracker, complete_symbol_mode::EXPRESSION,
  949. symbol_name_match_type::EXPRESSION,
  950. text, word);
  951. }
  952. /* Here are some useful test cases for completion. FIXME: These
  953. should be put in the test suite. They should be tested with both
  954. M-? and TAB.
  955. "show output-" "radix"
  956. "show output" "-radix"
  957. "p" ambiguous (commands starting with p--path, print, printf, etc.)
  958. "p " ambiguous (all symbols)
  959. "info t foo" no completions
  960. "info t " no completions
  961. "info t" ambiguous ("info target", "info terminal", etc.)
  962. "info ajksdlfk" no completions
  963. "info ajksdlfk " no completions
  964. "info" " "
  965. "info " ambiguous (all info commands)
  966. "p \"a" no completions (string constant)
  967. "p 'a" ambiguous (all symbols starting with a)
  968. "p b-a" ambiguous (all symbols starting with a)
  969. "p b-" ambiguous (all symbols)
  970. "file Make" "file" (word break hard to screw up here)
  971. "file ../gdb.stabs/we" "ird" (needs to not break word at slash)
  972. */
  973. enum complete_line_internal_reason
  974. {
  975. /* Preliminary phase, called by gdb_completion_word_break_characters
  976. function, is used to either:
  977. #1 - Determine the set of chars that are word delimiters
  978. depending on the current command in line_buffer.
  979. #2 - Manually advance RL_POINT to the "word break" point instead
  980. of letting readline do it (based on too-simple character
  981. matching).
  982. Simpler completers that just pass a brkchars array to readline
  983. (#1 above) must defer generating the completions to the main
  984. phase (below). No completion list should be generated in this
  985. phase.
  986. OTOH, completers that manually advance the word point(#2 above)
  987. must set "use_custom_word_point" in the tracker and generate
  988. their completion in this phase. Note that this is the convenient
  989. thing to do since they'll be parsing the input line anyway. */
  990. handle_brkchars,
  991. /* Main phase, called by complete_line function, is used to get the
  992. list of possible completions. */
  993. handle_completions,
  994. /* Special case when completing a 'help' command. In this case,
  995. once sub-command completions are exhausted, we simply return
  996. NULL. */
  997. handle_help,
  998. };
  999. /* Helper for complete_line_internal to simplify it. */
  1000. static void
  1001. complete_line_internal_normal_command (completion_tracker &tracker,
  1002. const char *command, const char *word,
  1003. const char *cmd_args,
  1004. complete_line_internal_reason reason,
  1005. struct cmd_list_element *c)
  1006. {
  1007. const char *p = cmd_args;
  1008. if (c->completer == filename_completer)
  1009. {
  1010. /* Many commands which want to complete on file names accept
  1011. several file names, as in "run foo bar >>baz". So we don't
  1012. want to complete the entire text after the command, just the
  1013. last word. To this end, we need to find the beginning of the
  1014. file name by starting at `word' and going backwards. */
  1015. for (p = word;
  1016. p > command
  1017. && strchr (gdb_completer_file_name_break_characters,
  1018. p[-1]) == NULL;
  1019. p--)
  1020. ;
  1021. }
  1022. if (reason == handle_brkchars)
  1023. {
  1024. completer_handle_brkchars_ftype *brkchars_fn;
  1025. if (c->completer_handle_brkchars != NULL)
  1026. brkchars_fn = c->completer_handle_brkchars;
  1027. else
  1028. {
  1029. brkchars_fn
  1030. = (completer_handle_brkchars_func_for_completer
  1031. (c->completer));
  1032. }
  1033. brkchars_fn (c, tracker, p, word);
  1034. }
  1035. if (reason != handle_brkchars && c->completer != NULL)
  1036. (*c->completer) (c, tracker, p, word);
  1037. }
  1038. /* Internal function used to handle completions.
  1039. TEXT is the caller's idea of the "word" we are looking at.
  1040. LINE_BUFFER is available to be looked at; it contains the entire
  1041. text of the line. POINT is the offset in that line of the cursor.
  1042. You should pretend that the line ends at POINT.
  1043. See complete_line_internal_reason for description of REASON. */
  1044. static void
  1045. complete_line_internal_1 (completion_tracker &tracker,
  1046. const char *text,
  1047. const char *line_buffer, int point,
  1048. complete_line_internal_reason reason)
  1049. {
  1050. char *tmp_command;
  1051. const char *p;
  1052. int ignore_help_classes;
  1053. /* Pointer within tmp_command which corresponds to text. */
  1054. const char *word;
  1055. struct cmd_list_element *c, *result_list;
  1056. /* Choose the default set of word break characters to break
  1057. completions. If we later find out that we are doing completions
  1058. on command strings (as opposed to strings supplied by the
  1059. individual command completer functions, which can be any string)
  1060. then we will switch to the special word break set for command
  1061. strings, which leaves out the '-' and '.' character used in some
  1062. commands. */
  1063. set_rl_completer_word_break_characters
  1064. (current_language->word_break_characters ());
  1065. /* Decide whether to complete on a list of gdb commands or on
  1066. symbols. */
  1067. tmp_command = (char *) alloca (point + 1);
  1068. p = tmp_command;
  1069. /* The help command should complete help aliases. */
  1070. ignore_help_classes = reason != handle_help;
  1071. strncpy (tmp_command, line_buffer, point);
  1072. tmp_command[point] = '\0';
  1073. if (reason == handle_brkchars)
  1074. {
  1075. gdb_assert (text == NULL);
  1076. word = NULL;
  1077. }
  1078. else
  1079. {
  1080. /* Since text always contains some number of characters leading up
  1081. to point, we can find the equivalent position in tmp_command
  1082. by subtracting that many characters from the end of tmp_command. */
  1083. word = tmp_command + point - strlen (text);
  1084. }
  1085. /* Move P up to the start of the command. */
  1086. p = skip_spaces (p);
  1087. if (*p == '\0')
  1088. {
  1089. /* An empty line is ambiguous; that is, it could be any
  1090. command. */
  1091. c = CMD_LIST_AMBIGUOUS;
  1092. result_list = 0;
  1093. }
  1094. else
  1095. c = lookup_cmd_1 (&p, cmdlist, &result_list, NULL, ignore_help_classes,
  1096. true);
  1097. /* Move p up to the next interesting thing. */
  1098. while (*p == ' ' || *p == '\t')
  1099. {
  1100. p++;
  1101. }
  1102. tracker.advance_custom_word_point_by (p - tmp_command);
  1103. if (!c)
  1104. {
  1105. /* It is an unrecognized command. So there are no
  1106. possible completions. */
  1107. }
  1108. else if (c == CMD_LIST_AMBIGUOUS)
  1109. {
  1110. const char *q;
  1111. /* lookup_cmd_1 advances p up to the first ambiguous thing, but
  1112. doesn't advance over that thing itself. Do so now. */
  1113. q = p;
  1114. while (valid_cmd_char_p (*q))
  1115. ++q;
  1116. if (q != tmp_command + point)
  1117. {
  1118. /* There is something beyond the ambiguous
  1119. command, so there are no possible completions. For
  1120. example, "info t " or "info t foo" does not complete
  1121. to anything, because "info t" can be "info target" or
  1122. "info terminal". */
  1123. }
  1124. else
  1125. {
  1126. /* We're trying to complete on the command which was ambiguous.
  1127. This we can deal with. */
  1128. if (result_list)
  1129. {
  1130. if (reason != handle_brkchars)
  1131. complete_on_cmdlist (*result_list->subcommands, tracker, p,
  1132. word, ignore_help_classes);
  1133. }
  1134. else
  1135. {
  1136. if (reason != handle_brkchars)
  1137. complete_on_cmdlist (cmdlist, tracker, p, word,
  1138. ignore_help_classes);
  1139. }
  1140. /* Ensure that readline does the right thing with respect to
  1141. inserting quotes. */
  1142. set_rl_completer_word_break_characters
  1143. (gdb_completer_command_word_break_characters);
  1144. }
  1145. }
  1146. else
  1147. {
  1148. /* We've recognized a full command. */
  1149. if (p == tmp_command + point)
  1150. {
  1151. /* There is no non-whitespace in the line beyond the
  1152. command. */
  1153. if (p[-1] == ' ' || p[-1] == '\t')
  1154. {
  1155. /* The command is followed by whitespace; we need to
  1156. complete on whatever comes after command. */
  1157. if (c->is_prefix ())
  1158. {
  1159. /* It is a prefix command; what comes after it is
  1160. a subcommand (e.g. "info "). */
  1161. if (reason != handle_brkchars)
  1162. complete_on_cmdlist (*c->subcommands, tracker, p, word,
  1163. ignore_help_classes);
  1164. /* Ensure that readline does the right thing
  1165. with respect to inserting quotes. */
  1166. set_rl_completer_word_break_characters
  1167. (gdb_completer_command_word_break_characters);
  1168. }
  1169. else if (reason == handle_help)
  1170. ;
  1171. else if (c->enums)
  1172. {
  1173. if (reason != handle_brkchars)
  1174. complete_on_enum (tracker, c->enums, p, word);
  1175. set_rl_completer_word_break_characters
  1176. (gdb_completer_command_word_break_characters);
  1177. }
  1178. else
  1179. {
  1180. /* It is a normal command; what comes after it is
  1181. completed by the command's completer function. */
  1182. complete_line_internal_normal_command (tracker,
  1183. tmp_command, word, p,
  1184. reason, c);
  1185. }
  1186. }
  1187. else
  1188. {
  1189. /* The command is not followed by whitespace; we need to
  1190. complete on the command itself, e.g. "p" which is a
  1191. command itself but also can complete to "print", "ptype"
  1192. etc. */
  1193. const char *q;
  1194. /* Find the command we are completing on. */
  1195. q = p;
  1196. while (q > tmp_command)
  1197. {
  1198. if (valid_cmd_char_p (q[-1]))
  1199. --q;
  1200. else
  1201. break;
  1202. }
  1203. /* Move the custom word point back too. */
  1204. tracker.advance_custom_word_point_by (q - p);
  1205. if (reason != handle_brkchars)
  1206. complete_on_cmdlist (result_list, tracker, q, word,
  1207. ignore_help_classes);
  1208. /* Ensure that readline does the right thing
  1209. with respect to inserting quotes. */
  1210. set_rl_completer_word_break_characters
  1211. (gdb_completer_command_word_break_characters);
  1212. }
  1213. }
  1214. else if (reason == handle_help)
  1215. ;
  1216. else
  1217. {
  1218. /* There is non-whitespace beyond the command. */
  1219. if (c->is_prefix () && !c->allow_unknown)
  1220. {
  1221. /* It is an unrecognized subcommand of a prefix command,
  1222. e.g. "info adsfkdj". */
  1223. }
  1224. else if (c->enums)
  1225. {
  1226. if (reason != handle_brkchars)
  1227. complete_on_enum (tracker, c->enums, p, word);
  1228. }
  1229. else
  1230. {
  1231. /* It is a normal command. */
  1232. complete_line_internal_normal_command (tracker,
  1233. tmp_command, word, p,
  1234. reason, c);
  1235. }
  1236. }
  1237. }
  1238. }
  1239. /* Wrapper around complete_line_internal_1 to handle
  1240. MAX_COMPLETIONS_REACHED_ERROR. */
  1241. static void
  1242. complete_line_internal (completion_tracker &tracker,
  1243. const char *text,
  1244. const char *line_buffer, int point,
  1245. complete_line_internal_reason reason)
  1246. {
  1247. try
  1248. {
  1249. complete_line_internal_1 (tracker, text, line_buffer, point, reason);
  1250. }
  1251. catch (const gdb_exception_error &except)
  1252. {
  1253. if (except.error != MAX_COMPLETIONS_REACHED_ERROR)
  1254. throw;
  1255. }
  1256. }
  1257. /* See completer.h. */
  1258. int max_completions = 200;
  1259. /* Initial size of the table. It automagically grows from here. */
  1260. #define INITIAL_COMPLETION_HTAB_SIZE 200
  1261. /* See completer.h. */
  1262. completion_tracker::completion_tracker ()
  1263. {
  1264. discard_completions ();
  1265. }
  1266. /* See completer.h. */
  1267. void
  1268. completion_tracker::discard_completions ()
  1269. {
  1270. xfree (m_lowest_common_denominator);
  1271. m_lowest_common_denominator = NULL;
  1272. m_lowest_common_denominator_unique = false;
  1273. m_lowest_common_denominator_valid = false;
  1274. m_entries_hash.reset (nullptr);
  1275. /* A callback used by the hash table to compare new entries with existing
  1276. entries. We can't use the standard htab_eq_string function here as the
  1277. key to our hash is just a single string, while the values we store in
  1278. the hash are a struct containing multiple strings. */
  1279. static auto entry_eq_func
  1280. = [] (const void *first, const void *second) -> int
  1281. {
  1282. /* The FIRST argument is the entry already in the hash table, and
  1283. the SECOND argument is the new item being inserted. */
  1284. const completion_hash_entry *entry
  1285. = (const completion_hash_entry *) first;
  1286. const char *name_str = (const char *) second;
  1287. return entry->is_name_eq (name_str);
  1288. };
  1289. /* Callback used by the hash table to compute the hash value for an
  1290. existing entry. This is needed when expanding the hash table. */
  1291. static auto entry_hash_func
  1292. = [] (const void *arg) -> hashval_t
  1293. {
  1294. const completion_hash_entry *entry
  1295. = (const completion_hash_entry *) arg;
  1296. return entry->hash_name ();
  1297. };
  1298. m_entries_hash.reset
  1299. (htab_create_alloc (INITIAL_COMPLETION_HTAB_SIZE,
  1300. entry_hash_func, entry_eq_func,
  1301. htab_delete_entry<completion_hash_entry>,
  1302. xcalloc, xfree));
  1303. }
  1304. /* See completer.h. */
  1305. completion_tracker::~completion_tracker ()
  1306. {
  1307. xfree (m_lowest_common_denominator);
  1308. }
  1309. /* See completer.h. */
  1310. bool
  1311. completion_tracker::maybe_add_completion
  1312. (gdb::unique_xmalloc_ptr<char> name,
  1313. completion_match_for_lcd *match_for_lcd,
  1314. const char *text, const char *word)
  1315. {
  1316. void **slot;
  1317. if (max_completions == 0)
  1318. return false;
  1319. if (htab_elements (m_entries_hash.get ()) >= max_completions)
  1320. return false;
  1321. hashval_t hash = htab_hash_string (name.get ());
  1322. slot = htab_find_slot_with_hash (m_entries_hash.get (), name.get (),
  1323. hash, INSERT);
  1324. if (*slot == HTAB_EMPTY_ENTRY)
  1325. {
  1326. const char *match_for_lcd_str = NULL;
  1327. if (match_for_lcd != NULL)
  1328. match_for_lcd_str = match_for_lcd->finish ();
  1329. if (match_for_lcd_str == NULL)
  1330. match_for_lcd_str = name.get ();
  1331. gdb::unique_xmalloc_ptr<char> lcd
  1332. = make_completion_match_str (match_for_lcd_str, text, word);
  1333. size_t lcd_len = strlen (lcd.get ());
  1334. *slot = new completion_hash_entry (std::move (name), std::move (lcd));
  1335. m_lowest_common_denominator_valid = false;
  1336. m_lowest_common_denominator_max_length
  1337. = std::max (m_lowest_common_denominator_max_length, lcd_len);
  1338. }
  1339. return true;
  1340. }
  1341. /* See completer.h. */
  1342. void
  1343. completion_tracker::add_completion (gdb::unique_xmalloc_ptr<char> name,
  1344. completion_match_for_lcd *match_for_lcd,
  1345. const char *text, const char *word)
  1346. {
  1347. if (!maybe_add_completion (std::move (name), match_for_lcd, text, word))
  1348. throw_error (MAX_COMPLETIONS_REACHED_ERROR, _("Max completions reached."));
  1349. }
  1350. /* See completer.h. */
  1351. void
  1352. completion_tracker::add_completions (completion_list &&list)
  1353. {
  1354. for (auto &candidate : list)
  1355. add_completion (std::move (candidate));
  1356. }
  1357. /* See completer.h. */
  1358. void
  1359. completion_tracker::remove_completion (const char *name)
  1360. {
  1361. hashval_t hash = htab_hash_string (name);
  1362. if (htab_find_slot_with_hash (m_entries_hash.get (), name, hash, NO_INSERT)
  1363. != NULL)
  1364. {
  1365. htab_remove_elt_with_hash (m_entries_hash.get (), name, hash);
  1366. m_lowest_common_denominator_valid = false;
  1367. }
  1368. }
  1369. /* Helper for the make_completion_match_str overloads. Returns NULL
  1370. as an indication that we want MATCH_NAME exactly. It is up to the
  1371. caller to xstrdup that string if desired. */
  1372. static char *
  1373. make_completion_match_str_1 (const char *match_name,
  1374. const char *text, const char *word)
  1375. {
  1376. char *newobj;
  1377. if (word == text)
  1378. {
  1379. /* Return NULL as an indication that we want MATCH_NAME
  1380. exactly. */
  1381. return NULL;
  1382. }
  1383. else if (word > text)
  1384. {
  1385. /* Return some portion of MATCH_NAME. */
  1386. newobj = xstrdup (match_name + (word - text));
  1387. }
  1388. else
  1389. {
  1390. /* Return some of WORD plus MATCH_NAME. */
  1391. size_t len = strlen (match_name);
  1392. newobj = (char *) xmalloc (text - word + len + 1);
  1393. memcpy (newobj, word, text - word);
  1394. memcpy (newobj + (text - word), match_name, len + 1);
  1395. }
  1396. return newobj;
  1397. }
  1398. /* See completer.h. */
  1399. gdb::unique_xmalloc_ptr<char>
  1400. make_completion_match_str (const char *match_name,
  1401. const char *text, const char *word)
  1402. {
  1403. char *newobj = make_completion_match_str_1 (match_name, text, word);
  1404. if (newobj == NULL)
  1405. newobj = xstrdup (match_name);
  1406. return gdb::unique_xmalloc_ptr<char> (newobj);
  1407. }
  1408. /* See completer.h. */
  1409. gdb::unique_xmalloc_ptr<char>
  1410. make_completion_match_str (gdb::unique_xmalloc_ptr<char> &&match_name,
  1411. const char *text, const char *word)
  1412. {
  1413. char *newobj = make_completion_match_str_1 (match_name.get (), text, word);
  1414. if (newobj == NULL)
  1415. return std::move (match_name);
  1416. return gdb::unique_xmalloc_ptr<char> (newobj);
  1417. }
  1418. /* See complete.h. */
  1419. completion_result
  1420. complete (const char *line, char const **word, int *quote_char)
  1421. {
  1422. completion_tracker tracker_handle_brkchars;
  1423. completion_tracker tracker_handle_completions;
  1424. completion_tracker *tracker;
  1425. /* The WORD should be set to the end of word to complete. We initialize
  1426. to the completion point which is assumed to be at the end of LINE.
  1427. This leaves WORD to be initialized to a sensible value in cases
  1428. completion_find_completion_word() fails i.e., throws an exception.
  1429. See bug 24587. */
  1430. *word = line + strlen (line);
  1431. try
  1432. {
  1433. *word = completion_find_completion_word (tracker_handle_brkchars,
  1434. line, quote_char);
  1435. /* Completers that provide a custom word point in the
  1436. handle_brkchars phase also compute their completions then.
  1437. Completers that leave the completion word handling to readline
  1438. must be called twice. */
  1439. if (tracker_handle_brkchars.use_custom_word_point ())
  1440. tracker = &tracker_handle_brkchars;
  1441. else
  1442. {
  1443. complete_line (tracker_handle_completions, *word, line, strlen (line));
  1444. tracker = &tracker_handle_completions;
  1445. }
  1446. }
  1447. catch (const gdb_exception &ex)
  1448. {
  1449. return {};
  1450. }
  1451. return tracker->build_completion_result (*word, *word - line, strlen (line));
  1452. }
  1453. /* Generate completions all at once. Does nothing if max_completions
  1454. is 0. If max_completions is non-negative, this will collect at
  1455. most max_completions strings.
  1456. TEXT is the caller's idea of the "word" we are looking at.
  1457. LINE_BUFFER is available to be looked at; it contains the entire
  1458. text of the line.
  1459. POINT is the offset in that line of the cursor. You
  1460. should pretend that the line ends at POINT. */
  1461. void
  1462. complete_line (completion_tracker &tracker,
  1463. const char *text, const char *line_buffer, int point)
  1464. {
  1465. if (max_completions == 0)
  1466. return;
  1467. complete_line_internal (tracker, text, line_buffer, point,
  1468. handle_completions);
  1469. }
  1470. /* Complete on command names. Used by "help". */
  1471. void
  1472. command_completer (struct cmd_list_element *ignore,
  1473. completion_tracker &tracker,
  1474. const char *text, const char *word)
  1475. {
  1476. complete_line_internal (tracker, word, text,
  1477. strlen (text), handle_help);
  1478. }
  1479. /* The corresponding completer_handle_brkchars implementation. */
  1480. static void
  1481. command_completer_handle_brkchars (struct cmd_list_element *ignore,
  1482. completion_tracker &tracker,
  1483. const char *text, const char *word)
  1484. {
  1485. set_rl_completer_word_break_characters
  1486. (gdb_completer_command_word_break_characters);
  1487. }
  1488. /* Complete on signals. */
  1489. void
  1490. signal_completer (struct cmd_list_element *ignore,
  1491. completion_tracker &tracker,
  1492. const char *text, const char *word)
  1493. {
  1494. size_t len = strlen (word);
  1495. int signum;
  1496. const char *signame;
  1497. for (signum = GDB_SIGNAL_FIRST; signum != GDB_SIGNAL_LAST; ++signum)
  1498. {
  1499. /* Can't handle this, so skip it. */
  1500. if (signum == GDB_SIGNAL_0)
  1501. continue;
  1502. signame = gdb_signal_to_name ((enum gdb_signal) signum);
  1503. /* Ignore the unknown signal case. */
  1504. if (!signame || strcmp (signame, "?") == 0)
  1505. continue;
  1506. if (strncasecmp (signame, word, len) == 0)
  1507. tracker.add_completion (make_unique_xstrdup (signame));
  1508. }
  1509. }
  1510. /* Bit-flags for selecting what the register and/or register-group
  1511. completer should complete on. */
  1512. enum reg_completer_target
  1513. {
  1514. complete_register_names = 0x1,
  1515. complete_reggroup_names = 0x2
  1516. };
  1517. DEF_ENUM_FLAGS_TYPE (enum reg_completer_target, reg_completer_targets);
  1518. /* Complete register names and/or reggroup names based on the value passed
  1519. in TARGETS. At least one bit in TARGETS must be set. */
  1520. static void
  1521. reg_or_group_completer_1 (completion_tracker &tracker,
  1522. const char *text, const char *word,
  1523. reg_completer_targets targets)
  1524. {
  1525. size_t len = strlen (word);
  1526. struct gdbarch *gdbarch;
  1527. const char *name;
  1528. gdb_assert ((targets & (complete_register_names
  1529. | complete_reggroup_names)) != 0);
  1530. gdbarch = get_current_arch ();
  1531. if ((targets & complete_register_names) != 0)
  1532. {
  1533. int i;
  1534. for (i = 0;
  1535. (name = user_reg_map_regnum_to_name (gdbarch, i)) != NULL;
  1536. i++)
  1537. {
  1538. if (*name != '\0' && strncmp (word, name, len) == 0)
  1539. tracker.add_completion (make_unique_xstrdup (name));
  1540. }
  1541. }
  1542. if ((targets & complete_reggroup_names) != 0)
  1543. {
  1544. for (const struct reggroup *group : gdbarch_reggroups (gdbarch))
  1545. {
  1546. name = group->name ();
  1547. if (strncmp (word, name, len) == 0)
  1548. tracker.add_completion (make_unique_xstrdup (name));
  1549. }
  1550. }
  1551. }
  1552. /* Perform completion on register and reggroup names. */
  1553. void
  1554. reg_or_group_completer (struct cmd_list_element *ignore,
  1555. completion_tracker &tracker,
  1556. const char *text, const char *word)
  1557. {
  1558. reg_or_group_completer_1 (tracker, text, word,
  1559. (complete_register_names
  1560. | complete_reggroup_names));
  1561. }
  1562. /* Perform completion on reggroup names. */
  1563. void
  1564. reggroup_completer (struct cmd_list_element *ignore,
  1565. completion_tracker &tracker,
  1566. const char *text, const char *word)
  1567. {
  1568. reg_or_group_completer_1 (tracker, text, word,
  1569. complete_reggroup_names);
  1570. }
  1571. /* The default completer_handle_brkchars implementation. */
  1572. static void
  1573. default_completer_handle_brkchars (struct cmd_list_element *ignore,
  1574. completion_tracker &tracker,
  1575. const char *text, const char *word)
  1576. {
  1577. set_rl_completer_word_break_characters
  1578. (current_language->word_break_characters ());
  1579. }
  1580. /* See definition in completer.h. */
  1581. completer_handle_brkchars_ftype *
  1582. completer_handle_brkchars_func_for_completer (completer_ftype *fn)
  1583. {
  1584. if (fn == filename_completer)
  1585. return filename_completer_handle_brkchars;
  1586. if (fn == location_completer)
  1587. return location_completer_handle_brkchars;
  1588. if (fn == command_completer)
  1589. return command_completer_handle_brkchars;
  1590. return default_completer_handle_brkchars;
  1591. }
  1592. /* Used as brkchars when we want to tell readline we have a custom
  1593. word point. We do that by making our rl_completion_word_break_hook
  1594. set RL_POINT to the desired word point, and return the character at
  1595. the word break point as the break char. This is two bytes in order
  1596. to fit one break character plus the terminating null. */
  1597. static char gdb_custom_word_point_brkchars[2];
  1598. /* Since rl_basic_quote_characters is not completer-specific, we save
  1599. its original value here, in order to be able to restore it in
  1600. gdb_rl_attempted_completion_function. */
  1601. static const char *gdb_org_rl_basic_quote_characters = rl_basic_quote_characters;
  1602. /* Get the list of chars that are considered as word breaks
  1603. for the current command. */
  1604. static char *
  1605. gdb_completion_word_break_characters_throw ()
  1606. {
  1607. /* New completion starting. Get rid of the previous tracker and
  1608. start afresh. */
  1609. delete current_completion.tracker;
  1610. current_completion.tracker = new completion_tracker ();
  1611. completion_tracker &tracker = *current_completion.tracker;
  1612. complete_line_internal (tracker, NULL, rl_line_buffer,
  1613. rl_point, handle_brkchars);
  1614. if (tracker.use_custom_word_point ())
  1615. {
  1616. gdb_assert (tracker.custom_word_point () > 0);
  1617. rl_point = tracker.custom_word_point () - 1;
  1618. gdb_assert (rl_point >= 0 && rl_point < strlen (rl_line_buffer));
  1619. gdb_custom_word_point_brkchars[0] = rl_line_buffer[rl_point];
  1620. rl_completer_word_break_characters = gdb_custom_word_point_brkchars;
  1621. rl_completer_quote_characters = NULL;
  1622. /* Clear this too, so that if we're completing a quoted string,
  1623. readline doesn't consider the quote character a delimiter.
  1624. If we didn't do this, readline would auto-complete {b
  1625. 'fun<tab>} to {'b 'function()'}, i.e., add the terminating
  1626. \', but, it wouldn't append the separator space either, which
  1627. is not desirable. So instead we take care of appending the
  1628. quote character to the LCD ourselves, in
  1629. gdb_rl_attempted_completion_function. Since this global is
  1630. not just completer-specific, we'll restore it back to the
  1631. default in gdb_rl_attempted_completion_function. */
  1632. rl_basic_quote_characters = NULL;
  1633. }
  1634. return (char *) rl_completer_word_break_characters;
  1635. }
  1636. char *
  1637. gdb_completion_word_break_characters ()
  1638. {
  1639. /* New completion starting. */
  1640. current_completion.aborted = false;
  1641. try
  1642. {
  1643. return gdb_completion_word_break_characters_throw ();
  1644. }
  1645. catch (const gdb_exception &ex)
  1646. {
  1647. /* Set this to that gdb_rl_attempted_completion_function knows
  1648. to abort early. */
  1649. current_completion.aborted = true;
  1650. }
  1651. return NULL;
  1652. }
  1653. /* See completer.h. */
  1654. const char *
  1655. completion_find_completion_word (completion_tracker &tracker, const char *text,
  1656. int *quote_char)
  1657. {
  1658. size_t point = strlen (text);
  1659. complete_line_internal (tracker, NULL, text, point, handle_brkchars);
  1660. if (tracker.use_custom_word_point ())
  1661. {
  1662. gdb_assert (tracker.custom_word_point () > 0);
  1663. *quote_char = tracker.quote_char ();
  1664. return text + tracker.custom_word_point ();
  1665. }
  1666. gdb_rl_completion_word_info info;
  1667. info.word_break_characters = rl_completer_word_break_characters;
  1668. info.quote_characters = gdb_completer_quote_characters;
  1669. info.basic_quote_characters = rl_basic_quote_characters;
  1670. return gdb_rl_find_completion_word (&info, quote_char, NULL, text);
  1671. }
  1672. /* See completer.h. */
  1673. void
  1674. completion_tracker::recompute_lcd_visitor (completion_hash_entry *entry)
  1675. {
  1676. if (!m_lowest_common_denominator_valid)
  1677. {
  1678. /* This is the first lowest common denominator that we are
  1679. considering, just copy it in. */
  1680. strcpy (m_lowest_common_denominator, entry->get_lcd ());
  1681. m_lowest_common_denominator_unique = true;
  1682. m_lowest_common_denominator_valid = true;
  1683. }
  1684. else
  1685. {
  1686. /* Find the common denominator between the currently-known lowest
  1687. common denominator and NEW_MATCH_UP. That becomes the new lowest
  1688. common denominator. */
  1689. size_t i;
  1690. const char *new_match = entry->get_lcd ();
  1691. for (i = 0;
  1692. (new_match[i] != '\0'
  1693. && new_match[i] == m_lowest_common_denominator[i]);
  1694. i++)
  1695. ;
  1696. if (m_lowest_common_denominator[i] != new_match[i])
  1697. {
  1698. m_lowest_common_denominator[i] = '\0';
  1699. m_lowest_common_denominator_unique = false;
  1700. }
  1701. }
  1702. }
  1703. /* See completer.h. */
  1704. void
  1705. completion_tracker::recompute_lowest_common_denominator ()
  1706. {
  1707. /* We've already done this. */
  1708. if (m_lowest_common_denominator_valid)
  1709. return;
  1710. /* Resize the storage to ensure we have enough space, the plus one gives
  1711. us space for the trailing null terminator we will include. */
  1712. m_lowest_common_denominator
  1713. = (char *) xrealloc (m_lowest_common_denominator,
  1714. m_lowest_common_denominator_max_length + 1);
  1715. /* Callback used to visit each entry in the m_entries_hash. */
  1716. auto visitor_func
  1717. = [] (void **slot, void *info) -> int
  1718. {
  1719. completion_tracker *obj = (completion_tracker *) info;
  1720. completion_hash_entry *entry = (completion_hash_entry *) *slot;
  1721. obj->recompute_lcd_visitor (entry);
  1722. return 1;
  1723. };
  1724. htab_traverse (m_entries_hash.get (), visitor_func, this);
  1725. m_lowest_common_denominator_valid = true;
  1726. }
  1727. /* See completer.h. */
  1728. void
  1729. completion_tracker::advance_custom_word_point_by (int len)
  1730. {
  1731. m_custom_word_point += len;
  1732. }
  1733. /* Build a new C string that is a copy of LCD with the whitespace of
  1734. ORIG/ORIG_LEN preserved.
  1735. Say the user is completing a symbol name, with spaces, like:
  1736. "foo ( i"
  1737. and the resulting completion match is:
  1738. "foo(int)"
  1739. we want to end up with an input line like:
  1740. "foo ( int)"
  1741. ^^^^^^^ => text from LCD [1], whitespace from ORIG preserved.
  1742. ^^ => new text from LCD
  1743. [1] - We must take characters from the LCD instead of the original
  1744. text, since some completions want to change upper/lowercase. E.g.:
  1745. "handle sig<>"
  1746. completes to:
  1747. "handle SIG[QUIT|etc.]"
  1748. */
  1749. static char *
  1750. expand_preserving_ws (const char *orig, size_t orig_len,
  1751. const char *lcd)
  1752. {
  1753. const char *p_orig = orig;
  1754. const char *orig_end = orig + orig_len;
  1755. const char *p_lcd = lcd;
  1756. std::string res;
  1757. while (p_orig < orig_end)
  1758. {
  1759. if (*p_orig == ' ')
  1760. {
  1761. while (p_orig < orig_end && *p_orig == ' ')
  1762. res += *p_orig++;
  1763. p_lcd = skip_spaces (p_lcd);
  1764. }
  1765. else
  1766. {
  1767. /* Take characters from the LCD instead of the original
  1768. text, since some completions change upper/lowercase.
  1769. E.g.:
  1770. "handle sig<>"
  1771. completes to:
  1772. "handle SIG[QUIT|etc.]"
  1773. */
  1774. res += *p_lcd;
  1775. p_orig++;
  1776. p_lcd++;
  1777. }
  1778. }
  1779. while (*p_lcd != '\0')
  1780. res += *p_lcd++;
  1781. return xstrdup (res.c_str ());
  1782. }
  1783. /* See completer.h. */
  1784. completion_result
  1785. completion_tracker::build_completion_result (const char *text,
  1786. int start, int end)
  1787. {
  1788. size_t element_count = htab_elements (m_entries_hash.get ());
  1789. if (element_count == 0)
  1790. return {};
  1791. /* +1 for the LCD, and +1 for NULL termination. */
  1792. char **match_list = XNEWVEC (char *, 1 + element_count + 1);
  1793. /* Build replacement word, based on the LCD. */
  1794. recompute_lowest_common_denominator ();
  1795. match_list[0]
  1796. = expand_preserving_ws (text, end - start,
  1797. m_lowest_common_denominator);
  1798. if (m_lowest_common_denominator_unique)
  1799. {
  1800. /* We don't rely on readline appending the quote char as
  1801. delimiter as then readline wouldn't append the ' ' after the
  1802. completion. */
  1803. char buf[2] = { (char) quote_char () };
  1804. match_list[0] = reconcat (match_list[0], match_list[0],
  1805. buf, (char *) NULL);
  1806. match_list[1] = NULL;
  1807. /* If the tracker wants to, or we already have a space at the
  1808. end of the match, tell readline to skip appending
  1809. another. */
  1810. char *match = match_list[0];
  1811. bool completion_suppress_append
  1812. = (suppress_append_ws ()
  1813. || (match[0] != '\0'
  1814. && match[strlen (match) - 1] == ' '));
  1815. return completion_result (match_list, 1, completion_suppress_append);
  1816. }
  1817. else
  1818. {
  1819. /* State object used while building the completion list. */
  1820. struct list_builder
  1821. {
  1822. list_builder (char **ml)
  1823. : match_list (ml),
  1824. index (1)
  1825. { /* Nothing. */ }
  1826. /* The list we are filling. */
  1827. char **match_list;
  1828. /* The next index in the list to write to. */
  1829. int index;
  1830. };
  1831. list_builder builder (match_list);
  1832. /* Visit each entry in m_entries_hash and add it to the completion
  1833. list, updating the builder state object. */
  1834. auto func
  1835. = [] (void **slot, void *info) -> int
  1836. {
  1837. completion_hash_entry *entry = (completion_hash_entry *) *slot;
  1838. list_builder *state = (list_builder *) info;
  1839. state->match_list[state->index] = entry->release_name ();
  1840. state->index++;
  1841. return 1;
  1842. };
  1843. /* Build the completion list and add a null at the end. */
  1844. htab_traverse_noresize (m_entries_hash.get (), func, &builder);
  1845. match_list[builder.index] = NULL;
  1846. return completion_result (match_list, builder.index - 1, false);
  1847. }
  1848. }
  1849. /* See completer.h */
  1850. completion_result::completion_result ()
  1851. : match_list (NULL), number_matches (0),
  1852. completion_suppress_append (false)
  1853. {}
  1854. /* See completer.h */
  1855. completion_result::completion_result (char **match_list_,
  1856. size_t number_matches_,
  1857. bool completion_suppress_append_)
  1858. : match_list (match_list_),
  1859. number_matches (number_matches_),
  1860. completion_suppress_append (completion_suppress_append_)
  1861. {}
  1862. /* See completer.h */
  1863. completion_result::~completion_result ()
  1864. {
  1865. reset_match_list ();
  1866. }
  1867. /* See completer.h */
  1868. completion_result::completion_result (completion_result &&rhs) noexcept
  1869. : match_list (rhs.match_list),
  1870. number_matches (rhs.number_matches)
  1871. {
  1872. rhs.match_list = NULL;
  1873. rhs.number_matches = 0;
  1874. }
  1875. /* See completer.h */
  1876. char **
  1877. completion_result::release_match_list ()
  1878. {
  1879. char **ret = match_list;
  1880. match_list = NULL;
  1881. return ret;
  1882. }
  1883. /* See completer.h */
  1884. void
  1885. completion_result::sort_match_list ()
  1886. {
  1887. if (number_matches > 1)
  1888. {
  1889. /* Element 0 is special (it's the common prefix), leave it
  1890. be. */
  1891. std::sort (&match_list[1],
  1892. &match_list[number_matches + 1],
  1893. compare_cstrings);
  1894. }
  1895. }
  1896. /* See completer.h */
  1897. void
  1898. completion_result::reset_match_list ()
  1899. {
  1900. if (match_list != NULL)
  1901. {
  1902. for (char **p = match_list; *p != NULL; p++)
  1903. xfree (*p);
  1904. xfree (match_list);
  1905. match_list = NULL;
  1906. }
  1907. }
  1908. /* Helper for gdb_rl_attempted_completion_function, which does most of
  1909. the work. This is called by readline to build the match list array
  1910. and to determine the lowest common denominator. The real matches
  1911. list starts at match[1], while match[0] is the slot holding
  1912. readline's idea of the lowest common denominator of all matches,
  1913. which is what readline replaces the completion "word" with.
  1914. TEXT is the caller's idea of the "word" we are looking at, as
  1915. computed in the handle_brkchars phase.
  1916. START is the offset from RL_LINE_BUFFER where TEXT starts. END is
  1917. the offset from RL_LINE_BUFFER where TEXT ends (i.e., where
  1918. rl_point is).
  1919. You should thus pretend that the line ends at END (relative to
  1920. RL_LINE_BUFFER).
  1921. RL_LINE_BUFFER contains the entire text of the line. RL_POINT is
  1922. the offset in that line of the cursor. You should pretend that the
  1923. line ends at POINT.
  1924. Returns NULL if there are no completions. */
  1925. static char **
  1926. gdb_rl_attempted_completion_function_throw (const char *text, int start, int end)
  1927. {
  1928. /* Completers that provide a custom word point in the
  1929. handle_brkchars phase also compute their completions then.
  1930. Completers that leave the completion word handling to readline
  1931. must be called twice. If rl_point (i.e., END) is at column 0,
  1932. then readline skips the handle_brkchars phase, and so we create a
  1933. tracker now in that case too. */
  1934. if (end == 0 || !current_completion.tracker->use_custom_word_point ())
  1935. {
  1936. delete current_completion.tracker;
  1937. current_completion.tracker = new completion_tracker ();
  1938. complete_line (*current_completion.tracker, text,
  1939. rl_line_buffer, rl_point);
  1940. }
  1941. completion_tracker &tracker = *current_completion.tracker;
  1942. completion_result result
  1943. = tracker.build_completion_result (text, start, end);
  1944. rl_completion_suppress_append = result.completion_suppress_append;
  1945. return result.release_match_list ();
  1946. }
  1947. /* Function installed as "rl_attempted_completion_function" readline
  1948. hook. Wrapper around gdb_rl_attempted_completion_function_throw
  1949. that catches C++ exceptions, which can't cross readline. */
  1950. char **
  1951. gdb_rl_attempted_completion_function (const char *text, int start, int end)
  1952. {
  1953. /* Restore globals that might have been tweaked in
  1954. gdb_completion_word_break_characters. */
  1955. rl_basic_quote_characters = gdb_org_rl_basic_quote_characters;
  1956. /* If we end up returning NULL, either on error, or simple because
  1957. there are no matches, inhibit readline's default filename
  1958. completer. */
  1959. rl_attempted_completion_over = 1;
  1960. /* If the handle_brkchars phase was aborted, don't try
  1961. completing. */
  1962. if (current_completion.aborted)
  1963. return NULL;
  1964. try
  1965. {
  1966. return gdb_rl_attempted_completion_function_throw (text, start, end);
  1967. }
  1968. catch (const gdb_exception &ex)
  1969. {
  1970. }
  1971. return NULL;
  1972. }
  1973. /* Skip over the possibly quoted word STR (as defined by the quote
  1974. characters QUOTECHARS and the word break characters BREAKCHARS).
  1975. Returns pointer to the location after the "word". If either
  1976. QUOTECHARS or BREAKCHARS is NULL, use the same values used by the
  1977. completer. */
  1978. const char *
  1979. skip_quoted_chars (const char *str, const char *quotechars,
  1980. const char *breakchars)
  1981. {
  1982. char quote_char = '\0';
  1983. const char *scan;
  1984. if (quotechars == NULL)
  1985. quotechars = gdb_completer_quote_characters;
  1986. if (breakchars == NULL)
  1987. breakchars = current_language->word_break_characters ();
  1988. for (scan = str; *scan != '\0'; scan++)
  1989. {
  1990. if (quote_char != '\0')
  1991. {
  1992. /* Ignore everything until the matching close quote char. */
  1993. if (*scan == quote_char)
  1994. {
  1995. /* Found matching close quote. */
  1996. scan++;
  1997. break;
  1998. }
  1999. }
  2000. else if (strchr (quotechars, *scan))
  2001. {
  2002. /* Found start of a quoted string. */
  2003. quote_char = *scan;
  2004. }
  2005. else if (strchr (breakchars, *scan))
  2006. {
  2007. break;
  2008. }
  2009. }
  2010. return (scan);
  2011. }
  2012. /* Skip over the possibly quoted word STR (as defined by the quote
  2013. characters and word break characters used by the completer).
  2014. Returns pointer to the location after the "word". */
  2015. const char *
  2016. skip_quoted (const char *str)
  2017. {
  2018. return skip_quoted_chars (str, NULL, NULL);
  2019. }
  2020. /* Return a message indicating that the maximum number of completions
  2021. has been reached and that there may be more. */
  2022. const char *
  2023. get_max_completions_reached_message (void)
  2024. {
  2025. return _("*** List may be truncated, max-completions reached. ***");
  2026. }
  2027. /* GDB replacement for rl_display_match_list.
  2028. Readline doesn't provide a clean interface for TUI(curses).
  2029. A hack previously used was to send readline's rl_outstream through a pipe
  2030. and read it from the event loop. Bleah. IWBN if readline abstracted
  2031. away all the necessary bits, and this is what this code does. It
  2032. replicates the parts of readline we need and then adds an abstraction
  2033. layer, currently implemented as struct match_list_displayer, so that both
  2034. CLI and TUI can use it. We copy all this readline code to minimize
  2035. GDB-specific mods to readline. Once this code performs as desired then
  2036. we can submit it to the readline maintainers.
  2037. N.B. A lot of the code is the way it is in order to minimize differences
  2038. from readline's copy. */
  2039. /* Not supported here. */
  2040. #undef VISIBLE_STATS
  2041. #if defined (HANDLE_MULTIBYTE)
  2042. #define MB_INVALIDCH(x) ((x) == (size_t)-1 || (x) == (size_t)-2)
  2043. #define MB_NULLWCH(x) ((x) == 0)
  2044. #endif
  2045. #define ELLIPSIS_LEN 3
  2046. /* gdb version of readline/complete.c:get_y_or_n.
  2047. 'y' -> returns 1, and 'n' -> returns 0.
  2048. Also supported: space == 'y', RUBOUT == 'n', ctrl-g == start over.
  2049. If FOR_PAGER is non-zero, then also supported are:
  2050. NEWLINE or RETURN -> returns 2, and 'q' -> returns 0. */
  2051. static int
  2052. gdb_get_y_or_n (int for_pager, const struct match_list_displayer *displayer)
  2053. {
  2054. int c;
  2055. for (;;)
  2056. {
  2057. RL_SETSTATE (RL_STATE_MOREINPUT);
  2058. c = displayer->read_key (displayer);
  2059. RL_UNSETSTATE (RL_STATE_MOREINPUT);
  2060. if (c == 'y' || c == 'Y' || c == ' ')
  2061. return 1;
  2062. if (c == 'n' || c == 'N' || c == RUBOUT)
  2063. return 0;
  2064. if (c == ABORT_CHAR || c < 0)
  2065. {
  2066. /* Readline doesn't erase_entire_line here, but without it the
  2067. --More-- prompt isn't erased and neither is the text entered
  2068. thus far redisplayed. */
  2069. displayer->erase_entire_line (displayer);
  2070. /* Note: The arguments to rl_abort are ignored. */
  2071. rl_abort (0, 0);
  2072. }
  2073. if (for_pager && (c == NEWLINE || c == RETURN))
  2074. return 2;
  2075. if (for_pager && (c == 'q' || c == 'Q'))
  2076. return 0;
  2077. displayer->beep (displayer);
  2078. }
  2079. }
  2080. /* Pager function for tab-completion.
  2081. This is based on readline/complete.c:_rl_internal_pager.
  2082. LINES is the number of lines of output displayed thus far.
  2083. Returns:
  2084. -1 -> user pressed 'n' or equivalent,
  2085. 0 -> user pressed 'y' or equivalent,
  2086. N -> user pressed NEWLINE or equivalent and N is LINES - 1. */
  2087. static int
  2088. gdb_display_match_list_pager (int lines,
  2089. const struct match_list_displayer *displayer)
  2090. {
  2091. int i;
  2092. displayer->puts (displayer, "--More--");
  2093. displayer->flush (displayer);
  2094. i = gdb_get_y_or_n (1, displayer);
  2095. displayer->erase_entire_line (displayer);
  2096. if (i == 0)
  2097. return -1;
  2098. else if (i == 2)
  2099. return (lines - 1);
  2100. else
  2101. return 0;
  2102. }
  2103. /* Return non-zero if FILENAME is a directory.
  2104. Based on readline/complete.c:path_isdir. */
  2105. static int
  2106. gdb_path_isdir (const char *filename)
  2107. {
  2108. struct stat finfo;
  2109. return (stat (filename, &finfo) == 0 && S_ISDIR (finfo.st_mode));
  2110. }
  2111. /* Return the portion of PATHNAME that should be output when listing
  2112. possible completions. If we are hacking filename completion, we
  2113. are only interested in the basename, the portion following the
  2114. final slash. Otherwise, we return what we were passed. Since
  2115. printing empty strings is not very informative, if we're doing
  2116. filename completion, and the basename is the empty string, we look
  2117. for the previous slash and return the portion following that. If
  2118. there's no previous slash, we just return what we were passed.
  2119. Based on readline/complete.c:printable_part. */
  2120. static char *
  2121. gdb_printable_part (char *pathname)
  2122. {
  2123. char *temp, *x;
  2124. if (rl_filename_completion_desired == 0) /* don't need to do anything */
  2125. return (pathname);
  2126. temp = strrchr (pathname, '/');
  2127. #if defined (__MSDOS__)
  2128. if (temp == 0 && ISALPHA ((unsigned char)pathname[0]) && pathname[1] == ':')
  2129. temp = pathname + 1;
  2130. #endif
  2131. if (temp == 0 || *temp == '\0')
  2132. return (pathname);
  2133. /* If the basename is NULL, we might have a pathname like '/usr/src/'.
  2134. Look for a previous slash and, if one is found, return the portion
  2135. following that slash. If there's no previous slash, just return the
  2136. pathname we were passed. */
  2137. else if (temp[1] == '\0')
  2138. {
  2139. for (x = temp - 1; x > pathname; x--)
  2140. if (*x == '/')
  2141. break;
  2142. return ((*x == '/') ? x + 1 : pathname);
  2143. }
  2144. else
  2145. return ++temp;
  2146. }
  2147. /* Compute width of STRING when displayed on screen by print_filename.
  2148. Based on readline/complete.c:fnwidth. */
  2149. static int
  2150. gdb_fnwidth (const char *string)
  2151. {
  2152. int width, pos;
  2153. #if defined (HANDLE_MULTIBYTE)
  2154. mbstate_t ps;
  2155. int left, w;
  2156. size_t clen;
  2157. wchar_t wc;
  2158. left = strlen (string) + 1;
  2159. memset (&ps, 0, sizeof (mbstate_t));
  2160. #endif
  2161. width = pos = 0;
  2162. while (string[pos])
  2163. {
  2164. if (CTRL_CHAR (string[pos]) || string[pos] == RUBOUT)
  2165. {
  2166. width += 2;
  2167. pos++;
  2168. }
  2169. else
  2170. {
  2171. #if defined (HANDLE_MULTIBYTE)
  2172. clen = mbrtowc (&wc, string + pos, left - pos, &ps);
  2173. if (MB_INVALIDCH (clen))
  2174. {
  2175. width++;
  2176. pos++;
  2177. memset (&ps, 0, sizeof (mbstate_t));
  2178. }
  2179. else if (MB_NULLWCH (clen))
  2180. break;
  2181. else
  2182. {
  2183. pos += clen;
  2184. w = wcwidth (wc);
  2185. width += (w >= 0) ? w : 1;
  2186. }
  2187. #else
  2188. width++;
  2189. pos++;
  2190. #endif
  2191. }
  2192. }
  2193. return width;
  2194. }
  2195. /* Print TO_PRINT, one matching completion.
  2196. PREFIX_BYTES is number of common prefix bytes.
  2197. Based on readline/complete.c:fnprint. */
  2198. static int
  2199. gdb_fnprint (const char *to_print, int prefix_bytes,
  2200. const struct match_list_displayer *displayer)
  2201. {
  2202. int printed_len, w;
  2203. const char *s;
  2204. #if defined (HANDLE_MULTIBYTE)
  2205. mbstate_t ps;
  2206. const char *end;
  2207. size_t tlen;
  2208. int width;
  2209. wchar_t wc;
  2210. end = to_print + strlen (to_print) + 1;
  2211. memset (&ps, 0, sizeof (mbstate_t));
  2212. #endif
  2213. printed_len = 0;
  2214. /* Don't print only the ellipsis if the common prefix is one of the
  2215. possible completions */
  2216. if (to_print[prefix_bytes] == '\0')
  2217. prefix_bytes = 0;
  2218. if (prefix_bytes)
  2219. {
  2220. char ellipsis;
  2221. ellipsis = (to_print[prefix_bytes] == '.') ? '_' : '.';
  2222. for (w = 0; w < ELLIPSIS_LEN; w++)
  2223. displayer->putch (displayer, ellipsis);
  2224. printed_len = ELLIPSIS_LEN;
  2225. }
  2226. s = to_print + prefix_bytes;
  2227. while (*s)
  2228. {
  2229. if (CTRL_CHAR (*s))
  2230. {
  2231. displayer->putch (displayer, '^');
  2232. displayer->putch (displayer, UNCTRL (*s));
  2233. printed_len += 2;
  2234. s++;
  2235. #if defined (HANDLE_MULTIBYTE)
  2236. memset (&ps, 0, sizeof (mbstate_t));
  2237. #endif
  2238. }
  2239. else if (*s == RUBOUT)
  2240. {
  2241. displayer->putch (displayer, '^');
  2242. displayer->putch (displayer, '?');
  2243. printed_len += 2;
  2244. s++;
  2245. #if defined (HANDLE_MULTIBYTE)
  2246. memset (&ps, 0, sizeof (mbstate_t));
  2247. #endif
  2248. }
  2249. else
  2250. {
  2251. #if defined (HANDLE_MULTIBYTE)
  2252. tlen = mbrtowc (&wc, s, end - s, &ps);
  2253. if (MB_INVALIDCH (tlen))
  2254. {
  2255. tlen = 1;
  2256. width = 1;
  2257. memset (&ps, 0, sizeof (mbstate_t));
  2258. }
  2259. else if (MB_NULLWCH (tlen))
  2260. break;
  2261. else
  2262. {
  2263. w = wcwidth (wc);
  2264. width = (w >= 0) ? w : 1;
  2265. }
  2266. for (w = 0; w < tlen; ++w)
  2267. displayer->putch (displayer, s[w]);
  2268. s += tlen;
  2269. printed_len += width;
  2270. #else
  2271. displayer->putch (displayer, *s);
  2272. s++;
  2273. printed_len++;
  2274. #endif
  2275. }
  2276. }
  2277. return printed_len;
  2278. }
  2279. /* Output TO_PRINT to rl_outstream. If VISIBLE_STATS is defined and we
  2280. are using it, check for and output a single character for `special'
  2281. filenames. Return the number of characters we output.
  2282. Based on readline/complete.c:print_filename. */
  2283. static int
  2284. gdb_print_filename (char *to_print, char *full_pathname, int prefix_bytes,
  2285. const struct match_list_displayer *displayer)
  2286. {
  2287. int printed_len, extension_char, slen, tlen;
  2288. char *s, c, *new_full_pathname;
  2289. const char *dn;
  2290. extern int _rl_complete_mark_directories;
  2291. extension_char = 0;
  2292. printed_len = gdb_fnprint (to_print, prefix_bytes, displayer);
  2293. #if defined (VISIBLE_STATS)
  2294. if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories))
  2295. #else
  2296. if (rl_filename_completion_desired && _rl_complete_mark_directories)
  2297. #endif
  2298. {
  2299. /* If to_print != full_pathname, to_print is the basename of the
  2300. path passed. In this case, we try to expand the directory
  2301. name before checking for the stat character. */
  2302. if (to_print != full_pathname)
  2303. {
  2304. /* Terminate the directory name. */
  2305. c = to_print[-1];
  2306. to_print[-1] = '\0';
  2307. /* If setting the last slash in full_pathname to a NUL results in
  2308. full_pathname being the empty string, we are trying to complete
  2309. files in the root directory. If we pass a null string to the
  2310. bash directory completion hook, for example, it will expand it
  2311. to the current directory. We just want the `/'. */
  2312. if (full_pathname == 0 || *full_pathname == 0)
  2313. dn = "/";
  2314. else if (full_pathname[0] != '/')
  2315. dn = full_pathname;
  2316. else if (full_pathname[1] == 0)
  2317. dn = "//"; /* restore trailing slash to `//' */
  2318. else if (full_pathname[1] == '/' && full_pathname[2] == 0)
  2319. dn = "/"; /* don't turn /// into // */
  2320. else
  2321. dn = full_pathname;
  2322. s = tilde_expand (dn);
  2323. if (rl_directory_completion_hook)
  2324. (*rl_directory_completion_hook) (&s);
  2325. slen = strlen (s);
  2326. tlen = strlen (to_print);
  2327. new_full_pathname = (char *)xmalloc (slen + tlen + 2);
  2328. strcpy (new_full_pathname, s);
  2329. if (s[slen - 1] == '/')
  2330. slen--;
  2331. else
  2332. new_full_pathname[slen] = '/';
  2333. new_full_pathname[slen] = '/';
  2334. strcpy (new_full_pathname + slen + 1, to_print);
  2335. #if defined (VISIBLE_STATS)
  2336. if (rl_visible_stats)
  2337. extension_char = stat_char (new_full_pathname);
  2338. else
  2339. #endif
  2340. if (gdb_path_isdir (new_full_pathname))
  2341. extension_char = '/';
  2342. xfree (new_full_pathname);
  2343. to_print[-1] = c;
  2344. }
  2345. else
  2346. {
  2347. s = tilde_expand (full_pathname);
  2348. #if defined (VISIBLE_STATS)
  2349. if (rl_visible_stats)
  2350. extension_char = stat_char (s);
  2351. else
  2352. #endif
  2353. if (gdb_path_isdir (s))
  2354. extension_char = '/';
  2355. }
  2356. xfree (s);
  2357. if (extension_char)
  2358. {
  2359. displayer->putch (displayer, extension_char);
  2360. printed_len++;
  2361. }
  2362. }
  2363. return printed_len;
  2364. }
  2365. /* GDB version of readline/complete.c:complete_get_screenwidth. */
  2366. static int
  2367. gdb_complete_get_screenwidth (const struct match_list_displayer *displayer)
  2368. {
  2369. /* Readline has other stuff here which it's not clear we need. */
  2370. return displayer->width;
  2371. }
  2372. extern int _rl_completion_prefix_display_length;
  2373. extern int _rl_print_completions_horizontally;
  2374. EXTERN_C int _rl_qsort_string_compare (const void *, const void *);
  2375. typedef int QSFUNC (const void *, const void *);
  2376. /* GDB version of readline/complete.c:rl_display_match_list.
  2377. See gdb_display_match_list for a description of MATCHES, LEN, MAX.
  2378. Returns non-zero if all matches are displayed. */
  2379. static int
  2380. gdb_display_match_list_1 (char **matches, int len, int max,
  2381. const struct match_list_displayer *displayer)
  2382. {
  2383. int count, limit, printed_len, lines, cols;
  2384. int i, j, k, l, common_length, sind;
  2385. char *temp, *t;
  2386. int page_completions = displayer->height != INT_MAX && pagination_enabled;
  2387. /* Find the length of the prefix common to all items: length as displayed
  2388. characters (common_length) and as a byte index into the matches (sind) */
  2389. common_length = sind = 0;
  2390. if (_rl_completion_prefix_display_length > 0)
  2391. {
  2392. t = gdb_printable_part (matches[0]);
  2393. temp = strrchr (t, '/');
  2394. common_length = temp ? gdb_fnwidth (temp) : gdb_fnwidth (t);
  2395. sind = temp ? strlen (temp) : strlen (t);
  2396. if (common_length > _rl_completion_prefix_display_length && common_length > ELLIPSIS_LEN)
  2397. max -= common_length - ELLIPSIS_LEN;
  2398. else
  2399. common_length = sind = 0;
  2400. }
  2401. /* How many items of MAX length can we fit in the screen window? */
  2402. cols = gdb_complete_get_screenwidth (displayer);
  2403. max += 2;
  2404. limit = cols / max;
  2405. if (limit != 1 && (limit * max == cols))
  2406. limit--;
  2407. /* If cols == 0, limit will end up -1 */
  2408. if (cols < displayer->width && limit < 0)
  2409. limit = 1;
  2410. /* Avoid a possible floating exception. If max > cols,
  2411. limit will be 0 and a divide-by-zero fault will result. */
  2412. if (limit == 0)
  2413. limit = 1;
  2414. /* How many iterations of the printing loop? */
  2415. count = (len + (limit - 1)) / limit;
  2416. /* Watch out for special case. If LEN is less than LIMIT, then
  2417. just do the inner printing loop.
  2418. 0 < len <= limit implies count = 1. */
  2419. /* Sort the items if they are not already sorted. */
  2420. if (rl_ignore_completion_duplicates == 0 && rl_sort_completion_matches)
  2421. qsort (matches + 1, len, sizeof (char *), (QSFUNC *)_rl_qsort_string_compare);
  2422. displayer->crlf (displayer);
  2423. lines = 0;
  2424. if (_rl_print_completions_horizontally == 0)
  2425. {
  2426. /* Print the sorted items, up-and-down alphabetically, like ls. */
  2427. for (i = 1; i <= count; i++)
  2428. {
  2429. for (j = 0, l = i; j < limit; j++)
  2430. {
  2431. if (l > len || matches[l] == 0)
  2432. break;
  2433. else
  2434. {
  2435. temp = gdb_printable_part (matches[l]);
  2436. printed_len = gdb_print_filename (temp, matches[l], sind,
  2437. displayer);
  2438. if (j + 1 < limit)
  2439. for (k = 0; k < max - printed_len; k++)
  2440. displayer->putch (displayer, ' ');
  2441. }
  2442. l += count;
  2443. }
  2444. displayer->crlf (displayer);
  2445. lines++;
  2446. if (page_completions && lines >= (displayer->height - 1) && i < count)
  2447. {
  2448. lines = gdb_display_match_list_pager (lines, displayer);
  2449. if (lines < 0)
  2450. return 0;
  2451. }
  2452. }
  2453. }
  2454. else
  2455. {
  2456. /* Print the sorted items, across alphabetically, like ls -x. */
  2457. for (i = 1; matches[i]; i++)
  2458. {
  2459. temp = gdb_printable_part (matches[i]);
  2460. printed_len = gdb_print_filename (temp, matches[i], sind, displayer);
  2461. /* Have we reached the end of this line? */
  2462. if (matches[i+1])
  2463. {
  2464. if (i && (limit > 1) && (i % limit) == 0)
  2465. {
  2466. displayer->crlf (displayer);
  2467. lines++;
  2468. if (page_completions && lines >= displayer->height - 1)
  2469. {
  2470. lines = gdb_display_match_list_pager (lines, displayer);
  2471. if (lines < 0)
  2472. return 0;
  2473. }
  2474. }
  2475. else
  2476. for (k = 0; k < max - printed_len; k++)
  2477. displayer->putch (displayer, ' ');
  2478. }
  2479. }
  2480. displayer->crlf (displayer);
  2481. }
  2482. return 1;
  2483. }
  2484. /* Utility for displaying completion list matches, used by both CLI and TUI.
  2485. MATCHES is the list of strings, in argv format, LEN is the number of
  2486. strings in MATCHES, and MAX is the length of the longest string in
  2487. MATCHES. */
  2488. void
  2489. gdb_display_match_list (char **matches, int len, int max,
  2490. const struct match_list_displayer *displayer)
  2491. {
  2492. /* Readline will never call this if complete_line returned NULL. */
  2493. gdb_assert (max_completions != 0);
  2494. /* complete_line will never return more than this. */
  2495. if (max_completions > 0)
  2496. gdb_assert (len <= max_completions);
  2497. if (rl_completion_query_items > 0 && len >= rl_completion_query_items)
  2498. {
  2499. char msg[100];
  2500. /* We can't use *query here because they wait for <RET> which is
  2501. wrong here. This follows the readline version as closely as possible
  2502. for compatibility's sake. See readline/complete.c. */
  2503. displayer->crlf (displayer);
  2504. xsnprintf (msg, sizeof (msg),
  2505. "Display all %d possibilities? (y or n)", len);
  2506. displayer->puts (displayer, msg);
  2507. displayer->flush (displayer);
  2508. if (gdb_get_y_or_n (0, displayer) == 0)
  2509. {
  2510. displayer->crlf (displayer);
  2511. return;
  2512. }
  2513. }
  2514. if (gdb_display_match_list_1 (matches, len, max, displayer))
  2515. {
  2516. /* Note: MAX_COMPLETIONS may be -1 or zero, but LEN is always > 0. */
  2517. if (len == max_completions)
  2518. {
  2519. /* The maximum number of completions has been reached. Warn the user
  2520. that there may be more. */
  2521. const char *message = get_max_completions_reached_message ();
  2522. displayer->puts (displayer, message);
  2523. displayer->crlf (displayer);
  2524. }
  2525. }
  2526. }
  2527. void _initialize_completer ();
  2528. void
  2529. _initialize_completer ()
  2530. {
  2531. add_setshow_zuinteger_unlimited_cmd ("max-completions", no_class,
  2532. &max_completions, _("\
  2533. Set maximum number of completion candidates."), _("\
  2534. Show maximum number of completion candidates."), _("\
  2535. Use this to limit the number of candidates considered\n\
  2536. during completion. Specifying \"unlimited\" or -1\n\
  2537. disables limiting. Note that setting either no limit or\n\
  2538. a very large limit can make completion slow."),
  2539. NULL, NULL, &setlist, &showlist);
  2540. }