DOCS/xml/README: improve
[mplayer/glamo.git] / parser-mecmd.c
blobc1820d99da8374efd23f9fed0b86b80864c41794
1 /*
2 * This file is part of MPlayer.
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.
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.
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.
19 /// \file
20 /// \ingroup ConfigParsers MEntry
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
29 #ifdef MP_DEBUG
30 #include <assert.h>
31 #endif
33 #include "mp_msg.h"
34 #include "m_option.h"
35 #include "m_config.h"
36 #include "parser-mecmd.h"
38 void
39 m_entry_list_free(m_entry_t* lst) {
40 int i,j;
42 for(i = 0 ; lst[i].name != NULL ; i++){
43 free(lst[i].name);
44 for(j = 0 ; lst[i].opts[2*j] != NULL ; j++) {
45 free(lst[i].opts[2*j]);
46 free(lst[i].opts[2*j+1]);
48 free(lst[i].opts);
50 free(lst);
53 int
54 m_entry_set_options(m_config_t *config, m_entry_t* entry) {
55 int i,r;
57 for(i = 0 ; entry->opts[2*i] != NULL ; i++){
58 r = m_config_set_option(config,entry->opts[2*i],entry->opts[2*i+1]);
59 if(r < 0)
60 return 0;
62 return 1;
68 m_entry_t*
69 m_config_parse_me_command_line(m_config_t *config, int argc, char **argv)
71 int i,nf = 0,no = 0;
72 int tmp;
73 char *opt;
74 int no_more_opts = 0;
75 int opt_exit = 0;
76 m_entry_t *lst = NULL, *entry = NULL;
78 #ifdef MP_DEBUG
79 assert(config != NULL);
80 assert(argv != NULL);
81 assert(argc >= 1);
82 #endif
84 config->mode = M_COMMAND_LINE;
86 lst = calloc(1,sizeof(m_entry_t));
88 for (i = 1; i < argc; i++) {
89 //next:
90 opt = argv[i];
91 /* check for -- (no more options id.) except --help! */
92 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
94 no_more_opts = 1;
95 if (i+1 >= argc)
97 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
98 goto err_out;
100 continue;
103 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
105 const m_option_t* mp_opt = NULL;
106 /* remove trailing '-' */
107 opt++;
108 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
109 mp_opt = m_config_get_option(config,opt);
110 if(!mp_opt) {
111 tmp = M_OPT_UNKNOWN;
112 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "-%s is not an MEncoder option\n", opt);
113 goto err_out;
115 if(!entry || (mp_opt->flags & M_OPT_GLOBAL)){
116 tmp = m_config_set_option(config, opt, argv[i + 1]);
117 if (tmp <= M_OPT_EXIT) {
118 opt_exit = 1;
119 tmp = M_OPT_EXIT - tmp;
121 else
122 if(tmp < 0){
123 // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
124 mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n", opt);
125 goto err_out;
127 } else {
128 tmp = m_config_check_option(config, opt, argv[i + 1]);
129 if (tmp <= M_OPT_EXIT) {
130 opt_exit = 1;
131 tmp = M_OPT_EXIT - tmp;
133 if(tmp >= 0) {
134 entry->opts = realloc(entry->opts,(no+2)*2*sizeof(char*));
135 entry->opts[2*no] = strdup(opt);
136 entry->opts[2*no+1] = argv[i + 1] ? strdup(argv[i + 1]) : NULL;
137 entry->opts[2*no+2] = entry->opts[2*no+3] = NULL;
138 no++;
139 } else {
140 // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
141 goto err_out;
144 i += tmp;
145 } else {/* filename */
146 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
147 lst = realloc(lst,(nf+2)*sizeof(m_entry_t));
148 lst[nf].name = strdup(argv[i]);
149 lst[nf].opts = calloc(2,sizeof(char*));
150 entry = &lst[nf];
151 no = 0;
152 memset(&lst[nf+1],0,sizeof(m_entry_t));
153 nf++;
157 if (opt_exit)
158 exit(0);
159 if(nf == 0) {
160 m_entry_list_free(lst);
161 mp_tmsg(MSGT_CFGPARSER, MSGL_ERR, "No file given\n");
162 return NULL;
164 return lst;
166 err_out:
167 m_entry_list_free(lst);
168 return NULL;