int ui_ask_char(char const *msg, char *valid) { int x = strlen(msg) + 5; char c = 0; do { ui_statusline_clear(); if (c != 0) { ui_statusline_msg("Bad choice, press any key to try again"); getch(); ui_statusline_clear(); } ui_statusline_msg(msg); echo(); show_cursor(); c = mvwgetch(bottom, 0, x); noecho(); hide_cursor(); } while (!strchr(valid, c)); ui_statusline_clear(); return c; }
void ui_statusline_ask_char(char *msg, char *c, char* valid) { int x = strlen(msg) + 5; char input[STRING_SHORT]; *c = 0; do { ui_statusline_clear(); if(*c != 0){ ui_statusline_msg("Bad choice, press any key to try again"); getch(); ui_statusline_clear(); } ui_statusline_msg(msg); echo(); show_cursor(); *c = mvwgetch(bottom, 1, x); noecho(); hide_cursor(); } while ( !strchr(valid, *c) ); ui_statusline_clear(); }
int action_list_launch() { int i; Pw* curpw; char msg[STRING_LONG]; switch(uilist_get_highlighted_type()){ case PW_ITEM: debug("list_launch: is a pw"); curpw = uilist_get_highlighted_item(); if(curpw){ i = launch(curpw); snprintf(msg, STRING_LONG, "Application exited with code %d", i); ui_statusline_msg(msg); } break; case PW_SUBLIST: case PW_UPLEVEL: case PW_NULL: default: /* do nothing */ break; } }
static void ui_display_help() { int i, l = 0; WINDOW *helpwin; int width = 65; helpwin = newwin(LINES - 5, width, 3, (COLS - width) / 2); box(helpwin, 0, 0); uilist_clear(); for (i = 0; help[i] != NULL; i++) { mvwaddstr(helpwin, l + 1, 1, help[i]); l++; if (!((i + 1) % (LINES - 8)) || (help[i + 1] == NULL)) { wrefresh(helpwin); ui_statusline_msg("Press any key to continue..."); getch(); wclear(helpwin); box(helpwin, 0, 0); l = 0; } } uilist_refresh(); ui_statusline_clear(); delwin(helpwin); }
char * ui_statusline_ask_str_with_autogen(char *msg, char *input, int len, char *(*autogen)(char *), int ch) { int i = 0; int c; char *text[2], *s; int x; if(input == NULL){ input = malloc(len); } text[0] = malloc(STRING_MEDIUM); text[1] = malloc(STRING_SHORT); strncpy(text[1], msg, STRING_SHORT); if(s = strrchr(text[1], ':')){ *s = 0; } snprintf(text[0], STRING_MEDIUM, "%s(%c for autogen):\t", text[1],ch); x = strlen(text[0]) + 5; ui_statusline_clear(); ui_statusline_msg(text[0]); show_cursor(); noecho(); wmove(bottom, 1, x); while(i < len){ c = wgetch(bottom); if(c == 0x7f){ if(i){ i--; mvwaddch(bottom, 1, x+i, ' '); wmove(bottom, 1, x+i); } } else if(c == 0xd){ input[i] = 0; break; } else if(c == ch){ input = autogen(input); break; } else { input[i] = c; mvwaddch(bottom, 1, x + i, c); i++; } } hide_cursor(); ui_statusline_clear(); free(text[0]); free(text[1]); return input; }
int action_input_gpgid_dialog(InputField *fields, int num_fields, char *title) { int i, valid_id; int ch = '1', first_time = 1; char *ret; WINDOW *dialog_win; char msg[] = "(press 'q' when export recipient list is complete)"; char msg2[80]; /* * initialize the info window */ disp_h = ((num_fields+2) * 2) + 3; dialog_win = newwin(disp_h, disp_w, (LINES - disp_h)/2, (COLS - disp_w)/2); keypad(dialog_win, TRUE); action_input_dialog_draw_items(dialog_win, fields, num_fields, title, msg); /* * actions loop - ignore read only as not changing main state */ while(first_time || ((ch = wgetch(dialog_win)) != 'q')){ // On first loop, drop straight into recipient 1 first_time = 0; if( (ch >= '1') && (ch <= NUM_TO_CHAR(num_fields)) ){ i = CHAR_TO_NUM(ch); fields[i].value = (char*)ui_statusline_ask_str(fields[i].name, (char*)fields[i].value, fields[i].max_length); // Now verify it's a valid recipient if(strlen(fields[i].value)) { valid_id = gnupg_check_id(fields[i].value); if(valid_id == 0) { // Good, valid id } else { // Invalid id. Warn and blank if(valid_id == -2) { snprintf(msg2, 80, "Key expired for '%s'", (char*)fields[i].value); } else { snprintf(msg2, 80, "Invalid recipient '%s'", (char*)fields[i].value); } ui_statusline_msg(msg2); snprintf(fields[i].value, STRING_LONG, ""); } // Redraw display action_input_dialog_draw_items(dialog_win, fields, num_fields, title, msg); } } } /* * clean up */ delwin(dialog_win); uilist_refresh(); }
int action_list_delete_item() { Pw* curpw; PWList* curpwl; int i; char str[STRING_LONG]; switch(uilist_get_highlighted_type()){ case PW_ITEM: curpw = uilist_get_highlighted_item(); if(curpw){ snprintf(str, STRING_LONG, "Really delete \"%s\"", curpw->name); i = ui_statusline_yes_no(str, 0); if(i){ pwlist_delete_pw(current_pw_sublist, curpw); ui_statusline_msg("Password deleted"); } else { ui_statusline_msg("Password not deleted"); } } break; case PW_SUBLIST: curpwl = uilist_get_highlighted_sublist(); if(curpwl){ snprintf(str, STRING_LONG, "Really delete Sublist \"%s\"", curpwl->name); i = ui_statusline_yes_no(str, 0); if(i){ pwlist_delete_sublist(curpwl->parent, curpwl); ui_statusline_msg("Password Sublist deleted"); } else { ui_statusline_msg("Password not deleted"); } } break; case PW_UPLEVEL: case PW_NULL: default: /* do nothing */ break; } uilist_refresh(); }
void pwgen_indep() { char *p, text[128]; p = pwgen_ask(); snprintf(text, sizeof(text), "Generated password: %s", p); free(p); ui_statusline_msg(text); }
int ui_statusline_yes_no(char *msg, int def) { int ret = -1, len; char *msg2; int ch; len = strlen(msg) + 10; msg2 = malloc(len); snprintf(msg2, len, "%s%s", msg, def ? " (Y/n)?" : " (y/N)?", NULL); while(ret == -1){ ui_statusline_msg(msg2); ch = getch(); switch( ch ){ case 'n': case 'N': ret = FALSE; break; case 'y': case 'Y': ret = TRUE; break; case 13: ret = def; break; default: ui_statusline_msg("Bad option, try again."); getch(); break; } } free(msg2); ui_statusline_clear(); return ret; }
char * ui_statusline_ask_passwd(char *msg, char *input, int len, int cancel) { int i = 0; int c; int x = strlen(msg) + 5; if(!input){ input = malloc(len); } ui_statusline_clear(); ui_statusline_msg(msg); show_cursor(); noecho(); wmove(bottom, 1, x); while(i < len){ c = wgetch(bottom); if(c == 0x7f){ /* 0x7f = delete */ if(i){ i--; mvwaddch(bottom, 1, x+i, ' '); wmove(bottom, 1, x+i); } } else if(c == 0xd){ /* 0xd == enter/return */ input[i] = 0; break; } else if(c == cancel){ free(input); input = NULL; return input; } else { input[i] = c; mvwaddch(bottom, 1, x + i, '*'); i++; } } hide_cursor(); ui_statusline_clear(); return input; }
void ui_statusline_ask_num(char *msg, int *i) { int x = strlen(msg) + 5; char input[STRING_SHORT]; ui_statusline_clear(); ui_statusline_msg(msg); echo(); show_cursor(); mvwgetnstr(bottom, 1, x, input, STRING_SHORT); *i = atoi(input); noecho(); hide_cursor(); ui_statusline_clear(); }
int ui_display_help() { int i; WINDOW *helpwin; helpwin = newwin(LINES - 5, COLS - 6, 3, 3); uilist_clear(); for(i = 0; help[i] != NULL; i++){ waddstr(helpwin, help[i]); if( !((i+1) % (LINES - 9)) || (help[i+1] == NULL) ){ /* refresh();*/ wrefresh(helpwin); ui_statusline_msg("Press any key to continue..."); getch(); wclear(helpwin); } } uilist_refresh(); ui_statusline_clear(); delwin(helpwin); }
int action_list_launch_xclip() { int i; Pw* curpw; char msg[STRING_LONG]; switch(uilist_get_highlighted_type()){ case PW_ITEM: debug("list_launch_xclip: is a pw"); curpw = uilist_get_highlighted_item(); if(curpw){ launch_xclip(curpw); ui_statusline_msg("Copied password to X clipboard"); } break; case PW_SUBLIST: case PW_UPLEVEL: case PW_NULL: default: /* do nothing */ break; } }
char * ui_statusline_ask_str(char *msg, char *input, int len) { char *tmp; char *tmp2; char *tmp3; int x = strlen(msg) + 5; if(input == NULL){ input = malloc(len); } ui_statusline_clear(); ui_statusline_msg(msg); echo(); show_cursor(); mvwgetnstr(bottom, 1, x, input, len); noecho(); hide_cursor(); ui_statusline_clear(); // Tabs don't play nicely with ncurses or xml // So, swap any for (a single) space tmp = input; while(*tmp != 0) { if(*tmp == 9) *tmp = ' '; tmp++; } // In some cases (eg when inside screen), the backspace // comes through to us. Handle it here if needed tmp = input; while(*tmp != 0) { if(*tmp == 8) { // tmp2 is where to copy to, tmp3 is where to copy from tmp3 = tmp + 1; if(tmp == input) { tmp2 = tmp; } else { tmp2 = tmp - 1; } // When we're done, start from the character // we copied in to tmp = tmp2; // Process forward while(*tmp3 != 0) { *tmp2 = *tmp3; tmp2++; tmp3++; } *tmp2 = 0; } else { tmp++; } } // All done return input; }
int ui_run() { int ch; int load_worked = 0; #ifdef DEBUG int debug_i = 0; #endif time_base = time(NULL); while (1) { can_resize = TRUE; if (should_resize) { ui_resize(); } ch = getch(); ui_statusline_clear(); can_resize = FALSE; if ((time_base < (time(NULL) - (options->passphrase_timeout * 60))) && options->passphrase_timeout != 0 && tolower(ch) != 'q') { folder_write_file(); folder_free_all(); ui_statusline_msg("Passphrase has timed out and you must enter it again."); getch(); load_worked = folder_read_file(); if (load_worked != 0) { ui_statusline_msg("Error - unable to re-load the password file!"); break; } if (search_results != NULL) search_remove(); time_base = time(NULL); continue; } switch (ch) { case 'Q': case 'q': if (search_results != NULL) search_remove(); else if (action_list_at_top_level()) return 0; break; case '?': ui_display_help(); break; case KEY_PPAGE: uilist_page_up(); break; case KEY_NPAGE: uilist_page_down(); break; case KEY_UP: case 'k': uilist_up(); break; case KEY_DOWN: case 'j': uilist_down(); break; case 'A': if (!options->readonly) action_list_add_sublist(); else statusline_readonly(); break; case 'U': action_list_up_one_level(); break; case 'r': if (!options->readonly) { action_list_rename(); folder_write_file(); } else { statusline_readonly(); } break; case 'a': if (!options->readonly) { action_list_add_pw(); folder_write_file(); } else { statusline_readonly(); } break; case 'e': case ' ': case 13: /* return/enter key */ action_list_select_item(); break; case 'x': action_list_mark(); break; case 'B': action_list_copy_username(); break; case 'C': action_list_copy_pw(); break; case 'd': case 0x14A: /* DEL key */ if (!options->readonly) action_list_delete_item(); else statusline_readonly(); break; case 'm': if (!options->readonly) action_list_move_item(); else statusline_readonly(); break; case 'o': action_edit_options(); break; case 0x17: /* control-w */ if (!options->readonly) folder_write_file(); else statusline_readonly(); break; case 0x12: /* control-r */ action_list_read_file(); break; case 0x07: /* control-g */ pwgen_indep(); break; case 0x06: /* control-f */ gnupg_forget_passphrase(); break; case 0x0C: /* control-l */ ui_refresh_windows(); break; case '/': case 'F': search_get(); break; case 'f': filter_get(); break; case 'E': action_list_export(); break; case 'I': if (!options->readonly) { folder_import_passwd(); uilist_refresh(); } else { statusline_readonly(); } break; case 'L': action_list_locate(); break; case 'l': action_list_launch(); break; case 0x0B: /* control-k (up) */ case '[': action_list_move_item_up(); break; case 0x0A: /* control-j (down) */ case ']': action_list_move_item_down(); break; #ifdef DEBUG case '$': debug_i++; snprintf(msg, 80, "Name %d", debug_i); folder_add(current_pw_sublist, msg, "myhost", "myuser", "mypasswd", "mylaucnh"); uilist_refresh(); break; #endif default: break; } } return 0; }
int action_list_move_item() { Pw* curpw; PWList *curpwl, *iter; int i; char str[STRING_LONG]; char answer[STRING_MEDIUM]; switch(uilist_get_highlighted_type()){ case PW_ITEM: curpw = uilist_get_highlighted_item(); if(curpw){ while(1){ snprintf(str, STRING_LONG, "Move \"%s\" to where?", curpw->name); ui_statusline_ask_str(str, answer, STRING_MEDIUM); /* if user just enters nothing do nothing */ if(answer[0] == 0){ return 0; } for(iter = current_pw_sublist->sublists; iter != NULL; iter = iter->next){ if( strcmp(iter->name, answer) == 0 ){ pwlist_detach_pw(current_pw_sublist, curpw); pwlist_add_ptr(iter, curpw); uilist_refresh(); return 0; } } ui_statusline_msg("Sublist does not exist, try again"); getch(); } } break; case PW_SUBLIST: curpwl = uilist_get_highlighted_sublist(); if(curpwl){ while(1){ snprintf(str, STRING_LONG, "Move sublist \"%s\" to where?", curpwl->name); ui_statusline_ask_str(str, answer, STRING_MEDIUM); /* if user just enters nothing, do nothing */ if(answer[0] == 0){ return 0; } if( strcmp(answer, curpwl->name) == 0 ){ return 0; } for(iter = current_pw_sublist->sublists; iter != NULL; iter = iter->next){ if( strcmp(iter->name, answer) == 0 ){ pwlist_detach_sublist(current_pw_sublist, curpwl); pwlist_add_sublist(iter, curpwl); uilist_refresh(); return 0; } } ui_statusline_msg("Sublist does not exist, try again"); getch(); } } break; case PW_UPLEVEL: case PW_NULL: default: /* do nothing */ break; } }
int action_list_add_pw() { Pw *pw; InputField fields[] = { {"Name:\t", NULL, STRING_MEDIUM, STRING}, {"Host:\t", NULL, STRING_MEDIUM, STRING}, {"User:\t", NULL, STRING_MEDIUM, STRING}, {"Password:\t", NULL, STRING_SHORT, STRING, pwgen_ask}, {"Launch Command:\t", NULL, STRING_LONG, STRING} }; int i, x; pw = pwlist_new_pw(); ui_statusline_ask_str(fields[0].name, pw->name, STRING_MEDIUM); ui_statusline_ask_str(fields[1].name, pw->host, STRING_MEDIUM); ui_statusline_ask_str(fields[2].name, pw->user, STRING_MEDIUM); /* int x = strlen(msg) + 5; if(input == NULL){ input = malloc(len); } statusline_clear(); statusline_msg(msg); echo(); show_cursor(); mvwgetnstr(bottom, 1, x, input, len); noecho(); hide_cursor(); statusline_clear();*/ ui_statusline_ask_str_with_autogen(fields[3].name, pw->passwd, STRING_SHORT, fields[3].autogen, 0x07); /* statusline_msg(fields[3].name); x = strlen(fields[3].name) + 5; if((i = getch()) == 0x07){ pw->passwd = fields[3].autogen(); } else { echo(); show_cursor(); mvwgetnstr(bottom, 1, x, pw->passwd, PASS_LEN); mvwaddch(bottom, 1, x, i); wmove(bottom, 1, x+1); noecho(); hide_cursor(); statusline_clear(); }*/ ui_statusline_ask_str(fields[4].name, pw->launch, STRING_LONG); fields[0].value = pw->name; fields[1].value = pw->host; fields[2].value = pw->user; fields[3].value = pw->passwd; fields[4].value = pw->launch; i = action_yes_no_dialog(fields, (sizeof(fields)/sizeof(InputField)), NULL, "Add this entry"); if(i){ pwlist_add_ptr(current_pw_sublist, pw); ui_statusline_msg("New password added"); } else { pwlist_free_pw(pw); ui_statusline_msg("New password cancelled"); } uilist_refresh(); }
void statusline_readonly() { ui_statusline_msg("Password file is opened readonly"); }