Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > including a Perl script in a C executable

Reply
Thread Tools

including a Perl script in a C executable

 
 
ccc31807
Guest
Posts: n/a
 
      01-22-2013
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.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      01-22-2013
ccc31807 <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      01-22-2013
Rainer Weikusat <(E-Mail Removed)> 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 ...
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      01-22-2013
Ben Morrow <(E-Mail Removed)> wrote:
>
>Quoth ccc31807 <(E-Mail Removed)>:
>> 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












 
Reply With Quote
 
C.DeRykus
Guest
Posts: n/a
 
      01-23-2013
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

 
Reply With Quote
 
ccc31807
Guest
Posts: n/a
 
      01-23-2013
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.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      01-24-2013
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create executable from executable with py2exe vedrandekovic@gmail.com Python 0 03-29-2008 12:23 PM
Running an Executable Before Executable JAR Jason Cavett Java 8 05-16-2007 07:14 PM
what is fast dynamically linked executable or statically linked executable ?how to decide? pratap C Programming 20 03-07-2007 04:46 AM
How can I run a c executable in pwd ,while the executable is in some other directory vishsid3@gmail.com C Programming 15 08-21-2006 06:04 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments