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"
35 #include "play_queue.h"
43 #include "format_print.h"
49 #include "config/datadir.h"
54 #include <sys/types.h>
65 static struct history cmd_history
;
66 static char *cmd_history_filename
;
67 static char *history_search_text
= NULL
;
68 static int arg_expand_cmd
= -1;
69 static int prev_view
= -1;
71 static char *get_home_dir(const char *username
)
73 struct passwd
*passwd
;
76 return xstrdup(home_dir
);
77 passwd
= getpwnam(username
);
80 /* don't free passwd */
81 return xstrdup(passwd
->pw_dir
);
84 static char *expand_filename(const char *name
)
89 slash
= strchr(name
, '/');
91 char *username
, *home
;
93 if (slash
- name
- 1 > 0) {
95 username
= xstrndup(name
+ 1, slash
- name
- 1);
100 home
= get_home_dir(username
);
105 expanded
= xstrjoin(home
, slash
);
109 return xstrdup(name
);
113 return xstrdup(home_dir
);
117 home
= get_home_dir(name
+ 1);
120 return xstrdup(name
);
124 return xstrdup(name
);
130 void view_clear(int view
)
135 worker_remove_jobs(JOB_TYPE_LIB
);
137 editable_clear(&lib_editable
);
139 /* FIXME: make this optional? */
145 worker_remove_jobs(JOB_TYPE_PL
);
147 editable_clear(&pl_editable
);
151 worker_remove_jobs(JOB_TYPE_QUEUE
);
153 editable_clear(&pq_editable
);
157 info_msg(":clear only works in views 1-4");
161 void view_add(int view
, char *arg
, int prepend
)
166 tmp
= expand_filename(arg
);
167 ft
= cmus_detect_ft(tmp
, &name
);
168 if (ft
== FILE_TYPE_INVALID
) {
169 error_msg("adding '%s': %s", tmp
, strerror(errno
));
178 cmus_add(lib_add_track
, name
, ft
, JOB_TYPE_LIB
);
181 cmus_add(pl_add_track
, name
, ft
, JOB_TYPE_PL
);
185 cmus_add(play_queue_prepend
, name
, ft
, JOB_TYPE_QUEUE
);
187 cmus_add(play_queue_append
, name
, ft
, JOB_TYPE_QUEUE
);
191 info_msg(":add only works in views 1-4");
196 void view_load(int view
, char *arg
)
201 tmp
= expand_filename(arg
);
202 ft
= cmus_detect_ft(tmp
, &name
);
203 if (ft
== FILE_TYPE_INVALID
) {
204 error_msg("loading '%s': %s", tmp
, strerror(errno
));
210 if (ft
== FILE_TYPE_FILE
)
212 if (ft
!= FILE_TYPE_PL
) {
213 error_msg("loading '%s': not a playlist file", name
);
221 worker_remove_jobs(JOB_TYPE_LIB
);
223 editable_clear(&lib_editable
);
225 cmus_add(lib_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_LIB
);
230 worker_remove_jobs(JOB_TYPE_PL
);
232 editable_clear(&pl_editable
);
234 cmus_add(pl_add_track
, name
, FILE_TYPE_PL
, JOB_TYPE_PL
);
239 info_msg(":load only works in views 1-3");
244 static void do_save(for_each_ti_cb for_each_ti
, const char *arg
, char **filenamep
)
246 char *filename
= *filenamep
;
250 filename
= xstrdup(arg
);
251 *filenamep
= filename
;
255 if (cmus_save(for_each_ti
, filename
) == -1)
256 error_msg("saving '%s': %s", filename
, strerror(errno
));
260 void view_save(int view
, char *arg
)
265 tmp
= expand_filename(arg
);
266 arg
= path_absolute(tmp
);
273 if (worker_has_job(JOB_TYPE_LIB
))
275 do_save(lib_for_each
, arg
, &lib_filename
);
278 if (worker_has_job(JOB_TYPE_PL
))
280 do_save(pl_for_each
, arg
, &pl_filename
);
283 info_msg(":save only works in views 1 & 2 (library) and 3 (playlist)");
288 error_msg("can't save when tracks are being added");
294 /* only returns the last flag which is enough for it's callers */
295 static int parse_flags(const char **strp
, const char *flags
)
297 const char *str
= *strp
;
312 if (str
[1] == '-' && (str
[2] == 0 || str
[2] == ' ')) {
318 if (str
[2] && str
[2] != ' ')
322 if (!strchr(flags
, flag
)) {
323 error_msg("invalid option -%c", flag
);
340 static int flag_to_view(int flag
)
346 return PLAYLIST_VIEW
;
355 static void cmd_add(char *arg
)
357 int flag
= parse_flags((const char **)&arg
, "lpqQ");
362 error_msg("not enough arguments\n");
365 view_add(flag_to_view(flag
), arg
, flag
== 'Q');
368 static void cmd_clear(char *arg
)
370 int flag
= parse_flags((const char **)&arg
, "lpq");
375 error_msg("too many arguments\n");
378 view_clear(flag_to_view(flag
));
381 static void cmd_load(char *arg
)
383 int flag
= parse_flags((const char **)&arg
, "lp");
388 error_msg("not enough arguments\n");
391 view_load(flag_to_view(flag
), arg
);
394 static void cmd_save(char *arg
)
396 int flag
= parse_flags((const char **)&arg
, "lp");
400 view_save(flag_to_view(flag
), arg
);
403 static void cmd_set(char *arg
)
408 for (i
= 0; arg
[i
]; i
++) {
416 option_set(arg
, value
);
417 help_win
->changed
= 1;
419 struct cmus_opt
*opt
;
420 char buf
[OPTION_MAX_SIZE
];
422 /* support "set <option>?" */
427 opt
= option_find(arg
);
429 opt
->get(opt
->id
, buf
);
430 info_msg("setting: '%s=%s'", arg
, buf
);
435 static void cmd_toggle(char *arg
)
437 struct cmus_opt
*opt
= option_find(arg
);
442 if (opt
->toggle
== NULL
) {
443 error_msg("%s is not toggle option", opt
->name
);
446 opt
->toggle(opt
->id
);
447 help_win
->changed
= 1;
450 static int get_number(char *str
, char **end
)
454 while (*str
>= '0' && *str
<= '9') {
462 static void cmd_seek(char *arg
)
465 int seek
= 0, sign
= 1, count
;
487 num
= get_number(arg
, &end
);
491 seek
= seek
* 60 + num
;
492 } while (++count
< 3);
499 switch (tolower(*arg
)) {
511 player_seek(seek
, relative
);
515 error_msg("expecting one argument: [+-]INTEGER[mh] or [+-]H:MM:SS");
518 static void cmd_factivate(char *arg
)
521 filters_activate_names(arg
);
525 static void cmd_filter(char *arg
)
528 filters_set_anonymous(arg
);
532 static void cmd_fset(char *arg
)
534 filters_set_filter(arg
);
537 static void cmd_invert(char *arg
)
542 editable_invert_marks(&lib_editable
);
545 editable_invert_marks(&pl_editable
);
548 editable_invert_marks(&pq_editable
);
551 info_msg(":invert only works in views 2-4");
556 static void cmd_mark(char *arg
)
561 editable_mark(&lib_editable
, arg
);
564 editable_mark(&pl_editable
, arg
);
567 editable_mark(&pq_editable
, arg
);
570 info_msg(":mark only works in views 2-4");
575 static void cmd_unmark(char *arg
)
580 editable_unmark(&lib_editable
);
583 editable_unmark(&pl_editable
);
586 editable_unmark(&pq_editable
);
589 info_msg(":unmark only works in views 2-4");
594 static void cmd_cd(char *arg
)
597 char *dir
, *absolute
;
599 dir
= expand_filename(arg
);
600 absolute
= path_absolute(dir
);
601 if (chdir(dir
) == -1) {
602 error_msg("could not cd to '%s': %s", dir
, strerror(errno
));
604 browser_chdir(absolute
);
609 if (chdir(home_dir
) == -1) {
610 error_msg("could not cd to '%s': %s", home_dir
, strerror(errno
));
612 browser_chdir(home_dir
);
617 static void cmd_bind(char *arg
)
619 int flag
= parse_flags((const char **)&arg
, "f");
628 key
= strchr(arg
, ' ');
635 func
= strchr(key
, ' ');
644 key_bind(arg
, key
, func
, flag
== 'f');
647 error_msg("expecting 3 arguments (context, key and function)\n");
650 static void cmd_unbind(char *arg
)
652 int flag
= parse_flags((const char **)&arg
, "f");
661 key
= strchr(arg
, ' ');
670 /* FIXME: remove spaces at end */
672 key_unbind(arg
, key
, flag
== 'f');
675 error_msg("expecting 2 arguments (context and key)\n");
678 static void cmd_showbind(char *arg
)
682 key
= strchr(arg
, ' ');
691 /* FIXME: remove spaces at end */
693 show_binding(arg
, key
);
696 error_msg("expecting 2 arguments (context and key)\n");
699 static void cmd_quit(char *arg
)
701 if (!worker_has_job(JOB_TYPE_ANY
) || yes_no_query("Tracks are being added. Quit and truncate playlist(s)? [y/N]"))
705 static void cmd_reshuffle(char *arg
)
713 static void cmd_source(char *arg
)
715 char *filename
= expand_filename(arg
);
717 if (source_file(filename
) == -1)
718 error_msg("sourcing %s: %s", filename
, strerror(errno
));
722 static void cmd_colorscheme(char *arg
)
726 snprintf(filename
, sizeof(filename
), "%s/%s.theme", cmus_config_dir
, arg
);
727 if (source_file(filename
) == -1) {
728 snprintf(filename
, sizeof(filename
), DATADIR
"/cmus/%s.theme", arg
);
729 if (source_file(filename
) == -1)
730 error_msg("sourcing %s: %s", filename
, strerror(errno
));
735 * \" inside double-quotes becomes "
736 * \\ inside double-quotes becomes \
738 static char *parse_quoted(const char **strp
)
740 const char *str
= *strp
;
759 ret
= xnew(char, str
- start
);
769 if (c
!= '"' && c
!= '\\')
777 error_msg("`\"' expected");
781 static char *parse_escaped(const char **strp
)
783 const char *str
= *strp
;
791 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
803 ret
= xnew(char, str
- start
+ 1);
809 if (c
== 0 || c
== ' ' || c
== '\'' || c
== '"')
827 static char *parse_one(const char **strp
)
829 const char *str
= *strp
;
839 part
= parse_quoted(&str
);
842 } else if (c
== '\'') {
843 /* backslashes are normal chars inside single-quotes */
847 end
= strchr(str
, '\'');
850 part
= xstrndup(str
, end
- str
);
853 part
= parse_escaped(&str
);
859 char *tmp
= xstrjoin(ret
, part
);
867 error_msg("`'' expected");
873 static char **parse_cmd(const char *cmd
, int *args_idx
, int *ac
)
882 /* there can't be spaces at start of command
883 * and there is at least one argument */
884 if (cmd
[0] == '{' && cmd
[1] == '}' && (cmd
[2] == ' ' || cmd
[2] == 0)) {
885 /* {} is replaced with file arguments */
887 goto only_once_please
;
892 arg
= parse_one(&cmd
);
898 alloc
= alloc
? alloc
* 2 : 4;
899 av
= xrenew(char *, av
, alloc
+ 1);
910 error_msg("{} can be used only once");
918 static struct track_info
**sel_tis
;
919 static int sel_tis_alloc
;
920 static int sel_tis_nr
;
922 static int add_ti(void *data
, struct track_info
*ti
)
924 if (sel_tis_nr
== sel_tis_alloc
) {
925 sel_tis_alloc
= sel_tis_alloc
? sel_tis_alloc
* 2 : 8;
926 sel_tis
= xrenew(struct track_info
*, sel_tis
, sel_tis_alloc
);
929 sel_tis
[sel_tis_nr
++] = ti
;
933 static void cmd_run(char *arg
)
936 int ac
, argc
, i
, run
, files_idx
= -1;
938 if (cur_view
> QUEUE_VIEW
) {
939 info_msg("Command execution is supported only in views 1-4");
943 av
= parse_cmd(arg
, &files_idx
, &ac
);
948 /* collect selected files (struct track_info) */
956 __tree_for_each_sel(add_ti
, NULL
, 0);
959 __editable_for_each_sel(&lib_editable
, add_ti
, NULL
, 0);
962 __editable_for_each_sel(&pl_editable
, add_ti
, NULL
, 0);
965 __editable_for_each_sel(&pq_editable
, add_ti
, NULL
, 0);
970 if (sel_tis_nr
== 0) {
971 /* no files selected, do nothing */
975 sel_tis
[sel_tis_nr
] = NULL
;
978 argv
= xnew(char *, ac
+ sel_tis_nr
+ 1);
980 if (files_idx
== -1) {
981 /* add selected files after rest of the args */
982 for (i
= 0; i
< ac
; i
++)
983 argv
[argc
++] = av
[i
];
984 for (i
= 0; i
< sel_tis_nr
; i
++)
985 argv
[argc
++] = sel_tis
[i
]->filename
;
987 for (i
= 0; i
< files_idx
; i
++)
988 argv
[argc
++] = av
[i
];
989 for (i
= 0; i
< sel_tis_nr
; i
++)
990 argv
[argc
++] = sel_tis
[i
]->filename
;
991 for (i
= files_idx
; i
< ac
; i
++)
992 argv
[argc
++] = av
[i
];
996 for (i
= 0; argv
[i
]; i
++)
997 d_print("ARG: '%s'\n", argv
[i
]);
1000 if (confirm_run
&& (sel_tis_nr
> 1 || strcmp(argv
[0], "rm") == 0)) {
1001 if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg
, sel_tis_nr
)) {
1002 info_msg("Aborted");
1009 if (spawn(argv
, &status
)) {
1010 error_msg("executing %s: %s", argv
[0], strerror(errno
));
1012 if (WIFEXITED(status
)) {
1013 int rc
= WEXITSTATUS(status
);
1016 error_msg("%s returned %d", argv
[0], rc
);
1018 if (WIFSIGNALED(status
))
1019 error_msg("%s received signal %d", argv
[0], WTERMSIG(status
));
1024 /* this must be done before sel_tis are unreffed */
1028 /* remove non-existed files, update tags for changed files */
1029 cmus_update_tis(sel_tis
, sel_tis_nr
);
1031 /* we don't own sel_tis anymore! */
1038 for (i
= 0; sel_tis
[i
]; i
++)
1039 track_info_unref(sel_tis
[i
]);
1043 static int get_one_ti(void *data
, struct track_info
*ti
)
1045 struct track_info
**sel_ti
= data
;
1049 /* stop the for each loop, we need only the first selected track */
1053 static void cmd_echo(char *arg
)
1055 struct track_info
*sel_ti
;
1059 ptr
= strchr(ptr
, '{');
1068 info_msg("%s", arg
);
1072 if (cur_view
> QUEUE_VIEW
) {
1073 info_msg("echo with {} in its arguments is supported only in views 1-4");
1080 /* get only the first selected track */
1086 __tree_for_each_sel(get_one_ti
, &sel_ti
, 0);
1089 __editable_for_each_sel(&lib_editable
, get_one_ti
, &sel_ti
, 0);
1092 __editable_for_each_sel(&pl_editable
, get_one_ti
, &sel_ti
, 0);
1095 __editable_for_each_sel(&pq_editable
, get_one_ti
, &sel_ti
, 0);
1103 info_msg("%s%s%s", arg
, sel_ti
->filename
, ptr
);
1104 track_info_unref(sel_ti
);
1107 #define VF_RELATIVE 0x01
1108 #define VF_PERCENTAGE 0x02
1110 static int parse_vol_arg(const char *arg
, int *value
, unsigned int *flags
)
1113 int ch
, val
= 0, digits
= 0, sign
= 1;
1119 } else if (*arg
== '+') {
1126 if (ch
< '0' || ch
> '9')
1142 *value
= sign
* val
;
1149 static int calc_vol(int val
, int old
, int max
, unsigned int flags
)
1151 if (flags
& VF_RELATIVE
) {
1152 if (flags
& VF_PERCENTAGE
)
1153 val
= scale_from_percentage(val
, max
);
1155 } else if (flags
& VF_PERCENTAGE
) {
1156 val
= scale_from_percentage(val
, max
);
1158 return clamp(val
, 0, max
);
1162 * :vol value [value]
1164 * where value is [-+]?[0-9]+%?
1166 static void cmd_vol(char *arg
)
1168 char **values
= get_words(arg
);
1169 unsigned int lf
, rf
;
1172 if (values
[1] && values
[2])
1175 if (parse_vol_arg(values
[0], &l
, &lf
))
1180 if (values
[1] && parse_vol_arg(values
[1], &r
, &rf
))
1183 free_str_array(values
);
1186 l
= calc_vol(l
, soft_vol_l
, 100, lf
);
1187 r
= calc_vol(r
, soft_vol_r
, 100, rf
);
1188 player_set_soft_volume(l
, r
);
1190 mixer_read_volume();
1191 l
= calc_vol(l
, volume_l
, volume_max
, lf
);
1192 r
= calc_vol(r
, volume_r
, volume_max
, rf
);
1193 mixer_set_volume(l
, r
);
1195 update_statusline();
1198 free_str_array(values
);
1199 error_msg("expecting 1 or 2 arguments (total or L and R volumes [+-]INTEGER[%%])\n");
1202 static void cmd_prev_view(char *arg
)
1205 if (prev_view
>= 0) {
1207 set_view(prev_view
);
1212 static void cmd_view(char *arg
)
1216 if (parse_enum(arg
, 1, NR_VIEWS
, view_names
, &view
) && (view
- 1) != cur_view
) {
1217 prev_view
= cur_view
;
1222 static void cmd_p_next(char *arg
)
1227 static void cmd_p_pause(char *arg
)
1232 static void cmd_p_play(char *arg
)
1235 cmus_play_file(arg
);
1241 static void cmd_p_prev(char *arg
)
1246 static void cmd_p_stop(char *arg
)
1251 static void cmd_search_next(char *arg
)
1254 if (!search_next(searchable
, search_str
, search_direction
))
1259 static void cmd_search_prev(char *arg
)
1262 if (!search_next(searchable
, search_str
, !search_direction
))
1267 static int sorted_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1269 return editable_for_each_sel(&lib_editable
, cb
, data
, reverse
);
1272 static int pl_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1274 return editable_for_each_sel(&pl_editable
, cb
, data
, reverse
);
1277 static int pq_for_each_sel(int (*cb
)(void *data
, struct track_info
*ti
), void *data
, int reverse
)
1279 return editable_for_each_sel(&pq_editable
, cb
, data
, reverse
);
1282 static for_each_sel_ti_cb view_for_each_sel
[4] = {
1284 sorted_for_each_sel
,
1289 /* wrapper for void lib_add_track(struct track_info *) etc. */
1290 static int wrapper_cb(void *data
, struct track_info
*ti
)
1292 add_ti_cb add
= data
;
1298 static void add_from_browser(add_ti_cb add
, int job_type
)
1300 char *sel
= browser_get_sel();
1306 ft
= cmus_detect_ft(sel
, &ret
);
1307 if (ft
!= FILE_TYPE_INVALID
) {
1308 cmus_add(add
, ret
, ft
, job_type
);
1309 window_down(browser_win
, 1);
1316 static void cmd_win_add_l(char *arg
)
1318 if (cur_view
== TREE_VIEW
|| cur_view
== SORTED_VIEW
)
1321 if (cur_view
<= QUEUE_VIEW
) {
1323 view_for_each_sel
[cur_view
](wrapper_cb
, lib_add_track
, 0);
1325 } else if (cur_view
== BROWSER_VIEW
) {
1326 add_from_browser(lib_add_track
, JOB_TYPE_LIB
);
1330 static void cmd_win_add_p(char *arg
)
1332 /* could allow adding dups? */
1333 if (cur_view
== PLAYLIST_VIEW
)
1336 if (cur_view
<= QUEUE_VIEW
) {
1338 view_for_each_sel
[cur_view
](wrapper_cb
, pl_add_track
, 0);
1340 } else if (cur_view
== BROWSER_VIEW
) {
1341 add_from_browser(pl_add_track
, JOB_TYPE_PL
);
1345 static void cmd_win_add_Q(char *arg
)
1347 if (cur_view
== QUEUE_VIEW
)
1350 if (cur_view
<= QUEUE_VIEW
) {
1352 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_prepend
, 1);
1354 } else if (cur_view
== BROWSER_VIEW
) {
1355 add_from_browser(play_queue_prepend
, JOB_TYPE_QUEUE
);
1359 static void cmd_win_add_q(char *arg
)
1361 if (cur_view
== QUEUE_VIEW
)
1364 if (cur_view
<= QUEUE_VIEW
) {
1366 view_for_each_sel
[cur_view
](wrapper_cb
, play_queue_append
, 0);
1368 } else if (cur_view
== BROWSER_VIEW
) {
1369 add_from_browser(play_queue_append
, JOB_TYPE_QUEUE
);
1373 static void cmd_win_activate(char *arg
)
1375 struct track_info
*info
= NULL
;
1380 info
= tree_set_selected();
1383 info
= sorted_set_selected();
1386 info
= pl_set_selected();
1403 /* update lib/pl mode */
1409 player_play_file(info
);
1413 static void cmd_win_mv_after(char *arg
)
1420 editable_move_after(&lib_editable
);
1423 editable_move_after(&pl_editable
);
1426 editable_move_after(&pq_editable
);
1438 static void cmd_win_mv_before(char *arg
)
1445 editable_move_before(&lib_editable
);
1448 editable_move_before(&pl_editable
);
1451 editable_move_before(&pq_editable
);
1463 static void cmd_win_remove(char *arg
)
1471 editable_remove_sel(&lib_editable
);
1474 editable_remove_sel(&pl_editable
);
1477 editable_remove_sel(&pq_editable
);
1483 filters_delete_filter();
1492 static void cmd_win_sel_cur(char *arg
)
1500 sorted_sel_current();
1517 static void cmd_win_toggle(char *arg
)
1522 tree_toggle_expand_artist();
1527 editable_toggle_mark(&lib_editable
);
1532 editable_toggle_mark(&pl_editable
);
1537 editable_toggle_mark(&pq_editable
);
1543 filters_toggle_filter();
1551 static struct window
*current_win(void)
1557 return lib_editable
.win
;
1559 return pl_editable
.win
;
1561 return pq_editable
.win
;
1572 static void cmd_win_bottom(char *arg
)
1575 window_goto_bottom(current_win());
1579 static void cmd_win_down(char *arg
)
1582 window_down(current_win(), 1);
1586 static void cmd_win_next(char *arg
)
1588 if (cur_view
== TREE_VIEW
) {
1590 tree_toggle_active_window();
1595 static void cmd_win_pg_down(char *arg
)
1598 window_page_down(current_win());
1602 static void cmd_win_pg_up(char *arg
)
1605 window_page_up(current_win());
1609 static void cmd_win_top(char *arg
)
1612 window_goto_top(current_win());
1616 static void cmd_win_up(char *arg
)
1619 window_up(current_win(), 1);
1623 static void cmd_win_update(char *arg
)
1636 static void cmd_browser_up(char *arg
)
1641 static void cmd_refresh(char *arg
)
1643 clearok(curscr
, TRUE
);
1647 static int cmp_intp(const void *ap
, const void *bp
)
1654 static int *rand_array(int size
, int nmax
)
1656 int *r
= xnew(int, size
+ 1);
1660 if (count
> nmax
/ 2) {
1662 * Imagine that there are 1000 tracks in library and we want to
1663 * add 998 random tracks to queue. After we have added 997
1664 * random numbers to the array it would be quite hard to find a
1665 * random number that isn't already in the array (3/1000
1668 * So we invert the logic:
1670 * Find two (1000 - 998) random numbers in 0..999 range and put
1671 * them at end of the array. Sort the numbers and then fill
1672 * the array starting at index 0 with incrementing values that
1673 * are not in the set of random numbers.
1675 count
= nmax
- count
;
1676 offset
= size
- count
;
1679 for (i
= 0; i
< count
; ) {
1683 for (j
= 0; j
< i
; j
++) {
1684 if (r
[offset
+ j
] == v
)
1687 r
[offset
+ i
++] = v
;
1689 qsort(r
+ offset
, count
, sizeof(*r
), cmp_intp
);
1694 /* simplifies next loop */
1697 /* convert the indexes we don't want to those we want */
1711 static int count_albums(void)
1713 struct artist
*artist
;
1714 struct list_head
*item
;
1717 list_for_each_entry(artist
, &lib_artist_head
, node
) {
1718 list_for_each(item
, &artist
->album_head
)
1725 struct list_head node
;
1726 const struct album
*album
;
1729 static void cmd_lqueue(char *arg
)
1732 const struct list_head
*item
;
1733 const struct album
*album
;
1734 int count
= 1, nmax
, i
, pos
;
1740 if (str_to_int(arg
, &val
) || val
<= 0) {
1741 error_msg("argument must be positive integer");
1747 nmax
= count_albums();
1753 r
= rand_array(count
, nmax
);
1754 album
= to_album(to_artist(lib_artist_head
.next
)->album_head
.next
);
1756 for (i
= 0; i
< count
; i
++) {
1757 struct album_list
*a
;
1759 while (pos
< r
[i
]) {
1760 struct artist
*artist
= album
->artist
;
1761 if (album
->node
.next
== &artist
->album_head
) {
1762 artist
= to_artist(artist
->node
.next
);
1763 album
= to_album(artist
->album_head
.next
);
1765 album
= to_album(album
->node
.next
);
1769 a
= xnew(struct album_list
, 1);
1771 list_add_rand(&head
, &a
->node
, i
);
1777 struct list_head
*next
= item
->next
;
1778 struct album_list
*a
= container_of(item
, struct album_list
, node
);
1779 struct tree_track
*t
;
1781 list_for_each_entry(t
, &a
->album
->track_head
, node
)
1782 editable_add(&pq_editable
, simple_track_new(tree_track_info(t
)));
1785 } while (item
!= &head
);
1790 static void cmd_tqueue(char *arg
)
1793 struct list_head
*item
;
1794 int count
= 1, i
, pos
;
1800 if (str_to_int(arg
, &val
) || val
<= 0) {
1801 error_msg("argument must be positive integer");
1807 if (count
> lib_editable
.nr_tracks
)
1808 count
= lib_editable
.nr_tracks
;
1812 r
= rand_array(count
, lib_editable
.nr_tracks
);
1813 item
= lib_editable
.head
.next
;
1815 for (i
= 0; i
< count
; i
++) {
1816 struct simple_track
*t
;
1818 while (pos
< r
[i
]) {
1822 t
= simple_track_new(to_simple_track(item
)->info
);
1823 list_add_rand(&head
, &t
->node
, i
);
1829 struct list_head
*next
= item
->next
;
1830 struct simple_track
*t
= to_simple_track(item
);
1831 editable_add(&pq_editable
, t
);
1833 } while (item
!= &head
);
1840 * these functions fill tabexp struct, which is resetted beforehand
1843 /* buffer used for tab expansion */
1844 static char expbuf
[512];
1846 static int filter_directories(const char *name
, const struct stat
*s
)
1848 return S_ISDIR(s
->st_mode
);
1851 static int filter_any(const char *name
, const struct stat
*s
)
1856 static int filter_playable(const char *name
, const struct stat
*s
)
1858 return S_ISDIR(s
->st_mode
) || cmus_is_playable(name
);
1861 static int filter_playlist(const char *name
, const struct stat
*s
)
1863 return S_ISDIR(s
->st_mode
) || cmus_is_playlist(name
);
1866 static int filter_supported(const char *name
, const struct stat
*s
)
1868 return S_ISDIR(s
->st_mode
) || cmus_is_supported(name
);
1871 static void expand_files(const char *str
)
1873 expand_files_and_dirs(str
, filter_any
);
1876 static void expand_directories(const char *str
)
1878 expand_files_and_dirs(str
, filter_directories
);
1881 static void expand_playable(const char *str
)
1883 expand_files_and_dirs(str
, filter_playable
);
1886 static void expand_playlist(const char *str
)
1888 expand_files_and_dirs(str
, filter_playlist
);
1891 static void expand_supported(const char *str
)
1893 expand_files_and_dirs(str
, filter_supported
);
1896 static void expand_add(const char *str
)
1898 int flag
= parse_flags(&str
, "lpqQ");
1904 expand_supported(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_load_save(const char *str
)
1915 int flag
= parse_flags(&str
, "lp");
1921 expand_playlist(str
);
1923 if (tabexp
.head
&& flag
) {
1924 snprintf(expbuf
, sizeof(expbuf
), "-%c %s", flag
, tabexp
.head
);
1926 tabexp
.head
= xstrdup(expbuf
);
1930 static void expand_key_context(const char *str
, const char *force
)
1932 int pos
, i
, len
= strlen(str
);
1935 tails
= xnew(char *, NR_CTXS
+ 1);
1937 for (i
= 0; key_context_names
[i
]; i
++) {
1938 int cmp
= strncmp(str
, key_context_names
[i
], len
);
1943 tails
[pos
++] = xstrdup(key_context_names
[i
] + len
);
1951 char *tmp
= xstrjoin(tails
[0], " ");
1956 snprintf(expbuf
, sizeof(expbuf
), "%s%s", force
, str
);
1957 tabexp
.head
= xstrdup(expbuf
);
1958 tabexp
.tails
= tails
;
1961 static int get_context(const char *str
, int len
)
1963 int i
, c
= -1, count
= 0;
1965 for (i
= 0; key_context_names
[i
]; i
++) {
1966 if (strncmp(str
, key_context_names
[i
], len
) == 0) {
1967 if (key_context_names
[i
][len
] == 0) {
1980 static void expand_command_line(const char *str
);
1982 static void expand_bind_args(const char *str
)
1984 /* :bind context key function
1986 * possible values for str:
1991 * you need to know context before you can expand function
1993 /* start and end pointers for context, key and function */
1994 const char *cs
, *ce
, *ks
, *ke
, *fs
;
1996 int flag
= parse_flags((const char **)&str
, "f");
1997 const char *force
= "";
2008 ce
= strchr(cs
, ' ');
2010 expand_key_context(cs
, force
);
2014 /* context must be expandable */
2015 c
= get_context(cs
, ce
- cs
);
2017 /* context is ambiguous or invalid */
2024 ke
= strchr(ks
, ' ');
2027 int len
= strlen(ks
);
2030 for (i
= 0; key_table
[i
].name
; i
++) {
2031 int cmp
= strncmp(ks
, key_table
[i
].name
, len
);
2036 ptr_array_add(&array
, xstrdup(key_table
[i
].name
+ len
));
2042 if (array
.count
== 1) {
2043 char **ptrs
= array
.ptrs
;
2044 char *tmp
= xstrjoin(ptrs
[0], " ");
2049 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s", force
, key_context_names
[c
], ks
);
2051 ptr_array_plug(&array
);
2052 tabexp
.head
= xstrdup(expbuf
);
2053 tabexp
.tails
= array
.ptrs
;
2057 /* key must be expandable */
2060 for (i
= 0; key_table
[i
].name
; i
++) {
2061 if (strncmp(ks
, key_table
[i
].name
, ke
- ks
) == 0) {
2062 if (key_table
[i
].name
[ke
- ks
] == 0) {
2073 /* key is ambiguous or invalid */
2084 /* expand com [arg...] */
2085 expand_command_line(fs
);
2086 if (tabexp
.head
== NULL
) {
2087 /* command expand failed */
2092 * tabexp.head is now "com"
2093 * tabexp.tails is [ mand1 mand2 ... ]
2095 * need to change tabexp.head to "context key com"
2098 snprintf(expbuf
, sizeof(expbuf
), "%s%s %s %s", force
, key_context_names
[c
],
2099 key_table
[k
].name
, tabexp
.head
);
2101 tabexp
.head
= xstrdup(expbuf
);
2104 static void expand_unbind_args(const char *str
)
2106 /* :unbind context key */
2107 /* start and end pointers for context and key */
2108 const char *cs
, *ce
, *ks
;
2109 const struct binding
*b
;
2114 ce
= strchr(cs
, ' ');
2116 expand_key_context(cs
, "");
2120 /* context must be expandable */
2121 c
= get_context(cs
, ce
- cs
);
2123 /* context is ambiguous or invalid */
2133 b
= key_bindings
[c
];
2135 if (!strncmp(ks
, b
->key
->name
, len
))
2136 ptr_array_add(&array
, xstrdup(b
->key
->name
+ len
));
2142 snprintf(expbuf
, sizeof(expbuf
), "%s %s", key_context_names
[c
], ks
);
2144 ptr_array_plug(&array
);
2145 tabexp
.head
= xstrdup(expbuf
);
2146 tabexp
.tails
= array
.ptrs
;
2149 static void expand_factivate(const char *str
)
2151 /* "name1 name2 name3", expand only name3 */
2152 struct filter_entry
*e
;
2155 int str_len
, len
, i
;
2157 str_len
= strlen(str
);
2160 if (str
[i
- 1] == ' ')
2167 list_for_each_entry(e
, &filters_head
, node
) {
2168 if (!strncmp(name
, e
->name
, len
))
2169 ptr_array_add(&array
, xstrdup(e
->name
+ len
));
2174 ptr_array_plug(&array
);
2175 tabexp
.head
= xstrdup(str
);
2176 tabexp
.tails
= array
.ptrs
;
2179 static void expand_options(const char *str
)
2181 struct cmus_opt
*opt
;
2185 /* tabexp is resetted */
2187 if (len
> 1 && str
[len
- 1] == '=') {
2189 char *var
= xstrndup(str
, len
- 1);
2191 list_for_each_entry(opt
, &option_head
, node
) {
2192 if (strcmp(var
, opt
->name
) == 0) {
2193 char buf
[OPTION_MAX_SIZE
];
2195 tails
= xnew(char *, 2);
2198 opt
->get(opt
->id
, buf
);
2199 tails
[0] = xstrdup(buf
);
2202 tabexp
.head
= xstrdup(str
);
2203 tabexp
.tails
= tails
;
2210 /* expand variable */
2213 tails
= xnew(char *, nr_options
+ 1);
2215 list_for_each_entry(opt
, &option_head
, node
) {
2216 if (strncmp(str
, opt
->name
, len
) == 0)
2217 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2221 /* only one variable matches, add '=' */
2222 char *tmp
= xstrjoin(tails
[0], "=");
2229 tabexp
.head
= xstrdup(str
);
2230 tabexp
.tails
= tails
;
2237 static void expand_toptions(const char *str
)
2239 struct cmus_opt
*opt
;
2243 tails
= xnew(char *, nr_options
+ 1);
2246 list_for_each_entry(opt
, &option_head
, node
) {
2247 if (opt
->toggle
== NULL
)
2249 if (strncmp(str
, opt
->name
, len
) == 0)
2250 tails
[pos
++] = xstrdup(opt
->name
+ len
);
2254 tabexp
.head
= xstrdup(str
);
2255 tabexp
.tails
= tails
;
2261 static void load_themes(const char *dirname
, const char *str
, struct ptr_array
*array
)
2263 struct directory dir
;
2264 const char *name
, *dot
;
2265 int len
= strlen(str
);
2267 if (dir_open(&dir
, dirname
))
2270 while ((name
= dir_read(&dir
))) {
2271 if (!S_ISREG(dir
.st
.st_mode
))
2273 if (strncmp(name
, str
, len
))
2275 dot
= strrchr(name
, '.');
2276 if (dot
== NULL
|| strcmp(dot
, ".theme"))
2278 if (dot
- name
< len
)
2280 * matches "foo.theme"
2281 * which also ends with ".theme"
2284 ptr_array_add(array
, xstrndup(name
+ len
, dot
- name
- len
));
2289 static void expand_colorscheme(const char *str
)
2293 load_themes(cmus_config_dir
, str
, &array
);
2294 load_themes(DATADIR
"/cmus", str
, &array
);
2297 ptr_array_sort(&array
, strptrcmp
);
2299 ptr_array_plug(&array
);
2300 tabexp
.head
= xstrdup(str
);
2301 tabexp
.tails
= array
.ptrs
;
2308 struct command commands
[] = {
2309 { "add", cmd_add
, 1, 1, expand_add
, 0, 0 },
2310 { "bind", cmd_bind
, 1, 1, expand_bind_args
, 0, CMD_UNSAFE
},
2311 { "browser-up", cmd_browser_up
, 0, 0, NULL
, 0, 0 },
2312 { "cd", cmd_cd
, 0, 1, expand_directories
, 0, 0 },
2313 { "clear", cmd_clear
, 0, 1, NULL
, 0, 0 },
2314 { "colorscheme", cmd_colorscheme
,1, 1, expand_colorscheme
, 0, 0 },
2315 { "echo", cmd_echo
, 1,-1, NULL
, 0, 0 },
2316 { "factivate", cmd_factivate
, 0, 1, expand_factivate
, 0, 0 },
2317 { "filter", cmd_filter
, 0, 1, NULL
, 0, 0 },
2318 { "fset", cmd_fset
, 1, 1, NULL
, 0, 0 },
2319 { "invert", cmd_invert
, 0, 0, NULL
, 0, 0 },
2320 { "load", cmd_load
, 1, 1, expand_load_save
, 0, 0 },
2321 { "lqueue", cmd_lqueue
, 0, 1, NULL
, 0, 0 },
2322 { "mark", cmd_mark
, 0, 1, NULL
, 0, 0 },
2323 { "player-next", cmd_p_next
, 0, 0, NULL
, 0, 0 },
2324 { "player-pause", cmd_p_pause
, 0, 0, NULL
, 0, 0 },
2325 { "player-play", cmd_p_play
, 0, 1, expand_playable
, 0, 0 },
2326 { "player-prev", cmd_p_prev
, 0, 0, NULL
, 0, 0 },
2327 { "player-stop", cmd_p_stop
, 0, 0, NULL
, 0, 0 },
2328 { "prev-view", cmd_prev_view
, 0, 0, NULL
, 0, 0 },
2329 { "quit", cmd_quit
, 0, 0, NULL
, 0, 0 },
2330 { "refresh", cmd_refresh
, 0, 0, NULL
, 0, 0 },
2331 { "run", cmd_run
, 1,-1, NULL
, 0, CMD_UNSAFE
},
2332 { "save", cmd_save
, 0, 1, expand_load_save
, 0, CMD_UNSAFE
},
2333 { "search-next", cmd_search_next
,0, 0, NULL
, 0, 0 },
2334 { "search-prev", cmd_search_prev
,0, 0, NULL
, 0, 0 },
2335 { "seek", cmd_seek
, 1, 1, NULL
, 0, 0 },
2336 { "set", cmd_set
, 1, 1, expand_options
, 0, 0 },
2337 { "showbind", cmd_showbind
, 1, 1, expand_unbind_args
, 0, 0 },
2338 { "shuffle", cmd_reshuffle
, 0, 0, NULL
, 0, 0 },
2339 { "source", cmd_source
, 1, 1, expand_files
, 0, CMD_UNSAFE
},
2340 { "toggle", cmd_toggle
, 1, 1, expand_toptions
, 0, 0 },
2341 { "tqueue", cmd_tqueue
, 0, 1, NULL
, 0, 0 },
2342 { "unbind", cmd_unbind
, 1, 1, expand_unbind_args
, 0, 0 },
2343 { "unmark", cmd_unmark
, 0, 0, NULL
, 0, 0 },
2344 { "view", cmd_view
, 1, 1, NULL
, 0, 0 },
2345 { "vol", cmd_vol
, 1, 2, NULL
, 0, 0 },
2346 { "win-activate", cmd_win_activate
,0, 0, NULL
, 0, 0 },
2347 { "win-add-l", cmd_win_add_l
, 0, 0, NULL
, 0, 0 },
2348 { "win-add-p", cmd_win_add_p
, 0, 0, NULL
, 0, 0 },
2349 { "win-add-Q", cmd_win_add_Q
, 0, 0, NULL
, 0, 0 },
2350 { "win-add-q", cmd_win_add_q
, 0, 0, NULL
, 0, 0 },
2351 { "win-bottom", cmd_win_bottom
, 0, 0, NULL
, 0, 0 },
2352 { "win-down", cmd_win_down
, 0, 0, NULL
, 0, 0 },
2353 { "win-mv-after", cmd_win_mv_after
,0, 0, NULL
, 0, 0 },
2354 { "win-mv-before", cmd_win_mv_before
,0, 0, NULL
, 0, 0 },
2355 { "win-next", cmd_win_next
, 0, 0, NULL
, 0, 0 },
2356 { "win-page-down", cmd_win_pg_down
,0, 0, NULL
, 0, 0 },
2357 { "win-page-up", cmd_win_pg_up
, 0, 0, NULL
, 0, 0 },
2358 { "win-remove", cmd_win_remove
, 0, 0, NULL
, 0, CMD_UNSAFE
},
2359 { "win-sel-cur", cmd_win_sel_cur
,0, 0, NULL
, 0, 0 },
2360 { "win-toggle", cmd_win_toggle
, 0, 0, NULL
, 0, 0 },
2361 { "win-top", cmd_win_top
, 0, 0, NULL
, 0, 0 },
2362 { "win-up", cmd_win_up
, 0, 0, NULL
, 0, 0 },
2363 { "win-update", cmd_win_update
, 0, 0, NULL
, 0, 0 },
2364 { NULL
, NULL
, 0, 0, 0, 0, 0 }
2367 /* fills tabexp struct */
2368 static void expand_commands(const char *str
)
2373 /* tabexp is resetted */
2374 tails
= xnew(char *, sizeof(commands
) / sizeof(struct command
));
2377 for (i
= 0; commands
[i
].name
; i
++) {
2378 if (strncmp(str
, commands
[i
].name
, len
) == 0)
2379 tails
[pos
++] = xstrdup(commands
[i
].name
+ len
);
2383 /* only one command matches, add ' ' */
2384 char *tmp
= xstrjoin(tails
[0], " ");
2390 tabexp
.head
= xstrdup(str
);
2391 tabexp
.tails
= tails
;
2397 struct command
*get_command(const char *str
)
2403 for (len
= 0; str
[len
] && str
[len
] != ' '; len
++)
2406 for (i
= 0; commands
[i
].name
; i
++) {
2407 if (strncmp(str
, commands
[i
].name
, len
))
2410 if (commands
[i
].name
[len
] == 0) {
2412 return &commands
[i
];
2415 if (commands
[i
+ 1].name
&& strncmp(str
, commands
[i
+ 1].name
, len
) == 0) {
2419 return &commands
[i
];
2424 /* fills tabexp struct */
2425 static void expand_command_line(const char *str
)
2427 /* :command [arg]...
2431 * str expanded value (tabexp.head)
2432 * -------------------------------------
2435 * se se (tabexp.tails = [ ek t ])
2437 /* command start/end, argument start */
2438 const char *cs
, *ce
, *as
;
2439 const struct command
*cmd
;
2442 ce
= strchr(cs
, ' ');
2444 /* expand command */
2445 expand_commands(cs
);
2449 /* command must be expandable */
2450 cmd
= get_command(cs
);
2452 /* command ambiguous or invalid */
2456 if (cmd
->expand
== NULL
) {
2457 /* can't expand argument */
2465 /* expand argument */
2467 if (tabexp
.head
== NULL
) {
2468 /* argument expansion failed */
2472 /* tabexp.head is now start of the argument string */
2473 snprintf(expbuf
, sizeof(expbuf
), "%s %s", cmd
->name
, tabexp
.head
);
2475 tabexp
.head
= xstrdup(expbuf
);
2478 static void tab_expand(void)
2480 char *s1
, *s2
, *tmp
;
2483 /* strip white space */
2485 while (cmdline
.line
[pos
] == ' ' && pos
< cmdline
.bpos
)
2488 /* string to expand */
2489 s1
= xstrndup(cmdline
.line
+ pos
, cmdline
.bpos
- pos
);
2492 s2
= xstrdup(cmdline
.line
+ cmdline
.bpos
);
2494 tmp
= tabexp_expand(s1
, expand_command_line
);
2501 cmdline
.blen
= l1
+ l2
;
2502 if (cmdline
.blen
>= cmdline
.size
) {
2503 while (cmdline
.blen
>= cmdline
.size
)
2505 cmdline
.line
= xrenew(char, cmdline
.line
, cmdline
.size
);
2507 sprintf(cmdline
.line
, "%s%s", tmp
, s2
);
2509 cmdline
.cpos
= u_strlen(tmp
);
2510 cmdline
.clen
= u_strlen(cmdline
.line
);
2517 static void reset_tab_expansion(void)
2520 arg_expand_cmd
= -1;
2523 int run_only_safe_commands
;
2525 /* FIXME: parse all arguments */
2526 void run_command(const char *buf
)
2529 int cmd_start
, cmd_end
, cmd_len
;
2530 int arg_start
, arg_end
;
2534 while (buf
[i
] && buf
[i
] == ' ')
2541 while (buf
[i
] && buf
[i
] != ' ')
2544 while (buf
[i
] && buf
[i
] == ' ')
2551 cmd_len
= cmd_end
- cmd_start
;
2555 cmd
= xstrndup(buf
+ cmd_start
, cmd_len
);
2556 if (arg_start
== arg_end
) {
2559 arg
= xstrndup(buf
+ arg_start
, arg_end
- arg_start
);
2563 const struct command
*c
= &commands
[i
];
2565 if (c
->name
== NULL
) {
2566 error_msg("unknown command\n");
2569 if (strncmp(cmd
, c
->name
, cmd_len
) == 0) {
2570 const char *next
= commands
[i
+ 1].name
;
2571 int exact
= c
->name
[cmd_len
] == 0;
2573 if (!exact
&& next
&& strncmp(cmd
, next
, cmd_end
- cmd_start
) == 0) {
2574 error_msg("ambiguous command\n");
2577 if (c
->min_args
> 0 && arg
== NULL
) {
2578 error_msg("not enough arguments\n");
2581 if (c
->max_args
== 0 && arg
) {
2582 error_msg("too many arguments\n");
2585 if (run_only_safe_commands
&& c
->flags
& CMD_UNSAFE
) {
2586 d_print("trying to execute unsafe command over net\n");
2598 static void reset_history_search(void)
2600 history_reset_search(&cmd_history
);
2601 free(history_search_text
);
2602 history_search_text
= NULL
;
2605 static void backspace(void)
2607 if (cmdline
.clen
> 0) {
2608 cmdline_backspace();
2610 input_mode
= NORMAL_MODE
;
2614 void command_mode_ch(uchar ch
)
2618 cmdline_move_home();
2621 cmdline_move_left();
2624 cmdline_delete_ch();
2630 cmdline_move_right();
2636 history_add_line(&cmd_history
, cmdline
.line
);
2639 input_mode
= NORMAL_MODE
;
2643 run_command(cmdline
.line
);
2644 history_add_line(&cmd_history
, cmdline
.line
);
2647 input_mode
= NORMAL_MODE
;
2650 cmdline_clear_end();
2656 cmdline_backspace_to_bol();
2662 cmdline_insert_ch(ch
);
2664 reset_history_search();
2666 reset_tab_expansion();
2669 void command_mode_key(int key
)
2671 reset_tab_expansion();
2674 cmdline_delete_ch();
2680 cmdline_move_left();
2683 cmdline_move_right();
2686 cmdline_move_home();
2695 if (history_search_text
== NULL
)
2696 history_search_text
= xstrdup(cmdline
.line
);
2697 s
= history_search_forward(&cmd_history
, history_search_text
);
2699 cmdline_set_text(s
);
2703 if (history_search_text
) {
2706 s
= history_search_backward(&cmd_history
, history_search_text
);
2708 cmdline_set_text(s
);
2710 cmdline_set_text(history_search_text
);
2715 d_print("key = %c (%d)\n", key
, key
);
2717 reset_history_search();
2720 void commands_init(void)
2722 cmd_history_filename
= xstrjoin(cmus_config_dir
, "/command-history");
2723 history_load(&cmd_history
, cmd_history_filename
, 2000);
2726 void commands_exit(void)
2728 history_save(&cmd_history
);
2729 free(cmd_history_filename
);