Add Indexes to Options as suggested by Jaeho Shin.
[mplayer/glamo.git] / parser-mecmd.c
blobe6bb81fb28f2612233fdc6a556399805cf2aa1e3
2 /// \file
3 /// \ingroup ConfigParsers MEntry
5 #include "config.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <errno.h>
12 #ifdef MP_DEBUG
13 #include <assert.h>
14 #endif
16 #include "mp_msg.h"
17 #include "m_option.h"
18 #include "m_config.h"
19 #include "parser-mecmd.h"
21 void
22 m_entry_list_free(m_entry_t* lst) {
23 int i,j;
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]);
31 free(lst[i].opts);
33 free(lst);
36 int
37 m_entry_set_options(m_config_t *config, m_entry_t* entry) {
38 int i,r;
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;
45 return 1;
51 m_entry_t*
52 m_config_parse_me_command_line(m_config_t *config, int argc, char **argv)
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;
61 #ifdef MP_DEBUG
62 assert(config != NULL);
63 assert(argv != NULL);
64 assert(argc >= 1);
65 #endif
67 config->mode = M_COMMAND_LINE;
69 lst = calloc(1,sizeof(m_entry_t));
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))
77 no_more_opts = 1;
78 if (i+1 >= argc)
80 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
81 goto err_out;
83 continue;
86 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
88 m_option_t* mp_opt = NULL;
89 /* remove trailing '-' */
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, "-%s is not an MEncoder option\n",opt);
96 goto err_out;
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) {
101 opt_exit = 1;
102 tmp = M_OPT_EXIT - tmp;
104 else
105 if(tmp < 0){
106 // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
107 mp_msg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing option on the command line: -%s\n",opt);
108 goto err_out;
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;
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;
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++;
140 if (opt_exit)
141 exit(0);
142 if(nf == 0) {
143 m_entry_list_free(lst);
144 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "No file given\n");
145 return NULL;
147 return lst;
149 err_out:
150 m_entry_list_free(lst);
151 return NULL;