1 //
2 //      aegis - project change supervisor
3 //      Copyright (C) 1999, 2001-2008, 2012 Peter Miller
4 //
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 3 of the License, or
8 //      (at your option) any later version.
9 //
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14 //
15 //      You should have received a copy of the GNU General Public License
16 //      along with this program. If not, see
17 //      <http://www.gnu.org/licenses/>.
18 //
19 
20 #include <common/ac/assert.h>
21 
22 #include <common/trace.h>
23 #include <libaegis/change.h>
24 #include <libaegis/change/env_set.h>
25 #include <libaegis/os.h>
26 #include <libaegis/project.h>
27 #include <libaegis/sub.h>
28 
29 
30 string_ty *
change_run_history_query_command(change::pointer cp,fstate_src_ty * src)31 change_run_history_query_command(change::pointer cp, fstate_src_ty *src)
32 {
33     sub_context_ty  *scp;
34     string_ty       *hp;
35     pconf_ty        *pconf_data;
36     string_ty       *the_command;
37     string_ty       *result;
38     string_ty       *hfn;
39 
40     //
41     // Ask the history file what its edit number is.  We use this
42     // method because the string returned is essentially random,
43     // between different history programs.  Only ever executed in
44     // the "being integrated" state.  Current directory will be
45     // set to the base of the history tree.  All of the
46     // substitutions described in aesub(5) are available.  In
47     // addition
48     //
49     // ${History}
50     //  absolute path of history file
51     //
52     trace(("change_run_history_query_command(cp = %p, "
53         "src->file_name = \"%s\")\n{\n", cp, src->file_name->str_text));
54     assert(cp->reference_count >= 1);
55     pconf_data = change_pconf_get(cp, 1);
56     hfn = project_history_filename_get(cp->pp, src);
57     scp = sub_context_new();
58     sub_var_set_string(scp, "History", hfn);
59     str_free(hfn);
60     the_command = pconf_data->history_query_command;
61     if (!the_command)
62     {
63         sub_context_ty  *scp2;
64 
65         assert(pconf_data->errpos);
66         scp2 = sub_context_new();
67         sub_var_set_string(scp2, "File_Name", pconf_data->errpos);
68         sub_var_set_charstar(scp2, "FieLD_Name", "history_query_command");
69         change_fatal
70         (
71             cp,
72             scp2,
73             i18n("$filename: contains no \"$field_name\" field")
74         );
75         // NOTREACHED
76         sub_context_delete(scp2);
77     }
78     the_command = substitute(scp, cp, the_command);
79     sub_context_delete(scp);
80 
81     change_env_set(cp, 0);
82     hp = cp->pp->history_path_get();
83     project_become(cp->pp);
84     result = os_execute_slurp(the_command, OS_EXEC_FLAG_NO_INPUT, hp);
85     project_become_undo(cp->pp);
86     str_free(the_command);
87     if (!result->str_length)
88         fatal_intl(0, i18n("history_query_command return empty"));
89     trace(("return \"%s\";\n", result->str_text));
90     trace(("}\n"));
91     return result;
92 }
93 
94 
95 // vim: set ts=8 sw=4 et :
96