Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief NDRX helper functions.
0003  *
0004  * @file utils.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <unistd.h>
0039 #include <ctype.h>
0040 
0041 #include <ndrstandard.h>
0042 #include <ndebug.h>
0043 
0044 #include <ndrx.h>
0045 #include <ndrxdcmn.h>
0046 #include <atmi_int.h>
0047 #include <nclopt.h>
0048 #include <pscript.h>
0049 #include <errno.h>
0050 #include <userlog.h>
0051 
0052 /*---------------------------Externs------------------------------------*/
0053 extern const char ndrx_G_resource_Exfields[];
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * Return node id (custom init, not ATMI pull in)
0063  * @return node id string or "?" in case of misconfig
0064  */
0065 expublic char * ndrx_xadmin_nodeid(void)
0066 {
0067     static char nodeid[64];
0068     static int first = EXTRUE;
0069     char *p;
0070     
0071     if (first)
0072     {
0073         if (NULL==(p = getenv(CONF_NDRX_NODEID)))
0074         {
0075             NDRX_STRCPY_SAFE(nodeid, "?");
0076         }
0077         else
0078         {
0079             NDRX_STRCPY_SAFE(nodeid, p);
0080         }
0081     }
0082     
0083     return nodeid;
0084 }
0085 
0086 /**
0087  * Quick wrapper for argument parsing...
0088  * @param message
0089  * @param artc
0090  * @param argv
0091  * @return 
0092  */
0093 expublic int chk_confirm_clopt(char *message, int argc, char **argv)
0094 {
0095     int ret = EXSUCCEED;
0096     short confirm = EXFALSE;
0097     
0098     ncloptmap_t clopt[] =
0099     {
0100         {'y', BFLD_SHORT, (void *)&confirm, 0, 
0101                                 NCLOPT_OPT | NCLOPT_TRUEBOOL, "Confirm"},
0102         {0}
0103     };
0104     
0105     /* parse command line */
0106     if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0107     {
0108         fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0109         EXFAIL_OUT(ret);
0110     }
0111     
0112     
0113     ret = ndrx_chk_confirm(message, confirm);
0114     
0115 out:
0116     return ret;
0117 }
0118 
0119 #ifndef NDRX_DISABLEPSCRIPT
0120 /**
0121  * Print function for pscript
0122  * @param v
0123  * @param s
0124  * @param ...
0125  */
0126 expublic void printfunc(HPSCRIPTVM v,const PSChar *s,...)
0127 {
0128     va_list vl;
0129     va_start(vl, s);
0130     vfprintf(stdout, s, vl);
0131     va_end(vl);
0132 }
0133 
0134 
0135 /**
0136  * Error function for pscript
0137  * @param v
0138  * @param s
0139  * @param ...
0140  */
0141 expublic void errorfunc(HPSCRIPTVM v,const PSChar *s,...)
0142 {
0143     va_list vl;
0144     va_start(vl, s);
0145     vfprintf(stderr, s, vl);
0146     va_end(vl);
0147 }
0148 
0149 
0150 /**
0151  * Load key:value string into VM table
0152  * @param v
0153  * @param key_val_string
0154  * @return 
0155  */
0156 expublic int load_value(HPSCRIPTVM v, char *key_val_string)
0157 {
0158     int ret = EXSUCCEED;
0159     char *p;
0160     int len = strlen(key_val_string);
0161     
0162     p=strchr(key_val_string, '=');
0163     
0164     if (!len)
0165     {
0166         fprintf(stderr, "Empty value string!\n");
0167         EXFAIL_OUT(ret);
0168     }
0169     
0170     if (NULL==p)
0171     {
0172         fprintf(stderr, "Missing '=' in value [%s]!\n", key_val_string);
0173         EXFAIL_OUT(ret);
0174     }
0175     
0176     if (p==key_val_string)
0177     {
0178         fprintf(stderr, "Missing value [%s]!\n", key_val_string);
0179         EXFAIL_OUT(ret);
0180     }
0181     
0182     if (p==key_val_string)
0183     {
0184         fprintf(stderr, "Missing value [%s]!\n", key_val_string);
0185         EXFAIL_OUT(ret);
0186     }
0187     
0188     *p = EXEOS;
0189     p++;
0190     
0191     /* fprintf(stderr, "Setting value: %s\n", p); */
0192     ps_pushstring(v, key_val_string, -1); /* 4 */
0193     ps_pushstring(v, p, -1);/* 5 */
0194     ps_newslot(v, -3, PSFalse );/* 3 */
0195     
0196 out:
0197     return ret;
0198 }
0199 
0200 
0201 /**
0202  * Add defaults from config file
0203  * @return 
0204  */
0205 expublic int add_defaults_from_config(HPSCRIPTVM v, char *section)
0206 {
0207     int ret = EXSUCCEED;
0208     ndrx_inicfg_section_keyval_t *val;
0209     char *ptr = NULL;
0210     char *p;
0211     /* get the value if have one */
0212     if (NULL!=(val=ndrx_keyval_hash_get(G_xadmin_config, section)))
0213     {
0214         userlog("Got config defaults for %s: [%s]", section, val->val);
0215         
0216         if (NULL==(ptr = strdup(val->val)))
0217         {
0218             userlog("Malloc failed: %s", strerror(errno));
0219             EXFAIL_OUT(ret);
0220         }
0221         
0222         /* OK token the string and do override */
0223         
0224         /* Now split the stuff */
0225         p = strtok (ptr, ARG_DEILIM);
0226         while (p != NULL)
0227         {
0228             if (0==strcmp(p, "-d"))
0229             {
0230                 PSBool isDefaulted = EXTRUE;
0231                 ps_pushstring(v, "isDefaulted", -1); /* 4 */
0232                 ps_pushbool(v, isDefaulted);
0233                 ps_newslot(v, -3, PSFalse );/* 3 */
0234             }
0235             else if (0==strncmp(p, "-v", 2) 
0236                     && 0!=strcmp(p, "-v"))
0237             {
0238                 if (strlen(p) < 4)
0239                 {
0240                     userlog("Invalid default settings for provision command [%s]", 
0241                             val->val);
0242                     EXFAIL_OUT(ret);
0243                 }
0244                 
0245                 /* pass in value definition (as string) 
0246                  * format <key>=<value>
0247                  */
0248                 if (EXSUCCEED!=load_value(v, p+2))
0249                 {
0250                     userlog("Invalid value\n");
0251                     EXFAIL_OUT(ret);
0252                 }
0253             }
0254             else if (0==strncmp(p, "-v", 2))
0255             {
0256                 p = strtok (NULL, ARG_DEILIM);
0257                 
0258                 /* next value is key */
0259                 if (NULL!=p)
0260                 {
0261                     if (EXSUCCEED!=load_value(v, p))
0262                     {
0263                         userlog("Invalid value on provision defaults [%s]", 
0264                                 val->val);
0265                         EXFAIL_OUT(ret);
0266                     }
0267                 }
0268                 else
0269                 {
0270                     userlog("Invalid command line missing value at end [%s]", 
0271                             val->val);
0272                     EXFAIL_OUT(ret);
0273                 }
0274             }
0275             
0276             p = strtok (NULL, ARG_DEILIM);
0277         }
0278     }
0279     
0280 out:
0281 
0282     if (NULL!=ptr)
0283     {
0284         free(ptr);
0285     }
0286 
0287     if (EXSUCCEED!=ret)
0288     {
0289         fprintf(stderr, "Failed to process defaults - invalid config [%s], see ULOG\n", 
0290                 G_xadmin_config_file);
0291     }
0292 
0293     return ret;
0294 }
0295 
0296 
0297 /**
0298  *Read the line from terminal
0299  *@return line read string
0300  */
0301 static PSInteger _xadmin_getExfields(HPSCRIPTVM v)
0302 {
0303     
0304     ps_pushstring(v,ndrx_G_resource_Exfields,-1);
0305 
0306     return 1;
0307 }
0308 
0309 /**
0310  * Provide the Exfields function to root table.
0311  */
0312 expublic int register_getExfields(HPSCRIPTVM v)
0313 {
0314     
0315     ps_pushstring(v,"getExfields",-1);
0316     ps_newclosure(v,_xadmin_getExfields,0);
0317     ps_setparamscheck(v,1,".s");
0318     ps_setnativeclosurename(v,-1,"getExfields");
0319     ps_newslot(v,-3,PSFalse);
0320 
0321     return 1;
0322 }
0323 
0324 #endif
0325 /* vim: set ts=4 sw=4 et smartindent: */