From 2752d1331dd76d418ee1df0a79c3c1eca758ac5c Mon Sep 17 00:00:00 2001 From: stephan Date: Mon, 30 Jun 2025 15:21:46 +0000 Subject: [PATCH] Add support for using $XDG_STATE_HOME/sqlite_history or ~/.local/state/sqlite_history before fallback back to the historical default of ~/.sqlite_history. Update sqlite3.1 (man page) with the new semantics. FossilOrigin-Name: 6b780655ccd68d068b6ee6621e56ac94c000916b7341651dc79725a0800cd1c0 --- manifest | 17 ++++----- manifest.uuid | 2 +- sqlite3.1 | 28 ++++++++++++--- src/shell.c.in | 97 +++++++++++++++++++++++++++++--------------------- 4 files changed, 89 insertions(+), 55 deletions(-) diff --git a/manifest b/manifest index 1ff571dcf0..3c96f8063d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Preliminary\srefactoring\sof\sthe\sXDG_CONFIG_HOME\ssupport\sto\ssupport\sthe\spending\saddition\sof\sother\sXDG-configurable\soptions.\sIn\sresponse\sto\s[forum:31db1a23f9\s|\sforum\spost\s31db1a23f9]. -D 2025-06-30T11:46:10.395 +C Add\ssupport\sfor\susing\s$XDG_STATE_HOME/sqlite_history\sor\s~/.local/state/sqlite_history\sbefore\sfallback\sback\sto\sthe\shistorical\sdefault\sof\s~/.sqlite_history.\sUpdate\ssqlite3.1\s(man\spage)\swith\sthe\snew\ssemantics. +D 2025-06-30T15:21:46.262 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -717,7 +717,7 @@ F mptest/crash02.subtest f4ef05adcd15d60e5d2bd654204f2c008b519df8 F mptest/mptest.c aa41ace6dbc5050d76b02548d3521e6bbccae4f0 F mptest/multiwrite01.test dab5c5f8f9534971efce679152c5146da265222d F sqlite.pc.in 42b7bf0d02e08b9e77734a47798d1a55a9e0716b -F sqlite3.1 acdff36db796e2d00225b911d3047d580cd136547298435426ce9d40347973cc +F sqlite3.1 9e426a150af072be26b0661bcd54567692d979e99bc7daf55b22b952ff8e41a6 F sqlite3.pc.in 0977c03a4da7c4204bd60e784a0efb8d51a190448aba78a4e973fe7192bdaf03 F src/alter.c fc7bbbeb9e89c7124bf5772ce474b333b7bdc18d6e080763211a40fde69fb1da F src/analyze.c 03bcfc083fc0cccaa9ded93604e1d4244ea245c17285d463ef6a60425fcb247d @@ -786,7 +786,7 @@ F src/random.c 606b00941a1d7dd09c381d3279a058d771f406c5213c9932bbd93d5587be4b9c F src/resolve.c d40fe18d7c2fd0339f5846ffcf7d6809866e380acdf14c76fb2af87e9fe13f64 F src/rowset.c 8432130e6c344b3401a8874c3cb49fefe6873fec593294de077afea2dce5ec97 F src/select.c 882d739e0d5e6c7a8b46a3cca3ada37fe1a56301f1360d6b141312c666bbe482 -F src/shell.c.in 98cc5a8ef982e9c60e0d8146ed8c38774a0f80f23c104a115c27f05893d3641d +F src/shell.c.in 1cf75c1b4aee3fb3dbcedb59c0512977450ada75d2ca00bb08df1adf0055db0b F src/sqlite.h.in 5c54f2461a1ea529bab8499148a2b238e2d4bb571d59e8ea5322d0c190abb693 F src/sqlite3.rc 015537e6ac1eec6c7050e17b616c2ffe6f70fca241835a84a4f0d5937383c479 F src/sqlite3ext.h 0bfd049bb2088cc44c2ad54f2079d1c6e43091a4e1ce8868779b75f6c1484f1e @@ -2208,11 +2208,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 1ad0169b022b280bcaaf94a7fa231591be96b514230ab5c98fbf15cd7df842dd F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P b48d95191662e09659b5b55ae65cd462c9e1700c4f92dd9d40b59548f0797c02 -R 6309a1fd152193cda47202f25b8c3c35 -T *branch * shell-xdg-vars -T *sym-shell-xdg-vars * -T -sym-trunk * Cancelled\sby\sbranch. +P 69b9244e3ad95edae38d8e66cedafbc1d7724ae0dd72eeee0fdbff913983598e +R b75f67e480beb6664bd060c4305917e3 U stephan -Z 195b94022bc466347bd2a5713b9a2796 +Z 13d46cdb1a0ac8d1c628b18b42886e33 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 1bc3ddd926..9fa37d6a51 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -69b9244e3ad95edae38d8e66cedafbc1d7724ae0dd72eeee0fdbff913983598e +6b780655ccd68d068b6ee6621e56ac94c000916b7341651dc79725a0800cd1c0 diff --git a/sqlite3.1 b/sqlite3.1 index 08b1ff262b..3332a164b3 100644 --- a/sqlite3.1 +++ b/sqlite3.1 @@ -137,17 +137,37 @@ continue prompt = " ...> " .sp .fi -o If the file +o If the environment variable XDG_CONFIG_HOME is set then .B ${XDG_CONFIG_HOME}/sqlite3/sqliterc -or +is checked, else +.B ~/.local/config/sqlite3/sqliterc +is checked. If the selected file does not exist then the fallback of .B ~/.sqliterc -exists, the first of those to be found is processed during startup. -It should generally only contain meta-commands. +is used. It should generally only contain meta-commands. o If the -init option is present, the specified file is processed. o All other command line options are processed. +.SH HISTORY FILE +.B sqlite3 +may be configured to use a history file to save SQL statements and +meta-commands entered interactively. These statements and commands can be +retrieved, edited and, reused at the main and continue prompts. If the +environment variable +.B SQLITE_HISTORY +is set, it will be used as the name of the history file, whether it +already exists or not. If it is not set but the XDG_STATE_HOME +environment variable is then +.B ${XDG_STATE_HOME}/sqlite_history +is used. If XDG_STATE_HOME is not set then +.B ~/.local/state/sqlite_history +is used. If the selected file does not exist then +.B ~/.sqlite_history +will be used as the history file. If any history file is found, it +will be written if the shell exits interactive mode normally, +regardless of whether it existed previously, though saving will +silently fail if the history file's directory does not exist. .SH SEE ALSO https://sqlite.org/cli.html .br diff --git a/src/shell.c.in b/src/shell.c.in index 6302a9a17c..790106cdb9 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -12759,17 +12759,25 @@ static char *find_home_dir(int clearFlag){ } /* -** On non-Windows platforms, look for $zEnvVar, which must be the name -** of an XDG_... environment variable. If ${zEnvVar}/${zBaseName} is -** found, return the path to it. If ${zEnvVar} is not set then look -** for ${HOME}/${zSubdir}/${zBaseName} and if found return that. If -** none of these are found, return 0. +** On non-Windows platforms, look for the first file found out of: +** +** - ${zEnvVar}/${zBaseName} +** - ${HOME}/${zSubdir}/${zBaseName} +** +** $zEnvVar is intended to be the name of an XDG_... environment +** variable, e.g. XDG_CONFIG_HOME or XDG_STATE_HOME. If zEnvVar is +** NULL or getenv(zEnvVar) is NULL then fall back to the second +** option. If the selected option is not found in the filesystem, +** return 0. +** +** zSubdir may be NULL or empty, in which case ${HOME}/${zBaseName} +** becomes the fallback. ** ** Both zSubdir and zBaseName may contain subdirectory parts. zSubdir -** will conventionally be ".config" or ".local". +** will conventionally be ".config" or ".local/state". ** -** The string returned is obtained from sqlite3_malloc() and -** should be freed by the caller. +** The returned string is obtained from sqlite3_malloc() and should be +** sqlite3_free()'d by the caller. */ static char *find_xdg_file(const char *zEnvVar, const char *zSubdir, const char *zBaseName){ @@ -12777,46 +12785,51 @@ static char *find_xdg_file(const char *zEnvVar, const char *zSubdir, || defined(__RTP__) || defined(_WRS_KERNEL) return 0; #else - char *zConfig = 0; + char *zConfigFile = 0; const char *zXdgDir; - zXdgDir = getenv(zEnvVar); - if( zXdgDir==0 ){ - const char *zHome = getenv("HOME"); - if( zHome==0 ) return 0; - zConfig = sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName); + zXdgDir = zEnvVar ? getenv(zEnvVar) : 0; + if( zXdgDir ){ + zConfigFile = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName); }else{ - zConfig = sqlite3_mprintf("%s/%s", zXdgDir, zBaseName); + const char * zHome = find_home_dir(0); + if( zHome==0 ) return 0; + zConfigFile = (zSubdir && *zSubdir) + ? sqlite3_mprintf("%s/%s/%s", zHome, zSubdir, zBaseName) + : sqlite3_mprintf("%s/%s", zHome, zBaseName); } - shell_check_oom(zConfig); - if( access(zConfig,0)!=0 ){ - sqlite3_free(zConfig); - zConfig = 0; + shell_check_oom(zConfigFile); + if( access(zConfigFile,0)!=0 ){ + sqlite3_free(zConfigFile); + zConfigFile = 0; } - return zConfig; + return zConfigFile; #endif } /* -** Read input from the file given by sqliterc_override. Or if that -** parameter is NULL, take input from the first of find_xdg_file() -** or ~/.sqliterc which is found. +** Read input from the file sqliterc_override. If that parameter is +** NULL, take it from find_xdg_file(), if found, or fall back to +** ~/.sqliterc. ** -** Returns the number of errors. +** Failure to read the config is only considered a failure if +** sqliterc_override is not NULL, in which case this function may emit +** a warning or, if ::bail_on_error is true, fail fatally if the file +** named by sqliterc_override is not found. */ static void process_sqliterc( ShellState *p, /* Configuration data */ const char *sqliterc_override /* Name of config file. NULL to use default */ ){ char *home_dir = NULL; - const char *sqliterc = sqliterc_override; - char *zBuf = 0; + char *sqliterc = sqliterc_override; FILE *inSaved = p->in; int savedLineno = p->lineno; if( sqliterc == NULL ){ - sqliterc = zBuf = find_xdg_file("XDG_CONFIG_HOME", - ".config", "sqlite3/sqliterc"); + sqliterc = find_xdg_file("XDG_CONFIG_HOME", + ".local/config", + "sqlite3/sqliterc"); } if( sqliterc == NULL ){ home_dir = find_home_dir(0); @@ -12825,11 +12838,10 @@ static void process_sqliterc( " cannot read ~/.sqliterc\n"); return; } - zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir); - shell_check_oom(zBuf); - sqliterc = zBuf; + sqliterc = sqlite3_mprintf("%s/.sqliterc",home_dir); + shell_check_oom(sqliterc); } - p->in = sqlite3_fopen(sqliterc,"rb"); + p->in = sqliterc ? sqlite3_fopen(sqliterc,"rb") : 0; if( p->in ){ if( stdin_is_interactive ){ sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc); @@ -12842,7 +12854,9 @@ static void process_sqliterc( } p->in = inSaved; p->lineno = savedLineno; - sqlite3_free(zBuf); + if( sqliterc != sqliterc_override ){ + sqlite3_free(sqliterc); + } } /* @@ -13610,7 +13624,6 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ if( stdin_is_interactive ){ char *zHome; char *zHistory; - int nHistory; sqlite3_fprintf(stdout, "SQLite version %s %.19s\n" /*extra-version-info*/ "Enter \".help\" for usage hints.\n", @@ -13623,11 +13636,15 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ } zHistory = getenv("SQLITE_HISTORY"); if( zHistory ){ - zHistory = strdup(zHistory); - }else if( (zHome = find_home_dir(0))!=0 ){ - nHistory = strlen30(zHome) + 20; - if( (zHistory = malloc(nHistory))!=0 ){ - sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome); + zHistory = sqlite3_mprintf("%s", zHistory); + shell_check_oom(zHistory); + }else{ + zHistory = find_xdg_file("XDG_STATE_HOME", + ".local/state", + "sqlite_history"); + if( 0==zHistory && (zHome = find_home_dir(0))!=0 ){ + zHistory = sqlite3_mprintf("%s/.sqlite_history", zHome); + shell_check_oom(zHistory); } } if( zHistory ){ shell_read_history(zHistory); } @@ -13643,7 +13660,7 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ if( zHistory ){ shell_stifle_history(2000); shell_write_history(zHistory); - free(zHistory); + sqlite3_free(zHistory); } }else{ data.in = stdin;