Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Part of UBF library
0003  *   Utility for generating field header files.
0004  *   Also the usage of default `fld.tbl' is not supported, as seems to be un-needed
0005  *   feature.
0006  *
0007  * @file viewc.c
0008  */
0009 /* -----------------------------------------------------------------------------
0010  * Enduro/X Middleware Platform for Distributed Transaction Processing
0011  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0012  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0013  * This software is released under one of the following licenses:
0014  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0015  * See LICENSE file for full text.
0016  * -----------------------------------------------------------------------------
0017  * AGPL license:
0018  *
0019  * This program is free software; you can redistribute it and/or modify it under
0020  * the terms of the GNU Affero General Public License, version 3 as published
0021  * by the Free Software Foundation;
0022  *
0023  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0024  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0025  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0026  * for more details.
0027  *
0028  * You should have received a copy of the GNU Affero General Public License along 
0029  * with this program; if not, write to the Free Software Foundation, Inc.,
0030  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0031  *
0032  * -----------------------------------------------------------------------------
0033  * A commercial use license is available from Mavimax, Ltd
0034  * contact@mavimax.com
0035  * -----------------------------------------------------------------------------
0036  */
0037 
0038 /*---------------------------Includes-----------------------------------*/
0039 
0040 #include <unistd.h>
0041 #include <stdio.h>
0042 #include <errno.h>
0043 #include <stdlib.h>
0044 #include <ndrx_config.h>
0045 #include <atmi.h>
0046 #include <atmi_int.h>
0047 #include <sys_unix.h>
0048 #include <ctype.h>
0049 
0050 #include <ubf.h>
0051 #include <ferror.h>
0052 #include <fieldtable.h>
0053 #include <fdatatype.h>
0054 
0055 #include <ndrstandard.h>
0056 #include <ndebug.h>
0057 #include <view_cmn.h>
0058 #include "viewc.h"
0059 
0060 /*---------------------------Externs------------------------------------*/
0061 /*---------------------------Macros-------------------------------------*/
0062 /*---------------------------Enums--------------------------------------*/
0063 /*---------------------------Typedefs-----------------------------------*/
0064 /*---------------------------Globals------------------------------------*/
0065 expublic char ndrx_G_build_cmd[NDRX_BPATH_MAX+1] = "buildclient";
0066 /*---------------------------Statics------------------------------------*/
0067 
0068 /**
0069  * print usage
0070  * @param argc
0071  * @param argv
0072  * @return 
0073  */
0074 exprivate void usage(char *progname)
0075 {
0076     fprintf (stderr, "Usage: %s [-b build_command] [-n] [-d viewdir] "
0077             "[-C] viewfile [viewfile ... ]\n", 
0078              progname);
0079 }
0080 
0081 /**
0082  * Main entry point for view compiler
0083  */
0084 int main(int argc, char **argv)
0085 {
0086     int no_UBF = EXFALSE;
0087     char outdir[NDRX_BPATH_MAX+1]="./";
0088     int i;
0089     int c;
0090     int ret = EXSUCCEED;
0091     int lang_mode = HDR_C_LANG;
0092     char basename[NDRX_BPATH_MAX+1];
0093     char *p, *env;
0094     int was_file = EXFALSE;
0095     char Vfile[NDRX_BPATH_MAX+1];
0096     char tmp[NDRX_BPATH_MAX+1];
0097     opterr = 0;
0098     
0099     NDRX_BANNER("VIEW File Compiler");
0100 
0101     while ((c = getopt (argc, argv, "nd:Chm:b:L:")) != -1)
0102     {
0103         switch (c)
0104         {
0105             case 'L':
0106                 /* Extra Library for runtime, colon separated...*/
0107                 env = getenv(NDRX_LD_LIBRARY_PATH);
0108                 
0109                 if (NULL!=env)
0110                 {
0111                     snprintf(tmp, sizeof(tmp), "%s:%s", env, optarg);
0112                 }
0113                 else
0114                 {
0115                     NDRX_STRCPY_SAFE(tmp, optarg);
0116                 }
0117                 
0118                 NDRX_LOG(log_debug, "Setting library path: [%s]", tmp);
0119                 
0120                 setenv(NDRX_LD_LIBRARY_PATH, tmp, EXTRUE);
0121                 
0122                 break;
0123             case 'b':
0124                 NDRX_STRCPY_SAFE(ndrx_G_build_cmd, optarg);
0125                 break;
0126             case 'n':
0127                 NDRX_LOG(log_debug, "No UBF mapping processing...");
0128                 no_UBF = EXTRUE;
0129                 
0130                 ndrx_view_loader_configure(no_UBF);
0131                         
0132                 break;
0133             case 'd':
0134                 NDRX_STRCPY_SAFE(outdir, optarg);
0135                 NDRX_LOG(log_debug, "Changing view object output directory to: [%s]", 
0136                         outdir);
0137                 break;
0138             case 'C':
0139                 NDRX_LOG(log_warn, "Ignoring option C for COBOL");
0140                 break;
0141             case 'm':
0142                 lang_mode = atoi(optarg);
0143                 NDRX_LOG(log_warn, "Language mode set to: %d", lang_mode);
0144 
0145                 if (HDR_C_LANG!=lang_mode)
0146                 {
0147                     NDRX_LOG(log_error, "Invalid language mode, currently %d supported only", 
0148                             HDR_C_LANG);
0149                     EXFAIL_OUT(ret);
0150                 }
0151 
0152                 break;
0153             case 'h':
0154                 usage(argv[0]);
0155                 EXFAIL_OUT(ret);
0156                 break;
0157             case '?':
0158                 if (optopt == 'c')
0159                 {
0160                     fprintf (stderr, "Option -%c requires an argument.\n", optopt);
0161                 }
0162                 else if (isprint (optopt))
0163                 {
0164                     fprintf (stderr, "Unknown option `-%c'.\n", optopt);
0165                 }
0166                 else
0167                 {
0168                     fprintf (stderr,
0169                         "Unknown option character `\\x%x'.\n",
0170                         optopt);
0171                 }
0172                 EXFAIL_OUT(ret);
0173             default:
0174                 NDRX_LOG(log_error, "Default case...");
0175                 EXFAIL_OUT(ret);
0176         }
0177     }
0178     
0179     NDRX_LOG(log_debug, "Build command set to: [%s]", ndrx_G_build_cmd);
0180     for (i = optind; i < argc; i++)
0181     {
0182         was_file = EXTRUE;
0183         NDRX_LOG (log_debug, "Processing VIEW file [%s]", argv[i]);
0184         
0185         /* Load the view file */
0186         if (EXSUCCEED!=ndrx_view_load_file(argv[i], EXFALSE))
0187         {
0188             NDRX_LOG(log_error, "Failed to load view file [%s]: %s", 
0189                     argv[i], Bstrerror(Berror));
0190             EXFAIL_OUT(ret);
0191         }
0192         
0193         /* get the base name of output file */
0194         
0195         NDRX_STRCPY_SAFE(basename, ndrx_basename(argv[i]));
0196         p = strrchr(basename, '.');
0197         
0198         if (NULL!=p)
0199         {
0200             *p=EXEOS;
0201         }
0202         
0203         if (strlen(basename) == 0)
0204         {
0205             NDRX_LOG(log_error, "Invalid view file name passed on CLI...");
0206             EXFAIL_OUT(ret);
0207         }
0208         
0209         /* Plot the the header */
0210         if (HDR_C_LANG==lang_mode)
0211         {
0212             if (EXSUCCEED!=ndrx_view_plot_c_header(outdir, basename))
0213             {
0214                 NDRX_LOG(log_error, "Failed to plot c header!");
0215                 EXFAIL_OUT(ret);
0216             }
0217         
0218             /* Get the offset - generate object file & invoke */
0219             
0220             if (EXSUCCEED!=ndrx_view_generate_code(outdir, basename, argv[i], 
0221                     Vfile, no_UBF))
0222             {
0223                 NDRX_LOG(log_error, "Failed to generate code or invoke compiler!");
0224                 EXFAIL_OUT(ret);
0225             }
0226         }
0227         
0228         /* Unload the view files (remove from hashes) */
0229         ndrx_view_deleteall();
0230         
0231         NDRX_LOG(log_info, ">>> About to test object file...");
0232         
0233         if (EXSUCCEED!=ndrx_view_load_file(Vfile, EXTRUE))
0234         {
0235             NDRX_LOG(log_error, "!!! Failed to test object file [%s] !!!", Vfile);
0236             EXFAIL_OUT(ret);
0237         }
0238         
0239         ndrx_view_deleteall();
0240         
0241         NDRX_LOG(log_info, ">>> [%s] COMPILED & TESTED OK!", Vfile);
0242     }
0243     
0244     if (!was_file)
0245     {
0246         usage(argv[0]);
0247         EXFAIL_OUT(ret);
0248     }
0249     
0250 out:
0251     /* So we need to load the view file now and generate header */
0252     return ret;
0253 }
0254 
0255 
0256 /* vim: set ts=4 sw=4 et smartindent: */