/* Returns pointer to a parsed sentence object, Returns NULL is parsing failed */ struct sentence *parse_sentence(struct sentence *new_sen, char *line) { char word[MAXLINE]; char *line_pointer = line; int coding_times; /* fetch the first word*/ getword(line_pointer, word, sizeof(word)); /* If the first word is empty, this is an empty sentence*/ if (strlen(line) == 1) { new_sen->type = EMPTY; return new_sen; } else if (strncmp(COMMENT_SIGN, word, 1) == 0) { new_sen->type = COMMENT; return new_sen; } /* Parsing the label of the sentence */ if (sscanf(line_pointer, " %[a-zA-Z0-9]: ", new_sen->label) == 1 && *(strchr(line, new_sen->label[0]) + strlen(new_sen->label)) == ':') { /* Skipping the label start */ line_pointer = strchr(line_pointer, ':') + 1; } /* nullifing the value that was read into new_sen.label */ else strcpy(new_sen->label, ""); /* trying to scan instruction into word, if successful, moving on to parsing */ if (sscanf(line_pointer, INSTRUCTION_FORMAT, word) == 1) { /* Moving the line pointer over the intruction*/ line_pointer = strchr(line_pointer, '.') + strlen(word) + 1; return parse_instructions(new_sen, word, line_pointer); } /* The sentence is operation sentence only if the first word is not empty*/ else if (sscanf(line_pointer, " %3[a-z]%1d ", word, &coding_times) == 2 || sscanf(line_pointer, " %4[a-z]%1d ", word, &coding_times) == 2) { /* setting the line pointer over the operator */ line_pointer = strchr(line_pointer, word[0]) + 4; return parse_operation(new_sen, line_pointer, word, coding_times); } return NULL; }
/* file format: <op count> add <value> delete <value> delete attribute rename <value> to <value> */ void parse_operations_file (char *name) { FILE *file = fopen (name, "r"); char line [256]; int i; if (file == NULL) { printf ("failed to open operations file %s: error = %d\n", name, errno); print_usage (); exit (1); } i = 0; while (fgets (line, sizeof (line), file)) { if (i == 0) { /* read operation count */ sim.op_count = atoi (line); if (sim.op_count < 1 || sim.op_count > MAX_OPS/2) { printf ("invalid operation count - %d; value must be between 1 and %d\n", sim.op_count, MAX_OPS/2); print_usage (); exit (1); } else { sim.ops = (Operation*)malloc (sim.op_count * sizeof (Operation)); } } else { if (strlen (line) == 0) /* skip empty lines */ continue; parse_operation (line, i); } i ++; } }
// confige line example: // mod_header [direction add|del|modify header_name [header_value]]{1,..n} allow|deny acl // ??????ͷ??ʼ?????? allow|deny ??ֹ // ???????????? static int func_sys_parse_param(char *cfg_line) { assert(cfg_line); struct mod_conf_param* data = NULL; char* token = NULL; int ret = 0; int i; // add by xueye.zhao // 2013/6/19 # if 0 //?ҵ?allow????deny?? Ȼ????֮???Ľ?ȥ //zhoudshu add for bug mantis 0002002 char* tmptoken = NULL; char* tmp_line = xstrdup(cfg_line); if ((tmptoken = strtok(tmp_line, w_space)) == NULL) { debug(98, 3)("(mod_header) -> parse line error\n"); safe_free(tmp_line); return -1; } int haskey = 0; while(NULL != (tmptoken = strtok(NULL, w_space))) { if(strcmp("allow",tmptoken) == 0 || strcmp("deny",tmptoken) == 0){ haskey = 1; } } safe_free(tmp_line); if(haskey != 1){ debug(98, 3)("(mod_header) -> has not key of allow or deny in config line \n"); return -1; } #endif if (NULL == strstr(cfg_line, " allow ")) { debug(98, 3)("(mod_header) -> has not key of allow or deny in config line \n"); return -1; } //end add //开始截取 if ((token = strtok(cfg_line, w_space)) != NULL) { if(strcmp(token, "mod_header")) goto errA; } else { debug(98, 3)("(mod_header) -> parse line error\n"); goto errA; } data = mod_config_pool_alloc(); data->count = 0; //?????↑ʼѭ??ȡ??????..???Dz???+header+value?ķ?ʽ, ????,ѭ???????? while(NULL != (token = strtok(NULL, w_space))) { if(strcmp("allow",token) == 0 || strcmp("deny",token) == 0 || strcmp("reply_check",token) == 0) { break; } struct action_part* part; struct header_info* info; part = action_part_pool_alloc(); info = header_info_pool_alloc(); data->acp[data->count] = part; data->acp[data->count]->hdr = info; data->count++; part->direct = atoi(token); //fetch the direction if(part->direct > 3 || part->direct < 0) { debug(98, 3)("(mod_header) -> parse direction error, cant be %d\n", part->direct); goto err; } //???↑ʼ?ǽ??????????? if (NULL == (token = strtok(NULL, w_space))) { debug(98, 3)("(mod_header) -> parse line data does not existed\n"); goto err; } ret = parse_operation(part, token);//??????????, ???????????ṹ if(-1 == ret) { debug(98, 3)("(mod_header) -> parse line operation error, cant be %s\n", token); goto err; } //?????и?header, ?????϶??е? if (NULL == (token = strtok(NULL, w_space))) { debug(98, 3)("(mod_header) -> parse line data does not existed\n"); goto err; } stringInit(&info->header, token); //?и???header, value?Ͳ?һ??????. ????Ҫ??????��?ж???. ??????Ҫ?и?value, ???? if (1 != part->action) { if (NULL == (token = strtok(NULL, w_space))) { debug(98, 3)("(mod_header) -> parse line data does not existed\n"); goto err; } //??Ȼ??value, ?ͱ???????. ?? header һ???????ŵ?????. stringInit(&info->value, token); /* case http://sis.ssr.chinacache.com/rms/view.php?id=4124 */ if (*token == '\"') { stringClean(&info->value); stringInit(&info->value, token+1); int len = 0; while (NULL !=(token = strtok(NULL,w_space))){ len = strlen(token); if (token[len-1] == '\"'){ stringAppend(&info->value, " ", 1); stringAppend(&info->value, token, len-1); break; } else if (strcmp("allow",token) == 0 || strcmp("deny",token) == 0) goto err; else { stringAppend(&info->value, " ", 1); stringAppend(&info->value, token, len); } } } } } //һ??û???κ????ݣ? ֱ?ӱ??? if(data->count == 0) goto err; else cc_register_mod_param(mod, data, free_callback); return 0; err: debug(98,1)("mod_header: parse error\n"); for(i=0; i<data->count; i++) { if(data->acp[i]->hdr) { if(strLen(data->acp[i]->hdr->header)) stringClean(&data->acp[i]->hdr->header); if(strLen(data->acp[i]->hdr->value)) stringClean(&data->acp[i]->hdr->value); memPoolFree(header_info_pool, data->acp[i]->hdr); data->acp[i]->hdr = NULL; } if(data->acp[i]) { memPoolFree(action_part_pool, data->acp[i]); data->acp[i] = NULL; } } errA: if (data) { memPoolFree(mod_config_pool, data); data = NULL; } return -1; }