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 
271f4706c3SVince Harron OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
28b5f0feabSGreg Clayton     OptionValue(),
29b5f0feabSGreg Clayton     m_current_value (),
30b5f0feabSGreg Clayton     m_default_value (),
31b5f0feabSGreg Clayton     m_data_sp(),
321f4706c3SVince Harron     m_completion_mask (CommandCompletions::eDiskFileCompletion),
331f4706c3SVince Harron     m_resolve (resolve)
34b5f0feabSGreg Clayton {
35b5f0feabSGreg Clayton }
36b5f0feabSGreg Clayton 
371f4706c3SVince Harron OptionValueFileSpec::OptionValueFileSpec (const FileSpec &value,
381f4706c3SVince Harron                                           bool resolve) :
39b5f0feabSGreg Clayton     OptionValue(),
40b5f0feabSGreg Clayton     m_current_value (value),
41b5f0feabSGreg Clayton     m_default_value (value),
42b5f0feabSGreg Clayton     m_data_sp(),
431f4706c3SVince Harron     m_completion_mask (CommandCompletions::eDiskFileCompletion),
441f4706c3SVince Harron     m_resolve (resolve)
45b5f0feabSGreg Clayton {
46b5f0feabSGreg Clayton }
47b5f0feabSGreg Clayton 
48b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
491f4706c3SVince Harron                                           const FileSpec &default_value,
501f4706c3SVince Harron                                           bool resolve) :
51b5f0feabSGreg Clayton     OptionValue(),
52b5f0feabSGreg Clayton     m_current_value (current_value),
53b5f0feabSGreg Clayton     m_default_value (default_value),
54b5f0feabSGreg Clayton     m_data_sp(),
551f4706c3SVince Harron     m_completion_mask (CommandCompletions::eDiskFileCompletion),
561f4706c3SVince Harron     m_resolve (resolve)
57b5f0feabSGreg Clayton {
58b5f0feabSGreg Clayton }
59b5f0feabSGreg Clayton 
6067cc0636SGreg Clayton void
6167cc0636SGreg Clayton OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
6267cc0636SGreg Clayton {
6367cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
6467cc0636SGreg Clayton         strm.Printf ("(%s)", GetTypeAsCString ());
6567cc0636SGreg Clayton     if (dump_mask & eDumpOptionValue)
6667cc0636SGreg Clayton     {
6767cc0636SGreg Clayton         if (dump_mask & eDumpOptionType)
6867cc0636SGreg Clayton             strm.PutCString (" = ");
6967cc0636SGreg Clayton 
7067cc0636SGreg Clayton         if (m_current_value)
7167cc0636SGreg Clayton         {
72b5ad4ec7SGreg Clayton             strm << '"' << m_current_value.GetPath().c_str() << '"';
7367cc0636SGreg Clayton         }
7467cc0636SGreg Clayton     }
7567cc0636SGreg Clayton }
7667cc0636SGreg Clayton 
7767cc0636SGreg Clayton Error
78*c95f7e2aSPavel Labath OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
7967cc0636SGreg Clayton                                           VarSetOperationType op)
8067cc0636SGreg Clayton {
8167cc0636SGreg Clayton     Error error;
8267cc0636SGreg Clayton     switch (op)
8367cc0636SGreg Clayton     {
8467cc0636SGreg Clayton     case eVarSetOperationClear:
8567cc0636SGreg Clayton         Clear ();
86332e8b1cSGreg Clayton         NotifyValueChanged();
8767cc0636SGreg Clayton         break;
8867cc0636SGreg Clayton 
8967cc0636SGreg Clayton     case eVarSetOperationReplace:
9067cc0636SGreg Clayton     case eVarSetOperationAssign:
91*c95f7e2aSPavel Labath         if (value.size() > 0)
9267cc0636SGreg Clayton         {
935c98b1cfSJason Molenda             // The setting value may have whitespace, double-quotes, or single-quotes around the file
945c98b1cfSJason Molenda             // path to indicate that internal spaces are not word breaks.  Strip off any ws & quotes
955c98b1cfSJason Molenda             // from the start and end of the file path - we aren't doing any word // breaking here so
965c98b1cfSJason Molenda             // the quoting is unnecessary.  NB this will cause a problem if someone tries to specify
975c98b1cfSJason Molenda             // a file path that legitimately begins or ends with a " or ' character, or whitespace.
98*c95f7e2aSPavel Labath             value = value.trim("\"' \t");
9967cc0636SGreg Clayton             m_value_was_set = true;
100*c95f7e2aSPavel Labath             m_current_value.SetFile(value.str().c_str(), m_resolve);
1012e3881c0SJim Ingham             m_data_sp.reset();
102332e8b1cSGreg Clayton             NotifyValueChanged();
10367cc0636SGreg Clayton         }
10467cc0636SGreg Clayton         else
10567cc0636SGreg Clayton         {
10667cc0636SGreg Clayton             error.SetErrorString("invalid value string");
10767cc0636SGreg Clayton         }
10867cc0636SGreg Clayton         break;
10967cc0636SGreg Clayton 
11067cc0636SGreg Clayton     case eVarSetOperationInsertBefore:
11167cc0636SGreg Clayton     case eVarSetOperationInsertAfter:
11267cc0636SGreg Clayton     case eVarSetOperationRemove:
11367cc0636SGreg Clayton     case eVarSetOperationAppend:
11467cc0636SGreg Clayton     case eVarSetOperationInvalid:
115*c95f7e2aSPavel Labath         error = OptionValue::SetValueFromString (value, op);
11667cc0636SGreg Clayton         break;
11767cc0636SGreg Clayton     }
11867cc0636SGreg Clayton     return error;
11967cc0636SGreg Clayton }
12067cc0636SGreg Clayton 
12167cc0636SGreg Clayton lldb::OptionValueSP
12267cc0636SGreg Clayton OptionValueFileSpec::DeepCopy () const
12367cc0636SGreg Clayton {
12467cc0636SGreg Clayton     return OptionValueSP(new OptionValueFileSpec(*this));
12567cc0636SGreg Clayton }
12667cc0636SGreg Clayton 
12767cc0636SGreg Clayton 
12867cc0636SGreg Clayton size_t
12967cc0636SGreg Clayton OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
13067cc0636SGreg Clayton                                    const char *s,
13167cc0636SGreg Clayton                                    int match_start_point,
13267cc0636SGreg Clayton                                    int max_return_elements,
13367cc0636SGreg Clayton                                    bool &word_complete,
13467cc0636SGreg Clayton                                    StringList &matches)
13567cc0636SGreg Clayton {
13667cc0636SGreg Clayton     word_complete = false;
13767cc0636SGreg Clayton     matches.Clear();
13867cc0636SGreg Clayton     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
139b5f0feabSGreg Clayton                                                          m_completion_mask,
14067cc0636SGreg Clayton                                                          s,
14167cc0636SGreg Clayton                                                          match_start_point,
14267cc0636SGreg Clayton                                                          max_return_elements,
143d78c9576SEd Maste                                                          nullptr,
14467cc0636SGreg Clayton                                                          word_complete,
14567cc0636SGreg Clayton                                                          matches);
14667cc0636SGreg Clayton     return matches.GetSize();
14767cc0636SGreg Clayton }
14867cc0636SGreg Clayton 
14967cc0636SGreg Clayton 
15067cc0636SGreg Clayton 
1516920b52bSGreg Clayton const lldb::DataBufferSP &
1520b0b512fSGreg Clayton OptionValueFileSpec::GetFileContents(bool null_terminate)
1536920b52bSGreg Clayton {
1546920b52bSGreg Clayton     if (!m_data_sp && m_current_value)
1550b0b512fSGreg Clayton     {
1560b0b512fSGreg Clayton         if (null_terminate)
1570b0b512fSGreg Clayton             m_data_sp = m_current_value.ReadFileContentsAsCString();
1580b0b512fSGreg Clayton         else
1596920b52bSGreg Clayton             m_data_sp = m_current_value.ReadFileContents();
1600b0b512fSGreg Clayton     }
1616920b52bSGreg Clayton     return m_data_sp;
1626920b52bSGreg Clayton }
1636920b52bSGreg Clayton 
16467cc0636SGreg Clayton 
165