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;
68 static int prev_view
= -1;
70 static char *get_home_dir(const char *username
)
72 struct passwd
*passwd
;
75 return xstrdup(home_dir
);
76 passwd
= getpwnam(username
);
79 /* don't free passwd */
80 return xstrdup(passwd
->pw_dir
);
83 static char *expand_filename(const char *name
)
88 slash
= strchr(name
, '/');
90 char *username
, *home
;
92 if (slash
- name
- 1 > 0) {
94 username
= xstrndup(name
+ 1, slash
- name
- 1);
99 home
= get_home_dir(username
);
104 expanded
= xstrjoin(home
, slash
);
108 return xstrdup(name
);
112 return xstrdup(home_dir
);
116 home
= get_home_dir(name
+ 1);
119 return xstrdup(name
);
123 return xstrdup(name
);
129 void view_clear(int view
)
134 worker_remove_jobs(JOB_TYPE_LIB
);
136 editable_clear(&lib_editable
);
138 /* FIXME: make this optional? */
144 worker_remove_jobs(JOB_TYPE_PL
);
146 editable_clear(&pl_editable
);
150 worker_remove_jobs(JOB_TYPE_QUEUE
);
152 editable_clear(&pq_editable
);
156 info_msg(":clear only works in views 1-4");
160 void view_add(int view
, char *arg
, int prepend
)
165 tmp
= expand_filename(arg
);
166 ft
= cmus_detect_ft(tmp
, &name
);
167 if (ft
== FILE_TYPE_INVALID
) {
168 error_msg("adding '%s': %s", tmp
, strerror(errno
));
177 cmus_add(lib_add_track
, name
, ft
, JOB_TYPE_LIB
);
180 cmus_add(pl_add_track
, name
, ft
, JOB_TYPE_PL
);
184 cmus_add(play_queue_prepend
, name
, ft
, JOB_TYPE_QUEUE
);
186 cmus_add(play_queue_append
, name
, ft
, JOB_TYPE_QUEUE
);
190 info_msg(":add only works in views 1-4");
195 void view_load(int view
, char *arg
)
200 tmp
= expand_filename(arg
);
201 ft
= cmus_detect_ft(tmp
, &name
);
202 if (ft
== FILE_TYPE_INVALID
) {
203 error_msg("loading '%s': %s", tmp
, strerror(errno
));
209 if (ft
== FILE_TYPE_FILE
)
211 if (ft
!= FILE_TYPE_PL
) {
212 error_msg("loading '%s': not a playlist file", name
);
220 worker_remove_jobs(JOB_TYPE_LIB
);
222 editable_clear(&lib_editable
);
224 cmus_add(lib_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
229 worker_remove_jobs(JOB_TYPE_PL
);
231 editable_clear(&pl_editable
);
233 cmus_add(pl_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_PL
);
238 info_msg(":load only works in views 1-3");
243 static void do_save(for_each_ti_cb for_each_ti
, const char *arg
, char **filenamep
)
245 char *filename
= *filenamep
;
249 filename
= xstrdup(arg
);
250 *filenamep
= filename
;
254 if (cmus_save(for_each_ti
, filename
) == -1)
255 error_msg("saving '%s': %s", filename
, strerror(errno
));
259 void view_save(int view
, char *arg
)
264 tmp
= expand_filename(arg
);
265 arg
= path_absolute(tmp
);
272 do_save(lib_for_each
, arg
, &lib_filename
);
275 do_save(pl_for_each
, arg
, &pl_filename
);
278 info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
285 /* only returns the last flag which is enough for it's callers */
286 static int parse_flags(const char **strp
, const char *flags
)
288 const char *str
= *strp
;
303 if (str
[1] == '-' && (str
[2] == 0 || str
[2] == ' ')) {
309 if (str
[2] && str
[2] != ' ')
313 if (!strchr(flags
, flag
)) {
314 error_msg("invalid option -%c", flag
);
331 static int flag_to_view(int flag
)
337 return PLAYLIST_VIEW
;
346 static void cmd_add(char *arg
)
348 int flag
= parse_flags((const char **)&arg
, "lpqQ");
353 error_msg("not enough arguments\n");
356 view_add(flag_to_view(flag
), arg
, flag
== 'Q');
359 static void cmd_clear(char *arg
)
361 int flag
= parse_flags((const char **)&arg
, "lpq");
366 error_msg("too many arguments\n");
369 view_clear(flag_to_view(flag
));
372 static void cmd_load(char *arg
)
374 int flag
= parse_flags((const char **)&arg
, "lp");
379 error_msg("not enough arguments\n");
382 view_load(flag_to_view(flag
), arg
);
385 static void cmd_save(char *arg
)
387 int flag
= parse_flags((const char **)&arg
, "lp");
391 view_save(flag_to_view(flag
), arg
);
394 static void cmd_set(char *arg
)
399 for (i
= 0; arg
[i
]; i
++) {
407 option_set(arg
, value
);
408 help_win
->changed
= 1;
410 struct cmus_opt
*opt
;
411 char buf
[OPTION_MAX_SIZE
];
413 /* support "set <option>?" */
418 opt
= option_find(arg
);
420 opt
->get(opt
->id
, buf
);
421 info_msg("setting: '%s=%s'", arg
, buf
);
426 static void cmd_toggle(char *arg
)
428 struct cmus_opt
*opt
= option_find(arg
);
433 if (opt
->toggle
== NULL
) {
434 error_msg("%s is not toggle option", opt
->name
);
437 opt
->toggle(opt
->id
);
438 help_win
->changed
= 1;
441 static int get_number(char *str
, char **end
)
445 while (*str
>= '0' && *str
<= '9') {
453 static void cmd_seek(char *arg
)
456 int seek
= 0, sign
= 1, count
;
478 num
= get_number(arg
, &end
);
482 seek
= seek
* 60 + num
;
483 } while (++count
< 3);
490 switch (tolower(*arg
)) {
502 player_seek(seek
, relative
);
506 error_msg("expecting one argument: [+-]INTEGER[mh] or [+-]H:MM:SS");
509 static void cmd_factivate(char *arg
)
512 filters_activate_names(arg
);
516 static void cmd_filter(char *arg
)
519 filters_set_anonymous(arg
);
523 static void cmd_fset(char *arg
)
525 filters_set_filter(arg
);
528 static void cmd_invert(char *arg
)
533 editable_invert_marks(&lib_editable
);
536 editable_invert_marks(&pl_editable
);
539 editable_invert_marks(&pq_editable
);
542 info_msg(":invert only works in views 2-4");
547 static void cmd_mark(char *arg
)
552 editable_mark(&lib_editable
, arg
);
555 editable_mark(&pl_editable
, arg
);
558 editable_mark(&pq_editable
, arg
);
561 info_msg(":mark only works in views 2-4");
566 static void cmd_unmark(char *arg
)
571 editable_unmark(&lib_editable
);
574 editable_unmark(&pl_editable
);
577 editable_unmark(&pq_editable
);
580 info_msg(":unmark only works in views 2-4");
585 static void cmd_cd(char *arg
)
588 char *dir
, *absolute
;
590 dir
= expand_filename(arg
);
591 absolute
= path_absolute(dir
);
592 if (chdir(dir
) == -1) {
593 error_msg("could not cd to '%s': %s", dir
, strerror(errno
));
595 browser_chdir(absolute
);
600 if (chdir(home_dir
) == -1) {
601 error_msg("could not cd to '%s': %s", home_dir
, strerror(errno
));
603 browser_chdir(home_dir
);
608 static void cmd_bind(char *arg
)
610 int flag
= parse_flags((const char **)&arg
, "f");
619 key
= strchr(arg
, ' ');
626 func
= strchr(key
, ' ');
635 key_bind(arg
, key
, func
, flag
== 'f');
638 error_msg("expecting 3 arguments (context, key and function)\n");
641 static void cmd_unbind(char *arg
)
643 int flag
= parse_flags((const char **)&arg
, "f");
652 key
= strchr(arg
, ' ');
661 /* FIXME: remove spaces at end */
663 key_unbind(arg
, key
, flag
== 'f');
666 error_msg("expecting 2 arguments (context and key)\n");
669 static void cmd_showbind(char *arg
)
673 key
= strchr(arg
, ' ');
682 /* FIXME: remove spaces at end */
684 show_binding(arg
, key
);
687 error_msg("expecting 2 arguments (context and key)\n");
690 static void cmd_quit(char *arg
)
695 static void cmd_reshuffle(char *arg
)
703 static void cmd_source(char *arg
)
705 char *filename
= expand_filename(arg
);
707 if (source_file(filename
) == -1)
708 error_msg("sourcing %s: %s", filename
, strerror(errno
));
712 static void cmd_colorscheme(char *arg
)
716 snprintf(filename
, sizeof(filename
), "%s/%s.theme", cmus_config_dir
, arg
);
717 if (source_file(filename
) == -1) {
718 snprintf(filename
, sizeof(filename
), DATADIR
"/cmus/%s.theme", arg
);
719 if (source_file(filename
) == -1)
720 error_msg("sourcing %s: %s", filename
, strerror(errno
));
725 * \" inside double-quotes becomes "
726 * \\ inside double-quotes becomes \
728 static char *parse_quoted(const char **strp
)
730 const char *str
= *strp
;
749 ret
= xnew(char, str
- start
);
759 if (c
!= '"' && c
!= '\\')
767 error_msg("`\"' expected");
771 static char *parse_escaped(const char **strp
)
773 const char *str
= *strp
;
781 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
793 ret
= xnew(char, str
- start
+ 1);
799 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
817 static char *parse_one(const char **strp
)
819 const char *str
= *strp
;
829 part
= parse_quoted(&str
);
832 } else if (c
== '\'') {
833 /* backslashes are normal chars inside single-quotes */
837 end
= strchr(str
, '\'');
840 part
= xstrndup(str
, end
- str
);
843 part
= parse_escaped(&str
);
849 char *tmp
= xstrjoin(ret
, part
);
857 error_msg("`'' expected");
863 static char **parse_cmd(const char *cmd
, int *args_idx
, int *ac
)
872 /* there can't be spaces at start of command
873 * and there is at least one argument */
874 if (cmd
[0] == '{' && cmd
[1] == '}' && (cmd
[2] == ' ' || cmd
[2] == 0)) {
875 /* {} is replaced with file arguments */
877 goto only_once_please
;
882 arg
= parse_one(&cmd
);
888 alloc
= alloc
? alloc
* 2 : 4;
889 av
= xrenew(char *, av
, alloc
+ 1);
900 error_msg("{} can be used only once");
908 static struct track_info
**sel_tis
;
909 static int sel_tis_alloc
;
910 static int sel_tis_nr
;
912 static int add_ti(void *data
, struct track_info
*ti
)
914 if (sel_tis_nr
== sel_tis_alloc
) {
915 sel_tis_alloc
= sel_tis_alloc
? sel_tis_alloc
* 2 : 8;
916 sel_tis
= xrenew(struct track_info
*, sel_tis
, sel_tis_alloc
);
919 sel_tis
[sel_tis_nr
++] = ti
;
923 static void cmd_run(char *arg
)
926 int ac
, argc
, i
, run
, files_idx
= -1;
928 if (cur_view
> QUEUE_VIEW
) {
929 info_msg("Command execution is supported only in views 1-4");
933 av
= parse_cmd(arg
, &files_idx
, &ac
);
938 /* collect selected files (struct track_info) */
946 __tree_for_each_sel(add_ti
, NULL
, 0);
949 __editable_for_each_sel(&lib_editable
, add_ti
, NULL
, 0);
952 __editable_for_each_sel(&pl_editable
, add_ti
, NULL
, 0);
955 __editable_for_each_sel(&pq_editable
, add_ti
, NULL
, 0);
960 if (sel_tis_nr
== 0) {
961 /* no files selected, do nothing */
965 sel_tis
[sel_tis_nr
] = NULL
;
968 argv
= xnew(char *, ac
+ sel_tis_nr
+ 1);
970 if (files_idx
== -1) {
971 /* add selected files after rest of the args */
972 for (i
= 0; i
< ac
; i
++)
973 argv
[argc
++] = av
[i
];
974 for (i
= 0; i
< sel_tis_nr
; i
++)
975 argv
[argc
++] = sel_tis
[i
]->filename
;
977 for (i
= 0; i
< files_idx
; i
++)
978 argv
[argc
++] = av
[i
];
979 for (i
= 0; i
< sel_tis_nr
; i
++)
980 argv
[argc
++] = sel_tis
[i
]->filename
;
981 for (i
= files_idx
; i
< ac
; i
++)
982 argv
[argc
++] = av
[i
];
986 for (i
= 0; argv
[i
]; i
++)
987 d_print("ARG: '%s'\n", argv
[i
]);
990 if (confirm_run
&& (sel_tis_nr
> 1 || strcmp(argv
[0], "rm") == 0)) {
991 if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg
, sel_tis_nr
)) {
999 if (spawn(argv
, &status
)) {
1000 error_msg("executing %s: %s", argv
[0], strerror(errno
));
1002 if (WIFEXITED(status
)) {
1003 int rc
= WEXITSTATUS(status
);
1006 error_msg("%s returned %d", argv
[0], rc
);
1008 if (WIFSIGNALED(status
))
1009 error_msg("%s received signal %d", argv
[0], WTERMSIG(status
));
1014 /* this must be done before sel_tis are unreffed */
1018 /* remove non-existed files, update tags for changed files */
1019 cmus_update_tis(sel_tis
, sel_tis_nr
);
1021 /* we don't own sel_tis anymore! */
1028 for (i
= 0; sel_tis
[i
]; i
++)
1029 track_info_unref(sel_tis
[i
]);
1033 static int get_one_ti(void *data
, struct track_info
*ti
)
1035 struct track_info
**sel_ti
= data
;
1039 /* stop the for each loop, we need only the first selected track */
1043 static void cmd_echo(char *arg
)
1045 struct track_info
*sel_ti
;
1049 ptr
= strchr(ptr
, '{');
1058 info_msg("%s", arg
);
1062 if (cur_view
> QUEUE_VIEW
) {
1063 info_msg("echo with {} in its arguments is supported only in views 1-4");
1070 /* get only the first selected track */
1076 __tree_for_each_sel(get_one_ti
, &sel_ti
, 0);
1079 __editable_for_each_sel(&lib_editable
, get_one_ti
, &sel_ti
, 0);
1082 __editable_for_each_sel(&pl_editable
, get_one_ti
, &sel_ti
, 0);
1085 __editable_for_each_sel(&pq_editable
, get_one_ti
, &sel_ti
, 0);
1093 info_msg("%s%s%s", arg
, sel_ti
->filename
, ptr
);
1094 track_info_unref(sel_ti
);
1097 #define VF_RELATIVE 0x01
1098 #define VF_PERCENTAGE 0x02
1100 static int parse_vol_arg(const char *arg
, int *value
, unsigned int *flags
)
1103 int ch
, val
= 0, digits
= 0, sign
= 1;
1109 } else if (*arg
== '+') {
1116 if (ch
< '0' || ch
> '9')
1132 *value
= sign
* val
;
1139 static int calc_vol(int val
, int old
, unsigned int flags
)
1141 if (flags
& VF_RELATIVE
) {
1142 if (flags
& VF_PERCENTAGE
)
1143 val
= scale_from_percentage(val
, volume_max
);
1145 } else if (flags
& VF_PERCENTAGE
) {
1146 val
= scale_from_percentage(val
, volume_max
);
1148 return clamp(val
, 0, volume_max
);
1152 * :vol value [value]
1154 * where value is [-+]?[0-9]+%?
1156 static void cmd_vol(char *arg
)
1158 char **values
= get_words(arg
);
1159 unsigned int lf
, rf
;
1162 if (values
[1] && values
[2])
1165 if (parse_vol_arg(values
[0], &l
, &lf
))
1170 if (values
[1] && parse_vol_arg(values
[1], &r
, &rf
))
1173 free_str_array(values
);
1175 player_get_volume(&ol
, &or);
1176 l
= calc_vol(l
, ol
, lf
);
1177 r
= calc_vol(r
, or, rf
);
1178 player_set_volume(l
, r
);
1181 free_str_array(values
);
1182 error_msg("expecting 1 or 2 arguments (total or L and R volumes [+-]INTEGER[%%])\n");
1185 static void cmd_prev_view(char *arg
)
1188 if (prev_view
>= 0) {
1190 set_view(prev_view
);
1195 static void cmd_view(char *arg
)
1199 if (parse_enum(arg
, 1, NR_VIEWS
, view_names
, &view
) && (view
- 1) != cur_view
) {
1200 prev_view
= cur_view
;
1205 static void cmd_p_next(char *arg
)
1210 static void cmd_p_pause(char *arg
)
1215 static void cmd_p_play(char *arg
)
1218 cmus_play_file(arg
);
1224 static void cmd_p_prev(char *arg
)
1229 static void cmd_p_stop(char *arg
)
1234 static void cmd_search_next(char *arg
)
1237 if (!search_next(searchable
, search_str
, search_direction
))
1242 static void cmd_search_prev(char *arg
)
1245 if (!search_next(searchable
, search_str
, !search_direction
))
1250 static int sorted_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1252 return editable_for_each_sel(&lib_editable
, cb
, data
, reverse
);
1255 static int pl_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1257 return editable_for_each_sel(&pl_editable
, cb
, data
, reverse
);
1260 static int pq_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1262 return editable_for_each_sel(&pq_editable
, cb
, data
, reverse
);
1265 static for_each_sel_ti_cb view_for_each_sel
[4] = {
1267 sorted_for_each_sel
,
1272 /* wrapper for void lib_add_track(struct track_info *) etc. */
1273 static int wrapper_cb(void *data
, struct track_info
*ti
)
1275 add_ti_cb add
= data
;
1281 static void add_from_browser(add_ti_cb add
, int job_type
)
1283 char *sel
= browser_get_sel();
1289 ft
= cmus_detect_ft(sel
, &ret
);
1290 if (ft
!= FILE_TYPE_INVALID
) {
1291 cmus_add(add
, ret
, ft
, job_type
);
1292 window_down(browser_win
, 1);
1299 static void cmd_win_add_l(char *arg
)
1301 if (cur_view
== TREE_VIEW
|| cur_view
== SORTED_VIEW
)
1304 if (cur_view
<= QUEUE_VIEW
) {
1306 view_for_each_sel
[cur_view
](wrapper_cb
, lib_add_track
, 0);
1308 } else if (cur_view
== BROWSER_VIEW
) {
1309 add_from_browser(lib_add_track
, JOB_TYPE_LIB
);
1313 static void cmd_win_add_p(char *arg
)
1315 /* could allow adding dups? */
1316 if (cur_view
== PLAYLIST_VIEW
)
1319 if (cur_view
<= QUEUE_VIEW
) {
1321 view_for_each_sel
[cur_view
](wrapper_cb
, pl_add_track
, 0);
1323 } else if (cur_view
== BROWSER_VIEW
) {
1324 add_from_browser(pl_add_track
, JOB_TYPE_PL
);
1328 static void cmd_win_add_Q(char *arg
)
1330 if (cur_view
== QUEUE_VIEW
)
1333 if (cur_view
<= QUEUE_VIEW
) {
1335 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_prepend
, 1);
1337 } else if (cur_view
== BROWSER_VIEW
) {
1338 add_from_browser(play_queue_prepend
, JOB_TYPE_QUEUE
);
1342 static void cmd_win_add_q(char *arg
)
1344 if (cur_view
== QUEUE_VIEW
)
1347 if (cur_view
<= QUEUE_VIEW
) {
1349 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_append
, 0);
1351 } else if (cur_view
== BROWSER_VIEW
) {
1352 add_from_browser(play_queue_append
, JOB_TYPE_QUEUE
);
1356 static void cmd_win_activate(char *arg
)
1358 struct track_info
*info
= NULL
;
1363 info
= tree_set_selected();
1366 info
= sorted_set_selected();
1369 info
= pl_set_selected();
1386 /* update lib/pl mode */
1392 player_play_file(info
);
1396 static void cmd_win_mv_after(char *arg
)
1403 editable_move_after(&lib_editable
);
1406 editable_move_after(&pl_editable
);
1409 editable_move_after(&pq_editable
);
1421 static void cmd_win_mv_before(char *arg
)
1428 editable_move_before(&lib_editable
);
1431 editable_move_before(&pl_editable
);
1434 editable_move_before(&pq_editable
);
1446 static void cmd_win_remove(char *arg
)
1454 editable_remove_sel(&lib_editable
);
1457 editable_remove_sel(&pl_editable
);
1460 editable_remove_sel(&pq_editable
);
1466 filters_delete_filter();
1475 static void cmd_win_sel_cur(char *arg
)
1483 sorted_sel_current();
1500 static void cmd_win_toggle(char *arg
)
1505 tree_toggle_expand_artist();
1510 editable_toggle_mark(&lib_editable
);
1515 editable_toggle_mark(&pl_editable
);
1520 editable_toggle_mark(&pq_editable
);
1526 filters_toggle_filter();
1534 static struct window
*current_win(void)
1540 return lib_editable
.win
;
1542 return pl_editable
.win
;
1544 return pq_editable
.win
;
1555 static void cmd_win_bottom(char *arg
)
1558 window_goto_bottom(current_win());
1562 static void cmd_win_down(char *arg
)
1565 window_down(current_win(), 1);
1569 static void cmd_win_next(char *arg
)
1571 if (cur_view
== TREE_VIEW
) {
1573 tree_toggle_active_window();
1578 static void cmd_win_pg_down(char *arg
)
1581 window_page_down(current_win());
1585 static void cmd_win_pg_up(char *arg
)
1588 window_page_up(current_win());
1592 static void cmd_win_top(char *arg
)
1595 window_goto_top(current_win());
1599 static void cmd_win_up(char *arg
)
1602 window_up(current_win(), 1);
1606 static void cmd_win_update(char *arg
)
1619 static void cmd_browser_up(char *arg
)
1624 static void cmd_refresh(char *arg
)
1626 clearok(curscr
, TRUE
);
1630 static int cmp_intp(const void *ap
, const void *bp
)
1637 static int *rand_array(int size
, int nmax
)
1639 int *r
= xnew(int, size
+ 1);
1643 if (count
> nmax
/ 2) {
1645 * Imagine that there are 1000 tracks in library and we want to
1646 * add 998 random tracks to queue. After we have added 997
1647 * random numbers to the array it would be quite hard to find a
1648 * random number that isn't already in the array (3/1000
1651 * So we invert the logic:
1653 * Find two (1000 - 998) random numbers in 0..999 range and put
1654 * them at end of the array. Sort the numbers and then fill
1655 * the array starting at index 0 with incrementing values that
1656 * are not in the set of random numbers.
1658 count
= nmax
- count
;
1659 offset
= size
- count
;
1662 for (i
= 0; i
< count
; ) {
1666 for (j
= 0; j
< i
; j
++) {
1667 if (r
[offset
+ j
] == v
)
1670 r
[offset
+ i
++] = v
;
1672 qsort(r
+ offset
, count
, sizeof(*r
), cmp_intp
);
1677 /* simplifies next loop */
1680 /* convert the indexes we don't want to those we want */
1694 static int count_albums(void)
1696 struct artist
*artist
;
1697 struct list_head
*item
;
1700 list_for_each_entry(artist
, &lib_artist_head
, node
) {
1701 list_for_each(item
, &artist
->album_head
)
1708 struct list_head node
;
1709 const struct album
*album
;
1712 static void cmd_lqueue(char *arg
)
1715 const struct list_head
*item
;
1716 const struct album
*album
;
1717 int count
= 1, nmax
, i
, pos
;
1723 if (str_to_int(arg
, &val
) || val
<= 0) {
1724 error_msg("argument must be positive integer");
1730 nmax
= count_albums();
1736 r
= rand_array(count
, nmax
);
1737 album
= to_album(to_artist(lib_artist_head
.next
)->album_head
.next
);
1739 for (i
= 0; i
< count
; i
++) {
1740 struct album_list
*a
;
1742 while (pos
< r
[i
]) {
1743 struct artist
*artist
= album
->artist
;
1744 if (album
->node
.next
== &artist
->album_head
) {
1745 artist
= to_artist(artist
->node
.next
);
1746 album
= to_album(artist
->album_head
.next
);
1748 album
= to_album(album
->node
.next
);
1752 a
= xnew(struct album_list
, 1);
1754 list_add_rand(&head
, &a
->node
, i
);
1760 struct list_head
*next
= item
->next
;
1761 struct album_list
*a
= container_of(item
, struct album_list
, node
);
1762 struct tree_track
*t
;
1764 list_for_each_entry(t
, &a
->album
->track_head
, node
)
1765 editable_add(&pq_editable
, simple_track_new(tree_track_info(t
)));
1768 } while (item
!= &head
);
1773 static void cmd_tqueue(char *arg
)
1776 struct list_head
*item
;
1777 int count
= 1, i
, pos
;
1783 if (str_to_int(arg
, &val
) || val
<= 0) {
1784 error_msg("argument must be positive integer");
1790 if (count
> lib_editable
.nr_tracks
)
1791 count
= lib_editable
.nr_tracks
;
1795 r
= rand_array(count
, lib_editable
.nr_tracks
);
1796 item
= lib_editable
.head
.next
;
1798 for (i
= 0; i
< count
; i
++) {
1799 struct simple_track
*t
;
1801 while (pos
< r
[i
]) {
1805 t
= simple_track_new(to_simple_track(item
)->info
);
1806 list_add_rand(&head
, &t
->node
, i
);
1812 struct list_head
*next
= item
->next
;
1813 struct simple_track
*t
= to_simple_track(item
);
1814 editable_add(&pq_editable
, t
);
1816 } while (item
!= &head
);
1823 * these functions fill tabexp struct, which is resetted beforehand
1826 /* buffer used for tab expansion */
1827 static char expbuf
[512];
1829 static int filter_directories(const char *name
, const struct stat
*s
)
1831 return S_ISDIR(s
->st_mode
);
1834 static int filter_any(const char *name
, const struct stat
*s
)
1839 static int filter_playable(const char *name
, const struct stat
*s
)
1841 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1844 static int filter_playlist(const char *name
, const struct stat
*s
)
1846 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1849 static int filter_supported(const char *name
, const struct stat
*s
)
1851 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1854 static void expand_files(const char *str
)
1856 expand_files_and_dirs(str
, filter_any
);
1859 static void expand_directories(const char *str
)
1861 expand_files_and_dirs(str
, filter_directories
);
1864 static void expand_playable(const char *str
)
1866 expand_files_and_dirs(str
, filter_playable
);
1869 static void expand_playlist(const char *str
)
1871 expand_files_and_dirs(str
, filter_playlist
);
1874 static void expand_supported(const char *str
)
1876 expand_files_and_dirs(str
, filter_supported
);
1879 static void expand_add(const char *str
)
1881 int flag
= parse_flags(&str
, "lpqQ");
1887 expand_supported(str
);
1889 if (tabexp
.head
&& flag
) {
1890 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1892 tabexp
.head
= xstrdup(expbuf
);
1896 static void expand_load_save(const char *str
)
1898 int flag
= parse_flags(&str
, "lp");
1904 expand_playlist(str
);
1906 if (tabexp
.head
&& flag
) {
1907 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1909 tabexp
.head
= xstrdup(expbuf
);
1913 static void expand_key_context(const char *str
, const char *force
)
1915 int pos
, i
, len
= strlen(str
);
1918 tails
= xnew(char *, NR_CTXS
+ 1);
1920 for (i
= 0; key_context_names
[i
]; i
++) {
1921 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1926 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1934 char *tmp
= xstrjoin(tails
[0], " ");
1939 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1940 tabexp
.head
= xstrdup(expbuf
);
1941 tabexp
.tails
= tails
;
1944 static int get_context(const char *str
, int len
)
1946 int i
, c
= -1, count
= 0;
1948 for (i
= 0; key_context_names
[i
]; i
++) {
1949 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1950 if (key_context_names
[i
][len
] == 0) {
1963 static void expand_command_line(const char *str
);
1965 static void expand_bind_args(const char *str
)
1967 /* :bind context key function
1969 * possible values for str:
1974 * you need to know context before you can expand function
1976 /* start and end pointers for context, key and function */
1977 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1979 int flag
= parse_flags((const char **)&str
, "f");
1980 const char *force
= "";
1991 ce
= strchr(cs
, ' ');
1993 expand_key_context(cs
, force
);
1997 /* context must be expandable */
1998 c
= get_context(cs
, ce
- cs
);
2000 /* context is ambiguous or invalid */
2007 ke
= strchr(ks
, ' ');
2010 int len
= strlen(ks
);
2013 for (i
= 0; key_table
[i
].name
; i
++) {
2014 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
2019 ptr_array_add(&array
, xstrdup(key_table
[i
].name
+ len
));
2025 if (array
.count
== 1) {
2026 char **ptrs
= array
.ptrs
;
2027 char *tmp
= xstrjoin(ptrs
[0], " ");
2032 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
2034 ptr_array_plug(&array
);
2035 tabexp
.head
= xstrdup(expbuf
);
2036 tabexp
.tails
= array
.ptrs
;
2040 /* key must be expandable */
2043 for (i
= 0; key_table
[i
].name
; i
++) {
2044 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
2045 if (key_table
[i
].name
[ke
- ks
] == 0) {
2056 /* key is ambiguous or invalid */
2067 /* expand com [arg...] */
2068 expand_command_line(fs
);
2069 if (tabexp
.head
== NULL
) {
2070 /* command expand failed */
2075 * tabexp.head is now "com"
2076 * tabexp.tails is [ mand1 mand2 ... ]
2078 * need to change tabexp.head to "context key com"
2081 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
2082 key_table
[k
].name
, tabexp
.head
);
2084 tabexp
.head
= xstrdup(expbuf
);
2087 static void expand_unbind_args(const char *str
)
2089 /* :unbind context key */
2090 /* start and end pointers for context and key */
2091 const char *cs
, *ce
, *ks
;
2092 const struct binding
*b
;
2097 ce
= strchr(cs
, ' ');
2099 expand_key_context(cs
, "");
2103 /* context must be expandable */
2104 c
= get_context(cs
, ce
- cs
);
2106 /* context is ambiguous or invalid */
2116 b
= key_bindings
[c
];
2118 if (!strncmp(ks
, b
->key
->name
, len
))
2119 ptr_array_add(&array
, xstrdup(b
->key
->name
+ len
));
2125 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
2127 ptr_array_plug(&array
);
2128 tabexp
.head
= xstrdup(expbuf
);
2129 tabexp
.tails
= array
.ptrs
;
2132 static void expand_factivate(const char *str
)
2134 /* "name1 name2 name3", expand only name3 */
2135 struct filter_entry
*e
;
2138 int str_len
, len
, i
;
2140 str_len
= strlen(str
);
2143 if (str
[i
- 1] == ' ')
2150 list_for_each_entry(e
, &filters_head
, node
) {
2151 if (!strncmp(name
, e
->name
, len
))
2152 ptr_array_add(&array
, xstrdup(e
->name
+ len
));
2157 ptr_array_plug(&array
);
2158 tabexp
.head
= xstrdup(str
);
2159 tabexp
.tails
= array
.ptrs
;
2162 static void expand_options(const char *str
)
2164 struct cmus_opt
*opt
;
2168 /* tabexp is resetted */
2170 if (len
> 1 && str
[len
- 1] == '=') {
2172 char *var
= xstrndup(str
, len
- 1);
2174 list_for_each_entry(opt
, &option_head
, node
) {
2175 if (strcmp(var
, opt
->name
) == 0) {
2176 char buf
[OPTION_MAX_SIZE
];
2178 tails
= xnew(char *, 2);
2181 opt
->get(opt
->id
, buf
);
2182 tails
[0] = xstrdup(buf
);
2185 tabexp
.head
= xstrdup(str
);
2186 tabexp
.tails
= tails
;
2193 /* expand variable */
2196 tails
= xnew(char *, nr_options
+ 1);
2198 list_for_each_entry(opt
, &option_head
, node
) {
2199 if (strncmp(str
, opt
->name
, len
) == 0)
2200 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2204 /* only one variable matches, add '=' */
2205 char *tmp
= xstrjoin(tails
[0], "=");
2212 tabexp
.head
= xstrdup(str
);
2213 tabexp
.tails
= tails
;
2220 static void expand_toptions(const char *str
)
2222 struct cmus_opt
*opt
;
2226 tails
= xnew(char *, nr_options
+ 1);
2229 list_for_each_entry(opt
, &option_head
, node
) {
2230 if (opt
->toggle
== NULL
)
2232 if (strncmp(str
, opt
->name
, len
) == 0)
2233 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2237 tabexp
.head
= xstrdup(str
);
2238 tabexp
.tails
= tails
;
2244 static void load_themes(const char *dirname
, const char *str
, struct ptr_array
*array
)
2246 struct directory dir
;
2247 const char *name
, *dot
;
2248 int len
= strlen(str
);
2250 if (dir_open(&dir
, dirname
))
2253 while ((name
= dir_read(&dir
))) {
2254 if (!S_ISREG(dir
.st
.st_mode
))
2256 if (strncmp(name
, str
, len
))
2258 dot
= strrchr(name
, '.');
2259 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2261 if (dot
- name
< len
)
2263 * matches "foo.theme"
2264 * which also ends with ".theme"
2267 ptr_array_add(array
, xstrndup(name
+ len
, dot
- name
- len
));
2272 static void expand_colorscheme(const char *str
)
2276 load_themes(cmus_config_dir
, str
, &array
);
2277 load_themes(DATADIR
"/cmus", str
, &array
);
2280 ptr_array_sort(&array
, strptrcmp
);
2282 ptr_array_plug(&array
);
2283 tabexp
.head
= xstrdup(str
);
2284 tabexp
.tails
= array
.ptrs
;
2291 struct command commands
[] = {
2292 { "add", cmd_add
, 1, 1, expand_add
, 0, 0 },
2293 { "bind", cmd_bind
, 1, 1, expand_bind_args
, 0, CMD_UNSAFE
},
2294 { "browser-up", cmd_browser_up
, 0, 0, NULL
, 0, 0 },
2295 { "cd", cmd_cd
, 0, 1, expand_directories
, 0, 0 },
2296 { "clear", cmd_clear
, 0, 1, NULL
, 0, 0 },
2297 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
, 0, 0 },
2298 { "echo", cmd_echo
, 1,-1, NULL
, 0, 0 },
2299 { "factivate", cmd_factivate
, 0, 1, expand_factivate
, 0, 0 },
2300 { "filter", cmd_filter
, 0, 1, NULL
, 0, 0 },
2301 { "fset", cmd_fset
, 1, 1, NULL
, 0, 0 },
2302 { "invert", cmd_invert
, 0, 0, NULL
, 0, 0 },
2303 { "load", cmd_load
, 1, 1, expand_load_save
, 0, 0 },
2304 { "lqueue", cmd_lqueue
, 0, 1, NULL
, 0, 0 },
2305 { "mark", cmd_mark
, 0, 1, NULL
, 0, 0 },
2306 { "player-next", cmd_p_next
, 0, 0, NULL
, 0, 0 },
2307 { "player-pause", cmd_p_pause
, 0, 0, NULL
, 0, 0 },
2308 { "player-play", cmd_p_play
, 0, 1, expand_playable
, 0, 0 },
2309 { "player-prev", cmd_p_prev
, 0, 0, NULL
, 0, 0 },
2310 { "player-stop", cmd_p_stop
, 0, 0, NULL
, 0, 0 },
2311 { "prev-view", cmd_prev_view
, 0, 0, NULL
, 0, 0 },
2312 { "quit", cmd_quit
, 0, 0, NULL
, 0, 0 },
2313 { "refresh", cmd_refresh
, 0, 0, NULL
, 0, 0 },
2314 { "run", cmd_run
, 1,-1, NULL
, 0, CMD_UNSAFE
},
2315 { "save", cmd_save
, 0, 1, expand_load_save
, 0, CMD_UNSAFE
},
2316 { "search-next", cmd_search_next
,0, 0, NULL
, 0, 0 },
2317 { "search-prev", cmd_search_prev
,0, 0, NULL
, 0, 0 },
2318 { "seek", cmd_seek
, 1, 1, NULL
, 0, 0 },
2319 { "set", cmd_set
, 1, 1, expand_options
, 0, 0 },
2320 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
, 0, 0 },
2321 { "shuffle", cmd_reshuffle
, 0, 0, NULL
, 0, 0 },
2322 { "source", cmd_source
, 1, 1, expand_files
, 0, CMD_UNSAFE
},
2323 { "toggle", cmd_toggle
, 1, 1, expand_toptions
, 0, 0 },
2324 { "tqueue", cmd_tqueue
, 0, 1, NULL
, 0, 0 },
2325 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
, 0, 0 },
2326 { "unmark", cmd_unmark
, 0, 0, NULL
, 0, 0 },
2327 { "view", cmd_view
, 1, 1, NULL
, 0, 0 },
2328 { "vol", cmd_vol
, 1, 2, NULL
, 0, 0 },
2329 { "win-activate", cmd_win_activate
,0, 0, NULL
, 0, 0 },
2330 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
, 0, 0 },
2331 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
, 0, 0 },
2332 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
, 0, 0 },
2333 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
, 0, 0 },
2334 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
, 0, 0 },
2335 { "win-down", cmd_win_down
, 0, 0, NULL
, 0, 0 },
2336 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
, 0, 0 },
2337 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
, 0, 0 },
2338 { "win-next", cmd_win_next
, 0, 0, NULL
, 0, 0 },
2339 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
, 0, 0 },
2340 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
, 0, 0 },
2341 { "win-remove", cmd_win_remove
, 0, 0, NULL
, 0, CMD_UNSAFE
},
2342 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
, 0, 0 },
2343 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
, 0, 0 },
2344 { "win-top", cmd_win_top
, 0, 0, NULL
, 0, 0 },
2345 { "win-up", cmd_win_up
, 0, 0, NULL
, 0, 0 },
2346 { "win-update", cmd_win_update
, 0, 0, NULL
, 0, 0 },
2347 { NULL
, NULL
, 0, 0, 0, 0, 0 }
2350 /* fills tabexp struct */
2351 static void expand_commands(const char *str
)
2356 /* tabexp is resetted */
2357 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2360 for (i
= 0; commands
[i
].name
; i
++) {
2361 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2362 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2366 /* only one command matches, add ' ' */
2367 char *tmp
= xstrjoin(tails
[0], " ");
2373 tabexp
.head
= xstrdup(str
);
2374 tabexp
.tails
= tails
;
2380 struct command
*get_command(const char *str
)
2386 for (len
= 0; str
[len
] && str
[len
] != ' '; len
++)
2389 for (i
= 0; commands
[i
].name
; i
++) {
2390 if (strncmp(str
, commands
[i
].name
, len
))
2393 if (commands
[i
].name
[len
] == 0) {
2395 return &commands
[i
];
2398 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2402 return &commands
[i
];
2407 /* fills tabexp struct */
2408 static void expand_command_line(const char *str
)
2410 /* :command [arg]...
2414 * str expanded value (tabexp.head)
2415 * -------------------------------------
2418 * se se (tabexp.tails = [ ek t ])
2420 /* command start/end, argument start */
2421 const char *cs
, *ce
, *as
;
2422 const struct command
*cmd
;
2425 ce
= strchr(cs
, ' ');
2427 /* expand command */
2428 expand_commands(cs
);
2432 /* command must be expandable */
2433 cmd
= get_command(cs
);
2435 /* command ambiguous or invalid */
2439 if (cmd
->expand
== NULL
) {
2440 /* can't expand argument */
2448 /* expand argument */
2450 if (tabexp
.head
== NULL
) {
2451 /* argument expansion failed */
2455 /* tabexp.head is now start of the argument string */
2456 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2458 tabexp
.head
= xstrdup(expbuf
);
2461 static void tab_expand(void)
2463 char *s1
, *s2
, *tmp
;
2466 /* strip white space */
2468 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2471 /* string to expand */
2472 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2475 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2477 tmp
= tabexp_expand(s1
, expand_command_line
);
2484 cmdline
.blen
= l1
+ l2
;
2485 if (cmdline
.blen
>= cmdline
.size
) {
2486 while (cmdline
.blen
>= cmdline
.size
)
2488 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2490 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2492 cmdline
.cpos
= u_strlen(tmp
);
2493 cmdline
.clen
= u_strlen(cmdline
.line
);
2500 static void reset_tab_expansion(void)
2503 arg_expand_cmd
= -1;
2506 int run_only_safe_commands
;
2508 /* FIXME: parse all arguments */
2509 void run_command(const char *buf
)
2512 int cmd_start
, cmd_end
, cmd_len
;
2513 int arg_start
, arg_end
;
2517 while (buf
[i
] && buf
[i
] == ' ')
2524 while (buf
[i
] && buf
[i
] != ' ')
2527 while (buf
[i
] && buf
[i
] == ' ')
2534 cmd_len
= cmd_end
- cmd_start
;
2538 cmd
= xstrndup(buf
+ cmd_start
, cmd_len
);
2539 if (arg_start
== arg_end
) {
2542 arg
= xstrndup(buf
+ arg_start
, arg_end
- arg_start
);
2546 const struct command
*c
= &commands
[i
];
2548 if (c
->name
== NULL
) {
2549 error_msg("unknown command\n");
2552 if (strncmp(cmd
, c
->name
, cmd_len
) == 0) {
2553 const char *next
= commands
[i
+ 1].name
;
2554 int exact
= c
->name
[cmd_len
] == 0;
2556 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2557 error_msg("ambiguous command\n");
2560 if (c
->min_args
> 0 && arg
== NULL
) {
2561 error_msg("not enough arguments\n");
2564 if (c
->max_args
== 0 && arg
) {
2565 error_msg("too many arguments\n");
2568 if (run_only_safe_commands
&& c
->flags
& CMD_UNSAFE
) {
2569 d_print("trying to execute unsafe command over net\n");
2581 static void reset_history_search(void)
2583 history_reset_search(&cmd_history
);
2584 free(history_search_text
);
2585 history_search_text
= NULL
;
2588 static void backspace(void)
2590 if (cmdline
.clen
> 0) {
2591 cmdline_backspace();
2593 input_mode
= NORMAL_MODE
;
2597 void command_mode_ch(uchar ch
)
2601 cmdline_move_home();
2604 cmdline_move_left();
2607 cmdline_delete_ch();
2613 cmdline_move_right();
2619 history_add_line(&cmd_history
, cmdline
.line
);
2622 input_mode
= NORMAL_MODE
;
2626 run_command(cmdline
.line
);
2627 history_add_line(&cmd_history
, cmdline
.line
);
2630 input_mode
= NORMAL_MODE
;
2633 cmdline_clear_end();
2639 cmdline_backspace_to_bol();
2645 cmdline_insert_ch(ch
);
2647 reset_history_search();
2649 reset_tab_expansion();
2652 void command_mode_key(int key
)
2654 reset_tab_expansion();
2657 cmdline_delete_ch();
2663 cmdline_move_left();
2666 cmdline_move_right();
2669 cmdline_move_home();
2678 if (history_search_text
== NULL
)
2679 history_search_text
= xstrdup(cmdline
.line
);
2680 s
= history_search_forward(&cmd_history
, history_search_text
);
2682 cmdline_set_text(s
);
2686 if (history_search_text
) {
2689 s
= history_search_backward(&cmd_history
, history_search_text
);
2691 cmdline_set_text(s
);
2693 cmdline_set_text(history_search_text
);
2698 d_print("key = %c (%d)\n", key
, key
);
2700 reset_history_search();
2703 void commands_init(void)
2705 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2706 history_load(&cmd_history
, cmd_history_filename
, 2000);
2709 void commands_exit(void)
2711 history_save(&cmd_history
);
2712 free(cmd_history_filename
);