forked from jedbarlow/links
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsint.c
3957 lines (3235 loc) · 108 KB
/
jsint.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
/* jsint.c
* (c) 2002 Mikulas Patocka (Vrxni Ideolog), Petr 'Brain' Kulhavy
* This file is a part of the Links program, relased under GPL.
*/
/*
* Ve vsech upcallech plati, ze pokud dostanu ID nejakeho objektu, tak
* javascript ma k tomu objektu pristupova prava. Jinymi slovy pristupova prava
* se testuji v upcallech jen, aby se neco neproneslo vratnici ven. Dovnitr se
* muze donaset vsechno, co si javascript donese, na to ma prava.
*
* Navic vsechny upcally dostanou pointer na f_data_c, kde bezi javascript,
* takze se bude moci testovat, zda javascript nesaha na f_data_c, ke kteremu
* nema pristupova prava.
*
* Brain
*/
/* Uctovani pameti:
* js_mem_alloc/js_mem_free se bude pouzivat na struktury fax_me_tender
* dale se bude pouzivat take ve funkcich pro praci s cookies, protoze string
* cookies v javascript_context se tez alokuje pomoci js_mem_alloc/js_mem_free.
*/
/*
Retezce:
vsechny retezce v ramci javascriptu jsou predavany v kodovani f_data->cp
(tedy tak, jak prisly v dokumentu ze site)
*/
#include "links.h"
#ifdef JS
tcount jsint_execute_seq = 0;
#include "struct.h"
#include "ipret.h"
#include "builtin_keys.h"
/*
vypisuje to: jaky kod byl zarazen do fronty. jaky kod byl predan interpretu do js_execute_code. jaky kod byl vykonan a ukoncen intepretem jsint_done_execution
#define TRACE_EXECUTE
*/
struct js_request {
struct js_request *next;
struct js_request *prev;
int onclick_submit; /* >=0 (znamena cislo formulare) pokud tohle je request onclick handleru u submit tlacitka nebo onsubmit handleru */
int onsubmit; /* dtto pro submit handler */
struct event ev; /* event, ktery se ma poslat pri uspechu */
int write_pos; /* document.write position from END of document. -1 if document.write cannot be used */
int wrote; /* this request called document.write */
int len;
tcount seq;
unsigned char code[1];
};
/* set_cookies bude parsovat takhle:
* +....=............;...............................+ <- tohle je strinzik
* ^ ^
* najdu 1. rovnase a za nim 1. strednik, najdu nasledujici rovnase a
* nasledujici strednik, pokud to bude platne (&'='<&';') a 2. jmeno bude
* "expires", tak to je furt jedna cookie -> poslu Mikulasovi.
*
* kdyz ne, tak je to jina susenka a poslu to 1. Mikulasovi
*
* pokud najdu ';' a za nim whitespace, za kterym neni rovnase, tak od toho
* stredniku je to garbaz, kterou vratim do strinziku (fd->js->ctx->cookies)
*/
/* sets all cookies in fd>js->ctx->cookies */
/* final_flush means, that set_cookies was called from jsint_done_execution */
/* JESTLI V TYHLE FUNKCI BUDE NEJAKA BUGA, tak za to muze PerM, Clock a pan GNU,
* ktery tady kolem rusili, ze jsem se nemohl soustredit. Takze se s
* pripadnejma reklamacema obratte na ne!
*
* Brain
*/
/* prototypes */
void jsint_send_event(struct f_data_c *fd, struct event *ev);
int jsint_create(struct f_data_c *);
void jsint_done_execution(struct f_data_c *);
int jsint_can_access(struct f_data_c *, struct f_data_c *);
struct f_data_c *jsint_find_recursive(struct f_data_c *, long); /* line 89 */
void *jsint_find_object(struct f_data_c *, long);
long *add_id(long *, int *, long );
long *add_fd_id(long *, int *, long, long, unsigned char *);
void send_vodevri_v_novym_vokne(struct terminal *, void (*)(struct terminal *, unsigned char *, unsigned char *), struct session *);
void add_all_recursive_in_fd(long **, int *, struct f_data_c *, struct f_data_c *);
void jsint_set_cookies(struct f_data_c *fd, int final_flush)
{
unsigned char *str;
unsigned char *next;
unsigned char *eq1, *semic1, *eq2, *semic2;
if(!(fd->js)||!(fd->js->ctx))internal("jsint_set_cookies called with NULL context.\n");
if (!(fd->js->ctx->cookies)||!(fd->rq))return; /* cookies string is empty, nothing to set */
str=fd->js->ctx->cookies;
a_znova:
eq1=strchr(str,'=');
semic1=strchr(str,';');
if (!*str||(!final_flush&&!semic1)) /* na konci neni strednik a skript jeste bezi, takze to musime vratit do stringu a vypadnout */
{
unsigned char *bla=NULL;
if (*str)bla=stracpy1(str);
js_mem_free(fd->js->ctx->cookies);
fd->js->ctx->cookies=bla;
return;
}
/* ted se v str bud vyskytuje strednik, nebo skript uz skoncil */
if (semic1&&eq1>semic1) /* '=' je za ';' takze to pred strednikem a strednik skipnem */
{
str=semic1+1;
goto a_znova;
}
next=semic1?semic1+1:str+strlen(cast_const_char str);
if (!eq1) /* neni tam '=', takze to preskocime */
{
str=next;
goto a_znova;
}
/* ted by to mela bejt regulerni susenka */
next_par:
eq2=NULL,semic2=NULL;
if (semic1!=NULL)
{
eq2=strchr(semic1+1,'=');
semic2=strchr(semic1+1,';');
}
if (eq2&&semic1&&(final_flush||semic2))
{
unsigned char *p=strstr(semic1+1,"expires");
if (!p)p=strstr(semic1+1,"Expires");
if (!p)p=strstr(semic1+1,"EXPIRES");
if (!p)p=strstr(semic1+1,"domain");
if (!p)p=strstr(semic1+1,"Domain");
if (!p)p=strstr(semic1+1,"DOMAIN");
if (!p)p=strstr(semic1+1,"path");
if (!p)p=strstr(semic1+1,"Path");
if (!p)p=strstr(semic1+1,"PATH");
if (!p)p=strstr(semic1+1,"comment");
if (!p)p=strstr(semic1+1,"Comment");
if (!p)p=strstr(semic1+1,"COMMENT");
if (!p)p=strstr(semic1+1,"max-age");
if (!p)p=strstr(semic1+1,"Max-age");
if (!p)p=strstr(semic1+1,"Max-Age");
if (!p)p=strstr(semic1+1,"MAX-AGE");
if (!p)p=strstr(semic1+1,"version");
if (!p)p=strstr(semic1+1,"Version");
if (!p)p=strstr(semic1+1,"VERSION");
if (p&&p>semic1&&p<eq2) /* za 1. prirazenim nasleduje "expires=", takze to je porad jedna susenka */
{
next=semic2?semic2+1:str+strlen(cast_const_char str);
semic1=semic2;
goto next_par;
}
}
if (*next)next[-1]=0;
for (;*str&&WHITECHAR(*str);str++); /* skip whitechars */
/*debug("set_cookie: \"%s\"", str);*/
set_cookie(fd->ses->term, fd->rq->url, str);
str=next;
goto a_znova;
}
int jsint_object_type(long to_je_on_Padre)
{
return to_je_on_Padre&JS_OBJ_MASK;
}
int jsint_create(struct f_data_c *fd)
{
struct js_state *js;
if (fd->js) internal("javascript state present");
js = mem_calloc(sizeof(struct js_state));
if (!(js->ctx = js_create_context(fd, ((fd->id)<<JS_OBJ_MASK_SIZE)|JS_OBJ_T_DOCUMENT))) {
mem_free(js);
return 1;
}
init_list(js->queue);
fd->js = js;
return 0;
}
void jsint_destroy(struct f_data_c *fd)
{
struct js_state *js = fd->js;
fd->script_t = 0;
if (!js) return;
fd->js = NULL;
pr(js_destroy_context(js->ctx)) return;
if (js->src) mem_free(js->src);
if (js->active) mem_free(js->active);
js_zaflaknuto_pameti-=js->newdata;
free_list(js->queue);
mem_free(js);
}
/* for <a href="javascript:..."> */
void javascript_func(struct session *ses, unsigned char *hlavne_ze_je_vecirek)
{
unsigned char *code=get_url_data(hlavne_ze_je_vecirek);
jsint_execute_code(current_frame(ses),code,strlen(cast_const_char code),-1,-1,-1, NULL);
}
void jsint_send_event(struct f_data_c *fd, struct event *ev)
{
if (!ev || !ev->b) return;
send_event(fd->ses, ev);
}
/* executes or queues javascript code in frame:
write_pos is number of bytes from the position where document.write should write to the end of document
write_pos == -1 if it is not from <SCRIPT> statement and cannot use document.write
*/
/* data je cislo formulare pro onclick submit handler, jinak se nepouziva */
/* ev je udalost, ktera se ma znovu poslat, pokud bylo vraceno true */
void jsint_execute_code(struct f_data_c *fd, unsigned char *code, int len, int write_pos, int onclick_submit, int onsubmit, struct event *ev)
{
struct js_request *r, *q;
for (;code&&len&&*code&&((*code)==' '||(*code)==9||(*code)==13||(*code)==10);code++,len--);
/*
FUJ !!!!
if (!strncasecmp(cast_const_char code,"javascript:",strlen(cast_const_char "javascript:")))code+=strlen(cast_const_char "javascript:");
*/
if (len >= 11 && !casecmp(code, "javascript:", 11)) code += 11, len -= 11;
if (!js_enable) {
jsint_send_event(fd, ev);
return;
}
#ifdef TRACE_EXECUTE
fprintf(stderr, "Submitted: ^^%.*s^^\n", len, code);
#endif
if (!fd->js && jsint_create(fd)) {
jsint_send_event(fd, ev);
return;
}
if ((unsigned)len > MAXINT - sizeof(struct js_request)) overalloc();
r = mem_calloc(sizeof(struct js_request) + len);
r->seq = jsint_execute_seq++;
r->write_pos = write_pos;
r->len = len;
r->onclick_submit = onclick_submit;
r->onsubmit = onsubmit;
memcpy(r->code, code, len);
if (ev) memcpy(&r->ev, ev, sizeof(struct event));
if (write_pos == -1) {
struct list_head *l = (struct list_head *)fd->js->queue.prev;
add_to_list(*l, r);
} else {
/* add it beyond all <SCRIPT> requests but before non-<SCRIPT> ones */
foreach(q, fd->js->queue) if (q->write_pos == -1) break;
q = q->prev;
add_at_pos(q, r);
}
jsint_run_queue(fd);
}
void jsint_done_execution(struct f_data_c *fd)
{
struct js_request *r, *to_exec;
struct js_state *js = fd->js;
struct event ev = { 0, 0, 0, 0 };
if (!js) {
internal("no js in frame");
return;
}
if (!js->active) {
internal("jsint_done_execution: completion function called on inactive js");
return;
}
#ifdef TRACE_EXECUTE
fprintf(stderr, "Done: ^^%.*s^^\n", js->active->len, js->active->code);
#endif
/* accept all cookies set by the script */
jsint_set_cookies(fd,1);
/* js->active obsahuje request prave dobehnuteho skriptu */
/* dobehl onclick_handler a nezaplatil (vratil false), budou se dit veci */
if (js->active->ev.b && js->ctx->zaplatim)
memcpy(&ev, &js->active->ev, sizeof(struct event));
if (js->active->onclick_submit >=0 && !js->ctx->zaplatim)
{
/* pokud je handler od stejneho formulare, jako je defered, tak odlozeny skok znicime a zlikvidujem prislusny onsubmit handler z fronty */
if (js->active->onclick_submit == fd->ses->defered_data)
{
foreach (r,js->queue)
/* to je onsubmit od naseho formulare, tak ho smazem */
if (r->onsubmit == js->active->onclick_submit)
{
del_from_list(r);
mem_free(r);
break; /* zadny dalsi onsubmit tohoto formulare uz nebude */
}
ses_destroy_defered_jump(fd->ses);
}
}
if (js->active->write_pos == -1) mem_free(js->active), js->active = NULL;
else {
r = js->active; js->active = NULL;
if (r->wrote) js->wrote = 1;
jsint_scan_script_tags(fd);
if (!f_is_finished(fd->f_data)) {
fd->done = 0;
fd->parsed_done = 0;
}
if (js->wrote && fd->script_t == -1) {
fd->done = 0;
fd->parsed_done = 0;
fd_loaded(NULL, fd);
js->wrote = 0;
}
mem_free(r);
}
to_exec = js->active;
if (!to_exec && !list_empty(fd->js->queue)) to_exec = fd->js->queue.next;
if (fd->ses->defered_url && (!to_exec || (to_exec->seq > fd->ses->defered_seq && to_exec->write_pos == -1)))
{
unsigned char *url, *target;
url=stracpy(fd->ses->defered_url);
target=stracpy(fd->ses->defered_url);
goto_url_f(fd->ses,NULL,url,target,fd->ses->defered_target_base,fd->ses->defered_data,0,0,0);
mem_free(url);
mem_free(target);
}
else
jsint_run_queue(fd);
jsint_send_event(fd, &ev);
}
void jsint_run_queue(struct f_data_c *fd)
{
struct js_request *r;
struct js_state *js = fd->js;
if ((!fd->done && fd->f_data) || !js || js->active || list_empty(js->queue)) return;
r = js->queue.next;
del_from_list(r);
js->active = r;
#ifdef TRACE_EXECUTE
fprintf(stderr, "Executing: ^^%.*s^^\n", r->len, r->code);
#endif
pr(js_execute_code(js->ctx, r->code, r->len, (void (*)(void *))jsint_done_execution)) {};
}
/* returns: 1 - source is modified by document.write
0 - original source
*/
int jsint_get_source(struct f_data_c *fd, unsigned char **start, unsigned char **end)
{
struct js_state *js = fd->js;
if (!js || !js->src) return 0;
if (start) *start = js->src;
if (end) *end = js->src + js->srclen;
return 1;
}
/*
* tests if script running in frame "running" can access document in frame "accessed"
* 0=no permission, 1=permission OK
*/
int jsint_can_access(struct f_data_c *running, struct f_data_c *accessed)
{
int a;
unsigned char *h1, *h2;
if (!running || !accessed || !running->rq || !accessed->rq) return 0;
h1 = get_host_name(running->rq->url);
h2 = get_host_name(accessed->rq->url);
a = !strcasecmp(cast_const_char h1, cast_const_char h2);
mem_free(h1);
mem_free(h2);
return a;
}
/* doc_id is real document id, whithout any type */
/* fd must be a valid pointer */
struct f_data_c *jsint_find_recursive(struct f_data_c *fd, long doc_id)
{
struct f_data_c *sub, *fdd;
if (fd->id == doc_id) return fd;
foreach(sub, fd->subframes) {
if ((fdd = jsint_find_recursive(sub, doc_id))) return fdd;
}
return NULL;
}
/*
* This function finds document that has given ID
*/
struct f_data_c *jsint_find_document(long doc_id)
{
struct f_data_c *fd;
struct session *ses;
int type=jsint_object_type(doc_id);
if (type!=JS_OBJ_T_DOCUMENT&&type!=JS_OBJ_T_FRAME)
{unsigned char txt[256]; snprintf(txt,256,"jsint_find_document called with type=%d\n",type);internal(txt);}
doc_id>>=JS_OBJ_MASK_SIZE;
foreach(ses, sessions) if ((fd = jsint_find_recursive(ses->screen, doc_id))) return fd;
return NULL;
}
/* Document has just loaded. Scan for <SCRIPT> tags and execute each of them */
void jsint_scan_script_tags(struct f_data_c *fd)
{
unsigned char *name, *attr;
int namelen;
unsigned char *val, *e, *ee;
unsigned char *s, *ss, *eof;
unsigned char *start, *end;
unsigned char *onload_code=NULL;
int uv;
int bs;
if (!js_enable)return;
if (!fd->rq || !fd->rq->ce || !fd->f_data) return;
if (!jsint_get_source(fd, &ss, &eof)) {
if (get_file(fd->rq, &ss, &eof)) return;
}
d_opt = &fd->f_data->opt;
s = ss;
se:
while (s < eof && *s != '<') sp:s++;
if (s >= eof || fd->script_t < 0)
{
if (onload_code && fd->script_t != -1)
{
jsint_execute_code(fd,onload_code,strlen(cast_const_char onload_code),-1,-1,-1, NULL);
}
fd->script_t = -1;
goto ret;
}
if (s + 2 <= eof && (s[1] == '!' || s[1] == '?')) {
s = skip_comment(s, eof);
goto se;
}
if (parse_element(s, eof, &name, &namelen, &attr, &s)) goto sp;
if (!onload_code&&namelen==4&&!casecmp(name,"BODY",4))
{
onload_code=get_attr_val(attr,"onload"); /* if the element doesn't have onload attribute get_attr_val returns NULL */
goto se;
}
if (!onload_code&&namelen==3&&!casecmp(name,"IMG",3))
{
onload_code=get_attr_val(attr,"onload"); /* if the element doesn't have onload attribute get_attr_val returns NULL */
goto se;
}
if (namelen != 6 || casecmp(name, "SCRIPT", 6) || s - ss < fd->script_t) goto se;
start = end = NULL;
if ((val = get_attr_val(attr, "src"))) {
unsigned char *url;
if (fd->f_data->script_href_base && ((url = join_urls(fd->f_data->script_href_base, val)))) {
int code, version;
struct additional_file *af = request_additional_file(fd->f_data, url);
mem_free(url);
mem_free(val);
if (!af || !af->rq) goto se;
if (af->rq->state >= 0) goto ret;
if (!af->rq->ce) goto se;
if (!get_http_code(af->rq->ce->head, &code, &version)) {
if (code < 200 || code >= 300) goto se;
}
if (get_file(af->rq, &start, &end)) goto se;
if (start == end) goto se;
} else {
mem_free(val);
goto se;
}
}
e = s;
uv = 0;
bs = 0;
while (e < eof && *e != '<') {
es:
if (!uv && (*e == '"' || *e == '\'')) uv = *e, bs = 0;
else if (*e == '\\' && uv) bs = 1;
else if (*e == uv && !bs) uv = 0;
else bs = 0;
e++;
}
if (e + 8 <= eof) {
if (/*uv ||*/ casecmp(e, "</SCRIPT", 8)) goto es;
} else e = eof;
ee = e;
while (ee < eof && *ee != '>') ee++;
if (ee < eof) ee++;
fd->script_t = ee - ss;
if (!start || !end) jsint_execute_code(fd, s, e - s, eof - ee,-1,-1, NULL);
else jsint_execute_code(fd, start, end - start, eof - ee,-1,-1, NULL);
ret:
if (onload_code)mem_free(onload_code);
d_opt = &dd_opt;
}
struct hopla_mladej
{
struct form_control *fc;
struct form_state *fs;
};
/* Returns pointer to the object with given ID in the document, or NULL when
* there's no such object. Document must be a valid pointer.
*
* Pointer type depends on type of object, caller must know the type and
* interpret the pointer in the right way.
*/
void *jsint_find_object(struct f_data_c *document, long obj_id)
{
int type=obj_id&JS_OBJ_MASK;
int orig_obj_id=obj_id;
obj_id>>=JS_OBJ_MASK_SIZE;
switch (type)
{
/* form element
* obj_id can be from 0 up to number of form fields
* (document->vs->form_info_len may be actually lower if the fields were never
* touched)
* returns allocated struct hopla_mladej, you must free it after use
*/
case JS_OBJ_T_TEXT:
case JS_OBJ_T_PASSWORD:
case JS_OBJ_T_TEXTAREA:
case JS_OBJ_T_CHECKBOX:
case JS_OBJ_T_RADIO:
case JS_OBJ_T_SELECT:
case JS_OBJ_T_SUBMIT:
case JS_OBJ_T_RESET:
case JS_OBJ_T_HIDDEN:
case JS_OBJ_T_BUTTON:
{
struct hopla_mladej *hopla;
struct form_control *fc;
/*int n=document->vs->form_info_len;*/
int a=0;
if (obj_id<0/*||obj_id>=n*/)return NULL;
hopla=mem_alloc(sizeof(struct hopla_mladej));
if (!(document->f_data)){mem_free(hopla);return NULL;};
foreachback(fc,document->f_data->forms)
if (fc->g_ctrl_num==obj_id){a=1;break;}
if (!a){mem_free(hopla);return NULL;}
if (!(hopla->fs=find_form_state(document, fc))){mem_free(hopla);return NULL;}
hopla->fc=fc;
return hopla;
}
/* link
* obj_id can be from 0 to (nlinks-1)
*/
case JS_OBJ_T_LINK:
{
struct link*l;
int n;
if (!(document->f_data))return NULL;
l=document->f_data->links;
n=document->f_data->nlinks;
if (obj_id<0||obj_id>=n)return NULL;
return l+obj_id;
}
/* form
* obj_id is form_num in struct form_control (f_data->forms)
*/
case JS_OBJ_T_FORM:
{
struct form_control *f;
if (!(document->f_data))return NULL;
foreachback(f, document->f_data->forms) if ((f->form_num)==obj_id)return f;
return NULL;
}
/* anchors
* obj_id is position in list of all tags
*/
case JS_OBJ_T_ANCHOR:
{
struct tag *t;
int a=0;
if (!(document->f_data))return NULL;
foreach(t,document->f_data->tags)
{
if (obj_id==a)return t;
a++;
}
return NULL;
}
break;
/* this is document
* call jsint_find_document
* returned value is struct f_data_c
*/
case JS_OBJ_T_FRAME:
case JS_OBJ_T_DOCUMENT:
return jsint_find_document(orig_obj_id);
/* image
* returned value is struct g_object_image *
*/
case JS_OBJ_T_IMAGE:
#ifdef G
if (F)
{
struct xlist_head *fi;
if (!document->f_data)return NULL;
foreach(fi,document->f_data->images)
{
struct g_object_image *gi;
struct g_object_image goi;
gi = (struct g_object_image *)((char *)fi + ((char *)&goi - (char *)&(goi.image_list)));
if (gi->id==obj_id)return gi;
}
return NULL;
}else
#endif
return NULL;
default:
internal("jsint_find_object: unknown type %d.",type);
return NULL; /* never called, but GCC likes it ;-) */
}
}
long *add_id(long *field,int *len,long id)
{
long *p;
int a;
for (a=0;a<(*len);a++) /* this object is already on the list */
if (field[a]==id)return field;
(*len)++;
if ((unsigned)*len > MAXINT / sizeof(long)) overalloc();
p=mem_realloc(field,(*len)*sizeof(long));
p[(*len)-1]=id;
return p;
}
long *add_fd_id(long *field,int *len,long fd,long id, unsigned char *name)
{
long *p;
int a;
for (a=0;a<(*len);a+=3) /* this object is already on the list */
if (field[a]==fd&&field[a+1]==id)return field;
(*len)+=3;
if ((unsigned)*len > MAXINT / sizeof(long)) overalloc();
p=mem_realloc(field,(*len)*sizeof(long));
p[(*len)-3]=fd;
p[(*len)-2]=id;
p[(*len)-1]=(name&&(*name))?(long)stracpy(name):(long)NULL;
return p;
}
static long js_upcall_get_frame_id(void *data);
/* finds all objects with name takhle_tomu_u_nas_nadavame
* in fd and all it's subframes with rq==NULL
* js_ctx is f_data_c of the accessing script
*/
static long *find_in_subframes(struct f_data_c *js_ctx, struct f_data_c *fd, long *pole_vole, int *n_items, unsigned char *takhle_tomu_u_nas_nadavame)
{
struct f_data_c *ff;
struct form_control *f;
#ifdef G
struct xlist_head *fi;
#endif
/* search frame */
foreach(ff,fd->subframes)
if (ff->loc&&ff->loc->name&&!strcmp(cast_const_char ff->loc->name,cast_const_char takhle_tomu_u_nas_nadavame)&&jsint_can_access(js_ctx,ff)) /* to je on! */
if (!(pole_vole=add_id(pole_vole,n_items,js_upcall_get_frame_id(ff))))return NULL;
if (!(fd->f_data))goto a_je_po_ptakach;
#ifdef G
if (F)
/* search images */
foreach(fi,fd->f_data->images)
{
struct g_object_image *gi;
struct g_object_image goi;
gi = (struct g_object_image *)((char *)fi + ((char *)(&goi) - (char *)(&(goi.image_list))));
if (gi->name&&!strcmp(cast_const_char gi->name, cast_const_char takhle_tomu_u_nas_nadavame))
if (!(pole_vole=add_id(pole_vole,n_items,JS_OBJ_T_IMAGE+((gi->id)<<JS_OBJ_MASK_SIZE))))return NULL;
}
#endif
/* search forms */
foreachback(f,fd->f_data->forms)
if (f->form_name&&!strcmp(cast_const_char f->form_name,cast_const_char takhle_tomu_u_nas_nadavame)) /* tak tohle JE Jim Beam */
if (!(pole_vole=add_id(pole_vole,n_items,((f->form_num)<<JS_OBJ_MASK_SIZE)+JS_OBJ_T_FORM)))return NULL;
/* search form elements */
foreachback(f,fd->f_data->forms)
if (f->name&&!strcmp(cast_const_char f->name,cast_const_char takhle_tomu_u_nas_nadavame)) /* tak tohle JE Jim Beam */
{
long tak_mu_to_ukaz=0;
tak_mu_to_ukaz=(f->g_ctrl_num)<<JS_OBJ_MASK_SIZE;
switch (f->type)
{
case FC_TEXT: tak_mu_to_ukaz|=JS_OBJ_T_TEXT; break;
case FC_PASSWORD: tak_mu_to_ukaz|=JS_OBJ_T_PASSWORD; break;
case FC_TEXTAREA: tak_mu_to_ukaz|=JS_OBJ_T_TEXTAREA; break;
case FC_CHECKBOX: tak_mu_to_ukaz|=JS_OBJ_T_CHECKBOX; break;
case FC_RADIO: tak_mu_to_ukaz|=JS_OBJ_T_RADIO; break;
case FC_IMAGE:
case FC_SELECT: tak_mu_to_ukaz|=JS_OBJ_T_SELECT; break;
case FC_SUBMIT: tak_mu_to_ukaz|=JS_OBJ_T_SUBMIT ; break;
case FC_RESET: tak_mu_to_ukaz|=JS_OBJ_T_RESET ; break;
case FC_HIDDEN: tak_mu_to_ukaz|=JS_OBJ_T_HIDDEN ; break;
case FC_BUTTON: tak_mu_to_ukaz|=JS_OBJ_T_BUTTON ; break;
default: /* internal("Invalid form element type.\n"); */
tak_mu_to_ukaz=0;break;
}
if (tak_mu_to_ukaz&&!(pole_vole=add_id(pole_vole,n_items,tak_mu_to_ukaz)))return NULL;
}
a_je_po_ptakach:
/* find in all rq==NULL */
foreach(ff,fd->subframes)
if (!(ff->rq)) pole_vole=find_in_subframes(js_ctx,ff,pole_vole,n_items,takhle_tomu_u_nas_nadavame);
return pole_vole;
}
/* resolves name of an object, returns field of all ID's with the name
* obj_id is object in which we're searching
* takhle_tomu_u_nas_nadavame is the searched name
* context is identifier of the javascript context
* n_items is number of returned items
*
* on error returns NULL
*/
long *jsint_resolve(void *context, long obj_id, char *takhle_tomu_u_nas_nadavame,int *n_items)
{
struct f_data_c *fd;
struct f_data_c *js_ctx=(struct f_data_c*)context;
long *pole_vole;
*n_items=0;
if (!takhle_tomu_u_nas_nadavame||!(*takhle_tomu_u_nas_nadavame))return NULL;
pole_vole=mem_alloc(sizeof(long));
switch(jsint_object_type(obj_id))
{
/* searched object can be a frame, image, form or a form element */
case JS_OBJ_T_DOCUMENT:
case JS_OBJ_T_FRAME:
fd=jsint_find_document(obj_id);
if (!fd||!(jsint_can_access(js_ctx,fd)))break;
pole_vole=find_in_subframes(js_ctx, fd, pole_vole, n_items, takhle_tomu_u_nas_nadavame);
break;
/* searched name can be a form element */
case JS_OBJ_T_FORM:
{
struct form_control *fc=jsint_find_object(js_ctx,obj_id);
struct form_control *f;
if (!fc){mem_free(pole_vole);return NULL;}
if (!(js_ctx->f_data)){mem_free(pole_vole);return NULL;}
foreachback(f,js_ctx->f_data->forms)
{
if (f->form_num==fc->form_num) /* this form */
if (f->name&&!strcmp(cast_const_char f->name,cast_const_char takhle_tomu_u_nas_nadavame)) /* this IS Jim Beam */
{
long tak_mu_to_ukaz=0;
tak_mu_to_ukaz=(f->g_ctrl_num)<<JS_OBJ_MASK_SIZE;
switch (f->type)
{
case FC_TEXT: tak_mu_to_ukaz|=JS_OBJ_T_TEXT; break;
case FC_PASSWORD: tak_mu_to_ukaz|=JS_OBJ_T_PASSWORD; break;
case FC_TEXTAREA: tak_mu_to_ukaz|=JS_OBJ_T_TEXTAREA; break;
case FC_CHECKBOX: tak_mu_to_ukaz|=JS_OBJ_T_CHECKBOX; break;
case FC_RADIO: tak_mu_to_ukaz|=JS_OBJ_T_RADIO; break;
case FC_IMAGE:
case FC_SELECT: tak_mu_to_ukaz|=JS_OBJ_T_SELECT; break;
case FC_SUBMIT: tak_mu_to_ukaz|=JS_OBJ_T_SUBMIT ; break;
case FC_RESET: tak_mu_to_ukaz|=JS_OBJ_T_RESET ; break;
case FC_HIDDEN: tak_mu_to_ukaz|=JS_OBJ_T_HIDDEN ; break;
case FC_BUTTON: tak_mu_to_ukaz|=JS_OBJ_T_BUTTON ; break;
default: tak_mu_to_ukaz=0;break;
/* internal("Invalid form element type.\n"); */
}
if ((tak_mu_to_ukaz&JS_OBJ_MASK)&&!(pole_vole=add_id(pole_vole,n_items,tak_mu_to_ukaz)))return NULL;
}
}
}
break;
}
if (!pole_vole)return NULL;
if (!(*n_items)){mem_free(pole_vole);pole_vole=NULL;}
return pole_vole;
}
/*------------------------>>>>>>>> UPCALLS <<<<<<<<-------------------------*/
/* tyhle upcally se volaji ze select smycky:
void js_upcall_confirm(void *data)
void js_upcall_alert(void * data)
void js_upcall_close_window(void *data)
void js_upcall_get_string(void *data)
void js_upcall_goto_url(void * data)
void js_upcall_goto_history(void * data)
void js_upcall_set_image_src(void* data)
V nich se musi volat js_spec_vykill_timer, aby se znicil timer, ktery upcall
zavolal.
Tyto upcally MUZOU dostavat f_data_c pointer primo, protoze kdyz ten f_data_c
umre a s nim i ten JS, tak se timery znicej --- tudiz se nic nestane.
*/
static void redraw_document(struct f_data_c *f)
{
/*
if (F) {
f->xl = -1;
f->yl = -1;
draw_to_window(f->ses->win, (void (*)(struct terminal *, void *))draw_doc, f);
}
*/
draw_fd(f);
}
/* returns ID of a document with the javascript */
long js_upcall_get_document_id(void *data)
{
struct f_data_c *fd;
if (!data)internal("js_upcall_get_document_id called with NULL pointer!");
fd=(struct f_data_c*)data;
return (((fd->id)<<JS_OBJ_MASK_SIZE)|JS_OBJ_T_DOCUMENT);
}
/* same as get_document_id, but returned type is FRAME */
static long js_upcall_get_frame_id(void *data)
{
struct f_data_c *fd;
if (!data)internal("js_upcall_get_document_id called with NULL pointer!");
fd=(struct f_data_c*)data;
return (((fd->id)<<JS_OBJ_MASK_SIZE)|JS_OBJ_T_FRAME);
}
/* writes "len" bytes starting at "str" to document */
void js_upcall_document_write(void *p, unsigned char *str, int len)
{
int pos;
unsigned char *s;
struct f_data_c *fd = p;
struct js_state *js = fd->js;
if (!js)return;
if (!js->active) internal("js_upcall_document_write: no request active");
if (js->active->write_pos == -1) return;
if (js->active->write_pos < 0) internal("js_upcall_document_write: js->active trashed");
if (!js->src) {
unsigned char *s, *eof;
if (get_file(fd->rq, &s, &eof)) return;
if (!(js->src = memacpy(s, eof - s))) return;
js->srclen = eof - s;
}
if ((unsigned)js->srclen + (unsigned)len > MAXINT) overalloc();
if ((unsigned)js->srclen + (unsigned)len < (unsigned)len) overalloc();
s = mem_realloc(js->src, js->srclen + len);
js->src = s;
if ((pos = js->srclen - js->active->write_pos) < 0) pos = 0;
memmove(s + pos + len, s + pos, js->srclen - pos);
memcpy(s + pos, str, len);
js->srclen += len;
js->newdata += len;
js_zaflaknuto_pameti += len;
js->active->wrote = 1;
}
/* returns title of actual document (=document in the script context) */
/* when an error occurs, returns NULL */
/* returned string should be deallocated after use */
unsigned char *js_upcall_get_title(void *data)
{
struct f_data_c *fd;
unsigned char *title, *t;
struct conv_table* ct;
if (!data)internal("js_upcall_get_title called with NULL pointer!");
fd=(struct f_data_c *)data;
title=mem_alloc(MAX_STR_LEN*sizeof(unsigned char));
if (!(get_current_title(fd->ses,title,MAX_STR_LEN))){mem_free(title);return NULL;}
if (fd->f_data)
{
ct=get_translation_table(fd->f_data->opt.cp,fd->f_data->cp);
t = convert_string(ct, title, strlen(cast_const_char title), NULL);
mem_free(title);
title=t;
}
return title;
}
/* sets title of actual document (=document in the script context) */
/* string title will be deallocated after use */
void js_upcall_set_title(void *data, unsigned char *title)
{
unsigned char *t;
struct conv_table* ct;
struct f_data_c *fd;
int l=0;
if (!data)internal("js_upcall_get_title called with NULL pointer!");
fd=(struct f_data_c *)data;
if (!title)return;
if (!(fd->f_data)){mem_free(title);return;}
if (fd->f_data->title)mem_free(fd->f_data->title);
fd->f_data->title=init_str();
fd->f_data->uncacheable=1;