home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / esqlc / esql / samples.c / gcutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-21  |  9.7 KB  |  322 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. //  FILE: gcutil.c
  4. //
  5. //      Generic Embedded SQL for C utilities
  6. //
  7. //  FUNCTIONS:
  8. //
  9. //      GetConnectToInfo() - Get Embedded SQL for C CONNECT TO information
  10. //      GetOption() - Get next command line option and parameter
  11. //
  12. //  COMMENTS:
  13. //
  14. //      Copyright (C) 1992 - 1994 Microsoft Corporation
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17.  
  18. #include <stddef.h>         // standard C run-time header
  19. #include <stdio.h>          // standard C run-time header
  20. #include <ctype.h>          // standard C run-time header
  21. #include <string.h>         // standard C run-time header
  22. #include "gcutil.h"         // utility header
  23.  
  24. // GLOBAL VARIABLES
  25.  
  26. ///////////////////////////////////////////////////////////////////////////////
  27. //
  28. //  FUNCTION: GetConnectToInfo()
  29. //
  30. //      Read the command-line arguments and prompt the user (if necessary) to
  31. //      get the [server_name.]database_name and login[.password] information
  32. //      required for the Embedded SQL for C CONNECT TO statement, and store 
  33. //      it in two caller-provided string buffers
  34. //
  35. //  PARAMETERS:
  36. //
  37. //      argc - count of command line args
  38. //      argv - array of command line argument strings
  39. //      pszServerDatabase - pointer to caller-provided string buffer for 
  40. //                          storing [server_name.]database_name
  41. //      pszLoginPassword - pointer to caller-provided string buffer for
  42. //                         storing login[.password]
  43. //
  44. //  RETURNS:
  45. //
  46. //      1 if successful, 0 if error occurs
  47. //
  48. //  COMMENTS:
  49. //
  50. ///////////////////////////////////////////////////////////////////////////////
  51.  
  52. int GetConnectToInfo (
  53.     int argc,
  54.     char** argv,
  55.     char* pszServerDatabase,
  56.     char* pszLoginPassword)
  57. {
  58.     char chOpt;                         // gotten option character
  59.     char* pszParam;                     // gotten parameter
  60.     char szLogin[SQLID_MAX+1] = "";     // login ID
  61.     char szPassword[SQLID_MAX+1] = "";  // password
  62.     char szServer[SQLID_MAX+1] = "";    // SQL Server
  63.     char szDatabase[SQLID_MAX+1] = "";  // database to use
  64.     int bLogin = FALSE,                 // flags for tracking options
  65.         bPassword = FALSE,
  66.         bServer = FALSE,
  67.         bDatabase = FALSE;
  68.  
  69.     // get command-line options and parameters
  70.     while (TRUE)
  71.     {
  72.         // all these command-line options are valid
  73.         //     {/s|/S} [sql_server]
  74.         //     {/u|/U} login_id
  75.         //     {/p|/P} [password]
  76.         //     {/d|/D} [database]
  77.  
  78.         chOpt = GetOption(argc, argv, "u:U:p:P:s:S:d:D:", &pszParam);
  79.         if (chOpt > 1)
  80.         {
  81.             // chOpt is valid argument
  82.             switch (chOpt)
  83.             {
  84.             case 'u':   // login ID
  85.             case 'U':
  86.                 bLogin = TRUE;
  87.                 if (pszParam != NULL)
  88.                 {
  89.                     strcpy(szLogin, pszParam);
  90.                 }
  91.                 break;
  92.             case 'p':   // password
  93.             case 'P':
  94.                 bPassword = TRUE;
  95.                 if (pszParam != NULL)
  96.                 {
  97.                     strcpy(szPassword, pszParam);
  98.                 }
  99.                 break;
  100.             case 's':   // SQL Server
  101.             case 'S':
  102.                 bServer = TRUE;
  103.                 if (pszParam != NULL)
  104.                 {
  105.                     strcpy(szServer, pszParam);
  106.                 }
  107.                 break;
  108.             case 'd':   // database
  109.             case 'D':
  110.                 bDatabase = TRUE;
  111.                 if (pszParam != NULL)
  112.                 {
  113.                     strcpy(szDatabase, pszParam);
  114.                 }
  115.                 break;
  116.             }
  117.         }
  118.         if (chOpt == 0)
  119.         {
  120.             // end of argument list
  121.             break;
  122.         }
  123.         if ((chOpt == 1) || (chOpt == -1))
  124.         {
  125.             // standalone param or error
  126.             printf("ERROR: Argument '%s' not recognized\n", pszParam);
  127.             break;
  128.         }
  129.     }
  130.  
  131.     if ((chOpt == 1) || (chOpt == -1))
  132.     {
  133.         // exit on error
  134.         return (0);
  135.     }
  136.  
  137.     // prompt for unspecified options
  138.     if (!bServer)
  139.     {
  140.         printf("Type the SQL Server to connect to: ");
  141.         gets(szServer);
  142.     }
  143.     if (!bLogin)
  144.     {
  145.         printf("Type your login ID: ");
  146.         gets(szLogin);
  147.     }
  148.     if (!bPassword)
  149.     {
  150.         printf("Type your password: ");
  151.         gets(szPassword);
  152.     }
  153.     if (!bDatabase)
  154.     {
  155.         printf("Type the database to use (default is 'pubs'): ");
  156.         gets(szDatabase);
  157.     }
  158.  
  159.     // set defaults
  160.     if (strlen(szDatabase) == 0)
  161.     {
  162.         strcpy(szDatabase, "pubs");
  163.     }
  164.     
  165.     printf("Using:\n");
  166.     printf("    SQL Server: %s\n", szServer);
  167.     printf("    Database: %s\n", szDatabase);
  168.     printf("    Login ID: %s\n", szLogin);
  169.     printf("    Password: %s\n", szPassword);
  170.  
  171.     // build variables for CONNECT TO statement
  172.     if (strlen(szServer) != 0)
  173.     {
  174.         strcat(pszServerDatabase, szServer);
  175.         strcat(pszServerDatabase, ".");
  176.     }
  177.     if (strlen(szDatabase) != 0)
  178.     {
  179.         strcat(pszServerDatabase, szDatabase);
  180.     }
  181.  
  182.     if (strlen(szLogin) != 0)
  183.     {
  184.         strcat(pszLoginPassword, szLogin);
  185.     }
  186.     if (strlen(szPassword) != 0)
  187.     {
  188.         strcat(pszLoginPassword, ".");
  189.         strcat(pszLoginPassword, szPassword);
  190.     }
  191.  
  192.     return (1);
  193. }
  194.  
  195. ///////////////////////////////////////////////////////////////////////////////
  196. //
  197. //  FUNCTION: GetOption()
  198. //
  199. //      Get next command line option and parameter
  200. //
  201. //  PARAMETERS:
  202. //
  203. //      argc - count of command line args
  204. //      argv - array of command line argument strings
  205. //      pszValidOpts - string of valid, case-sensitive option characters,
  206. //                     a colon ':' following a given character means that
  207. //                     option can take a parameter
  208. //      ppszParam - pointer to a pointer to a string for output
  209. //
  210. //  RETURNS:
  211. //
  212. //      If valid option is found, the character value of that option
  213. //          is returned, and *ppszParam points to the parameter if given,
  214. //          or is NULL if no param
  215. //      If standalone parameter (with no option) is found, 1 is returned,
  216. //          and *ppszParam points to the standalone parameter
  217. //      If option is found, but it is not in the list of valid options,
  218. //          -1 is returned, and *ppszParam points to the invalid argument
  219. //      When end of argument list is reached, 0 is returned, and
  220. //          *ppszParam is NULL
  221. //
  222. //  COMMENTS:
  223. //
  224. ///////////////////////////////////////////////////////////////////////////////
  225.  
  226. int GetOption (
  227.     int argc,
  228.     char** argv,
  229.     char* pszValidOpts,
  230.     char** ppszParam)
  231. {
  232.     static int iArg = 1;
  233.     char chOpt;
  234.     char* psz = NULL;
  235.     char* pszParam = NULL;
  236.  
  237.     if (iArg < argc)
  238.     {
  239.         psz = &(argv[iArg][0]);
  240.         if (*psz == '-' || *psz == '/')
  241.         {
  242.             // we have an option specifier
  243.             chOpt = argv[iArg][1];
  244.             if (isalnum(chOpt) || ispunct(chOpt))
  245.             {
  246.                 // we have an option character
  247.                 psz = strchr(pszValidOpts, chOpt);
  248.                 if (psz != NULL)
  249.                 {
  250.                     // option is valid, we want to return chOpt
  251.                     if (psz[1] == ':')
  252.                     {
  253.                         // option can have a parameter
  254.                         psz = &(argv[iArg][2]);
  255.                         if (*psz == '\0')
  256.                         {
  257.                             // must look at next argv for param
  258.                             if (iArg+1 < argc)
  259.                             {
  260.                                 psz = &(argv[iArg+1][0]);
  261.                                 if (*psz == '-' || *psz == '/')
  262.                                 {
  263.                                     // next argv is a new option, so param
  264.                                     // not given for current option
  265.                                 }
  266.                                 else
  267.                                 {
  268.                                     // next argv is the param
  269.                                     iArg++;
  270.                                     pszParam = psz;
  271.                                 }
  272.                             }
  273.                             else
  274.                             {
  275.                                 // reached end of args looking for param
  276.                             }
  277.  
  278.                         }
  279.                         else
  280.                         {
  281.                             // param is attached to option
  282.                             pszParam = psz;
  283.                         }
  284.                     }
  285.                     else
  286.                     {
  287.                         // option is alone, has no parameter
  288.                     }
  289.                 }
  290.                 else
  291.                 {
  292.                     // option specified is not in list of valid options
  293.                     chOpt = -1;
  294.                     pszParam = &(argv[iArg][0]);
  295.                 }
  296.             }
  297.             else
  298.             {
  299.                 // though option specifier was given, option character
  300.                 // is not alpha or was was not specified
  301.                 chOpt = -1;
  302.                 pszParam = &(argv[iArg][0]);
  303.             }
  304.         }
  305.         else
  306.         {
  307.             // standalone arg given with no option specifier
  308.             chOpt = 1;
  309.             pszParam = &(argv[iArg][0]);
  310.         }
  311.     }
  312.     else
  313.     {
  314.         // end of argument list
  315.         chOpt = 0;
  316.     }
  317.  
  318.     iArg++;
  319.     *ppszParam = pszParam;
  320.     return (chOpt);
  321. }
  322.