xref: /sqlite-3.40.0/ext/wasm/version-info.c (revision 419e0d3e)
1 /*
2 ** 2022-10-16
3 **
4 ** The author disclaims copyright to this source code.  In place of a
5 ** legal notice, here is a blessing:
6 **
7 ** *   May you do good and not evil.
8 ** *   May you find forgiveness for yourself and forgive others.
9 ** *   May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 ** This file simply outputs sqlite3 version information in JSON form,
13 ** intended for embedding in the sqlite3 JS API build.
14 */
15 #ifdef TEST_VERSION
16 /*3029003 3039012*/
17 #define SQLITE_VERSION "X.Y.Z"
18 #define SQLITE_VERSION_NUMBER TEST_VERSION
19 #define SQLITE_SOURCE_ID "dummy"
20 #else
21 #include "sqlite3.h"
22 #endif
23 #include <stdio.h>
24 #include <string.h>
usage(const char * zAppName)25 static void usage(const char *zAppName){
26   puts("Emits version info about the sqlite3 it is built against.");
27   printf("Usage: %s [--quote] --INFO-FLAG:\n\n", zAppName);
28   puts("  --version          Emit SQLITE_VERSION (3.X.Y)");
29   puts("  --version-number   Emit SQLITE_VERSION_NUMBER (30XXYYZZ)");
30   puts("  --download-version Emit /download.html version number (3XXYYZZ)");
31   puts("  --source-id        Emit SQLITE_SOURCE_ID");
32   puts("  --json             Emit all info in JSON form");
33   puts("\nThe non-JSON formats may be modified by:\n");
34   puts("  --quote            Add double quotes around output.");
35 }
36 
main(int argc,char const * const * argv)37 int main(int argc, char const * const * argv){
38   int fJson = 0;
39   int fVersion = 0;
40   int fVersionNumber = 0;
41   int fDlVersion = 0;
42   int dlVersion = 0;
43   int fSourceInfo = 0;
44   int fQuote = 0;
45   int nFlags = 0;
46   int i;
47 
48   for( i = 1; i < argc; ++i ){
49     const char * zArg = argv[i];
50     while('-'==*zArg) ++zArg;
51     if( 0==strcmp("version", zArg) ){
52       fVersion = 1;
53     }else if( 0==strcmp("version-number", zArg) ){
54       fVersionNumber = 1;
55     }else if( 0==strcmp("download-version", zArg) ){
56       fDlVersion = 1;
57     }else if( 0==strcmp("source-id", zArg) ){
58       fSourceInfo = 1;
59     }else if( 0==strcmp("json", zArg) ){
60       fJson = 1;
61     }else if( 0==strcmp("quote", zArg) ){
62       fQuote = 1;
63       --nFlags;
64     }else{
65       printf("Unhandled flag: %s\n", argv[i]);
66       usage(argv[0]);
67       return 1;
68     }
69     ++nFlags;
70   }
71 
72   if( 0==nFlags ) fJson = 1;
73 
74   {
75     const int v = SQLITE_VERSION_NUMBER;
76     int ver[4] = {0,0,0,0};
77     ver[0] = (v / 1000000) * 1000000;
78     ver[1] = v % 1000000 / 100 * 1000;
79     ver[2] = v % 100 * 100;
80     dlVersion = ver[0] + ver[1] + ver[2] + ver[3];
81   }
82   if( fJson ){
83     printf("{\"libVersion\": \"%s\", "
84            "\"libVersionNumber\": %d, "
85            "\"sourceId\": \"%s\","
86            "\"downloadVersion\": %d}"/*missing newline is intentional*/,
87            SQLITE_VERSION,
88            SQLITE_VERSION_NUMBER,
89            SQLITE_SOURCE_ID,
90            dlVersion);
91   }else{
92     if(fQuote) printf("%c", '"');
93     if( fVersion ){
94       printf("%s", SQLITE_VERSION);
95     }else if( fVersionNumber ){
96       printf("%d", SQLITE_VERSION_NUMBER);
97     }else if( fSourceInfo ){
98       printf("%s", SQLITE_SOURCE_ID);
99     }else if( fDlVersion ){
100       printf("%d", dlVersion);
101     }
102     if(fQuote) printf("%c", '"');
103     puts("");
104   }
105   return 0;
106 }
107