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 cmus_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
);
1383 static void cmd_win_mv_after(char *arg
)
1390 editable_move_after(&lib_editable
);
1393 editable_move_after(&pl_editable
);
1396 editable_move_after(&pq_editable
);
1408 static void cmd_win_mv_before(char *arg
)
1415 editable_move_before(&lib_editable
);
1418 editable_move_before(&pl_editable
);
1421 editable_move_before(&pq_editable
);
1433 static void cmd_win_remove(char *arg
)
1441 editable_remove_sel(&lib_editable
);
1444 editable_remove_sel(&pl_editable
);
1447 editable_remove_sel(&pq_editable
);
1453 filters_delete_filter();
1462 static void cmd_win_sel_cur(char *arg
)
1470 sorted_sel_current();
1487 static void cmd_win_toggle(char *arg
)
1492 tree_toggle_expand_artist();
1497 editable_toggle_mark(&lib_editable
);
1502 editable_toggle_mark(&pl_editable
);
1507 editable_toggle_mark(&pq_editable
);
1513 filters_toggle_filter();
1521 static struct window
*current_win(void)
1527 return lib_editable
.win
;
1529 return pl_editable
.win
;
1531 return pq_editable
.win
;
1542 static void cmd_win_bottom(char *arg
)
1545 window_goto_bottom(current_win());
1549 static void cmd_win_down(char *arg
)
1552 window_down(current_win(), 1);
1556 static void cmd_win_next(char *arg
)
1558 if (cur_view
== TREE_VIEW
) {
1560 tree_toggle_active_window();
1565 static void cmd_win_pg_down(char *arg
)
1568 window_page_down(current_win());
1572 static void cmd_win_pg_up(char *arg
)
1575 window_page_up(current_win());
1579 static void cmd_win_top(char *arg
)
1582 window_goto_top(current_win());
1586 static void cmd_win_up(char *arg
)
1589 window_up(current_win(), 1);
1593 static void cmd_win_update(char *arg
)
1606 static void cmd_browser_up(char *arg
)
1611 static void cmd_refresh(char *arg
)
1613 clearok(curscr
, TRUE
);
1617 static int cmp_intp(const void *ap
, const void *bp
)
1624 static int *rand_array(int size
, int nmax
)
1626 int *r
= xnew(int, size
+ 1);
1630 if (count
> nmax
/ 2) {
1632 * Imagine that there are 1000 tracks in library and we want to
1633 * add 998 random tracks to queue. After we have added 997
1634 * random numbers to the array it would be quite hard to find a
1635 * random number that isn't already in the array (3/1000
1638 * So we invert the logic:
1640 * Find two (1000 - 998) random numbers in 0..999 range and put
1641 * them at end of the array. Sort the numbers and then fill
1642 * the array starting at index 0 with incrementing values that
1643 * are not in the set of random numbers.
1645 count
= nmax
- count
;
1646 offset
= size
- count
;
1649 for (i
= 0; i
< count
; ) {
1653 for (j
= 0; j
< i
; j
++) {
1654 if (r
[offset
+ j
] == v
)
1657 r
[offset
+ i
++] = v
;
1659 qsort(r
+ offset
, count
, sizeof(*r
), cmp_intp
);
1664 /* simplifies next loop */
1667 /* convert the indexes we don't want to those we want */
1681 static int count_albums(void)
1683 struct artist
*artist
;
1684 struct list_head
*item
;
1687 list_for_each_entry(artist
, &lib_artist_head
, node
) {
1688 list_for_each(item
, &artist
->album_head
)
1695 struct list_head node
;
1696 const struct album
*album
;
1699 static void cmd_lqueue(char *arg
)
1702 const struct list_head
*item
;
1703 const struct album
*album
;
1704 int count
= 1, nmax
, i
, pos
;
1710 if (str_to_int(arg
, &val
) || val
<= 0) {
1711 error_msg("argument must be positive integer");
1716 nmax
= count_albums();
1722 r
= rand_array(count
, nmax
);
1723 album
= to_album(to_artist(lib_artist_head
.next
)->album_head
.next
);
1725 for (i
= 0; i
< count
; i
++) {
1726 struct album_list
*a
;
1728 while (pos
< r
[i
]) {
1729 struct artist
*artist
= album
->artist
;
1730 if (album
->node
.next
== &artist
->album_head
) {
1731 artist
= to_artist(artist
->node
.next
);
1732 album
= to_album(artist
->album_head
.next
);
1734 album
= to_album(album
->node
.next
);
1738 a
= xnew(struct album_list
, 1);
1740 list_add_rand(&head
, &a
->node
, i
);
1746 struct list_head
*next
= item
->next
;
1747 struct album_list
*a
= container_of(item
, struct album_list
, node
);
1748 struct tree_track
*t
;
1750 list_for_each_entry(t
, &a
->album
->track_head
, node
)
1751 editable_add(&pq_editable
, simple_track_new(tree_track_info(t
)));
1754 } while (item
!= &head
);
1757 static void cmd_tqueue(char *arg
)
1760 struct list_head
*item
;
1761 int count
= 1, i
, pos
;
1767 if (str_to_int(arg
, &val
) || val
<= 0) {
1768 error_msg("argument must be positive integer");
1773 if (count
> lib_editable
.nr_tracks
)
1774 count
= lib_editable
.nr_tracks
;
1778 r
= rand_array(count
, lib_editable
.nr_tracks
);
1779 item
= lib_editable
.head
.next
;
1781 for (i
= 0; i
< count
; i
++) {
1782 struct simple_track
*t
;
1784 while (pos
< r
[i
]) {
1788 t
= simple_track_new(to_simple_track(item
)->info
);
1789 list_add_rand(&head
, &t
->node
, i
);
1795 struct list_head
*next
= item
->next
;
1796 struct simple_track
*t
= to_simple_track(item
);
1797 editable_add(&pq_editable
, t
);
1799 } while (item
!= &head
);
1804 * these functions fill tabexp struct, which is resetted beforehand
1807 /* buffer used for tab expansion */
1808 static char expbuf
[512];
1810 static int filter_directories(const char *name
, const struct stat
*s
)
1812 return S_ISDIR(s
->st_mode
);
1815 static int filter_any(const char *name
, const struct stat
*s
)
1820 static int filter_playable(const char *name
, const struct stat
*s
)
1822 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1825 static int filter_playlist(const char *name
, const struct stat
*s
)
1827 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1830 static int filter_supported(const char *name
, const struct stat
*s
)
1832 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1835 static void expand_files(const char *str
)
1837 expand_files_and_dirs(str
, filter_any
);
1840 static void expand_directories(const char *str
)
1842 expand_files_and_dirs(str
, filter_directories
);
1845 static void expand_playable(const char *str
)
1847 expand_files_and_dirs(str
, filter_playable
);
1850 static void expand_playlist(const char *str
)
1852 expand_files_and_dirs(str
, filter_playlist
);
1855 static void expand_supported(const char *str
)
1857 expand_files_and_dirs(str
, filter_supported
);
1860 static void expand_add(const char *str
)
1862 int flag
= parse_flags(&str
, "lpqQ");
1868 expand_supported(str
);
1870 if (tabexp
.head
&& flag
) {
1871 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1873 tabexp
.head
= xstrdup(expbuf
);
1877 static void expand_load_save(const char *str
)
1879 int flag
= parse_flags(&str
, "lp");
1885 expand_playlist(str
);
1887 if (tabexp
.head
&& flag
) {
1888 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1890 tabexp
.head
= xstrdup(expbuf
);
1894 static void expand_key_context(const char *str
, const char *force
)
1896 int pos
, i
, len
= strlen(str
);
1899 tails
= xnew(char *, NR_CTXS
+ 1);
1901 for (i
= 0; key_context_names
[i
]; i
++) {
1902 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1907 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1915 char *tmp
= xstrjoin(tails
[0], " ");
1920 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1921 tabexp
.head
= xstrdup(expbuf
);
1922 tabexp
.tails
= tails
;
1925 static int get_context(const char *str
, int len
)
1927 int i
, c
= -1, count
= 0;
1929 for (i
= 0; key_context_names
[i
]; i
++) {
1930 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1931 if (key_context_names
[i
][len
] == 0) {
1944 static void expand_command_line(const char *str
);
1946 static void expand_bind_args(const char *str
)
1948 /* :bind context key function
1950 * possible values for str:
1955 * you need to know context before you can expand function
1957 /* start and end pointers for context, key and function */
1958 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1960 int flag
= parse_flags((const char **)&str
, "f");
1961 const char *force
= "";
1972 ce
= strchr(cs
, ' ');
1974 expand_key_context(cs
, force
);
1978 /* context must be expandable */
1979 c
= get_context(cs
, ce
- cs
);
1981 /* context is ambiguous or invalid */
1988 ke
= strchr(ks
, ' ');
1991 int len
= strlen(ks
);
1994 for (i
= 0; key_table
[i
].name
; i
++) {
1995 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
2000 ptr_array_add(&array
, xstrdup(key_table
[i
].name
+ len
));
2006 if (array
.count
== 1) {
2007 char **ptrs
= array
.ptrs
;
2008 char *tmp
= xstrjoin(ptrs
[0], " ");
2013 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
2015 ptr_array_plug(&array
);
2016 tabexp
.head
= xstrdup(expbuf
);
2017 tabexp
.tails
= array
.ptrs
;
2021 /* key must be expandable */
2024 for (i
= 0; key_table
[i
].name
; i
++) {
2025 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
2026 if (key_table
[i
].name
[ke
- ks
] == 0) {
2037 /* key is ambiguous or invalid */
2048 /* expand com [arg...] */
2049 expand_command_line(fs
);
2050 if (tabexp
.head
== NULL
) {
2051 /* command expand failed */
2056 * tabexp.head is now "com"
2057 * tabexp.tails is [ mand1 mand2 ... ]
2059 * need to change tabexp.head to "context key com"
2062 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
2063 key_table
[k
].name
, tabexp
.head
);
2065 tabexp
.head
= xstrdup(expbuf
);
2068 static void expand_unbind_args(const char *str
)
2070 /* :unbind context key */
2071 /* start and end pointers for context and key */
2072 const char *cs
, *ce
, *ks
;
2073 const struct binding
*b
;
2078 ce
= strchr(cs
, ' ');
2080 expand_key_context(cs
, "");
2084 /* context must be expandable */
2085 c
= get_context(cs
, ce
- cs
);
2087 /* context is ambiguous or invalid */
2097 b
= key_bindings
[c
];
2099 if (!strncmp(ks
, b
->key
->name
, len
))
2100 ptr_array_add(&array
, xstrdup(b
->key
->name
+ len
));
2106 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
2108 ptr_array_plug(&array
);
2109 tabexp
.head
= xstrdup(expbuf
);
2110 tabexp
.tails
= array
.ptrs
;
2113 static void expand_factivate(const char *str
)
2115 /* "name1 name2 name3", expand only name3 */
2116 struct filter_entry
*e
;
2119 int str_len
, len
, i
;
2121 str_len
= strlen(str
);
2124 if (str
[i
- 1] == ' ')
2131 list_for_each_entry(e
, &filters_head
, node
) {
2132 if (!strncmp(name
, e
->name
, len
))
2133 ptr_array_add(&array
, xstrdup(e
->name
+ len
));
2138 ptr_array_plug(&array
);
2139 tabexp
.head
= xstrdup(str
);
2140 tabexp
.tails
= array
.ptrs
;
2143 static void expand_options(const char *str
)
2145 struct cmus_opt
*opt
;
2149 /* tabexp is resetted */
2151 if (len
> 1 && str
[len
- 1] == '=') {
2153 char *var
= xstrndup(str
, len
- 1);
2155 list_for_each_entry(opt
, &option_head
, node
) {
2156 if (strcmp(var
, opt
->name
) == 0) {
2157 char buf
[OPTION_MAX_SIZE
];
2159 tails
= xnew(char *, 2);
2162 opt
->get(opt
->id
, buf
);
2163 tails
[0] = xstrdup(buf
);
2166 tabexp
.head
= xstrdup(str
);
2167 tabexp
.tails
= tails
;
2174 /* expand variable */
2177 tails
= xnew(char *, nr_options
+ 1);
2179 list_for_each_entry(opt
, &option_head
, node
) {
2180 if (strncmp(str
, opt
->name
, len
) == 0)
2181 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2185 /* only one variable matches, add '=' */
2186 char *tmp
= xstrjoin(tails
[0], "=");
2193 tabexp
.head
= xstrdup(str
);
2194 tabexp
.tails
= tails
;
2201 static void expand_toptions(const char *str
)
2203 struct cmus_opt
*opt
;
2207 tails
= xnew(char *, nr_options
+ 1);
2210 list_for_each_entry(opt
, &option_head
, node
) {
2211 if (opt
->toggle
== NULL
)
2213 if (strncmp(str
, opt
->name
, len
) == 0)
2214 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2218 tabexp
.head
= xstrdup(str
);
2219 tabexp
.tails
= tails
;
2225 static void load_themes(const char *dirname
, const char *str
, struct ptr_array
*array
)
2227 struct directory dir
;
2228 const char *name
, *dot
;
2229 int len
= strlen(str
);
2231 if (dir_open(&dir
, dirname
))
2234 while ((name
= dir_read(&dir
))) {
2235 if (!S_ISREG(dir
.st
.st_mode
))
2237 if (strncmp(name
, str
, len
))
2239 dot
= strrchr(name
, '.');
2240 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2242 if (dot
- name
< len
)
2244 * matches "foo.theme"
2245 * which also ends with ".theme"
2248 ptr_array_add(array
, xstrndup(name
+ len
, dot
- name
- len
));
2253 static void expand_colorscheme(const char *str
)
2257 load_themes(cmus_config_dir
, str
, &array
);
2258 load_themes(DATADIR
"/cmus", str
, &array
);
2261 ptr_array_sort(&array
, strptrcmp
);
2263 ptr_array_plug(&array
);
2264 tabexp
.head
= xstrdup(str
);
2265 tabexp
.tails
= array
.ptrs
;
2272 struct command commands
[] = {
2273 { "add", cmd_add
, 1, 1, expand_add
, 0, 0 },
2274 { "bind", cmd_bind
, 1, 1, expand_bind_args
, 0, CMD_UNSAFE
},
2275 { "browser-up", cmd_browser_up
, 0, 0, NULL
, 0, 0 },
2276 { "cd", cmd_cd
, 0, 1, expand_directories
, 0, 0 },
2277 { "clear", cmd_clear
, 0, 1, NULL
, 0, 0 },
2278 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
, 0, 0 },
2279 { "echo", cmd_echo
, 1,-1, NULL
, 0, 0 },
2280 { "factivate", cmd_factivate
, 0, 1, expand_factivate
, 0, 0 },
2281 { "filter", cmd_filter
, 0, 1, NULL
, 0, 0 },
2282 { "fset", cmd_fset
, 1, 1, NULL
, 0, 0 },
2283 { "invert", cmd_invert
, 0, 0, NULL
, 0, 0 },
2284 { "load", cmd_load
, 1, 1, expand_load_save
, 0, 0 },
2285 { "lqueue", cmd_lqueue
, 0, 1, NULL
, 0, 0 },
2286 { "mark", cmd_mark
, 0, 1, NULL
, 0, 0 },
2287 { "player-next", cmd_p_next
, 0, 0, NULL
, 0, 0 },
2288 { "player-pause", cmd_p_pause
, 0, 0, NULL
, 0, 0 },
2289 { "player-play", cmd_p_play
, 0, 1, expand_playable
, 0, 0 },
2290 { "player-prev", cmd_p_prev
, 0, 0, NULL
, 0, 0 },
2291 { "player-stop", cmd_p_stop
, 0, 0, NULL
, 0, 0 },
2292 { "quit", cmd_quit
, 0, 0, NULL
, 0, 0 },
2293 { "refresh", cmd_refresh
, 0, 0, NULL
, 0, 0 },
2294 { "run", cmd_run
, 1,-1, NULL
, 0, CMD_UNSAFE
},
2295 { "save", cmd_save
, 0, 1, expand_load_save
, 0, CMD_UNSAFE
},
2296 { "search-next", cmd_search_next
,0, 0, NULL
, 0, 0 },
2297 { "search-prev", cmd_search_prev
,0, 0, NULL
, 0, 0 },
2298 { "seek", cmd_seek
, 1, 1, NULL
, 0, 0 },
2299 { "set", cmd_set
, 1, 1, expand_options
, 0, 0 },
2300 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
, 0, 0 },
2301 { "shuffle", cmd_reshuffle
, 0, 0, NULL
, 0, 0 },
2302 { "source", cmd_source
, 1, 1, expand_files
, 0, CMD_UNSAFE
},
2303 { "toggle", cmd_toggle
, 1, 1, expand_toptions
, 0, 0 },
2304 { "tqueue", cmd_tqueue
, 0, 1, NULL
, 0, 0 },
2305 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
, 0, 0 },
2306 { "unmark", cmd_unmark
, 0, 0, NULL
, 0, 0 },
2307 { "view", cmd_view
, 1, 1, NULL
, 0, 0 },
2308 { "vol", cmd_vol
, 1, 2, NULL
, 0, 0 },
2309 { "win-activate", cmd_win_activate
,0, 0, NULL
, 0, 0 },
2310 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
, 0, 0 },
2311 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
, 0, 0 },
2312 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
, 0, 0 },
2313 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
, 0, 0 },
2314 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
, 0, 0 },
2315 { "win-down", cmd_win_down
, 0, 0, NULL
, 0, 0 },
2316 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
, 0, 0 },
2317 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
, 0, 0 },
2318 { "win-next", cmd_win_next
, 0, 0, NULL
, 0, 0 },
2319 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
, 0, 0 },
2320 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
, 0, 0 },
2321 { "win-remove", cmd_win_remove
, 0, 0, NULL
, 0, CMD_UNSAFE
},
2322 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
, 0, 0 },
2323 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
, 0, 0 },
2324 { "win-top", cmd_win_top
, 0, 0, NULL
, 0, 0 },
2325 { "win-up", cmd_win_up
, 0, 0, NULL
, 0, 0 },
2326 { "win-update", cmd_win_update
, 0, 0, NULL
, 0, 0 },
2327 { NULL
, NULL
, 0, 0, 0, 0, 0 }
2330 /* fills tabexp struct */
2331 static void expand_commands(const char *str
)
2336 /* tabexp is resetted */
2337 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2340 for (i
= 0; commands
[i
].name
; i
++) {
2341 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2342 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2346 /* only one command matches, add ' ' */
2347 char *tmp
= xstrjoin(tails
[0], " ");
2353 tabexp
.head
= xstrdup(str
);
2354 tabexp
.tails
= tails
;
2360 struct command
*get_command(const char *str
)
2366 for (len
= 0; str
[len
] && str
[len
] != ' '; len
++)
2369 for (i
= 0; commands
[i
].name
; i
++) {
2370 if (strncmp(str
, commands
[i
].name
, len
))
2373 if (commands
[i
].name
[len
] == 0) {
2375 return &commands
[i
];
2378 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2382 return &commands
[i
];
2387 /* fills tabexp struct */
2388 static void expand_command_line(const char *str
)
2390 /* :command [arg]...
2394 * str expanded value (tabexp.head)
2395 * -------------------------------------
2398 * se se (tabexp.tails = [ ek t ])
2400 /* command start/end, argument start */
2401 const char *cs
, *ce
, *as
;
2402 const struct command
*cmd
;
2405 ce
= strchr(cs
, ' ');
2407 /* expand command */
2408 expand_commands(cs
);
2412 /* command must be expandable */
2413 cmd
= get_command(cs
);
2415 /* command ambiguous or invalid */
2419 if (cmd
->expand
== NULL
) {
2420 /* can't expand argument */
2428 /* expand argument */
2430 if (tabexp
.head
== NULL
) {
2431 /* argument expansion failed */
2435 /* tabexp.head is now start of the argument string */
2436 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2438 tabexp
.head
= xstrdup(expbuf
);
2441 static void tab_expand(void)
2443 char *s1
, *s2
, *tmp
;
2446 /* strip white space */
2448 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2451 /* string to expand */
2452 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2455 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2457 tmp
= tabexp_expand(s1
, expand_command_line
);
2464 cmdline
.blen
= l1
+ l2
;
2465 if (cmdline
.blen
>= cmdline
.size
) {
2466 while (cmdline
.blen
>= cmdline
.size
)
2468 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2470 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2472 cmdline
.cpos
= u_strlen(tmp
);
2473 cmdline
.clen
= u_strlen(cmdline
.line
);
2480 static void reset_tab_expansion(void)
2483 arg_expand_cmd
= -1;
2486 int run_only_safe_commands
;
2488 /* FIXME: parse all arguments */
2489 void run_command(const char *buf
)
2492 int cmd_start
, cmd_end
, cmd_len
;
2493 int arg_start
, arg_end
;
2497 while (buf
[i
] && buf
[i
] == ' ')
2504 while (buf
[i
] && buf
[i
] != ' ')
2507 while (buf
[i
] && buf
[i
] == ' ')
2514 cmd_len
= cmd_end
- cmd_start
;
2518 cmd
= xstrndup(buf
+ cmd_start
, cmd_len
);
2519 if (arg_start
== arg_end
) {
2522 arg
= xstrndup(buf
+ arg_start
, arg_end
- arg_start
);
2526 const struct command
*c
= &commands
[i
];
2528 if (c
->name
== NULL
) {
2529 error_msg("unknown command\n");
2532 if (strncmp(cmd
, c
->name
, cmd_len
) == 0) {
2533 const char *next
= commands
[i
+ 1].name
;
2534 int exact
= c
->name
[cmd_len
] == 0;
2536 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2537 error_msg("ambiguous command\n");
2540 if (c
->min_args
> 0 && arg
== NULL
) {
2541 error_msg("not enough arguments\n");
2544 if (c
->max_args
== 0 && arg
) {
2545 error_msg("too many arguments\n");
2548 if (run_only_safe_commands
&& c
->flags
& CMD_UNSAFE
) {
2549 d_print("trying to execute unsafe command over net\n");
2561 static void reset_history_search(void)
2563 history_reset_search(&cmd_history
);
2564 free(history_search_text
);
2565 history_search_text
= NULL
;
2568 static void backspace(void)
2570 if (cmdline
.clen
> 0) {
2571 cmdline_backspace();
2573 input_mode
= NORMAL_MODE
;
2577 void command_mode_ch(uchar ch
)
2581 cmdline_move_home();
2584 cmdline_move_left();
2587 cmdline_delete_ch();
2593 cmdline_move_right();
2599 history_add_line(&cmd_history
, cmdline
.line
);
2602 input_mode
= NORMAL_MODE
;
2606 run_command(cmdline
.line
);
2607 history_add_line(&cmd_history
, cmdline
.line
);
2610 input_mode
= NORMAL_MODE
;
2613 cmdline_clear_end();
2616 /* tab expansion should not complain */
2622 cmdline_backspace_to_bol();
2628 cmdline_insert_ch(ch
);
2630 reset_history_search();
2632 reset_tab_expansion();
2635 void command_mode_key(int key
)
2637 reset_tab_expansion();
2640 cmdline_delete_ch();
2646 cmdline_move_left();
2649 cmdline_move_right();
2652 cmdline_move_home();
2661 if (history_search_text
== NULL
)
2662 history_search_text
= xstrdup(cmdline
.line
);
2663 s
= history_search_forward(&cmd_history
, history_search_text
);
2665 cmdline_set_text(s
);
2669 if (history_search_text
) {
2672 s
= history_search_backward(&cmd_history
, history_search_text
);
2674 cmdline_set_text(s
);
2676 cmdline_set_text(history_search_text
);
2681 d_print("key = %c (%d)\n", key
, key
);
2683 reset_history_search();
2686 void commands_init(void)
2688 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2689 history_load(&cmd_history
, cmd_history_filename
, 2000);
2692 void commands_exit(void)
2694 history_save(&cmd_history
);
2695 free(cmd_history_filename
);