Fossil SCM
Add support for setting environment variables from within CGI script files.
Commit
0af7024db1246f1fad4ff6b23aa03879f7157edc
Parent
448c9cfae5f5a8d…
2 files changed
+19
+7
+19
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -1262,10 +1262,29 @@ | ||
| 1262 | 1262 | char *zValue = getenv(zName); |
| 1263 | 1263 | #endif |
| 1264 | 1264 | if( zValue ) zValue = fossil_filename_to_utf8(zValue); |
| 1265 | 1265 | return zValue; |
| 1266 | 1266 | } |
| 1267 | + | |
| 1268 | +/* | |
| 1269 | +** Sets the value of an environment variable as UTF8. | |
| 1270 | +*/ | |
| 1271 | +int fossil_setenv(const char *zName, const char *zValue){ | |
| 1272 | + int rc; | |
| 1273 | + char *zString = mprintf("%s=%s", zName, zValue); | |
| 1274 | +#ifdef _WIN32 | |
| 1275 | + wchar_t *uString = fossil_utf8_to_unicode(zString); | |
| 1276 | + rc = _wputenv(uString); | |
| 1277 | + fossil_unicode_free(uString); | |
| 1278 | + fossil_free(zString); | |
| 1279 | +#else | |
| 1280 | + rc = putenv(zString); | |
| 1281 | + /* NOTE: Cannot free the string on POSIX. */ | |
| 1282 | + /* fossil_free(zString); */ | |
| 1283 | +#endif | |
| 1284 | + return rc; | |
| 1285 | +} | |
| 1267 | 1286 | |
| 1268 | 1287 | /* |
| 1269 | 1288 | ** Like fopen() but always takes a UTF8 argument. |
| 1270 | 1289 | */ |
| 1271 | 1290 | FILE *fossil_fopen(const char *zName, const char *zMode){ |
| 1272 | 1291 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1262,10 +1262,29 @@ | |
| 1262 | char *zValue = getenv(zName); |
| 1263 | #endif |
| 1264 | if( zValue ) zValue = fossil_filename_to_utf8(zValue); |
| 1265 | return zValue; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | ** Like fopen() but always takes a UTF8 argument. |
| 1270 | */ |
| 1271 | FILE *fossil_fopen(const char *zName, const char *zMode){ |
| 1272 |
| --- src/file.c | |
| +++ src/file.c | |
| @@ -1262,10 +1262,29 @@ | |
| 1262 | char *zValue = getenv(zName); |
| 1263 | #endif |
| 1264 | if( zValue ) zValue = fossil_filename_to_utf8(zValue); |
| 1265 | return zValue; |
| 1266 | } |
| 1267 | |
| 1268 | /* |
| 1269 | ** Sets the value of an environment variable as UTF8. |
| 1270 | */ |
| 1271 | int fossil_setenv(const char *zName, const char *zValue){ |
| 1272 | int rc; |
| 1273 | char *zString = mprintf("%s=%s", zName, zValue); |
| 1274 | #ifdef _WIN32 |
| 1275 | wchar_t *uString = fossil_utf8_to_unicode(zString); |
| 1276 | rc = _wputenv(uString); |
| 1277 | fossil_unicode_free(uString); |
| 1278 | fossil_free(zString); |
| 1279 | #else |
| 1280 | rc = putenv(zString); |
| 1281 | /* NOTE: Cannot free the string on POSIX. */ |
| 1282 | /* fossil_free(zString); */ |
| 1283 | #endif |
| 1284 | return rc; |
| 1285 | } |
| 1286 | |
| 1287 | /* |
| 1288 | ** Like fopen() but always takes a UTF8 argument. |
| 1289 | */ |
| 1290 | FILE *fossil_fopen(const char *zName, const char *zMode){ |
| 1291 |
+7
| --- src/main.c | ||
| +++ src/main.c | ||
| @@ -1837,10 +1837,17 @@ | ||
| 1837 | 1837 | } |
| 1838 | 1838 | if( blob_eq(&key, "files:") && blob_token(&line, &value) ){ |
| 1839 | 1839 | pFileGlob = glob_create(blob_str(&value)); |
| 1840 | 1840 | blob_reset(&value); |
| 1841 | 1841 | continue; |
| 1842 | + } | |
| 1843 | + if( blob_eq(&key, "setenv:") && blob_token(&line, &value) | |
| 1844 | + && blob_token(&line, &value2) ){ | |
| 1845 | + fossil_setenv(blob_str(&value), blob_str(&value2)); | |
| 1846 | + blob_reset(&value); | |
| 1847 | + blob_reset(&value2); | |
| 1848 | + continue; | |
| 1842 | 1849 | } |
| 1843 | 1850 | } |
| 1844 | 1851 | blob_reset(&config); |
| 1845 | 1852 | if( g.db==0 && g.zRepositoryName==0 && nRedirect==0 ){ |
| 1846 | 1853 | cgi_panic("Unable to find or open the project repository"); |
| 1847 | 1854 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -1837,10 +1837,17 @@ | |
| 1837 | } |
| 1838 | if( blob_eq(&key, "files:") && blob_token(&line, &value) ){ |
| 1839 | pFileGlob = glob_create(blob_str(&value)); |
| 1840 | blob_reset(&value); |
| 1841 | continue; |
| 1842 | } |
| 1843 | } |
| 1844 | blob_reset(&config); |
| 1845 | if( g.db==0 && g.zRepositoryName==0 && nRedirect==0 ){ |
| 1846 | cgi_panic("Unable to find or open the project repository"); |
| 1847 |
| --- src/main.c | |
| +++ src/main.c | |
| @@ -1837,10 +1837,17 @@ | |
| 1837 | } |
| 1838 | if( blob_eq(&key, "files:") && blob_token(&line, &value) ){ |
| 1839 | pFileGlob = glob_create(blob_str(&value)); |
| 1840 | blob_reset(&value); |
| 1841 | continue; |
| 1842 | } |
| 1843 | if( blob_eq(&key, "setenv:") && blob_token(&line, &value) |
| 1844 | && blob_token(&line, &value2) ){ |
| 1845 | fossil_setenv(blob_str(&value), blob_str(&value2)); |
| 1846 | blob_reset(&value); |
| 1847 | blob_reset(&value2); |
| 1848 | continue; |
| 1849 | } |
| 1850 | } |
| 1851 | blob_reset(&config); |
| 1852 | if( g.db==0 && g.zRepositoryName==0 && nRedirect==0 ){ |
| 1853 | cgi_panic("Unable to find or open the project repository"); |
| 1854 |