home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume3
/
lib_term
/
GetInt.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-11-30
|
3KB
|
172 lines
#include <stdio.h>
#include <ctype.h>
int GetInt(Default, field_width)
int Default, field_width;
/*
---------------------------------------------------------------------------
Last revision -
16 November 1984 - GWS
Ignore XON, XOFF
11 April 1984 - GWS
NAME
GetInt - "crash-proof" integer from keyboard routine
SYNOPSIS
int GetInt(Default, field_width)
int Default, field_width;
DESCRIPTION
On a good day this routine will get an integer value from the
keyboard and return it safely. The terminal is placed in raw
mode and most non-digit values are beeped at and discarded. Entry
is terminated by filling the field or by CR. CR as first character
assumes Default. ^X restarts.
SEE ALSO
TermSetUp, TermRewind
DIAGNOSTICS
none - cannot fail :-)
BUGS
Doesn't check for silly things like Default too big to fit in
field_width, etc. Let's be particularly careful out there.
AUTHOR
George W. Sherouse
6 April 1984
---------------------------------------------------------------------------
*/
{
int c, val;
int loop;
char line_buff[20];
char Format[80];
char erase, pad;
int count;
int cookie;
void underline();
void clean_up();
int tgetnum();
char TermSetUp();
void TermRewind();
pad = ' ';
if ((cookie = tgetnum("ug")) < 0)
cookie = 0;
underline(1);
for (loop = 0; loop < field_width; loop++)
printf(" ");
if (cookie)
{
underline(0);
TermRewind(cookie);
}
TermRewind(field_width);
sprintf(Format, "%%%dd", field_width);
printf(Format, Default);
TermRewind(field_width);
for (loop = 0; loop <= field_width; loop++)
line_buff[loop] = 0;
erase = TermSetUp(); /* set no echo, single char input */
/* get erase character */
count = 0;
while (1)
{
switch (c = (getchar() & 0177))
{
case '\015':
if (count && line_buff[count - 1] != '-')
{
sscanf(line_buff, "%d", &val);
clean_up(count, field_width, Format, val);
return(val);
}
else
{
clean_up(count, field_width, Format, Default);
return(Default);
}
break;
case 030:
TermRewind(count);
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
count = 0;
printf(Format, Default);
TermRewind(field_width);
break;
case '\021':
case '\023':
break;
default:
if (c == erase && count)
{
printf("\b%c\b", pad);
line_buff[--count] = 0;
break;
}
if (!count && c == '-')
{
for (loop = 0; loop < field_width; loop++)
printf("%c", pad);
TermRewind(field_width);
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
line_buff[count++] = (char) c;
printf("-");
break;
}
if (isdigit(c) && count < field_width)
{
if (!count)
{
for (loop = 0; loop < field_width; loop++)
printf("%c", pad);
TermRewind(field_width);
for (loop = 0; loop < field_width; loop++)
line_buff[loop] = 0;
}
printf("%c", c);
line_buff[count++] = (char) c;
}
else
printf("%c", '\007');
}
}
}
void clean_up(count, field_width, Format, val)
int count, field_width;
char *Format;
int val;
{
int loop;
void underline();
char TermSetUp();
void TermRewind();
TermRewind(count);
printf(Format, val);
underline(0);
(void) TermSetUp();
}