Add explanatory comments to the #endif part of multiple inclusion guards.
[mplayer/greg.git] / libmenu / menu_filesel.c
blobba8ceae550a259683ab50c8a9ad3b88505d9f80e
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <dirent.h>
5 #include <errno.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <ctype.h>
11 #include <unistd.h>
12 #include <limits.h>
15 #include "config.h"
16 #include "mp_msg.h"
17 #include "help_mp.h"
19 #include "m_struct.h"
20 #include "m_option.h"
22 #include "libmpcodecs/img_format.h"
23 #include "libmpcodecs/mp_image.h"
25 #include "menu.h"
26 #include "menu_list.h"
27 #include "input/input.h"
28 #include "osdep/keycodes.h"
30 #define MENU_KEEP_PATH "/tmp/mp_current_path"
32 int menu_keepdir = 0;
33 char *menu_chroot = NULL;
34 extern char *filename;
36 struct list_entry_s {
37 struct list_entry p;
38 int d;
41 struct menu_priv_s {
42 menu_list_priv_t p;
43 char* dir; // current dir
44 /// Cfg fields
45 char* path;
46 char* title;
47 char* file_action;
48 char* dir_action;
49 char** actions;
50 char* filter;
53 static struct menu_priv_s cfg_dflt = {
54 MENU_LIST_PRIV_DFLT,
55 NULL,
57 NULL,
58 "Select a file: %p",
59 "loadfile '%p'",
60 NULL,
61 NULL,
62 NULL
65 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m)
67 static m_option_t cfg_fields[] = {
68 MENU_LIST_PRIV_FIELDS,
69 { "path", ST_OFF(path), CONF_TYPE_STRING, 0, 0, 0, NULL },
70 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL },
71 { "file-action", ST_OFF(file_action), CONF_TYPE_STRING, 0, 0, 0, NULL },
72 { "dir-action", ST_OFF(dir_action), CONF_TYPE_STRING, 0, 0, 0, NULL },
73 { "actions", ST_OFF(actions), CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
74 { "filter", ST_OFF(filter), CONF_TYPE_STRING, 0, 0, 0, NULL},
75 { NULL, NULL, NULL, 0,0,0,NULL }
78 #define mpriv (menu->priv)
80 static void free_entry(list_entry_t* entry) {
81 free(entry->p.txt);
82 free(entry);
85 static char* replace_path(char* title , char* dir) {
86 char *p = strstr(title,"%p");
87 if(p) {
88 int tl = strlen(title);
89 int dl = strlen(dir);
90 int t1l = p-title;
91 int l = tl - 2 + dl;
92 char *r, *n, *d = dir;
93 char term = *(p-1);
95 do {
96 if (*d == '\\' || *d == term)
97 l++;
98 } while (*d++);
99 r = malloc(l + 1);
100 n = r + t1l;
101 memcpy(r,title,t1l);
102 do {
103 if (*dir == '\\' || *dir == term)
104 *n++ = '\\';
105 } while ((*n++ = *dir++));
106 if(tl - t1l - 2 > 0)
107 strcpy(n-1,p+2);
108 return r;
109 } else
110 return title;
113 typedef int (*kill_warn)(const void*, const void*);
115 static int mylstat(char *dir, char *file,struct stat* st) {
116 int l = strlen(dir) + strlen(file);
117 char s[l+2];
118 if (!strcmp("..", file)) {
119 char *slash;
120 l -= 3;
121 strcpy(s, dir);
122 #if defined(__MINGW32__) || defined(__CYGWIN__)
123 if (s[l] == '/' || s[l] == '\\')
124 #else
125 if (s[l] == '/')
126 #endif
127 s[l] = '\0';
128 slash = strrchr(s, '/');
129 #if defined(__MINGW32__) || defined(__CYGWIN__)
130 if (!slash)
131 slash = strrchr(s,'\\');
132 #endif
133 if (!slash)
134 return stat(dir,st);
135 slash[1] = '\0';
136 return stat(s,st);
138 sprintf(s,"%s/%s",dir,file);
139 return stat(s,st);
142 static int compare(char **a, char **b){
143 if((*a)[strlen(*a) - 1] == '/') {
144 if((*b)[strlen(*b) - 1] == '/')
145 return strcmp(*b, *a) ;
146 else
147 return 1;
148 } else {
149 if((*b)[strlen(*b) - 1] == '/')
150 return -1;
151 else
152 return strcmp(*b, *a);
156 static char **get_extensions(menu_t *menu){
157 char **extensions, ext[32];
158 FILE *fp;
159 int n = 1;
161 if (!mpriv->filter)
162 return NULL;
164 fp = fopen(mpriv->filter, "r");
165 if(!fp)
166 return NULL;
168 extensions = (char **) malloc(sizeof(*extensions));
169 *extensions = NULL;
171 while(fgets(ext,sizeof(ext),fp)) {
172 char **l, *e;
173 int s = strlen (ext);
175 if(ext[s-1] == '\n') {
176 ext[s-1] = '\0';
177 s--;
179 e = (char *) malloc(s+1);
180 extensions = (char **) realloc(extensions, ++n * sizeof(*extensions));
181 extensions = (char **) realloc(extensions, ++n * sizeof(*extensions));
182 strcpy (e, ext);
183 for (l=extensions; *l; l++);
184 *l++ = e;
185 *l = NULL;
188 fclose (fp);
189 return extensions;
192 static void free_extensions(char **extensions){
193 if (extensions) {
194 char **l = extensions;
195 while (*l)
196 free (*l++);
197 free (extensions);
201 static int open_dir(menu_t* menu,char* args) {
202 char **namelist, **tp;
203 struct dirent *dp;
204 struct stat st;
205 int n;
206 int path_fp;
207 char* p = NULL;
208 list_entry_t* e;
209 DIR* dirp;
210 extern int file_filter;
211 char **extensions, **elem, *ext;
213 menu_list_init(menu);
215 if(mpriv->dir)
216 free(mpriv->dir);
217 mpriv->dir = strdup(args);
218 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title)
219 free(mpriv->p.title);
220 p = strstr(mpriv->title,"%p");
222 mpriv->p.title = replace_path(mpriv->title,mpriv->dir);
224 if ((dirp = opendir (mpriv->dir)) == NULL){
225 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_OpendirError, strerror(errno));
226 return 0;
229 if (menu_keepdir) {
230 path_fp = open (MENU_KEEP_PATH, O_CREAT | O_WRONLY | O_TRUNC, 0666);
231 if (path_fp >= 0) {
232 write (path_fp, mpriv->dir, strlen (mpriv->dir));
233 close (path_fp);
237 namelist = (char **) malloc(sizeof(char *));
238 extensions = get_extensions(menu);
240 n=0;
241 while ((dp = readdir(dirp)) != NULL) {
242 if(dp->d_name[0] == '.' && strcmp(dp->d_name,"..") != 0)
243 continue;
244 if (menu_chroot && !strcmp (dp->d_name,"..")) {
245 int len = strlen (menu_chroot);
246 if ((strlen (mpriv->dir) == len || strlen (mpriv->dir) == len + 1)
247 && !strncmp (mpriv->dir, menu_chroot, len))
248 continue;
250 if (mylstat(args,dp->d_name,&st))
251 continue;
252 if (file_filter && extensions && !S_ISDIR(st.st_mode)) {
253 if((ext = strrchr(dp->d_name,'.')) == NULL)
254 continue;
255 ext++;
256 elem = extensions;
257 do {
258 if (!strcasecmp(ext, *elem))
259 break;
260 } while (*++elem);
261 if (*elem == NULL)
262 continue;
264 if(n%20 == 0){ // Get some more mem
265 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *)))
266 == NULL) {
267 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReallocError, strerror(errno));
268 n--;
269 goto bailout;
271 namelist=tp;
274 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2);
275 if(namelist[n] == NULL){
276 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_MallocError, strerror(errno));
277 n--;
278 goto bailout;
281 strcpy(namelist[n], dp->d_name);
282 if(S_ISDIR(st.st_mode))
283 strcat(namelist[n], "/");
284 n++;
287 bailout:
288 free_extensions (extensions);
289 closedir(dirp);
291 qsort(namelist, n, sizeof(char *), (kill_warn)compare);
293 if (n < 0) {
294 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_ReaddirError,strerror(errno));
295 return 0;
297 while(n--) {
298 if((e = calloc(1,sizeof(list_entry_t))) != NULL){
299 e->p.next = NULL;
300 e->p.txt = strdup(namelist[n]);
301 if(strchr(namelist[n], '/') != NULL)
302 e->d = 1;
303 menu_list_add_entry(menu,e);
304 }else{
305 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_MallocError, strerror(errno));
307 free(namelist[n]);
309 free(namelist);
311 return 1;
314 static char *action;
316 static void read_cmd(menu_t* menu,int cmd) {
317 switch(cmd) {
318 case MENU_CMD_LEFT:
319 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../
320 case MENU_CMD_RIGHT:
321 case MENU_CMD_OK: {
322 // Directory
323 if(mpriv->p.current->d && !mpriv->dir_action) {
324 // Default action : open this dirctory ourself
325 int l = strlen(mpriv->dir);
326 char *slash = NULL, *p = NULL;
327 if(strcmp(mpriv->p.current->p.txt,"../") == 0) {
328 if(l <= 1) break;
329 mpriv->dir[l-1] = '\0';
330 slash = strrchr(mpriv->dir,'/');
331 #if defined(__MINGW32__) || defined(__CYGWIN__)
332 if (!slash)
333 slash = strrchr(mpriv->dir,'\\');
334 #endif
335 if(!slash) break;
336 slash[1] = '\0';
337 p = strdup(mpriv->dir);
338 } else {
339 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1);
340 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
342 menu_list_uninit(menu,free_entry);
343 if(!open_dir(menu,p)) {
344 mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_CantOpenDirectory,p);
345 menu->cl = 1;
347 free(p);
348 } else { // File and directory dealt with action string.
349 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
350 char filename[fname_len];
351 char *str;
352 char *action = mpriv->p.current->d ? mpriv->dir_action:mpriv->file_action;
353 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
354 str = replace_path(action, filename);
355 mp_input_parse_and_queue_cmds(str);
356 if (str != action)
357 free(str);
359 } break;
360 case MENU_CMD_ACTION: {
361 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
362 char filename[fname_len];
363 char *str;
364 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
365 str = replace_path(action, filename);
366 mp_input_parse_and_queue_cmds(str);
367 if(str != action)
368 free(str);
369 } break;
370 default:
371 menu_list_read_cmd(menu,cmd);
375 static int read_key(menu_t* menu,int c){
376 char **str;
377 for (str=mpriv->actions; str && *str; str++)
378 if (c == (*str)[0]) {
379 action = &(*str)[2];
380 read_cmd(menu,MENU_CMD_ACTION);
381 return 1;
383 if (menu_dflt_read_key(menu, c))
384 return 1;
385 return menu_list_jump_to_key(menu, c);
388 static void clos(menu_t* menu) {
389 menu_list_uninit(menu,free_entry);
390 free(mpriv->dir);
393 static int open_fs(menu_t* menu, char* args) {
394 char *path = mpriv->path, *freepath = NULL;
395 int r = 0;
396 char wd[PATH_MAX+1], b[PATH_MAX+1];
397 args = NULL; // Warning kill
399 menu->draw = menu_list_draw;
400 menu->read_cmd = read_cmd;
401 menu->read_key = read_key;
402 menu->close = clos;
404 if (menu_keepdir) {
405 if (!path || path[0] == '\0') {
406 struct stat st;
407 int path_fp;
409 path_fp = open (MENU_KEEP_PATH, O_RDONLY);
410 if (path_fp >= 0) {
411 if (!fstat (path_fp, &st) && (st.st_size > 0)) {
412 path = malloc(st.st_size+1);
413 if ((read(path_fp, path, st.st_size) == st.st_size) && path[0] == '/'
414 && !stat(path, &st) && S_ISDIR(st.st_mode)){
415 freepath = path;
416 path[st.st_size] = '\0';
418 else {
419 free(path);
420 path = NULL;
423 close (path_fp);
428 getcwd(wd,PATH_MAX);
429 if (!path || path[0] == '\0') {
430 char *slash = NULL;
431 if (filename && !strstr(filename, "://") && (path=realpath(filename, b))) {
432 slash = strrchr(path, '/');
433 #if defined(__MINGW32__) || defined(__CYGWIN__)
434 // FIXME: Do we need and can convert all '\\' in path to '/' on win32?
435 if (!slash)
436 slash = strrchr(path, '\\');
437 #endif
439 if (slash)
440 slash[1] = '\0';
441 else
442 path = wd;
444 if (path[0] != '/') {
445 if(path[strlen(path)-1] != '/')
446 snprintf(b,sizeof(b),"%s/%s/",wd,path);
447 else
448 snprintf(b,sizeof(b),"%s/%s",wd,path);
449 path = b;
450 } else if (path[strlen(path)-1]!='/') {
451 sprintf(b,"%s/",path);
452 path = b;
454 if (menu_chroot && menu_chroot[0] == '/') {
455 int l = strlen(menu_chroot);
456 if (l > 0 && menu_chroot[l-1] == '/')
457 --l;
458 if (strncmp(menu_chroot, path, l) || (path[l] != '\0' && path[l] != '/')) {
459 if (menu_chroot[l] == '/')
460 path = menu_chroot;
461 else {
462 sprintf(b,"%s/",menu_chroot);
463 path = b;
467 r = open_dir(menu,path);
469 if (freepath)
470 free(freepath);
472 return r;
475 const menu_info_t menu_info_filesel = {
476 "File seletor menu",
477 "filesel",
478 "Albeu",
481 "fs_cfg",
482 sizeof(struct menu_priv_s),
483 &cfg_dflt,
484 cfg_fields
486 open_fs