Mercurial > mplayer.hg
annotate libmenu/menu_filesel.c @ 8499:e18f7b016a4a
fixed memleak caused by multiple config() calls - thx Gabu
author | arpi |
---|---|
date | Thu, 19 Dec 2002 22:33:59 +0000 |
parents | ecf6a4cf3272 |
children | f88ba181ecf2 |
rev | line source |
---|---|
8197 | 1 |
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 <ctype.h> | |
10 #include <unistd.h> | |
8291
abe95dde3223
limits.h required to get a PATH_MAX definition (on solaris)
jkeil
parents:
8226
diff
changeset
|
11 #include <limits.h> |
8197 | 12 |
13 | |
14 #include "../config.h" | |
15 | |
16 #include "../m_struct.h" | |
17 #include "../m_option.h" | |
18 | |
19 #include "img_format.h" | |
20 #include "mp_image.h" | |
21 | |
22 #include "menu.h" | |
23 #include "menu_list.h" | |
24 #include "../input/input.h" | |
25 #include "../linux/keycodes.h" | |
26 | |
27 struct list_entry_s { | |
28 struct list_entry p; | |
29 int d; | |
30 }; | |
31 | |
32 struct menu_priv_s { | |
33 menu_list_priv_t p; | |
34 char* dir; // current dir | |
35 /// Cfg fields | |
36 char* path; | |
37 char* title; | |
38 char* file_action; | |
39 char* dir_action; | |
40 int auto_close; | |
41 }; | |
42 | |
43 static struct menu_priv_s cfg_dflt = { | |
44 MENU_LIST_PRIV_DFLT, | |
45 NULL, | |
46 | |
47 NULL, | |
48 "Select a file: %p", | |
8226 | 49 "loadfile '%p'", |
8197 | 50 NULL, |
51 0 | |
52 }; | |
53 | |
54 #define ST_OFF(m) M_ST_OFF(struct menu_priv_s,m) | |
55 | |
56 static m_option_t cfg_fields[] = { | |
57 MENU_LIST_PRIV_FIELDS, | |
58 { "path", ST_OFF(path), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
59 { "title", ST_OFF(title), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
60 { "file-action", ST_OFF(file_action), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
61 { "dir-action", ST_OFF(dir_action), CONF_TYPE_STRING, 0, 0, 0, NULL }, | |
62 { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL }, | |
63 { NULL, NULL, NULL, 0,0,0,NULL } | |
64 }; | |
65 | |
66 #define mpriv (menu->priv) | |
67 | |
68 static void free_entry(list_entry_t* entry) { | |
69 free(entry->p.txt); | |
70 free(entry); | |
71 } | |
72 | |
73 static char* replace_path(char* title , char* dir) { | |
74 char *p = strstr(title,"%p"); | |
75 if(p) { | |
76 int tl = strlen(title); | |
77 int dl = strlen(dir); | |
78 int t1l = p-title; | |
79 int l = tl - 2 + dl; | |
80 char*r = malloc(l + 1); | |
81 memcpy(r,title,t1l); | |
82 memcpy(r+t1l,dir,dl); | |
83 if(tl - t1l - 2 > 0) | |
84 memcpy(r+t1l+dl,p+2,tl - t1l - 2); | |
85 r[l] = '\0'; | |
86 return r; | |
87 } else | |
88 return title; | |
89 } | |
90 | |
91 typedef int (*kill_warn)(const void*, const void*); | |
92 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
93 static int mylstat(char *dir, char *file,struct stat* st) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
94 int l = strlen(dir) + strlen(file); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
95 char s[l+1]; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
96 sprintf(s,"%s/%s",dir,file); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
97 return lstat(s,st); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
98 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
99 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
100 static int compare(char **a, char **b){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
101 int la,lb; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
102 la = strlen(*a); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
103 lb = strlen(*b); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
104 if((*a)[strlen(*a) - 1] == '/') { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
105 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
106 return strcmp(*b, *a) ; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
107 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
108 return 1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
109 } else { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
110 if((*b)[strlen(*b) - 1] == '/') |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
111 return -1; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
112 else |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
113 return strcmp(*b, *a); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
114 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
115 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
116 |
8197 | 117 static int open_dir(menu_t* menu,char* args) { |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
118 char **namelist, **tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
119 struct dirent *dp; |
8197 | 120 struct stat st; |
121 int n; | |
122 char* p = NULL; | |
123 list_entry_t* e; | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
124 DIR* dirp; |
8197 | 125 |
126 menu_list_init(menu); | |
127 | |
128 if(mpriv->dir) | |
129 free(mpriv->dir); | |
130 mpriv->dir = strdup(args); | |
131 if(mpriv->p.title && mpriv->p.title != mpriv->title && mpriv->p.title != cfg_dflt.p.title) | |
132 free(mpriv->p.title); | |
133 p = strstr(mpriv->title,"%p"); | |
134 | |
135 mpriv->p.title = replace_path(mpriv->title,mpriv->dir); | |
136 | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
137 if ((dirp = opendir (mpriv->dir)) == NULL){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
138 printf("opendir error: %s", strerror(errno)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
139 return 0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
140 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
141 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
142 namelist = (char **) malloc(sizeof(char *)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
143 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
144 n=0; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
145 while ((dp = readdir(dirp)) != NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
146 if(dp->d_name[0] == '.' && strcmp(dp->d_name,"..") != 0) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
147 continue; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
148 if(n%20 == 0){ // Get some more mem |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
149 if((tp = (char **) realloc(namelist, (n+20) * sizeof (char *))) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
150 == NULL) { |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
151 printf("realloc error: %s", strerror(errno)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
152 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
153 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
154 namelist=tp; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
155 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
156 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
157 namelist[n] = (char *) malloc(strlen(dp->d_name) + 2); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
158 if(namelist[n] == NULL){ |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
159 printf("malloc error: %s", strerror(errno)); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
160 goto bailout; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
161 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
162 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
163 strcpy(namelist[n], dp->d_name); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
164 mylstat(args,namelist[n],&st); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
165 if(S_ISDIR(st.st_mode)) |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
166 strcat(namelist[n], "/"); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
167 n++; |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
168 } |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
169 qsort(namelist, n, sizeof(char *), (kill_warn)compare); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
170 |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
171 bailout: |
8197 | 172 if (n < 0) { |
173 printf("scandir error: %s\n",strerror(errno)); | |
174 return 0; | |
175 } | |
176 while(n--) { | |
177 e = calloc(1,sizeof(list_entry_t)); | |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
178 e->p.txt = strdup(namelist[n]); |
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
179 if(strchr(namelist[n], '/') != NULL) |
8197 | 180 e->d = 1; |
8358
ecf6a4cf3272
No scandir() an no functions defined within other functions.
arpi
parents:
8291
diff
changeset
|
181 menu_list_add_entry(menu,e); |
8197 | 182 free(namelist[n]); |
183 } | |
184 free(namelist); | |
185 | |
186 return 1; | |
187 } | |
188 | |
189 | |
190 static void read_cmd(menu_t* menu,int cmd) { | |
191 mp_cmd_t* c = NULL; | |
192 switch(cmd) { | |
193 case MENU_CMD_OK: { | |
194 // Directory | |
195 if(mpriv->p.current->d) { | |
196 if(mpriv->dir_action) { | |
197 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
198 char filename[fname_len]; | |
199 char* str; | |
200 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
201 str = replace_path(mpriv->dir_action,filename); | |
202 c = mp_input_parse_cmd(str); | |
203 if(str != mpriv->dir_action) | |
204 free(str); | |
205 } else { // Default action : open this dirctory ourself | |
206 int l = strlen(mpriv->dir); | |
207 char *slash = NULL, *p = NULL; | |
208 if(strcmp(mpriv->p.current->p.txt,"../") == 0) { | |
209 if(l <= 1) break; | |
210 mpriv->dir[l-1] = '\0'; | |
211 slash = strrchr(mpriv->dir,'/'); | |
212 if(!slash) break; | |
213 slash[1] = '\0'; | |
214 p = strdup(mpriv->dir); | |
215 } else { | |
216 p = malloc(l + strlen(mpriv->p.current->p.txt) + 1); | |
217 sprintf(p,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
218 } | |
219 menu_list_uninit(menu,free_entry); | |
220 if(!open_dir(menu,p)) { | |
221 printf("Can't open directory %s\n",p); | |
222 menu->cl = 1; | |
223 } | |
224 free(p); | |
225 } | |
226 } else { // Files | |
227 int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1; | |
228 char filename[fname_len]; | |
229 char *str; | |
230 sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt); | |
231 str = replace_path(mpriv->file_action,filename); | |
232 c = mp_input_parse_cmd(str); | |
233 if(str != mpriv->file_action) | |
234 free(str); | |
235 } | |
236 if(c) { | |
237 mp_input_queue_cmd(c); | |
238 if(mpriv->auto_close) | |
239 menu->cl = 1; | |
240 } | |
241 } break; | |
242 default: | |
243 menu_list_read_cmd(menu,cmd); | |
244 } | |
245 } | |
246 | |
247 static void read_key(menu_t* menu,int c){ | |
248 if(c == KEY_BS) { | |
249 mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../ | |
250 read_cmd(menu,MENU_CMD_OK); | |
251 } else | |
252 menu_list_read_key(menu,c,1); | |
253 } | |
254 | |
255 static void clos(menu_t* menu) { | |
256 menu_list_uninit(menu,free_entry); | |
257 free(mpriv->dir); | |
258 } | |
259 | |
260 static int open_fs(menu_t* menu, char* args) { | |
261 char *path = mpriv->path; | |
262 int r = 0; | |
263 char wd[PATH_MAX+1]; | |
264 args = NULL; // Warning kill | |
265 | |
266 menu->draw = menu_list_draw; | |
267 menu->read_cmd = read_cmd; | |
268 menu->read_key = read_key; | |
269 menu->close = clos; | |
270 | |
271 getcwd(wd,PATH_MAX); | |
272 if(!path || path[0] == '\0') { | |
273 int l = strlen(wd) + 2; | |
274 char b[l]; | |
275 sprintf(b,"%s/",wd); | |
276 r = open_dir(menu,b); | |
277 } else if(path[0] != '/') { | |
278 int al = strlen(path); | |
279 int l = strlen(wd) + al + 3; | |
280 char b[l]; | |
281 if(b[al-1] != '/') | |
282 sprintf(b,"%s/%s/",wd,path); | |
283 else | |
284 sprintf(b,"%s/%s",wd,path); | |
285 r = open_dir(menu,b); | |
286 } else | |
287 r = open_dir(menu,path); | |
288 | |
289 return r; | |
290 } | |
291 | |
292 const menu_info_t menu_info_filesel = { | |
293 "File seletor menu", | |
294 "filesel", | |
295 "Albeu", | |
296 "", | |
297 { | |
298 "fs_cfg", | |
299 sizeof(struct menu_priv_s), | |
300 &cfg_dflt, | |
301 cfg_fields | |
302 }, | |
303 open_fs | |
304 }; |