diff options
Diffstat (limited to 'src/sh/convert.cc')
| -rw-r--r-- | src/sh/convert.cc | 376 |
1 files changed, 376 insertions, 0 deletions
diff --git a/src/sh/convert.cc b/src/sh/convert.cc new file mode 100644 index 0000000..8828c34 --- /dev/null +++ b/src/sh/convert.cc | |||
| @@ -0,0 +1,376 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <sh/htc.h> | ||
| 21 | #include <hdb/buffer.h> | ||
| 22 | #include <http/http.h> | ||
| 23 | |||
| 24 | static const charset htcmask("{}\\%&*$;"); | ||
| 25 | static const charset htmlmask("<&>\""); | ||
| 26 | |||
| 27 | static void Mask(mstream& out,const charset& mask,const sref& msg) | ||
| 28 | { | ||
| 29 | sref q,p=msg; | ||
| 30 | while(p.nempty()) { | ||
| 31 | p=p.adv(q=p.left_first_of(mask)); | ||
| 32 | out<<q; | ||
| 33 | while(mask.test(p.front())) { | ||
| 34 | unsigned code=(unsigned char)*p; p=p.popf(); | ||
| 35 | out<<"&#"<<code<<";"; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | TOK_IMPL(mask_htc) { | ||
| 41 | Mask(out,htcmask,env[0]); | ||
| 42 | } | ||
| 43 | |||
| 44 | TOK_IMPL(mask_html) { | ||
| 45 | Mask(out,htmlmask,env[0]); | ||
| 46 | } | ||
| 47 | |||
| 48 | TOK_IMPL(cdata) { | ||
| 49 | out.put('\"'); Mask(out,htmlmask,env[0]); out.put('\"'); | ||
| 50 | } | ||
| 51 | |||
| 52 | ////////////////////////////////////////////////////////// | ||
| 53 | |||
| 54 | TOK_IMPL(wrap) { | ||
| 55 | int num=atoi(env[0]); | ||
| 56 | sref body=env[1]; | ||
| 57 | int col=0; | ||
| 58 | for(int i=0;i<body.size();i++) { | ||
| 59 | if(cset_newl.test(body[i])) col=0; else col++; | ||
| 60 | if(cset_spaces.test(body[i])&&col>num) { out.put('\n'); col=0; } | ||
| 61 | else out.put(body[i]); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | TOK_IMPL(htmlize) { | ||
| 66 | sref first,rest=env[0]; | ||
| 67 | while(Splitln(first,rest)) { | ||
| 68 | Mask(out,htmlmask,first); | ||
| 69 | out<<"<br>\n"; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | TOK_IMPL(lf2crlf) { | ||
| 74 | LF2CRLF(out,env[0]); | ||
| 75 | } | ||
| 76 | TOK_IMPL(crlf2lf) { | ||
| 77 | CRLF2LF(out,env[0]); | ||
| 78 | } | ||
| 79 | |||
| 80 | ////////////////////////////////////////////////////////// | ||
| 81 | |||
| 82 | TOK_IMPL(twocolumn) { | ||
| 83 | int num=atoi(env[0]); | ||
| 84 | int page=atoi(env[1]); | ||
| 85 | sref head=env[2],mid=env[3],foot=env[4],body=env[5]; | ||
| 86 | int row=0,col=0,pg=1; | ||
| 87 | out<<head; | ||
| 88 | for(int i=0;i<body.size()&&pg<=page;i++) { | ||
| 89 | if(pg==page) out.put(body[i]); | ||
| 90 | if(body[i]=='\n') { | ||
| 91 | row++; | ||
| 92 | if(row>=num) { | ||
| 93 | row=0; | ||
| 94 | col++; | ||
| 95 | if(col>=2) { col=0; pg++; } | ||
| 96 | else if(pg==page) { out<<mid; } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | out<<foot; | ||
| 101 | } | ||
| 102 | |||
| 103 | ////////////////////////////////////////////////////////// | ||
| 104 | |||
| 105 | TOK_IMPL(encode) { | ||
| 106 | Pack(out,env[0]); | ||
| 107 | } | ||
| 108 | |||
| 109 | TOK_IMPL(decode) { | ||
| 110 | Unpack(out,env[0]); | ||
| 111 | } | ||
| 112 | |||
| 113 | TOK_IMPL(encode_header) { | ||
| 114 | encodeHeader(out,mime_default,env[0]); | ||
| 115 | } | ||
| 116 | |||
| 117 | TOK_IMPL(encode_base64) { | ||
| 118 | mstring buffer,path=DocPath(env,env[0]); | ||
| 119 | LoadBuffer(buffer,path); | ||
| 120 | encode64(out,buffer); | ||
| 121 | } | ||
| 122 | |||
| 123 | TOK_IMPL(pack) { | ||
| 124 | rrpile pile; | ||
| 125 | spile var(env.args); | ||
| 126 | for(int i=0;i<env.args;i++) { | ||
| 127 | env.parg(var[i],i); | ||
| 128 | pile.push_back(rrstring(var[i],env.glob(var[i]))); | ||
| 129 | } | ||
| 130 | Pack(out,pile); | ||
| 131 | } | ||
| 132 | |||
| 133 | ////////////////////////////////////////////////////////// | ||
| 134 | |||
| 135 | TOK_IMPL(decode_base64) { | ||
| 136 | mstring buffer,name=env.parg(-1),uri=env.URI(env[0]),path=DocPath(env,env[0]); | ||
| 137 | LoadBuffer(buffer,path); | ||
| 138 | if(name.nempty()) { | ||
| 139 | swrite sw(Reference(*env.toks,name,uri,1)->data); | ||
| 140 | decode64(sw,buffer); | ||
| 141 | } | ||
| 142 | else decode64(out,buffer); | ||
| 143 | } | ||
| 144 | |||
| 145 | ////////////////////////////////////////////////////////// | ||
| 146 | |||
| 147 | TOK_IMPL(strip) { | ||
| 148 | sref s=env[0],a=env[1]; | ||
| 149 | for(int i=0;i<s.size();i++) if(a.find(s[i])<0) out.put(s[i]); | ||
| 150 | } | ||
| 151 | |||
| 152 | TOK_IMPL(replace) { | ||
| 153 | mstring s=env[0]; sref a=env[1],b=env[2]; | ||
| 154 | if(a!=b) { int p; while((p=s.find(a))>=0) s.replace(p,a.size(),b); } | ||
| 155 | out<<s; | ||
| 156 | } | ||
| 157 | |||
| 158 | ////////////////////////////////////////////////////////// | ||
| 159 | |||
| 160 | TOK_IMPL(left) { | ||
| 161 | int pad=atoi(env[1])-env[0].size(); | ||
| 162 | out<<env[0]; while(pad-->0) out.put(' '); | ||
| 163 | } | ||
| 164 | TOK_IMPL(right) { | ||
| 165 | int pad=atoi(env[1])-env[0].size(); | ||
| 166 | while(pad-->0) out.put(' '); out<<env[0]; | ||
| 167 | } | ||
| 168 | |||
| 169 | TOK_IMPL(sub) {out<<env[0](atoi(env[1]),atoi(env[2]));} | ||
| 170 | |||
| 171 | ////////////////////////////////////////////////////////// | ||
| 172 | |||
| 173 | TOK_IMPL(file2mime) { | ||
| 174 | const sref* p=file2mime(env[0]); | ||
| 175 | if(p) out<<*p; else out<<http_mime_default; | ||
| 176 | } | ||
| 177 | |||
| 178 | ////////////////////////////////////////////////////////// | ||
| 179 | |||
| 180 | TOK_IMPL(minilang) { | ||
| 181 | static const mstring lang_tag("\\lang"); | ||
| 182 | sref p=env[0]; | ||
| 183 | while(p.nempty()) { | ||
| 184 | sref e=p.left_first(lang_tag); | ||
| 185 | out<<e; | ||
| 186 | p=p.adv(e); | ||
| 187 | if(p.nempty()) { | ||
| 188 | p=p.adv(lang_tag.size()); | ||
| 189 | e=p.right_first_not_of(cset_ws); | ||
| 190 | int n=0,s=e.skip(cset_braces); | ||
| 191 | while(s>0) { | ||
| 192 | p=e.adv(s); | ||
| 193 | if(n==env.req->lang) out<<e(1,s-2); | ||
| 194 | e=p.right_first_not_of(cset_ws); | ||
| 195 | s=e.skip(cset_braces); | ||
| 196 | n++; | ||
| 197 | } | ||
| 198 | if(n==0) out<<lang_tag; | ||
| 199 | if(*p==';') p=p.adv(1); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | ////////////////////////////////////////////////////////// | ||
| 205 | |||
| 206 | void envAddConvert(tokmap& T) | ||
| 207 | { | ||
| 208 | TOK_ADD(mask_htc,"x",tok_t::SYSONLY, | ||
| 209 | "%Mask HTC language\n" | ||
| 210 | "%Syntax: \\mask.htc{text}\n" | ||
| 211 | "%\n" | ||
| 212 | "%Converts 'hot' characters to HTML CDATA encoding\n" | ||
| 213 | "%CDATA encoding: &#hex;\n" | ||
| 214 | ); | ||
| 215 | TOK_ADD(mask_html,"x",tok_t::PUBLIC, | ||
| 216 | "%Mask HTML language\n" | ||
| 217 | "%Syntax: \\mask.html{text}\n" | ||
| 218 | "%\n" | ||
| 219 | "%Converts 'hot' characters to HTML CDATA encoding\n" | ||
| 220 | "%CDATA encoding: &#hex;\n" | ||
| 221 | ); | ||
| 222 | TOK_ADD(cdata,"x",tok_t::PUBLIC, | ||
| 223 | "%Convert to CDATA\n" | ||
| 224 | "%Syntax: \\cdata{text}\n" | ||
| 225 | "%\n" | ||
| 226 | "%Converts 'hot' characters to HTML CDATA encoding\n" | ||
| 227 | "%and encloses in \" characters\n" | ||
| 228 | "%\n" | ||
| 229 | "%HTML parsors usually does not nest the \" character,\n" | ||
| 230 | "%so this function is used extensively in arguments to\n" | ||
| 231 | "%HTML commands.\n" | ||
| 232 | ); | ||
| 233 | |||
| 234 | /////////////////////////////////////////////// | ||
| 235 | |||
| 236 | TOK_ADD(wrap,"xx",tok_t::PUBLIC, | ||
| 237 | "%Wrap long lines\n" | ||
| 238 | "%Syntax: \\wrap{at column}{text}\n" | ||
| 239 | "%\n" | ||
| 240 | "%Wraps words that extend the given column by inserting\n" | ||
| 241 | "%newline characters.\n" | ||
| 242 | ); | ||
| 243 | TOK_ADD(htmlize,"x",tok_t::PUBLIC, | ||
| 244 | "%HTMLize plain text\n" | ||
| 245 | "%Syntax: \\htmlize{text}\n" | ||
| 246 | "%\n" | ||
| 247 | "%Inserts a HTML break at each newline.\n" | ||
| 248 | ); | ||
| 249 | TOK_ADD(lf2crlf,"x",tok_t::PUBLIC, | ||
| 250 | "%LF to CRLF\n" | ||
| 251 | "%Syntax: \\lf2crlf{text}\n" | ||
| 252 | "%\n" | ||
| 253 | "%Inserts a carriage return (CR) at each newline.\n" | ||
| 254 | ); | ||
| 255 | TOK_ADD(crlf2lf,"x",tok_t::PUBLIC, | ||
| 256 | "%CRLF to LF\n" | ||
| 257 | "%Syntax: \\crlf2lf{text}\n" | ||
| 258 | "%\n" | ||
| 259 | "%Removes all carriage return (CR) characters.\n" | ||
| 260 | ); | ||
| 261 | |||
| 262 | /////////////////////////////////////////////// | ||
| 263 | |||
| 264 | TOK_ADD(twocolumn,"xxxxxx",tok_t::PUBLIC, | ||
| 265 | "%Output column text\n" | ||
| 266 | "%Syntax: \\twocolumn{rows}{page}{head}{mid}{foot}{body}\n" | ||
| 267 | ); | ||
| 268 | |||
| 269 | /////////////////////////////////////////////// | ||
| 270 | |||
| 271 | TOK_ADD(encode,"x",tok_t::PUBLIC, | ||
| 272 | "%Encode text\n" | ||
| 273 | "%Syntax: \\encode{text}\n" | ||
| 274 | "%\n" | ||
| 275 | "%Encodes text in x-www-form-urlencoded format (RFC HTTP)\n" | ||
| 276 | ); | ||
| 277 | TOK_ADD(decode,"x",tok_t::PUBLIC, | ||
| 278 | "%Decode text\n" | ||
| 279 | "%Syntax: \\decode{text}\n" | ||
| 280 | "%\n" | ||
| 281 | "%Decodes text given in x-www-form-urlencoded format (RFC HTTP)\n" | ||
| 282 | ); | ||
| 283 | |||
| 284 | TOK_ADD(encode_header,"x",tok_t::PUBLIC, | ||
| 285 | "%Encode header\n" | ||
| 286 | "%Syntax: \\encode.header{text}\n" | ||
| 287 | "%\n" | ||
| 288 | "%Encodes text in Quoted-printable header format (RFC MIME)\n" | ||
| 289 | ); | ||
| 290 | TOK_ADD(encode_base64,"x",tok_t::PUBLIC, | ||
| 291 | "%Encode base64\n" | ||
| 292 | "%Syntax: \\encode.base64{uri}\n" | ||
| 293 | "%\n" | ||
| 294 | "%Encodes the uri, possible containing binary data,\n" | ||
| 295 | "%in BASE64 format (RFC MIME)\n" | ||
| 296 | ); | ||
| 297 | TOK_ADD(decode_base64,"?x",tok_t::PUBLIC, | ||
| 298 | "%Decode base64\n" | ||
| 299 | "%Syntax: \\decode.base64{var}{uri}\n" | ||
| 300 | "%\n" | ||
| 301 | "%Encodes the uri, presumed to be in BASE64 format,\n" | ||
| 302 | "%and outputs or saves in var. (RFC MIME)\n" | ||
| 303 | ); | ||
| 304 | TOK_ADD(pack,"*",tok_t::PUBLIC, | ||
| 305 | "%Encode text\n" | ||
| 306 | "%Syntax: \\pack<var><var>..\n" | ||
| 307 | "%\n" | ||
| 308 | "%Encodes a sequence of variables in\n" | ||
| 309 | "%x-www-form-urlencoded format (RFC HTTP)\n" | ||
| 310 | ); | ||
| 311 | |||
| 312 | /////////////////////////////////////////////// | ||
| 313 | |||
| 314 | TOK_ADD(strip,"xx",tok_t::PUBLIC, | ||
| 315 | "%Strip text from special characters\n" | ||
| 316 | "%Syntax: \\stip{text}{chars}\n" | ||
| 317 | ); | ||
| 318 | TOK_ADD(replace,"xxx",tok_t::PUBLIC, | ||
| 319 | "%Replace text\n" | ||
| 320 | "%Syntax: \\replace{text}{string}{subst}\n" | ||
| 321 | "%\n" | ||
| 322 | "%Replaces each occurence of string in text with subst.\n" | ||
| 323 | ); | ||
| 324 | |||
| 325 | /////////////////////////////////////////////// | ||
| 326 | |||
| 327 | TOK_ADD(left,"xx",tok_t::PUBLIC, | ||
| 328 | "%Left-adjust text\n" | ||
| 329 | "%Syntax: \\left{text}{n}\n" | ||
| 330 | "%\n" | ||
| 331 | "%Outputs n characters, padding with spaces to the right.\n" | ||
| 332 | "%\n" | ||
| 333 | "%Writes out the full text if longer than n.\n" | ||
| 334 | ); | ||
| 335 | TOK_ADD(right,"xx",tok_t::PUBLIC, | ||
| 336 | "%Right-adjust text\n" | ||
| 337 | "%Syntax: \\right{text}{n}\n" | ||
| 338 | "%\n" | ||
| 339 | "%Outputs n characters, padding with spaces to the left.\n" | ||
| 340 | "%\n" | ||
| 341 | "%Writes out the full text if longer than n.\n" | ||
| 342 | ); | ||
| 343 | |||
| 344 | /////////////////////////////////////////////// | ||
| 345 | |||
| 346 | TOK_ADD(sub,"xxx",tok_t::PUBLIC, | ||
| 347 | "%Substring\n" | ||
| 348 | "%Syntax: \\sub{string}{pos}{n}\n" | ||
| 349 | "%\n" | ||
| 350 | "%Gets a substring of n characters, beginning at pos, from string.\n" | ||
| 351 | "%\n" | ||
| 352 | "%Negative pos values wraps to end.\n" | ||
| 353 | "%Negative n values wraps to 1+size.\n" | ||
| 354 | ); | ||
| 355 | |||
| 356 | /////////////////////////////////////////////// | ||
| 357 | |||
| 358 | TOK_ADD(file2mime,"x",tok_t::PUBLIC, | ||
| 359 | "%Get MIME type\n" | ||
| 360 | "%Syntax: \\file2mime{path}\n" | ||
| 361 | "%\n" | ||
| 362 | "%Looks up the MIME type mapped to the extension of path.\n" | ||
| 363 | "%\n" | ||
| 364 | "%Defaults to application/octet-stream if not found.\n" | ||
| 365 | ); | ||
| 366 | |||
| 367 | /////////////////////////////////////////////// | ||
| 368 | |||
| 369 | TOK_ADD(minilang,"x",tok_t::PUBLIC, | ||
| 370 | "%Parse lang only\n" | ||
| 371 | "%Syntax: \\minilang{body}\n" | ||
| 372 | "%\n" | ||
| 373 | "%This function is used to enable lang in html files,\n" | ||
| 374 | "%which otherwise are not parsed.\n" | ||
| 375 | ); | ||
| 376 | } | ||
