.ad 8
.bm 8
.fm 4
.bt $Copyright (c) 1997-2005 SAP AG$$Page %$
.tm 12
.hm 6
.hs 3
.tt 1 $SQL$Project Distributed Database System$VNI01C$
.tt 2 $$$
.tt 3 $$Argument$1997-05-06$
***********************************************************
.nf

.nf

.nf

    ========== licence begin  GPL
    Copyright (c) 1997-2005 SAP AG

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
    ========== licence end
.fo


.fo


.fo
.nf
.sp
Module  :       Argument
=========
.sp
Purpose :
.CM *-END-* purpose -------------------------------------
.sp
.cp 3
Define  :

.CM *-END-* define --------------------------------------
.sp ; .cp 3
Use     :

.CM *-END-* use -----------------------------------------
.sp ; .cp 3
Synonym :

.CM *-END-* synonym -------------------------------------
.sp ; .cp 3
Author  : FrankS
.sp
.cp 3
Created : 1990-06-12
.sp
.cp 3
Version : 1997-05-06
.sp
.cp 3
Release :  6.2 	 Date : 1997-05-06
.sp
Release :  6.2 	 Date : 1997-05-06
.sp
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Specification:

.CM *-END-* specification -------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.fo
.oc _/1
Description:

.CM *-END-* description ---------------------------------
.sp 2
***********************************************************
.sp
.cp 10
.nf
.oc _/1
Structure:

.CM *-END-* structure -----------------------------------
.sp 2
**********************************************************
.sp
.cp 10
.nf
.oc _/1
.CM -lll-
Code    :
/* PRETTY */

/*      vni01c                                                            */
/*                                                                        */
/*************************************************************
***                                                        ***
*** sql.cpc: Programm zum interaktiven Abarbeiten von      ***
***          SQL-Kommandos.                                ***
***                                                        ***
*** Modul:   argument.c                         ( vni01c ) ***
***                                                        ***
*** Benoetigte Files: sql.cpc                              ***
***                   argument.c                           ***
***                   form.c                               ***
***                   sql.h                                ***
***                                                        ***
*** Autor:   Detlef Engelbrecht                            ***
***          Frank Strassenburg                            ***
***                                                        ***
*** Datum:   12.06.1990                                    ***
***          05.11.1990                                    ***
***          11.03.1993                                    ***
***                                                        ***
*************************************************************/
/*==========================================================================*/

extern short optionflag;

extern char username [65];   /* username                     */
extern char password [65];   /* password                     */
extern char dbname   [65] ;  /* dbname                       */
extern char dbnode   [513] ; /* hostname                     */
extern char xuserkey [65] ;  /* xuserkey                     */
extern char language[41];    /* Language from set            */
extern char nullvalue[41];   /* Nullvalue from set           */
extern char sep[41];         /* Separator from set           */
extern char decimalrep[41];  /* Format for decimals from set */
extern char dat[41];         /* Format for dates from set    */
extern char tim[41];         /* Format for time from set     */
#if REL30
extern char dattim[41];      /* Format for time and date     */
#endif
extern FILE *fehlfile;       /* File fuer "stderr"           */

&if $OS in [ OS2,WIN32 ]
#if REL30
#define         getopt                  sql80_GetOpt
#define         optarg                  sql80_OptArg
#define         opterr                  sql80_OptErr
#define         optind                  sql80_OptInd
#endif
&endif


extern char *optarg;

static  void                    check_uid_and_pw () ;


tsp4_xuser_record               ni01_user_params ;

#define missing_quote           "missing quote in userid or password     "


/*==========================================================================*/

/* Ueberpruefung der uebergebenen Dezimalformatierung */
checkdecimal(str)
char *str;
{
    if (!str[1])
    {
	str[1] = *str;
	*str = 'N';
    }
    if (*str == str[1] || str[2]
	|| (str[1] != '.' && str[1] != ',')
	|| strspn(str,".,N") != strlen(str))
    {
	fputs("Only '.,' ',.' ',' '.' ',' '.' allowed!\n",stderr);
	exit(1);
    }
}

/*==========================================================================*/

/* Ueberpruefung der uebergebenen Zeitformatierung */
checktimeform(str)
char *str;
{
    char *ptr;
    int n, fehler;

    /* Alles gross schreiben */
    for (ptr=str;*ptr;ptr++) if (islower(*ptr)) *ptr=_toupper(*ptr);

    /* zuerst Stunde */
    for (ptr=str,n=fehler=0;*ptr && *ptr == 'H';)
    {
	ptr++;
	n++;
    }
    if (n != 2 && n != 4) fehler=1;
    else
    {
	/* ...dann Trennzeichen */
	for (n=0;*ptr && *ptr != 'M';)
	{
	    ptr++;
	    n++;
	}
	if (n > 1) fehler=2;
	else
	{
	    /* ...dann Minuten */
	    for (n=0;*ptr && *ptr == 'M';)
	    {
		ptr++;
		n++;
	    }
	    if (!n && *ptr || n && n != 2) fehler=3;
	    else
	    {
		/* ...dann Trennzeichen */
		for (n=0;*ptr && *ptr != 'S';)
		{
		    ptr++;
		    n++;
		}
		if (n > 1) fehler=4;
		else
		{
		    /* ...dann Sekunden */
		    for (n=0;*ptr && *ptr == 'S';)
		    {
			ptr++;
			n++;
		    }
		    if (!n && *ptr || n && n != 2) fehler=5;
		}
	    }
	}
    }
    if (fehler)
    {
	fprintf(stderr,"Illegal time format (%d)\n",fehler);
	exit(1);
    }
}

/*==========================================================================*/

/* Ueberpruefung der uebergebenen Zeitformatierung */
checkdateform ( str )
char * str ;
{
    char * ptr ;
    int n , m , fehler ;
    char array [ 3 ];

    fehler = 0 ;
    array [ 0 ] = array [ 1 ] = array [ 2 ] = 0 ;
    reformdate ( str );

    /* Check Laenge der einzelnen Datumsbestandteile */
    for (ptr=str;*ptr;ptr++)
	 if (*ptr=='0' || *ptr=='1' || *ptr=='2')
	     array[*ptr-'0']++;

    /* Stimmen die Teillaenge ? */
    if (array[0] && array[0] != 2 && array[0] != 4) fehler=1;
	else if (array[1] && array[1] != 2) fehler=2;
	    else if (array[2] && array[2] != 2) fehler=3;

    /* Check ob die einzelnen Datumsbestandteile in sich
       zusammenhaengend sind */
    for (ptr=str;!fehler && *ptr;ptr++)
	switch(*ptr)
	{
	case '0': /* Jahr */
	    if (strncmp(ptr,"0000",array[0])) fehler=4;
	    ptr += array[0];
	    ptr--;
	    break;
	case '1': /* Monat */
	    if (strncmp(ptr,"11",array[1])) fehler=5;
	    ptr += array[1];
	    ptr--;
	    break;
	case '2': /* Tag */
	    if (strncmp(ptr,"22",array[2])) fehler=6;
	    ptr += array[2];
	    ptr--;
	    break;
	}

    /* fehlen noch die Separatoren */
    if (!fehler)
    {
	/* Separator am Anfang oder Ende? */
	n = strlen(str);
	n--;
	if (*str != '0' && *str != '1' && *str != '2'
	    || str[n]!='0' && str[n]!='1' && str[n]!='2')
		fehler=7;
    }

    /* Anzahl Separatoren */
    for (ptr=str;!fehler && *ptr;ptr++)
    {
	/* n = Anzahl Separatorenstrings */
	if (*ptr != '0' && *ptr != '1' && *ptr != '2')
	{
	    for(n=0;*ptr && *ptr!='0' && *ptr!='1' && *ptr!='2';)
	    {
		ptr++;
		n++;
	    }
	    if (n>2) fehler=8;
	    else if (n>1 && *(ptr-n) == *(ptr-n+1)) fehler=9;
	}
    }
    if (fehler)
    {
	fprintf(stderr,"Illegal date format (%d)\n",fehler);
	exit(1);
    }
    for (ptr=str;*ptr;ptr++)
	if (*ptr == '0') *ptr = 'Y';
	else if (*ptr == '1') *ptr = 'M';
	     else if (*ptr == '2') *ptr = 'D';
}

#if REL30
/*==========================================================================*/

/* Ueberpruefung der uebergebenen Zeitformatierung */
checkdtform ( str )
char * str ;
{
    char *ptr;

    /* Alles gross schreiben */
    for (ptr=str;*ptr;ptr++) if (islower(*ptr)) *ptr=_toupper(*ptr);

    if ( ! strcmp ( str , "INTERNAL" ) )
    {
	strcpy ( dat , "yyyy.mm.dd" ) ;
	strcpy ( tim , "hh:mm:ss" ) ;
    }

    if ( ! strcmp ( str , "ISO" ) )
    {
	strcpy ( dat , "yyyy-mm-dd" ) ;
	strcpy ( tim , "hh.mm.ss" ) ;
    }

    if ( ! strcmp ( str , "USA" ) )
    {
	strcpy ( dat , "mm/dd/yyyy" ) ;
	strcpy ( tim , "hh:mm:ss" ) ;
    }

    if ( ! strcmp ( str , "EUR" ) )
    {
	strcpy ( dat , "dd.mm.yyyy" ) ;
	strcpy ( tim , "hh.mm.ss" ) ;
    }

    if ( ! strcmp ( str , "JIS" ) )
    {
	strcpy ( dat , "yyyy-mm-dd" ) ;
	strcpy ( tim , "hh:mm:ss" ) ;
    }

}
#endif
/*==========================================================================*/

usage(art)
int art;
{
    puts("Usage: xsql [-u <user>,<password>]");
    puts("            [-d <database name>]");
    puts("            [-n <host name>]");
    puts("            [-U <xuser key>]");
    puts("            [-D <date/time format>]");
    puts("            [-J <separator>  ] [-p <decimal format>]");
    puts("            [-g <null value> ] [-hTaQzKkbf]");
    puts("\nType 'xsql -h' for an explanation of the options.");
    if (art) exit(1);
}

/*==========================================================================*/

explain()
{
    usage(0);
    puts("\nxsql:     Read and execute SQL commands. If no argument other");
    puts("          than the options are specified, it reads from standard");
    puts("          input; end with ^D or by typing exit.\n");
    puts("Options: -h Show this explanation");
    puts("         -T Write trace file");
    puts("         -K Protocol commands to standard out");
    puts("         -u <user name>,<password>:");
    puts("            User to be connected to the database.");
    puts("            If no -u option is specified, xsql attempts to get the");
    puts("            user name and password from the xuser file.");
    puts("         -d <database name>:");
    puts("            If no -d option is specified, xsql attempts to get the");
    puts("            database name from the xuser file.");
    puts("         -n <host name>:");
    puts("            If no -n option is specified, xsql attempts to get the");
    puts("            host name from the xuser file.");
    puts("         -U <xuser key>:");
    puts("            If no -U option is specified, xsql attempts to get the");
    puts("            connection from the DEFAULT xuser entry.");
    puts("         -D <date/time format>:");
    puts("            Use this date and/or time format instead of the");
    puts("            one specified in the database set command.");
    puts("            (INTERNAL, ISO, USA, EUR, JIS )");
    puts("         -J <separator>: Use this separator instead of the");
    puts("            one specified in the database set command.");
    puts("         -p <decimal format>: Use this decimal format instead of the");
    puts("            one specified in the database set command.");
    puts("         -g <null value>: Use this null value instead of the");
    puts("            one specified in the database set command.");
    puts("         -z No auto commit work.");
    puts("         -a Do not print any header for select and show commands.");
    puts("         -Q Show each column on a separate line.");
    puts("         -b batch mode. Don't display prompt.");
    puts("         -f protfile mode. Commands are written to sql.prot.");
    puts("         -k Rollback work in case of errors.");
    puts("            (-k has no effect, if -z is not specified)");
    puts("\n       UNIX commands can be executed with !<command>");
    exit(0);
}

/*==========================================================================*/

getargument(number,ptr)
int number;
char *ptr[];
{
    int                 rc ;
    extern int          optind ;
    extern int          opterr ;
    BOOLEAN             ok ;
    tsp00_C40             errtext ;


    /* ***** initialize connect parameters ***** */

    dbname   [ 0 ] = '\0' ;
    username [ 0 ] = '\0' ;
    password [ 0 ] = '\0' ;
    strcpy(xuserkey, "DEFAULT");

    opterr=0;
    optind=1;
    while((rc=getopt(number,ptr,"hTafQbU:D:zp:Kg:kJ:u:d:n:")) != -1)
    {
        switch(rc)
        {
	    /*  FSt: brauch man nicht mehr, da diese Argumente mit "SQLOPT"
	     *  implizit uebergeben werden
	     */
	case  'T': /* zeige Erklaerung */
	    DBG (("getargument     : -T option is set\n",0 ))
	    break;
	case  'h': /* Help Function */
	    explain ();
	    DBG (("getargument     : -h option is set\n",0 ))
	    break;
	case  'D': /* Datumsdarstellung, Zeitdarstellung */
	    strncpy(dattim,optarg,40);
	    dattim [ 40 ] =  '\0' ;
	    checkdtform ( dattim );
	    DBG (("getargument     : dattim                         = >%s<\n",
				     dattim ))
	    DBG (("getargument     : -D option is set\n",0 ))
	    break;
	case  'K': /* Befehle werden auf stdout protokolliert */
	    optionflag |= PROTOKOLL;
	    DBG (("getargument     : -K option is set\n",0 ))
	    break;
	case  'z': /* kein automatisches Commit */
	    optionflag |= NOAUTOCOMMIT;
	    DBG (("getargument     : -z option is set\n",0 ))
	    break;
	case  'p': /* Dezimaldarstellung */
            strncpy(decimalrep,optarg,40);
            decimalrep[40] = '\0';
            checkdecimal(decimalrep);
	    DBG (("getargument     : -p option is set\n",0 ))
	    break;
	case  'a': /* drucke Header */
	    optionflag |= NOHEADER;
	    DBG (("getargument     : -a option is set\n",0 ))
	    break;
	case  'Q': /* Column Format */
	    optionflag |= COLUMNFORM ;
	    optionflag |= NOHEADER;
	    DBG (("getargument     : -Q option is set\n",0 ))
	    break;
	case  'b': /* batch mode: no prompt */
	    optionflag |= BATCHMODE ;
	    DBG (("getargument     : -b option is set\n",0 ))
	    break;
	case  'f': /* sql.prot wird geschrieben */
	    optionflag |= PROTMODE ;
	    DBG (("getargument     : -f option is set\n",0 ))
	    break;
	case  'g': /* Nullwertdarstellung */
            strncpy(nullvalue,optarg,40);
            nullvalue[40] = '\0';
	    DBG (("getargument     : -g option is set\n",0 ))
	    break;
	case  'k': /* Rollback im Fehlerfall */
	    optionflag |= ROLLBACK;
	    DBG (("getargument     : -k option is set\n",0 ))
	    break;
	case  'J': /* definiere neuen Separator */
            strncpy(sep,optarg,40);
	    sep[40] = '\0';
	    DBG (("getargument     : -J option is set\n",0 ))
	    break;
	case  'u': /* Benutzername, Benutzerpasswort */
	    /*  FSt: brauch man nicht mehr, da diese Argumente mit "SQLOPT"
	     *  implizit uebergeben werden
	    DBG (("getargument     : username,password , optind     = %d\n",
				     optind   ))
	     */
	    username [0] = '\0';
	    password [0] = '\0';
	    DBG (("getargument     : username                       = >%s<\n",
				     username ))
	    DBG (("getargument     : password                       = >%s<\n",
				     password ))
	    break;
	case  'd': /* Datenbankname */
          strncpy ( dbname , optarg , 64 );
          break;
	case  'n':
          strncpy(dbnode, optarg, 512);
          break;
	case  'U':
          strncpy(xuserkey, optarg, 64);
          break;
	default  : /* Falsches Argument */
	    usage (1);
	}
    }
    return(optind);
}


.CM *-END-* code ----------------------------------------
.SP 2
***********************************************************
*-PRETTY-*  statements    :        446
*-PRETTY-*  lines of code :        446        PRETTY  3.09
*-PRETTY-*  lines in file :        533         1992-11-23
.PA
