string simplifyPath(string path) { vector<string> buf; string token; for (int i = 0; i < path.length(); ++i) { if (path[i] == '/') { process_token(buf, token); } else { token += path[i]; } } process_token(buf, token); string ans; for (int i = 0; i < buf.size(); ++i) { ans += "/" + buf[i]; } if (ans.empty()) { ans = "/" ; } return ans; }
inline bool operator()(const std::string& data, unsigned char octet[4]) { typedef std::pair<const char*,const char*> iterator_type; typedef iterator_type* iterator_type_ptr; iterator_type token[4]; if (4 != strtk::split_n(predicate_,data,4,token)) return false; if (!process_token(token[0],octet[0])) return false; if (!process_token(token[1],octet[1])) return false; if (!process_token(token[2],octet[2])) return false; if (!process_token(token[3],octet[3])) return false; return true; }
static int showdisplay(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *tok, dispname[80]; int line = 0, flag = 0, cmd = 3; struct adsi_display *disp; /* Get display */ if (!(tok = get_token(&args, script, lineno)) || process_token(dispname, tok, sizeof(dispname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid display name: %s at line %d of %s\n", tok ? tok : "<nothing>", lineno, script); return 0; } if (!(disp = getdisplaybyname(state, dispname, script, lineno, 0))) { ast_log(LOG_WARNING, "Display '%s' is undefined at line %d of %s\n", dispname, lineno, script); return 0; } if (!(tok = get_token(&args, script, lineno)) || strcasecmp(tok, "AT")) { ast_log(LOG_WARNING, "Missing token 'AT' at line %d of %s\n", lineno, script); return 0; } /* Get line number */ if (!(tok = get_token(&args, script, lineno)) || process_token(&line, tok, sizeof(line), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid line: '%s' at line %d of %s\n", tok ? tok : "<nothing>", lineno, script); return 0; } if ((tok = get_token(&args, script, lineno)) && !strcasecmp(tok, "NOUPDATE")) { cmd = 1; tok = get_token(&args, script, lineno); } if (tok && !strcasecmp(tok, "UNLESS")) { /* Check for trailing UNLESS flag */ if (!(tok = get_token(&args, script, lineno))) ast_log(LOG_WARNING, "Missing argument for UNLESS clause at line %d of %s\n", lineno, script); else if (process_token(&flag, tok, sizeof(flag), ARG_NUMBER)) ast_log(LOG_WARNING, "Invalid flag number '%s' at line %d of %s\n", tok, lineno, script); if ((tok = get_token(&args, script, lineno))) ast_log(LOG_WARNING, "Extra arguments after UNLESS clause: '%s' at line %d of %s\n", tok, lineno, script); } buf[0] = id; buf[1] = (cmd << 6) | (disp->id & 0x3f); buf[2] = ((line & 0x1f) << 3) | (flag & 0x7); return 3; }
static int goto_line_rel(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *dir = get_token(&args, script, lineno); char *gline = get_token(&args, script, lineno); int line; unsigned char cmd; if (!dir || !gline) { ast_log(LOG_WARNING, "Expecting direction and number of lines for GOTOLINEREL at line %d of %s\n", lineno, script); return 0; } if (!strcasecmp(dir, "UP")) cmd = 0; else if (!strcasecmp(dir, "DOWN")) cmd = 0x20; else { ast_log(LOG_WARNING, "Expecting either 'UP' or 'DOWN' direction, got '%s' at line %d of %s\n", dir, lineno, script); return 0; } if (process_token(&line, gline, sizeof(line), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid line number '%s' at line %d of %s\n", gline, lineno, script); return 0; } cmd |= line; buf[0] = 0x8c; buf[1] = cmd; return 2; }
static int clearflag(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *tok = get_token(&args, script, lineno); struct adsi_flag *flag; char sname[80]; if (!tok) { ast_log(LOG_WARNING, "Clearing flag requires a flag number at line %d of %s\n", lineno, script); return 0; } if (process_token(sname, tok, sizeof(sname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid flag '%s' at line %d of %s\n", tok, lineno, script); return 0; } if (!(flag = getflagbyname(state, sname, script, lineno, 0))) { ast_log(LOG_WARNING, "Flag '%s' is undeclared at line %d of %s\n", sname, lineno, script); return 0; } buf[0] = id; buf[1] = ((flag->id & 0x7) << 4); return 2; }
size_t parse_block(bool (*process_token) (const char*, const char*, Data*), const char* terminator, const char* str, Data* data) { data->reset(); data->valid = false; const size_t LONGEST_LINE = 0x7F; static char buf[LONGEST_LINE+1] = {'\0'}; size_t i = 0; while (1) { char c = '\0'; size_t n = 0; while ((c = str[i++]) != '\0' && c != '\n' && n < LONGEST_LINE) buf[n++] = c; buf[n] = '\0'; GS_ASSERT(c == '\n'); if (c != '\n') return i; if (strcmp(buf, terminator) == 0) break; GS_ASSERT(n > TAG_LENGTH + TAG_DELIMITER_LENGTH); if (n <= TAG_LENGTH + TAG_DELIMITER_LENGTH) return i; GS_ASSERT(buf[TAG_LENGTH] == TAG_DELIMITER[0]); if (buf[TAG_LENGTH] != TAG_DELIMITER[0]) return i; buf[TAG_LENGTH] = '\0'; const char* key = &buf[0]; const char* token = &buf[TAG_LENGTH+1]; if (!process_token(key, token, data)) return i; } data->valid = true; return i; }
static int send_dtmf(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char dtmfstr[80], *a; int bytes = 0; if (!(a = get_token(&args, script, lineno))) { ast_log(LOG_WARNING, "Expecting something to send for SENDDTMF at line %d of %s\n", lineno, script); return 0; } if (process_token(dtmfstr, a, sizeof(dtmfstr) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid token for SENDDTMF at line %d of %s\n", lineno, script); return 0; } a = dtmfstr; while (*a) { if (strchr(validdtmf, *a)) { *buf = *a; buf++; bytes++; } else ast_log(LOG_WARNING, "'%c' is not a valid DTMF tone at line %d of %s\n", *a, lineno, script); a++; } return bytes; }
static int goto_line(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *page = get_token(&args, script, lineno); char *gline = get_token(&args, script, lineno); int line; unsigned char cmd; if (!page || !gline) { ast_log(LOG_WARNING, "Expecting page and line number for GOTOLINE at line %d of %s\n", lineno, script); return 0; } if (!strcasecmp(page, "INFO")) cmd = 0; else if (!strcasecmp(page, "COMM")) cmd = 0x80; else { ast_log(LOG_WARNING, "Expecting either 'INFO' or 'COMM' page, got got '%s' at line %d of %s\n", page, lineno, script); return 0; } if (process_token(&line, gline, sizeof(line), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid line number '%s' at line %d of %s\n", gline, lineno, script); return 0; } cmd |= line; buf[0] = 0x8b; buf[1] = cmd; return 2; }
static int showkeys(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *tok, newkey[80]; int bytes, x, flagid = 0; unsigned char keyid[6]; struct adsi_soft_key *key; struct adsi_flag *flag; for (x = 0; x < 7; x++) { /* Up to 6 key arguments */ if (!(tok = get_token(&args, script, lineno))) break; if (!strcasecmp(tok, "UNLESS")) { /* Check for trailing UNLESS flag */ if (!(tok = get_token(&args, script, lineno))) ast_log(LOG_WARNING, "Missing argument for UNLESS clause at line %d of %s\n", lineno, script); else if (process_token(newkey, tok, sizeof(newkey) - 1, ARG_STRING)) ast_log(LOG_WARNING, "Invalid flag name '%s' at line %d of %s\n", tok, lineno, script); else if (!(flag = getflagbyname(state, newkey, script, lineno, 0))) ast_log(LOG_WARNING, "Flag '%s' is undeclared at line %d of %s\n", newkey, lineno, script); else flagid = flag->id; if ((tok = get_token(&args, script, lineno))) ast_log(LOG_WARNING, "Extra arguments after UNLESS clause: '%s' at line %d of %s\n", tok, lineno, script); break; } if (x > 5) { ast_log(LOG_WARNING, "Only 6 keys can be defined, ignoring '%s' at line %d of %s\n", tok, lineno, script); break; } if (process_token(newkey, tok, sizeof(newkey) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid token for key name: %s\n", tok); continue; } if (!(key = getkeybyname(state, newkey, script, lineno))) break; keyid[x] = key->id; } buf[0] = id; buf[1] = (flagid & 0x7) << 3 | (x & 0x7); for (bytes = 0; bytes < x; bytes++) buf[bytes + 2] = keyid[bytes]; return 2 + x; }
HYD_status HYDU_parse_hostfile(const char *hostfile, struct HYD_node **node_list, HYD_status(*process_token) (char *token, int newline, struct HYD_node ** node_list)) { char line[HYD_TMP_STRLEN], **tokens; FILE *fp; int i; HYD_status status = HYD_SUCCESS; HYDU_FUNC_ENTER(); if ((fp = fopen(hostfile, "r")) == NULL) HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "unable to open host file: %s\n", hostfile); if (node_list) *node_list = NULL; while (fgets(line, HYD_TMP_STRLEN, fp)) { char *linep = NULL; linep = line; strtok(linep, "#"); while (isspace(*linep)) linep++; /* Ignore blank lines & comments */ if ((*linep == '#') || (*linep == '\0')) continue; tokens = HYDU_str_to_strlist(linep); if (!tokens) HYDU_ERR_SETANDJUMP(status, HYD_INTERNAL_ERROR, "Unable to convert host file entry to strlist\n"); for (i = 0; tokens[i]; i++) { status = process_token(tokens[i], !i, node_list); HYDU_ERR_POP(status, "unable to process token\n"); } HYDU_free_strlist(tokens); MPL_free(tokens); } fclose(fp); fn_exit: HYDU_FUNC_EXIT(); return status; fn_fail: goto fn_exit; }
void DialogParser::run() { bool quoted, backslash, endofline; int c; while(true) // this thread will be ended up by terminate() call from the destructor { quoted=backslash=endofline=false; do { if(buffer_index!=token) token=++buffer_index; while ((c=fgetc(input))!=EOF) { if(isspace(c) && !quoted) { if(isblank(c)) { if(buffer_index==token) continue; else break; } else { if(!backslash) { endofline=true; break; } else { backslash=false; continue; } } } if(c=='"' && buffer_index==token && !quoted && !backslash) { quoted=true; continue; } if(c=='"' && quoted && !backslash) { quoted=false; break; } if(c=='\\' && !backslash) { backslash=true; continue; } if(backslash && c!='"') buffer[buffer_index++]='\\'; backslash=false; buffer[buffer_index++]=c; if(buffer_index>=BUFFER_SIZE-2) break; // potentially we need space // for backslash and terminating zero } if(backslash) buffer[buffer_index++]='\\'; buffer[buffer_index]='\0'; process_token(); if(endofline) { issue_command(); endofline=false; } } while(c!=EOF); issue_command(); msleep(50); // this call is to reduce the CPU time consumption } }
static void process_file(object list, char *file) { char buf[256], *p; object fobj; fobj = gOpenFile(File, file, "r"); if (!fobj) { vPrintf(stdoutStream, "Can't open %s\n", file); exit(1); } while (gGets(fobj, buf, sizeof buf)) for (p=buf ; *(p=process_token(p, list)) ; ); gDispose(fobj); }
int ToTeX::process(char* name) { cout << " +++ Opening Input File: " << name << NL; infile.open(name,ios::in); if (!infile) return NOINFILE; outfile << "\\newpage " << NL; outfile << "\\markboth{" << name << "}{" << name << "}" << NL; if (read_token()) { if (strcmp(inbuf,"/*TEX")==0) tex_mode = ON; else { if (tex_mode == ON) texmode_off(); outfile << process_token(inbuf); } } while(read_token()) { outfile << process_token(inbuf); if (delim != EOS) outfile << delim; } infile.close(); if (tex_mode == OFF) outfile << "\\end{verbatim} " << endl; return SUCCESS; }
void parse_line(bool (*process_token) (const char*, const char*, Data*), char* str, size_t length, Data* data) { data->valid = false; GS_ASSERT(length); if (!length) return; // walk the string backwards size_t token_length = 0; for (int i=(int)length-1; i>=0; i--) { char c = str[i]; if (c == ' ') str[i] = '\0'; // convert all spaces to NUL so that padded strings get shortened if (i && c != PROPERTY_DELIMITER[0]) { token_length++; continue; } // make sure at least the key + delimiter fits GS_ASSERT(token_length >= TAG_LENGTH + TAG_DELIMITER_LENGTH); if (token_length < TAG_LENGTH + TAG_DELIMITER_LENGTH) return; char* key = NULL; if (i) { // we are pointing at a token delimiter, the key starts on the next line str[i] = '\0'; key = &str[i+1]; } else // we are at position 0, we can't go back any further key = &str[i]; // check that the tag delimiter is correct GS_ASSERT(key[TAG_LENGTH] == TAG_DELIMITER[0]); if (key[TAG_LENGTH] != TAG_DELIMITER[0]) return; // replace the tag delimiter with NUL key[TAG_LENGTH] = '\0'; // index to the value char* val = &key[TAG_LENGTH + TAG_DELIMITER_LENGTH]; if (!process_token(key, val, data)) return; token_length = 0; } data->valid = true; }
int main(int argc, char **argv) { const char *filename; scanstate scanner, *ss = &scanner; char buffer[BUFSIZ]; int token; FILE *fp; filename = argv[1]; if(filename == NULL) { fprintf(stderr, "You must supply the file to open!\n"); exit(1); } fp = fopen(filename, "r"); if(!fp) { fprintf(stderr, "Could not open file %s\n", filename); exit(1); } // Create the scanner scanstate_init(ss, buffer, sizeof(buffer)); readfp_attach(ss, fp); // attach the readproc scanner_attach(ss); // attach the scanner do { token = scan_next_token(ss); if(token < 0) { printf("Scanner's readproc returned an error: %d\n", token); break; } process_token(filename, ss->line, token, scan_token_start(ss), scan_token_length(ss)); } while(token); // No need to deallocate the scanner because everything // was allocated on the stack. fclose(fp); return 0; }
void lexer_process(void) { token_t token; token_t (*process_token)(void) = NULL; if (_.eoi_reached) return; _scan(); _flush_buffer(); switch (_cur_char()) { case '\0': process_token = _process_eoi; _.eoi_reached = true; break; case SIGIL_STR_VAR: case SIGIL_INT_VAR: case SIGIL_BOOL_VAR: process_token = _process_variable; break; case DELIMITER_STR: process_token = _process_string; break; default: // identifer if (_char_is_alpha(_cur_char())) { process_token = _process_identifier; break; } // literal unsigned integer without prefix sign if (_char_is_digit(_cur_char())) { process_token = _process_luinteger; break; } // unknown process_token = _process_unknown; } token = process_token(); _set_token(token); }
std::pair<bool, file_md5_anim::data_anim> file_md5_anim::process( std::string file_path ){ data_anim d {}; std::fstream f( file_path, std::fstream::in ); std::pair< file_md5_common::token, std::string> t = file_md5_common::get_token( _keyword_map, f ); while( file_md5_common::token::END != t.first ){ if( file_md5_common::token::INVALID == t.first ){ assert( 0 && "invalid token encountered" ); break; } if( !process_token( t, f, (void*)&d ) ){ return { false, {} }; } t = file_md5_common::get_token( _keyword_map, f ); } if( !check_consistency( d ) ){ return { false, {} }; } return std::pair<bool, data_anim>( true, std::move(d) ); }
scoped_handle CreateLowboxToken() { PSID package_sid_p; if (!ConvertStringSidToSid(L"S-1-15-2-1-1-1-1-1-1-1-1-1-1-1", &package_sid_p)) { printf("[ERROR] creating SID: %d\n", GetLastError()); return nullptr; } local_free_ptr package_sid(package_sid_p); HANDLE process_token_h; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &process_token_h)) { printf("[ERROR] error opening process token SID: %d\n", GetLastError()); return nullptr; } scoped_handle process_token(process_token_h); NtCreateLowBoxToken fNtCreateLowBoxToken = (NtCreateLowBoxToken)GetProcAddress(GetModuleHandle(L"ntdll"), "NtCreateLowBoxToken"); HANDLE lowbox_token_h; OBJECT_ATTRIBUTES obja = {}; obja.Length = sizeof(obja); NTSTATUS status = fNtCreateLowBoxToken(&lowbox_token_h, process_token_h, TOKEN_ALL_ACCESS, &obja, package_sid_p, 0, nullptr, 0, nullptr); if (status != 0) { printf("[ERROR] creating lowbox token: %08X\n", status); return nullptr; } scoped_handle lowbox_token(lowbox_token_h); HANDLE imp_token; if (!DuplicateTokenEx(lowbox_token_h, TOKEN_ALL_ACCESS, nullptr, SecurityImpersonation, TokenImpersonation, &imp_token)) { printf("[ERROR] duplicating lowbox: %d\n", GetLastError()); return nullptr; } return scoped_handle(imp_token); }
static int set_state(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno) { char *gstate = get_token(&args, script, lineno); int state; if (!gstate) { ast_log(LOG_WARNING, "Expecting state number at line %d of %s\n", lineno, script); return 0; } if (process_token(&state, gstate, sizeof(state), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid state number '%s' at line %d of %s\n", gstate, lineno, script); return 0; } buf[0] = id; buf[1] = state; return 2; }
static int starttimer(char *buf, char *name, int id, char *args, struct adsi_script *istate, const char *script, int lineno) { char *tok = get_token(&args, script, lineno); int secs; if (!tok) { ast_log(LOG_WARNING, "Missing number of seconds at line %d of %s\n", lineno, script); return 0; } if (process_token(&secs, tok, sizeof(secs), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid number of seconds '%s' at line %d of %s\n", tok, lineno, script); return 0; } buf[0] = id; buf[1] = 0x1; buf[2] = secs; return 3; }
int main(int argc, char **argv) { min_length = MIN_LENGTH; //默认的token长度 max_length = MAX_LENGTH; if (argc == 2) max_length = atoi(argv[1]); else if (argc == 3) { min_length = atoi(argv[1]); max_length = atoi(argv[2]); } if (min_length < MIN_LENGTH || max_length > MAX_LENGTH || min_length > max_length) { usage(); exit(-1); } process_token(); return 0; }
int read_and_process_token(int fd) { struct token *toke; struct fdinfo *inf = &fdinfo[fd]; char *buf = inf->buf; int nr; nr = read(fd, buf, BUFSIZE); check(nr != -1, "Failed to read from fd that was supposedly ready: %d", fd); toke = (struct token *)buf; while (nr >= BUFSIZE) { process_token(fd, toke, BUFSIZE); toke++; nr -= BUFSIZE; } check(nr == 0, "Didn't write everythig to pipe."); return 0; error: return -1; }
static int send_delay(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *gtime = get_token(&args, script, lineno); int ms; if (!gtime) { ast_log(LOG_WARNING, "Expecting number of milliseconds to wait at line %d of %s\n", lineno, script); return 0; } if (process_token(&ms, gtime, sizeof(ms), ARG_NUMBER)) { ast_log(LOG_WARNING, "Invalid delay milliseconds '%s' at line %d of %s\n", gtime, lineno, script); return 0; } buf[0] = 0x90; if (id == 11) buf[1] = ms / 100; else buf[1] = ms / 10; return 2; }
static int subscript(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *tok = get_token(&args, script, lineno); char subscr[80]; struct adsi_subscript *sub; if (!tok) { ast_log(LOG_WARNING, "Missing subscript to call at line %d of %s\n", lineno, script); return 0; } if (process_token(subscr, tok, sizeof(subscr) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid number of seconds '%s' at line %d of %s\n", tok, lineno, script); return 0; } if (!(sub = getsubbyname(state, subscr, script, lineno))) return 0; buf[0] = 0x9d; buf[1] = sub->id; return 2; }
/* * Process token tk */ void pp_push_token(pp_t *pp, void *tk) { process_token(&pp->formatter, tk); }
static int onevent(char *buf, char *name, int id, char *args, struct adsi_script *state, const char *script, int lineno) { char *tok = get_token(&args, script, lineno); char subscr[80], sname[80]; int sawin = 0, event, snums[8], scnt = 0, x; struct adsi_subscript *sub; if (!tok) { ast_log(LOG_WARNING, "Missing event for 'ONEVENT' at line %d of %s\n", lineno, script); return 0; } if ((event = geteventbyname(tok)) < 1) { ast_log(LOG_WARNING, "'%s' is not a valid event name, at line %d of %s\n", args, lineno, script); return 0; } tok = get_token(&args, script, lineno); while ((!sawin && !strcasecmp(tok, "IN")) || (sawin && !strcasecmp(tok, "OR"))) { sawin = 1; if (scnt > 7) { ast_log(LOG_WARNING, "No more than 8 states may be specified for inclusion at line %d of %s\n", lineno, script); return 0; } /* Process 'in' things */ tok = get_token(&args, script, lineno); if (process_token(sname, tok, sizeof(sname), ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid state name at line %d of %s\n", tok, lineno, script); return 0; } if ((snums[scnt] = getstatebyname(state, sname, script, lineno, 0) == NULL)) { ast_log(LOG_WARNING, "State '%s' not declared at line %d of %s\n", sname, lineno, script); return 0; } scnt++; if (!(tok = get_token(&args, script, lineno))) break; } if (!tok || strcasecmp(tok, "GOTO")) { if (!tok) tok = "<nothing>"; if (sawin) ast_log(LOG_WARNING, "Got '%s' while looking for 'GOTO' or 'OR' at line %d of %s\n", tok, lineno, script); else ast_log(LOG_WARNING, "Got '%s' while looking for 'GOTO' or 'IN' at line %d of %s\n", tok, lineno, script); } if (!(tok = get_token(&args, script, lineno))) { ast_log(LOG_WARNING, "Missing subscript to call at line %d of %s\n", lineno, script); return 0; } if (process_token(subscr, tok, sizeof(subscr) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Invalid subscript '%s' at line %d of %s\n", tok, lineno, script); return 0; } if (!(sub = getsubbyname(state, subscr, script, lineno))) return 0; buf[0] = 8; buf[1] = event; buf[2] = sub->id | 0x80; for (x = 0; x < scnt; x++) buf[3 + x] = snums[x]; return 3 + scnt; }
static int adsi_process(struct adsi_script *state, char *buf, const char *script, int lineno) { char *keyword = get_token(&buf, script, lineno); char *args, vname[256], tmp[80], tmp2[80]; int lrci, wi, event; struct adsi_display *disp; struct adsi_subscript *newsub; if (!keyword) return 0; switch(state->state) { case STATE_NORMAL: if (!strcasecmp(keyword, "DESCRIPTION")) { if ((args = get_token(&buf, script, lineno))) { if (process_token(state->desc, args, sizeof(state->desc) - 1, ARG_STRING)) ast_log(LOG_WARNING, "'%s' is not a valid token for DESCRIPTION at line %d of %s\n", args, lineno, script); } else ast_log(LOG_WARNING, "Missing argument for DESCRIPTION at line %d of %s\n", lineno, script); } else if (!strcasecmp(keyword, "VERSION")) { if ((args = get_token(&buf, script, lineno))) { if (process_token(&state->ver, args, sizeof(state->ver) - 1, ARG_NUMBER)) ast_log(LOG_WARNING, "'%s' is not a valid token for VERSION at line %d of %s\n", args, lineno, script); } else ast_log(LOG_WARNING, "Missing argument for VERSION at line %d of %s\n", lineno, script); } else if (!strcasecmp(keyword, "SECURITY")) { if ((args = get_token(&buf, script, lineno))) { if (process_token(state->sec, args, sizeof(state->sec) - 1, ARG_STRING | ARG_NUMBER)) ast_log(LOG_WARNING, "'%s' is not a valid token for SECURITY at line %d of %s\n", args, lineno, script); } else ast_log(LOG_WARNING, "Missing argument for SECURITY at line %d of %s\n", lineno, script); } else if (!strcasecmp(keyword, "FDN")) { if ((args = get_token(&buf, script, lineno))) { if (process_token(state->fdn, args, sizeof(state->fdn) - 1, ARG_STRING | ARG_NUMBER)) ast_log(LOG_WARNING, "'%s' is not a valid token for FDN at line %d of %s\n", args, lineno, script); } else ast_log(LOG_WARNING, "Missing argument for FDN at line %d of %s\n", lineno, script); } else if (!strcasecmp(keyword, "KEY")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "KEY definition missing name at line %d of %s\n", lineno, script); break; } if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script); break; } if (!(state->key = getkeybyname(state, vname, script, lineno))) { ast_log(LOG_WARNING, "Out of key space at line %d of %s\n", lineno, script); break; } if (state->key->defined) { ast_log(LOG_WARNING, "Cannot redefine key '%s' at line %d of %s\n", vname, lineno, script); break; } if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) { ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script); break; } if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "KEY definition missing short name at line %d of %s\n", lineno, script); break; } if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY short name at line %d of %s\n", args, lineno, script); break; } if ((args = get_token(&buf, script, lineno))) { if (strcasecmp(args, "OR")) { ast_log(LOG_WARNING, "Expecting 'OR' but got '%s' instead at line %d of %s\n", args, lineno, script); break; } if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "KEY definition missing optional long name at line %d of %s\n", lineno, script); break; } if (process_token(tmp2, args, sizeof(tmp2) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY long name at line %d of %s\n", args, lineno, script); break; } } else { ast_copy_string(tmp2, tmp, sizeof(tmp2)); } if (strlen(tmp2) > 18) { ast_log(LOG_WARNING, "Truncating full name to 18 characters at line %d of %s\n", lineno, script); tmp2[18] = '\0'; } if (strlen(tmp) > 7) { ast_log(LOG_WARNING, "Truncating short name to 7 bytes at line %d of %s\n", lineno, script); tmp[7] = '\0'; } /* Setup initial stuff */ state->key->retstr[0] = 128; /* 1 has the length */ state->key->retstr[2] = state->key->id; /* Put the Full name in */ memcpy(state->key->retstr + 3, tmp2, strlen(tmp2)); /* Update length */ state->key->retstrlen = strlen(tmp2) + 3; /* Put trailing 0xff */ state->key->retstr[state->key->retstrlen++] = 0xff; /* Put the short name */ memcpy(state->key->retstr + state->key->retstrlen, tmp, strlen(tmp)); /* Update length */ state->key->retstrlen += strlen(tmp); /* Put trailing 0xff */ state->key->retstr[state->key->retstrlen++] = 0xff; /* Record initial length */ state->key->initlen = state->key->retstrlen; state->state = STATE_INKEY; } else if (!strcasecmp(keyword, "SUB")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script); break; } if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script); break; } if (!(state->sub = getsubbyname(state, vname, script, lineno))) { ast_log(LOG_WARNING, "Out of subroutine space at line %d of %s\n", lineno, script); break; } if (state->sub->defined) { ast_log(LOG_WARNING, "Cannot redefine subroutine '%s' at line %d of %s\n", vname, lineno, script); break; } /* Setup sub */ state->sub->data[0] = 130; /* 1 is the length */ state->sub->data[2] = 0x0; /* Clear extensibility bit */ state->sub->datalen = 3; if (state->sub->id) { /* If this isn't the main subroutine, make a subroutine label for it */ state->sub->data[3] = 9; state->sub->data[4] = state->sub->id; /* 5 is length */ state->sub->data[6] = 0xff; state->sub->datalen = 7; } if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) { ast_log(LOG_WARNING, "Expecting 'IS', but got '%s' at line %d of %s\n", args ? args : "<nothing>", lineno, script); break; } state->state = STATE_INSUB; } else if (!strcasecmp(keyword, "STATE")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "STATE definition missing name at line %d of %s\n", lineno, script); break; } if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a STATE name at line %d of %s\n", args, lineno, script); break; } if (getstatebyname(state, vname, script, lineno, 0)) { ast_log(LOG_WARNING, "State '%s' is already defined at line %d of %s\n", vname, lineno, script); break; } getstatebyname(state, vname, script, lineno, 1); } else if (!strcasecmp(keyword, "FLAG")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "FLAG definition missing name at line %d of %s\n", lineno, script); break; } if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a FLAG name at line %d of %s\n", args, lineno, script); break; } if (getflagbyname(state, vname, script, lineno, 0)) { ast_log(LOG_WARNING, "Flag '%s' is already defined\n", vname); break; } getflagbyname(state, vname, script, lineno, 1); } else if (!strcasecmp(keyword, "DISPLAY")) { lrci = 0; wi = 0; if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "SUB definition missing name at line %d of %s\n", lineno, script); break; } if (process_token(vname, args, sizeof(vname) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid token for a KEY name at line %d of %s\n", args, lineno, script); break; } if (getdisplaybyname(state, vname, script, lineno, 0)) { ast_log(LOG_WARNING, "State '%s' is already defined\n", vname); break; } if (!(disp = getdisplaybyname(state, vname, script, lineno, 1))) break; if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "IS")) { ast_log(LOG_WARNING, "Missing 'IS' at line %d of %s\n", lineno, script); break; } if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "Missing Column 1 text at line %d of %s\n", lineno, script); break; } if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "Token '%s' is not valid column 1 text at line %d of %s\n", args, lineno, script); break; } if (strlen(tmp) > 20) { ast_log(LOG_WARNING, "Truncating column one to 20 characters at line %d of %s\n", lineno, script); tmp[20] = '\0'; } memcpy(disp->data + 5, tmp, strlen(tmp)); disp->datalen = strlen(tmp) + 5; disp->data[disp->datalen++] = 0xff; args = get_token(&buf, script, lineno); if (args && !process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) { /* Got a column two */ if (strlen(tmp) > 20) { ast_log(LOG_WARNING, "Truncating column two to 20 characters at line %d of %s\n", lineno, script); tmp[20] = '\0'; } memcpy(disp->data + disp->datalen, tmp, strlen(tmp)); disp->datalen += strlen(tmp); args = get_token(&buf, script, lineno); } while (args) { if (!strcasecmp(args, "JUSTIFY")) { args = get_token(&buf, script, lineno); if (!args) { ast_log(LOG_WARNING, "Qualifier 'JUSTIFY' requires an argument at line %d of %s\n", lineno, script); break; } lrci = getjustifybyname(args); if (lrci < 0) { ast_log(LOG_WARNING, "'%s' is not a valid justification at line %d of %s\n", args, lineno, script); break; } } else if (!strcasecmp(args, "WRAP")) { wi = 0x80; } else { ast_log(LOG_WARNING, "'%s' is not a known qualifier at line %d of %s\n", args, lineno, script); break; } args = get_token(&buf, script, lineno); } if (args) { /* Something bad happened */ break; } disp->data[0] = 129; disp->data[1] = disp->datalen - 2; disp->data[2] = ((lrci & 0x3) << 6) | disp->id; disp->data[3] = wi; disp->data[4] = 0xff; } else { ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in PROGRAM\n", keyword); } break; case STATE_INKEY: if (process_returncode(state->key, keyword, buf, state, script, lineno)) { if (!strcasecmp(keyword, "ENDKEY")) { /* Return to normal operation and increment current key */ state->state = STATE_NORMAL; state->key->defined = 1; state->key->retstr[1] = state->key->retstrlen - 2; state->key = NULL; } else { ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SOFTKEY definition at line %d of %s\n", keyword, lineno, script); } } break; case STATE_INIF: if (process_opcode(state->sub, keyword, buf, state, script, lineno)) { if (!strcasecmp(keyword, "ENDIF")) { /* Return to normal SUB operation and increment current key */ state->state = STATE_INSUB; state->sub->defined = 1; /* Store the proper number of instructions */ state->sub->ifdata[2] = state->sub->ifinscount; } else if (!strcasecmp(keyword, "GOTO")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "GOTO clause missing Subscript name at line %d of %s\n", lineno, script); break; } if (process_token(tmp, args, sizeof(tmp) - 1, ARG_STRING)) { ast_log(LOG_WARNING, "'%s' is not a valid subscript name token at line %d of %s\n", args, lineno, script); break; } if (!(newsub = getsubbyname(state, tmp, script, lineno))) break; /* Somehow you use GOTO to go to another place */ state->sub->data[state->sub->datalen++] = 0x8; state->sub->data[state->sub->datalen++] = state->sub->ifdata[1]; state->sub->data[state->sub->datalen++] = newsub->id; /* Terminate */ state->sub->data[state->sub->datalen++] = 0xff; /* Increment counters */ state->sub->inscount++; state->sub->ifinscount++; } else { ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in IF clause at line %d of %s\n", keyword, lineno, script); } } else state->sub->ifinscount++; break; case STATE_INSUB: if (process_opcode(state->sub, keyword, buf, state, script, lineno)) { if (!strcasecmp(keyword, "ENDSUB")) { /* Return to normal operation and increment current key */ state->state = STATE_NORMAL; state->sub->defined = 1; /* Store the proper length */ state->sub->data[1] = state->sub->datalen - 2; if (state->sub->id) { /* if this isn't main, store number of instructions, too */ state->sub->data[5] = state->sub->inscount; } state->sub = NULL; } else if (!strcasecmp(keyword, "IFEVENT")) { if (!(args = get_token(&buf, script, lineno))) { ast_log(LOG_WARNING, "IFEVENT clause missing Event name at line %d of %s\n", lineno, script); break; } if ((event = geteventbyname(args)) < 1) { ast_log(LOG_WARNING, "'%s' is not a valid event\n", args); break; } if (!(args = get_token(&buf, script, lineno)) || strcasecmp(args, "THEN")) { ast_log(LOG_WARNING, "IFEVENT clause missing 'THEN' at line %d of %s\n", lineno, script); break; } state->sub->ifinscount = 0; state->sub->ifdata = state->sub->data + state->sub->datalen; /* Reserve header and insert op codes */ state->sub->ifdata[0] = 0x1; state->sub->ifdata[1] = event; /* 2 is for the number of instructions */ state->sub->ifdata[3] = 0xff; state->sub->datalen += 4; /* Update Subscript instruction count */ state->sub->inscount++; state->state = STATE_INIF; } else { ast_log(LOG_WARNING, "Invalid or Unknown keyword '%s' in SUB definition at line %d of %s\n", keyword, lineno, script); } } break; default: ast_log(LOG_WARNING, "Can't process keyword '%s' in weird state %d\n", keyword, state->state); } return 0; }
/** * Given a program struct, will process the (already opened) input file and * begin compilation line by line. */ void process_input_program(struct program *program){ print_asterisk(GRN_C, stdout); printf("Processing File...\n"); char *tok; char buf[MAX_LINE_LEN+1]; buf[MAX_LINE_LEN] = 0; // parse input file do{ program->line_count++; #ifdef DEBUG fprintf(stderr, "*** Reading Line %d...\n", program->line_count); #endif fgetpos(program->in, &program->str_line); tok = read_next_token(buf, program->in, MAX_LINE_LEN); #ifdef DEBUG fprintf(stderr, "Read Token '%s'\n", tok); #endif if(!tok){ continue; // just a whitespace line, move on } else{ process_token(tok, program); } // the instruction processor should have consumed all relevant tokens, // check if there is garbage at the end of the line if(!program->error_code) check_garbage(program); }while(!check_EOF(program->in) && !program->error_code); if(program->error_code) return; // resolve constants/labels #ifdef DEBUG fprintf(stderr, "LAST TERM: '%s' TRANS: %d\n", program->end_term->term, program->end_term->trans); #endif translate_terms(program->terms, program); // write terms to out file struct Term *t = program->terms; write_terms(t, program); // process warnings if(warnings){ check_warnings(program); } #ifdef DEBUG // why did we quit? if(program->error_code){ fprintf(stderr, "Stopped processing because of an error.\n"); } else if(check_EOF(program->in)){ fprintf(stderr, "Stopped processing because EOF reached.\n"); } #endif }