My application is basically a shell which expects an input of type cmd [x], where cmd is constant and x is optional. So cmd 1 is legal as well as cmd by itself - then I assume a default parameter for x.
I am doing this:
char cmd[64];
scanf("%s", cmd);
int arg;
scanf("%d", &arg); // but this should be optional
How can I read the integer parameter, and set it to a default if none is currently available in the prompt? I do not want the prompt to wait for additional input if it was not given in the original command.
I tried several versions using fgetc() and getchar() and comparing them to EOF but to no avail. Each version I tried ends up waiting on that optional integer parameter.
The easy way:
char b[73]; //powers of 2 magic is evil.
if(fgets(b,sizeof b,stdin) != NULL) {
char cmd[59]; //59, no command will ever be > 58. Ever I say.
int arg;
if(sscanf(b,"%58s %d",cmd,&arg) == 2) {
//handle cmd and arg
} else if(sscanf(b,"%58s",cmd) == 1) {
//handle cmd only
} else {
// :-/
}
}
Simple answer, you can't. The C runtime takes input from the OS, but doesn't control it. To do something like this you will need to interact directly with the OS using platform specific APIs.
Are you reading line-by-line? Can't you just read the whole command until you reach a "\n" (newline)? If you get two tokens before the newline, it is a command and the argument; if you read only one, it is the command only and you set the second argument to the default.
Here's a program that works (sorry my previous answer was made in haste).
int main(){
char cmd[100], line[100];
int man = 0;
printf("OK: ");
fgets(line, 100, stdin);
int num = sscanf(line,"%s %d",cmd,&man);
if (num==1)printf("one! %s %d\n", cmd, man);
else if (num==2)printf("two! %s %d\n", cmd, man);
}
fgets reads the line (with bounds checking), and sscanf will assess whether one or two tokens were entered.
Related
I am writing an interactive REPL program in c.
Some examples of commands (lines starting with >) I would like to handle are:
$ ./my_program // run the program
> add user
id: 123 // this is the output of above command
> update user 123 name "somename"
> remove user 123
> quit
So basically the command is a line with multiple strings.
This is how I am trying to handle the commands.
scan the whole line
parse the command and get a corresponding int value unique to command
do whatever needs to be done for the command
#include <stdio.h>
int parse_cmd(const char *buffer)
{
// parse command
}
int main(int argc, const char **argv)
{
// init code
char buffer[100];
int cmd;
while (1) {
printf("> ");
scanf("%[^\n]%*c", buffer);
cmd = parse_cmd(buffer);
if (cmd < 0) {
printf("error: invalid command\n");
continue;
}
switch (cmd) {
// handle commands
}
}
// deinit code
}
There are a lot of cli programs I have seen that take command inputs in similar way.
I wonder if there is a general way of writing cli programs?
I can write code to parse the commands, just wanted to know the standard approach for such situations?
While there's no real standard way, quite a lot of opensource console tools with an interactive mode use the GNU readline library (https://tiswww.case.edu/php/chet/readline/rltop.html).
It's actually quite easy to use, even simpler than implementing everything 100% correctly by yourself.
Your example rebased on readline:
int main(int argc, const char **argv)
{
// init code
int cmd;
char* line;
while (1) {
line = readline("> ");
if (line) {
cmd = parse_cmd(line);
switch (cmd) {
// handle commands
default:
printf("error: invalid command\n");
}
free(line);
} else {
break;
}
}
// deinit code
}
This isn't any more complex than your example, but you immediately gain:
command line editing at the interactive prompt, with correct handling of each and every possible terminal
correct handling of EOF (important if stdin is redirected)
unlimited input line size
And it's not very hard to add a command history, with arrow-up and down to repeat previous lines, incremental search, optionally persisted to a file, et et.
There's not really a standard way to do it. This is not a 100% fair comparison, but your question is kind of like if there is a standard way to construct a compiler, because you are in fact constructing a language, although a very simple one.
But one reasonably common way that works fairly well for simple programs is this approach. Let's assume that we have two commands add and del. Create a function for both these commands. First we search for one of the strings "add " or "del ". Notice the spaces. Put a pointer on the next character and call the corresponding function with the rest of the line as argument and allow them to determine things.
Here is some pseudo:
parse(bufferptr)
word = getFirstWord(bufferptr)
ptr = bufferptr + strlen(word)
if word == "add"
return add(ptr)
else if word == "del"
return del(ptr)
return -1
add(bufferptr)
word = getFirstWord(bufferptr)
if userExist(word)
return -1
else
return addUser(word)
del(bufferptr)
word = getFirstWord(bufferptr)
if not userExist(word)
return -1
else
return delUser(word)
buffer = input()
int res = parse(buffer)
I have a binary file which prints the result instead of returning the value, if I execute it using cmd I am getting printed text, I managed to execute it from C code but it seems like I can not get the text it usually prints to be stored in a variable I can use later for further decisions.
I do not have that much of experience in C and I googled a lot.
I came across the idea of using clip but my cmd is saying that clip command can not be found.
any help or ideas would be appreciated.
The correct function pair to use on POSIX systems is popen() and
pclose(). You can perhaps use Microsoft's _popen() and
_pclose() unless the warning 'This API cannot be used in applications that execute in the Windows Runtime' matters to you.
You would use it more or less like this. I've had to invent the name of the command you wish to execute since the question doesn't specify that. I chose ./example.exe as the name — and I'm assuming it needs no arguments.
char cmd[] = "./example.exe";
FILE *fp = popen(cmd, "r");
if (fp != NULL)
{
char buffer[4096];
size_t nbytes;
while ((nbytes = fread(buffer, sizeof(buffer), sizeof(char), fp)) != 0)
{
…process nbytes of data…
…it is not a null-terminated string unless you add the null byte…
}
pclose(fp);
}
else
{
…report error for failure to execute command…
}
You can use the system function from <stdlib.h> to run the command you want. To get the command's output, you modify your command like in this question to save the command's output to a file. Then you can use the file I/O functions in <stdio.h> to process the command output.
In Linux, you may do command substitution and pass its result as arguments to the program, Something like this
./your_program "$(/path/to/your/binary/file)"
Suppose your main is
int main(int argc,char* argv[]){
.
.
return 0;
}
Acess the arguments like argv[1] and so.
Here the $(command) does the substitution and it passes the printed values from the binary as arguments to the pgm. Hope this helps.
Use snprintf function. For e.g.
snprintf(cmdbuff, BUFFER_LEN, "dmidecode --type 17 | grep -i Size | grep -o '\\<[0-9]*\\>' | paste -sd+ | bc");
Here cmdbuff is character array where command will be stored , BUFFER_LEN is a size of the character array
Then use popen and fgets to get the output of command into some buffer as shown below
if((fd = popen(cmdbuff,"r")) != NULL)
{
fgets(buffer, BUFFER_LEN, fd);
sprintf(vnfc_configured_memory, "%s", buffer);
vnfc_configured_totalRAM = atof(vnfc_configured_memory);
}
Is there a clean way to open a file like this without system calls:
ID*_LogConfig.csv
I tried the following but it didn't worked.
/*Read setup file*/
getcwd(cwd, sizeof(cwd));
snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);
if( NULL == (input = fopen(source,"r")))
{
snprintf(errbuffer,sizeof(errbuffer), "Could not open file %s - check existence/rights\n", source);
exitHandler(1, errbuffer);
}
It outputs:
/mnt/dataflash/canfilter.d/ID*_LogConfig.csv not found
But with e.g. cat /mnt/dataflash/canfilter.d/ID*_LogConfig.csv it shows the file content.
My compromise solution would be a system call ll ID*_LogConfig.csv and using the output as filename.
This line
snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);
likely does not produce what you think it does.
The %*d portion is an format specifier with a field-width, per the POSIX printf() documentation
A field width, or precision, or both, may be indicated by an
( '*' ). In this case an argument of type int supplies the
field width or precision. Applications shall ensure that arguments
specifying field width, or precision, or both appear in that order
before the argument, if any, to be converted. A negative field width
is taken as a '-' flag followed by a positive field width. A negative
precision is taken as if the precision were omitted. In format strings
containing the "%n$" form of a conversion specification, a field width
or precision may be indicated by the sequence "*m$", where m is a
decimal integer in the range [1,{NL_ARGMAX}] giving the position in
the argument list (after the format argument) of an integer argument
containing the field width or precision, for example:
printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);
So, this line
snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);
expects two more integer arguments to be passed. Since you don't pass them, you invoke undefined behavior.
See this answer: https://stackoverflow.com/a/19897395/4756299
Is there a clean way to open a file like this without system calls
No.
fopen() makes use of a system call. You cannot 'open' a file without a system call.
If you're referring to the system(3) function, then you're probably in for some pain - it's best to avoid it if possible, from a performance, reliability and security point of view.
If you want to open 'all files that match the pattern', then look at glob(3), which is likely what your shell is using to handle such wildcards.
You will need to iterate over each of the resulting paths, calling fopen(), fread() and fclose() on each.
Example usage of glob(3):
#include <stdio.h>
#include <glob.h>
void main(void) {
int ret;
int i;
glob_t glob_info;
ret = glob("*.csv", 0, NULL, &glob_info);
if (ret != 0)
{
fprintf(stderr, "glob() failed: %d\n", ret);
return;
}
for (i = 0; i < glob_info.gl_pathc; i++) {
printf("%d: %s\n", i, glob_info.gl_pathv[i]);
}
globfree(&glob_info);
}
It is not really a good idea to open lots of files and treat the stream as a single 'thing' (as you are doing with your cat example).
As #Andrew has pointed out, you must be careful with your use of printf() format strings...
You have provided the following: %s/ID%*d_LogConfig.csv. A % denotes the beginning of a format specifier, you have thus given the following:
%s - a char * (string) parameter follows
%*d - similar to %d, but the * means that the precision is provided as an int parameter, followed by the number itself.
For example:
printf(">%s< >%*d<\n", "Hello", 5, 3);
Will output: (note the 5 characters that the %d outputs)
>Hello< > 3<
If you are after a *, then just put a * in the format string.
If you are after a %, then you need to escape the % but putting %% in the format string.
Ok I solved the "problem" by using the following:
(Processing the output of ls -t and use the newest file as config-file)
/*Search for config-file*/
FILE *file_config;
file_config = popen("ls -t ID*_LogConfig.csv","r");
if (file_config == NULL) {
exitHandler(1, "Error opening date pipe.");
}
fgets(configfile, sizeof(configfile), file_config);
if (strlen(configfile) > 0)
configfile[strlen(configfile)-1] = '\0';
else {
exitHandler(1, "Could not find a ID*_LogConfig.csv\n");
}
getcwd(cwd, sizeof(cwd));
snprintf(source, sizeof(source),"%s/%s",cwd,configfile);
/*Read setup file*/
if( NULL == (input = fopen(source,"r")))
{
snprintf(errbuffer,sizeof(errbuffer), "Could not open file |%s| - check existence/rights\n", source);
exitHandler(1, errbuffer);
}
It seems that this is the only simple way.
Thanks to all.
I'm reading from a file descriptor (fp) to get my commands from a client to my TCP server. The current way to get the command looks something like this
char cmd[21];
while ( fscanf( fp, "%s", cmd ) == 1 && strcmp( cmd, "quit" ) != 0 ) {
//do work
}
My issue is that one of the commands can be "move" which is followed by two integers. I know that I can do something like
char cmd[21];
int row = 0;
int col = 0;
fscanf( fp, "%s%d%d", cmd, &row, &col );
but I can't do that unless I know that the cmd is "move". Is there a way that I can get the command string, check for "move" and then get the integers?
You can certainly do that. The trick is to wait until you know what kind of command you are processing.
At some point in your code your program would need to do something like this:
if (strcmp(cmd, "move") == 0) {
...
}
This is a good place to read your optional parameters for the move command:
fscanf( fp, "%d%d", &row, &col );
Note: It is not safe to use %s, no matter how big a buffer you allocate. You should always specify a limit for the number of characters your command buffer is prepared to accept, like this:
fscanf( fp, "%20s", cmd )
// ^^
You can't easily do it exactly as you've outlined above, but you could do something like:
if (2 == fscanf(fp, "move %d %d", &row, &col))
move(row, col);
// possibly similar checks for other commands here.
else if (fscanf(fp, "quit"))
quit();
If the commands you need to support are all known when you write the code, this can be easier/simpler than reading the command, sorting out which command you have, then reading parameters appropriate for that command.
Conversely, if you might (for example) have some sort of plug-ins that add new commands to be supported, then you probably want to read the command, check it against some collection of installed commands (e.g., a hash table) and use that to look up how to process that command (e.g., at least find a number signifying the number of parameters to read for that command, and a function to which to pass those parameters for processing).
In the while loop body:
if(strcmp(cmd, "move") == 0) {
fscanf(fp, "%d%d", &row, &col);
}
Note that your code as it is written now is not safe, due to the fact that a buffer overrun will occur if the command is longer than 20 characters. You may wish to use, for example, "%20s" as your format string, to limit the length of the command.
Alternatively, you can use fgets() to get a line of command, then parse it afterwards.
I am trying to simulate a shell terminal in c, one of the functionalities is to be provide a simple memory to remember the last command executed. So how I am going about is:
Every time the user enters a command (String) the string is saved in a file (command_histroy.txt)
If the user enters "r" (command=="r"), the terminal calls the function getSavedCommand(), as I am only saving only one command so my function is:
char* getSavedCommand(void){
char cmd[1000];
int i=0;
char* filename = "files/command_history.txt";
FILE* file = fopen(filename,"r");
if(file!=NULL){
int c;
do{
c = fgetc(file);
cmd[i]=c;
i++;
} while (c != EOF);
}else{
puts("Error Reading file");
}
return cmd;
}
So as in the file "command_history.txt", there is only one line stored, I reassumed that it would return this one line in an array of chars. To test I printed the results:
cmd = getSavedCommand();
printf("|%s|",cmd);
And the result I get is:
arj#arj-Inspiron-1545:~/projet$ ./a.out
|ls -l /home/arj
�|
arj#arj-Inspiron-1545:~/projet$
What I want is:
|ls -l /home/arj|
I think the EOF is creating the problem. Can someone help me?
One of the problem is you don't null terminate your array before returning. You need something like cmd[i] = '\0' at the end.
One more serious problem is you are returning a pointer to an object that is destroyed when the function returns. cmd object has automatic storage and is destroyed at the end of the function. Use malloc to allocate the array, or pass a pointer to the array as the argument of your getSavedFunction.
This functionality (plus command line edition, and a slew of other goodies) is the whole point of GNU readline (if on Linux, it is probably provided as a prebuilt package) or its BSD clone libedit (probably already available on BSD Unix).