1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* -*- c -*- ------------------------------------------------------------- *
*
* Copyright 2004-2006 Murali Krishnan Ganapathy - All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, Inc., 53 Temple Place Ste 330,
* Boston MA 02111-1307, USA; either version 2 of the License, or
* (at your option) any later version; incorporated herein by reference.
*
* ----------------------------------------------------------------------- */
#include "tui.h"
#include <string.h>
#include <com32.h>
#include <stdlib.h>
#include "com32io.h"
com32sys_t inreg, outreg; // Global register sets for use
char bkspstr[] = " \b$";
char eolstr[] = "\n$";
// Reads a line of input from stdin. Replace CR with NUL byte
// password <> 0 implies not echoed on screen
// showoldvalue <> 0 implies currentvalue displayed first
// If showoldvalue <> 0 then caller responsibility to ensure that
// str is NULL terminated.
void getuserinput(char *stra, unsigned int size, unsigned int password,
unsigned int showoldvalue)
{
unsigned int c;
char *p, *q; // p = current char of string, q = tmp
char *last; // The current last char of string
char *str; // pointer to string which is going to be allocated
char row, col;
char start, end; // Cursor shape
char fudge; // How many chars should be removed from output
char insmode; // Are we in insert or overwrite
getpos(&row, &col, 0); // Get current position
getcursorshape(&start, &end);
insmode = 1;
str = (char *)malloc(size + 1); // Allocate memory to store user input
memset(str, 0, size + 1); // Zero it out
if (password != 0)
showoldvalue = 0; // Password's never displayed
if (showoldvalue != 0)
strcpy(str, stra); // If show old value copy current value
last = str;
while (*last) {
last++;
} // Find the terminating null byte
p = str + strlen(str);
if (insmode == 0)
setcursorshape(1, 7); // Block cursor
else
setcursorshape(6, 7); // Normal cursor
// Invariants: p is the current char
// col is the corresponding column on the screen
if (password == 0) // Not a password, print initial value
{
gotoxy(row, col);
csprint(str, GETSTRATTR);
}
while (1) { // Do forever
c = get_key(stdin, 0);
if (c == KEY_ENTER)
break; // User hit Enter getout of loop
if (c == KEY_ESC) // User hit escape getout and nullify string
{
*str = 0;
break;
}
fudge = 0;
// if scan code is regognized do something
// else if char code is recognized do something
// else ignore
switch (c) {
case KEY_HOME:
p = str;
break;
case KEY_END:
p = last;
break;
case KEY_LEFT:
if (p > str)
p--;
break;
case KEY_CTRL(KEY_LEFT):
if (p == str)
break;
if (*p == ' ')
while ((p > str) && (*p == ' '))
p--;
else {
if (*(p - 1) == ' ') {
p--;
while ((p > str) && (*p == ' '))
p--;
}
}
while ((p > str) && ((*p == ' ') || (*(p - 1) != ' ')))
p--;
break;
case KEY_RIGHT:
if (p < last)
p++;
break;
case KEY_CTRL(KEY_RIGHT):
if (*p == 0)
break; // At end of string
if (*p != ' ')
while ((*p != 0) && (*p != ' '))
p++;
while ((*p != 0) && ((*p == ' ') && (*(p + 1) != ' ')))
p++;
if (*p == ' ')
p++;
break;
case KEY_DEL:
case KEY_DELETE:
q = p;
while (*(q + 1)) {
*q = *(q + 1);
q++;
}
if (last > str)
last--;
fudge = 1;
break;
case KEY_INSERT:
insmode = 1 - insmode; // Switch mode
if (insmode == 0)
setcursorshape(1, 7); // Block cursor
else
setcursorshape(6, 7); // Normal cursor
break;
case KEY_BACKSPACE: // Move over by one
q = p;
while (q <= last) {
*(q - 1) = *q;
q++;
}
if (last > str)
last--;
if (p > str)
p--;
fudge = 1;
break;
case KEY_CTRL('U'): /* Ctrl-U: kill input */
fudge = last - str;
while (p > str)
*p-- = 0;
p = str;
*p = 0;
last = str;
break;
default: // Handle insert and overwrite mode
if ((c >= ' ') && (c < 128) &&
((unsigned int)(p - str) < size - 1)) {
if (insmode == 0) { // Overwrite mode
if (p == last)
last++;
*last = 0;
*p++ = c;
} else { // Insert mode
if (p == last) { // last char
last++;
*last = 0;
*p++ = c;
} else { // Non-last char
q = last++;
while (q >= p) {
*q = *(q - 1);
q--;
}
*p++ = c;
}
}
} else
beep();
break;
}
// Now the string has been modified, print it
if (password == 0) {
gotoxy(row, col);
csprint(str, GETSTRATTR);
if (fudge > 0)
cprint(' ', GETSTRATTR, fudge);
gotoxy(row, col + (p - str));
}
} /* while */
*p = '\0';
if (password == 0)
csprint("\r\n", GETSTRATTR);
setcursorshape(start, end); // Block cursor
// If user hit ESCAPE so return without any changes
if (c != KEY_ESC)
strcpy(stra, str);
free(str);
}
//////////////////////////////Box Stuff
// Draw box and lines
void drawbox(const char top, const char left, const char bot,
const char right, const char attr)
{
unsigned char x;
putchar(SO);
// Top border
gotoxy(top, left);
putch(TOP_LEFT_CORNER_BORDER, attr);
cprint(TOP_BORDER, attr, right - left - 1);
putch(TOP_RIGHT_CORNER_BORDER, attr);
// Bottom border
gotoxy(bot, left);
putch(BOTTOM_LEFT_CORNER_BORDER, attr);
cprint(BOTTOM_BORDER, attr, right - left - 1);
putch(BOTTOM_RIGHT_CORNER_BORDER, attr);
// Left & right borders
for (x = top + 1; x < bot; x++) {
gotoxy(x, left);
putch(LEFT_BORDER, attr);
gotoxy(x, right);
putch(RIGHT_BORDER, attr);
}
putchar(SI);
}
void drawhorizline(const char top, const char left, const char right,
const char attr, char dumb)
{
unsigned char start, end;
if (dumb == 0) {
start = left + 1;
end = right - 1;
} else {
start = left;
end = right;
}
gotoxy(top, start);
putchar(SO);
cprint(MIDDLE_BORDER, attr, end - start + 1);
if (dumb == 0) {
gotoxy(top, left);
putch(MIDDLE_BORDER, attr);
gotoxy(top, right);
putch(MIDDLE_BORDER, attr);
}
putchar(SI);
}
|