~medibuntu-maintainers/mplayer/medibuntu.lucid

1.1.2 by Reinhard Tartler
Import upstream version 0.99+1.0pre8
1
/// \file
2
/// \ingroup ConfigParsers MEntry
3
4
#include "config.h"
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
5
6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
#include <errno.h>
10
11
#ifdef MP_DEBUG
12
#include <assert.h>
13
#endif
14
15
#include "mp_msg.h"
16
#include "help_mp.h"
1.1.4 by Mario Limonciello
Import upstream version 1.0~rc2
17
#include "m_option.h"
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
18
#include "m_config.h"
19
#include "parser-mecmd.h"
20
21
void
22
m_entry_list_free(m_entry_t* lst) {
23
  int i,j;
24
25
  for(i = 0 ; lst[i].name != NULL ; i++){
26
    free(lst[i].name);
27
    for(j = 0 ; lst[i].opts[2*j] != NULL ; j++) {
28
      free(lst[i].opts[2*j]);
29
      free(lst[i].opts[2*j+1]);
30
    }
31
    free(lst[i].opts);
32
  }
33
  free(lst);
34
}
35
36
int
37
m_entry_set_options(m_config_t *config, m_entry_t* entry) {
38
  int i,r;
39
40
  for(i = 0 ; entry->opts[2*i] != NULL ; i++){
41
    r = m_config_set_option(config,entry->opts[2*i],entry->opts[2*i+1]);
42
    if(r < 0)
43
      return 0;
44
  }
45
  return 1;
46
}
47
48
49
  
50
51
m_entry_t*
52
m_config_parse_me_command_line(m_config_t *config, int argc, char **argv)
53
{
54
  int i,nf = 0,no = 0;
55
  int tmp;
56
  char *opt;
57
  int no_more_opts = 0;
58
  int opt_exit = 0;
59
  m_entry_t *lst = NULL, *entry = NULL;
60
	
61
#ifdef MP_DEBUG
62
  assert(config != NULL);
63
  assert(argv != NULL);
64
  assert(argc >= 1);
65
#endif
66
67
  config->mode = M_COMMAND_LINE;
68
69
  lst = calloc(1,sizeof(m_entry_t));
70
71
  for (i = 1; i < argc; i++) {
72
    //next:
73
    opt = argv[i];
74
    /* check for -- (no more options id.) except --help! */
75
    if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
76
      {
77
	no_more_opts = 1;
78
	if (i+1 >= argc)
79
	  {
80
	    mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine);
1.1.4 by Mario Limonciello
Import upstream version 1.0~rc2
81
	    goto err_out;
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
82
	  }
83
	continue;
84
      }
85
			
86
    if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
87
      {
88
	const m_option_t* mp_opt = NULL;
0.2.1 by Reinhard Tartler
Import upstream version 1.0~rc2+svn20090303
89
	/* remove trailing '-' */
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
90
	opt++;
91
	mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
92
	mp_opt = m_config_get_option(config,opt);
93
	if(!mp_opt) {
94
	  tmp = M_OPT_UNKNOWN;
95
	  mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NotAnMEncoderOption, opt);
1.1.4 by Mario Limonciello
Import upstream version 1.0~rc2
96
	  goto err_out;
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
97
	}
98
	if(!entry || (mp_opt->flags & M_OPT_GLOBAL)){
99
	  tmp = m_config_set_option(config, opt, argv[i + 1]);
100
	  if (tmp <= M_OPT_EXIT) {
1.1.2 by Reinhard Tartler
Import upstream version 0.99+1.0pre8
101
	    opt_exit = 1;
102
	    tmp = M_OPT_EXIT - tmp;
103
	  }
104
	  else
105
	  if(tmp < 0){
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
106
//	    mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
107
	    mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt);
1.1.4 by Mario Limonciello
Import upstream version 1.0~rc2
108
	    goto err_out;
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
109
	  }
110
	} else {
111
	  tmp = m_config_check_option(config, opt, argv[i + 1]);
112
	  if (tmp <= M_OPT_EXIT) {
113
	    opt_exit = 1;
114
	    tmp = M_OPT_EXIT - tmp;
115
	  }
116
	  if(tmp >= 0) {
117
	    entry->opts = realloc(entry->opts,(no+2)*2*sizeof(char*));
118
	    entry->opts[2*no] = strdup(opt);
119
	    entry->opts[2*no+1] = argv[i + 1] ? strdup(argv[i + 1]) : NULL;
120
	    entry->opts[2*no+2] =  entry->opts[2*no+3] = NULL;
121
	    no++;
122
	  } else {
123
//	    mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
124
	    goto err_out;
125
	  }
126
	}
127
	i += tmp;
128
      } else  {/* filename */
129
	mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
130
	lst = realloc(lst,(nf+2)*sizeof(m_entry_t));
131
	lst[nf].name = strdup(argv[i]);
132
	lst[nf].opts = calloc(2,sizeof(char*));
133
	entry = &lst[nf];
134
	no = 0;
135
	memset(&lst[nf+1],0,sizeof(m_entry_t));
136
	nf++;
137
      }
138
  }
139
140
  if (opt_exit)
141
    exit(0);
142
  if(nf == 0) {
143
    m_entry_list_free(lst);
144
    mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGiven);
1.1.4 by Mario Limonciello
Import upstream version 1.0~rc2
145
    return NULL;
1 by Sebastian Dröge
Import upstream version 0.99+1.0pre7try2+cvs20051205
146
  }
147
  return lst;
148
149
 err_out:
150
   m_entry_list_free(lst);
151
  return NULL;
152
}
153