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 
1067cc0636SGreg Clayton #include "lldb/Interpreter/OptionValueFileSpec.h"
1167cc0636SGreg Clayton 
1267cc0636SGreg Clayton // C Includes
1367cc0636SGreg Clayton // C++ Includes
1467cc0636SGreg Clayton // Other libraries and framework includes
1567cc0636SGreg Clayton // Project includes
1667cc0636SGreg Clayton #include "lldb/Core/State.h"
175548cb50SEnrico Granata #include "lldb/DataFormatters/FormatManager.h"
1867cc0636SGreg Clayton #include "lldb/Interpreter/Args.h"
1967cc0636SGreg Clayton #include "lldb/Interpreter/CommandCompletions.h"
20*e1cfbc79STodd Fiala #include "lldb/Interpreter/CommandInterpreter.h"
2167cc0636SGreg Clayton 
2267cc0636SGreg Clayton using namespace lldb;
2367cc0636SGreg Clayton using namespace lldb_private;
2467cc0636SGreg Clayton 
25b5f0feabSGreg Clayton 
261f4706c3SVince Harron OptionValueFileSpec::OptionValueFileSpec (bool resolve) :
27b5f0feabSGreg Clayton     OptionValue(),
28b5f0feabSGreg Clayton     m_current_value (),
29b5f0feabSGreg Clayton     m_default_value (),
30b5f0feabSGreg Clayton     m_data_sp(),
3139fb1389SGreg Clayton     m_data_mod_time (),
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(),
4339fb1389SGreg Clayton     m_data_mod_time (),
441f4706c3SVince Harron     m_completion_mask (CommandCompletions::eDiskFileCompletion),
451f4706c3SVince Harron     m_resolve (resolve)
46b5f0feabSGreg Clayton {
47b5f0feabSGreg Clayton }
48b5f0feabSGreg Clayton 
49b5f0feabSGreg Clayton OptionValueFileSpec::OptionValueFileSpec (const FileSpec &current_value,
501f4706c3SVince Harron                                           const FileSpec &default_value,
511f4706c3SVince Harron                                           bool resolve) :
52b5f0feabSGreg Clayton     OptionValue(),
53b5f0feabSGreg Clayton     m_current_value (current_value),
54b5f0feabSGreg Clayton     m_default_value (default_value),
55b5f0feabSGreg Clayton     m_data_sp(),
5639fb1389SGreg Clayton     m_data_mod_time (),
571f4706c3SVince Harron     m_completion_mask (CommandCompletions::eDiskFileCompletion),
581f4706c3SVince Harron     m_resolve (resolve)
59b5f0feabSGreg Clayton {
60b5f0feabSGreg Clayton }
61b5f0feabSGreg Clayton 
6267cc0636SGreg Clayton void
6367cc0636SGreg Clayton OptionValueFileSpec::DumpValue (const ExecutionContext *exe_ctx, Stream &strm, uint32_t dump_mask)
6467cc0636SGreg Clayton {
6567cc0636SGreg Clayton     if (dump_mask & eDumpOptionType)
6667cc0636SGreg Clayton         strm.Printf ("(%s)", GetTypeAsCString ());
6767cc0636SGreg Clayton     if (dump_mask & eDumpOptionValue)
6867cc0636SGreg Clayton     {
6967cc0636SGreg Clayton         if (dump_mask & eDumpOptionType)
7067cc0636SGreg Clayton             strm.PutCString (" = ");
7167cc0636SGreg Clayton 
7267cc0636SGreg Clayton         if (m_current_value)
7367cc0636SGreg Clayton         {
74b5ad4ec7SGreg Clayton             strm << '"' << m_current_value.GetPath().c_str() << '"';
7567cc0636SGreg Clayton         }
7667cc0636SGreg Clayton     }
7767cc0636SGreg Clayton }
7867cc0636SGreg Clayton 
7967cc0636SGreg Clayton Error
80c95f7e2aSPavel Labath OptionValueFileSpec::SetValueFromString (llvm::StringRef value,
8167cc0636SGreg Clayton                                           VarSetOperationType op)
8267cc0636SGreg Clayton {
8367cc0636SGreg Clayton     Error error;
8467cc0636SGreg Clayton     switch (op)
8567cc0636SGreg Clayton     {
8667cc0636SGreg Clayton     case eVarSetOperationClear:
8767cc0636SGreg Clayton         Clear ();
88332e8b1cSGreg Clayton         NotifyValueChanged();
8967cc0636SGreg Clayton         break;
9067cc0636SGreg Clayton 
9167cc0636SGreg Clayton     case eVarSetOperationReplace:
9267cc0636SGreg Clayton     case eVarSetOperationAssign:
93c95f7e2aSPavel Labath         if (value.size() > 0)
9467cc0636SGreg Clayton         {
955c98b1cfSJason Molenda             // The setting value may have whitespace, double-quotes, or single-quotes around the file
965c98b1cfSJason Molenda             // path to indicate that internal spaces are not word breaks.  Strip off any ws & quotes
975c98b1cfSJason Molenda             // from the start and end of the file path - we aren't doing any word // breaking here so
985c98b1cfSJason Molenda             // the quoting is unnecessary.  NB this will cause a problem if someone tries to specify
995c98b1cfSJason Molenda             // a file path that legitimately begins or ends with a " or ' character, or whitespace.
100c95f7e2aSPavel Labath             value = value.trim("\"' \t");
10167cc0636SGreg Clayton             m_value_was_set = true;
102c95f7e2aSPavel Labath             m_current_value.SetFile(value.str().c_str(), m_resolve);
1032e3881c0SJim Ingham             m_data_sp.reset();
10439fb1389SGreg Clayton             m_data_mod_time.Clear();
105332e8b1cSGreg Clayton             NotifyValueChanged();
10667cc0636SGreg Clayton         }
10767cc0636SGreg Clayton         else
10867cc0636SGreg Clayton         {
10967cc0636SGreg Clayton             error.SetErrorString("invalid value string");
11067cc0636SGreg Clayton         }
11167cc0636SGreg Clayton         break;
11267cc0636SGreg Clayton 
11367cc0636SGreg Clayton     case eVarSetOperationInsertBefore:
11467cc0636SGreg Clayton     case eVarSetOperationInsertAfter:
11567cc0636SGreg Clayton     case eVarSetOperationRemove:
11667cc0636SGreg Clayton     case eVarSetOperationAppend:
11767cc0636SGreg Clayton     case eVarSetOperationInvalid:
118c95f7e2aSPavel Labath         error = OptionValue::SetValueFromString (value, op);
11967cc0636SGreg Clayton         break;
12067cc0636SGreg Clayton     }
12167cc0636SGreg Clayton     return error;
12267cc0636SGreg Clayton }
12367cc0636SGreg Clayton 
12467cc0636SGreg Clayton lldb::OptionValueSP
12567cc0636SGreg Clayton OptionValueFileSpec::DeepCopy () const
12667cc0636SGreg Clayton {
12767cc0636SGreg Clayton     return OptionValueSP(new OptionValueFileSpec(*this));
12867cc0636SGreg Clayton }
12967cc0636SGreg Clayton 
13067cc0636SGreg Clayton 
13167cc0636SGreg Clayton size_t
13267cc0636SGreg Clayton OptionValueFileSpec::AutoComplete (CommandInterpreter &interpreter,
13367cc0636SGreg Clayton                                    const char *s,
13467cc0636SGreg Clayton                                    int match_start_point,
13567cc0636SGreg Clayton                                    int max_return_elements,
13667cc0636SGreg Clayton                                    bool &word_complete,
13767cc0636SGreg Clayton                                    StringList &matches)
13867cc0636SGreg Clayton {
13967cc0636SGreg Clayton     word_complete = false;
14067cc0636SGreg Clayton     matches.Clear();
14167cc0636SGreg Clayton     CommandCompletions::InvokeCommonCompletionCallbacks (interpreter,
142b5f0feabSGreg Clayton                                                          m_completion_mask,
14367cc0636SGreg Clayton                                                          s,
14467cc0636SGreg Clayton                                                          match_start_point,
14567cc0636SGreg Clayton                                                          max_return_elements,
146d78c9576SEd Maste                                                          nullptr,
14767cc0636SGreg Clayton                                                          word_complete,
14867cc0636SGreg Clayton                                                          matches);
14967cc0636SGreg Clayton     return matches.GetSize();
15067cc0636SGreg Clayton }
15167cc0636SGreg Clayton 
15267cc0636SGreg Clayton 
15367cc0636SGreg Clayton 
1546920b52bSGreg Clayton const lldb::DataBufferSP &
1550b0b512fSGreg Clayton OptionValueFileSpec::GetFileContents(bool null_terminate)
1566920b52bSGreg Clayton {
15739fb1389SGreg Clayton     if (m_current_value)
1580b0b512fSGreg Clayton     {
15939fb1389SGreg Clayton         const TimeValue file_mod_time = m_current_value.GetModificationTime();
16039fb1389SGreg Clayton         if (m_data_sp && m_data_mod_time == file_mod_time)
16139fb1389SGreg Clayton             return m_data_sp;
1620b0b512fSGreg Clayton         if (null_terminate)
1630b0b512fSGreg Clayton             m_data_sp = m_current_value.ReadFileContentsAsCString();
1640b0b512fSGreg Clayton         else
1656920b52bSGreg Clayton             m_data_sp = m_current_value.ReadFileContents();
16639fb1389SGreg Clayton         m_data_mod_time = file_mod_time;
1670b0b512fSGreg Clayton     }
1686920b52bSGreg Clayton     return m_data_sp;
1696920b52bSGreg Clayton }
1706920b52bSGreg Clayton 
17167cc0636SGreg Clayton 
172