static Token *savePreviousToken(Token_state *ts, TokensTable *tt) { // save the previous token Token *temp_tk = createTokenByLen(ts->begin, ts->cur - ts->begin); if(!temp_tk) { error_process(ts, Error_type_not_enough_mem); return NULL; } else { if(!addTokenToTable(tt, temp_tk)) { freeToken(temp_tk); return NULL; } else { ts->state = Lex_state_begin; cc_skip_space((const char **)&ts->cur); ts->begin = ts->cur; return temp_tk; } } }
static int command_absolute(char **argv, t_sh *datas) { pid_t pid; int ret; pid = fork(); if (pid > 0) { wait(&ret); datas->ret_err = ret & 0xFF; datas->ret_val = (ret & 0xFF00) >> 8; if (datas->ret_err) error_process(datas->ret_err); else ft_printf("%{F}(%d)%{!F} ", (datas->ret_val) ? 1 : 2, (int)datas->ret_val); }
// an optimized getting token method that can get the token type after the process Token * getNextToken_Optimized(Token_state *ts, TokensTable *tt) { cc_skip_space((const char **)&ts->cur); while(*ts->cur) { // alpha if(isalpha(*ts->cur)) { if(ts->state == Lex_state_begin || ts->state == Lex_state_id) { ts->state = Lex_state_id; ts->sub_state = Lex_sub_state_alpha; goto will_continue; } else if((*ts->cur >= 'a' && *ts->cur <= 'f') || (*ts->cur >= 'A' && *ts->cur <= 'F')) { if(ts->sub_state == Lex_sub_state_hex_num) { goto will_continue; } } else { } } else if(*ts->cur == '_') // '_' { if(ts->state == Lex_state_begin || ts->state == Lex_state_id) { ts->state = Lex_state_id; ts->sub_state = Lex_sub_state_underline; goto will_continue; } } else if(isdigit(*ts->cur)) // digit { if(ts->state == Lex_state_begin) { if(*ts->cur == '0') { ts->state = Lex_state_literal_num; ts->sub_state = Lex_sub_state_oct_num; goto will_continue; } else { ts->state = Lex_state_literal_num; ts->sub_state = Lex_sub_state_dec_num; goto will_continue; } } else if(ts->sub_state == Lex_sub_state_dec_num) { ts->state = Lex_state_literal_num; ts->sub_state = Lex_sub_state_dec_num; goto will_continue; } else if(ts->sub_state == Lex_sub_state_oct_num) { if(*ts->cur >= '8') { error_process(ts, Error_type_oct_include_over_7_num); return false; } else { ts->state = Lex_state_literal_num; ts->sub_state = Lex_sub_state_oct_num; goto will_continue; } } else if(ts->state == Lex_state_id) { goto will_continue; } else if(ts->state == Lex_sub_state_hex_num) { goto will_continue; } else { } } else if(isspace(*ts->cur)) // ' ', '\t', '\n', '\v',... { if(ts->state != Lex_state_begin) { return savePreviousToken(ts, tt); } else { cc_skip_space((const char **)&ts->cur); ts->begin = ts->cur; continue; } } else if(isOpeChar(ts->cur)) // operator , '<', '=' and so on { if(ts->state == Lex_state_begin) { ts->state = Lex_state_op; goto will_continue; } else if(ts->state == Lex_state_op) { if(isOpeChar_n(ts->begin, ts->cur - ts->begin + 1)) { ts->state = Lex_state_op; goto will_continue; } else { return savePreviousToken(ts, tt); } } } else if(*ts->cur == ';') { if(ts->state != Lex_state_begin) { return savePreviousToken(ts, tt); } else { ++ts->cur; return savePreviousToken(ts, tt); } } will_continue: ++ts->cur; continue; } if(*ts->cur == '\0' && ts->cur != ts->begin) { return savePreviousToken(ts, tt); } return NULL; }