DISPLIB Reposting part 5 of 6
Gary K. Sloane
sloane at noscvax.UUCP
Sat Aug 9 10:23:49 AEST 1986
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
# part3
# This archive created: Fri Aug 8 17:14:03 1986
# By: Gary K. Sloane (Computer Sciences Corporation, San Diego)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'part3'
then
echo shar: "will not over-write existing file 'part3'"
else
cat << \SHAR_EOF > 'part3'
/**************************/
/* Enter or Edit a String */
/**************************/
/*VARARGS4*/
edits(string, len, flag, pgm, a1, a2, a3, a4, a5)
char *pgm, *string;
int flag, len;
{
register int cnt;
int initline, initcol, dline, dcol, i;
int mode = FREAD;
char *sptr, c;
/* save cursor positions and string for ctrl-u */
initline = termctrl.curline;
initcol = termctrl.curcol;
/* highlight the ':' if requested */
if(flag == COLON)
{
gotoxy(initcol-2, initline);
inverse();
printf(":");
normal();
}
/* highlight the entire field if requested */
if(flag == INVERSE)
{
inverse();
printf("%s", string);
for(i=strlen(string); i<len; i++)
putchar(' ');
}
/* set up for enter or edit */
cnt = strlen(string);
sptr = string + cnt;
gotoxy(initcol+cnt,initline);
/* flush the type ahead buffer */
(void)ioctl(0, (int)TIOCFLUSH, (char *)&mode);
/* go into raw mode */
ttyset("raw");
/* accept characters and process them */
for(;;)
{
switch(c = getc(stdin))
{
/* DEL entered - abort or continue */
case '\177': dline = termctrl.curline;
dcol = termctrl.curcol;
bell();
gotoxy(25,21);
inverse();
printf(" Restart(y/n)? ");
gotoxy(40,21);
c = getc(stdin);
putchar(c);
normal();
if(c != 'y')
{
gotoxy(25,21);
printf("------------------");
gotoxy(dcol, dline);
if(flag == INVERSE)
inverse();
break;
}
/* go into cbreak mode */
ttyset("cbreak");
/* start the program over */
if(*pgm != '\0')
reload(pgm, a1, a2, a3, a4, a5);
termreset();
exit(0);
/* ctrl-u entered */
case '\025': gotoxy(initcol,initline);
for(i=0; i<strlen(string); i++)
printf(" ");
*string = '\0';
cnt = 0;
sptr = string;
gotoxy(termctrl.curcol,termctrl.curline);
break;
/* carriage return entered - return the string */
case '\015': *sptr = '\0';
/* un-highlight the ':' if it was requested */
if(flag == COLON)
{
gotoxy(termctrl.curcol-cnt-2, termctrl.curline);
normal();
printf(":");
}
/* un-highlight the field if it was requested */
if(flag == INVERSE)
{
gotoxy(initcol, initline);
normal();
printf("%s", string);
for(i=strlen(string); i<len; i++)
putchar(' ');
}
/* set cbreak mode & return to calling routine */
ttyset("cbreak");
return;
/* backspace entered - backspace over previous character */
/* if no previous character, ignore the backspace */
case '\010': if(sptr != string)
{
printf("\010 \010");
--termctrl.curcol;
--sptr;
--cnt;
}
else
bell();
break;
/* check character for printability - if ok, echo it to screen */
/* and spool it onto string - do not allow entry past length */
/* and do not allow colons as they are used for field delimiters */
default: if((isprint(c) == 0) || (cnt >= len) || (c == ':'))
bell();
else
{
*sptr++ = c;
putchar(c);
++termctrl.curcol;
++cnt;
}
break;
}
}
}
/*******************************************/
/* Enter or Edit a Date in mm/dd/yy Format */
/*******************************************/
/*VARARGS7*/
editdate(edate, flag, empty, oldate, date, pgm, a1, a2, a3, a4, a5)
char *edate, *date, *pgm;
int flag, empty, oldate;
{
int cnt, cd, chcnt=0, scnt=0, erflg=0;
char *sptr, c;
int initline, initcol, dline, dcol, i, j;
int mode = FREAD;
/* save cursor positions and string for ctrl-u */
initline = termctrl.curline;
initcol = termctrl.curcol;
/* highlight the ':' if requested */
if(flag == COLON)
{
gotoxy(initcol-2, initline);
inverse();
printf(":");
normal();
}
/* highlight the entire field if requested */
if(flag == INVERSE)
{
inverse();
printf("%s", edate);
for(j=strlen(edate); j<8; j++)
putchar(' ');
}
/* set up for enter or edit */
cnt = strlen(edate);
for(sptr = edate; *sptr != '\0'; sptr++)
if(*sptr == '/')
scnt++;
cnt = strlen(edate);
sptr = edate + cnt;
chcnt = cntdate(edate, sptr);
gotoxy(initcol+cnt,initline);
/* flush the type ahead buffer */
if(ioctl(0, (int)TIOCFLUSH, (char *)&mode) != 0)
{
printf("*** ERROR: Bad call to ioctl in editdate()... ***");
exit(0);
}
/* go into raw mode */
ttyset("raw");
/* accept characters and process them */
for(;;)
{
c=getc(stdin);
if(erflg)
{
erflg = 0;
clmsg();
}
switch(c)
{
/* Delete entered - abort the program? */
case '\177':
dline = termctrl.curline;
dcol = termctrl.curcol;
bell();
gotoxy(25,21);
inverse();
printf(" Restart(y/n)? ");
gotoxy(40,21);
c = getc(stdin);
putchar(c);
normal();
if(c != 'y')
{
gotoxy(25,21);
printf("------------------");
gotoxy(dcol, dline);
if(flag == INVERSE)
inverse();
break;
}
/* go into cbreak mode and restart the program */
ttyset("cbreak");
if(*pgm != '\0')
reload(pgm, a1, a2, a3, a4, a5);
termreset();
exit(0);
/* ctrl-u entered */
case '\025': gotoxy(initcol,initline);
for(i=0; i<strlen(edate); i++)
printf(" ");
*edate = '\0';
cnt = 0;
chcnt = 0;
scnt = 0;
sptr = edate;
gotoxy(termctrl.curcol,termctrl.curline);
break;
/* carriage return entered - return the string unless last character */
/* is a '/' or unless an invalid date is entered. Month must be 1-12, */
/* day must be less than or equal to max for month entered, date must */
/* be after current date. Two slashes must have been entered */
case '\015':
if((empty == 1) && (cnt == 0))
{
/* un-highlight the ':' if it was requested */
if(flag == COLON)
{
gotoxy(termctrl.curcol-cnt-2, termctrl.curline);
normal();
printf(":");
}
/* un-highlight the field if it was requested */
if(flag == INVERSE)
{
gotoxy(initcol, initline);
normal();
printf("%s", edate);
for(j=strlen(edate); j<8; j++)
putchar(' ');
}
/* go into cbreak mode & return to calling routine */
*sptr = '\0';
ttyset("cbreak");
return;
}
/* last character in date can't be a '/' */
if( *(sptr-1) == '/')
{
msg("Format to enter a date is MM/DD/YY...",BEEP,0);
erflg++;
break;
}
/* 2 slashes must have been entered */
if (scnt < 2)
{
msg("Format to enter a date is MM/DD/YY...",BEEP,0);
erflg++;
break;
}
/* put the NULL at the end of the string */
*sptr = '\0';
/* is date valid? */
cd = chkdate(edate, date, oldate);
/* date is valid */
if (cd == 1)
{
/* un-highlight the ':' if it was requested */
if(flag == COLON)
{
gotoxy(termctrl.curcol-cnt-2, termctrl.curline);
normal();
printf(":");
}
/* un-highlight the field if it was requested */
if(flag == INVERSE)
{
gotoxy(initcol, initline);
normal();
printf("%s", edate);
for(j=strlen(edate); j<8; j++)
putchar(' ');
}
/* go into cbreak mode with no echo & return to calling routine */
ttyset("cbreak");
return;
}
/* month is not 1 thru 12 */
else if (cd == 2)
{
msg("Month must be 1 thru 12 - try again...",BEEP,2);
break;
}
/* day not in month entered */
else if (cd == 3)
{
msg("Day entered does not exist in month entered - try again...",BEEP,2);
break;
}
/* date not after today's date */
else
{
msg("Date must be after today's date - try again...",BEEP,2);
break;
}
/* backspace entered - backspace over previous character */
/* if no previous character, ignore the backspace */
case '\010':
/* only backspace if there is something to backspace over */
if(sptr != edate)
{
/* do the backspace */
printf("\010 \010");
--termctrl.curcol;
--sptr;
--cnt;
--chcnt;
if(erflg)
{
erflg = 0;
clmsg();
}
/* if a slash is being backspaced over, adjust the counts */
if( *sptr == '/')
{
/* reduce field count (# of '/' in date) */
--scnt;
chcnt = cntdate(edate, sptr);
}
}
/* No backspace is allowed, so... */
else
bell();
break;
/* make sure character is a digit - if ok, echo it to screen */
/* and spool it onto field - do not allow entry past length */
/* and do not allow blanks to be entered */
default:
if( (c != '/') && (isdigit(c) == 0) )
{
msg("Format to enter a date is MM/DD/YY...", BEEP,0);
erflg++;
break;
}
/* is it a slash? */
if(c == '/')
{
/* first char can't be a '/' */
if(sptr == edate)
{
msg("Format to enter a date is MM/DD/YY...", BEEP,0);
erflg++;
break;
}
/* can't have 2 slashes next to each other */
if( *(sptr-1) == '/')
{
msg("Format to enter a date is MM/DD/YY...", BEEP,0);
erflg++;
break;
}
/* can't have more than 2 slashes in date */
if( (scnt+1) > 2)
{
msg("Format to enter a date is MM/DD/YY...", BEEP,0);
erflg++;
break;
}
else
++scnt;
chcnt = -1;
}
/* If it's not a '/', is the month, day, or year too long? */
else
{
if(chcnt > 1)
{
msg("Format to enter a date is MM/DD/YY...", BEEP,0);
erflg++;
break;
}
}
/* everything's ok, put it on the date and bump the ctrs */
++chcnt;
*sptr++ = c;
putchar(c);
++termctrl.curcol;
++cnt;
break;
}
}
}
SHAR_EOF
fi
exit 0
# End of shell archive
More information about the Comp.sources.unix
mailing list