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>
47 #include "config/datadir.h"
52 #include <sys/types.h>
57 static struct history cmd_history
;
58 static char *cmd_history_filename
;
59 static char *history_search_text
= NULL
;
60 static int arg_expand_cmd
= -1;
62 static char *get_home_dir(const char *username
)
64 struct passwd
*passwd
;
67 return xstrdup(home_dir
);
68 passwd
= getpwnam(username
);
71 /* don't free passwd */
72 return xstrdup(passwd
->pw_dir
);
75 static char *expand_filename(const char *name
)
80 slash
= strchr(name
, '/');
82 char *username
, *home
;
84 if (slash
- name
- 1 > 0) {
86 username
= xstrndup(name
+ 1, slash
- name
- 1);
91 home
= get_home_dir(username
);
96 expanded
= xstrjoin(home
, slash
);
100 return xstrdup(name
);
104 return xstrdup(home_dir
);
108 home
= get_home_dir(name
+ 1);
111 return xstrdup(name
);
115 return xstrdup(name
);
121 void view_clear(int view
)
126 worker_remove_jobs(JOB_TYPE_LIB
);
128 editable_clear(&lib_editable
);
130 /* FIXME: make this optional? */
136 worker_remove_jobs(JOB_TYPE_PL
);
138 editable_clear(&pl_editable
);
142 worker_remove_jobs(JOB_TYPE_QUEUE
);
144 editable_clear(&pq_editable
);
148 info_msg(":clear only works in views 1-4");
152 void view_add(int view
, char *arg
, int prepend
)
157 tmp
= expand_filename(arg
);
158 ft
= cmus_detect_ft(tmp
, &name
);
159 if (ft
== FILE_TYPE_INVALID
) {
160 error_msg("adding '%s': %s", tmp
, strerror(errno
));
169 cmus_add(lib_add_track
, name
, ft
, JOB_TYPE_LIB
);
172 cmus_add(pl_add_track
, name
, ft
, JOB_TYPE_PL
);
176 cmus_add(play_queue_prepend
, name
, ft
, JOB_TYPE_QUEUE
);
178 cmus_add(play_queue_append
, name
, ft
, JOB_TYPE_QUEUE
);
182 info_msg(":add only works in views 1-4");
187 void view_load(int view
, char *arg
)
192 tmp
= expand_filename(arg
);
193 ft
= cmus_detect_ft(tmp
, &name
);
194 if (ft
== FILE_TYPE_INVALID
) {
195 error_msg("loading '%s': %s", tmp
, strerror(errno
));
201 if (ft
== FILE_TYPE_FILE
)
203 if (ft
!= FILE_TYPE_PL
) {
204 error_msg("loading '%s': not a playlist file", name
);
212 worker_remove_jobs(JOB_TYPE_LIB
);
214 editable_clear(&lib_editable
);
216 cmus_add(lib_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
221 worker_remove_jobs(JOB_TYPE_PL
);
223 editable_clear(&pl_editable
);
225 cmus_add(pl_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_PL
);
230 info_msg(":load only works in views 1-3");
235 static void do_save(for_each_ti_cb for_each_ti
, const char *arg
, char **filenamep
)
237 char *filename
= *filenamep
;
241 filename
= xstrdup(arg
);
242 *filenamep
= filename
;
246 if (cmus_save(for_each_ti
, filename
) == -1)
247 error_msg("saving '%s': %s", filename
, strerror(errno
));
251 void view_save(int view
, char *arg
)
256 tmp
= expand_filename(arg
);
257 arg
= path_absolute(tmp
);
264 do_save(lib_for_each
, arg
, &lib_filename
);
267 do_save(pl_for_each
, arg
, &pl_filename
);
270 info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
277 /* only returns the last flag which is enough for it's callers */
278 static int parse_flags(const char **strp
, const char *flags
)
280 const char *str
= *strp
;
295 if (str
[1] == '-' && (str
[2] == 0 || str
[2] == ' ')) {
301 if (str
[2] && str
[2] != ' ')
305 if (!strchr(flags
, flag
)) {
306 error_msg("invalid option -%c", flag
);
323 static int flag_to_view(int flag
)
329 return PLAYLIST_VIEW
;
338 static void cmd_add(char *arg
)
340 int flag
= parse_flags((const char **)&arg
, "lpqQ");
345 error_msg("not enough arguments\n");
348 view_add(flag_to_view(flag
), arg
, flag
== 'Q');
351 static void cmd_clear(char *arg
)
353 int flag
= parse_flags((const char **)&arg
, "lpq");
358 error_msg("too many arguments\n");
361 view_clear(flag_to_view(flag
));
364 static void cmd_load(char *arg
)
366 int flag
= parse_flags((const char **)&arg
, "lp");
371 error_msg("not enough arguments\n");
374 view_load(flag_to_view(flag
), arg
);
377 static void cmd_save(char *arg
)
379 int flag
= parse_flags((const char **)&arg
, "lp");
383 view_save(flag_to_view(flag
), arg
);
386 static void cmd_set(char *arg
)
391 for (i
= 0; arg
[i
]; i
++) {
399 option_set(arg
, value
);
401 struct cmus_opt
*opt
;
402 char buf
[OPTION_MAX_SIZE
];
404 /* support "set <option>?" */
409 opt
= option_find(arg
);
411 opt
->get(opt
->id
, buf
);
412 info_msg("setting: '%s=%s'", arg
, buf
);
417 static void cmd_toggle(char *arg
)
419 struct cmus_opt
*opt
= option_find(arg
);
424 if (opt
->toggle
== NULL
) {
425 error_msg("%s is not toggle option", opt
->name
);
428 opt
->toggle(opt
->id
);
431 static int get_number(char *str
, char **end
)
435 while (*str
>= '0' && *str
<= '9') {
443 static void cmd_seek(char *arg
)
445 int seek_mode
= SEEK_SET
;
446 int seek
= 0, sign
= 1, count
;
452 seek_mode
= SEEK_CUR
;
468 num
= get_number(arg
, &end
);
472 seek
= seek
* 60 + num
;
473 } while (++count
< 3);
480 switch (tolower(*arg
)) {
492 player_seek(seek
, seek_mode
);
496 error_msg("expecting one argument: [+-]INTEGER[mh] or [+-]H:MM:SS");
499 static void cmd_factivate(char *arg
)
502 filters_activate_names(arg
);
506 static void cmd_filter(char *arg
)
509 filters_set_anonymous(arg
);
513 static void cmd_fset(char *arg
)
515 filters_set_filter(arg
);
518 static void cmd_invert(char *arg
)
523 editable_invert_marks(&lib_editable
);
526 editable_invert_marks(&pl_editable
);
529 editable_invert_marks(&pq_editable
);
532 info_msg(":invert only works in views 2-4");
537 static void cmd_mark(char *arg
)
542 editable_mark(&lib_editable
, arg
);
545 editable_mark(&pl_editable
, arg
);
548 editable_mark(&pq_editable
, arg
);
551 info_msg(":mark only works in views 2-4");
556 static void cmd_unmark(char *arg
)
561 editable_unmark(&lib_editable
);
564 editable_unmark(&pl_editable
);
567 editable_unmark(&pq_editable
);
570 info_msg(":unmark only works in views 2-4");
575 static void cmd_cd(char *arg
)
578 char *dir
, *absolute
;
580 dir
= expand_filename(arg
);
581 absolute
= path_absolute(dir
);
582 if (chdir(dir
) == -1) {
583 error_msg("could not cd to '%s': %s", dir
, strerror(errno
));
585 browser_chdir(absolute
);
590 if (chdir(home_dir
) == -1) {
591 error_msg("could not cd to '%s': %s", home_dir
, strerror(errno
));
593 browser_chdir(home_dir
);
598 static void cmd_bind(char *arg
)
600 int flag
= parse_flags((const char **)&arg
, "f");
609 key
= strchr(arg
, ' ');
616 func
= strchr(key
, ' ');
625 key_bind(arg
, key
, func
, flag
== 'f');
628 error_msg("expecting 3 arguments (context, key and function)\n");
631 static void cmd_unbind(char *arg
)
633 int flag
= parse_flags((const char **)&arg
, "f");
642 key
= strchr(arg
, ' ');
651 /* FIXME: remove spaces at end */
653 key_unbind(arg
, key
, flag
== 'f');
656 error_msg("expecting 2 arguments (context and key)\n");
659 static void cmd_showbind(char *arg
)
663 key
= strchr(arg
, ' ');
672 /* FIXME: remove spaces at end */
674 show_binding(arg
, key
);
677 error_msg("expecting 2 arguments (context and key)\n");
680 static void cmd_quit(char *arg
)
685 static void cmd_reshuffle(char *arg
)
693 static void cmd_source(char *arg
)
695 char *filename
= expand_filename(arg
);
697 if (source_file(filename
) == -1)
698 error_msg("sourcing %s: %s", filename
, strerror(errno
));
702 static void cmd_colorscheme(char *arg
)
706 snprintf(filename
, sizeof(filename
), "%s/%s.theme", cmus_config_dir
, arg
);
707 if (source_file(filename
) == -1) {
708 snprintf(filename
, sizeof(filename
), DATADIR
"/cmus/%s.theme", arg
);
709 if (source_file(filename
) == -1)
710 error_msg("sourcing %s: %s", filename
, strerror(errno
));
715 * \" inside double-quotes becomes "
716 * \\ inside double-quotes becomes \
718 static char *parse_quoted(const char **strp
)
720 const char *str
= *strp
;
739 ret
= xnew(char, str
- start
);
749 if (c
!= '"' && c
!= '\\')
757 error_msg("`\"' expected");
761 static char *parse_escaped(const char **strp
)
763 const char *str
= *strp
;
771 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
783 ret
= xnew(char, str
- start
+ 1);
789 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
807 static char *parse_one(const char **strp
)
809 const char *str
= *strp
;
819 part
= parse_quoted(&str
);
822 } else if (c
== '\'') {
823 /* backslashes are normal chars inside single-quotes */
827 end
= strchr(str
, '\'');
830 part
= xstrndup(str
, end
- str
);
833 part
= parse_escaped(&str
);
839 char *tmp
= xstrjoin(ret
, part
);
847 error_msg("`'' expected");
853 static char **parse_cmd(const char *cmd
, int *args_idx
, int *ac
)
862 /* there can't be spaces at start of command
863 * and there is at least one argument */
864 if (cmd
[0] == '{' && cmd
[1] == '}' && (cmd
[2] == ' ' || cmd
[2] == 0)) {
865 /* {} is replaced with file arguments */
867 goto only_once_please
;
872 arg
= parse_one(&cmd
);
878 alloc
= alloc
? alloc
* 2 : 4;
879 av
= xrenew(char *, av
, alloc
+ 1);
890 error_msg("{} can be used only once");
898 static struct track_info
**sel_tis
;
899 static int sel_tis_alloc
;
900 static int sel_tis_nr
;
902 static int add_ti(void *data
, struct track_info
*ti
)
904 if (sel_tis_nr
== sel_tis_alloc
) {
905 sel_tis_alloc
= sel_tis_alloc
? sel_tis_alloc
* 2 : 8;
906 sel_tis
= xrenew(struct track_info
*, sel_tis
, sel_tis_alloc
);
909 sel_tis
[sel_tis_nr
++] = ti
;
913 static void cmd_run(char *arg
)
916 int ac
, argc
, i
, run
, files_idx
= -1;
918 if (cur_view
> QUEUE_VIEW
) {
919 info_msg("Command execution is supported only in views 1-4");
923 av
= parse_cmd(arg
, &files_idx
, &ac
);
928 /* collect selected files (struct track_info) */
936 __tree_for_each_sel(add_ti
, NULL
, 0);
939 __editable_for_each_sel(&lib_editable
, add_ti
, NULL
, 0);
942 __editable_for_each_sel(&pl_editable
, add_ti
, NULL
, 0);
945 __editable_for_each_sel(&pq_editable
, add_ti
, NULL
, 0);
950 if (sel_tis_nr
== 0) {
951 /* no files selected, do nothing */
955 sel_tis
[sel_tis_nr
] = NULL
;
958 argv
= xnew(char *, ac
+ sel_tis_nr
+ 1);
960 if (files_idx
== -1) {
961 /* add selected files after rest of the args */
962 for (i
= 0; i
< ac
; i
++)
963 argv
[argc
++] = av
[i
];
964 for (i
= 0; i
< sel_tis_nr
; i
++)
965 argv
[argc
++] = sel_tis
[i
]->filename
;
967 for (i
= 0; i
< files_idx
; i
++)
968 argv
[argc
++] = av
[i
];
969 for (i
= 0; i
< sel_tis_nr
; i
++)
970 argv
[argc
++] = sel_tis
[i
]->filename
;
971 for (i
= files_idx
; i
< ac
; i
++)
972 argv
[argc
++] = av
[i
];
978 for (i
= 0; argv
[i
]; i
++)
979 d_print("ARG: '%s'\n", argv
[i
]);
982 if (confirm_run
&& (sel_tis_nr
> 1 || strcmp(argv
[0], "rm") == 0)) {
983 if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg
, sel_tis_nr
)) {
991 if (spawn(argv
, &status
)) {
992 error_msg("executing %s: %s", argv
[0], strerror(errno
));
994 if (WIFEXITED(status
)) {
995 int rc
= WEXITSTATUS(status
);
998 error_msg("%s returned %d", argv
[0], rc
);
1000 if (WIFSIGNALED(status
))
1001 error_msg("%s received signal %d", argv
[0], WTERMSIG(status
));
1006 /* this must be done before sel_tis are unreffed */
1007 free_str_array(argv
);
1009 /* remove non-existed files, update tags for changed files */
1010 cmus_update_tis(sel_tis
, sel_tis_nr
);
1012 /* we don't own sel_tis anymore! */
1017 free_str_array(argv
);
1018 for (i
= 0; sel_tis
[i
]; i
++)
1019 track_info_unref(sel_tis
[i
]);
1023 static int get_one_ti(void *data
, struct track_info
*ti
)
1025 struct track_info
**sel_ti
= data
;
1029 /* stop the for each loop, we need only the first selected track */
1033 static void cmd_echo(char *arg
)
1035 struct track_info
*sel_ti
;
1039 ptr
= strchr(ptr
, '{');
1048 info_msg("%s", arg
);
1052 if (cur_view
> QUEUE_VIEW
) {
1053 info_msg("echo with {} in its arguments is supported only in views 1-4");
1060 /* get only the first selected track */
1066 __tree_for_each_sel(get_one_ti
, &sel_ti
, 0);
1069 __editable_for_each_sel(&lib_editable
, get_one_ti
, &sel_ti
, 0);
1072 __editable_for_each_sel(&pl_editable
, get_one_ti
, &sel_ti
, 0);
1075 __editable_for_each_sel(&pq_editable
, get_one_ti
, &sel_ti
, 0);
1083 info_msg("%s%s%s", arg
, sel_ti
->filename
, ptr
);
1084 track_info_unref(sel_ti
);
1087 #define VF_RELATIVE 0x01
1088 #define VF_PERCENTAGE 0x02
1090 static int parse_vol_arg(const char *arg
, int *value
, unsigned int *flags
)
1093 int ch
, val
= 0, digits
= 0, sign
= 1;
1099 } else if (*arg
== '+') {
1106 if (ch
< '0' || ch
> '9')
1122 *value
= sign
* val
;
1129 static int calc_vol(int val
, int old
, unsigned int flags
)
1131 if (flags
& VF_RELATIVE
) {
1132 if (flags
& VF_PERCENTAGE
)
1133 val
= scale_from_percentage(val
, volume_max
);
1135 } else if (flags
& VF_PERCENTAGE
) {
1136 val
= scale_from_percentage(val
, volume_max
);
1138 return clamp(val
, 0, volume_max
);
1142 * :vol value [value]
1144 * where value is [-+]?[0-9]+%?
1146 static void cmd_vol(char *arg
)
1148 char **values
= get_words(arg
);
1149 unsigned int lf
, rf
;
1152 if (values
[1] && values
[2])
1155 if (parse_vol_arg(values
[0], &l
, &lf
))
1160 if (values
[1] && parse_vol_arg(values
[1], &r
, &rf
))
1163 free_str_array(values
);
1165 player_get_volume(&ol
, &or);
1166 l
= calc_vol(l
, ol
, lf
);
1167 r
= calc_vol(r
, or, rf
);
1168 player_set_volume(l
, r
);
1171 free_str_array(values
);
1172 error_msg("expecting 1 or 2 arguments (total or L and R volumes [+-]INTEGER[%%])\n");
1175 static void cmd_view(char *arg
)
1179 if (parse_enum(arg
, 1, NR_VIEWS
, view_names
, &view
))
1183 static void cmd_p_next(char *arg
)
1188 static void cmd_p_pause(char *arg
)
1193 static void cmd_p_play(char *arg
)
1196 player_play_file(arg
);
1202 static void cmd_p_prev(char *arg
)
1207 static void cmd_p_stop(char *arg
)
1212 static void cmd_search_next(char *arg
)
1215 if (!search_next(searchable
, search_str
, search_direction
))
1220 static void cmd_search_prev(char *arg
)
1223 if (!search_next(searchable
, search_str
, !search_direction
))
1228 static int sorted_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1230 return editable_for_each_sel(&lib_editable
, cb
, data
, reverse
);
1233 static int pl_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1235 return editable_for_each_sel(&pl_editable
, cb
, data
, reverse
);
1238 static int pq_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1240 return editable_for_each_sel(&pq_editable
, cb
, data
, reverse
);
1243 static for_each_sel_ti_cb view_for_each_sel
[4] = {
1245 sorted_for_each_sel
,
1250 /* wrapper for void lib_add_track(struct track_info *) etc. */
1251 static int wrapper_cb(void *data
, struct track_info
*ti
)
1253 add_ti_cb add
= data
;
1259 static void add_from_browser(add_ti_cb add
, int job_type
)
1261 char *sel
= browser_get_sel();
1267 ft
= cmus_detect_ft(sel
, &ret
);
1268 if (ft
!= FILE_TYPE_INVALID
) {
1269 cmus_add(add
, ret
, ft
, job_type
);
1270 window_down(browser_win
, 1);
1277 static void cmd_win_add_l(char *arg
)
1279 if (cur_view
== TREE_VIEW
|| cur_view
== SORTED_VIEW
)
1282 if (cur_view
<= QUEUE_VIEW
) {
1284 view_for_each_sel
[cur_view
](wrapper_cb
, lib_add_track
, 0);
1286 } else if (cur_view
== BROWSER_VIEW
) {
1287 add_from_browser(lib_add_track
, JOB_TYPE_LIB
);
1291 static void cmd_win_add_p(char *arg
)
1293 /* could allow adding dups? */
1294 if (cur_view
== PLAYLIST_VIEW
)
1297 if (cur_view
<= QUEUE_VIEW
) {
1299 view_for_each_sel
[cur_view
](wrapper_cb
, pl_add_track
, 0);
1301 } else if (cur_view
== BROWSER_VIEW
) {
1302 add_from_browser(pl_add_track
, JOB_TYPE_PL
);
1306 static void cmd_win_add_Q(char *arg
)
1308 if (cur_view
== QUEUE_VIEW
)
1311 if (cur_view
<= QUEUE_VIEW
) {
1313 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_prepend
, 1);
1315 } else if (cur_view
== BROWSER_VIEW
) {
1316 add_from_browser(play_queue_prepend
, JOB_TYPE_QUEUE
);
1320 static void cmd_win_add_q(char *arg
)
1322 if (cur_view
== QUEUE_VIEW
)
1325 if (cur_view
<= QUEUE_VIEW
) {
1327 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_append
, 0);
1329 } else if (cur_view
== BROWSER_VIEW
) {
1330 add_from_browser(play_queue_append
, JOB_TYPE_QUEUE
);
1334 static void cmd_win_activate(char *arg
)
1336 struct track_info
*info
= NULL
;
1341 info
= tree_set_selected();
1344 info
= sorted_set_selected();
1347 info
= pl_set_selected();
1361 /* update lib/pl mode */
1367 player_play_file(info
->filename
);
1368 track_info_unref(info
);
1372 static void cmd_win_mv_after(char *arg
)
1379 editable_move_after(&lib_editable
);
1382 editable_move_after(&pl_editable
);
1385 editable_move_after(&pq_editable
);
1395 static void cmd_win_mv_before(char *arg
)
1402 editable_move_before(&lib_editable
);
1405 editable_move_before(&pl_editable
);
1408 editable_move_before(&pq_editable
);
1418 static void cmd_win_remove(char *arg
)
1426 editable_remove_sel(&lib_editable
);
1429 editable_remove_sel(&pl_editable
);
1432 editable_remove_sel(&pq_editable
);
1438 filters_delete_filter();
1444 static void cmd_win_sel_cur(char *arg
)
1452 sorted_sel_current();
1467 static void cmd_win_toggle(char *arg
)
1472 tree_toggle_expand_artist();
1475 editable_toggle_mark(&lib_editable
);
1478 editable_toggle_mark(&pl_editable
);
1481 editable_toggle_mark(&pq_editable
);
1486 filters_toggle_filter();
1492 static struct window
*current_win(void)
1498 return lib_editable
.win
;
1500 return pl_editable
.win
;
1502 return pq_editable
.win
;
1511 static void cmd_win_bottom(char *arg
)
1514 window_goto_bottom(current_win());
1518 static void cmd_win_down(char *arg
)
1521 window_down(current_win(), 1);
1525 static void cmd_win_next(char *arg
)
1527 if (cur_view
== TREE_VIEW
) {
1529 tree_toggle_active_window();
1534 static void cmd_win_pg_down(char *arg
)
1537 window_page_down(current_win());
1541 static void cmd_win_pg_up(char *arg
)
1544 window_page_up(current_win());
1548 static void cmd_win_top(char *arg
)
1551 window_goto_top(current_win());
1555 static void cmd_win_up(char *arg
)
1558 window_up(current_win(), 1);
1562 static void cmd_win_update(char *arg
)
1575 static void cmd_browser_up(char *arg
)
1580 static void cmd_refresh(char *arg
)
1582 clearok(curscr
, TRUE
);
1588 * these functions fill tabexp struct, which is resetted beforehand
1591 /* buffer used for tab expansion */
1592 static char expbuf
[512];
1594 static int filter_directories(const char *name
, const struct stat
*s
)
1596 return S_ISDIR(s
->st_mode
);
1599 static int filter_any(const char *name
, const struct stat
*s
)
1604 static int filter_playable(const char *name
, const struct stat
*s
)
1606 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1609 static int filter_playlist(const char *name
, const struct stat
*s
)
1611 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1614 static int filter_supported(const char *name
, const struct stat
*s
)
1616 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1619 static void expand_files(const char *str
)
1621 expand_files_and_dirs(str
, filter_any
);
1624 static void expand_directories(const char *str
)
1626 expand_files_and_dirs(str
, filter_directories
);
1629 static void expand_playable(const char *str
)
1631 expand_files_and_dirs(str
, filter_playable
);
1634 static void expand_playlist(const char *str
)
1636 expand_files_and_dirs(str
, filter_playlist
);
1639 static void expand_supported(const char *str
)
1641 expand_files_and_dirs(str
, filter_supported
);
1644 static void expand_add(const char *str
)
1646 int flag
= parse_flags(&str
, "lpqQ");
1652 expand_supported(str
);
1654 if (tabexp
.head
&& flag
) {
1655 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1657 tabexp
.head
= xstrdup(expbuf
);
1661 static void expand_load_save(const char *str
)
1663 int flag
= parse_flags(&str
, "lp");
1669 expand_playlist(str
);
1671 if (tabexp
.head
&& flag
) {
1672 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1674 tabexp
.head
= xstrdup(expbuf
);
1678 static void expand_key_context(const char *str
, const char *force
)
1680 int pos
, i
, len
= strlen(str
);
1683 tails
= xnew(char *, NR_CTXS
+ 1);
1685 for (i
= 0; key_context_names
[i
]; i
++) {
1686 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1691 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1699 char *tmp
= xstrjoin(tails
[0], " ");
1704 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1705 tabexp
.head
= xstrdup(expbuf
);
1706 tabexp
.tails
= tails
;
1707 tabexp
.nr_tails
= pos
;
1710 static int get_context(const char *str
, int len
)
1712 int i
, c
= -1, count
= 0;
1714 for (i
= 0; key_context_names
[i
]; i
++) {
1715 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1716 if (key_context_names
[i
][len
] == 0) {
1729 static void expand_command_line(const char *str
);
1731 static void expand_bind_args(const char *str
)
1733 /* :bind context key function
1735 * possible values for str:
1740 * you need to know context before you can expand function
1742 /* start and end pointers for context, key and function */
1743 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1745 int i
, c
, k
, len
, pos
, alloc
, count
;
1746 int flag
= parse_flags((const char **)&str
, "f");
1747 const char *force
= "";
1758 ce
= strchr(cs
, ' ');
1760 expand_key_context(cs
, force
);
1764 /* context must be expandable */
1765 c
= get_context(cs
, ce
- cs
);
1767 /* context is ambiguous or invalid */
1774 ke
= strchr(ks
, ' ');
1781 for (i
= 0; key_table
[i
].name
; i
++) {
1782 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
1787 tails
= str_array_add(tails
, &alloc
, &pos
, xstrdup(key_table
[i
].name
+ len
));
1794 tmp
= xstrjoin(tails
[0], " ");
1799 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
1802 tabexp
.head
= xstrdup(expbuf
);
1803 tabexp
.tails
= tails
;
1804 tabexp
.nr_tails
= pos
;
1808 /* key must be expandable */
1811 for (i
= 0; key_table
[i
].name
; i
++) {
1812 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
1813 if (key_table
[i
].name
[ke
- ks
] == 0) {
1824 /* key is ambiguous or invalid */
1835 /* expand com [arg...] */
1836 expand_command_line(fs
);
1837 if (tabexp
.head
== NULL
) {
1838 /* command expand failed */
1843 * tabexp.head is now "com"
1844 * tabexp.tails is [ mand1 mand2 ... ]
1846 * need to change tabexp.head to "context key com"
1849 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
1850 key_table
[k
].name
, tabexp
.head
);
1852 tabexp
.head
= xstrdup(expbuf
);
1855 static void expand_unbind_args(const char *str
)
1857 /* :unbind context key */
1858 /* start and end pointers for context and key */
1859 const char *cs
, *ce
, *ks
;
1861 int c
, len
, pos
, alloc
;
1862 const struct binding
*b
;
1865 ce
= strchr(cs
, ' ');
1867 expand_key_context(cs
, "");
1871 /* context must be expandable */
1872 c
= get_context(cs
, ce
- cs
);
1874 /* context is ambiguous or invalid */
1887 b
= key_bindings
[c
];
1889 if (strncmp(ks
, b
->key
->name
, len
) == 0)
1890 tails
= str_array_add(tails
, &alloc
, &pos
, xstrdup(b
->key
->name
+ len
));
1896 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
1899 tabexp
.head
= xstrdup(expbuf
);
1900 tabexp
.tails
= tails
;
1901 tabexp
.nr_tails
= pos
;
1904 static void expand_factivate(const char *str
)
1906 /* "name1 name2 name3", expand only name3 */
1907 struct filter_entry
*e
;
1908 int str_len
, len
, i
, pos
, alloc
;
1912 str_len
= strlen(str
);
1915 if (str
[i
- 1] == ' ')
1925 list_for_each_entry(e
, &filters_head
, node
) {
1926 if (strncmp(name
, e
->name
, len
) == 0)
1927 tails
= str_array_add(tails
, &alloc
, &pos
, xstrdup(e
->name
+ len
));
1933 tabexp
.head
= xstrdup(str
);
1934 tabexp
.tails
= tails
;
1935 tabexp
.nr_tails
= pos
;
1938 static void expand_options(const char *str
)
1940 struct cmus_opt
*opt
;
1944 /* tabexp is resetted */
1946 if (len
> 1 && str
[len
- 1] == '=') {
1948 char *var
= xstrndup(str
, len
- 1);
1950 list_for_each_entry(opt
, &option_head
, node
) {
1951 if (strcmp(var
, opt
->name
) == 0) {
1952 char buf
[OPTION_MAX_SIZE
];
1954 tails
= xnew(char *, 1);
1957 opt
->get(opt
->id
, buf
);
1958 tails
[0] = xstrdup(buf
);
1961 tabexp
.head
= xstrdup(str
);
1962 tabexp
.tails
= tails
;
1963 tabexp
.nr_tails
= 1;
1970 /* expand variable */
1973 tails
= xnew(char *, nr_options
+ 1);
1975 list_for_each_entry(opt
, &option_head
, node
) {
1976 if (strncmp(str
, opt
->name
, len
) == 0)
1977 tails
[pos
++] = xstrdup(opt
->name
+ len
);
1981 /* only one variable matches, add '=' */
1982 char *tmp
= xstrjoin(tails
[0], "=");
1989 tabexp
.head
= xstrdup(str
);
1990 tabexp
.tails
= tails
;
1991 tabexp
.nr_tails
= pos
;
1998 static void expand_toptions(const char *str
)
2000 struct cmus_opt
*opt
;
2004 tails
= xnew(char *, nr_options
+ 1);
2007 list_for_each_entry(opt
, &option_head
, node
) {
2008 if (opt
->toggle
== NULL
)
2010 if (strncmp(str
, opt
->name
, len
) == 0)
2011 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2015 tabexp
.head
= xstrdup(str
);
2016 tabexp
.tails
= tails
;
2017 tabexp
.nr_tails
= pos
;
2023 static void load_themes(const char *dir
, const char *str
, int *allocp
)
2026 struct dirent
*dirent
;
2027 int len
= strlen(str
);
2033 while ((dirent
= readdir(d
)) != NULL
) {
2034 const char *dot
, *name
= dirent
->d_name
;
2038 if (strncmp(name
, str
, len
))
2041 dot
= strrchr(name
, '.');
2042 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2045 if (dot
- name
< len
) {
2047 * matches "foo.theme"
2048 * which also ends with ".theme"
2053 snprintf(filename
, sizeof(filename
), "%s/%s", dir
, name
);
2054 if (stat(filename
, &s
) || !S_ISREG(s
.st_mode
))
2057 tabexp
.tails
= str_array_add(tabexp
.tails
, allocp
, &tabexp
.nr_tails
,
2058 xstrndup(name
+ len
, dot
- name
- len
));
2063 static void expand_colorscheme(const char *str
)
2067 tabexp
.nr_tails
= 0;
2069 load_themes(cmus_config_dir
, str
, &alloc
);
2070 load_themes(DATADIR
"/cmus", str
, &alloc
);
2073 qsort(tabexp
.tails
, tabexp
.nr_tails
, sizeof(char *), strptrcmp
);
2075 tabexp
.tails
[tabexp
.nr_tails
] = NULL
;
2076 tabexp
.head
= xstrdup(str
);
2084 void (*func
)(char *arg
);
2086 /* min/max number of arguments */
2090 void (*expand
)(const char *str
);
2094 static struct command commands
[] = {
2095 { "add", cmd_add
, 1, 1, expand_add
},
2096 { "bind", cmd_bind
, 1, 1, expand_bind_args
},
2097 { "browser-up", cmd_browser_up
, 0, 0, NULL
},
2098 { "cd", cmd_cd
, 0, 1, expand_directories
},
2099 { "clear", cmd_clear
, 0, 1, NULL
},
2100 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
},
2101 { "echo", cmd_echo
, 1,-1, NULL
},
2102 { "factivate", cmd_factivate
, 0, 1, expand_factivate
},
2103 { "filter", cmd_filter
, 0, 1, NULL
},
2104 { "fset", cmd_fset
, 1, 1, NULL
},
2105 { "invert", cmd_invert
, 0, 0, NULL
},
2106 { "load", cmd_load
, 1, 1, expand_load_save
},
2107 { "mark", cmd_mark
, 0, 1, NULL
},
2108 { "player-next", cmd_p_next
, 0, 0, NULL
},
2109 { "player-pause", cmd_p_pause
, 0, 0, NULL
},
2110 { "player-play", cmd_p_play
, 0, 1, expand_playable
},
2111 { "player-prev", cmd_p_prev
, 0, 0, NULL
},
2112 { "player-stop", cmd_p_stop
, 0, 0, NULL
},
2113 { "quit", cmd_quit
, 0, 0, NULL
},
2114 { "refresh", cmd_refresh
, 0, 0, NULL
},
2115 { "run", cmd_run
, 1,-1, NULL
},
2116 { "save", cmd_save
, 0, 1, expand_load_save
},
2117 { "search-next", cmd_search_next
,0, 0, NULL
},
2118 { "search-prev", cmd_search_prev
,0, 0, NULL
},
2119 { "seek", cmd_seek
, 1, 1, NULL
},
2120 { "set", cmd_set
, 1, 1, expand_options
},
2121 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
},
2122 { "shuffle", cmd_reshuffle
, 0, 0, NULL
},
2123 { "source", cmd_source
, 1, 1, expand_files
},
2124 { "toggle", cmd_toggle
, 1, 1, expand_toptions
},
2125 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
},
2126 { "unmark", cmd_unmark
, 0, 0, NULL
},
2127 { "view", cmd_view
, 1, 1, NULL
},
2128 { "vol", cmd_vol
, 1, 2, NULL
},
2129 { "win-activate", cmd_win_activate
,0, 0, NULL
},
2130 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
},
2131 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
},
2132 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
},
2133 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
},
2134 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
},
2135 { "win-down", cmd_win_down
, 0, 0, NULL
},
2136 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
},
2137 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
},
2138 { "win-next", cmd_win_next
, 0, 0, NULL
},
2139 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
},
2140 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
},
2141 { "win-remove", cmd_win_remove
, 0, 0, NULL
},
2142 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
},
2143 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
},
2144 { "win-top", cmd_win_top
, 0, 0, NULL
},
2145 { "win-up", cmd_win_up
, 0, 0, NULL
},
2146 { "win-update", cmd_win_update
, 0, 0, NULL
},
2147 { NULL
, NULL
, 0, 0, 0 }
2150 /* fills tabexp struct */
2151 static void expand_commands(const char *str
)
2156 /* tabexp is resetted */
2157 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2160 for (i
= 0; commands
[i
].name
; i
++) {
2161 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2162 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2166 /* only one command matches, add ' ' */
2167 char *tmp
= xstrjoin(tails
[0], " ");
2173 tabexp
.head
= xstrdup(str
);
2174 tabexp
.tails
= tails
;
2175 tabexp
.nr_tails
= pos
;
2181 static const struct command
*get_command(const char *str
, int len
)
2185 for (i
= 0; commands
[i
].name
; i
++) {
2186 if (strncmp(str
, commands
[i
].name
, len
))
2189 if (commands
[i
].name
[len
] == 0) {
2191 return &commands
[i
];
2194 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2198 return &commands
[i
];
2203 /* fills tabexp struct */
2204 static void expand_command_line(const char *str
)
2206 /* :command [arg]...
2210 * str expanded value (tabexp.head)
2211 * -------------------------------------
2214 * se se (tabexp.tails = [ ek t ])
2216 /* command start/end, argument start */
2217 const char *cs
, *ce
, *as
;
2218 const struct command
*cmd
;
2221 ce
= strchr(cs
, ' ');
2223 /* expand command */
2224 expand_commands(cs
);
2228 /* command must be expandable */
2229 cmd
= get_command(cs
, ce
- cs
);
2231 /* command ambiguous or invalid */
2235 if (cmd
->expand
== NULL
) {
2236 /* can't expand argument */
2244 /* expand argument */
2246 if (tabexp
.head
== NULL
) {
2247 /* argument expansion failed */
2251 /* tabexp.head is now start of the argument string */
2252 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2254 tabexp
.head
= xstrdup(expbuf
);
2257 static void tab_expand(void)
2259 char *s1
, *s2
, *tmp
;
2262 /* strip white space */
2264 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2267 /* string to expand */
2268 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2271 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2273 tmp
= tabexp_expand(s1
, expand_command_line
);
2280 cmdline
.blen
= l1
+ l2
;
2281 if (cmdline
.blen
>= cmdline
.size
) {
2282 while (cmdline
.blen
>= cmdline
.size
)
2284 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2286 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2288 cmdline
.cpos
= u_strlen(tmp
);
2289 cmdline
.clen
= u_strlen(cmdline
.line
);
2296 static void reset_tab_expansion(void)
2299 arg_expand_cmd
= -1;
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 if (commands
[i
].name
== NULL
) {
2341 error_msg("unknown command\n");
2344 if (strncmp(cmd
, commands
[i
].name
, cmd_len
) == 0) {
2345 const char *next
= commands
[i
+ 1].name
;
2346 int exact
= commands
[i
].name
[cmd_len
] == 0;
2348 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2349 error_msg("ambiguous command\n");
2352 if (commands
[i
].min_args
> 0 && arg
== NULL
) {
2353 error_msg("not enough arguments\n");
2356 if (commands
[i
].max_args
== 0 && arg
) {
2357 error_msg("too many arguments\n");
2360 commands
[i
].func(arg
);
2369 static void reset_history_search(void)
2371 history_reset_search(&cmd_history
);
2372 free(history_search_text
);
2373 history_search_text
= NULL
;
2376 static void backspace(void)
2378 if (cmdline
.clen
> 0) {
2379 cmdline_backspace();
2381 input_mode
= NORMAL_MODE
;
2385 void command_mode_ch(uchar ch
)
2389 cmdline_move_home();
2392 cmdline_move_left();
2395 cmdline_delete_ch();
2401 cmdline_move_right();
2407 history_add_line(&cmd_history
, cmdline
.line
);
2410 input_mode
= NORMAL_MODE
;
2414 run_command(cmdline
.line
);
2415 history_add_line(&cmd_history
, cmdline
.line
);
2418 input_mode
= NORMAL_MODE
;
2421 cmdline_clear_end();
2424 /* tab expansion should not complain */
2430 cmdline_backspace_to_bol();
2436 cmdline_insert_ch(ch
);
2438 reset_history_search();
2440 reset_tab_expansion();
2443 void command_mode_key(int key
)
2445 reset_tab_expansion();
2448 cmdline_delete_ch();
2454 cmdline_move_left();
2457 cmdline_move_right();
2460 cmdline_move_home();
2469 if (history_search_text
== NULL
)
2470 history_search_text
= xstrdup(cmdline
.line
);
2471 s
= history_search_forward(&cmd_history
, history_search_text
);
2473 cmdline_set_text(s
);
2477 if (history_search_text
) {
2480 s
= history_search_backward(&cmd_history
, history_search_text
);
2482 cmdline_set_text(s
);
2484 cmdline_set_text(history_search_text
);
2489 d_print("key = %c (%d)\n", key
, key
);
2491 reset_history_search();
2494 void commands_init(void)
2496 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2497 history_load(&cmd_history
, cmd_history_filename
, 2000);
2500 void commands_exit(void)
2502 history_save(&cmd_history
);
2503 free(cmd_history_filename
);