Stopping a while loop with user input ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ern

    Stopping a while loop with user input ?

    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'){
    keepExecutingSc ript();
    }

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

    Thanks,

  • Michael Mair

    #2
    Re: Stopping a while loop with user input ?

    ern wrote:[color=blue]
    > 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'){
    > keepExecutingSc ript();
    > }
    >
    > How can I do the "user hasn't pressed 'any key' " of this logic?
    > Running Linux Red Hat V3.[/color]

    This cannot done in standard C; try comp.unix.progr ammer

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Default User

      #3
      Re: Stopping a while loop with user input ?

      ern wrote:


      [color=blue]
      > I need a way to stop the execution with user input. I was thinking
      > something like this:
      >
      > while(user hasn't pressed 'any key'){
      > keepExecutingSc ript();
      > }[/color]






      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.

      Comment

      • Dave Vandervies

        #4
        Re: Stopping a while loop with user input ?

        In article <1129130317.601 351.247600@g44g 2000cwa.googleg roups.com>,
        ern <erniedude@gmai l.com> wrote:[color=blue]
        >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'){
        > keepExecutingSc ript();
        >}[/color]

        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,S IG_DFL);
        }

        void do_main_loop(so me_args args)
        {
        void (*old_sighandle r)(int);

        old_sighandler= signal(SIGINT,s igint_handler);
        if(old_sighandl er==SIG_IGN)
        {
        /*Assume it was ignored for a good reason, re-ignore*/
        signal(SIGINT,S IG_IGN);
        }
        if(old_sighandl er==SIG_ERR)
        {
        report_signal_i nstallation_err or();
        abort_if_we_don t_want_to_conti nue_uninterrupt ible();
        }

        while(time_to_s top==0)
        {
        keep_executing_ script(args);
        }
        do_any_cleanup_ required();

        /*Depending on what comes next, we may want to restore the original
        handler:
        */
        if(old_sighandl er!=SIG_ERR)
        signal(SIGINT,o ld_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 dj3vande@csclub .uwaterloo.ca

        Don't write in troglodyte here, it annoys the natives.
        --Lawrence Kirby in comp.lang.c

        Comment

        • Mabden

          #5
          Re: Stopping a while loop with user input ?

          "ern" <erniedude@gmai l.com> wrote in message
          news:1129130317 .601351.247600@ g44g2000cwa.goo glegroups.com.. .[color=blue]
          > I have a program that runs scripts. If the user types "script
          > myScript.dat" the program will grab commands from the text file,[/color]
          verify[color=blue]
          > 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'){
          > keepExecutingSc ript();
          > }
          >
          > How can I do the "user hasn't pressed 'any key' " of this logic?
          > Running Linux Red Hat V3.[/color]

          getch() might work.

          --
          Mabden


          Comment

          • Jordan Abel

            #6
            Re: Stopping a while loop with user input ?

            On 2005-10-26, Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
            > "ern" <erniedude@gmai l.com> wrote in message
            > news:1129130317 .601351.247600@ g44g2000cwa.goo glegroups.com.. .[color=green]
            >> I have a program that runs scripts. If the user types "script
            >> myScript.dat" the program will grab commands from the text file,[/color]
            > verify[color=green]
            >> 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'){
            >> keepExecutingSc ript();
            >> }
            >>
            >> How can I do the "user hasn't pressed 'any key' " of this logic?
            >> Running Linux Red Hat V3.[/color]
            >
            > getch() might work.[/color]

            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.

            Comment

            • Mabden

              #7
              Re: Stopping a while loop with user input ?

              "Jordan Abel" <jmabel@purdue. edu> wrote in message
              news:slrndlungi .2u8s.jmabel@ra ndom.yi.org...[color=blue]
              > On 2005-10-26, Mabden <mabden@sbc_glo bal.net> wrote:[color=green]
              > > "ern" <erniedude@gmai l.com> wrote in message
              > > news:1129130317 .601351.247600@ g44g2000cwa.goo glegroups.com.. .[color=darkred]
              > >> I have a program that runs scripts. If the user types "script
              > >> myScript.dat" the program will grab commands from the text file,[/color]
              > > verify[color=darkred]
              > >> 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'){
              > >> keepExecutingSc ript();
              > >> }
              > >>
              > >> How can I do the "user hasn't pressed 'any key' " of this logic?
              > >> Running Linux Red Hat V3.[/color]
              > >
              > > getch() might work.[/color]
              >
              > 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.[/color]

              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


              Comment

              • Jordan Abel

                #8
                Re: Stopping a while loop with user input ?

                On 2005-10-26, Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
                > "Jordan Abel" <jmabel@purdue. edu> wrote in message
                > news:slrndlungi .2u8s.jmabel@ra ndom.yi.org...[color=green]
                >> On 2005-10-26, Mabden <mabden@sbc_glo bal.net> wrote:[color=darkred]
                >> > "ern" <erniedude@gmai l.com> wrote in message
                >> > news:1129130317 .601351.247600@ g44g2000cwa.goo glegroups.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'){
                >> >> keepExecutingSc ript();
                >> >> }
                >> >>
                >> >> How can I do the "user hasn't pressed 'any key' " of this logic?
                >> >> Running Linux Red Hat V3.
                >> >
                >> > getch() might work.[/color]
                >>
                >> 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.[/color]
                >
                > Curses? Unixish? what's that? My 'puter speaks DOS. My compiler knows
                > getch() and so does my bible, the K&R.[/color]

                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.
                [color=blue]
                > 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.[/color]

                he _can_ do it, using the curses getch()
                [color=blue]
                > 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:[/color]

                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]

                Comment

                • Mabden

                  #9
                  Re: Stopping a while loop with user input ?

                  "Jordan Abel" <jmabel@purdue. edu> wrote in message
                  news:slrndluu3s .2ure.jmabel@ra ndom.yi.org...[color=blue]
                  > On 2005-10-26, Mabden <mabden@sbc_glo bal.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][/color]

                  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


                  Comment

                  • Joe Wright

                    #10
                    Re: Stopping a while loop with user input ?

                    Mabden wrote:[color=blue]
                    > "ern" <erniedude@gmai l.com> wrote in message
                    > news:1129130317 .601351.247600@ g44g2000cwa.goo glegroups.com.. .
                    >[color=green]
                    >>I have a program that runs scripts. If the user types "script
                    >>myScript.da t" the program will grab commands from the text file,[/color]
                    >
                    > verify
                    >[color=green]
                    >>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'){
                    >> keepExecutingSc ript();
                    >>}
                    >>
                    >>How can I do the "user hasn't pressed 'any key' " of this logic?
                    >>Running Linux Red Hat V3.[/color]
                    >
                    >
                    > getch() might work.
                    >[/color]
                    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 ---

                    Comment

                    • Mabden

                      #11
                      Re: Stopping a while loop with user input ?

                      "Joe Wright" <jwright@comcas t.net> wrote in message
                      news:KdqdndjOA8 ykYsLeRVn-1A@comcast.com. ..[color=blue]
                      > Mabden wrote:[color=green]
                      > > getch() might work.
                      > >[/color]
                      > Just when I thought you were learning.. getch() simply does not exist[/color]
                      in[color=blue]
                      > Standard C, the topic of this newsgroup. Various implementations may
                      > provide it (an others) as extensions but it isn't C.
                      >[/color]

                      Awww.. how sweet, you thought...

                      getch() is on page 78-79 of the only C standard that matters, K&R2.

                      I've never compiled a program using getch() and had it fail. But as I
                      said, I was surprized to find that I actually used SIGINT more than I
                      thought, and getch() less than you think.

                      So, while you are not wrong - you're wrong! ;-)

                      --
                      Mabden


                      Comment

                      • Jordan Abel

                        #12
                        Re: Stopping a while loop with user input ?

                        On 2005-10-27, Mabden <mabden@sbc_glo bal.net> wrote:[color=blue]
                        > "Joe Wright" <jwright@comcas t.net> wrote in message
                        > news:KdqdndjOA8 ykYsLeRVn-1A@comcast.com. ..[color=green]
                        >> Mabden wrote:[color=darkred]
                        >> > getch() might work.
                        >> >[/color]
                        >> 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.[/color]
                        >
                        > Awww.. how sweet, you thought...
                        >
                        > getch() is on page 78-79 of the only C standard that matters,
                        > K&R2.
                        >
                        > I've never compiled a program using getch() and had it fail. But
                        > as I said, I was surprized to find that I actually used SIGINT
                        > more than I thought, and getch() less than you think.
                        >
                        > So, while you are not wrong - you're wrong! ;-)[/color]

                        You are incorrect. K&R2 does NOT claim getch() exists or a standard
                        function. It uses the name in an example, which is in fact an
                        implicit claim that it is NOT a standard function

                        Comment

                        • Flash Gordon

                          #13
                          Re: Stopping a while loop with user input ?

                          Mabden wrote:[color=blue]
                          > "Joe Wright" <jwright@comcas t.net> wrote in message
                          > news:KdqdndjOA8 ykYsLeRVn-1A@comcast.com. ..
                          >[color=green]
                          >>Mabden wrote:
                          >>[color=darkred]
                          >>>getch() might work.[/color]
                          >>
                          >>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.[/color]
                          >
                          > Awww.. how sweet, you thought...
                          >
                          > getch() is on page 78-79 of the only C standard that matters, K&R2.[/color]

                          Where it provides an implementation of getch which has different
                          semantics to the implementation I have on my Linux box and different
                          semantics to the implementation provided my MS in their C library. If
                          you look at Appendix B which describes the standard library you will
                          find absolutely NO reference to getch.

                          Also, it is the ISO standard that defines C and therefore what is
                          topical here, K&R2 is merely a very good, but not perfect, book about
                          the language.
                          [color=blue]
                          > I've never compiled a program using getch() and had it fail.[/color]

                          Which proves nothing apart from your experience being limited.
                          [color=blue]
                          > But as I
                          > said, I was surprized to find that I actually used SIGINT more than I
                          > thought, and getch() less than you think.
                          >
                          > So, while you are not wrong - you're wrong! ;-)[/color]

                          No, Joe is COMPLETELY correct and you are COMPLETELY wrong.

                          Since you are obviously not improving I'll put you back in the kill file.
                          --
                          Flash Gordon
                          Living in interesting times.
                          Although my email address says spam, it is real and I read it.

                          Comment

                          • Mabden

                            #14
                            Re: Stopping a while loop with user input ?

                            "Flash Gordon" <spam@flash-gordon.me.uk> wrote in message
                            news:mcu633xkev .ln2@news.flash-gordon.me.uk...[color=blue]
                            > Since you are obviously not improving I'll put you back in the kill[/color]
                            file.
                            Hey! I just got to stretch my legs! Haven't you ever been wrong!!

                            --
                            Mabden



                            Comment

                            • pete

                              #15
                              Re: Stopping a while loop with user input ?

                              Jordan Abel wrote:
                              [color=blue]
                              > K&R2 does NOT claim getch() exists or a standard function.[/color]

                              You are correct.
                              [color=blue]
                              > It uses the name in an example, which is in fact an
                              > implicit claim that it is NOT a standard function[/color]

                              You would think so, but there is a counterexample in K&R.

                              🔑Surf like a Boss! Unleash the indomitable power of our lightning-fast, rock-solid VPN shield. Maximum cybersecurity and privacy protection. Built on WireGuard


                              119-121(§5.11): The qsort discussion needs recasting in several ways.
                              First, qsort is a standard routine in ANSI/ISO C, so
                              the rendition here should be given a different name, especially because
                              the arguments to standard qsort are a bit different: the
                              standard accepts a base pointer and a count, while this example uses a
                              base pointer and two offsets.

                              --
                              pete

                              Comment

                              Working...