1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2006 - 2021, Tomas Babej, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // https://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26 
27 #include <cmake.h>
28 #include <CmdGet.h>
29 #include <Variant.h>
30 #include <Context.h>
31 #include <DOM.h>
32 #include <main.h>
33 #include <shared.h>
34 #include <format.h>
35 
36 ////////////////////////////////////////////////////////////////////////////////
CmdGet()37 CmdGet::CmdGet ()
38 {
39   _keyword               = "_get";
40   _usage                 = "task          _get <DOM> [<DOM> ...]";
41   _description           = "DOM Accessor";
42   _read_only             = true;
43   _displays_id           = false;
44   _needs_gc              = false;
45   _uses_context          = false;
46   _accepts_filter        = false;
47   _accepts_modifications = false;
48   _accepts_miscellaneous = true;
49   _category              = Command::Category::internal;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 // Rely on the Lexer to correctly identify DOM references, then jsut iterate
54 // over those.
55 //
56 // It is an error to specify no DOM references.
57 // It is not an error for a DOM reference to resolve to a blank value.
execute(std::string & output)58 int CmdGet::execute (std::string& output)
59 {
60   std::vector <std::string> results;
61   for (auto& arg : Context::getContext ().cli2._args)
62   {
63     switch (arg._lextype)
64     {
65     case Lexer::Type::dom:
66       {
67         Task t;
68         Variant result;
69         if (getDOM (arg.attribute ("raw"), t, result))
70           results.emplace_back (result);
71         else
72           results.emplace_back ("");
73       }
74       break;
75 
76     // Look for non-refs to complain about.
77     case Lexer::Type::word:
78     case Lexer::Type::identifier:
79       if (! arg.hasTag ("BINARY") &&
80           ! arg.hasTag ("CMD"))
81         throw format ("'{1}' is not a DOM reference.", arg.attribute ("raw"));
82 
83     default:
84       break;
85     }
86   }
87 
88   if (results.size () == 0)
89     throw std::string ("No DOM reference specified.");
90 
91   output = join (" ", results);
92   output += '\n';
93   return 0;
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97