Unnamed Fossil Project

Check-in [1c0b0643a5]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:phreads on winows and linux
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:1c0b0643a56e61b944bab62a187ff2ee5a90453c
User & Date: monster 2012-07-30 05:24:17
Context
2012-07-30
05:25
(no comment) check-in: 6ac907cea0 user: monster tags: trunk
05:24
phreads on winows and linux check-in: 1c0b0643a5 user: monster tags: trunk
2012-07-25
01:29
Rsa_Sign/Rsa_Verify on Xdata check-in: 5059020756 user: monster tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cgi.hc.

129
130
131
132
133
134
135


136
137
138
139
140
141
142
...
180
181
182
183
184
185
186


187
188
189
190
191
192






















193
194
195
196
197
198
199
...
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
...
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
...
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
...
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
...
480
481
482
483
484
485
486




















487
488
489
490
491
492
493
...
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
...
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
...
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
...
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
...
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
    int content_type;
    YO_CGI_COOKIE *cookie_in;
    YO_CGI_COOKIE *cookie_out;
    YO_CGI_UPLOAD *upload;
    YO_BUFFER *out;
    void *dstrm;
    char *dstrm_mimetype;


  } YO_CGI;

#define Cgi_Langlist(Cgir) ((Cgir)->langlist)

void Cgi_Destruct(YO_CGI *self)
#ifdef _YO_CGI_BUILTIN
  {
................................................................................
    free(self->query_string);
    free(self->path_info);
    free(self->content_boundary);
    free(self->dstrm_mimetype);
    __Unrefe(self->params);
    __Unrefe(self->out);
    __Unrefe(self->dstrm);


    memset(self,0xfe,sizeof(*self)); /* !!! */
    __Destruct(self);
  }
#endif
  ;
  






















YO_CGI_COOKIE **Cgi_Find_Cookie_(YO_CGI_COOKIE **qoo,char *name, int name_len)
#ifdef _YO_CGI_BUILTIN
  {
    while ( *qoo )
      {
        int i;
        char *Q = (*qoo)->name;
................................................................................
    (*cookie)->secure = secure;
    (*cookie)->expire = expire;
    (*cookie)->next = 0;
  }
#endif
  ;
  
char *Cgi_Get_Cookie(YO_CGI *cgir,char *name)
#ifdef _YO_CGI_BUILTIN
  {
    if ( name )
      {
        YO_CGI_COOKIE **q = Cgi_Find_Cookie_(&cgir->cookie_in,name,strlen(name));
        if ( *q )
          return (*q)->value;
      }
    return 0;
  }
#endif
  ;

YO_CGI_UPLOAD *Cgi_Find_Upload(YO_CGI *cgir, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = cgir->upload;
    for ( ; up ; up = up->next )
      if ( Str_Equal_Nocase(up->name,upload_name) )
        break;
    return up;
  }
#endif
  ;

char *Cgi_Mime_Of_Upload(YO_CGI *cgir, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = Cgi_Find_Upload(cgir,upload_name);
    if ( up )
      return up->mimetype;
    return 0;
  }
#endif
  ;

char *Cgi_Get_Upload_Path(YO_CGI *cgir, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = Cgi_Find_Upload(cgir,upload_name);
    if ( up )
      return up->path;
    else
      __Raise_Format(YO_ERROR_INVALID_PARAM,
        ("CGI request does not have upload with name '%s'",upload_name));
    return 0;
  }
#endif
  ;

void *Cgi_Open_Upload(YO_CGI *cgir, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    return Cfile_Open(Cgi_Get_Upload_Path(cgir,upload_name),"r");
  }
#endif
  ;

YO_CGI_UPLOAD *Cgi_Attach_Upload(YO_CGI_UPLOAD **upload, char *path, char *mimetype, char *name, char *original, int len)
#ifdef _YO_CGI_BUILTIN
  {
................................................................................
          return YO_CGI_HTTP_1_0;
      }
    return 0;
  }
#endif
  ;
  
YO_CGI *Cgi_Init()
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI *self = __Object_Dtor(sizeof(YO_CGI),Cgi_Destruct);


















    setlocale(LC_NUMERIC,"C");
    setlocale(LC_TIME,"C");

    self->server_software = Str_Copy_Npl(getenv("SERVER_SOFTWARE"),-1);
    self->gateway_interface = Cgi_Recognize_Gateway_Ifs(getenv("GATEWAY_INTERFACE"));
    self->server_protocol = Cgi_Recognize_Protocol(getenv("SERVER_PROTOCOL"));

    self->server_name = Str_Copy_Npl(getenv("SERVER_NAME"),-1);
    self->server_port = Str_To_Int_Dflt(getenv("SERVER_PORT"),80);

    self->request_uri = Str_Copy_Npl(getenv("REQUEST_URI"),-1);
    self->remote_addr = Str_Copy_Npl(getenv("REMOTE_ADDR"),-1);
    self->referer = Str_Copy_Npl(getenv("HTTP_REFERER"),-1);

    self->request_method = Cgi_Recognize_Request_Method(getenv("REQUEST_METHOD"));
    self->query_string = Str_Copy_Npl(getenv("QUERY_STRING"),-1);
    self->path_info = Str_Copy_Npl(getenv("PATH_INFO"),-1);    
    self->content_length = Str_To_Int_Dflt(getenv("CONTENT_LENGTH"),0);
    
    __Gogo
      {
        char *S = getenv("CONTENT_TYPE");
        self->content_type = Cgi_Recognize_Content_Type(S);
        if ( self->content_type == YO_CGI_MULTIPART )
          {
            char *bndr = strchr(S,';');
            if ( bndr )
              {
                ++bndr;
................................................................................
                  self->content_boundary = Str_Concat_Npl("--",bndr+9);
              }
          }
      }
     
     __Gogo
      {
        char *S = getenv("HTTP_COOKIE");
        if ( S ) while (*S)
          {
            int nam_len,val_len;
            char *nam;
            char *val;
            while ( Isspace(*S) ) ++S;
            nam = S;
................................................................................
            Cgi_Set_Cookie_(&self->cookie_in,nam,nam_len,val,val_len,0,0);
          }
      }
      
    __Gogo
      {
        YO_ARRAY *arr = 0;
        char *S1, *S2, *S = getenv("HTTP_ACCEPT_LANGUAGE");
        if ( S )
          {
            arr = Array_Pchars();
            while ( *S )
              {
                S1 = S;
                S2 = S;
................................................................................
            self->langlist = Array_Take_Data_Npl(arr);
          }
      }
    
    self->out = __Refe(Buffer_Init(0));
    return self;
  }




















#endif
  ;
  
void Cgi_Format_Bf(YO_CGI *self, YO_BUFFER *bf)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_COOKIE *q;
................................................................................
    else
      return "";
  }
#endif
  ;

char *Cgi_Upload_Part(
  YO_CGI *cgir, longptr_t out, Unknown_Write_Proc xwrite, 
  char *S, char *bf, int *L, int maxlen)
#ifdef _YO_CGI_BUILTIN
  {
    int count = 0;
    int cb_l = strlen(cgir->content_boundary);
    
    while (S && *L)
      {
        int j = 0, k = 0;
        if ( strncmp(S,cgir->content_boundary,cb_l) == 0 )
          break;
        
        //++j;
        
        while ( *L-j >= cb_l+2 )
          {
            k = 0;
            if ( S[j] == '\r' ) ++k;
            if ( S[j+k] == '\n' ) ++k;
            if ( 0 == strncmp(S+j+k,cgir->content_boundary,cb_l) )
              break;
            ++j;
          }
        
        if ( count + j > maxlen )
          __Raise(YO_ERROR_TO_BIG,__yoTa("payload size out of limit",0));
        Unknown_Write(out,S,j,xwrite);
        count += j;
        S = Stdin_Pump_Part(bf,S+j+k,L);
        if ( *L < cb_l ) 
          __Raise(YO_ERROR_IO,__yoTa("uncomplete request",0));
      }

    return S;
  }
#endif
................................................................................
      }
    return S;
  }
#endif
  ;
  
char *Cgi_Multipart_Next(
  YO_CGI *cgir, char *upload_dir, int upload_maxsize, char *bf,char *S, int *L)
#ifdef _YO_CGI_BUILTIN
  {
    char *SE;
    char *name, *filename, *ctype;
    int cb_l = strlen(cgir->content_boundary);
     
    while ( *S && Isspace(*S) ) ++S;
    
    if ( 0 == strncmp(S,cgir->content_boundary,cb_l) )
      {
        S = Stdin_Pump_Part(bf,S,L);
        SE = S+1;
        while ( *SE )
          {
            if ( *SE == '\n' && SE[1] == '\n' ) 
              { SE += 2; break; }
            if ( *SE == '\n' && SE[1] == '\r' && SE[2] == '\n') 
              { SE += 3; break; }
................................................................................
          }
        if ( *SE )
          {
            SE[-1] = 0;
            name = Cgi_Strip(Str_Fetch_Substr(S,"name=",0,";\n\r"));
            filename = Cgi_Strip(Str_Fetch_Substr(S,"filename=",0,";\n\r"));
            ctype = Cgi_Strip(Str_Fetch_Substr(S,"Content-Type:"," ",";\n\r"));
            S = Stdin_Pump_Part(bf,SE,L);
            if ( ctype || filename )
              {
                char *tmpfname = Path_Unique_Name(upload_dir,"cgi-",".upl");
                YO_CFILE *f = Cfile_Open(tmpfname,"w+P");
                YO_CGI_UPLOAD *u = Cgi_Attach_Upload(&cgir->upload,tmpfname,ctype,name,filename,0);
                S = Cgi_Upload_Part(cgir,(longptr_t)f,&Cf_Write,S,bf,L,upload_maxsize);
                Cfile_Flush(f);
                u->length = Cfile_Length(f);
                Cfile_Close(f);
              }
            else
              {
                YO_BUFFER *val = Buffer_Init(0);
                S = Cgi_Upload_Part(cgir,(longptr_t)val,&Bf_Write,S,bf,L,YO_CGI_POST_MAX_PAYLOAD);
                Xvalue_Set_Str(Xnode_Deep_Value(cgir->params,name),val->at,val->count);
              }
            return S;
          }
      }
    return 0;
  }
#endif
  ;
  
void Cgi_Process_Multipart_Content(YO_CGI *cgir, char *upload_dir, int upload_maxsize)
#ifdef _YO_CGI_BUILTIN
  {
    char bf[YO_STDF_PUMP_BUFFER];
    int L = Stdin_Pump(bf);
    char *S = bf;
    while ( S && L )
      __Auto_Release
        S = Cgi_Multipart_Next(cgir,upload_dir,upload_maxsize,bf,S,&L);
  }
#endif
  ;
  
YO_XNODE *Cgi_Query_Params(YO_CGI *cgir, char *upload_dir, int upload_maxsize)
#ifdef _YO_CGI_BUILTIN
  {


    if ( !cgir->params ) __Auto_Release
      {
        char lbuf[1024], c;
        char *q, *key, *value, *qE = lbuf+(sizeof(lbuf)-1);
        char *S = 0;
        cgir->params = __Refe(Xdata_Init());
        if ( !upload_dir ) upload_dir = Temp_Directory();
        if ( cgir->request_method == YO_CGI_GET )
          {
            S = cgir->query_string;
          }
        else if ( cgir->request_method == YO_CGI_POST )
          {
            if ( cgir->content_type == YO_CGI_MULTIPART )
              {
                if ( cgir->content_boundary )
                  Cgi_Process_Multipart_Content(cgir,upload_dir,upload_maxsize);
              }
            else if ( cgir->content_length < YO_CGI_POST_MAX_PAYLOAD )
              {
                int count = cgir->content_length;
                int l = 0;
                S = __Malloc(count+1);
                while ( l < count )
                  {
                    int q = fread(S+l,1,count-l,stdin);
                    if ( q < 0 )
                      __Raise_Format(YO_ERROR_IO,
................................................................................
                    l += q;
                  }
                S[count] = 0;
              }
            else
              __Raise(YO_ERROR_TO_BIG,__yoTa("payload size out of limit",0));
          }
        if ( S && (cgir->request_method == YO_CGI_GET || cgir->content_type == YO_CGI_URLENCODED) )
          {
            while ( *S )
              {
                value = 0;
                key = q = lbuf;
                while ( *S && *S == '&' ) ++S;
                while ( *S && *S != '=' && *S != '&' )
................................................................................
                    while ( *S && *S != '&' )
                      {
                        c = ( *S != '%' && *S != '+' ) ? *S++ : Str_Urldecode_Char(&S);
                        if ( q >= lbuf && q < qE ) *q++ = c;
                      }
                  }
                *q = 0; /* if (q == qE) there is one more char for final zero */
                Xvalue_Set_Str(Xnode_Deep_Value(cgir->params,key),value,q-value);
              }
          }
      }

    return cgir->params;
  }
#endif
  ;
  
#define Cgi_Get_Outbuf(Cgir) ((Cgir)->out)
#define Cgi_Get_Len(Cgir) ((Cgir)->out->count)
#define Cgi_Get_Cstr(Cgir) ((Cgir)->out->at)
#define Cgi_Puts(Cgir,S) Buffer_Puts(Cgir->out,S)
#define Cgi_Fill(Cgir,C,N) Buffer_Fill_Append(Cgir->out,C,N)
#define Cgi_Append(Cgir,S,L) Buffer_Append(Cgir->out,S,L)

void Cgi_Printf(YO_CGI *cgir, char *fmt, ...)
#ifdef _YO_CGI_BUILTIN
  {
    va_list va;
    va_start(va,fmt);
    Buffer_Printf_Va(cgir->out,fmt,va);
    va_end(va);
  }
#endif
  ;













void Cgi_Write_Out(YO_CGI *cgir, int out_status)
#ifdef _YO_CGI_BUILTIN
  {
    if ( out_status == YO_CGI_OUT_TEXTHTML )
      {
        Stdout_Put("Content-Type: text/html; charset=utf-8\r\n");
        printf("Content-Length: %d\r\n\r\n",Cgi_Get_Len(cgir));
        Stdout_Put(cgir->out->at);
      }
    else if ( out_status == YO_CGI_OUT_REDIRECT )
      {
        Stdout_Put("Location: ");
        Stdout_Put(cgir->out->at);
        Stdout_Put("\r\n");
        Stdout_Put(Cgi_Format_Cookies_Out(cgir));
        Stdout_Put("\r\n\r\n");
      }
    else if ( out_status == YO_CGI_OUT_DATASTREAM )
      {
        char bf[YO_FILE_COPY_BUFFER_SIZE];
        int count = 0;
        void *src = cgir->dstrm;
        int (*xread)(void*,void*,int,int) = Yo_Find_Method_Of(&src,Oj_Read_OjMID,YO_RAISE_ERROR);
        int len = Oj_Available(cgir->dstrm);
        Stdout_Put("Content-Type: ");
        Stdout_Put(cgir->dstrm_mimetype);
        Stdout_Put("\r\n");
        printf("Content-Length: %d\r\n\r\n",len);
        for ( ;; )
          {
            int i = xread(src,bf,YO_FILE_COPY_BUFFER_SIZE,0);
            if ( !i ) break;



            fwrite(bf,1,i,stdout);
            count += i;
          }
      }
    else /* empty page */
      {
        Stdout_Put("Content-Type: text/html; charset=utf-8\n");
        Stdout_Put("Content-Length: 0\n\n");
      }




    fflush(stdout);
  }
#endif
  ;

#endif /* C_once_C02CA609_3FE5_45AB_B64B_15A73FD72214 */







>
>







 







>
>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|




|








|


|








|


|







|


|










|


|







 







|


|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



|
|
|

|
|

|
|
|

|
|
|
|



|







 







|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|




|




|









|








|







 







|




|



|

|







 







|




|
|







|
|









|



|



|




|


>
>
|




|

|

|

|

|

|
|

|

|







 







|







 







|



>
|











|




|





>
>
>
>
>
>
>
>
>
>
>
>
|




|
|
|



|
|
|
|
|





|

|
|
|
|
|




>
>
>
|





|
|

>
>
>
>
|





129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
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
...
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
...
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
...
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
...
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
...
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
...
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
...
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
...
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
...
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
...
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
    int content_type;
    YO_CGI_COOKIE *cookie_in;
    YO_CGI_COOKIE *cookie_out;
    YO_CGI_UPLOAD *upload;
    YO_BUFFER *out;
    void *dstrm;
    char *dstrm_mimetype;
    void *istrm;
    void *ostrm;
  } YO_CGI;

#define Cgi_Langlist(Cgir) ((Cgir)->langlist)

void Cgi_Destruct(YO_CGI *self)
#ifdef _YO_CGI_BUILTIN
  {
................................................................................
    free(self->query_string);
    free(self->path_info);
    free(self->content_boundary);
    free(self->dstrm_mimetype);
    __Unrefe(self->params);
    __Unrefe(self->out);
    __Unrefe(self->dstrm);
    __Unrefe(self->ostrm);
    __Unrefe(self->istrm);
    memset(self,0xfe,sizeof(*self)); /* !!! */
    __Destruct(self);
  }
#endif
  ;
  
char *Cgi_Pump_Part(YO_CGI *cgi, char *buf, char *S, int *L)
#ifdef _YO_CGI_BUILTIN
  {
    if ( cgi->istrm )
      return Oj_Pump_Part(cgi->istrm,buf,S,L);
    else
      return Stdin_Pump_Part(buf,S,L);
  }
#endif
  ;
    
int Cgi_Pump(YO_CGI *cgi, char *buf)
#ifdef _YO_CGI_BUILTIN
  {
    if ( cgi->istrm )
      return Oj_Read(cgi->istrm,buf,YO_STDF_PUMP_BUFFER_W,0);
    else
      return Stdin_Pump(buf);  
  }
#endif
  ;
    
YO_CGI_COOKIE **Cgi_Find_Cookie_(YO_CGI_COOKIE **qoo,char *name, int name_len)
#ifdef _YO_CGI_BUILTIN
  {
    while ( *qoo )
      {
        int i;
        char *Q = (*qoo)->name;
................................................................................
    (*cookie)->secure = secure;
    (*cookie)->expire = expire;
    (*cookie)->next = 0;
  }
#endif
  ;
  
char *Cgi_Get_Cookie(YO_CGI *cgi,char *name)
#ifdef _YO_CGI_BUILTIN
  {
    if ( name )
      {
        YO_CGI_COOKIE **q = Cgi_Find_Cookie_(&cgi->cookie_in,name,strlen(name));
        if ( *q )
          return (*q)->value;
      }
    return 0;
  }
#endif
  ;

YO_CGI_UPLOAD *Cgi_Find_Upload(YO_CGI *cgi, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = cgi->upload;
    for ( ; up ; up = up->next )
      if ( Str_Equal_Nocase(up->name,upload_name) )
        break;
    return up;
  }
#endif
  ;

char *Cgi_Mime_Of_Upload(YO_CGI *cgi, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = Cgi_Find_Upload(cgi,upload_name);
    if ( up )
      return up->mimetype;
    return 0;
  }
#endif
  ;

char *Cgi_Get_Upload_Path(YO_CGI *cgi, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_UPLOAD *up = Cgi_Find_Upload(cgi,upload_name);
    if ( up )
      return up->path;
    else
      __Raise_Format(YO_ERROR_INVALID_PARAM,
        ("CGI request does not have upload with name '%s'",upload_name));
    return 0;
  }
#endif
  ;

void *Cgi_Open_Upload(YO_CGI *cgi, char *upload_name)
#ifdef _YO_CGI_BUILTIN
  {
    return Cfile_Open(Cgi_Get_Upload_Path(cgi,upload_name),"r");
  }
#endif
  ;

YO_CGI_UPLOAD *Cgi_Attach_Upload(YO_CGI_UPLOAD **upload, char *path, char *mimetype, char *name, char *original, int len)
#ifdef _YO_CGI_BUILTIN
  {
................................................................................
          return YO_CGI_HTTP_1_0;
      }
    return 0;
  }
#endif
  ;
  
char *Cgi_Get_Env(char **env,char *name)
#ifdef _YO_CGI_BUILTIN
  {
    if ( !env ) 
      return getenv(name); 
    else
      {
        int L = strlen(name);
        for (; *env; ++env ) 
          {
            if ( !strncmp(*env,name,L) && (*env)[L] == '=' ) 
              return *env + L + 1;
          }
      }
    return 0;
  }
#endif
  ;
    
YO_CGI *Cgi_Init_(YO_CGI *self,char **env)
#ifdef _YO_CGI_BUILTIN
  {
    setlocale(LC_NUMERIC,"C");
    setlocale(LC_TIME,"C");

    self->server_software = Str_Copy_Npl(Cgi_Get_Env(env,"SERVER_SOFTWARE"),-1);
    self->gateway_interface = Cgi_Recognize_Gateway_Ifs(Cgi_Get_Env(env,"GATEWAY_INTERFACE"));
    self->server_protocol = Cgi_Recognize_Protocol(Cgi_Get_Env(env,"SERVER_PROTOCOL"));

    self->server_name = Str_Copy_Npl(Cgi_Get_Env(env,"SERVER_NAME"),-1);
    self->server_port = Str_To_Int_Dflt(Cgi_Get_Env(env,"SERVER_PORT"),80);

    self->request_uri = Str_Copy_Npl(Cgi_Get_Env(env,"REQUEST_URI"),-1);
    self->remote_addr = Str_Copy_Npl(Cgi_Get_Env(env,"REMOTE_ADDR"),-1);
    self->referer = Str_Copy_Npl(Cgi_Get_Env(env,"HTTP_REFERER"),-1);

    self->request_method = Cgi_Recognize_Request_Method(Cgi_Get_Env(env,"REQUEST_METHOD"));
    self->query_string = Str_Copy_Npl(Cgi_Get_Env(env,"QUERY_STRING"),-1);
    self->path_info = Str_Copy_Npl(Cgi_Get_Env(env,"PATH_INFO"),-1);    
    self->content_length = Str_To_Int_Dflt(Cgi_Get_Env(env,"CONTENT_LENGTH"),0);
    
    __Gogo
      {
        char *S = Cgi_Get_Env(env,"CONTENT_TYPE");
        self->content_type = Cgi_Recognize_Content_Type(S);
        if ( self->content_type == YO_CGI_MULTIPART )
          {
            char *bndr = strchr(S,';');
            if ( bndr )
              {
                ++bndr;
................................................................................
                  self->content_boundary = Str_Concat_Npl("--",bndr+9);
              }
          }
      }
     
     __Gogo
      {
        char *S = Cgi_Get_Env(env,"HTTP_COOKIE");
        if ( S ) while (*S)
          {
            int nam_len,val_len;
            char *nam;
            char *val;
            while ( Isspace(*S) ) ++S;
            nam = S;
................................................................................
            Cgi_Set_Cookie_(&self->cookie_in,nam,nam_len,val,val_len,0,0);
          }
      }
      
    __Gogo
      {
        YO_ARRAY *arr = 0;
        char *S1, *S2, *S = Cgi_Get_Env(env,"HTTP_ACCEPT_LANGUAGE");
        if ( S )
          {
            arr = Array_Pchars();
            while ( *S )
              {
                S1 = S;
                S2 = S;
................................................................................
            self->langlist = Array_Take_Data_Npl(arr);
          }
      }
    
    self->out = __Refe(Buffer_Init(0));
    return self;
  }
#endif
  ;
  
YO_CGI *Cgi_Init()
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI *self = __Object_Dtor(sizeof(YO_CGI),Cgi_Destruct);
    return Cgi_Init_(self,0);
  }
#endif
  ;
    
YO_CGI *Cgi_Init_2(void *istrm, void *ostrm, char **env)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI *self = __Object_Dtor(sizeof(YO_CGI),Cgi_Destruct);
    self->istrm = __Refe(istrm);
    self->ostrm = __Refe(ostrm);
    return Cgi_Init_(self,env);
  }
#endif
  ;
  
void Cgi_Format_Bf(YO_CGI *self, YO_BUFFER *bf)
#ifdef _YO_CGI_BUILTIN
  {
    YO_CGI_COOKIE *q;
................................................................................
    else
      return "";
  }
#endif
  ;

char *Cgi_Upload_Part(
  YO_CGI *cgi, longptr_t out, Unknown_Write_Proc xwrite, 
  char *S, char *bf, int *L, int maxlen)
#ifdef _YO_CGI_BUILTIN
  {
    int count = 0;
    int cb_l = strlen(cgi->content_boundary);
    
    while (S && *L)
      {
        int j = 0, k = 0;
        if ( strncmp(S,cgi->content_boundary,cb_l) == 0 )
          break;
        
        //++j;
        
        while ( *L-j >= cb_l+2 )
          {
            k = 0;
            if ( S[j] == '\r' ) ++k;
            if ( S[j+k] == '\n' ) ++k;
            if ( 0 == strncmp(S+j+k,cgi->content_boundary,cb_l) )
              break;
            ++j;
          }
        
        if ( count + j > maxlen )
          __Raise(YO_ERROR_TO_BIG,__yoTa("payload size out of limit",0));
        Unknown_Write(out,S,j,xwrite);
        count += j;
        S = Cgi_Pump_Part(cgi,bf,S+j+k,L);
        if ( *L < cb_l ) 
          __Raise(YO_ERROR_IO,__yoTa("uncomplete request",0));
      }

    return S;
  }
#endif
................................................................................
      }
    return S;
  }
#endif
  ;
  
char *Cgi_Multipart_Next(
  YO_CGI *cgi, char *upload_dir, int upload_maxsize, char *bf,char *S, int *L)
#ifdef _YO_CGI_BUILTIN
  {
    char *SE;
    char *name, *filename, *ctype;
    int cb_l = strlen(cgi->content_boundary);
     
    while ( *S && Isspace(*S) ) ++S;
    
    if ( 0 == strncmp(S,cgi->content_boundary,cb_l) )
      {
        S = Cgi_Pump_Part(cgi,bf,S,L);
        SE = S+1;
        while ( *SE )
          {
            if ( *SE == '\n' && SE[1] == '\n' ) 
              { SE += 2; break; }
            if ( *SE == '\n' && SE[1] == '\r' && SE[2] == '\n') 
              { SE += 3; break; }
................................................................................
          }
        if ( *SE )
          {
            SE[-1] = 0;
            name = Cgi_Strip(Str_Fetch_Substr(S,"name=",0,";\n\r"));
            filename = Cgi_Strip(Str_Fetch_Substr(S,"filename=",0,";\n\r"));
            ctype = Cgi_Strip(Str_Fetch_Substr(S,"Content-Type:"," ",";\n\r"));
            S = Cgi_Pump_Part(cgi,bf,SE,L);
            if ( ctype || filename )
              {
                char *tmpfname = Path_Unique_Name(upload_dir,"cgi-",".upl");
                YO_CFILE *f = Cfile_Open(tmpfname,"w+P");
                YO_CGI_UPLOAD *u = Cgi_Attach_Upload(&cgi->upload,tmpfname,ctype,name,filename,0);
                S = Cgi_Upload_Part(cgi,(longptr_t)f,&Cf_Write,S,bf,L,upload_maxsize);
                Cfile_Flush(f);
                u->length = Cfile_Length(f);
                Cfile_Close(f);
              }
            else
              {
                YO_BUFFER *val = Buffer_Init(0);
                S = Cgi_Upload_Part(cgi,(longptr_t)val,&Bf_Write,S,bf,L,YO_CGI_POST_MAX_PAYLOAD);
                Xvalue_Set_Str(Xnode_Deep_Value(cgi->params,name),val->at,val->count);
              }
            return S;
          }
      }
    return 0;
  }
#endif
  ;
  
void Cgi_Process_Multipart_Content(YO_CGI *cgi, char *upload_dir, int upload_maxsize)
#ifdef _YO_CGI_BUILTIN
  {
    char bf[YO_STDF_PUMP_BUFFER];
    int L = Cgi_Pump(cgi,bf);
    char *S = bf;
    while ( S && L )
      __Auto_Release
        S = Cgi_Multipart_Next(cgi,upload_dir,upload_maxsize,bf,S,&L);
  }
#endif
  ;
  
YO_CGI *Cgi_Query_Params(YO_CGI *cgi, char *upload_dir, int upload_maxsize)
#ifdef _YO_CGI_BUILTIN
  {
    if ( !cgi ) cgi = Cgi_Init();
    
    if ( !cgi->params ) __Auto_Release
      {
        char lbuf[1024], c;
        char *q, *key, *value, *qE = lbuf+(sizeof(lbuf)-1);
        char *S = 0;
        cgi->params = __Refe(Xdata_Init());
        if ( !upload_dir ) upload_dir = Temp_Directory();
        if ( cgi->request_method == YO_CGI_GET )
          {
            S = cgi->query_string;
          }
        else if ( cgi->request_method == YO_CGI_POST )
          {
            if ( cgi->content_type == YO_CGI_MULTIPART )
              {
                if ( cgi->content_boundary )
                  Cgi_Process_Multipart_Content(cgi,upload_dir,upload_maxsize);
              }
            else if ( cgi->content_length < YO_CGI_POST_MAX_PAYLOAD )
              {
                int count = cgi->content_length;
                int l = 0;
                S = __Malloc(count+1);
                while ( l < count )
                  {
                    int q = fread(S+l,1,count-l,stdin);
                    if ( q < 0 )
                      __Raise_Format(YO_ERROR_IO,
................................................................................
                    l += q;
                  }
                S[count] = 0;
              }
            else
              __Raise(YO_ERROR_TO_BIG,__yoTa("payload size out of limit",0));
          }
        if ( S && (cgi->request_method == YO_CGI_GET || cgi->content_type == YO_CGI_URLENCODED) )
          {
            while ( *S )
              {
                value = 0;
                key = q = lbuf;
                while ( *S && *S == '&' ) ++S;
                while ( *S && *S != '=' && *S != '&' )
................................................................................
                    while ( *S && *S != '&' )
                      {
                        c = ( *S != '%' && *S != '+' ) ? *S++ : Str_Urldecode_Char(&S);
                        if ( q >= lbuf && q < qE ) *q++ = c;
                      }
                  }
                *q = 0; /* if (q == qE) there is one more char for final zero */
                Xvalue_Set_Str(Xnode_Deep_Value(cgi->params,key),value,q-value);
              }
          }
      }
      
    return cgi;
  }
#endif
  ;
  
#define Cgi_Get_Outbuf(Cgir) ((Cgir)->out)
#define Cgi_Get_Len(Cgir) ((Cgir)->out->count)
#define Cgi_Get_Cstr(Cgir) ((Cgir)->out->at)
#define Cgi_Puts(Cgir,S) Buffer_Puts(Cgir->out,S)
#define Cgi_Fill(Cgir,C,N) Buffer_Fill_Append(Cgir->out,C,N)
#define Cgi_Append(Cgir,S,L) Buffer_Append(Cgir->out,S,L)

void Cgi_Printf(YO_CGI *cgi, char *fmt, ...)
#ifdef _YO_CGI_BUILTIN
  {
    va_list va;
    va_start(va,fmt);
    Buffer_Printf_Va(cgi->out,fmt,va);
    va_end(va);
  }
#endif
  ;

void Cgi_Put_Out_(YO_CGI *cgi, char *S)
#ifdef _YO_CGI_BUILTIN
  {
    if (!S); else 
      if ( cgi->ostrm ) 
        Oj_Print(cgi->ostrm,S);
      else
        fputs(S,stdout);
  }
#endif
  ;
  
void Cgi_Write_Out(YO_CGI *cgi, int out_status)
#ifdef _YO_CGI_BUILTIN
  {
    if ( out_status == YO_CGI_OUT_TEXTHTML )
      {
        Cgi_Put_Out_(cgi,"Content-Type: text/html; charset=utf-8\r\n");
        Cgi_Put_Out_(cgi,__Format("Content-Length: %d\r\n\r\n",Cgi_Get_Len(cgi)));
        Cgi_Put_Out_(cgi,cgi->out->at);
      }
    else if ( out_status == YO_CGI_OUT_REDIRECT )
      {
        Cgi_Put_Out_(cgi,"Location: ");
        Cgi_Put_Out_(cgi,cgi->out->at);
        Cgi_Put_Out_(cgi,"\r\n");
        Cgi_Put_Out_(cgi,Cgi_Format_Cookies_Out(cgi));
        Cgi_Put_Out_(cgi,"\r\n\r\n");
      }
    else if ( out_status == YO_CGI_OUT_DATASTREAM )
      {
        char bf[YO_FILE_COPY_BUFFER_SIZE];
        int count = 0;
        void *src = cgi->dstrm;
        int (*xread)(void*,void*,int,int) = Yo_Find_Method_Of(&src,Oj_Read_OjMID,YO_RAISE_ERROR);
        int len = Oj_Available(cgi->dstrm);
        Cgi_Put_Out_(cgi,"Content-Type: ");
        Cgi_Put_Out_(cgi,cgi->dstrm_mimetype);
        Cgi_Put_Out_(cgi,"\r\n");
        Cgi_Put_Out_(cgi,__Format("Content-Length: %d\r\n\r\n",len));
        for ( ;; )
          {
            int i = xread(src,bf,YO_FILE_COPY_BUFFER_SIZE,0);
            if ( !i ) break;
            if ( cgi->ostrm )
              Oj_Write_Full(cgi->ostrm,bf,i);
            else
              fwrite(bf,1,i,stdout);
            count += i;
          }
      }
    else /* empty page */
      {
        Cgi_Put_Out_(cgi,"Content-Type: text/html; charset=utf-8\n");
        Cgi_Put_Out_(cgi,"Content-Length: 0\n\n");
      }
      
    if ( cgi->ostrm ) 
      Oj_Flush(cgi->ostrm);
    else 
      fflush(stdout);
  }
#endif
  ;

#endif /* C_once_C02CA609_3FE5_45AB_B64B_15A73FD72214 */

Changes to dicto.hc.

368
369
370
371
372
373
374


375
376
377
378
379

380
381
382
383
384
385
386
...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
...
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
  {
    YO_DICTO *dicto = Yo_Object_Dtor(sizeof(YO_DICTO),Dicto_Destruct);
    return dicto;
  }
#endif
  ;



void Dicto_Apply(YO_DICTO *o,void (*filter)(char *,void *,void *),void *state)  
#ifdef _YO_DICTO_BUILTIN  
  {
    int i;
    YO_DICTO_REC *nrec;

    if ( o && o->table ) 
      for ( i = 0; i < o->width; ++i )
        {      
          nrec = o->table[i];
          while ( nrec )
            {
              filter(nrec->key,nrec->ptr,state);
................................................................................
#endif
  ;
    
YO_ARRAY *Dicto_Values(YO_DICTO *o)
#ifdef _YO_DICTO_BUILTIN  
  {
    YO_ARRAY *a = Array_Void();
    Dicto_Apply(o,_Dicto_Filter_Push_Value,a);
    return a;
  }
#endif
  ;
    
void _Dicto_Filter_Push_Key(char *key, void *value, YO_ARRAY *a)
#ifdef _YO_DICTO_BUILTIN  
................................................................................
#endif
  ;
    
YO_ARRAY *Dicto_Keys(YO_DICTO *o)
#ifdef _YO_DICTO_BUILTIN  
  {
    YO_ARRAY *a = Array_Void();
    Dicto_Apply(o,_Dicto_Filter_Push_Key,a);
    return a;
  }
#endif
  ;
  
typedef void (*dicto_format_printer_t)(YO_BUFFER *bf,void *S);

char *Dicto_Format(YO_DICTO *o, dicto_format_printer_t print, YO_BUFFER *_bf, int pretty)
#ifdef _YO_DICTO_BUILTIN  
  {
    int start = 0, i, j=0;
    YO_BUFFER *bf = _bf;
    YO_DICTO_REC *nrec;


    if ( !bf ) bf = Buffer_Init(0);
    start = bf->count;
    
    Buffer_Fill_Append(bf,'{',1);
    if ( pretty )
      Buffer_Fill_Append(bf,'\n',1); 







>
>
|




>







 







|







 







|







|





>







368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
...
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
...
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
  {
    YO_DICTO *dicto = Yo_Object_Dtor(sizeof(YO_DICTO),Dicto_Destruct);
    return dicto;
  }
#endif
  ;

typedef void (*dicto_apply_filter_t)(char *,void *,void *);

void Dicto_Apply(YO_DICTO *o, /*dicto_apply_filter_t*/ void *_filter, void *state)  
#ifdef _YO_DICTO_BUILTIN  
  {
    int i;
    YO_DICTO_REC *nrec;
    dicto_apply_filter_t filter = _filter;
    if ( o && o->table ) 
      for ( i = 0; i < o->width; ++i )
        {      
          nrec = o->table[i];
          while ( nrec )
            {
              filter(nrec->key,nrec->ptr,state);
................................................................................
#endif
  ;
    
YO_ARRAY *Dicto_Values(YO_DICTO *o)
#ifdef _YO_DICTO_BUILTIN  
  {
    YO_ARRAY *a = Array_Void();
    Dicto_Apply(o,(dicto_apply_filter_t)_Dicto_Filter_Push_Value,a);
    return a;
  }
#endif
  ;
    
void _Dicto_Filter_Push_Key(char *key, void *value, YO_ARRAY *a)
#ifdef _YO_DICTO_BUILTIN  
................................................................................
#endif
  ;
    
YO_ARRAY *Dicto_Keys(YO_DICTO *o)
#ifdef _YO_DICTO_BUILTIN  
  {
    YO_ARRAY *a = Array_Void();
    Dicto_Apply(o,(dicto_apply_filter_t)_Dicto_Filter_Push_Key,a);
    return a;
  }
#endif
  ;
  
typedef void (*dicto_format_printer_t)(YO_BUFFER *bf,void *S);

char *Dicto_Format(YO_DICTO *o, /*dicto_format_printer_t*/ void *_print, YO_BUFFER *_bf, int pretty)
#ifdef _YO_DICTO_BUILTIN  
  {
    int start = 0, i, j=0;
    YO_BUFFER *bf = _bf;
    YO_DICTO_REC *nrec;
    dicto_format_printer_t print = _print;

    if ( !bf ) bf = Buffer_Init(0);
    start = bf->count;
    
    Buffer_Fill_Append(bf,'{',1);
    if ( pretty )
      Buffer_Fill_Append(bf,'\n',1); 

Changes to file.hc.

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
      {
      #ifdef __windoze
        wchar_t *tmp = __Malloc((tmp_len+1)*sizeof(wchar_t));
        GetTempPathW(tmp_len+1,tmp);
        ptr = Str_Unicode_To_Utf8(tmp);
      #else
        char *tmp = getenv("TEMP");
        if ( !tmp ) tmp = "/tmp"
        ptr = Str_Copy(tmp);
      #endif
      }
    return ptr;
  }
#endif
  ;
................................................................................
    __Auto_Ptr(ptr)
      {
      #ifdef __windoze
        wchar_t *tmp = __Malloc((tmp_len+1)*sizeof(wchar_t));
        ptr = Str_Unicode_To_Utf8(_wgetcwd(tmp,tmp_len));
      #else
        char *tmp = __Malloc(tmp_len+1);
        ptr = Str_Copy(getcwd(tmp,tmp_len),-1);
      #endif
      }
    return ptr;
  }
#endif
  ;








|







 







|







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
      {
      #ifdef __windoze
        wchar_t *tmp = __Malloc((tmp_len+1)*sizeof(wchar_t));
        GetTempPathW(tmp_len+1,tmp);
        ptr = Str_Unicode_To_Utf8(tmp);
      #else
        char *tmp = getenv("TEMP");
        if ( !tmp ) tmp = "/tmp";
        ptr = Str_Copy(tmp);
      #endif
      }
    return ptr;
  }
#endif
  ;
................................................................................
    __Auto_Ptr(ptr)
      {
      #ifdef __windoze
        wchar_t *tmp = __Malloc((tmp_len+1)*sizeof(wchar_t));
        ptr = Str_Unicode_To_Utf8(_wgetcwd(tmp,tmp_len));
      #else
        char *tmp = __Malloc(tmp_len+1);
        ptr = Str_Copy(getcwd(tmp,tmp_len));
      #endif
      }
    return ptr;
  }
#endif
  ;

Changes to httpx.hc.

55
56
57
58
59
60
61

62
63
64
65
66
67
68
...
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
...
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
...
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include "url.hc"
#include "tasque.hc"

#ifdef _LIBYOYO
#define _HTTPX_BUILTIN
#endif


typedef void (*httpx_collback_t)(void *obj, struct _YO_HTTPX *httpx, int status);

typedef struct _YO_HTTPX
  {
    void *netchnl;
    YO_URL *url;
    YO_URL *proxy_url;
................................................................................
    HTTPX_INVALID_RESPONSE        = 0x00000d01,
    HTTPX_INVALID_RESPONSE_CODE   = 0x00000d02,
  };

#define HTTPX_SUCCEEDED(Status) (((Status)&0x8f000000) == 0)
#define HTTPX_STATUS(Status)    ((Status)&0x0fff)

void Httpx_Aio_Recv(YO_HTTPX *httpx, void *dta, int count, int mincount, tcp_recv_callback_t callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      Tcp_Aio_Recv(httpx->netchnl,dta,count,mincount,callback,httpx);
  }
#endif
  ;
  
void Httpx_Aio_Send(YO_HTTPX *httpx, void *dta, int count, tcp_send_callback_t callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      Tcp_Aio_Send(httpx->netchnl,dta,count,callback,httpx);
  }
#endif
  ;
................................................................................
                return;
              }
                          
            while ( *q != '\n' && *q != '\r' && Isspace(*q) ) ++q;
            Q = q;
            while ( *q && *q != '\n' && *q != '\r' && !Isspace(*q) ) ++q;
            httpx->status_text = Str_Range_Npl(Q,q);
            bf->count = (q - bf->at);         
            count -= bf->count; 
            httpx->nline = 0;
            __Unrefe(httpx->hdrs);
            httpx->hdrs = __Refe(Dicto_Ptrs());
          }
        else
          {
................................................................................

int Httpx_Perform_Query(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    if ( httpx->async ) return 0;
    while ( httpx->state != HTTPX_FINISHED && !(httpx->state&HTTPX_BROKEN) )
      {
        Tasque_Perform_Update(10,0);
      }
    return httpx->status;
  }
#endif
  ;
  
int Httpx_Head(YO_HTTPX *httpx, char *uri)







>







 







|








|







 







|







 







|







55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
...
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
#include "url.hc"
#include "tasque.hc"

#ifdef _LIBYOYO
#define _HTTPX_BUILTIN
#endif

struct _YO_HTTPX;
typedef void (*httpx_collback_t)(void *obj, struct _YO_HTTPX *httpx, int status);

typedef struct _YO_HTTPX
  {
    void *netchnl;
    YO_URL *url;
    YO_URL *proxy_url;
................................................................................
    HTTPX_INVALID_RESPONSE        = 0x00000d01,
    HTTPX_INVALID_RESPONSE_CODE   = 0x00000d02,
  };

#define HTTPX_SUCCEEDED(Status) (((Status)&0x8f000000) == 0)
#define HTTPX_STATUS(Status)    ((Status)&0x0fff)

void Httpx_Aio_Recv(YO_HTTPX *httpx, void *dta, int count, int mincount, /*tcp_recv_callback_t*/ void *callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      Tcp_Aio_Recv(httpx->netchnl,dta,count,mincount,callback,httpx);
  }
#endif
  ;
  
void Httpx_Aio_Send(YO_HTTPX *httpx, void *dta, int count, /*tcp_send_callback_t*/ void *callback)
#ifdef _HTTPX_BUILTIN
  {
    if ( !httpx->ssl )
      Tcp_Aio_Send(httpx->netchnl,dta,count,callback,httpx);
  }
#endif
  ;
................................................................................
                return;
              }
                          
            while ( *q != '\n' && *q != '\r' && Isspace(*q) ) ++q;
            Q = q;
            while ( *q && *q != '\n' && *q != '\r' && !Isspace(*q) ) ++q;
            httpx->status_text = Str_Range_Npl(Q,q);
            bf->count = (q - (char*)bf->at);         
            count -= bf->count; 
            httpx->nline = 0;
            __Unrefe(httpx->hdrs);
            httpx->hdrs = __Refe(Dicto_Ptrs());
          }
        else
          {
................................................................................

int Httpx_Perform_Query(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    if ( httpx->async ) return 0;
    while ( httpx->state != HTTPX_FINISHED && !(httpx->state&HTTPX_BROKEN) )
      {
        if ( !Tasque_Perform_Update(10,0) ) break;
      }
    return httpx->status;
  }
#endif
  ;
  
int Httpx_Head(YO_HTTPX *httpx, char *uri)

Changes to pipex.hc.

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
...
258
259
260
261
262
263
264









265
266
267
268
269
270
271
        if ( !wr )  wp->pin = 0; else wp->pout = 0;
        return ret;
      }
  }
#endif
  ;
  
YO_PIPEX *Pipex_Exec(char *command, char *xstdin, char *xstdout, char *xstderr)
#ifdef _YO_PIPEX_BUILTIN
  {
    YO_PIPEX *pipex = 0;
    __Auto_Ptr(pipex)
      {
        int foo;
        PROCESS_INFORMATION piProcInfo = {0};
................................................................................
        if ( !siStartInfo.hStdError )  siStartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);

        SetHandleInformation(siStartInfo.hStdInput, HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
        SetHandleInformation(siStartInfo.hStdOutput,HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
        SetHandleInformation(siStartInfo.hStdError, HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
          
        GetEnvironmentVariableW(L"COMSPEC", comspec, 256);










        if (!CreateProcessW(NULL,
            Str_Utf8_To_Unicode(__Format("\"%s\" /c %s",Str_Unicode_To_Utf8(comspec),command)),
            NULL,
            NULL,
            TRUE,
            0,//CREATE_NO_WINDOW,







|







 







>
>
>
>
>
>
>
>
>







218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
        if ( !wr )  wp->pin = 0; else wp->pout = 0;
        return ret;
      }
  }
#endif
  ;
  
YO_PIPEX *Pipex_Exec(char *command, char *xstdin, char *xstdout, char *xstderr, char **env)
#ifdef _YO_PIPEX_BUILTIN
  {
    YO_PIPEX *pipex = 0;
    __Auto_Ptr(pipex)
      {
        int foo;
        PROCESS_INFORMATION piProcInfo = {0};
................................................................................
        if ( !siStartInfo.hStdError )  siStartInfo.hStdError  = GetStdHandle(STD_ERROR_HANDLE);

        SetHandleInformation(siStartInfo.hStdInput, HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
        SetHandleInformation(siStartInfo.hStdOutput,HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
        SetHandleInformation(siStartInfo.hStdError, HANDLE_FLAG_INHERIT,HANDLE_FLAG_INHERIT);
          
        GetEnvironmentVariableW(L"COMSPEC", comspec, 256);
        
        if ( env )
          while ( *env )
            {
              wchar_t *q = Str_Utf8_To_Unicode(*env++);
              wchar_t *p = wcschr(q,'=');
              *p++ = 0;
              SetEnvironmentVariableW(q,p);
            }

        if (!CreateProcessW(NULL,
            Str_Utf8_To_Unicode(__Format("\"%s\" /c %s",Str_Unicode_To_Utf8(comspec),command)),
            NULL,
            NULL,
            TRUE,
            0,//CREATE_NO_WINDOW,

Changes to stdf.hc.

80
81
82
83
84
85
86
















87
88
89
90
91
92
93
    STRICT_REQUIRE(l <= YO_STDF_PUMP_BUFFER_W);
    buf[l] = 0;
    return buf;
  }
#endif
  ;
  
















#define Stdout_Put(S) if (!S); else fputs(S,stdout)
#define Stdin_Pump(B) Stdf_Pump(stdin,B)
#define Stdin_Pump_Part(B,S,L) Stdf_Pump_Part(stdin,B,S,L)

/* returns -1 if error and read bytes count on success */
typedef int Unknown_Write_Proc(void *buf, longptr_t f, int count, int *err);








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
    STRICT_REQUIRE(l <= YO_STDF_PUMP_BUFFER_W);
    buf[l] = 0;
    return buf;
  }
#endif
  ;
  
char *Oj_Pump_Part(void *f, char *buf, char *S, int *L)
#ifdef _YO_STDF_BUILTIN
  {
    int l = *L;
    if ( !S ) S = buf+l;
    l -= ( S-buf);
    if ( l ) memmove(buf,S,l);
    l += Oj_Read(f,buf+l,YO_STDF_PUMP_BUFFER_W-l,0);
    *L = l;
    STRICT_REQUIRE(l <= YO_STDF_PUMP_BUFFER_W);
    buf[l] = 0;
    return buf;
  }
#endif
  ;

#define Stdout_Put(S) if (!S); else fputs(S,stdout)
#define Stdin_Pump(B) Stdf_Pump(stdin,B)
#define Stdin_Pump_Part(B,S,L) Stdf_Pump_Part(stdin,B,S,L)

/* returns -1 if error and read bytes count on success */
typedef int Unknown_Write_Proc(void *buf, longptr_t f, int count, int *err);

Changes to tasque.hc.

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
...
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
...
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
...
321
322
323
324
325
326
327


void Tasque_Task_Processor(void *_)
#ifdef _YO_TASQUE_BUILTIN
  {
    int alive = 1;
    thread_t th = Thread_Get_Current();
    YO_TASQUE_TASK *t = 0;
    
    __Thr_Lock(&Tasque_Lock) 
      {
        ++Tasque_ONCARE;
        ++Tasque_COUNT;

      }
       
    while ( alive ) 
      {
        __Thr_Lock(&Tasque_Lock)
          {            
            if ( Tasque_Task_In ) 
              {
                t = Slist_Pop((YO_TASQUE_TASK**)&Tasque_Task_In);

                STRICT_REQUIRE(Tasque_ONCARE > 0);
                STRICT_REQUIRE(!t->processor);

                t->processor = th;
                Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_Care,t);








                --Tasque_ONCARE;
              }
            else
              {
                if ( Tasque_ONCARE > Tasque_RESERVE ) 
                  {
                    alive = 0;
                    --Tasque_COUNT;
                    --Tasque_ONCARE;
                  }
                else
                  Unlock_Wait_Lock(&Tasque_Lock,-1);
              }
          }

          
        if ( t )
          {
            t->task(t->obj);

            __Thr_Lock(&Tasque_Lock)
              {
                Slist_Remove((YO_TASQUE_TASK**)&Tasque_Task_Care,t);
                Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_Out,t);
                t = 0;
                ++Tasque_ONCARE;
              }
              

            Nolock_Notify(&Tasque_Out_Monitor);
         }
      }
      
    Nolock_Notify(&Tasque_Lock);
  }
#endif
  ;

void Tasque_Queue(tasque_proc_t task, void *obj, tasque_update_t update, void *monitor)
#ifdef _YO_TASQUE_BUILTIN
  {
    __Thr_Lock(&Tasque_Lock)
      {
        YO_TASQUE_TASK *t = __Object_Dtor(sizeof(YO_TASQUE_TASK),YO_TASQUE_TASK_Destruct);
        t->task = task;
        t->obj  = __Refe(obj);
        t->update = update;
        t->monitor = __Refe(monitor);
        Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_In,__Retain(t));
        if ( !Tasque_ONCARE && Tasque_COUNT < Tasque_MAX )
          Thread_Run_Close(Tasque_Task_Processor,0,TASQUE_STACK_SIZE);
      }

    Nolock_Notify(&Tasque_Lock);

  }
#endif
  ;

void Tasque_Alert(int ms, tasque_alert_t callback, void *obj)
#ifdef _YO_TASQUE_BUILTIN
  {
    YO_TASQUE_ALERT *t = __Object_Dtor(sizeof(YO_TASQUE_ALERT),YO_TASQUE_ALERT_Destruct);
    quad_t deadline = Get_System_Millis() + ms;
    t->deadline = deadline;
    t->callback = callback;
    t->obj = __Refe(obj);
    Slist_Push(&Tasque_Alerts_List,__Retain(t));
  }
#endif
  ;
  

int Tasque_Perform_Update(int ms, int worktime)
#ifdef _YO_TASQUE_BUILTIN
  {
    int i;
    quad_t deadline = worktime ? Get_System_Millis() + worktime : 0;
    YO_TASQUE_TASK *t;
    YO_TASQUE_ALERT *k;
    YO_TASQUE_ALERT **a; 
    



    if ( !Tasque_Task_Out )



      Nolock_Wait(&Tasque_Out_Monitor,ms);  
    

    do for ( i=0; i < 10 && Tasque_Task_Out; ++i ) __Auto_Release
      {
        __Thr_Lock(&Tasque_Lock)
          t = __Pool_RefPtr(Slist_Pop((YO_TASQUE_TASK**)&Tasque_Task_Out));
        t->update(t->monitor,t->obj,TASQUE_COMPLETE);
      }
    while (Tasque_Task_Out && ( !deadline || Get_System_Millis() < deadline) ); 
................................................................................
              }
          }
        while ( *a );
      }
    
    __Thr_Lock(&Tasque_Lock)
      i =  !!Tasque_Task_In || !!Tasque_Task_Out || !!Tasque_Task_Care || !!Tasque_Alerts_List;
    
    return i;
  }
#endif
  ;

void Tasque_Finish_Task_List(YO_TASQUE_TASK * volatile *lst,int status)
#ifdef _YO_TASQUE_BUILTIN
  {
    YO_TASQUE_TASK *t;
    while ( *lst ) __Auto_Release
      {
        t = __Pool_RefPtr(Slist_Pop((YO_TASQUE_TASK**)lst));
        if ( t->processor )
          Thread_Terminate(t->processor);
        t->update(t->monitor,t->obj,status);
      }
  }
#endif
  ;
  
................................................................................
  {
    YO_TASQUE_TASK *t;
    Tasque_MAX = 0;
    Tasque_RESERVE = 0;
 
    if ( Tasque_COUNT )
      {
        Nolock_Notify(&Tasque_Lock);
        Sleep(30);
      }
          
    __Thr_Lock(&Tasque_Lock)
      {
        Tasque_Finish_Task_List(&Tasque_Task_Care,TASQUE_FAIL);
        Tasque_Finish_Task_List(&Tasque_Task_In,TASQUE_FAIL);
................................................................................
  {
    quad_t t = System_Millis() + ms;
    Tasque_MAX = 0;
    Tasque_RESERVE = 0;
    
    do 
      {
        Nolock_Notify(&Tasque_Lock);
        Sleep(10);
      }
    while ( Tasque_ONCARE || t > System_Millis() );

    __Thr_Lock(&Tasque_Lock)
      Tasque_Finish_Task_List(&Tasque_Task_Out,TASQUE_COMPLETE);
    
................................................................................
  }
#endif
  ;



#endif /* C_once_FDCDEF27_5EA4_4C1D_99AB_1CD0A992A18A */








|
|
<
|
|
>
|
<


|
<
|
|
|

|
|

|
|
>
>
>
>
>
>
>
>




<
<
<
<
<
<
<
|


>





|
<
|
|
|
|
<
<
>
|



|




|










<
<

>
|
>




|












>








|
>
>
>

>
>
>
|
|
>







 







<












|







 







|







 







|







 







>
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
...
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
...
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
...
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
...
326
327
328
329
330
331
332
333

void Tasque_Task_Processor(void *_)
#ifdef _YO_TASQUE_BUILTIN
  {
    int alive = 1;
    thread_t th = Thread_Get_Current();
    YO_TASQUE_TASK *t = 0;

    Thread_Lock(&Tasque_Lock);

    ++Tasque_ONCARE;
    ++Tasque_COUNT;
    Thread_Unlock(&Tasque_Lock);
       

    while ( alive ) 
      {
        Thread_Lock(&Tasque_Lock);

        if ( Tasque_Task_In ) 
          {
            t = Slist_Pop((YO_TASQUE_TASK**)&Tasque_Task_In);

            STRICT_REQUIRE(Tasque_ONCARE > 0);
            STRICT_REQUIRE(!t->processor);

            t->processor = th;
            Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_Care,t);
            --Tasque_ONCARE;
          }
        else
          {
            if ( Tasque_ONCARE > Tasque_RESERVE ) 
              {
                alive = 0;
                --Tasque_COUNT;
                --Tasque_ONCARE;
              }
            else
              {







                Thread_Wait(&Tasque_Lock,10);
              }
          }
        Thread_Unlock(&Tasque_Lock);
          
        if ( t )
          {
            t->task(t->obj);

            Thread_Lock(&Tasque_Lock);

            Slist_Remove((YO_TASQUE_TASK**)&Tasque_Task_Care,t);
            Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_Out,t);
            t = 0;
            ++Tasque_ONCARE;


            Thread_Unlock(&Tasque_Lock);
            Thread_Notify(&Tasque_Out_Monitor);
         }
      }
      
    Thread_Notify(&Tasque_Lock);
  }
#endif
  ;

void Tasque_Queue(/*tasque_proc_t*/ void *task, void *obj, /*tasque_update_t*/ void *update, void *monitor)
#ifdef _YO_TASQUE_BUILTIN
  {
    __Thr_Lock(&Tasque_Lock)
      {
        YO_TASQUE_TASK *t = __Object_Dtor(sizeof(YO_TASQUE_TASK),YO_TASQUE_TASK_Destruct);
        t->task = task;
        t->obj  = __Refe(obj);
        t->update = update;
        t->monitor = __Refe(monitor);
        Slist_Push((YO_TASQUE_TASK**)&Tasque_Task_In,__Retain(t));


      }
    
    Thread_Notify(&Tasque_Lock);
    Switch_to_Thread();
  }
#endif
  ;

void Tasque_Alert(int ms, /*tasque_alert_t*/ void *callback, void *obj)
#ifdef _YO_TASQUE_BUILTIN
  {
    YO_TASQUE_ALERT *t = __Object_Dtor(sizeof(YO_TASQUE_ALERT),YO_TASQUE_ALERT_Destruct);
    quad_t deadline = Get_System_Millis() + ms;
    t->deadline = deadline;
    t->callback = callback;
    t->obj = __Refe(obj);
    Slist_Push(&Tasque_Alerts_List,__Retain(t));
  }
#endif
  ;
  
YO_THRSYNC Tasque_Out_Monitor1 = YO_THRSYNC_INIT;  
int Tasque_Perform_Update(int ms, int worktime)
#ifdef _YO_TASQUE_BUILTIN
  {
    int i;
    quad_t deadline = worktime ? Get_System_Millis() + worktime : 0;
    YO_TASQUE_TASK *t;
    YO_TASQUE_ALERT *k;
    YO_TASQUE_ALERT **a; 

    if ( !Tasque_ONCARE && Tasque_COUNT < Tasque_MAX )
      Thread_Run_Close(Tasque_Task_Processor,0,TASQUE_STACK_SIZE);

    if ( !Tasque_Task_Out )
      __Thr_Lock(&Tasque_Out_Monitor)
        {
          if ( Tasque_Task_In || Tasque_Task_Care ) 
            Thread_Wait(&Tasque_Out_Monitor,ms);  
        }
        
    do for ( i=0; i < 10 && Tasque_Task_Out; ++i ) __Auto_Release
      {
        __Thr_Lock(&Tasque_Lock)
          t = __Pool_RefPtr(Slist_Pop((YO_TASQUE_TASK**)&Tasque_Task_Out));
        t->update(t->monitor,t->obj,TASQUE_COMPLETE);
      }
    while (Tasque_Task_Out && ( !deadline || Get_System_Millis() < deadline) ); 
................................................................................
              }
          }
        while ( *a );
      }
    
    __Thr_Lock(&Tasque_Lock)
      i =  !!Tasque_Task_In || !!Tasque_Task_Out || !!Tasque_Task_Care || !!Tasque_Alerts_List;

    return i;
  }
#endif
  ;

void Tasque_Finish_Task_List(YO_TASQUE_TASK * volatile *lst,int status)
#ifdef _YO_TASQUE_BUILTIN
  {
    YO_TASQUE_TASK *t;
    while ( *lst ) __Auto_Release
      {
        t = __Pool_RefPtr(Slist_Pop((YO_TASQUE_TASK**)lst));
        if ( Thread_Is_OK(t->processor) )
          Thread_Terminate(t->processor);
        t->update(t->monitor,t->obj,status);
      }
  }
#endif
  ;
  
................................................................................
  {
    YO_TASQUE_TASK *t;
    Tasque_MAX = 0;
    Tasque_RESERVE = 0;
 
    if ( Tasque_COUNT )
      {
        Thread_Notify(&Tasque_Lock);
        Sleep(30);
      }
          
    __Thr_Lock(&Tasque_Lock)
      {
        Tasque_Finish_Task_List(&Tasque_Task_Care,TASQUE_FAIL);
        Tasque_Finish_Task_List(&Tasque_Task_In,TASQUE_FAIL);
................................................................................
  {
    quad_t t = System_Millis() + ms;
    Tasque_MAX = 0;
    Tasque_RESERVE = 0;
    
    do 
      {
        Thread_Notify(&Tasque_Lock);
        Sleep(10);
      }
    while ( Tasque_ONCARE || t > System_Millis() );

    __Thr_Lock(&Tasque_Lock)
      Tasque_Finish_Task_List(&Tasque_Task_Out,TASQUE_COMPLETE);
    
................................................................................
  }
#endif
  ;



#endif /* C_once_FDCDEF27_5EA4_4C1D_99AB_1CD0A992A18A */

Changes to tcpip.hc.

66
67
68
69
70
71
72



73
74
75
76
77
78
79
...
302
303
304
305
306
307
308




309

310
311
312
313
314
315
316
...
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
...
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
...
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
...
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
...
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
...
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
...
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
...
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
...
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
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  typedef int socket_t;



#endif

#ifdef _LIBYOYO
#define _YO_TCPIP_BUILTIN
#endif

typedef struct ipaddr_t 
................................................................................
  ;
  
void Tcp_Graceful_Close(YO_TCPSOK *sok)
#ifdef _YO_TCPIP_BUILTIN
  {
    if ( sok->skt != INVALID_SOCKET ) 
      {




        shutdown(sok->skt,SD_SEND);

        Tcp_Close(sok);
      }
  }
#endif
  ;

void YO_TCPSOK_Destruct(YO_TCPSOK *sok)
................................................................................
      iot->cbk(iot->obj,TCP_AIO_FAIL);
    else
      iot->cbk(iot->obj,TCP_AIO_COMPLETE);
  }
#endif
  ;
    
void Tcp_Aio_Send(YO_TCPSOK *sok, void *out, int count, tcp_send_callback_t callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release 
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = (tcp_any_callback_t)callback;
        iot->mincount = iot->count = count;
        iot->dta = out;
        iot->obj = __Refe(obj);
        Tasque_Queue(Wrk_Tcp_Send,iot,Cbk_Tcp_Send,sok);
      }
  }
#endif
  ;

void Wrk_Tcp_Recv(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
................................................................................
      iot->cbk(iot->obj,TCP_AIO_FAIL,0);
    else
      iot->cbk(iot->obj,TCP_AIO_COMPLETE,iot->count);
  }
#endif
  ;
    
void Tcp_Aio_Recv(YO_TCPSOK *sok, void *out, int count, int mincount, tcp_recv_callback_t callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release 
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = (tcp_any_callback_t)callback;
        iot->count = count;
        iot->mincount = mincount?mincount:count;
        iot->dta = out;
        iot->obj = __Refe(obj);
        Tasque_Queue(Wrk_Tcp_Recv,iot,Cbk_Tcp_Recv,sok);
      }
  }
#endif
  ;
  
YO_TCPSOK *Tcp_Socket()
#ifdef _YO_TCPIP_BUILTIN  
................................................................................
  }
#endif
  ;
        
#define Tcp_Ip_Connect(Sok,Ip,Port) Tcp_Ip_Connect_(0,Sok,Ip,Port,0,0)
#define Tcp_Aio_Ip_Connect(Sok,Ip,Port,callback,obj) Tcp_Ip_Connect_(1,Sok,Ip,Port,callback,obj)

int Tcp_Ip_Connect_(int use_aio, YO_TCPSOK *sok, ipaddr_t ip, int port, tcp_connect_callback_t callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    socket_t skt;
    int conerr;
    int addr_len = 0;
    IPV4V6_ADDR_MIX addr;
    
................................................................................
      {
        __Auto_Release
          {
            YO_TCP_IOT *iot = Tcp_Alloc_Iot();
            memcpy(&iot->addr,&addr,sizeof(addr));
            iot->count = addr_len;
            iot->skt = sok->skt;
            iot->cbk = callback;
            iot->obj = __Refe(obj);
            Tasque_Queue(Wrk_Tcp_Connect,iot,Cbk_Tcp_Connect,sok);
          }
        return 0;
      }
    else
      {
        conerr = (skt != INVALID_SOCKET ) ? connect(skt,(struct sockaddr*)&addr,addr_len) : -1;
        
................................................................................
        else
          iot->cbk(iot->obj,0,iot->ip);
      }
  }
#endif
  ;
   
void Tcp_Aio_Resolve(char *host, tcp_resolve_callback_t callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    _WSA_Init();

    __Auto_Release
      { 
        YO_TCP_IOT *o = Tcp_Alloc_Iot();
        o->host = Str_Copy_Npl(host,-1);
        o->cbk = callback;
        o->obj = __Refe(obj);
        Tasque_Queue(Wrk_Tcp_Resolve,o,Cbk_Tcp_Resolve,0);
      }
  }
#endif
  ;

typedef struct _YO_HOST_CONNECT
  {
................................................................................
  {
    ipaddr_t ip = Dns_Resolve(host);
    Tcp_Ip_Connect(sok,ip,port);    
  }
#endif
  ;

void Tcp_Aio_Connect(YO_TCPSOK *sok,char *host,int port,tcp_connect_callback_t callback,void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_HOST_CONNECT *o = __Object_Dtor(sizeof(YO_HOST_CONNECT),YO_HOST_CONNECT_Destruct);
    o->sok = __Refe(sok);
    o->port = port;
    o->cbkobj = __Refe(obj);
    o->callback = callback;
    Tcp_Aio_Resolve(host,Cbk_Tcp_Host_Connect,o);
  }
#endif
  ;

YO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YO_TCPIP_BUILTIN
  {
................................................................................
  ;

YO_TCPSOK *Tcp_Listen(char *host, int port, int listlen)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_TCPSOK *sok;
    ipaddr_t ip;
    int addr_len = 0;
    IPV4V6_ADDR_MIX addr;
    
    ip = Dns_Resolve(host);
    Tcp_Expand_Ip_Addr(&ip,port,&addr,&addr_len);
    
    sok = __Refe(Tcp_Socket(0));
    sok->port = port;
    sok->ip = ip;
    
    if ( INVALID_SOCKET == (sok->skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) )  goto sok_error;

    if ( -1 == bind(sok->skt,(struct sockaddr*)&addr,addr_len) ) goto sok_error;
    if ( -1 == listen(sok->skt,(listlen?listlen:SOMAXCONN)) ) goto sok_error;

    if ( sok->skt == INVALID_SOCKET )
      {       
    sok_error: 
        __Raise_Format(YO_ERROR_IO,
................................................................................
  }

void Wrk_Tcp_Accept(YO_TCP_IOT *iot)
  {
    int skt,addr_len = sizeof(iot->addr);
    
    skt = accept(iot->skt,(struct sockaddr*)&iot->addr,&addr_len);
    
    if ( skt != INVALID_SOCKET )
      {
        iot->ip   = Tcp_Sin_Ip(&iot->addr);
        iot->port = ntohs(iot->addr.addr4.sin_port);
        iot->skt  = skt;
        iot->err  = 0;
        iot->count = addr_len;
................................................................................
      {
        iot->skt = INVALID_SOCKET;
        iot->err = Tcp_Errno();
      }
  }
#endif

void Tcp_Aio_Accept(YO_TCPSOK *sok, tcp_accept_callback_t callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = callback;
        iot->obj = __Refe(obj);
        Tasque_Queue(Wrk_Tcp_Accept,iot,Cbk_Tcp_Accept,sok);
      }
  }
#endif
  ;

#define Tcp_Shutdown(Sok) Tcp_Graceful_Close(Sok)

#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */








>
>
>







 







>
>
>
>
|
>







 







|










|







 







|











|







 







|







 







|

|







 







|








|

|







 







|







|







 







|










>







 







<







 







|






|

|









66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
...
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
...
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
...
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
...
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
...
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
...
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
...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
...
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
...
817
818
819
820
821
822
823

824
825
826
827
828
829
830
...
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
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/socket.h>
  #include <netdb.h>
  #include <netinet/in.h>
  #include <arpa/inet.h>
  typedef int socket_t;
  #define INVALID_SOCKET -1
  #define SD_SEND SHUT_WR
  #define SD_BOTH SHUT_RDWR
#endif

#ifdef _LIBYOYO
#define _YO_TCPIP_BUILTIN
#endif

typedef struct ipaddr_t 
................................................................................
  ;
  
void Tcp_Graceful_Close(YO_TCPSOK *sok)
#ifdef _YO_TCPIP_BUILTIN
  {
    if ( sok->skt != INVALID_SOCKET ) 
      {
        struct linger lin;
        lin.l_onoff=1;
        lin.l_linger=3;      
        setsockopt(sok->skt,SOL_SOCKET,SO_LINGER,(void*)&lin,sizeof(lin));
        shutdown(sok->skt,SD_BOTH);
        Switch_to_Thread();
        Tcp_Close(sok);
      }
  }
#endif
  ;

void YO_TCPSOK_Destruct(YO_TCPSOK *sok)
................................................................................
      iot->cbk(iot->obj,TCP_AIO_FAIL);
    else
      iot->cbk(iot->obj,TCP_AIO_COMPLETE);
  }
#endif
  ;
    
void Tcp_Aio_Send(YO_TCPSOK *sok, void *out, int count, /*tcp_send_callback_t*/ void *callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release 
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = (tcp_any_callback_t)callback;
        iot->mincount = iot->count = count;
        iot->dta = out;
        iot->obj = __Refe(obj);
        Tasque_Queue((tasque_proc_t)Wrk_Tcp_Send,iot,(tasque_update_t)Cbk_Tcp_Send,sok);
      }
  }
#endif
  ;

void Wrk_Tcp_Recv(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
................................................................................
      iot->cbk(iot->obj,TCP_AIO_FAIL,0);
    else
      iot->cbk(iot->obj,TCP_AIO_COMPLETE,iot->count);
  }
#endif
  ;
    
void Tcp_Aio_Recv(YO_TCPSOK *sok, void *out, int count, int mincount, /*tcp_recv_callback_t*/ void *callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release 
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = (tcp_any_callback_t)callback;
        iot->count = count;
        iot->mincount = mincount?mincount:count;
        iot->dta = out;
        iot->obj = __Refe(obj);
        Tasque_Queue((tasque_proc_t)Wrk_Tcp_Recv,iot,(tasque_update_t)Cbk_Tcp_Recv,sok);
      }
  }
#endif
  ;
  
YO_TCPSOK *Tcp_Socket()
#ifdef _YO_TCPIP_BUILTIN  
................................................................................
  }
#endif
  ;
        
#define Tcp_Ip_Connect(Sok,Ip,Port) Tcp_Ip_Connect_(0,Sok,Ip,Port,0,0)
#define Tcp_Aio_Ip_Connect(Sok,Ip,Port,callback,obj) Tcp_Ip_Connect_(1,Sok,Ip,Port,callback,obj)

int Tcp_Ip_Connect_(int use_aio, YO_TCPSOK *sok, ipaddr_t ip, int port, /*tcp_connect_callback_t*/ void *callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    socket_t skt;
    int conerr;
    int addr_len = 0;
    IPV4V6_ADDR_MIX addr;
    
................................................................................
      {
        __Auto_Release
          {
            YO_TCP_IOT *iot = Tcp_Alloc_Iot();
            memcpy(&iot->addr,&addr,sizeof(addr));
            iot->count = addr_len;
            iot->skt = sok->skt;
            iot->cbk = (tcp_any_callback_t)callback;
            iot->obj = __Refe(obj);
            Tasque_Queue((tasque_proc_t)Wrk_Tcp_Connect,iot,(tasque_update_t)Cbk_Tcp_Connect,sok);
          }
        return 0;
      }
    else
      {
        conerr = (skt != INVALID_SOCKET ) ? connect(skt,(struct sockaddr*)&addr,addr_len) : -1;
        
................................................................................
        else
          iot->cbk(iot->obj,0,iot->ip);
      }
  }
#endif
  ;
   
void Tcp_Aio_Resolve(char *host, /*tcp_resolve_callback_t*/ void *callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    _WSA_Init();

    __Auto_Release
      { 
        YO_TCP_IOT *o = Tcp_Alloc_Iot();
        o->host = Str_Copy_Npl(host,-1);
        o->cbk = (tcp_any_callback_t)callback;
        o->obj = __Refe(obj);
        Tasque_Queue((tasque_proc_t)Wrk_Tcp_Resolve,o,(tasque_update_t)Cbk_Tcp_Resolve,0);
      }
  }
#endif
  ;

typedef struct _YO_HOST_CONNECT
  {
................................................................................
  {
    ipaddr_t ip = Dns_Resolve(host);
    Tcp_Ip_Connect(sok,ip,port);    
  }
#endif
  ;

void Tcp_Aio_Connect(YO_TCPSOK *sok,char *host,int port, /*tcp_connect_callback_t*/ void *callback,void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_HOST_CONNECT *o = __Object_Dtor(sizeof(YO_HOST_CONNECT),YO_HOST_CONNECT_Destruct);
    o->sok = __Refe(sok);
    o->port = port;
    o->cbkobj = __Refe(obj);
    o->callback = callback;
    Tcp_Aio_Resolve(host,(tcp_resolve_callback_t)Cbk_Tcp_Host_Connect,o);
  }
#endif
  ;

YO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YO_TCPIP_BUILTIN
  {
................................................................................
  ;

YO_TCPSOK *Tcp_Listen(char *host, int port, int listlen)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_TCPSOK *sok;
    ipaddr_t ip;
    int addr_len = 0, on;
    IPV4V6_ADDR_MIX addr;
    
    ip = Dns_Resolve(host);
    Tcp_Expand_Ip_Addr(&ip,port,&addr,&addr_len);
    
    sok = __Refe(Tcp_Socket(0));
    sok->port = port;
    sok->ip = ip;
    
    if ( INVALID_SOCKET == (sok->skt = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) )  goto sok_error;
    setsockopt( sok->skt, SOL_SOCKET, SO_REUSEADDR, (void*)&on, sizeof(on) );
    if ( -1 == bind(sok->skt,(struct sockaddr*)&addr,addr_len) ) goto sok_error;
    if ( -1 == listen(sok->skt,(listlen?listlen:SOMAXCONN)) ) goto sok_error;

    if ( sok->skt == INVALID_SOCKET )
      {       
    sok_error: 
        __Raise_Format(YO_ERROR_IO,
................................................................................
  }

void Wrk_Tcp_Accept(YO_TCP_IOT *iot)
  {
    int skt,addr_len = sizeof(iot->addr);
    
    skt = accept(iot->skt,(struct sockaddr*)&iot->addr,&addr_len);

    if ( skt != INVALID_SOCKET )
      {
        iot->ip   = Tcp_Sin_Ip(&iot->addr);
        iot->port = ntohs(iot->addr.addr4.sin_port);
        iot->skt  = skt;
        iot->err  = 0;
        iot->count = addr_len;
................................................................................
      {
        iot->skt = INVALID_SOCKET;
        iot->err = Tcp_Errno();
      }
  }
#endif

void Tcp_Aio_Accept(YO_TCPSOK *sok, /*tcp_accept_callback_t*/ void *callback, void *obj)
#ifdef _YO_TCPIP_BUILTIN
  {
    __Auto_Release
      {
        YO_TCP_IOT *iot = Tcp_Alloc_Iot();
        iot->skt = sok->skt;
        iot->cbk = (tcp_any_callback_t)callback;
        iot->obj = __Refe(obj);
        Tasque_Queue((tasque_proc_t)Wrk_Tcp_Accept,iot,(tasque_update_t)Cbk_Tcp_Accept,sok);
      }
  }
#endif
  ;

#define Tcp_Shutdown(Sok) Tcp_Graceful_Close(Sok)

#endif /* C_once_F8F16072_F92E_49E7_A983_54F60965F4C9 */

Changes to tests/cgi.c.

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
#include "../cgi.hc"
#include "../httpd.hc"
#include "../httpx.hc"
#include "../webhost.hc"
#include "../prog.hc"
#include "../tesuite.hc"

void test_client_simple(YO_CGI *cgi)
  {
    Tesuite_Case("CGI: test simple");
    Cgi_Append(cgi,"SIMPLE",-1);
    Cgi_Write_Out(cgi,YO_CGI_OUT_TEXTHTML);
    Tesuite_Case_Successed();
  }

void test_client()
  {
    __Auto_Release
      {
        char *test;
        YO_CGI *cgi = Cgi_Init();
        Cgi_Query_Params(cgi,0,0);
        
        test = Xnode_Value_Get_Str(cgi->params,"test",0);
        if ( !test )
          ;




        else if ( !strcmp_I(test,"simple") )
          test_client_simple(cgi); 









      }
  }

void test_server_0()
  {
    Tesuite_Case("client/server: simple GET request");
    __Auto_Release
      {
        int http_status;
        YO_BUFFER *bf = Buffer_Init(0);
        YO_WEBHOST *webhost = Webhost_Init();
        YO_HTTPD *httpd = Httpd_Server(Webhost_Callback,webhost);




        Webhost_Set_Exec_Root(webhost,Prog_Directory());
        Httpd_Listen(httpd,"0.0.0.0",9000);        
        http_status = Httpx_Get(0,"http://localhost:9000/cgi?test=simple",0,Buffer_As_File(bf),0);
        Httpd_Shutdown(httpd);
        while ( Tasque_Perform_Update(0,0) ) ;
        if ( http_status != 200 || !bf->count || !!strcmp(bf->at,"SIMPLE") )
          __Raise_Format(YO_ERROR_TESUITE_FAIL,("response: %d, %s",http_status,bf->at?bf->at:"<null>"));
      }
    Tesuite_Case_Successed();
  }

int main(int argc, char **argv)
  {
    Prog_Init(argc,argv,0,0);
    
    __Try_Abort 
      {
        Tesuite_Init();
   
        if ( getenv("GATEWAY_INTERFACE") ) 
          {
            Tesuite_Perform(test_client());
          }
        else      
          Tesuite_Perform(test_server_0());
      }
  }







|

|


|


|




<
|



<
>
>
>
>

|
>
>
>
>
>
>
>
>
>





|






>
>
>
>

|
|



|

|




|







|





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
#include "../cgi.hc"
#include "../httpd.hc"
#include "../httpx.hc"
#include "../webhost.hc"
#include "../prog.hc"
#include "../tesuite.hc"

void test_cgi_simple(YO_CGI *cgi)
  {
    quad_t cid = Tesuite_Case("CGI: test simple");
    Cgi_Append(cgi,"SIMPLE",-1);
    Cgi_Write_Out(cgi,YO_CGI_OUT_TEXTHTML);
    Tesuite_Case_Successed_Id(cid);
  }

void test_cgi(YO_CGI *cgi)
  {
    __Auto_Release
      {
        char *test;

        cgi = Cgi_Query_Params(cgi,0,0);
        
        test = Xnode_Value_Get_Str(cgi->params,"test",0);
        if ( !test )

          {
            Cgi_Append(cgi,"ERROR",-1);
            Cgi_Write_Out(cgi,YO_CGI_OUT_TEXTHTML);
          }
        else if ( !strcmp_I(test,"simple") )
          test_cgi_simple(cgi); 
      }
  }

void hosted_test_cgi(YO_WEBHOST_CGIST *st, int error)
  {
    if ( !error )
      {
        test_cgi(st->cgi);
        Webhost_Exit_Service(st,0);
      }
  }

void test_server_0()
  {
    quad_t cid = Tesuite_Case("client/server: simple GET request");
    __Auto_Release
      {
        int http_status;
        YO_BUFFER *bf = Buffer_Init(0);
        YO_WEBHOST *webhost = Webhost_Init();
        YO_HTTPD *httpd = Httpd_Server(Webhost_Callback,webhost);

        if ( Prog_Has_Opt("hosted") )
          Webhost_Register_Service(webhost,"/cgi",hosted_test_cgi,0);        
        
        Webhost_Set_Exec_Root(webhost,Prog_Directory());
        Httpd_Listen(httpd,"127.0.0.1",9123);        
        http_status = Httpx_Get(0,"http://localhost:9123/cgi?test=simple",0,Buffer_As_File(bf),0);
        Httpd_Shutdown(httpd);
        while ( Tasque_Perform_Update(0,0) ) ;
        if ( http_status != 200 || !bf->count || !!strcmp(bf->at,"SIMPLE") )
          __Raise_Format(YO_ERROR_TESUITE_FAIL,("response: %d, %s",http_status,(!bf->at?"<null>":(char*)bf->at)));
      }
    Tesuite_Case_Successed_Id(cid);
  }

int main(int argc, char **argv)
  {
    Prog_Init(argc,argv,"hosted",0);
    
    __Try_Abort 
      {
        Tesuite_Init();
   
        if ( getenv("GATEWAY_INTERFACE") ) 
          {
            Tesuite_Perform(test_cgi(0));
          }
        else      
          Tesuite_Perform(test_server_0());
      }
  }

Changes to tests/xddb.c.

101
102
103
104
105
106
107











































































108
109
110
111
112
113
114
115
116
117

118
119
            Xddb_Delete(xddb,key);
            if ( Xddb_Has(xddb,key) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document still present in store");
            Tesuite_Case_Successed();
          }
      }
  }












































































int main(int argc, char **argv)
  {
    Prog_Init(argc,argv,"fs,ldb,sql3,msql",0);
    
    __Try_Abort
      {
        Tesuite_Init();
        Tesuite_Perform(test_0());
        Tesuite_Perform(test_1());

      }
  }







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










>


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
            Xddb_Delete(xddb,key);
            if ( Xddb_Has(xddb,key) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document still present in store");
            Tesuite_Case_Successed();
          }
      }
  }

void test_2()
  {
    __Auto_Release
      {
        YO_XDDB *xddb;
        YO_XDATA *doc;
        void *seq;
        char *key;
        char *unique;

        unique = Xddb_Build_Unique_Key();
        key    = Str_Concat("test:",Xddb_Build_Unique_Key());
        
        __Auto_Ptr(key)
          {
            Tesuite_Case("create db/create document with pfx");            
            xddb = Open_Xddb(XDDB_CREATE_ALWAYS);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
                                        
            doc = Xdata_Init();
            Xnode_Value_Set_Str(&doc->root,"unique",unique);
            Xddb_Overwrite(xddb,doc,key);
            Tesuite_Info(__Format("document key: %s",key));
            Tesuite_Case_Successed();
          }
          
        __Auto_Release
          {
            Tesuite_Case("open db/get document with pfx");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            doc = Xddb_Get(xddb,key,XDDB_RAISE_IF_DSNT_EXSIS);
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {
            Tesuite_Case("open db/search document with pfx");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            seq = Xddb_Seq_Open(xddb,"test:",XDDB_OPEN_EXISTING);
            doc = Xddb_Seq_Next(seq);
            if ( !doc )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not present in prefixed sequence");
            if ( strcmp(Xnode_Value_Get_Str(&doc->root,"unique",""),unique) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document is not consistent");
            Tesuite_Case_Successed();
          }

        __Auto_Release
          {
            Tesuite_Case("open db/delete document pfx");
            xddb = Open_Xddb(XDDB_OPEN_EXISTING);
            Tesuite_Info(__Format("xddb test, source: %s, format: %s",
                                        Xddb_Source_String(xddb),
                                        Xddb_Format_String(xddb)));
            Tesuite_Info(__Format("document key: %s",key));
            Xddb_Delete(xddb,key);
            if ( Xddb_Has(xddb,key) )
              __Raise(YO_ERROR_TESUITE_FAIL,"document still present in store");
            Tesuite_Case_Successed();
          }
      }
  }

int main(int argc, char **argv)
  {
    Prog_Init(argc,argv,"fs,ldb,sql3,msql",0);
    
    __Try_Abort
      {
        Tesuite_Init();
        Tesuite_Perform(test_0());
        Tesuite_Perform(test_1());
        Tesuite_Perform(test_2());
      }
  }

Changes to tesuite.hc.

47
48
49
50
51
52
53


54
55
56
57
58
59
60
..
73
74
75
76
77
78
79
80
81
82
83

84
85
86
87
88
89
90
...
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
#ifndef C_once_22EC0578_5C71_4FE1_9C29_1EE80DFD34A5
#define C_once_22EC0578_5C71_4FE1_9C29_1EE80DFD34A5

#include "yoyo.hc"
#include "datetime.hc"
#include "newdes96.hc"
#include "string.hc"



#ifdef _LIBYOYO
# define _YO_TESUITE_BUILTIN
#endif

#ifdef _YO_TESUITE_BUILTIN
int Tesuite_Case_Log = -1;
................................................................................
    val = (val << 16) | (getpid()&0x0ffff);
    return val;
  }

void Tesuite_Case_Value(quad_t val, quad_t *case_id, char case_sid[15])
#ifdef _YO_TESUITE_BUILTIN
  {
    *case_id = val;
    memset(case_sid,0,sizeof(case_sid));
    NEWDES96_Encrypt8(&Tesuite_Nds96,&val);
    Str_Xbit_Encode(&val,8*8,5,Str_5bit_Encoding_Table_Upper,case_sid);

  }
#endif
  ;

void Tesuite_Init()
#ifdef _YO_TESUITE_BUILTIN
  {
................................................................................
    char format[4096];
    int  L = snprintf(format,sizeof(format),"\n[%s] INFO %s",Tesuite_Subcase_Sid,text);
    write(Tesuite_Case_Log,format,L);
  }
#endif
  ;
 
void Tesuite_Case(char *text)
#ifdef _YO_TESUITE_BUILTIN
  {
    quad_t cid;
    char format[4096];
    int  L;
    Sleep(1);
    cid = Tesuite_Case_Cid();
    Tesuite_Case_Value(cid,&Tesuite_Subcase_Id,Tesuite_Subcase_Sid);
    L = snprintf(format,sizeof(format),"\n[%s] SUBCASE [%s] %s",Tesuite_Subcase_Sid,Tesuite_Case_Sid,text);
    write(Tesuite_Case_Log,format,L);

  }
#endif
  ;

void Tesuite_Case_Successed()
#ifdef _YO_TESUITE_BUILTIN
  {
    char format[128];
    int  L = snprintf(format,sizeof(format),"\n[%s] OK",Tesuite_Subcase_Sid);
    write(Tesuite_Case_Log,format,L);
  }













#endif
  ;

void Tesuite_Case_Fail(char *text)
#ifdef _YO_TESUITE_BUILTIN
  {
    char format[4096];







>
>







 







|
|
|
|
>







 







|










>











>
>
>
>
>
>
>
>
>
>
>
>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
...
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
#ifndef C_once_22EC0578_5C71_4FE1_9C29_1EE80DFD34A5
#define C_once_22EC0578_5C71_4FE1_9C29_1EE80DFD34A5

#include "yoyo.hc"
#include "datetime.hc"
#include "newdes96.hc"
#include "string.hc"
#include "file.hc"
#include "prog.hc"

#ifdef _LIBYOYO
# define _YO_TESUITE_BUILTIN
#endif

#ifdef _YO_TESUITE_BUILTIN
int Tesuite_Case_Log = -1;
................................................................................
    val = (val << 16) | (getpid()&0x0ffff);
    return val;
  }

void Tesuite_Case_Value(quad_t val, quad_t *case_id, char case_sid[15])
#ifdef _YO_TESUITE_BUILTIN
  {
    quad_t tmp = val;
    memset(case_sid,0,15);
    NEWDES96_Encrypt8(&Tesuite_Nds96,&tmp);
    Str_Xbit_Encode(&tmp,8*8,5,Str_5bit_Encoding_Table_Upper,case_sid);
    if ( case_id ) *case_id = val;
  }
#endif
  ;

void Tesuite_Init()
#ifdef _YO_TESUITE_BUILTIN
  {
................................................................................
    char format[4096];
    int  L = snprintf(format,sizeof(format),"\n[%s] INFO %s",Tesuite_Subcase_Sid,text);
    write(Tesuite_Case_Log,format,L);
  }
#endif
  ;
 
quad_t Tesuite_Case(char *text)
#ifdef _YO_TESUITE_BUILTIN
  {
    quad_t cid;
    char format[4096];
    int  L;
    Sleep(1);
    cid = Tesuite_Case_Cid();
    Tesuite_Case_Value(cid,&Tesuite_Subcase_Id,Tesuite_Subcase_Sid);
    L = snprintf(format,sizeof(format),"\n[%s] SUBCASE [%s] %s",Tesuite_Subcase_Sid,Tesuite_Case_Sid,text);
    write(Tesuite_Case_Log,format,L);
    return cid;
  }
#endif
  ;

void Tesuite_Case_Successed()
#ifdef _YO_TESUITE_BUILTIN
  {
    char format[128];
    int  L = snprintf(format,sizeof(format),"\n[%s] OK",Tesuite_Subcase_Sid);
    write(Tesuite_Case_Log,format,L);
  }
#endif
  ;

void Tesuite_Case_Successed_Id(quad_t cid)
#ifdef _YO_TESUITE_BUILTIN
  {
    int L;
    char format[128];
    char tmp[15];
    Tesuite_Case_Value(cid,0,tmp);
    L = snprintf(format,sizeof(format),"\n[%s] OK",tmp);
    write(Tesuite_Case_Log,format,L);
  }
#endif
  ;

void Tesuite_Case_Fail(char *text)
#ifdef _YO_TESUITE_BUILTIN
  {
    char format[4096];

Changes to threads.hc.

45
46
47
48
49
50
51
52
53
54

55
56





57
58
59
60
61
62
63
64
65
66
67
..
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


#ifndef C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614
#define C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614

#include "yoyo.hc"

#ifdef __windoze
typedef HANDLE thread_t;
#define INVALID_THREAD_VALIUE ((HANDLE)0)

#else
typedef pthread_t thread_t;





#endif

#ifdef _LIBYOYO
#define _YO_THREADS_BUILTIN
#endif

enum 
  {
    THREAD_STILL_ACTIVE = -1,
  };

................................................................................
static YO_THREAD_COOKIE *_Thread_Allocate_Cookie(thread_func_t func, void *o) 
  {
    YO_THREAD_COOKIE *cookie = malloc(sizeof(YO_THREAD_COOKIE));
    cookie->func = func;
    cookie->o = o;
    return cookie;
  }








static DWORD __stdcall _Thread_Proc(YO_THREAD_COOKIE *cookie)
  {
    thread_func_t func = cookie->func;
    void *o = cookie->o;    
    free(cookie);
    func(o);
    return 0;
  }
#endif

thread_t Thread_Get_Current()
#ifdef _YO_THREADS_BUILTIN
  {
  #ifdef __windoze
    return GetCurrentThread();
  #else

  #endif
  }
#endif;


thread_t Thread_Run(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    YO_THREAD_COOKIE *cookie = _Thread_Allocate_Cookie(func,o);
  #ifdef __windoze
    thread_t thr = CreateThread(0,stacksize,_Thread_Proc,cookie,0,0);
    if ( thr == INVALID_THREAD_VALIUE )
      _Thread_Release_Cookie(cookie);
    return thr;
  #else














  #endif

  }
#endif
  ;
 
#ifdef _YO_THREADS_BUILTIN
static void __No_Return _Thread_Raise_Error()
  {
    __Raise_System_Error();
  }
#endif
    
/* be sure thread is running */
thread_t Thread_Run_Sure(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    thread_t thr = Thread_Run(func,o,stacksize);
    if ( thr != INVALID_THREAD_VALIUE )
      {
    #ifdef __windoze
        //WaitForSingleObject(thr,INFINITE);
    #else
    #endif  
      }
    else
      _Thread_Raise_Error();
    return thr;
  }
#endif
  ;
  















void Thread_Run_Close(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    thread_t thr = Thread_Run(func,o,stacksize);
    if ( thr != INVALID_THREAD_VALIUE )
      Thread_Close(thr);
    else
      _Thread_Raise_Error();
  }
#endif
  ;
  
int Thread_Close(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( thr != INVALID_THREAD_VALIUE )
      {
      #ifdef __windoze
        DWORD ecode;
        GetExitCodeThread(thr,&ecode);
        CloseHandle(thr);
        if ( ecode == STILL_ACTIVE ) ecode = THREAD_STILL_ACTIVE;
        return ecode;
      #else
      #endif
      }

    return 0;    
  }
#endif
  ;
    
int Thread_Join(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( thr != INVALID_THREAD_VALIUE )
      {
    #ifdef __windoze
        for(;;)
          {
            DWORD ecode;
            if ( GetExitCodeThread(thr,&ecode) )
              if ( ecode != STILL_ACTIVE )
                return ecode;
            WaitForSingleObject(thr,100);
          }
    #else


    #endif  
      }
      
    return 0;
  }
#endif
  ;
  
void __No_Return Thread_Exit(int ecode)
#ifdef _YO_THREADS_BUILTIN
  {
    #ifdef __windoze
      ExitThread(ecode);
    #else
    #endif
  }
#endif
  ;
    
void Thread_Terminate(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( thr != INVALID_THREAD_VALIUE )
      {
      #ifdef __windoze
        TerminateThread(thr,-1);
        CloseHandle(thr);
      #else



      #endif
      }
  } 
#endif
  ;
     
#ifdef __windoze


typedef struct _YO_THRSYNC
  {
    CRITICAL_SECTION *cs;



    HANDLE notify;

  } YO_THRSYNC;
  
void Thrsync_Free(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      {
        DeleteCriticalSection(l->cs);
        free(l->cs);
        l->cs = 0;
      }
    if ( l->notify )
      {



        CloseHandle(l->notify);
        l->notify = 0;


      }
  }
#endif
  ;

void Thrsync_Init_CS(CRITICAL_SECTION * volatile *cs)
#ifdef _YO_THREADS_BUILTIN
  {
    CRITICAL_SECTION *ccs = malloc(sizeof(CRITICAL_SECTION));
    InitializeCriticalSection(ccs);
    if ( 0 != InterlockedCompareExchangePointer(cs,ccs,0) )
      {
        DeleteCriticalSection(ccs);
        free(ccs);
      }
  }
#endif
  ;
  
void Thrsync_Init_Notify(HANDLE volatile *notify)
#ifdef _YO_THREADS_BUILTIN
  {






    HANDLE h = CreateEvent(0,0,0,0);
    if ( 0 != InterlockedCompareExchangePointer(notify,h,0) )
      {
        CloseHandle(h);
      }

  }
#endif
  ;

void Nolock_Wait(YO_THRSYNC *l, long ms)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->notify ) Thrsync_Init_Notify(&l->notify);





    WaitForSingleObject(l->notify,ms>=0?ms:INFINITE);















  }
#endif
  ;

void Nolock_Notify(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->notify ) Thrsync_Init_Notify(&l->notify);
    SetEvent(l->notify);
  }
#endif
  ;

void Thrsync_Acquire(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->cs ) Thrsync_Init_CS(&l->cs);
    EnterCriticalSection(l->cs);
  }
#endif
  ;

void Thrsync_Release(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      LeaveCriticalSection(l->cs);
  }
#endif
  ;


























































void Unlock_Wait_Lock(YO_THRSYNC *l, long ms)
#ifdef _YO_THREADS_BUILTIN
  {
    Thrsync_Release(l);
    Nolock_Wait(l,ms);
    Thrsync_Acquire(l);































  }
#endif
  ;

void Unlock_Notify_Lock(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    Thrsync_Release(l);
    Nolock_Notify(l);
    Switch_to_Thread();
    Thrsync_Acquire(l);
  }
#endif
  ;




void YO_THRSYNC_Destruct(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    Thrsync_Free(l);
    __Destruct(l);
  }
#endif
  ;
  
YO_THRSYNC *Thrsync_Init()
#ifdef _YO_THREADS_BUILTIN
  {
    return __Object_Dtor(sizeof(YO_THRSYNC),YO_THRSYNC_Destruct);
  }
#endif
  ;
  
#define YO_THRSYNC_INIT { 0, 0}

/* __windoze way ends here */

#endif 

#define __Thr_Lock(l) \
              __Interlock_Opt(((void)0),l, \
                  Thrsync_Acquire,Thrsync_Release,Thrsync_Release)

#endif /*C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614*/







|

|
>


>
>
>
>
>



|







 







>
>
>
>
>
>
>
>
|



|








|


>


|
>





|
|
|

<

>
>
>
>
>
>
>
>
>
>
>
>
>
>

>










|





|













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|

|









>
>





<
<
<
<
<
<
<
<
<
<
<






|

|



>
>
>






|
>




>
>
>
|
>


|








|

>
>
>
|
<
>
>





|













|


>
>
>
>
>
>

|
<
|
<
>




|


|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|


|
<
<
<
<
<
<
<
<
|





|








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|


<
<
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|


|
|
<
<



>
>
>




|





|







|
<
<
<
<



|


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
..
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


#ifndef C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614
#define C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614

#include "yoyo.hc"

#if defined __windoze && !defined _PTHREADS
typedef HANDLE thread_t;
# define INVALID_THREAD_VALUE ((HANDLE)0)
#define Thread_Is_OK(Thr) ( Thr != INVALID_THREAD_VALUE )
#else
typedef pthread_t thread_t;
static pthread_t INVALID_THREAD_VALUE; // zero initialized
#define Thread_Is_OK(Thr) ( 0 != memcmp(&Thr,&INVALID_THREAD_VALUE,sizeof(pthread_t)) )
# ifndef INFINITE
#  define INFINITE -1 
# endif
#endif

#ifdef _LIBYOYO
# define _YO_THREADS_BUILTIN
#endif

enum 
  {
    THREAD_STILL_ACTIVE = -1,
  };

................................................................................
static YO_THREAD_COOKIE *_Thread_Allocate_Cookie(thread_func_t func, void *o) 
  {
    YO_THREAD_COOKIE *cookie = malloc(sizeof(YO_THREAD_COOKIE));
    cookie->func = func;
    cookie->o = o;
    return cookie;
  }

#if defined __windoze && !defined _PTHREADS 
#define _THREAD_START_STDCALL_DECL __stdcall
typedef unsigned (__stdcall *thread_proc_t)(void*);
#else
#define _THREAD_START_STDCALL_DECL
typedef void *(*thread_proc_t)(void*);
#endif
static int _THREAD_START_STDCALL_DECL _Thread_Proc(YO_THREAD_COOKIE *cookie)
  {
    thread_func_t func = cookie->func;
    void *o = cookie->o;    
    _Thread_Release_Cookie(cookie);
    func(o);
    return 0;
  }
#endif

thread_t Thread_Get_Current()
#ifdef _YO_THREADS_BUILTIN
  {
  #if defined __windoze && !defined _PTHREADS
    return GetCurrentThread();
  #else
    return pthread_self();
  #endif
  }
#endif
  ;

thread_t Thread_Run(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    YO_THREAD_COOKIE *cookie = _Thread_Allocate_Cookie(func,o);
  #if defined __windoze && !defined _PTHREADS
    thread_t thr = (thread_t)_beginthreadex(0,stacksize,(thread_proc_t)_Thread_Proc,cookie,0,0);
    if ( thr == INVALID_THREAD_VALUE )
      _Thread_Release_Cookie(cookie);

  #else
    int err;
    thread_t thr;
    pthread_attr_t att;

    memset(&INVALID_THREAD_VALUE,-1,sizeof(INVALID_THREAD_VALUE));
    pthread_attr_init(&att);
    pthread_attr_setstacksize(&att,stacksize);
    err = pthread_create(&thr,&att,(thread_proc_t)_Thread_Proc,cookie);
    pthread_attr_destroy(&att);
    if ( err )
      {
        _Thread_Release_Cookie(cookie);
        memset(&thr,-1,sizeof(thr));
      }
  #endif
    return thr;
  }
#endif
  ;
 
#ifdef _YO_THREADS_BUILTIN
static void __No_Return _Thread_Raise_Error()
  {
    __Raise_System_Error();
  }
#endif
        
/* be sure thread is running */
thread_t Thread_Run_Sure(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    thread_t thr = Thread_Run(func,o,stacksize);
    if ( Thread_Is_OK(thr) )
      {
    #ifdef __windoze
        //WaitForSingleObject(thr,INFINITE);
    #else
    #endif  
      }
    else
      _Thread_Raise_Error();
    return thr;
  }
#endif
  ;
  
void Thread_Close(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( Thread_Is_OK(thr) )
      {
      #if defined __windoze && !defined _PTHREADS
        CloseHandle(thr);
      #else
        pthread_detach(thr);
      #endif
      }
  }
#endif
  ;

void Thread_Run_Close(thread_func_t func, void *o, int stacksize)
#ifdef _YO_THREADS_BUILTIN
  {
    thread_t thr = Thread_Run(func,o,stacksize);
    if ( Thread_Is_OK(thr) )
      Thread_Close(thr);
    else
      _Thread_Raise_Error();
  }
#endif
  ;
  



















    
int Thread_Join(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( Thread_Is_OK(thr) )
      {
    #if defined __windoze && !defined _PTHREADS
        for(;;)
          {
            DWORD ecode;
            if ( GetExitCodeThread(thr,&ecode) )
              if ( ecode != STILL_ACTIVE )
                return ecode;
            WaitForSingleObject(thr,100);
          }
    #else
        void *r;
        pthread_join(thr,&r);
    #endif  
      }
      
    return 0;
  }











#endif
  ;
    
void Thread_Terminate(thread_t thr)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( Thread_Is_OK(thr) )
      {
      #if defined __windoze && !defined _PTHREADS
        TerminateThread(thr,-1);
        CloseHandle(thr);
      #else
        void *r;
        pthread_cancel(thr);
        pthread_join(thr,&r);
      #endif
      }
  } 
#endif
  ;
     
#if defined __windoze && !defined _PTHREADS
#define _USECONDWAIT

typedef struct _YO_THRSYNC
  {
    CRITICAL_SECTION *cs;
  #ifdef _USECONDWAIT
    CONDITION_VARIABLE *condwait;
  #else
    HANDLE condwait;
  #endif
  } YO_THRSYNC;
  
void Thread_Free(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      {
        DeleteCriticalSection(l->cs);
        free(l->cs);
        l->cs = 0;
      }
    if ( l->condwait )
      {
      #ifdef _USECONDWAIT
        free(l->condwait);
      #else
        CloseHandle(l->condwait);

      #endif
        l->condwait = 0;
      }
  }
#endif
  ;

void Thread_Init_CS(CRITICAL_SECTION * volatile *cs)
#ifdef _YO_THREADS_BUILTIN
  {
    CRITICAL_SECTION *ccs = malloc(sizeof(CRITICAL_SECTION));
    InitializeCriticalSection(ccs);
    if ( 0 != InterlockedCompareExchangePointer(cs,ccs,0) )
      {
        DeleteCriticalSection(ccs);
        free(ccs);
      }
  }
#endif
  ;
  
void Thread_Init_Condwait(void * volatile *condwait)
#ifdef _YO_THREADS_BUILTIN
  {
  #ifdef _USECONDWAIT
    CONDITION_VARIABLE *tmp = malloc(sizeof(CONDITION_VARIABLE));
    InitializeConditionVariable(tmp);
    if ( 0 != InterlockedCompareExchangePointer(condwait,tmp,0) )
      free(tmp);
  #else
    HANDLE h = CreateEvent(0,0,0,0);
    if ( 0 != InterlockedCompareExchangePointer(condwait,h,0) )

      CloseHandle(h);

  #endif
  }
#endif
  ;

void Thread_Wait(YO_THRSYNC *l, long ms)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->cs ) Thread_Init_CS(&l->cs);
    if ( !l->condwait ) Thread_Init_Condwait(&l->condwait);
  #ifdef _USECONDWAIT
    SleepConditionVariableCS(l->condwait,l->cs,ms);
  #else
    LeaveCriticalSection(l->cs);
    WaitForSingleObject(l->condwait,ms>=0?ms:INFINITE);
    EnterCriticalSection(l->cs);
  #endif
  }
#endif
  ;

void Thread_Notify(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->condwait ) Thread_Init_Condwait(&l->condwait);
  #ifdef _USECONDWAIT
    WakeConditionVariable(l->condwait);
  #else
    SetEvent(l->condwait);
  #endif
  }
#endif
  ;

void Thread_Lock(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->cs ) 








      Thread_Init_CS(&l->cs);
    EnterCriticalSection(l->cs);
  }
#endif
  ;

void Thread_Unlock(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      LeaveCriticalSection(l->cs);
  }
#endif
  ;

/* __windoze way ends here */

#else /* POSIX */

typedef struct _YO_THRSYNC
  {
    pthread_mutex_t *cs;
    pthread_cond_t  *condwait;
  } YO_THRSYNC;
  
void Thread_Free(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      {
        pthread_mutex_destroy(l->cs);
        free(l->cs);
        l->cs = 0;
      }
    if ( l->condwait )
      {
        pthread_cond_destroy(l->condwait);
        free(l->condwait);
        l->condwait = 0;
      }
  }
#endif
  ;

void Thread_Init_CS(pthread_mutex_t * volatile *cs)
#ifdef _YO_THREADS_BUILTIN
  {
    pthread_mutex_t *ccs = malloc(sizeof(pthread_mutex_t));
    pthread_mutex_init(ccs,0);
    if ( !Atomic_CmpXchg_Ptr(cs,ccs,0) )
      {
        pthread_mutex_destroy(ccs);
        free(ccs);
      }
  }
#endif
  ;
  
void Thread_Init_Condwait(pthread_cond_t * volatile *condwait)
#ifdef _YO_THREADS_BUILTIN
  {
    pthread_cond_t *cnd = malloc(sizeof(pthread_cond_t));
    pthread_cond_init(cnd,0);
    if ( !Atomic_CmpXchg_Ptr(condwait,cnd,0) )
      {
        pthread_cond_destroy(cnd);
        free(cnd);
      }
  }
#endif
  ;

void Thread_Wait(YO_THRSYNC *l, long ms)
#ifdef _YO_THREADS_BUILTIN
  {



    if ( !l->condwait ) Thread_Init_Condwait(&l->condwait);
    if ( !l->cs ) Thread_Init_CS(&l->cs); 
    if ( ms == INFINITE )
      pthread_cond_wait(l->condwait,l->cs);
    else
      {
        quad_t us = Get_System_Useconds() + ((quad_t)ms * 1000);
        struct timespec ts;
        ts.tv_nsec = 0;//(us%1000000)*1000;
        ts.tv_sec = 0;//us/(1000000);
        pthread_cond_timedwait(l->condwait,l->cs,&ts);
      }
  }
#endif
  ;

void Thread_Notify(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->condwait ) Thread_Init_Condwait(&l->condwait);
    pthread_cond_signal(l->condwait);
  }
#endif
  ;

void Thread_Lock(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( !l->cs ) 
      Thread_Init_CS(&l->cs);
    pthread_mutex_lock(l->cs);
  }
#endif
  ;

void Thread_Unlock(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    if ( l->cs )
      pthread_mutex_unlock(l->cs);


  }
#endif
  ;

#endif 


void YO_THRSYNC_Destruct(YO_THRSYNC *l)
#ifdef _YO_THREADS_BUILTIN
  {
    Thread_Free(l);
    __Destruct(l);
  }
#endif
  ;
  
YO_THRSYNC *Thread_Init()
#ifdef _YO_THREADS_BUILTIN
  {
    return __Object_Dtor(sizeof(YO_THRSYNC),YO_THRSYNC_Destruct);
  }
#endif
  ;
  
#define YO_THRSYNC_INIT { 0, 0 }





#define __Thr_Lock(l) \
              __Interlock_Opt(((void)0),l, \
                  Thread_Lock,Thread_Unlock,Thread_Unlock)

#endif /*C_once_7BF4D1A1_7ED4_4CFB_AA04_0E72090D6614*/

Changes to webhost.hc.

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
...
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
...
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
...
360
361
362
363
364
365
366


367
368
369
370
371
372
373
...
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432
433
434
435
...
446
447
448
449
450
451
452


453
454
455
456

457

458
459
460
461
462
463
464
...
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
*/

#ifndef C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE
#define C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE

#include "httpd.hc"
#include "mime.hc"
#include "pipex.hc"
#include "logout.hc"

#ifdef _LIBYOYO
#define _YO_WEBHOST_BUILTIN
#endif








enum { WEBHOST_VERSION = 100 };





















typedef struct _YO_WEBHOST
  {
    char *doc_root;
    char *exec_root;
    YO_ARRAY *indexes;

    
    int disable_index: 1;
    
  } YO_WEBHOST;
  
void YO_WEBHOST_Destruct(YO_WEBHOST *host)
#ifdef _YO_WEBHOST_BUILTIN
  {
    __Unrefe(host->indexes);
    free(host->doc_root);
    free(host->exec_root);
    __Destruct(host);
  }




























#endif
  ;
  
YO_WEBHOST *Webhost_Init()
#ifdef _YO_WEBHOST_BUILTIN
  {
    YO_WEBHOST *host = __Object_Dtor(sizeof(YO_WEBHOST),YO_WEBHOST_Destruct);
................................................................................
      }
      
    return fpath;    
  }
#endif
  ;
    
void Webhost_Setup_Environment(YO_WEBHOST *host, YO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YO_URL *url)
#ifdef _YO_WEBHOST_BUILTIN
  {

    __Auto_Release
      {    
        char *method = 0;
        

        putenv("GATEWAY_INTERFACE=CGI/1.1");

        putenv("SERVER_PROTOCOL=HTTP/1.1");
        putenv(__Format("SERVER_SOFTWARE=YOYO/WEBHOST/%d",WEBHOST_VERSION));
        putenv(Str_Concat("SCRIPT_NAME=",script));
        putenv(Str_Concat("SCRIPT_FILENAME=",path));
        putenv(Str_Concat("PATH_INFO=",pathinfo));
        putenv(Str_Concat("QUERY_STRING=",url->args));
        
        if ( rqst->instrm_type )
          {
            putenv(__Format("CONTENT_LENGTH=%d",rqst->instrm_length));
            putenv(Str_Concat("CONTENT_TYPE=",rqst->instrm_type));
          }
        else
          {
            putenv("CONTENT_LENGTH=0");
            putenv("CONTENT_TYPE=text/html");
          }
          
        switch(rqst->method)
          {
            case HTTPD_GET_METHOD:  method = "GET";  break;
            case HTTPD_POST_METHOD: method = "POST"; break;
            case HTTPD_HEAD_METHOD: method = "HEAD"; break;
            case HTTPD_PUT_METHOD:  method = "PUT";  break;
          }
        
        putenv(Str_Concat("REQUEST_METHOD=",method));
        putenv(Str_Concat("REMOTE_ADDR=",rqst->remote_addr));
        putenv(Str_Concat("REMOTE_HOST=",rqst->remote_addr));
        putenv(Str_Concat("REQUEST_URI=",url->uri));
        putenv(Str_Concat("DOCUMENT_ROOT=",host->doc_root));
        
        putenv(Str_Concat("HTTP_REFERRER=",Dicto_Get(rqst->qhdr,"REFERRER","")));
        if ( Dicto_Has(rqst->qhdr,"COOKIE") )
          putenv(Str_Concat("HTTP_COOKIE=",Dicto_Get(rqst->qhdr,"COOKIE","")));
        
        //putenv("HTTP_USER_AGENT");
        //putenv("HTTP_ACCEPT_LANGUAGE");

        //putenv("SERVER_NAME");
        //putenv("SERVER_PORT");

      } 

  }
#endif
  ;
  





































int Webhost_CGI_Headers(YO_HTTPD_REQUEST *rqst, void *strm)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int retcode = HTTPD_SUCCESS;
    for (;;)
      {
        static char s_Content_Type[] = "Content-Type:";
................................................................................
          }
      }
    return retcode;
  }
#endif
  ;
    
typedef struct _YO_WEBHOST_CGIST
  {
    YO_WEBHOST *host;
    YO_HTTPD_REQUEST *rqst;
    YO_URL *url;
    YO_PIPEX *pipex;
    char *path;
    char *script;
    char *pathinfo;
  } YO_WEBHOST_CGIST;

void YO_WEBHOST_CGIST_Destruct(YO_WEBHOST_CGIST *st)
#ifdef _YO_WEBHOST_BUILTIN
  {
    __Unrefe(st->host);
    __Unrefe(st->url);
    __Unrefe(st->rqst);
    __Unrefe(st->pipex);
    free(st->path);
    __Destruct(st);























  }
#endif
  ;
  
YO_WEBHOST_CGIST *Webhost_CGI_State(YO_WEBHOST *host, YO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YO_URL *url)
#ifdef _YO_WEBHOST_BUILTIN
  {
................................................................................
    st->path = path;
    st->script = script;
    st->pathinfo = pathinfo;
    return st;
  }
#endif
  ;


    
void Webhost_Check_CGI_Status(YO_WEBHOST_CGIST *st, int error)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int retcode = 0;
    
    if ( error ) return;
................................................................................
    
    __Auto_Release 
      {
        char *curdir = Current_Directory();
        
        __Try_Except
          {

            Change_Directory(Path_Dirname(st->path));
            Webhost_Setup_Environment(st->host,st->rqst,st->path,st->script,st->pathinfo,st->url);
            
            st->pipex = __Refe(Pipex_Exec(Path_Basename(st->path),0,PIPEX_TMPFILE,PIPEX_NONE));
            
            if ( st->rqst->instrm )
              {
                int c = Oj_Copy_File(st->rqst->instrm,st->pipex->fin);
                Oj_Flush(st->pipex->fin);
                Oj_Close(st->pipex->fin);
              }
................................................................................
        Change_Directory(curdir);
      }
      
    return retcode;
  }
#endif
  ;


    
int Webhost_Callback(void *_host, YO_HTTPD_REQUEST *rqst, int status)
#ifdef _YO_WEBHOST_BUILTIN
  {

    YO_WEBHOST *host = _host;

    
    if ( status == HTTPD_RQST_CONNECT )
      {
        Log_Debug("[%p] CONNECT %s:%d",rqst,rqst->remote_addr,rqst->remote_port);
        return HTTPD_ACCEPT;
      }
    else if ( status == HTTPD_RQST_POSTDTA )
................................................................................

        Log_Info("[%p] %s/1.%d %s",rqst,Httpd_Method_String(rqst->method),rqst->httpver,rqst->uri);
        Logout_Debug(Dicto_Format(rqst->qhdr,Buffer_Print,0,1));
        
        if ( rqst->instrm_length && Log_Level(YO_LOG_INFO) )
          {
            enum { MAX_L = 60 };
            int i, L = Yo_Minu(MAX_L,rqst->instrm_length);
            byte_t *b = __Zero_Malloc(MAX_L+1);
            Oj_Seek(rqst->instrm,0,0);
            Oj_Read(rqst->instrm,b,L,L);
            Oj_Seek(rqst->instrm,0,0);
            for ( i = 0; i < L; ++i ) if ( b[i] < 30 || b[i] > 127 ) b[i] = '.';
            if ( rqst->instrm_length > MAX_L ) memcpy(b+MAX_L-3,">>>",3);
            Log_Info("[%p] POSTDTA %s",rqst,b);
          }
        













        if ( host->exec_root )
          {
            char *script = 0, *pathinfo = 0;
            path = Webhost_Find_Executable(host->exec_root,url->query,&script,&pathinfo);
            if ( path )
              return Webhost_Execute_CGI(Webhost_CGI_State(host, rqst, __Retain(path), script, pathinfo, url));
          }

          
        path = Path_Join(host->doc_root,url->query);
        path = Path_Normposix(path);
        
        if ( File_Get_Stats(path,&stats,1)->f.exists )
          {
            if ( stats.f.is_directory )
              {
                int i;
                for ( i = 0; i < host->indexes->count; ++i )
                  {
                    char *path1 = Path_Join(path,host->indexes->at[i]);
                    if ( File_Get_Stats(path1,&stats,1)->f.exists )
                      {
                        path = path1;
                        goto open_file;







|
|





>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>


<










>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|


>
|


<
>
|
>
|
|
|
|
|
|



|
|



|
|










|
|
|
|
|
<
|

|

|
|
<
|
|
>

>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







 







>

|

|







 







>
>




>

>







 







|









>
>
>
>
>
>
>
>
>
>
>
>
>


<




>








<







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
...
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
...
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
...
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
...
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
...
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
*/

#ifndef C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE
#define C_once_47E24D4B_C225_4002_B318_6FB9DDE274BE

#include "httpd.hc"
#include "mime.hc"
#include "logout.hc"
#include "cgi.hc"

#ifdef _LIBYOYO
#define _YO_WEBHOST_BUILTIN
#endif

#ifdef __windoze
#include "pipex.hc"
#else
struct _YO_PIPEX;
typedef struct _YO_PIPEX YO_PIPEX;
#endif

enum { WEBHOST_VERSION = 100 };

struct _YO_WEBHOST_CGIST;
typedef void (*webhost_callback_t)(struct _YO_WEBHOST_CGIST *st,int error);

typedef struct _YO_WEBHOST_SERVICE
  {
    char *prefix;
    void *obj;
    webhost_callback_t callback;
  } YO_WEBHOST_SERVICE;  
  
void YO_WEBHOST_SERVICE_Destruct(YO_WEBHOST_SERVICE *svc)
#ifdef _YO_WEBHOST_BUILTIN
  {
    free(svc->prefix);
    __Unrefe(svc->obj);
    __Destruct(svc);
  }
#endif
  ;
    
typedef struct _YO_WEBHOST
  {
    char *doc_root;
    char *exec_root;
    YO_ARRAY *indexes;
    YO_ARRAY *services;
    
    int disable_index: 1;

  } YO_WEBHOST;
  
void YO_WEBHOST_Destruct(YO_WEBHOST *host)
#ifdef _YO_WEBHOST_BUILTIN
  {
    __Unrefe(host->indexes);
    free(host->doc_root);
    free(host->exec_root);
    __Destruct(host);
  }
#endif
  ;
  
typedef struct _YO_WEBHOST_CGIST
  {
    YO_WEBHOST *host;
    YO_HTTPD_REQUEST *rqst;
    YO_URL *url;
    YO_PIPEX *pipex;
    YO_WEBHOST_SERVICE *svc;
    YO_CGI *cgi;
    char *path;
    char *script;
    char *pathinfo;
  } YO_WEBHOST_CGIST;

void YO_WEBHOST_CGIST_Destruct(YO_WEBHOST_CGIST *st)
#ifdef _YO_WEBHOST_BUILTIN
  {
    __Unrefe(st->host);
    __Unrefe(st->url);
    __Unrefe(st->rqst);
    __Unrefe(st->pipex);
    __Unrefe(st->svc);
    __Unrefe(st->cgi);
    free(st->path);
    __Destruct(st);
  }
#endif
  ;
  
YO_WEBHOST *Webhost_Init()
#ifdef _YO_WEBHOST_BUILTIN
  {
    YO_WEBHOST *host = __Object_Dtor(sizeof(YO_WEBHOST),YO_WEBHOST_Destruct);
................................................................................
      }
      
    return fpath;    
  }
#endif
  ;
    
YO_ARRAY *Webhost_Setup_Environment(YO_WEBHOST *host, YO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YO_URL *url)
#ifdef _YO_WEBHOST_BUILTIN
  {
    YO_ARRAY *env;
    __Auto_Ptr(env)
      {    
        char *method = 0;

        env = Array_Pchars();
        Array_Push(env,Str_Copy_Npl("GATEWAY_INTERFACE=CGI/1.1",-1));
        Array_Push(env,Str_Copy_Npl("GATEWAY_INTERFACE=CGI/1.1",-1));
        Array_Push(env,Str_Copy_Npl("SERVER_PROTOCOL=HTTP/1.1",-1));
        Array_Push(env,__Format_Npl("SERVER_SOFTWARE=YOYO/WEBHOST/%d",WEBHOST_VERSION));
        Array_Push(env,Str_Concat_Npl("SCRIPT_NAME=",script));
        Array_Push(env,Str_Concat_Npl("SCRIPT_FILENAME=",path));
        Array_Push(env,Str_Concat_Npl("PATH_INFO=",pathinfo));
        Array_Push(env,Str_Concat_Npl("QUERY_STRING=",url->args));
        
        if ( rqst->instrm_type )
          {
            Array_Push(env,__Format_Npl("CONTENT_LENGTH=%d",rqst->instrm_length));
            Array_Push(env,Str_Concat_Npl("CONTENT_TYPE=",rqst->instrm_type));
          }
        else
          {
            Array_Push(env,Str_Copy_Npl("CONTENT_LENGTH=0",-1));
            Array_Push(env,Str_Copy_Npl("CONTENT_TYPE=text/html",-1));
          }
          
        switch(rqst->method)
          {
            case HTTPD_GET_METHOD:  method = "GET";  break;
            case HTTPD_POST_METHOD: method = "POST"; break;
            case HTTPD_HEAD_METHOD: method = "HEAD"; break;
            case HTTPD_PUT_METHOD:  method = "PUT";  break;
          }
        
        Array_Push(env,Str_Concat_Npl("REQUEST_METHOD=",method));
        Array_Push(env,Str_Concat_Npl("REMOTE_ADDR=",rqst->remote_addr));
        Array_Push(env,Str_Concat_Npl("REMOTE_HOST=",rqst->remote_addr));
        Array_Push(env,Str_Concat_Npl("REQUEST_URI=",url->uri));
        Array_Push(env,Str_Concat_Npl("DOCUMENT_ROOT=",host->doc_root));

        Array_Push(env,Str_Concat_Npl("HTTP_REFERRER=",Dicto_Get(rqst->qhdr,"REFERRER","")));
        if ( Dicto_Has(rqst->qhdr,"COOKIE") )
          Array_Push(env,Str_Concat_Npl("HTTP_COOKIE=",Dicto_Get(rqst->qhdr,"COOKIE","")));
        
        //("HTTP_USER_AGENT");
        //("HTTP_ACCEPT_LANGUAGE");

        //("SERVER_NAME");
        //("SERVER_PORT");
        Array_Push(env,0);
      } 
    return env;
  }
#endif
  ;
  
YO_WEBHOST_SERVICE *Webhost_Find_Service(YO_ARRAY *services, char *query, char **script, char **pathinfo)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i,L;
    char c;
    for ( i = 0; i < services->count; ++i )
      {
        YO_WEBHOST_SERVICE * svc = services->at[i];
        if ( Str_Starts_With(query,svc->prefix) )
          {
            L = strlen(svc->prefix); 
            c = query[L]; 
            if ( !c || c == '/' )
              {
                *script = Str_Copy_L(query,L);
                *pathinfo = Str_Copy(query+L);
                return svc;
              }
          }
      }
    return 0;
  }
#endif
  ;
    
void Webhost_Execute_Service(YO_WEBHOST_CGIST *st)
#ifdef _YO_WEBHOST_BUILTIN
  {
    YO_ARRAY *env = Webhost_Setup_Environment(st->host,st->rqst,st->path,st->script,st->pathinfo,st->url);
    st->rqst->outstrm_type = Mime_String_Of_Npl(YO_MIME_HTML);
    st->rqst->outstrm = __Refe(Cfile_Temp());
    st->cgi = __Refe(Cgi_Init_2(st->rqst->instrm,st->rqst->outstrm,(char**)env->at));
    Tasque_Alert(0,st->svc->callback,st);
  }
#endif
  ;

int Webhost_CGI_Headers(YO_HTTPD_REQUEST *rqst, void *strm)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int retcode = HTTPD_SUCCESS;
    for (;;)
      {
        static char s_Content_Type[] = "Content-Type:";
................................................................................
          }
      }
    return retcode;
  }
#endif
  ;
    
void Webhost_Exit_Service(YO_WEBHOST_CGIST *st, int error)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int retcode = 0;
    
    __Auto_Release 
      {
        if ( !error )
          {
            YO_HTTPD_REQUEST *rqst = st->rqst;
            rqst->outstrm_type = Mime_String_Of_Npl(YO_MIME_HTML);
            Oj_Seek(rqst->outstrm,0,0);
            retcode = Webhost_CGI_Headers(rqst,rqst->outstrm);
            rqst->outstrm_length = Oj_Available(rqst->outstrm);
          }
        else
          {
            retcode = 500;
          }
      }
        
    if ( retcode )
      Httpd_Continue_Request(st->rqst,retcode);
  }
#endif
  ;

void Webhost_Register_Service(YO_WEBHOST *host, char *prefix, webhost_callback_t callback, void *obj)
#ifdef _YO_WEBHOST_BUILTIN
  {
    __Auto_Release
      {
        YO_WEBHOST_SERVICE *svc = __Object_Dtor(sizeof(YO_WEBHOST_SERVICE),
                                                YO_WEBHOST_SERVICE_Destruct);
        svc->callback = callback;
        svc->obj = __Refe(obj);
        svc->prefix = Str_Copy_Npl(prefix,-1);
        
        if ( !host->services ) 
          host->services = __Refe(Array_Refs());
        
        Array_Push(host->services,__Refe(svc));
      }
  }
#endif
  ;
  
YO_WEBHOST_CGIST *Webhost_CGI_State(YO_WEBHOST *host, YO_HTTPD_REQUEST *rqst, char *path, char *script, char *pathinfo, YO_URL *url)
#ifdef _YO_WEBHOST_BUILTIN
  {
................................................................................
    st->path = path;
    st->script = script;
    st->pathinfo = pathinfo;
    return st;
  }
#endif
  ;
 
#ifdef __windoze
    
void Webhost_Check_CGI_Status(YO_WEBHOST_CGIST *st, int error)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int retcode = 0;
    
    if ( error ) return;
................................................................................
    
    __Auto_Release 
      {
        char *curdir = Current_Directory();
        
        __Try_Except
          {
            YO_ARRAY *env;
            Change_Directory(Path_Dirname(st->path));
            env = Webhost_Setup_Environment(st->host,st->rqst,st->path,st->script,st->pathinfo,st->url);
            
            st->pipex = __Refe(Pipex_Exec(Path_Basename(st->path),0,PIPEX_TMPFILE,PIPEX_NONE,(char**)env->at));
            
            if ( st->rqst->instrm )
              {
                int c = Oj_Copy_File(st->rqst->instrm,st->pipex->fin);
                Oj_Flush(st->pipex->fin);
                Oj_Close(st->pipex->fin);
              }
................................................................................
        Change_Directory(curdir);
      }
      
    return retcode;
  }
#endif
  ;
  
#endif
    
int Webhost_Callback(void *_host, YO_HTTPD_REQUEST *rqst, int status)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i;
    YO_WEBHOST *host = _host;
    char *script = 0, *pathinfo = 0;
    
    if ( status == HTTPD_RQST_CONNECT )
      {
        Log_Debug("[%p] CONNECT %s:%d",rqst,rqst->remote_addr,rqst->remote_port);
        return HTTPD_ACCEPT;
      }
    else if ( status == HTTPD_RQST_POSTDTA )
................................................................................

        Log_Info("[%p] %s/1.%d %s",rqst,Httpd_Method_String(rqst->method),rqst->httpver,rqst->uri);
        Logout_Debug(Dicto_Format(rqst->qhdr,Buffer_Print,0,1));
        
        if ( rqst->instrm_length && Log_Level(YO_LOG_INFO) )
          {
            enum { MAX_L = 60 };
            int L = Yo_Minu(MAX_L,rqst->instrm_length);
            byte_t *b = __Zero_Malloc(MAX_L+1);
            Oj_Seek(rqst->instrm,0,0);
            Oj_Read(rqst->instrm,b,L,L);
            Oj_Seek(rqst->instrm,0,0);
            for ( i = 0; i < L; ++i ) if ( b[i] < 30 || b[i] > 127 ) b[i] = '.';
            if ( rqst->instrm_length > MAX_L ) memcpy(b+MAX_L-3,">>>",3);
            Log_Info("[%p] POSTDTA %s",rqst,b);
          }
        
        if ( host->services )
          {
            YO_WEBHOST_SERVICE *svc = Webhost_Find_Service(host->services,url->query,&script,&pathinfo);
            if ( svc )
              {
                YO_WEBHOST_CGIST *st = Webhost_CGI_State(host, rqst, __Retain(path), script, pathinfo, url);
                st->svc = __Refe(svc);
                Webhost_Execute_Service(st);
                return 0;
              }
          }
        
        #ifdef __windoze
        if ( host->exec_root )
          {

            path = Webhost_Find_Executable(host->exec_root,url->query,&script,&pathinfo);
            if ( path )
              return Webhost_Execute_CGI(Webhost_CGI_State(host, rqst, __Retain(path), script, pathinfo, url));
          }
        #endif
          
        path = Path_Join(host->doc_root,url->query);
        path = Path_Normposix(path);
        
        if ( File_Get_Stats(path,&stats,1)->f.exists )
          {
            if ( stats.f.is_directory )
              {

                for ( i = 0; i < host->indexes->count; ++i )
                  {
                    char *path1 = Path_Join(path,host->indexes->at[i]);
                    if ( File_Get_Stats(path1,&stats,1)->f.exists )
                      {
                        path = path1;
                        goto open_file;

Changes to xddb.hc.

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
...
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
...
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
...
256
257
258
259
260
261
262













263
264
265
266
267
268
269
...
286
287
288
289
290
291
292
293



294




295


























#endif

typedef struct _YO_XDDB
  {
    int doc_format;
  } YO_XDDB;




enum
  {
    XDDB_FORMAT_TEXT          = 'A',
    XDDB_FORMAT_BINARY        = 'B',
    XDDB_FORMAT_ZIPPED        = 'Z',
    YO_XDDB_MAX_KEYLEN      = 128,
    YO_XDDB_UUID_LENGTH     = 9,
    XDDB_OPEN_EXISTING        = 0,
    XDDB_OPEN_READONLY        = 1,
    XDDB_CREATE_IF_NOT_EXISTS = 2,
    XDDB_CREATE_NEW           = 3,
    XDDB_CREATE_ALWAYS        = 4,
  };

#define XDDB_RAISE_IF_DSNT_EXSIS ((void*)1)


char *Xddb_Get_Key(YO_XDATA *doc)
#ifdef _YO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Str(&doc->root,"$key$",0);
  }
#endif
  ;
  
int Xddb_Get_Revision(YO_XDATA *doc)
#ifdef _YO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Int((YO_XNODE*)doc,"$rev$",0);
  }
#endif
  ;
  
void Xddb_Set_Revision(YO_XDATA *doc, int rev)
#ifdef _YO_XDDB_BUILTIN 
  {
    Xnode_Value_Set_Int((YO_XNODE*)doc,"$rev$",rev);
  }
#endif
  ;

YO_XDATA *Xddb_Binary_Decode(byte_t *at,int count,int zipped)
#ifdef _YO_XDDB_BUILTIN 
  {
................................................................................
          __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));
      }
    return doc;
  }
#endif
  ;

#define Xddb_Build_Unique_Key() __Pool(Xddb_Build_Unique_Key_Npl());
char *Xddb_Build_Unique_Key_Npl()
#ifdef _YO_XDDB_BUILTIN 
  {
    int pid = getpid();
    time_t tmx = time(0);
    double clo = clock();
    
................................................................................

#define Xddb_Update(Xddb,Doc) Xddb_Store(Xddb,Doc,1)

int Xddb_Overwrite(YO_XDDB *xddb, YO_XDATA *doc, char *key)
#ifdef _YO_XDDB_BUILTIN 
  {
    if ( key )
      Xnode_Value_Set_Str(&doc->root,"$key$",key);
    else if ( !Xnode_Value_Get_Str(&doc->root,"$key$",0) )
      {
        char *k = Xddb_Build_Unique_Key_Npl();
        Xnode_Value_Put_Str(&doc->root,"$key$",k);
      }
    return Xddb_Store(xddb,doc,0);
  }
#endif
  ;

char *Xddb_Unique(YO_XDDB *xddb, YO_XDATA *doc)
................................................................................
      case XDDB_FORMAT_BINARY: return "binary";
      case XDDB_FORMAT_ZIPPED: return "zipped";
      }
    return "unknown";
  }
#endif
  ;













  
_YO_XDDB_EXTERN char Xddb_Strm_Cancel_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_cancel/@"); 
void Xddb_Strm_Cancel(void *strm) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*))Yo_Find_Method_Of(&strm,Xddb_Strm_Cancel_OjMID,YO_RAISE_ERROR))
        (strm); });

_YO_XDDB_EXTERN char Xddb_Strm_Commit_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_commit/@*i"); 
................................................................................
        (xddb,mime); });

_YO_XDDB_EXTERN char Xddb_Strm_Delete_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_delete/@*"); 
void Xddb_Strm_Delete(YO_XDDB *xddb, char *key) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Strm_Delete_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

#endif /* C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB */










































>
>
>





|
|


|




|
>




|







|







|







 







|







 







|
|


|







 







>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>

>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
...
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
...
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
#endif

typedef struct _YO_XDDB
  {
    int doc_format;
  } YO_XDDB;

#define YO_XDDB_KEY_PROPERTY "$$key"
#define YO_XDDB_REV_PROPERTY "$$rev"

enum
  {
    XDDB_FORMAT_TEXT          = 'A',
    XDDB_FORMAT_BINARY        = 'B',
    XDDB_FORMAT_ZIPPED        = 'Z',
    YO_XDDB_MAX_KEYLEN        = 128,
    YO_XDDB_UUID_LENGTH       = 9,
    XDDB_OPEN_EXISTING        = 0,
    XDDB_OPEN_READONLY        = 1,
    XDDB_CREATE_IF_DSNT_EXIST = 2,
    XDDB_CREATE_NEW           = 3,
    XDDB_CREATE_ALWAYS        = 4,
  };

#define XDDB_RAISE_IF_DSNT_EXSIST ((void*)1)
#define XDDB_EMPTY_IF_DSNT_EXSIST ((void*)2)

char *Xddb_Get_Key(YO_XDATA *doc)
#ifdef _YO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Str(&doc->root,YO_XDDB_KEY_PROPERTY,0);
  }
#endif
  ;
  
int Xddb_Get_Revision(YO_XDATA *doc)
#ifdef _YO_XDDB_BUILTIN 
  {
    return Xnode_Value_Get_Int((YO_XNODE*)doc,YO_XDDB_REV_PROPERTY,0);
  }
#endif
  ;
  
void Xddb_Set_Revision(YO_XDATA *doc, int rev)
#ifdef _YO_XDDB_BUILTIN 
  {
    Xnode_Value_Set_Int((YO_XNODE*)doc,YO_XDDB_REV_PROPERTY,rev);
  }
#endif
  ;

YO_XDATA *Xddb_Binary_Decode(byte_t *at,int count,int zipped)
#ifdef _YO_XDDB_BUILTIN 
  {
................................................................................
          __Raise(YO_ERROR_INCONSISTENT,__yoTa("unsupported document format",0));
      }
    return doc;
  }
#endif
  ;

#define Xddb_Build_Unique_Key() __Pool(Xddb_Build_Unique_Key_Npl())
char *Xddb_Build_Unique_Key_Npl()
#ifdef _YO_XDDB_BUILTIN 
  {
    int pid = getpid();
    time_t tmx = time(0);
    double clo = clock();
    
................................................................................

#define Xddb_Update(Xddb,Doc) Xddb_Store(Xddb,Doc,1)

int Xddb_Overwrite(YO_XDDB *xddb, YO_XDATA *doc, char *key)
#ifdef _YO_XDDB_BUILTIN 
  {
    if ( key )
      Xnode_Value_Set_Str(&doc->root,YO_XDDB_KEY_PROPERTY,key);
    else if ( !Xnode_Value_Get_Str(&doc->root,YO_XDDB_KEY_PROPERTY,0) )
      {
        char *k = Xddb_Build_Unique_Key_Npl();
        Xnode_Value_Put_Str(&doc->root,YO_XDDB_KEY_PROPERTY,k);
      }
    return Xddb_Store(xddb,doc,0);
  }
#endif
  ;

char *Xddb_Unique(YO_XDDB *xddb, YO_XDATA *doc)
................................................................................
      case XDDB_FORMAT_BINARY: return "binary";
      case XDDB_FORMAT_ZIPPED: return "zipped";
      }
    return "unknown";
  }
#endif
  ;

char *Xddb_Key_Prefix(char *key)
#ifdef _YO_XDDB_BUILTIN 
  {
    char *p = strrchr(key,':');
    if ( p )
      {
        return Str_Copy_L(key,(p-key)+1);
      }
    return 0;
  }
#endif
  ;
  
_YO_XDDB_EXTERN char Xddb_Strm_Cancel_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_cancel/@"); 
void Xddb_Strm_Cancel(void *strm) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*))Yo_Find_Method_Of(&strm,Xddb_Strm_Cancel_OjMID,YO_RAISE_ERROR))
        (strm); });

_YO_XDDB_EXTERN char Xddb_Strm_Commit_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_commit/@*i"); 
................................................................................
        (xddb,mime); });

_YO_XDDB_EXTERN char Xddb_Strm_Delete_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_strm_delete/@*"); 
void Xddb_Strm_Delete(YO_XDDB *xddb, char *key) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Strm_Delete_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

_YO_XDDB_EXTERN char Xddb_Seq_Open_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_open/@*i"); 
void *Xddb_Seq_Open(YO_XDDB *xddb, char *key, int mknew) _YO_XDDB_BUILTIN_CODE(
  { return ((void *(*)(void*,void*, int))Yo_Find_Method_Of(&xddb,Xddb_Seq_Open_OjMID,YO_RAISE_ERROR))
        (xddb,key,mknew); });

_YO_XDDB_EXTERN char Xddb_Seq_Delete_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_delete/@*"); 
void Xddb_Seq_Delete(YO_XDDB *xddb, char *key) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&xddb,Xddb_Seq_Delete_OjMID,YO_RAISE_ERROR))
        (xddb,key); });

_YO_XDDB_EXTERN char Xddb_Seq_Take_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_take/@"); 
YO_XDATA *Xddb_Seq_Take(void *seq) _YO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*))Yo_Find_Method_Of(&seq,Xddb_Seq_Take_OjMID,YO_RAISE_ERROR))
        (seq); });

_YO_XDDB_EXTERN char Xddb_Seq_Next_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_next/@"); 
YO_XDATA *Xddb_Seq_Next(void *seq) _YO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*))Yo_Find_Method_Of(&seq,Xddb_Seq_Next_OjMID,YO_RAISE_ERROR))
        (seq); });

_YO_XDDB_EXTERN char Xddb_Seq_Multi_Next_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_multi_next/@i"); 
YO_ARRAY *Xddb_Seq_Multi_Next(void *seq,int count) _YO_XDDB_BUILTIN_CODE(
  { return ((void*(*)(void*,int))Yo_Find_Method_Of(&seq,Xddb_Seq_Multi_Next_OjMID,YO_RAISE_ERROR))
        (seq,count); });

_YO_XDDB_EXTERN char Xddb_Seq_Push_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_push/@*"); 
void Xddb_Seq_Push(YO_XDDB *seq, YO_XDATA *doc) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*,void*))Yo_Find_Method_Of(&seq,Xddb_Seq_Push_OjMID,YO_RAISE_ERROR))
        (seq,doc); });

_YO_XDDB_EXTERN char Xddb_Seq_Erase_OjMID[] _YO_XDDB_BUILTIN_CODE( = "xddb_seq_erase/@*"); 
void Xddb_Seq_Erase(YO_XDDB *seq) _YO_XDDB_BUILTIN_CODE(
  { ((void(*)(void*))Yo_Find_Method_Of(&seq,Xddb_Seq_Erase_OjMID,YO_RAISE_ERROR))
        (seq); });

#endif /* C_once_63CF3893_FBF5_4DA0_9B2D_792FE7BBBCAB */

Changes to xddbfs.hc.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
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
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
...
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
...
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

#ifndef C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF
#define C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF

#include "xddb.hc"
#include "file.hc"
#include "datetime.hc"
#include "md5.hc"

#ifdef _LIBYOYO
#define _YO_XDDBFS_BUILTIN
#endif

typedef struct _YO_XDDBFS
  {
................................................................................
        free(strm->tmp_path);
      }
    if ( strm->fd >= 0 ) close(strm->fd);
    free(strm->key);
    __Unrefe(strm->xddb);
    __Destruct(strm);    
  };








































char *Xddbfs_Tmp_File_Path_Sfx(YO_XDDBFS *xdb, char *sfx)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    char *S = Xddb_Build_Unique_Key();
    if ( sfx )
      S = Str_Concat(S,sfx);
    return Str_Join_3('/',xdb->basepath,"tmp",S);
  }
#endif
  ;

char *Xddbfs_File_Path_Sfx_Md5(YO_XDDBFS *xddb, byte_t *md5, char *sfx)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    char *S = 0;
    int i;
    char md5_hex[33] = {0};
    char hash[4*3] = "\0\0/\0\0/\0\0/\0\0";
    for ( i = 0; i < 16; ++i )
      Str_Hex_Byte(md5[i],0,md5_hex+i*2);
    if ( !xddb->fs_depth )
      {
        S = Str_Join_2('/',xddb->basepath,md5_hex);
      }
    else
      {
        switch ( xddb->fs_depth )
          {
            case 1:
              {
                byte_t c = Crc_8(0,md5,16);
                Str_Hex_Byte(c,0,hash);
                hash[2] = 0;
                break;
              }
            case 2:
              {
                ushort_t c = Crc_16(0,md5,16);
                for ( i = 0; i < 2; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                hash[5] = 0;
                break;
              }
            default:
              {
                uint_t c = Crc_32(0,md5,16);
                for ( i = 0; i < 4; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                break;
              }
          }
      
        S = Str_Join_3('/',xddb->basepath,hash,md5_hex);
      }
      
    if ( sfx )
      S = Str_Concat(S,sfx);

    return S;
  }
................................................................................
          }
        S = Str_Join_2('/',xddb->basepath,S);
        if ( sfx )
          S = Str_Concat(S,sfx);
      }
    else
      {
        byte_t md5[16];
        Md5_Digest(key,strlen(key),md5);
        S = Xddbfs_File_Path_Sfx_Md5(xddb,md5,sfx);
      }
      
    return S;
  }
#endif
  ;

................................................................................
        char *coid = Xddbfs_File_Path(xddb,key);
        __Pfd_Lock(&xddb->jur_fd)
          File_Unlink(coid,0);
      }
  }
#endif
  ;



int Xddbfs_Store(YO_XDDBFS *xddb, YO_XDATA *doc, int strict_revision)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    int revision = 0; 
    
    __Auto_Release
      {
        //byte_t md5[16];
        char *key;
        char *coid;
        YO_BUFFER *bf;
        
        key = Xnode_Value_Get_Str(&doc->root,"$key$",0);
        if ( !key )
          __Raise(YO_ERROR_ILLFORMED,"$key$ property doesn't present or has invalid value");


        
        //Md5_Sign_Data(key,strlen(key),md5);
        coid = Xddbfs_File_Path(xddb,key);
        
        __Pfd_Lock(&xddb->jur_fd)
          {
            bf = Xddbfs_Read_Id(xddb,coid,8);

            if ( bf ) revision = Hex8_To_Unsigned(bf->at);


            
            if ( strict_revision )
              if ( revision && revision != Xddb_Get_Revision(doc) )
                __Raise(YO_ERROR_INCONSISTENT,
                  __Format("Inconsistent revision of document '%s', store:%d != docu:%d",
                           coid,
                           revision,
                           Xddb_Get_Revision(doc)));              
            
            ++revision;
            Xddb_Set_Revision(doc,revision);
            bf = Xddb_Encode(&xddb->xddb,doc);
            Xddbfs_Write_Id(xddb,coid,bf->at,bf->count);
          }



      }
      
    return revision;
  }
#endif
  ;

................................................................................
char *Xddbfs_Source_String(YO_XDDBFS *xddb)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    return __Format("fs:%s:%d",xddb->basepath,xddb->fs_depth);
  } 
#endif 
  ;








































































































































































































































































































































































YO_XDDB *Xddbfs_Open(char *path, int create_new, int format, int depth)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YO_XDDBFS_Destruct},
................................................................................
        {Xddb_Delete_OjMID,       Xddbfs_Delete},
        {Xddb_Get_OjMID,          Xddbfs_Get},
        {Xddb_Has_OjMID,          Xddbfs_Has},
        {Xddb_Store_OjMID,        Xddbfs_Store},
        {Xddb_Strm_Open_OjMID,    Xddbfs_Strm_Open},
        {Xddb_Strm_Create_OjMID,  Xddbfs_Strm_Create},
        {Xddb_Strm_Delete_OjMID,  Xddbfs_Strm_Delete},


        {Xddb_Source_String_OjMID,Xddbfs_Source_String},
        {0}
      };

    YO_XNODE *xcf;
    YO_XDDBFS *xddb = 0;
    YO_FILE_STATS bp_stats = {0};







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|




|

|
|


|







|






|







|






|







 







|
|
|







 







>
>








<
|



|

|
>
>

<





>
|
>
>




|









>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
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
...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
...
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
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
....
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105

#ifndef C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF
#define C_once_BAF0727D_FCA6_445A_8E5A_0EEAA9374EEF

#include "xddb.hc"
#include "file.hc"
#include "datetime.hc"
#include "sha1.hc"

#ifdef _LIBYOYO
#define _YO_XDDBFS_BUILTIN
#endif

typedef struct _YO_XDDBFS
  {
................................................................................
        free(strm->tmp_path);
      }
    if ( strm->fd >= 0 ) close(strm->fd);
    free(strm->key);
    __Unrefe(strm->xddb);
    __Destruct(strm);    
  };

#ifndef _XDDBFS_SQTEST
enum { YO_XDDBFS_SQRECO_COUNT = 204 }; /* 4K per page */
#else
enum { YO_XDDBFS_SQRECO_COUNT = 3 };
#endif

typedef struct _YO_XDDBFS_SQRECO
  {
    byte_t sha1[20];
  } YO_XDDBFS_SQRECO;

typedef struct _YO_XDDBFS_SQJUR
  {
    byte_t version[8];
    quad_t datetime;
    quad_t first_page;
    quad_t last_page;
    int    first_recno;
    int    reco_next;
  } YO_XDDBFS_SQJUR;

typedef struct _YO_XDDBFS_SEQ
  {
    YO_XDDBFS *xddb;
    char      *path;
    quad_t    page;
    int       recno;
    int       jur_fd;
  } YO_XDDBFS_SEQ;

#ifndef _YO_XDDBFS_BUILTIN 
extern
#endif
byte_t YO_XDDBFS_SEQ_JUR_VERSION[8] 
#ifdef _YO_XDDBFS_BUILTIN 
= {'S','E','Q','0','0','1','\r','\n'}
#endif
;

char *Xddbfs_Tmp_File_Path_Sfx(YO_XDDBFS *xdb, char *sfx)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    char *S = Xddb_Build_Unique_Key();
    if ( sfx )
      S = Str_Concat(S,sfx);
    return Str_Join_3('/',xdb->basepath,"tmp",S);
  }
#endif
  ;

char *Xddbfs_File_Path_Sfx_Sha1(YO_XDDBFS *xddb, byte_t *sha1, char *sfx)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    char *S = 0;
    int i;
    char sha1_hex[41] = {0};
    char hash[4*3] = "\0\0/\0\0/\0\0/\0\0";
    for ( i = 0; i < 20; ++i )
      Str_Hex_Byte(sha1[i],0,sha1_hex+i*2);
    if ( !xddb->fs_depth )
      {
        S = Str_Join_2('/',xddb->basepath,sha1_hex);
      }
    else
      {
        switch ( xddb->fs_depth )
          {
            case 1:
              {
                byte_t c = Crc_8(0,sha1,20);
                Str_Hex_Byte(c,0,hash);
                hash[2] = 0;
                break;
              }
            case 2:
              {
                ushort_t c = Crc_16(0,sha1,20);
                for ( i = 0; i < 2; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                hash[5] = 0;
                break;
              }
            default:
              {
                uint_t c = Crc_32(0,sha1,20);
                for ( i = 0; i < 4; ++i )
                  Str_Hex_Byte((byte_t)(c>>(i*8)),0,hash+i*3);
                break;
              }
          }
      
        S = Str_Join_3('/',xddb->basepath,hash,sha1_hex);
      }
      
    if ( sfx )
      S = Str_Concat(S,sfx);

    return S;
  }
................................................................................
          }
        S = Str_Join_2('/',xddb->basepath,S);
        if ( sfx )
          S = Str_Concat(S,sfx);
      }
    else
      {
        byte_t sha1[20];
        Sha1_Digest(key,strlen(key),sha1);
        S = Xddbfs_File_Path_Sfx_Sha1(xddb,sha1,sfx);
      }
      
    return S;
  }
#endif
  ;

................................................................................
        char *coid = Xddbfs_File_Path(xddb,key);
        __Pfd_Lock(&xddb->jur_fd)
          File_Unlink(coid,0);
      }
  }
#endif
  ;

void Xddbfs_Pfx_Add_Key(YO_XDDBFS *xddb, char *pfx, char *key);

int Xddbfs_Store(YO_XDDBFS *xddb, YO_XDATA *doc, int strict_revision)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    int revision = 0; 
    
    __Auto_Release
      {

        char *key,*pfx = 0;
        char *coid;
        YO_BUFFER *bf;
        
        key = Xnode_Value_Get_Str(&doc->root,YO_XDDB_KEY_PROPERTY,0);
        if ( !key )
          __Raise_Format(YO_ERROR_ILLFORMED,
             (__yoTa("%s property doesn't exist or has invalid value",0),
                     YO_XDDB_KEY_PROPERTY));
        

        coid = Xddbfs_File_Path(xddb,key);
        
        __Pfd_Lock(&xddb->jur_fd)
          {
            bf = Xddbfs_Read_Id(xddb,coid,8);
            if ( bf ) 
              revision = Hex8_To_Unsigned(bf->at);
            else
              pfx = Xddb_Key_Prefix(key); // if document doesn't exist
            
            if ( strict_revision )
              if ( revision && revision != Xddb_Get_Revision(doc) )
                __Raise(YO_ERROR_INCONSISTENT,
                  __Format(__yoTa("inconsistent revision of document '%s', store:%d != docu:%d",0),
                           coid,
                           revision,
                           Xddb_Get_Revision(doc)));              
            
            ++revision;
            Xddb_Set_Revision(doc,revision);
            bf = Xddb_Encode(&xddb->xddb,doc);
            Xddbfs_Write_Id(xddb,coid,bf->at,bf->count);
          }
          
        if ( pfx )
          Xddbfs_Pfx_Add_Key(xddb,pfx,key);
      }
      
    return revision;
  }
#endif
  ;

................................................................................
char *Xddbfs_Source_String(YO_XDDBFS *xddb)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    return __Format("fs:%s:%d",xddb->basepath,xddb->fs_depth);
  } 
#endif 
  ;

void Xddbfs_Seq_Push_Key(YO_XDDBFS_SEQ *seq, char *key)
#ifdef _YO_XDDBFS_BUILTIN
  {
    YO_XDDBFS_SQRECO reco = {0};
    int pg_fd = -1;
    int new_pg, recno;
    char pgno[32] = {0};
    YO_XDDBFS_SQJUR jur = {0};
    
    if ( seq->jur_fd < 0 )
      __Raise(YO_ERROR_IO,__yoTa("sequense is closed",0));

    Sha1_Digest(key,strlen(key),reco.sha1);

    __Pfd_Lock(&seq->jur_fd)
      {
        Lseek_Raise(seq->jur_fd,0);
        Read_Into_Raise(seq->jur_fd,&jur,sizeof(jur));
        
        __Pfd_Guard(&pg_fd)
          {            
            if ( YO_XDDBFS_SQRECO_COUNT > jur.reco_next )
              {
                new_pg = 0;
                recno  = jur.reco_next++;
              }
            else
              {
                new_pg = 1;
                ++jur.last_page;
                jur.reco_next = 1;
                recno = 0;
              }
            
            Quad_To_Hex16(jur.last_page,pgno);
            jur.datetime = Get_Curr_Datetime();
            
            pg_fd = Open_File_Raise(Str_Join_2('/',seq->path,pgno),O_RDWR|(new_pg?O_CREAT:0));
            Lseek_Raise(pg_fd,sizeof(reco)*recno);
            Write_Out_Raise(pg_fd,&reco,sizeof(reco));
          }
        
        Lseek_Raise(seq->jur_fd,0);
        Write_Out_Raise(seq->jur_fd,&jur,sizeof(jur));
      }
  }
#endif
  ;

void Xddbfs_Seq_Push(YO_XDDBFS_SEQ *seq, YO_XDATA *doc)
#ifdef _YO_XDDBFS_BUILTIN
  {
    char *key = Xnode_Value_Get_Str(&doc->root,YO_XDDB_KEY_PROPERTY,0);
    if ( !key )
      __Raise_Format(YO_ERROR_ILLFORMED,
        (__yoTa("%s property doesn't exist or has invalid value",0),YO_XDDB_KEY_PROPERTY));
    Xddbfs_Seq_Push_Key(seq,key);
  }
#endif
  ;

int Xddbfs_Seq_Next_Internal(YO_XDDBFS_SEQ *seq, YO_XDATA **arr, int count)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    int arr_i;
    int pg_fd = -1;
    YO_XDDBFS_SQJUR jur = {0};
    
    Lseek_Raise(seq->jur_fd,0);
    Read_Into_Raise(seq->jur_fd,&jur,sizeof(jur));

    for ( arr_i = 0; arr_i < count; ++arr_i )
      arr[arr_i] = 0;
      
    for ( arr_i = 0; arr_i < count; ++arr_i )
      {
      repeat:
      
        if ( seq->recno >= YO_XDDBFS_SQRECO_COUNT )
          {
            ++seq->page;
            seq->recno = 0;
          }

        if ( seq->page < jur.first_page ) 
          {
            seq->page = jur.first_page;
            seq->recno = jur.first_recno;
          }
        else if ( seq->recno < jur.first_recno )
          {
            seq->recno = jur.first_recno;
          }

        if ( seq->page > jur.last_page || ( seq->page == jur.last_page && seq->recno >= jur.reco_next ) )
          break;
        else
          {
            YO_BUFFER *bf = 0;
            YO_XDATA *doc = 0;
            char *coid = 0;
            YO_XDDBFS_SQRECO reco = {0};
            char pgno[32] = {0};

            Quad_To_Hex16(seq->page,pgno);
       
            __Pfd_Guard(&pg_fd)
              {
                pg_fd = Open_File_Raise(Str_Join_2('/',seq->path,pgno),O_RDONLY);
                Lseek_Raise(pg_fd,sizeof(reco)*seq->recno);
                Read_Into_Raise(pg_fd,&reco,sizeof(reco));
                ++seq->recno;
              }
            
            __Auto_Ptr(doc)
              {
                coid = Xddbfs_File_Path_Sfx_Sha1(seq->xddb, reco.sha1, 0);
                __Pfd_Lock(&seq->xddb->jur_fd)
                  bf = Xddbfs_Read_Id(seq->xddb,coid,-1);
                if ( bf )
                  doc = Xddb_Decode(&seq->xddb->xddb,bf->at,bf->count);
              }
              
            if ( doc )
              arr[arr_i++] = doc;
            else
              goto repeat;
          }
      }
    return  arr_i;
  }
#endif
  ;

int Xddbfs_Seq_Next_(YO_XDDBFS_SEQ *seq, YO_XDATA **arr, int count)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    int r = 0;
    if ( seq->jur_fd < 0 )
      __Raise(YO_ERROR_IO,__yoTa("sequense is closed",0));
    
    __Pfd_Lock(&seq->jur_fd)
      {
        r = Xddbfs_Seq_Next_Internal(seq, arr, count);
      }
    return r;
  }
#endif
  ;

YO_XDATA *Xddbfs_Seq_Next(YO_XDDBFS_SEQ *seq)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    YO_XDATA *doc = 0;
    if ( Xddbfs_Seq_Next_(seq,&doc,1) )
      return doc;
    return 0;
  }
#endif
  ;

YO_ARRAY *Xddbfs_Seq_Multi_Next(YO_XDDBFS_SEQ *seq, int count)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    YO_ARRAY *arr = 0;
    __Auto_Ptr(arr)
      {
        YO_XDATA **docs = __Malloc(count*sizeof(void*));
        count = Xddbfs_Seq_Next_(seq,docs,count);
        arr = Array_Refs_Copy(docs,count);
      }
    return arr;
  }
#endif
  ;

void Xddbfs_Seq_Erase_Internal(YO_XDDBFS_SEQ *seq)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    YO_XDDBFS_SQJUR jur = {0};
    quad_t last_first_pg = jur.first_page;
    
    Lseek_Raise(seq->jur_fd,0);
    Read_Into_Raise(seq->jur_fd,&jur,sizeof(jur));

    if ( seq->recno >= YO_XDDBFS_SQRECO_COUNT )
      {
        ++seq->page;
        seq->recno = 0;
      }

    if ( jur.first_page < seq->page )
      {
        jur.first_page = seq->page;
        jur.first_recno = 0;
      }
    if ( seq->page == jur.first_page && jur.first_recno <= seq->recno)
      {
        jur.first_recno = seq->recno;
        
        Lseek_Raise(seq->jur_fd,0);
        Write_Out_Raise(seq->jur_fd,&jur,sizeof(jur));
        
        __Auto_Release while ( last_first_pg < jur.first_page )
          {
            char *S;
            char pgno[32] = {0};
            Quad_To_Hex16(last_first_pg,pgno);
            S = Str_Join_2('/',seq->path,pgno);
            if ( File_Exists(S) ) File_Unlink(S,0);
            ++last_first_pg;
          }
      }
  }
#endif
  ;
  
void Xddbfs_Seq_Erase(YO_XDDBFS_SEQ *seq)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    if ( seq->jur_fd < 0 )
      __Raise(YO_ERROR_IO,__yoTa("sequense is closed",0));
    
    __Pfd_Lock(&seq->jur_fd)
      {
        Xddbfs_Seq_Erase_Internal(seq);
      }
  }
#endif
  ;

int Xddbfs_Seq_Take_(YO_XDDBFS_SEQ *seq, YO_XDATA **arr, int count)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    int r = 0;
    if ( seq->jur_fd < 0 )
      __Raise(YO_ERROR_IO,__yoTa("sequense is closed",0));
    
    __Pfd_Lock(&seq->jur_fd)
      {
        r = Xddbfs_Seq_Next_Internal(seq, arr, count);
        Xddbfs_Seq_Erase_Internal(seq);
      }
    return r;
  }
#endif
  ;

YO_XDATA *Xddbfs_Seq_Take(YO_XDDBFS_SEQ *seq)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    YO_XDATA *doc = 0;
    if ( Xddbfs_Seq_Take_(seq,&doc,1) )
      return doc;
    return 0;
  }
#endif
  ;

void Xddbfs_Seq_Delete(YO_XDDBFS *xddb, char *key)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    __Auto_Release
      {
        char *path = Xddbfs_File_Path_Sfx(xddb,key,".seq");
        File_Unlink(path,1);
      }
  }
#endif
  ;

void YO_XDDBFS_SEQ_Destroy(YO_XDDBFS_SEQ *seq)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    __Unrefe(seq->xddb);
    if ( seq->jur_fd >=0 ) close(seq->jur_fd);
    free( seq->path );
    __Destruct(seq);
  }
#endif
  ;

YO_XDDBFS_SEQ *Xddbfs_Seq_Open(YO_XDDBFS *xddb, char *key, int mknew)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    YO_XDDBFS_SEQ *seq;
    char *coid;
    char *coid_jur;
    int exists;

    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,           YO_XDDBFS_SEQ_Destroy},
        {Xddb_Seq_Take_OjMID,         Xddbfs_Seq_Take},
        {Xddb_Seq_Erase_OjMID,        Xddbfs_Seq_Erase},
        {Xddb_Seq_Next_OjMID,         Xddbfs_Seq_Next},
        {Xddb_Seq_Multi_Next_OjMID,   Xddbfs_Seq_Multi_Next},
        {Xddb_Seq_Push_OjMID,         Xddbfs_Seq_Push},
        {0}
      };    
      
    seq = __Object(sizeof(YO_XDDBFS_SEQ),funcs);
    seq->jur_fd = -1;
    seq->xddb = __Refe(xddb);
    
    coid = Xddbfs_File_Path_Sfx(xddb,key,".seq");
    coid_jur = Str_Join_2('/',coid,".jur");

    exists = File_Exists(coid_jur);
    
    if ( mknew == XDDB_CREATE_NEW && exists )
      __Raise_Format(YO_ERROR_ALREADY_EXISTS,(__yoTa("sequence '%s' already exists",0),key));

    if ( mknew == XDDB_CREATE_ALWAYS && exists )
      {
        Xddbfs_Seq_Delete(xddb,key);  
        exists = 0;
      }
      
    if ( mknew != XDDB_OPEN_EXISTING && mknew != XDDB_OPEN_READONLY && !exists )
      {
        int pg_fd = -1;
        YO_XDDBFS_SQJUR jur = {0};
        __Pfd_Lock(&xddb->jur_fd)
          {
            Create_Directory_In_Depth(coid);
            memcpy(jur.version,YO_XDDBFS_SEQ_JUR_VERSION,8);
            jur.datetime = Get_Curr_Datetime();
            __Pfd_Guard(&pg_fd)
              pg_fd = Open_File_Raise(Str_Join_2('/',coid,"0000000000000000"),O_RDWR|O_CREAT);
            seq->jur_fd = Open_File_Raise(coid_jur,O_RDWR|O_CREAT);
            Write_Out_Raise(seq->jur_fd,&jur,sizeof(jur));
          }
      }
    else
      {
        __Pfd_Lock(&xddb->jur_fd)
          {
            if ( !exists )
              __Raise_Format(YO_ERROR_DOESNT_EXIST,(__yoTa("sequence '%s' does not exist",0),key));
            seq->jur_fd = Open_File_Raise(coid_jur,O_RDWR);
          }
      }
    
    seq->path = __Retain(coid);
    return seq;
  }
#endif
  ;

void Xddbfs_Pfx_Add_Key(YO_XDDBFS *xddb, char *pfx, char *key)
  {
    __Auto_Release
      {
        YO_XDDBFS_SEQ *seq = Xddbfs_Seq_Open(xddb,pfx,XDDB_CREATE_IF_NOT_EXISTS);
        Xddbfs_Seq_Push_Key(seq,key);
      }
  }

YO_XDDB *Xddbfs_Open(char *path, int create_new, int format, int depth)
#ifdef _YO_XDDBFS_BUILTIN 
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID,       YO_XDDBFS_Destruct},
................................................................................
        {Xddb_Delete_OjMID,       Xddbfs_Delete},
        {Xddb_Get_OjMID,          Xddbfs_Get},
        {Xddb_Has_OjMID,          Xddbfs_Has},
        {Xddb_Store_OjMID,        Xddbfs_Store},
        {Xddb_Strm_Open_OjMID,    Xddbfs_Strm_Open},
        {Xddb_Strm_Create_OjMID,  Xddbfs_Strm_Create},
        {Xddb_Strm_Delete_OjMID,  Xddbfs_Strm_Delete},
        {Xddb_Seq_Open_OjMID,     Xddbfs_Seq_Open},
        {Xddb_Seq_Delete_OjMID,   Xddbfs_Seq_Delete},
        {Xddb_Source_String_OjMID,Xddbfs_Source_String},
        {0}
      };

    YO_XNODE *xcf;
    YO_XDDBFS *xddb = 0;
    YO_FILE_STATS bp_stats = {0};

Changes to yoyo.hc.

135
136
137
138
139
140
141











142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
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
...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
...
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
...
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
...
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
...
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
...
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
...
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
....
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
....
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
....
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
....
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
....
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
....
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
....
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
....
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
....
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
....
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
....
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
....
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
....
1665
1666
1667
1668
1669
1670
1671













1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
....
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
....
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
....
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
....
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
....
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
# include <windef.h>
# include <winbase.h>
# include <excpt.h>
# include <objbase.h>
# include <io.h>
# include <process.h>
# include <malloc.h> /* alloca */











#else
# include <sys/time.h>
# include <unistd.h>
# include <dlfcn.h>
# ifdef _THREADS
#   include <pthreads.h>
# endif
# if defined __APPLE__
#   include <malloc/malloc.h> /* malloc_size */
# elif defined __NetBSD__
#   ifndef _NBSDMUTE
#     warning {! NetBSD has malloc_usable_size commented !}
#   endif
#   define malloc_size(Ptr) (0)
................................................................................
typedef unsigned int   udword_t; /* 32 bit unsigned ( i386 word ) */
typedef int            dword_t;  /* 32 bit signed ( i386 word )   */

typedef unsigned long  ulong_t;

#ifndef __windoze
  typedef unsigned long long  uquad_t; /* 64-bit unsigned ( double word ) historically named as quad word */
# if !defined __APPLE__
    typedef long long  quad_t; /* 64-bit signed word ( double word ) */
# endif
#else
  typedef unsigned __int64  uquad_t;
  typedef __int64  quad_t;
#endif

................................................................................

__No_Return void _Yo_Fatal(int err,void *ctx,char *filename,int lineno);
__No_Return void _Yo_Raise(int err,char *msg,char *filename,int lineno);

#ifdef __windoze
#define Switch_to_Thread() SwitchToThread()
#else
#define Switch_to_Thread() ((void)0)

#endif








































#ifndef _THREADS

# define Yo_Atomic_Increment(Ptr) (++*(Ptr))
# define Yo_Atomic_Decrement(Ptr) (--*(Ptr))
# define Yo_Atomic_CmpXchg(Ptr,Val,Comp) ( *(Ptr) == (Comp) ? (*(Ptr) = (Val), 1) : 0 )
# define Yo_Atomic_CmpXchg_Ptr(Ptr,Val,Comp) ( *(Ptr) == (Comp) ? (*(Ptr) = (Val), 1) : 0 )

# define YO_TLS_DEFINE(Name)  void * volatile Name = 0

# define YO_TLS_DECLARE(Name) extern void * volatile Name
# define Yo_Tls_Set(Name,Val) ((Name) = (Val))
# define Yo_Tls_Get(Name)     (Name)

# define YO_MULTITHREADED(Expr)

# define __Xchg_Interlock if (0) {;} else
# define __Xchg_Sync(Lx)  if (0) {;} else
# define Yo_Wait_Xchg_Lock(Ptr)
# define Yo_Xchg_Unlock(Ptr)

# define __Mtx_Sync(Mtx)  if (0) {;} else

#else /* -D _THREADS */

#ifdef __windoze




#else
# ifdef __GNUC__

























# else



# endif




















#endif






# define YO_MULTITHREADED(Expr) Expr

# define _xchg_YO_LOCAL_LX static int volatile YO_LOCAL_ID(lx)
# define _xchg_YO_LOCAL_ID_REF &YO_LOCAL_ID(lx)
# define __Xchg_Interlock \
              __Interlock_Opt( _xchg_YO_LOCAL_LX, _xchg_YO_LOCAL_ID_REF, \
                  Yo_Wait_Xchg_Lock,Yo_Xchg_Unlock,Yo_Xchg_Unlock_Proc)

# define __Xchg_Sync(Lx) \
              __Interlock_Opt(((void)0),Lx, \
                  Yo_Wait_Xchg_Lock,Yo_Xchg_Unlock,Yo_Xchg_Unlock_Proc)

# define Yo_Wait_Xchg_Lock(Ptr) while ( !Yo_Atomic_CmpXchg(Ptr,1,0) ) Switch_to_Thread()
# define Yo_Xchg_Unlock(Ptr) Yo_Atomic_CmpXchg(Ptr,0,1)

void Yo_Xchg_Unlock_Proc(int volatile *p) _YO_CORE_BUILTIN_CODE({Yo_Atomic_CmpXchg(p,0,1);});

/*# define __Mtx_Sync(Mtx) \
              __Interlock_Opt(((void)0),Mtx, \
                  Yo_Wait_Mtx_Lock,Yo_Mtx_Unlock,Yo_Mtx_Unlock_Proc)*/

#endif
  
#if defined __GNUC__ && ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__>=6) )
  #define __Static_Assert_S(expr,S) _Static_assert(expr,S)
#elif !defined _MSC_VER || _MSC_VER < 1600 || defined __ICL
  #define __Static_Assert_S(expr,S) \
................................................................................
# define STRICT_REQUIRE(Expr) REQUIRE(Expr)
# define STRICT_CHECK(Expr) (Expr)
#else
# define STRICT_REQUIRE(Expr) ((void)0)
# define STRICT_CHECK(Expr) (1)
#endif /* _STRICT */

#define Slist_Remove_By(ListPtr,Next,Val) Yo_Slist_Remove(ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)),Val)
#define Slist_Remove(ListPtr,Val) Slist_Remove_By(ListPtr,next,Val)

void Yo_Slist_Remove(void **p, int offs_of_next, void *val)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p ) 
      {
................................................................................
              p =  (void**)((byte_t*)*p + offs_of_next);
          }
      }
  }
#endif
  ;

#define Slist_Push_By(ListPtr,Next,Val) Yo_Slist_Push(ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)),Val)
#define Slist_Push(ListPtr,Val) Slist_Push_By(ListPtr,next,Val)

void Yo_Slist_Push(void **p, int offs_of_next, void *val)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p ) 
      {
................................................................................
        *p = val;
        *(void**)((byte_t*)*p + offs_of_next) = 0;
      }
  }
#endif
  ;
  
#define Slist_Pop_By(ListPtr,Next) Yo_Slist_Pop(ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)))
#define Slist_Pop(ListPtr) Slist_Pop_By(ListPtr,next)

void *Yo_Slist_Pop(void **p, int offs_of_next)
#ifdef _YO_CORE_BUILTIN
  {
    void *r = 0;
    
................................................................................

enum { YO_MAX_CS_COUNT = 7 };
enum { YO_INI_JB_COUNT = 5 };
enum { YO_EXT_JB_COUNT = 3 };
enum { YO_INI_POOL_COUNT = 256 };
enum { YO_EXT_POOL_COUNT = 128 };

typedef void (*Yo_JMPBUF_Unlock)(void *);

typedef struct _YO_JMPBUF_LOCK
  {
    void *cs;
    Yo_JMPBUF_Unlock unlock;
  } YO_JMPBUF_LOCK;

typedef struct _YO_JMPBUF
................................................................................
      } stats;
    YO_ERROR_INFO err;
    YO_AUTORELEASE *auto_pool;
    YO_JMPBUF jb[YO_INI_JB_COUNT];
  } YO_C_SUPPORT_INFO;

#ifdef _YO_CORE_BUILTIN
YO_TLS_DEFINE(Yo_Csup_Nfo_Tls);
#else
YO_TLS_DECLARE(Yo_Csup_Nfo_Tls);
#endif

YO_C_SUPPORT_INFO *Yo_Acquire_Csup_Nfo()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Malloc_Npl(sizeof(YO_C_SUPPORT_INFO));
    memset(nfo,0,sizeof(*nfo));
    nfo->jb_count = sizeof(nfo->jb)/sizeof(nfo->jb[0]);
    nfo->jb_top = -1;
    nfo->auto_pool = Yo_Malloc_Npl(sizeof(*nfo->auto_pool)*YO_INI_POOL_COUNT);
    nfo->auto_count = YO_INI_POOL_COUNT;
    nfo->auto_top = -1;
    Yo_Tls_Set(Yo_Csup_Nfo_Tls,nfo);
    return nfo;
  }
#endif
  ;

YO_C_SUPPORT_INFO *Yo_C_Support_Nfo()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
    return nfo;
  }
#endif
  ;

YO_C_SUPPORT_INFO *Yo_Extend_Csup_JmpBuf()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    nfo = Yo_Realloc_Npl(nfo,sizeof(YO_C_SUPPORT_INFO)
              + (nfo->jb_count - YO_INI_JB_COUNT + YO_EXT_JB_COUNT)*sizeof(YO_JMPBUF));
    nfo->jb_count += YO_EXT_JB_COUNT;
    Yo_Tls_Set(Yo_Csup_Nfo_Tls,nfo);
    return nfo;
  }
#endif
  ;

void Yo_Extend_Csup_Autopool()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    uint_t ncount = nfo->auto_count + YO_EXT_POOL_COUNT;
    nfo->auto_pool = Yo_Realloc_Npl(nfo->auto_pool,sizeof(*nfo->auto_pool)*ncount);
    nfo->auto_count = ncount;
    //nfo->auto_top = ncount;
  }
#endif
  ;
................................................................................

void *Yo_Unrefe(void *p);
void *Yo_Pool_Ptr(void *ptr,void *cleanup)
#ifdef _YO_CORE_BUILTIN
  {
    if ( ptr || cleanup == Yo_Pool_Marker_Tag )
      {
        YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
        if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
        STRICT_REQUIRE( (cleanup == Yo_Pool_Marker_Tag)
                      ||(cleanup == Yo_Unrefe)
                      ||!Yo_Find_Ptr_In_Pool(nfo,ptr) );

        ++nfo->auto_top;
        STRICT_REQUIRE(nfo->auto_top <= nfo->auto_count);
................................................................................

#define Yo_Release(Pooled) ((void)Yo_Unpool((Pooled),1))
#define Yo_Retain(Pooled) Yo_Unpool((Pooled),0)

void *Yo_Unpool(void *pooled,int do_cleanup)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && pooled )
      {
        int n = nfo->auto_top;
        while ( n >= 0 )
          {
            if ( nfo->auto_pool[n].ptr == pooled )
              {
................................................................................
  }
#endif
  ;

void *Yo_Unwind_Scope(void *pooled,int min_top,void *mark)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    int L = min_top>=0?min_top:0;
    //int counter = 0;
    if ( nfo )
      {
        YO_AUTORELEASE *q_p = 0;
        nfo->stats.unwinding = 1;
        while ( nfo->auto_top >= L )
................................................................................

#define Yo_Refresh(Old,New) Yo_Refresh_Ptr(Old,New,0)
void *Yo_Refresh_Ptr(void *old,void *new,void *cleaner)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo;
    REQUIRE( new != 0 );
    if ( old && !!(nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls)) )
      {
        YO_AUTORELEASE *p = Yo_Find_Ptr_In_Pool(nfo,old);
        if ( !p ) Yo_Fatal(YO_ERROR_OUT_OF_POOL,old,0,0);
        p->ptr = new;
      }
    else
      Yo_Pool_Ptr(new,cleaner);
................................................................................
  ;

void Yo_Thread_Cleanup()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo;
    Yo_Unwind_Scope(0,-1,0);
    if ( !!(nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls)) )
      {
        free(nfo->err.msg);
        free(nfo->auto_pool);
        free(nfo);
        Yo_Tls_Set(Yo_Csup_Nfo_Tls,0);
      }
  }
#endif
  ;

void Yo_Global_Cleanup()
#ifdef _YO_CORE_BUILTIN
................................................................................

char *Yo_Format_Npl(char *fmt,...)
  _YO_CORE_BUILTIN_CODE({va_list va;char *t; va_start(va,fmt); t = Yo_Format_(fmt,va);va_end(va); return t;});

char *Yo_Format(char *fmt,...)
  _YO_CORE_BUILTIN_CODE({va_list va;char *t; va_start(va,fmt); t = Yo_Pool(Yo_Format_(fmt,va));va_end(va); return t;});

void Yo_Print_FILE(FILE *st, char *text, unsigned flags)
#ifdef _YO_CORE_BUILTIN
  {
    __Xchg_Interlock
      {
        fputs(text,st);
        if ( flags & YO_PRINT_NEWLINE ) fputc('\n',st);
        if ( flags & YO_PRINT_FLUSH ) fflush(st);
      }
  }
#endif
  ;

#define StdOut_Print(Text) Yo_Print_FILE(stdout,Text,YO_PRINT_FLUSH)
#define StdOut_Print_Nl(Text) Yo_Print_FILE(stdout,Text,YO_PRINT_FLUSH|YO_PRINT_NEWLINE)
#define StdErr_Print(Text) Yo_Print_FILE(stderr,Text,YO_PRINT_FLUSH)
#define StdErr_Print_Nl(Text) Yo_Print_FILE(stderr,Text,YO_PRINT_FLUSH|YO_PRINT_NEWLINE)

typedef struct _YO_FUNCTABLE
  {
................................................................................
      }
    else
      {
        T->dynamic = Yo_Malloc_Npl(sizeof(YO_DYNAMIC));
        T->dynamic->contsig = YO_DYNCO_NYD<<8;
      }

    T->dynamic->typeid = Yo_Atomic_Increment(&Yo_Typeid_Counter);
    f = T->dynamic->funcs+(T->dynamic->contsig&0x0ff);
    ++T->dynamic->contsig;
    f->name = func_name;
    f->func = func;

    return o;
  }
................................................................................
  {
    void **Q = p;
    STRICT_REQUIRE(plidx >= 0 && plidx <YO_MEMPOOL_SLOTS_COUNT); 
    for (;;)
      {
        void * volatile *S = Yo_Mempool_Slots+plidx;
        *Q = *S;
        if ( Yo_Atomic_CmpXchg_Ptr(S,Q,*Q) ) break;
      }
  }
#endif
  ;
  
void Yo_Mempool_Extend(int plidx)
#ifdef _YO_CORE_BUILTIN
................................................................................
            int i,piece = (plidx+1)*YO_MEMPOOL_PIECE_STEP; 
            void **Q = Yo_Malloc_Npl(piece*YO_MEMPOOL_PIECE_ON_BLOCK);
            Yo_Mempool_Counts[plidx] += YO_MEMPOOL_PIECE_ON_BLOCK;
            for ( i=0; i < YO_MEMPOOL_PIECE_ON_BLOCK; ++i, Q = (void**)((byte_t*)Q + piece) )
              for (;;)
                {
                  *Q = *S;
                  if ( Yo_Atomic_CmpXchg_Ptr(S,Q,*Q) ) break;
                }
          }
      }
  }
#endif
  ;
  
................................................................................
    for (;;)
      {
        void * volatile *S = Yo_Mempool_Slots+plidx;
        Q = *S;
        if ( !Q ) 
          Yo_Mempool_Extend(plidx);
        else
          if ( Yo_Atomic_CmpXchg_Ptr(S,*Q,Q) ) break;
      }
    memset(Q,0,(plidx+1)*YO_MEMPOOL_PIECE_STEP);
    return Q;              
  }
#endif
  ;

................................................................................
        {
          YO_DYNAMIC *dynco = (YO_DYNAMIC*)tbl;
          if ( !dynco->contsig )
            {
              int count;
              for ( count = 0; tbl[count+1].name; ) { ++count; }
              dynco->contsig = (YO_DYNCO_ATS<<8)|count;
              dynco->typeid = Yo_Atomic_Increment(&Yo_Typeid_Counter);
            }
        }}

    return Yo_Pool_Ptr(o+1,Yo_Unrefe);
  }
#endif
  ;
................................................................................
    int Sz = Yo_Align(sizeof(YO_OBJECT)+size);
    YO_OBJECT *o = Yo_Object_Alloc(Sz+sizeof(YO_DYNAMIC));
    o->rc = 1;
    o->dynamic = (YO_DYNAMIC*)((char*)o + Sz);
    o->dynamic->contsig = (YO_DYNCO_ATS<<8)|1;
    o->dynamic->funcs[0].name = Oj_Destruct_OjMID;
    o->dynamic->funcs[0].func = dtor;
    o->dynamic->typeid = Yo_Atomic_Increment(&Yo_Typeid_Counter);
    return Yo_Pool_Ptr(o+1,Yo_Unrefe);
  }
#endif
  ;

void Yo_Object_Destruct(void *ptr)
#ifdef _YO_CORE_BUILTIN
................................................................................
#endif
  ;

void *Yo_Refe(void *p)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p && STRICT_CHECK(YO_SIGNAT_IS_OK(p)) )
      Yo_Atomic_Increment(&YO_RC(p));
    return p;
  }
#endif
  ;

#ifdef _YO_CORE_BUILTIN
void *Yo_Unrefe(void *p)
  {
    if ( p && STRICT_CHECK(YO_SIGNAT_IS_OK(p))
           && !(Yo_Atomic_Decrement(&YO_RC(p))&0x7fffff) )
      {
        void (*destruct)(void *) = Yo_Find_Method_Of(&p,Oj_Destruct_OjMID,0);
        if ( !destruct )
          Yo_Object_Destruct(p);
        else
          destruct(p);
        return 0;
................................................................................
        /* switch jumps to here */ \
        case 0: if (1) { Prolog; goto YO_LOCAL_ID(ap_Body); } else \
           YO_LOCAL_ID(ap_Body):

YO_ERROR_INFO *Error_Info()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && nfo->err.code )
      return &nfo->err;
    else
      return 0;
  }
#endif
  ;
................................................................................
    YO_ERROR_INFO *info = Error_Info();
    if ( info )
      return info->lineno;
    return 0;
  }
#endif
  ;














char *Error_Print_N_Exit(char *prefix, int code)
#ifdef _YO_CORE_BUILTIN
  {
    StdErr_Print_Nl(Yo_Format(__yoTa("%s: %s",0),prefix,Error_Message()));
    exit(code);
  }
#endif
  ;

#define Yo_Pop_JmpBuf() \
   (--((YO_C_SUPPORT_INFO *)Yo_Tls_Get(Yo_Csup_Nfo_Tls))->jb_top)

YO_JMPBUF *Yo_Push_JmpBuf(void)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_C_Support_Nfo();
    YO_JMPBUF *jb;

................................................................................
  }
#endif
  ;

void Yo_JmpBuf_Push_Cs(void *cs,Yo_JMPBUF_Unlock unlock)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    STRICT_REQUIRE ( cs );
    if ( nfo && cs )
      {
        STRICT_REQUIRE(nfo->jb_top < nfo->jb_count);
        STRICT_REQUIRE(nfo->jb_top >= -1);
        if ( nfo->jb_top > -1 && !nfo->stats.unwinding )
          {
................................................................................
#endif
  ;


void Yo_JmpBuf_Pop_Cs(void *cs)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && cs )
      {
        STRICT_REQUIRE(nfo->jb_top < nfo->jb_count);
        STRICT_REQUIRE(nfo->jb_top >= -1);
        if ( nfo->jb_top > -1 && !nfo->stats.unwinding )
          {
            int i;
................................................................................
  ;

char *Yo_Btrace(void);

#ifdef _YO_CORE_BUILTIN
__No_Return void _Yo_Raise(int err,char *msg,char *filename,int lineno)
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Tls_Get(Yo_Csup_Nfo_Tls);
    STRICT_REQUIRE( !nfo || nfo->jb_top < nfo->jb_count );

    printf(__yoTa("err: %d, msg: %s, filename: %s, lineno: %d\n",0),err,msg,filename,lineno);
  
  #if defined _DEBUG && defined __windoze
    if ( IsDebuggerPresent() ) __debugbreak();
  #endif  
................................................................................

#define __Object(Size,Funcs)            Yo_Object(Size,Funcs)
#define __Object_Dtor(Size,Dtor)        Yo_Object_Dtor(Size,Dtor)
#define __Object_Extend(Obj,Name,Func)  Yo_Object_Extend(Obj,Name,Func)
#define __Destruct(Ptr)                 Yo_Object_Destruct(Ptr)
#define __Clone(Size,Ptr)               Yo_Object_Clone(Size,Ptr)

#define __Atomic_Increment(Ptr)         Yo_Atomic_Increment(Ptr)
#define __Atomic_Decrement(Ptr)         Yo_Atomic_Decrement(Ptr)
#define __Tls_Define(Name)              YO_TLS_DEDINE(Name)
#define __Tls_Declare(Name)             YO_TLS_DECLARE(Name)
#define __Tls_Set(Name,Val)             Yo_Tls_Set(Name,Val)
#define __Tls_Get(Name)                 Yo_Tls_Get(Name)

#ifdef _STRONGPOOL

#define __Auto_Release \
  switch ( 0 ) while ( 1 ) \
    if ( 1 ) \
      { \
        Yo_Unwind_Scope(0,0,0); \
................................................................................
        goto YO_LOCAL_ID(ap_Body);\
      } \
    else \
      YO_LOCAL_ID(ap_Body):

#endif /* _STRONGPOOL */

#define __Interlock_Opt(Decl,Lx,Lock,Unlock,Unlock_Proc) \
  switch ( 0 ) while ( 1 ) \
    if ( 1 ) \
      goto YO_LOCAL_ID(Do_Unlock); \
    else if ( 1 ) \
      case 0: \
        { \
          Decl;\
          Lock(Lx); \
          Yo_JmpBuf_Push_Cs(Lx,(Yo_JMPBUF_Unlock)Unlock_Proc); \
          goto YO_LOCAL_ID(Do_Code); \
        YO_LOCAL_ID(Do_Unlock): \
          Yo_JmpBuf_Pop_Cs(Lx); \
          Unlock(Lx); \
          break; \
        } \
    else \
      YO_LOCAL_ID(Do_Code):

#define __Gogo \
  if ( 1 ) goto YO_LOCAL_ID(__gogo); \
  else YO_LOCAL_ID(__gogo):

#define __Elm_Resize_Npl(MemPptr,L,Width,CpsPtr) Yo_Elm_Resize_Npl((void**)MemPptr,L,Width,CpsPtr)
void Yo_Elm_Resize_Npl(void **inout, int L, int type_width, int *capacity_ptr)
#ifdef _YO_CORE_BUILTIN







>
>
>
>
>
>
>
>
>
>
>




<
|
<







 







|







 







|
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|
|
|
|

<
>
|
|
|












|
>
>
>
>

<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>
>
>
>
>


|









|
|
<
|
<
<
<
<







 







|







 







|







 







|







 







<
<







 







|

|












|








|









|



|








|







 







|







 







|







 







|







 







|







 







|




|







 







<
<
<
<
<
<
<
<
<
<
<
<
<







 







|







 







|







 







|







 







|







 







|







 







|







 







|









|







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>











|







 







|







 







|







 







|







 







<
<
<
<
<
<
<







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







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
...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
...
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
...
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
...
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
...
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
...
937
938
939
940
941
942
943


944
945
946
947
948
949
950
...
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
....
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
....
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
....
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
....
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
....
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
....
1265
1266
1267
1268
1269
1270
1271













1272
1273
1274
1275
1276
1277
1278
....
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
....
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
....
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
....
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
....
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
....
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
....
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
....
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
....
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
....
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
....
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
....
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
....
2198
2199
2200
2201
2202
2203
2204







2205
2206
2207
2208
2209
2210
2211
....
2254
2255
2256
2257
2258
2259
2260



















2261
2262
2263
2264
2265
2266
2267
# include <windef.h>
# include <winbase.h>
# include <excpt.h>
# include <objbase.h>
# include <io.h>
# include <process.h>
# include <malloc.h> /* alloca */
# ifdef _MSC_VER
#  include <intrin.h>
# endif
# ifdef _PTHREADS 
#   define _XTHREADS
#   ifdef __GNUC__
#     include <pthread.h>
#   else
#     include "otros/winpthreads.hc" /* testing purposes only! */ 
#   endif
# endif
#else
# include <sys/time.h>
# include <unistd.h>
# include <dlfcn.h>

# include <pthread.h>

# if defined __APPLE__
#   include <malloc/malloc.h> /* malloc_size */
# elif defined __NetBSD__
#   ifndef _NBSDMUTE
#     warning {! NetBSD has malloc_usable_size commented !}
#   endif
#   define malloc_size(Ptr) (0)
................................................................................
typedef unsigned int   udword_t; /* 32 bit unsigned ( i386 word ) */
typedef int            dword_t;  /* 32 bit signed ( i386 word )   */

typedef unsigned long  ulong_t;

#ifndef __windoze
  typedef unsigned long long  uquad_t; /* 64-bit unsigned ( double word ) historically named as quad word */
# if !defined __APPLE__ && !defined __linux__
    typedef long long  quad_t; /* 64-bit signed word ( double word ) */
# endif
#else
  typedef unsigned __int64  uquad_t;
  typedef __int64  quad_t;
#endif

................................................................................

__No_Return void _Yo_Fatal(int err,void *ctx,char *filename,int lineno);
__No_Return void _Yo_Raise(int err,char *msg,char *filename,int lineno);

#ifdef __windoze
#define Switch_to_Thread() SwitchToThread()
#else
#define Switch_to_Thread() pthread_yield()
#define Sleep(Ms) usleep((Ms)*1000)
#endif

#if defined __GNUC__
# define __RwBarrier() __asm__ __volatile__ ("" ::: "memory")
# define Atomic_Increment(Ptr) __sync_sub_and_fetch((i32_t volatile *)Ptr,1)
# define Atomic_Decrement(Ptr) __sync_sub_and_fetch((i32_t volatile *)Ptr,1)
# define Atomic_CmpXchg(Ptr,Val,Comp) __sync_bool_compare_and_swap((u32_t *volatile)Ptr,(u32_t)Comp,(u32_t)Val)
# define Atomic_CmpXchg_Ptr(Ptr,Val,Comp) __sync_bool_compare_and_swap((void *volatile*)Ptr,(void*)Comp,(void*)Val)
#elif defined _MSC_VER
# pragma intrinsic(_ReadWriteBarrier)
# define __RwBarrier() _ReadWriteBarrier()
# define Atomic_Increment(Ptr) InterlockedIncrement(Ptr)
# define Atomic_Decrement(Ptr) InterlockedDecrement(Ptr)
# define Atomic_CmpXchg(Ptr,Val,Comp) (InterlockedCompareExchange(Ptr,Val,Comp) == (Comp))
# define Atomic_CmpXchg_Ptr(Ptr,Val,Comp) (InterlockedCompareExchangePointer(Ptr,Val,Comp) == (Comp))
#endif 

typedef void (*Yo_JMPBUF_Unlock)(void *);
void Yo_JmpBuf_Push_Cs(void *cs,Yo_JMPBUF_Unlock unlock);
void Yo_JmpBuf_Pop_Cs(void *cs);

#define __Interlock_Opt(Decl,Lx,Lock,Unlock,Unlock_Proc) \
  switch ( 0 ) while ( 1 ) \
    if ( 1 ) \
      goto YO_LOCAL_ID(Do_Unlock); \
    else if ( 1 ) \
      case 0: \
        { \
          Decl;\
          Lock(Lx); \
          Yo_JmpBuf_Push_Cs(Lx,(Yo_JMPBUF_Unlock)Unlock_Proc); \
          goto YO_LOCAL_ID(Do_Code); \
        YO_LOCAL_ID(Do_Unlock): \
          Yo_JmpBuf_Pop_Cs(Lx); \
          Unlock(Lx); \
          break; \
        } \
    else \
      YO_LOCAL_ID(Do_Code):


#ifndef _THREADS

# define __Atomic_Increment(Ptr) (++*(Ptr))
# define __Atomic_Decrement(Ptr) (--*(Ptr))
# define __Atomic_CmpXchg(Ptr,Val,Comp) ( *(Ptr) == (Comp) ? (*(Ptr) = (Val), 1) : 0 )
# define __Atomic_CmpXchg_Ptr(Ptr,Val,Comp) ( *(Ptr) == (Comp) ? (*(Ptr) = (Val), 1) : 0 )


# define __Tls_Define(Name)  void *Name = 0
# define __Tls_Declare(Name) extern void *Name
# define __Tls_Set(Name,Val) ((Name) = (Val))
# define __Tls_Get(Name)     (Name)

# define YO_MULTITHREADED(Expr)

# define __Xchg_Interlock if (0) {;} else
# define __Xchg_Sync(Lx)  if (0) {;} else
# define Yo_Wait_Xchg_Lock(Ptr)
# define Yo_Xchg_Unlock(Ptr)

# define __Mtx_Sync(Mtx)  if (0) {;} else

#else /* -D _THREADS */

#if defined _MSC_VER && !defined _PTHREADS && !defined _RTTLS
# define __Tls_Define(Name)  __declspec(thread) void *Name = 0
# define __Tls_Declare(Name) extern __declspec(thread) void *Name
# define __Tls_Set(Name,Val) ((Name) = (Val))
# define __Tls_Get(Name)     (Name)
#else

# define __Tls_Set(Name,Val) Tls_Set(&Name,Val)
# define __Tls_Get(Name)     Tls_Get(Name)
# if defined __windoze && !defined _PTHREADS
#   define INVALID_TLS_VALUE TLS_OUT_OF_INDEXES
#   define __Tls_Define(Name)  ulong_t volatile Name = INVALID_TLS_VALUE;
#   define __Tls_Declare(Name) extern ulong_t volatile Name;

void Tls_Set(ulong_t volatile *name, void *val)
  {
    if ( *name == INVALID_TLS_VALUE ) 
      {
        ulong_t tmp = TlsAlloc();
        if ( InterlockedCompareExchange(name,tmp,INVALID_TLS_VALUE) != INVALID_TLS_VALUE )
          TlsFree(tmp);
      }
    TlsSetValue(*name,val);
  }

void *Tls_Get(ulong_t name)
  {
    if ( name == INVALID_TLS_VALUE ) 
      return 0;
    return TlsGetValue(name);
  }

# else // posix threads
#   define INVALID_TLS_VALUE ((pthread_key_t)-1) 
#   define __Tls_Define(Name)  pthread_key_t Name = INVALID_TLS_VALUE; /* pthread_once way is madness! */
#   define __Tls_Declare(Name) extern pthread_key_t Name;
# endif

void Tls_Set(pthread_key_t volatile *name, void *val)
  {
    if ( *name == INVALID_TLS_VALUE ) 
      {
        pthread_key_t tmp;
        pthread_key_create(&tmp,0);
        if ( !Atomic_CmpXchg(name,tmp,INVALID_TLS_VALUE) )
          pthread_key_delete(tmp);
      }
    pthread_setspecific(*name,val);
  }

void *Tls_Get(pthread_key_t name)
  {
    if ( name == INVALID_TLS_VALUE ) 
      return 0;
    return pthread_getspecific(name);
  }

#endif 

# define __Atomic_Increment(Ptr) Atomic_Increment(Ptr)
# define __Atomic_Decrement(Ptr) Atomic_Decrement(Ptr)
# define __Atomic_CmpXchg(Ptr,Val,Comp) Atomic_CmpXchg(Ptr,Val,Comp)
# define __Atomic_CmpXchg_Ptr(Ptr,Val,Comp) Atomic_CmpXchg_Ptr(Ptr,Val,Comp)

# define YO_MULTITHREADED(Expr) Expr

# define _xchg_YO_LOCAL_LX static int YO_LOCAL_ID(lx)
# define _xchg_YO_LOCAL_ID_REF &YO_LOCAL_ID(lx)
# define __Xchg_Interlock \
              __Interlock_Opt( _xchg_YO_LOCAL_LX, _xchg_YO_LOCAL_ID_REF, \
                  Yo_Wait_Xchg_Lock,Yo_Xchg_Unlock,Yo_Xchg_Unlock_Proc)

# define __Xchg_Sync(Lx) \
              __Interlock_Opt(((void)0),Lx, \
                  Yo_Wait_Xchg_Lock,Yo_Xchg_Unlock,Yo_Xchg_Unlock_Proc)

# define Yo_Wait_Xchg_Lock(Ptr) while ( !__Atomic_CmpXchg(Ptr,1,0) ) Switch_to_Thread()
# define Yo_Xchg_Unlock(Ptr) __Atomic_CmpXchg(Ptr,0,1)

void Yo_Xchg_Unlock_Proc(int volatile *p) _YO_CORE_BUILTIN_CODE({__Atomic_CmpXchg(p,0,1);});





#endif
  
#if defined __GNUC__ && ( __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__>=6) )
  #define __Static_Assert_S(expr,S) _Static_assert(expr,S)
#elif !defined _MSC_VER || _MSC_VER < 1600 || defined __ICL
  #define __Static_Assert_S(expr,S) \
................................................................................
# define STRICT_REQUIRE(Expr) REQUIRE(Expr)
# define STRICT_CHECK(Expr) (Expr)
#else
# define STRICT_REQUIRE(Expr) ((void)0)
# define STRICT_CHECK(Expr) (1)
#endif /* _STRICT */

#define Slist_Remove_By(ListPtr,Next,Val) Yo_Slist_Remove((void**)ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)),Val)
#define Slist_Remove(ListPtr,Val) Slist_Remove_By(ListPtr,next,Val)

void Yo_Slist_Remove(void **p, int offs_of_next, void *val)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p ) 
      {
................................................................................
              p =  (void**)((byte_t*)*p + offs_of_next);
          }
      }
  }
#endif
  ;

#define Slist_Push_By(ListPtr,Next,Val) Yo_Slist_Push((void**)ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)),Val)
#define Slist_Push(ListPtr,Val) Slist_Push_By(ListPtr,next,Val)

void Yo_Slist_Push(void **p, int offs_of_next, void *val)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p ) 
      {
................................................................................
        *p = val;
        *(void**)((byte_t*)*p + offs_of_next) = 0;
      }
  }
#endif
  ;
  
#define Slist_Pop_By(ListPtr,Next) Yo_Slist_Pop((void**)ListPtr,(int)((size_t)(&(*ListPtr)->Next)-(size_t)(*ListPtr)))
#define Slist_Pop(ListPtr) Slist_Pop_By(ListPtr,next)

void *Yo_Slist_Pop(void **p, int offs_of_next)
#ifdef _YO_CORE_BUILTIN
  {
    void *r = 0;
    
................................................................................

enum { YO_MAX_CS_COUNT = 7 };
enum { YO_INI_JB_COUNT = 5 };
enum { YO_EXT_JB_COUNT = 3 };
enum { YO_INI_POOL_COUNT = 256 };
enum { YO_EXT_POOL_COUNT = 128 };



typedef struct _YO_JMPBUF_LOCK
  {
    void *cs;
    Yo_JMPBUF_Unlock unlock;
  } YO_JMPBUF_LOCK;

typedef struct _YO_JMPBUF
................................................................................
      } stats;
    YO_ERROR_INFO err;
    YO_AUTORELEASE *auto_pool;
    YO_JMPBUF jb[YO_INI_JB_COUNT];
  } YO_C_SUPPORT_INFO;

#ifdef _YO_CORE_BUILTIN
__Tls_Define(Yo_Csup_Nfo_Tls);
#else
__Tls_Declare(Yo_Csup_Nfo_Tls);
#endif

YO_C_SUPPORT_INFO *Yo_Acquire_Csup_Nfo()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_Malloc_Npl(sizeof(YO_C_SUPPORT_INFO));
    memset(nfo,0,sizeof(*nfo));
    nfo->jb_count = sizeof(nfo->jb)/sizeof(nfo->jb[0]);
    nfo->jb_top = -1;
    nfo->auto_pool = Yo_Malloc_Npl(sizeof(*nfo->auto_pool)*YO_INI_POOL_COUNT);
    nfo->auto_count = YO_INI_POOL_COUNT;
    nfo->auto_top = -1;
    __Tls_Set(Yo_Csup_Nfo_Tls,nfo);
    return nfo;
  }
#endif
  ;

YO_C_SUPPORT_INFO *Yo_C_Support_Nfo()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
    return nfo;
  }
#endif
  ;

YO_C_SUPPORT_INFO *Yo_Extend_Csup_JmpBuf()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    nfo = Yo_Realloc_Npl(nfo,sizeof(YO_C_SUPPORT_INFO)
              + (nfo->jb_count - YO_INI_JB_COUNT + YO_EXT_JB_COUNT)*sizeof(YO_JMPBUF));
    nfo->jb_count += YO_EXT_JB_COUNT;
    __Tls_Set(Yo_Csup_Nfo_Tls,nfo);
    return nfo;
  }
#endif
  ;

void Yo_Extend_Csup_Autopool()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    uint_t ncount = nfo->auto_count + YO_EXT_POOL_COUNT;
    nfo->auto_pool = Yo_Realloc_Npl(nfo->auto_pool,sizeof(*nfo->auto_pool)*ncount);
    nfo->auto_count = ncount;
    //nfo->auto_top = ncount;
  }
#endif
  ;
................................................................................

void *Yo_Unrefe(void *p);
void *Yo_Pool_Ptr(void *ptr,void *cleanup)
#ifdef _YO_CORE_BUILTIN
  {
    if ( ptr || cleanup == Yo_Pool_Marker_Tag )
      {
        YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
        if ( !nfo ) nfo = Yo_Acquire_Csup_Nfo();
        STRICT_REQUIRE( (cleanup == Yo_Pool_Marker_Tag)
                      ||(cleanup == Yo_Unrefe)
                      ||!Yo_Find_Ptr_In_Pool(nfo,ptr) );

        ++nfo->auto_top;
        STRICT_REQUIRE(nfo->auto_top <= nfo->auto_count);
................................................................................

#define Yo_Release(Pooled) ((void)Yo_Unpool((Pooled),1))
#define Yo_Retain(Pooled) Yo_Unpool((Pooled),0)

void *Yo_Unpool(void *pooled,int do_cleanup)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && pooled )
      {
        int n = nfo->auto_top;
        while ( n >= 0 )
          {
            if ( nfo->auto_pool[n].ptr == pooled )
              {
................................................................................
  }
#endif
  ;

void *Yo_Unwind_Scope(void *pooled,int min_top,void *mark)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    int L = min_top>=0?min_top:0;
    //int counter = 0;
    if ( nfo )
      {
        YO_AUTORELEASE *q_p = 0;
        nfo->stats.unwinding = 1;
        while ( nfo->auto_top >= L )
................................................................................

#define Yo_Refresh(Old,New) Yo_Refresh_Ptr(Old,New,0)
void *Yo_Refresh_Ptr(void *old,void *new,void *cleaner)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo;
    REQUIRE( new != 0 );
    if ( old && !!(nfo = __Tls_Get(Yo_Csup_Nfo_Tls)) )
      {
        YO_AUTORELEASE *p = Yo_Find_Ptr_In_Pool(nfo,old);
        if ( !p ) Yo_Fatal(YO_ERROR_OUT_OF_POOL,old,0,0);
        p->ptr = new;
      }
    else
      Yo_Pool_Ptr(new,cleaner);
................................................................................
  ;

void Yo_Thread_Cleanup()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo;
    Yo_Unwind_Scope(0,-1,0);
    if ( !!(nfo = __Tls_Get(Yo_Csup_Nfo_Tls)) )
      {
        free(nfo->err.msg);
        free(nfo->auto_pool);
        free(nfo);
        __Tls_Set(Yo_Csup_Nfo_Tls,0);
      }
  }
#endif
  ;

void Yo_Global_Cleanup()
#ifdef _YO_CORE_BUILTIN
................................................................................

char *Yo_Format_Npl(char *fmt,...)
  _YO_CORE_BUILTIN_CODE({va_list va;char *t; va_start(va,fmt); t = Yo_Format_(fmt,va);va_end(va); return t;});

char *Yo_Format(char *fmt,...)
  _YO_CORE_BUILTIN_CODE({va_list va;char *t; va_start(va,fmt); t = Yo_Pool(Yo_Format_(fmt,va));va_end(va); return t;});














#define StdOut_Print(Text) Yo_Print_FILE(stdout,Text,YO_PRINT_FLUSH)
#define StdOut_Print_Nl(Text) Yo_Print_FILE(stdout,Text,YO_PRINT_FLUSH|YO_PRINT_NEWLINE)
#define StdErr_Print(Text) Yo_Print_FILE(stderr,Text,YO_PRINT_FLUSH)
#define StdErr_Print_Nl(Text) Yo_Print_FILE(stderr,Text,YO_PRINT_FLUSH|YO_PRINT_NEWLINE)

typedef struct _YO_FUNCTABLE
  {
................................................................................
      }
    else
      {
        T->dynamic = Yo_Malloc_Npl(sizeof(YO_DYNAMIC));
        T->dynamic->contsig = YO_DYNCO_NYD<<8;
      }

    T->dynamic->typeid = __Atomic_Increment(&Yo_Typeid_Counter);
    f = T->dynamic->funcs+(T->dynamic->contsig&0x0ff);
    ++T->dynamic->contsig;
    f->name = func_name;
    f->func = func;

    return o;
  }
................................................................................
  {
    void **Q = p;
    STRICT_REQUIRE(plidx >= 0 && plidx <YO_MEMPOOL_SLOTS_COUNT); 
    for (;;)
      {
        void * volatile *S = Yo_Mempool_Slots+plidx;
        *Q = *S;
        if ( __Atomic_CmpXchg_Ptr(S,Q,*Q) ) break;
      }
  }
#endif
  ;
  
void Yo_Mempool_Extend(int plidx)
#ifdef _YO_CORE_BUILTIN
................................................................................
            int i,piece = (plidx+1)*YO_MEMPOOL_PIECE_STEP; 
            void **Q = Yo_Malloc_Npl(piece*YO_MEMPOOL_PIECE_ON_BLOCK);
            Yo_Mempool_Counts[plidx] += YO_MEMPOOL_PIECE_ON_BLOCK;
            for ( i=0; i < YO_MEMPOOL_PIECE_ON_BLOCK; ++i, Q = (void**)((byte_t*)Q + piece) )
              for (;;)
                {
                  *Q = *S;
                  if ( __Atomic_CmpXchg_Ptr(S,Q,*Q) ) break;
                }
          }
      }
  }
#endif
  ;
  
................................................................................
    for (;;)
      {
        void * volatile *S = Yo_Mempool_Slots+plidx;
        Q = *S;
        if ( !Q ) 
          Yo_Mempool_Extend(plidx);
        else
          if ( __Atomic_CmpXchg_Ptr(S,*Q,Q) ) break;
      }
    memset(Q,0,(plidx+1)*YO_MEMPOOL_PIECE_STEP);
    return Q;              
  }
#endif
  ;

................................................................................
        {
          YO_DYNAMIC *dynco = (YO_DYNAMIC*)tbl;
          if ( !dynco->contsig )
            {
              int count;
              for ( count = 0; tbl[count+1].name; ) { ++count; }
              dynco->contsig = (YO_DYNCO_ATS<<8)|count;
              dynco->typeid = __Atomic_Increment(&Yo_Typeid_Counter);
            }
        }}

    return Yo_Pool_Ptr(o+1,Yo_Unrefe);
  }
#endif
  ;
................................................................................
    int Sz = Yo_Align(sizeof(YO_OBJECT)+size);
    YO_OBJECT *o = Yo_Object_Alloc(Sz+sizeof(YO_DYNAMIC));
    o->rc = 1;
    o->dynamic = (YO_DYNAMIC*)((char*)o + Sz);
    o->dynamic->contsig = (YO_DYNCO_ATS<<8)|1;
    o->dynamic->funcs[0].name = Oj_Destruct_OjMID;
    o->dynamic->funcs[0].func = dtor;
    o->dynamic->typeid = __Atomic_Increment(&Yo_Typeid_Counter);
    return Yo_Pool_Ptr(o+1,Yo_Unrefe);
  }
#endif
  ;

void Yo_Object_Destruct(void *ptr)
#ifdef _YO_CORE_BUILTIN
................................................................................
#endif
  ;

void *Yo_Refe(void *p)
#ifdef _YO_CORE_BUILTIN
  {
    if ( p && STRICT_CHECK(YO_SIGNAT_IS_OK(p)) )
      __Atomic_Increment(&YO_RC(p));
    return p;
  }
#endif
  ;

#ifdef _YO_CORE_BUILTIN
void *Yo_Unrefe(void *p)
  {
    if ( p && STRICT_CHECK(YO_SIGNAT_IS_OK(p))
           && !(__Atomic_Decrement(&YO_RC(p))&0x7fffff) )
      {
        void (*destruct)(void *) = Yo_Find_Method_Of(&p,Oj_Destruct_OjMID,0);
        if ( !destruct )
          Yo_Object_Destruct(p);
        else
          destruct(p);
        return 0;
................................................................................
        /* switch jumps to here */ \
        case 0: if (1) { Prolog; goto YO_LOCAL_ID(ap_Body); } else \
           YO_LOCAL_ID(ap_Body):

YO_ERROR_INFO *Error_Info()
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && nfo->err.code )
      return &nfo->err;
    else
      return 0;
  }
#endif
  ;
................................................................................
    YO_ERROR_INFO *info = Error_Info();
    if ( info )
      return info->lineno;
    return 0;
  }
#endif
  ;

void Yo_Print_FILE(FILE *st, char *text, unsigned flags)
#ifdef _YO_CORE_BUILTIN
  {
    __Xchg_Interlock
      {
        fputs(text,st);
        if ( flags & YO_PRINT_NEWLINE ) fputc('\n',st);
        if ( flags & YO_PRINT_FLUSH ) fflush(st);
      }
  }
#endif
  ;

char *Error_Print_N_Exit(char *prefix, int code)
#ifdef _YO_CORE_BUILTIN
  {
    StdErr_Print_Nl(Yo_Format(__yoTa("%s: %s",0),prefix,Error_Message()));
    exit(code);
  }
#endif
  ;

#define Yo_Pop_JmpBuf() \
   (--((YO_C_SUPPORT_INFO *)__Tls_Get(Yo_Csup_Nfo_Tls))->jb_top)

YO_JMPBUF *Yo_Push_JmpBuf(void)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = Yo_C_Support_Nfo();
    YO_JMPBUF *jb;

................................................................................
  }
#endif
  ;

void Yo_JmpBuf_Push_Cs(void *cs,Yo_JMPBUF_Unlock unlock)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    STRICT_REQUIRE ( cs );
    if ( nfo && cs )
      {
        STRICT_REQUIRE(nfo->jb_top < nfo->jb_count);
        STRICT_REQUIRE(nfo->jb_top >= -1);
        if ( nfo->jb_top > -1 && !nfo->stats.unwinding )
          {
................................................................................
#endif
  ;


void Yo_JmpBuf_Pop_Cs(void *cs)
#ifdef _YO_CORE_BUILTIN
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    if ( nfo && cs )
      {
        STRICT_REQUIRE(nfo->jb_top < nfo->jb_count);
        STRICT_REQUIRE(nfo->jb_top >= -1);
        if ( nfo->jb_top > -1 && !nfo->stats.unwinding )
          {
            int i;
................................................................................
  ;

char *Yo_Btrace(void);

#ifdef _YO_CORE_BUILTIN
__No_Return void _Yo_Raise(int err,char *msg,char *filename,int lineno)
  {
    YO_C_SUPPORT_INFO *nfo = __Tls_Get(Yo_Csup_Nfo_Tls);
    STRICT_REQUIRE( !nfo || nfo->jb_top < nfo->jb_count );

    printf(__yoTa("err: %d, msg: %s, filename: %s, lineno: %d\n",0),err,msg,filename,lineno);
  
  #if defined _DEBUG && defined __windoze
    if ( IsDebuggerPresent() ) __debugbreak();
  #endif  
................................................................................

#define __Object(Size,Funcs)            Yo_Object(Size,Funcs)
#define __Object_Dtor(Size,Dtor)        Yo_Object_Dtor(Size,Dtor)
#define __Object_Extend(Obj,Name,Func)  Yo_Object_Extend(Obj,Name,Func)
#define __Destruct(Ptr)                 Yo_Object_Destruct(Ptr)
#define __Clone(Size,Ptr)               Yo_Object_Clone(Size,Ptr)








#ifdef _STRONGPOOL

#define __Auto_Release \
  switch ( 0 ) while ( 1 ) \
    if ( 1 ) \
      { \
        Yo_Unwind_Scope(0,0,0); \
................................................................................
        goto YO_LOCAL_ID(ap_Body);\
      } \
    else \
      YO_LOCAL_ID(ap_Body):

#endif /* _STRONGPOOL */




















#define __Gogo \
  if ( 1 ) goto YO_LOCAL_ID(__gogo); \
  else YO_LOCAL_ID(__gogo):

#define __Elm_Resize_Npl(MemPptr,L,Width,CpsPtr) Yo_Elm_Resize_Npl((void**)MemPptr,L,Width,CpsPtr)
void Yo_Elm_Resize_Npl(void **inout, int L, int type_width, int *capacity_ptr)
#ifdef _YO_CORE_BUILTIN