/* Parse a line of the configuration file. For each keyword recognised, perform appropriate handling. If the keyword is not recognised, print a message to syslog and continue. Returns 0 success, or recoverable config file error non-zero serious system error, processing aborted */ static errcode_t process_config_line ( config_t * conf, char * line, config_file_context_t * context ) { char * curr = line; char * word; word = get_next_word (curr, &curr); if (! word || word [0] == k_comment_char) { // Nothing interesting on this line return 0; } if (strcmp (word, k_keyword_domain) == 0) { word = get_next_word (curr, &curr); if (word) { int errcode = add_domain (conf, word); if (errcode) { // something badly wrong, bail return errcode; } if (get_next_word (curr, NULL)) { LOG(PHIDGET_LOG_WARNING, "%s, line %d: ignored extra text found after domain", context->filename, context->linenum ); } } else { LOG(PHIDGET_LOG_WARNING, "%s, line %d: no domain specified", context->filename, context->linenum ); } } else { LOG(PHIDGET_LOG_WARNING, "%s, line %d: unknown keyword %s - skipping", context->filename, context->linenum, word ); } return 0; }
int get_next_ngram(id__t* id,TBROWSE* br) { int i,k; k=br->k; /* deal with unigram */ if (k==1) return get_next_word(id,br); dprintf(("get_next_ngram(%x,%d): ", (int)(long)br->lm&0xff,k)); /* deal with k gram */ /* If we have reached the last word for position k, we are * done. */ if (br->pos[k-1]>=br->lm->table_sizes[k-1]) { dprintf(("no more %d-grams\n", k)); return 0; } for (i = 0; i < k; ++i) dprintf(("%d ", br->pos[i])); /* Get word IDs */ id[0]=br->pos[0]; dprintf(("%s ", br->lm->vocab[id[0]])); for (i=1;i<k;i++) { id[i]=br->lm->word_id[i][br->pos[i]]; dprintf(("%s ", br->lm->vocab[id[i]])); } dprintf(("\n")); increase_pos(br->pos,br->end,k,br->lm); return 1; }
/*replace all words with synonyms*/ void replace_words(FILE *fp_sc, FILE *fp_dict, char *search_array , char** after_replace) { char word[WORD_SIZE] = ""; int word_location = -1; /*get file length*/ fseek(fp_sc, 0L, SEEK_END); size_t file_size = ftell(fp_sc); fseek(fp_sc, 0L, SEEK_SET); /*in case the synonyms are bigger than origianl word allocate more memory*/ char *whole_file = malloc(file_size*1.5); strcpy(whole_file, ""); while (!feof(fp_sc) && get_next_word(fp_sc, word) != NULL){ if (word != "") { word_location = find_word_index(word, search_array, word_amount); if (word_location >= 0) {/* found */ strcat(whole_file, get_synonym(fp_dict, word_location)); } else { strcat(whole_file, word); } } strcat(whole_file, get_non_alpha(fp_sc)); strcpy(word, ""); /* clear the word*/ } *after_replace = whole_file; }
/* * BOOLEAN get_synonym(FILE *p_dictionary, char *buff) * * Given a file pointer that points to the first character * of the synonym region * * */ static BOOLEAN get_synonym(FILE *p_dictionary, char *buff) { BOOLEAN flag; char s_num_of_synonyms[3]; /* get number of synonyms this word has */ flag = get_next_word(p_dictionary, s_num_of_synonyms); if(flag == false) { if (PRINT_ERROR) { fprintf(stderr, "get_next_word returned false\n"); PRINT_FILE_LINE(); } return false; } int num_of_synonyms = 0; /* convert string to int */ int i; for(i = 0; *(s_num_of_synonyms + i) != NTS; i++) { num_of_synonyms *= 10; num_of_synonyms += (*(s_num_of_synonyms + i) - '0'); } /* generate random number to pick a synonym */ srand(time(NULL)); i = rand() % num_of_synonyms; for(; i > 0; i--) { flag = get_next_word(p_dictionary, buff); if(flag == false) { if (PRINT_ERROR) { fprintf(stderr, "get_next_word returned false\n"); PRINT_FILE_LINE(); } return false; } } flag = get_next_word(p_dictionary, buff); return flag; }
static Bit32s get_next_keymap_line (FILE *fp, char *bxsym, char *modsym, Bit32s *ascii, char *hostsym) { char line[256]; char buf[256]; line[0] = 0; while (1) { lineCount++; if (!fgets(line, sizeof(line)-1, fp)) return -1; // EOF init_parse_line (line); if (get_next_word (bxsym) >= 0) { modsym[0] = 0; char *p; if ((p = strchr (bxsym, '+')) != NULL) { *p = 0; // truncate bxsym. p++; // move one char beyond the + strcpy (modsym, p); // copy the rest to modsym } if (get_next_word (buf) < 0) { BX_PANIC (("keymap line %d: expected 3 columns", lineCount)); return -1; } if (buf[0] == '\'' && buf[2] == '\'' && buf[3]==0) { *ascii = (Bit8u) buf[1]; } else if (!strcmp(buf, "space")) { *ascii = ' '; } else if (!strcmp(buf, "return")) { *ascii = '\n'; } else if (!strcmp(buf, "tab")) { *ascii = '\t'; } else if (!strcmp(buf, "backslash")) { *ascii = '\\'; } else if (!strcmp(buf, "apostrophe")) { *ascii = '\''; } else if (!strcmp(buf, "none")) { *ascii = -1; } else { BX_PANIC (("keymap line %d: ascii equivalent is \"%s\" but it must be char constant like 'x', or one of space,tab,return,none", lineCount, buf)); } if (get_next_word (hostsym) < 0) { BX_PANIC (("keymap line %d: expected 3 columns", lineCount)); return -1; } return 0; } // no words on this line, keep reading. } }
void Segmenter::string_to_characters(const string& content, vector<string>& characters) { int start = 0; while(start < content.size()) { const string& character = get_next_word(content,start); if(character != " ") characters.push_back(character); start += character.size(); } }
int is_dim(char *line, t_screen *screen) { char *word; int i; i = -1; if ((word = get_next_word(line, &i)) && is_equal(word, "screen_dim")) { free(word); if ((word = get_next_word(line, &i))) screen->width = my_getcolor(word, -1); free(word); if ((word = get_next_word(line, &i))) screen->height = my_getcolor(word, -1); free(word); return (1); } free(word); return (0); }
static int merge_cont(int degree) { /* Get the minimum of the top element of each buffer Write it into ioBufs[degree], which is the buffer of output file Refill with the next element of this buffer block Until all the buffer block is empty. */ int ret = 0; topElem = (GIT_T *)malloc(sizeof(GIT_T) * (degree + 1)); if (topElem == NULL) { printf("Malloc memory for topElem failed.\n"); return -1; } memset(topElem, 0, sizeof(GIT_T) * (degree + 1)); // Initiate topElem[i] with the first record of each input file. int i; for (i = 0; i < degree ; i++) { get_next_word(i); } int min = 0; // If all files are done, min = -1. while (min >= 0) { // Get the order of buffer with minimum word_id min = sort_curr(degree); // Copy minimum to output buffer ret = write_min(min, degree); if (ret != 0) { return -1; } // Update record of that buffer get_next_word(min); } free(topElem); return 0; }
int kmp(char *s, char *t) { int *next; int i = 0, j, cond; next = get_next_word(t); while(1) { cond = match_word(s, t, i); if(cond == -1) return i; else if(cond == -2) break; else i += (cond - next[cond]); } return -1; }
/* * char *cmp_words(char *word, FILE *p_dictionary) * * The function compares all the words under a certain * letter to the given string word until there is a reason * to stop. The function returns (enum) BOOL.true if it found a match * or BOOL.false if it failed. * */ static BOOLEAN cmp_words(char *word, FILE *p_dictionary) { /* initializing variables */ char cmp_to[WORD_MAX_LENGTH]; int cmp_diff; while(get_next_word(p_dictionary, cmp_to) != false) { cmp_diff = strcmp(word, cmp_to); if(cmp_diff == 0) {return true;} else if(cmp_diff < 0) {break;} next_line(p_dictionary); } return false; }
/******************************************************************************************* * Trys to find a word that is a 'pair value' of another word. * Example: port = 25; * Here the word '25' is a pair value of word 'port'. * The function assumes that the file pointer fp points to the first character that is * located behind the first word of the pair value (behind 'port'). The function * searches for a '=' character and places the word that comes after * into *val. ********************************************************************************************/ void get_pair_value(char *val, FILE *fp) { char c; skip(fp); c = fgetc(fp); if(c == EOF) { ungetc(c, fp); } else if(c == '=') { get_next_word(val, fp); } else { ungetc(c, fp); } }
ProcessCommandArgs() { char *user_data; char next_word[WORD_LEN + 1]; int num_spaces; user_data = UserData; while (*user_data != '\0') { get_next_word(user_data, next_word, &num_spaces); user_data += (strlen(next_word) + num_spaces); if (strcmp(next_word, "-bb") == 0) { manager_needed = BULLETINBOARD; continue; } if (strcmp(next_word, "-fo") == 0) { manager_needed = FORM; continue; } if (strcmp(next_word, "-pw") == 0) { manager_needed = PANEDWINDOW; continue; } if (strcmp(next_word, "-rc") == 0) { manager_needed = ROWCOLUMN; continue; } } free(UserData); }
/* * Description: Processes a line in input file * Input: 1. Current transition data * 2. Output files */ void second_transition_process_line(transition_data* transition, compiler_output_files* output_files) { char* type = NULL; int index; /* Step 3 - Skips label if exists */ skip_label(transition->current_line_information); index = transition->current_line_information->current_index; /* Read line type */ type = get_next_word(transition); /* Step 4 - Handle line type */ if (type == NULL) { print_compiler_error("Invalid line", transition->current_line_information); transition->is_compiler_error = true; } /* Line is data initialization - Ignores it */ else if ((strcmp(type, DATA_OPERATION) == 0) || (strcmp(type, STRING_OPERATION) == 0)) { /* Ignore */ } /* Step 5 - Line is extern */ else if (strcmp(type, EXTERN_OPERATION) == 0) { create_extern_output_file_if_needed(output_files, transition->current_line_information->file_name); if (output_files->extern_file == NULL) { transition->is_runtimer_error = true; } } else if (strcmp(type, ENTRY_OPERATION) == 0) { /* Process entry */ second_transition_process_entry(transition, output_files); } else { /* Process operation */ transition->current_line_information->current_index = index; second_transition_process_operation(transition, output_files); } if (type != NULL) { free(type); } }
/* * Description: Process the extern definition in first transition. * Adds it into the symbol table * Input: 1. Current transition */ void first_transition_process_extern(transition_data* transition) { symbol_node_ptr p_symbol = NULL; char* extern_name = get_next_word(transition); /* If we have an extern*/ if (extern_name) { symbol_node_ptr p_searched_symbol = search_symbol(extern_name); /* Extern does not exists in symbol table */ if (!p_searched_symbol) { /* Create new symbol in table or die */ p_symbol = create_symbol(extern_name, NO_ADDRESS, true, true); if (p_symbol) { add_symbol_to_list(p_symbol); /* Make sure that the line does not contain left overs */ if (!is_end_of_data_in_line(transition->current_line_information)) { print_compiler_error("Invalid tokens after extern definition", transition->current_line_information); transition->is_compiler_error = true; return; } } /* Could not allocat memory, die*/ else { transition->is_runtimer_error = true; free(extern_name); return; } } /* Extern is already in table */ else { print_compiler_error("Each extern can be defined only once", transition->current_line_information); transition->is_compiler_error = true; return; } } }
int main(int argc, char **argv) { // Good to include a usage message if necessary. if (argc != 2) { std::cerr << "Usage: " << argv[0] << " filename\n"; return 1; } // Get the file names. std::string input_name = argv[1]; std::string output_name = input_name; output_name += ".q"; // Open the files. std::ifstream input_file(input_name.c_str()); if (!input_file) { std::cerr << "Can't open " << input_name << " for reading.\n"; return 1; } std::ofstream output_file(output_name.c_str()); if (!output_file) { std::cerr << "Can't open " << output_name << " for writing.\n"; return 1; } // Initialize. std::string input_line; std::string output_line = ">"; // Read the input file a line at a time. while (getline(input_file, input_line)) { // If this is a blank like handle it. This might involve outputing a partially filled // output line. // if (is_blank(input_line)) { if (output_line.length() > 2) { output_file << output_line << "\n"; output_line = ">"; } output_file << ">\n"; } // Otherwise the line is not blank and needs to be broken down by words. else { std::string word; while (get_next_word(input_line, word)) { if (output_line.length() + word.length() + 1 > 76) { output_file << output_line << "\n"; output_line = ">"; } output_line += ' '; output_line += word; } } } // If there is a partially filled line at the end, output that too. if (output_line.length() > 2) { output_file << output_line << "\n"; } return 0; }
// runs a script // returns -1 is esc was pressed, 0 o/w int run_script(char *script, DATAFILE *d) { char buf[512]; Ttoken *token; int i; // set datafile data = d; clear_keybuf(); // init sound memory for(i = 0; i < MAX_SCRIPT_SOUNDS; i ++) active_sounds[i] = -1; // create gfx buffers swap_buffer = create_bitmap(160, 120); buffer = create_bitmap(160, 120); script_done = FALSE; while(!script_done) { // get commands from script string script = get_line(buf, script); if (buf[0] != '#' && buf[0] != '\n' && buf[0] != '\r' && buf[0] != '-') { token = tokenize(buf); if (token != NULL) { if (!stricmp(token->word, "load_map")) cmd_loadmap(get_next_word(token)); else if (!stricmp(token->word, "draw_map")) cmd_drawmap(); else if (!stricmp(token->word, "set")) cmd_set((Ttoken *)token->next); else if (!stricmp(token->word, "move")) cmd_move((Ttoken *)token->next); else if (!stricmp(token->word, "delete")) cmd_del((Ttoken *)token->next); else if (!stricmp(token->word, "run")) cmd_run((Ttoken *)token->next); else if (!stricmp(token->word, "speak")) cmd_speak((Ttoken *)token->next, 1); else if (!stricmp(token->word, "text")) cmd_speak((Ttoken *)token->next, 0); else if (!stricmp(token->word, "save_buffer")) cmd_savebmp(); else if (!stricmp(token->word, "show_bmp")) cmd_showbmp(get_next_word(token)); else if (!stricmp(token->word, "blit")) cmd_blit(); else if (!stricmp(token->word, "fade_in")) cmd_fadein(); else if (!stricmp(token->word, "fade_out")) cmd_fadeout(); else if (!stricmp(token->word, "wait")) cmd_wait(atoi(get_next_word(token))); else if (!stricmp(token->word, "play_sample")) cmd_play_sample((Ttoken *)token->next); else if (!stricmp(token->word, "stop_sample")) cmd_stop_sample((Ttoken *)token->next); else if (!stricmp(token->word, "end_script")) cmd_end(); else { char msg[256]; sprintf(msg, "unknown: %s", token->word); msg_box(msg); } flush_tokens(token); } } } // destroy buffers delete_all_objects(); destroy_bitmap(buffer); destroy_bitmap(swap_buffer); // stop old sounds for(i = 0; i < MAX_SCRIPT_SOUNDS; i ++) { if (active_sounds[i] != -1) { stop_sound_id(active_sounds[i]); forget_sound(active_sounds[i]); } } return (script_done == -1 ? -1 : 0); }
/* parse_group_member() */ static link_t * parse_group_member(const char *gname, uint32_t num, struct group_acct *grps) { link_t *l; linkiter_t iter; struct group_acct *g; int cc; char *w; char *p; uint32_t sum; struct share_acct *sacct; l = make_link(); g = NULL; for (cc = 0; cc < num; cc++) { /* Match the group name and the group * must have shares. */ if (strcmp(gname, grps[cc].group) == 0 && grps[cc].user_shares) { g = calloc(1, sizeof(struct group_acct)); assert(g); g->group = strdup(grps[cc].group); g->memberList = strdup(grps[cc].memberList); g->user_shares = strdup(grps[cc].user_shares); tokenize(g->user_shares); break; } } /* gudness leaf member * caller will free the link */ if (g == NULL) return l; p = g->memberList; sum = 0; while ((w = get_next_word(&p))) { sacct = get_sacct(w, g->user_shares); if (sacct == NULL) { while ((sacct = pop_link(l))) free_sacct(sacct); fin_link(l); return NULL; } sum = sum + sacct->shares; enqueue_link(l, sacct); } traverse_init(l, &iter); while ((sacct = traverse_link(&iter))) { sacct->dshares = (double)sacct->shares/(double)sum; } _free_(g->group); _free_(g->memberList); _free_(g->user_shares); _free_(g); return l; }
int main( int argc, char **argv ) { // Check Command Args if ( argc < 2 ) { fprintf( stderr, "Error: Please pass input file\n" ); return 1; } // Initialise SDL if ( SDL_Init( SDL_INIT_VIDEO ) != 0 ) { fprintf( stderr, "Error: Unable to Initialise SDL:\n%s\n", SDL_GetError() ); return 1; } // Open Input File FILE *file = fopen( argv[1], "rb" ); if ( file == NULL ) { fprintf( stderr, "Error: Unable to open Input File:\n%s\n", argv[1] ); return 1; } // Read Input File to Memory fseek( file, 0, SEEK_END ); size_t file_size = ftell( file ); fseek( file, 0, SEEK_SET ); char *input = (char *)malloc( file_size+1 ); if ( input == NULL ) { fprintf( stderr, "Error: Unable to allocate memory for input: %u bytes\n", file_size+1 ); return 1; } fread( input, file_size, 1, file ); input[file_size] = '\0'; fclose( file ); // Declare Iterator and Value for File Data char *at = input; char val_string[16]; bool read_result; // Check For ASCII Format read_result = get_next_word( &at, val_string, 15 ); if ( !read_result || ( strcmp(val_string, "P3") != 0 && strcmp(val_string, "p3") != 0 ) ) { fprintf( stderr, "Error: Format is not in ascii - cannot read!\n" ); return 1; } // Create Window long s_width = 0; long s_height = 0; get_next_value( &at, &s_width ); read_result = get_next_value( &at, &s_height ); if ( !read_result ) { fprintf( stderr, "Error: Invalid Input; Could not Read Size\n:" ); return 1; } if ( s_width < DISPLAY_MIN_WIDTH || s_height < DISPLAY_MIN_HEIGHT ) { fprintf( stderr, "Error: Image too small - min width/height = (%d,%d)\n", DISPLAY_MIN_WIDTH, DISPLAY_MIN_HEIGHT ); return 1; } if ( s_width > DISPLAY_MAX_WIDTH || s_height > DISPLAY_MAX_HEIGHT ) { fprintf( stderr, "Error: Image too large - man width/height = (%d,%d)\n", DISPLAY_MAX_WIDTH, DISPLAY_MAX_HEIGHT ); return 1; } SDL_Window *window = SDL_CreateWindow( "PPM Viewer", 128, 128, s_width, s_height, SDL_WINDOW_OPENGL ); if ( window == NULL ) { fprintf( stderr, "Error: Could not create SDL Window!\n" ); return 1; } // Create OpenGL Context from SDL SDL_GLContext context = SDL_GL_CreateContext( window ); if ( context == 0 ) { fprintf( stderr, "Error: Unable to create GL context for SDL\n" ); return 1; } // Read Max Color Value long max_color = 255; read_result = get_next_value( &at, &max_color ); if ( !read_result ) { fprintf( stderr, "Error: Could not read max color value" ); return 1; } // Init OpenGL glClearColor( 1.0f, 1.0f, 1.0f, 0.0f ); glMatrixMode( GL_PROJECTION ); gluOrtho2D( 0.0, (GLdouble)s_width, (GLdouble)s_height, 0.0 ); // Clear Screen glClear( GL_COLOR_BUFFER_BIT ); // Remember this iterator location // We will need to reset to it, if we // need to redraw the image char *pixel_read_start = at; // Loop through each RGB triplet and put a // pixel on the screen long pixel_count = s_width * s_height; long pixel_read = 0; long r, g, b; while( pixel_read < pixel_count ) { if ( !get_next_value(&at, &r) ) break; if ( !get_next_value(&at, &g) ) break; if ( !get_next_value(&at, &b) ) break; r = clampl( r, 0, max_color ); g = clampl( g, 0, max_color ); b = clampl( b, 0, max_color ); glColor3f( (GLfloat)(r)/(GLfloat)(max_color), (GLfloat)(g)/(GLfloat)(max_color), (GLfloat)(b)/(GLfloat)(max_color) ); put_pixel( pixel_read % s_width, pixel_read / s_width ); ++pixel_read; } // Swap Bit Buffer SDL_GL_SwapWindow( window ); // Main Loop bool running = true; while( running ) { SDL_Event event; while ( SDL_PollEvent( &event ) ) { if ( event.type == SDL_WINDOWEVENT ) { switch( event.window.event ) { // Close Window case SDL_WINDOWEVENT_CLOSE: { running = false; } break; // Restored Focus case SDL_WINDOWEVENT_RESTORED: { // We need to Refresh the Screen // Clear the Screen glClear( GL_COLOR_BUFFER_BIT ); // Reset our string iterator to the start // of where our pixels are read at = pixel_read_start; // Loop through each RGB triplet and put a // pixel on the screen pixel_read = 0; while( pixel_read < pixel_count ) { if ( !get_next_value(&at, &r) ) break; if ( !get_next_value(&at, &g) ) break; if ( !get_next_value(&at, &b) ) break; r = clampl( r, 0, max_color ); g = clampl( g, 0, max_color ); b = clampl( b, 0, max_color ); glColor3f( (GLfloat)(r)/(GLfloat)(max_color), (GLfloat)(g)/(GLfloat)(max_color), (GLfloat)(b)/(GLfloat)(max_color) ); put_pixel( pixel_read % s_width, pixel_read / s_width ); ++pixel_read; } // Swap Bit Buffer SDL_GL_SwapWindow( window ); } break; } } } } // NOTE(troy): Our other resources have not been cleared, but for this small // program it should be okay. The OS will clean up the memory, and the // program will close faster because of it. // Free File Memory free( input ); // Return Success return 0; }
int get_command(char* buffer, int* it, int bufSize, command_t com, int* lineNum) { word next_word = get_next_word(buffer, it, bufSize, lineNum); com->status = -1; command_t newCom = checked_malloc(sizeof(struct command)); switch (next_word.type) { case IF: com->type = IF_COMMAND; // IF com->u.command[0] = newCom; if (get_command(buffer, it, bufSize, newCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type == NEWLINE) { next_word = get_next_word(buffer, it, bufSize, lineNum); } // THEN if (next_word.type == THEN) { command_t thenCom = checked_malloc(sizeof(struct command)); com->u.command[1] = thenCom; if (get_command(buffer, it, bufSize, thenCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type == NEWLINE) { next_word = get_next_word(buffer, it, bufSize, lineNum); } // ELSE if (next_word.type == ELSE) { command_t elseCom = checked_malloc(sizeof(struct command)); com->u.command[2] = elseCom; if (get_command(buffer, it, bufSize, elseCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type == NEWLINE) { next_word = get_next_word(buffer, it, bufSize, lineNum); } // FI AFTER ELSE if (next_word.type == FI) return 1; else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } // NO ELSE - STRAIGHT TO FI else if (next_word.type == FI) return 1; else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); case WHILE: ; int w = 1; com->type = WHILE_COMMAND; // WHILE command_t whileCom = checked_malloc(sizeof(struct command)); whileCom->type = SEQUENCE_COMMAND; command_t whileWhileCom = whileCom; whileCom->u.command[0] = newCom; while (get_command(buffer, it, bufSize, newCom, lineNum)) { int s = *it; int* whileIt = &(s); next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type != DO) { whileCom->type = SEQUENCE_COMMAND; whileCom->u.command[1] = checked_malloc(sizeof(struct command)); newCom = whileCom->u.command[1]; whileCom = whileCom->u.command[1]; *it = *whileIt; w++; } else { if (w == 1) com->u.command[0] = whileWhileCom->u.command[0]; else com->u.command[0] = whileWhileCom; command_t doCom = checked_malloc(sizeof(struct command)); com->u.command[1] = doCom; if (get_command(buffer, it, bufSize, doCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type == NEWLINE) { next_word = get_next_word(buffer, it, bufSize, lineNum); } // DONE if (next_word.type == DONE) { // can't have another command after done int v = *it; int* doneIt = &(v); next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == SEMICOLON || next_word.type == INPUT || next_word.type == OUTPUT || next_word.type == END) { *it = *doneIt; return generate_from_simple(com, 0, buffer, it, bufSize, com, lineNum); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } } bad_error(lineNum, __LINE__); return 0; case UNTIL: ; int u = 1; com->type = UNTIL_COMMAND; // UNTIL command_t untilCom = checked_malloc(sizeof(struct command)); untilCom->type = SEQUENCE_COMMAND; command_t untilUntilCom = untilCom; untilCom->u.command[0] = newCom; while (get_command(buffer, it, bufSize, newCom, lineNum)) { int t = *it; int* untilIt = &(t); next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type != DO) { untilCom->type = SEQUENCE_COMMAND; untilCom = untilCom->u.command[1]; untilCom = newCom; *it = *untilIt; u++; } else { if (u == 1) com->u.command[0] = untilUntilCom->u.command[0]; else com->u.command[0] = untilUntilCom; command_t udoCom = checked_malloc(sizeof(struct command)); com->u.command[1] = udoCom; if (get_command(buffer, it, bufSize, udoCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type == NEWLINE) { next_word = get_next_word(buffer, it, bufSize, lineNum); } // DONE if (next_word.type == DONE) { // can't have another command after done int z = *it; int* udoneIt = &(z); next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == SEMICOLON || next_word.type == INPUT || next_word.type == OUTPUT || next_word.type == END) { *it = *udoneIt; return generate_from_simple(com, 0, buffer, it, bufSize, com, lineNum); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); } } bad_error(lineNum, __LINE__); return 0; case LPARENS: com->type = SUBSHELL_COMMAND; if (get_command(buffer, it, bufSize, newCom, lineNum)) { next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == RPARENS) { com->u.command[0] = newCom; return 1; } else bad_error(lineNum, __LINE__); } else bad_error(lineNum, __LINE__); case COMMENT: case NEWLINE: return get_command(buffer, it, bufSize, com, lineNum); case SEMICOLON: bad_error(lineNum, __LINE__); return 0; case END: return 0; case SIMPLE: ; // make temporary command command_t tempCom = checked_malloc(sizeof(struct command)); tempCom->type = SIMPLE_COMMAND; //TODO FIX ALLOCATION MAYBE (number of words) tempCom->u.word = checked_malloc(sizeof(char*)*256); int word_len = strlen(next_word.string); tempCom->u.word[0] = checked_malloc(sizeof(char)*word_len); tempCom->u.word[0] = next_word.string; int word_count = 1; return generate_from_simple(tempCom, word_count, buffer, it, bufSize, com, lineNum); case THEN: case ELSE: case FI: case DO: case DONE: default: bad_error(lineNum, __LINE__); return 0; } }
int generate_from_simple(command_t tempCom, int word_count, char* buffer, int* it, int bufSize, command_t com, int* lineNum) { word next_word = get_next_word(buffer, it, bufSize, lineNum); //command_t newCom = checked_malloc(sizeof(struct command)); switch(next_word.type) { case PIPE: com->type = PIPE_COMMAND; com->u.command[0] = checked_malloc(sizeof(struct command)); *(com->u.command[0]) = *tempCom; command_t secondCom = checked_malloc(sizeof(struct command)); if (get_command(buffer, it, bufSize, secondCom, lineNum)) { com->u.command[1] = checked_malloc(sizeof(struct command)); *(com->u.command[1]) = *secondCom; return 1; } else bad_error(lineNum, __LINE__); case SEMICOLON: ; int s = *it; int* seqIt = &(s); next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == END || next_word.type == THEN || next_word.type == ELSE || next_word.type == DO || next_word.type == DONE || next_word.type == FI) { *it = *seqIt; *com = *tempCom; return 1; } else { com->type = SEQUENCE_COMMAND; com->u.command[0] = tempCom; command_t secondCom = checked_malloc(sizeof(struct command)); get_command(buffer, seqIt, bufSize, secondCom, lineNum); com->u.command[1] = secondCom; *it = *seqIt; return 1; } case LPARENS: // NOTE: subshells cannot be a part of simple commands in bash so disregard // tempCom->u.word[1] = checked_malloc(sizeof(char)*128); // next_word = get_next_word(buffer, it, bufSize, lineNum); // char* tempString = checked_malloc(sizeof(char)*128); // tempString = next_word.string; // while (next_word.type != RPARENS) { // next_word = get_next_word(buffer, it, bufSize, lineNum); // strcat(tempString, next_word.string); // if (next_word.type == NEWLINE || next_word.type == END || // next_word.type == PIPE || next_word.type == LPARENS) // { // bad_error(lineNum, __LINE__); // return 0; // } // } // strcat(tempString, next_word.string); // *(tempCom->u.word[1]) = *tempString; // *com = *tempCom; // return 1; bad_error(lineNum, __LINE__); return 0; case RPARENS: *it = (*it)-1; *com = *tempCom; return 1; case INPUT: next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == SEMICOLON || next_word.type == END || next_word.type == PIPE || next_word.type == LPARENS || next_word.type == RPARENS || next_word.type == INPUT || next_word.type == OUTPUT) { bad_error(lineNum, __LINE__); return 0; } tempCom->input = next_word.string; int t = *it; int* inIt = &(t); next_word = get_next_word(buffer, inIt, bufSize, lineNum); if (next_word.type == OUTPUT) { next_word = get_next_word(buffer, inIt, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == SEMICOLON || next_word.type == END || next_word.type == PIPE || next_word.type == LPARENS || next_word.type == RPARENS || next_word.type == INPUT || next_word.type == OUTPUT) { bad_error(lineNum, __LINE__); return 0; } tempCom->output = next_word.string; *it = *inIt; } return generate_from_simple(tempCom, word_count, buffer, it, bufSize, com, lineNum); case OUTPUT: next_word = get_next_word(buffer, it, bufSize, lineNum); if (next_word.type == NEWLINE || next_word.type == SEMICOLON || next_word.type == END || next_word.type == PIPE || next_word.type == LPARENS || next_word.type == RPARENS || next_word.type == INPUT || next_word.type == OUTPUT) { bad_error(lineNum, __LINE__); return 0; } tempCom->output = next_word.string; return generate_from_simple(tempCom, word_count, buffer, it, bufSize, com, lineNum); case COMMENT: next_word = get_next_word(buffer, it, bufSize, lineNum); while (next_word.type != NEWLINE && next_word.type != END) { next_word = get_next_word(buffer, it, bufSize, lineNum); } return 1; case NEWLINE: case END: *com = *tempCom; return 1; case SIMPLE: ; int word_len = strlen(next_word.string); tempCom->u.word[word_count] = checked_malloc(sizeof(char)*word_len); strcpy(tempCom->u.word[word_count], next_word.string); *com = *tempCom; word_count++; return generate_from_simple(tempCom, word_count, buffer, it, bufSize, com, lineNum); case IF: case THEN: case ELSE: case FI: case WHILE: case DO: case DONE: case UNTIL: default: bad_error(lineNum, __LINE__); return 0; } }
void CompilationCppParser::parse_src_file_rec( CompilationEnvironment &ce, const String &filename ) { if ( is_a_directory( filename ) ) return; String current_dir = directory_of( filename ) + "/"; File file( filename, "r" ); for ( const char *c = file.c_str(); *c; ) { // string ? if ( c[ 0 ] == '"' ) { go_after_next_double_quote( ++c ); continue; } // comment ? if ( c[ 0 ] == '/' ) { if ( c[ 1 ] == '/' ) { go_to_next_line( c += 2 ); continue; } if ( c[ 1 ] == '*' ) { go_to_next_mul_slash( c += 2 ); continue; } } // QOBJECT ? if ( c[ 0 ] == 'Q' ) { if ( strncmp( c + 1, "_OBJECT", 7 ) == 0 ) { c += 8; if ( not filename.begins_by( "/usr/" ) ) // hum !! moc_files.push_back_unique( filename ); continue; } } // if ( c[ 0 ] == '#' ) { if ( c[ 1 ] == 'd' and c[ 2 ] == 'e' and c[ 3 ] == 'f' and c[ 4 ] == 'i' and c[ 5 ] == 'n' and c[ 6 ] == 'e' and c[ 7 ] == ' ' ) { defines[ get_next_word( c += 8 ) ]; continue; } if ( c[ 1 ] == 'i' ) { if ( c[ 2 ] == 'f' ) { if ( c[ 3 ] == 'd' and c[ 4 ] == 'e' and c[ 5 ] == 'f' and c[ 6 ] == ' ' ) { if ( not defines.count( get_next_word( c += 7 ) ) ) skip_lines_until_endif_or_else( c ); continue; } if ( c[ 3 ] == 'n' and c[ 4 ] == 'd' and c[ 5 ] == 'e' and c[ 6 ] == 'f' and c[ 7 ] == ' ' ) { if ( defines.count( get_next_word( c += 8 ) ) ) skip_lines_until_endif_or_else( c ); continue; } } if ( c[ 2 ] == 'n' and c[ 3 ] == 'c' and c[ 4 ] == 'l' and c[ 5 ] == 'u' and c[ 6 ] == 'd' and c[ 7 ] == 'e' and c[ 8 ] == ' ' ) { bool syst; String bas_name = get_include_filename( c += 9, syst ); String inc_file = ce.find_src( bas_name, current_dir, inc_paths, not syst ); if ( not inc_file ) inc_file = ce.find_src( bas_name, current_dir, ce_inc_paths, not syst ); // formulation. if ( not inc_file ) { int form_ind = bas_name.find( "formulation." ); if ( form_ind >= 0 and bas_name != "formulation.h" ) { String h_py = bas_name + ".py"; if ( not file_exists( h_py ) ) { int beg_form = form_ind + 12; int end_form = bas_name.find( ".", beg_form ); String form = bas_name.beg_upto( end_form ).end_from( beg_form ); int beg_elem = end_form + 1; int end_elem = bas_name.find( ".", beg_elem ); String elem = bas_name.beg_upto( end_elem ).end_from( beg_elem ); BasicVec<String> elem_list = tokenize( elem, ',' ); int beg_parm = end_elem + 1; int end_parm = bas_name.find( ".", beg_parm ); String parm = bas_name.beg_upto( end_parm ).end_from( beg_parm ); BasicVec<String> parm_list = tokenize( parm, ',' ); BasicVec<String> der_vars; BasicVec<String> dim; for( int np = 0; np < parm_list.size(); ++np ) { if ( parm_list[ np ].begins_by( "name_der_vars=" ) ) der_vars << parm_list[ np ].end_from( 14 ); else if ( parm_list[ np ].begins_by( "dim=" ) ) dim << parm_list[ np ].end_from( 4 ); } File f( h_py, "w" ); // f << "import sys, os\n"; // f << "sys.path.append( os.getcwd() + '/LMT' )\n"; // f << "sys.path.append( os.getcwd() + '/LMTpp' )\n"; f << "import formal_lf\n"; f << "formal_lf.write_pb(\n"; f << " name = '" << form << "',\n"; f << " formulations = ['" << form << "'],\n"; f << " elements = ["; for( int i = 0; i < elem_list.size(); ++i ) f << ( i ? ",'" : "'" ) << elem_list[ i ] << "'"; f << "],\n"; f << " incpaths = ['.'"; for( int i = 0; i < ce_inc_paths.size(); ++i ) if ( ce_inc_paths[ i ].find( "LMT" ) >= 0 ) f << ",'" << ce_inc_paths[ i ].beg_upto( ce_inc_paths[ i ].find( "/include" ) ) << "/formulations'"; f << "],\n"; f << " name_der_vars=["; for( int nd = 0; nd < der_vars.size(); ++nd ) f << ( nd ? "," : "" ) << '"' << der_vars[ nd ] << '"'; f << "],\n"; if ( dim.size() ) f << " dim='" << dim[ 0 ] << "',\n"; f << ")\n"; // f << " options = { 'behavior_simplification' : 'plane stress', 'behavior_law' : s }, // f << " # name_der_vars = [ "frac_E2", "frac_G12", "nu12" ] # } } } // .h.py ? if ( bas_name.ends_with( ".h" ) ) { if ( String h_py = ce.find_src( bas_name + ".py", current_dir ) ) { inc_file = h_py.beg_upto( h_py.size() - 3 ); if ( last_modification_time_or_zero_of_file_named( h_py ) > last_modification_time_or_zero_of_file_named( inc_file ) ) make_h_py( h_py, inc_file ); } } // parse rec if ( inc_file ) { inc_file = canonicalize_filename( inc_file ); inc_files << inc_file; // if ( not already_parsed.contains( inc_file ) ) { already_parsed << inc_file; parse_src_file_rec( ce, inc_file ); } } continue; } } if ( c[ 1 ] == 'p' and c[ 2 ] == 'r' and c[ 3 ] == 'a' and c[ 4 ] == 'g' and c[ 5 ] == 'm' and c[ 6 ] == 'a' and c[ 7 ] == ' ' ) { c += 8; if ( strncmp( c, "src_file ", 9 ) == 0 ) { String o = get_pragma_arg( c += 9 ); String s = ce.find_src( o, current_dir ); if ( s.size() ) src_files << s; else cerrn << "Impossible to find src '" << o << "'"; continue; } if ( strncmp( c, "lib_path ", 9 ) == 0 ) { lib_paths << ce.find_src( get_pragma_arg( c += 9 ), current_dir ); continue; } if ( strncmp( c, "lib_name ", 9 ) == 0 ) { lib_names << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "fra_name ", 9 ) == 0 ) { fra_names << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "inc_path ", 9 ) == 0 ) { String path = ce.find_src( get_pragma_arg( c += 9 ), current_dir ); if ( not ( path.ends_with( '/' ) or path.ends_with( '\\' ) ) ) path += '/'; inc_paths << path; continue; } if ( strncmp( c, "cpp_flag ", 9 ) == 0 ) { cpp_flags << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "lnk_flag ", 9 ) == 0 ) { lnk_flags << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "gpu_flag ", 9 ) == 0 ) { gpu_flags << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "template ", 9 ) == 0 ) { var_templ << get_pragma_arg( c += 9 ); continue; } if ( strncmp( c, "cxx_name ", 9 ) == 0 ) { cxx_name << get_pragma_arg( c += 9 ); continue; } } } // ++c; } }
/******************************************************************************** * Deterministic finit automata. A word is read from the input file. The automata * is then fed with the word. ********************************************************************************/ void start_automata(config_t *conf, FILE *fp) { struct s_node *node = NULL; int next_state = 0; int last_state = 0; char word[MAXWORDSIZE], val[MAXWORDSIZE]; automata_t automata; automata.state = BEGIN; reset_automata(&automata); init_automata(); conf->traffic.pattern = NULL; while(get_next_word(word, fp) != EOF) { /* remember line of word */ word_line = fp_line; /* the closing bracket indicates that the current state ist finished with its work. The automata * should return to the the state that calls the current state */ if(strcmp(word, "}") == 0) { node = get_node(automata.state); if(node->check != NULL) { node->check(conf); //fprintf(stderr, "leaving state %i, calling check...\n", automata.state); } automata.state = node->return_state; continue; } next_state = get_next_state(automata.state, word); /* change state */ if(next_state != -1) { /* before we change the state: call the check function of the last state */ // fprintf(stderr, "changing %i ==> %i \n", automata.state, next_state); node = get_node(automata.state); if(node != NULL) { if(node->check != NULL) { node->check(conf); // fprintf(stderr, "check function called \n"); } } last_state = automata.state; automata.state = next_state; node = get_node(automata.state); node->return_state = last_state; // fprintf(stderr, "%i ==> %i\n", automata.state, last_state); if(node->init != NULL) { node->init(conf); /* a new conf structure could have been malloced ( node->init(conf) could be the * pointer to the function init_stream in states.c, which mallocs a new conf structure. * conf sould always point to the last element in the linked list ) */ while(conf->next != NULL) conf = conf->next; } } /* remain in current state */ else { struct s_node *node; node = get_node(automata.state); if(node != NULL) { get_pair_value(val, fp); node->read(word, val, conf, &automata); // fprintf(stderr, "in state...automata.state = %i\n", automata.state); } else { fprintf(stderr, "INTERNAL ERROR: automata in unknown state (%i)\n", automata.state); exit(0); } } } /* automata should end up in state BEGIN!! */ if(automata.state != BEGIN) { fprintf(stderr, "ERROR: unexpected end of file, %i\n", automata.state); exit(0); } //conf->next = NULL; }
void cmd_parser(unsigned long data) { char *curr_pos = &user_string[0]; int ret; while(1) { while(cmd_in_progress) os_TaskDelay(10); DBG_P(( DBG_L0 "\r\n> ")); cmd_parser_read_line(); if(strlen(user_string) == 0) { /* Do Nothing. */ } else if(!memcmp(user_string,"iwlist",6)){ userif_prepare_scan_cmd(0); } else if(!memcmp(user_string,"iwconf",6)){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos,"essid",5)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; specificSSID.Len = strlen(curr_pos); memcpy((void *)specificSSID.SsId, curr_pos, strlen(curr_pos)); if(link_present) { if(currbss_type != BSS_INDEPENDENT) userif_prepare_deauth_cmd(); else userif_prepare_adhoc_stop_cmd(); } else { userif_prepare_scan_cmd(1); } link_present = 0; } else if (!memcmp(curr_pos,"mode",4)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; if(!memcmp(curr_pos,"ad-hoc",6)) { bss_type = BSS_INDEPENDENT; } else if(!memcmp(curr_pos,"manage",6)) { bss_type = BSS_INFRASTRUCTURE; } else { bss_type = BSS_ANY; } } else if (!memcmp(curr_pos,"ap",2)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; get_macaddr(curr_pos,(char *)specificBSSID); if (FindBSSIDinList()) { userif_prepare_auth_cmd(); } else { if(link_present) { link_present = 0; userif_prepare_deauth_cmd(); } else { userif_prepare_scan_cmd(2); } } } } else if(!memcmp(user_string,"econfi",6)){ unsigned int ip; unsigned int nm; unsigned int gw; curr_pos = &user_string[get_next_word(user_string)]; get_ipaddr(curr_pos, (char *)&ip); curr_pos = &curr_pos[get_next_word(curr_pos)]; get_ipaddr(curr_pos, (char *)&nm); curr_pos = &curr_pos[get_next_word(curr_pos)]; get_ipaddr(curr_pos, (char *)&gw); sys_tcpip_init(ip, nm); } else if(!memcmp(user_string, "printip", 7)){ DBG_P(( DBG_L0 "%d.%d.%d.%d,", ip_addr[0], ip_addr[1], ip_addr[2], ip_addr[3] )); DBG_P(( DBG_L0 "%d.%d.%d.%d,", net_mask[0], net_mask[1], net_mask[2], net_mask[3] )); DBG_P(( DBG_L0 "%d.%d.%d.%d\r\n", def_gtwy[0], def_gtwy[1], def_gtwy[2], def_gtwy[3] )); } else if(!memcmp(user_string, "printmac", 8)){ char mac[6]; GetMACAddr(NULL, mac); DBG_P(( DBG_L0 "%02x:%02x:%02x:%02x:%02x:%02x\r\n", \ mac[0], mac[1], mac[2], mac[3], mac[4], mac[5])); } else if(!memcmp(user_string,"ping",4)){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos,"stop",4)) { send_ping = 0; } else { get_ipaddr(curr_pos, ping_ipaddr); #ifdef EMBEDDED_TCPIP userif_prepare_open_raw_socket(); #else userif_prepare_macaddr_get_cmd(); #endif send_ping = 1; } } /* Link-local address manager. */ else if(!memcmp(user_string, "linklocal", 9)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { /* Launch the link local manager */ ret = ll_init(); if(ret) DBG_P(( DBG_L0 "Error launching link local: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "stop", 4)) { /* Kill the link local manager. */ ret = ll_shutdown(); if(ret) DBG_P(( DBG_L0 "Error killing link local: %d.\r\n", ret)); } } /* mDNS responder */ else if(!memcmp(user_string, "mdns", 4)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { /* launch the mDNS responder */ ret = mdns_responder_init(); if(ret) DBG_P(( DBG_L0 "Error launching mDNS responder: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "stop", 4)) { /* stop the mDNS responder */ ret = mdns_responder_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping mDNS responder: %d.\r\n", ret)); } } else if(!memcmp(user_string, "mcast", 5 )){ curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "get", 3 )) { userif_prepare_mcast_cmd(); } else if(!memcmp(curr_pos, "set", 3 )) { userif_prepare_mcast_add_cmd(); } } /* httpd interface */ else if(!memcmp(user_string, "httpd", 5)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "start", 5)) { ret = httpd_init(); if(ret) DBG_P(( DBG_L0 "Error launching httpd: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "stop", 4)) { ret = httpd_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping httpd: %d.\r\n", ret)); } else { DBG_P(( DBG_L0 "No such httpd command: %s.\r\n", curr_pos)); } } /* log interface */ else if(!memcmp(user_string, "log", 3)) { curr_pos = &user_string[get_next_word(user_string)]; if(!memcmp(curr_pos, "init", 5)) { ret = log_init(); if(ret) DBG_P(( DBG_L0 "Error launching logger: %d.\r\n",ret)); } else if(!memcmp(curr_pos, "shutdown", 8)) { ret = log_shutdown(); if(ret) DBG_P(( DBG_L0 "Error stopping logger: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "dump", 4)) { ret = log_dump(); if(ret) DBG_P(( DBG_L0 "Error dumping log: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "purge", 5)) { ret = log_purge(); if(ret) DBG_P(( DBG_L0 "Error purging log: %d.\r\n", ret)); } else if(!memcmp(curr_pos, "write", 5)) { curr_pos = &curr_pos[get_next_word(curr_pos)]; ret = log(curr_pos); if(ret) DBG_P(( DBG_L0 "Failed to write log: %d.\r\n", ret)); } else { DBG_P(( DBG_L0 "No such log command: %s.\r\n", curr_pos)); } } else if(!memcmp(user_string, "help", 4)) { print_usage(); } else { #ifdef UART_DRV DBG_P(( DBG_L0 "Unknown command.\r\n")); #endif } } }
int get_token() { int c, len, i, max_word_len, state, token, flag, pause_line, pause_block; state = START; pause_line = 0; pause_block = 0; while( state != OVER ) { if( state == PAUSE ) { if( pause_line ) { pause_line = 0; pos = input_len; state = START; } if( pause_block ) { while( (c = get_next_word(&pos)) != FINISH ) { if( c == '*' && input_line[pos] == '/' ) { input_line[pos++]; break; } } if( c == FINISH ) return FINISH; pause_block = 0; state = START; } } c = get_next_word(&pos); if( c == FINISH ) return FINISH; if( c == ' ' || c == '\t' || c == '\n' ) continue; if( isalpha( c ) || c == '_' ) { i = 0; max_word_len = 0; do { save_word[i++] = c; c = input_line[pos++]; max_word_len++; }while( isdigit( c ) || c == '_' || isalpha( c ) ); pos--; if( max_word_len >= 20 ) { fprintf( stderr, "word's max len is 20"); // do a error deal function exit( 0 ); } save_word[i] = '\0'; token = TOK_ID; state = OVER; } else if( isdigit( c ) ) { i = 0; if( flag == '-' ) save_word[i++] = '-'; while( isdigit( c ) ) { save_word[i++] = c; c = input_line[pos++]; } pos--; save_word[i] = '\0'; save_num = atoi( save_word ); token = TOK_NUM; state = OVER; flag = 0; } else { state = OVER; switch( c ) { case '+': token = TOK_PLUS; break; case '-': token = TOK_MINUS; break; case '*': token = TOK_MUL; break; case '/': if( input_line[pos] == '/' ) { input_line[pos++]; state = PAUSE; pause_line = 1; } else if( input_line[pos] == '*' ) { input_line[pos++]; state = PAUSE; pause_block = 1; } else { token = TOK_DIV; } break; case '=': if( input_line[pos] == '=' ) { input_line[pos++]; token = TOK_EQ; } else token = TOK_ASSIGN; break; case '>': if( input_line[pos] == '=' ) { input_line[pos++]; token = TOK_GE; } else token = TOK_GT; break; case '<': if( input_line[pos] == '=' ) { input_line[pos++]; token = TOK_LE; } else token = TOK_LT; break; case '!': if( input_line[pos] == '=' ) { input_line[pos++]; token = TOK_NE; } else token = TOK_NOT; break; case '&': if( input_line[pos] == '&' ) { input_line[pos++]; token = TOK_ANDAND; } else token = TOK_AND; break; case '|': if( input_line[pos] == '|' ) { input_line[pos++]; token = TOK_OROR; } else token = TOK_OR; break; case ',': token = TOK_COMMA; break; case ':'://no gonna use 'condition ? true : false' token = TOK_COLON; break; case ';': token = TOK_SEMI; break; case 39: // ' token = TOK_SQUTOA; break; case 34: // " token = TOK_DQUTOA; break; case '(': token = TOK_LPAREN; break; case ')': token = TOK_RPAREN; break; case '[': token = TOK_LSQUARE; break; case ']': token = TOK_RSQUARE; break; case '{': token = TOK_LBRACKET; break; case '}': token = TOK_RBRACKET; break; } } } if( token == TOK_ID ) { int t; if( (t = is_resever_word(save_word)) != -1 ) token = t; } return token; }