2
0

Fall back to getline if ncurses cannot be initialized (eg. TERM not set).

This commit is contained in:
Maarten Billemont 2018-09-22 11:00:08 -04:00
parent 06ebe954f1
commit e4837a284a

View File

@ -104,6 +104,7 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
#if MPW_COLOR #if MPW_COLOR
// Initialize a curses screen. // Initialize a curses screen.
SCREEN *screen = newterm( NULL, stderr, stdin ); SCREEN *screen = newterm( NULL, stderr, stdin );
if (screen) {
start_color(); start_color();
init_pair( 1, COLOR_WHITE, COLOR_BLUE ); init_pair( 1, COLOR_WHITE, COLOR_BLUE );
init_pair( 2, COLOR_BLACK, COLOR_WHITE ); init_pair( 2, COLOR_BLACK, COLOR_WHITE );
@ -135,7 +136,8 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
noecho(); noecho();
result = mvgetnstr( rows / 2 + 1, (cols - 1) / 2, str, MPW_MAX_INPUT ); result = mvgetnstr( rows / 2 + 1, (cols - 1) / 2, str, MPW_MAX_INPUT );
echo(); echo();
} else { }
else {
mvprintw( rows / 2 + 1, (cols - (MPW_MAX_INPUT + 2)) / 2, "%*s", MPW_MAX_INPUT + 2, "" ); mvprintw( rows / 2 + 1, (cols - (MPW_MAX_INPUT + 2)) / 2, "%*s", MPW_MAX_INPUT + 2, "" );
refresh(); refresh();
@ -147,21 +149,22 @@ static const char *_mpw_getline(const char *prompt, bool silent) {
delscreen( screen ); delscreen( screen );
return result == ERR? NULL: mpw_strndup( str, MPW_MAX_INPUT ); return result == ERR? NULL: mpw_strndup( str, MPW_MAX_INPUT );
#else }
#endif
// Get password from terminal. // Get password from terminal.
fprintf( stderr, "%s ", prompt ); fprintf( stderr, "%s ", prompt );
size_t bufSize = 0; size_t bufSize = 0;
ssize_t lineSize = getline( &answer, &bufSize, stdin ); ssize_t lineSize = getline( (char **)&answer, &bufSize, stdin );
if (lineSize <= 1) { if (lineSize <= 1) {
mpw_free_string( &answer ); mpw_free_string( &answer );
return NULL; return NULL;
} }
// Remove trailing newline. // Remove trailing newline.
answer[lineSize - 1] = '\0'; mpw_replace_string( answer, mpw_strndup( answer, (size_t)lineSize - 1 ) );
return answer; return answer;
#endif
} }
const char *mpw_getline(const char *prompt) { const char *mpw_getline(const char *prompt) {