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 * ----------------------------------------------------------------------- */
18 com32sys_t inreg
,outreg
; // Global register sets for use
20 char bkspstr
[] = " \b$";
21 char eolstr
[] = "\n$";
22 #define GETSTRATTR 0x07
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
38 char start
,end
; // Cursor shape
39 char fudge
; // How many chars should be removed from output
40 char insmode
; // Are we in insert or overwrite
43 getpos(&row
,&col
,page
); // Get current position
44 getcursorshape(&start
,&end
);
47 str
= (char *)malloc(size
+1); // Allocate memory to store user input
48 memset(str
,0,size
+1); // Zero it out
49 if (password
!= 0) showoldvalue
= 0; // Password's never displayed
51 if (showoldvalue
!= 0) strcpy(str
,stra
); // If show old value copy current value
54 while (*last
) {last
++;} // Find the terminating null byte
58 setcursorshape(1,7); // Block cursor
59 else setcursorshape(6,7); // Normal cursor
61 // Invariants: p is the current char
62 // col is the corresponding column on the screen
63 if (password
== 0) // Not a password, print initial value
66 csprint(str
,GETSTRATTR
);
68 while (1) { // Do forever
70 if (c
== '\r') break; // User hit Enter getout of loop
71 if (scan
== ESCAPE
) // User hit escape getout and nullify string
76 // if scan code is regognized do something
77 // else if char code is recognized do something
92 while ((p
> str
) && (*p
== ' ')) p
--;
96 while ((p
> str
) && (*p
== ' ')) p
--;
99 while ((p
> str
) && ((*p
== ' ') || (*(p
-1) != ' '))) p
--;
105 if (*p
==0) break; // At end of string
107 while ((*p
!=0) && (*p
!= ' ')) p
++;
108 while ((*p
!=0) && ((*p
== ' ') && (*(p
+1) != ' '))) p
++;
113 while (*(q
+1)) {*q
= *(q
+1); q
++; }
114 if (last
> str
) last
--;
118 insmode
= 1-insmode
; // Switch mode
120 setcursorshape(1,7); // Block cursor
121 else setcursorshape(6,7); // Normal cursor
124 default: // Unrecognized scan code, look at the ascii value
126 case '\b': // Move over by one
128 while ( q
<= last
) { *(q
-1)=*q
; q
++;}
129 if (last
> str
) last
--;
133 case '\x15': /* Ctrl-U: kill input */
135 while ( p
> str
) *p
--=0;
136 p
= str
; *p
=0; last
= str
;
138 default: // Handle insert and overwrite mode
139 if ((c
>= ' ') && (c
< 128) &&
140 ((unsigned int)(p
-str
) < size
-1) ) {
141 if (insmode
== 0) { // Overwrite mode
145 } else { // Insert mode
146 if (p
==last
) { // last char
150 } else { // Non-last char
152 while (q
>= p
) { *q
=*(q
-1); q
--;}
161 // Now the string has been modified, print it
163 gotoxy(row
,col
,page
);
164 csprint(str
,GETSTRATTR
);
165 if (fudge
> 0) cprint(' ',GETSTRATTR
,fudge
,page
);
166 gotoxy(row
,col
+(p
-str
),page
);
170 if (password
== 0) csprint("\r\n",GETSTRATTR
);
171 setcursorshape(start
,end
); // Block cursor
172 // If user hit ESCAPE so return without any changes
173 if (scan
!= ESCAPE
) strcpy(stra
,str
);
177 /* Print a C string (NUL-terminated) */
178 void cswprint(const char *str
,char attr
,char left
)
180 char page
= getdisppage();
181 char newattr
=0,cha
,chb
;
187 getpos(&row
,&col
,page
);
201 case BELL
: // Bell Char
204 case CHRELATTR
: // change attribute (relatively)
205 case CHABSATTR
: // change attribute (absolute)
208 if ((((cha
>= '0') && (cha
<= '9')) ||
209 ((cha
>= 'A') && (cha
<= 'F'))) &&
210 (((chb
>= '0') && (chb
<= '9')) ||
211 ((chb
>= 'A') && (chb
<= 'F')))) // Next two chars are legal
213 if ((cha
>= 'A') && (cha
<= 'F'))
215 else cha
= cha
- '0';
216 if ((chb
>= 'A') && (chb
<= 'F'))
218 else chb
= chb
- '0';
219 newattr
= (cha
<< 4) + chb
;
220 attr
= (*str
== CHABSATTR
? newattr
: attr
^ newattr
);
221 str
+= 2; // Will be incremented again later
225 putch(*str
, attr
, page
);
238 gotoxy(row
,col
,page
);
243 void clearwindow(char top
, char left
, char bot
, char right
, char page
, char fillchar
, char fillattr
)
246 for (x
=top
; x
< bot
+1; x
++)
249 cprint(fillchar
,fillattr
,right
-left
+1,page
);
255 unsigned char dp
= getdisppage();
257 cprint(' ',GETSTRATTR
,(1+getnumrows())*getnumcols(),dp
);
260 //////////////////////////////Box Stuff
262 // This order of numbers must match
263 // the values of BOX_TOPLEFT,... in the header file
265 unsigned char SINSIN_CHARS
[] = {218,192,191,217, //Corners
266 196,179, // Horiz and Vertical
267 195,180,194,193,197}; // Connectors & Middle
269 unsigned char DBLDBL_CHARS
[] = {201,200,187,188, // Corners
270 205,186, // Horiz and Vertical
271 199,182,203,202,206}; // Connectors & Middle
273 unsigned char SINDBL_CHARS
[] = {214,211,183,189, // Corners
274 196,186, // Horiz & Vert
275 199,182,210,208,215}; // Connectors & Middle
277 unsigned char DBLSIN_CHARS
[] = {213,212,184,190, // Corners
278 205,179, // Horiz & Vert
279 198,181,209,207,216}; // Connectors & Middle
281 unsigned char * getboxchars(boxtype bt
)
304 // Draw box and lines
305 void drawbox(char top
,char left
,char bot
, char right
,
306 char page
, char attr
,boxtype bt
)
308 unsigned char *box_chars
; // pointer to array of box chars
311 box_chars
= getboxchars(bt
);
313 gotoxy(top
,left
,page
);
314 cprint(box_chars
[BOX_TOPLEFT
],attr
,1,page
);
315 gotoxy(top
,left
+1,page
);
316 cprint(box_chars
[BOX_TOP
],attr
,right
-left
,page
);
317 gotoxy(top
,right
,page
);
318 cprint(box_chars
[BOX_TOPRIGHT
],attr
,1,page
);
320 gotoxy(bot
,left
,page
);
321 cprint(box_chars
[BOX_BOTLEFT
],attr
,1,page
);
322 gotoxy(bot
,left
+1,page
);
323 cprint(box_chars
[BOX_BOT
],attr
,right
-left
,page
);
324 gotoxy(bot
,right
,page
);
325 cprint(box_chars
[BOX_BOTRIGHT
],attr
,1,page
);
326 // Left & right borders
327 for (x
=top
+1; x
< bot
; x
++)
330 cprint(box_chars
[BOX_LEFT
],attr
,1,page
);
331 gotoxy(x
,right
,page
);
332 cprint(box_chars
[BOX_RIGHT
],attr
,1,page
);
336 void drawhorizline(char top
, char left
, char right
, char page
, char attr
,
337 boxtype bt
, char dumb
)
339 unsigned char start
,end
;
340 unsigned char *box_chars
= getboxchars(bt
);
348 gotoxy(top
,start
,page
);
349 cprint(box_chars
[BOX_HORIZ
],attr
,end
-start
+1,page
);
352 gotoxy(top
,left
,page
);
353 cprint(box_chars
[BOX_LTRT
],attr
,1,page
);
354 gotoxy(top
,right
,page
);
355 cprint(box_chars
[BOX_RTLT
],attr
,1,page
);