I'm working in my C program and I have a doubt about how to use the sscanf function.
I have to get from a string like something$HOME/something2 three strings: the first one has to contain "something", the second one "HOME" and the third one "/something2".
But also it has to be able to split $HOME or $HOME/something, or something$HOME...
What I wrote was something like that:
sscanf(str,"%m[^$]$%m[_a-zA-Z0-9]%*m", &ant, &act, &sig);
But for the cases $HOME, $HOME/something, something$HOME... it takes very strange strings. Any ideas of what can I put on my sscanf to get the other values by NULL in case they don't exist?
Sorry, but you should not use sscanf for that. Your input seems to be a shell command line argument and you seem to want implement variable expansion.
For that, you have to write your own parser that would parse the expression, identify and do variable expansion. It's not a job for scanf - it's a job for your custom loop where you inspect each and every character in a string.
There's a utility with like exactly that functionality called envsubst. You could inspect its source gettext/envsubst.c. You could also take a look at wordexp; however, wordexp function is a way broader function.
Related
I have a program that parses command line arguments using a while loop. Simply, while iterating through the length of argc, if an argument matches a flag than the next argument is taken as a variable. Now in my assignment we are asked to do this in a way that spaces between flags and integer arguments are optional.
For example if i input -k1 it is the same as -k 1 and 1 is the value stored.
I can't find anything that allows this. The only thing I can think is that if argc is an even number it means that there are no pals between a set of argument and i could use scanf("-k%d",key).
Any helpful pointers for me?
At a POSIX-compatible OS you can use a standart API for that: man getopt. It will do all the dirty job to parse the parameters and will provide you a convenient interface to deal with.
Here is a good example for it: http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html#Example-of-Getopt
I have an assignment which tells me i need to accept arguments from the command line.
I know how to accept arguments from the command line, however this is what i need
I am told my arguments are as follows name_of_function name_of_variable argument1, arugment2
is there an easy way to map name_of_function to the name of the function and name_of_variable to the name of the global variable, without going strcmp on each of them ?
There is no tool or library that converts a string to the corresponding variable, function, or anything else in C. When you have e.g. a .NET runtime environment you could use reflection to see, if an object is in your program and to access it.
You will have to use strcmp or similar to interpret the command line arguments and decide how to deal with the commands.
I'm interested in something like strstr() function but that I could pass a formatted string as argument, like what I pass to printf(). To be clear, let's get an example:
Suppose that I want to find this text: "abc:123" where abc could be any string with any size followed by ':' and then followed by some integer number. I suppose a good function could receive as argument something like this: "%s:%d".
Something else, I want to use this embedded, so I can't get big and/or esoteric libraries.
Thanks and best regards!
You can use sscanf. It takes a string and a format as input and you fill variables as a result. Regular expressions are also something to consider
Use should use regular expressions.
This thread may help you: Compiling/Matching POSIX Regular Expressions in C
Gentleman,
I found this CRX and it is exactly what I wanted. Thanks everybody!
Alternatively, you can directly use printf function. The function outputs strings on default uart (mine is uart0).
But prior to using printf, proper set-up and configuration is required.
I am fairly new into regexes, so I wrote the following simple regex using positive lookahead that detects functions and function calls in a C source file-
\w+(?=\s*\()
It works fine, but the problem is it detects non-function syntaxes like if(), while()etc too.
I can easily avoid this by saying-
(if(?!\()) | (while(?!\())
But the problem is how to combine the second regex with the first one? I cant OR them, cos the first one still matches if(), while() etc and in an OR expression, its enough if one of the term matches.
How to combine these regexes or have a better simpler one which will not match non-function syntaxes like if(), while()
PS: I use the following tools to test my regexes
GSkinner
RegexPal
There are quite a lot of assumptions when you are searching for function call in C with regex. That aside, if you are happy with what is matched (there are valid function calls that will not be matched), and you want to exclude if and while from the result list, you can use the following regex:
(?!\b(if|while|for)\b)\b\w+(?=\s*\()
The regex uses word boundary \b to make sure that the whole name is matched (prevent partial matching of hile in while), and the whole name is not keyword (prevent rejection of whilenothinghappens).
I am developing a custom LISP interpreter. It won't support defining functions like in LISP, instead all functions are mapped to C functions. When it sees an expression like,
(substr 'input '1 '1)
it knows to call internal substr function and return the result.
Now I am planning to implement a message function which supports basic formatting and writes the output to stdout. Something like,
(message "Hello, %s" name)
%s will be replaced with value in variable name.
Current plan is to directly pass the format and arguments to functions like printf. In that way, I can support all formats that printf supports. But problem comes with variable number of arguments. One way to do will be something like,
if(argcount == 1)
/* call printf with one arg */
else if(argcount == 2)
/* call printf with two arg */
....
This works, but I am wondering is there a better way to achieve this?
I doubt there is a way to do this. The reason is that the number of parameters to your lisp function is only known at runtime, but the number of arguments to a C function must be known at compile time.
This includes va_lists unless you want to hack at them in some kind of platform specific way.
The best you can really do is write a function in C which is capable of looping through the arguments one at a time and doing something with them. The only way I can see around this is to not only store a function pointer for each of your internal functions, but to also store a "calling convention" which will give information about whether it takes parameters in the ordinary way or whether it finishes with the equivalent of a va_list.
Functions like printf would have a wrapper, printf_wrapper, say, and you'd store a function pointer to the wrapper. This wrapper would accept the format string as an ordinary parameter, followed by a list or array of other parameters (roughly analogous to a va_list).
You might indicate that printf_wrapper finishes with a parameter that expects a list by specifying the calling conventions for the printf_wrapper function as "va_list_type", meaning that it takes the usual fixed parameters, and that all remaining parameters must be bundled up and supplied to it as a list.
Of course writing a printf_wrapper function which can split up and parse a format string into multiple format strings is a bit of work. Here's an example of where I did precisely this so that I could add my own custom format specifiers:
https://github.com/wbhart/bsdnt/blob/v0.26/helper.c
Have your C function take parameters somewhat like argc/argv. That is, take a parameter specifying the number of parameters, and then a pointer to a list of pointers for each parameter.
Slightly better than an if-else chain would be a switch.
switch(argcount){
case 1: printf(arg[0]); break;
case 2: printf(arg[0],arg[1]); break;
//etc.
}