Standard input/output in Pyclewn(GDB front end for vim) - c++

I've just installed Pyclewn. It works and shows variables and etc. But it doesn't show my program's output and when my program wants to input something, it doesn't do anything(I can write ":C run output" and it works. but not with standard I/O.
There was something in its documentation: http://pyclewn.sourceforge.net/_static/pyclewn.html
But I didn't understand what it says.
P.S: I've done that. Now I want to map for example to run those commands. but because the "nn" in /dev/pts/nn may vary, I should manually enter the number(see it from the xterm opened). I also have another problem when I map a key to a sequence of gdb commands, it says gdb is busy, I can add ":sleep 100m" between commands and the problem will be soved. but in the documentation it says that I should enable async option. but when I run pyclewn from vim with :Pyclewn command I don't know how to enable the async option.

You should use inferior_tty.py to create a terminal to be used with the program being debugged.
Abridged summary (most relevant bits only) from the FAQ:
:Cshell setsid xterm -e inferior_tty.py &
Determine what the name of the tty to be used is from this newly spawned window, then:
:Cset inferior-tty /dev/pts/nn

Or just start pyclewn from a terminal and it will automatically grab that terminal for input and output.
E.g:
pyclewn -c "main.cc other.h other.cc"

Related

Is there a way to open a new console from a program then cout into it?

I'm trying to have a separate console window for my program that is already in a console. How would I be able to open this new console window then output to that specific console?
I've found ways to do it that work in windows using "cconsolelogger", but not for Linux.
I assume that by "new console", you mean a terminal emulator window.
A terminal is a program like any other, so you start it like any other program. The only standard way in C++ to open another program is std::system, which executes a shell command. Here is an example of opening a terminal emulator:
std::system("xterm");
Note that it is not safe to pass arbitrary user provided input into the command, because it is vulnerable to shell injection.
The POSIX standard - that is followed by Linux operating systems in general - provides other, lower level tools to run another executable. In particular the exec family of functions allows executing another program without starting a sub process and without involving the shell.
Maybe you can use popen.
You can choose the program to write command to it, for example:
gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w");
fprintf(gp, "set term png\n");

Handling colored bash prompt

I have an adapter code which executes some specific commands on a Linux machine via SSH. It executes commands and handles the output of each command in a specified manner.
The issue being faced is very straightforward.
I am connecting to a Bash prompt that uses colors. Here are the details of the PS1 variable:
PS1="\[\033[1;34m\][\$(date +%H%M)][\u#\h:\w]$\[^[[0m}\]"
The code uses regex which decides a valid terminal first and then proceeds with the commands.
For example, .*\$ is considered valid
I can't seem to handle [^[[0m} properly and code is not able to identify the terminal as a valid one. If I remove it from PS1 variable, things work fine.
Can anyone please suggest what regular expressions might be entered so that the code takes it as a valid terminal?
I think the PS1 variable you provided above is broken.
I ran the following experiment in my terminal window:
[guest#localhost ~] $ export PS1="\\[\033[1;34m\\][\$(date +%H%M)][\u#\h:\w]$\\[^[[0m}\\]"
[2137][guest#localhost:~]$^[[0m}
As you can see ^[[0m} is not interpreted properly. I assume it is the end of the color in the prompt. I think it should be \\[\033[0m\\].
Here is the whole PS1 variable:
PS1="\\[\033[1;34m\\][\$(date +%H%M)][\u#\h:\w]$\\[\033[0m\\]"
After testing it you get this:
[2137][guest#localhost:~]$^[[0m}export PS1="\\[\033[1;34m\\][\$(date +%H%M)][\u#\h:\w]$\\[\033[0m\\]"
[2139][guest#localhost:~]$

system() executes shell command differently C++

I need to run this shell command in a C++ script:
"/usr/local/bin/mjpg_streamer -i "/usr/local/lib/input_uvc.so" -o "/usr/local/lib/output_http.so –w /usr/local/www" -b"
This command launches an application which broadcasts a video feed. When I execute this command via system() in C++ the application doesn't start properly.
I use:
system("/usr/local/bin/mjpg_streamer -i \"/usr/local/lib/input_uvc.so\" -o \"/usr/local/lib/output_http.so –w /usr/local/www\" -b");
When I try to access the video stream after I started it with the C++ application the webpage returns:
501: Not Implemented!
no www-folder configured
I can't expect you guys to give me an application related solution, but I'm wondering if there's a difference in the way commands from a C++ application using system() and commands directly entered in a terminal are executed.
EDIT: The application broadcasts the video stream on IP:8080. I access it by going to that IP in my browser. Usually it opens a webpage with the stream in it but when I execute the command with the C++ application I get that error.
Edit: The old idea of mis-placed quotes was wrong; I realize that -w is actually an option to output_http.so, so the whole shebang must be passed as a single parameter to the -o option, as shown here or here etc.
In that case, check file permissions etc. Does /usr/local/www exist? Is it possible that you are running the shell command from a root shell?
Hey, I have a book recommendation, too, "one of the best tech books ever published": Stevens' Advanced Programming in the Unix Environment. The guy knows -- sorry: knew -- what he was talking about.
I would avoid using the system(3) library function, or at the very least, check its returning error code. I don't understand why you are using " inside your command (I believe that in your particular case, you don't need them; but in general beware of code injection!). Read about globbing
You could use popen(3) to at least get the output of the command.
Even better, code yourself the running of the mjpg_streamer program using the fork(2) & execve(2) & waitpid(2) and other syscalls(2) (perhaps pipe(2), poll(2), dup2(2) etc...). Read Advanced Lnux Programming for more.

Open the terminal and execute commands via C programming

Does someone know how to open the terminal and execute several commands using a C program ?
I have a program in C and another sets of commands executed by the terminal. I need to combine them into one program in C.
I'm using Ubuntu 10.04.
Thanks!
Your question may be somewhat misleading.
Because you want to run all the terminal commands in the c-code, perhaps you actually have only textual input / output with these commands. If so, you probably do not need the terminal.
I use popen when the output of the (terminal) program is a text stream. It is probably the easiest to use. As an example:
...
const char* cmndStr = "ls -lsa";
FILE* pipe = popen(cmndStr, "r");
...
The popen instruction executes the command in the cmndStr, and any text written to the commands (ls -lsa) standard output, is redirected into the pipe, which is then available for your C program to read in.
popen opens a separate process (but without a terminal to work in, just the pipe)
'Fork' is another way to launch a separate process, with some control over the launched processes' std i/o, but again, I think not a terminal.
On the other hand, if your output is not a simple text stream, maybe you can get by with a output-only dedicated terminal screen to accommodate special output activity. For instance, when I work with ncurses:
I manually open a terminal in the conventional way, and in the terminal
issue the command "tty" to find out the device name, and
issue a "cd" to set the focus to the working dir.
dmoen#C5:~$ tty
/dev/pts/1
dmoen#C5:~$ cd work
dmoen#C5:~/work$
Then I start my program (in a different tty), and let the program know which device I want it to use for the special output (i.e. /dev/pts/1 ) ... I typically use command line parameters to tell my program which pts or extra terminals I want it to use, but environment variables, pipes, in/out redirection, and other choices exist.
I have not tried (lately) to launch a terminal (as suggested by smrt28), except in shell. I believe this will work, but I do not see how the output from the terminal command (ls in the example) would be delivered back to your program. popen trivially delivers a text stream.
A long time ago, I used a device called 'pty' which works like a terminal, but I don't remember how to connect it usefully.
There is a set of 'exec' commands ... see man exec. To connect them back to your program, you will probably work with files, or perhaps redirecting i/o. Too many choices to list here.
And also, maybe you can connect these commands with your c program using shell pipes.
Check "man xterm", parameter -e. Then, in C, you can:
system("xterm -e ls")

C++ Program taking control of terminal window

When using vim in the terminal, it essentially blanks out the terminal's window and gives you a new one to start coding in, yet when you exit vim the terminal's previous output is still listed. How do you clear the terminal so that it only outputs your program's output, but returns to its normal state once the process has ended? (In linux, fedora)
At the low level, you send the terminal program a set of control characters that tell it what to do. This can be a bit too complex to to manage manually.
So instead, you might want to look at a console library like ncurses, which can manage all this complexity for you.
With respect specifically to the previous content magically appearing after the program exits, that's actually an xterm feature which vim is taking advantage of and which most modern terminals support. It's called "alternate screen" or simply "altscreen". Essentially you tell the terminal program "Ok, now switch to a completely new screen, we'll come back to the other one later".
The command to switch to the alternate screen is typically \E[?47h, while the command to switch back is \E[?47l For fun try this:
echo -e "\033[?47h"
and then to switch back:
echo -e "\033[?47l"
Or for a more more complete solution which relies a bit less on your shell to set things right (these are the sequences vim normally uses):
echo -e "\0337\033[?47h" # Save cursor position & switch to alternate screen
# do whatever
#Clear alternate screen, switch back to primary, restore cursor
echo -e "\033[2J\033[?47l\0338"
You can type "clear", or add a system command in your program to call "clear". Also, if you're not aware, you can run system commands from inside vim, so you dont have to exit and type clear. You can compile and run your programs from inside vim as well, for example ->
:!clear
:!make
:!./programName
Also, I never use this technique, but I believe you can have vim call a new terminal by using :set terminal