home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
GLEN
/
ASK-YN3.ZIP
/
ASK-YN3.C
next >
Wrap
Text File
|
1988-01-01
|
5KB
|
223 lines
/*
Small Lattice-C pgm to echo command line args, prompt for single-char
response, & set ERRORLEVEL accordingly. -n on the command line switches
default response to [N] from [Y]. ERRORLEVEL set to 0 for [Y], 1 for [N].
Skip Gilbrech - CompuServe 71445,534
Added -w switch to use default after s seconds. good for batch file that
might be unattended.
James Rich 12/17/87
Added countdown effect and -v switch to display it. Usage will now ALWAYS
reflect current file name.
James Rich 1/1/88
*/
#include <ctype.h>
#define NO 1
#define YES 0
#define MAXLINE 132
#define SECMSGV1 "(Y/N) [" /* (verbose) goes before number of seconds */
#define SECMSGV2 " seconds before " /* verbose after number of seconds */
#define SECMSGT1 "(Y/N) " /* (terse) goes before number of seconds */
#define SECMSGT2 " [" /* terse after number of seconds */
#define NOSECMSG "(Y/N) [" /* goes instead of number of seconds */
void puts(char *);
void putchar(char);
char sleepkb(int);
main(argc,argv)
int argc;
char *argv[];
{
int i, retcode;
int wait_secs = 0;
char askstr[MAXLINE],
secs_str[8],
drive[3],
path[64],
node[9],
ext[4],
dflt = YES,
wait = NO,
visual = NO,
terse = NO,
*s,
c,
ctr;
if (argc < 2)
{
puts("Asks a yes/no question from within a batch file. Used with the IF ERRORLEVEL\n");
puts(" batch file command.\n");
puts("Usage: "); /* DOS 3.0 and up supply the actual file name */
if (strlen(argv[0]) > 1) /* earlier DOSs use "C" for argv[0] */
strsfn(argv[0], drive, path, node, ext);
puts(strlen(argv[0]) > 1 ? node : "ASK-YN");
puts(" [-n] [-ws] [-t | -v] <prompt string>\n");
puts(" -n switches default response to [N]\n");
puts(" -w waits s second(s) before using the default response\n");
puts(" -t displays a terse countdown of seconds (used with -w)\n");
puts(" -v displays a verbose \" \" \" \" \" \"\n");
puts(" (-t and -v are mutually exclusive)\n");
puts(" ERRORLEVEL is set to 0 for [Y], 1 for [N]\n");
_exit(NO);
}
askstr[0] = '\0';
for (ctr = 1; ctr < argc; ctr++)
{
s = argv[ctr];
if (*s == '-' || *s == '/')
{
++s;
if (toupper(*s) == 'N')
dflt = NO;
else if (toupper(*s) == 'W')
{
++s;
wait_secs = atoi(s);
wait = YES;
}
else if (toupper(*s) == 'V')
visual = YES;
else if (toupper(*s) == 'T')
{
visual = YES;
terse = YES;
}
}
else
{
strcat(askstr, s);
strcat(askstr, " ");
}
}
retcode = dflt;
if (wait == YES && visual == YES)
{
puts(askstr);
stci_d(secs_str, wait_secs);
if (terse == YES)
puts(SECMSGT1);
else
puts(SECMSGV1);
puts(secs_str);
if (terse == YES)
puts(SECMSGT2);
else
puts(SECMSGV2);
puts(dflt == YES ? "Y]\b\b" : "N]\b\b");
}
else
{
puts(askstr);
puts(NOSECMSG);
puts(dflt == YES ? "Y]\b\b" : "N]\b\b");
}
if (wait == YES && visual == YES)
{
while(wait_secs--)
{
if ((c = sleepkb(1)) != NO)
break;
puts(" ");
for (i = 0; i < (strlen(terse == YES ? SECMSGT2 : SECMSGV2) + num_digits(wait_secs + 1) + 2); i++)
putchar('\b');
stci_d(secs_str, wait_secs);
puts(secs_str);
if (terse == YES)
puts(SECMSGT2);
else
puts(SECMSGV2);
puts(dflt == YES ? "Y]\b\b" : "N]\b\b");
}
}
else if (wait == YES && visual == NO)
c = sleepkb(wait_secs);
else
c = getch();
c = toupper(c);
if (c == NO) /* a little sign that the default was used automatically */
{
if (terse == YES || visual == NO)
puts(dflt == YES ? "Yes]" : "No]");
else /* a bigger sign */
{
puts("Default \"");
puts(dflt == YES ? "YES" : "NO");
puts("\" used]");
}
}
if (dflt == YES)
{
if (c == 'N')
{
putchar('N');
retcode = NO;
}
}
else if (c == 'Y')
{
putchar('Y');
retcode = YES;
}
putchar('\n');
_exit(retcode);
}
void puts(s)
char *s;
{
while(*s)
putchar(*s++);
}
void putchar(c)
char c;
{
if (c == '\n')
putchar('\r');
bdos(2, c);
}
char sleepkb(n) /* waits approx n secs or for key press */
int n;
{
long t1, t2, s1;
s1 = (long) n;
for (time(&t1), time(&t2); (t1 + s1) > t2; time(&t2))
if (kbhit())
{
return((char) getch());
}
return((char) NO);
}
num_digits(n)
int n;
{
int digits = 0;
while (n)
{
n /= 10;
digits++;
}
return (digits);
}
/* EOF */