LCOV - code coverage report
Current view: top level - lib/LineEditor - LineEditor.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 60 106 56.6 %
Date: 2018-10-20 13:21:21 Functions: 9 15 60.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- LineEditor.cpp - line editor --------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/LineEditor/LineEditor.h"
      11             : #include "llvm/ADT/SmallString.h"
      12             : #include "llvm/Config/config.h"
      13             : #include "llvm/Support/Path.h"
      14             : #include "llvm/Support/raw_ostream.h"
      15             : #include <algorithm>
      16             : #include <cassert>
      17             : #include <cstdio>
      18             : #ifdef HAVE_LIBEDIT
      19             : #include <histedit.h>
      20             : #endif
      21             : 
      22             : using namespace llvm;
      23             : 
      24           0 : std::string LineEditor::getDefaultHistoryPath(StringRef ProgName) {
      25             :   SmallString<32> Path;
      26           0 :   if (sys::path::home_directory(Path)) {
      27           0 :     sys::path::append(Path, "." + ProgName + "-history");
      28             :     return Path.str();
      29             :   }
      30             :   return std::string();
      31             : }
      32             : 
      33           3 : LineEditor::CompleterConcept::~CompleterConcept() {}
      34           3 : LineEditor::ListCompleterConcept::~ListCompleterConcept() {}
      35             : 
      36           3 : std::string LineEditor::ListCompleterConcept::getCommonPrefix(
      37             :     const std::vector<Completion> &Comps) {
      38             :   assert(!Comps.empty());
      39             : 
      40           3 :   std::string CommonPrefix = Comps[0].TypedText;
      41           3 :   for (std::vector<Completion>::const_iterator I = Comps.begin() + 1,
      42           3 :                                                E = Comps.end();
      43           7 :        I != E; ++I) {
      44           5 :     size_t Len = std::min(CommonPrefix.size(), I->TypedText.size());
      45             :     size_t CommonLen = 0;
      46           7 :     for (; CommonLen != Len; ++CommonLen) {
      47          10 :       if (CommonPrefix[CommonLen] != I->TypedText[CommonLen])
      48             :         break;
      49             :     }
      50             :     CommonPrefix.resize(CommonLen);
      51             :   }
      52           3 :   return CommonPrefix;
      53             : }
      54             : 
      55             : LineEditor::CompletionAction
      56           3 : LineEditor::ListCompleterConcept::complete(StringRef Buffer, size_t Pos) const {
      57             :   CompletionAction Action;
      58           6 :   std::vector<Completion> Comps = getCompletions(Buffer, Pos);
      59           3 :   if (Comps.empty()) {
      60           0 :     Action.Kind = CompletionAction::AK_ShowCompletions;
      61           0 :     return Action;
      62             :   }
      63             : 
      64           3 :   std::string CommonPrefix = getCommonPrefix(Comps);
      65             : 
      66             :   // If the common prefix is non-empty we can simply insert it. If there is a
      67             :   // single completion, this will insert the full completion. If there is more
      68             :   // than one, this might be enough information to jog the user's memory but if
      69             :   // not the user can also hit tab again to see the completions because the
      70             :   // common prefix will then be empty.
      71           3 :   if (CommonPrefix.empty()) {
      72           1 :     Action.Kind = CompletionAction::AK_ShowCompletions;
      73             :     for (std::vector<Completion>::iterator I = Comps.begin(), E = Comps.end();
      74           3 :          I != E; ++I)
      75           2 :       Action.Completions.push_back(I->DisplayText);
      76             :   } else {
      77           2 :     Action.Kind = CompletionAction::AK_Insert;
      78           2 :     Action.Text = CommonPrefix;
      79             :   }
      80             : 
      81             :   return Action;
      82             : }
      83             : 
      84           3 : LineEditor::CompletionAction LineEditor::getCompletionAction(StringRef Buffer,
      85             :                                                              size_t Pos) const {
      86           3 :   if (!Completer) {
      87           0 :     CompletionAction Action;
      88           0 :     Action.Kind = CompletionAction::AK_ShowCompletions;
      89             :     return Action;
      90             :   }
      91             : 
      92           3 :   return Completer->complete(Buffer, Pos);
      93             : }
      94             : 
      95             : #ifdef HAVE_LIBEDIT
      96             : 
      97             : // libedit-based implementation.
      98             : 
      99           0 : struct LineEditor::InternalData {
     100             :   LineEditor *LE;
     101             : 
     102             :   History *Hist;
     103             :   EditLine *EL;
     104             : 
     105             :   unsigned PrevCount;
     106             :   std::string ContinuationOutput;
     107             : 
     108             :   FILE *Out;
     109             : };
     110             : 
     111             : namespace {
     112             : 
     113           0 : const char *ElGetPromptFn(EditLine *EL) {
     114             :   LineEditor::InternalData *Data;
     115           0 :   if (el_get(EL, EL_CLIENTDATA, &Data) == 0)
     116           0 :     return Data->LE->getPrompt().c_str();
     117             :   return "> ";
     118             : }
     119             : 
     120             : // Handles tab completion.
     121             : //
     122             : // This function is really horrible. But since the alternative is to get into
     123             : // the line editor business, here we are.
     124           0 : unsigned char ElCompletionFn(EditLine *EL, int ch) {
     125             :   LineEditor::InternalData *Data;
     126           0 :   if (el_get(EL, EL_CLIENTDATA, &Data) == 0) {
     127           0 :     if (!Data->ContinuationOutput.empty()) {
     128             :       // This is the continuation of the AK_ShowCompletions branch below.
     129           0 :       FILE *Out = Data->Out;
     130             : 
     131             :       // Print the required output (see below).
     132           0 :       ::fwrite(Data->ContinuationOutput.c_str(),
     133             :                Data->ContinuationOutput.size(), 1, Out);
     134             : 
     135             :       // Push a sequence of Ctrl-B characters to move the cursor back to its
     136             :       // original position.
     137           0 :       std::string Prevs(Data->PrevCount, '\02');
     138           0 :       ::el_push(EL, const_cast<char *>(Prevs.c_str()));
     139             : 
     140           0 :       Data->ContinuationOutput.clear();
     141             : 
     142             :       return CC_REFRESH;
     143             :     }
     144             : 
     145           0 :     const LineInfo *LI = ::el_line(EL);
     146           0 :     LineEditor::CompletionAction Action = Data->LE->getCompletionAction(
     147           0 :         StringRef(LI->buffer, LI->lastchar - LI->buffer),
     148           0 :         LI->cursor - LI->buffer);
     149           0 :     switch (Action.Kind) {
     150             :     case LineEditor::CompletionAction::AK_Insert:
     151           0 :       ::el_insertstr(EL, Action.Text.c_str());
     152           0 :       return CC_REFRESH;
     153             : 
     154             :     case LineEditor::CompletionAction::AK_ShowCompletions:
     155           0 :       if (Action.Completions.empty()) {
     156             :         return CC_REFRESH_BEEP;
     157             :       } else {
     158             :         // Push a Ctrl-E and a tab. The Ctrl-E causes libedit to move the cursor
     159             :         // to the end of the line, so that when we emit a newline we will be on
     160             :         // a new blank line. The tab causes libedit to call this function again
     161             :         // after moving the cursor. There doesn't seem to be anything we can do
     162             :         // from here to cause libedit to move the cursor immediately. This will
     163             :         // break horribly if the user has rebound their keys, so for now we do
     164             :         // not permit user rebinding.
     165           0 :         ::el_push(EL, const_cast<char *>("\05\t"));
     166             : 
     167             :         // This assembles the output for the continuation block above.
     168           0 :         raw_string_ostream OS(Data->ContinuationOutput);
     169             : 
     170             :         // Move cursor to a blank line.
     171           0 :         OS << "\n";
     172             : 
     173             :         // Emit the completions.
     174             :         for (std::vector<std::string>::iterator I = Action.Completions.begin(),
     175             :                                                 E = Action.Completions.end();
     176           0 :              I != E; ++I) {
     177           0 :           OS << *I << "\n";
     178             :         }
     179             : 
     180             :         // Fool libedit into thinking nothing has changed. Reprint its prompt
     181             :         // and the user input. Note that the cursor will remain at the end of
     182             :         // the line after this.
     183           0 :         OS << Data->LE->getPrompt()
     184           0 :            << StringRef(LI->buffer, LI->lastchar - LI->buffer);
     185             : 
     186             :         // This is the number of characters we need to tell libedit to go back:
     187             :         // the distance between end of line and the original cursor position.
     188           0 :         Data->PrevCount = LI->lastchar - LI->cursor;
     189             : 
     190             :         return CC_REFRESH;
     191             :       }
     192             :     }
     193             :   }
     194             :   return CC_ERROR;
     195             : }
     196             : 
     197             : } // end anonymous namespace
     198             : 
     199           2 : LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
     200           2 :                        FILE *Out, FILE *Err)
     201           2 :     : Prompt((ProgName + "> ").str()), HistoryPath(HistoryPath),
     202           4 :       Data(new InternalData) {
     203           2 :   if (HistoryPath.empty())
     204           0 :     this->HistoryPath = getDefaultHistoryPath(ProgName);
     205             : 
     206           2 :   Data->LE = this;
     207           2 :   Data->Out = Out;
     208             : 
     209           2 :   Data->Hist = ::history_init();
     210             :   assert(Data->Hist);
     211             : 
     212           4 :   Data->EL = ::el_init(ProgName.str().c_str(), In, Out, Err);
     213             :   assert(Data->EL);
     214             : 
     215           2 :   ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
     216           2 :   ::el_set(Data->EL, EL_EDITOR, "emacs");
     217           2 :   ::el_set(Data->EL, EL_HIST, history, Data->Hist);
     218           2 :   ::el_set(Data->EL, EL_ADDFN, "tab_complete", "Tab completion function",
     219             :            ElCompletionFn);
     220           2 :   ::el_set(Data->EL, EL_BIND, "\t", "tab_complete", NULL);
     221           2 :   ::el_set(Data->EL, EL_BIND, "^r", "em-inc-search-prev",
     222             :            NULL); // Cycle through backwards search, entering string
     223           2 :   ::el_set(Data->EL, EL_BIND, "^w", "ed-delete-prev-word",
     224             :            NULL); // Delete previous word, behave like bash does.
     225           2 :   ::el_set(Data->EL, EL_BIND, "\033[3~", "ed-delete-next-char",
     226             :            NULL); // Fix the delete key.
     227           2 :   ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
     228             : 
     229             :   HistEvent HE;
     230           2 :   ::history(Data->Hist, &HE, H_SETSIZE, 800);
     231           2 :   ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
     232           2 :   loadHistory();
     233           2 : }
     234             : 
     235           4 : LineEditor::~LineEditor() {
     236           2 :   saveHistory();
     237             : 
     238           2 :   ::history_end(Data->Hist);
     239           2 :   ::el_end(Data->EL);
     240           2 :   ::fwrite("\n", 1, 1, Data->Out);
     241           2 : }
     242             : 
     243           3 : void LineEditor::saveHistory() {
     244           3 :   if (!HistoryPath.empty()) {
     245             :     HistEvent HE;
     246           3 :     ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
     247             :   }
     248           3 : }
     249             : 
     250           3 : void LineEditor::loadHistory() {
     251           3 :   if (!HistoryPath.empty()) {
     252             :     HistEvent HE;
     253           3 :     ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
     254             :   }
     255           3 : }
     256             : 
     257           0 : Optional<std::string> LineEditor::readLine() const {
     258             :   // Call el_gets to prompt the user and read the user's input.
     259           0 :   int LineLen = 0;
     260           0 :   const char *Line = ::el_gets(Data->EL, &LineLen);
     261             : 
     262             :   // Either of these may mean end-of-file.
     263           0 :   if (!Line || LineLen == 0)
     264             :     return Optional<std::string>();
     265             : 
     266             :   // Strip any newlines off the end of the string.
     267           0 :   while (LineLen > 0 &&
     268           0 :          (Line[LineLen - 1] == '\n' || Line[LineLen - 1] == '\r'))
     269           0 :     --LineLen;
     270             : 
     271             :   HistEvent HE;
     272           0 :   if (LineLen > 0)
     273           0 :     ::history(Data->Hist, &HE, H_ENTER, Line);
     274             : 
     275           0 :   return std::string(Line, LineLen);
     276             : }
     277             : 
     278             : #else // HAVE_LIBEDIT
     279             : 
     280             : // Simple fgets-based implementation.
     281             : 
     282             : struct LineEditor::InternalData {
     283             :   FILE *In;
     284             :   FILE *Out;
     285             : };
     286             : 
     287             : LineEditor::LineEditor(StringRef ProgName, StringRef HistoryPath, FILE *In,
     288             :                        FILE *Out, FILE *Err)
     289             :     : Prompt((ProgName + "> ").str()), Data(new InternalData) {
     290             :   Data->In = In;
     291             :   Data->Out = Out;
     292             : }
     293             : 
     294             : LineEditor::~LineEditor() {
     295             :   ::fwrite("\n", 1, 1, Data->Out);
     296             : }
     297             : 
     298             : void LineEditor::saveHistory() {}
     299             : void LineEditor::loadHistory() {}
     300             : 
     301             : Optional<std::string> LineEditor::readLine() const {
     302             :   ::fprintf(Data->Out, "%s", Prompt.c_str());
     303             : 
     304             :   std::string Line;
     305             :   do {
     306             :     char Buf[64];
     307             :     char *Res = ::fgets(Buf, sizeof(Buf), Data->In);
     308             :     if (!Res) {
     309             :       if (Line.empty())
     310             :         return Optional<std::string>();
     311             :       else
     312             :         return Line;
     313             :     }
     314             :     Line.append(Buf);
     315             :   } while (Line.empty() ||
     316             :            (Line[Line.size() - 1] != '\n' && Line[Line.size() - 1] != '\r'));
     317             : 
     318             :   while (!Line.empty() &&
     319             :          (Line[Line.size() - 1] == '\n' || Line[Line.size() - 1] == '\r'))
     320             :     Line.resize(Line.size() - 1);
     321             : 
     322             :   return Line;
     323             : }
     324             : 
     325             : #endif // HAVE_LIBEDIT

Generated by: LCOV version 1.13