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