LCOV - code coverage report
Current view: top level - lib - rc.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 77 77 100.0 %
Date: 2014-11-22 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* rc.cpp -- written by Alexis WILKE for Made to Order Software Corp. (c) 2005-2014 */
       2             : 
       3             : /*
       4             : 
       5             : Copyright (c) 2005-2014 Made to Order Software Corp.
       6             : 
       7             : http://snapwebsites.org/project/as2js
       8             : 
       9             : Permission is hereby granted, free of charge, to any
      10             : person obtaining a copy of this software and
      11             : associated documentation files (the "Software"), to
      12             : deal in the Software without restriction, including
      13             : without limitation the rights to use, copy, modify,
      14             : merge, publish, distribute, sublicense, and/or sell
      15             : copies of the Software, and to permit persons to whom
      16             : the Software is furnished to do so, subject to the
      17             : following conditions:
      18             : 
      19             : The above copyright notice and this permission notice
      20             : shall be included in all copies or substantial
      21             : portions of the Software.
      22             : 
      23             : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
      24             : ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
      25             : LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
      26             : FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
      27             : EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      28             : LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      29             : WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      30             : ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      31             : SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
      32             : SOFTWARE.
      33             : 
      34             : */
      35             : 
      36             : // this is private
      37             : #include    "rc.h"
      38             : 
      39             : #include    "as2js/exceptions.h"
      40             : #include    "as2js/json.h"
      41             : #include    "as2js/message.h"
      42             : 
      43             : #include    <controlled_vars/controlled_vars_auto_enum_init.h>
      44             : 
      45             : #include    <cstring>
      46             : 
      47             : 
      48             : namespace as2js
      49             : {
      50             : 
      51             : namespace
      52             : {
      53             : 
      54             : char const *g_rc_directories[] =
      55             : {
      56             :     // check user defined variable
      57             :     "$AS2JS_RC",
      58             :     // try locally first (assuming you are a heavy JS developer, you'd
      59             :     // probably start with your local files)
      60             :     "as2js",
      61             :     // try your user "global" installation directory
      62             :     "~/.config/as2js",
      63             :     // try the system directory
      64             :     "/etc/as2js",
      65             :     nullptr
      66             : };
      67             : 
      68          21 : controlled_vars::fbool_t    g_home_initialized;
      69          21 : String                      g_home;
      70             : 
      71             : }
      72             : // no name namespace
      73             : 
      74             : 
      75             : /** \brief Initialize the resources with defaults.
      76             :  *
      77             :  * The constructor calls the reset() function to initialize the
      78             :  * variable resource parameters to internal defaults.
      79             :  */
      80          27 : rc_t::rc_t()
      81             :     //: f_script("") -- auto-init
      82             :     //, f_db("") -- auto-init
      83             :     //, f_temporary_variable_name("") -- auto-init
      84             : {
      85          27 :     reset();
      86          27 : }
      87             : 
      88             : 
      89             : /** \brief Reset the resources to internal defaults.
      90             :  *
      91             :  * This function resets all the rc_t variables to internal defaults:
      92             :  *
      93             :  * \li scripts -- "as2js/scripts"
      94             :  * \li db -- "/tmp/as2js_packages.db"
      95             :  * \li temporary_variable_name -- "@temp"
      96             :  *
      97             :  * This function is called on construction and when calling init_rc().
      98             :  *
      99             :  * Note that does not reset the home parameter which has no internal
     100             :  * default and is managed differently.
     101             :  */
     102        9392 : void rc_t::reset()
     103             : {
     104             :     // internal defaults
     105        9392 :     f_scripts = "as2js/scripts";
     106        9392 :     f_db = "/tmp/as2js_packages.db";
     107        9392 :     f_temporary_variable_name = "@temp";
     108        9392 : }
     109             : 
     110             : 
     111             : /** \brief Find the resource file.
     112             :  *
     113             :  * This function tries to find a resource file.
     114             :  *
     115             :  * The resource file defines two paths where we can find the system
     116             :  * definitions and user imports.
     117             :  *
     118             :  * \param[in] accept_if_missing  Whether an error is generated (false)
     119             :  *                               if the file cannot be found.
     120             :  */
     121        9365 : void rc_t::init_rc(bool const accept_if_missing)
     122             : {
     123        9365 :     reset();
     124             : 
     125             :     // first try to find a place with a .rc file
     126        9365 :     FileInput::pointer_t in(new FileInput());
     127       18730 :     String rcfilename;
     128       18746 :     for(char const **dir = g_rc_directories; *dir != nullptr; ++dir)
     129             :     {
     130       18740 :         std::stringstream buffer;
     131       18740 :         if(**dir == '$')
     132             :         {
     133        9365 :             String env_defined(getenv(*dir + 1));
     134        9365 :             if(env_defined.empty())
     135             :             {
     136        9356 :                 continue;
     137             :             }
     138           9 :             buffer << env_defined << "/as2js.rc";
     139             :         }
     140        9375 :         else if(**dir == '~' && (*dir)[1] == '/')
     141             :         {
     142          12 :             String home(get_home());
     143          12 :             if(home.empty())
     144             :             {
     145             :                 // no valid $HOME variable
     146           1 :                 continue;
     147             :             }
     148          11 :             buffer << home << "/" << (*dir + 2) << "/as2js.rc";
     149             :         }
     150             :         else
     151             :         {
     152        9363 :             buffer << *dir << "/as2js.rc";
     153             :         }
     154        9383 :         rcfilename.from_utf8(buffer.str().c_str());
     155        9383 :         if(!rcfilename.empty())
     156             :         {
     157        9383 :             if(in->open(rcfilename))
     158             :             {
     159             :                 // it worked, we are done
     160        9359 :                 break;
     161             :             }
     162          24 :             rcfilename.clear();
     163             :         }
     164          24 :     }
     165             : 
     166        9365 :     if(rcfilename.empty())
     167             :     {
     168           6 :         if(!accept_if_missing)
     169             :         {
     170             :             // no position in this case...
     171           4 :             Message msg(message_level_t::MESSAGE_LEVEL_FATAL, err_code_t::AS_ERR_INSTALLATION);
     172           4 :             msg << "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc";
     173           4 :             throw exception_exit(1, "cannot find the as2js.rc file; the system default is usually put in /etc/as2js/as2js.rc");
     174             :         }
     175             : 
     176             :         // nothing to load in this case...
     177             :     }
     178             :     else
     179             :     {
     180        9359 :         JSON::pointer_t json(new JSON);
     181       18718 :         JSON::JSONValue::pointer_t root(json->parse(in));
     182        9359 :         JSON::JSONValue::type_t type(root->get_type());
     183             :         // null is accepted, in which case we keep the defaults
     184        9359 :         if(type != JSON::JSONValue::type_t::JSON_TYPE_NULL)
     185             :         {
     186        9356 :             if(type != JSON::JSONValue::type_t::JSON_TYPE_OBJECT)
     187             :             {
     188           3 :                 Message msg(message_level_t::MESSAGE_LEVEL_FATAL, err_code_t::AS_ERR_UNEXPECTED_RC, root->get_position());
     189           3 :                 msg << "A resource file (.rc) must be defined as a JSON object, or set to 'null'.";
     190           3 :                 throw exception_exit(1, "A resource file (.rc) must be defined as a JSON object, or set to 'null'.");
     191             :             }
     192             : 
     193        9353 :             JSON::JSONValue::object_t const& obj(root->get_object());
     194       37386 :             for(JSON::JSONValue::object_t::const_iterator it(obj.begin()); it != obj.end(); ++it)
     195             :             {
     196             :                 // the only type of values in the resource files are strings
     197       28036 :                 JSON::JSONValue::type_t sub_type(it->second->get_type());
     198       28036 :                 if(sub_type != JSON::JSONValue::type_t::JSON_TYPE_STRING)
     199             :                 {
     200           3 :                     Message msg(message_level_t::MESSAGE_LEVEL_FATAL, err_code_t::AS_ERR_UNEXPECTED_RC, it->second->get_position());
     201           3 :                     msg << "A resource file is expected to be an object of string elements.";
     202           3 :                     throw exception_exit(1, "A resource file is expected to be an object of string elements.");
     203             :                 }
     204             : 
     205       28033 :                 String parameter_name(it->first);
     206       56066 :                 String parameter_value(it->second->get_string());
     207             : 
     208       28033 :                 if(parameter_name == "scripts")
     209             :                 {
     210        9346 :                     f_scripts = parameter_value;
     211             :                 }
     212       18687 :                 else if(parameter_name == "db")
     213             :                 {
     214        9346 :                     f_db = parameter_value;
     215             :                 }
     216        9341 :                 else if(parameter_name == "temporary_variable_name")
     217             :                 {
     218        9341 :                     f_temporary_variable_name = parameter_value;
     219             :                 }
     220       28033 :             }
     221        9359 :         }
     222        9365 :     }
     223        9355 : }
     224             : 
     225             : 
     226        9375 : String const& rc_t::get_scripts() const
     227             : {
     228        9375 :     return f_scripts;
     229             : }
     230             : 
     231             : 
     232        9362 : String const& rc_t::get_db() const
     233             : {
     234        9362 :     return f_db;
     235             : }
     236             : 
     237             : 
     238           5 : String const& rc_t::get_temporary_variable_name() const
     239             : {
     240           5 :     return f_temporary_variable_name;
     241             : }
     242             : 
     243             : 
     244          14 : String const& rc_t::get_home()
     245             : {
     246          14 :     if(!g_home_initialized)
     247             :     {
     248           2 :         g_home_initialized = true;
     249           2 :         g_home.from_utf8(getenv("HOME"));
     250             : 
     251             :         // TODO: add test for tainted getenv() result
     252             :     }
     253             : 
     254          14 :     return g_home;
     255             : }
     256             : 
     257          63 : }
     258             : // namespace as2js
     259             : 
     260             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.10