Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Generate main() for the XATMI server
0003  *
0004  * @file buildserver.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 
0035 /*---------------------------Includes-----------------------------------*/
0036 
0037 #include <unistd.h>
0038 #include <stdio.h>
0039 #include <errno.h>
0040 #include <stdlib.h>
0041 #include <ndrx_config.h>
0042 #include <atmi.h>
0043 #include <atmi_int.h>
0044 #include <sys_unix.h>
0045 #include <ctype.h>
0046 #include <limits.h>
0047 
0048 #include <ubf.h>
0049 #include <ferror.h>
0050 #include <fieldtable.h>
0051 #include <fdatatype.h>
0052 
0053 #include <ndrstandard.h>
0054 #include <ndebug.h>
0055 #include "buildtools.h"
0056 
0057 /*---------------------------Externs------------------------------------*/
0058 /*---------------------------Macros-------------------------------------*/
0059 /*---------------------------Enums--------------------------------------*/
0060 /*---------------------------Typedefs-----------------------------------*/
0061 /*---------------------------Globals------------------------------------*/
0062 /*---------------------------Statics------------------------------------*/
0063 
0064 /**
0065  * print help for the command
0066  * @param name name of the program, argv[0]
0067  */
0068 exprivate void print_help(char *name)
0069 {
0070     fprintf(stderr, "Usage: %s [options]\n", name);
0071     fprintf(stderr, "Options:\n");
0072     fprintf(stderr, "  -C               COBOL code (RFU)\n");
0073     fprintf(stderr, "  -o <filename>    Output compiled file name, default is a.out\n");
0074     fprintf(stderr, "  -f <firstfiles>  File names to be passed to compiler, on left side before Enduro/X libraries\n");
0075     fprintf(stderr, "  -a <firstfiles>  Alias of -f\n");
0076     fprintf(stderr, "  -l <lastfiles>   File names to be passed to compiler, on right side after Enduro/X libraries\n");
0077     fprintf(stderr, "  -r <RM_NAME>     Resource manager name to be searched in $NDRX_HOME/udataobj/RM.\n");
0078     fprintf(stderr, "                   If not set, null switch is used.\n");
0079     fprintf(stderr, "  -k               Keep generated source file\n");
0080     fprintf(stderr, "  -v               Verbose mode (print build command)\n");
0081     fprintf(stderr, "  -h               Print this help\n");   
0082 }
0083 
0084 /**
0085  * Main entry point for view compiler
0086  */
0087 int main(int argc, char **argv)
0088 {
0089     int ret = EXSUCCEED;
0090     int c;
0091     int lang_mode = HDR_C_LANG;
0092     char ofile[NDRX_BPATH_MAX+1]=" a.out";
0093     char cfile[NDRX_BPATH_MAX+1]="ndrx_bc_XXXXXX.c";
0094     int keep_main=EXFALSE;
0095     char firstfiles[NDRX_BPATH_MAX+1] = {EXEOS};
0096     char lastfiles[NDRX_BPATH_MAX+1] = {EXEOS};
0097     int verbose = EXFALSE;
0098     ndrx_rm_def_t rmdef;
0099     FILE *out_fptr = NULL;
0100     
0101     NDRX_BANNER("BUILDCLIENT Tool");
0102     
0103     /* clear any error... */
0104     _Nunset_error();
0105     
0106     memset(&rmdef, 0, sizeof(rmdef));
0107 
0108     while ((c = getopt (argc, argv, "vwr:o:f:l:Cka:h")) != -1)
0109     {
0110         switch (c)
0111         {
0112             case 'h':
0113                 print_help(argv[0]);
0114                 return 0; /*<<<< RETURN ! */
0115                 break;
0116             case 'C':
0117                 NDRX_LOG(log_warn, "COBOL mode not yet supported");
0118                 _Nset_error_fmt(NESUPPORT, "COBOL mode not yet supported");
0119                 EXFAIL_OUT(ret);
0120                 break;
0121             case 'o':
0122                 NDRX_STRCPY_SAFE(ofile, optarg);
0123                 NDRX_LOG(log_debug, "ofile: [%s]", ofile);
0124                 break;
0125             case 'a':
0126             case 'f':
0127                 if (EXEOS==firstfiles[0])
0128                 {
0129                     NDRX_STRCPY_SAFE(firstfiles, optarg);
0130                 }
0131                 else
0132                 {
0133                     NDRX_STRCAT_S(firstfiles, sizeof(firstfiles), " ");
0134                     NDRX_STRCAT_S(firstfiles, sizeof(firstfiles), optarg);
0135                 }
0136                 break;
0137             case 'l':
0138                 if (EXEOS==lastfiles[0])
0139                 {
0140                     NDRX_STRCPY_SAFE(lastfiles, optarg);
0141                 }
0142                 else
0143                 {
0144                     NDRX_STRCAT_S(lastfiles, sizeof(lastfiles), " ");
0145                     NDRX_STRCAT_S(lastfiles, sizeof(lastfiles), optarg);
0146                 }
0147                 break;
0148             case 'r':
0149                 
0150                 if ( EXFAIL == (ret=ndrx_get_rm_name(optarg, &rmdef)))
0151                 {
0152                     /* error is set */
0153                     NDRX_LOG(log_error, 
0154                          "Failed to parse resource manager: [%s], check -r", optarg);
0155                     EXFAIL_OUT(ret);
0156                 }
0157                 else if (EXTRUE!=ret)
0158                 {
0159                     NDRX_LOG(log_error, 
0160                          "Resource manager not defined: [%s], check -r", optarg);
0161                     /* set error */
0162                     _Nset_error_fmt(NEINVAL, "Resource manager [%s] not found "
0163                             "in udataobj/RM files", optarg);
0164                     EXFAIL_OUT(ret);
0165                 }
0166                 
0167                 ret = EXSUCCEED;
0168 
0169                 break;
0170             case 'k':
0171                 keep_main=EXTRUE;
0172                 break;
0173             case 'v':
0174                 NDRX_LOG(log_debug, "running in verbose mode");
0175                 verbose = EXTRUE;
0176                 break;
0177             case '?':
0178             default:
0179             
0180                 print_help(argv[0]);
0181                 return EXFAIL; /*<<<< RETURN ! */
0182         }
0183     }
0184     
0185     /* Plot the the header */
0186     if (HDR_C_LANG==lang_mode)
0187     {
0188         if ( NULL==(out_fptr=ndrx_mkstemps(cfile,2, 0) ))
0189         {
0190             int err = errno;
0191             NDRX_LOG(log_error, "Failed with error %s", strerror(err));
0192             _Nset_error_fmt(NEUNIX, "Failed to create temporary file: %s", 
0193                     strerror(err));
0194             EXFAIL_OUT(ret);
0195         }
0196         
0197         if (EXSUCCEED!=ndrx_buildclt_generate_code(&out_fptr, cfile, &rmdef))
0198         {
0199             NDRX_LOG(log_error, "Failed to generate code!");
0200             EXFAIL_OUT(ret);
0201         }
0202     }
0203     
0204     if (HDR_C_LANG==lang_mode)
0205     {
0206         if (EXSUCCEED!=ndrx_compile_c(COMPILE_CLT, verbose, cfile, ofile, 
0207                 firstfiles, lastfiles, &rmdef))
0208         {
0209             NDRX_LOG(log_error, "Failed to build");
0210             EXFAIL_OUT(ret);
0211         }
0212     }
0213     else
0214     {
0215         NDRX_LOG(log_error, "Invalid language mode: %d", lang_mode);
0216         _Nset_error_fmt(NEINVAL, "Invalid language mode %d", lang_mode);
0217         EXFAIL_OUT(ret);
0218     }
0219     
0220 out:
0221 
0222     /* cleanup hash lists... */
0223     if (EXSUCCEED!=ret)
0224     {
0225         if (!_Nis_error())
0226         {
0227             _Nset_error_fmt(NESYSTEM, "Generic error - see logs.");
0228         }
0229         
0230         /* print error */
0231         fprintf(stderr, "%s: %s\n", argv[0], ndrx_Nstrerror2(Nerror));
0232     }
0233 
0234     if (NULL!=out_fptr)
0235     {
0236         NDRX_FCLOSE(out_fptr);
0237     }
0238 
0239     if (EXFALSE == keep_main)
0240     {
0241         unlink(cfile);
0242     }
0243 
0244     return ret;
0245 }
0246 
0247 /* vim: set ts=4 sw=4 et smartindent: */