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 if (worker_has_job(JOB_TYPE_LIB
))
274 do_save(lib_for_each
, arg
, &lib_filename
);
277 if (worker_has_job(JOB_TYPE_PL
))
279 do_save(pl_for_each
, arg
, &pl_filename
);
282 info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
287 error_msg("can't save when tracks are being added");
293 /* only returns the last flag which is enough for it's callers */
294 static int parse_flags(const char **strp
, const char *flags
)
296 const char *str
= *strp
;
311 if (str
[1] == '-' && (str
[2] == 0 || str
[2] == ' ')) {
317 if (str
[2] && str
[2] != ' ')
321 if (!strchr(flags
, flag
)) {
322 error_msg("invalid option -%c", flag
);
339 static int flag_to_view(int flag
)
345 return PLAYLIST_VIEW
;
354 static void cmd_add(char *arg
)
356 int flag
= parse_flags((const char **)&arg
, "lpqQ");
361 error_msg("not enough arguments\n");
364 view_add(flag_to_view(flag
), arg
, flag
== 'Q');
367 static void cmd_clear(char *arg
)
369 int flag
= parse_flags((const char **)&arg
, "lpq");
374 error_msg("too many arguments\n");
377 view_clear(flag_to_view(flag
));
380 static void cmd_load(char *arg
)
382 int flag
= parse_flags((const char **)&arg
, "lp");
387 error_msg("not enough arguments\n");
390 view_load(flag_to_view(flag
), arg
);
393 static void cmd_save(char *arg
)
395 int flag
= parse_flags((const char **)&arg
, "lp");
399 view_save(flag_to_view(flag
), arg
);
402 static void cmd_set(char *arg
)
407 for (i
= 0; arg
[i
]; i
++) {
415 option_set(arg
, value
);
416 help_win
->changed
= 1;
418 struct cmus_opt
*opt
;
419 char buf
[OPTION_MAX_SIZE
];
421 /* support "set <option>?" */
426 opt
= option_find(arg
);
428 opt
->get(opt
->id
, buf
);
429 info_msg("setting: '%s=%s'", arg
, buf
);
434 static void cmd_toggle(char *arg
)
436 struct cmus_opt
*opt
= option_find(arg
);
441 if (opt
->toggle
== NULL
) {
442 error_msg("%s is not toggle option", opt
->name
);
445 opt
->toggle(opt
->id
);
446 help_win
->changed
= 1;
449 static int get_number(char *str
, char **end
)
453 while (*str
>= '0' && *str
<= '9') {
461 static void cmd_seek(char *arg
)
464 int seek
= 0, sign
= 1, count
;
486 num
= get_number(arg
, &end
);
490 seek
= seek
* 60 + num
;
491 } while (++count
< 3);
498 switch (tolower(*arg
)) {
510 player_seek(seek
, relative
);
514 error_msg("expecting one argument: [+-]INTEGER[mh] or [+-]H:MM:SS");
517 static void cmd_factivate(char *arg
)
520 filters_activate_names(arg
);
524 static void cmd_filter(char *arg
)
527 filters_set_anonymous(arg
);
531 static void cmd_fset(char *arg
)
533 filters_set_filter(arg
);
536 static void cmd_invert(char *arg
)
541 editable_invert_marks(&lib_editable
);
544 editable_invert_marks(&pl_editable
);
547 editable_invert_marks(&pq_editable
);
550 info_msg(":invert only works in views 2-4");
555 static void cmd_mark(char *arg
)
560 editable_mark(&lib_editable
, arg
);
563 editable_mark(&pl_editable
, arg
);
566 editable_mark(&pq_editable
, arg
);
569 info_msg(":mark only works in views 2-4");
574 static void cmd_unmark(char *arg
)
579 editable_unmark(&lib_editable
);
582 editable_unmark(&pl_editable
);
585 editable_unmark(&pq_editable
);
588 info_msg(":unmark only works in views 2-4");
593 static void cmd_cd(char *arg
)
596 char *dir
, *absolute
;
598 dir
= expand_filename(arg
);
599 absolute
= path_absolute(dir
);
600 if (chdir(dir
) == -1) {
601 error_msg("could not cd to '%s': %s", dir
, strerror(errno
));
603 browser_chdir(absolute
);
608 if (chdir(home_dir
) == -1) {
609 error_msg("could not cd to '%s': %s", home_dir
, strerror(errno
));
611 browser_chdir(home_dir
);
616 static void cmd_bind(char *arg
)
618 int flag
= parse_flags((const char **)&arg
, "f");
627 key
= strchr(arg
, ' ');
634 func
= strchr(key
, ' ');
643 key_bind(arg
, key
, func
, flag
== 'f');
646 error_msg("expecting 3 arguments (context, key and function)\n");
649 static void cmd_unbind(char *arg
)
651 int flag
= parse_flags((const char **)&arg
, "f");
660 key
= strchr(arg
, ' ');
669 /* FIXME: remove spaces at end */
671 key_unbind(arg
, key
, flag
== 'f');
674 error_msg("expecting 2 arguments (context and key)\n");
677 static void cmd_showbind(char *arg
)
681 key
= strchr(arg
, ' ');
690 /* FIXME: remove spaces at end */
692 show_binding(arg
, key
);
695 error_msg("expecting 2 arguments (context and key)\n");
698 static void cmd_quit(char *arg
)
700 if (!worker_has_job(JOB_TYPE_ANY
) || yes_no_query("Tracks are being added. Quit and truncate playlist(s)? [y/N]"))
704 static void cmd_reshuffle(char *arg
)
712 static void cmd_source(char *arg
)
714 char *filename
= expand_filename(arg
);
716 if (source_file(filename
) == -1)
717 error_msg("sourcing %s: %s", filename
, strerror(errno
));
721 static void cmd_colorscheme(char *arg
)
725 snprintf(filename
, sizeof(filename
), "%s/%s.theme", cmus_config_dir
, arg
);
726 if (source_file(filename
) == -1) {
727 snprintf(filename
, sizeof(filename
), DATADIR
"/cmus/%s.theme", arg
);
728 if (source_file(filename
) == -1)
729 error_msg("sourcing %s: %s", filename
, strerror(errno
));
734 * \" inside double-quotes becomes "
735 * \\ inside double-quotes becomes \
737 static char *parse_quoted(const char **strp
)
739 const char *str
= *strp
;
758 ret
= xnew(char, str
- start
);
768 if (c
!= '"' && c
!= '\\')
776 error_msg("`\"' expected");
780 static char *parse_escaped(const char **strp
)
782 const char *str
= *strp
;
790 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
802 ret
= xnew(char, str
- start
+ 1);
808 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
826 static char *parse_one(const char **strp
)
828 const char *str
= *strp
;
838 part
= parse_quoted(&str
);
841 } else if (c
== '\'') {
842 /* backslashes are normal chars inside single-quotes */
846 end
= strchr(str
, '\'');
849 part
= xstrndup(str
, end
- str
);
852 part
= parse_escaped(&str
);
858 char *tmp
= xstrjoin(ret
, part
);
866 error_msg("`'' expected");
872 static char **parse_cmd(const char *cmd
, int *args_idx
, int *ac
)
881 /* there can't be spaces at start of command
882 * and there is at least one argument */
883 if (cmd
[0] == '{' && cmd
[1] == '}' && (cmd
[2] == ' ' || cmd
[2] == 0)) {
884 /* {} is replaced with file arguments */
886 goto only_once_please
;
891 arg
= parse_one(&cmd
);
897 alloc
= alloc
? alloc
* 2 : 4;
898 av
= xrenew(char *, av
, alloc
+ 1);
909 error_msg("{} can be used only once");
917 static struct track_info
**sel_tis
;
918 static int sel_tis_alloc
;
919 static int sel_tis_nr
;
921 static int add_ti(void *data
, struct track_info
*ti
)
923 if (sel_tis_nr
== sel_tis_alloc
) {
924 sel_tis_alloc
= sel_tis_alloc
? sel_tis_alloc
* 2 : 8;
925 sel_tis
= xrenew(struct track_info
*, sel_tis
, sel_tis_alloc
);
928 sel_tis
[sel_tis_nr
++] = ti
;
932 static void cmd_run(char *arg
)
935 int ac
, argc
, i
, run
, files_idx
= -1;
937 if (cur_view
> QUEUE_VIEW
) {
938 info_msg("Command execution is supported only in views 1-4");
942 av
= parse_cmd(arg
, &files_idx
, &ac
);
947 /* collect selected files (struct track_info) */
955 __tree_for_each_sel(add_ti
, NULL
, 0);
958 __editable_for_each_sel(&lib_editable
, add_ti
, NULL
, 0);
961 __editable_for_each_sel(&pl_editable
, add_ti
, NULL
, 0);
964 __editable_for_each_sel(&pq_editable
, add_ti
, NULL
, 0);
969 if (sel_tis_nr
== 0) {
970 /* no files selected, do nothing */
974 sel_tis
[sel_tis_nr
] = NULL
;
977 argv
= xnew(char *, ac
+ sel_tis_nr
+ 1);
979 if (files_idx
== -1) {
980 /* add selected files after rest of the args */
981 for (i
= 0; i
< ac
; i
++)
982 argv
[argc
++] = av
[i
];
983 for (i
= 0; i
< sel_tis_nr
; i
++)
984 argv
[argc
++] = sel_tis
[i
]->filename
;
986 for (i
= 0; i
< files_idx
; i
++)
987 argv
[argc
++] = av
[i
];
988 for (i
= 0; i
< sel_tis_nr
; i
++)
989 argv
[argc
++] = sel_tis
[i
]->filename
;
990 for (i
= files_idx
; i
< ac
; i
++)
991 argv
[argc
++] = av
[i
];
995 for (i
= 0; argv
[i
]; i
++)
996 d_print("ARG: '%s'\n", argv
[i
]);
999 if (confirm_run
&& (sel_tis_nr
> 1 || strcmp(argv
[0], "rm") == 0)) {
1000 if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg
, sel_tis_nr
)) {
1001 info_msg("Aborted");
1008 if (spawn(argv
, &status
)) {
1009 error_msg("executing %s: %s", argv
[0], strerror(errno
));
1011 if (WIFEXITED(status
)) {
1012 int rc
= WEXITSTATUS(status
);
1015 error_msg("%s returned %d", argv
[0], rc
);
1017 if (WIFSIGNALED(status
))
1018 error_msg("%s received signal %d", argv
[0], WTERMSIG(status
));
1023 /* this must be done before sel_tis are unreffed */
1027 /* remove non-existed files, update tags for changed files */
1028 cmus_update_tis(sel_tis
, sel_tis_nr
);
1030 /* we don't own sel_tis anymore! */
1037 for (i
= 0; sel_tis
[i
]; i
++)
1038 track_info_unref(sel_tis
[i
]);
1042 static int get_one_ti(void *data
, struct track_info
*ti
)
1044 struct track_info
**sel_ti
= data
;
1048 /* stop the for each loop, we need only the first selected track */
1052 static void cmd_echo(char *arg
)
1054 struct track_info
*sel_ti
;
1058 ptr
= strchr(ptr
, '{');
1067 info_msg("%s", arg
);
1071 if (cur_view
> QUEUE_VIEW
) {
1072 info_msg("echo with {} in its arguments is supported only in views 1-4");
1079 /* get only the first selected track */
1085 __tree_for_each_sel(get_one_ti
, &sel_ti
, 0);
1088 __editable_for_each_sel(&lib_editable
, get_one_ti
, &sel_ti
, 0);
1091 __editable_for_each_sel(&pl_editable
, get_one_ti
, &sel_ti
, 0);
1094 __editable_for_each_sel(&pq_editable
, get_one_ti
, &sel_ti
, 0);
1102 info_msg("%s%s%s", arg
, sel_ti
->filename
, ptr
);
1103 track_info_unref(sel_ti
);
1106 #define VF_RELATIVE 0x01
1107 #define VF_PERCENTAGE 0x02
1109 static int parse_vol_arg(const char *arg
, int *value
, unsigned int *flags
)
1112 int ch
, val
= 0, digits
= 0, sign
= 1;
1118 } else if (*arg
== '+') {
1125 if (ch
< '0' || ch
> '9')
1141 *value
= sign
* val
;
1148 static int calc_vol(int val
, int old
, unsigned int flags
)
1150 if (flags
& VF_RELATIVE
) {
1151 if (flags
& VF_PERCENTAGE
)
1152 val
= scale_from_percentage(val
, volume_max
);
1154 } else if (flags
& VF_PERCENTAGE
) {
1155 val
= scale_from_percentage(val
, volume_max
);
1157 return clamp(val
, 0, volume_max
);
1161 * :vol value [value]
1163 * where value is [-+]?[0-9]+%?
1165 static void cmd_vol(char *arg
)
1167 char **values
= get_words(arg
);
1168 unsigned int lf
, rf
;
1171 if (values
[1] && values
[2])
1174 if (parse_vol_arg(values
[0], &l
, &lf
))
1179 if (values
[1] && parse_vol_arg(values
[1], &r
, &rf
))
1182 free_str_array(values
);
1184 player_get_volume(&ol
, &or);
1185 l
= calc_vol(l
, ol
, lf
);
1186 r
= calc_vol(r
, or, rf
);
1187 player_set_volume(l
, r
);
1190 free_str_array(values
);
1191 error_msg("expecting 1 or 2 arguments (total or L and R volumes [+-]INTEGER[%%])\n");
1194 static void cmd_prev_view(char *arg
)
1197 if (prev_view
>= 0) {
1199 set_view(prev_view
);
1204 static void cmd_view(char *arg
)
1208 if (parse_enum(arg
, 1, NR_VIEWS
, view_names
, &view
) && (view
- 1) != cur_view
) {
1209 prev_view
= cur_view
;
1214 static void cmd_p_next(char *arg
)
1219 static void cmd_p_pause(char *arg
)
1224 static void cmd_p_play(char *arg
)
1227 cmus_play_file(arg
);
1233 static void cmd_p_prev(char *arg
)
1238 static void cmd_p_stop(char *arg
)
1243 static void cmd_search_next(char *arg
)
1246 if (!search_next(searchable
, search_str
, search_direction
))
1251 static void cmd_search_prev(char *arg
)
1254 if (!search_next(searchable
, search_str
, !search_direction
))
1259 static int sorted_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1261 return editable_for_each_sel(&lib_editable
, cb
, data
, reverse
);
1264 static int pl_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1266 return editable_for_each_sel(&pl_editable
, cb
, data
, reverse
);
1269 static int pq_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1271 return editable_for_each_sel(&pq_editable
, cb
, data
, reverse
);
1274 static for_each_sel_ti_cb view_for_each_sel
[4] = {
1276 sorted_for_each_sel
,
1281 /* wrapper for void lib_add_track(struct track_info *) etc. */
1282 static int wrapper_cb(void *data
, struct track_info
*ti
)
1284 add_ti_cb add
= data
;
1290 static void add_from_browser(add_ti_cb add
, int job_type
)
1292 char *sel
= browser_get_sel();
1298 ft
= cmus_detect_ft(sel
, &ret
);
1299 if (ft
!= FILE_TYPE_INVALID
) {
1300 cmus_add(add
, ret
, ft
, job_type
);
1301 window_down(browser_win
, 1);
1308 static void cmd_win_add_l(char *arg
)
1310 if (cur_view
== TREE_VIEW
|| cur_view
== SORTED_VIEW
)
1313 if (cur_view
<= QUEUE_VIEW
) {
1315 view_for_each_sel
[cur_view
](wrapper_cb
, lib_add_track
, 0);
1317 } else if (cur_view
== BROWSER_VIEW
) {
1318 add_from_browser(lib_add_track
, JOB_TYPE_LIB
);
1322 static void cmd_win_add_p(char *arg
)
1324 /* could allow adding dups? */
1325 if (cur_view
== PLAYLIST_VIEW
)
1328 if (cur_view
<= QUEUE_VIEW
) {
1330 view_for_each_sel
[cur_view
](wrapper_cb
, pl_add_track
, 0);
1332 } else if (cur_view
== BROWSER_VIEW
) {
1333 add_from_browser(pl_add_track
, JOB_TYPE_PL
);
1337 static void cmd_win_add_Q(char *arg
)
1339 if (cur_view
== QUEUE_VIEW
)
1342 if (cur_view
<= QUEUE_VIEW
) {
1344 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_prepend
, 1);
1346 } else if (cur_view
== BROWSER_VIEW
) {
1347 add_from_browser(play_queue_prepend
, JOB_TYPE_QUEUE
);
1351 static void cmd_win_add_q(char *arg
)
1353 if (cur_view
== QUEUE_VIEW
)
1356 if (cur_view
<= QUEUE_VIEW
) {
1358 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_append
, 0);
1360 } else if (cur_view
== BROWSER_VIEW
) {
1361 add_from_browser(play_queue_append
, JOB_TYPE_QUEUE
);
1365 static void cmd_win_activate(char *arg
)
1367 struct track_info
*info
= NULL
;
1372 info
= tree_set_selected();
1375 info
= sorted_set_selected();
1378 info
= pl_set_selected();
1395 /* update lib/pl mode */
1401 player_play_file(info
);
1405 static void cmd_win_mv_after(char *arg
)
1412 editable_move_after(&lib_editable
);
1415 editable_move_after(&pl_editable
);
1418 editable_move_after(&pq_editable
);
1430 static void cmd_win_mv_before(char *arg
)
1437 editable_move_before(&lib_editable
);
1440 editable_move_before(&pl_editable
);
1443 editable_move_before(&pq_editable
);
1455 static void cmd_win_remove(char *arg
)
1463 editable_remove_sel(&lib_editable
);
1466 editable_remove_sel(&pl_editable
);
1469 editable_remove_sel(&pq_editable
);
1475 filters_delete_filter();
1484 static void cmd_win_sel_cur(char *arg
)
1492 sorted_sel_current();
1509 static void cmd_win_toggle(char *arg
)
1514 tree_toggle_expand_artist();
1519 editable_toggle_mark(&lib_editable
);
1524 editable_toggle_mark(&pl_editable
);
1529 editable_toggle_mark(&pq_editable
);
1535 filters_toggle_filter();
1543 static struct window
*current_win(void)
1549 return lib_editable
.win
;
1551 return pl_editable
.win
;
1553 return pq_editable
.win
;
1564 static void cmd_win_bottom(char *arg
)
1567 window_goto_bottom(current_win());
1571 static void cmd_win_down(char *arg
)
1574 window_down(current_win(), 1);
1578 static void cmd_win_next(char *arg
)
1580 if (cur_view
== TREE_VIEW
) {
1582 tree_toggle_active_window();
1587 static void cmd_win_pg_down(char *arg
)
1590 window_page_down(current_win());
1594 static void cmd_win_pg_up(char *arg
)
1597 window_page_up(current_win());
1601 static void cmd_win_top(char *arg
)
1604 window_goto_top(current_win());
1608 static void cmd_win_up(char *arg
)
1611 window_up(current_win(), 1);
1615 static void cmd_win_update(char *arg
)
1628 static void cmd_browser_up(char *arg
)
1633 static void cmd_refresh(char *arg
)
1635 clearok(curscr
, TRUE
);
1639 static int cmp_intp(const void *ap
, const void *bp
)
1646 static int *rand_array(int size
, int nmax
)
1648 int *r
= xnew(int, size
+ 1);
1652 if (count
> nmax
/ 2) {
1654 * Imagine that there are 1000 tracks in library and we want to
1655 * add 998 random tracks to queue. After we have added 997
1656 * random numbers to the array it would be quite hard to find a
1657 * random number that isn't already in the array (3/1000
1660 * So we invert the logic:
1662 * Find two (1000 - 998) random numbers in 0..999 range and put
1663 * them at end of the array. Sort the numbers and then fill
1664 * the array starting at index 0 with incrementing values that
1665 * are not in the set of random numbers.
1667 count
= nmax
- count
;
1668 offset
= size
- count
;
1671 for (i
= 0; i
< count
; ) {
1675 for (j
= 0; j
< i
; j
++) {
1676 if (r
[offset
+ j
] == v
)
1679 r
[offset
+ i
++] = v
;
1681 qsort(r
+ offset
, count
, sizeof(*r
), cmp_intp
);
1686 /* simplifies next loop */
1689 /* convert the indexes we don't want to those we want */
1703 static int count_albums(void)
1705 struct artist
*artist
;
1706 struct list_head
*item
;
1709 list_for_each_entry(artist
, &lib_artist_head
, node
) {
1710 list_for_each(item
, &artist
->album_head
)
1717 struct list_head node
;
1718 const struct album
*album
;
1721 static void cmd_lqueue(char *arg
)
1724 const struct list_head
*item
;
1725 const struct album
*album
;
1726 int count
= 1, nmax
, i
, pos
;
1732 if (str_to_int(arg
, &val
) || val
<= 0) {
1733 error_msg("argument must be positive integer");
1739 nmax
= count_albums();
1745 r
= rand_array(count
, nmax
);
1746 album
= to_album(to_artist(lib_artist_head
.next
)->album_head
.next
);
1748 for (i
= 0; i
< count
; i
++) {
1749 struct album_list
*a
;
1751 while (pos
< r
[i
]) {
1752 struct artist
*artist
= album
->artist
;
1753 if (album
->node
.next
== &artist
->album_head
) {
1754 artist
= to_artist(artist
->node
.next
);
1755 album
= to_album(artist
->album_head
.next
);
1757 album
= to_album(album
->node
.next
);
1761 a
= xnew(struct album_list
, 1);
1763 list_add_rand(&head
, &a
->node
, i
);
1769 struct list_head
*next
= item
->next
;
1770 struct album_list
*a
= container_of(item
, struct album_list
, node
);
1771 struct tree_track
*t
;
1773 list_for_each_entry(t
, &a
->album
->track_head
, node
)
1774 editable_add(&pq_editable
, simple_track_new(tree_track_info(t
)));
1777 } while (item
!= &head
);
1782 static void cmd_tqueue(char *arg
)
1785 struct list_head
*item
;
1786 int count
= 1, i
, pos
;
1792 if (str_to_int(arg
, &val
) || val
<= 0) {
1793 error_msg("argument must be positive integer");
1799 if (count
> lib_editable
.nr_tracks
)
1800 count
= lib_editable
.nr_tracks
;
1804 r
= rand_array(count
, lib_editable
.nr_tracks
);
1805 item
= lib_editable
.head
.next
;
1807 for (i
= 0; i
< count
; i
++) {
1808 struct simple_track
*t
;
1810 while (pos
< r
[i
]) {
1814 t
= simple_track_new(to_simple_track(item
)->info
);
1815 list_add_rand(&head
, &t
->node
, i
);
1821 struct list_head
*next
= item
->next
;
1822 struct simple_track
*t
= to_simple_track(item
);
1823 editable_add(&pq_editable
, t
);
1825 } while (item
!= &head
);
1832 * these functions fill tabexp struct, which is resetted beforehand
1835 /* buffer used for tab expansion */
1836 static char expbuf
[512];
1838 static int filter_directories(const char *name
, const struct stat
*s
)
1840 return S_ISDIR(s
->st_mode
);
1843 static int filter_any(const char *name
, const struct stat
*s
)
1848 static int filter_playable(const char *name
, const struct stat
*s
)
1850 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1853 static int filter_playlist(const char *name
, const struct stat
*s
)
1855 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1858 static int filter_supported(const char *name
, const struct stat
*s
)
1860 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1863 static void expand_files(const char *str
)
1865 expand_files_and_dirs(str
, filter_any
);
1868 static void expand_directories(const char *str
)
1870 expand_files_and_dirs(str
, filter_directories
);
1873 static void expand_playable(const char *str
)
1875 expand_files_and_dirs(str
, filter_playable
);
1878 static void expand_playlist(const char *str
)
1880 expand_files_and_dirs(str
, filter_playlist
);
1883 static void expand_supported(const char *str
)
1885 expand_files_and_dirs(str
, filter_supported
);
1888 static void expand_add(const char *str
)
1890 int flag
= parse_flags(&str
, "lpqQ");
1896 expand_supported(str
);
1898 if (tabexp
.head
&& flag
) {
1899 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1901 tabexp
.head
= xstrdup(expbuf
);
1905 static void expand_load_save(const char *str
)
1907 int flag
= parse_flags(&str
, "lp");
1913 expand_playlist(str
);
1915 if (tabexp
.head
&& flag
) {
1916 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1918 tabexp
.head
= xstrdup(expbuf
);
1922 static void expand_key_context(const char *str
, const char *force
)
1924 int pos
, i
, len
= strlen(str
);
1927 tails
= xnew(char *, NR_CTXS
+ 1);
1929 for (i
= 0; key_context_names
[i
]; i
++) {
1930 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1935 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1943 char *tmp
= xstrjoin(tails
[0], " ");
1948 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1949 tabexp
.head
= xstrdup(expbuf
);
1950 tabexp
.tails
= tails
;
1953 static int get_context(const char *str
, int len
)
1955 int i
, c
= -1, count
= 0;
1957 for (i
= 0; key_context_names
[i
]; i
++) {
1958 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1959 if (key_context_names
[i
][len
] == 0) {
1972 static void expand_command_line(const char *str
);
1974 static void expand_bind_args(const char *str
)
1976 /* :bind context key function
1978 * possible values for str:
1983 * you need to know context before you can expand function
1985 /* start and end pointers for context, key and function */
1986 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1988 int flag
= parse_flags((const char **)&str
, "f");
1989 const char *force
= "";
2000 ce
= strchr(cs
, ' ');
2002 expand_key_context(cs
, force
);
2006 /* context must be expandable */
2007 c
= get_context(cs
, ce
- cs
);
2009 /* context is ambiguous or invalid */
2016 ke
= strchr(ks
, ' ');
2019 int len
= strlen(ks
);
2022 for (i
= 0; key_table
[i
].name
; i
++) {
2023 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
2028 ptr_array_add(&array
, xstrdup(key_table
[i
].name
+ len
));
2034 if (array
.count
== 1) {
2035 char **ptrs
= array
.ptrs
;
2036 char *tmp
= xstrjoin(ptrs
[0], " ");
2041 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
2043 ptr_array_plug(&array
);
2044 tabexp
.head
= xstrdup(expbuf
);
2045 tabexp
.tails
= array
.ptrs
;
2049 /* key must be expandable */
2052 for (i
= 0; key_table
[i
].name
; i
++) {
2053 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
2054 if (key_table
[i
].name
[ke
- ks
] == 0) {
2065 /* key is ambiguous or invalid */
2076 /* expand com [arg...] */
2077 expand_command_line(fs
);
2078 if (tabexp
.head
== NULL
) {
2079 /* command expand failed */
2084 * tabexp.head is now "com"
2085 * tabexp.tails is [ mand1 mand2 ... ]
2087 * need to change tabexp.head to "context key com"
2090 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
2091 key_table
[k
].name
, tabexp
.head
);
2093 tabexp
.head
= xstrdup(expbuf
);
2096 static void expand_unbind_args(const char *str
)
2098 /* :unbind context key */
2099 /* start and end pointers for context and key */
2100 const char *cs
, *ce
, *ks
;
2101 const struct binding
*b
;
2106 ce
= strchr(cs
, ' ');
2108 expand_key_context(cs
, "");
2112 /* context must be expandable */
2113 c
= get_context(cs
, ce
- cs
);
2115 /* context is ambiguous or invalid */
2125 b
= key_bindings
[c
];
2127 if (!strncmp(ks
, b
->key
->name
, len
))
2128 ptr_array_add(&array
, xstrdup(b
->key
->name
+ len
));
2134 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
2136 ptr_array_plug(&array
);
2137 tabexp
.head
= xstrdup(expbuf
);
2138 tabexp
.tails
= array
.ptrs
;
2141 static void expand_factivate(const char *str
)
2143 /* "name1 name2 name3", expand only name3 */
2144 struct filter_entry
*e
;
2147 int str_len
, len
, i
;
2149 str_len
= strlen(str
);
2152 if (str
[i
- 1] == ' ')
2159 list_for_each_entry(e
, &filters_head
, node
) {
2160 if (!strncmp(name
, e
->name
, len
))
2161 ptr_array_add(&array
, xstrdup(e
->name
+ len
));
2166 ptr_array_plug(&array
);
2167 tabexp
.head
= xstrdup(str
);
2168 tabexp
.tails
= array
.ptrs
;
2171 static void expand_options(const char *str
)
2173 struct cmus_opt
*opt
;
2177 /* tabexp is resetted */
2179 if (len
> 1 && str
[len
- 1] == '=') {
2181 char *var
= xstrndup(str
, len
- 1);
2183 list_for_each_entry(opt
, &option_head
, node
) {
2184 if (strcmp(var
, opt
->name
) == 0) {
2185 char buf
[OPTION_MAX_SIZE
];
2187 tails
= xnew(char *, 2);
2190 opt
->get(opt
->id
, buf
);
2191 tails
[0] = xstrdup(buf
);
2194 tabexp
.head
= xstrdup(str
);
2195 tabexp
.tails
= tails
;
2202 /* expand variable */
2205 tails
= xnew(char *, nr_options
+ 1);
2207 list_for_each_entry(opt
, &option_head
, node
) {
2208 if (strncmp(str
, opt
->name
, len
) == 0)
2209 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2213 /* only one variable matches, add '=' */
2214 char *tmp
= xstrjoin(tails
[0], "=");
2221 tabexp
.head
= xstrdup(str
);
2222 tabexp
.tails
= tails
;
2229 static void expand_toptions(const char *str
)
2231 struct cmus_opt
*opt
;
2235 tails
= xnew(char *, nr_options
+ 1);
2238 list_for_each_entry(opt
, &option_head
, node
) {
2239 if (opt
->toggle
== NULL
)
2241 if (strncmp(str
, opt
->name
, len
) == 0)
2242 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2246 tabexp
.head
= xstrdup(str
);
2247 tabexp
.tails
= tails
;
2253 static void load_themes(const char *dirname
, const char *str
, struct ptr_array
*array
)
2255 struct directory dir
;
2256 const char *name
, *dot
;
2257 int len
= strlen(str
);
2259 if (dir_open(&dir
, dirname
))
2262 while ((name
= dir_read(&dir
))) {
2263 if (!S_ISREG(dir
.st
.st_mode
))
2265 if (strncmp(name
, str
, len
))
2267 dot
= strrchr(name
, '.');
2268 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2270 if (dot
- name
< len
)
2272 * matches "foo.theme"
2273 * which also ends with ".theme"
2276 ptr_array_add(array
, xstrndup(name
+ len
, dot
- name
- len
));
2281 static void expand_colorscheme(const char *str
)
2285 load_themes(cmus_config_dir
, str
, &array
);
2286 load_themes(DATADIR
"/cmus", str
, &array
);
2289 ptr_array_sort(&array
, strptrcmp
);
2291 ptr_array_plug(&array
);
2292 tabexp
.head
= xstrdup(str
);
2293 tabexp
.tails
= array
.ptrs
;
2300 struct command commands
[] = {
2301 { "add", cmd_add
, 1, 1, expand_add
, 0, 0 },
2302 { "bind", cmd_bind
, 1, 1, expand_bind_args
, 0, CMD_UNSAFE
},
2303 { "browser-up", cmd_browser_up
, 0, 0, NULL
, 0, 0 },
2304 { "cd", cmd_cd
, 0, 1, expand_directories
, 0, 0 },
2305 { "clear", cmd_clear
, 0, 1, NULL
, 0, 0 },
2306 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
, 0, 0 },
2307 { "echo", cmd_echo
, 1,-1, NULL
, 0, 0 },
2308 { "factivate", cmd_factivate
, 0, 1, expand_factivate
, 0, 0 },
2309 { "filter", cmd_filter
, 0, 1, NULL
, 0, 0 },
2310 { "fset", cmd_fset
, 1, 1, NULL
, 0, 0 },
2311 { "invert", cmd_invert
, 0, 0, NULL
, 0, 0 },
2312 { "load", cmd_load
, 1, 1, expand_load_save
, 0, 0 },
2313 { "lqueue", cmd_lqueue
, 0, 1, NULL
, 0, 0 },
2314 { "mark", cmd_mark
, 0, 1, NULL
, 0, 0 },
2315 { "player-next", cmd_p_next
, 0, 0, NULL
, 0, 0 },
2316 { "player-pause", cmd_p_pause
, 0, 0, NULL
, 0, 0 },
2317 { "player-play", cmd_p_play
, 0, 1, expand_playable
, 0, 0 },
2318 { "player-prev", cmd_p_prev
, 0, 0, NULL
, 0, 0 },
2319 { "player-stop", cmd_p_stop
, 0, 0, NULL
, 0, 0 },
2320 { "prev-view", cmd_prev_view
, 0, 0, NULL
, 0, 0 },
2321 { "quit", cmd_quit
, 0, 0, NULL
, 0, 0 },
2322 { "refresh", cmd_refresh
, 0, 0, NULL
, 0, 0 },
2323 { "run", cmd_run
, 1,-1, NULL
, 0, CMD_UNSAFE
},
2324 { "save", cmd_save
, 0, 1, expand_load_save
, 0, CMD_UNSAFE
},
2325 { "search-next", cmd_search_next
,0, 0, NULL
, 0, 0 },
2326 { "search-prev", cmd_search_prev
,0, 0, NULL
, 0, 0 },
2327 { "seek", cmd_seek
, 1, 1, NULL
, 0, 0 },
2328 { "set", cmd_set
, 1, 1, expand_options
, 0, 0 },
2329 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
, 0, 0 },
2330 { "shuffle", cmd_reshuffle
, 0, 0, NULL
, 0, 0 },
2331 { "source", cmd_source
, 1, 1, expand_files
, 0, CMD_UNSAFE
},
2332 { "toggle", cmd_toggle
, 1, 1, expand_toptions
, 0, 0 },
2333 { "tqueue", cmd_tqueue
, 0, 1, NULL
, 0, 0 },
2334 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
, 0, 0 },
2335 { "unmark", cmd_unmark
, 0, 0, NULL
, 0, 0 },
2336 { "view", cmd_view
, 1, 1, NULL
, 0, 0 },
2337 { "vol", cmd_vol
, 1, 2, NULL
, 0, 0 },
2338 { "win-activate", cmd_win_activate
,0, 0, NULL
, 0, 0 },
2339 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
, 0, 0 },
2340 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
, 0, 0 },
2341 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
, 0, 0 },
2342 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
, 0, 0 },
2343 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
, 0, 0 },
2344 { "win-down", cmd_win_down
, 0, 0, NULL
, 0, 0 },
2345 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
, 0, 0 },
2346 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
, 0, 0 },
2347 { "win-next", cmd_win_next
, 0, 0, NULL
, 0, 0 },
2348 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
, 0, 0 },
2349 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
, 0, 0 },
2350 { "win-remove", cmd_win_remove
, 0, 0, NULL
, 0, CMD_UNSAFE
},
2351 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
, 0, 0 },
2352 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
, 0, 0 },
2353 { "win-top", cmd_win_top
, 0, 0, NULL
, 0, 0 },
2354 { "win-up", cmd_win_up
, 0, 0, NULL
, 0, 0 },
2355 { "win-update", cmd_win_update
, 0, 0, NULL
, 0, 0 },
2356 { NULL
, NULL
, 0, 0, 0, 0, 0 }
2359 /* fills tabexp struct */
2360 static void expand_commands(const char *str
)
2365 /* tabexp is resetted */
2366 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2369 for (i
= 0; commands
[i
].name
; i
++) {
2370 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2371 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2375 /* only one command matches, add ' ' */
2376 char *tmp
= xstrjoin(tails
[0], " ");
2382 tabexp
.head
= xstrdup(str
);
2383 tabexp
.tails
= tails
;
2389 struct command
*get_command(const char *str
)
2395 for (len
= 0; str
[len
] && str
[len
] != ' '; len
++)
2398 for (i
= 0; commands
[i
].name
; i
++) {
2399 if (strncmp(str
, commands
[i
].name
, len
))
2402 if (commands
[i
].name
[len
] == 0) {
2404 return &commands
[i
];
2407 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2411 return &commands
[i
];
2416 /* fills tabexp struct */
2417 static void expand_command_line(const char *str
)
2419 /* :command [arg]...
2423 * str expanded value (tabexp.head)
2424 * -------------------------------------
2427 * se se (tabexp.tails = [ ek t ])
2429 /* command start/end, argument start */
2430 const char *cs
, *ce
, *as
;
2431 const struct command
*cmd
;
2434 ce
= strchr(cs
, ' ');
2436 /* expand command */
2437 expand_commands(cs
);
2441 /* command must be expandable */
2442 cmd
= get_command(cs
);
2444 /* command ambiguous or invalid */
2448 if (cmd
->expand
== NULL
) {
2449 /* can't expand argument */
2457 /* expand argument */
2459 if (tabexp
.head
== NULL
) {
2460 /* argument expansion failed */
2464 /* tabexp.head is now start of the argument string */
2465 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2467 tabexp
.head
= xstrdup(expbuf
);
2470 static void tab_expand(void)
2472 char *s1
, *s2
, *tmp
;
2475 /* strip white space */
2477 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2480 /* string to expand */
2481 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2484 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2486 tmp
= tabexp_expand(s1
, expand_command_line
);
2493 cmdline
.blen
= l1
+ l2
;
2494 if (cmdline
.blen
>= cmdline
.size
) {
2495 while (cmdline
.blen
>= cmdline
.size
)
2497 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2499 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2501 cmdline
.cpos
= u_strlen(tmp
);
2502 cmdline
.clen
= u_strlen(cmdline
.line
);
2509 static void reset_tab_expansion(void)
2512 arg_expand_cmd
= -1;
2515 int run_only_safe_commands
;
2517 /* FIXME: parse all arguments */
2518 void run_command(const char *buf
)
2521 int cmd_start
, cmd_end
, cmd_len
;
2522 int arg_start
, arg_end
;
2526 while (buf
[i
] && buf
[i
] == ' ')
2533 while (buf
[i
] && buf
[i
] != ' ')
2536 while (buf
[i
] && buf
[i
] == ' ')
2543 cmd_len
= cmd_end
- cmd_start
;
2547 cmd
= xstrndup(buf
+ cmd_start
, cmd_len
);
2548 if (arg_start
== arg_end
) {
2551 arg
= xstrndup(buf
+ arg_start
, arg_end
- arg_start
);
2555 const struct command
*c
= &commands
[i
];
2557 if (c
->name
== NULL
) {
2558 error_msg("unknown command\n");
2561 if (strncmp(cmd
, c
->name
, cmd_len
) == 0) {
2562 const char *next
= commands
[i
+ 1].name
;
2563 int exact
= c
->name
[cmd_len
] == 0;
2565 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2566 error_msg("ambiguous command\n");
2569 if (c
->min_args
> 0 && arg
== NULL
) {
2570 error_msg("not enough arguments\n");
2573 if (c
->max_args
== 0 && arg
) {
2574 error_msg("too many arguments\n");
2577 if (run_only_safe_commands
&& c
->flags
& CMD_UNSAFE
) {
2578 d_print("trying to execute unsafe command over net\n");
2590 static void reset_history_search(void)
2592 history_reset_search(&cmd_history
);
2593 free(history_search_text
);
2594 history_search_text
= NULL
;
2597 static void backspace(void)
2599 if (cmdline
.clen
> 0) {
2600 cmdline_backspace();
2602 input_mode
= NORMAL_MODE
;
2606 void command_mode_ch(uchar ch
)
2610 cmdline_move_home();
2613 cmdline_move_left();
2616 cmdline_delete_ch();
2622 cmdline_move_right();
2628 history_add_line(&cmd_history
, cmdline
.line
);
2631 input_mode
= NORMAL_MODE
;
2635 run_command(cmdline
.line
);
2636 history_add_line(&cmd_history
, cmdline
.line
);
2639 input_mode
= NORMAL_MODE
;
2642 cmdline_clear_end();
2648 cmdline_backspace_to_bol();
2654 cmdline_insert_ch(ch
);
2656 reset_history_search();
2658 reset_tab_expansion();
2661 void command_mode_key(int key
)
2663 reset_tab_expansion();
2666 cmdline_delete_ch();
2672 cmdline_move_left();
2675 cmdline_move_right();
2678 cmdline_move_home();
2687 if (history_search_text
== NULL
)
2688 history_search_text
= xstrdup(cmdline
.line
);
2689 s
= history_search_forward(&cmd_history
, history_search_text
);
2691 cmdline_set_text(s
);
2695 if (history_search_text
) {
2698 s
= history_search_backward(&cmd_history
, history_search_text
);
2700 cmdline_set_text(s
);
2702 cmdline_set_text(history_search_text
);
2707 d_print("key = %c (%d)\n", key
, key
);
2709 reset_history_search();
2712 void commands_init(void)
2714 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2715 history_load(&cmd_history
, cmd_history_filename
, 2000);
2718 void commands_exit(void)
2720 history_save(&cmd_history
);
2721 free(cmd_history_filename
);