Shadow Files

- Re-enabled shadow files, but in a new, simpler manner, that allows the
  rc.shadow.command to override settings like color if necessary.
- Modifed A3 to capture program name and store it in the Context.
This commit is contained in:
Paul Beckingham 2011-09-07 00:24:00 -04:00
parent c62f36ef87
commit 9ed0c5c86d
2 changed files with 43 additions and 44 deletions

View file

@ -211,6 +211,9 @@ void A3::categorize ()
arg->_category = Arg::cat_command; arg->_category = Arg::cat_command;
found_command = true; found_command = true;
} }
// Context needs a copy.
context.program = arg->_raw;
} }
// command // command

View file

@ -333,6 +333,10 @@ int Context::dispatch (std::string &out)
if (auto_commit) if (auto_commit)
tdb2.commit (); tdb2.commit ();
// Write commands cause an update of the shadow file, if configured.
if (! c->read_only ())
shadow ();
return rc; return rc;
} }
@ -395,69 +399,61 @@ bool Context::verbose (const std::string& token)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// TODO OBSOLETE // This needs to be taken out and shot, as soon as Lua extensions will allow.
void Context::shadow () void Context::shadow ()
{ {
/* std::string file_name = config.get ("shadow.file");
// Determine if shadow file is enabled. std::string command = config.get ("shadow.command");
File shadowFile (config.get ("shadow.file"));
if (shadowFile._data != "")
{
inShadow = true; // Prevents recursion in case shadow command writes.
// Check for silly shadow file settings. // A missing shadow file command uses the default command instead.
std::string dataLocation = config.get ("data.location"); if (command == "")
if (shadowFile.data == dataLocation + "/pending.data") command = config.get ("default.command");
if (file_name != "" &&
command != "")
{
File shadow_file (file_name);
// Check for dangerous shadow file settings.
std::string location = config.get ("data.location");
if (shadow_file._data == location + "/pending.data")
throw std::string ("Configuration variable 'shadow.file' is set to " throw std::string ("Configuration variable 'shadow.file' is set to "
"overwrite your pending tasks. Please change it."); "overwrite your pending tasks. Please change it.");
if (shadowFile.data == dataLocation + "/completed.data") if (shadow_file._data == location + "/completed.data")
throw std::string ("Configuration variable 'shadow.file' is set to " throw std::string ("Configuration variable 'shadow.file' is set to "
"overwrite your completed tasks. Please change it."); "overwrite your completed tasks. Please change it.");
if (shadowFile.data == dataLocation + "/undo.data") if (shadow_file._data == location + "/undo.data")
throw std::string ("Configuration variable 'shadow.file' is set to " throw std::string ("Configuration variable 'shadow.file' is set to "
"overwrite your undo log. Please change it."); "overwrite your undo log. Please change it.");
std::string oldDetection = config.get ("detection"); if (shadow_file._data == location + "/backlog.data")
std::string oldColor = config.get ("color"); throw std::string ("Configuration variable 'shadow.file' is set to "
"overwrite your backlog file. Please change it.");
clear (); if (shadow_file._data == location + "/synch.key")
throw std::string ("Configuration variable 'shadow.file' is set to "
"overwrite your synch.key file. Please change it.");
// Run report. Use shadow.command, using default.command as a fallback // Compose the command. Put the rc overrides up front, so that they may
// with "list" as a default. // be overridden by rc.shadow.command.
std::string command = config.get ("shadow.command"); command = program +
if (command == "") " rc.detection:off" + // No need to determine terminal size
command = config.get ("default.command"); " rc.color:off" + // Color off by default
" rc.gc:off " + // GC off, to reduce headaches
command + // User specified command
" >" + // Capture
shadow_file._data; // User specified file
split (args, command, ' '); debug ("Running shadow command: " + command);
system (command.c_str ());
//initialize ();
config.set ("detection", "off");
config.set ("color", "off");
// parse ();
std::string result;
(void)dispatch (result);
std::ofstream out (shadowFile._data.c_str ());
if (out.good ())
{
out << result;
out.close ();
}
else
throw std::string ("Could not write file '") + shadowFile._data + "'";
config.set ("detection", oldDetection);
config.set ("color", oldColor);
// Optionally display a notification that the shadow file was updated. // Optionally display a notification that the shadow file was updated.
// TODO Convert to a verbosity token.
if (config.getBoolean ("shadow.notify")) if (config.getBoolean ("shadow.notify"))
footnote (std::string ("[Shadow file '") + shadowFile._data + "' updated.]"); footnote (std::string ("[Shadow file '") + shadow_file._data + "' updated.]");
inShadow = false;
} }
*/
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////