static void play_tree_iter_push_params(play_tree_iter_t* iter) { int n; play_tree_t* pt; #ifdef MP_DEBUG assert(iter != NULL); assert(iter->config != NULL); assert(iter->tree != NULL); #endif pt = iter->tree; // We always push a config because we can set some option // while playing m_config_push(iter->config); if(pt->params == NULL) return; for(n = 0; pt->params[n].name != NULL ; n++) { int e; if((e = m_config_set_option(iter->config,pt->params[n].name,pt->params[n].value)) < 0) { mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Error %d while setting option '%s' with value '%s'\n",e, pt->params[n].name,pt->params[n].value); } } if(!pt->child) iter->entry_pushed = 1; }
int m_entry_set_options(m_config_t *config, m_entry_t* entry) { int i,r; for(i = 0 ; entry->opts[2*i] != NULL ; i++){ r = m_config_set_option(config,entry->opts[2*i],entry->opts[2*i+1]); if(r < 0) return 0; } return 1; }
void m_config_set_profile(m_config_t* config, m_profile_t* p) { int i; if(config->profile_depth > MAX_PROFILE_DEPTH) { mp_msg(MSGT_CFGPARSER, MSGL_WARN, MSGTR_ProfileInclusionTooDeep); return; } config->profile_depth++; for(i = 0 ; i < p->num_opts ; i++) m_config_set_option(config,p->opts[2*i],p->opts[2*i+1]); config->profile_depth--; }
play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv) { ProcessSerialNumber myPsn; char myPsnStr[5+10+1+10+1]; GetCurrentProcess(&myPsn); snprintf(myPsnStr, 5+10+1+10+1, "-psn_%u_%u", myPsn.highLongOfPSN, myPsn.lowLongOfPSN); myPsnStr[5+10+1+10]=0; if((argc==2) && !strcmp(myPsnStr, argv[1])) { m_config_set_option(config, "quiet", NULL); InitCursor(); AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(AppleEventHandlerProc), 0, FALSE); RunApplicationEventLoop(); } return files; }
m_entry_t* m_config_parse_me_command_line(m_config_t *config, int argc, char **argv) { int i,nf = 0,no = 0; int tmp; char *opt; int no_more_opts = 0; int opt_exit = 0; m_entry_t *lst = NULL, *entry = NULL; #ifdef MP_DEBUG assert(config != NULL); assert(argv != NULL); assert(argc >= 1); #endif config->mode = M_COMMAND_LINE; lst = calloc(1,sizeof(m_entry_t)); for (i = 1; i < argc; i++) { //next: opt = argv[i]; /* check for -- (no more options id.) except --help! */ if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0)) { no_more_opts = 1; if (i+1 >= argc) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine); goto err_out; } continue; } if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ { const m_option_t* mp_opt = NULL; /* remove trailing '-' */ opt++; mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt); mp_opt = m_config_get_option(config,opt); if(!mp_opt) { tmp = M_OPT_UNKNOWN; mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NotAnMEncoderOption, opt); goto err_out; } if(!entry || (mp_opt->flags & M_OPT_GLOBAL)){ tmp = m_config_set_option(config, opt, argv[i + 1]); if (tmp <= M_OPT_EXIT) { opt_exit = 1; tmp = M_OPT_EXIT - tmp; } else if(tmp < 0){ // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp); mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt); goto err_out; } } else { tmp = m_config_check_option(config, opt, argv[i + 1]); if (tmp <= M_OPT_EXIT) { opt_exit = 1; tmp = M_OPT_EXIT - tmp; } if(tmp >= 0) { entry->opts = realloc(entry->opts,(no+2)*2*sizeof(char*)); entry->opts[2*no] = strdup(opt); entry->opts[2*no+1] = argv[i + 1] ? strdup(argv[i + 1]) : NULL; entry->opts[2*no+2] = entry->opts[2*no+3] = NULL; no++; } else { // mp_msg(MSGT_CFGPARSER, MSGL_ERR, "m_config_set_option() failed (%d)\n",tmp); goto err_out; } } i += tmp; } else {/* filename */ mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]); lst = realloc(lst,(nf+2)*sizeof(m_entry_t)); lst[nf].name = strdup(argv[i]); lst[nf].opts = calloc(2,sizeof(char*)); entry = &lst[nf]; no = 0; memset(&lst[nf+1],0,sizeof(m_entry_t)); nf++; } } if (opt_exit) exit(0); if(nf == 0) { m_entry_list_free(lst); mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGiven); return NULL; } return lst; err_out: m_entry_list_free(lst); return NULL; }
/** \ingroup ConfigParsers */ play_tree_t* m_config_parse_mp_command_line(m_config_t *config, int argc, char **argv) { int i,j,start_title=-1,end_title=-1; char *opt,*splitpos=NULL; char entbuf[15]; int no_more_opts = 0; int opt_exit = 0; // flag indicating whether mplayer should exit without playing anything play_tree_t *last_parent, *last_entry = NULL, *root; #ifdef MP_DEBUG assert(config != NULL); assert(argv != NULL); assert(argc >= 1); #endif config->mode = M_COMMAND_LINE; mode = GLOBAL; #ifdef CONFIG_MACOSX_FINDER root=macosx_finder_args(config, argc, argv); if(root) return root; #endif last_parent = root = play_tree_new(); for (i = 1; i < argc; i++) { //next: opt = argv[i]; /* check for -- (no more options id.) except --help! */ if ((*opt == '-') && (*(opt+1) == '-') && (*(opt+2) == 0)) { no_more_opts = 1; if (i+1 >= argc) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_NoFileGivenOnCommandLine); goto err_out; } continue; } if((opt[0] == '{') && (opt[1] == '\0')) { play_tree_t* entry = play_tree_new(); UNSET_GLOBAL; if(last_parent->flags & PLAY_TREE_RND) entry->flags |= PLAY_TREE_RND; if(last_entry == NULL) { play_tree_set_child(last_parent,entry); } else { play_tree_append_entry(last_entry,entry); last_entry = NULL; } last_parent = entry; continue; } if((opt[0] == '}') && (opt[1] == '\0')) { if( ! last_parent || ! last_parent->parent) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, "too much }-\n"); goto err_out; } last_entry = last_parent; last_parent = last_entry->parent; continue; } if ((no_more_opts == 0) && (*opt == '-') && (*(opt+1) != 0)) /* option */ { int tmp = 0; /* remove trailing '-' */ opt++; mp_msg(MSGT_CFGPARSER, MSGL_DBG3, "this_opt = option: %s\n", opt); // We handle here some specific option // Loop option when it apply to a group if(strcasecmp(opt,"loop") == 0 && (! last_entry || last_entry->child) ) { int l; char* end = NULL; l = (i+1<argc) ? strtol(argv[i+1],&end,0) : 0; if(!end || *end != '\0') { mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_TheLoopOptionMustBeAnInteger, argv[i+1]); tmp = ERR_OUT_OF_RANGE; } else { play_tree_t* pt = last_entry ? last_entry : last_parent; l = l <= 0 ? -1 : l; pt->loop = l; tmp = 1; } } else if(strcasecmp(opt,"shuffle") == 0) { if(last_entry && last_entry->child) last_entry->flags |= PLAY_TREE_RND; else last_parent->flags |= PLAY_TREE_RND; } else if(strcasecmp(opt,"noshuffle") == 0) { if(last_entry && last_entry->child) last_entry->flags &= ~PLAY_TREE_RND; else last_parent->flags &= ~PLAY_TREE_RND; } else { const m_option_t* mp_opt = NULL; play_tree_t* entry = NULL; tmp = is_entry_option(opt,(i+1<argc) ? argv[i + 1] : NULL,&entry); if(tmp > 0) { // It's an entry if(entry) { add_entry(&last_parent,&last_entry,entry); if((last_parent->flags & PLAY_TREE_RND) && entry->child) entry->flags |= PLAY_TREE_RND; UNSET_GLOBAL; } else if(mode == LOCAL) // Entry is empty we have to drop his params mode = DROP_LOCAL; } else if(tmp == 0) { // 'normal' options mp_opt = m_config_get_option(config,opt); if (mp_opt != NULL) { // Option exist if(mode == GLOBAL || (mp_opt->flags & M_OPT_GLOBAL)) tmp = (i+1<argc) ? m_config_set_option(config, opt, argv[i + 1]) : m_config_set_option(config, opt, NULL); else { tmp = m_config_check_option(config, opt, (i+1<argc) ? argv[i + 1] : NULL); if(tmp >= 0 && mode != DROP_LOCAL) { play_tree_t* pt = last_entry ? last_entry : last_parent; play_tree_set_param(pt,opt, argv[i + 1]); } } } else { tmp = M_OPT_UNKNOWN; mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_UnknownOptionOnCommandLine, opt); } } } if (tmp <= M_OPT_EXIT) { opt_exit = 1; tmp = M_OPT_EXIT - tmp; } else if (tmp < 0) { mp_msg(MSGT_CFGPARSER, MSGL_FATAL, MSGTR_ErrorParsingOptionOnCommandLine, opt); goto err_out; } i += tmp; } else /* filename */ { int is_dvdnav = strstr(argv[i],"dvdnav://") != NULL; play_tree_t* entry = play_tree_new(); mp_msg(MSGT_CFGPARSER, MSGL_DBG2,"Adding file %s\n",argv[i]); // if required expand DVD filename entries like dvd://1-3 into component titles if ( strstr(argv[i],"dvd://") != NULL || is_dvdnav) { int offset = is_dvdnav ? 9 : 6; splitpos=strstr(argv[i]+offset,"-"); if(splitpos != NULL) { start_title=strtol(argv[i]+offset,NULL,10); if (start_title<0) { //entries like dvd://-2 start title implied 1 end_title=abs(start_title); start_title=1; } else { end_title=strtol(splitpos+1,NULL,10); } if (dvd_range(start_title) && dvd_range(end_title) && (start_title<end_title)) { for (j=start_title;j<=end_title;j++) { if (j!=start_title) entry=play_tree_new(); snprintf(entbuf,sizeof(entbuf),is_dvdnav ? "dvdnav://%d" : "dvd://%d",j); play_tree_add_file(entry,entbuf); add_entry(&last_parent,&last_entry,entry); last_entry = entry; } } else { mp_msg(MSGT_CFGPARSER, MSGL_ERR, MSGTR_InvalidPlayEntry, argv[i]); } } else { // dvd:// or dvd://x entry play_tree_add_file(entry,argv[i]); } } else { play_tree_add_file(entry,argv[i]); } // Lock stdin if it will be used as input if(strcasecmp(argv[i],"-") == 0) m_config_set_option(config,"noconsolecontrols",NULL); add_entry(&last_parent,&last_entry,entry); UNSET_GLOBAL; // We start entry specific options } } if (opt_exit) goto err_out; if(last_parent != root) mp_msg(MSGT_CFGPARSER, MSGL_ERR,"Missing }- ?\n"); return root; err_out: play_tree_free(root,1); return NULL; }
bool m_config_parse_mp_command_line(m_config_t *config, struct playlist *files, int argc, char **argv) { int mode = 0; struct playlist_entry *local_start = NULL; bool shuffle = false; int local_params_count = 0; struct playlist_param *local_params = 0; assert(config != NULL); assert(!config->file_local_mode); config->mode = M_COMMAND_LINE; mode = GLOBAL; #ifdef CONFIG_MACOSX_FINDER if (macosx_finder_args(config, files, argc, argv)) return true; #endif struct parse_state p = {config, argc, argv}; while (split_opt(&p)) { if (p.mp_opt) { int r; if (mode == GLOBAL && !(p.mp_opt->flags & M_OPT_PRE_PARSE)) { r = m_config_set_option(config, p.arg, p.param); } else { r = m_config_check_option(config, p.arg, p.param); } if (r <= M_OPT_EXIT) goto err_out; if (r < 0) { char *msg = m_option_strerror(r); if (!msg) goto print_err; mp_tmsg(MSGT_CFGPARSER, MSGL_FATAL, "Error parsing commandline option %.*s: %s\n", BSTR_P(p.arg), msg); goto err_out; } // Handle some special arguments outside option parser. if (!bstrcmp0(p.arg, "{")) { if (mode != GLOBAL) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, "'--{' can not be nested.\n"); goto err_out; } mode = LOCAL; // Needed for option checking. m_config_enter_file_local(config); assert(!local_start); local_start = files->last; continue; } if (!bstrcmp0(p.arg, "}")) { if (mode != LOCAL) { mp_msg(MSGT_CFGPARSER, MSGL_ERR, "Too many closing '--}'.\n"); goto err_out; } if (local_params_count) { // The files added between '{' and '}' are the entries from // the entry _after_ local_start, until the end of the list. // If local_start is NULL, the list was empty on '{', and we // want all files in the list. struct playlist_entry *cur = local_start ? local_start->next : files->first; if (!cur) mp_msg(MSGT_CFGPARSER, MSGL_WARN, "Ignored options!\n"); while (cur) { playlist_entry_add_params(cur, local_params, local_params_count); cur = cur->next; } } local_params_count = 0; mode = GLOBAL; m_config_leave_file_local(config); local_start = NULL; shuffle = false; continue; } if (bstrcmp0(p.arg, "shuffle") == 0) { shuffle = parse_flag(p.arg, p.param); continue; } if (bstrcmp0(p.arg, "playlist") == 0) { // append the playlist to the local args char *param0 = bstrdup0(NULL, p.param); struct playlist *pl = playlist_parse_file(param0); talloc_free(param0); if (!pl) goto print_err; playlist_transfer_entries(files, pl); talloc_free(pl); continue; } if (mode == LOCAL) { MP_TARRAY_APPEND(NULL, local_params, local_params_count, (struct playlist_param) {p.arg, p.param}); }
/** \param config The config object. * \param conffile Path to the config file. * \return 1 on sucess, -1 on error. */ int m_config_parse_config_file(m_config_t* config, char *conffile) { #define PRINT_LINENUM mp_msg(MSGT_CFGPARSER,MSGL_V,"%s(%d): ", conffile, line_num) #define MAX_LINE_LEN 10000 #define MAX_OPT_LEN 1000 #define MAX_PARAM_LEN 1500 FILE *fp; char *line; char opt[MAX_OPT_LEN + 1]; char param[MAX_PARAM_LEN + 1]; char c; /* for the "" and '' check */ int tmp; int line_num = 0; int line_pos; /* line pos */ int opt_pos; /* opt pos */ int param_pos; /* param pos */ int ret = 1; int errors = 0; int prev_mode = config->mode; m_profile_t* profile = NULL; #ifdef MP_DEBUG assert(config != NULL); // assert(conf_list != NULL); #endif mp_msg(MSGT_CFGPARSER,MSGL_V,"Reading config file %s", conffile); if (recursion_depth > MAX_RECURSION_DEPTH) { mp_msg(MSGT_CFGPARSER,MSGL_ERR,": too deep 'include'. check your configfiles\n"); ret = -1; goto out; } else config->mode = M_CONFIG_FILE; if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) { mp_msg(MSGT_CFGPARSER,MSGL_FATAL,"\ncan't get memory for 'line': %s", strerror(errno)); ret = -1; goto out; } else mp_msg(MSGT_CFGPARSER,MSGL_V,"\n"); if ((fp = fopen(conffile, "r")) == NULL) { mp_msg(MSGT_CFGPARSER,MSGL_V,": %s\n", strerror(errno)); free(line); ret = 0; goto out; } while (fgets(line, MAX_LINE_LEN, fp)) { if (errors >= 16) { mp_msg(MSGT_CFGPARSER,MSGL_FATAL,"too many errors\n"); goto out; } line_num++; line_pos = 0; /* skip whitespaces */ while (isspace(line[line_pos])) ++line_pos; /* EOL / comment */ if (line[line_pos] == '\0' || line[line_pos] == '#') continue; /* read option. */ for (opt_pos = 0; isprint(line[line_pos]) && line[line_pos] != ' ' && line[line_pos] != '#' && line[line_pos] != '='; /* NOTHING */) { opt[opt_pos++] = line[line_pos++]; if (opt_pos >= MAX_OPT_LEN) { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long option at line %d\n",line_num); errors++; ret = -1; goto nextline; } } if (opt_pos == 0) { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"parse error at line %d\n",line_num); ret = -1; errors++; continue; } opt[opt_pos] = '\0'; /* Profile declaration */ if(opt_pos > 2 && opt[0] == '[' && opt[opt_pos-1] == ']') { opt[opt_pos-1] = '\0'; if(strcmp(opt+1,"default")) profile = m_config_add_profile(config,opt+1); else profile = NULL; continue; } #ifdef MP_DEBUG PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_V,"option: %s\n", opt); #endif /* skip whitespaces */ while (isspace(line[line_pos])) ++line_pos; /* check '=' */ if (line[line_pos++] != '=') { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s needs a parameter at line %d\n",opt,line_num); ret = -1; errors++; continue; } /* whitespaces... */ while (isspace(line[line_pos])) ++line_pos; /* read the parameter */ if (line[line_pos] == '"' || line[line_pos] == '\'') { c = line[line_pos]; ++line_pos; for (param_pos = 0; line[line_pos] != c; /* NOTHING */) { param[param_pos++] = line[line_pos++]; if (param_pos >= MAX_PARAM_LEN) { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s has a too long parameter at line %d\n",opt,line_num); ret = -1; errors++; goto nextline; } } line_pos++; /* skip the closing " or ' */ } else { for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos]) && line[line_pos] != '#'; /* NOTHING */) { param[param_pos++] = line[line_pos++]; if (param_pos >= MAX_PARAM_LEN) { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"too long parameter\n"); ret = -1; errors++; goto nextline; } } } param[param_pos] = '\0'; /* did we read a parameter? */ if (param_pos == 0) { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Option %s needs a parameter at line %d\n",opt,line_num); ret = -1; errors++; continue; } #ifdef MP_DEBUG PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_V,"parameter: %s\n", param); #endif /* now, check if we have some more chars on the line */ /* whitespace... */ while (isspace(line[line_pos])) ++line_pos; /* EOL / comment */ if (line[line_pos] != '\0' && line[line_pos] != '#') { PRINT_LINENUM; mp_msg(MSGT_CFGPARSER,MSGL_WARN,"extra characters on line %d: %s\n",line_num, line+line_pos); ret = -1; } if(profile) { if(!strcmp(opt,"profile-desc")) m_profile_set_desc(profile,param), tmp = 1; else tmp = m_config_set_profile_option(config,profile, opt,param); } else tmp = m_config_set_option(config, opt, param); if (tmp < 0) { PRINT_LINENUM; if(tmp == M_OPT_UNKNOWN) { mp_msg(MSGT_CFGPARSER,MSGL_WARN,"Warning unknown option %s at line %d\n", opt,line_num); continue; } mp_msg(MSGT_CFGPARSER,MSGL_ERR,"Error parsing option %s=%s at line %d\n",opt,param,line_num); ret = -1; errors++; continue; /* break */ } nextline: ; } free(line); fclose(fp); out: config->mode = prev_mode; --recursion_depth; return ret; }