support for Geforce FX5500 based on patch by Pascal Yu <yu_pascal at hotmail.com>
[mplayer/greg.git] / parser-mecmd.c
blob9070f9890aeb507f48ef2bf8ccadacc99d1dc60a
1 #include "config.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <errno.h>
8 #ifdef MP_DEBUG
9 #include <assert.h>
10 #endif
12 #include "mp_msg.h"
13 #include "m_option.h"
14 #include "m_config.h"
15 #include "parser-mecmd.h"
17 void
18 m_entry_list_free(m_entry_t* lst) {
19 int i,j;
21 for(i = 0 ; lst[i].name != NULL ; i++){
22 free(lst[i].name);
23 for(j = 0 ; lst[i].opts[2*j] != NULL ; j++) {
24 free(lst[i].opts[2*j]);
25 free(lst[i].opts[2*j+1]);
27 free(lst[i].opts);
29 free(lst);
32 int
33 m_entry_set_options(m_config_t *config, m_entry_t* entry) {
34 int i,r;
36 for(i = 0 ; entry->opts[2*i] != NULL ; i++){
37 r = m_config_set_option(config,entry->opts[2*i],entry->opts[2*i+1]);
38 if(r < 0)
39 return 0;
41 return 1;
47 m_entry_t*
48 m_config_parse_me_command_line(m_config_t *config, int argc, char **argv)
50 int i,nf = 0,no = 0;
51 int tmp;
52 char *opt;
53 int no_more_opts = 0;
54 int opt_exit = 0;
55 m_entry_t *lst = NULL, *entry = NULL;
57 #ifdef MP_DEBUG
58 assert(config != NULL);
59 assert(argv != NULL);
60 assert(argc >= 1);
61 #endif
63 config->mode = M_COMMAND_LINE;
65 lst = calloc(1,sizeof(m_entry_t));
67 for (i = 1; i < argc; i++) {
68 //next:
69 opt = argv[i];
70 /* check for -- (no more options id.) except --help! */
71 if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0))
73 no_more_opts = 1;
74 if (i+1 >= argc)
76 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--' indicates no more options, but no filename was given on the command line.\n");
77 goto err_out;
79 continue;
82 if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */
84 m_option_t* mp_opt = NULL;
85 /* remove trailing '-' */
86 opt++;
87 mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt);
88 mp_opt = m_config_get_option(config,opt);
89 if(!mp_opt) {
90 tmp = M_OPT_UNKNOWN;
91 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "-%s is not an MEncoder option\n",opt);
92 goto err_out;
94 if(!entry || (mp_opt->flags & M_OPT_GLOBAL)){
95 tmp = m_config_set_option(config, opt, argv[i + 1]);
96 if (tmp <= M_OPT_EXIT) {
97 opt_exit = 1;
98 tmp = M_OPT_EXIT - tmp;
100 if(tmp < 0){
101 // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
102 goto err_out;
104 } else {
105 tmp = m_config_check_option(config, opt, argv[i + 1]);
106 if (tmp <= M_OPT_EXIT) {
107 opt_exit = 1;
108 tmp = M_OPT_EXIT - tmp;
110 if(tmp >= 0) {
111 entry->opts = realloc(entry->opts,(no+2)*2*sizeof(char*));
112 entry->opts[2*no] = strdup(opt);
113 entry->opts[2*no+1] = argv[i + 1] ? strdup(argv[i + 1]) : NULL;
114 entry->opts[2*no+2] = entry->opts[2*no+3] = NULL;
115 no++;
116 } else {
117 // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp);
118 goto err_out;
121 i += tmp;
122 } else {/* filename */
123 mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]);
124 lst = realloc(lst,(nf+2)*sizeof(m_entry_t));
125 lst[nf].name = strdup(argv[i]);
126 lst[nf].opts = calloc(2,sizeof(char*));
127 entry = &lst[nf];
128 no = 0;
129 memset(&lst[nf+1],0,sizeof(m_entry_t));
130 nf++;
134 if (opt_exit)
135 exit(0);
136 if(nf == 0) {
137 m_entry_list_free(lst);
138 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "No file given\n");
139 return NULL;
141 return lst;
143 err_out:
144 m_entry_list_free(lst);
145 return NULL;