Fossil SCM

fossil-scm / src / builtin.c
Source Blame History 957 lines
db6a8e8… drh 1 /*
db6a8e8… drh 2 ** Copyright (c) 2014 D. Richard Hipp
db6a8e8… drh 3 **
db6a8e8… drh 4 ** This program is free software; you can redistribute it and/or
db6a8e8… drh 5 ** modify it under the terms of the Simplified BSD License (also
db6a8e8… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
db6a8e8… drh 7
db6a8e8… drh 8 ** This program is distributed in the hope that it will be useful,
db6a8e8… drh 9 ** but without any warranty; without even the implied warranty of
db6a8e8… drh 10 ** merchantability or fitness for a particular purpose.
db6a8e8… drh 11 **
db6a8e8… drh 12 ** Author contact information:
db6a8e8… drh 13 ** [email protected]
db6a8e8… drh 14 ** http://www.hwaci.com/drh/
db6a8e8… drh 15 **
db6a8e8… drh 16 *******************************************************************************
db6a8e8… drh 17 **
db6a8e8… drh 18 ** This file contains built-in string and BLOB resources packaged as
db6a8e8… drh 19 ** byte arrays.
db6a8e8… drh 20 */
db6a8e8… drh 21 #include "config.h"
db6a8e8… drh 22 #include "builtin.h"
db6a8e8… drh 23 #include <assert.h>
db6a8e8… drh 24
db6a8e8… drh 25 /*
db6a8e8… drh 26 ** The resources provided by this file are packaged by the "mkbuiltin.c"
db6a8e8… drh 27 ** utility program during the built process and stored in the
db6a8e8… drh 28 ** builtin_data.h file. Include that information here:
db6a8e8… drh 29 */
db6a8e8… drh 30 #include "builtin_data.h"
db6a8e8… drh 31
db6a8e8… drh 32 /*
956d2f8… drh 33 ** Return the index in the aBuiltinFiles[] array for the file
956d2f8… drh 34 ** whose name is zFilename. Or return -1 if the file is not
956d2f8… drh 35 ** found.
db6a8e8… drh 36 */
956d2f8… drh 37 static int builtin_file_index(const char *zFilename){
96fdf09… drh 38 int lwr, upr, i, c;
db6a8e8… drh 39 lwr = 0;
3cb9ba4… andygoth 40 upr = count(aBuiltinFiles) - 1;
db6a8e8… drh 41 while( upr>=lwr ){
db6a8e8… drh 42 i = (upr+lwr)/2;
96fdf09… drh 43 c = strcmp(aBuiltinFiles[i].zName,zFilename);
96fdf09… drh 44 if( c<0 ){
96fdf09… drh 45 lwr = i+1;
96fdf09… drh 46 }else if( c>0 ){
96fdf09… drh 47 upr = i-1;
96fdf09… drh 48 }else{
956d2f8… drh 49 return i;
96fdf09… drh 50 }
96fdf09… drh 51 }
956d2f8… drh 52 return -1;
956d2f8… drh 53 }
956d2f8… drh 54
956d2f8… drh 55 /*
956d2f8… drh 56 ** Return a pointer to built-in content
dd20f34… drh 57 **
dd20f34… drh 58 ** If the filename contains "-vNNNNNNNN" just before the final file
dd20f34… drh 59 ** suffix, where each N is a random digit, then omit that part of the
dd20f34… drh 60 ** filename before doing the lookup. The extra -vNNNNNNNN was added
dd20f34… drh 61 ** to defeat overly aggressive caching by web browsers. There must be
dd20f34… drh 62 ** at least 8 digits in NNNNNNNN but more than 8 are allowed.
956d2f8… drh 63 */
956d2f8… drh 64 const unsigned char *builtin_file(const char *zFilename, int *piSize){
956d2f8… drh 65 int i = builtin_file_index(zFilename);
956d2f8… drh 66 if( i>=0 ){
956d2f8… drh 67 if( piSize ) *piSize = aBuiltinFiles[i].nByte;
956d2f8… drh 68 return aBuiltinFiles[i].pData;
956d2f8… drh 69 }else{
ea09902… danield 70 const char *zV = strstr(zFilename, "-v");
dd20f34… drh 71 if( zV!=0 ){
dd20f34… drh 72 for(i=0; fossil_isdigit(zV[i+2]); i++){}
dd20f34… drh 73 if( i>=8 && zV[i+2]=='.' ){
dd20f34… drh 74 char *zNew = mprintf("%.*s%s", (int)(zV-zFilename), zFilename, zV+i+2);
dd20f34… drh 75 const unsigned char *pRes = builtin_file(zNew, piSize);
dd20f34… drh 76 fossil_free(zNew);
dd20f34… drh 77 return pRes;
dd20f34… drh 78 }
dd20f34… drh 79 }
956d2f8… drh 80 if( piSize ) *piSize = 0;
956d2f8… drh 81 return 0;
956d2f8… drh 82 }
96fdf09… drh 83 }
96fdf09… drh 84 const char *builtin_text(const char *zFilename){
96fdf09… drh 85 return (char*)builtin_file(zFilename, 0);
db6a8e8… drh 86 }
db6a8e8… drh 87
db6a8e8… drh 88 /*
6ba0e14… stephan 89 ** COMMAND: test-builtin-list
db6a8e8… drh 90 **
7df4678… stephan 91 ** If -verbose is used, it outputs a line at the end
7df4678… stephan 92 ** with the total item count and size.
7df4678… stephan 93 **
8bfd995… rberteig 94 ** List the names and sizes of all built-in resources.
db6a8e8… drh 95 */
db6a8e8… drh 96 void test_builtin_list(void){
7df4678… stephan 97 int i, size = 0;;
cee662d… drh 98 for(i=0; i<count(aBuiltinFiles); i++){
7df4678… stephan 99 const int n = aBuiltinFiles[i].nByte;
956d2f8… drh 100 fossil_print("%3d. %-45s %6d\n", i+1, aBuiltinFiles[i].zName,n);
7df4678… stephan 101 size += n;
7df4678… stephan 102 }
7df4678… stephan 103 if(find_option("verbose","v",0)!=0){
7df4678… stephan 104 fossil_print("%d entries totaling %d bytes\n", i, size);
cee662d… drh 105 }
cee662d… drh 106 }
cee662d… drh 107
cee662d… drh 108 /*
cee662d… drh 109 ** WEBPAGE: test-builtin-files
cee662d… drh 110 **
cee662d… drh 111 ** Show all built-in text files.
cee662d… drh 112 */
cee662d… drh 113 void test_builtin_list_page(void){
db6a8e8… drh 114 int i;
112c713… drh 115 style_set_current_feature("test");
cee662d… drh 116 style_header("Built-in Text Files");
956d2f8… drh 117 @ <ol>
3cb9ba4… andygoth 118 for(i=0; i<count(aBuiltinFiles); i++){
cee662d… drh 119 const char *z = aBuiltinFiles[i].zName;
956d2f8… drh 120 char *zUrl = href("%R/builtin?name=%T&id=%.8s&mimetype=text/plain",
956d2f8… drh 121 z,fossil_exe_id());
956d2f8… drh 122 @ <li>%z(zUrl)%h(z)</a>
db6a8e8… drh 123 }
956d2f8… drh 124 @ </ol>
112c713… drh 125 style_finish_page();
db6a8e8… drh 126 }
db6a8e8… drh 127
db6a8e8… drh 128 /*
db6a8e8… drh 129 ** COMMAND: test-builtin-get
db6a8e8… drh 130 **
db6a8e8… drh 131 ** Usage: %fossil test-builtin-get NAME ?OUTPUT-FILE?
db6a8e8… drh 132 */
db6a8e8… drh 133 void test_builtin_get(void){
db6a8e8… drh 134 const unsigned char *pData;
db6a8e8… drh 135 int nByte;
db6a8e8… drh 136 Blob x;
db6a8e8… drh 137 if( g.argc!=3 && g.argc!=4 ){
db6a8e8… drh 138 usage("NAME ?OUTPUT-FILE?");
db6a8e8… drh 139 }
db6a8e8… drh 140 pData = builtin_file(g.argv[2], &nByte);
db6a8e8… drh 141 if( pData==0 ){
db6a8e8… drh 142 fossil_fatal("no such built-in file: [%s]", g.argv[2]);
db6a8e8… drh 143 }
db6a8e8… drh 144 blob_init(&x, (const char*)pData, nByte);
db6a8e8… drh 145 blob_write_to_file(&x, g.argc==4 ? g.argv[3] : "-");
db6a8e8… drh 146 blob_reset(&x);
956d2f8… drh 147 }
956d2f8… drh 148
956d2f8… drh 149 /*
956d2f8… drh 150 ** Input zList is a list of numeric identifiers for files in
7fcb462… stephan 151 ** aBuiltinFiles[]. Return the concatenation of all of those files
7fcb462… stephan 152 ** using mimetype zType, or as text/javascript if zType is 0.
956d2f8… drh 153 */
956d2f8… drh 154 static void builtin_deliver_multiple_js_files(
956d2f8… drh 155 const char *zList, /* List of numeric identifiers */
956d2f8… drh 156 const char *zType /* Override mimetype */
956d2f8… drh 157 ){
956d2f8… drh 158 Blob *pOut;
7fcb462… stephan 159 if( zType==0 ) zType = "text/javascript";
956d2f8… drh 160 cgi_set_content_type(zType);
956d2f8… drh 161 pOut = cgi_output_blob();
956d2f8… drh 162 while( zList[0] ){
956d2f8… drh 163 int i = atoi(zList);
956d2f8… drh 164 if( i>0 && i<=count(aBuiltinFiles) ){
139db4c… drh 165 blob_appendf(pOut, "/* %s */\n", aBuiltinFiles[i-1].zName);
956d2f8… drh 166 blob_append(pOut, (const char*)aBuiltinFiles[i-1].pData,
956d2f8… drh 167 aBuiltinFiles[i-1].nByte);
956d2f8… drh 168 }
76c9bbb… stephan 169 while( zList[0] && fossil_isdigit(zList[0]) ) zList++;
76c9bbb… stephan 170 while( zList[0] && !fossil_isdigit(zList[0]) ) zList++;
956d2f8… drh 171 }
956d2f8… drh 172 return;
956d2f8… drh 173 }
956d2f8… drh 174
956d2f8… drh 175 /*
9413395… drh 176 ** WEBPAGE: builtin loadavg-exempt
956d2f8… drh 177 **
956d2f8… drh 178 ** Return one of many built-in content files. Query parameters:
956d2f8… drh 179 **
956d2f8… drh 180 ** name=FILENAME Return the single file whose name is FILENAME.
956d2f8… drh 181 ** mimetype=TYPE Override the mimetype in the returned file to
956d2f8… drh 182 ** be TYPE. If this query parameter is omitted
956d2f8… drh 183 ** (the usual case) then the mimetype is inferred
956d2f8… drh 184 ** from the suffix on FILENAME
956d2f8… drh 185 ** m=IDLIST IDLIST is a comma-separated list of integers
956d2f8… drh 186 ** that specify multiple javascript files to be
956d2f8… drh 187 ** concatenated and returned all at once.
956d2f8… drh 188 ** id=UNIQUEID Version number of the "builtin" files. Used
956d2f8… drh 189 ** for cache control only.
956d2f8… drh 190 **
956d2f8… drh 191 ** At least one of the name= or m= query parameters must be present.
956d2f8… drh 192 **
956d2f8… drh 193 ** If the id= query parameter is present, then Fossil assumes that the
956d2f8… drh 194 ** result is immutable and sets a very large cache retention time (1 year).
956d2f8… drh 195 */
956d2f8… drh 196 void builtin_webpage(void){
956d2f8… drh 197 Blob out;
956d2f8… drh 198 const char *zName = P("name");
be93625… stephan 199 const char *zContent = 0;
be93625… stephan 200 int nContent = 0;
956d2f8… drh 201 const char *zId = P("id");
956d2f8… drh 202 const char *zType = P("mimetype");
956d2f8… drh 203 int nId;
be93625… stephan 204 if( zName ) zContent = (const char *)builtin_file(zName, &nContent);
be93625… stephan 205 if( zContent==0 ){
956d2f8… drh 206 const char *zM = P("m");
956d2f8… drh 207 if( zM ){
139db4c… drh 208 if( zId && (nId = (int)strlen(zId))>=8
139db4c… drh 209 && strncmp(zId,fossil_exe_id(),nId)==0
139db4c… drh 210 ){
139db4c… drh 211 g.isConst = 1;
139db4c… drh 212 }
ec5a063… drh 213 etag_check(0,0);
956d2f8… drh 214 builtin_deliver_multiple_js_files(zM, zType);
956d2f8… drh 215 return;
956d2f8… drh 216 }
956d2f8… drh 217 cgi_set_status(404, "Not Found");
956d2f8… drh 218 @ File "%h(zName)" not found
956d2f8… drh 219 return;
956d2f8… drh 220 }
956d2f8… drh 221 if( zType==0 ){
956d2f8… drh 222 if( sqlite3_strglob("*.js", zName)==0 ){
7fcb462… stephan 223 zType = "text/javascript";
956d2f8… drh 224 }else{
956d2f8… drh 225 zType = mimetype_from_name(zName);
956d2f8… drh 226 }
956d2f8… drh 227 }
956d2f8… drh 228 cgi_set_content_type(zType);
956d2f8… drh 229 if( zId
956d2f8… drh 230 && (nId = (int)strlen(zId))>=8
956d2f8… drh 231 && strncmp(zId,fossil_exe_id(),nId)==0
956d2f8… drh 232 ){
956d2f8… drh 233 g.isConst = 1;
956d2f8… drh 234 }
ec5a063… drh 235 etag_check(0,0);
be93625… stephan 236 blob_init(&out, zContent, nContent);
956d2f8… drh 237 cgi_set_content(&out);
036a9d5… drh 238 }
036a9d5… drh 239
036a9d5… drh 240 /* Variables controlling the JS cache.
036a9d5… drh 241 */
036a9d5… drh 242 static struct {
036a9d5… drh 243 int aReq[30]; /* Indexes of all requested built-in JS files */
036a9d5… drh 244 int nReq; /* Number of slots in aReq[] currently used */
036a9d5… drh 245 int nSent; /* Number of slots in aReq[] fulfilled */
036a9d5… drh 246 int eDelivery; /* Delivery mechanism */
036a9d5… drh 247 } builtin;
036a9d5… drh 248
036a9d5… drh 249 #if INTERFACE
036a9d5… drh 250 /* Various delivery mechanisms. The 0 option is the default.
b258a41… florian 251 ** MAINTENANCE NOTE: Review/update the builtin_set_js_delivery_mode() and
b258a41… florian 252 ** builtin_get_js_delivery_mode_name() functions if values are changed/added.
036a9d5… drh 253 */
036a9d5… drh 254 #define JS_INLINE 0 /* inline, batched together at end of file */
036a9d5… drh 255 #define JS_SEPARATE 1 /* Separate HTTP request for each JS file */
036a9d5… drh 256 #define JS_BUNDLED 2 /* One HTTP request to load all JS files */
036a9d5… drh 257 /* concatenated together into a bundle */
036a9d5… drh 258 #endif /* INTERFACE */
036a9d5… drh 259
036a9d5… drh 260 /*
036a9d5… drh 261 ** The argument is a request to change the javascript delivery mode.
036a9d5… drh 262 ** The argument is a string which is a command-line option or CGI
036a9d5… drh 263 ** parameter. Try to match it against one of the delivery options
036a9d5… drh 264 ** and set things up accordingly. Throw an error if no match unless
036a9d5… drh 265 ** bSilent is true.
036a9d5… drh 266 */
036a9d5… drh 267 void builtin_set_js_delivery_mode(const char *zMode, int bSilent){
036a9d5… drh 268 if( zMode==0 ) return;
036a9d5… drh 269 if( strcmp(zMode, "inline")==0 ){
036a9d5… drh 270 builtin.eDelivery = JS_INLINE;
036a9d5… drh 271 }else
036a9d5… drh 272 if( strcmp(zMode, "separate")==0 ){
036a9d5… drh 273 builtin.eDelivery = JS_SEPARATE;
036a9d5… drh 274 }else
036a9d5… drh 275 if( strcmp(zMode, "bundled")==0 ){
036a9d5… drh 276 builtin.eDelivery = JS_BUNDLED;
036a9d5… drh 277 }else if( !bSilent ){
036a9d5… drh 278 fossil_fatal("unknown javascript delivery mode \"%s\" - should be"
036a9d5… drh 279 " one of: inline separate bundled", zMode);
34f7fd7… stephan 280 }
34f7fd7… stephan 281 }
34f7fd7… stephan 282
34f7fd7… stephan 283 /*
34f7fd7… stephan 284 ** Returns the current JS delivery mode: one of JS_INLINE,
34f7fd7… stephan 285 ** JS_SEPARATE, JS_BUNDLED.
34f7fd7… stephan 286 */
34f7fd7… stephan 287 int builtin_get_js_delivery_mode(void){
34f7fd7… stephan 288 return builtin.eDelivery;
1142db3… florian 289 }
1142db3… florian 290
1142db3… florian 291 /*
1142db3… florian 292 ** Returns the name of the current JS delivery mode for reuse with the --jsmode
1142db3… florian 293 ** option, i.e. the other way around than builtin_set_js_delivery_mode().
1142db3… florian 294 */
1142db3… florian 295 const char *builtin_get_js_delivery_mode_name(void){
1142db3… florian 296 switch( builtin.eDelivery ){
b258a41… florian 297 case JS_SEPARATE: {
b258a41… florian 298 return "separate";
b258a41… florian 299 }
b258a41… florian 300 case JS_BUNDLED: {
b258a41… florian 301 return "bundled";
b258a41… florian 302 }
b258a41… florian 303 case JS_INLINE:
b258a41… florian 304 /*FALLTHROUGH*/
b258a41… florian 305 default: {
b258a41… florian 306 return "inline";
b258a41… florian 307 }
1142db3… florian 308 }
036a9d5… drh 309 }
036a9d5… drh 310
036a9d5… drh 311 /*
036a9d5… drh 312 ** The caller wants the Javascript file named by zFilename to be
036a9d5… drh 313 ** included in the generated page. Add the file to the queue of
036a9d5… drh 314 ** requested javascript resources, if it is not there already.
036a9d5… drh 315 **
036a9d5… drh 316 ** The current implementation queues the file to be included in the
036a9d5… drh 317 ** output later. However, the caller should not depend on that
036a9d5… drh 318 ** behavior. In the future, this routine might decide to insert
036a9d5… drh 319 ** the requested javascript inline, immedaitely, or to insert
036a9d5… drh 320 ** a <script src=..> element to reference the javascript as a
036a9d5… drh 321 ** separate resource. The exact behavior might change in the future
036a9d5… drh 322 ** so pages that use this interface must not rely on any particular
036a9d5… drh 323 ** behavior.
036a9d5… drh 324 **
036a9d5… drh 325 ** All this routine guarantees is that the named javascript file
036a9d5… drh 326 ** will be requested by the browser at some point. This routine
036a9d5… drh 327 ** does not guarantee when the javascript will be included, and it
036a9d5… drh 328 ** does not guarantee whether the javascript will be added inline or
036a9d5… drh 329 ** delivered as a separate resource.
036a9d5… drh 330 */
036a9d5… drh 331 void builtin_request_js(const char *zFilename){
036a9d5… drh 332 int i = builtin_file_index(zFilename);
036a9d5… drh 333 int j;
036a9d5… drh 334 if( i<0 ){
036a9d5… drh 335 fossil_panic("unknown javascript file: \"%s\"", zFilename);
036a9d5… drh 336 }
036a9d5… drh 337 for(j=0; j<builtin.nReq; j++){
036a9d5… drh 338 if( builtin.aReq[j]==i ) return; /* Already queued or sent */
036a9d5… drh 339 }
036a9d5… drh 340 if( builtin.nReq>=count(builtin.aReq) ){
036a9d5… drh 341 fossil_panic("too many javascript files requested");
036a9d5… drh 342 }
036a9d5… drh 343 builtin.aReq[builtin.nReq++] = i;
036a9d5… drh 344 }
036a9d5… drh 345
036a9d5… drh 346 /*
036a9d5… drh 347 ** Fulfill all pending requests for javascript files.
036a9d5… drh 348 **
036a9d5… drh 349 ** The current implementation delivers all javascript in-line. However,
036a9d5… drh 350 ** the caller should not depend on this. Future changes to this routine
036a9d5… drh 351 ** might choose to deliver javascript as separate resources.
036a9d5… drh 352 */
036a9d5… drh 353 void builtin_fulfill_js_requests(void){
036a9d5… drh 354 if( builtin.nSent>=builtin.nReq ) return; /* nothing to do */
036a9d5… drh 355 switch( builtin.eDelivery ){
036a9d5… drh 356 case JS_INLINE: {
036a9d5… drh 357 CX("<script nonce='%h'>\n",style_nonce());
036a9d5… drh 358 do{
036a9d5… drh 359 int i = builtin.aReq[builtin.nSent++];
6854244… drh 360 CX("/* %s %.60c*/\n", aBuiltinFiles[i].zName, '*');
036a9d5… drh 361 cgi_append_content((const char*)aBuiltinFiles[i].pData,
036a9d5… drh 362 aBuiltinFiles[i].nByte);
036a9d5… drh 363 }while( builtin.nSent<builtin.nReq );
036a9d5… drh 364 CX("</script>\n");
036a9d5… drh 365 break;
036a9d5… drh 366 }
036a9d5… drh 367 case JS_BUNDLED: {
036a9d5… drh 368 if( builtin.nSent+1<builtin.nReq ){
036a9d5… drh 369 Blob aList;
036a9d5… drh 370 blob_init(&aList,0,0);
036a9d5… drh 371 while( builtin.nSent<builtin.nReq ){
036a9d5… drh 372 blob_appendf(&aList, ",%d", builtin.aReq[builtin.nSent++]+1);
036a9d5… drh 373 }
036a9d5… drh 374 CX("<script src='%R/builtin?m=%s&id=%.8s'></script>\n",
036a9d5… drh 375 blob_str(&aList)+1, fossil_exe_id());
036a9d5… drh 376 blob_reset(&aList);
036a9d5… drh 377 break;
036a9d5… drh 378 }
036a9d5… drh 379 /* If there is only one JS file, fall through into the
036a9d5… drh 380 ** JS_SEPARATE case below. */
036a9d5… drh 381 /*FALLTHROUGH*/
036a9d5… drh 382 }
036a9d5… drh 383 case JS_SEPARATE: {
036a9d5… drh 384 /* Each JS file as a separate resource */
036a9d5… drh 385 while( builtin.nSent<builtin.nReq ){
036a9d5… drh 386 int i = builtin.aReq[builtin.nSent++];
036a9d5… drh 387 CX("<script src='%R/builtin?name=%t&id=%.8s'></script>\n",
036a9d5… drh 388 aBuiltinFiles[i].zName, fossil_exe_id());
036a9d5… drh 389 }
036a9d5… drh 390 break;
036a9d5… drh 391 }
036a9d5… drh 392 }
c6359b4… drh 393 }
c6359b4… drh 394
c6359b4… drh 395 /*****************************************************************************
c6359b4… drh 396 ** A virtual table for accessing the information in aBuiltinFiles[].
c6359b4… drh 397 */
c6359b4… drh 398
c6359b4… drh 399 /* builtinVtab_vtab is a subclass of sqlite3_vtab which is
c6359b4… drh 400 ** underlying representation of the virtual table
c6359b4… drh 401 */
c6359b4… drh 402 typedef struct builtinVtab_vtab builtinVtab_vtab;
c6359b4… drh 403 struct builtinVtab_vtab {
c6359b4… drh 404 sqlite3_vtab base; /* Base class - must be first */
c6359b4… drh 405 /* Add new fields here, as necessary */
c6359b4… drh 406 };
c6359b4… drh 407
c6359b4… drh 408 /* builtinVtab_cursor is a subclass of sqlite3_vtab_cursor which will
c6359b4… drh 409 ** serve as the underlying representation of a cursor that scans
c6359b4… drh 410 ** over rows of the result
c6359b4… drh 411 */
c6359b4… drh 412 typedef struct builtinVtab_cursor builtinVtab_cursor;
c6359b4… drh 413 struct builtinVtab_cursor {
c6359b4… drh 414 sqlite3_vtab_cursor base; /* Base class - must be first */
c6359b4… drh 415 /* Insert new fields here. For this builtinVtab we only keep track
c6359b4… drh 416 ** of the rowid */
c6359b4… drh 417 sqlite3_int64 iRowid; /* The rowid */
c6359b4… drh 418 };
c6359b4… drh 419
c6359b4… drh 420 /*
c6359b4… drh 421 ** The builtinVtabConnect() method is invoked to create a new
c6359b4… drh 422 ** builtin virtual table.
c6359b4… drh 423 **
c6359b4… drh 424 ** Think of this routine as the constructor for builtinVtab_vtab objects.
c6359b4… drh 425 **
c6359b4… drh 426 ** All this routine needs to do is:
c6359b4… drh 427 **
c6359b4… drh 428 ** (1) Allocate the builtinVtab_vtab object and initialize all fields.
c6359b4… drh 429 **
c6359b4… drh 430 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
c6359b4… drh 431 ** result set of queries against the virtual table will look like.
c6359b4… drh 432 */
c6359b4… drh 433 static int builtinVtabConnect(
c6359b4… drh 434 sqlite3 *db,
c6359b4… drh 435 void *pAux,
c6359b4… drh 436 int argc, const char *const*argv,
c6359b4… drh 437 sqlite3_vtab **ppVtab,
c6359b4… drh 438 char **pzErr
c6359b4… drh 439 ){
c6359b4… drh 440 builtinVtab_vtab *pNew;
c6359b4… drh 441 int rc;
c6359b4… drh 442
c6359b4… drh 443 rc = sqlite3_declare_vtab(db,
c6359b4… drh 444 "CREATE TABLE x(name,size,data)"
c6359b4… drh 445 );
c6359b4… drh 446 if( rc==SQLITE_OK ){
c6359b4… drh 447 pNew = sqlite3_malloc( sizeof(*pNew) );
c6359b4… drh 448 *ppVtab = (sqlite3_vtab*)pNew;
c6359b4… drh 449 if( pNew==0 ) return SQLITE_NOMEM;
c6359b4… drh 450 memset(pNew, 0, sizeof(*pNew));
c6359b4… drh 451 }
c6359b4… drh 452 return rc;
c6359b4… drh 453 }
c6359b4… drh 454
c6359b4… drh 455 /*
c6359b4… drh 456 ** This method is the destructor for builtinVtab_vtab objects.
c6359b4… drh 457 */
c6359b4… drh 458 static int builtinVtabDisconnect(sqlite3_vtab *pVtab){
c6359b4… drh 459 builtinVtab_vtab *p = (builtinVtab_vtab*)pVtab;
c6359b4… drh 460 sqlite3_free(p);
c6359b4… drh 461 return SQLITE_OK;
c6359b4… drh 462 }
c6359b4… drh 463
c6359b4… drh 464 /*
c6359b4… drh 465 ** Constructor for a new builtinVtab_cursor object.
c6359b4… drh 466 */
c6359b4… drh 467 static int builtinVtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
c6359b4… drh 468 builtinVtab_cursor *pCur;
c6359b4… drh 469 pCur = sqlite3_malloc( sizeof(*pCur) );
c6359b4… drh 470 if( pCur==0 ) return SQLITE_NOMEM;
c6359b4… drh 471 memset(pCur, 0, sizeof(*pCur));
c6359b4… drh 472 *ppCursor = &pCur->base;
c6359b4… drh 473 return SQLITE_OK;
c6359b4… drh 474 }
c6359b4… drh 475
c6359b4… drh 476 /*
c6359b4… drh 477 ** Destructor for a builtinVtab_cursor.
c6359b4… drh 478 */
c6359b4… drh 479 static int builtinVtabClose(sqlite3_vtab_cursor *cur){
c6359b4… drh 480 builtinVtab_cursor *pCur = (builtinVtab_cursor*)cur;
c6359b4… drh 481 sqlite3_free(pCur);
c6359b4… drh 482 return SQLITE_OK;
c6359b4… drh 483 }
c6359b4… drh 484
c6359b4… drh 485
c6359b4… drh 486 /*
c6359b4… drh 487 ** Advance a builtinVtab_cursor to its next row of output.
c6359b4… drh 488 */
c6359b4… drh 489 static int builtinVtabNext(sqlite3_vtab_cursor *cur){
c6359b4… drh 490 builtinVtab_cursor *pCur = (builtinVtab_cursor*)cur;
c6359b4… drh 491 pCur->iRowid++;
c6359b4… drh 492 return SQLITE_OK;
c6359b4… drh 493 }
c6359b4… drh 494
c6359b4… drh 495 /*
c6359b4… drh 496 ** Return values of columns for the row at which the builtinVtab_cursor
c6359b4… drh 497 ** is currently pointing.
c6359b4… drh 498 */
c6359b4… drh 499 static int builtinVtabColumn(
c6359b4… drh 500 sqlite3_vtab_cursor *cur, /* The cursor */
c6359b4… drh 501 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
c6359b4… drh 502 int i /* Which column to return */
c6359b4… drh 503 ){
c6359b4… drh 504 builtinVtab_cursor *pCur = (builtinVtab_cursor*)cur;
cf563c7… florian 505 const struct BuiltinFileTable *pFile = aBuiltinFiles + pCur->iRowid - 1;
c6359b4… drh 506 switch( i ){
c6359b4… drh 507 case 0: /* name */
c6359b4… drh 508 sqlite3_result_text(ctx, pFile->zName, -1, SQLITE_STATIC);
c6359b4… drh 509 break;
c6359b4… drh 510 case 1: /* size */
c6359b4… drh 511 sqlite3_result_int(ctx, pFile->nByte);
c6359b4… drh 512 break;
c6359b4… drh 513 case 2: /* data */
c6359b4… drh 514 sqlite3_result_blob(ctx, pFile->pData, pFile->nByte, SQLITE_STATIC);
c6359b4… drh 515 break;
c6359b4… drh 516 }
c6359b4… drh 517 return SQLITE_OK;
c6359b4… drh 518 }
c6359b4… drh 519
c6359b4… drh 520 /*
c6359b4… drh 521 ** Return the rowid for the current row. In this implementation, the
c6359b4… drh 522 ** rowid is the same as the output value.
c6359b4… drh 523 */
c6359b4… drh 524 static int builtinVtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
c6359b4… drh 525 builtinVtab_cursor *pCur = (builtinVtab_cursor*)cur;
c6359b4… drh 526 *pRowid = pCur->iRowid;
c6359b4… drh 527 return SQLITE_OK;
c6359b4… drh 528 }
c6359b4… drh 529
c6359b4… drh 530 /*
c6359b4… drh 531 ** Return TRUE if the cursor has been moved off of the last
c6359b4… drh 532 ** row of output.
c6359b4… drh 533 */
c6359b4… drh 534 static int builtinVtabEof(sqlite3_vtab_cursor *cur){
c6359b4… drh 535 builtinVtab_cursor *pCur = (builtinVtab_cursor*)cur;
cf563c7… florian 536 return pCur->iRowid>count(aBuiltinFiles);
c6359b4… drh 537 }
c6359b4… drh 538
c6359b4… drh 539 /*
c6359b4… drh 540 ** This method is called to "rewind" the builtinVtab_cursor object back
c6359b4… drh 541 ** to the first row of output. This method is always called at least
275da70… danield 542 ** once prior to any call to builtinVtabColumn() or builtinVtabRowid() or
c6359b4… drh 543 ** builtinVtabEof().
c6359b4… drh 544 */
c6359b4… drh 545 static int builtinVtabFilter(
275da70… danield 546 sqlite3_vtab_cursor *pVtabCursor,
c6359b4… drh 547 int idxNum, const char *idxStr,
c6359b4… drh 548 int argc, sqlite3_value **argv
c6359b4… drh 549 ){
c6359b4… drh 550 builtinVtab_cursor *pCur = (builtinVtab_cursor *)pVtabCursor;
c6359b4… drh 551 pCur->iRowid = 1;
c6359b4… drh 552 return SQLITE_OK;
c6359b4… drh 553 }
c6359b4… drh 554
c6359b4… drh 555 /*
c6359b4… drh 556 ** SQLite will invoke this method one or more times while planning a query
c6359b4… drh 557 ** that uses the virtual table. This routine needs to create
c6359b4… drh 558 ** a query plan for each invocation and compute an estimated cost for that
c6359b4… drh 559 ** plan.
c6359b4… drh 560 */
c6359b4… drh 561 static int builtinVtabBestIndex(
c6359b4… drh 562 sqlite3_vtab *tab,
c6359b4… drh 563 sqlite3_index_info *pIdxInfo
c6359b4… drh 564 ){
c6359b4… drh 565 pIdxInfo->estimatedCost = (double)count(aBuiltinFiles);
c6359b4… drh 566 pIdxInfo->estimatedRows = count(aBuiltinFiles);
c6359b4… drh 567 return SQLITE_OK;
c6359b4… drh 568 }
c6359b4… drh 569
c6359b4… drh 570 /*
275da70… danield 571 ** This following structure defines all the methods for the
c6359b4… drh 572 ** virtual table.
c6359b4… drh 573 */
c6359b4… drh 574 static sqlite3_module builtinVtabModule = {
c6359b4… drh 575 /* iVersion */ 0,
c6359b4… drh 576 /* xCreate */ 0, /* The builtin vtab is eponymous and read-only */
c6359b4… drh 577 /* xConnect */ builtinVtabConnect,
c6359b4… drh 578 /* xBestIndex */ builtinVtabBestIndex,
c6359b4… drh 579 /* xDisconnect */ builtinVtabDisconnect,
c6359b4… drh 580 /* xDestroy */ 0,
c6359b4… drh 581 /* xOpen */ builtinVtabOpen,
c6359b4… drh 582 /* xClose */ builtinVtabClose,
c6359b4… drh 583 /* xFilter */ builtinVtabFilter,
c6359b4… drh 584 /* xNext */ builtinVtabNext,
c6359b4… drh 585 /* xEof */ builtinVtabEof,
c6359b4… drh 586 /* xColumn */ builtinVtabColumn,
c6359b4… drh 587 /* xRowid */ builtinVtabRowid,
c6359b4… drh 588 /* xUpdate */ 0,
c6359b4… drh 589 /* xBegin */ 0,
c6359b4… drh 590 /* xSync */ 0,
c6359b4… drh 591 /* xCommit */ 0,
c6359b4… drh 592 /* xRollback */ 0,
c6359b4… drh 593 /* xFindMethod */ 0,
c6359b4… drh 594 /* xRename */ 0,
c6359b4… drh 595 /* xSavepoint */ 0,
c6359b4… drh 596 /* xRelease */ 0,
c6359b4… drh 597 /* xRollbackTo */ 0,
ab05475… danield 598 /* xShadowName */ 0,
ab05475… danield 599 /* xIntegrity */ 0
c6359b4… drh 600 };
c6359b4… drh 601
c6359b4… drh 602
c6359b4… drh 603 /*
c6359b4… drh 604 ** Register the builtin virtual table
c6359b4… drh 605 */
c6359b4… drh 606 int builtin_vtab_register(sqlite3 *db){
c6359b4… drh 607 int rc = sqlite3_create_module(db, "builtin", &builtinVtabModule, 0);
c6359b4… drh 608 return rc;
c6359b4… drh 609 }
c6359b4… drh 610 /* End of the builtin virtual table
c6359b4… drh 611 ******************************************************************************/
34f7fd7… stephan 612
34f7fd7… stephan 613
34f7fd7… stephan 614 /*
34f7fd7… stephan 615 ** The first time this is called, it emits code to install and
34f7fd7… stephan 616 ** bootstrap the window.fossil object, using the built-in file
34f7fd7… stephan 617 ** fossil.bootstrap.js (not to be confused with bootstrap.js).
34f7fd7… stephan 618 **
34f7fd7… stephan 619 ** Subsequent calls are no-ops.
34f7fd7… stephan 620 **
34f7fd7… stephan 621 ** It emits 2 parts:
34f7fd7… stephan 622 **
34f7fd7… stephan 623 ** 1) window.fossil core object, some of which depends on C-level
34f7fd7… stephan 624 ** runtime data. That part of the script is always emitted inline. If
34f7fd7… stephan 625 ** addScriptTag is true then it is wrapped in its own SCRIPT tag, else
34f7fd7… stephan 626 ** it is assumed that the caller already opened a tag.
34f7fd7… stephan 627 **
34f7fd7… stephan 628 ** 2) Emits the static fossil.bootstrap.js using builtin_request_js().
34f7fd7… stephan 629 */
34f7fd7… stephan 630 void builtin_emit_script_fossil_bootstrap(int addScriptTag){
34f7fd7… stephan 631 static int once = 0;
34f7fd7… stephan 632 if(0==once++){
34f7fd7… stephan 633 char * zName;
34f7fd7… stephan 634 /* Set up the generic/app-agnostic parts of window.fossil
34f7fd7… stephan 635 ** which require C-level state... */
34f7fd7… stephan 636 if(addScriptTag!=0){
6854244… drh 637 style_script_begin(__FILE__,__LINE__);
34f7fd7… stephan 638 }
34f7fd7… stephan 639 CX("(function(){\n");
34f7fd7… stephan 640 CX(/*MSIE NodeList.forEach polyfill, courtesy of Mozilla:
34f7fd7… stephan 641 https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach#Polyfill
34f7fd7… stephan 642 */
34f7fd7… stephan 643 "if(window.NodeList && !NodeList.prototype.forEach){"
34f7fd7… stephan 644 "NodeList.prototype.forEach = Array.prototype.forEach;"
34f7fd7… stephan 645 "}\n");
34f7fd7… stephan 646 CX("if(!window.fossil) window.fossil={};\n"
34f7fd7… stephan 647 "window.fossil.version = %!j;\n"
34f7fd7… stephan 648 /* fossil.rootPath is the top-most CGI/server path,
34f7fd7… stephan 649 ** including a trailing slash. */
34f7fd7… stephan 650 "window.fossil.rootPath = %!j+'/';\n",
34f7fd7… stephan 651 get_version(), g.zTop);
34f7fd7… stephan 652 /* fossil.config = {...various config-level options...} */
34f7fd7… stephan 653 CX("window.fossil.config = {");
34f7fd7… stephan 654 zName = db_get("project-name", "");
34f7fd7… stephan 655 CX("projectName: %!j,\n", zName);
34f7fd7… stephan 656 fossil_free(zName);
34f7fd7… stephan 657 zName = db_get("short-project-name", "");
34f7fd7… stephan 658 CX("shortProjectName: %!j,\n", zName);
34f7fd7… stephan 659 fossil_free(zName);
34f7fd7… stephan 660 zName = db_get("project-code", "");
34f7fd7… stephan 661 CX("projectCode: %!j,\n", zName);
34f7fd7… stephan 662 fossil_free(zName);
34f7fd7… stephan 663 CX("/* Length of UUID hashes for display purposes. */");
34f7fd7… stephan 664 CX("hashDigits: %d, hashDigitsUrl: %d,\n",
34f7fd7… stephan 665 hash_digits(0), hash_digits(1));
51c1efd… stephan 666 CX("diffContextLines: %d,\n",
51c1efd… stephan 667 diff_context_lines(0));
34f7fd7… stephan 668 CX("editStateMarkers: {"
34f7fd7… stephan 669 "/*Symbolic markers to denote certain edit states.*/"
34f7fd7… stephan 670 "isNew:'[+]', isModified:'[*]', isDeleted:'[-]'},\n");
34f7fd7… stephan 671 CX("confirmerButtonTicks: 3 "
0b2f838… drh 672 "/*default fossil.confirmer tick count.*/,\n");
ca90659… stephan 673 CX("attachmentSizeLimit: %d,\n",
ca90659… stephan 674 db_get_int("attachment-size-limit",0));
0b2f838… drh 675 /* Inject certain info about the current skin... */
0b2f838… drh 676 CX("skin:{");
0b2f838… drh 677 /* can leak a local filesystem path:
0b2f838… drh 678 CX("name: %!j,", skin_in_use());*/
0b2f838… drh 679 CX("isDark: %s"
0b2f838… drh 680 "/*true if the current skin has the 'white-foreground' detail*/",
0b2f838… drh 681 skin_detail_boolean("white-foreground") ? "true" : "false");
0b2f838… drh 682 CX("}\n"/*fossil.config.skin*/);
34f7fd7… stephan 683 CX("};\n"/* fossil.config */);
a6b17d2… stephan 684 CX("window.fossil.config.forum = {");
ca90659… stephan 685 if( forum_statuses()->n>1 ){
ca90659… stephan 686 const ForumStatusList * fsl = forum_statuses();
ca90659… stephan 687 int i;
a6b17d2… stephan 688 CX("statuses: [");
ca90659… stephan 689 for(i = 0; i < fsl->n; ++i){
ca90659… stephan 690 const ForumStatus *fs = &fsl->aStatus[i];
ca90659… stephan 691 if(i) CX(",");
ca90659… stephan 692 CX("{label:%!j, value:%!j}", fs->zLabel, fs->zValue);
ca90659… stephan 693 }
a6b17d2… stephan 694 CX("],\n");
ca90659… stephan 695 }
a6b17d2… stephan 696 CX("maxTitleLength: %d", db_get_int("forum-max-title", 0));
a6b17d2… stephan 697 CX("};\n"/*window.fossil.config.forum*/);
ca90659… stephan 698 #define JBOOL(COND) ((COND) ? "true" : "false")
e8ba89b… drh 699 CX("window.fossil.user = {");
2472761… stephan 700 CX("name: %!j,", (g.zLogin&&*g.zLogin) ? g.zLogin : "guest");
ca90659… stephan 701 CX("isAdmin: %s,", JBOOL(g.perm.Admin || g.perm.Setup));
0e1be49… stephan 702 CX("mayAttachForum: %d,", g.perm.AttachForum
0e1be49… stephan 703 ? 1 : (g.perm.WrTForum
0e1be49… stephan 704 ? -1 : 0))
0e1be49… stephan 705 /* 0 = no, 1 = 'B' permissions,
0e1be49… stephan 706 ** -1 = moderated attachments. Potential TODO is a setting
0e1be49… stephan 707 ** to enable this. */;
ca90659… stephan 708 CX("enableDebug: %s,", JBOOL(g.perm.Debug || g.perm.Admin));
ca90659… stephan 709 CX("isIndividual: %s", JBOOL(login_is_individual()));
e8ba89b… drh 710 CX("};\n"/*fossil.user*/);
c952560… stephan 711 { /* Workaround for cases like:
c952560… stephan 712 ** https://fossil-scm.org/forum/forumpost/31cc00b361496cb8
c952560… stephan 713 ** Summary: custom skins which emit their own BODY need to have
c952560… stephan 714 ** the BODY element's CSS classes set so that various CSS selectors
c952560… stephan 715 ** and JS-side filters will work. */
c952560… stephan 716 char *pSlash = strchr(g.zPath,'/')/*hack taken from style.c*/;
c952560… stephan 717 if( pSlash ) *pSlash = 0;
db3c8e1… stephan 718 CX("for(const x of [%!j, \"rpage-%j\",\"cpage-%j\"]) {"
db3c8e1… stephan 719 "if(x) document.body.classList.add(x);};\n",
c952560… stephan 720 style_get_current_feature(), g.zPath, g.zPhase+1);
c952560… stephan 721 if( pSlash ) *pSlash = '/';
c952560… stephan 722 }
e8f603f… stephan 723 CX("if(fossil.config.skin.isDark) "
e8f603f… stephan 724 "document.body.classList.add('fossil-dark-style');\n");
34f7fd7… stephan 725 /*
34f7fd7… stephan 726 ** fossil.page holds info about the current page. This is also
34f7fd7… stephan 727 ** where the current page "should" store any of its own
34f7fd7… stephan 728 ** page-specific state, and it is reserved for that purpose.
34f7fd7… stephan 729 */
34f7fd7… stephan 730 CX("window.fossil.page = {"
34f7fd7… stephan 731 "name:\"%T\""
34f7fd7… stephan 732 "};\n", g.zPath);
34f7fd7… stephan 733 CX("})();\n");
34f7fd7… stephan 734 if(addScriptTag!=0){
6854244… drh 735 style_script_end();
34f7fd7… stephan 736 }
34f7fd7… stephan 737 /* The remaining window.fossil bootstrap code is not dependent on
34f7fd7… stephan 738 ** C-runtime state... */
34f7fd7… stephan 739 builtin_request_js("fossil.bootstrap.js");
6854244… drh 740 }
ca90659… stephan 741 #undef JBOOL
9b2b6f5… stephan 742 }
9b2b6f5… stephan 743
9b2b6f5… stephan 744 /*
9b2b6f5… stephan 745 ** Given the NAME part of fossil.NAME.js, this function checks whether
9b2b6f5… stephan 746 ** that module has been emitted by this function before. If it has,
9b2b6f5… stephan 747 ** it returns -1 with no side effects. If it has not, it queues up
9b2b6f5… stephan 748 ** (via builtin_request_js()) an emit of the module via and all of its
9b2b6f5… stephan 749 ** known (by this function) fossil.XYZ.js dependencies (in their
9b2b6f5… stephan 750 ** dependency order) and returns 1. If it does not find the given
9b2b6f5… stephan 751 ** module name it returns 0.
9b2b6f5… stephan 752 **
9b2b6f5… stephan 753 ** As a special case, if passed 0 then it queues up all known modules
9b2b6f5… stephan 754 ** and returns -1.
9b2b6f5… stephan 755 **
9b2b6f5… stephan 756 ** The very first time this is called, it unconditionally calls
9b2b6f5… stephan 757 ** builtin_emit_script_fossil_bootstrap().
9b2b6f5… stephan 758 **
9b2b6f5… stephan 759 ** Any given module is only queued once, whether it is explicitly
9b2b6f5… stephan 760 ** passed to the function or resolved as a dependency. Any attempts to
9b2b6f5… stephan 761 ** re-queue them later are harmless no-ops.
9b2b6f5… stephan 762 */
9b2b6f5… stephan 763 static int builtin_emit_fossil_js_once(const char * zName){
9b2b6f5… stephan 764 static int once = 0;
9b2b6f5… stephan 765 int i;
9b2b6f5… stephan 766 static struct FossilJs {
9b2b6f5… stephan 767 const char * zName; /* NAME part of fossil.NAME.js */
9b2b6f5… stephan 768 int emitted; /* True if already emitted. */
9b2b6f5… stephan 769 const char * zDeps; /* \0-delimited list of other FossilJs
9b2b6f5… stephan 770 ** entries: all known deps of this one. Each
9b2b6f5… stephan 771 ** REQUIRES an EXPLICIT trailing \0, including
9b2b6f5… stephan 772 ** the final one! */
9b2b6f5… stephan 773 } fjs[] = {
9b2b6f5… stephan 774 /* This list ordering isn't strictly important. */
ca90659… stephan 775 {"attach", 0, "dom\0"},
9b2b6f5… stephan 776 {"confirmer", 0, 0},
9b2b6f5… stephan 777 {"copybutton", 0, "dom\0"},
77adf8d… stephan 778 {"diff", 0, "dom\0fetch\0storage\0"
e2bdc10… danield 779 /* maintenance note: "diff" needs "storage" for storing the
77adf8d… stephan 780 ** sbs-sync-scroll toggle. */},
9b2b6f5… stephan 781 {"dom", 0, 0},
9b2b6f5… stephan 782 {"fetch", 0, 0},
9b2b6f5… stephan 783 {"numbered-lines", 0, "popupwidget\0copybutton\0"},
9b2b6f5… stephan 784 {"pikchr", 0, "dom\0"},
9b2b6f5… stephan 785 {"popupwidget", 0, "dom\0"},
9b2b6f5… stephan 786 {"storage", 0, 0},
9b2b6f5… stephan 787 {"tabs", 0, "dom\0"}
9b2b6f5… stephan 788 };
9b2b6f5… stephan 789 const int nFjs = sizeof(fjs) / sizeof(fjs[0]);
9b2b6f5… stephan 790 if(0==once){
9b2b6f5… stephan 791 ++once;
9b2b6f5… stephan 792 builtin_emit_script_fossil_bootstrap(1);
9b2b6f5… stephan 793 }
9b2b6f5… stephan 794 if(0==zName){
9b2b6f5… stephan 795 for( i = 0; i < nFjs; ++i ){
9b2b6f5… stephan 796 builtin_emit_fossil_js_once(fjs[i].zName);
9b2b6f5… stephan 797 }
539eb61… stephan 798 return -1;
9b2b6f5… stephan 799 }
9b2b6f5… stephan 800 for( i = 0; i < nFjs; ++i ){
9b2b6f5… stephan 801 if(0==strcmp(zName, fjs[i].zName)){
9b2b6f5… stephan 802 if(fjs[i].emitted){
9b2b6f5… stephan 803 return -1;
9b2b6f5… stephan 804 }else{
9b2b6f5… stephan 805 char nameBuffer[50];
9b2b6f5… stephan 806 if(fjs[i].zDeps){
9b2b6f5… stephan 807 const char * zDep = fjs[i].zDeps;
9b2b6f5… stephan 808 while(*zDep!=0){
9b2b6f5… stephan 809 builtin_emit_fossil_js_once(zDep);
9b2b6f5… stephan 810 zDep += strlen(zDep)+1/*NUL delimiter*/;
9b2b6f5… stephan 811 }
9b2b6f5… stephan 812 }
9b2b6f5… stephan 813 sqlite3_snprintf(sizeof(nameBuffer)-1, nameBuffer,
9b2b6f5… stephan 814 "fossil.%s.js", fjs[i].zName);
9b2b6f5… stephan 815 builtin_request_js(nameBuffer);
9b2b6f5… stephan 816 fjs[i].emitted = 1;
9b2b6f5… stephan 817 return 1;
9b2b6f5… stephan 818 }
9b2b6f5… stephan 819 }
9b2b6f5… stephan 820 }
9b2b6f5… stephan 821 return 0;
9b2b6f5… stephan 822 }
9b2b6f5… stephan 823
9b2b6f5… stephan 824 /*
9b2b6f5… stephan 825 ** COMMAND: test-js-once
9b2b6f5… stephan 826 **
9b2b6f5… stephan 827 ** Tester for builtin_emit_fossil_js_once().
9b2b6f5… stephan 828 **
9b2b6f5… stephan 829 ** Usage: %fossil test-js-once filename
9b2b6f5… stephan 830 */
9b2b6f5… stephan 831 void test_js_once(void){
9b2b6f5… stephan 832 int i;
9b2b6f5… stephan 833 if(g.argc<2){
9b2b6f5… stephan 834 usage("?FILENAME...?");
9b2b6f5… stephan 835 }
9b2b6f5… stephan 836 if(2==g.argc){
9b2b6f5… stephan 837 builtin_emit_fossil_js_once(0);
9b2b6f5… stephan 838 assert(builtin.nReq>8);
9b2b6f5… stephan 839 }else{
9b2b6f5… stephan 840 for(i = 2; i < g.argc; ++i){
9b2b6f5… stephan 841 builtin_emit_fossil_js_once(g.argv[i]);
9b2b6f5… stephan 842 }
9b2b6f5… stephan 843 assert(builtin.nReq>1 && "don't forget implicit fossil.bootstrap.js");
9b2b6f5… stephan 844 }
9b2b6f5… stephan 845 for(i = 0; i < builtin.nReq; ++i){
9b2b6f5… stephan 846 fossil_print("ndx#%d = %d = %s\n", i, builtin.aReq[i],
9b2b6f5… stephan 847 aBuiltinFiles[builtin.aReq[i]].zName);
9b2b6f5… stephan 848 }
0b2f838… drh 849 }
34f7fd7… stephan 850
34f7fd7… stephan 851 /*
34f7fd7… stephan 852 ** Convenience wrapper which calls builtin_request_js() for a series
34f7fd7… stephan 853 ** of builtin scripts named fossil.NAME.js. The first time it is
34f7fd7… stephan 854 ** called, it also calls builtin_emit_script_fossil_bootstrap() to
34f7fd7… stephan 855 ** initialize the window.fossil JS API. The first argument is the NAME
34f7fd7… stephan 856 ** part of the first API to emit. All subsequent arguments must be
34f7fd7… stephan 857 ** strings of the NAME part of additional fossil.NAME.js files,
34f7fd7… stephan 858 ** followed by a NULL argument to terminate the list.
34f7fd7… stephan 859 **
4d88ef4… wyoung 860 ** e.g. pass it ("fetch", "dom", "tabs", NULL) to load those 3 APIs (or
4d88ef4… wyoung 861 ** pass it ("fetch","tabs",NULL), as "dom" is a dependency of "tabs", so
4d88ef4… wyoung 862 ** it will be automatically loaded). Do not forget the trailing NULL,
4d88ef4… wyoung 863 ** and do not pass 0 instead, since that isn't always equivalent to NULL
4d88ef4… wyoung 864 ** in this context.
9b2b6f5… stephan 865 **
9b2b6f5… stephan 866 ** If it is JS_BUNDLED then this routine queues up an emit of ALL of
9b2b6f5… stephan 867 ** the JS fossil.XYZ.js APIs which are not strictly specific to a
9b2b6f5… stephan 868 ** single page, and then calls builtin_fulfill_js_requests(). The idea
9b2b6f5… stephan 869 ** is that we can get better bundle caching and reduced HTTP requests
9b2b6f5… stephan 870 ** by including all JS, rather than creating separate bundles on a
9b2b6f5… stephan 871 ** per-page basis. In this case, all arguments are ignored!
9b2b6f5… stephan 872 **
9b2b6f5… stephan 873 ** This function has an internal mapping of the dependencies for each
9b2b6f5… stephan 874 ** of the known fossil.XYZ.js modules and ensures that the
9b2b6f5… stephan 875 ** dependencies also get queued (recursively) and that each module is
9b2b6f5… stephan 876 ** queued only once.
9b2b6f5… stephan 877 **
275da70… danield 878 ** If passed a name which is not a base fossil module name then it
9b2b6f5… stephan 879 ** will fail fatally!
9b2b6f5… stephan 880 **
9b2b6f5… stephan 881 ** DO NOT use this for loading fossil.page.*.js: use
9b2b6f5… stephan 882 ** builtin_request_js() for those.
9b2b6f5… stephan 883 **
9b2b6f5… stephan 884 ** If the current JS delivery mode is *not* JS_BUNDLED then this
9b2b6f5… stephan 885 ** function queues up a request for each given module and its known
9b2b6f5… stephan 886 ** dependencies, but does not immediately fulfill the request, thus it
9b2b6f5… stephan 887 ** can be called multiple times.
9b2b6f5… stephan 888 **
9b2b6f5… stephan 889 ** If a given module is ever passed to this more than once, either in
9b2b6f5… stephan 890 ** a single invocation or multiples, it is only queued for emit a
9b2b6f5… stephan 891 ** single time (i.e. the 2nd and subsequent ones become
9b2b6f5… stephan 892 ** no-ops). Likewise, if a module is requested but was already
9b2b6f5… stephan 893 ** automatically queued to fulfill a dependency, the explicit request
9b2b6f5… stephan 894 ** becomes a no-op.
9b2b6f5… stephan 895 **
9b2b6f5… stephan 896 ** Bundled mode is the only mode in which this API greatly improves
9b2b6f5… stephan 897 ** aggregate over-the-wire and HTTP request costs. For other modes,
9b2b6f5… stephan 898 ** reducing the inclusion of fossil.XYZ APIs to their bare minimum
9b2b6f5… stephan 899 ** provides the lowest aggregate costs. For debate and details, see
9b2b6f5… stephan 900 ** the discussion at:
9b2b6f5… stephan 901 **
9b2b6f5… stephan 902 ** https://fossil-scm.org/forum/forumpost/3fa2633f3e
34f7fd7… stephan 903 **
34f7fd7… stephan 904 ** In practice it is normally necessary (or preferred) to call
34f7fd7… stephan 905 ** builtin_fulfill_js_requests() after calling this, before proceeding
34f7fd7… stephan 906 ** to call builtin_request_js() for page-specific JS, in order to
34f7fd7… stephan 907 ** improve cachability.
34f7fd7… stephan 908 **
34f7fd7… stephan 909 ** Minor caveat: the purpose of emitting all of the fossil.XYZ JS APIs
34f7fd7… stephan 910 ** at once is to reduce over-the-wire transfers by enabling cross-page
34f7fd7… stephan 911 ** caching, but if there are other JS scripts pending via
34f7fd7… stephan 912 ** builtin_request_js() when this is called then they will be included
34f7fd7… stephan 913 ** in the JS request emitted by this routine, resulting in a different
34f7fd7… stephan 914 ** script URL than if they were not included. Thus, if a given page
34f7fd7… stephan 915 ** has its own scripts to install via builtin_request_js(), they
34f7fd7… stephan 916 ** should, if possible, be delayed until after this is called OR the
34f7fd7… stephan 917 ** page should call builtin_fulfill_js_requests() to flush the request
34f7fd7… stephan 918 ** queue before calling this routine.
f5c0408… stephan 919 **
9b2b6f5… stephan 920 ** Achtung: the fossil.page.XYZ.js files are page-specific, containing
9b2b6f5… stephan 921 ** the app-level logic for that specific page, and loading more than
9b2b6f5… stephan 922 ** one of them in a single page will break that page. Each of those
9b2b6f5… stephan 923 ** expects to "own" the page it is loaded in, and it should be loaded
9b2b6f5… stephan 924 ** as late in the JS-loading process as feasible, ideally bundled (via
9b2b6f5… stephan 925 ** builtin_request_js()) with any other app-/page-specific JS it may
9b2b6f5… stephan 926 ** need.
9b2b6f5… stephan 927 **
f5c0408… stephan 928 ** Example usage:
f5c0408… stephan 929 **
815b4fc… wyoung 930 ** builtin_fossil_js_bundle_or("dom", "fetch", NULL);
f5c0408… stephan 931 **
9b2b6f5… stephan 932 ** In bundled mode, that will (the first time it is called) emit all
9b2b6f5… stephan 933 ** builtin fossil JS APIs and "fulfill" the queue immediately. In
f5c0408… stephan 934 ** non-bundled mode it will queue up the "dom" and "fetch" APIs to be
f5c0408… stephan 935 ** emitted the next time builtin_fulfill_js_requests() is called.
34f7fd7… stephan 936 */
815b4fc… wyoung 937 NULL_SENTINEL void builtin_fossil_js_bundle_or( const char * zApi, ... ) {
34f7fd7… stephan 938 static int bundled = 0;
9b2b6f5… stephan 939 const char *zArg;
9b2b6f5… stephan 940 va_list vargs;
9b2b6f5… stephan 941
34f7fd7… stephan 942 if(JS_BUNDLED == builtin_get_js_delivery_mode()){
34f7fd7… stephan 943 if(!bundled){
34f7fd7… stephan 944 bundled = 1;
9b2b6f5… stephan 945 builtin_emit_fossil_js_once(0);
34f7fd7… stephan 946 builtin_fulfill_js_requests();
34f7fd7… stephan 947 }
9b2b6f5… stephan 948 return;
9b2b6f5… stephan 949 }
9b2b6f5… stephan 950 va_start(vargs,zApi);
815b4fc… wyoung 951 for( zArg = zApi; zArg!=NULL; (zArg = va_arg (vargs, const char *))){
9b2b6f5… stephan 952 if(0==builtin_emit_fossil_js_once(zArg)){
9b2b6f5… stephan 953 fossil_fatal("Unknown fossil JS module: %s\n", zArg);
9b2b6f5… stephan 954 }
34f7fd7… stephan 955 }
9b2b6f5… stephan 956 va_end(vargs);
34f7fd7… stephan 957 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button