167cc0636SGreg Clayton //===-- OptionValueFileSpec.cpp ---------------------------------*- C++ -*-===//
267cc0636SGreg Clayton //
367cc0636SGreg Clayton //                     The LLVM Compiler Infrastructure
467cc0636SGreg Clayton //
567cc0636SGreg Clayton // This file is distributed under the University of Illinois Open Source
667cc0636SGreg Clayton // License. See LICENSE.TXT for details.
767cc0636SGreg Clayton //
867cc0636SGreg Clayton //===----------------------------------------------------------------------===//
967cc0636SGreg Clayton 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
1267cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueFileSpec.h"
1367cc0636SGreg Clayton 
1467cc0636SGreg Clayton // C Includes
1567cc0636SGreg Clayton // C++ Includes
1667cc0636SGreg Clayton // Other libraries and framework includes
1767cc0636SGreg Clayton // Project includes
1867cc0636SGreg Clayton #include "lldb/Core/State.h"
195548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h"
2067cc0636SGreg Clayton #include "lldb/Interpreter/Args.h"
2167cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
2267cc0636SGreg Clayton 
2367cc0636SGreg Clayton using namespace lldb;
2467cc0636SGreg Clayton using namespace lldb_private;
2567cc0636SGreg Clayton 
26b5f0feabSGreg Clayton 
27b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec () :
28b5f0feabSGreg Clayton     OptionValue(),
29b5f0feabSGreg Clayton     m_current_value (),
30b5f0feabSGreg Clayton     m_default_value (),
31b5f0feabSGreg Clayton     m_data_sp(),
32b5f0feabSGreg Clayton     m_completion_mask (CommandCompletions::eDiskFileCompletion)
33b5f0feabSGreg Clayton {
34b5f0feabSGreg Clayton }
35b5f0feabSGreg Clayton 
36b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value) :
37b5f0feabSGreg Clayton     OptionValue(),
38b5f0feabSGreg Clayton     m_current_value (value),
39b5f0feabSGreg Clayton     m_default_value (value),
40b5f0feabSGreg Clayton     m_data_sp(),
41b5f0feabSGreg Clayton     m_completion_mask (CommandCompletions::eDiskFileCompletion)
42b5f0feabSGreg Clayton {
43b5f0feabSGreg Clayton }
44b5f0feabSGreg Clayton 
45b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
46b5f0feabSGreg Clayton                                           const FileSpec &default_value) :
47b5f0feabSGreg Clayton     OptionValue(),
48b5f0feabSGreg Clayton     m_current_value (current_value),
49b5f0feabSGreg Clayton     m_default_value (default_value),
50b5f0feabSGreg Clayton     m_data_sp(),
51b5f0feabSGreg Clayton     m_completion_mask (CommandCompletions::eDiskFileCompletion)
52b5f0feabSGreg Clayton {
53b5f0feabSGreg Clayton }
54b5f0feabSGreg Clayton 
5567cc0636SGreg Clayton void
5667cc0636SGreg Clayton OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
5767cc0636SGreg Clayton {
5867cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
5967cc0636SGreg Clayton         strm.Printf ("(%s)", GetTypeAsCString ());
6067cc0636SGreg Clayton     if (dump_mask & eDumpOptionValue)
6167cc0636SGreg Clayton     {
6267cc0636SGreg Clayton         if (dump_mask & eDumpOptionType)
6367cc0636SGreg Clayton             strm.PutCString (" = ");
6467cc0636SGreg Clayton 
6567cc0636SGreg Clayton         if (m_current_value)
6667cc0636SGreg Clayton         {
67b5ad4ec7SGreg Clayton             strm << '"' << m_current_value.GetPath().c_str() << '"';
6867cc0636SGreg Clayton         }
6967cc0636SGreg Clayton     }
7067cc0636SGreg Clayton }
7167cc0636SGreg Clayton 
7267cc0636SGreg Clayton Error
7367cc0636SGreg Clayton OptionValueFileSpec::SetValueFromCString (const char *value_cstr,
7467cc0636SGreg Clayton                                           VarSetOperationType op)
7567cc0636SGreg Clayton {
7667cc0636SGreg Clayton     Error error;
7767cc0636SGreg Clayton     switch (op)
7867cc0636SGreg Clayton     {
7967cc0636SGreg Clayton     case eVarSetOperationClear:
8067cc0636SGreg Clayton         Clear ();
8167cc0636SGreg Clayton         break;
8267cc0636SGreg Clayton 
8367cc0636SGreg Clayton     case eVarSetOperationReplace:
8467cc0636SGreg Clayton     case eVarSetOperationAssign:
8567cc0636SGreg Clayton         if (value_cstr && value_cstr[0])
8667cc0636SGreg Clayton         {
87*5c98b1cfSJason Molenda             // The setting value may have whitespace, double-quotes, or single-quotes around the file
88*5c98b1cfSJason Molenda             // path to indicate that internal spaces are not word breaks.  Strip off any ws & quotes
89*5c98b1cfSJason Molenda             // from the start and end of the file path - we aren't doing any word // breaking here so
90*5c98b1cfSJason Molenda             // the quoting is unnecessary.  NB this will cause a problem if someone tries to specify
91*5c98b1cfSJason Molenda             // a file path that legitimately begins or ends with a " or ' character, or whitespace.
92*5c98b1cfSJason Molenda             std::string filepath(value_cstr);
93*5c98b1cfSJason Molenda             auto prefix_chars_to_trim = filepath.find_first_not_of ("\"' \t");
94*5c98b1cfSJason Molenda             if (prefix_chars_to_trim != std::string::npos && prefix_chars_to_trim > 0)
95*5c98b1cfSJason Molenda                 filepath.erase(0, prefix_chars_to_trim);
96*5c98b1cfSJason Molenda             auto suffix_chars_to_trim = filepath.find_last_not_of ("\"' \t");
97*5c98b1cfSJason Molenda             if (suffix_chars_to_trim != std::string::npos && suffix_chars_to_trim < filepath.size())
98*5c98b1cfSJason Molenda                 filepath.erase (suffix_chars_to_trim + 1);
99*5c98b1cfSJason Molenda 
10067cc0636SGreg Clayton             m_value_was_set = true;
101*5c98b1cfSJason Molenda             m_current_value.SetFile(filepath.c_str(), true);
10267cc0636SGreg Clayton         }
10367cc0636SGreg Clayton         else
10467cc0636SGreg Clayton         {
10567cc0636SGreg Clayton             error.SetErrorString("invalid value string");
10667cc0636SGreg Clayton         }
10767cc0636SGreg Clayton         break;
10867cc0636SGreg Clayton 
10967cc0636SGreg Clayton     case eVarSetOperationInsertBefore:
11067cc0636SGreg Clayton     case eVarSetOperationInsertAfter:
11167cc0636SGreg Clayton     case eVarSetOperationRemove:
11267cc0636SGreg Clayton     case eVarSetOperationAppend:
11367cc0636SGreg Clayton     case eVarSetOperationInvalid:
11467cc0636SGreg Clayton         error = OptionValue::SetValueFromCString (value_cstr, op);
11567cc0636SGreg Clayton         break;
11667cc0636SGreg Clayton     }
11767cc0636SGreg Clayton     return error;
11867cc0636SGreg Clayton }
11967cc0636SGreg Clayton 
12067cc0636SGreg Clayton lldb::OptionValueSP
12167cc0636SGreg Clayton OptionValueFileSpec::DeepCopy () const
12267cc0636SGreg Clayton {
12367cc0636SGreg Clayton     return OptionValueSP(new OptionValueFileSpec(*this));
12467cc0636SGreg Clayton }
12567cc0636SGreg Clayton 
12667cc0636SGreg Clayton 
12767cc0636SGreg Clayton size_t
12867cc0636SGreg Clayton OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
12967cc0636SGreg Clayton                                    const char *s,
13067cc0636SGreg Clayton                                    int match_start_point,
13167cc0636SGreg Clayton                                    int max_return_elements,
13267cc0636SGreg Clayton                                    bool &word_complete,
13367cc0636SGreg Clayton                                    StringList &matches)
13467cc0636SGreg Clayton {
13567cc0636SGreg Clayton     word_complete = false;
13667cc0636SGreg Clayton     matches.Clear();
13767cc0636SGreg Clayton     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
138b5f0feabSGreg Clayton                                                          m_completion_mask,
13967cc0636SGreg Clayton                                                          s,
14067cc0636SGreg Clayton                                                          match_start_point,
14167cc0636SGreg Clayton                                                          max_return_elements,
14267cc0636SGreg Clayton                                                          NULL,
14367cc0636SGreg Clayton                                                          word_complete,
14467cc0636SGreg Clayton                                                          matches);
14567cc0636SGreg Clayton     return matches.GetSize();
14667cc0636SGreg Clayton }
14767cc0636SGreg Clayton 
14867cc0636SGreg Clayton 
14967cc0636SGreg Clayton 
1506920b52bSGreg Clayton const lldb::DataBufferSP &
1510b0b512fSGreg Clayton OptionValueFileSpec::GetFileContents(bool null_terminate)
1526920b52bSGreg Clayton {
1536920b52bSGreg Clayton     if (!m_data_sp && m_current_value)
1540b0b512fSGreg Clayton     {
1550b0b512fSGreg Clayton         if (null_terminate)
1560b0b512fSGreg Clayton             m_data_sp = m_current_value.ReadFileContentsAsCString();
1570b0b512fSGreg Clayton         else
1586920b52bSGreg Clayton             m_data_sp = m_current_value.ReadFileContents();
1590b0b512fSGreg Clayton     }
1606920b52bSGreg Clayton     return m_data_sp;
1616920b52bSGreg Clayton }
1626920b52bSGreg Clayton 
16367cc0636SGreg Clayton 
164