1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2006 Murali Krishnan Ganapathy - All Rights Reserved
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 * Boston MA 02111-1307, USA; either version 2 of the License, or
9 * (at your option) any later version; incorporated herein by reference.
11 * ----------------------------------------------------------------------- */
19 com32sys_t inreg
, outreg
; // Global register sets for use
21 char bkspstr
[] = " \b$";
22 char eolstr
[] = "\n$";
24 // Reads a line of input from stdin. Replace CR with NUL byte
25 // password <> 0 implies not echoed on screen
26 // showoldvalue <> 0 implies currentvalue displayed first
27 // If showoldvalue <> 0 then caller responsibility to ensure that
28 // str is NULL terminated.
29 void getuserinput(char *stra
, unsigned int size
, unsigned int password
,
30 unsigned int showoldvalue
)
33 char *p
, *q
; // p = current char of string, q = tmp
34 char *last
; // The current last char of string
35 char *str
; // pointer to string which is going to be allocated
37 char start
, end
; // Cursor shape
38 char fudge
; // How many chars should be removed from output
39 char insmode
; // Are we in insert or overwrite
41 getpos(&row
, &col
, 0); // Get current position
42 getcursorshape(&start
, &end
);
45 str
= (char *)malloc(size
+ 1); // Allocate memory to store user input
46 memset(str
, 0, size
+ 1); // Zero it out
48 showoldvalue
= 0; // Password's never displayed
50 if (showoldvalue
!= 0)
51 strcpy(str
, stra
); // If show old value copy current value
56 } // Find the terminating null byte
57 p
= str
+ strlen(str
);
60 setcursorshape(1, 7); // Block cursor
62 setcursorshape(6, 7); // Normal cursor
64 // Invariants: p is the current char
65 // col is the corresponding column on the screen
66 if (password
== 0) // Not a password, print initial value
69 csprint(str
, GETSTRATTR
);
71 while (1) { // Do forever
72 c
= get_key(stdin
, 0);
74 break; // User hit Enter getout of loop
75 if (c
== KEY_ESC
) // User hit escape getout and nullify string
81 // if scan code is regognized do something
82 // else if char code is recognized do something
95 case KEY_CTRL(KEY_LEFT
):
99 while ((p
> str
) && (*p
== ' '))
102 if (*(p
- 1) == ' ') {
104 while ((p
> str
) && (*p
== ' '))
108 while ((p
> str
) && ((*p
== ' ') || (*(p
- 1) != ' ')))
115 case KEY_CTRL(KEY_RIGHT
):
117 break; // At end of string
119 while ((*p
!= 0) && (*p
!= ' '))
121 while ((*p
!= 0) && ((*p
== ' ') && (*(p
+ 1) != ' ')))
138 insmode
= 1 - insmode
; // Switch mode
140 setcursorshape(1, 7); // Block cursor
142 setcursorshape(6, 7); // Normal cursor
144 case KEY_BACKSPACE
: // Move over by one
156 case KEY_CTRL('U'): /* Ctrl-U: kill input */
164 default: // Handle insert and overwrite mode
165 if ((c
>= ' ') && (c
< 128) &&
166 ((unsigned int)(p
- str
) < size
- 1)) {
167 if (insmode
== 0) { // Overwrite mode
172 } else { // Insert mode
173 if (p
== last
) { // last char
177 } else { // Non-last char
190 // Now the string has been modified, print it
193 csprint(str
, GETSTRATTR
);
195 cprint(' ', GETSTRATTR
, fudge
);
196 gotoxy(row
, col
+ (p
- str
));
201 csprint("\r\n", GETSTRATTR
);
202 setcursorshape(start
, end
); // Block cursor
203 // If user hit ESCAPE so return without any changes
209 //////////////////////////////Box Stuff
211 // Draw box and lines
212 void drawbox(const char top
, const char left
, const char bot
,
213 const char right
, const char attr
)
219 putch(TOP_LEFT_CORNER_BORDER
, attr
);
220 cprint(TOP_BORDER
, attr
, right
- left
- 1);
221 putch(TOP_RIGHT_CORNER_BORDER
, attr
);
224 putch(BOTTOM_LEFT_CORNER_BORDER
, attr
);
225 cprint(BOTTOM_BORDER
, attr
, right
- left
- 1);
226 putch(BOTTOM_RIGHT_CORNER_BORDER
, attr
);
227 // Left & right borders
228 for (x
= top
+ 1; x
< bot
; x
++) {
230 putch(LEFT_BORDER
, attr
);
232 putch(RIGHT_BORDER
, attr
);
237 void drawhorizline(const char top
, const char left
, const char right
,
238 const char attr
, char dumb
)
240 unsigned char start
, end
;
250 cprint(MIDDLE_BORDER
, attr
, end
- start
+ 1);
253 putch(MIDDLE_BORDER
, attr
);
255 putch(MIDDLE_BORDER
, attr
);