tags:

views:

155

answers:

5

I have 3 questions. I am making a C++ executable to launch a Perl program I made. I will compile it for Winows, Mac OSX and Linux. It's pretty much just: system("perl progam.pl");

  1. When compiled with Mac OSX, the program starts in ~. How would I get it to start in the dir it was launched from, or is it just a problem with the compiler?

  2. I'm using - echo -n -e "\033[0;Program\007" - in an attempt to make the windows title "Program". Is this is best way?

  3. I'm using - echo -n -e "\033[7;30;47m" - to make the background of the window black. Is this the best way?

Thanks.

+3  A: 
  1. This sounds like something Finder is doing. Launching the app from a shell should work as you expect.
  2. Use tput
  3. See answer to 2, above.
Ignacio Vazquez-Abrams
Is tput only for Mac OSX? What would I used for Linux-like systems?
Blaise Roth
You would use `tput`, of course. http://linux.die.net/man/1/tput
Ignacio Vazquez-Abrams
Sorry for the lateness of this response (I was away for the weekend), but I don't exactly understand how tput can change the title or background colour of the Terminal...
Blaise Roth
The `terminfo(5)` man page describes all the codes available for `tput`. `echo "$(tput setb 4)"'Hello, world!'"$(tput sgr0)"`
Ignacio Vazquez-Abrams
A: 
  1. On Mac OS/Unix, invoking system does not change the current working dir. When executing program.pl the current working directory is the same from which you executed the C++ executable. When you launch the executable using Launch Services (e.g. the Finder) the working directory should be /.
Nikolai Ruhe
A: 

On #1 you can refer to the current directory with ./ so system("perl ./progam.pl"); should do it assuming both scripts are sitting in the same folder. ../program.pl would be one level higher.

Joshua Levitsky
A: 

For #1, use getcwd & then pass an explicit path to system:

cwd=getcwd(NULL, PATH_MAX);
sprintf(cmd, "perl %s/program.pl", cwd);
system(cmd);
free(cwd);

If your perl program itself relies on a specific working directory, then do this instead:

sprintf(cmd, "cd %s && perl program.pl", cwd);
pra
A: 

This is probably a silly question, but why are you making an application to launch a perl script? Just add the following to the top of your perl script and use "chmod a+x" to make it executable:

#! /usr/bin/perl

When you use the system command from C and C++, you are basically launching the default system shell and executing the given command in that shell. Doing that is not very portable and somewhat defeats the purpose of using C or C++ (since you could simply create a shell script that does the same thing). If you want to actually do this with C++, you should probably use popen or fork+exec to launch perl. Generally speaking, it isn't nice to end users to play with their Terminal in the manner that you have proposed; most users, by default, have the Terminal configured to display the most recently executed command or their current directory or some other information of their choosing, and changing that is -- on UNIX systems such as Mac OS X and Linux -- considered improper etiquitte. If you are trying to create a terminal interface, though, you might want to look at the curses library.

Michael Aaron Safyan