Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Create and application instance
0003  *
0004  * @file cmd_provision.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 <sys/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 
0043 #include <ndrx.h>
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <errno.h>
0048 
0049 #include "nclopt.h"
0050 
0051 #ifndef NDRX_DISABLEPSCRIPT
0052 #include <pscript.h>
0053 #include <psstdblob.h>
0054 #include <psstdio.h>
0055 #include <psstdsystem.h>
0056 #include <psstdmath.h>
0057 #include <psstdstring.h>
0058 #include <psstdexutil.h>
0059 #include <psstdaux.h>
0060 #endif
0061 
0062 
0063 #ifndef NDRX_DISABLEPSCRIPT
0064 /*---------------------------Externs------------------------------------*/
0065 /*---------------------------Macros-------------------------------------*/
0066 /* Have some access to resources */
0067 extern const char ndrx_G_resource_provision[];
0068 extern const size_t ndrx_G_resource_provision_len;
0069 
0070 /*---------------------------Enums--------------------------------------*/
0071 /*---------------------------Typedefs-----------------------------------*/
0072 /*---------------------------Globals------------------------------------*/
0073 /*---------------------------Statics------------------------------------*/
0074 /*---------------------------Prototypes---------------------------------*/
0075 
0076 /**
0077  * Run the wizard for application via pscrip
0078  * @param p_cmd_map
0079  * @param argc
0080  * @param argv
0081  * @return SUCCEED
0082  */
0083 expublic int cmd_provision(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0084 {
0085     int ret=EXSUCCEED;
0086     const PSChar *s;
0087     HPSCRIPTVM v;
0088     PSInteger res;
0089     int i;
0090     const PSChar *err;
0091     PSMemReader reader;
0092     
0093     v = ps_open(1024); /* creates a VM with initial stack size 1024 */
0094     
0095     ps_setprintfunc(v,printfunc,errorfunc);
0096     
0097     ps_pushroottable(v);
0098     
0099     /* register functions */
0100     psstd_register_bloblib(v);
0101     psstd_register_iolib(v);
0102     psstd_register_systemlib(v);
0103     psstd_register_mathlib(v);
0104     psstd_register_stringlib(v);
0105     psstd_register_exutillib(v);
0106 
0107     register_getExfields(v);
0108     /* aux library
0109      * sets error handlers */
0110     psstd_seterrorhandlers(v);
0111     
0112     /* Load any parameters in different table */
0113     
0114     ps_pushstring(v, "args", -1); /* 2 */
0115     ps_newtable(v); /* 3 */
0116     
0117     if (EXSUCCEED!=add_defaults_from_config(v, "provision"))
0118     {
0119         EXFAIL_OUT(ret);
0120     }
0121     
0122     if (argc>=2)
0123     {
0124         for (i=1; i<argc; i++)
0125         {
0126             /* load defaults */
0127             if (0==strcmp(argv[i], "-d"))
0128             {
0129                 PSBool isDefaulted = EXTRUE;
0130                 ps_pushstring(v, "isDefaulted", -1); /* 4 */
0131                 ps_pushbool(v, isDefaulted);
0132                 ps_newslot(v, -3, PSFalse );/* 3 */
0133             }
0134             else if (0==strncmp(argv[i], "-v", 2) 
0135                     && 0!=strcmp(argv[i], "-v") )
0136             {
0137                 /* pass in value definition (as string) 
0138                  * format <key>=<value>
0139                  */
0140                 if (EXSUCCEED!=load_value(v, argv[i]+2))
0141                 {
0142                     fprintf(stderr, "Invalid value\n");
0143                     EXFAIL_OUT(ret);
0144                 }
0145             }
0146             else if (0==strncmp(argv[i], "-v", 2))
0147             {
0148                 /* next value is key */
0149                 if (i+1<argc)
0150                 {
0151                     i++;
0152                     if (EXSUCCEED!=load_value(v, argv[i]))
0153                     {
0154                         fprintf(stderr, "Invalid value\n");
0155                         EXFAIL_OUT(ret);
0156                     }
0157                 }
0158                 else
0159                 {
0160                     fprintf(stderr, "Invalid command line missing value at end.\n");
0161                     EXFAIL_OUT(ret);
0162                 }
0163             }
0164             
0165         }
0166     }
0167     
0168     /* Load the command line arguments to the script */
0169     
0170     ps_newslot(v, -3, PSFalse );/*1*/
0171     memset(&reader, 0, sizeof(reader));
0172     reader.memptr = (char *)ndrx_G_resource_provision;
0173     reader.size = ndrx_G_resource_provision_len;
0174 
0175     /* do some stuff with pscript here */
0176     if (PS_FAILED(psstd_loadmem(v, &reader)))
0177     {
0178         fprintf(stderr, "Failed to load byte\n");
0179 
0180         if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0181         {
0182             fprintf(stderr, _SC("Error [%s]\n"),err);
0183             return EXFAIL;
0184         }
0185     }
0186     else
0187     {
0188         ps_pushroottable(v);
0189     }
0190 
0191     /* Load the script & run globals... */
0192     if (PS_FAILED(ps_call(v,1,PSTrue, PSTrue)))
0193     {
0194         fprintf(stderr, "Failed to load script\n");
0195         ps_getlasterror(v);
0196         if(PS_SUCCEEDED(ps_getstring(v,-1,&err)))
0197         {
0198             printf(_SC("Error [%s]\n"),err);
0199             EXFAIL_OUT(ret);
0200         }
0201     }
0202     
0203     /* pop the result out of the stack */
0204     ps_getinteger(v,-1,&res);
0205     
0206     if (res>=0)
0207     {
0208         ret = EXSUCCEED;
0209     }
0210     else
0211     {
0212         ret = EXFAIL;
0213     }
0214     
0215     ps_pop(v,3); /* pops the roottable and the function */
0216 
0217     ps_close(v);
0218     
0219 out:
0220     return ret;
0221 }
0222 
0223 #else
0224 /**
0225  * Not compiled on this platform
0226  * @param p_cmd_map
0227  * @param argc
0228  * @param argv
0229  * @return SUCCEED
0230  */
0231 expublic int cmd_provision(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0232 {
0233     fprintf(stderr, "Pscript not compiled for this platform!\n");
0234     return EXFAIL;
0235 }
0236 
0237 #endif
0238 /* vim: set ts=4 sw=4 et smartindent: */