including a Perl script in a C executable

C

ccc31807

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.
 
R

Rainer Weikusat

ccc31807 said:
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?
 
R

Rainer Weikusat

[...]

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 ...
 
J

Jürgen Exner

Ben Morrow said:
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.

perldoc -f sprintf

The program must be self

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
 
C

C.DeRykus

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);
}
 
C

ccc31807

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.
 
J

J. Gleixner

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top