0.1.2
by Francois Marier
Import upstream version 0.8.1 |
1 |
/* edit.c - Editing the file names.
|
2 |
*
|
|
3 |
* Copyright (C) 2001, 2002, 2004, 2005 Oskar Liljeblad
|
|
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 2 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 Library 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, write to the Free Software
|
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
18 |
*/
|
|
19 |
||
20 |
#if HAVE_CONFIG_H
|
|
21 |
#include <config.h> |
|
22 |
#endif
|
|
23 |
#include <string.h> /* C89 */ |
|
24 |
#include <stdlib.h> /* C89 */ |
|
25 |
#include <stdio.h> /* C89 */ |
|
26 |
#include <stdbool.h> /* Gnulib (POSIX) */ |
|
27 |
#include <quotearg.h> /* Gnulib */ |
|
28 |
#include <gettext.h> /* Gnulib (gettext) */ |
|
29 |
#define _(s) gettext(s)
|
|
30 |
#define N_(s) (s)
|
|
31 |
#include "xalloc.h" |
|
32 |
#include "xvasprintf.h" |
|
33 |
#include "common/string-utils.h" |
|
34 |
#include "common/io-utils.h" |
|
35 |
#include "common/error.h" |
|
36 |
#include "common/llist.h" |
|
37 |
#include "qcmd.h" |
|
38 |
||
39 |
LList *edit_file_list; |
|
40 |
static bool accept_reedit = false; |
|
41 |
static EditFormat *formats[] = { |
|
42 |
&destination_only_format, |
|
43 |
&dual_column_format, |
|
44 |
&single_column_format |
|
45 |
};
|
|
46 |
||
47 |
/* Find an edit format by its long or short name.
|
|
48 |
* Return NULL if no format was found.
|
|
49 |
*/
|
|
50 |
EditFormat * |
|
51 |
find_edit_format_by_name(const char *name) |
|
52 |
{
|
|
53 |
int c; |
|
54 |
||
55 |
for (c = 0; c < sizeof(formats)/sizeof(*formats); c++) { |
|
56 |
if (strcmp(name, formats[c]->name) == 0 |
|
57 |
|| strcmp(name, formats[c]->short_name) == 0) |
|
58 |
return formats[c]; |
|
59 |
}
|
|
60 |
||
61 |
return NULL; |
|
62 |
}
|
|
63 |
||
64 |
/* A readline completion generator for the various edit formats.
|
|
65 |
*/
|
|
66 |
char * |
|
67 |
edit_format_generator(const char *text, int state) |
|
68 |
{
|
|
69 |
static int c; |
|
70 |
||
71 |
if (state == 0) |
|
72 |
c = 0; |
|
73 |
||
74 |
while (c < sizeof(formats)/sizeof(*formats)) { |
|
75 |
char *name = formats[c]->name; |
|
76 |
c++; |
|
77 |
if (starts_with(name, text)) |
|
78 |
return xstrdup(name); |
|
79 |
}
|
|
80 |
||
81 |
return NULL; |
|
82 |
}
|
|
83 |
||
84 |
bool
|
|
85 |
edit_files(bool all, bool force) |
|
86 |
{
|
|
87 |
LListIterator it; |
|
88 |
FILE *file; |
|
89 |
char *cmd; |
|
90 |
int rc; |
|
91 |
||
92 |
if (!accept_reedit || force) { |
|
93 |
accept_reedit = false; |
|
94 |
||
95 |
/* Determine what files we want to display to user */
|
|
96 |
llist_clear(edit_file_list); |
|
97 |
if (!all) { |
|
98 |
if (plan != NULL && llist_size(plan->error) != 0) |
|
99 |
llist_add_all(edit_file_list, plan->error); |
|
100 |
} else { |
|
101 |
llist_add_all(edit_file_list, work_files); |
|
102 |
}
|
|
103 |
||
104 |
if (llist_is_empty(edit_file_list)) { |
|
105 |
warn(_("There are no files to edit.")); |
|
106 |
return false; |
|
107 |
}
|
|
108 |
||
109 |
/* Prepare the edit file */
|
|
110 |
file = fopen(edit_filename, "w"); |
|
111 |
if (file == NULL) { |
|
112 |
warn(_("cannot create `%s': %s"), quotearg(edit_filename), errstr); |
|
113 |
llist_clear(edit_file_list); |
|
114 |
return false; |
|
115 |
}
|
|
116 |
||
117 |
/* Write file names to the output file */
|
|
118 |
format->output(file, edit_file_list); |
|
119 |
if (fclose(file) != 0) { |
|
120 |
warn_errno(_("cannot close `%s'"), quotearg(edit_filename)); |
|
121 |
llist_clear(edit_file_list); |
|
122 |
return false; |
|
123 |
}
|
|
124 |
||
125 |
accept_reedit = true; |
|
126 |
}
|
|
127 |
||
128 |
/* Start the editor */
|
|
129 |
cmd = xasprintf("%s %s", editor_program, edit_filename); |
|
130 |
/* Fortunately, edit_filename usually does not contain
|
|
131 |
* special characters and does not start with a dash. */
|
|
132 |
rc = system(cmd); |
|
133 |
free(cmd); |
|
134 |
if (rc == -1) { |
|
135 |
warn(_("cannot start editor - %s"), quotearg(editor_program), errstr); |
|
136 |
return false; |
|
137 |
}
|
|
138 |
if (rc != 0) { |
|
139 |
warn(_("editor exited with status %d"), quotearg(editor_program), rc); |
|
140 |
return false; |
|
141 |
}
|
|
142 |
||
143 |
/* Open the file */
|
|
144 |
file = fopen(edit_filename, "r"); |
|
145 |
if (file == NULL) { |
|
146 |
warn(_("cannot open `%s' for reading: %s"), quotearg(edit_filename), errstr); |
|
147 |
return false; |
|
148 |
}
|
|
149 |
||
150 |
/* Read the file */
|
|
151 |
if (!format->input(file, edit_file_list)) |
|
152 |
return false; |
|
153 |
if (fclose(file) != 0) |
|
154 |
warn(_("cannot close `%s': %s"), quotearg(edit_filename), errstr); |
|
155 |
accept_reedit = false; |
|
156 |
||
157 |
/* Reset status of all files. */
|
|
158 |
for (llist_iterator(edit_file_list, &it); it.has_next(&it); ) { |
|
159 |
FileSpec *spec = it.next(&it); |
|
160 |
spec->status = STATUS_UNCHECKED; |
|
161 |
}
|
|
162 |
llist_clear(edit_file_list); |
|
163 |
||
164 |
return true; |
|
165 |
}
|