]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/input.c
2 * Copyright (c) 2000-2003 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #if defined(ENABLE_READLINE)
29 # include <readline/history.h>
30 # include <readline/readline.h>
31 #elif defined(ENABLE_EDITLINE)
32 # include <histedit.h>
39 static void popfile(void);
40 static int source_f(int argc
, char **argv
);
42 static const cmdinfo_t source_cmd
=
43 { "source", NULL
, source_f
, 1, 1, 0, N_("source-file"),
44 N_("get commands from source-file"), NULL
};
46 /* our homegrown strtok that understands strings */
52 static char *last_place
= NULL
;
61 if (last_place
== NULL
)
65 if (*last_place
!= '\0')
68 start
= last_place
+ 1;
73 while (*start
== ' ' || *start
== '\t')
77 for (;*walk
!= '\0'; walk
++) {
84 else if (*walk
== '\"')
87 if (!in_string
&& !in_escape
&&
88 (*walk
== ' ' || *walk
== '\t')) {
112 rval
= xcalloc(sizeof(char *), 1);
121 rval
= xrealloc(rval
, sizeof(*rval
) * (c
+ 1));
141 static char prompt
[FILENAME_MAX
+ 1];
144 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
149 fetchline_internal(void)
158 for (rlen
= iscont
= 0; ; ) {
159 if (curinput
== stdin
) {
163 dbprintf(get_prompt(), progname
);
167 (!fgets(buf
, sizeof(buf
), curinput
) &&
168 ferror(curinput
) && seenint())) {
182 if (ferror(curinput
) || feof(curinput
) ||
183 (len
= strlen(buf
)) == 0) {
185 * No more input at this inputstack level; pop
186 * our fd off and return so that a lower
187 * level fetchline can handle us. If this was
188 * an interactive session, print a newline
189 * because ^D doesn't emit one.
191 if (curinput
== stdin
)
203 if (inputstacksize
== 1)
204 logprintf("%s", buf
);
205 rval
= xrealloc(rval
, rlen
+ len
+ 1);
210 if (buf
[len
- 1] == '\n') {
211 if (len
> 1 && buf
[len
- 2] == '\\') {
212 rval
[rlen
- 2] = ' ';
213 rval
[rlen
- 1] = '\0';
217 rval
[rlen
- 1] = '\0';
226 #ifdef ENABLE_READLINE
232 if (inputstacksize
== 1) {
233 line
= readline(get_prompt());
236 else if (line
&& *line
) {
238 logprintf("%s", line
);
241 line
= fetchline_internal();
245 #elif defined(ENABLE_EDITLINE)
246 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
251 static History
*hist
;
257 hist
= history_init();
258 history(hist
, &hevent
, H_SETSIZE
, 100);
259 el
= el_init(progname
, stdin
, stdout
, stderr
);
261 el_set(el
, EL_SIGNAL
, 1);
262 el_set(el
, EL_PROMPT
, el_get_prompt
);
263 el_set(el
, EL_HIST
, history
, (const char *)hist
);
266 if (inputstacksize
== 1) {
267 line
= xstrdup(el_gets(el
, &count
));
270 line
[count
-1] = '\0';
272 history(hist
, &hevent
, H_ENTER
, line
);
273 logprintf("%s", line
);
277 line
= fetchline_internal();
282 char * fetchline(void) { return fetchline_internal(); }
288 if (inputstacksize
== 0) {
292 if (curinput
!= stdin
)
296 if (inputstacksize
) {
298 xrealloc(inputstack
, inputstacksize
* sizeof(*inputstack
));
299 curinput
= inputstack
[inputstacksize
- 1];
313 (inputstacksize
+ 1) * sizeof(*inputstack
));
315 curinput
= inputstack
[inputstacksize
- 1] = file
;
329 f
= fopen(argv
[1], "r");
331 dbprintf(_("can't open %s\n"), argv
[0]);
335 /* Run the sourced commands now. */
338 if ((input
= fetchline_internal()) == NULL
)
340 v
= breakline(input
, &c
);
342 done
= command(c
, v
);
352 add_command(&source_cmd
);