Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Environment management, commands: `set', `unset', printenv (pe)
0003  *   TODO: Improve error handling (print more accurate error from ndrxd instead of 0)
0004  *
0005  * @file cmd_env.c
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0013  * See LICENSE file for full text.
0014  * -----------------------------------------------------------------------------
0015  * AGPL license:
0016  *
0017  * This program is free software; you can redistribute it and/or modify it under
0018  * the terms of the GNU Affero General Public License, version 3 as published
0019  * by the Free Software Foundation;
0020  *
0021  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0023  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0024  * for more details.
0025  *
0026  * You should have received a copy of the GNU Affero General Public License along 
0027  * with this program; if not, write to the Free Software Foundation, Inc.,
0028  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0029  *
0030  * -----------------------------------------------------------------------------
0031  * A commercial use license is available from Mavimax, Ltd
0032  * contact@mavimax.com
0033  * -----------------------------------------------------------------------------
0034  */
0035 #include <string.h>
0036 #include <stdio.h>
0037 #include <stdlib.h>
0038 #include <memory.h>
0039 #include <sys/param.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 <gencall.h>
0048 
0049 #include "nstopwatch.h"
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 /**
0058  * Process response back.
0059  * @param reply
0060  * @param reply_len
0061  * @return
0062  */
0063 expublic int pe_rsp_process(command_reply_t *reply, size_t reply_len)
0064 {
0065     if (NDRXD_CALL_TYPE_PE==reply->msg_type)
0066     {
0067         command_reply_pe_t * pe_info = (command_reply_pe_t*)reply;
0068         fprintf(stdout, "%s\n", pe_info->env);
0069     }
0070     
0071     return EXSUCCEED;
0072 }
0073 
0074 /**
0075  * Get service listings
0076  * @param p_cmd_map
0077  * @param argc
0078  * @param argv
0079  * @return SUCCEED
0080  */
0081 expublic int cmd_pe(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0082 {
0083     command_call_t call;
0084     memset(&call, 0, sizeof(call));
0085 
0086     /* Then get listing... */
0087     return cmd_generic_listcall(p_cmd_map->ndrxd_cmd, NDRXD_SRC_ADMIN,
0088                         NDRXD_CALL_TYPE_GENERIC,
0089                         &call, sizeof(call),
0090                         G_config.reply_queue_str,
0091                         G_config.reply_queue,
0092                         G_config.ndrxd_q,
0093                         G_config.ndrxd_q_str,
0094                         argc, argv,
0095                         p_have_next,
0096                         G_call_args,
0097                         EXFALSE,
0098                         G_config.listcall_flags);
0099 }
0100 
0101 /**
0102  * Set env variable value
0103  * @param p_cmd_map
0104  * @param argc
0105  * @param argv
0106  * @param p_have_next
0107  * @return 
0108  */
0109 expublic int cmd_set(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0110 {
0111     int ret=EXSUCCEED;
0112     command_setenv_t call;
0113     int i=1;
0114     
0115     memset(&call, 0, sizeof(call));
0116     
0117     if (argc>=2)
0118     {
0119         while (i<argc && strlen(call.env)+strlen(argv[i]) < EX_ENV_MAX-2)
0120         {
0121             if (i>1)
0122             {
0123                 strcat(call.env, " ");
0124             }
0125             strcat(call.env, argv[i]);
0126             i++;
0127         }
0128     }
0129     else
0130     {
0131         fprintf(stderr, "Invalid value, format set 'ENV_VAR=VALUE'\n");
0132         EXFAIL_OUT(ret);
0133     }
0134     
0135     ret=cmd_generic_listcall(p_cmd_map->ndrxd_cmd, NDRXD_SRC_ADMIN,
0136                         NDRXD_CALL_TYPE_GENERIC,
0137                         (command_call_t *)&call, sizeof(call),
0138                         G_config.reply_queue_str,
0139                         G_config.reply_queue,
0140                         G_config.ndrxd_q,
0141                         G_config.ndrxd_q_str,
0142                         argc, argv,
0143                         p_have_next,
0144                         G_call_args,
0145                         EXFALSE,
0146                         G_config.listcall_flags);
0147 out:
0148     return ret;
0149 }
0150 
0151 
0152 /**
0153  * Unset env
0154  * @param p_cmd_map
0155  * @param argc
0156  * @param argv
0157  * @param p_have_next
0158  * @return 
0159  */
0160 expublic int cmd_unset(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0161 {
0162     int ret=EXSUCCEED;
0163     command_setenv_t call;
0164     
0165     memset(&call, 0, sizeof(call));
0166     
0167     if (argc>=2)
0168     {
0169         NDRX_STRCPY_SAFE(call.env, argv[1]);
0170         if (NULL!=strchr(call.env, '='))
0171         {
0172                 fprintf(stderr, "Invalid format\n");
0173                 EXFAIL_OUT(ret);
0174         }
0175         strcat(call.env,"=");
0176     }
0177     else
0178     {
0179         fprintf(stderr, "Missing ENV name\n");
0180         EXFAIL_OUT(ret);
0181     }
0182     
0183     ret=cmd_generic_listcall(p_cmd_map->ndrxd_cmd, NDRXD_SRC_ADMIN,
0184                         NDRXD_CALL_TYPE_GENERIC,
0185                         (command_call_t *)&call, sizeof(call),
0186                         G_config.reply_queue_str,
0187                         G_config.reply_queue,
0188                         G_config.ndrxd_q,
0189                         G_config.ndrxd_q_str,
0190                         argc, argv,
0191                         p_have_next,
0192                         G_call_args,
0193                         EXFALSE,
0194                         G_config.listcall_flags);
0195 out:
0196     return ret;
0197 }
0198 
0199 /* vim: set ts=4 sw=4 et smartindent: */