tpl.c (1888B)
1 /* form and string handling */ 2 #include <form.h> 3 #include <string.h> 4 5 /* for writing to file descriptors */ 6 #include <unistd.h> 7 8 /* user & password handling */ 9 #include <sys/types.h> 10 #include <pwd.h> 11 12 int main() 13 { 14 FIELD *field[3]; 15 FORM *login_form; 16 int row, col; 17 char user[32]; 18 char password[255]; 19 20 initscr(); /* initialize */ 21 cbreak(); /* fix tty from 'cooked' mode */ 22 getmaxyx(stdscr, row, col); /* getting the max rows & columns */ 23 noecho(); /* disable general echoing */ 24 keypad(stdscr, TRUE); /* setup keyboard */ 25 26 // TODO: display forms, fields & input on subwindow 27 //WINDOW *formwin = subwin() 28 29 int prow = row / 2; /* centered prompt row y */ 30 int pcol = col / 2; /* centered prompt col x */ 31 32 /* Initialize the fields */ 33 /* new_field(height, width, topleft row, topleft column, off-screen rows, additional buffers) */ 34 field[0] = new_field(1, 10, prow, pcol, 0, 0); 35 field[1] = new_field(1, 10, prow + 2, pcol, 0, 0); 36 field[2] = NULL; // *field[] is NULL terminated 37 38 /* Set field options */ 39 set_field_back(field[0], A_UNDERLINE); /* Print a line for the option */ 40 field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when filled */ 41 set_field_back(field[1], A_UNDERLINE); 42 field_opts_off(field[1], O_AUTOSKIP); 43 44 /* Create the form and post it */ 45 login_form = new_form(field); 46 post_form(login_form); 47 mvprintw(prow, pcol - 9, "Username:"); 48 mvprintw(prow + 2, pcol - 9, "Password:"); 49 box(stdscr, ACS_VLINE, ACS_HLINE); 50 refresh(); 51 echo(); 52 53 /* readiness notification for process supervision */ 54 write(3, "\n", 2); 55 close(3); 56 57 // TODO: error handling if password is wrong 58 mvgetnstr(prow, pcol, user, 32); 59 noecho(); 60 mvgetnstr(prow + 2, pcol, password, 255); 61 62 /* free our forms and fields */ 63 unpost_form(login_form); 64 free_form(login_form); 65 free_field(field[0]); 66 free_field(field[1]); 67 68 endwin(); /* safely close ncurses */ 69 70 return 0; 71 }