forked from revk/xmlsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlsql.c
4250 lines (4160 loc) · 133 KB
/
xmlsql.c
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2004 Adrian Kennard
// This software is provided under the terms of the GPL v2 or later.
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <popt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <dirent.h>
#include <arpa/inet.h>
#include <glob.h>
#include <sqllib.h>
#include "xmlparse.h"
#include "punycode.h"
#include "sqlexpand.h"
#include <stringdecimaleval.h>
const char BASE64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
#define BODGEEVAL // TODO remove some time soon
#ifndef SECURITYTAG // Env/tag fro security
#define SECURITYTAG * // Default tag
#endif
#define FLAG_RAW 1 // RAW output
#define FLAG_MARKUP 2 // Handle normal markup
#define FLAG_URL 4 // Link things that look like web pages
#define FLAG_SMILE 8 // IMG things that look like smilies
#define FLAG_SAFE 16 // Allow additional markup
#define FLAG_JSON 32 // Escape as JSON
#define FLAG_XML 128 // Escape as XML object
#define FLAG_TEXTAREA 256 // Escape as input for textarea
#define MAXTEMP 50000
#define Q(x) #x // Trick to quote defined fields
#define QUOTE(x) Q(x)
struct markup_s {
int len;
const char *match;
};
#define m(s) {sizeof(#s)-1,#s}
const struct markup_s markup[] = {
m(br), // break, self closing
m(hr), // hr, self closing
m(i), // italic
m(p), // paragraph
m(s), // strikeout
m(b), // bold
m(u), // underline
m(h1), // heading
m(ul), // unnumbered list
m(li), // list item
m(blink), // blink
};
#undef m
typedef struct smiley_s {
struct smiley_s *next;
int base;
char file[];
} smiley_t;
smiley_t *smiley = 0;
int safe = 0;
int debug = 0;
int comment = 0;
int noform = 0;
int showhidden = 0;
int maxinputsize = 80;
const char *sqlconf = NULL;
const char *sqlhost = NULL;
unsigned int sqlport = 0;
const char *sqluser = NULL;
const char *sqlpass = NULL;
const char *sqldatabase = NULL;
const char *smileydir = NULL;
const char *security = NULL;
FILE *of = 0;
int isxml = 0;
int allowexec = 0;
#define MAXLEVEL 10
int level = 0;
MYSQL sql;
MYSQL_RES *res[MAXLEVEL];
MYSQL_ROW row[MAXLEVEL];
MYSQL_FIELD *field[MAXLEVEL];
int fields[MAXLEVEL];
char sqlconnected = { 0 };
char sqlactive[MAXLEVEL] = { 0 };
typedef struct {
int selectmultiple:1;
int selectedoption:1;
char *selectvalue;
} process_t;
// Misc
xmltoken *loadfile(char *fn);
char *eval(char *e)
{ // evaluate e as simple decimal add/substract, ret 0 if not valid
static char temp[100];
char *p;
int sign = 0;
int places = 0,
digits = 0,
l;
long long t = 0;
p = e;
while (*p)
{
while (*p == '+' || *p == '-')
{
p++;
sign = 1;
}
// if (!isdigit (*p)) return 0;
int l = 0;
while (*p == '0')
p++;
while (isdigit(*p) || *p == ',')
{
p++;
l++;
}
if (l > digits)
digits = l;
if (*p == '.')
{
l = 0;
p++;
while (isdigit(*p))
{
p++;
l++;
}
if (l > places)
places = l;
}
if (*p && *p != '-' && *p != '+')
return 0; // not valid
}
if (!sign)
return 0; // numbers only, no sum
if (digits > 18)
return 0; // Too big
if (digits + places > 18)
places = 18 - digits;
p = e;
while (*p)
{
long long v = 0;
l = places;
sign = 1;
while (*p == '-' || *p == '+')
{
if (*p == '-')
sign = -sign;
p++;
}
char last = '0';
while (*p == '0')
p++;
while (isdigit(*p) || *p == ',')
{
if (isdigit(*p))
v = v * 10 + *p - '0';
last = *p++;
}
if (*p == '.')
{
p++;
while (isdigit(*p) && l > 0)
{
v = v * 10 + *p - '0';
l--;
last = *p++;
}
if (isdigit(*p) && *p > '5')
v++;
else if (*p == '5')
{
p++;
while (*p == '0')
p++;
if (isdigit(*p) || (last & 1))
v++; // bankers rounding
}
while (isdigit(*p))
p++;
}
while (l--)
v *= 10;
t += v * sign;
}
sign = 1;
if (t < 0)
{
sign = -1;
t = 0 - t;
};
if (!places)
sprintf(temp + 1, "%lld", t);
else
{
l = sprintf(temp + 2, "%lld", t);
if (l <= places)
{
sprintf(temp + 1, "0.%0*lld", places, t);
} else
{
memmove(temp + 1, temp + 2, l - places);
temp[1 + l - places] = '.';
}
}
*temp = '-';
if (sign < 0)
return temp;
return temp + 1;
}
char *readtime(char *val, time_t * when)
{
char *fmt = 0;
struct tm t = { 0 };
t.tm_isdst = -1;
if (!val)
return 0;
if (strlen(val) == 19)
{ /* YYYY-MM-DD HH:MM:SS */
t.tm_year = 1000 * (val[0] - '0') + 100 * (val[1] - '0') + 10 * (val[2] - '0') + val[3] - '0';
t.tm_mon = 10 * (val[5] - '0') + val[6] - '0';
t.tm_mday = 10 * (val[8] - '0') + val[9] - '0';
t.tm_hour = 10 * (val[11] - '0') + val[12] - '0';
t.tm_min = 10 * (val[14] - '0') + val[15] - '0';
t.tm_sec = 10 * (val[17] - '0') + val[18] - '0';
fmt = "%Y-%m-%d %H:%M:%S";
} else if (strlen(val) == 10)
{ /* YYYY-MM-DD */
t.tm_year = 1000 * (val[0] - '0') + 100 * (val[1] - '0') + 10 * (val[2] - '0') + val[3] - '0';
t.tm_mon = 10 * (val[5] - '0') + val[6] - '0';
t.tm_mday = 10 * (val[8] - '0') + val[9] - '0';
fmt = "%Y-%m-%d";
} else if (strlen(val) == 8 && val[2] == ':' && val[5] == ':')
{ /* HH:MM:SS */
t.tm_year = 2000; // 2000-01-01
t.tm_mon = 1;
t.tm_mday = 1;
t.tm_hour = 10 * (val[0] - '0') + val[1] - '0';
t.tm_min = 10 * (val[3] - '0') + val[4] - '0';
t.tm_sec = 10 * (val[6] - '0') + val[7] - '0';
fmt = "%H:%M:%S";
} else if (strlen(val) == 8)
{ /* YYYYMMDD */
t.tm_year = 1000 * (val[0] - '0') + 100 * (val[1] - '0') + 10 * (val[2] - '0') + val[3] - '0';
t.tm_mon = 10 * (val[4] - '0') + val[5] - '0';
t.tm_mday = 10 * (val[6] - '0') + val[7] - '0';
fmt = "%Y%m%d";
} else if (strlen(val) == 14)
{ /* YYYYMMDDHHMMSS */
t.tm_year = 1000 * (val[0] - '0') + 100 * (val[1] - '0') + 10 * (val[2] - '0') + val[3] - '0';
t.tm_mon = 10 * (val[4] - '0') + val[5] - '0';
t.tm_mday = 10 * (val[6] - '0') + val[7] - '0';
t.tm_hour = 10 * (val[8] - '0') + val[9] - '0';
t.tm_min = 10 * (val[10] - '0') + val[11] - '0';
t.tm_sec = 10 * (val[12] - '0') + val[13] - '0';
fmt = "%Y%m%d%H%M%S";
}
if (fmt && when && t.tm_year >= 1900 && t.tm_mon)
{
t.tm_year -= 1900;
t.tm_mon--;
*when = mktime(&t);
if (!*when)
fmt = 0;
} else
fmt = 0;
return fmt;
}
char matchvalue(char *tabbed, char *val)
{ // is val in a tabbed list
if (!val && !tabbed)
return 1;
if (!val || !tabbed)
return 0;
while (*tabbed)
{
char *v = val;
while (*v && *tabbed == *v)
{
tabbed++;
v++;
}
if (!*v && (!*tabbed || *tabbed == 9 || *tabbed == ','))
return 1; // match
while (*tabbed && *tabbed != 9 && *tabbed != ',')
tabbed++;
if (*tabbed == 9 || *tabbed == ',')
tabbed++;
}
return 0;
}
// Variable management functions:-
// Top level is SQL fields, working through nesting
// Then our set fields
// Then actual environment fields
int fieldlen(MYSQL_FIELD * f)
{
if (f->charsetnr == 45)
return f->length / 4;
if (f->charsetnr == 33)
return f->length / 3;
return f->length;
}
char *getvar(const char *n, int *lenp)
{ // Return a variable content
int l = level;
if (lenp)
*lenp = 0;
if (!n)
return 0;
if (*n == '$')
n = getvar(n + 1, NULL); // Nested get, e.g. ${$X}
// check SQL
while (l)
{
int f,
max;
l--;
max = fields[l];
if (sqlactive[l])
for (f = 0; f < max; f++)
if (!strcmp(field[l][f].name, n)) // TODO - what of database.field
{
char *v = NULL;
if (lenp)
*lenp = fieldlen(&field[l][f]);
if (sqlactive[l] == 2)
{
v = getenv(n);
if (!v)
v = field[l][f].def;
} else if (row[l][f])
v = (char *) row[l][f];
if (v && !strncmp(v, "0000", 4) && (field[l][f].type == FIELD_TYPE_DATE || field[l][f].type == FIELD_TYPE_DATETIME || field[l][f].type == FIELD_TYPE_TIMESTAMP))
v = "";
return v;
}
}
// last resort, environment
return getenv(n);
}
char *getvarexpand(const char *n)
{
return getvar(n, NULL);
}
char *expandd(char *buf, int len, const char *i, char sum)
{ // expand a string (sum set if allow variables without $ and default variables to zero, for maths in eval, etc)
char *o = buf,
*x = buf + len - 1;
if (!i)
return NULL;
{
// Does it need expanding?
const char *p = i;
while (*p)
{
if (*p == '$'
#ifndef BODGEEVAL
|| (sum && isalpha(*p) && (p == i || !isalnum(p[-1])))
#endif
)
break;
if (*p == '\\' && p[1])
{
p += 2;
continue;
}
p++;
}
if (!*p)
return (char *) i; // Unchanged
}
char q = 0;
// Expand
#ifndef BODGEEVAL
char *base = i;
#endif
while (*i && o < x)
{
if ((*i == '$' && i[1] && i[1] != '(' && !isspace(i[1])) // Note $( is jquery crap so we ignore and use in text
#ifndef BODGEEVAL
|| (sum && isalpha(*i) && (i == base || !isalnum(i[-1])))
#endif
)
{
const char *e;
dollar_expand_t *d = NULL;
char *fail(const char *e) {
warnx("Expand failed: %s\n[%s]", e, i);
dollar_expand_free(&d);
return NULL;
}
if (*i == '$')
i++;
d = dollar_expand_parse(&i, &e);
if (!d)
return fail(e);
if (e)
warnx("Expand: %s\n[%s]", e, i);
const char *name = dollar_expand_name(d);
if (!name)
warnx("Unexpected no name: %s", i);
else if (!strcmp(name, "$"))
{
o += sprintf(o, "%d", getppid());
continue;
} else
{
//char literal = dollar_expand_literal(d); // TODO use to control safe expansion...
char query = dollar_expand_query(d);
char *v = getvar(name, 0);
if (!v && query)
{
dollar_expand_free(&d);
return NULL; // expand fails as variable does not exist
}
if ((!v || !*v) && sum)
v = "0";
if (v && !query)
{
v = dollar_expand_process(d, v, &e, 0);
if (e)
warnx("Expand: %s\n[%s]", e, i);
if (v)
{
char safe = dollar_expand_underscore(d);
char list = dollar_expand_list(d);
char quote = dollar_expand_quote(d);
if (q)
quote = 0;
else if (quote)
{
if (o < x)
*o++ = (q = '"');
}
if (safe)
{
while (*v && o < x)
{
if (*v == '\'' || *v == '"')
*o = '_';
else
*o = *v;
o++;
v++;
}
} else if (q)
{
while (*v && o < x)
{
if (list && (*v == '\t' || *v == ','))
{ // List comma
if (q && o < x)
*o++ = q;
if (o < x)
*o++ = ',';
if (q && o < x)
*o++ = q;
} else
{
if (q && *v == q && o < x)
*o++ = *v;
if (o < x)
*o++ = *v;
}
v++;
}
} else
while (*v && o < x)
*o++ = *v++; // simple expansion
if (quote)
{
if (o < x)
*o++ = q;
q = 0;
}
}
}
}
dollar_expand_free(&d);
} else if (*i == '\\' && i[1])
{
*o++ = *i++;
*o++ = *i++;
} else // normal character
{
if (q && *i == q)
q = 0;
else if (*i == '\'' || *i == '"' || *i == '`')
q = *i;
*o++ = *i++;
}
}
*o = 0;
xmlutf8(buf);
return buf;
}
char *expand(char *buf, int len, char *i)
{
return expandd(buf, len, i, 0);
}
char *expandz(char *buf, int len, char *i)
{
return expandd(buf, len, i, 1);
}
static void xputc(unsigned char c, FILE * f, int flags)
{
if (flags & FLAG_XML)
{
if (c == '<')
fprintf(f, "%s", "<");
else if (c == '>')
fprintf(f, "%s", ">");
else if (c == '&' && !(flags & FLAG_MARKUP))
fprintf(f, "%s", "&");
else if (c < ' ')
fprintf(f, "&#%u;", c);
else
fputc(c, f);
} else if (flags & FLAG_JSON)
{
if (c == '"')
fprintf(f, "%s", "\\\"");
else if (c == '\\')
fprintf(f, "%s", "\\\\");
else if (c == '\r')
fprintf(f, "%s", "\\\\r");
else if (c == '\n')
fprintf(f, "%s", "\\\\n");
else if (c == '\t')
fprintf(f, "%s", "\\\\t");
else
fputc(c, f);
}
else
fputc(c, f);
}
static inline void xputs(char *s, FILE * f, int flags)
{
while (*s)
xputc(*s++, f, flags);
}
// alternative writeattr for tagwrite to use.
static void expandwriteattr(FILE * f, char *tag, char *value)
{
if (tag)
{
if (!value)
fprintf(f, " %s", tag);
else
{
char *p;
if (!*value)
fprintf(f, " %s=\"\"", tag);
else
{
char temp[MAXTEMP];
value = expand(temp, sizeof(temp), value);
if (value)
{
fprintf(f, " %s=\"", tag);
for (p = value; *p; p++)
{
if (*p == '"')
fprintf(f, """);
else if (isxml)
{
if (*p == '<')
fprintf(f, "<");
else if (*p == '>')
fprintf(f, ">");
else if (*p == '&')
fprintf(f, "&");
else if (*p & 0x80)
{ // possible UTF8
int n = 0;
if ((*p & 0xE0) == 0xC0)
n = 1;
else if ((*p & 0xF0) == 0xE0)
n = 2;
else if ((*p & 0xF8) == 0xF0)
n = 3;
else if ((*p & 0xFC) == 0xF8)
n = 4;
else if ((*p & 0xFE) == 0xFC)
n = 5;
if (n)
{
int q;
for (q = 0; q < n && (p[q + 1] & 0xC0) == 0x80; q++);
if (q < n)
n = 0;
}
if (n)
{ // valid UTF8
while (n--)
fputc(*p++, f);
fputc(*p, f);
} else
{ // take a guess that is ISO8859-1 to escape it
fputc(0xC0 + (*p >> 6), f);
fputc(0x80 + (*p & 0x3F), f);
}
} else // normal ASCII
fputc(*p, f);
} else
fputc(*p, f);
}
fputc('"', f);
}
}
}
}
}
// Alternative xmlwrite that expands all attributes
void tagwrite(FILE * f, xmltoken * t, ...)
{ // write token to file
if (!t)
return;
if (!t->type)
{ // write text
fprintf(f, "%s", t->content);
} else if (t->type & XML_COMMENT)
{ // comment
fprintf(f, "<!--%s-->", t->content);
} else if (t->type & (XML_START | XML_END))
{ // write token
fputc('<', f);
if (!(t->type & XML_START))
fputc('/', f);
fprintf(f, "%s", t->content);
if (t->type & XML_START)
{ // attributes only if start type...
char *hide = 0;
int a;
if (t->attrs)
{
hide = malloc(t->attrs);
if (!hide)
errx(1, "malloc at line %d", __LINE__);
memset(hide, 0, t->attrs);
}
{ // custom attributes
va_list ap;
va_start(ap, t);
while (1)
{
char *tag = va_arg(ap, char *),
*val;
if (!tag)
break;
val = va_arg(ap, char *);
for (a = 0; a < t->attrs; a++)
if (t->attr[a].attribute && !strcasecmp(t->attr[a].attribute, tag))
hide[a] = 1;
if (val != XMLATTREMOVE)
xmlwriteattr(f, tag, val);
}
va_end(ap);
}
if (t->attrs)
{ // attributes
for (a = 0; a < t->attrs; a++)
if (!hide[a])
expandwriteattr(f, t->attr[a].attribute, t->attr[a].value);
free(hide);
}
}
if ((t->type & XML_START) && (t->type & XML_END))
{
fputc(' ', f);
fputc('/', f);
}
fputc('>', f);
}
}
// Processing functions
void warning(xmltoken * x, char *w, ...)
{
va_list ap;
if (comment)
{
va_start(ap, w);
fprintf(of, "<!-- \n");
vfprintf(of, w, ap);
fprintf(of, " -->");
va_end(ap);
}
if (debug)
{
va_start(ap, w);
if (x)
fprintf(stderr, "%s:%d ", x->filename, x->line);
vfprintf(stderr, w, ap);
va_end(ap);
fputc('\n', stderr);
}
}
void info(xmltoken * x, char *w, ...)
{
va_list ap;
if (comment)
{
va_start(ap, w);
fprintf(of, "<!-- ");
vfprintf(of, w, ap);
fprintf(of, " -->");
va_end(ap);
}
if (debug)
{
va_start(ap, w);
if (x)
fprintf(stderr, "%s:%d ", x->filename, x->line);
vfprintf(stderr, w, ap);
va_end(ap);
fputc('\n', stderr);
}
}
xmltoken *processxml(xmltoken * x, xmltoken * e, process_t * state);
char *getattbp(xmltoken * x, char *n, char **breakpoint)
{ // get an attribute (if it has a value)
xmlattr *a = xmlfindattrbp(x, n, breakpoint);
if (a && a->value)
return a->value;
return 0;
}
char *checkmarkup(char *v, char *e, char *m, int l, int flags)
{ // check balanced markup contained from just after <m> and return start of </m> at end, else return 0
//fprintf (stderr, "Check (%.*s) [%.*s]\n", l, m, e - v, v);
while (v < e)
{
if (*v == '<')
{
char *q = v + 1;
if (*q == '/')
q++;
char *p = q;
while (isalnum(*p) && p < e)
p++;
char *z = p;
if (flags & FLAG_SAFE)
{ // skip attributes
while (z < e && *z != '>')
z++;
if (z < e && z > v && z[-1] == '/')
z--;
} else
while (isspace(*z) && z < e)
z++;
if (v[1] != '/' && z < e && *z == '/' && z + 1 < e && z[1] == '>')
{ // self ending markup
{ // allowed self ending markup
v = z + 2;
continue;
}
} else // if (*p == '>' && p < e)
{
if (v[1] == '/' && p - q == l && !strncasecmp(q, m, l))
{
//fprintf (stderr, "Found end at %.*s\n", e - v, v);
return v; // found end
}
{ // one we consider maybe valid
if (v[1] == '/')
return 0; // unbalanced
char *E = checkmarkup(z + 1, e, q, p - q, flags);
if (!E)
return 0; // this is not balanced
v = E + 3 + (p - q);
continue; // that was balanced, carry on
}
}
}
v++;
}
//fprintf (stderr, "Not balanced\n");
return 0;
}
#define getatt(x,t) getattbp(x,t,0)
void writeoutput(xmltoken * x, char *v, char *e, int hasreplace, int flags, int ps, int maxsize, int *count)
{
//fprintf (stderr, "Write out [%.*s]\n", e - v, v);
if ((*count) < 0)
return;
int space = 1;
while (v < e)
{
if (maxsize && ((*count) < 0 || (*count) >= maxsize))
{
(*count) = -1;
break;
}
if (space && smiley && (flags & FLAG_SMILE))
{ // markup and xml markup smiley
smiley_t *s;
for (s = smiley; s; s = s->next)
if (e - v >= s->base && !strncmp(v, s->file, s->base) && (v + s->base == e || !isalnum(v[s->base])))
break;
if (s)
{
xputc('<', of, flags);
fprintf(of, "img class='smiley' alt='smiley' src='%s/", smileydir);
char *p;
for (p = s->file; *p; p++)
{
if (isalpha(*p) || *p == '.')
fputc(*p, of);
else
fprintf(of, "%%%02X", *p);
}
fprintf(of, "' /");
xputc('>', of, flags);
v += s->base;
(*count)++;
continue;
}
}
if (hasreplace)
{
char match = 2;
int a;
for (a = hasreplace; a < x->attrs; a++)
{
if (!x->attr[a].value && !strcasecmp(x->attr[a].attribute, "MATCH"))
{ // expand for all tag matches
match = 1;
continue;
}
if (!x->attr[a].value && !strcasecmp(x->attr[a].attribute, "REPLACE"))
{ // expanded later
match = 2;
continue;
}
if (match == 2 && x->attr[a].attribute)
{
char temptag[1000];
char *t = expand(temptag, sizeof(temptag), x->attr[a].attribute);
if (t)
{
int l = strlen(t);
if (l && e - v >= l && !strncmp(v, t, l))
{
char tempval[1000];
char *e = expand(tempval, sizeof(tempval), x->attr[a].value);
if (e)
{
(*count) += strlen(e);
if (!maxsize || (*count) <= maxsize)
fprintf(of, "%s", e);
}
v += l;
break;
}
}
}
}
if (a < x->attrs)
{
space = 1;
continue; // matched and replaced
}
}
if (flags & (FLAG_MARKUP | FLAG_SAFE))
{ // markup and xml markup
if ((flags & FLAG_URL) && space && e - v > 4 && !strncasecmp(v, "www.", 4))
{
char *p = v + 4;
while (p < e)
{
if (!isalnum(*p))
break;
if (p < e && isalnum(*p))
p++;
while (p < e && (isalnum(*p) || *p == '-'))
p++;
if (p[-1] == '-')
break;
if (isspace(*p) || p == e)
{ // possible www.domain
(*count) += (p - v);
if (maxsize && (*count) > maxsize)
(*count) = -1;
else
{
xputc('<', of, flags);
fprintf(of, "a href='http://%.*s/' target='_blank'", (int) (p - v), v);
xputc('>', of, flags);
fprintf(of, "%.*s", (int) (p - v), v);
xputs("</a>", of, flags);
}
v = p;
break;
}
if (*p == '.')
p++;
else
break;
}
}
if ((flags & FLAG_URL) && space && ((e - v > 7 && !strncasecmp(v, "http://", 7)) || (e - v > 8 && !strncasecmp(v, "https://", 8))))
{
char *p = v + 4;
if (*p == 's')
p++;
p += 3;
while (p < e)
{
if (!isalnum(*p))
break;
if (p < e && isalnum(*p))
p++;
while (p < e && (isalnum(*p) || *p == '-'))
p++;
if (p[-1] == '-')
break;
if (p == e || isspace(*p) || *p == '/')
{ // possible www.domain
if (*p == '/')
while (p < e && !isspace(*p) && *p != '\'' && *p != '<' && *p != '>')
p++; // rest of URL
{
if (!strncasecmp(p - 4, ".jpg", 4) || !strncasecmp(p - 4, ".bmp", 4) || !strncasecmp(p - 4, ".png", 4) || !strncasecmp(p - 4, ".gif", 4) || !strncasecmp(p - 5, ".jpeg", 5))
{
(*count)++;
xputc('<', of, flags);
fprintf(of, "a href='%.*s' target='_blank'", (int) (p - v), v);
xputs("><", of, flags);
fprintf(of, "img class='pic' src='%.*s'", (int) (p - v), v);
xputs("/></a>", of, flags);
} else if (!strncasecmp(p - 4, ".flv", 4))
{
static int n = 0;
n++;
(*count)++;
xputc('<', of, flags);
fprintf(of, "div id=\"vp%u\" class=\"videocontainer\"\n", n);
xputs("></div><script type=\"text/javascript\">", of, flags);
fprintf(of, "playvideo('%.*s','vp%u');", (int) (p - v), v, n);
xputs("</script>", of, flags);
} else
{
(*count) += (p - v);
if (maxsize && (*count) > maxsize)
(*count) = -1;
else
{
xputc('<', of, flags);
fprintf(of, "a href='%.*s' target='_blank'", (int) (p - v), v);
xputc('>', of, flags);