Fossil SCM

Found a workaround for the th1 puts escaping, but am still wondering whether it's a viable long-term solution.

stephan 2020-09-13 18:26 UTC pikchr-th
Commit 18abb60d42333922b1286f4235eaecb90de3fadb59d326c3f77f3d8096dfb10e
2 files changed +41 -28 +8 -6
+41 -28
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -167,11 +167,11 @@
167167
builtin_fulfill_js_requests();
168168
style_footer();
169169
}
170170
171171
static void pikchr_th_init(u32 fThInit){
172
- Th_FossilInit(fThInit);
172
+ Th_FossilInit(fThInit & TH_INIT_MASK);
173173
}
174174
175175
/*
176176
** COMMAND: pikchr
177177
**
@@ -184,10 +184,13 @@
184184
**
185185
** -th Process the input using TH1 before passing it to pikchr.
186186
**
187187
** -th-novar Disable $var and $<var> TH1 processing. Only applies
188188
** with the -th flag.
189
+**
190
+** -th-nopic When using -th, output the post-TH1'd script
191
+** instead of the pikchr-rendered output.
189192
**
190193
** -th-trace Trace TH1 execution (for debugging purposes)
191194
**
192195
** TH1 Caveats: the built-in TH1 commands make some assumptions about
193196
** HTML escaping and output which do not apply via this
@@ -200,13 +203,14 @@
200203
Blob bOut = empty_blob;
201204
const char * zInfile = "-";
202205
const char * zOutfile = "-";
203206
const int fWithDiv = find_option("div",0,0)!=0;
204207
const int fTh1 = find_option("th",0,0)!=0;
205
- const int fNoVar = find_option("th-novar",0,0)!=0;
208
+ const int fNoPic = find_option("th-nopic",0,0)!=0;
206209
int isErr = 0;
207
- u32 fThInit = TH_INIT_DEFAULT;
210
+ u32 fThFlags = TH_INIT_DEFAULT | TH_INIT_NO_ESC
211
+ | (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);
208212
209213
Th_InitTraceLog()/*processes -th-trace flag*/;
210214
verify_all_options();
211215
if(g.argc>4){
212216
usage("?INFILE? ?OUTFILE?");
@@ -218,44 +222,53 @@
218222
zOutfile = g.argv[3];
219223
}
220224
blob_read_from_file(&bIn, zInfile, ExtFILE);
221225
if(fTh1){
222226
Blob out = empty_blob;
223
- Blob * oldOut;
224227
db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
225
- /* ^^^ needed for certain functions to work */;
226
- oldOut = Th_SetOutputBlob(&out);
227
- pikchr_th_init(fThInit);
228
- isErr = Th_RenderToBlob(blob_str(&bIn), &out,
229
- fNoVar ? TH_R2B_NO_VARS : 0);
230
- blob_reset(&bIn);
231
- bIn = out;
232
- Th_SetOutputBlob(oldOut);
233
- /*fossil_print("th'd:\n%b\n", &bIn);*/
228
+ /* ^^^ needed for certain TH1 functions to work */;
229
+ pikchr_th_init(fThFlags);
230
+ isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags) ? 1 : 0;
231
+ if(isErr){
232
+ blob_reset(&bOut);
233
+ bOut = out;
234
+ }else{
235
+ blob_reset(&bIn);
236
+ bIn = out;
237
+ }
234238
}
235239
if(!isErr){
236
- int w = 0, h = 0;
237
- const char * zContent = blob_str(&bIn);
238
- char *zOut = pikchr(zContent, "pikchr", 0, &w, &h);
239
- if( w>0 && h>0 ){
240
- if(fWithDiv){
241
- blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
242
- }
243
- blob_append(&bOut, zOut, -1);
244
- if(fWithDiv){
245
- blob_append(&bOut,"</div>\n", 7);
246
- }
240
+ if(fTh1 && fNoPic){
241
+ bOut = bIn;
242
+ bIn = empty_blob;
247243
}else{
248
- isErr = 1;
249
- blob_append(&bOut, zOut, -1);
244
+ int w = 0, h = 0;
245
+ const char * zContent = blob_str(&bIn);
246
+ char *zOut;
247
+
248
+ zOut = pikchr(zContent, "pikchr", 0, &w, &h);
249
+ if( w>0 && h>0 ){
250
+ if(fWithDiv){
251
+ blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
252
+ }
253
+ blob_append(&bOut, zOut, -1);
254
+ if(fWithDiv){
255
+ blob_append(&bOut,"</div>\n", 7);
256
+ }
257
+ }else{
258
+ isErr = 2;
259
+ blob_append(&bOut, zOut, -1);
260
+ }
261
+ fossil_free(zOut);
250262
}
251
- fossil_free(zOut);
252263
}
253264
if(isErr){
254
- fossil_fatal("ERROR: %b", &bOut);
265
+ /*fossil_print("ERROR: raw input:\n%b\n", &bIn);*/
266
+ fossil_fatal("%s ERROR: %b", 1==isErr ? "TH1" : "pikchr",
267
+ &bOut);
255268
}else{
256269
blob_write_to_file(&bOut, zOutfile);
257270
}
258271
Th_PrintTraceLog();
259272
blob_reset(&bIn);
260273
blob_reset(&bOut);
261274
}
262275
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -167,11 +167,11 @@
167 builtin_fulfill_js_requests();
168 style_footer();
169 }
170
171 static void pikchr_th_init(u32 fThInit){
172 Th_FossilInit(fThInit);
173 }
174
175 /*
176 ** COMMAND: pikchr
177 **
@@ -184,10 +184,13 @@
184 **
185 ** -th Process the input using TH1 before passing it to pikchr.
186 **
187 ** -th-novar Disable $var and $<var> TH1 processing. Only applies
188 ** with the -th flag.
 
 
 
189 **
190 ** -th-trace Trace TH1 execution (for debugging purposes)
191 **
192 ** TH1 Caveats: the built-in TH1 commands make some assumptions about
193 ** HTML escaping and output which do not apply via this
@@ -200,13 +203,14 @@
200 Blob bOut = empty_blob;
201 const char * zInfile = "-";
202 const char * zOutfile = "-";
203 const int fWithDiv = find_option("div",0,0)!=0;
204 const int fTh1 = find_option("th",0,0)!=0;
205 const int fNoVar = find_option("th-novar",0,0)!=0;
206 int isErr = 0;
207 u32 fThInit = TH_INIT_DEFAULT;
 
208
209 Th_InitTraceLog()/*processes -th-trace flag*/;
210 verify_all_options();
211 if(g.argc>4){
212 usage("?INFILE? ?OUTFILE?");
@@ -218,44 +222,53 @@
218 zOutfile = g.argv[3];
219 }
220 blob_read_from_file(&bIn, zInfile, ExtFILE);
221 if(fTh1){
222 Blob out = empty_blob;
223 Blob * oldOut;
224 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
225 /* ^^^ needed for certain functions to work */;
226 oldOut = Th_SetOutputBlob(&out);
227 pikchr_th_init(fThInit);
228 isErr = Th_RenderToBlob(blob_str(&bIn), &out,
229 fNoVar ? TH_R2B_NO_VARS : 0);
230 blob_reset(&bIn);
231 bIn = out;
232 Th_SetOutputBlob(oldOut);
233 /*fossil_print("th'd:\n%b\n", &bIn);*/
 
234 }
235 if(!isErr){
236 int w = 0, h = 0;
237 const char * zContent = blob_str(&bIn);
238 char *zOut = pikchr(zContent, "pikchr", 0, &w, &h);
239 if( w>0 && h>0 ){
240 if(fWithDiv){
241 blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
242 }
243 blob_append(&bOut, zOut, -1);
244 if(fWithDiv){
245 blob_append(&bOut,"</div>\n", 7);
246 }
247 }else{
248 isErr = 1;
249 blob_append(&bOut, zOut, -1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250 }
251 fossil_free(zOut);
252 }
253 if(isErr){
254 fossil_fatal("ERROR: %b", &bOut);
 
 
255 }else{
256 blob_write_to_file(&bOut, zOutfile);
257 }
258 Th_PrintTraceLog();
259 blob_reset(&bIn);
260 blob_reset(&bOut);
261 }
262
--- src/pikchrshow.c
+++ src/pikchrshow.c
@@ -167,11 +167,11 @@
167 builtin_fulfill_js_requests();
168 style_footer();
169 }
170
171 static void pikchr_th_init(u32 fThInit){
172 Th_FossilInit(fThInit & TH_INIT_MASK);
173 }
174
175 /*
176 ** COMMAND: pikchr
177 **
@@ -184,10 +184,13 @@
184 **
185 ** -th Process the input using TH1 before passing it to pikchr.
186 **
187 ** -th-novar Disable $var and $<var> TH1 processing. Only applies
188 ** with the -th flag.
189 **
190 ** -th-nopic When using -th, output the post-TH1'd script
191 ** instead of the pikchr-rendered output.
192 **
193 ** -th-trace Trace TH1 execution (for debugging purposes)
194 **
195 ** TH1 Caveats: the built-in TH1 commands make some assumptions about
196 ** HTML escaping and output which do not apply via this
@@ -200,13 +203,14 @@
203 Blob bOut = empty_blob;
204 const char * zInfile = "-";
205 const char * zOutfile = "-";
206 const int fWithDiv = find_option("div",0,0)!=0;
207 const int fTh1 = find_option("th",0,0)!=0;
208 const int fNoPic = find_option("th-nopic",0,0)!=0;
209 int isErr = 0;
210 u32 fThFlags = TH_INIT_DEFAULT | TH_INIT_NO_ESC
211 | (find_option("th-novar",0,0)!=0 ? TH_R2B_NO_VARS : 0);
212
213 Th_InitTraceLog()/*processes -th-trace flag*/;
214 verify_all_options();
215 if(g.argc>4){
216 usage("?INFILE? ?OUTFILE?");
@@ -218,44 +222,53 @@
222 zOutfile = g.argv[3];
223 }
224 blob_read_from_file(&bIn, zInfile, ExtFILE);
225 if(fTh1){
226 Blob out = empty_blob;
 
227 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0)
228 /* ^^^ needed for certain TH1 functions to work */;
229 pikchr_th_init(fThFlags);
230 isErr = Th_RenderToBlob(blob_str(&bIn), &out, fThFlags) ? 1 : 0;
231 if(isErr){
232 blob_reset(&bOut);
233 bOut = out;
234 }else{
235 blob_reset(&bIn);
236 bIn = out;
237 }
238 }
239 if(!isErr){
240 if(fTh1 && fNoPic){
241 bOut = bIn;
242 bIn = empty_blob;
 
 
 
 
 
 
 
 
243 }else{
244 int w = 0, h = 0;
245 const char * zContent = blob_str(&bIn);
246 char *zOut;
247
248 zOut = pikchr(zContent, "pikchr", 0, &w, &h);
249 if( w>0 && h>0 ){
250 if(fWithDiv){
251 blob_appendf(&bOut,"<div style='max-width:%dpx;'>\n", w);
252 }
253 blob_append(&bOut, zOut, -1);
254 if(fWithDiv){
255 blob_append(&bOut,"</div>\n", 7);
256 }
257 }else{
258 isErr = 2;
259 blob_append(&bOut, zOut, -1);
260 }
261 fossil_free(zOut);
262 }
 
263 }
264 if(isErr){
265 /*fossil_print("ERROR: raw input:\n%b\n", &bIn);*/
266 fossil_fatal("%s ERROR: %b", 1==isErr ? "TH1" : "pikchr",
267 &bOut);
268 }else{
269 blob_write_to_file(&bOut, zOutfile);
270 }
271 Th_PrintTraceLog();
272 blob_reset(&bIn);
273 blob_reset(&bOut);
274 }
275
+8 -6
--- src/th_main.c
+++ src/th_main.c
@@ -31,11 +31,12 @@
3131
#define TH_INIT_NEED_CONFIG ((u32)0x00000001) /* Open configuration first? */
3232
#define TH_INIT_FORCE_TCL ((u32)0x00000002) /* Force Tcl to be enabled? */
3333
#define TH_INIT_FORCE_RESET ((u32)0x00000004) /* Force TH1 commands re-added? */
3434
#define TH_INIT_FORCE_SETUP ((u32)0x00000008) /* Force eval of setup script? */
3535
#define TH_INIT_NO_REPO ((u32)0x00000010) /* Skip opening repository. */
36
-#define TH_INIT_MASK ((u32)0x0000001F) /* All possible init flags. */
36
+#define TH_INIT_NO_ESC ((u32)0x00000020) /* Do not html-escape certain output. */
37
+#define TH_INIT_MASK ((u32)0x0000003F) /* All possible init flags. */
3738
3839
/*
3940
** Useful and/or "well-known" combinations of flag values.
4041
*/
4142
#define TH_INIT_DEFAULT (TH_INIT_NONE) /* Default flags. */
@@ -471,14 +472,16 @@
471472
void *pConvert,
472473
int argc,
473474
const char **argv,
474475
int *argl
475476
){
477
+ unsigned int doEscape = (TH_INIT_NO_ESC & g.th1Flags)
478
+ ? 0U : *(unsigned int*)pConvert;
476479
if( argc!=2 ){
477480
return Th_WrongNumArgs(interp, "puts STRING");
478481
}
479
- sendText(0,(char*)argv[1], argl[1], *(unsigned int*)pConvert);
482
+ sendText(0,(char*)argv[1], argl[1], doEscape);
480483
return TH_OK;
481484
}
482485
483486
/*
484487
** TH1 command: redirect URL ?withMethod?
@@ -2138,11 +2141,11 @@
21382141
int needConfig = flags & TH_INIT_NEED_CONFIG;
21392142
int forceReset = flags & TH_INIT_FORCE_RESET;
21402143
int forceTcl = flags & TH_INIT_FORCE_TCL;
21412144
int forceSetup = flags & TH_INIT_FORCE_SETUP;
21422145
int noRepo = flags & TH_INIT_NO_REPO;
2143
- static unsigned int aFlags[] = { 0, 1, WIKI_LINKSONLY };
2146
+ static unsigned int aFlags[] = {0, 1, WIKI_LINKSONLY };
21442147
static int anonFlag = LOGIN_ANON;
21452148
static int zeroInt = 0;
21462149
static struct _Command {
21472150
const char *zName;
21482151
Th_CommandProc xProc;
@@ -2654,14 +2657,13 @@
26542657
int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
26552658
int i = 0;
26562659
int n;
26572660
int rc = TH_OK;
26582661
char *zResult;
2659
- Blob * const origOut = pThOut;
2662
+ Blob * const origOut = Th_SetOutputBlob(pOut);
26602663
26612664
assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
2662
- pThOut = pOut;
26632665
Th_FossilInit(mFlags & TH_INIT_MASK);
26642666
while( z[i] ){
26652667
if( !(TH_R2B_NO_VARS & mFlags)
26662668
&& z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
26672669
const char *zVar;
@@ -2709,11 +2711,11 @@
27092711
zResult = (char*)Th_GetResult(g.interp, &n);
27102712
sendError(pOut,zResult, n, 1);
27112713
}else{
27122714
sendText(pOut,z, i, 0);
27132715
}
2714
- pThOut = origOut;
2716
+ Th_SetOutputBlob(origOut);
27152717
return rc;
27162718
}
27172719
27182720
/*
27192721
** The z[] input contains text mixed with TH1 scripts.
27202722
--- src/th_main.c
+++ src/th_main.c
@@ -31,11 +31,12 @@
31 #define TH_INIT_NEED_CONFIG ((u32)0x00000001) /* Open configuration first? */
32 #define TH_INIT_FORCE_TCL ((u32)0x00000002) /* Force Tcl to be enabled? */
33 #define TH_INIT_FORCE_RESET ((u32)0x00000004) /* Force TH1 commands re-added? */
34 #define TH_INIT_FORCE_SETUP ((u32)0x00000008) /* Force eval of setup script? */
35 #define TH_INIT_NO_REPO ((u32)0x00000010) /* Skip opening repository. */
36 #define TH_INIT_MASK ((u32)0x0000001F) /* All possible init flags. */
 
37
38 /*
39 ** Useful and/or "well-known" combinations of flag values.
40 */
41 #define TH_INIT_DEFAULT (TH_INIT_NONE) /* Default flags. */
@@ -471,14 +472,16 @@
471 void *pConvert,
472 int argc,
473 const char **argv,
474 int *argl
475 ){
 
 
476 if( argc!=2 ){
477 return Th_WrongNumArgs(interp, "puts STRING");
478 }
479 sendText(0,(char*)argv[1], argl[1], *(unsigned int*)pConvert);
480 return TH_OK;
481 }
482
483 /*
484 ** TH1 command: redirect URL ?withMethod?
@@ -2138,11 +2141,11 @@
2138 int needConfig = flags & TH_INIT_NEED_CONFIG;
2139 int forceReset = flags & TH_INIT_FORCE_RESET;
2140 int forceTcl = flags & TH_INIT_FORCE_TCL;
2141 int forceSetup = flags & TH_INIT_FORCE_SETUP;
2142 int noRepo = flags & TH_INIT_NO_REPO;
2143 static unsigned int aFlags[] = { 0, 1, WIKI_LINKSONLY };
2144 static int anonFlag = LOGIN_ANON;
2145 static int zeroInt = 0;
2146 static struct _Command {
2147 const char *zName;
2148 Th_CommandProc xProc;
@@ -2654,14 +2657,13 @@
2654 int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
2655 int i = 0;
2656 int n;
2657 int rc = TH_OK;
2658 char *zResult;
2659 Blob * const origOut = pThOut;
2660
2661 assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
2662 pThOut = pOut;
2663 Th_FossilInit(mFlags & TH_INIT_MASK);
2664 while( z[i] ){
2665 if( !(TH_R2B_NO_VARS & mFlags)
2666 && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
2667 const char *zVar;
@@ -2709,11 +2711,11 @@
2709 zResult = (char*)Th_GetResult(g.interp, &n);
2710 sendError(pOut,zResult, n, 1);
2711 }else{
2712 sendText(pOut,z, i, 0);
2713 }
2714 pThOut = origOut;
2715 return rc;
2716 }
2717
2718 /*
2719 ** The z[] input contains text mixed with TH1 scripts.
2720
--- src/th_main.c
+++ src/th_main.c
@@ -31,11 +31,12 @@
31 #define TH_INIT_NEED_CONFIG ((u32)0x00000001) /* Open configuration first? */
32 #define TH_INIT_FORCE_TCL ((u32)0x00000002) /* Force Tcl to be enabled? */
33 #define TH_INIT_FORCE_RESET ((u32)0x00000004) /* Force TH1 commands re-added? */
34 #define TH_INIT_FORCE_SETUP ((u32)0x00000008) /* Force eval of setup script? */
35 #define TH_INIT_NO_REPO ((u32)0x00000010) /* Skip opening repository. */
36 #define TH_INIT_NO_ESC ((u32)0x00000020) /* Do not html-escape certain output. */
37 #define TH_INIT_MASK ((u32)0x0000003F) /* All possible init flags. */
38
39 /*
40 ** Useful and/or "well-known" combinations of flag values.
41 */
42 #define TH_INIT_DEFAULT (TH_INIT_NONE) /* Default flags. */
@@ -471,14 +472,16 @@
472 void *pConvert,
473 int argc,
474 const char **argv,
475 int *argl
476 ){
477 unsigned int doEscape = (TH_INIT_NO_ESC & g.th1Flags)
478 ? 0U : *(unsigned int*)pConvert;
479 if( argc!=2 ){
480 return Th_WrongNumArgs(interp, "puts STRING");
481 }
482 sendText(0,(char*)argv[1], argl[1], doEscape);
483 return TH_OK;
484 }
485
486 /*
487 ** TH1 command: redirect URL ?withMethod?
@@ -2138,11 +2141,11 @@
2141 int needConfig = flags & TH_INIT_NEED_CONFIG;
2142 int forceReset = flags & TH_INIT_FORCE_RESET;
2143 int forceTcl = flags & TH_INIT_FORCE_TCL;
2144 int forceSetup = flags & TH_INIT_FORCE_SETUP;
2145 int noRepo = flags & TH_INIT_NO_REPO;
2146 static unsigned int aFlags[] = {0, 1, WIKI_LINKSONLY };
2147 static int anonFlag = LOGIN_ANON;
2148 static int zeroInt = 0;
2149 static struct _Command {
2150 const char *zName;
2151 Th_CommandProc xProc;
@@ -2654,14 +2657,13 @@
2657 int Th_RenderToBlob(const char *z, Blob * pOut, u32 mFlags){
2658 int i = 0;
2659 int n;
2660 int rc = TH_OK;
2661 char *zResult;
2662 Blob * const origOut = Th_SetOutputBlob(pOut);
2663
2664 assert(0==(TH_R2B_MASK & TH_INIT_MASK) && "init/r2b mask conflict");
 
2665 Th_FossilInit(mFlags & TH_INIT_MASK);
2666 while( z[i] ){
2667 if( !(TH_R2B_NO_VARS & mFlags)
2668 && z[i]=='$' && (n = validVarName(&z[i+1]))>0 ){
2669 const char *zVar;
@@ -2709,11 +2711,11 @@
2711 zResult = (char*)Th_GetResult(g.interp, &n);
2712 sendError(pOut,zResult, n, 1);
2713 }else{
2714 sendText(pOut,z, i, 0);
2715 }
2716 Th_SetOutputBlob(origOut);
2717 return rc;
2718 }
2719
2720 /*
2721 ** The z[] input contains text mixed with TH1 scripts.
2722

Keyboard Shortcuts

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