How do you do TCL exec command on PHP script that uses user input?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.tcl

    How do you do TCL exec command on PHP script that uses user input?

    The TCL command I am using will do a command-line action on a PHP
    script:

    set cannotRunPHP [catch {
    eval "exec php -q $root/scripts/info/php/info.php"
    } errMsg]

    I have to do it this way as both the TCL script and the PHP script run
    as CLI. However, "info.php" requires user input to run; this causes
    the TCL script calling the PHP script to hose up and die.

    Is there a way I can do this so that the TCL script can call the PHP
    script without any problems? Is there an alternate way of calling the
    PHP script other than using exec were that to solve this issue?

    Thanks
    Phil

  • Ulrich Schöbel

    #2
    Re: How do you do TCL exec command on PHP script that uses user input?

    Hi Phillip,

    if your php script needs input from stdin, try open.
    If it needs input from the terminal, use expect.

    Best regards

    Ulrich


    In article <1132329306.392 127.205410@f14g 2000cwb.googleg roups.com>,
    "comp.lang. tcl" <phillip.s.powe ll@gmail.com> writes:[color=blue]
    > The TCL command I am using will do a command-line action on a PHP
    > script:
    >
    > set cannotRunPHP [catch {
    > eval "exec php -q $root/scripts/info/php/info.php"
    > } errMsg]
    >
    > I have to do it this way as both the TCL script and the PHP script run
    > as CLI. However, "info.php" requires user input to run; this causes
    > the TCL script calling the PHP script to hose up and die.
    >
    > Is there a way I can do this so that the TCL script can call the PHP
    > script without any problems? Is there an alternate way of calling the
    > PHP script other than using exec were that to solve this issue?
    >
    > Thanks
    > Phil
    >[/color]

    Comment

    • comp.lang.tcl

      #3
      Re: How do you do TCL exec command on PHP script that uses user input?


      Ulrich Schöbel wrote:[color=blue]
      > Hi Phillip,
      >
      > if your php script needs input from stdin, try open.
      > If it needs input from the terminal, use expect.[/color]

      Do you mean http://us2.php.net/manual/en/ref.expect.php ? If so, it'll
      be running on PHP 4.1.2 and I have no immediate permissions to download
      nor install the PECL expect library either. But something to think
      about. PTY?

      Phil

      [color=blue]
      >
      > Best regards
      >
      > Ulrich
      >
      >
      > In article <1132329306.392 127.205410@f14g 2000cwb.googleg roups.com>,
      > "comp.lang. tcl" <phillip.s.powe ll@gmail.com> writes:[color=green]
      > > The TCL command I am using will do a command-line action on a PHP
      > > script:
      > >
      > > set cannotRunPHP [catch {
      > > eval "exec php -q $root/scripts/info/php/info.php"
      > > } errMsg]
      > >
      > > I have to do it this way as both the TCL script and the PHP script run
      > > as CLI. However, "info.php" requires user input to run; this causes
      > > the TCL script calling the PHP script to hose up and die.
      > >
      > > Is there a way I can do this so that the TCL script can call the PHP
      > > script without any problems? Is there an alternate way of calling the
      > > PHP script other than using exec were that to solve this issue?
      > >
      > > Thanks
      > > Phil
      > >[/color][/color]

      Comment

      • Cameron Laird

        #4
        Re: How do you do TCL exec command on PHP script that uses user input?

        In article <dll010$dbv$1@c tb-nnrp2.saix.net> ,
        Ulrich Schöbel <ulrich@outvert .com> wrote:[color=blue]
        >Hi Phillip,
        >
        >if your php script needs input from stdin, try open.
        >If it needs input from the terminal, use expect.[/color]

        Comment

        • Ulrich Schöbel

          #5
          Re: How do you do TCL exec command on PHP script that uses user input?

          Hi Phillip,

          In article <1132332232.666 927.107240@g43g 2000cwa.googleg roups.com>,
          "comp.lang. tcl" <phillip.s.powe ll@gmail.com> writes:[color=blue]
          > Ulrich Schöbel wrote:[color=green]
          >> Hi Phillip,
          >>
          >> if your php script needs input from stdin, try open.
          >> If it needs input from the terminal, use expect.[/color]
          > Do you mean http://us2.php.net/manual/en/ref.expect.php ?[/color]
          No (Yes?). I don't know much of php. I meant

          a Tcl extension/replacement.

          Best regards

          Ulrich

          Comment

          • Ralf Fassel

            #6
            Re: How do you do TCL exec command on PHP script that uses userinput?

            * "comp.lang. tcl" <phillip.s.powe ll@gmail.com>
            | set cannotRunPHP [catch {
            | eval "exec php -q $root/scripts/info/php/info.php"
            | } errMsg]

            Get rid of that 'eval' there,
            set cannotRunPHP [catch {
            exec php -q $root/scripts/info/php/info.php
            } errMsg]
            is enough and avoids problems with double-eval of $root.

            | I have to do it this way as both the TCL script and the PHP script
            | run as CLI. However, "info.php" requires user input to run; this
            | causes the TCL script calling the PHP script to hose up and die.

            Usually, if you exec, TCLs stdin becomes the stdin for the exec'd
            process, so there is no immediate reason why the PHP script should
            terminate.

            If you have the input available inside TCL, you can redirect the stdin
            of the exec'd command like this:

            # get PHP input from some source
            set php_input {some stuff}

            # redirect stdin for PHP via <<
            set cannotRunPHP [catch {
            exec php -q $root/scripts/info/php/info.php << $php_input
            } errMsg]

            Now the PHP process will see the contents of the TCL php_input
            variable on stdin. Check the TCL 'exec' manpage for more details on
            input redirection.

            HTH
            R'

            Comment

            • SM Ryan

              #7
              Re: How do you do TCL exec command on PHP script that uses user input?

              "comp.lang. tcl" <phillip.s.powe ll@gmail.com> wrote:
              # The TCL command I am using will do a command-line action on a PHP
              # script:
              #
              # set cannotRunPHP [catch {
              # eval "exec php -q $root/scripts/info/php/info.php"
              # } errMsg]

              As a side note, you don't need to use eval in this command,
              and it can actually fail if $root has any spaces. Instead
              you can

              set cannotRunPHP [catch {
              exec php -q $root/scripts/info/php/info.php
              } errMsg]

              --
              SM Ryan http://www.rawbw.com/~wyrmwif/
              Where do you get those wonderful toys?

              Comment

              • comp.lang.tcl

                #8
                Re: How do you do TCL exec command on PHP script that uses user input?

                Please see below, thanx

                Ralf Fassel wrote:[color=blue]
                > * "comp.lang. tcl" <phillip.s.powe ll@gmail.com>
                > | set cannotRunPHP [catch {
                > | eval "exec php -q $root/scripts/info/php/info.php"
                > | } errMsg]
                >
                > Get rid of that 'eval' there,
                > set cannotRunPHP [catch {
                > exec php -q $root/scripts/info/php/info.php
                > } errMsg]
                > is enough and avoids problems with double-eval of $root.
                >[/color]

                For some reason that I never understood, leaving off "eval" on TCL exec
                commands insured that the file included within eval "exec php -q
                [myfile]" would never be run unless I added eval. No explanation at
                all.
                [color=blue]
                > | I have to do it this way as both the TCL script and the PHP script
                > | run as CLI. However, "info.php" requires user input to run; this
                > | causes the TCL script calling the PHP script to hose up and die.
                >
                > Usually, if you exec, TCLs stdin becomes the stdin for the exec'd
                > process, so there is no immediate reason why the PHP script should
                > terminate.
                >
                > If you have the input available inside TCL, you can redirect the stdin
                > of the exec'd command like this:
                >
                > # get PHP input from some source
                > set php_input {some stuff}
                >[/color]

                That would not work here that I understood. You are entering the data
                yourself. The PHP script will prompt you to enter something:

                Enter your username: [here is where you enter something in the command
                line]
                Etc.
                [color=blue]
                > # redirect stdin for PHP via <<
                > set cannotRunPHP [catch {
                > exec php -q $root/scripts/info/php/info.php << $php_input
                > } errMsg]
                >
                > Now the PHP process will see the contents of the TCL php_input
                > variable on stdin. Check the TCL 'exec' manpage for more details on
                > input redirection.
                >
                > HTH
                > R'[/color]

                Maybe if you clarified it a bit more I would know how to implement, as
                it is, I'm just as confused as before, I'm afraid and I'm sorry.

                Phil

                Comment

                • comp.lang.tcl

                  #9
                  Re: How do you do TCL exec command on PHP script that uses user input?

                  Please see below, thanx

                  Cameron Laird wrote:[color=blue]
                  > In article <dll010$dbv$1@c tb-nnrp2.saix.net> ,
                  > Ulrich Schöbel <ulrich@outvert .com> wrote:[color=green]
                  > >Hi Phillip,
                  > >
                  > >if your php script needs input from stdin, try open.
                  > >If it needs input from the terminal, use expect.[/color]
                  > .
                  > .
                  > .[color=green][color=darkred]
                  > >> The TCL command I am using will do a command-line action on a PHP
                  > >> script:
                  > >>
                  > >> set cannotRunPHP [catch {
                  > >> eval "exec php -q $root/scripts/info/php/info.php"
                  > >> } errMsg]
                  > >>
                  > >> I have to do it this way as both the TCL script and the PHP script run
                  > >> as CLI. However, "info.php" requires user input to run; this causes
                  > >> the TCL script calling the PHP script to hose up and die.
                  > >>
                  > >> Is there a way I can do this so that the TCL script can call the PHP
                  > >> script without any problems? Is there an alternate way of calling the
                  > >> PHP script other than using exec were that to solve this issue?[/color][/color]
                  > .
                  > .
                  > .
                  > Ulrich, I suspect this is easier than imagined; neither
                  > [open] nor [expect] will be necessary.
                  >
                  > Mr. Powell, I understand your description only partially.
                  > It might take a few trips before you have what you want.
                  >
                  > What kind of "user input" does info.php require? When
                  > running info.php on a (Linux?) command-line, do you always
                  > give it the same user input? Is the user input something
                  > like
                  > yes
                  > 14
                  > no
                  > ? Can you automate info.php with the use of a conventional
                  > shell, on the model of such an example as
                  > php -q $root/scripts/info/php/info.php << HERE
                  > yes
                  > 14
                  > no
                  > HERE
                  > ?[/color]

                  No, because you will be entering the input yourself via command line.
                  The PHP script itself will prompt you to enter things like your
                  username, password, etc, etc., on the command line, from there you will
                  manually enter the data yourself.

                  Phil[color=blue]
                  >
                  > Incidentally,
                  > eval "exec php -q $root/scripts/info/php/info.php"
                  > does nothing for you that
                  > exec php -q $root/scripts/info/php/info.php
                  > doesn't do better.[/color]

                  Comment

                  • Cameron Laird

                    #10
                    Re: How do you do TCL exec command on PHP script that uses user input?

                    In article <1132352850.734 347.80770@o13g2 000cwo.googlegr oups.com>,
                    comp.lang.tcl <phillip.s.powe ll@gmail.com> wrote:

                    Comment

                    • Ralf Fassel

                      #11
                      Re: How do you do TCL exec command on PHP script that uses userinput?

                      * "comp.lang. tcl" <phillip.s.powe ll@gmail.com>
                      | For some reason that I never understood, leaving off "eval" on TCL
                      | exec commands insured that the file included within eval "exec php
                      | -q [myfile]" would never be run unless I added eval. No explanation
                      | at all.

                      You do not want double quotes there, use
                      catch {exec php -q myfile}
                      This will call the command 'exec' with 'php' as first argument, '-p'
                      as second and 'myfile' as third argument.

                      If you use
                      catch {"exec php -q myfile"}
                      TCL will try to call a 4-word command named
                      "exec php -q myfile"
                      with no arguments.

                      Adding an 'eval' splits this up, but adds a second round of
                      interpretation. This makes a difference if any of your variables have
                      special characters in them.
                      Eg:
                      set myfile {c:\windows\sys tem32}
                      catch {exec ls $myfile} res
                      => returns 0, directory listing is in $res

                      catch {"exec ls $myfile"} res
                      => returns 1, invalid command name "exec ls c:\windows\syst em32"

                      catch {eval "exec ls $myfile"} result
                      => returns 1, /usr/bin/ls: c:windowssystem 32: No such file or directory

                      The 'eval' has interpreted the contents of $myfile a second time and
                      stripped off the backslashes, thus you end up with a wrong file name
                      when calling the 'ls' command.

                      Rule of thumb: if you want to use catch around some command
                      some_cmd arg1 arg2 arg3
                      just add 'catch {' before the command and '}' after it.
                      catch { some_cmd arg1 arg2 arg3 }
                      Do _not_ add additional quoting. From there on, build up further
                      set err [catch { some_cmd arg1 arg2 arg3 }]
                      if {$err} { ... }
                      Or directly
                      if {[catch { some_cmd arg1 arg2 arg3 }]} { ... }


                      | The PHP script will prompt you to enter something:
                      |
                      | Enter your username: [here is where you enter something in the
                      | command line] Etc.

                      Hmm, most probably this does not work because TCL captures the output
                      of the process and returns it as result of the exec call. So you
                      don't see the prompt etc.

                      Try to redirect stdout and stderr when calling the php script, like
                      this:
                      catch { exec php -q myfile >@stdout 2>@stderr }

                      This will redirect stdout of the PHP script to TCLs stdout, and stderr
                      of the PHP script to TCLs stderr. stdin should already have been
                      handled by TCL. Again, the manpage for exec describes the details.

                      | Maybe if you clarified it a bit more I would know how to implement,
                      | as it is, I'm just as confused as before, I'm afraid and I'm sorry.

                      I hope this has not added to the confusion :-/

                      R'

                      Comment

                      • comp.lang.tcl

                        #12
                        Re: How do you do TCL exec command on PHP script that uses user input?

                        Please see below, thanx!

                        Cameron Laird wrote:[color=blue]
                        > In article <1132352850.734 347.80770@o13g2 000cwo.googlegr oups.com>,
                        > comp.lang.tcl <phillip.s.powe ll@gmail.com> wrote:
                        > .
                        > .
                        > .[color=green][color=darkred]
                        > >> What kind of "user input" does info.php require? When
                        > >> running info.php on a (Linux?) command-line, do you always
                        > >> give it the same user input? Is the user input something
                        > >> like
                        > >> yes
                        > >> 14
                        > >> no
                        > >> ? Can you automate info.php with the use of a conventional
                        > >> shell, on the model of such an example as
                        > >> php -q $root/scripts/info/php/info.php << HERE
                        > >> yes
                        > >> 14
                        > >> no
                        > >> HERE
                        > >> ?[/color]
                        > >
                        > >No, because you will be entering the input yourself via command line.
                        > >The PHP script itself will prompt you to enter things like your
                        > >username, password, etc, etc., on the command line, from there you will
                        > >manually enter the data yourself.[/color]
                        > .
                        > .
                        > .
                        > Ah! Does
                        > exec php -q $root/scripts/info/php/info.php >&@stdout <@stdin
                        > get you closer to where you want to be?[/color]

                        Yes it does!! You will have to explain that one to me, though, way
                        beyond my able to figure it out, but cool!

                        Problem is that the PHP script contains an exit() function that dies if
                        user does not input something upon prompting. The TCL script does not
                        die, it keeps going, which is not the desired effect, but hey I'm
                        halfway there already, thanx!!

                        Phil

                        Comment

                        • Cameron Laird

                          #13
                          Re: How do you do TCL exec command on PHP script that uses user input?

                          In article <1132452443.558 092.275200@g14g 2000cwa.googleg roups.com>,
                          comp.lang.tcl <phillip.s.powe ll@gmail.com> wrote:[color=blue][color=green]
                          >> .
                          >> .
                          >> .
                          >> Ah! Does
                          >> exec php -q $root/scripts/info/php/info.php >&@stdout <@stdin
                          >> get you closer to where you want to be?[/color]
                          >
                          >Yes it does!! You will have to explain that one to me, though, way
                          >beyond my able to figure it out, but cool![/color]

                          Comment

                          • Cameron Laird

                            #14
                            Re: How do you do TCL exec command on PHP script that uses user input?

                            In article <1132452443.558 092.275200@g14g 2000cwa.googleg roups.com>,
                            comp.lang.tcl <phillip.s.powe ll@gmail.com> wrote:

                            Comment

                            • Ralf Fassel

                              #15
                              Re: How do you do TCL exec command on PHP script that uses userinput?

                              * "comp.lang. tcl" <phillip.s.powe ll@gmail.com>
                              | 4) If you just hit the carriage return, it throws an error message "You
                              | must enter..." and dies
                              --<snip-snip>--
                              | However, when the same PHP script is now being accessed by the TCL
                              | script, it does 1), you do 2), 3) occurs, but if you do 4), instead
                              | of correctly throwing an error message and dying, it just keeps
                              | going and lets you enter more "nonentitie s".

                              TCL needs some way of knowing whether the PHP script was successful or
                              not. If the PHP script always exits with status 0 even if no valid
                              input was given, then TCL needs to check the output of the script.

                              Here you have the problem that on the one hand, the user needs to see
                              the prompt (redirect PHP stdout), on the other hand TCLs needs to see
                              the error message.

                              If you're lucky, you can change the PHP script to exit non-zero in
                              case of invalid input. Then the 'catch' in TCL will trigger.

                              If you're not *that* lucky, but still *somewhat* lucky, the "You must
                              enter..." message from the PHP script goes to stderr. In that case
                              just don't redirect stderr when exec'ing the PHP script. Then the
                              'catch' in TCL will trigger, since anything on stderr of a subprocess
                              is considered an error in TCL.

                              if {[catch {exec php -q $root >@stdout} err]} {
                              puts "error while calling PHP:"
                              puts "errorCode $::errorCode"
                              puts "message: $err"
                              }

                              If the 'error' message of PHP also goes to stdout, you will have to
                              use some other means (expect comes to my mind then), since then you
                              will need to separate the prompt from the rest of the output.

                              HTH & good luck!
                              R'

                              Comment

                              Working...