Re: How to execute C program and display output to webpage?

Discussion in 'Perl' started by John Bokma, Sep 9, 2003.

  1. John Bokma

    John Bokma Guest

    Abby wrote:

    > Hi,
    >
    > I have no experience in Perl, but I need to write a Perl script.
    > What the script will do is .. it will pass some arguments, which I got
    > from text box in the form on webpage, to my c program (.exe file) as
    > arguments, run the program, then display the command-line output to
    > the web browser. Could anyone please give me an idea how to start,
    > please?


    CGI.pm (getting parameters, creating headers).
    -T option in the she-bang
    -w option in the she-bang
    use strict;

    read on "system"

    If you generate HTML using CGI.pm notice that recent versions default to
    XHTML. Understand the impacts of this.

    understand all the dangers of passing parameters from a form to an
    executable.

    What I recommend if:

    a given parameter can only be a number between 1 and 100 *check* this.
    Check it is a number *and* check if it falls in the range. Never ever
    assume that your form gets valid values *nor* ever assume that client
    side checks (JavaScript, maxlength etc) succeed (zie link in my sig).

    If a parameter value falls out of a range, is not what you expect then
    give an error. This is an exception. Something is *wrong*. Don't ever
    try to fix it or fall back to defaults. If you get a silly value shout,
    yell, complain. Don't silently fix & accept.

    Be very very careful what you allow to be passed via a web form. All
    your enemies are out there and able to call your script in every
    possible bad way. Don't trust anything, ever. Even not the headers you
    get (for example referer). Assume the worst.

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 9, 2003
    #1
    1. Advertising

  2. John Bokma

    Abby Guest

    Thanks a lot for your great tips. Just wonder about one thing. After I
    use system("[program] [parameter]"), how can I get the output and
    display all of them in webpage? Thank you so much.



    John Bokma <> wrote in message news:<>...
    > Abby wrote:
    >
    > > Hi,
    > >
    > > I have no experience in Perl, but I need to write a Perl script.
    > > What the script will do is .. it will pass some arguments, which I got
    > > from text box in the form on webpage, to my c program (.exe file) as
    > > arguments, run the program, then display the command-line output to
    > > the web browser. Could anyone please give me an idea how to start,
    > > please?

    >
    > CGI.pm (getting parameters, creating headers).
    > -T option in the she-bang
    > -w option in the she-bang
    > use strict;
    >
    > read on "system"
    >
    > If you generate HTML using CGI.pm notice that recent versions default to
    > XHTML. Understand the impacts of this.
    >
    > understand all the dangers of passing parameters from a form to an
    > executable.
    >
    > What I recommend if:
    >
    > a given parameter can only be a number between 1 and 100 *check* this.
    > Check it is a number *and* check if it falls in the range. Never ever
    > assume that your form gets valid values *nor* ever assume that client
    > side checks (JavaScript, maxlength etc) succeed (zie link in my sig).
    >
    > If a parameter value falls out of a range, is not what you expect then
    > give an error. This is an exception. Something is *wrong*. Don't ever
    > try to fix it or fall back to defaults. If you get a silly value shout,
    > yell, complain. Don't silently fix & accept.
    >
    > Be very very careful what you allow to be passed via a web form. All
    > your enemies are out there and able to call your script in every
    > possible bad way. Don't trust anything, ever. Even not the headers you
    > get (for example referer). Assume the worst.
     
    Abby, Sep 10, 2003
    #2
    1. Advertising

  3. John Bokma

    John Bokma Guest

    Abby wrote:

    please don't top post, or if you do, cut away everything below it if it
    is no longer relevant.

    > Thanks a lot for your great tips. Just wonder about one thing. After I
    > use system("[program] [parameter]"), how can I get the output and
    > display all of them in webpage? Thank you so much.


    if you want the output you can use `` (back ticks, not ') or open a pipe
    to read from:

    $output = `program parameter`; # one, multilined string
    @output = `program parameter`; # one line per item

    open(PROGRAM, "program parameter |" or die "Can't run program: $!";
    while ....
    :
    :


    Note: both call a shell to run the program. Understand the impact of this.

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 10, 2003
    #3
  4. John Bokma

    Greg Schmidt Guest

    On 9 Sep 2003 19:42:43 -0700, (Abby) wrote:

    >Thanks a lot for your great tips. Just wonder about one thing. After I
    >use system("[program] [parameter]"), how can I get the output and
    >display all of them in webpage? Thank you so much.


    If you want all of the output to be displayed exactly as-is in the
    browser, you don't have to "get" the output with your Perl script at
    all. Just let the C app print to it's standard output, the same as you
    do with your Perl scripts, and the server will take care of getting it
    to the browser for you. If you need to wrap something around the C
    output, then print the first part with Perl, run the C app, and finish
    up by printing the last part with Perl. Only if you want to somehow
    change the output of the C app before sending it to the browser do you
    need to be concerned with it in Perl.

    --
    Greg Schmidt ()
    Trawna Publications (http://www.trawna.com/)
     
    Greg Schmidt, Sep 10, 2003
    #4
  5. [Please don't top-post]
    [Please trim your quotes to a reasonable minimum]

    Abby wrote:
    > [...] After I
    > use system("[program] [parameter]"), how can I get the output


    You don't. Capturing the output of an external program is not what system()
    does.
    For further details and what to do instead please see the third paragraph of
    the documentation for system():

    perldoc -f system

    > and display all of them in webpage?


    Well, you could print them to a file, return them to a CGI server, ....

    jue
     
    Jürgen Exner, Sep 10, 2003
    #5
  6. John Bokma

    John Bokma Guest

    Jürgen Exner wrote:

    [output C program]
    > Well, you could print them to a file, return them to a CGI server, ....


    rather not. Overhead and file locking / temp file creation with all the
    fun associated with this :).

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 10, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    479
  2. Paul
    Replies:
    14
    Views:
    902
    Alexey Smirnov
    Jun 19, 2008
  3. Niraj Ranka

    Execute process even if webpage is closed

    Niraj Ranka, Jun 24, 2010, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    Niraj Ranka
    Jun 24, 2010
  4. sifar
    Replies:
    5
    Views:
    471
  5. soren625
    Replies:
    2
    Views:
    406
    soren625
    Dec 12, 2006
Loading...

Share This Page