+// SPDX-License-Identifier: GPL-2.0
/*
- * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like. Any license provided herein, whether implied or
- * otherwise, applies only to this software file. Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc., 59
- * Temple Place - Suite 330, Boston MA 02111-1307, USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA 94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
+ * Copyright (c) 2000-2003 Silicon Graphics, Inc.
+ * All Rights Reserved.
*/
-#include <libxfs.h>
+#include "libxfs.h"
#include <signal.h>
#include "command.h"
-#include "data.h"
#include "input.h"
#include "output.h"
#include "sig.h"
#include "malloc.h"
#include "init.h"
-int inputstacksize;
-FILE **inputstack;
-FILE *curinput;
+#if defined(ENABLE_READLINE)
+# include <readline/history.h>
+# include <readline/readline.h>
+#elif defined(ENABLE_EDITLINE)
+# include <histedit.h>
+#endif
+
+static int inputstacksize;
+static FILE **inputstack;
+static FILE *curinput;
static void popfile(void);
static int source_f(int argc, char **argv);
static const cmdinfo_t source_cmd =
- { "source", NULL, source_f, 1, 1, 0, "source-file",
- "get commands from source-file", NULL };
+ { "source", NULL, source_f, 1, 1, 0, N_("source-file"),
+ N_("get commands from source-file"), NULL };
/* our homegrown strtok that understands strings */
xfree(vec);
}
-char *
-fetchline(void)
+static char *
+get_prompt(void)
+{
+ static char prompt[FILENAME_MAX + 1];
+
+ if (!prompt[0])
+ snprintf(prompt, sizeof(prompt), "%s> ", progname);
+ return prompt;
+}
+
+static char *
+fetchline_internal(void)
{
char buf[1024];
int iscont;
rval = NULL;
for (rlen = iscont = 0; ; ) {
- if (inputstacksize == 1) {
+ if (curinput == stdin) {
if (iscont)
dbprintf("... ");
else
- dbprintf("%s: ", progname);
+ dbprintf(get_prompt(), progname);
fflush(stdin);
}
if (seenint() ||
}
if (ferror(curinput) || feof(curinput) ||
(len = strlen(buf)) == 0) {
- popfile();
- if (curinput == NULL) {
+ /*
+ * No more input at this inputstack level; pop
+ * our fd off and return so that a lower
+ * level fetchline can handle us. If this was
+ * an interactive session, print a newline
+ * because ^D doesn't emit one.
+ */
+ if (curinput == stdin)
dbprintf("\n");
- return NULL;
- }
+
+ popfile();
iscont = 0;
rlen = 0;
if (rval) {
xfree(rval);
rval = NULL;
}
- continue;
+ return NULL;
}
if (inputstacksize == 1)
logprintf("%s", buf);
return rval;
}
-void
-input_init(void)
+#ifdef ENABLE_READLINE
+char *
+fetchline(void)
{
- add_command(&source_cmd);
+ char *line;
+
+ if (inputstacksize == 1) {
+ line = readline(get_prompt());
+ if (!line)
+ dbprintf("\n");
+ else if (line && *line) {
+ add_history(line);
+ logprintf("%s", line);
+ }
+ } else {
+ line = fetchline_internal();
+ }
+ return line;
}
+#elif defined(ENABLE_EDITLINE)
+static char *el_get_prompt(EditLine *e) { return get_prompt(); }
+char *
+fetchline(void)
+{
+ static EditLine *el;
+ static History *hist;
+ HistEvent hevent;
+ char *line;
+ int count;
+
+ if (!el) {
+ hist = history_init();
+ history(hist, &hevent, H_SETSIZE, 100);
+ el = el_init(progname, stdin, stdout, stderr);
+ el_source(el, NULL);
+ el_set(el, EL_SIGNAL, 1);
+ el_set(el, EL_PROMPT, el_get_prompt);
+ el_set(el, EL_HIST, history, (const char *)hist);
+ }
+
+ if (inputstacksize == 1) {
+ line = xstrdup(el_gets(el, &count));
+ if (line) {
+ if (count > 0)
+ line[count-1] = '\0';
+ if (*line) {
+ history(hist, &hevent, H_ENTER, line);
+ logprintf("%s", line);
+ }
+ }
+ } else {
+ line = fetchline_internal();
+ }
+ return line;
+}
+#else
+char * fetchline(void) { return fetchline_internal(); }
+#endif
static void
popfile(void)
}
if (curinput != stdin)
fclose(curinput);
-
+
inputstacksize--;
- if (inputstacksize) {
+ if (inputstacksize) {
inputstack =
xrealloc(inputstack, inputstacksize * sizeof(*inputstack));
- curinput = inputstack[inputstacksize - 1];
- } else {
- free(inputstack);
- curinput = NULL;
- inputstack = NULL;
- }
+ curinput = inputstack[inputstacksize - 1];
+ } else {
+ free(inputstack);
+ curinput = NULL;
+ inputstack = NULL;
+ }
}
void
char **argv)
{
FILE *f;
+ int c, done = 0;
+ char *input;
+ char **v;
f = fopen(argv[1], "r");
- if (f == NULL)
- dbprintf("can't open %s\n", argv[0]);
- else
- pushfile(f);
+ if (f == NULL) {
+ dbprintf(_("can't open %s\n"), argv[0]);
+ return 0;
+ }
+
+ /* Run the sourced commands now. */
+ pushfile(f);
+ while (!done) {
+ if ((input = fetchline_internal()) == NULL)
+ break;
+ v = breakline(input, &c);
+ if (c)
+ done = command(c, v);
+ doneline(input, v);
+ }
+
return 0;
}
+
+void
+input_init(void)
+{
+ add_command(&source_cmd);
+}