Re: DSO question
On Tue, March 9, 2010 6:43 am, Graf, László wrote: I have a shared library containing a function to calculate a multiple of 10. The header file (apr_dso_f.h): int f10(int p1); I use this library with an APR console application and it woks fine. OK. My module loads also this SO library at start-up. What I would like to have is when I access the URL /gl/f10?p1=2 to call the function f10 with 2 as parameter..., calculate the result and generate the HTML content containig 20. This means that, when I have the function's name (f10) and the parameter's value (2) from the request string I need to evaluate a statement like int n; n = f10(2); like EVAL in Perl or JavaScript and use then the n like a C variable. Is that possible? Can somebody help me? Yes. Programming 101. If using C, I'd reading up on the following things : Understand how to parse the request_rec-args (QUERY_STRING) to get the param. Read up on atoi to convert the text param value into an integer call directly the n = f10(variable); Joe
Re: DSO question
Hi Joe, You are so kind. Thank you for your reply. I think that I wasn't clear enough. I know how to parse the HTTP request to access the arguments, I know how to use the atoi function but now it is not the case. In that moment when I have the name of the function, ex. f10, I have this information represented by a char* and it is not the same when you write your C code. Knowing the function and its parameter I have to evaluate it somehow. That is why I wrote in my first mail int n; n = f10(2); It is like the EVAL in Perl, where you specify the statement, prepared prior, and it calculates or evaluates it. What I need is to tell to APR, hey APR please find the function f10 in all loaded libraries, then execute the function and give me back the result. grafl 2010.03.09. 18:16 keltezéssel, Joe Lewis írta: On Tue, March 9, 2010 6:43 am, Graf, László wrote: I have a shared library containing a function to calculate a multiple of 10. The header file (apr_dso_f.h): int f10(int p1); I use this library with an APR console application and it woks fine. OK. My module loads also this SO library at start-up. What I would like to have is when I access the URL /gl/f10?p1=2 to call the function f10 with 2 as parameter..., calculate the result and generate the HTML content containig 20. This means that, when I have the function's name (f10) and the parameter's value (2) from the request string I need to evaluate a statement like int n; n = f10(2); like EVAL in Perl or JavaScript and use then the n like a C variable. Is that possible? Can somebody help me? Yes. Programming 101. If using C, I'd reading up on the following things : Understand how to parse the request_rec-args (QUERY_STRING) to get the param. Read up on atoi to convert the text param value into an integer call directly the n = f10(variable); Joe
Re: DSO question
What I need is to tell to APR, hey APR please find the function f10 in all loaded libraries, then execute the function and give me back the result. That is not how it works in C. Function names only exist in source code, at run-time it is nothing but an address in memory. Google 'function pointers'. The best you can do is a lookup table like this: static int my_f10_fun(int arg) { return arg * arg; } /* ... */ static struct { const char *name; int (*fun)(int arg); } lookup = { { f10, my_f10_fun }, { f11, my_f11_fun } }; /* ... */ int arg = /* ... */; const char *func_name = /* ... */; int result = 0; for (i = 0; i sizeof(lookup) / sizeof(lookup[0]); i++) { if (!strcmp(lookup[i].name, func_name)) { result = lookup[i].fun(arg); break; } }
RE: DSO question
-Original Message- From: Ben Noordhuis [mailto:i...@bnoordhuis.nl] Sent: Tuesday, March 09, 2010 1:34 PM To: modules-...@httpd.apache.org Subject: Re: DSO question What I need is to tell to APR, hey APR please find the function f10 in all loaded libraries, then execute the function and give me back the result. Thankfully APR defines handy macros for such a purpose... Look at the macros: APR_DECLARE_OPTIONAL_FN APR_REGISTER_OPTIONAL_FN APR_OPTIONAL_FN_TYPE APR_RETRIEVE_OPTIONAL_FN As the other poster pointed out, there is no magic bullet for knowing where the function is declared! You will have to include the relevant header where the APR_DECLARE_OPTIONAL_FN appears. So, if you want to use ssl_var_lookup from mod_ssl.h you will still have to #include mod_ssl.h in your module. If you look at ./srclib/apr-util/include/apr_optional.h and ./srclib/apr-util/hooks/apr_hooks.c, you can see under the hood that the implementation is much like the other respondent suggested, except that an apr_hash is used to register and look up functions, instead of an array. See, for example, this line from apr_hooks.c: [...] return (void(*)(void))apr_hash_get(s_phOptionalFunctions,szName,strlen(szName)) ; [...] --Pete