symbol_table * create_symbol_table(){ char * lib_functions[] = { "print","input","objectmemberkeys","objecttotalmembers", "objectcopy","totalarguments","argument","typeof","strtonum", "sqrt","cos","sin" }; unsigned int i; st_entry * symbol; symbol_table * st = (symbol_table *)malloc(sizeof(symbol_table)); if(memerror(st,"symbol table")) return NULL; for(i=0;i<BUCKET_SIZE;i++) st->hash_table[i] = NULL; st->scope_list = NULL; // We add all the library functions from the beginning in the symbol table. for(i=0;i<12;i++){ symbol = create_symbol(lib_functions[i],1,0,0,LIBFUNC,0,0); if(memerror(symbol,"initalize lib func")){ free(st); return NULL; } st_insert(&st,&symbol); } return st; }
int args_insert(arg_node ** args,const char * arg_name){ arg_node * temp = *args; if(args_lookup(*args,arg_name)!=NULL) return 0; arg_node * arg = (arg_node *)malloc(sizeof(arg_node)); if(memerror(arg,"func arg")) return 0; arg->name = malloc(strlen(arg_name)+1); if(memerror(arg->name,"func arg:name")){ free(arg); return 0; } strcpy(arg->name,arg_name); arg->next = NULL; if(*args==NULL) *args = arg; else { while((*args)->next!=NULL) *args = (*args)->next; (*args)->next = arg; *args = temp; } return 1; }
st_entry * create_symbol(const char * name, unsigned int active, unsigned int scope,unsigned int line,st_entry_type type, unsigned int offset, scopespace_t space){ st_entry * symbol = (st_entry *)malloc(sizeof(st_entry)); if(memerror(symbol,"symbol")) return NULL; symbol->name = malloc(strlen(name)+1); if(memerror(symbol->name,"symbol:name")){ free(symbol); return NULL; } strcpy(symbol->name,name); symbol->active = active; symbol->scope = scope; symbol->line = line; symbol->type = type; symbol->offset = offset; symbol->space = space; // Depending on the symbol type we set the correct variables. if(type==USERFUNC || type==LIBFUNC){ symbol->value_type.funVal = (function *)malloc(sizeof(function)); if(memerror(symbol->value_type.funVal,"funval")) { free(symbol->name); free(symbol); return NULL; } symbol->value_type.funVal->arguments = NULL; } else { symbol->value_type.varVal = (variable *)malloc(sizeof(variable)); if(memerror(symbol->value_type.varVal,"varVal")){ free(symbol->name); free(symbol); return NULL; } symbol->value_type.varVal->used_in_func = NULL; } symbol->next = NULL; return symbol; }
/** * @brief Creates a new tree node (by doing the required allocations). @param name: The name of the node. @param ptr: The pointer to the data that will be copied to the node. @param data_ptr: A pointer to the data that will be copied to the new node. @return: The new allocated tree node. */ tree_node * create_tree_node(const char * name, void * data_ptr){ tree_node * new_node = (tree_node *)malloc(sizeof(tree_node)); if(memerror(new_node,"new tree node")) return NULL; new_node->name = malloc(strlen(name)+1); if(memerror(new_node->name,"new tree node name")){ free(new_node); return NULL; } strcpy(new_node->name,name); new_node->data_ptr = data_ptr; new_node->lc = NULL; new_node->rc = NULL; return new_node; }
int deletefile(char *file, BCoptions options, char *key, struct stat statbuf) { int lsize; long g; uLong j = 0, k = 0; signed char i; char *state, *garbage; FILE *fd; if (options.securedelete > 0) { lsize = sizeof(long); k = (statbuf.st_size / lsize) + 1; if ((state = malloc(257)) == NULL) memerror(); initstate((unsigned long) key, state, 256); if ((garbage = malloc(lsize)) == NULL) memerror(); fd = fopen(file, "r+b"); if (!fd) { fprintf(stderr, "Error deleting file %s: %s\n", file, strerror(errno)); return(1); } for (i = options.securedelete; i > 0; i--) { fseek(fd, 0, SEEK_SET); for (j = 0; j < k; j += lsize) { g = random(); memcpy(garbage, &g, lsize); fwrite(garbage, lsize, 1, fd); } fflush(fd); } fclose(fd); } if (unlink(file)) { fprintf(stderr, "Error deleting file %s: %s\n", file, strerror(errno)); return(1); } return(0); }
uLong attachKey(char **input, char *key, uLong sz) { /* +3 so we have room for info tags at the beginning of the file */ if ((*input = realloc(*input, sz + MAXKEYBYTES + 3)) == NULL) memerror(); memcpy(*input+sz, key, MAXKEYBYTES); sz += MAXKEYBYTES; return(sz); }
st_entry * set_var_func(st_entry * symbol,const char * func_name){ if(symbol->type==USERFUNC || symbol->type==LIBFUNC){ printf("Error : Symbol (%s) varVal not initialized, is it a function?\n",symbol->name); return symbol; } symbol->value_type.varVal->used_in_func = malloc(strlen(func_name)+1); if(memerror(symbol->value_type.varVal->used_in_func,"user_in_funct")) return NULL; strcpy(symbol->value_type.varVal->used_in_func,func_name); return symbol; }
int st_insert(symbol_table ** st, st_entry ** symbol) { unsigned int key = generate_key((*symbol)->name); scope_entry * temp = (*st)->scope_list; scope_entry * previous = NULL; // Making a copy of the symbol to link it to the scope list. st_entry * symbol_cpy = create_symbol((*symbol)->name,(*symbol)->active,(*symbol)->scope,(*symbol)->line,(*symbol)->type,(*symbol)->offset,(*symbol)->space); if((*symbol)->value_type.varVal->used_in_func != NULL) symbol_cpy = set_var_func(symbol_cpy,(*symbol)->value_type.varVal->used_in_func); // Insertion in the hash table. if ((*st)->hash_table[key]) (*symbol)->next = (*st)->hash_table[key]; (*st)->hash_table[key] = *symbol; // Insertion in the scope list. while (temp && temp->scope<(*symbol)->scope){ previous = temp; temp = temp->next; } if (temp == NULL || temp->scope != (*symbol)->scope) { temp = (scope_entry *)malloc(sizeof(scope_entry)); if (memerror(temp,"scope entry")) return 0; temp->scope = (*symbol)->scope; if(previous!=NULL){ temp->next = previous->next; previous->next = temp; } else { temp->next = (*st)->scope_list; (*st)->scope_list = temp; } temp->symbols = symbol_cpy; } else symbol_cpy->next = temp->symbols; temp->symbols = symbol_cpy; // Seting the pointer to the last symbol that was added if(symbol_cpy->type!=FORMAL) (*st)->last_symbol = symbol_cpy; return 1; }
uLong padInput(char **input, uLong sz) { int r, j; j = sizeof(uInt32) * 2; if (sz >= j) r = getremain(sz, j); else r = j - sz; if ( r < j) { if ((*input = realloc(*input, sz + r + 1)) == NULL) memerror(); memset(*input+sz, 0, r + 1); sz+=r; } return(sz); }
uLong readfile(char *infile, char **input, int type, char *key, struct stat statbuf) { FILE *fd; int readsize; uLong sz = 0; readsize = statbuf.st_size + 1; fd = fopen(infile, "rb"); if (!fd) { fprintf(stderr, "Unable to open file %s\n", infile); return(-1); } if ((*input = malloc(readsize + sz + 1)) == NULL) memerror(); memset(*input+sz, 0, readsize); sz += fread(*input+sz, 1, readsize - 1, fd); fclose(fd); return(sz); }
void memcheck(void * p, const char * name){ if(memerror(p,name)) exit(0); }