Fossil

Diff
Login

Differences From Artifact [ae215f9af5]:

To Artifact [4430e80f5b]:


11
12
13
14
15
16
17

18
19
20
21
22
23
24
25
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
**

** An implementation of printf() with extra conversion fields.
*/
#include "config.h"
#include "printf.h"

/*
** Conversion types fall into various categories as defined by the
** following enumeration.







>
|







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*******************************************************************************
**
** This file contains implementions of routines for formatting output
** (ex: mprintf()) and for output to the console.
*/
#include "config.h"
#include "printf.h"

/*
** Conversion types fall into various categories as defined by the
** following enumeration.
886
887
888
889
890
891
892

893





894
895


896
897
898
899

900




901



902
903



904
905
906


907
908
909
910




911
912
913
914
915
916



917

918
919
920
921

922

923
924
925







926



927










928
929
930

931
932




933
934











935





936


937
938
939

940
941
942
943



944
945
946










947

948
949

950



951
952
953
954







955


956
957
  b = empty_blob;
  vxprintf(&b, zFormat, ap);
  fossil_puts(blob_str(&b), 1);
  blob_reset(&b);
  va_end(ap);
}


/*





** Like strcmp() except that it accepts NULL pointers.  NULL sorts before
** all non-NULL string pointers.  Also, this strcmp() is a binary comparison


** that does not consider locale.
*/
int fossil_strcmp(const char *zA, const char *zB){
  if( zA==0 ){

    if( zB==0 ) return 0;




    return -1;



  }else if( zB==0 ){
    return +1;



  }else{
    int a, b;
    do{


      a = *zA++;
      b = *zB++;
    }while( a==b && a!=0 );
    return ((unsigned char)a) - (unsigned char)b;




  }
}
int fossil_strncmp(const char *zA, const char *zB, int nByte){
  if( zA==0 ){
    if( zB==0 ) return 0;
    return -1;



  }else if( zB==0 ){

    return +1;
  }else if( nByte>0 ){
    int a, b;
    do{

      a = *zA++;

      b = *zB++;
    }while( a==b && a!=0 && (--nByte)>0 );
    return ((unsigned char)a) - (unsigned char)b;







  }else{



    return 0;










  }
}


/*
** Case insensitive string comparison.




*/
int fossil_strnicmp(const char *zA, const char *zB, int nByte){











  if( zA==0 ){





    if( zB==0 ) return 0;


    return -1;
  }else if( zB==0 ){
    return +1;

  }
  if( nByte<0 ) nByte = strlen(zB);
  return sqlite3_strnicmp(zA, zB, nByte);
}



int fossil_stricmp(const char *zA, const char *zB){
  int nByte;
  int rc;










  if( zA==0 ){

    if( zB==0 ) return 0;
    return -1;

  }else if( zB==0 ){



    return +1;
  }
  nByte = strlen(zB);
  rc = sqlite3_strnicmp(zA, zB, nByte);







  if( rc==0 && zA[nByte] ) rc = 1;


  return rc;
}







>

>
>
>
>
>
|
<
>
>
|

|
|
>
|
>
>
>
>
|
>
>
>
|
<
>
>
>
|
<
|
>
>
|
<
<
<
>
>
>
>
|
|
<
<
<
|
>
>
>
|
>
|
<
|
<
>
|
>
|
<
|
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
|
<
|
>
|
|
>
>
>
>

|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
|
>
>
|
|
<
>
|
<
<
|
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
|
>
|
<
>
|
>
>
>
|
|
|
<
>
>
>
>
>
>
>
|
>
>
|

887
888
889
890
891
892
893
894
895
896
897
898
899
900
901

902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918

919
920
921
922

923
924
925
926



927
928
929
930
931
932



933
934
935
936
937
938
939

940

941
942
943
944

945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968

969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000

1001
1002


1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022

1023
1024
1025
1026
1027
1028
1029
1030

1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  b = empty_blob;
  vxprintf(&b, zFormat, ap);
  fossil_puts(blob_str(&b), 1);
  blob_reset(&b);
  va_end(ap);
}


/*
** The following variable becomes true while processing a fatal error
** or a panic.  If additional "recursive-fatal" errors occur while
** shutting down, the recursive errors are silently ignored.
*/
static int mainInFatalError = 0;


/*
** Print an error message, rollback all databases, and quit.  These
** routines never return.
*/
NORETURN void fossil_panic(const char *zFormat, ...){
  char *z;
  va_list ap;
  int rc = 1;
  static int once = 1;
  mainInFatalError = 1;
  va_start(ap, zFormat);
  z = vmprintf(zFormat, ap);
  va_end(ap);
#ifdef FOSSIL_ENABLE_JSON
  if( g.json.isJsonMode ){
    json_err( 0, z, 1 );
    if( g.isHTTP ){

      rc = 0 /* avoid HTTP 500 */;
    }
  }
  else

#endif
  {
    if( g.cgiOutput && once ){
      once = 0;



      cgi_printf("<p class=\"generalError\">%h</p>", z);
      cgi_reply();
    }else if( !g.fQuiet ){
      fossil_trace("%s: %s\n", g.argv[0], z);
    }
  }



  free(z);
  db_force_rollback();
  fossil_exit(rc);
}

NORETURN void fossil_fatal(const char *zFormat, ...){
  char *z;

  int rc = 1;

  va_list ap;
  mainInFatalError = 1;
  va_start(ap, zFormat);
  z = vmprintf(zFormat, ap);

  va_end(ap);
#ifdef FOSSIL_ENABLE_JSON
  if( g.json.isJsonMode ){
    json_err( g.json.resultCode, z, 1 );
    if( g.isHTTP ){
      rc = 0 /* avoid HTTP 500 */;
    }
  }
  else
#endif
  {
    if( g.cgiOutput ){
      g.cgiOutput = 0;
      cgi_printf("<p class=\"generalError\">\n%h\n</p>\n", z);
      cgi_reply();
    }else if( !g.fQuiet ){
      fossil_trace("%s: %s\n", g.argv[0], z);
    }
  }
  free(z);
  db_force_rollback();
  fossil_exit(rc);
}


/* This routine works like fossil_fatal() except that if called
** recursively, the recursive call is a no-op.
**
** Use this in places where an error might occur while doing
** fatal error shutdown processing.  Unlike fossil_panic() and
** fossil_fatal() which never return, this routine might return if
** the fatal error handing is already in process.  The caller must
** be prepared for this routine to return.
*/
void fossil_fatal_recursive(const char *zFormat, ...){
  char *z;
  va_list ap;
  int rc = 1;
  if( mainInFatalError ) return;
  mainInFatalError = 1;
  va_start(ap, zFormat);
  z = vmprintf(zFormat, ap);
  va_end(ap);
#ifdef FOSSIL_ENABLE_JSON
  if( g.json.isJsonMode ){
    json_err( g.json.resultCode, z, 1 );
    if( g.isHTTP ){
      rc = 0 /* avoid HTTP 500 */;
    }
  } else
#endif
  {
    if( g.cgiOutput ){
      g.cgiOutput = 0;
      cgi_printf("<p class=\"generalError\">\n%h\n</p>\n", z);
      cgi_reply();
    }else{

      fossil_trace("%s: %s\n", g.argv[0], z);
    }


  }
  db_force_rollback();
  fossil_exit(rc);
}


/* Print a warning message */
void fossil_warning(const char *zFormat, ...){
  char *z;
  va_list ap;
  va_start(ap, zFormat);
  z = vmprintf(zFormat, ap);
  va_end(ap);
#ifdef FOSSIL_ENABLE_JSON
  if(g.json.isJsonMode){
    json_warn( FSL_JSON_W_UNKNOWN, z );
  }else
#endif
  {
    if( g.cgiOutput ){

      cgi_printf("<p class=\"generalError\">\n%h\n</p>\n", z);
    }else{
      fossil_trace("%s: %s\n", g.argv[0], z);
    }
  }
  free(z);
}


/*
** Turn off any NL to CRNL translation on the stream given as an
** argument.  This is a no-op on unix but is necessary on windows.
*/
void fossil_binary_mode(FILE *p){
#if defined(_WIN32)
  _setmode(_fileno(p), _O_BINARY);
#endif
#ifdef __EMX__     /* OS/2 */
  setmode(fileno(p), O_BINARY);
#endif
}