including a Perl script in a C executable

Discussion in 'Perl Misc' started by ccc31807, Jan 22, 2013.

  1. ccc31807

    ccc31807 Guest

    I have a small C program that displays file attributes, essentially runningthe stat command and emitting the output to the console.

    This morning, I have a new requirement -- to nicely format the output, printing the field names in CAPS and justifying the field values.

    C isn't my game, and while I probably could (with some effort) read up on reading in system output as strings, munging them, and displaying them, if it's possible I'd rather use Perl. The program must be self contained in oneexe file. The object is to save a manager from knowing anything about the stat command (yeah, I know.)

    Is this doable? Or must I write the entire thing in C?

    Thanks, CC.
    ccc31807, Jan 22, 2013
    #1
    1. Advertising

  2. ccc31807 <> writes:
    > I have a small C program that displays file attributes, essentially
    > running the stat command and emitting the output to the console.
    >
    > This morning, I have a new requirement -- to nicely format the
    > output, printing the field names in CAPS and justifying the field
    > values.
    >
    > C isn't my game, and while I probably could (with some effort) read
    > up on reading in system output as strings, munging them, and
    > displaying them, if it's possible I'd rather use Perl. The program
    > must be self contained in one exe file. The object is to save a
    > manager from knowing anything about the stat command (yeah, I know.)
    >
    > Is this doable?


    You could write a Perl script which generates a C source file from a
    perl script by creating an initialized array of char *s containg the
    lines of the Perl script, short mockup of that:

    ------------------
    print("static char const *script[] = {\n");

    while (<>) {
    chomp;
    s/(["\\])/\\$1/g;
    print("\t\"$_\\n\",\n");
    }

    print("\t0\n};\n");
    -----------------

    If you want to keep things simple, you could then use popen to start
    the perl interpreter with input from a pipe and (assuming the FILE *
    was named perl_in) use a loop a la

    char *cur;

    cur = script;
    while (*cur) {
    fprintf(perl_in, "%s\n", *cur);
    ++cur;
    }
    pclose(perl_in);

    to feed the 'compiled in' script to perl for execution.

    OTOH, why don't you use Perl to determine the the necessary output
    data with the help of stat/fstat (=> perldoc -f -f) and print the
    'nicely formatted' output in one go?
    Rainer Weikusat, Jan 22, 2013
    #2
    1. Advertising

  3. Rainer Weikusat <> writes:

    [...]


    > You could write a Perl script which generates a C source file from a
    > perl script by creating an initialized array of char *s containg the
    > lines of the Perl script, short mockup of that:
    >
    > ------------------
    > print("static char const *script[] = {\n");


    [...]

    > use a loop a la
    >
    > char *cur;


    This needs to be char const **cur ...
    Rainer Weikusat, Jan 22, 2013
    #3
  4. Ben Morrow <> wrote:
    >
    >Quoth ccc31807 <>:
    >> I have a small C program that displays file attributes, essentially
    >> running the stat command and emitting the output to the console.
    >>
    >> This morning, I have a new requirement -- to nicely format the output,
    >> printing the field names in CAPS and justifying the field values.
    >>
    >> C isn't my game, and while I probably could (with some effort) read up
    >> on reading in system output as strings, munging them, and displaying

    >
    >That would be an insane way to do this, in either C or Perl. Both C and
    >Perl have a stat() function, so use that.
    >
    >> them, if it's possible I'd rather use Perl.


    perldoc -f sprintf


    >The program must be self
    >> contained in one exe file. The object is to save a manager from knowing
    >> anything about the stat command (yeah, I know.)


    Teach your manager to select in Windows Explorer (you did mention exe,
    therefore I conclude that you are on Windows) "View" -> "Details";
    right-click on the header bar where it says "Name, Date Modified, Type
    .... " (actual headers will vary), and select "More..." from the context
    menu. Then he can select to display whatever information his heart
    desires.

    jue
    Jürgen Exner, Jan 22, 2013
    #4
  5. ccc31807

    C.DeRykus Guest

    On Tuesday, January 22, 2013 7:11:52 AM UTC-8, ccc31807 wrote:
    > I have a small C program that displays file attributes, essentially running the stat command and emitting the output to the console.
    >
    >
    >
    > This morning, I have a new requirement -- to nicely format the output, printing the field names in CAPS and justifying the field values.
    >
    >
    >
    > C isn't my game, and while I probably could (with some effort) read up onreading in system output as strings, munging them, and displaying them, ifit's possible I'd rather use Perl. The program must be self contained in one exe file. The object is to save a manager from knowing anything about the stat command (yeah, I know.)
    >
    >
    >
    > Is this doable? Or must I write the entire thing in C?
    >
    >


    IIUC (and I'm not at all sure I do), you could
    comment out the existing code in the .exe and
    hack up a replacement to shell out to a runnable
    perl script

    Of course, just adding some formatting to the
    original would probably be easier. But, if you
    want the former and, assuming .exe takes a file
    as its only argument, here's a quick sketch:

    #include <stdio.h>

    int main( int argc, char *argv[] ) {

    // put entire perl script in string
    char *perlsource = "#!/usr/bin/perl\n...";

    // output perl source to file
    FILE *fp;
    char *perlfile = "/tmp/doppelgaenger.pl"; // edit

    // add error checking to open/flose/system
    fp = fopen(perlfile, "w");
    fprintf(fp, "%s\n", perlsource);
    fclose(fp);

    char cmd[100];
    sprintf(cmd, "perl %s %s", perlfile, argv[1] );
    system(cmd);
    }


    --
    Charles DeRykus
    C.DeRykus, Jan 23, 2013
    #5
  6. ccc31807

    ccc31807 Guest

    On Tuesday, January 22, 2013 1:11:18 PM UTC-5, Jürgen Exner wrote:
    >
    > therefore I conclude that you are on Windows) "View" -> "Details";
    > right-click on the header bar where it says "Name, Date Modified, Type
    > ... " (actual headers will vary), and select "More..." from the context
    > menu. Then he can select to display whatever information his heart
    > desires.


    It's a 'manager problem' and you are exactly right. I can't cater to every whim, at least not when I've got work to do otherwise. He can get the information he wants, but not necessarily the format he wants, and we've worked it out.

    Besides, I just wondered if it could be done ... an idle thought that I'm now a little ashamed of posting. I write very little in C, I write a lot of Perl, and I suppose I can blame the question on a neuron misfire thinking that I should be able to invoke a Perl script from C the same way I can invoke an executable in Perl (by system(), e.g.).

    Thanks to all who replied, CC.
    ccc31807, Jan 23, 2013
    #6
  7. ccc31807

    J. Gleixner Guest

    On 01/23/13 10:25, ccc31807 wrote:
    > On Tuesday, January 22, 2013 1:11:18 PM UTC-5, Jürgen Exner wrote:
    >>
    >> therefore I conclude that you are on Windows) "View" -> "Details";
    >> right-click on the header bar where it says "Name, Date Modified, Type
    >> ... " (actual headers will vary), and select "More..." from the context
    >> menu. Then he can select to display whatever information his heart
    >> desires.

    >
    > It's a 'manager problem' and you are exactly right. I can't cater to every whim, at least not when I've got work to do otherwise. He can get the information he wants, but not necessarily the format he wants, and we've worked it out.
    >
    > Besides, I just wondered if it could be done ... an idle thought that I'm now a little ashamed of posting. I write very little in C, I write a lot of Perl, and I suppose I can blame the question on a neuron misfire thinking that I should be able to invoke a Perl script from C the same way I can invoke an executable in Perl (by system(), e.g.).


    C has system(), but that's a dumb way to go since there is printf
    and toupper in C, there's no need to involve Perl. The program is
    already using printf, so it should be a very simple change.

    Do it right. Spend 5-minutes learning now and avoid hours of
    frustration later.
    J. Gleixner, Jan 24, 2013
    #7
    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. dpackwood
    Replies:
    3
    Views:
    1,793
  2. Replies:
    15
    Views:
    488
    Dave Thompson
    Aug 21, 2006
  3. pratap
    Replies:
    20
    Views:
    758
    Flash Gordon
    Mar 7, 2007
  4. Jason Cavett
    Replies:
    8
    Views:
    619
    Sanjay
    May 16, 2007
  5. Replies:
    0
    Views:
    438
Loading...

Share This Page