~ubuntu-branches/debian/sid/mplayer/sid

« back to all changes in this revision

Viewing changes to libmenu/menu_filesel.c

  • Committer: Bazaar Package Importer
  • Author(s): A Mennucc1
  • Date: 2009-03-23 10:05:45 UTC
  • mfrom: (4.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090323100545-x8h79obawnnte7kk
Tags: 1.0~rc2+svn20090303-5
debian/control : move docbook-xml,docbook-xsl,xsltproc from 
Build-Depends-Indep to Build-Depends, since they are needed to run
configure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file is part of MPlayer.
 
3
 *
 
4
 * MPlayer is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * MPlayer is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
17
 */
1
18
 
2
19
#include <stdlib.h>
3
20
#include <stdio.h>
31
48
 
32
49
int menu_keepdir = 0;
33
50
char *menu_chroot = NULL;
 
51
extern char *filename;
34
52
 
35
53
struct list_entry_s {
36
54
  struct list_entry p;
45
63
  char* title;
46
64
  char* file_action;
47
65
  char* dir_action;
48
 
  int auto_close;
49
66
  char** actions;
50
67
  char* filter; 
51
68
};
58
75
  "Select a file: %p",
59
76
  "loadfile '%p'",
60
77
  NULL,
61
 
  0,
62
78
  NULL,
63
79
  NULL
64
80
};
71
87
  { "title", ST_OFF(title),  CONF_TYPE_STRING, 0, 0, 0, NULL },
72
88
  { "file-action", ST_OFF(file_action),  CONF_TYPE_STRING, 0, 0, 0, NULL },
73
89
  { "dir-action", ST_OFF(dir_action),  CONF_TYPE_STRING, 0, 0, 0, NULL },
74
 
  { "auto-close", ST_OFF(auto_close), CONF_TYPE_FLAG, 0, 0, 1, NULL },
75
90
  { "actions", ST_OFF(actions), CONF_TYPE_STRING_LIST, 0, 0, 0, NULL},
76
91
  { "filter", ST_OFF(filter), CONF_TYPE_STRING, 0, 0, 0, NULL},
77
92
  { NULL, NULL, NULL, 0,0,0,NULL }
84
99
  free(entry);
85
100
}
86
101
 
87
 
static char* replace_path(char* title , char* dir) {
 
102
static char* replace_path(char* title , char* dir , int escape) {
88
103
  char *p = strstr(title,"%p");
89
104
  if(p) {
90
105
    int tl = strlen(title);
92
107
    int t1l = p-title; 
93
108
    int l = tl - 2 + dl;
94
109
    char *r, *n, *d = dir;
95
 
    char term = *(p-1);
96
110
 
 
111
    if (escape) {
97
112
    do {
98
 
      if (*d == '\\' || *d == term)
 
113
      if (*d == '\\')
99
114
        l++;
 
115
      else if (*d == '\'') /* ' -> \'\\\'\' */
 
116
        l+=7;
100
117
    } while (*d++);
 
118
    }
101
119
    r = malloc(l + 1);
102
120
    n = r + t1l;
103
121
    memcpy(r,title,t1l);
104
122
    do {
105
 
      if (*dir == '\\' || *dir == term)
106
 
        *n++ = '\\';
 
123
      if (escape) {
 
124
      if (*dir == '\\')
 
125
        *n++ = '\\';
 
126
      else if (*dir == '\'') { /* ' -> \'\\\'\' */
 
127
        *n++ = '\\'; *n++ = '\'';
 
128
        *n++ = '\\'; *n++ = '\\';
 
129
        *n++ = '\\'; *n++ = '\'';
 
130
        *n++ = '\\';
 
131
      }
 
132
      }
107
133
    } while ((*n++ = *dir++));
108
134
    if(tl - t1l - 2 > 0)
109
135
      strcpy(n-1,p+2);
117
143
static int mylstat(char *dir, char *file,struct stat* st) {
118
144
  int l = strlen(dir) + strlen(file);
119
145
  char s[l+2];
 
146
  if (!strcmp("..", file)) {
 
147
    char *slash;
 
148
    l -= 3;
 
149
    strcpy(s, dir);
 
150
#if defined(__MINGW32__) || defined(__CYGWIN__)
 
151
    if (s[l] == '/' || s[l] == '\\')
 
152
#else
 
153
    if (s[l] == '/')
 
154
#endif
 
155
      s[l] = '\0';
 
156
    slash = strrchr(s, '/');
 
157
#if defined(__MINGW32__) || defined(__CYGWIN__)
 
158
    if (!slash)
 
159
      slash = strrchr(s,'\\');
 
160
#endif
 
161
    if (!slash)
 
162
      return stat(dir,st);
 
163
    slash[1] = '\0';
 
164
    return stat(s,st);
 
165
  }
120
166
  sprintf(s,"%s/%s",dir,file);
121
167
  return stat(s,st);
122
168
}
201
247
    free(mpriv->p.title);
202
248
  p = strstr(mpriv->title,"%p");
203
249
 
204
 
  mpriv->p.title = replace_path(mpriv->title,mpriv->dir);
 
250
  mpriv->p.title = replace_path(mpriv->title,mpriv->dir,0);
205
251
 
206
252
  if ((dirp = opendir (mpriv->dir)) == NULL){
207
253
    mp_msg(MSGT_GLOBAL,MSGL_ERR,MSGTR_LIBMENU_OpendirError, strerror(errno));
224
270
    if(dp->d_name[0] == '.' && strcmp(dp->d_name,"..") != 0)
225
271
      continue;
226
272
    if (menu_chroot && !strcmp (dp->d_name,"..")) {
227
 
      int len = strlen (menu_chroot);
 
273
      size_t len = strlen (menu_chroot);
228
274
      if ((strlen (mpriv->dir) == len || strlen (mpriv->dir) == len + 1)
229
275
          && !strncmp (mpriv->dir, menu_chroot, len))
230
276
        continue;
231
277
    }
232
 
    mylstat(args,dp->d_name,&st);
 
278
    if (mylstat(args,dp->d_name,&st))
 
279
      continue;
233
280
    if (file_filter && extensions && !S_ISDIR(st.st_mode)) {
234
281
      if((ext = strrchr(dp->d_name,'.')) == NULL)
235
282
        continue;
291
338
 
292
339
  return 1;
293
340
}
294
 
    
295
341
 
296
342
static char *action;
297
343
 
298
344
static void read_cmd(menu_t* menu,int cmd) {
299
 
  mp_cmd_t* c = NULL;
300
345
  switch(cmd) {
301
346
  case MENU_CMD_LEFT:
302
347
    mpriv->p.current = mpriv->p.menu; // Hack : we consider that the first entry is ../
303
348
  case MENU_CMD_RIGHT:
304
349
  case MENU_CMD_OK: {
305
350
    // Directory
306
 
    if(mpriv->p.current->d) {
307
 
      if(mpriv->dir_action) {
308
 
        int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
309
 
        char filename[fname_len];
310
 
        char* str;
311
 
        sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
312
 
        str = replace_path(mpriv->dir_action,filename);
313
 
        c = mp_input_parse_cmd(str);
314
 
        if(str != mpriv->dir_action)
315
 
          free(str);
316
 
      } else { // Default action : open this dirctory ourself
 
351
    if(mpriv->p.current->d && !mpriv->dir_action) {
 
352
        // Default action : open this dirctory ourself
317
353
        int l = strlen(mpriv->dir);
318
354
        char *slash =  NULL, *p = NULL;
319
355
        if(strcmp(mpriv->p.current->p.txt,"../") == 0) {
337
373
          menu->cl = 1;
338
374
        }
339
375
        free(p);
340
 
      }
341
 
    } else { // Files
 
376
    } else { // File and directory dealt with action string.
342
377
      int fname_len = strlen(mpriv->dir) + strlen(mpriv->p.current->p.txt) + 1;
343
378
      char filename[fname_len];
344
379
      char *str;
 
380
      char *action = mpriv->p.current->d ? mpriv->dir_action:mpriv->file_action;
345
381
      sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
346
 
      str = replace_path(mpriv->file_action,filename);
347
 
      c = mp_input_parse_cmd(str);
348
 
      if(str != mpriv->file_action)
 
382
      str = replace_path(action, filename,1);
 
383
      mp_input_parse_and_queue_cmds(str);
 
384
      if (str != action)
349
385
        free(str);
350
 
    }     
351
 
    if(c) {
352
 
      mp_input_queue_cmd(c);
353
 
      if(mpriv->auto_close)
354
 
        menu->cl = 1;
355
386
    }
356
387
  } break;
357
388
  case MENU_CMD_ACTION: {
359
390
    char filename[fname_len];
360
391
    char *str;
361
392
    sprintf(filename,"%s%s",mpriv->dir,mpriv->p.current->p.txt);
362
 
    str = replace_path(action, filename);
363
 
    mp_input_queue_cmd(mp_input_parse_cmd(str));
 
393
    str = replace_path(action, filename,1);
 
394
    mp_input_parse_and_queue_cmds(str);
364
395
    if(str != action)
365
396
      free(str);
366
397
  } break;
369
400
  }
370
401
}
371
402
 
372
 
static void read_key(menu_t* menu,int c){
373
 
  if(c == KEY_BS)
374
 
    read_cmd(menu,MENU_CMD_LEFT);
375
 
  else {
 
403
static int read_key(menu_t* menu,int c){
376
404
    char **str;
377
405
    for (str=mpriv->actions; str && *str; str++)
378
406
      if (c == (*str)[0]) {
379
407
        action = &(*str)[2];
380
408
        read_cmd(menu,MENU_CMD_ACTION);
381
 
        break;
 
409
        return 1;
382
410
      }
383
 
    if (!str || !*str)
384
 
      menu_list_read_key(menu,c,1);
385
 
  }
 
411
  if (menu_dflt_read_key(menu, c))
 
412
    return 1;
 
413
  return menu_list_jump_to_key(menu, c);
386
414
}
387
415
 
388
416
static void clos(menu_t* menu) {
391
419
}
392
420
 
393
421
static int open_fs(menu_t* menu, char* args) {
394
 
  char *path = mpriv->path, *freepath = NULL;
 
422
  char *path = mpriv->path;
395
423
  int r = 0;
396
 
  char wd[PATH_MAX+1];
 
424
  char wd[PATH_MAX+1], b[PATH_MAX+1];
397
425
  args = NULL; // Warning kill
398
426
 
399
427
  menu->draw = menu_list_draw;
410
438
      if (path_fp >= 0) {
411
439
        if (!fstat (path_fp, &st) && (st.st_size > 0)) {
412
440
          path = malloc(st.st_size+1);
413
 
          if ((read(path_fp, path, st.st_size) == st.st_size) && path[0] != '\0'){
414
 
            freepath = path;
415
 
            path[st.st_size] = '\0';
416
 
          }
417
 
          else {
 
441
          path[st.st_size] = '\0';
 
442
          if (!((read(path_fp, path, st.st_size) == st.st_size) && path[0] == '/'
 
443
              && !stat(path, &st) && S_ISDIR(st.st_mode))) {
418
444
            free(path);
419
445
            path = NULL;
420
446
          }
425
451
  }
426
452
  
427
453
  getcwd(wd,PATH_MAX);
428
 
  if(!path || path[0] == '\0') {
429
 
    int l = strlen(wd) + 2;
430
 
    char b[l];
431
 
    sprintf(b,"%s/",wd);
432
 
    r = open_dir(menu,b);
433
 
  } else if(path[0] != '/') {
434
 
    int al = strlen(path);
435
 
    int l = strlen(wd) + al + 3;
436
 
    char b[l];
437
 
    if(b[al-1] != '/')
438
 
      sprintf(b,"%s/%s/",wd,path);
439
 
    else
440
 
      sprintf(b,"%s/%s",wd,path);
441
 
    r = open_dir(menu,b);
442
 
  } else
443
 
    r = open_dir(menu,path);
 
454
  if (!path || path[0] == '\0') {
 
455
#if 0
 
456
    char *slash = NULL;
 
457
    if (filename && !strstr(filename, "://") && (path=realpath(filename, b))) {
 
458
      slash = strrchr(path, '/');
 
459
#if defined(__MINGW32__) || defined(__CYGWIN__)
 
460
      // FIXME: Do we need and can convert all '\\' in path to '/' on win32?
 
461
      if (!slash)
 
462
        slash = strrchr(path, '\\');
 
463
#endif
 
464
    }
 
465
    if (slash)
 
466
      slash[1] = '\0';
 
467
    else
 
468
#endif
 
469
      path = wd;
 
470
  }
 
471
  if (path[0] != '/') {
 
472
    if(path[strlen(path)-1] != '/')
 
473
      snprintf(b,sizeof(b),"%s/%s/",wd,path);
 
474
    else
 
475
      snprintf(b,sizeof(b),"%s/%s",wd,path);
 
476
    path = b;
 
477
  } else if (path[strlen(path)-1]!='/') {
 
478
    sprintf(b,"%s/",path);
 
479
    path = b;
 
480
  }
 
481
  if (menu_chroot && menu_chroot[0] == '/') {
 
482
    int l = strlen(menu_chroot);
 
483
    if (l > 0 && menu_chroot[l-1] == '/')
 
484
      --l;
 
485
    if (strncmp(menu_chroot, path, l) || (path[l] != '\0' && path[l] != '/')) {
 
486
      if (menu_chroot[l] == '/')
 
487
        path = menu_chroot;
 
488
      else {
 
489
        sprintf(b,"%s/",menu_chroot);
 
490
        path = b;
 
491
      }
 
492
    }
 
493
  }
 
494
  r = open_dir(menu,path);
444
495
 
445
 
  if (freepath)
446
 
    free(freepath);
447
 
  
448
496
  return r;
449
497
}
450
498