send CTRL-Z through C++ program - c++

I am trying to write a simple C++ program that sends an SMS message out based on its input from the user. The simple C++ program fails to do the job:
#include<stdio.h>
#include <stdlib.h> /* system, NULL, EXIT_FAILURE */
#include<iostream>
#define CTRL(x) (#x[0]-'a'+1)
using namespace std;
int main()
{
char buffer[128];
sprintf(buffer, "/opt/modemcli AT+CMGC=\"+112345678\"\rTEST%c", CTRL(z));
printf (buffer);
system(buffer);
return 0;
}
modemcli is just a simple C++ program that writes messages to the USB port and reads the response.
modemcli is simple, here is a test:
/opt/modemcli AT
Received AT
OK
My guess is CMGC is not formed properly. The format is:
AT+CMGC="PHONE_NUMBER"<CR>SMS MESSAGE BODY.<Ctrl+z>
Can someone please help me figure this out?

First of all, I think you want to use the AT+CMGS command, not AT+CMGC. Check out the description of each command in 27.005 to see if you really want AT+CMGC. But before reading that document, read all of chapter 5 in V.250, that will teach you all the basics for AT command handling you need.
What you are attempting is impossible to do by using a generic command line program for sending AT command like modemcli or my atinout program. In order to run AT+CMGS on a modem, the program issuing it must have explicitly support for this specific AT command's behaviour.
This is because the need to wait for the "ready to receive" prompt from the modem before sending the payload. See the first part of this answer for details.
I have started working on a companion program to atinout specifically to handle AT+CMGS, but it is not done yet and do not hold your breath, the development is currently on hold.

Related

sending an email with C++ (Linux/Mac)

I was used to use Matlab, and for very long simulation I created a function which sent me an email whenever Matlab finished. It was a very easy Matlab function, you just had to add your email, password and the SMTP (I think).
Now, because of university stuff, I have to use C++ (I'm not very familiar with it, as you have probably guessed) but I can't find an equivalent way for sending an email to myself.
I compile my .cpp in Terminal, using g++.
Can you please help me? I don't know if I miss some libraries or what.
If you want to do this in C++ it would be the best to go for some library like
libquickmail
vmime
If it is ok for you to call some other program (like linux terminal program) go and check this stackoverflow answers send-mail-from-linux-terminal-in-one-line
Using the last method will leave with you with something like that (minimal example):
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[]){
int status;
status = system(R"(echo "this is the body" | mail -s "this is the subject" "to#address")");
return 0;
}
R"()" is c++ string literal so you don't have to care about escape characters (but is available since C++11).
Here see the documentation for system to check how it work.

Piping output from one program to another not working for this particular program

I expect this is a basic question, but I haven't been able to find an answer.
I'm building a web server in C++, and in order to help me visualise the system as it's running I'm building a separate program to do the visualisation. The web server will inform the visualiser of its current state by printing statements to stdout that are piped into the visualiser, which will parse the input and render a nice schematic view of the whole system along with various stats. The visualiser will be written in Python.
To check that I understand how piping works I created two simple programs:
#include <iostream>
using namespace std;
int main() {
cout << "Hello world!\n";
return 0;
}
, and
#include <iostream>
using namespace std;
int main() {
char buf[128];
while (!cin.eof()) {
cin.getline(buf, 128, '\n');
cout << "Received line: " << buf << "\n";
}
return 0;
}
This works as expected when I execute the command
./one | ./two
However, when I run my web server like so:
./aril | ./two
I get no output at all.
Running the server on its own produces output like the following:
Spawning handlers
Waiting for HTTP clients
Server: got connection from 127.0.0.1, port 52168
Connection timed out
(Obviously that isn't actually the kind of output I'll be passing to the visualiser -- it will need a more easily parse-able syntax).
Possibly relevant info:
The web server is divided into two processes: aril and arild. aril runs with root privileges, whereas arild doesn't. Both processes print to stdout using std::cout.
I can't think of any reason why this isn't working.
EDIT:
The solution, it turns out, is simply to explicitly flush the output. Should have been the first thing I tried really..
Your web server is printing in STDERR while in two you are reading from STDIN. So it'll not work.
Change ./aril | ./two to
./aril 2>&1 | ./two
This will redirect all the STDERR and STOUT of aril to STDOUT and thus two will be able to read it.
It is possible that aril detects if its output is piped (see fstat() and this thread http://cboard.cprogramming.com/cplusplus-programming/73909-prevent-piping.html for details) and switches to silent mode, i.e does not produce any output. Try piping to something else, cat maybe, and see if it produces an output.
I think your programs are designed to work as pairs, but not interchangeably. ./one | ./two are an example of piping, but ./aril is expecting to communicate with ./arild via sockets. It probably doesn't make sense to mix ./aril | ./two.

Put the code generated by flex to a normal C++ program

I create a simple file, using flex, it generate a file lex.yy.c, for now, I want to put it to C++ program.
%{
#include < stdio.h>
%}
%%
stop printf("Stop command received\n");
start printf("Start command received\n");
%%
When I type start or stop in command line, there is a output. What I want to do is to give the input by my C++ program, and the output of it should be sent to a variable in my program, is it possible? Thanks a lot!
I know the code I post is quite simple, but the result I imagine is:
create c file by flex and bison, and I use it as a header, so in the c++ program, I just need to call a function lex_yacc() to use it. ex. lex_yacc() is a calculator, so I sent an expression with evariables to this function, and it will return the result. I want to use this function in a C++ program, I am confused...Thanks a lot!
See the section about multiple input buffers in the manual. Especially the section about yy_scan_string and yy_scan_bytes.
For the "output", of course the is "output" when you give "stop" or "start" as input, you explicitly do that (i.e. the printf calls). You can put any code you want there.

Communicating with XBoard (chess engine) (C++/C)

I was just toying around with making a basic chess engine. I was able to get a lot of advice from http://web.archive.org/web/20070704121716/http://www.brucemo.com/compchess/programming/alphabeta.htm, but the real site is down and not all the pages are archived. (Anyone know where to find a full version of Bruce's site?)
But now to the real question: how do I communicate with XBoard? I understand it is via stdin and stdout, but I've been having problems in code. Basically, to get started, I just want to
receive input from XBoard and print it to the console/screen
Give a move of hard-coded input to XBoard and have it make the move
program utility functions and have a random chess ai which chooses random moves.
After that, I can start implementing real things like alpha-beta searching.
I am stuck on the first two things right now. Here is some code I have tried to write/borrowed.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define STR_BUFF 256
using namespace std;
int main (int argc, const char * argv[])
{
char input[STR_BUFF];
char output[STR_BUFF];
while(true){
fflush(stdout);
// read input
if (!fgets(input, STR_BUFF, stdin)){
printf("terminated");
return 0;;
}
printf("%s", input);
}
return 0;
}
I am just getting back into C after about 6 months break and this is the first project that I have used stdin/stdout pipelines to communicate with another program (minus a few basic programs) so I would appreciate any help and any explanations. I know programming a chess engine is a herculean task, but I have already programmed the rules of chess before and what I can find of Bruce's site is absolutely amazing.
You are doing it almost right: get a command from XBoard with fgets, then report a move with printf and fflush. (One thing is wrong, though: you don't need to 'print the command to the console/screen'; you are not communicating with the console/screen; you only read commands from XBoard and send moves back to XBoard).
Probably, it would be easier to start with some existing code. Try to read sources for GNU Chess. Or download sources for any other chess engine, supporting XBoard protocol.
And here is other question with lots of information on chess engine programming: "What are some good resources for writing a chess engine?".
I think that you're looking for pipe(), included in unistd.h. Take a look at Can popen() make bidirectional pipes like pipe() + fork()? for notes on implementation.

Do other tasks while a system() command is being executed

I have this c++ program that is doing a simple ping on a specified ip address. I am not into networking so i'm just using the system() command in c++ to execute the ping from a shell and store the results in a file, that's easy.
The problem is that i want some dots to be printed on the screen while the system() command is being executed. i tried with:
while(system(cmd))
{
//execute the task here
}
but no success. I think that i should create threads or something.
Can you help me ? What exactly i am supposed to do in order to get this done as i want to ?
The problem with system is that it suspends execution until completion. On unix systems you will need to use a sequence of fork, execv, dup2 and pipe. This will allow you to do something more useful. See http://docs.linux.cz/programming/c/unix_examples/execill.html for an example.
You need to create a forked process using fork, like this, and using popen to read the input from the output of the command ping google.com and process it accordingly. There is an interesting guide by Beej on understanding the IPC mechanisms which is included in the code sample below...
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int rv;
FILE *ping;
char buf[2000];
switch(pid = fork()) {
case -1:
perror("fork"); /* something went wrong */
exit(1); /* parent exits */
case 0:
// We're the child
ping = popen("ping google.com", "r");
if (ping != NULL){
fgets(buf, sizeof(buf), ping);
pclose(ping);
rv = 0;
}else{
perror("popen failed");
rv = -1;
}
exit(rv);
default:
// We're the parent...
wait(&rv);
}
// Now process the buffer
return 0;
}
Hope this helps,
Best regards,
Tom.
Edit On consideration, I believe that popen is the way to go with or without output from cmd.
Older
You are probably looking for something in the wait (2) family of commands plus a fork and exec.
From the manual page
The wait() function suspends execution of its calling process until
stat_loc information is available for a terminated child process, or a
signal is received. On return from a successful wait() call, the
stat_loc area contains termination information about the process that
exited as defined below.
Or if cmd returns some progress information you want popen (3) which is discussed in a number of existing SO questions; for instance How can I run an external program from C and parse its output?.
If you are on a unix system, you can start a command with an & to indicate that it runs in the background like this: myscript &
This will start a new process separate from the current program. You need to pick up the process number (hopefully from system, my c posix api knowledge is rusty) and then check up on it probably with a call to something like wait or waitpid with non-blocking turned on.
This is complicated for a beginner -- I'll let someone else post details if still interested.