xref: /sqlite-3.40.0/ext/wasm/version-info.c (revision 419e0d3e)
1cdc9ddc6Sstephan /*
2cdc9ddc6Sstephan ** 2022-10-16
3cdc9ddc6Sstephan **
4cdc9ddc6Sstephan ** The author disclaims copyright to this source code.  In place of a
5cdc9ddc6Sstephan ** legal notice, here is a blessing:
6cdc9ddc6Sstephan **
7cdc9ddc6Sstephan ** *   May you do good and not evil.
8cdc9ddc6Sstephan ** *   May you find forgiveness for yourself and forgive others.
9cdc9ddc6Sstephan ** *   May you share freely, never taking more than you give.
10cdc9ddc6Sstephan **
11cdc9ddc6Sstephan *************************************************************************
12cdc9ddc6Sstephan ** This file simply outputs sqlite3 version information in JSON form,
13cdc9ddc6Sstephan ** intended for embedding in the sqlite3 JS API build.
14cdc9ddc6Sstephan */
15*419e0d3eSstephan #ifdef TEST_VERSION
16*419e0d3eSstephan /*3029003 3039012*/
17*419e0d3eSstephan #define SQLITE_VERSION "X.Y.Z"
18*419e0d3eSstephan #define SQLITE_VERSION_NUMBER TEST_VERSION
19*419e0d3eSstephan #define SQLITE_SOURCE_ID "dummy"
20*419e0d3eSstephan #else
21cdc9ddc6Sstephan #include "sqlite3.h"
22*419e0d3eSstephan #endif
231b8c2467Sstephan #include <stdio.h>
241b8c2467Sstephan #include <string.h>
usage(const char * zAppName)251b8c2467Sstephan static void usage(const char *zAppName){
261b8c2467Sstephan   puts("Emits version info about the sqlite3 it is built against.");
271b8c2467Sstephan   printf("Usage: %s [--quote] --INFO-FLAG:\n\n", zAppName);
281b8c2467Sstephan   puts("  --version          Emit SQLITE_VERSION (3.X.Y)");
291b8c2467Sstephan   puts("  --version-number   Emit SQLITE_VERSION_NUMBER (30XXYYZZ)");
30*419e0d3eSstephan   puts("  --download-version Emit /download.html version number (3XXYYZZ)");
311b8c2467Sstephan   puts("  --source-id        Emit SQLITE_SOURCE_ID");
321b8c2467Sstephan   puts("  --json             Emit all info in JSON form");
331b8c2467Sstephan   puts("\nThe non-JSON formats may be modified by:\n");
341b8c2467Sstephan   puts("  --quote            Add double quotes around output.");
351b8c2467Sstephan }
361b8c2467Sstephan 
main(int argc,char const * const * argv)37cdc9ddc6Sstephan int main(int argc, char const * const * argv){
381b8c2467Sstephan   int fJson = 0;
391b8c2467Sstephan   int fVersion = 0;
401b8c2467Sstephan   int fVersionNumber = 0;
41*419e0d3eSstephan   int fDlVersion = 0;
42*419e0d3eSstephan   int dlVersion = 0;
431b8c2467Sstephan   int fSourceInfo = 0;
441b8c2467Sstephan   int fQuote = 0;
451b8c2467Sstephan   int nFlags = 0;
461b8c2467Sstephan   int i;
471b8c2467Sstephan 
481b8c2467Sstephan   for( i = 1; i < argc; ++i ){
491b8c2467Sstephan     const char * zArg = argv[i];
501b8c2467Sstephan     while('-'==*zArg) ++zArg;
511b8c2467Sstephan     if( 0==strcmp("version", zArg) ){
521b8c2467Sstephan       fVersion = 1;
531b8c2467Sstephan     }else if( 0==strcmp("version-number", zArg) ){
541b8c2467Sstephan       fVersionNumber = 1;
55*419e0d3eSstephan     }else if( 0==strcmp("download-version", zArg) ){
56*419e0d3eSstephan       fDlVersion = 1;
571b8c2467Sstephan     }else if( 0==strcmp("source-id", zArg) ){
581b8c2467Sstephan       fSourceInfo = 1;
591b8c2467Sstephan     }else if( 0==strcmp("json", zArg) ){
601b8c2467Sstephan       fJson = 1;
611b8c2467Sstephan     }else if( 0==strcmp("quote", zArg) ){
621b8c2467Sstephan       fQuote = 1;
631b8c2467Sstephan       --nFlags;
641b8c2467Sstephan     }else{
651b8c2467Sstephan       printf("Unhandled flag: %s\n", argv[i]);
661b8c2467Sstephan       usage(argv[0]);
671b8c2467Sstephan       return 1;
681b8c2467Sstephan     }
691b8c2467Sstephan     ++nFlags;
701b8c2467Sstephan   }
711b8c2467Sstephan 
721b8c2467Sstephan   if( 0==nFlags ) fJson = 1;
73*419e0d3eSstephan 
74*419e0d3eSstephan   {
75*419e0d3eSstephan     const int v = SQLITE_VERSION_NUMBER;
76*419e0d3eSstephan     int ver[4] = {0,0,0,0};
77*419e0d3eSstephan     ver[0] = (v / 1000000) * 1000000;
78*419e0d3eSstephan     ver[1] = v % 1000000 / 100 * 1000;
79*419e0d3eSstephan     ver[2] = v % 100 * 100;
80*419e0d3eSstephan     dlVersion = ver[0] + ver[1] + ver[2] + ver[3];
81*419e0d3eSstephan   }
821b8c2467Sstephan   if( fJson ){
83cdc9ddc6Sstephan     printf("{\"libVersion\": \"%s\", "
84cdc9ddc6Sstephan            "\"libVersionNumber\": %d, "
85*419e0d3eSstephan            "\"sourceId\": \"%s\","
86*419e0d3eSstephan            "\"downloadVersion\": %d}"/*missing newline is intentional*/,
87cdc9ddc6Sstephan            SQLITE_VERSION,
88cdc9ddc6Sstephan            SQLITE_VERSION_NUMBER,
89*419e0d3eSstephan            SQLITE_SOURCE_ID,
90*419e0d3eSstephan            dlVersion);
911b8c2467Sstephan   }else{
921b8c2467Sstephan     if(fQuote) printf("%c", '"');
931b8c2467Sstephan     if( fVersion ){
941b8c2467Sstephan       printf("%s", SQLITE_VERSION);
951b8c2467Sstephan     }else if( fVersionNumber ){
961b8c2467Sstephan       printf("%d", SQLITE_VERSION_NUMBER);
971b8c2467Sstephan     }else if( fSourceInfo ){
981b8c2467Sstephan       printf("%s", SQLITE_SOURCE_ID);
99*419e0d3eSstephan     }else if( fDlVersion ){
100*419e0d3eSstephan       printf("%d", dlVersion);
1011b8c2467Sstephan     }
1021b8c2467Sstephan     if(fQuote) printf("%c", '"');
1031b8c2467Sstephan     puts("");
1041b8c2467Sstephan   }
105cdc9ddc6Sstephan   return 0;
106cdc9ddc6Sstephan }
107