1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
/*
* bif_tidy.c
*
* $Id$
*
* Build in Functions for tidying HTML pages
*
* This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
* project.
*
* Copyright (C) 1998-2012 OpenLink Software
*
* This project is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; only version 2 of the License, dated June 1991.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define OLD_TIDY
#include "libutil.h"
#include "sqlnode.h"
#include "sqlbif.h"
#include "wi.h"
#include "Dk.h"
#ifndef WIN32
#define __USE_MISC 1 /* hack for platform.h defining ulong & uint */
#endif
#ifdef OLD_TIDY
#include "html.h"
#else
#include <tidy/tidy.h>
#include <tidy/buffio.h>
#endif
#ifndef WIN32
#undef __USE_MISC
#endif
static dk_mutex_t *tidy_mtx;
#ifndef OLD_TIDY
static void * TIDY_CALL
tidy_malloc (size_t len)
{
if (len >= MAX_BOX_LENGTH)
return NULL;
return t_alloc_box (len, DV_CUSTOM);
}
static void * TIDY_CALL
tidy_realloc (void * buf, size_t len)
{
int buf_size = IS_BOX_POINTER (buf) ? box_length (buf) : 0;
int copy_size = buf_size > len ? len : buf_size;
void *new;
if (len >= MAX_BOX_LENGTH)
return NULL;
new = t_alloc_box (len, DV_CUSTOM);
if (buf && copy_size)
memcpy (new, buf, copy_size);
return new;
}
static void TIDY_CALL
tidy_free (void * buf)
{
/* void, will release on MP_DONE */
}
static void TIDY_CALL
tidy_panic (const char * err)
{
/* log_error ("Tidy panic: %s", err); */
sqlr_new_error ("42000", "TIDYE", "Tidy panic: %s", err);
}
#define READING_NAME 1
#define READING_VALUE 2
static int
tidy_parse_config (TidyDoc doc, caddr_t config_str)
{
dk_session_t * ses;
volatile int rc = -1, i = 0;
char name[64] = {0}, value[8192] = {0}, stat = READING_NAME;
ses = strses_allocate ();
ses->dks_in_buffer = config_str;
ses->dks_in_fill = box_length (config_str) - 1;
CATCH_READ_FAIL (ses)
{
char c;
for (;;)
{
c = session_buffered_read_char (ses);
if (READING_VALUE == stat && (c == '\r' || c == '\n'))
{
value[i] = 0;
rc = tidyOptParseValue (doc, name, value);
i = 0;
stat = READING_NAME;
continue;
}
if (isspace (c))
continue;
if (READING_NAME == stat && c == ':') /* delimiter */
{
name[i] = 0;
i = 0;
stat = READING_VALUE;
continue;
}
if (READING_NAME == stat)
name[i++] = c;
if (READING_VALUE == stat)
value[i++] = c;
/* check for overflow */
if (READING_NAME == stat && i >= sizeof (name))
break;
if (READING_VALUE == stat && i >= sizeof (value))
break;
}
}
FAILED
{
if (READING_VALUE == stat)
{
value[i] = 0;
rc = tidyOptParseValue (doc, name, value);
}
}
END_READ_FAIL (ses);
ses->dks_in_buffer = NULL;
dk_free_box (ses);
return 0;
}
#endif
caddr_t
bif_tidy_html (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t html_input = bif_string_arg (qst, args, 0, "tidy_html");
caddr_t config_input = bif_string_arg (qst, args, 1, "tidy_html");
caddr_t html_output = NULL;
int res;
#ifdef OLD_TIDY
tidy_io_t tidy_errout;
tidy_errout.tio_data.lm_memblock = NULL;
tidy_errout.tio_data.lm_length = 0;
tidy_errout.tio_pos = 0;
mutex_enter (tidy_mtx);
errout = &tidy_errout;
res = do_tidy (html_input, config_input, &html_output);
errout = NULL;
mutex_leave (tidy_mtx);
if (NULL != tidy_errout.tio_data.lm_memblock)
dk_free (tidy_errout.tio_data.lm_memblock, -1);
if ((NULL == html_output) || (2 == res)) /* errors */
{
dk_free_box (html_output);
sqlr_new_error ("42000", "HT076", "HTML Tidy failed, try tidy_list_errors(...) to get more information");
}
#else
TidyBuffer output;
TidyBuffer errbuf;
TidyDoc doc;
MP_START ();
QR_RESET_CTX
{
doc = tidyCreate ();
tidyBufInit (&output);
tidyBufInit (&errbuf);
tidySetErrorBuffer (doc, &errbuf);
/* cannot load cfg file here, must parse the config string */
tidy_parse_config (doc, config_input);
res = tidyParseString (doc, html_input);
if (res >= 0)
res = tidyCleanAndRepair (doc);
if (res >= 0)
res = tidyRunDiagnostics (doc);
if (res > 1)
res = (tidyOptSetBool (doc, TidyForceOutput, yes) ? res : -1);
if (res >= 0)
res = tidySaveBuffer (doc, &output);
if (res >= 0)
html_output = box_dv_short_string ((char *) output.bp);
}
QR_RESET_CODE
{
caddr_t err;
POP_QR_RESET;
err = thr_get_error_code (THREAD_CURRENT_THREAD);
MP_DONE ();
sqlr_resignal (err);
}
END_QR_RESET;
MP_DONE ();
/*
tidyBufFree( &output );
tidyBufFree( &errbuf );
tidyRelease (doc);
*/
if (res < 0)
{
dk_free_box (html_output);
sqlr_new_error ("42000", "HT076", "HTML Tidy failed, try tidy_list_errors(...) to get more information");
}
#endif
return html_output;
}
caddr_t
bif_tidy_list_errors (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
caddr_t html_input = bif_string_arg (qst, args, 0, "tidy_list_errors");
caddr_t config_input = bif_string_arg (qst, args, 1, "tidy_list_errors");
caddr_t errlist = NULL;
#ifdef OLD_TIDY
tidy_io_t tidy_errout;
tidy_errout.tio_data.lm_memblock = NULL;
tidy_errout.tio_data.lm_length = 0;
tidy_errout.tio_pos = 0;
mutex_enter (tidy_mtx);
errout = &tidy_errout;
do_tidy (html_input, config_input, null);
errout = NULL;
mutex_leave (tidy_mtx);
if (NULL == tidy_errout.tio_data.lm_memblock)
errlist = box_dv_short_string ("");
else
{
errlist = box_dv_short_nchars (tidy_errout.tio_data.lm_memblock, tidy_errout.tio_pos);
dk_free (tidy_errout.tio_data.lm_memblock, -1);
}
#else
int res = -1;
TidyBuffer errbuf;
TidyDoc doc;
MP_START ();
QR_RESET_CTX
{
doc = tidyCreate ();
tidyBufInit (&errbuf);
tidySetErrorBuffer (doc, &errbuf);
/* cannot load cfg file here, must parse the config string */
tidy_parse_config (doc, config_input);
res = tidyParseString (doc, html_input);
if (res >= 0)
res = tidyCleanAndRepair (doc);
if (res >= 0)
res = tidyRunDiagnostics (doc);
if (res > 1)
res = (tidyOptSetBool (doc, TidyForceOutput, yes) ? res : -1);
if (res >= 0)
errlist = box_dv_short_string ((char *) errbuf.bp);
else
errlist = box_dv_short_string ("");
}
QR_RESET_CODE
{
caddr_t err;
POP_QR_RESET;
err = thr_get_error_code (THREAD_CURRENT_THREAD);
MP_DONE ();
sqlr_resignal (err);
}
END_QR_RESET;
MP_DONE ();
/*
tidyBufFree( &errbuf );
tidyRelease (doc);
*/
#endif
return errlist;
}
caddr_t
bif_tidy_external (caddr_t * qst, caddr_t * err_ret, state_slot_t ** args)
{
#ifdef OLD_TIDY
return box_num (0);
#else
return box_num (1);
#endif
}
int bif_tidy_init(void)
{
tidy_mtx = mutex_allocate ();
bif_define ("tidy_html", bif_tidy_html);
bif_define ("tidy_list_errors", bif_tidy_list_errors);
bif_define ("tidy_external", bif_tidy_external);
#ifndef OLD_TIDY
tidySetMallocCall (tidy_malloc);
tidySetReallocCall (tidy_realloc);
tidySetFreeCall (tidy_free);
tidySetPanicCall (tidy_panic);
#endif
return 0;
}
|