PRIVATE ScriptLineCond * _CondBuildTree(CondDatStack & condStack) { //there should at least be one item if(condStack.size() == 0) return 0; ScriptLineCond *newCond = (ScriptLineCond *)SCRIPT_MALLOC(sizeof(ScriptLineCond)); if(!newCond) { ASSERT_MSG(0, "Unable to allocate new script line condition!", "_CondBuildTree"); return 0; } //pop first item newCond->dat = *condStack.begin(); condStack.pop_front(); //check if var.paramVarType != eVarNULL if(newCond->dat.var.paramVarType != eVarNULL) { newCond->dat.condTest = eCONDTSTNONE; return newCond; } //build up the branching conditions newCond->right = _CondBuildTree(condStack); newCond->left = _CondBuildTree(condStack); return newCond; }
///////////////////////////////////// // Name: ScriptVarCreate // Purpose: create a variable from file // Output: variable added to global list // if bGlobal == true // Return: new variable ///////////////////////////////////// PROTECTED hVAR ScriptVarCreate(void *owner, FILE *fp, const ScriptInd & sInd, bool bGlobal) { hVAR newVar = (hVAR)SCRIPT_MALLOC(sizeof(Variable)); if(!newVar) { ASSERT_MSG(0, "Unable to allocate new variable!", "ScriptVarCreate"); return 0; } //set type newVar->type = sInd; //get name from file ParserReadWordFile(fp, newVar->name, MAXCHARNAME, 0); if(ScriptVarCall(owner, newVar, VAR_CREATE, 0, 0) != RETCODE_SUCCESS) { ASSERT_MSG(0, "Error initializing new variable!", "ScriptVarCreate"); ScriptVarDestroy(owner, &newVar); return 0; } //add to global if bGlobal is true if(bGlobal) { ScriptTeaseAddGlobalVar(newVar); } return newVar; }
///////////////////////////////////// // Name: ScriptLineCreate // Purpose: creates a script line // depending on initType: // func call -> sInd -> function type call // var set -> sInd.ind -> variable index in variables, also uses bGlobal // condition -> sInd.ind -> (eCondition) // Output: // Return: ///////////////////////////////////// PROTECTED ScriptLine * ScriptLineCreate(void *owner, FILE *fp, VarPtrArray *variables, const ScriptInd & sInd, bool bGlobal, eScriptLineInit initType) { ScriptLine *newScriptLine = (ScriptLine *)SCRIPT_MALLOC(sizeof(ScriptLine)); if(!newScriptLine) { ASSERT_MSG(0, "Unable to allocate new script line!", "ScriptLineCreate"); return 0; } //do something depending on init type switch(initType) { case eScriptLine_Function: { //initialize function newScriptLine->func = ScriptFuncCreate(owner, sInd); assert(newScriptLine->func); ///////////////////////////////////////////////////////////// //create parameters newScriptLine->params = new ParamArray; assert(newScriptLine->params); //get parameters char *buff; int buffSize; if(ParserCountStringFile(fp, &buffSize, '(', ')') != RETCODE_ENDREACHED) { ASSERT_MSG(0, "Error reading parameters", "ScriptLineCreate"); ScriptLineDestroy(owner, &newScriptLine); return 0; } buff = (char*)SCRIPT_MALLOC(sizeof(char)*(buffSize+1)); ParserReadStringFile(fp, buff, buffSize+1, '(', ')'); //now parse the buffer containing stuff inside '(...)' //insert them in param ScriptInd fInd = ScriptFuncGetTypeInd(newScriptLine->func); ScriptParamParseBuff(buff, variables, &SCRIPT_FUNCTYPE(fInd.ascii, fInd.ind).funcParam, newScriptLine->params); SCRIPT_FREE(buff); ///////////////////////////////////////////////////////////// } break; case eScriptLine_VarSet: { //set var ind newScriptLine->var.bGlobal = bGlobal; newScriptLine->var.dat._varInd = sInd.ind; newScriptLine->var.paramVarType = eVarPtr; //get the variable hVAR var = bGlobal ? ScriptTeaseGetGlobalVar(sInd.ind) : ScriptGetVar(sInd.ind, variables); assert(var); //this can't be null! ///////////////////////////////////////////////////////////// //create parameters newScriptLine->params = new ParamArray; assert(newScriptLine->params); //get parameters char *buff; int buffSize; if(ParserCountStringFile(fp, &buffSize, '(', ')') != RETCODE_ENDREACHED) { ASSERT_MSG(0, "Error reading parameters", "ScriptLineCreate"); ScriptLineDestroy(owner, &newScriptLine); return 0; } buff = (char*)SCRIPT_MALLOC(sizeof(char)*(buffSize+1)); ParserReadStringFile(fp, buff, buffSize+1, '(', ')'); //now parse the buffer containing stuff inside '(...)' //insert them in param ScriptInd vInd = ScriptVarGetTypeInd(var); ScriptParamParseBuff(buff, variables, &SCRIPT_VARTYPE(vInd.ascii, vInd.ind).funcParam, newScriptLine->params); SCRIPT_FREE(buff); ///////////////////////////////////////////////////////////// } break; case eScriptLine_Condition: //extra precaution... newScriptLine->func = 0; newScriptLine->var.paramVarType = eVarNULL; //intialize some stuff newScriptLine->curSubLine = -1; //this is so that we only check the condition //once newScriptLine->prevRet = SCRIPTLINERET_DONE; //create script condition newScriptLine->condition = (eCondition)sInd.ind; ///////////////////////////////////////////////////////////// //create conditions if(newScriptLine->condition != eCondNone && newScriptLine->condition != eElse) { //get conditions tests, eg: if (...) char *buff; int buffSize; if(ParserCountStringFile(fp, &buffSize, '(', ')') != RETCODE_ENDREACHED) { ASSERT_MSG(0, "Error reading parameters", "ScriptLineCreate"); ScriptLineDestroy(owner, &newScriptLine); return 0; } buff = (char*)SCRIPT_MALLOC(sizeof(char)*(buffSize+1)); ParserReadStringFile(fp, buff, buffSize+1, '(', ')'); newScriptLine->condDat = ScriptCondCreate(buff, variables); SCRIPT_FREE(buff); } ///////////////////////////////////////////////////////////// //allocate the sub lines newScriptLine->subLines = new ScriptLinePtrArray; assert(newScriptLine->subLines); //now get the sub lines inside the condition if(ScriptParseFile(owner, fp, variables, newScriptLine->subLines, "{", "}") != RETCODE_SUCCESS) { ScriptLineDestroy(owner, &newScriptLine); return 0; } break; } return newScriptLine; }
/* * init script */ int __init script_init(void) { int i, j, count; script_origin_head_t *script_hdr = __va(SYS_CONFIG_MEMBASE); script_origin_main_key_t *origin_main; script_origin_sub_key_t *origin_sub; script_main_key_t *main_key; script_sub_key_t *sub_key, *tmp_sub, swap_sub; script_item_u *sub_val, *tmp_val, swap_val, *pval_temp; printk("%s enter!\n", __func__); if(!script_hdr) { printk(KERN_ERR "script buffer is NULL!\n"); return -1; } /* alloc memory for main keys */ g_script = SCRIPT_MALLOC(script_hdr->main_cnt*sizeof(script_main_key_t)); if(!g_script) { printk(KERN_ERR "try to alloc memory for main keys!\n"); return -1; } origin_main = &script_hdr->main_key; for(i=0; i<script_hdr->main_cnt; i++) { main_key = &g_script[i]; /* copy main key name */ strncpy(main_key->name, origin_main[i].name, SCRIPT_NAME_SIZE_MAX); /* calculate hash value */ main_key->hash = hash(main_key->name); if (origin_main[i].sub_cnt == 0) { /* this mainkey have no subkey, skip subkey initialize */ main_key->subkey = NULL; main_key->subkey_val = NULL; count = 0; goto next_mainkey; } /* allock memory for sub-keys */ main_key->subkey = SCRIPT_MALLOC(origin_main[i].sub_cnt*sizeof(script_sub_key_t)); main_key->subkey_val = SCRIPT_MALLOC(origin_main[i].sub_cnt*sizeof(script_item_u)); if(!main_key->subkey || !main_key->subkey_val) { printk(KERN_ERR "try alloc memory for sub keys failed!\n"); goto err_out; } sub_key = main_key->subkey; sub_val = main_key->subkey_val; origin_sub = (script_origin_sub_key_t *)((unsigned int)script_hdr + (origin_main[i].offset<<2)); /* process sub keys */ for(j=0; j<origin_main[i].sub_cnt; j++) { strncpy(sub_key[j].name, origin_sub[j].name, SCRIPT_NAME_SIZE_MAX); sub_key[j].hash = hash(sub_key[j].name); sub_key[j].type = (script_item_value_type_e)origin_sub[j].pattern.type; sub_key[j].value = &sub_val[j]; if(origin_sub[j].pattern.type == SCIRPT_PARSER_VALUE_TYPE_SINGLE_WORD) { sub_val[j].val = *(int *)((unsigned int)script_hdr + (origin_sub[j].offset<<2)); sub_key[j].type = SCIRPT_ITEM_VALUE_TYPE_INT; } else if(origin_sub[j].pattern.type == SCIRPT_PARSER_VALUE_TYPE_STRING) { sub_val[j].str = SCRIPT_MALLOC((origin_sub[j].pattern.cnt<<2) + 1); memcpy(sub_val[j].str, (char *)((unsigned int)script_hdr + (origin_sub[j].offset<<2)), origin_sub[j].pattern.cnt<<2); sub_key[j].type = SCIRPT_ITEM_VALUE_TYPE_STR; } else if(origin_sub[j].pattern.type == SCIRPT_PARSER_VALUE_TYPE_GPIO_WORD) { script_origin_gpio_t *origin_gpio = (script_origin_gpio_t *)((unsigned int)script_hdr + (origin_sub[j].offset<<2) - 32); u32 gpio_tmp = port_to_index(origin_gpio->port, origin_gpio->port_num); if(GPIO_INDEX_INVALID == gpio_tmp) printk(KERN_ERR "%s:%s->%s gpio cfg invalid, please check sys_config.fex!\n",__func__,main_key->name,sub_key[j].name); sub_val[j].gpio.gpio = gpio_tmp; sub_val[j].gpio.mul_sel = (u32)origin_gpio->mul_sel; sub_val[j].gpio.pull = (u32)origin_gpio->pull; sub_val[j].gpio.drv_level = (u32)origin_gpio->drv_level; sub_val[j].gpio.data = (u32)origin_gpio->data; sub_key[j].type = SCIRPT_ITEM_VALUE_TYPE_PIO; } else { sub_key[j].type = SCIRPT_ITEM_VALUE_TYPE_INVALID; } } /* process gpios */ tmp_sub = main_key->subkey; tmp_val = main_key->subkey_val; count = 0; for(j=0; j<origin_main[i].sub_cnt; j++) { if(sub_key[j].type == SCIRPT_ITEM_VALUE_TYPE_PIO) { /* swap sub key */ swap_sub = *tmp_sub; *tmp_sub = sub_key[j]; sub_key[j] = swap_sub; /* swap sub key value ptr */ pval_temp = tmp_sub->value; tmp_sub->value = sub_key[j].value; sub_key[j].value = pval_temp; /* swap key value */ swap_val = *tmp_val; *tmp_val = main_key->subkey_val[j]; main_key->subkey_val[j] = swap_val; tmp_sub++; tmp_val++; count++; } } /* process sub key link */ for(j=0; j<origin_main[i].sub_cnt-1; j++) { main_key->subkey[j].next = &main_key->subkey[j+1]; } /* set gpio infermation */ next_mainkey: main_key->gpio = main_key->subkey_val; main_key->gpio_cnt = count; main_key->next = &g_script[i+1]; } g_script[script_hdr->main_cnt-1].next = 0; /* dump all the item */ //script_dump_mainkey(NULL); printk("%s exit!\n", __func__); return 0; err_out: /* script init failed, release resource */ printk(KERN_ERR "init sys_config script failed!\n"); if(g_script) { for(i=0; i<script_hdr->main_cnt; i++) { main_key = &g_script[i]; origin_sub = (script_origin_sub_key_t *)((unsigned int)script_hdr + (origin_main[i].offset<<2)); if(main_key->subkey_val) { for(j=0; j<origin_main[i].sub_cnt; j++) { if(main_key->subkey[j].type == SCIRPT_ITEM_VALUE_TYPE_STR) { if (main_key->subkey_val[j].str) { SCRIPT_FREE(main_key->subkey_val[j].str, (origin_sub[j].pattern.cnt<<2) + 1); } } } SCRIPT_FREE(main_key->subkey_val, sizeof(script_item_u)); } if(main_key->subkey) { SCRIPT_FREE(main_key->subkey, sizeof(script_sub_key_t)); } } SCRIPT_FREE(g_script, script_hdr->main_cnt*sizeof(script_main_key_t)); g_script = 0; } return -1; }