Is it possible to copy the output of linux terminal with the keyboard only - debian-based

I am trying to write a macro to connect to my school's workstations. I am trying to copy a single number from the output of another command. For example, the output is something like:
vnc viewer: -via username#address :5, where the 5 is the port. Is it possible to copy the number 5 from just keyboard inputs? Or, alternatively, is it possible to copy the output of a command to the clipboard so I can paste it somewhere else?

Related

Access watch variables from trace32 scripting language

I'd like to create a diagnosis script and would like somehow to get all the variables the user inputs in a watch window to a script. How may i access the watch variables and manipulate them?
I tried with a DIALOG.view but that wastes too much time. There might be another trace command but I don't know it. Thank you!
Getting the content of open Var.Watch windows from script is not directly supported in TRACE32.
However you can do the following in you script
Redirect printing to a file PRinTer.FILE "~~~/winpage.txt" ASCIIE (of course you can choose any other filename instead of winpage.txt)
The window WinPAGE.List shows you all open child windows. With WinPrint.WinPAGE.List you can sent the list of all open windows to the file specified before (winpage.txt).
Now parse the content of winpage.txt for the names of the windows, which are a watch windows. The window names start by default with a capital 'W' followed by three decimal digits (but can also be totally different) and are then followed by the command (in round brackets) which was used to open the windows. Compare case insensitive!
The watch windows have a command which starts with:
B::Var.Watch
B::V.Watch
B::Var.W
B::V.W
Redirect printing to a new file e.g. PRinTer.OPEN "~~~/varwatch.txt" ASCIIE
Send the content of each open watch window to the file varwatch.txt with the command WinPRT <window name>. The relevant window names you've got from step 3. Execute WinPrt for each open watch window.
Close varwatch.txt with PRinTer.CLOSE
Now you should have the content of all open watch-windows in the file varwatch.txt.
Other idea:
Use command STOre "mywindows.cmm" Win to save commands to create all open windows to a script.
Parse this script for all lines starting with Var.Watch (or one of it's shorts forms) and the lines starting with Var.AddWatch (or one of it's shorts forms). Parse case insensitive!. The arguments followed by Var.Watch or Var.AddWatch are the variables currently shown in the watch windows.

How do I copy everything from my terminal to a file including the stdout and the prompt using C?

I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?
Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.
Can't comment cause of low reputation.
I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is.
In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.
Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script
And that's all you want to capture, I think.
Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.

C: open second custom sized terminal and use it for output

I am writing a program in C that is started simply by the terminal. Now I want to make the program itself open another terminal with a custom size and write its output in there.
I found the command
system("gnome-terminal");
which opens another terminal, but I can't find a function that lets me write into this second terminal. I am using Ubuntu.
If you have any idea, that would be great
The easiest thing would probably be to write the output to a file, say /tmp/tmp96888 (tip: mkstemp) and then do something like
system("gnome-terminal --geometry=40x14 --command 'less /tmp/tmp96888'");
Or, to update continuously from the file:
system("gnome-terminal --geometry=40x14 --command 'tail -f /tmp/tmp96888'");
But if you can, I think the best way is to open a new terminal and run the program itself in it, and just print the output. It's only if you actually need to do things in the original window that you have to bother with a separate output window.

Retrieve data from COM port using a batch file

I'm trying to automatically retrieve data from a COM port using a batch file.
I'm able to configure the com port and to send the command in other to ask my device for the info.
The problem is that I'm not able to capture the data that the device sends. I've tried with RealTerm and the device is working and sends the info back to the pc, but I really need the batch file to do it automatically, here is the code:
echo off
MODE COMxx ...
COPY retrievecommand.txt \\\\.\COMxx:
COPY \\\\.\COMxx: data.txt
Any suggestions?
Use the TYPE command in a recursive loop using the DOS GOTO command to a DOS LABEL. Use 'append output' to capture text like TYPE COM1:>>Data.txt The double > means continually concatenate (or append) to Data.txt. A single > or 'redirect output' would replace the text in Data.txt every loop (if com data present on port). Add a 2nd line that redirects to the monitor screen so you can watch activity too (i.e. TYPE COM1:>CON [CON means console or monitor screen but you can omit it as console is default anyway])
Control-Z is not needed by TYPE command. It will just dump text continually until operator does a Control-C and then a Y to break the loop. You really don't need to stop the loop unless you are done with the batch file all together. The Data.txt file will be available to other programs live and will not present a 'Sharing Violation' if you try to access it with another program like NOTEPAD.EXE while this batch file is still looping.
Also if you make a 3rd line in the batch file that says TYPE COM1:>Data1.txt [notice only one redirect], you will have a single line of instant text that will disappear with next iteration. But sometimes that is helpful if you need only one line of data. There are creative ways to extract one line of data to another text file using the DOS FIND command.
When reading, the COPY command will continue until it detects the end of file. As the source is a device (with a potentially infinite stream) it only knows to stop when it detects an end of file marker. This is the Ctrl-Z (0x1A) character.
The suggestion in the duplicate question of using the TYPE command to read is likely to result in the same problem.
There is no standard mechanism to read a single line. If you can port your application to PowerShell, you should be able to read single lines with the results you expect.

output to a new terminal

Suppose I create a thread from my "main" thread, which aims to monitor and print some variables in "main". But "main" is also generating some output, so I want to print the outputs of these two threads separately. How can I redirect the output of the new thread to a new console other than the one "main" thread is using in my c program?
I would output the monitoring information to a file, then use tail -f filename to display it in my other terminal.
That has the advantage that you can run grep or what-have-you on the output as well.
I would go with writing the data to a log file and then using tail -f log_file.txt from another window to watch it since that way you get to keep a copy of the data, but if you decide that you need to use separate terminals then you can just open the new terminal (tty) like a regular file. The main problem with this is that you probably really want it to open a new terminal window for you as well.
Opening a new terminal window to do something like this is tricky and is different between different terminal emulators. Here is another question about opening new terminals from a make file. You could try doing the same thing from your program or from a script that runs your program and run cat or tail in the new terminal to be your log window.
#!/bin/sh
truncate --size=0 ./logfile.txt
xterm "tail -f ./logfile.txt" 2>&1 > /dev/null &
your_program --log-file=./logfile.txt
I don't currently know of a better way to accomplish this.
Another thing you might want to look into is syslog.
What would you expect to happen in such case (externally)?
If you run a program you are attached to a console. You of course don't have to write output to the console (you can use files), but the console cannot be duplicated obviously.
Isn't using a different file descriptor good enough for you?

Resources