home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxsqmn13.zip / msql / rexxsql.c < prev    next >
C/C++ Source or Header  |  1996-03-09  |  6KB  |  207 lines

  1. /***********************************************************************/
  2. /* rexxsql.c - REXX/SQL for mSQL                                       */
  3. /***********************************************************************/
  4. /*
  5.  * REXX/SQL. A REXX interface to SQL databases.
  6.  * Copyright Mark Hessling 1995-1996.
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to 
  29.  * address below.
  30.  * This software is going to be maintained and enhanced as deemed
  31.  * necessary by the community.
  32.  *
  33.  * Mark Hessling                     email: M.Hessling@qut.edu.au
  34.  * 36 David Road                     Phone: +61 7 3849 7731
  35.  * Holland Park                      
  36.  * QLD 4121
  37.  * Australia
  38.  *
  39.  * Author:    Mark Hessling
  40.  *
  41.  *    Purpose:    This module fires up the REXX/SQL version of the REXX
  42.  *        interpreter. All real work is done elsewhere!
  43.  *        This module is not required to be compiled when building 
  44.  *        Dynamic Libraies
  45.  */
  46.  
  47. #if !defined(DYNAMIC_LIBRARY)
  48.  
  49. #define INCL_RXSHV    /* Shared variable support */
  50. #define INCL_RXFUNC    /* External functions support */
  51.  
  52. #ifndef __EMX__
  53. # include "rexxsaa.h"
  54. #endif
  55. #include "rexxsql.h"
  56.  
  57.  
  58. /* Debugging flags */
  59. extern int run_flags;
  60.  
  61.  
  62.  
  63. /* Program name...passed as arg[0] to main. */
  64. static char *Program=NULL;
  65.  
  66.  
  67. /* These are required by the getopt() function */
  68. extern char *optarg;
  69. extern int  optind;
  70.  
  71.  
  72. /*-----------------------------------------------------------------------------
  73.  * Returns a pointer to the basename of a file (i.e. to the 1st character past
  74.  * the directory part.
  75.  *----------------------------------------------------------------------------*/
  76. static char *my_basename
  77.  
  78. #ifdef __STDC__
  79.     (char *filename)
  80. #else
  81.     (filename)
  82.     char  *filename;
  83. #endif
  84. {
  85.     int   len = strlen(filename);
  86.     char  *p=NULL;
  87.  
  88.     for (p = filename + len - 1; len && !(DIRSEP(*p)); p--, len--)
  89.         ;
  90.     return ++p;
  91. }
  92.  
  93.  
  94.  
  95.  
  96. /*-----------------------------------------------------------------------------
  97.  * Print a usage message.
  98.  *----------------------------------------------------------------------------*/
  99. static void usage
  100.  
  101. #ifdef __STDC__
  102.     (void)
  103. #else
  104.     ()
  105. #endif
  106.  
  107. {
  108.     (void)fprintf(stderr,
  109.                   "\n\nUsage: %s [-h]\n       %s [-dv] REXX-script-name\n\n",
  110.                   Program, Program);
  111.     exit(BAD_ARGS);
  112. }
  113.  
  114. /*-----------------------------------------------------------------------------
  115.  * Processing starts here for stand-alone rexxsql executable...
  116.  *----------------------------------------------------------------------------*/
  117. int main
  118.  
  119. #if __STDC__
  120.     (int argc, char *argv[])
  121. #else
  122.     (argc, argv)
  123.     int   argc;
  124.     char  *argv[];
  125. #endif
  126.  
  127. {
  128.     int      c=0;
  129.     char     *ScriptName=NULL;
  130.     long     i=0, ArgCount=0;
  131. #if defined(OS2)
  132.     int      rc=0;
  133. #else
  134.     long     rc=0;
  135. #endif
  136.     RXSTRING retstr;
  137.     CHAR retbuf[250];
  138.     RXSTRING *Arg=NULL, *ArgList = (RXSTRING*)NULL;
  139.  
  140.     /* Get the name of this executable. */
  141.     Program = my_basename(argv[0]);
  142.  
  143.     /* Get any program options. */
  144.     while ((c = getopt(argc, argv, "dvh?")) != EOF) {
  145.         switch (c) {
  146.             case 'v': run_flags |= MODE_VERBOSE; break;
  147.             case 'd': run_flags |= MODE_DEBUG; break;
  148.             case 'h':
  149.             default : usage();
  150.         }
  151.     }
  152.  
  153.     /* Ensure that a script has been specified! */
  154.     if (optind >= argc) usage();
  155.  
  156.     /* Next argument is the name of the REXX script */
  157.     ScriptName = argv[optind++];
  158.  
  159.     /* Get number of arguments to the REXX script */
  160.     ArgCount = argc - optind;
  161.  
  162.     /* Build an array of arguments if any. */
  163.     if (ArgCount) {
  164.         if ((ArgList = (RXSTRING*)calloc((size_t)ArgCount, sizeof(RXSTRING)))
  165.                           == (RXSTRING*)NULL) {
  166.             (void)fprintf(stderr, "%s: out of memory\n", Program);
  167.             exit(REXX_FAIL);
  168.         }
  169.         for (Arg = ArgList, i = 0; i < ArgCount; Arg++, i++) {
  170.             Arg->strptr = argv[optind++];
  171.             Arg->strlength = strlen(Arg->strptr);
  172.         }
  173.     }
  174.  
  175.     /* Initialise the REXX/SQL interface. */
  176.     InitRexxSQL(Program);
  177.  
  178.     MAKERXSTRING(retstr,retbuf,sizeof(retbuf));
  179.     /*
  180.      * Execute the REXX script. Use RXSUBROUTINE mode so that an array
  181.      * of arguments can be passed to the REXX script. This allows passing
  182.      * strings containing spaces and is much more useful than RXCOMMAND
  183.      * mode which passes a command as a single string!
  184.      */
  185.     RexxStart(ArgCount, ArgList, ScriptName, NULL, DLLNAME, RXSUBROUTINE,
  186.               NULL,
  187. #if defined(OS2)
  188.               (PSHORT)&rc,
  189. #else
  190.               &rc, 
  191. #endif
  192.               (PRXSTRING)&retstr);
  193.  
  194.     /* Terminate the REXX/SQL interface. */
  195.     (void)TerminateRexxSQL(Program);
  196.  
  197.     if (ArgList)
  198.        free(ArgList);
  199.     /*
  200.      * Return the exit value from the script. This is useful for UNIX/DOS etc.
  201.      * if the value is kept to 0-success, small positive numbers (say < 100)
  202.      * to indicate errors!
  203.      */
  204.     return (int)rc;
  205. }
  206. #endif
  207.