Article View: microsoft.public.vc.language
Article #13Re: choiceyn: cut down choice command, use of sleep, kbhit and getch
From: Hal Styli
Date: Thu, 15 Jan 2004 12:21
Date: Thu, 15 Jan 2004 12:21
140 lines
6028 bytes
6028 bytes
I use WSH for scripting (as well as c++ ;o). I started the choice program to
ease porting win98 bat files that have been thoroughly tested.
Here is the final ( I think ) choiceyn.c, I think it looks pretty good,
could be a cut down ASK.EXE from Norton too.
Its got line numbers so no need for long quotes...
All comments welcome
Hal
______________________________________
/*: 1:*/ /* choiceyn.c - heavily cut down clone of
/*: 2:*/ ** CHOICE.COM command found on Windows 98.
/*: 3:*/ **
/*: 4:*/ ** The only parameter allowed is -t which works like
/*: 5:*/ ** 'CHOICE /t:y,10' otherwise works like
/*: 6:*/ ** 'CHOICE.COM' (i.e. with no arguments)
/*: 7:*/ **
/*: 8:*/ ** No warranty whatsoever given or implied.
/*: 9:*/ */
/*: 10:*/ #define VER "1.2a, Hal Styli, 2004"
/*: 11:*/
/*: 12:*/ #if !defined( _MSC_VER ) && !defined( __BORLANDC__ )
/*: 13:*/ #error Untested on this platform / with this compiler.
/*: 14:*/ #endif
/*: 15:*/
/*: 16:*/ #include <stdio.h>
/*: 17:*/ #include <conio.h> /* for getch() & kbhit() */
/*: 18:*/ #include <windows.h> /* for Sleep() */
/*: 19:*/
/*: 20:*/ #define WAITTIME 200
/*: 21:*/ #define TIMEOUT (10*(1000/WAITTIME))
/*: 22:*/ #define PATHDELIM '\\'
/*: 23:*/ #define PR(s) fputs(s,stdout)
/*: 24:*/ #define PRE(s) fputs(s,stderr)
/*: 25:*/ /*---------------------------------------------------*/
/*: 26:*/ char *fname(char *p)
/*: 27:*/ /*similar to fnsplit(p, NULL, NULL, name, NULL)
/*: 28:*/ and _splitpath(p, NULL, NULL, name, NULL) */
/*: 29:*/ {
/*: 30:*/ char *q;
/*: 31:*/
/*: 32:*/ while(q=strchr(p,PATHDELIM))
/*: 33:*/ p=q+1;
/*: 34:*/
/*: 35:*/ return p;
/*: 36:*/ }
/*: 37:*/ /*---------------------------------------------------*/
/*: 38:*/ int getargs(int argc, char **argv)
/*: 39:*/ {
/*: 40:*/ int t=0;
/*: 41:*/
/*: 42:*/ if(argc==2)
/*: 43:*/ {
/*: 44:*/ if( (argv[1][0]=='-') && (argv[1][1]=='t'))
/*: 45:*/ t= TIMEOUT;
/*: 46:*/ else
/*: 47:*/ t= -1;
/*: 48:*/ }
/*: 49:*/ else if(argc!=1)
/*: 50:*/ t= -1;
/*: 51:*/
/*: 52:*/ if(t==-1)
/*: 53:*/ {
/*: 54:*/ PRE("-----------------------------------------\n");
/*: 55:*/ PRE("\n Error! Format is: '");
/*: 56:*/ PRE(fname(*argv));
/*: 57:*/ PRE(" [-t]'. \n\n [version: " VER "].\n\n");
/*: 58:*/ PRE("-----------------------------------------\n");
/*: 59:*/ exit(255);
/*: 60:*/ }
/*: 61:*/ else
/*: 62:*/ return t;
/*: 63:*/
/*: 64:*/ /* to stop compiler warning...*/
/*: 65:*/ return 0;
/*: 66:*/ }
/*: 67:*/ /*---------------------------------------------------*/
/*: 68:*/ void yn(void)
/*: 69:*/ {
/*: 70:*/ switch(getch())
/*: 71:*/ {
/*: 72:*/ case 'y': case 'Y':
/*: 73:*/ PR("Y\n");
/*: 74:*/ exit(1);
/*: 75:*/ /*break;*/
/*: 76:*/ case 'n': case 'N':
/*: 77:*/ PR("N\n");
/*: 78:*/ exit(2);
/*: 79:*/ /*break;*/
/*: 80:*/
/*: 81:*/ /* Discard special keys (function, arrows,etc.)...?*/
/*: 82:*/ /* maybe necessary but seems to work without for me
/*: 83:*/ case '\0': case 0xE0:
/*: 84:*/ getch();
/*: 85:*/ */
/*: 86:*/ default:
/*: 87:*/ putchar('\a'); /*beep*/
/*: 88:*/ break;
/*: 89:*/ }
/*: 90:*/ }
/*: 91:*/ /*---------------------------------------------------*/
/*: 92:*/ int main(int argc, char **argv )
/*: 93:*/ {
/*: 94:*/ int t, otherkey=0;
/*: 95:*/
/*: 96:*/ t=getargs(argc,argv);
/*: 97:*/
/*: 98:*/ PR("[Y,N]?");
/*: 99:*/
/*:100:*/ if(t>0)
/*:101:*/ {
/*:102:*/ while(t--)
/*:103:*/ {
/*:104:*/ if (kbhit())
/*:105:*/ {
/*:106:*/ yn();
/*:107:*/ otherkey=1;
/*:108:*/ break;
/*:109:*/ }
/*:110:*/
/*:111:*/ Sleep(WAITTIME);
/*:112:*/ }
/*:113:*/
/*:114:*/ if (otherkey) /* then revert to no timeout.*/
/*:115:*/ for(;;)
/*:116:*/ yn();
/*:117:*/ {
/*:118:*/ PR("Y\n");
/*:119:*/ exit(1);
/*:120:*/ }
/*:121:*/ }
/*:122:*/ else
/*:123:*/ for(;;)
/*:124:*/ yn();
/*:125:*/
/*:126:*/ /* to stop compiler warning...*/
/*:127:*/ return 0;
/*:128:*/ }
Technical Details
Message-ID:
<400685d3_5@127.0.0.1>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news-out2.nntp.be!local!corp-news!not-for-mailReferences:
<4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com> <4003717f_5@127.0.0.1> <yJOdnaEvW6CsFZ7d4p2dnA@comcast.com> <4003a1fc_5@127.0.0.1> <ofCdnf-Ex48ELJ7dRVn-hg@comcast.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f8d38,6ae23aa1a5869fb6 X-Google-Attributes: gidf8d38,public X-Google-Thread: f7a51,6ae23aa1a5869fb6 X-Google-Attributes: gidf7a51,public X-Google-ArrivalTime: 2004-01-15 04:22:00 PST From: "Hal Styli" <no_spam@all> Newsgroups: microsoft.public.vc.language,borland.public.cpp.language References: <4002d4eb_5@127.0.0.1> <uIWdnZPEO6ofaJ_dRVn-ig@comcast.com> <4003717f_5@127.0.0.1> <yJOdnaEvW6CsFZ7d4p2dnA@comcast.com> <4003a1fc_5@127.0.0.1> <ofCdnf-Ex48ELJ7dRVn-hg@comcast.com> Subject: Re: choiceyn: cut down choice command, use of sleep, kbhit and getch Date: Thu, 15 Jan 2004 12:21:24 -0000 Keywords: CHOICE.EXE ASK.EXE Lines: 143 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2727.1300 Message-ID: <400685d3_5@127.0.0.1> X-Comments: This message was posted through Newsfeed.com X-Comments2: IMPORTANT: Newsfeeds.com does not condone, nor support, spam or any illegal or copyrighted postings. X-Comments3: IMPORTANT: Under NO circumstances will postings containing illegal or copyrighted material through this service be tolerated!! X-Report: Please report illegal or inappropriate use to <abuse@newsfeed.com> You may also use our online abuse reporting from: http://www.newsfeed.com/abuseform.htm X-Abuse-Info: Please be sure to forward a copy of ALL headers, INCLUDING the body (DO NOT SEND ATTACHMENTS) Organization: Newsfeed.com http://www.newsfeed.com 100,000+ UNCENSORED Newsgroups. Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!news-out2.nntp.be!local!corp-news!not-for-mail