char rc_open_cmd[SIZ];
char rc_gotmail_cmd[SIZ];
-char *gl_string;
int next_lazy_cmd = 5;
extern int screenwidth, screenheight;
* Function to read a line of text from the terminal.
*
* string Pointer to string buffer
- * lim Maximum length - if negative, echo asterisks instead of characters
+ * lim Maximum length
+ * noshow Echo asterisks instead of keystrokes?
+ * bs Allow backspacing out of the prompt? (returns -1 if this happens)
+ *
+ * returns: string length
*/
-void ctdl_getline(char *string, int lim)
+int ctdl_getline(char *string, int lim, int noshow, int bs)
{
- int a, b;
- char noshow = 0;
+ int pos = strlen(string);
+ int ch;
- if (lim < 0) {
- lim = (0 - lim);
- noshow = 1;
- }
- strcpy(string, "");
- gl_string = string;
async_ka_start();
-
-GLA: a = inkey();
- if ((a == 8 || a == 23) && (IsEmptyStr(string)))
- goto GLA;
- if ((a != 10) && (a != 8) && (strlen(string) == lim))
- goto GLA;
- if ((a == 8) && (string[0] != 0)) {
- string[strlen(string) - 1] = 0;
- scr_putc(8); scr_putc(32); scr_putc(8);
- goto GLA;
- }
- if ((a == 23) && (string[0] != 0)) {
- do {
- string[strlen(string) - 1] = 0;
- scr_putc(8); scr_putc(32); scr_putc(8);
- } while (!IsEmptyStr(string) && string[strlen(string) - 1] != ' ');
- goto GLA;
- }
- if ((a == 10)) {
- scr_putc(10);
- async_ka_end();
- return;
- }
- if (a < 32) {
- a = '.';
- }
- b = strlen(string);
- string[b] = a;
- string[b + 1] = 0;
- if (noshow) {
- scr_putc('*');
+ if (noshow && !IsEmptyStr(string)) {
+ int num_stars = strlen(string);
+ while (num_stars--) {
+ scr_putc('*');
+ }
}
else {
- scr_putc(a);
+ scr_printf("%s", string);
+ }
+
+ while(1) {
+ ch = inkey();
+
+ if ((ch == 8) && (pos > 0)) { /* backspace */
+ --pos;
+ scr_putc(8); scr_putc(32); scr_putc(8);
+ }
+
+ else if ((ch == 8) && (pos == 0) && (bs)) { /* backspace out of the prompt */
+ async_ka_end();
+ return(-1);
+ }
+
+ else if ((ch == 23) && (pos > 0)) { /* Ctrl-W deletes a word */
+ while ((pos > 0) && !isspace(string[pos])) {
+ --pos;
+ scr_putc(8); scr_putc(32); scr_putc(8);
+ }
+ while ((pos > 0) && !isspace(string[pos-1])) {
+ --pos;
+ scr_putc(8); scr_putc(32); scr_putc(8);
+ }
+ }
+
+ else if (ch == 10) { /* return */
+ string[pos] = 0;
+ scr_printf("\n");
+ async_ka_end();
+ return(pos);
+ }
+
+ else if (isprint(ch)) { /* payload characters */
+ scr_putc((noshow ? '*' : ch));
+ string[pos] = ch;
+ ++pos;
+ }
}
- goto GLA;
}
-/*
- * strprompt() - prompt for a string, print the existing value and
- * allow the user to press return to keep it...
+/*
+ * newprompt() prompt for a string, print the existing value, and
+ * allow the user to press return to keep it...
+ * If len is negative, pass the "noshow" flag to ctdl_getline()
*/
void strprompt(char *prompt, char *str, int len)
{
- int i;
- char buf[128];
-
print_instant();
color(DIM_WHITE);
- scr_printf("%s ", prompt);
- color(DIM_MAGENTA);
- scr_printf("[");
- color(BRIGHT_MAGENTA);
-
- if (len >= 0) {
- scr_printf("%s", str);
- }
- else {
- for (i=0; !IsEmptyStr(&str[i]); ++i) {
- scr_printf("*");
- }
- }
-
- color(DIM_MAGENTA);
- scr_printf("]");
+ scr_printf("%s", prompt);
color(DIM_WHITE);
scr_printf(": ");
color(BRIGHT_CYAN);
- ctdl_getline(buf, len);
- if (buf[0] != 0)
- strcpy(str, buf);
+ ctdl_getline(str, abs(len), (len<0), 0);
color(DIM_WHITE);
}
}
/*
- * newprompt() - prompt for a string with no existing value
- * (clears out string buffer first)
+ * newprompt() prompt for a string with no existing value
+ * (clears out string buffer first)
+ * If len is negative, pass the "noshow" flag to ctdl_getline()
*/
void newprompt(char *prompt, char *str, int len)
{
+ str[0] = 0;
color(BRIGHT_MAGENTA);
scr_printf("%s", prompt);
color(DIM_MAGENTA);
- ctdl_getline(str, len);
+ ctdl_getline(str, abs(len), (len<0), 0);
color(DIM_WHITE);
}
if (cmdmatch(cmdbuf, cptr, 5)) {
/* We've found our command. */
if (requires_string(cptr, cmdpos)) {
- ctdl_getline(argbuf, 64);
+ argbuf[0] = 0;
+ ctdl_getline(argbuf, 64, 0, 0);
} else {
scr_printf("\n");
}