1 /* -*- c -*- ------------------------------------------------------------- *
3 * Copyright 2004-2005 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 #include "scancodes.h"
21 com32sys_t inreg,outreg; // Global register sets for use
23 char bkspstr[] = " \b$";
24 char eolstr[] = "\n$";
25 #define GETSTRATTR 0x07
27 // Reads a line of input from stdin. Replace CR with NUL byte
28 // password <> 0 implies not echoed on screen
29 // showoldvalue <> 0 implies currentvalue displayed first
30 // If showoldvalue <> 0 then caller responsibility to ensure that
31 // str is NULL terminated.
32 void getuserinput(char *stra, unsigned int size, unsigned int password,
33 unsigned int showoldvalue)
36 char *p,*q; // p = current char of string, q = tmp
37 char *last; // The current last char of string
38 char *str; // pointer to string which is going to be allocated
41 char start,end; // Cursor shape
42 char fudge; // How many chars should be removed from output
43 char insmode; // Are we in insert or overwrite
46 getpos(&row,&col,page); // Get current position
47 getcursorshape(&start,&end);
50 str = (char *)malloc(size+1); // Allocate memory to store user input
51 memset(str,0,size+1); // Zero it out
52 if (password != 0) showoldvalue = 0; // Password's never displayed
54 if (showoldvalue != 0) strcpy(str,stra); // If show old value copy current value
57 while (*last) {last++;} // Find the terminating null byte
61 setcursorshape(1,7); // Block cursor
62 else 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
73 if (c == '\r') break; // User hit Enter getout of loop
74 if (scan == ESCAPE) // User hit escape getout and nullify string
79 // if scan code is regognized do something
80 // else if char code is recognized do something
95 while ((p > str) && (*p == ' ')) p--;
99 while ((p > str) && (*p == ' ')) p--;
102 while ((p > str) && ((*p == ' ') || (*(p-1) != ' '))) p--;
108 if (*p==0) break; // At end of string
110 while ((*p!=0) && (*p != ' ')) p++;
111 while ((*p!=0) && ((*p == ' ') && (*(p+1) != ' '))) p++;
116 while (*(q+1)) {*q = *(q+1); q++; }
117 if (last > str) last--;
121 insmode = 1-insmode; // Switch mode
123 setcursorshape(1,7); // Block cursor
124 else setcursorshape(6,7); // Normal cursor
127 default: // Unrecognized scan code, look at the ascii value
129 case '\b': // Move over by one
131 while ( q <= last ) { *(q-1)=*q; q++;}
132 if (last > str) last--;
136 case '\x15': /* Ctrl-U: kill input */
138 while ( p > str ) *p--=0;
139 p = str; *p=0; last = str;
141 default: // Handle insert and overwrite mode
142 if ((c >= ' ') && (c < 128) &&
143 ((unsigned int)(p-str) < size-1) ) {
144 if (insmode == 0) { // Overwrite mode
148 } else { // Insert mode
149 if (p==last) { // last char
153 } else { // Non-last char
155 while (q >= p) { *q=*(q-1); q--;}
164 // Now the string has been modified, print it
166 gotoxy(row,col,page);
167 csprint(str,GETSTRATTR);
168 if (fudge > 0) cprint(' ',GETSTRATTR,fudge,page);
169 gotoxy(row,col+(p-str),page);
173 if (password == 0) csprint("\r\n",GETSTRATTR);
174 setcursorshape(start,end); // Block cursor
175 // If user hit ESCAPE so return without any changes
176 if (scan != ESCAPE) strcpy(stra,str);
180 /* Print a C string (NUL-terminated) */
181 void cswprint(const char *str,char attr,char left)
183 char page = getdisppage();
184 char newattr=0,cha,chb;
190 getpos(&row,&col,page);
204 case BELL: // Bell Char
207 case CHRELATTR: // change attribute (relatively)
208 case CHABSATTR: // change attribute (absolute)
211 if ((((cha >= '0') && (cha <= '9')) ||
212 ((cha >= 'A') && (cha <= 'F'))) &&
213 (((chb >= '0') && (chb <= '9')) ||
214 ((chb >= 'A') && (chb <= 'F')))) // Next two chars are legal
216 if ((cha >= 'A') && (cha <= 'F'))
218 else cha = cha - '0';
219 if ((chb >= 'A') && (chb <= 'F'))
221 else chb = chb - '0';
222 newattr = (cha << 4) + chb;
223 attr = (*str == CHABSATTR ? newattr : attr ^ newattr);
224 str += 2; // Will be incremented again later
228 putch(*str, attr, page);
241 gotoxy(row,col,page);
246 void clearwindow(char top, char left, char bot, char right, char page, char fillchar, char fillattr)
249 for (x=top; x < bot+1; x++)
252 cprint(fillchar,fillattr,right-left+1,page);
258 unsigned char dp = getdisppage();
260 cprint(' ',GETSTRATTR,(1+getnumrows())*getnumcols(),dp);
263 //////////////////////////////Box Stuff
265 // This order of numbers must match
266 // the values of BOX_TOPLEFT,... in the header file
268 unsigned char SINSIN_CHARS[] = {218,192,191,217, //Corners
269 196,179, // Horiz and Vertical
270 195,180,194,193,197}; // Connectors & Middle
272 unsigned char DBLDBL_CHARS[] = {201,200,187,188, // Corners
273 205,186, // Horiz and Vertical
274 199,182,203,202,206}; // Connectors & Middle
276 unsigned char SINDBL_CHARS[] = {214,211,183,189, // Corners
277 196,186, // Horiz & Vert
278 199,182,210,208,215}; // Connectors & Middle
280 unsigned char DBLSIN_CHARS[] = {213,212,184,190, // Corners
281 205,179, // Horiz & Vert
282 198,181,209,207,216}; // Connectors & Middle
284 unsigned char * getboxchars(boxtype bt)
307 // Draw box and lines
308 void drawbox(char top,char left,char bot, char right,
309 char page, char attr,boxtype bt)
311 unsigned char *box_chars; // pointer to array of box chars
314 box_chars = getboxchars(bt);
316 gotoxy(top,left,page);
317 cprint(box_chars[BOX_TOPLEFT],attr,1,page);
318 gotoxy(top,left+1,page);
319 cprint(box_chars[BOX_TOP],attr,right-left,page);
320 gotoxy(top,right,page);
321 cprint(box_chars[BOX_TOPRIGHT],attr,1,page);
323 gotoxy(bot,left,page);
324 cprint(box_chars[BOX_BOTLEFT],attr,1,page);
325 gotoxy(bot,left+1,page);
326 cprint(box_chars[BOX_BOT],attr,right-left,page);
327 gotoxy(bot,right,page);
328 cprint(box_chars[BOX_BOTRIGHT],attr,1,page);
329 // Left & right borders
330 for (x=top+1; x < bot; x++)
333 cprint(box_chars[BOX_LEFT],attr,1,page);
334 gotoxy(x,right,page);
335 cprint(box_chars[BOX_RIGHT],attr,1,page);
339 void drawhorizline(char top, char left, char right, char page, char attr,
340 boxtype bt, char dumb)
342 unsigned char start,end;
343 unsigned char *box_chars = getboxchars(bt);
351 gotoxy(top,start,page);
352 cprint(box_chars[BOX_HORIZ],attr,end-start+1,page);
355 gotoxy(top,left,page);
356 cprint(box_chars[BOX_LTRT],attr,1,page);
357 gotoxy(top,right,page);
358 cprint(box_chars[BOX_RTLT],attr,1,page);