modules_s/print/print.c

00001 /*$Id$
00002  *
00003  * Example ser module, it will just print its string parameter to stdout
00004  *
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  */
00029 /*
00030  * History:
00031  * --------
00032  *  2003-03-10  module export interface updated to the new format (andrei)
00033  *  2003-03-11  flags export parameter added (janakj)
00034  *  2006-01-07  str export parameter added, overloading test (tma)
00035  */
00036 
00037 
00038 
00039 
00040 #include "../../sr_module.h"
00041 #include "../../route_struct.h"
00042 #include "../../str.h"
00043 #include <stdio.h>
00044 
00045 MODULE_VERSION
00046 
00047 static int print_fixup_f_1(void **param, int param_no);
00048 static int print_fixup_f_2(void **param, int param_no);
00049 static int print_f_0(struct sip_msg*, char*, char*);
00050 static int print_f_1(struct sip_msg*, char*, char*);
00051 static int print_f_2(struct sip_msg*, char*, char*);
00052 static int print_f1(struct sip_msg*, char*, char*);
00053 static int print_f2(struct sip_msg*, char*, char*);
00054 static int print_f3(struct sip_msg*, char*, char*, char*);
00055 static int print_f_var(struct sip_msg*, int argc, action_u_t argv[]);
00056 static int mod_init(void);
00057 
00058 /* the parameters are not used, they are only meant as an example*/
00059 char* string_param = 0;
00060 int int_param = 0;
00061 str str_param = STR_STATIC_INIT("");
00062 
00063 static cmd_export_t cmds[]={
00064         {"print", print_f_0, 0, 0, REQUEST_ROUTE},   // overload test
00065         {"print", print_f_1, 1, print_fixup_f_1, REQUEST_ROUTE},
00066         {"print", print_f_2, 2, print_fixup_f_2, REQUEST_ROUTE},
00067         {"print1", print_f1, 1, 0, REQUEST_ROUTE},
00068         {"print2", print_f2, 2, fixup_var_str_12, REQUEST_ROUTE},
00069         {"print3", (cmd_function)print_f3, 3, 0, REQUEST_ROUTE},
00070         {"printv", (cmd_function)print_f_var, VAR_PARAM_NO, 0, REQUEST_ROUTE},
00071         {0, 0, 0, 0, 0}
00072 };
00073 
00074 static param_export_t params[]={
00075         {"string_param", PARAM_STRING, &string_param},
00076         {"str_param",    PARAM_STR, &str_param},
00077         {"int_param",    PARAM_INT, &int_param},
00078         {0,0,0}
00079 };
00080 
00081 struct module_exports exports = {
00082         "print_stdout",
00083         cmds,
00084         0,        /* RPC methods */
00085         params,
00086 
00087         mod_init, /* module initialization function */
00088         0,        /* response function*/
00089         0,        /* destroy function */
00090         0,        /* oncancel function */
00091         0         /* per-child init function */
00092 };
00093 
00094 
00095 static int mod_init(void)
00096 {
00097         fprintf(stderr, "print - initializing\n");
00098         DBG("print: string_param = '%s'\n", string_param);
00099         DBG("print: str_param = '%.*s'\n", str_param.len, str_param.s);
00100         DBG("print: int_param = %d\n", int_param);
00101         WARN("this is an example module, it has no practical use\n");
00102         return 0;
00103 }
00104 
00105 
00106 static int print_fixup_f(void **param, int param_no) {
00107         action_u_t *a;
00108         int n, i;
00109         n = fixup_get_param_count(param, param_no);
00110         for (i=1; i<=n; i++) {
00111                 a = fixup_get_param(param, param_no, i);
00112                 DBG("param #%d: '%s'\n", i, a->u.string);
00113         }
00114         return 1;
00115 }
00116 
00117 static int print_f_0(struct sip_msg* msg, char* s1, char* s2)
00118 {
00119         printf("<null>\n");
00120         return 1;
00121 }
00122 
00123 static int print_f_1(struct sip_msg* msg, char* s1, char* s2)
00124 {
00125         printf("%s\n",s1);
00126         return 1;
00127 }
00128 
00129 static int print_f_2(struct sip_msg* msg, char* s1, char* s2)
00130 {
00131         printf("%s%s\n",s1, s2);
00132         return 1;
00133 }
00134 
00135 static int print_fixup_f_1(void **param, int param_no) {
00136         DBG("print: print_fixup_f_1('%s')\n", (char*)*param);
00137         return print_fixup_f(param, param_no);
00138 }
00139 
00140 static int print_fixup_f_2(void **param, int param_no) {
00141         DBG("print: print_fixup_f_2('%s')\n", (char*)*param);
00142         return print_fixup_f(param, param_no);
00143 }
00144 
00145 
00146 
00147 /* 1 parameter, no fixup version */
00148 static int print_f1(struct sip_msg* msg, char* s1, char* not_used)
00149 {
00150         printf("%s\n", s1);
00151         return 1;
00152 }
00153 
00154 
00155 /* 2 parameters, fparam fixup version */
00156 static int print_f2(struct sip_msg* msg, char* s1, char* s2)
00157 {
00158         str a, b;
00159         if (get_str_fparam(&a, msg, (fparam_t*)s1) !=0 ||
00160                  get_str_fparam(&b, msg, (fparam_t*)s2) !=0) {
00161                 BUG("get_str_fparam failed\n");
00162                 return -1;
00163         }
00164         printf("%.*s%.*s\n", a.len, a.s, b.len, b.s);
00165         return 1;
00166 }
00167 
00168 
00169 /* 3 parameters, no fixup version */
00170 static int print_f3(struct sip_msg* msg, char* s1, char* s2, char* s3)
00171 {
00172         printf("%s%s%s\n", s1, s2, s3);
00173         return 1;
00174 }
00175 
00176 
00177 /* variable number of parameters, no fixup version */
00178 static int print_f_var(struct sip_msg* msg, int argc, action_u_t argv[])
00179 {
00180         int i;
00181         for (i = 0; i < argc; i++)
00182                 printf("%s", argv[i].u.string);
00183         printf("\n");
00184         return 1;
00185 }