+/* Support for binding readline key sequences to Unix commands. */
+static Keymap cmd_xmap;
+
+static int
+bash_execute_unix_command (count, key)
+ int count; /* ignored */
+ int key;
+{
+ Keymap ckmap; /* current keymap */
+ Keymap xkmap; /* unix command executing keymap */
+ register int i;
+ char *cmd;
+ sh_parser_state_t ps;
+
+ /* First, we need to find the right command to execute. This is tricky,
+ because we might have already indirected into another keymap. */
+ ckmap = rl_get_keymap ();
+ if (ckmap != rl_executing_keymap)
+ {
+ /* bogus. we have to search. only handle one level of indirection. */
+ for (i = 0; i < KEYMAP_SIZE; i++)
+ {
+ if (ckmap[i].type == ISKMAP && (Keymap)ckmap[i].function == rl_executing_keymap)
+ break;
+ }
+ if (i < KEYMAP_SIZE)
+ xkmap = (Keymap)cmd_xmap[i].function;
+ else
+ {
+ rl_crlf ();
+ internal_error (_("bash_execute_unix_command: cannot find keymap for command"));
+ rl_forced_update_display ();
+ return 1;
+ }
+ }
+ else
+ xkmap = cmd_xmap;
+
+ cmd = (char *)xkmap[key].function;
+
+ if (cmd == 0)
+ {
+ rl_ding ();
+ return 1;
+ }
+
+ rl_crlf (); /* move to a new line */
+
+ save_parser_state (&ps);
+
+ cmd = savestring (cmd);
+ parse_and_execute (cmd, "bash_execute_unix_command", SEVAL_NOHIST);
+
+ restore_parser_state (&ps);
+
+ /* and restore the readline buffer and display after command execution. */
+ rl_forced_update_display ();
+ return 0;
+}
+
+static void
+init_unix_command_map ()
+{
+ cmd_xmap = rl_make_bare_keymap ();
+}
+
+static int
+isolate_sequence (string, ind, need_dquote, startp)
+ char *string;
+ int ind, need_dquote, *startp;
+{
+ register int i;
+ int c, passc, delim;
+
+ for (i = ind; string[i] && whitespace (string[i]); i++)
+ ;
+ /* NEED_DQUOTE means that the first non-white character *must* be `"'. */
+ if (need_dquote && string[i] != '"')
+ {
+ builtin_error (_("%s: first non-whitespace character is not `\"'"), string);
+ return -1;
+ }
+
+ /* We can have delimited strings even if NEED_DQUOTE == 0, like the command
+ string to bind the key sequence to. */
+ delim = (string[i] == '"' || string[i] == '\'') ? string[i] : 0;
+
+ if (startp)
+ *startp = delim ? ++i : i;
+
+ for (passc = 0; c = string[i]; i++)
+ {
+ if (passc)
+ {
+ passc = 0;
+ continue;
+ }
+ if (c == '\\')
+ {
+ passc++;
+ continue;
+ }
+ if (c == delim)
+ break;
+ }
+
+ if (delim && string[i] != delim)
+ {
+ builtin_error (_("no closing `%c' in %s"), delim, string);
+ return -1;
+ }
+
+ return i;
+}
+
+int
+bind_keyseq_to_unix_command (line)
+ char *line;
+{
+ Keymap kmap;
+ char *kseq, *value;
+ int i, kstart;
+
+ if (cmd_xmap == 0)
+ init_unix_command_map ();
+
+ kmap = rl_get_keymap ();
+
+ /* We duplicate some of the work done by rl_parse_and_bind here, but
+ this code only has to handle `"keyseq": ["]command["]' and can
+ generate an error for anything else. */
+ i = isolate_sequence (line, 0, 1, &kstart);
+ if (i < 0)
+ return -1;
+
+ /* Create the key sequence string to pass to rl_generic_bind */
+ kseq = substring (line, kstart, i);
+
+ for ( ; line[i] && line[i] != ':'; i++)
+ ;
+ if (line[i] != ':')
+ {
+ builtin_error (_("%s: missing colon separator"), line);
+ return -1;
+ }
+
+ i = isolate_sequence (line, i + 1, 0, &kstart);
+ if (i < 0)
+ return -1;
+
+ /* Create the value string containing the command to execute. */
+ value = substring (line, kstart, i);
+
+ /* Save the command to execute and the key sequence in the CMD_XMAP */
+ rl_generic_bind (ISMACR, kseq, value, cmd_xmap);
+
+ /* and bind the key sequence in the current keymap to a function that
+ understands how to execute from CMD_XMAP */
+ rl_bind_keyseq_in_map (kseq, bash_execute_unix_command, kmap);
+
+ return 0;
+}
+
+/* Used by the programmable completion code. Complete TEXT as a filename,
+ but return only directories as matches. Dequotes the filename before
+ attempting to find matches. */
+char **
+bash_directory_completion_matches (text)
+ const char *text;
+{
+ char **m1;
+ char *dfn;
+ int qc;
+
+ qc = rl_dispatching ? rl_completion_quote_character : 0;
+ dfn = bash_dequote_filename ((char *)text, qc);
+ m1 = rl_completion_matches (dfn, rl_filename_completion_function);
+ free (dfn);
+
+ if (m1 == 0 || m1[0] == 0)
+ return m1;
+ /* We don't bother recomputing the lcd of the matches, because it will just
+ get thrown away by the programmable completion code and recomputed
+ later. */
+ (void)bash_ignore_filenames (m1);
+ return m1;
+}
+
+char *
+bash_dequote_text (text)
+ const char *text;
+{
+ char *dtxt;
+ int qc;
+
+ qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
+ dtxt = bash_dequote_filename ((char *)text, qc);
+ return (dtxt);
+}