Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Stopping a while loop with user input ?

Reply
Thread Tools

Stopping a while loop with user input ?

 
 
ern
Guest
Posts: n/a
 
      10-12-2005
I have a program that runs scripts. If the user types "script
myScript.dat" the program will grab commands from the text file, verify
correctness, and begin executing the script UNTIL...

I need a way to stop the execution with user input. I was thinking
something like this:

while(user hasn't pressed 'any key'){
keepExecutingScript();
}

How can I do the "user hasn't pressed 'any key' " of this logic?
Running Linux Red Hat V3.

Thanks,

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      10-12-2005
ern wrote:
> I have a program that runs scripts. If the user types "script
> myScript.dat" the program will grab commands from the text file, verify
> correctness, and begin executing the script UNTIL...
>
> I need a way to stop the execution with user input. I was thinking
> something like this:
>
> while(user hasn't pressed 'any key'){
> keepExecutingScript();
> }
>
> How can I do the "user hasn't pressed 'any key' " of this logic?
> Running Linux Red Hat V3.


This cannot done in standard C; try comp.unix.programmer

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Default User
Guest
Posts: n/a
 
      10-12-2005
ern wrote:



> I need a way to stop the execution with user input. I was thinking
> something like this:
>
> while(user hasn't pressed 'any key'){
> keepExecutingScript();
> }



http://www.eskimo.com/~scs/C-faq/q19.1.html



Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      10-20-2005
In article < .com>,
ern <> wrote:
>I have a program that runs scripts. If the user types "script
>myScript.dat" the program will grab commands from the text file, verify
>correctness, and begin executing the script UNTIL...
>
>I need a way to stop the execution with user input. I was thinking
>something like this:
>
>while(user hasn't pressed 'any key'){
> keepExecutingScript();
>}


If you're allowed to choose which key the user uses to interrupt the
program, there's a good chance you can do this without going beyond what
the standard defines.
(This may or may not be something you'd want to do instead of using
something system-specific instead.)

Most implementations have a way to cause SIGINT to be raised in a running
program, so you can just install a handler that sets a flag indicating
it's time to stop, and check that in your main loop.
(Note that the way you arrange for SIGINT to be raised asynchronously
is entirely dependent on your system and there may not be a way at all.
(All of the systems that used nontrivially use ctrl-C by default, but
check your documentation.) So you're not actually gaining portability
to systems that don't support user-caused SIGINT, you're just gaining
not having to change the code to move between systems that do.)

--------
#include <signal.h>

volatile sig_atomic_t time_to_stop=0;

void sigint_handler(int ignored)
{
time_to_stop=1;
/*Revert to the default signal handler (usually immediate
termination) for if the user gets impatient and interrupts
again
*/
signal(SIGINT,SIG_DFL);
}

void do_main_loop(some_args args)
{
void (*old_sighandler)(int);

old_sighandler=signal(SIGINT,sigint_handler);
if(old_sighandler==SIG_IGN)
{
/*Assume it was ignored for a good reason, re-ignore*/
signal(SIGINT,SIG_IGN);
}
if(old_sighandler==SIG_ERR)
{
report_signal_installation_error();
abort_if_we_dont_want_to_continue_uninterruptible( );
}

while(time_to_stop==0)
{
keep_executing_script(args);
}
do_any_cleanup_required();

/*Depending on what comes next, we may want to restore the original
handler:
*/
if(old_sighandler!=SIG_ERR)
signal(SIGINT,old_sighandler);
}
--------

Note that the things you can do in a handler for an asynchronous signal
are seriously limited. Storing a value in an object of type volatile
sig_atomic_t is guaranteed to do what you'd expect (store the value
completely and correctly and become available the next time it's checked
outside the signal handler), and using signal() to install a new handler
for the signal you're handling is also well-defined. Calling most library
functions isn't safe, examining and/or modifying most objects isn't safe,
and there's really not a whole lot that you can do without trying to do
one or the other of those.


dave

--
Dave Vandervies

Don't write in troglodyte here, it annoys the natives.
--Lawrence Kirby in comp.lang.c
 
Reply With Quote
 
Mabden
Guest
Posts: n/a
 
      10-26-2005
"ern" <> wrote in message
news: oups.com...
> I have a program that runs scripts. If the user types "script
> myScript.dat" the program will grab commands from the text file,

verify
> correctness, and begin executing the script UNTIL...
>
> I need a way to stop the execution with user input. I was thinking
> something like this:
>
> while(user hasn't pressed 'any key'){
> keepExecutingScript();
> }
>
> How can I do the "user hasn't pressed 'any key' " of this logic?
> Running Linux Red Hat V3.


getch() might work.

--
Mabden


 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-26-2005
On 2005-10-26, Mabden <mabden@sbc_global.net> wrote:
> "ern" <> wrote in message
> news: oups.com...
>> I have a program that runs scripts. If the user types "script
>> myScript.dat" the program will grab commands from the text file,

> verify
>> correctness, and begin executing the script UNTIL...
>>
>> I need a way to stop the execution with user input. I was thinking
>> something like this:
>>
>> while(user hasn't pressed 'any key'){
>> keepExecutingScript();
>> }
>>
>> How can I do the "user hasn't pressed 'any key' " of this logic?
>> Running Linux Red Hat V3.

>
> getch() might work.


however, you need curses for that. The only "portable" way to do
this would be to use SIGINT and limit the user's ability to stop
the "script" to causing SIGINT (in an implementation-defined way:
on any unix system this is controlled by the tty driver and is
caused by default by hitting ctrl-c - it's not unreasonable to
use ctrl-c and/or ctrl-break on windows/dos, etc.)

your signal handler could set a global variable that is then
checked in the while loop.

it's also a lot easier than using curses, and a lot more in
keeping with how other unix-ish applications do things.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-26-2005
On 2005-10-26, Mabden <mabden@sbc_global.net> wrote:
> "Jordan Abel" <> wrote in message
> news:...
>> On 2005-10-26, Mabden <mabden@sbc_global.net> wrote:
>> > "ern" <> wrote in message
>> > news: oups.com...
>> >> I have a program that runs scripts. If the user types "script
>> >> myScript.dat" the program will grab commands from the text file,
>> > verify
>> >> correctness, and begin executing the script UNTIL...
>> >>
>> >> I need a way to stop the execution with user input. I was thinking
>> >> something like this:
>> >>
>> >> while(user hasn't pressed 'any key'){
>> >> keepExecutingScript();
>> >> }
>> >>
>> >> How can I do the "user hasn't pressed 'any key' " of this logic?
>> >> Running Linux Red Hat V3.
>> >
>> > getch() might work.

>>
>> however, you need curses for that. The only "portable" way to do
>> this would be to use SIGINT and limit the user's ability to stop
>> the "script" to causing SIGINT (in an implementation-defined way:
>> on any unix system this is controlled by the tty driver and is
>> caused by default by hitting ctrl-c - it's not unreasonable to
>> use ctrl-c and/or ctrl-break on windows/dos, etc.)
>>
>> your signal handler could set a global variable that is then
>> checked in the while loop.
>>
>> it's also a lot easier than using curses, and a lot more in
>> keeping with how other unix-ish applications do things.

>
> Curses? Unixish? what's that? My 'puter speaks DOS. My compiler knows
> getch() and so does my bible, the K&R.


there is a getch() that exists on DOS and one that exists on unix.
they do not have the same semantics. K&R uses the name, but it is
for neither of the two commonly-existing functions of that name, but
rather of its own example of how one might write a getc-ish function
that could play nice with an ungetc-ish one. Moreover, the getch in
K&R, the one in unix/curses under some circumstances, and the dos
one, will ALL block until the user actually presses a key - none of
them [except the curses one under SOME sets of options] will return
immediately if there is no key pressed by the user.

> Can't he read a line of script and then check if a key was hit? I mean,
> the one line of the script may take a little while to finish, but at
> least the whole script doesn't have to finish before the user gets
> control.


he _can_ do it, using the curses getch()

> SIGINT is fine, but seems overkill for processing a script, since you
> can just poll after every line. I use SIGINT for trapping Ctrl-C to stop
> something lengthy like parsing a directory tree, but a script file of
> presumably short-lived commands could be halted by just polling the
> keyboard buffer with getch(). Unless the script was something like:


the problem is that getch() will not return if there is _not_ a
character. [well, you can do it with curses, but it's tricky]
 
Reply With Quote
 
Mabden
Guest
Posts: n/a
 
      10-26-2005
"Jordan Abel" <> wrote in message
news:...
> On 2005-10-26, Mabden <mabden@sbc_global.net> wrote:
> > "ern" <> wrote in message
> > news: oups.com...
> >> I have a program that runs scripts. If the user types "script
> >> myScript.dat" the program will grab commands from the text file,

> > verify
> >> correctness, and begin executing the script UNTIL...
> >>
> >> I need a way to stop the execution with user input. I was thinking
> >> something like this:
> >>
> >> while(user hasn't pressed 'any key'){
> >> keepExecutingScript();
> >> }
> >>
> >> How can I do the "user hasn't pressed 'any key' " of this logic?
> >> Running Linux Red Hat V3.

> >
> > getch() might work.

>
> however, you need curses for that. The only "portable" way to do
> this would be to use SIGINT and limit the user's ability to stop
> the "script" to causing SIGINT (in an implementation-defined way:
> on any unix system this is controlled by the tty driver and is
> caused by default by hitting ctrl-c - it's not unreasonable to
> use ctrl-c and/or ctrl-break on windows/dos, etc.)
>
> your signal handler could set a global variable that is then
> checked in the while loop.
>
> it's also a lot easier than using curses, and a lot more in
> keeping with how other unix-ish applications do things.


Curses? Unixish? what's that? My 'puter speaks DOS. My compiler knows
getch() and so does my bible, the K&R.

Can't he read a line of script and then check if a key was hit? I mean,
the one line of the script may take a little while to finish, but at
least the whole script doesn't have to finish before the user gets
control.

SIGINT is fine, but seems overkill for processing a script, since you
can just poll after every line. I use SIGINT for trapping Ctrl-C to stop
something lengthy like parsing a directory tree, but a script file of
presumably short-lived commands could be halted by just polling the
keyboard buffer with getch(). Unless the script was something like:

1 cure cancer
2 cure Michael Jackson
3 fix Madben

--
Mabden



--
Mabden


 
Reply With Quote
 
Mabden
Guest
Posts: n/a
 
      10-26-2005
"Jordan Abel" <> wrote in message
news:...
> On 2005-10-26, Mabden <mabden@sbc_global.net> wrote:
> the problem is that getch() will not return if there is _not_ a
> character. [well, you can do it with curses, but it's tricky]


Oops.. so That's why I use SIGINT... I looked back at some DOS programs
and there were more of them than I remembered. But they all seemed to be
for Ctrl-C handling.

Did the OP say they wanted to trap "The Any Key" or a specific key? Does
SIGINT flag, say a space bar? I forget, since I used it to exit
gracefully from a program when ctrl-c or ctrl-break was hit. What about
the old ctrl-d ctrl-s combo for pausing and restarting a program, maybe
that might work for the OP.

I guess I need to read the first post.

--
Mabden


 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      10-26-2005
Mabden wrote:
> "ern" <> wrote in message
> news: oups.com...
>
>>I have a program that runs scripts. If the user types "script
>>myScript.dat" the program will grab commands from the text file,

>
> verify
>
>>correctness, and begin executing the script UNTIL...
>>
>>I need a way to stop the execution with user input. I was thinking
>>something like this:
>>
>>while(user hasn't pressed 'any key'){
>> keepExecutingScript();
>>}
>>
>>How can I do the "user hasn't pressed 'any key' " of this logic?
>>Running Linux Red Hat V3.

>
>
> getch() might work.
>

Just when I thought you were learning.. getch() simply does not exist in
Standard C, the topic of this newsgroup. Various implementations may
provide it (an others) as extensions but it isn't C.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Re: Stopping a loop with user input. in curses Raymond Arthur St. Marie II of III Python 1 07-23-2003 02:20 AM
Re: Stopping a loop with user input. in curses Raymond Arthur St. Marie II of III Python 2 07-22-2003 03:22 AM
Re: Stopping a loop with user input. in curses Chatralias Python 0 07-20-2003 03:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57