// set the value of var <name> to <src>, return 0 if nonexistant bool ListVars::var_set (c_char name, char *src) { struct var_type *v = var_exist (name); if (v == NULL) return 0; v->handler (s, name, src, 0); return 1; }
// put the value of var <name> in <dest> with size <n>, return 0 if nonexistant bool ListVars::var_get (c_char name, char *dest, size_t n) { struct var_type *v = var_exist (name); if (v == NULL) return 0; v->handler (s, name, dest, n); return 1; }
// add a var with <name> and <handler>, return 0 if <name> is in use bool ListVars::var_add (c_char name, void (*handler)(NetServer *, c_char, char *, size_t)) { if (var_exist (name) != NULL) return 0; struct var_type *v = new var_type (name, handler); add ((void *)v); return 1; }
int var_exist(char *name, t_var *g_var) { if (strcmp(name, "RANDOM") == 0) return (1); if (strcmp(name, g_var->name) == 0) return (1); if (g_var->next) return (var_exist(name, g_var->next)); return (0); }
// delete a var with <name>, return 0 if nonexistant bool ListVars::var_del (c_char name) { struct var_type *v = var_exist (name); if (v == NULL) return 0; delete v; del ((void *)v); return 1; }
static t_env *init_list_no_env(char **env) { t_env *ret; char *tmp; ret = NULL; if (!var_exist(env, "PATH=")) { tmp = get_path(); ret = add_elem_end(NULL, "PATH", tmp); free(tmp); } if (!var_exist(env, "PWD=") || !var_exist(env, "OLDPWD=")) { tmp = getcwd(NULL, 0); ret = add_elem_end(ret, "PWD", tmp); ret = add_elem_end(ret, "OLDPWD", tmp); free(tmp); } if (!var_exist(env, "HOME=")) ret = add_elem_end(ret, "HOME", "/"); if (!var_exist(env, "SHLVL=")) ret = add_elem_end(ret, "SHLVL", "0"); if (!var_exist(env, "TERM=")) ret = add_elem_end(ret, "TERM", "xterm-256color"); return (ret); }
//Create new variable menu. int create_var_menu(Variable *varArry, int *arrySize, int arrySpot){ String type = (String)malloc(ARRAYLENGHT * sizeof (char)); String name = (String)malloc(ARRAYLENGHT * sizeof (char)); int flag; //Check if memory allocated properly. mem_check(type); mem_check(name); //Get the variable's type do{ //User interaction. printf("%s", "\n\nGive the variable's type. Valid types are Integer, Decimal, String and Bool. To go back type <:"); fgets(type, ARRAYLENGHT, stdin); flag = false; if (type[strlen(type) - 1] == '\n' && strlen(type) > 1){ type[strcspn(type, "\n")] = 0; //Drop the \n from fgets input strtolower(type); //Convert the string to lowercase for ease of use. //Check for back input if (!strcmp(type, "<")){ return false; } //Check for valid input. if (strcmp(type, "integer") && strcmp(type, "decimal") && strcmp(type, "string") && strcmp(type, "bool")){ printf("%s", "\n\nInvalid type!\n\n"); } } else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(type) > 1) clear_stdin(); //Clear stdin from extra input } } while (strcmp(type, "integer") && strcmp(type, "decimal") && strcmp(type, "string") && strcmp(type, "bool") || flag); //Loop until user decides to cooperate. //Get the variable's name. do{ //User interaction. printf("%s", "\n\nGive a name for the variable. The max lenght is 255 characters. It can't contain whitespaces or symbols, except underscores (_):"); fgets(name, ARRAYLENGHT, stdin); flag = false; //Check for valid lenght if (name[strlen(name) - 1] == '\n' && strlen(name) > 2){ name[strcspn(name, "\n")] = 0; //Drop the \n from fgets input. //Check for valid input if (strpbrk(name, " !@#$%^&*()-+=\\|[]{};\':\",./<>?") != NULL){ printf("%s", "\n\nInvalid name!\n\n"); } else if (var_exist(varArry, name, *arrySize)){ printf("%s", "\n\nA variable with that name already exists!\n\n"); } } else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); if (strlen(name) > 1) clear_stdin(); //Clear stdin from extra input } } while (flag || strpbrk(name, " `~!@#$%^&*()-+=\\|[]{};\':\",./<>?") != NULL || var_exist(varArry, name, *arrySize)); //Loop until valid. //Copy the name and the type in the variables struct array. strcpy(varArry[arrySpot].type, type); strcpy(varArry[arrySpot].name, name); //Free the memory we don't need anymore. free(name); free(type); //Run the function corresponding to the entered type. if (!strcmp(varArry[arrySpot].type, "integer")){ set_var_int(varArry, arrySpot); } else if (!strcmp(varArry[arrySpot].type, "decimal")){ set_var_decimal(varArry, arrySpot); } else if (!strcmp(varArry[arrySpot].type, "string")){ //Loop until the lenght is correct. do{ //User interaction. For strings we don't need any valid input checks. printf("%s", "\n\nGive the variables value (String, 255 characters max):"); fgets(varArry[arrySpot].value, ARRAYLENGHT, stdin); flag = false; if (varArry[arrySpot].value[strlen(varArry[arrySpot].value) - 1] == '\n') varArry[arrySpot].value[strcspn(varArry[arrySpot].value, "\n")] = 0; //Drop the \n from fgets input. else{ flag = true; printf("%s", "\n\nInvalid lenght. It must be between 1 and 255 characters!\n\n"); clear_stdin(); //Clear stdin from extra input } } while (flag); } else{ set_var_bool(varArry, arrySpot); } return true; }