2 * Copyright 2004-2005 Timo Hirvonen
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * 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 to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 #include "command_mode.h"
21 #include "search_mode.h"
24 #include "ui_curses.h"
27 #include "tabexp_file.h"
34 #include "play_queue.h"
42 #include "format_print.h"
48 #include "config/datadir.h"
53 #include <sys/types.h>
64 static struct history cmd_history
;
65 static char *cmd_history_filename
;
66 static char *history_search_text
= NULL
;
67 static int arg_expand_cmd
= -1;
69 static char *get_home_dir(const char *username
)
71 struct passwd
*passwd
;
74 return xstrdup(home_dir
);
75 passwd
= getpwnam(username
);
78 /* don't free passwd */
79 return xstrdup(passwd
->pw_dir
);
82 static char *expand_filename(const char *name
)
87 slash
= strchr(name
, '/');
89 char *username
, *home
;
91 if (slash
- name
- 1 > 0) {
93 username
= xstrndup(name
+ 1, slash
- name
- 1);
98 home
= get_home_dir(username
);
103 expanded
= xstrjoin(home
, slash
);
107 return xstrdup(name
);
111 return xstrdup(home_dir
);
115 home
= get_home_dir(name
+ 1);
118 return xstrdup(name
);
122 return xstrdup(name
);
128 void view_clear(int view
)
133 worker_remove_jobs(JOB_TYPE_LIB
);
135 editable_clear(&lib_editable
);
137 /* FIXME: make this optional? */
143 worker_remove_jobs(JOB_TYPE_PL
);
145 editable_clear(&pl_editable
);
149 worker_remove_jobs(JOB_TYPE_QUEUE
);
151 editable_clear(&pq_editable
);
155 info_msg(":clear only works in views 1-4");
159 void view_add(int view
, char *arg
, int prepend
)
164 tmp
= expand_filename(arg
);
165 ft
= cmus_detect_ft(tmp
, &name
);
166 if (ft
== FILE_TYPE_INVALID
) {
167 error_msg("adding '%s': %s", tmp
, strerror(errno
));
176 cmus_add(lib_add_track
, name
, ft
, JOB_TYPE_LIB
);
179 cmus_add(pl_add_track
, name
, ft
, JOB_TYPE_PL
);
183 cmus_add(play_queue_prepend
, name
, ft
, JOB_TYPE_QUEUE
);
185 cmus_add(play_queue_append
, name
, ft
, JOB_TYPE_QUEUE
);
189 info_msg(":add only works in views 1-4");
194 void view_load(int view
, char *arg
)
199 tmp
= expand_filename(arg
);
200 ft
= cmus_detect_ft(tmp
, &name
);
201 if (ft
== FILE_TYPE_INVALID
) {
202 error_msg("loading '%s': %s", tmp
, strerror(errno
));
208 if (ft
== FILE_TYPE_FILE
)
210 if (ft
!= FILE_TYPE_PL
) {
211 error_msg("loading '%s': not a playlist file", name
);
219 worker_remove_jobs(JOB_TYPE_LIB
);
221 editable_clear(&lib_editable
);
223 cmus_add(lib_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
228 worker_remove_jobs(JOB_TYPE_PL
);
230 editable_clear(&pl_editable
);
232 cmus_add(pl_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_PL
);
237 info_msg(":load only works in views 1-3");
242 static void do_save(for_each_ti_cb for_each_ti
, const char *arg
, char **filenamep
)
244 char *filename
= *filenamep
;
248 filename
= xstrdup(arg
);
249 *filenamep
= filename
;
253 if (cmus_save(for_each_ti
, filename
) == -1)
254 error_msg("saving '%s': %s", filename
, strerror(errno
));
258 void view_save(int view
, char *arg
)
263 tmp
= expand_filename(arg
);
264 arg
= path_absolute(tmp
);
271 do_save(lib_for_each
, arg
, &lib_filename
);
274 do_save(pl_for_each
, arg
, &pl_filename
);
277 info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
284 /* only returns the last flag which is enough for it's callers */
285 static int parse_flags(const char **strp
, const char *flags
)
287 const char *str
= *strp
;
302 if (str
[1] == '-' && (str
[2] == 0 || str
[2] == ' ')) {
308 if (str
[2] && str
[2] != ' ')
312 if (!strchr(flags
, flag
)) {
313 error_msg("invalid option -%c", flag
);
330 static int flag_to_view(int flag
)
336 return PLAYLIST_VIEW
;
345 static void cmd_add(char *arg
)
347 int flag
= parse_flags((const char **)&arg
, "lpqQ");
352 error_msg("not enough arguments\n");
355 view_add(flag_to_view(flag
), arg
, flag
== 'Q');
358 static void cmd_clear(char *arg
)
360 int flag
= parse_flags((const char **)&arg
, "lpq");
365 error_msg("too many arguments\n");
368 view_clear(flag_to_view(flag
));
371 static void cmd_load(char *arg
)
373 int flag
= parse_flags((const char **)&arg
, "lp");
378 error_msg("not enough arguments\n");
381 view_load(flag_to_view(flag
), arg
);
384 static void cmd_save(char *arg
)
386 int flag
= parse_flags((const char **)&arg
, "lp");
390 view_save(flag_to_view(flag
), arg
);
393 static void cmd_set(char *arg
)
398 for (i
= 0; arg
[i
]; i
++) {
406 option_set(arg
, value
);
407 help_win
->changed
= 1;
409 struct cmus_opt
*opt
;
410 char buf
[OPTION_MAX_SIZE
];
412 /* support "set <option>?" */
417 opt
= option_find(arg
);
419 opt
->get(opt
->id
, buf
);
420 info_msg("setting: '%s=%s'", arg
, buf
);
425 static void cmd_toggle(char *arg
)
427 struct cmus_opt
*opt
= option_find(arg
);
432 if (opt
->toggle
== NULL
) {
433 error_msg("%s is not toggle option", opt
->name
);
436 opt
->toggle(opt
->id
);
437 help_win
->changed
= 1;
440 static int get_number(char *str
, char **end
)
444 while (*str
>= '0' && *str
<= '9') {
452 static void cmd_seek(char *arg
)
455 int seek
= 0, sign
= 1, count
;
477 num
= get_number(arg
, &end
);
481 seek
= seek
* 60 + num
;
482 } while (++count
< 3);
489 switch (tolower(*arg
)) {
501 player_seek(seek
, relative
);
505 error_msg("expecting one argument: [+-]INTEGER[mh] or [+-]H:MM:SS");
508 static void cmd_factivate(char *arg
)
511 filters_activate_names(arg
);
515 static void cmd_filter(char *arg
)
518 filters_set_anonymous(arg
);
522 static void cmd_fset(char *arg
)
524 filters_set_filter(arg
);
527 static void cmd_invert(char *arg
)
532 editable_invert_marks(&lib_editable
);
535 editable_invert_marks(&pl_editable
);
538 editable_invert_marks(&pq_editable
);
541 info_msg(":invert only works in views 2-4");
546 static void cmd_mark(char *arg
)
551 editable_mark(&lib_editable
, arg
);
554 editable_mark(&pl_editable
, arg
);
557 editable_mark(&pq_editable
, arg
);
560 info_msg(":mark only works in views 2-4");
565 static void cmd_unmark(char *arg
)
570 editable_unmark(&lib_editable
);
573 editable_unmark(&pl_editable
);
576 editable_unmark(&pq_editable
);
579 info_msg(":unmark only works in views 2-4");
584 static void cmd_cd(char *arg
)
587 char *dir
, *absolute
;
589 dir
= expand_filename(arg
);
590 absolute
= path_absolute(dir
);
591 if (chdir(dir
) == -1) {
592 error_msg("could not cd to '%s': %s", dir
, strerror(errno
));
594 browser_chdir(absolute
);
599 if (chdir(home_dir
) == -1) {
600 error_msg("could not cd to '%s': %s", home_dir
, strerror(errno
));
602 browser_chdir(home_dir
);
607 static void cmd_bind(char *arg
)
609 int flag
= parse_flags((const char **)&arg
, "f");
618 key
= strchr(arg
, ' ');
625 func
= strchr(key
, ' ');
634 key_bind(arg
, key
, func
, flag
== 'f');
637 error_msg("expecting 3 arguments (context, key and function)\n");
640 static void cmd_unbind(char *arg
)
642 int flag
= parse_flags((const char **)&arg
, "f");
651 key
= strchr(arg
, ' ');
660 /* FIXME: remove spaces at end */
662 key_unbind(arg
, key
, flag
== 'f');
665 error_msg("expecting 2 arguments (context and key)\n");
668 static void cmd_showbind(char *arg
)
672 key
= strchr(arg
, ' ');
681 /* FIXME: remove spaces at end */
683 show_binding(arg
, key
);
686 error_msg("expecting 2 arguments (context and key)\n");
689 static void cmd_quit(char *arg
)
694 static void cmd_reshuffle(char *arg
)
702 static void cmd_source(char *arg
)
704 char *filename
= expand_filename(arg
);
706 if (source_file(filename
) == -1)
707 error_msg("sourcing %s: %s", filename
, strerror(errno
));
711 static void cmd_colorscheme(char *arg
)
715 snprintf(filename
, sizeof(filename
), "%s/%s.theme", cmus_config_dir
, arg
);
716 if (source_file(filename
) == -1) {
717 snprintf(filename
, sizeof(filename
), DATADIR
"/cmus/%s.theme", arg
);
718 if (source_file(filename
) == -1)
719 error_msg("sourcing %s: %s", filename
, strerror(errno
));
724 * \" inside double-quotes becomes "
725 * \\ inside double-quotes becomes \
727 static char *parse_quoted(const char **strp
)
729 const char *str
= *strp
;
748 ret
= xnew(char, str
- start
);
758 if (c
!= '"' && c
!= '\\')
766 error_msg("`\"' expected");
770 static char *parse_escaped(const char **strp
)
772 const char *str
= *strp
;
780 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
792 ret
= xnew(char, str
- start
+ 1);
798 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
816 static char *parse_one(const char **strp
)
818 const char *str
= *strp
;
828 part
= parse_quoted(&str
);
831 } else if (c
== '\'') {
832 /* backslashes are normal chars inside single-quotes */
836 end
= strchr(str
, '\'');
839 part
= xstrndup(str
, end
- str
);
842 part
= parse_escaped(&str
);
848 char *tmp
= xstrjoin(ret
, part
);
856 error_msg("`'' expected");
862 static char **parse_cmd(const char *cmd
, int *args_idx
, int *ac
)
871 /* there can't be spaces at start of command
872 * and there is at least one argument */
873 if (cmd
[0] == '{' && cmd
[1] == '}' && (cmd
[2] == ' ' || cmd
[2] == 0)) {
874 /* {} is replaced with file arguments */
876 goto only_once_please
;
881 arg
= parse_one(&cmd
);
887 alloc
= alloc
? alloc
* 2 : 4;
888 av
= xrenew(char *, av
, alloc
+ 1);
899 error_msg("{} can be used only once");
907 static struct track_info
**sel_tis
;
908 static int sel_tis_alloc
;
909 static int sel_tis_nr
;
911 static int add_ti(void *data
, struct track_info
*ti
)
913 if (sel_tis_nr
== sel_tis_alloc
) {
914 sel_tis_alloc
= sel_tis_alloc
? sel_tis_alloc
* 2 : 8;
915 sel_tis
= xrenew(struct track_info
*, sel_tis
, sel_tis_alloc
);
918 sel_tis
[sel_tis_nr
++] = ti
;
922 static void cmd_run(char *arg
)
925 int ac
, argc
, i
, run
, files_idx
= -1;
927 if (cur_view
> QUEUE_VIEW
) {
928 info_msg("Command execution is supported only in views 1-4");
932 av
= parse_cmd(arg
, &files_idx
, &ac
);
937 /* collect selected files (struct track_info) */
945 __tree_for_each_sel(add_ti
, NULL
, 0);
948 __editable_for_each_sel(&lib_editable
, add_ti
, NULL
, 0);
951 __editable_for_each_sel(&pl_editable
, add_ti
, NULL
, 0);
954 __editable_for_each_sel(&pq_editable
, add_ti
, NULL
, 0);
959 if (sel_tis_nr
== 0) {
960 /* no files selected, do nothing */
964 sel_tis
[sel_tis_nr
] = NULL
;
967 argv
= xnew(char *, ac
+ sel_tis_nr
+ 1);
969 if (files_idx
== -1) {
970 /* add selected files after rest of the args */
971 for (i
= 0; i
< ac
; i
++)
972 argv
[argc
++] = av
[i
];
973 for (i
= 0; i
< sel_tis_nr
; i
++)
974 argv
[argc
++] = sel_tis
[i
]->filename
;
976 for (i
= 0; i
< files_idx
; i
++)
977 argv
[argc
++] = av
[i
];
978 for (i
= 0; i
< sel_tis_nr
; i
++)
979 argv
[argc
++] = sel_tis
[i
]->filename
;
980 for (i
= files_idx
; i
< ac
; i
++)
981 argv
[argc
++] = av
[i
];
985 for (i
= 0; argv
[i
]; i
++)
986 d_print("ARG: '%s'\n", argv
[i
]);
989 if (confirm_run
&& (sel_tis_nr
> 1 || strcmp(argv
[0], "rm") == 0)) {
990 if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg
, sel_tis_nr
)) {
998 if (spawn(argv
, &status
)) {
999 error_msg("executing %s: %s", argv
[0], strerror(errno
));
1001 if (WIFEXITED(status
)) {
1002 int rc
= WEXITSTATUS(status
);
1005 error_msg("%s returned %d", argv
[0], rc
);
1007 if (WIFSIGNALED(status
))
1008 error_msg("%s received signal %d", argv
[0], WTERMSIG(status
));
1013 /* this must be done before sel_tis are unreffed */
1017 /* remove non-existed files, update tags for changed files */
1018 cmus_update_tis(sel_tis
, sel_tis_nr
);
1020 /* we don't own sel_tis anymore! */
1027 for (i
= 0; sel_tis
[i
]; i
++)
1028 track_info_unref(sel_tis
[i
]);
1032 static int get_one_ti(void *data
, struct track_info
*ti
)
1034 struct track_info
**sel_ti
= data
;
1038 /* stop the for each loop, we need only the first selected track */
1042 static void cmd_echo(char *arg
)
1044 struct track_info
*sel_ti
;
1048 ptr
= strchr(ptr
, '{');
1057 info_msg("%s", arg
);
1061 if (cur_view
> QUEUE_VIEW
) {
1062 info_msg("echo with {} in its arguments is supported only in views 1-4");
1069 /* get only the first selected track */
1075 __tree_for_each_sel(get_one_ti
, &sel_ti
, 0);
1078 __editable_for_each_sel(&lib_editable
, get_one_ti
, &sel_ti
, 0);
1081 __editable_for_each_sel(&pl_editable
, get_one_ti
, &sel_ti
, 0);
1084 __editable_for_each_sel(&pq_editable
, get_one_ti
, &sel_ti
, 0);
1092 info_msg("%s%s%s", arg
, sel_ti
->filename
, ptr
);
1093 track_info_unref(sel_ti
);
1096 #define VF_RELATIVE 0x01
1097 #define VF_PERCENTAGE 0x02
1099 static int parse_vol_arg(const char *arg
, int *value
, unsigned int *flags
)
1102 int ch
, val
= 0, digits
= 0, sign
= 1;
1108 } else if (*arg
== '+') {
1115 if (ch
< '0' || ch
> '9')
1131 *value
= sign
* val
;
1138 static int calc_vol(int val
, int old
, unsigned int flags
)
1140 if (flags
& VF_RELATIVE
) {
1141 if (flags
& VF_PERCENTAGE
)
1142 val
= scale_from_percentage(val
, volume_max
);
1144 } else if (flags
& VF_PERCENTAGE
) {
1145 val
= scale_from_percentage(val
, volume_max
);
1147 return clamp(val
, 0, volume_max
);
1151 * :vol value [value]
1153 * where value is [-+]?[0-9]+%?
1155 static void cmd_vol(char *arg
)
1157 char **values
= get_words(arg
);
1158 unsigned int lf
, rf
;
1161 if (values
[1] && values
[2])
1164 if (parse_vol_arg(values
[0], &l
, &lf
))
1169 if (values
[1] && parse_vol_arg(values
[1], &r
, &rf
))
1172 free_str_array(values
);
1174 player_get_volume(&ol
, &or);
1175 l
= calc_vol(l
, ol
, lf
);
1176 r
= calc_vol(r
, or, rf
);
1177 player_set_volume(l
, r
);
1180 free_str_array(values
);
1181 error_msg("expecting 1 or 2 arguments (total or L and R volumes [+-]INTEGER[%%])\n");
1184 static void cmd_view(char *arg
)
1188 if (parse_enum(arg
, 1, NR_VIEWS
, view_names
, &view
))
1192 static void cmd_p_next(char *arg
)
1197 static void cmd_p_pause(char *arg
)
1202 static void cmd_p_play(char *arg
)
1205 player_play_file(arg
);
1211 static void cmd_p_prev(char *arg
)
1216 static void cmd_p_stop(char *arg
)
1221 static void cmd_search_next(char *arg
)
1224 if (!search_next(searchable
, search_str
, search_direction
))
1229 static void cmd_search_prev(char *arg
)
1232 if (!search_next(searchable
, search_str
, !search_direction
))
1237 static int sorted_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1239 return editable_for_each_sel(&lib_editable
, cb
, data
, reverse
);
1242 static int pl_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1244 return editable_for_each_sel(&pl_editable
, cb
, data
, reverse
);
1247 static int pq_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1249 return editable_for_each_sel(&pq_editable
, cb
, data
, reverse
);
1252 static for_each_sel_ti_cb view_for_each_sel
[4] = {
1254 sorted_for_each_sel
,
1259 /* wrapper for void lib_add_track(struct track_info *) etc. */
1260 static int wrapper_cb(void *data
, struct track_info
*ti
)
1262 add_ti_cb add
= data
;
1268 static void add_from_browser(add_ti_cb add
, int job_type
)
1270 char *sel
= browser_get_sel();
1276 ft
= cmus_detect_ft(sel
, &ret
);
1277 if (ft
!= FILE_TYPE_INVALID
) {
1278 cmus_add(add
, ret
, ft
, job_type
);
1279 window_down(browser_win
, 1);
1286 static void cmd_win_add_l(char *arg
)
1288 if (cur_view
== TREE_VIEW
|| cur_view
== SORTED_VIEW
)
1291 if (cur_view
<= QUEUE_VIEW
) {
1293 view_for_each_sel
[cur_view
](wrapper_cb
, lib_add_track
, 0);
1295 } else if (cur_view
== BROWSER_VIEW
) {
1296 add_from_browser(lib_add_track
, JOB_TYPE_LIB
);
1300 static void cmd_win_add_p(char *arg
)
1302 /* could allow adding dups? */
1303 if (cur_view
== PLAYLIST_VIEW
)
1306 if (cur_view
<= QUEUE_VIEW
) {
1308 view_for_each_sel
[cur_view
](wrapper_cb
, pl_add_track
, 0);
1310 } else if (cur_view
== BROWSER_VIEW
) {
1311 add_from_browser(pl_add_track
, JOB_TYPE_PL
);
1315 static void cmd_win_add_Q(char *arg
)
1317 if (cur_view
== QUEUE_VIEW
)
1320 if (cur_view
<= QUEUE_VIEW
) {
1322 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_prepend
, 1);
1324 } else if (cur_view
== BROWSER_VIEW
) {
1325 add_from_browser(play_queue_prepend
, JOB_TYPE_QUEUE
);
1329 static void cmd_win_add_q(char *arg
)
1331 if (cur_view
== QUEUE_VIEW
)
1334 if (cur_view
<= QUEUE_VIEW
) {
1336 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_append
, 0);
1338 } else if (cur_view
== BROWSER_VIEW
) {
1339 add_from_browser(play_queue_append
, JOB_TYPE_QUEUE
);
1343 static void cmd_win_activate(char *arg
)
1345 struct track_info
*info
= NULL
;
1350 info
= tree_set_selected();
1353 info
= sorted_set_selected();
1356 info
= pl_set_selected();
1373 /* update lib/pl mode */
1379 player_play_file(info
->filename
);
1380 track_info_unref(info
);
1384 static void cmd_win_mv_after(char *arg
)
1391 editable_move_after(&lib_editable
);
1394 editable_move_after(&pl_editable
);
1397 editable_move_after(&pq_editable
);
1409 static void cmd_win_mv_before(char *arg
)
1416 editable_move_before(&lib_editable
);
1419 editable_move_before(&pl_editable
);
1422 editable_move_before(&pq_editable
);
1434 static void cmd_win_remove(char *arg
)
1442 editable_remove_sel(&lib_editable
);
1445 editable_remove_sel(&pl_editable
);
1448 editable_remove_sel(&pq_editable
);
1454 filters_delete_filter();
1463 static void cmd_win_sel_cur(char *arg
)
1471 sorted_sel_current();
1488 static void cmd_win_toggle(char *arg
)
1493 tree_toggle_expand_artist();
1498 editable_toggle_mark(&lib_editable
);
1503 editable_toggle_mark(&pl_editable
);
1508 editable_toggle_mark(&pq_editable
);
1514 filters_toggle_filter();
1522 static struct window
*current_win(void)
1528 return lib_editable
.win
;
1530 return pl_editable
.win
;
1532 return pq_editable
.win
;
1543 static void cmd_win_bottom(char *arg
)
1546 window_goto_bottom(current_win());
1550 static void cmd_win_down(char *arg
)
1553 window_down(current_win(), 1);
1557 static void cmd_win_next(char *arg
)
1559 if (cur_view
== TREE_VIEW
) {
1561 tree_toggle_active_window();
1566 static void cmd_win_pg_down(char *arg
)
1569 window_page_down(current_win());
1573 static void cmd_win_pg_up(char *arg
)
1576 window_page_up(current_win());
1580 static void cmd_win_top(char *arg
)
1583 window_goto_top(current_win());
1587 static void cmd_win_up(char *arg
)
1590 window_up(current_win(), 1);
1594 static void cmd_win_update(char *arg
)
1607 static void cmd_browser_up(char *arg
)
1612 static void cmd_refresh(char *arg
)
1614 clearok(curscr
, TRUE
);
1620 * these functions fill tabexp struct, which is resetted beforehand
1623 /* buffer used for tab expansion */
1624 static char expbuf
[512];
1626 static int filter_directories(const char *name
, const struct stat
*s
)
1628 return S_ISDIR(s
->st_mode
);
1631 static int filter_any(const char *name
, const struct stat
*s
)
1636 static int filter_playable(const char *name
, const struct stat
*s
)
1638 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1641 static int filter_playlist(const char *name
, const struct stat
*s
)
1643 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1646 static int filter_supported(const char *name
, const struct stat
*s
)
1648 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1651 static void expand_files(const char *str
)
1653 expand_files_and_dirs(str
, filter_any
);
1656 static void expand_directories(const char *str
)
1658 expand_files_and_dirs(str
, filter_directories
);
1661 static void expand_playable(const char *str
)
1663 expand_files_and_dirs(str
, filter_playable
);
1666 static void expand_playlist(const char *str
)
1668 expand_files_and_dirs(str
, filter_playlist
);
1671 static void expand_supported(const char *str
)
1673 expand_files_and_dirs(str
, filter_supported
);
1676 static void expand_add(const char *str
)
1678 int flag
= parse_flags(&str
, "lpqQ");
1684 expand_supported(str
);
1686 if (tabexp
.head
&& flag
) {
1687 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1689 tabexp
.head
= xstrdup(expbuf
);
1693 static void expand_load_save(const char *str
)
1695 int flag
= parse_flags(&str
, "lp");
1701 expand_playlist(str
);
1703 if (tabexp
.head
&& flag
) {
1704 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1706 tabexp
.head
= xstrdup(expbuf
);
1710 static void expand_key_context(const char *str
, const char *force
)
1712 int pos
, i
, len
= strlen(str
);
1715 tails
= xnew(char *, NR_CTXS
+ 1);
1717 for (i
= 0; key_context_names
[i
]; i
++) {
1718 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1723 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1731 char *tmp
= xstrjoin(tails
[0], " ");
1736 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1737 tabexp
.head
= xstrdup(expbuf
);
1738 tabexp
.tails
= tails
;
1741 static int get_context(const char *str
, int len
)
1743 int i
, c
= -1, count
= 0;
1745 for (i
= 0; key_context_names
[i
]; i
++) {
1746 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1747 if (key_context_names
[i
][len
] == 0) {
1760 static void expand_command_line(const char *str
);
1762 static void expand_bind_args(const char *str
)
1764 /* :bind context key function
1766 * possible values for str:
1771 * you need to know context before you can expand function
1773 /* start and end pointers for context, key and function */
1774 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1776 int flag
= parse_flags((const char **)&str
, "f");
1777 const char *force
= "";
1788 ce
= strchr(cs
, ' ');
1790 expand_key_context(cs
, force
);
1794 /* context must be expandable */
1795 c
= get_context(cs
, ce
- cs
);
1797 /* context is ambiguous or invalid */
1804 ke
= strchr(ks
, ' ');
1807 int len
= strlen(ks
);
1810 for (i
= 0; key_table
[i
].name
; i
++) {
1811 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
1816 ptr_array_add(&array
, xstrdup(key_table
[i
].name
+ len
));
1822 if (array
.count
== 1) {
1823 char **ptrs
= array
.ptrs
;
1824 char *tmp
= xstrjoin(ptrs
[0], " ");
1829 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
1831 ptr_array_plug(&array
);
1832 tabexp
.head
= xstrdup(expbuf
);
1833 tabexp
.tails
= array
.ptrs
;
1837 /* key must be expandable */
1840 for (i
= 0; key_table
[i
].name
; i
++) {
1841 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
1842 if (key_table
[i
].name
[ke
- ks
] == 0) {
1853 /* key is ambiguous or invalid */
1864 /* expand com [arg...] */
1865 expand_command_line(fs
);
1866 if (tabexp
.head
== NULL
) {
1867 /* command expand failed */
1872 * tabexp.head is now "com"
1873 * tabexp.tails is [ mand1 mand2 ... ]
1875 * need to change tabexp.head to "context key com"
1878 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
1879 key_table
[k
].name
, tabexp
.head
);
1881 tabexp
.head
= xstrdup(expbuf
);
1884 static void expand_unbind_args(const char *str
)
1886 /* :unbind context key */
1887 /* start and end pointers for context and key */
1888 const char *cs
, *ce
, *ks
;
1889 const struct binding
*b
;
1894 ce
= strchr(cs
, ' ');
1896 expand_key_context(cs
, "");
1900 /* context must be expandable */
1901 c
= get_context(cs
, ce
- cs
);
1903 /* context is ambiguous or invalid */
1913 b
= key_bindings
[c
];
1915 if (!strncmp(ks
, b
->key
->name
, len
))
1916 ptr_array_add(&array
, xstrdup(b
->key
->name
+ len
));
1922 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
1924 ptr_array_plug(&array
);
1925 tabexp
.head
= xstrdup(expbuf
);
1926 tabexp
.tails
= array
.ptrs
;
1929 static void expand_factivate(const char *str
)
1931 /* "name1 name2 name3", expand only name3 */
1932 struct filter_entry
*e
;
1935 int str_len
, len
, i
;
1937 str_len
= strlen(str
);
1940 if (str
[i
- 1] == ' ')
1947 list_for_each_entry(e
, &filters_head
, node
) {
1948 if (!strncmp(name
, e
->name
, len
))
1949 ptr_array_add(&array
, xstrdup(e
->name
+ len
));
1954 ptr_array_plug(&array
);
1955 tabexp
.head
= xstrdup(str
);
1956 tabexp
.tails
= array
.ptrs
;
1959 static void expand_options(const char *str
)
1961 struct cmus_opt
*opt
;
1965 /* tabexp is resetted */
1967 if (len
> 1 && str
[len
- 1] == '=') {
1969 char *var
= xstrndup(str
, len
- 1);
1971 list_for_each_entry(opt
, &option_head
, node
) {
1972 if (strcmp(var
, opt
->name
) == 0) {
1973 char buf
[OPTION_MAX_SIZE
];
1975 tails
= xnew(char *, 2);
1978 opt
->get(opt
->id
, buf
);
1979 tails
[0] = xstrdup(buf
);
1982 tabexp
.head
= xstrdup(str
);
1983 tabexp
.tails
= tails
;
1990 /* expand variable */
1993 tails
= xnew(char *, nr_options
+ 1);
1995 list_for_each_entry(opt
, &option_head
, node
) {
1996 if (strncmp(str
, opt
->name
, len
) == 0)
1997 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2001 /* only one variable matches, add '=' */
2002 char *tmp
= xstrjoin(tails
[0], "=");
2009 tabexp
.head
= xstrdup(str
);
2010 tabexp
.tails
= tails
;
2017 static void expand_toptions(const char *str
)
2019 struct cmus_opt
*opt
;
2023 tails
= xnew(char *, nr_options
+ 1);
2026 list_for_each_entry(opt
, &option_head
, node
) {
2027 if (opt
->toggle
== NULL
)
2029 if (strncmp(str
, opt
->name
, len
) == 0)
2030 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2034 tabexp
.head
= xstrdup(str
);
2035 tabexp
.tails
= tails
;
2041 static void load_themes(const char *dirname
, const char *str
, struct ptr_array
*array
)
2043 struct directory dir
;
2044 const char *name
, *dot
;
2045 int len
= strlen(str
);
2047 if (dir_open(&dir
, dirname
))
2050 while ((name
= dir_read(&dir
))) {
2051 if (!S_ISREG(dir
.st
.st_mode
))
2053 if (strncmp(name
, str
, len
))
2055 dot
= strrchr(name
, '.');
2056 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2058 if (dot
- name
< len
)
2060 * matches "foo.theme"
2061 * which also ends with ".theme"
2064 ptr_array_add(array
, xstrndup(name
+ len
, dot
- name
- len
));
2069 static void expand_colorscheme(const char *str
)
2073 load_themes(cmus_config_dir
, str
, &array
);
2074 load_themes(DATADIR
"/cmus", str
, &array
);
2077 ptr_array_sort(&array
, strptrcmp
);
2079 ptr_array_plug(&array
);
2080 tabexp
.head
= xstrdup(str
);
2081 tabexp
.tails
= array
.ptrs
;
2088 struct command commands
[] = {
2089 { "add", cmd_add
, 1, 1, expand_add
, 0, 0 },
2090 { "bind", cmd_bind
, 1, 1, expand_bind_args
, 0, CMD_UNSAFE
},
2091 { "browser-up", cmd_browser_up
, 0, 0, NULL
, 0, 0 },
2092 { "cd", cmd_cd
, 0, 1, expand_directories
, 0, 0 },
2093 { "clear", cmd_clear
, 0, 1, NULL
, 0, 0 },
2094 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
, 0, 0 },
2095 { "echo", cmd_echo
, 1,-1, NULL
, 0, 0 },
2096 { "factivate", cmd_factivate
, 0, 1, expand_factivate
, 0, 0 },
2097 { "filter", cmd_filter
, 0, 1, NULL
, 0, 0 },
2098 { "fset", cmd_fset
, 1, 1, NULL
, 0, 0 },
2099 { "invert", cmd_invert
, 0, 0, NULL
, 0, 0 },
2100 { "load", cmd_load
, 1, 1, expand_load_save
, 0, 0 },
2101 { "mark", cmd_mark
, 0, 1, NULL
, 0, 0 },
2102 { "player-next", cmd_p_next
, 0, 0, NULL
, 0, 0 },
2103 { "player-pause", cmd_p_pause
, 0, 0, NULL
, 0, 0 },
2104 { "player-play", cmd_p_play
, 0, 1, expand_playable
, 0, 0 },
2105 { "player-prev", cmd_p_prev
, 0, 0, NULL
, 0, 0 },
2106 { "player-stop", cmd_p_stop
, 0, 0, NULL
, 0, 0 },
2107 { "quit", cmd_quit
, 0, 0, NULL
, 0, 0 },
2108 { "refresh", cmd_refresh
, 0, 0, NULL
, 0, 0 },
2109 { "run", cmd_run
, 1,-1, NULL
, 0, CMD_UNSAFE
},
2110 { "save", cmd_save
, 0, 1, expand_load_save
, 0, CMD_UNSAFE
},
2111 { "search-next", cmd_search_next
,0, 0, NULL
, 0, 0 },
2112 { "search-prev", cmd_search_prev
,0, 0, NULL
, 0, 0 },
2113 { "seek", cmd_seek
, 1, 1, NULL
, 0, 0 },
2114 { "set", cmd_set
, 1, 1, expand_options
, 0, 0 },
2115 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
, 0, 0 },
2116 { "shuffle", cmd_reshuffle
, 0, 0, NULL
, 0, 0 },
2117 { "source", cmd_source
, 1, 1, expand_files
, 0, CMD_UNSAFE
},
2118 { "toggle", cmd_toggle
, 1, 1, expand_toptions
, 0, 0 },
2119 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
, 0, 0 },
2120 { "unmark", cmd_unmark
, 0, 0, NULL
, 0, 0 },
2121 { "view", cmd_view
, 1, 1, NULL
, 0, 0 },
2122 { "vol", cmd_vol
, 1, 2, NULL
, 0, 0 },
2123 { "win-activate", cmd_win_activate
,0, 0, NULL
, 0, 0 },
2124 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
, 0, 0 },
2125 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
, 0, 0 },
2126 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
, 0, 0 },
2127 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
, 0, 0 },
2128 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
, 0, 0 },
2129 { "win-down", cmd_win_down
, 0, 0, NULL
, 0, 0 },
2130 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
, 0, 0 },
2131 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
, 0, 0 },
2132 { "win-next", cmd_win_next
, 0, 0, NULL
, 0, 0 },
2133 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
, 0, 0 },
2134 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
, 0, 0 },
2135 { "win-remove", cmd_win_remove
, 0, 0, NULL
, 0, CMD_UNSAFE
},
2136 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
, 0, 0 },
2137 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
, 0, 0 },
2138 { "win-top", cmd_win_top
, 0, 0, NULL
, 0, 0 },
2139 { "win-up", cmd_win_up
, 0, 0, NULL
, 0, 0 },
2140 { "win-update", cmd_win_update
, 0, 0, NULL
, 0, 0 },
2141 { NULL
, NULL
, 0, 0, 0, 0, 0 }
2144 /* fills tabexp struct */
2145 static void expand_commands(const char *str
)
2150 /* tabexp is resetted */
2151 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2154 for (i
= 0; commands
[i
].name
; i
++) {
2155 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2156 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2160 /* only one command matches, add ' ' */
2161 char *tmp
= xstrjoin(tails
[0], " ");
2167 tabexp
.head
= xstrdup(str
);
2168 tabexp
.tails
= tails
;
2174 struct command
*get_command(const char *str
)
2180 for (len
= 0; str
[len
] && str
[len
] != ' '; len
++)
2183 for (i
= 0; commands
[i
].name
; i
++) {
2184 if (strncmp(str
, commands
[i
].name
, len
))
2187 if (commands
[i
].name
[len
] == 0) {
2189 return &commands
[i
];
2192 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2196 return &commands
[i
];
2201 /* fills tabexp struct */
2202 static void expand_command_line(const char *str
)
2204 /* :command [arg]...
2208 * str expanded value (tabexp.head)
2209 * -------------------------------------
2212 * se se (tabexp.tails = [ ek t ])
2214 /* command start/end, argument start */
2215 const char *cs
, *ce
, *as
;
2216 const struct command
*cmd
;
2219 ce
= strchr(cs
, ' ');
2221 /* expand command */
2222 expand_commands(cs
);
2226 /* command must be expandable */
2227 cmd
= get_command(cs
);
2229 /* command ambiguous or invalid */
2233 if (cmd
->expand
== NULL
) {
2234 /* can't expand argument */
2242 /* expand argument */
2244 if (tabexp
.head
== NULL
) {
2245 /* argument expansion failed */
2249 /* tabexp.head is now start of the argument string */
2250 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2252 tabexp
.head
= xstrdup(expbuf
);
2255 static void tab_expand(void)
2257 char *s1
, *s2
, *tmp
;
2260 /* strip white space */
2262 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2265 /* string to expand */
2266 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2269 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2271 tmp
= tabexp_expand(s1
, expand_command_line
);
2278 cmdline
.blen
= l1
+ l2
;
2279 if (cmdline
.blen
>= cmdline
.size
) {
2280 while (cmdline
.blen
>= cmdline
.size
)
2282 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2284 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2286 cmdline
.cpos
= u_strlen(tmp
);
2287 cmdline
.clen
= u_strlen(cmdline
.line
);
2294 static void reset_tab_expansion(void)
2297 arg_expand_cmd
= -1;
2300 int run_only_safe_commands
;
2302 /* FIXME: parse all arguments */
2303 void run_command(const char *buf
)
2306 int cmd_start
, cmd_end
, cmd_len
;
2307 int arg_start
, arg_end
;
2311 while (buf
[i
] && buf
[i
] == ' ')
2318 while (buf
[i
] && buf
[i
] != ' ')
2321 while (buf
[i
] && buf
[i
] == ' ')
2328 cmd_len
= cmd_end
- cmd_start
;
2332 cmd
= xstrndup(buf
+ cmd_start
, cmd_len
);
2333 if (arg_start
== arg_end
) {
2336 arg
= xstrndup(buf
+ arg_start
, arg_end
- arg_start
);
2340 const struct command
*c
= &commands
[i
];
2342 if (c
->name
== NULL
) {
2343 error_msg("unknown command\n");
2346 if (strncmp(cmd
, c
->name
, cmd_len
) == 0) {
2347 const char *next
= commands
[i
+ 1].name
;
2348 int exact
= c
->name
[cmd_len
] == 0;
2350 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2351 error_msg("ambiguous command\n");
2354 if (c
->min_args
> 0 && arg
== NULL
) {
2355 error_msg("not enough arguments\n");
2358 if (c
->max_args
== 0 && arg
) {
2359 error_msg("too many arguments\n");
2362 if (run_only_safe_commands
&& c
->flags
& CMD_UNSAFE
) {
2363 d_print("trying to execute unsafe command over net\n");
2375 static void reset_history_search(void)
2377 history_reset_search(&cmd_history
);
2378 free(history_search_text
);
2379 history_search_text
= NULL
;
2382 static void backspace(void)
2384 if (cmdline
.clen
> 0) {
2385 cmdline_backspace();
2387 input_mode
= NORMAL_MODE
;
2391 void command_mode_ch(uchar ch
)
2395 cmdline_move_home();
2398 cmdline_move_left();
2401 cmdline_delete_ch();
2407 cmdline_move_right();
2413 history_add_line(&cmd_history
, cmdline
.line
);
2416 input_mode
= NORMAL_MODE
;
2420 run_command(cmdline
.line
);
2421 history_add_line(&cmd_history
, cmdline
.line
);
2424 input_mode
= NORMAL_MODE
;
2427 cmdline_clear_end();
2430 /* tab expansion should not complain */
2436 cmdline_backspace_to_bol();
2442 cmdline_insert_ch(ch
);
2444 reset_history_search();
2446 reset_tab_expansion();
2449 void command_mode_key(int key
)
2451 reset_tab_expansion();
2454 cmdline_delete_ch();
2460 cmdline_move_left();
2463 cmdline_move_right();
2466 cmdline_move_home();
2475 if (history_search_text
== NULL
)
2476 history_search_text
= xstrdup(cmdline
.line
);
2477 s
= history_search_forward(&cmd_history
, history_search_text
);
2479 cmdline_set_text(s
);
2483 if (history_search_text
) {
2486 s
= history_search_backward(&cmd_history
, history_search_text
);
2488 cmdline_set_text(s
);
2490 cmdline_set_text(history_search_text
);
2495 d_print("key = %c (%d)\n", key
, key
);
2497 reset_history_search();
2500 void commands_init(void)
2502 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2503 history_load(&cmd_history
, cmd_history_filename
, 2000);
2506 void commands_exit(void)
2508 history_save(&cmd_history
);
2509 free(cmd_history_filename
);