Fossil SCM
Refactored sendText() to use the new output abstraction layer.
Commit
d3a623497404755832197ebac6329bb9a05a0786
Parent
3c0209f5dcdc4e7…
1 file changed
+20
-19
+20
-19
| --- src/th_main.c | ||
| +++ src/th_main.c | ||
| @@ -98,22 +98,22 @@ | ||
| 98 | 98 | |
| 99 | 99 | /* |
| 100 | 100 | ** Send text to the appropriate output: Either to the console |
| 101 | 101 | ** or to the CGI reply buffer. |
| 102 | 102 | */ |
| 103 | -static void sendText(const char *z, int n, int encode){ | |
| 103 | +static void sendText(Th_Interp *pInterp, const char *z, int n, int encode){ | |
| 104 | + if(NULL == pInterp){ | |
| 105 | + pInterp = g.interp; | |
| 106 | + } | |
| 107 | + assert( NULL != pInterp ); | |
| 104 | 108 | if( enableOutput && n ){ |
| 105 | 109 | if( n<0 ) n = strlen(z); |
| 106 | 110 | if( encode ){ |
| 107 | 111 | z = htmlize(z, n); |
| 108 | 112 | n = strlen(z); |
| 109 | 113 | } |
| 110 | - if( g.cgiOutput ){ | |
| 111 | - Th_output_f_cgi_content(z, n, NULL); | |
| 112 | - }else{ | |
| 113 | - Th_output_f_FILE( z, n, stdout ); | |
| 114 | - } | |
| 114 | + Th_output( pInterp, z, n ); | |
| 115 | 115 | if( encode ) fossil_free((char*)z); |
| 116 | 116 | } |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | struct PutsCmdData { |
| @@ -142,16 +142,16 @@ | ||
| 142 | 142 | if( argc<2 ){ |
| 143 | 143 | return Th_WrongNumArgs(interp, "puts STRING ...STRING_N"); |
| 144 | 144 | } |
| 145 | 145 | for( i = 1; i < argc; ++i ){ |
| 146 | 146 | if(sepLen && (i>1)){ |
| 147 | - sendText(fmt->sep, sepLen, 0); | |
| 147 | + sendText(interp, fmt->sep, sepLen, 0); | |
| 148 | 148 | } |
| 149 | - sendText((char const*)argv[i], argl[i], fmt->escapeHtml); | |
| 149 | + sendText(interp, (char const*)argv[i], argl[i], fmt->escapeHtml); | |
| 150 | 150 | } |
| 151 | 151 | if(fmt->eol){ |
| 152 | - sendText(fmt->eol, strlen(fmt->eol), 0); | |
| 152 | + sendText(interp, fmt->eol, strlen(fmt->eol), 0); | |
| 153 | 153 | } |
| 154 | 154 | return TH_OK; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /* |
| @@ -362,11 +362,11 @@ | ||
| 362 | 362 | Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem); |
| 363 | 363 | blob_init(&name, (char*)argv[1], argl[1]); |
| 364 | 364 | zValue = Th_Fetch(blob_str(&name), &nValue); |
| 365 | 365 | z = mprintf("<select name=\"%z\" size=\"%d\">", |
| 366 | 366 | htmlize(blob_buffer(&name), blob_size(&name)), height); |
| 367 | - sendText(z, -1, 0); | |
| 367 | + sendText(interp, z, -1, 0); | |
| 368 | 368 | free(z); |
| 369 | 369 | blob_reset(&name); |
| 370 | 370 | for(i=0; i<nElem; i++){ |
| 371 | 371 | zH = htmlize((char*)azElem[i], aszElem[i]); |
| 372 | 372 | if( zValue && aszElem[i]==nValue |
| @@ -375,14 +375,14 @@ | ||
| 375 | 375 | zH, zH); |
| 376 | 376 | }else{ |
| 377 | 377 | z = mprintf("<option value=\"%s\">%s</option>", zH, zH); |
| 378 | 378 | } |
| 379 | 379 | free(zH); |
| 380 | - sendText(z, -1, 0); | |
| 380 | + sendText(interp, z, -1, 0); | |
| 381 | 381 | free(z); |
| 382 | 382 | } |
| 383 | - sendText("</select>", -1, 0); | |
| 383 | + sendText(interp, "</select>", -1, 0); | |
| 384 | 384 | Th_Free(interp, azElem); |
| 385 | 385 | } |
| 386 | 386 | return TH_OK; |
| 387 | 387 | } |
| 388 | 388 | |
| @@ -1294,10 +1294,11 @@ | ||
| 1294 | 1294 | vtab.out.f = Th_output_f_cgi_content; |
| 1295 | 1295 | }else{ |
| 1296 | 1296 | vtab.out.f = Th_output_f_FILE; |
| 1297 | 1297 | vtab.out.pState = stdout; |
| 1298 | 1298 | } |
| 1299 | + vtab.out.enabled = enableOutput; | |
| 1299 | 1300 | g.interp = Th_CreateInterp(&vtab); |
| 1300 | 1301 | th_register_language(g.interp); /* Basic scripting commands. */ |
| 1301 | 1302 | #ifdef FOSSIL_ENABLE_TCL |
| 1302 | 1303 | if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ |
| 1303 | 1304 | th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ |
| @@ -1442,11 +1443,11 @@ | ||
| 1442 | 1443 | while( z[i] ){ |
| 1443 | 1444 | if( z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){ |
| 1444 | 1445 | const char *zVar; |
| 1445 | 1446 | int nVar; |
| 1446 | 1447 | int encode = 1; |
| 1447 | - sendText(z, i, 0); | |
| 1448 | + sendText(g.interp, z, i, 0); | |
| 1448 | 1449 | if( z[i+1]=='<' ){ |
| 1449 | 1450 | /* Variables of the form $<aaa> are html escaped */ |
| 1450 | 1451 | zVar = &z[i+2]; |
| 1451 | 1452 | nVar = n-2; |
| 1452 | 1453 | }else{ |
| @@ -1457,13 +1458,13 @@ | ||
| 1457 | 1458 | } |
| 1458 | 1459 | rc = Th_GetVar(g.interp, (char*)zVar, nVar); |
| 1459 | 1460 | z += i+1+n; |
| 1460 | 1461 | i = 0; |
| 1461 | 1462 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1462 | - sendText((char*)zResult, n, encode); | |
| 1463 | + sendText(g.interp, (char*)zResult, n, encode); | |
| 1463 | 1464 | }else if( z[i]=='<' && isBeginScriptTag(&z[i]) ){ |
| 1464 | - sendText(z, i, 0); | |
| 1465 | + sendText(g.interp, z, i, 0); | |
| 1465 | 1466 | z += i+5; |
| 1466 | 1467 | for(i=0; z[i] && (z[i]!='<' || !isEndScriptTag(&z[i])); i++){} |
| 1467 | 1468 | rc = Th_Eval(g.interp, 0, (const char*)z, i); |
| 1468 | 1469 | if( rc!=TH_OK ) break; |
| 1469 | 1470 | z += i; |
| @@ -1472,16 +1473,16 @@ | ||
| 1472 | 1473 | }else{ |
| 1473 | 1474 | i++; |
| 1474 | 1475 | } |
| 1475 | 1476 | } |
| 1476 | 1477 | if( rc==TH_ERROR ){ |
| 1477 | - sendText("<hr><p class=\"thmainError\">ERROR: ", -1, 0); | |
| 1478 | + sendText(g.interp, "<hr><p class=\"thmainError\">ERROR: ", -1, 0); | |
| 1478 | 1479 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1479 | - sendText((char*)zResult, n, 1); | |
| 1480 | - sendText("</p>", -1, 0); | |
| 1480 | + sendText(g.interp, (char*)zResult, n, 1); | |
| 1481 | + sendText(g.interp, "</p>", -1, 0); | |
| 1481 | 1482 | }else{ |
| 1482 | - sendText(z, i, 0); | |
| 1483 | + sendText(g.interp, z, i, 0); | |
| 1483 | 1484 | } |
| 1484 | 1485 | return rc; |
| 1485 | 1486 | } |
| 1486 | 1487 | |
| 1487 | 1488 | /* |
| 1488 | 1489 |
| --- src/th_main.c | |
| +++ src/th_main.c | |
| @@ -98,22 +98,22 @@ | |
| 98 | |
| 99 | /* |
| 100 | ** Send text to the appropriate output: Either to the console |
| 101 | ** or to the CGI reply buffer. |
| 102 | */ |
| 103 | static void sendText(const char *z, int n, int encode){ |
| 104 | if( enableOutput && n ){ |
| 105 | if( n<0 ) n = strlen(z); |
| 106 | if( encode ){ |
| 107 | z = htmlize(z, n); |
| 108 | n = strlen(z); |
| 109 | } |
| 110 | if( g.cgiOutput ){ |
| 111 | Th_output_f_cgi_content(z, n, NULL); |
| 112 | }else{ |
| 113 | Th_output_f_FILE( z, n, stdout ); |
| 114 | } |
| 115 | if( encode ) fossil_free((char*)z); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | struct PutsCmdData { |
| @@ -142,16 +142,16 @@ | |
| 142 | if( argc<2 ){ |
| 143 | return Th_WrongNumArgs(interp, "puts STRING ...STRING_N"); |
| 144 | } |
| 145 | for( i = 1; i < argc; ++i ){ |
| 146 | if(sepLen && (i>1)){ |
| 147 | sendText(fmt->sep, sepLen, 0); |
| 148 | } |
| 149 | sendText((char const*)argv[i], argl[i], fmt->escapeHtml); |
| 150 | } |
| 151 | if(fmt->eol){ |
| 152 | sendText(fmt->eol, strlen(fmt->eol), 0); |
| 153 | } |
| 154 | return TH_OK; |
| 155 | } |
| 156 | |
| 157 | /* |
| @@ -362,11 +362,11 @@ | |
| 362 | Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem); |
| 363 | blob_init(&name, (char*)argv[1], argl[1]); |
| 364 | zValue = Th_Fetch(blob_str(&name), &nValue); |
| 365 | z = mprintf("<select name=\"%z\" size=\"%d\">", |
| 366 | htmlize(blob_buffer(&name), blob_size(&name)), height); |
| 367 | sendText(z, -1, 0); |
| 368 | free(z); |
| 369 | blob_reset(&name); |
| 370 | for(i=0; i<nElem; i++){ |
| 371 | zH = htmlize((char*)azElem[i], aszElem[i]); |
| 372 | if( zValue && aszElem[i]==nValue |
| @@ -375,14 +375,14 @@ | |
| 375 | zH, zH); |
| 376 | }else{ |
| 377 | z = mprintf("<option value=\"%s\">%s</option>", zH, zH); |
| 378 | } |
| 379 | free(zH); |
| 380 | sendText(z, -1, 0); |
| 381 | free(z); |
| 382 | } |
| 383 | sendText("</select>", -1, 0); |
| 384 | Th_Free(interp, azElem); |
| 385 | } |
| 386 | return TH_OK; |
| 387 | } |
| 388 | |
| @@ -1294,10 +1294,11 @@ | |
| 1294 | vtab.out.f = Th_output_f_cgi_content; |
| 1295 | }else{ |
| 1296 | vtab.out.f = Th_output_f_FILE; |
| 1297 | vtab.out.pState = stdout; |
| 1298 | } |
| 1299 | g.interp = Th_CreateInterp(&vtab); |
| 1300 | th_register_language(g.interp); /* Basic scripting commands. */ |
| 1301 | #ifdef FOSSIL_ENABLE_TCL |
| 1302 | if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ |
| 1303 | th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ |
| @@ -1442,11 +1443,11 @@ | |
| 1442 | while( z[i] ){ |
| 1443 | if( z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){ |
| 1444 | const char *zVar; |
| 1445 | int nVar; |
| 1446 | int encode = 1; |
| 1447 | sendText(z, i, 0); |
| 1448 | if( z[i+1]=='<' ){ |
| 1449 | /* Variables of the form $<aaa> are html escaped */ |
| 1450 | zVar = &z[i+2]; |
| 1451 | nVar = n-2; |
| 1452 | }else{ |
| @@ -1457,13 +1458,13 @@ | |
| 1457 | } |
| 1458 | rc = Th_GetVar(g.interp, (char*)zVar, nVar); |
| 1459 | z += i+1+n; |
| 1460 | i = 0; |
| 1461 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1462 | sendText((char*)zResult, n, encode); |
| 1463 | }else if( z[i]=='<' && isBeginScriptTag(&z[i]) ){ |
| 1464 | sendText(z, i, 0); |
| 1465 | z += i+5; |
| 1466 | for(i=0; z[i] && (z[i]!='<' || !isEndScriptTag(&z[i])); i++){} |
| 1467 | rc = Th_Eval(g.interp, 0, (const char*)z, i); |
| 1468 | if( rc!=TH_OK ) break; |
| 1469 | z += i; |
| @@ -1472,16 +1473,16 @@ | |
| 1472 | }else{ |
| 1473 | i++; |
| 1474 | } |
| 1475 | } |
| 1476 | if( rc==TH_ERROR ){ |
| 1477 | sendText("<hr><p class=\"thmainError\">ERROR: ", -1, 0); |
| 1478 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1479 | sendText((char*)zResult, n, 1); |
| 1480 | sendText("</p>", -1, 0); |
| 1481 | }else{ |
| 1482 | sendText(z, i, 0); |
| 1483 | } |
| 1484 | return rc; |
| 1485 | } |
| 1486 | |
| 1487 | /* |
| 1488 |
| --- src/th_main.c | |
| +++ src/th_main.c | |
| @@ -98,22 +98,22 @@ | |
| 98 | |
| 99 | /* |
| 100 | ** Send text to the appropriate output: Either to the console |
| 101 | ** or to the CGI reply buffer. |
| 102 | */ |
| 103 | static void sendText(Th_Interp *pInterp, const char *z, int n, int encode){ |
| 104 | if(NULL == pInterp){ |
| 105 | pInterp = g.interp; |
| 106 | } |
| 107 | assert( NULL != pInterp ); |
| 108 | if( enableOutput && n ){ |
| 109 | if( n<0 ) n = strlen(z); |
| 110 | if( encode ){ |
| 111 | z = htmlize(z, n); |
| 112 | n = strlen(z); |
| 113 | } |
| 114 | Th_output( pInterp, z, n ); |
| 115 | if( encode ) fossil_free((char*)z); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | struct PutsCmdData { |
| @@ -142,16 +142,16 @@ | |
| 142 | if( argc<2 ){ |
| 143 | return Th_WrongNumArgs(interp, "puts STRING ...STRING_N"); |
| 144 | } |
| 145 | for( i = 1; i < argc; ++i ){ |
| 146 | if(sepLen && (i>1)){ |
| 147 | sendText(interp, fmt->sep, sepLen, 0); |
| 148 | } |
| 149 | sendText(interp, (char const*)argv[i], argl[i], fmt->escapeHtml); |
| 150 | } |
| 151 | if(fmt->eol){ |
| 152 | sendText(interp, fmt->eol, strlen(fmt->eol), 0); |
| 153 | } |
| 154 | return TH_OK; |
| 155 | } |
| 156 | |
| 157 | /* |
| @@ -362,11 +362,11 @@ | |
| 362 | Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem); |
| 363 | blob_init(&name, (char*)argv[1], argl[1]); |
| 364 | zValue = Th_Fetch(blob_str(&name), &nValue); |
| 365 | z = mprintf("<select name=\"%z\" size=\"%d\">", |
| 366 | htmlize(blob_buffer(&name), blob_size(&name)), height); |
| 367 | sendText(interp, z, -1, 0); |
| 368 | free(z); |
| 369 | blob_reset(&name); |
| 370 | for(i=0; i<nElem; i++){ |
| 371 | zH = htmlize((char*)azElem[i], aszElem[i]); |
| 372 | if( zValue && aszElem[i]==nValue |
| @@ -375,14 +375,14 @@ | |
| 375 | zH, zH); |
| 376 | }else{ |
| 377 | z = mprintf("<option value=\"%s\">%s</option>", zH, zH); |
| 378 | } |
| 379 | free(zH); |
| 380 | sendText(interp, z, -1, 0); |
| 381 | free(z); |
| 382 | } |
| 383 | sendText(interp, "</select>", -1, 0); |
| 384 | Th_Free(interp, azElem); |
| 385 | } |
| 386 | return TH_OK; |
| 387 | } |
| 388 | |
| @@ -1294,10 +1294,11 @@ | |
| 1294 | vtab.out.f = Th_output_f_cgi_content; |
| 1295 | }else{ |
| 1296 | vtab.out.f = Th_output_f_FILE; |
| 1297 | vtab.out.pState = stdout; |
| 1298 | } |
| 1299 | vtab.out.enabled = enableOutput; |
| 1300 | g.interp = Th_CreateInterp(&vtab); |
| 1301 | th_register_language(g.interp); /* Basic scripting commands. */ |
| 1302 | #ifdef FOSSIL_ENABLE_TCL |
| 1303 | if( getenv("TH1_ENABLE_TCL")!=0 || db_get_boolean("tcl", 0) ){ |
| 1304 | th_register_tcl(g.interp, &g.tcl); /* Tcl integration commands. */ |
| @@ -1442,11 +1443,11 @@ | |
| 1443 | while( z[i] ){ |
| 1444 | if( z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){ |
| 1445 | const char *zVar; |
| 1446 | int nVar; |
| 1447 | int encode = 1; |
| 1448 | sendText(g.interp, z, i, 0); |
| 1449 | if( z[i+1]=='<' ){ |
| 1450 | /* Variables of the form $<aaa> are html escaped */ |
| 1451 | zVar = &z[i+2]; |
| 1452 | nVar = n-2; |
| 1453 | }else{ |
| @@ -1457,13 +1458,13 @@ | |
| 1458 | } |
| 1459 | rc = Th_GetVar(g.interp, (char*)zVar, nVar); |
| 1460 | z += i+1+n; |
| 1461 | i = 0; |
| 1462 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1463 | sendText(g.interp, (char*)zResult, n, encode); |
| 1464 | }else if( z[i]=='<' && isBeginScriptTag(&z[i]) ){ |
| 1465 | sendText(g.interp, z, i, 0); |
| 1466 | z += i+5; |
| 1467 | for(i=0; z[i] && (z[i]!='<' || !isEndScriptTag(&z[i])); i++){} |
| 1468 | rc = Th_Eval(g.interp, 0, (const char*)z, i); |
| 1469 | if( rc!=TH_OK ) break; |
| 1470 | z += i; |
| @@ -1472,16 +1473,16 @@ | |
| 1473 | }else{ |
| 1474 | i++; |
| 1475 | } |
| 1476 | } |
| 1477 | if( rc==TH_ERROR ){ |
| 1478 | sendText(g.interp, "<hr><p class=\"thmainError\">ERROR: ", -1, 0); |
| 1479 | zResult = (char*)Th_GetResult(g.interp, &n); |
| 1480 | sendText(g.interp, (char*)zResult, n, 1); |
| 1481 | sendText(g.interp, "</p>", -1, 0); |
| 1482 | }else{ |
| 1483 | sendText(g.interp, z, i, 0); |
| 1484 | } |
| 1485 | return rc; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 |