home *** CD-ROM | disk | FTP | other *** search
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FILE: gcutil.c
- //
- // Generic Embedded SQL for C utilities
- //
- // FUNCTIONS:
- //
- // GetConnectToInfo() - Get Embedded SQL for C CONNECT TO information
- // GetOption() - Get next command line option and parameter
- //
- // COMMENTS:
- //
- // Copyright (C) 1992 - 1994 Microsoft Corporation
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- #include <stddef.h> // standard C run-time header
- #include <stdio.h> // standard C run-time header
- #include <ctype.h> // standard C run-time header
- #include <string.h> // standard C run-time header
- #include "gcutil.h" // utility header
-
- // GLOBAL VARIABLES
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: GetConnectToInfo()
- //
- // Read the command-line arguments and prompt the user (if necessary) to
- // get the [server_name.]database_name and login[.password] information
- // required for the Embedded SQL for C CONNECT TO statement, and store
- // it in two caller-provided string buffers
- //
- // PARAMETERS:
- //
- // argc - count of command line args
- // argv - array of command line argument strings
- // pszServerDatabase - pointer to caller-provided string buffer for
- // storing [server_name.]database_name
- // pszLoginPassword - pointer to caller-provided string buffer for
- // storing login[.password]
- //
- // RETURNS:
- //
- // 1 if successful, 0 if error occurs
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- int GetConnectToInfo (
- int argc,
- char** argv,
- char* pszServerDatabase,
- char* pszLoginPassword)
- {
- char chOpt; // gotten option character
- char* pszParam; // gotten parameter
- char szLogin[SQLID_MAX+1] = ""; // login ID
- char szPassword[SQLID_MAX+1] = ""; // password
- char szServer[SQLID_MAX+1] = ""; // SQL Server
- char szDatabase[SQLID_MAX+1] = ""; // database to use
- int bLogin = FALSE, // flags for tracking options
- bPassword = FALSE,
- bServer = FALSE,
- bDatabase = FALSE;
-
- // get command-line options and parameters
- while (TRUE)
- {
- // all these command-line options are valid
- // {/s|/S} [sql_server]
- // {/u|/U} login_id
- // {/p|/P} [password]
- // {/d|/D} [database]
-
- chOpt = GetOption(argc, argv, "u:U:p:P:s:S:d:D:", &pszParam);
- if (chOpt > 1)
- {
- // chOpt is valid argument
- switch (chOpt)
- {
- case 'u': // login ID
- case 'U':
- bLogin = TRUE;
- if (pszParam != NULL)
- {
- strcpy(szLogin, pszParam);
- }
- break;
- case 'p': // password
- case 'P':
- bPassword = TRUE;
- if (pszParam != NULL)
- {
- strcpy(szPassword, pszParam);
- }
- break;
- case 's': // SQL Server
- case 'S':
- bServer = TRUE;
- if (pszParam != NULL)
- {
- strcpy(szServer, pszParam);
- }
- break;
- case 'd': // database
- case 'D':
- bDatabase = TRUE;
- if (pszParam != NULL)
- {
- strcpy(szDatabase, pszParam);
- }
- break;
- }
- }
- if (chOpt == 0)
- {
- // end of argument list
- break;
- }
- if ((chOpt == 1) || (chOpt == -1))
- {
- // standalone param or error
- printf("ERROR: Argument '%s' not recognized\n", pszParam);
- break;
- }
- }
-
- if ((chOpt == 1) || (chOpt == -1))
- {
- // exit on error
- return (0);
- }
-
- // prompt for unspecified options
- if (!bServer)
- {
- printf("Type the SQL Server to connect to: ");
- gets(szServer);
- }
- if (!bLogin)
- {
- printf("Type your login ID: ");
- gets(szLogin);
- }
- if (!bPassword)
- {
- printf("Type your password: ");
- gets(szPassword);
- }
- if (!bDatabase)
- {
- printf("Type the database to use (default is 'pubs'): ");
- gets(szDatabase);
- }
-
- // set defaults
- if (strlen(szDatabase) == 0)
- {
- strcpy(szDatabase, "pubs");
- }
-
- printf("Using:\n");
- printf(" SQL Server: %s\n", szServer);
- printf(" Database: %s\n", szDatabase);
- printf(" Login ID: %s\n", szLogin);
- printf(" Password: %s\n", szPassword);
-
- // build variables for CONNECT TO statement
- if (strlen(szServer) != 0)
- {
- strcat(pszServerDatabase, szServer);
- strcat(pszServerDatabase, ".");
- }
- if (strlen(szDatabase) != 0)
- {
- strcat(pszServerDatabase, szDatabase);
- }
-
- if (strlen(szLogin) != 0)
- {
- strcat(pszLoginPassword, szLogin);
- }
- if (strlen(szPassword) != 0)
- {
- strcat(pszLoginPassword, ".");
- strcat(pszLoginPassword, szPassword);
- }
-
- return (1);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // FUNCTION: GetOption()
- //
- // Get next command line option and parameter
- //
- // PARAMETERS:
- //
- // argc - count of command line args
- // argv - array of command line argument strings
- // pszValidOpts - string of valid, case-sensitive option characters,
- // a colon ':' following a given character means that
- // option can take a parameter
- // ppszParam - pointer to a pointer to a string for output
- //
- // RETURNS:
- //
- // If valid option is found, the character value of that option
- // is returned, and *ppszParam points to the parameter if given,
- // or is NULL if no param
- // If standalone parameter (with no option) is found, 1 is returned,
- // and *ppszParam points to the standalone parameter
- // If option is found, but it is not in the list of valid options,
- // -1 is returned, and *ppszParam points to the invalid argument
- // When end of argument list is reached, 0 is returned, and
- // *ppszParam is NULL
- //
- // COMMENTS:
- //
- ///////////////////////////////////////////////////////////////////////////////
-
- int GetOption (
- int argc,
- char** argv,
- char* pszValidOpts,
- char** ppszParam)
- {
- static int iArg = 1;
- char chOpt;
- char* psz = NULL;
- char* pszParam = NULL;
-
- if (iArg < argc)
- {
- psz = &(argv[iArg][0]);
- if (*psz == '-' || *psz == '/')
- {
- // we have an option specifier
- chOpt = argv[iArg][1];
- if (isalnum(chOpt) || ispunct(chOpt))
- {
- // we have an option character
- psz = strchr(pszValidOpts, chOpt);
- if (psz != NULL)
- {
- // option is valid, we want to return chOpt
- if (psz[1] == ':')
- {
- // option can have a parameter
- psz = &(argv[iArg][2]);
- if (*psz == '\0')
- {
- // must look at next argv for param
- if (iArg+1 < argc)
- {
- psz = &(argv[iArg+1][0]);
- if (*psz == '-' || *psz == '/')
- {
- // next argv is a new option, so param
- // not given for current option
- }
- else
- {
- // next argv is the param
- iArg++;
- pszParam = psz;
- }
- }
- else
- {
- // reached end of args looking for param
- }
-
- }
- else
- {
- // param is attached to option
- pszParam = psz;
- }
- }
- else
- {
- // option is alone, has no parameter
- }
- }
- else
- {
- // option specified is not in list of valid options
- chOpt = -1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // though option specifier was given, option character
- // is not alpha or was was not specified
- chOpt = -1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // standalone arg given with no option specifier
- chOpt = 1;
- pszParam = &(argv[iArg][0]);
- }
- }
- else
- {
- // end of argument list
- chOpt = 0;
- }
-
- iArg++;
- *ppszParam = pszParam;
- return (chOpt);
- }
-