/* * Load .psqlrc file, if found. */ static void process_psqlrc(char *argv0) { char home[MAXPGPATH]; char rc_file[MAXPGPATH]; char my_exec_path[MAXPGPATH]; char etc_path[MAXPGPATH]; char *envrc = getenv("PSQLRC"); find_my_exec(argv0, my_exec_path); get_etc_path(my_exec_path, etc_path); snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC); process_psqlrc_file(rc_file); if (envrc != NULL && strlen(envrc) > 0) { /* might need to free() this */ char *envrc_alloc = pstrdup(envrc); expand_tilde(&envrc_alloc); process_psqlrc_file(envrc_alloc); } else if (get_home_path(home)) { snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC); process_psqlrc_file(rc_file); } }
/* * Load .psqlrc file, if found. */ static void process_psqlrc(char *argv0) { char home[MAXPGPATH]; char rc_file[MAXPGPATH]; char my_exec_path[MAXPGPATH]; char etc_path[MAXPGPATH]; char *envrc = getenv("PSQLRC"); if (find_my_exec(argv0, my_exec_path) < 0) { fprintf(stderr, _("%s: could not find own program executable\n"), argv0); exit(EXIT_FAILURE); } get_etc_path(my_exec_path, etc_path); snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC); process_psqlrc_file(rc_file); if (envrc != NULL && strlen(envrc) > 0) { /* might need to free() this */ char *envrc_alloc = pstrdup(envrc); expand_tilde(&envrc_alloc); process_psqlrc_file(envrc_alloc); } else if (get_home_path(home)) { snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC); process_psqlrc_file(rc_file); } }
/* expand_tilde * * substitute '~' with HOME or '~username' with username's home dir * */ char * expand_tilde(char **filename) { if (!filename || !(*filename)) return NULL; /* * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde * for short versions of long file names, though the tilde is usually * toward the end, not at the beginning. */ #ifndef WIN32 /* try tilde expansion */ if (**filename == '~') { char *fn; char oldp, *p; struct passwd *pw; char home[MAXPGPATH]; fn = *filename; *home = '\0'; p = fn + 1; while (*p != '/' && *p != '\0') p++; oldp = *p; *p = '\0'; if (*(fn + 1) == '\0') get_home_path(home); /* ~ or ~/ only */ else if ((pw = getpwnam(fn + 1)) != NULL) strlcpy(home, pw->pw_dir, sizeof(home)); /* ~user */ *p = oldp; if (strlen(home) != 0) { char *newfn; newfn = pg_malloc(strlen(home) + strlen(p) + 1); strcpy(newfn, home); strcat(newfn, p); free(fn); *filename = newfn; } } #endif return *filename; }
/* * Put any startup stuff related to input in here. It's good to maintain * abstraction this way. * * The only "flag" right now is 1 for use readline & history. */ void initializeInput(int flags) { #ifdef USE_READLINE if (flags & 1) { const char *histfile; char home[MAXPGPATH]; useReadline = true; initialize_readline(); useHistory = true; using_history(); history_lines_added = 0; histfile = GetVariable(pset.vars, "HISTFILE"); if (histfile == NULL) { char *envhist; envhist = getenv("PSQL_HISTORY"); if (envhist != NULL && strlen(envhist) > 0) histfile = envhist; } if (histfile == NULL) { if (get_home_path(home)) { psql_history = pg_malloc(strlen(home) + 1 + strlen(PSQLHISTORY) + 1); snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY); } } else { psql_history = pg_strdup(histfile); expand_tilde(&psql_history); } if (psql_history) { read_history(psql_history); decode_history(); } } #endif atexit(finishInput); }
/* * Put any startup stuff related to input in here. It's good to maintain * abstraction this way. * * The only "flag" right now is 1 for use readline & history. */ void initializeInput(int flags) { #ifdef USE_READLINE if (flags & 1) { const char *histfile; char home[MAXPGPATH]; useReadline = true; initialize_readline(); useHistory = true; using_history(); histfile = GetVariable(pset.vars, "HISTFILE"); if (histfile == NULL) { if (get_home_path(home)) { psql_history = pg_malloc(strlen(home) + 1 + strlen(PSQLHISTORY) + 1); snprintf(psql_history, MAXPGPATH, "%s/%s", home, PSQLHISTORY); } } else { psql_history = pg_strdup(histfile); expand_tilde(&psql_history); } if (psql_history) { read_history(psql_history); decode_history(); } } #endif #ifdef HAVE_ATEXIT atexit(finishInput); #else on_exit(finishInput, NULL); #endif }
/* * Load .psqlrc file, if found. */ static void process_psqlrc(char *argv0) { char home[MAX_PG_PATH]; char rc_file[MAX_PG_PATH]; char my_exec_path[MAX_PG_PATH]; char etc_path[MAX_PG_PATH]; find_my_exec(argv0, my_exec_path); get_etc_path(my_exec_path, etc_path); snprintf(rc_file, MAX_PG_PATH, "%s/%s", etc_path, SYSPSQLRC); process_psqlrc_file(rc_file); if (get_home_path(home)) { snprintf(rc_file, MAX_PG_PATH, "%s/%s", home, PSQLRC); process_psqlrc_file(rc_file); } }
int ssh::init() { int rc; get_home_path(_home_path); get_kownhostfile(_home_path , _full_kownhost); //the trust mode need load public key file and private key file if(_password.empty()){ get_publickey_file(_home_path , _full_dpublicfile ,_full_rpublicfile); get_privatekey_file(_home_path , _full_dprivatefile,_full_rprivatefile); } rc = libssh2_init (0); if (rc != 0) { cout << "libssh2 initialization failed (" << rc << ")" << std::endl; return 1; } return 0; }
s32 load_file(char **wildcards, char *result) { SDL_Event gui_event; char current_dir_name[MAX__PATH]; DIR *current_dir; struct dirent *current_file; struct stat file_info; char current_dir_short[81]; u32 current_dir_length; u32 total_filenames_allocated; u32 total_dirnames_allocated; char **file_list; char **dir_list; u32 num_files; u32 num_dirs; char *file_name; u32 file_name_length; u32 ext_pos = -1; u32 chosen_file, chosen_dir; u32 dialog_result = 1; s32 return_value = 1; u32 current_file_selection; u32 current_file_scroll_value; u32 current_dir_selection; u32 current_dir_scroll_value; u32 current_file_in_scroll; u32 current_dir_in_scroll; u32 current_file_number, current_dir_number; u32 current_column = 0; u32 repeat; u32 i; if(was_used == 0) { get_home_path(); chdir(config_home_path); was_used = 1; } while(return_value == 1) { current_file_selection = 0; current_file_scroll_value = 0; current_dir_selection = 0; current_dir_scroll_value = 0; current_file_in_scroll = 0; current_dir_in_scroll = 0; total_filenames_allocated = 32; total_dirnames_allocated = 32; file_list = (char **)malloc(sizeof(char *) * 32); dir_list = (char **)malloc(sizeof(char *) * 32); memset(file_list, 0, sizeof(char *) * 32); memset(dir_list, 0, sizeof(char *) * 32); num_files = 0; num_dirs = 0; chosen_file = 0; chosen_dir = 0; getcwd(current_dir_name, MAX__PATH); current_dir = opendir(current_dir_name); fprintf(stderr, "start_dir = %s\n", config_home_path); // DEBUG printf("Current directory: %s\n", current_dir_name); do { if(current_dir) current_file = readdir(current_dir); else current_file = NULL; if(current_file) { file_name = current_file->d_name; file_name_length = strlen(file_name); if((stat(file_name, &file_info) >= 0) && ((file_name[0] != '.') || (file_name[1] == '.'))) { if(S_ISDIR(file_info.st_mode)) { dir_list[num_dirs] = (char *)malloc(file_name_length + 1); strcpy(dir_list[num_dirs], file_name); num_dirs++; } else { // Must match one of the wildcards, also ignore the . if(file_name_length >= 4) { if(file_name[file_name_length - 4] == '.') ext_pos = file_name_length - 4; else if(file_name[file_name_length - 3] == '.') ext_pos = file_name_length - 3; else ext_pos = 0; for(i = 0; wildcards[i] != NULL; i++) { if(!strcasecmp((file_name + ext_pos), wildcards[i])) { file_list[num_files] = (char *)malloc(file_name_length + 1); strcpy(file_list[num_files], file_name); num_files++; break; } } } } } if(num_files == total_filenames_allocated) { file_list = (char **)realloc(file_list, sizeof(char *) * total_filenames_allocated * 2); memset(file_list + total_filenames_allocated, 0, sizeof(char *) * total_filenames_allocated); total_filenames_allocated *= 2; } if(num_dirs == total_dirnames_allocated) { dir_list = (char **)realloc(dir_list, sizeof(char *) * total_dirnames_allocated * 2); memset(dir_list + total_dirnames_allocated, 0, sizeof(char *) * total_dirnames_allocated); total_dirnames_allocated *= 2; } } } while(current_file); qsort((void *)file_list, num_files, sizeof(char *), sort_function); qsort((void *)dir_list, num_dirs, sizeof(char *), sort_function); // DEBUG //for(i = 0; i < num_dirs; i++) printf("%s\n", dir_list[i]); //for(i = 0; i < num_files; i++) printf("%s\n", file_list[i]); closedir(current_dir); current_dir_length = strlen(current_dir_name); if(current_dir_length > 80) { memcpy(current_dir_short, "...", 3); memcpy(current_dir_short + 3, current_dir_name + current_dir_length - 77, 77); current_dir_short[80] = 0; } else { memcpy(current_dir_short, current_dir_name, current_dir_length + 1); } repeat = 1; if(num_files == 0) current_column = 1; //clear_screen(COLOR_BG); char print_buffer[81]; while(repeat) { //flip_screen(); SDL_FillRect(menuSurface, NULL, COLOR_BG); print_string(current_dir_short, COLOR_ACTIVE_ITEM, COLOR_BG, 0, 0); print_string("Press B to return to the main menu", COLOR_HELP_TEXT, COLOR_BG, 20, 220); for(i = 0, current_file_number = i + current_file_scroll_value; i < FILE_LIST_ROWS; i++, current_file_number++) { if(current_file_number < num_files) { strncpy(print_buffer,file_list[current_file_number], 38); print_buffer[38] = 0; if((current_file_number == current_file_selection) && (current_column == 0)) { print_string(print_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, FILE_LIST_POSITION, ((i + 2) * 8)); } else { print_string(print_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, FILE_LIST_POSITION, ((i + 2) * 8)); } } } for(i = 0, current_dir_number = i + current_dir_scroll_value; i < FILE_LIST_ROWS; i++, current_dir_number++) { if(current_dir_number < num_dirs) { strncpy(print_buffer,dir_list[current_dir_number], 13); print_buffer[14] = 0; if((current_dir_number == current_dir_selection) && (current_column == 1)) { print_string(print_buffer, COLOR_ACTIVE_ITEM, COLOR_BG, DIR_LIST_POSITION, ((i + 2) * 8)); } else { print_string(print_buffer, COLOR_INACTIVE_ITEM, COLOR_BG, DIR_LIST_POSITION, ((i + 2) * 8)); } } } // Catch input // change to read key state later while(SDL_PollEvent(&gui_event)) { if(gui_event.type == SDL_KEYDOWN) { if(gui_event.key.keysym.sym == SDLK_LCTRL) { // DINGOO A - apply parameter or enter submenu if(current_column == 1) { repeat = 0; chdir(dir_list[current_dir_selection]); } else { if(num_files != 0) { repeat = 0; return_value = 0; //strcpy(result, file_list[current_file_selection]); sprintf(result, "%s/%s", current_dir_name, file_list[current_file_selection]); break; } } } if(gui_event.key.keysym.sym == SDLK_LALT) { // DINGOO B - exit or back to previous menu return_value = -1; repeat = 0; break; } if(gui_event.key.keysym.sym == SDLK_UP) { // DINGOO UP - arrow down if(current_column == 0) { if(current_file_selection) { current_file_selection--; if(current_file_in_scroll == 0) { //clear_screen(COLOR_BG); current_file_scroll_value--; } else { current_file_in_scroll--; } } } else { if(current_dir_selection) { current_dir_selection--; if(current_dir_in_scroll == 0) { //clear_screen(COLOR_BG); current_dir_scroll_value--; } else { current_dir_in_scroll--; } } } } if(gui_event.key.keysym.sym == SDLK_DOWN) { // DINGOO DOWN - arrow up if(current_column == 0) { if(current_file_selection < (num_files - 1)) { current_file_selection++; if(current_file_in_scroll == (FILE_LIST_ROWS - 1)) { //clear_screen(COLOR_BG); current_file_scroll_value++; } else { current_file_in_scroll++; } } } else { if(current_dir_selection < (num_dirs - 1)) { current_dir_selection++; if(current_dir_in_scroll == (FILE_LIST_ROWS - 1)) { //clear_screen(COLOR_BG); current_dir_scroll_value++; } else { current_dir_in_scroll++; } } } } if(gui_event.key.keysym.sym == SDLK_LEFT) { // DINGOO LEFT - decrease parameter value if(current_column == 1) { if(num_files != 0) current_column = 0; } } if(gui_event.key.keysym.sym == SDLK_RIGHT) { // DINGOO RIGHT - increase parameter value if(current_column == 0) { if(num_dirs != 0) current_column = 1; } } } } SDL_Delay(16); gui_Flip(); } // free pointers for(i = 0; i < num_files; i++) free(file_list[i]); free(file_list); for(i = 0; i < num_dirs; i++) free(dir_list[i]); free(dir_list); } return return_value; }