Changes to handle vfs_path_t object:
[midnight-commander.git] / src / filemanager / command.c
blobe7d5735fe0d8fd6819f3020a855408370fd98bf1
1 /*
2 Command line widget.
3 This widget is derived from the WInput widget, it's used to cope
4 with all the magic of the command input line, we depend on some
5 help from the program's callback.
7 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
8 2007, 2011
9 The Free Software Foundation, Inc.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file command.c
28 * \brief Source: command line widget
31 #include <config.h>
33 #include <stdlib.h>
34 #include <errno.h>
35 #include <string.h>
37 #include "lib/global.h" /* home_dir */
38 #include "lib/tty/tty.h"
39 #include "lib/vfs/vfs.h"
40 #include "lib/strescape.h"
41 #include "lib/skin.h" /* DEFAULT_COLOR */
42 #include "lib/util.h"
43 #include "lib/widget.h"
45 #include "src/main.h" /* do_cd */
46 #include "src/subshell.h" /* SUBSHELL_EXIT */
47 #include "src/execute.h" /* shell_execute */
49 #include "midnight.h" /* current_panel */
50 #include "layout.h" /* for command_prompt variable */
51 #include "usermenu.h" /* expand_format */
52 #include "tree.h" /* for tree_chdir */
54 #include "command.h"
56 /*** global variables ****************************************************************************/
58 /* This holds the command line */
59 WInput *cmdline;
61 /*** file scope macro definitions ****************************************************************/
63 #define CD_OPERAND_OFFSET 3
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 /*** file scope functions ************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
72 /**
73 * Expand the argument to "cd" and change directory. First try tilde
74 * expansion, then variable substitution. If the CDPATH variable is set
75 * (e.g. CDPATH=".:~:/usr"), try all the paths contained there.
76 * We do not support such rare substitutions as ${var:-value} etc.
77 * No quoting is implemented here, so ${VAR} and $VAR will be always
78 * substituted. Wildcards are not supported either.
79 * Advanced users should be encouraged to use "\cd" instead of "cd" if
80 * they want the behavior they are used to in the shell.
83 static char *
84 examine_cd (const char *_path)
86 typedef enum { copy_sym, subst_var } state_t;
88 state_t state = copy_sym;
89 char *q;
90 size_t qlen;
91 char *path_tilde, *path;
92 char *p, *r;
94 /* Tilde expansion */
95 path = strutils_shell_unescape (_path);
96 path_tilde = tilde_expand (path);
97 g_free (path);
99 /* Leave space for further expansion */
100 qlen = strlen (path_tilde) + MC_MAXPATHLEN;
101 q = g_malloc (qlen);
103 /* Variable expansion */
104 for (p = path_tilde, r = q; *p != '\0' && r < q + MC_MAXPATHLEN;)
107 switch (state)
109 case copy_sym:
110 if (p[0] == '\\' && p[1] == '$')
112 /* skip backslash */
113 p++;
114 /* copy dollar */
115 *(r++) = *(p++);
117 else if (p[0] != '$' || p[1] == '[' || p[1] == '(')
118 *(r++) = *(p++);
119 else
120 state = subst_var;
121 break;
123 case subst_var:
125 char *s;
126 char c;
127 const char *t;
129 /* skip dollar */
130 p++;
132 if (p[0] != '{')
133 s = NULL;
134 else
136 p++;
137 s = strchr (p, '}');
139 if (s == NULL)
140 s = strchr (p, PATH_SEP);
141 if (s == NULL)
142 s = strchr (p, '\0');
143 c = *s;
144 *s = '\0';
145 t = getenv (p);
146 *s = c;
147 if (t == NULL)
149 *(r++) = '$';
150 if (p[-1] != '$')
151 *(r++) = '{';
153 else
155 size_t tlen;
157 tlen = strlen (t);
159 if (r + tlen < q + MC_MAXPATHLEN)
161 strncpy (r, t, tlen + 1);
162 r += tlen;
164 p = s;
165 if (*s == '}')
166 p++;
169 state = copy_sym;
170 break;
175 g_free (path_tilde);
177 *r = '\0';
179 return q;
182 /* --------------------------------------------------------------------------------------------- */
184 /* CDPATH handling */
185 static gboolean
186 handle_cdpath (const char *path)
188 gboolean result = FALSE;
190 /* CDPATH handling */
191 if (*path != PATH_SEP)
193 char *cdpath, *p;
194 char c;
196 cdpath = g_strdup (getenv ("CDPATH"));
197 p = cdpath;
198 c = (p == NULL) ? '\0' : ':';
200 while (!result && c == ':')
202 char *s;
204 s = strchr (p, ':');
205 if (s == NULL)
206 s = strchr (p, '\0');
207 c = *s;
208 *s = '\0';
209 if (*p != '\0')
211 vfs_path_t *r_vpath;
213 r_vpath = vfs_path_build_filename (p, path, NULL);
214 result = do_cd (r_vpath, cd_parse_command);
215 vfs_path_free (r_vpath);
217 *s = c;
218 p = s + 1;
220 g_free (cdpath);
223 return result;
226 /* --------------------------------------------------------------------------------------------- */
227 /** Handle Enter on the command line */
229 static cb_ret_t
230 enter (WInput * lc_cmdline)
232 char *cmd = lc_cmdline->buffer;
234 if (!command_prompt)
235 return MSG_HANDLED;
237 /* Any initial whitespace should be removed at this point */
238 while (*cmd == ' ' || *cmd == '\t' || *cmd == '\n')
239 cmd++;
241 if (!*cmd)
242 return MSG_HANDLED;
244 if (strncmp (cmd, "cd ", 3) == 0 || strcmp (cmd, "cd") == 0)
246 do_cd_command (cmd);
247 input_clean (lc_cmdline);
248 return MSG_HANDLED;
250 else if (strcmp (cmd, "exit") == 0)
252 input_assign_text (lc_cmdline, "");
253 if (!quiet_quit_cmd ())
254 return MSG_NOT_HANDLED;
256 else
258 char *command, *s;
259 size_t i, j, cmd_len;
261 if (!vfs_current_is_local ())
263 message (D_ERROR, MSG_ERROR, _("Cannot execute commands on non-local filesystems"));
264 return MSG_NOT_HANDLED;
266 #ifdef HAVE_SUBSHELL_SUPPORT
267 /* Check this early before we clean command line
268 * (will be checked again by shell_execute) */
269 if (mc_global.tty.use_subshell && subshell_state != INACTIVE)
271 message (D_ERROR, MSG_ERROR, _("The shell is already running a command"));
272 return MSG_NOT_HANDLED;
274 #endif
275 cmd_len = strlen (cmd);
276 command = g_malloc (cmd_len + 1);
277 command[0] = 0;
278 for (i = j = 0; i < cmd_len; i++)
280 if (cmd[i] == '%')
282 i++;
283 s = expand_format (NULL, cmd[i], TRUE);
284 command = g_realloc (command, j + strlen (s) + cmd_len - i + 1);
285 strcpy (command + j, s);
286 g_free (s);
287 j = strlen (command);
289 else
291 command[j] = cmd[i];
292 j++;
294 command[j] = 0;
296 input_clean (lc_cmdline);
297 shell_execute (command, 0);
298 g_free (command);
300 #ifdef HAVE_SUBSHELL_SUPPORT
301 if ((quit & SUBSHELL_EXIT) != 0)
303 if (quiet_quit_cmd ())
304 return MSG_HANDLED;
306 quit = 0;
307 /* restart subshell */
308 if (mc_global.tty.use_subshell)
309 init_subshell ();
312 if (mc_global.tty.use_subshell)
313 do_load_prompt ();
314 #endif
316 return MSG_HANDLED;
319 /* --------------------------------------------------------------------------------------------- */
321 static cb_ret_t
322 command_callback (Widget * w, widget_msg_t msg, int parm)
324 WInput *cmd = (WInput *) w;
326 switch (msg)
328 case WIDGET_FOCUS:
329 /* Never accept focus, otherwise panels will be unselected */
330 return MSG_NOT_HANDLED;
332 case WIDGET_KEY:
333 /* Special case: we handle the enter key */
334 if (parm == '\n')
336 return enter (cmd);
338 /* fall through */
340 default:
341 return input_callback (w, msg, parm);
345 /* --------------------------------------------------------------------------------------------- */
346 /*** public functions ****************************************************************************/
347 /* --------------------------------------------------------------------------------------------- */
349 /* --------------------------------------------------------------------------------------------- */
350 /** Execute the cd command on the command line */
351 void
352 do_cd_command (char *orig_cmd)
354 int len;
355 int operand_pos = CD_OPERAND_OFFSET;
356 const char *cmd;
358 /* Any final whitespace should be removed here
359 (to see why, try "cd fred "). */
360 /* NOTE: I think we should not remove the extra space,
361 that way, we can cd into hidden directories */
362 /* FIXME: what about interpreting quoted strings like the shell.
363 so one could type "cd <tab> M-a <enter>" and it would work. */
364 len = strlen (orig_cmd) - 1;
365 while (len >= 0 && (orig_cmd[len] == ' ' || orig_cmd[len] == '\t' || orig_cmd[len] == '\n'))
367 orig_cmd[len] = 0;
368 len--;
371 cmd = orig_cmd;
372 if (cmd[CD_OPERAND_OFFSET - 1] == 0)
373 cmd = "cd "; /* 0..2 => given text, 3 => \0 */
375 /* allow any amount of white space in front of the path operand */
376 while (cmd[operand_pos] == ' ' || cmd[operand_pos] == '\t')
377 operand_pos++;
379 if (get_current_type () == view_tree)
381 if (cmd[0] == 0)
383 sync_tree (mc_config_get_home_dir ());
385 else if (strcmp (cmd + operand_pos, "..") == 0)
387 char *str_path;
389 if (vfs_path_elements_count (current_panel->cwd_vpath) != 1 ||
390 strlen (vfs_path_get_by_index (current_panel->cwd_vpath, 0)->path) > 1)
392 vfs_path_t *tmp_vpath = current_panel->cwd_vpath;
394 current_panel->cwd_vpath =
395 vfs_path_vtokens_get (tmp_vpath, 0, vfs_path_tokens_count (tmp_vpath) - 1);
396 vfs_path_free (tmp_vpath);
398 str_path = vfs_path_to_str (current_panel->cwd_vpath);
399 sync_tree (str_path);
400 g_free (str_path);
402 else if (cmd[operand_pos] == PATH_SEP)
404 sync_tree (cmd + operand_pos);
406 else
408 char *str_path;
409 vfs_path_t *new_vpath;
411 new_vpath = vfs_path_append_new (current_panel->cwd_vpath, cmd + operand_pos, NULL);
412 str_path = vfs_path_to_str (new_vpath);
413 vfs_path_free (new_vpath);
414 sync_tree (str_path);
415 g_free (str_path);
418 else
420 char *path;
421 vfs_path_t *q_vpath;
422 gboolean ok;
424 path = examine_cd (&cmd[operand_pos]);
426 if (*path == '\0')
427 q_vpath = vfs_path_from_str (mc_config_get_home_dir());
428 else
429 q_vpath = vfs_path_from_str (path);
431 ok = do_cd (q_vpath, cd_parse_command);
432 if (!ok)
433 ok = handle_cdpath (path);
435 if (!ok)
437 char *d;
439 d = vfs_path_to_str_flags (q_vpath, 0, VPF_STRIP_PASSWORD);
440 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"), d,
441 unix_error_string (errno));
442 g_free (d);
445 vfs_path_free (q_vpath);
446 g_free (path);
450 /* --------------------------------------------------------------------------------------------- */
452 WInput *
453 command_new (int y, int x, int cols)
455 WInput *cmd;
456 const input_colors_t command_colors = {
457 DEFAULT_COLOR,
458 COMMAND_MARK_COLOR,
459 DEFAULT_COLOR,
460 COMMAND_HISTORY_COLOR
463 cmd = input_new (y, x, (int *) command_colors, cols, "", "cmdline",
464 INPUT_COMPLETE_DEFAULT | INPUT_COMPLETE_CD | INPUT_COMPLETE_COMMANDS |
465 INPUT_COMPLETE_SHELL_ESC);
467 /* Add our hooks */
468 cmd->widget.callback = command_callback;
470 return cmd;
473 /* --------------------------------------------------------------------------------------------- */
475 * Insert quoted text in input line. The function is meant for the
476 * command line, so the percent sign is quoted as well.
479 void
480 command_insert (WInput * in, const char *text, gboolean insert_extra_space)
482 char *quoted_text;
484 quoted_text = name_quote (text, 1);
485 input_insert (in, quoted_text, insert_extra_space);
486 g_free (quoted_text);
489 /* --------------------------------------------------------------------------------------------- */