1 //
2 // aegis - project change supervisor
3 // Copyright (C) 2009 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 (at
8 // 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 GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License along
16 // with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 
19 #include <common/ac/string.h>
20 #include <common/nstring/list.h>
21 
22 #include <libaegis/arglex2.h>
23 #include <libaegis/os.h>
24 #include <libaegis/sub.h>
25 #include <libaegis/user.h>
26 
27 
28 void
os_edit(const nstring_list & filenames,edit_ty et,const nstring & dir)29 os_edit(const nstring_list &filenames, edit_ty et, const nstring &dir)
30 {
31     //
32     // find the editor to use
33     //
34     os_become_must_not_be_active();
35     nstring editor;
36     if (et == edit_background)
37     {
38 	editor = user_ty::create()->editor_command();
39     }
40     else
41     {
42 	editor = user_ty::create()->visual_command();
43 	const char *cp = strrchr(editor.c_str(), '/');
44 	if (!cp)
45             cp = editor.c_str();
46 	if (0 == strcmp(cp, "ed"))
47             et = edit_background;
48     }
49 
50     //
51     // make sure we are in a position to edit
52     //
53     if (et != edit_background && os_background())
54     {
55 	sub_context_ty sc;
56 	sc.var_set_charstar("Name", arglex_token_name(arglex_token_edit));
57 	sc.fatal_intl(i18n("may not use $name in the background"));
58 	// NOTREACHED
59     }
60 
61     //
62     // edit the files
63     //
64     // Please note: we ignore the exit status on purpose.
65     // This is because vi (amongst others) returns a silly exit status.
66     //
67     nstring_list args;
68     args.push_back(editor);
69     for (size_t j = 0; j < filenames.size(); ++j)
70         args.push_back(filenames[j].quote_shell());
71     nstring cmd = args.unsplit(" ");
72 
73     os_become_orig();
74     os_execute(cmd, OS_EXEC_FLAG_INPUT | OS_EXEC_FLAG_ERROK, dir);
75     os_become_undo();
76 }
77