0

I was wondering if you could have it so when you go and click on a program in linux it always automatically brings up the command line for the information being displayed or if I decided to use ncurses for an interface. If so is this a system specific call or can you do this with ncurses? Because half of my program is going to be via terminal.

Thanks

Yoshi65
  • 23
  • 2
  • In short, you want a command-line interface? – Potatoswatter Apr 10 '11 at 01:22
  • Yes. Displayed automatically when you open the program. Like a normal terminal. – Yoshi65 Apr 10 '11 at 03:56
  • 2
    Most Linux users would just expect to start a terminal program from the terminal itself. Clicking an application and having the terminal start is a bit unusual. Consider that there are a great number of terminal programs and every user will have their preference. – edA-qa mort-ora-y Apr 10 '11 at 05:23

3 Answers3

1

Since nitt wouldn't let me amend his code snippet, I'm posting a corrected snippet in case anyone would like to use it:

#include <cstdio>
#include <unistd.h>
#include <iostream>

int main(int argc, char* argv[])
{
    if (isatty(0))
    {
        std::cout << "Hello, World!" << std::endl;

        for (int i=0; i<argc; i++)
            std::cout << "arg: " << i << "\t" << argv[i] << std::endl;

        std::cout << "Press return to continue . . ." << std::flush;
        std::cin.get();
    }
    else
    {
        const char* args[argc+3], **it=args;
        *it++ = "gnome-terminal";
        *it++ = "-x";
        it = std::copy(argv, argv+argc, it);
        *it++ = 0;

        if (-1 == execvp("gnome-terminal", (char* const*) &args[0]))
            perror("exec");
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • You'd have done better by using `isatty(0)` as your main test. – Donal Fellows Oct 04 '11 at 13:53
  • @DonalFellows: thanks for the suggestion. I have updated the code - now forwarding all the command line arguments transparently as well - yay! – sehe Oct 04 '11 at 20:17
  • +1: That's looking quite good now. Minor nit: you don't need to check for -1 out of execvp() as that never returns on success. – Donal Fellows Oct 05 '11 at 13:07
  • @DonalFellows: thank you for your continued interest. I like that. It may be a minor nit, but I'm sharpening my saw here, and all these tidbits just hone my linux system programming skills :) cheers – sehe Oct 05 '11 at 13:11
  • You've gone the other way now. :-) execvp() _can_ fail (e.g., if it can't find the program) in which case it will return. It just never returns _successfully_. – Donal Fellows Oct 05 '11 at 14:56
  • @DonalFellows: no problem, I'll add the error check back in. FYI, the execvp was already there while you suggested the non-return :) – sehe Oct 05 '11 at 15:04
0

Yes, just invoke a terminal with your app in it. For example:

rxvt -e myapp

Starts a terminal running your app. You could also use xterm. If you want to use wide chars/unicode I recommend rxvt-unicode.

You can put this in a .desktop file with an icon defined there, and then that will be placed in the system menu.

Keith
  • 42,110
  • 11
  • 57
  • 76
-1
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int fileExists(string x321) {
ifstream x123 (x321.c_str());
string x213;
    x123 >> x213;
    if (x213 == "") {
        return false;
    } else {
        return true;
    }   

}

int createConsole(string fname) {
    if (fileExists("~tmp") == false) {
        ofstream tmp ("~tmp");
        tmp << "tmpfile";
        fname = "gnome-terminal -e " + fname;
        system(fname.c_str());
            system("exit");
        return 0;
    }
    remove("~tmp");
    return 1;
}

int main(int argc, char** args) {
            createConsole(args[0]);
        cout << "Hello, World!" << endl;
        cout << "Press return to continue . . .";
        cin.get();

}

Pay attention to the "createConsole" and "fileExists" function. I wrote this myself.

nitt
  • 1
  • 1
  • 1
    Hi nitt, welcome to Stack overflow! I hope I didn't scare you off with my edits. On SO it is not uncommon that errors get fixed by the community. If you agree, I can still edit your snippet, in which case I'll no longer have to downvote it because it doesn't work :) Just let me know, cheers (in the meantime I have posted a working snippet as a separate answer) – sehe Oct 03 '11 at 01:00