// Remove all the stop words not preceded by the inclusion operator void remove_stop_words(syntree **free_text_syntree) { ASSERT(free_text_syntree); ASSERT(*free_text_syntree); // If a stop word is alone, return an error. // Otherwise if a word is alone, return. if (((word_syntree *)(*free_text_syntree))->type == FT_WORD_TYPE) { if (is_stop_word(get_chars(((word_syntree *)(*free_text_syntree))->word))) { logwrite("Note: The following word is very common and was not included in your search: %s.", get_chars (((word_syntree *)(*free_text_syntree))->word)); ft_word_delete((word_syntree *)(*free_text_syntree)); *free_text_syntree = NULL; return; } else { return; } } // If a phrase is alone, return. if (((word_syntree *)(*free_text_syntree))->type == FT_PHRASE_TYPE) { return; } remove_stop_words_helper(free_text_syntree); }
/* get the char offset corresponding to a given byte offset, taking the charset into account */ int eb_get_char_offset(EditBuffer *b, int offset) { int pos; Page *p, *p_end; /* if no decoding function in charset, it means it is 8 bit only */ if (b->charset_state.decode_func == NULL) { pos = offset; if (pos > b->total_size) pos = b->total_size; } else { p = b->page_table; p_end = p + b->nb_pages; pos = 0; for (;;) { if (p >= p_end) goto the_end; if (offset < p->size) break; if (!(p->flags & PG_VALID_CHAR)) { p->nb_chars = get_chars(p->data, p->size, b->charset); p->flags |= PG_VALID_CHAR; } pos += p->nb_chars; offset -= p->size; p++; } pos += get_chars(p->data, offset, b->charset); the_end: ; } return pos; }
// Creates a new RULE CONDITION GRAPH - it creates a RCG node for every tuple variable // Takes the tuple variable and the data source list along with the number of tuple variables // as the input RCG *RCG_new(int num_tuple_var, List *tuple_var_list, List *datasrc_list) { RCG *graph; int i; ListElement *cursor1, *cursor2; RCG_node *node; void *tuple_var_name, *datasrc_name; ASSERT(tuple_var_list); ASSERT(datasrc_list); graph = (RCG *)tman_malloc(sizeof(RCG)); memset(graph, '\0', sizeof(RCG)); graph->type = RCG_TYPE; graph->num_tuple_variables = num_tuple_var; graph->nodes = List_new(); graph->catch_all = List_new(); if (!num_tuple_var) { /* The RCG does not have any tuple variables. For ex. ** when 1 = 1. */ return graph; } /* create the individual nodes -- one for each tuple variable*/ node = RCG_node_new(); tuple_var_name = (char *)List_getFirst(tuple_var_list, &cursor1); datasrc_name = (char *)List_getFirst(datasrc_list, &cursor2); node->tuple_variable_name = get_chars(tuple_var_name); node->datasrc_name = get_chars(datasrc_name); /* Insert the node into the RCG */ List_insertElement(graph->nodes, node); for (i = 1; i < num_tuple_var; i++) { /* create the individual nodes */ node = RCG_node_new(); tuple_var_name = (char *)List_getNext(&cursor1); datasrc_name = (char *)List_getNext(&cursor2); node->tuple_variable_name = get_chars(tuple_var_name); node->datasrc_name = get_chars(datasrc_name); /* Insert the node into the RCG */ List_insertElement(graph->nodes, node); } return graph; }
// Recursively find the NOT nodes that are preceded or followed by an expression // and distruibute the NOT node across the expression. A few examples are given below. // Case 1 : -A AND (B OR C), after distribution would look like (-A AND B) OR (-A AND C) // Case 2 : A AND -B AND (C OR D) would look like A AND ((-B AND C) OR (-A AND D)) after distribution. void distribute_NOT_node_helper(syntree *subtree_to_be_expanded, syntree *NOT_subtree) { // We don't want to look at/below the WORD nodes, PHRASE nodes or NOT nodes. // Because the tree below them is inconsequential. if (subtree_to_be_expanded->left_tree && (subtree_to_be_expanded->left_tree->type != FT_WORD_TYPE && subtree_to_be_expanded->left_tree->type != FT_PHRASE_TYPE && subtree_to_be_expanded->left_tree->type != FT_NOT_TYPE)) { distribute_NOT_node_helper(subtree_to_be_expanded->left_tree, NOT_subtree); } if (subtree_to_be_expanded->right_tree && (subtree_to_be_expanded->right_tree->type != FT_WORD_TYPE && subtree_to_be_expanded->right_tree->type != FT_PHRASE_TYPE && subtree_to_be_expanded->right_tree->type != FT_NOT_TYPE)) { distribute_NOT_node_helper(subtree_to_be_expanded->right_tree, NOT_subtree); } // When distributing the NOT node, the following happens. // -A AND (B OR C) => (-A AND B) OR/AND (-A AND C). // This is done in two steps. First (-A AND B) and then (-A AND C) are formed. // Then they are ORed using the already present OR node (or) ANDed using the already present AND node // (based on whether the expression has OR or AND type). // The code below constructs the (-A AND B), (-A AND C) nodes. if (subtree_to_be_expanded->type == FT_AND_TYPE || subtree_to_be_expanded->type == FT_OR_TYPE) { if (subtree_to_be_expanded->left_tree->type == FT_WORD_TYPE || subtree_to_be_expanded->left_tree->type == FT_PHRASE_TYPE) { { syntree *new_NOT_node, *new_AND_node; new_NOT_node = ft_not_new((syntree *)ft_word_new(get_chars(((word_syntree *)NOT_subtree->left_tree)->word))); new_AND_node = ft_and_new(new_NOT_node, subtree_to_be_expanded->left_tree); subtree_to_be_expanded->left_tree = new_AND_node; } } if (subtree_to_be_expanded->right_tree->type == FT_WORD_TYPE || subtree_to_be_expanded->right_tree->type == FT_PHRASE_TYPE) { { syntree *new_NOT_node, *new_AND_node; new_NOT_node = ft_not_new((syntree *)ft_word_new(get_chars(((word_syntree *)NOT_subtree->left_tree)->word))); new_AND_node = ft_and_new(new_NOT_node, subtree_to_be_expanded->right_tree); subtree_to_be_expanded->right_tree = new_AND_node; } } } }
/* gives the byte offset of a given character, taking the charset into account */ int eb_goto_char(EditBuffer *b, int pos) { int offset; Page *p, *p_end; if (b->charset != &charset_utf8) { offset = pos; if (offset > b->total_size) offset = b->total_size; } else { offset = 0; p = b->page_table; p_end = b->page_table + b->nb_pages; while (p < p_end) { if (!(p->flags & PG_VALID_CHAR)) { p->flags |= PG_VALID_CHAR; p->nb_chars = get_chars(p->data, p->size, b->charset); } if (pos < p->nb_chars) { offset += goto_char(p->data, pos, b->charset); break; } else { pos -= p->nb_chars; offset += p->size; p++; } } } return offset; }
// Masks the constant value in the signature tree of a trigger node, if the trigger node was the first // node to be installed in a constant node under the index structure, and there is some constant value // that was not copied completely in the constant node; rather its reference was stored. static void save_const_node_info (syntree *signature_tree) { switch (signature_tree->type) { case FUNCTION_CALL_TYPE: // Currently it is being used only by free text predicate index. ASSERT (tm_stricmp(get_chars (signature_tree->left_tree->datum), "contains") == 0); // We set the free text predicate syntree in the signature tree to be NULL, so that it is not // deleted with the triggger's syntax tree. This free text predicate syntree is required by // the constant node in the index. signature_tree->right_tree->right_tree->left_tree = NULL; break; default: logwrite ("Internal Error: bad indexable predicate found."); TMAN_HALT; } }
static int search_word (ft_doc_index *doc_idx, word_syntree *word_node, ft_word_info *word_info) { TCHAR *word_str; ASSERT (word_node); word_str = get_chars (word_node->word); ASSERT (word_str); // The preceding '+' is not considered while searching. if (*word_str == _TEXT ('+')) { word_str++; } // If a word is found in the document, fill its selectivity information from the word's syntree node. if (search_doc_index (doc_idx, word_str, word_info) == TMAN_FOUND) { word_info->selectivity = word_node->selectivity; return TMAN_FOUND; } return TMAN_NOT_FOUND; }
int main(void) { // INITIALIZING struct queue cmd_queue; q_init (&cmd_queue); uint8_t *mem = calloc (__MEMORY_SIZE, sizeof(uint8_t)); struct queue *oplist = malloc (sizeof(struct queue) * __TABLE_SIZE); char *input = malloc (sizeof(char)*__INPUT_SIZE); char *cmd = malloc (sizeof(char)*__CMD_SIZE); if (mem == NULL || input == NULL || cmd == NULL || oplist == NULL) { puts("MEMORY INSUFFICIENT"); goto memory_clear; } // OPCODE READ int i; for (i=0; i<__TABLE_SIZE; ++i) q_init (&oplist[i]); // Open file for opcode reference FILE * fp = fopen(__OPCODE_FILENAME, "r"); if (fp == NULL) { printf("%s NOT FOUND\n", __OPCODE_FILENAME); goto memory_clear; } // Formatting string i = snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%hhx %%%ds %%s", __CMD_SIZE - 1); if (i < 0 || i > __CMD_FORMAT_SIZE) { puts("COMMAND SIZE IS TOO BIG"); goto memory_clear; } // opcode hash table generation while (fgets(input, __INPUT_SIZE, fp) != NULL) { uint8_t code; char form[__OPCODE_FORMAT_SIZE]; if (sscanf(input, (const char *) __CMD_FORMAT, &code, cmd, &form) != 3) { printf("%s IS BROKEN\n", __OPCODE_FILENAME); goto memory_clear; } // Saving opcode struct op_elem *oe = malloc(sizeof(struct op_elem)); if (oe == NULL) { puts("MEMORY INSUFFICIENT"); goto memory_clear; } oe->opcode = malloc(sizeof(char)*(strlen(cmd)+1)); if(oe->opcode == NULL) { puts("MEMORY INSUFFICIENT"); goto memory_clear; } strcpy(oe->opcode, cmd); strcpy(oe->format, form); oe->code = code; code = str_hash (cmd) % __TABLE_SIZE; q_insert (&oplist[code], &(oe->elem)); } // COMMAND PROCESSING while (true) { struct q_elem *qe; uint8_t value; uint32_t start, end; DIR *dirp = NULL; struct dirent *dir = NULL; char check[2]; bool is_valid_cmd = false; printf("%s", __SHELL_FORM); if (!get_chars(input, __INPUT_SIZE)) goto memory_clear; // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds", __CMD_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd)!=1) cmd[0] = '\0'; // Switching with commands switch(get_cmd_index(cmd)) { case CMD_HELP: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } puts(__HELP_FORM); is_valid_cmd = true; break; case CMD_DIR: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // open directory and read through all elem. i = 1; dirp = opendir("."); dir = readdir(dirp); for(; dir!=NULL; dir = readdir(dirp)) { struct stat st; if(stat((const char*) dir->d_name, &st)!=0) { puts("FILE NOT FOUND"); goto memory_clear; } // FIX: ignore . and .. if(_SAME_STR(dir->d_name, ".") || _SAME_STR(dir->d_name, "..")) continue; printf("%20s", dir->d_name); if(S_ISDIR(st.st_mode)) // is Directory? putchar('/'); else if( (st.st_mode & S_IXUSR) // is exe? || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH) ) putchar('*'); putchar('\t'); // print newline after 3 elements if((i++)%3==0) putchar('\n'); } if((i-1)%3!=0) putchar('\n'); is_valid_cmd = true; break; case CMD_QUIT: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } is_valid_cmd = true; goto memory_clear; case CMD_HISTORY: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } qe = q_begin (&cmd_queue); i = 1; // print every formatted history for (; qe!=q_end(&cmd_queue); qe=q_next(qe)) printf("%-4d %s\n", i++, q_entry(qe, struct cmd_elem, elem)->cmd); printf("%-4d %s\n", i, input); is_valid_cmd = true; break; case CMD_DUMP: switch(sscanf(input, "%s %x , %x", cmd, &start, &end)) { case 1: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } start = get_location (0, false); end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 2: if(sscanf(input, "%*s %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (start >= __MEMORY_SIZE) { puts("OUT OF MEMORY BOUNDS."); break; } end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (!(start<=end && end<__MEMORY_SIZE)) { puts("OUT OF MEMORY BOUNDS."); break; } hexdump (mem, start, end); // if end is too large, point to end and go 0 if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_EDIT: switch(sscanf(input, "%s %x , %hhx", cmd, &start, &value)) { case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, start, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_FILL: switch(sscanf(input, "%s %x , %x , %hhx", cmd, &start, &end, &value)) { case 4: if(sscanf(input, "%*s %*x , %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, end, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_RESET: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // equivalent to fill 0, __MEMORY_SIZE-1 hexfill (mem, __MEMORY_SIZE, 0, __MEMORY_SIZE - 1, 0); is_valid_cmd = true; break; case CMD_OPCODE: switch(sscanf(input, "%*s %s", cmd)) { case 1: if(sscanf(input, "%*s %*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // look for opcode in hash table i = str_hash(cmd) % __TABLE_SIZE; if (!q_empty(&oplist[i])) { bool found = false; qe = q_begin (&oplist[i]); for(; qe != q_end(&oplist[i]); qe = q_next(qe)) { struct op_elem *oe = q_entry (qe, struct op_elem, elem); if (_SAME_STR(cmd, oe->opcode)) { printf("opcode is %2X\n", oe->code); found = true; break; } } if (found) { is_valid_cmd = true; break; } } printf("%s: NO SUCH OPCODE\n", cmd); break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_OPCODELIST: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // traverse through every table for(i=0; i<__TABLE_SIZE; ++i) { printf("%d : ", i); if (!q_empty(&oplist[i])) { qe = q_begin (&oplist[i]); struct op_elem *oe = q_entry (qe, struct op_elem, elem); printf ("[%s:%02X] ", oe->opcode, oe->code); for(qe = q_next(qe); qe != q_end(&oplist[i]); qe = q_next(qe)) { oe = q_entry (qe, struct op_elem, elem); printf ("-> [%s:%02X] ", oe->opcode, oe->code); } } puts(""); } is_valid_cmd = true; break; default: if(sscanf(input, "%1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } }
int main(void) { // INITIALIZING struct queue cmd_queue; q_init (&cmd_queue); uint8_t *mem = calloc (__MEMORY_SIZE, sizeof(uint8_t)); char *input = malloc (sizeof(char)*__INPUT_SIZE); char *cmd = malloc (sizeof(char)*__CMD_SIZE); char *filename = malloc (sizeof(char)*__FILENAME_SIZE); if (mem == NULL || input == NULL || filename == NULL || cmd == NULL) { puts("MEMORY INSUFFICIENT"); goto memory_clear; } if (!init_oplist (__OPCODE_FILENAME)) { puts("OPCODE LIST INITIALIZATION FAILED."); goto memory_clear; } // COMMAND PROCESSING while (true) { int i; struct q_elem *qe; uint8_t value; uint32_t start, end; DIR *dirp = NULL; struct dirent *dir = NULL; char check[2]; bool is_valid_cmd = false; char *tok = NULL; printf("%s", __SHELL_FORM); if (!get_chars(input, __INPUT_SIZE)) goto memory_clear; // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds", __CMD_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd)!=1) cmd[0] = '\0'; // Switching with commands switch(get_cmd_index(cmd)) { case CMD_HELP: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } puts(__HELP_FORM); is_valid_cmd = true; break; case CMD_DIR: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // open directory and read through all elem. i = 1; dirp = opendir("."); dir = readdir(dirp); for(; dir!=NULL; dir = readdir(dirp)) { struct stat st; if(stat((const char*) dir->d_name, &st)!=0) { puts("FILE NOT FOUND"); goto memory_clear; } // FIX: ignore . and .. if(_SAME_STR(dir->d_name, ".") || _SAME_STR(dir->d_name, "..")) continue; printf("%20s", dir->d_name); if(S_ISDIR(st.st_mode)) // is Directory? putchar('/'); else if( (st.st_mode & S_IXUSR) // is exe? || (st.st_mode & S_IXGRP) || (st.st_mode & S_IXOTH) ) putchar('*'); putchar('\t'); // print newline after 3 elements if((i++)%3==0) putchar('\n'); } if((i-1)%3!=0) putchar('\n'); is_valid_cmd = true; break; case CMD_QUIT: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } is_valid_cmd = true; goto memory_clear; case CMD_HISTORY: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } qe = q_begin (&cmd_queue); i = 1; // print every formatted history for (; qe!=q_end(&cmd_queue); qe=q_next(qe)) printf("%-4d %s\n", i++, q_entry(qe, struct cmd_elem, elem)->cmd); printf("%-4d %s\n", i, input); is_valid_cmd = true; break; case CMD_DUMP: switch(sscanf(input, "%s %x , %x", cmd, &start, &end)) { case 1: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } start = get_location (0, false); end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 2: if(sscanf(input, "%*s %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (start >= __MEMORY_SIZE) { puts("OUT OF MEMORY BOUNDS."); break; } end = start + 0x10 * 10 - 1; // if end is too large, point to end and go 0 if ( end >= __MEMORY_SIZE ) end = __MEMORY_SIZE - 1; hexdump (mem, start, end); if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (!(start<=end && end<__MEMORY_SIZE)) { puts("OUT OF MEMORY BOUNDS."); break; } hexdump (mem, start, end); // if end is too large, point to end and go 0 if ( end == __MEMORY_SIZE - 1) get_location (0, true); else get_location (end + 1, true); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_EDIT: switch(sscanf(input, "%s %x , %hhx", cmd, &start, &value)) { case 3: if(sscanf(input, "%*s %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, start, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_FILL: switch(sscanf(input, "%s %x , %x , %hhx", cmd, &start, &end, &value)) { case 4: if(sscanf(input, "%*s %*x , %*x , %*x %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } hexfill (mem, __MEMORY_SIZE, start, end, value); is_valid_cmd = true; break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_RESET: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } // equivalent to fill 0, __MEMORY_SIZE-1 hexfill (mem, __MEMORY_SIZE, 0, __MEMORY_SIZE - 1, 0); is_valid_cmd = true; break; case CMD_OPCODE: switch(sscanf(input, "%*s %s", cmd)) { case 1: if(sscanf(input, "%*s %*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } i = find_oplist (cmd); if (i != -1) printf("opcode is %02X\n", i); else { printf("%s: NO SUCH OPCODE\n", cmd); is_valid_cmd = false; } break; default: puts("WRONG INSTRUCTION"); break; } break; case CMD_OPCODELIST: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } print_oplist (); is_valid_cmd = true; break; case CMD_ASSEMBLE: // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds %%%ds %%1s", __CMD_SIZE - 1, __FILENAME_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd, filename, check)!=2) { puts("WRONG INSTRUCTION"); break; } if (!is_file((const char*)filename)) { puts("FILE NOT FOUND"); break; } is_valid_cmd = assemble_file (filename); break; case CMD_TYPE: // Processing input string snprintf((char *) __CMD_FORMAT, __CMD_FORMAT_SIZE, "%%%ds %%%ds %%1s", __CMD_SIZE - 1, __FILENAME_SIZE - 1); if (sscanf(input, (const char *) __CMD_FORMAT, cmd, filename, check)!=2) { puts("WRONG INSTRUCTION"); break; } if (!is_file((const char*)filename)) { puts("FILE NOT FOUND"); break; } else { print_file((const char*)filename); is_valid_cmd = true; } break; case CMD_SYMBOL: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } print_symbol_table (); is_valid_cmd = true; break; case CMD_PROGADDR: if(sscanf(input, "%*s %*x %1s", check) == 1 || sscanf(input, "%*s %x", &i) != 1) { puts("WRONG INSTRUCTION"); break; } if (i < 0 || i >= __MEMORY_SIZE - 1) { puts("INVALID PROGRAM ADDRESS"); break; } set_progaddr ((uint32_t) i); is_valid_cmd = true; break; case CMD_LOADER: init_loader (); tok = strtok (input, " "); while ( (tok = strtok (NULL, " ")) != NULL) { if (!is_file (tok)) { printf ("[%s]: INVALID FILE\n", tok); free_loader (); break; } if (!add_obj_loader (tok)) { printf ("[%s]: LOADER FAILED\n", tok); free_loader (); break; } } // if normally added if (tok == NULL) { // address __MEMORY_SIZE is reserved for boot if (get_proglen()+get_progaddr()>=__MEMORY_SIZE-1) { puts ("PROGRAM IS TOO BIG: LOADER FAILED"); free_loader (); break; } if (!run_loader (mem)) { puts ("LOADER FAILED"); free_loader (); break; } print_load_map (); } free_loader (); is_valid_cmd = true; break; case CMD_RUN: if(sscanf(input, "%*s %1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } if (!init_run ()) { puts ("RUN FAILED"); free_run (); break; } run (mem); free_run (); is_valid_cmd = true; break; case CMD_BP: if(sscanf(input, "%*s %1s", check) != 1) { print_bp (); is_valid_cmd = true; break; } if(sscanf(input, "%*s %6s %1s", cmd, check) == 2 || sscanf(input, "%*s %6s", cmd) != 1) { puts("WRONG INSTRUCTION"); break; } if (_SAME_STR(cmd, "clear")) { puts ("\t[ok] clear all breakpoints"); free_bp (); is_valid_cmd = true; break; } if(sscanf(input, "%*s %*x %1s", check) == 1 || sscanf(input, "%*s %x", &start) != 1) { puts("WRONG INSTRUCTION"); break; } if (start >= __MEMORY_SIZE - 1) { puts ("INVALID BREAKPOINT ADDRESS"); break; } if (add_bp (start)) printf ("\t[ok] create breakpoint %x\n", start); is_valid_cmd = true; break; default: if(sscanf(input, "%1s", check) == 1) { puts("WRONG INSTRUCTION"); break; } } if (is_valid_cmd) { // Saving commands struct cmd_elem *e = malloc(sizeof(struct cmd_elem)); if (e == NULL) { puts("MEMORY INSUFFICIENT."); goto memory_clear; } e->cmd = malloc(sizeof(char)*(strlen(input)+1)); if (e->cmd == NULL) { puts("MEMORY INSUFFICIENT."); goto memory_clear; } strcpy(e->cmd, input); q_insert (&cmd_queue, &(e->elem)); } } memory_clear: if (mem != NULL) free (mem); if (input != NULL) free (input); if (cmd != NULL) free (cmd); while (!q_empty(&cmd_queue)) { struct q_elem *e = q_delete(&cmd_queue); struct cmd_elem *ce = q_entry(e, struct cmd_elem, elem); if (ce->cmd != NULL) free(ce->cmd); free(ce); } free_oplist (); free_loader (); free_bp (); free_run (); return 0; }
// Helper function to remove stop words not preceded by the inclusion operator void remove_stop_words_helper(syntree **free_text_tree) { int right_tree_stop_word = FALSE; int left_tree_stop_word = FALSE; syntree *left_stop_word_to_be_deleted = NULL; syntree *right_stop_word_to_be_deleted = NULL; syntree *operator_tree_to_be_deleted = NULL; syntree *left_word_tree = NULL; syntree *right_word_tree = NULL; // We don't want to look at/below the WORD nodes or PHRASE nodes. // Because the tree below them is inconsequential. if ((*free_text_tree)->left_tree && ((*free_text_tree)->left_tree->type != FT_WORD_TYPE && (*free_text_tree)->left_tree->type != FT_PHRASE_TYPE && (*free_text_tree)->left_tree->type != FT_NOT_TYPE)) { remove_stop_words_helper(&((*free_text_tree)->left_tree)); } if ((*free_text_tree)->right_tree && ((*free_text_tree)->right_tree->type != FT_WORD_TYPE && (*free_text_tree)->right_tree->type != FT_PHRASE_TYPE && (*free_text_tree)->right_tree->type != FT_NOT_TYPE)) { remove_stop_words_helper(&((*free_text_tree)->right_tree)); } if (((*free_text_tree)->type == FT_AND_TYPE) || ((*free_text_tree)->type == FT_OR_TYPE)) { // Check if the right or left tree of an operator is NULL. If it is, // the remove that node and make it point to its non-null child. // Case 1 : Left tree is NULL while right tree is not NULL. if ((*free_text_tree)->left_tree == NULL && (*free_text_tree)->right_tree != NULL) { right_word_tree = (*free_text_tree)->right_tree; operator_tree_to_be_deleted = (*free_text_tree); *free_text_tree = right_word_tree; syntree_delete_root_node(operator_tree_to_be_deleted); return; } // Case 2 : Right tree is NULL while left tree is not NULL. if ((*free_text_tree)->right_tree == NULL && (*free_text_tree)->left_tree != NULL) { left_word_tree = (*free_text_tree)->left_tree; operator_tree_to_be_deleted = (*free_text_tree); *free_text_tree = left_word_tree; syntree_delete_root_node(operator_tree_to_be_deleted); return; } // Look at the left tree for a stop word if ((*free_text_tree)->left_tree->type == FT_WORD_TYPE) { if (is_stop_word(get_chars(((word_syntree *)(*free_text_tree)->left_tree)->word))) { left_tree_stop_word = TRUE; } } // Look at the right tree for a stop word if ((*free_text_tree)->right_tree->type == FT_WORD_TYPE) { if (is_stop_word(get_chars(((word_syntree *)(*free_text_tree)->right_tree)->word))) { right_tree_stop_word = TRUE; } } // Case 1 : Both right and left trees have stop words // Delete the left and right stop word nodes along with the current operator node and // make it null. if (left_tree_stop_word == TRUE && right_tree_stop_word == TRUE) { left_stop_word_to_be_deleted = (*free_text_tree)->left_tree; right_stop_word_to_be_deleted = (*free_text_tree)->right_tree; operator_tree_to_be_deleted = (*free_text_tree); *free_text_tree = NULL; logwrite ("Note: The following words are very common and were not included in your search: %s %s.", get_chars (((word_syntree *)left_stop_word_to_be_deleted)->word), get_chars (((word_syntree *)right_stop_word_to_be_deleted)->word)); ft_word_delete((word_syntree *)left_stop_word_to_be_deleted); ft_word_delete((word_syntree *)right_stop_word_to_be_deleted); syntree_delete_root_node(operator_tree_to_be_deleted); } // Case 2 : Left tree is a stop word // Delete the left node, the operator node and make the current node point // to the right tree. if (left_tree_stop_word == TRUE && right_tree_stop_word == FALSE) { left_stop_word_to_be_deleted = (*free_text_tree)->left_tree; right_word_tree = (*free_text_tree)->right_tree; operator_tree_to_be_deleted = (*free_text_tree); *free_text_tree = right_word_tree; logwrite("Note: The following word is very common and was not included in your search: %s.", get_chars (((word_syntree *)left_stop_word_to_be_deleted)->word)); ft_word_delete((word_syntree *)left_stop_word_to_be_deleted); syntree_delete_root_node(operator_tree_to_be_deleted); } // Case 3 : Right tree is a stop word // Delete the right node, the operator node and make the current node point // to the left tree. if (right_tree_stop_word == TRUE && left_tree_stop_word == FALSE) { right_stop_word_to_be_deleted = (*free_text_tree)->right_tree; left_word_tree = (*free_text_tree)->left_tree; operator_tree_to_be_deleted = (*free_text_tree); *free_text_tree = left_word_tree; logwrite("Note: The following word is very common and was not included in your search: %s.", get_chars (((word_syntree *)right_stop_word_to_be_deleted)->word)); ft_word_delete((word_syntree *)right_stop_word_to_be_deleted); syntree_delete_root_node(operator_tree_to_be_deleted); } } }
void delivered_list_update(void) { TMAN_ROW *tmdatasrcmtb_row; TMAN_CURSOR *tmdatasrcmtb_cursor; int status; int update_queue_col_no; int datasrc_col_no; char sql_cmd_str[SQL_STATEMENT_SIZE]; // SQL_STATEMENT_SIZE is 4k. This is more than enough to hold this query. // 1. Pull the rows from the vl_datasrcmtb table where modified = 1 sql_cmd_str[0] = '\0'; sprintf(sql_cmd_str, "select updQueueName, DataSrcName from vl_datasrcmtb where modified = 1"); // Set the column numbers for the data source name and the update queue table name // based on the query. datasrc_col_no = 1; update_queue_col_no = 0; tmdatasrcmtb_cursor = tman_open_cursor(sql_cmd_str, TM_QUERY_NORMAL); if (tmdatasrcmtb_cursor != NULL) { status = TYPE_VALUE_NOT_EMBEDDED_IN_ALIAS; tmdatasrcmtb_row = tman_fetch(tmdatasrcmtb_cursor, &status); // None of the data sources have been updated. if (tmdatasrcmtb_row == NULL) { tman_close_cursor(tmdatasrcmtb_cursor); return; } } else { return; } // 2. For each row selected from vl_datasrcmtb, add the datasrc name to updated data source list. while (tmdatasrcmtb_row != NULL) { int datasrc_status; int datasrc_type; char *upd_queue_name; char *datasrc_name; DATUM *datasrc_obj; // Get the update data source table name and the update queue table name for this data source datasrc_obj = tman_get_field(tmdatasrcmtb_row, datasrc_col_no); datasrc_name = get_chars(datasrc_obj); upd_queue_name = get_chars(tman_get_field(tmdatasrcmtb_row, update_queue_col_no)); // Find the status of the data source. get_datasrc_status_and_type(datasrc_obj, &datasrc_status, &datasrc_type); ASSERT(datasrc_type == DATABASE_DATASRC || datasrc_type == VOLATILE_STREAM_DATASRC || datasrc_type == PERSISTENT_STREAM_DATASRC || datasrc_type == DATASRC_DROPPED); // A dropped data source has no type. ASSERT(datasrc_status == DATASRC_DROPPED || datasrc_status == DATASRC_DISABLED || datasrc_status == DATASRC_ENABLED); if (datasrc_status == DATASRC_DROPPED) { // The update queue table and native triggers are not there anymore. // SO DONT DO ANYTHING. } else if(datasrc_status == DATASRC_DISABLED) { // The data source is disabled. Triggers should not fire for any new update descriptors. // Delete the update descriptors from the update queue table for this data source. del_upd_descr_for_disabled_datasrc(upd_queue_name); } else if (datasrc_status == DATASRC_ENABLED) { // The data source needs to be processed. // Add the updated data source to the main memory list vl_deliver_upd(datasrc_name, upd_queue_name); } else { TMAN_HALT; } tman_delete(tmdatasrcmtb_row); tmdatasrcmtb_row = tman_fetch(tmdatasrcmtb_cursor, &status); } if (tmdatasrcmtb_cursor) { tman_close_cursor(tmdatasrcmtb_cursor); } }
// Drop the trigger object and all the related ADTs int delTrigger(trigger *arg) { DATUM *after_clause_datasrc = NULL; char *after_clause_datasrc_name = NULL; RCG *temp_rcg; RCG_node *rcg_node; ListElement *cursor; // create trigger ADT cannot be null ASSERT(arg); // Get the name of the datasrc in the after clause if there is one. if (arg->after_clause == NULL) { after_clause_datasrc = NULL; } else { get_after_clause_datasrc(arg->after_clause, &after_clause_datasrc); after_clause_datasrc_name = get_chars(after_clause_datasrc); } // Extract the RCG pointer temp_rcg = (RCG *)arg->rule_condition_graph; // The RCG graph cannot be NULL ASSERT(temp_rcg); // Delete the trigger ID nodes of this trigger for all the data sources rcg_node = (RCG_node *)List_getFirst(temp_rcg->nodes, &cursor); while (rcg_node != NULL) { // If the after clause is specified, then check whether the tuple variable name of this RCG node matches the // tuple variable name specified in the after clause. If they match, then this rcg node has a corresponding // trigger id node, otherwise it does not have one. if (after_clause_datasrc != NULL) { if (strcmp(rcg_node->tuple_variable_name, after_clause_datasrc_name) != 0) { // The rcg node's tuple variable name does not match with the after clause. // So it does not have a trigger ID node entry in the SPI, so skip this rcg node. rcg_node = (RCG_node *)List_getNext(&cursor); continue; } } // Find the trigger ID nodes for this rcg node and delete them delete_trigger_ID_node(rcg_node, DEFAULT_INDEX); // If the trigger ID list is empty, then delete the constant. if (List_isEmpty(rcg_node->trigger_ID_list[DEFAULT_INDEX])) { // Delete the trigger ID list for this constant set tman_delete(rcg_node->trigger_ID_list[DEFAULT_INDEX]); // Remove the constant node from the expression list remove_constant_node_from_sig(rcg_node, DEFAULT_INDEX); } // Check whether the constant set for the expression signature is empty. // If yes, then delete the expression signature . del_expr_sig_when_const_set_is_empty(rcg_node, DEFAULT_INDEX); /* If the rcg node is for an INSERT/UPDATE trigger, then we have to delete all the data structures from the UPDATE branch.. */ if( rcg_node->opcode == TM_INSERT_UPDATE ) { delete_trigger_ID_node(rcg_node, INS_UPD_INDEX); if (List_isEmpty(rcg_node->trigger_ID_list[INS_UPD_INDEX])) { // Delete the trigger ID list for this constant set tman_delete(rcg_node->trigger_ID_list[INS_UPD_INDEX]); // Remove the constant node from the expression list remove_constant_node_from_sig(rcg_node, INS_UPD_INDEX); } // Check whether the constant set for the expression signature is empty. // If yes, then delete the expression signature . del_expr_sig_when_const_set_is_empty(rcg_node, INS_UPD_INDEX); } rcg_node = (RCG_node *)List_getNext(&cursor); } // Delete the Gator network for this trigger tman_delete(arg->gator); // Delete the RCG network for this trigger tman_delete(arg->rule_condition_graph); // Delete the create trigger data structure for this trigger // This deletes the actual syntax trees when the trigger was first created tman_delete(arg); return TMAN_OK; }
inline auto get_chars_count(uintptr_t const Codepage, std::string_view const Str) { return get_chars(Codepage, Str, nullptr, 0); }
const char *HASH_get_chars(const hash_t *hash, const char *key){ return get_chars(hash, key, 0); }
void build_RCG(RCG *graph, syntree *arg) { ASSERT(graph); if (arg == NULL) return; if (arg->type == AND_TYPE || arg->type == WHEN_CLAUSE_TYPE) { /* left and right tree of the arg are collections of predicates */ build_RCG(graph, arg->left_tree); build_RCG(graph, arg->right_tree); } else { /* Node is other than the AND and WHEN_CLAUSE type. Hence is the root of the predicate */ int num_var; syntree *p, *predicate_root; stack *s; List *datasrc_names; RCG_node *node; p = arg; predicate_root = arg; s = stack_new(); /* Get the num of tuple variables in the predicate */ datasrc_names = List_new(); num_var = get_num_tuple_variables(arg, datasrc_names); // Delete the backbone of the list containing the REF to the data source names // The actual data source names get deleted during drop trigger List_deleteBackbone(datasrc_names); // Add the constant only predicate with no variables to every RCG node. // Also make sure it is not a boolean predicate like (WHEN TRUE - any trigger // without a WHEN CLAUSE is considered a WHEN TRUE predicate). if (num_var == 0 && arg->type != BOOL_LIT_TYPE) { // Is a selection predicate. ListElement *cursor; RCG_node *rcg_node; for (rcg_node = (RCG_node *)List_getFirst(graph->nodes, &cursor); rcg_node; rcg_node = (RCG_node *)List_getNext(&cursor)) { List_insertElement(rcg_node->selection_predicates_list, predicate_root); } } else if (num_var == 2) { /* Is a join predicate */ /* Pre-Order traversal of tree without recursion */ push(s, p); while(!is_stack_empty(s)) { p = pop(s); if (p->type == ID_TYPE && p->augtype == DATA_SRC_TYPE) { /*node is of DATASRC type */ /* Get the index of datasrc in the RCG */ node = get_node_from_RCG(get_chars(p->datum), graph); if (!node->already_inserted) { /* add the predicate root to the join predicate list of index node */ List_insertElement(node->join_predicates_list, predicate_root); /* Make an entry that it is inserted in index node */ node->already_inserted = 1; } } if (p->right_tree && p->type != DOT_TYPE) push(s, p->right_tree); if (p->left_tree) push(s, p->left_tree); } /* reset the already_inserted value */ reset(graph); /* predicate inserted in all nodes */ stack_delete(s); return; } else if (num_var == 1) { /* IS a selection predicate */ /* Pre-Order traversal of tree without recursion */ push(s, p); while(!is_stack_empty(s)) { p = pop(s); // A FT_PREDICATE_TYPE node must not be found here, the traversal must have ended before itself. ASSERT (p->type != FT_PREDICATE_TYPE); if (p->type == ID_TYPE && p->augtype == DATA_SRC_TYPE) { /*node is of DATASRC type */ /* Get the index of datasrc in the RCG */ node = get_node_from_RCG(get_chars(p->datum), graph); /* add the predicate root to the selection predicate list of index node */ List_insertElement(node->selection_predicates_list, predicate_root); /* Predicate inserted. No need to traverse any further*/ stack_delete(s); return; } if (p->right_tree && p->type != DOT_TYPE) push(s, p->right_tree); if (p->left_tree) push(s, p->left_tree); } /* not reached - to avoid compile time error */ return; } // We don't support zero tuple variable predicates like 0 = 0 or 0 + 5 < 10 // To process complex predicates involving more than 2 tuple var else if (num_var > 2) { // Predicates with more than two tuple variables use catch all list List_insertElement(graph->catch_all, predicate_root); stack_delete(s); return; } stack_delete(s); } }
int get_num_tuple_variables(syntree *arg, List *datasrc_names) { stack *s; int num_var = 0; ListElement *cursor; ASSERT (arg); ASSERT (datasrc_names); s = stack_new(); /* Pre-Order traversal of tree */ push(s, arg); while(!is_stack_empty(s)) { arg = pop(s); // In case of a free text predicate, don't traverse this subtree since it contains special tree nodes // that differ from general syntree structure. if (arg->type == FT_PREDICATE_TYPE) { continue; } if (arg->type == DOT_TYPE) { /* check if the datasrc is already present in the list */ int found = 0; char *temp_name; for (temp_name = (char *)List_getFirst(datasrc_names, &cursor); temp_name; temp_name = (char *)List_getNext(&cursor)) { /* Left tree of dot node is the data source name */ if (tm_strcmp(get_chars(arg->left_tree->datum), temp_name) == 0) { /* dtasrc already present in the list */ found = 1; break; } } if (!found) { /* Left tree of dot node is the data source name. ** datasrc not present. Insert it now. */ ASSERT (arg->left_tree); List_insertElement(datasrc_names, get_chars(arg->left_tree->datum)); //logwrite("%d", arg->left_tree->type); num_var++; } } if (arg->right_tree && arg->type != DOT_TYPE) push(s, arg->right_tree); if (arg->left_tree) push(s, arg->left_tree); } stack_delete(s); return num_var; }
/* when triggerman startup, ** it is necessary to check the all the update table. ** if there is any tasks in any update table, ** put them into the tman_update_delivered_list ** by calling vl_deliver_upd */ int delivered_list_init(void) { TMAN_ROW *datasrc_row; TMAN_CURSOR *datasrc_cursor; int status; char cmd_str1[SQL_STATEMENT_SIZE]; char cmd_str2[SQL_STATEMENT_SIZE]; char delimited_ident[TMAN_MAX_NAME + SPACE_FOR_DELIMITERS]; int datasrc_col_no, update_queue_col_no, datasrc_type_col_no; cmd_str1[0] = '\0'; //1. Pull the rows from the vl_datasrc table sprintf(cmd_str1, "select datasrcname, updqueuename, datasrctype from vl_DataSrc"); datasrc_cursor = tman_open_cursor(cmd_str1, TM_QUERY_NORMAL); status = TYPE_VALUE_NOT_EMBEDDED_IN_ALIAS; datasrc_row = tman_fetch(datasrc_cursor, &status); // Assign the column numbers based on the query. datasrc_col_no = 0; // Data source name update_queue_col_no = 1; // Update Queue name datasrc_type_col_no = 2; // Data source type // 2. For each row in vl_datasrc, check if there is any update in the corresponding update table. // if yes, call vl_deliver_upd while (datasrc_row) { int datasrc_type; // Get the data source type. datasrc_type = (int)get_int(tman_get_field(datasrc_row, datasrc_type_col_no)); // Only if it is a database data source, check for unprocessed updates. if (datasrc_type == DATABASE_DATASRC) { TMAN_ROW *count_row; TMAN_CURSOR *count_cursor; char *datasrc_name, *upd_queue_name; long update_count; datasrc_name = get_chars(tman_get_field(datasrc_row, datasrc_col_no)); upd_queue_name = get_chars(tman_get_field(datasrc_row, update_queue_col_no)); cmd_str2[0] = '\0'; sprintf(cmd_str2, "select count(ds_uid) from %s", add_delimiters_to_identifier_if_necessary(upd_queue_name, delimited_ident)); count_cursor = tman_open_cursor(cmd_str2, TM_QUERY_NORMAL); count_row = tman_fetch(count_cursor, &status); update_count = 0; if (count_row != NULL) { update_count = get_ds_uid_count(count_row); } if (update_count > 0) { #ifdef _MIDDLE vl_deliver_upd(datasrc_name, upd_queue_name); #endif #ifdef DATABLADE cmd_str2[0] = '\0'; sprintf(cmd_str2, "execute procedure vl_deliver_upd(\'%s\', \'%s\')", datasrc_name, upd_queue_name); tman_execsql(cmd_str2); #endif } // Free the row that got the DS_UID value if (count_row) { tman_delete(count_row); } if(count_cursor) { tman_close_cursor(count_cursor); } } tman_delete(datasrc_row); //get next row from the data source datasrc_row = tman_fetch(datasrc_cursor, &status); } if (datasrc_cursor) { tman_close_cursor(datasrc_cursor); } return TMAN_OK; }
const char *HASH_get_chars_at(const hash_t *hash, const char *key, int i){ return get_chars(hash, key, i); }
auto get_chars(uintptr_t const Codepage, std::string_view const Str, T& Buffer) { return get_chars(Codepage, Str, std::data(Buffer), std::size(Buffer)); }