static int find_hash(int fd, const char *hash, char *game_name, size_t max_len) { char token[MAX_TOKEN_LEN] = {0}; while (1) { if (find_token(fd, "game") < 0) { return -1; } if (find_token(fd, "name") < 0) { return -1; } if (get_token(fd, game_name, max_len) < 0) { return -1; } if (find_token(fd, "sha1") < 0) { return -1; } if (get_token(fd, token, MAX_TOKEN_LEN) < 0) { return -1; } if (strcasecmp(hash, token) == 0) { return 0; } } }
void *cvar_alloc_handle(const char *cvar_parameters, void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr)) { cvar_token_t *list_head;; cvar_token_t *t; handle_t handle; handle_t *state = NULL; int ret = 0; cvar_trace("entry"); /* Tokenize parameters supplied by filebench. */ list_head = NULL; ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER, DEFAULT_KEY_VALUE_DELIMITER, &list_head); if (ret) goto out; /* Get the value of mean and sigma. */ t = find_token(list_head, RN_MEAN); if (t && t->value) { t->used = 1; handle.mean = atof(t->value); } else handle.mean = RN_MEAN_DEFAULT; t = find_token(list_head, RN_SIGMA); if (t && t->value) { t->used = 1; handle.sigma = atof(t->value); } else handle.sigma = RN_SIGMA_DEFAULT; cvar_trace("mean = %lf, sigma = %lf", handle.mean, handle.sigma); t = unused_tokens(list_head); if (t) { cvar_log_error("Unsupported parameter %s", t->key); goto out; } /* Seed the state. */ mts_goodseed(&handle.state); /* All set. Now allocate space for the handle in the shared segment and * copy the state over. */ state = (handle_t *) cvar_malloc(sizeof(handle_t)); if (!state) { cvar_log_error("Out of memory"); goto out; } *state = handle; out: free_tokens(list_head); cvar_trace("exit"); return state; }
static int nilfs_read_debug_option(char *page, char **start, off_t off, int count, int *eof, void *data) { int len = 0; int flag; spin_lock(&debug_info_lock); for (flag = 0; flag < NILFS_VERBOSE_LIMIT; flag++) { const char *vopt, *p; int level = min(MAX_VLEVEL, (int)nilfs_debug_info.verbose[flag]); if (level >= 0) { vopt = find_token(Opt_quiet + level, opt_tokens); BUG_ON(vopt == NULL); p = find_token(flag, class_tokens); if (!p) break; if (len > 0) page[len++] = ' '; len += sprintf(page + len, "-%s %s", vopt, p); } } spin_unlock(&debug_info_lock); page[len++] = '\n'; return proc_calc_metrics(page, start, off, count, eof, len); }
/* read argument string of a single assignment */ char* get_arg( FILE *file, char *name, int type ) { char token[MAX_TOKEN_LENGTH]; char *arg = 0; /* search entry_name */ if ( !find_token( file, name, type, WARNING ) ) return 0; /* token was found so read it */ read_token( file, token ); /* next token must be an equation */ read_token( file, token ); if ( token[0] != '=' ) { fprintf( stderr, "get_arg: line %i: '=' expected after token '%s' but found '%s' instead\n", get_line_number( file ), name, token ); return 0; } /* get argument */ read_token( file, token ); if ( token[0] == 0 ) fprintf( stderr, "get_arg: line %i: warning: argument for '%s' is empty\n", get_line_number( file ), name ); arg = strdup( token ); #ifdef FILE_DEBUG printf( "get_arg: %s = %s\n", name, arg ); #endif return arg; }
static char * read_token(struct state_machine *sm) { char *line; char *token; for (;;) { printf("Enter a token (or null to advance to next state): "); fflush(stdout); line = read_line(); if (strcmp(line, "") == 0) break; token = find_token(line, sm->tokens); if (token != NULL) return token; printf("Invalid token entered.\n"); } return NULL; }
static const char *xfind_token(const char *const string_array[], const char *key) { const char *r = find_token(string_array, key, NULL); if (r) return r; bb_error_msg_and_die("header: %s", key); }
int find_first_data_track(const char *cue_path, int32_t *offset, char *track_path, size_t max_len) { int rv, m, s, f; char tmp_token[MAX_TOKEN_LEN]; char cue_dir[PATH_MAX_LENGTH]; RFILE *fd; strlcpy(cue_dir, cue_path, PATH_MAX_LENGTH); path_basedir(cue_dir); fd = retro_fopen(cue_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open CUE file '%s': %s\n", cue_path, strerror(errno)); return -errno; } RARCH_LOG("Parsing CUE file '%s'...\n", cue_path); while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) { if (strcmp(tmp_token, "FILE") == 0) { get_token(fd, tmp_token, MAX_TOKEN_LEN); fill_pathname_join(track_path, cue_dir, tmp_token, max_len); } else if (strcasecmp(tmp_token, "TRACK") == 0) { get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (strcasecmp(tmp_token, "AUDIO") == 0) continue; find_token(fd, "INDEX"); get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3) { RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token); return -errno; } *offset = ((m * 60) * (s * 75) * f) * 25; RARCH_LOG("Found 1st data track on file '%s+%d'\n", track_path, *offset); rv = 0; goto clean; } } rv = -EINVAL; clean: retro_fclose(fd); return rv; }
static void prepare_input(const char *usr_input, char *buf) { size_t i; size_t j; size_t k; int tmp; i = 0; j = 0; while (i < MAX_INPUT_SIZE && j < MAX_INPUT_SIZE && usr_input[i] != '\0') { k = 0; if ((tmp = find_token(usr_input + i)) != -1) { if (i > 0 && usr_input[i - 1] != ' ') buf[j++] = ' '; while (j < MAX_INPUT_SIZE && k < g_tok[tmp].size) buf[j++] = g_tok[tmp].id[k++]; i += g_tok[tmp].size; if (j < MAX_INPUT_SIZE && usr_input[i] != ' ') buf[j++] = ' '; } else buf[j++] = usr_input[i++]; } }
void prepare_nonterminals() { int i,j,g; char token[16]; char production[256]; nonterminal_n=0; for(i=0;i<production_n;i++) { g=find_nonterminal(productions[i].name); if(g==-1) { strcpy(nonterminals[nonterminal_n],productions[i].name); g=nonterminal_n++; } productions[i].nonterminal=g; } for(i=0;i<production_n;i++) { productions[i].size=0; strcpy(production,productions[i].production); for(char *p=strtok(production," ");p;p=strtok(NULL," ")) { if(p[0]=='@') { for(j=0;*++p!='@' && *p!='\0' && j<15;j++)token[j]=*p; if(*p!='@') { printf("Production #%d Is Wrong - Format error\n",i); printf("Syntax analyzer construct failed.\n"); return; } token[j]='\0'; g=find_token(token); if(g==-1) { printf("Production #%d Is Wrong - Token name %s not found.\n",i,token); printf("Syntax analyzer construct failed.\n"); return; } productions[i].elements[productions[i].size].type=1; productions[i].elements[productions[i].size].key=g; } else { g=find_nonterminal(p); if(g==-1) { printf("Production #%d Is Wrong - Nonterminal name %s not found.\n",i,p); printf("Syntax analyzer construct failed.\n"); return; } productions[i].elements[productions[i].size].type=0; productions[i].elements[productions[i].size].key=g; } productions[i].size++; } } }
static int get_run_info(struct RunInfo* info, char* game_name) { int fd = open("./launch.conf", O_RDONLY); int rv; char token[MAX_TOKEN_LEN]; if (fd < 0) { return -errno; } memset(info, 0, sizeof(struct RunInfo)); while (1) { if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } if (fnmatch(token, game_name, 0) != 0) { if ((rv = find_token(fd, ";")) < 0) { goto clean; } continue; } LOG_DEBUG("Matched rule '%s'", token); if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } break; } strncpy(info->core, token, 50); info->multitap = 0; info->dualanalog = 0; if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } while (strcmp(token, ";") != 0) { if (strcmp(token, "multitap") == 0) { info->multitap = 1; } else if (strcmp(token, "dualanalog") == 0) { info->dualanalog = 1; } if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } } rv = 0; clean: close(fd); return rv; }
void first_string(char *s,First *target) { memset(target,0,sizeof(*target)); char production[256]; strcpy(production,s); Split split(production); char *p; while(p=split.next()) { if(p[0]=='@') { int j; char token[16]; for(j=0;*++p!='@' && *p!='\0' && j<15;j++)token[j]=*p; if(*p!='@') { ConstructException ex; sprintf(ex.message,"Production \"%s\" Wrong - Format error.",s); throw ex; } token[j]='\0'; int t=find_token(token); if(t==-1) { ConstructException ex; sprintf(ex.message,"Production \"%s\" Is Wrong - Token name %s not found.",s,token); throw ex; } set(target->terminals,t); break; } else { bool canempty=false,found=false; for(int i=0;i<production_n;i++) { if(strcmp(productions[i].name,p)==0) { found=true; first_dfs(i); orfrom(target->terminals,first[i].terminals,token_n); if(first[i].empty)canempty=true; } } if(!found) { ConstructException ex; sprintf(ex.message,"Nonterminal %s not found.",p); throw ex; } if(!canempty)break; } } target->empty|=(p==NULL); }
fs::path Path::MakeAbsolute(fs::path path, std::string const& token) const { if (path.empty()) return path; int idx = find_token(token.c_str(), token.size()); if (idx == -1) throw agi::InternalError("Bad token: " + token); path.make_preferred(); const auto str = path.string(); if (boost::starts_with(str, "?dummy") || boost::starts_with(str, "dummy-audio:")) return path; return (paths[idx].empty() || path.is_absolute()) ? path : paths[idx]/path; }
static int tokenize(varnam *handle, const char *input, struct strbuf *string) { const char *text, *remaining; int matchpos = 0, counter = 0; struct varnam_internal *vi; struct strbuf *lookup; struct token *temp = NULL, *last = NULL; vi = handle->internal; lookup = vi->lookup; text = input; while( *text != '\0' ) { strbuf_addc( lookup, *text ); ++counter; temp = find_token( handle, lookup->buffer ); if (temp) { last = temp; matchpos = counter; if( last->children <= 0 ) break; } else if( !can_find_token( handle, last, lookup->buffer )) { break; } ++text; } if (last) { resolve_token(handle, last, string); remaining = input + matchpos; set_last_token (handle, last); } else { if(lookup->buffer[0] != '_') strbuf_addc( string, lookup->buffer[0] ); remaining = input + 1; set_last_token (handle, NULL); } strbuf_clear (lookup); if( strlen( remaining ) > 0 ) return tokenize( handle, remaining, string ); return VARNAM_SUCCESS; }
static int read_launch_conf(struct RunInfo *info, const char *game_name) { int fd = open("./launch.conf", O_RDONLY); int rv; int bci = 0; char token[MAX_TOKEN_LEN]; if (fd < 0) { return -errno; } while (1) { if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } if (rl_fnmatch(token, game_name, 0) != 0) { if ((rv = find_token(fd, ";")) < 0) { goto clean; } continue; } LOG_DEBUG("Matched rule '%s'", token); break; } if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } while (strcmp(token, ";") != 0) { if (strcmp(token, "multitap") == 0) { info->multitap = 1; } else if (strcmp(token, "dualanalog") == 0) { info->dualanalog = 1; } else if (token[0] == '!') { strncpy(&info->broken_cores[bci], &token[1], PATH_MAX - bci); bci += strnlen(&token[1], PATH_MAX) + 1; } if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } } rv = 0; clean: close(fd); return rv; }
void Path::SetToken(const char *token_name, fs::path const& token_value) { int idx = find_token(token_name, strlen(token_name)); if (idx == -1) throw agi::InternalError("Bad token: " + std::string(token_name)); if (token_value.empty()) paths[idx] = token_value; else if (!token_value.is_absolute()) paths[idx].clear(); else { paths[idx] = token_value; paths[idx].make_preferred(); if (fs::FileExists(paths[idx])) paths[idx] = paths[idx].parent_path(); } }
CliNode *cliParserFindCmd_internal(CliMode mode, CliNode *current, const char *str) { ASSERT(str != NULL); ASSERT(current != NULL); *op.word_multimatch = '\0'; rcpDebug("parser: reset multimatch\n"); // extract the word and set the new value of the string const char *ptr = str; while (*ptr == ' ') ptr++; int len = get_word_len(ptr); if (len == 0) return 0; // all fine, we reached the end of the command memcpy(op.word, ptr, len); op.word[len] = '\0'; // walk trough the next list and see if the word is already there CliNode *found = find_token(mode, current, op.word); if (found == NULL) return NULL; op.last_node_match = found; op.depth++; // the node was found, check if this was the last token ptr += len; while (*ptr == ' ') ptr++; if (*ptr == '\0') return found; // we are at the end of the token list // test next token if (found->rule == NULL) return NULL; return cliParserFindCmd_internal(mode, found->rule, ptr); }
static int select_core(char *core_path, size_t max_len, const struct RunInfo *info) { int fd = open("./cores.conf", O_RDONLY); int rv; int bci = 0; char token[MAX_TOKEN_LEN]; int broken = 0; if (fd < 0) { return -errno; } LOG_INFO("Selecting core for system '%s'", info->system); while (1) { if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } if (rl_fnmatch(token, info->system, 0) != 0) { if ((rv = find_token(fd, ";")) < 0) { goto clean; } continue; } LOG_INFO("Matched system '%s'", token); break; } if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } while (strcmp(token, ";") != 0) { broken = 0; for (bci = 0; info->broken_cores[bci] != '\0'; bci += strlen(&info->broken_cores[bci]) + 1) { LOG_DEBUG("%s, %s", &info->broken_cores[bci], token); if (strcmp(&info->broken_cores[bci], token) == 0) { broken = 1; LOG_DEBUG("Not using core %s because it is " "marked broken for this game", &info->broken_cores[bci]); break; } } if (!broken) { goto success; } if ((rv = get_token(fd, token, MAX_TOKEN_LEN)) < 0) { goto clean; } } rv = -EINVAL; goto clean; success: snprintf(core_path, max_len, "./cores/%s.so", token); rv = 0; clean: close(fd); return rv; }
/* * Tokenize the current set of NULL-terminated words, allowing for * one token sublevel starting from the current token level. */ static int tokenize(t_tokenline *tl, int *words, int num_words, t_token **complete_tokens, int *complete_arg) { t_token *token_stack[8], *arg_tokens; t_tokenline_parsed *p; float arg_float; uint32_t arg_uint, suffix_uint; int done, arg_needed, w, t, t_idx, size; int cur_tsp, cur_tp, cur_bufsize, i; char *word, *suffix; done = FALSE; p = &tl->parsed; token_stack[0] = tl->token_levels[tl->token_level]; cur_tsp = 0; cur_tp = 0; cur_bufsize = 0; arg_needed = 0; arg_tokens = NULL; for (w = 0; w < num_words; w++) { word = tl->buf + words[w]; if (done) { if (!complete_tokens) tl->print(tl->user, "Too many arguments."NL); return FALSE; } else if (!arg_needed) { /* Token needed. */ if ((suffix = strchr(word, TL_TOKEN_DELIMITER))) { *suffix++ = 0; if (!str_to_uint(suffix, &suffix_uint)) { tl->print(tl->user, "Invalid number."NL); return FALSE; } } else { suffix_uint = 0; } if ((t_idx = find_token(token_stack[cur_tsp], tl->token_dict, word)) > -1) { t = token_stack[cur_tsp][t_idx].token; p->tokens[cur_tp++] = t; if (t == T_ARG_UINT) { /* Integer token. */ str_to_uint(word, &arg_uint); p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); } if (suffix) { if (!(token_stack[cur_tsp][t_idx].flags & T_FLAG_SUFFIX_TOKEN_DELIM_INT)) { if (!complete_tokens) tl->print(tl->user, "Token suffix not allowed."NL); return FALSE; } if (suffix_uint > 1) { p->tokens[cur_tp++] = T_ARG_TOKEN_SUFFIX_INT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &suffix_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); } } p->last_token_entry = &token_stack[cur_tsp][t_idx]; if (token_stack[cur_tsp][t_idx].arg_type == T_ARG_HELP) { /* Nothing to do, just keep cur_tsp from increasing. */ } else if (token_stack[cur_tsp][t_idx].arg_type) { /* Token needs an argument */ arg_needed = token_stack[cur_tsp][t_idx].arg_type; if (arg_needed == T_ARG_TOKEN) /* Argument is one of these subtokens. */ arg_tokens = token_stack[cur_tsp][t_idx].subtokens; } else if (token_stack[cur_tsp][t_idx].subtokens) { /* Switch to a new token set. */ token_stack[cur_tsp + 1] = token_stack[cur_tsp][t_idx].subtokens; cur_tsp++; } else { /* Not expecting any more arguments or tokens. */ done = tl->one_command_per_line; } } else { /* * No matching token found, but maybe the token * set allows freeform strings? */ for (i = 0; token_stack[cur_tsp][i].token; i++) { if (token_stack[cur_tsp][i].token == T_ARG_STRING) break; } if (token_stack[cur_tsp][i].token) { /* Add it in as a token. */ p->tokens[cur_tp++] = T_ARG_STRING; p->tokens[cur_tp++] = cur_bufsize; size = strlen(word) + 1; memcpy(p->buf + cur_bufsize, word, size); cur_bufsize += size; p->buf[cur_bufsize] = 0; } else { if (!complete_tokens) tl->print(tl->user, "Invalid command."NL); return FALSE; } } } else { /* Parse word as the type in arg_needed */ switch (arg_needed) { case T_ARG_UINT: str_to_uint(word, &arg_uint); if (*suffix) { switch(*suffix) { case 'k': arg_uint *= 1000; break; case 'm': arg_uint *= 1000000; break; case 'g': arg_uint *= 1000000000L; break; default: if (!complete_tokens) tl->print(tl->user, "Invalid value."NL); return FALSE; } } p->tokens[cur_tp++] = T_ARG_UINT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_uint, sizeof(uint32_t)); cur_bufsize += sizeof(uint32_t); break; case T_ARG_FLOAT: arg_float = strtof(word, &suffix); if (*suffix) { switch(*suffix) { case 'k': arg_float *= 1000; break; case 'm': arg_float *= 1000000; break; case 'g': arg_float *= 1000000000L; break; default: if (!complete_tokens) tl->print(tl->user, "Invalid value."NL); return FALSE; } } p->tokens[cur_tp++] = T_ARG_FLOAT; p->tokens[cur_tp++] = cur_bufsize; memcpy(p->buf + cur_bufsize, &arg_float, sizeof(float)); cur_bufsize += sizeof(float); break; case T_ARG_STRING: p->tokens[cur_tp++] = T_ARG_STRING; p->tokens[cur_tp++] = cur_bufsize; size = strlen(word) + 1; memcpy(p->buf + cur_bufsize, word, size); cur_bufsize += size; p->buf[cur_bufsize] = 0; break; case T_ARG_TOKEN: if ((t_idx = find_token(arg_tokens, tl->token_dict, word)) > -1) { p->tokens[cur_tp++] = arg_tokens[t_idx].token; p->last_token_entry = &arg_tokens[t_idx]; } else { if (!complete_tokens) tl->print(tl->user, "Invalid value."NL); return FALSE; } break; } arg_needed = 0; done = tl->one_command_per_line; } } if (arg_needed && !complete_tokens) { tl->print(tl->user, "Missing argument."NL); return FALSE; } p->tokens[cur_tp] = 0; if (complete_tokens) { if (done) { /* Nothing to add. */ *complete_tokens = NULL; } else { /* Fill in the completion token list. */ if (arg_needed == T_ARG_TOKEN) *complete_tokens = arg_tokens; else *complete_tokens = token_stack[cur_tsp]; } } if (complete_arg) *complete_arg = arg_needed; return TRUE; }
int main(int argc, char **argv) { int offset=2,i; int linenum=0,lastline=0,link_offset; int link_value=0x801; /* start of applesoft program */ int token; while(1) { /* get line from input file */ line_ptr=fgets(input_line,BUFSIZ,stdin); line++; if (line_ptr==NULL) break; /* VMW extension: use leading ' as a comment char */ if (line_ptr[0]=='\'') continue; /* skip empty lines */ if (line_ptr[0]=='\n') continue; linenum=getnum(); if ((linenum>65535) || (linenum<0)) { fprintf(stderr,"Invalid line number %d\n",linenum); exit(-1); } if (linenum<lastline) { fprintf(stderr,"Line counted backwards %d->%d\n", lastline,linenum); exit(-1); } lastline=linenum; link_offset=offset; check_oflo(offset+4); output[offset+2]=LOW(linenum); output[offset+3]=HIGH(linenum); offset+=4; while(1) { token=find_token(); output[offset]=token; offset++; check_oflo(offset); if (!token) break; } /* remarks end at end of line */ in_rem=0; /* 2 bytes is to ignore size from beginning of file */ link_value=0x801+(offset-2); /* point link value to next line */ check_oflo(offset+2); output[link_offset]=LOW(link_value); output[link_offset+1]=HIGH(link_value); } /* set last link field to $00 $00 which indicates EOF */ check_oflo(offset+2); output[offset]='\0'; output[offset+1]='\0'; offset+=2; /* Set filesize */ /* -1 to match observed values */ output[0]=LOW(offset-1); output[1]=HIGH(offset-1); /* output our file */ for(i=0;i<offset;i++) putchar(output[i]); return 0; }
static int parse(const char *boundary, char **argv) { char *line, *s, *p; const char *type; int boundary_len = strlen(boundary); const char *delims = " ;\"\t\r\n"; const char *uniq; int ntokens; const char *tokens[32]; // 32 is enough // prepare unique string pattern uniq = xasprintf("%%llu.%u.%s", (unsigned)getpid(), safe_gethostname()); //bb_info_msg("PARSE[%s]", terminator); while ((line = xmalloc_fgets_str(stdin, "\r\n\r\n")) != NULL) { // seek to start of MIME section // N.B. to avoid false positives let us seek to the _last_ occurance p = NULL; s = line; while ((s=strcasestr(s, "Content-Type:")) != NULL) p = s++; if (!p) goto next; //bb_info_msg("L[%s]", p); // split to tokens // TODO: strip of comments which are of form: (comment-text) ntokens = 0; tokens[ntokens] = NULL; for (s = strtok(p, delims); s; s = strtok(NULL, delims)) { tokens[ntokens] = s; if (ntokens < ARRAY_SIZE(tokens) - 1) ntokens++; //bb_info_msg("L[%d][%s]", ntokens, s); } tokens[ntokens] = NULL; //bb_info_msg("N[%d]", ntokens); // analyse tokens type = find_token(tokens, "Content-Type:", "text/plain"); //bb_info_msg("T[%s]", type); if (0 == strncasecmp(type, "multipart/", 10)) { if (0 == strcasecmp(type+10, "mixed")) { parse(xfind_token(tokens, "boundary="), argv); } else bb_error_msg_and_die("no support of content type '%s'", type); } else { pid_t pid = pid; int rc; FILE *fp; // fetch charset const char *charset = find_token(tokens, "charset=", CONFIG_FEATURE_MIME_CHARSET); // fetch encoding const char *encoding = find_token(tokens, "Content-Transfer-Encoding:", "7bit"); // compose target filename char *filename = (char *)find_token(tokens, "filename=", NULL); if (!filename) filename = xasprintf(uniq, monotonic_us()); else filename = bb_get_last_path_component_strip(xstrdup(filename)); // start external helper, if any if (opts & OPT_X) { int fd[2]; xpipe(fd); pid = vfork(); if (0 == pid) { // child reads from fd[0] xdup2(fd[0], STDIN_FILENO); close(fd[0]); close(fd[1]); xsetenv("CONTENT_TYPE", type); xsetenv("CHARSET", charset); xsetenv("ENCODING", encoding); xsetenv("FILENAME", filename); BB_EXECVP(*argv, argv); _exit(EXIT_FAILURE); } // parent dumps to fd[1] close(fd[0]); fp = fdopen(fd[1], "w"); signal(SIGPIPE, SIG_IGN); // ignore EPIPE // or create a file for dump } else { char *fname = xasprintf("%s%s", *argv, filename); fp = xfopen_for_write(fname); free(fname); } // housekeeping free(filename); // dump to fp if (0 == strcasecmp(encoding, "base64")) { decode_base64(stdin, fp); } else if (0 != strcasecmp(encoding, "7bit") && 0 != strcasecmp(encoding, "8bit")) { // quoted-printable, binary, user-defined are unsupported so far bb_error_msg_and_die("no support of encoding '%s'", encoding); } else { // N.B. we have written redundant \n. so truncate the file // The following weird 2-tacts reading technique is due to // we have to not write extra \n at the end of the file // In case of -x option we could truncate the resulting file as // fseek(fp, -1, SEEK_END); // if (ftruncate(fileno(fp), ftell(fp))) // bb_perror_msg("ftruncate"); // But in case of -X we have to be much more careful. There is // no means to truncate what we already have sent to the helper. p = xmalloc_fgets_str(stdin, "\r\n"); while (p) { if ((s = xmalloc_fgets_str(stdin, "\r\n")) == NULL) break; if ('-' == s[0] && '-' == s[1] && 0 == strncmp(s+2, boundary, boundary_len)) break; fputs(p, fp); p = s; } /* while ((s = xmalloc_fgetline_str(stdin, "\r\n")) != NULL) { if ('-' == s[0] && '-' == s[1] && 0 == strncmp(s+2, boundary, boundary_len)) break; fprintf(fp, "%s\n", s); } // N.B. we have written redundant \n. so truncate the file fseek(fp, -1, SEEK_END); if (ftruncate(fileno(fp), ftell(fp))) bb_perror_msg("ftruncate"); */ } fclose(fp); // finalize helper if (opts & OPT_X) { signal(SIGPIPE, SIG_DFL); // exit if helper exited >0 rc = wait4pid(pid); if (rc) return rc+20; } // check multipart finalized if (s && '-' == s[2+boundary_len] && '-' == s[2+boundary_len+1]) { free(line); break; } } next: free(line); } //bb_info_msg("ENDPARSE[%s]", boundary); return EXIT_SUCCESS; }
// split a string into individual tokens void tokenise(const std::string &line, const std::string &filename, int line_num, const Options &opt, /*out*/ std::vector<Token> &tokens, bool test_is_raw_python = false // whether --test input is raw python code (vs just a list of boolean expressions) ) { size_t pos = 0; size_t len = line.length(); bool found_assign_op = false; bool ids_can_be_keywords = opt.assign || (opt.test && !test_is_raw_python); while (pos < len) { size_t space_start = pos; // skip whitespace (and finish if end of line or comment) while (std::isspace(char_at(line, pos))) { ++pos; } if (pos >= len || line[pos] == '#') { break; } size_t num_spaces = pos - space_start; size_t tok_start = pos; char ch = line[pos]; char next_ch = char_at(line, pos + 1); token_type type = t_undefined; std::string tok_str; if (opt.assign && found_assign_op) { type = t_string; pos = len; tok_str = trim_spaces(line.substr(tok_start, pos - tok_start)); if (is_quoted(tok_str)) { // make sure quotes are of the right type std::string unquoted_tok_str = tok_str.substr(1, tok_str.length() - 2); tok_str = quote(unquoted_tok_str, '\''); } else if (!is_number(tok_str)) { tok_str = quote(tok_str, '\''); } } else if (is_start_of_id(ch)) { // TODO: maybe allow spaces around array indexes , e.g. "a/b[ 10 ]/c" while (is_id(char_at(line, pos))) { ++pos; } std::string id = line.substr(tok_start, pos - tok_start); check_transform_id(id); if (is_keyword(id) && !(ids_can_be_keywords && is_keyword_allowed_as_id(id))) { tok_str = id; type = t_keyword; } else if (id != kwd_expect && next_nonblank_char(line, pos) == '(') { tok_str = id; type = t_function; } else { tok_str = (opt.demangle ? demangle_id(id, true) : mangle_id(id)); type = t_id; } } else if (std::isdigit(ch) || (ch == '.' && std::isdigit(next_ch)) || (ch == '-' && (std::isdigit(next_ch) || next_ch == '.'))) { bool any_digits = false; if (ch == '-') { ++pos; } while (std::isdigit(char_at(line, pos))) { ++pos; any_digits = true; } if (char_at(line, pos) == '.') { ++pos; while (std::isdigit(char_at(line, pos))) { ++pos; any_digits = true; } } // check for scientific notation // (TODO: merge common code with is_number function) if (any_digits && std::tolower(char_at(line, pos)) == 'e') { size_t pos2 = pos + 1; if (char_at(line, pos2) == '+' || char_at(line, pos2) == '-') { ++pos2; if (std::isdigit(char_at(line, pos2))) { for (pos = pos2 + 1;std::isdigit(char_at(line, pos));++pos) { } } } } if (any_digits) { type = t_number; } else { type = t_operator; pos = tok_start + 1; } // token is a single character ("-" or ".") tok_str = line.substr(tok_start, pos - tok_start); } else if (ch == '\"' || ch == '\'') { for (++pos;pos < len && line[pos] != ch;++pos) { // check for escape character if (line[pos] == '\\') { if (++pos >= len) break; } } if (pos < len) { ++pos; } tok_str = line.substr(tok_start, pos - tok_start); type = t_string; } else { pos += operator_length(line, pos); tok_str = line.substr(tok_start, pos - tok_start); type = t_operator; if (tok_str == "=") { found_assign_op = true; if (opt.test && (!test_is_raw_python || (tokens.size() > 0 && tokens[0].type == t_id && tokens[0].str == kwd_expect))) { tok_str = "=="; } } } tokens.push_back(Token(type, tok_str, num_spaces)); if (!opt.command && tokens.size() == 1) { // Python will complain if the line is indented tokens[0].spaces_before = 0; } } transform_special_tokens(tokens, filename, line_num); if (opt.assign) { // transform var= into var='' if (tokens.size() == 2 && tokens[1].str == "=") { tokens.push_back(Token(t_string, "''", 0)); } } if (opt.test) { force_string_comparison_if_quoted(tokens); } if (opt.command) { // add any missing ":" at the end of the line if (tokens.size() > 0 && tokens[0].type == t_keyword) { // check if there is a ":" anywhere in the line if (find_token(tokens, t_operator, ":") == -1) { if (tokens[0].str == "if" || tokens[0].str == "else" || tokens[0].str == "elif" || tokens[0].str == "while" || tokens[0].str == "for" || tokens[0].str == "try" || tokens[0].str == "except" || tokens[0].str == "finally") { tokens.push_back(Token(t_operator, ":", 0)); } } } } }
int find_first_data_track(const char *cue_path, int32_t *offset, char *track_path, size_t max_len) { int rv; char tmp_token[MAX_TOKEN_LEN]; RFILE *fd = filestream_open(cue_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open CUE file '%s': %s\n", cue_path, strerror(errno)); return -errno; } RARCH_LOG("Parsing CUE file '%s'...\n", cue_path); tmp_token[0] = '\0'; while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) { if (!string_is_empty(tmp_token)) { if (string_is_equal_fast(tmp_token, "FILE", 4)) { char cue_dir[PATH_MAX_LENGTH]; cue_dir[0] = '\0'; fill_pathname_basedir(cue_dir, cue_path, sizeof(cue_dir)); get_token(fd, tmp_token, MAX_TOKEN_LEN); fill_pathname_join(track_path, cue_dir, tmp_token, max_len); } else if (string_is_equal_fast(tmp_token, "TRACK", 5)) { int m, s, f; get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (string_is_equal_fast(tmp_token, "AUDIO", 5)) continue; find_token(fd, "INDEX"); get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3) { RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token); filestream_close(fd); return -errno; } *offset = ((m * 60) * (s * 75) * f) * 25; RARCH_LOG("%s '%s+%d'\n", msg_hash_to_str(MSG_FOUND_FIRST_DATA_TRACK_ON_FILE), track_path, *offset); rv = 0; goto clean; } } } rv = -EINVAL; clean: filestream_close(fd); return rv; }
void *cvar_alloc_handle(const char *cvar_parameters, void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr)) { cvar_token_t *list_head;; cvar_token_t *t; handle_t handle; handle_t *state = NULL; int ret = 0; cvar_trace("entry"); /* Tokenize parameters supplied by filebench. */ list_head = NULL; ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER, DEFAULT_KEY_VALUE_DELIMITER, &list_head); if (ret) goto out; /* Get the value of lower, upper and mode. */ t = find_token(list_head, RT_LOWER); if (t && t->value) { t->used = 1; handle.lower = atof(t->value); } else handle.lower = RT_LOWER_DEFAULT; t = find_token(list_head, RT_UPPER); if (t && t->value) { t->used = 1; handle.upper = atof(t->value); } else handle.upper = RT_UPPER_DEFAULT; t = find_token(list_head, RT_MODE); if (t && t->value) { t->used = 1; handle.mode = atof(t->value); } else handle.mode = RT_MODE_DEFAULT; cvar_trace("lower = %lf, upper = %lf, mode = %lf", handle.lower, handle.upper, handle.mode); /* Validate parameters. */ if (handle.upper < handle.lower) { cvar_log_error("Invalid parameter values: lower = %lf and upper = %lf. " "upper must be greater than lower", handle.lower, handle.upper); goto out; } if ((handle.mode > handle.upper) || (handle.mode < handle.lower)) { cvar_log_error("Invalid parameter values: lower = %lf, mode = %lf and " "upper = %lf. mode must be between lower and upper", handle.lower, handle.mode, handle.upper); goto out; } /* Check if there are unused tokens. */ t = unused_tokens(list_head); if (t) { cvar_log_error("Unsupported parameter %s", t->key); goto out; } /* Seed the state. */ mts_goodseed(&handle.state); /* All set. Now allocate space for the handle in the shared segment and * copy the state over. */ state = (handle_t *) cvar_malloc(sizeof(handle_t)); if (!state) { cvar_log_error("Out of memory"); goto out; } *state = handle; out: free_tokens(list_head); cvar_trace("exit"); return state; }
fs::path Path::MakeRelative(fs::path const& path, std::string const& token) const { int idx = find_token(token.c_str(), token.size()); if (idx == -1) throw agi::InternalError("Bad token: " + token); return MakeRelative(path, paths[idx]); }
fs::path Path::Decode(std::string const& path) const { int idx = find_token(path.c_str(), path.size()); if (idx == -1 || paths[idx].empty()) return fs::path(path).make_preferred(); return (paths[idx]/path.substr(strlen(tokens[idx]))).make_preferred(); }
/* This driver uses INST_IO links, * the string has to be of the format * * @PLC tag flags * * count: number of array elements to read (only != 1 for waveform record) * bits: if >0 indicates that we want binary data, * then it's the number of bits */ static long analyze_link(dbCommon *rec, EIPCallback cbtype, const DBLINK *link, size_t count, size_t bits) { DevicePrivate *pvt = (DevicePrivate *)rec->dpvt; char *p, *end; size_t i, tag_len, last_element, bit=0; unsigned long mask; double period = 0.0; eip_bool single_element = false; SpecialOptions special = 0; if (! EIP_strdup(&pvt->link_text, link->value.instio.string, strlen (link->value.instio.string))) { errlogPrintf("devEtherIP (%s): Cannot copy link\n", rec->name); return S_dev_noMemory; } /* Find PLC */ p = find_token(pvt->link_text, &end); if (! p) { errlogPrintf("devEtherIP (%s): Missing PLC in link '%s'\n", rec->name, pvt->link_text); return S_db_badField; } if (! EIP_strdup(&pvt->PLC_name, p, end-p)) { errlogPrintf("devEtherIP (%s): Cannot copy PLC\n", rec->name); return S_dev_noMemory; } /* Find Tag */ p = find_token(end, &end); if (! p) { errlogPrintf("devEtherIP (%s): Missing tag in link '%s'\n", rec->name, pvt->link_text); return(S_db_badField); } tag_len = end-p; if (! EIP_strdup(&pvt->string_tag, p, tag_len)) { errlogPrintf("devEtherIP (%s): Cannot copy tag\n", rec->name); return S_dev_noMemory; } /* Check for more flags */ while ((p = find_token(end, &end))) { for (i=0, mask=1; mask < SPCO_INVALID; ++i, mask=mask<<1) { if (strncmp(p, special_options[i].text, special_options[i].len) == 0) { special |= mask; if (mask==SPCO_READ_SINGLE_ELEMENT) { if (count != 1) { errlogPrintf("devEtherIP (%s): " "Array record cannot use 'E' flag " "('%s')\n", rec->name, pvt->link_text); return S_db_badField; } single_element = true; } else if (mask==SPCO_SCAN_PERIOD) { period = strtod(p+2, &end); if (end==p || period==HUGE_VAL || period==-HUGE_VAL) { errlogPrintf("devEtherIP (%s): " "Error in scan flag in link '%s'\n", rec->name, pvt->link_text); return S_db_badField; } } else if (mask==SPCO_BIT) { bit = strtod(p+2, &end); if (end==p || period==HUGE_VAL || period==-HUGE_VAL) { errlogPrintf("devEtherIP (%s): " "Error in bit flag in link '%s'\n", rec->name, pvt->link_text); return S_db_badField; } } break; } } if (mask >= SPCO_INVALID) { errlogPrintf("devEtherIP (%s): Invalid flag '%s' in link '%s'\n", rec->name, p, pvt->link_text); return S_db_badField; } } pvt->special = special; if (period <= 0.0) /* no scan flag-> get SCAN field: */ { period = get_period(rec); if (period <= 0) period = drvEtherIP_default_rate; if (period <= 0) { errlogPrintf("devEtherIP (%s): cannot decode SCAN field," " no scan flag given\n", rec->name); period = 1.0; /* default scan rate */ errlogPrintf("Device support will use the default of %g secs, ", period); errlogPrintf("please complete the record configuration\n"); } } /* Parsed link_text into PLC_name, string_tag, special flags. * Analyse further */ pvt->element = 0; p = &pvt->string_tag[tag_len-1]; if (*p == ']') /* array tag? */ { if (! single_element) { /* Cut "array_tag[el]" into "array_tag" + el */ while (p > pvt->string_tag) if (*(--p) == '[') break; if (p <= pvt->string_tag) { errlogPrintf("devEtherIP (%s): malformed array tag in '%s'\n", rec->name, pvt->link_text); return S_db_badField; } /* read element number */ pvt->element = strtol(p+1, &end, 0); if (end==p+1 || pvt->element==LONG_MAX || pvt->element==LONG_MIN) { errlogPrintf("devEtherIP (%s): malformed array tag in '%s'\n", rec->name, pvt->link_text); return S_db_badField; } /* remove element number text from tag */ *p = '\0'; } } pvt->plc = drvEtherIP_find_PLC(pvt->PLC_name); if (! pvt->plc) { errlogPrintf("devEtherIP (%s): unknown PLC '%s'\n", rec->name, pvt->PLC_name); return S_db_badField; } if (count > 1 && (bits > 0 || (special & SPCO_BIT))) { errlogPrintf("devEtherIP (%s): cannot access bits for array records\n", rec->name); return S_db_badField; } /* For Element==0 the following makes no difference, only * for binary records (bits=1 or more) * Options: * a) assume BOOL array (default) * b) non-BOOL, SPCO_BIT selected a bit in INT, DINT, ... */ if (bits>0 && !(special & SPCO_BIT)) { /* For element>0, assume that it's a BOOL array, * so the data is packed into UDINTs (CIP "BITS"). * The actual element requested is the UDINT index, * not the bit#. * Pick the bits within the UDINT via the mask. */ pvt->mask = 1U << (pvt->element & 0x1F); /* 0x1F == 31 */ last_element = pvt->element + bits - 1; pvt->element >>= 5; last_element >>= 5; }
void *cvar_alloc_handle(const char *cvar_parameters, void *(*cvar_malloc)(size_t size), void (*cvar_free)(void *cvar_ptr)) { cvar_token_t *list_head;; cvar_token_t *t; handle_t handle; handle_t *state = NULL; int ret = 0; cvar_trace("entry"); /* Tokenize parameters supplied by filebench. */ list_head = NULL; ret = tokenize(cvar_parameters, DEFAULT_PARAMETER_DELIMITER, DEFAULT_KEY_VALUE_DELIMITER, &list_head); if (ret) goto out; /* Get the value of shape and rate. */ t = find_token(list_head, RER_SHAPE); if (t && t->value) { t->used = 1; handle.shape = atoi(t->value); } else handle.shape = RER_SHAPE_DEFAULT; t = find_token(list_head, RER_RATE); if (t && t->value) { t->used = 1; handle.rate = atof(t->value); } else handle.rate = RER_RATE_DEFAULT; cvar_trace("shape = %d, rate = %lf", handle.shape, handle.rate); /* Validate parameters. */ if (handle.shape < 0) { cvar_log_error("Invalid parameter value: shape = %d. shape is a " "non-zero positive integer", handle.shape); goto out; } if (handle.rate < 0) { cvar_log_error("Invalid parameter value: rate = %lf. rate is a " "non-zero positive rational number", handle.rate); goto out; } t = unused_tokens(list_head); if (t) { cvar_log_error("Unsupported parameter %s", t->key); goto out; } /* Seed the state. */ mts_goodseed(&handle.state); /* All set. Now allocate space for the handle in the shared segment and * copy the state over. */ state = (handle_t *) cvar_malloc(sizeof(handle_t)); if (!state) { cvar_log_error("Out of memory"); goto out; } *state = handle; out: free_tokens(list_head); cvar_trace("exit"); return state; }