Unnamed Fossil Project

Check-in [5cba536d25]
Login

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

Overview
Comment:xtmpl added
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:5cba536d257a71c152b890d3082f3ba602de4dff
User & Date: monster 2012-08-03 06:54:42
Context
2012-08-07
05:12
updated. check-in: 5efd835985 user: monster tags: trunk
2012-08-03
06:54
xtmpl added check-in: 5cba536d25 user: monster tags: trunk
2012-07-30
05:25
(no comment) check-in: 6ac907cea0 user: monster tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to buffer.hc.

53
54
55
56
57
58
59



60

61
62
63
64
65
66
67
#include "yoyo.hc"

/* string.hc */
char *Str_Hex_Byte(byte_t val,char pfx,void *out);

typedef struct _YO_BUFFER
  { 



    byte_t *at; 

    int count;
    int capacity;
  } YO_BUFFER;

void Buffer_Clear(YO_BUFFER *bf)
#ifdef _YO_BUFFER_BUILTIN
  {







>
>
>
|
>







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "yoyo.hc"

/* string.hc */
char *Str_Hex_Byte(byte_t val,char pfx,void *out);

typedef struct _YO_BUFFER
  { 
    union {
      char   *at; 
      char   *chars;
      byte_t *bytes;
    };
    int count;
    int capacity;
  } YO_BUFFER;

void Buffer_Clear(YO_BUFFER *bf)
#ifdef _YO_BUFFER_BUILTIN
  {

Changes to cgi.hc.

78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
108
109
110
111
112
113
114

115
116

117
118
119
120
121
122
123
...
170
171
172
173
174
175
176


177
178
179
180
181
182
183
...
451
452
453
454
455
456
457

458

459
460
461
462
463
464
465
...
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
    YO_CGI_FAST_CGI_1_0   = 200100,
    YO_CGI_FAST_CGI_1_1   = 200101,
    YO_CGI_NO_CONTENT     = 0,
    YO_CGI_URLENCODED     = 1,
    YO_CGI_OCTETSTREAM    = 2,
    YO_CGI_MULTIPART      = 3,
    YO_CGI_POST_MAX_PAYLOAD = 64*KILOBYTE,
    YO_CGI_OUT_TEXTHTML = 200,
    YO_CGI_OUT_REDIRECT = 301,
    YO_CGI_OUT_DATASTREAM = 299,
    YO_CGI_OUT_NOTFOUND = 404,
  };

/* YO_CGI_COOKIE does not require specific destructor, use free */
typedef struct _YO_CGI_COOKIE
  {
    struct _YO_CGI_COOKIE *next;
    char *value;
................................................................................
    int status;
    char name[1];
  } YO_CGI_UPLOAD;

typedef struct _YO_CGI
  {
    YO_XNODE *params;

    char *server_software;
    char *server_name;

    char *request_uri;
    char *remote_addr;
    char *referer;
    char *query_string;
    char *path_info;
    char *content_boundary; /* can be null */
    char **langlist;
................................................................................
      {
        char **qoo;
        for ( qoo = self->langlist; *qoo; ++qoo )
          free(*qoo);
        free(self->langlist);
      }
    


    free(self->server_software);
    free(self->server_name);
    free(self->request_uri);
    free(self->remote_addr);
    free(self->referer);
    free(self->query_string);
    free(self->path_info);
................................................................................
    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 )
          {
................................................................................
  }
#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: ");







|
|
|
|







 







>


>







 







>
>







 







>

>







 







|





|







|







78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
...
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
...
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
    YO_CGI_FAST_CGI_1_0   = 200100,
    YO_CGI_FAST_CGI_1_1   = 200101,
    YO_CGI_NO_CONTENT     = 0,
    YO_CGI_URLENCODED     = 1,
    YO_CGI_OCTETSTREAM    = 2,
    YO_CGI_MULTIPART      = 3,
    YO_CGI_POST_MAX_PAYLOAD = 64*KILOBYTE,
    CGI_OUT_TEXTHTML      = 200,
    CGI_OUT_REDIRECT      = 301,
    CGI_OUT_DATASTREAM    = 299,
    CGI_OUT_NOTFOUND      = 404,
  };

/* YO_CGI_COOKIE does not require specific destructor, use free */
typedef struct _YO_CGI_COOKIE
  {
    struct _YO_CGI_COOKIE *next;
    char *value;
................................................................................
    int status;
    char name[1];
  } YO_CGI_UPLOAD;

typedef struct _YO_CGI
  {
    YO_XNODE *params;
    char *document_root;
    char *server_software;
    char *server_name;
    char *script_name;
    char *request_uri;
    char *remote_addr;
    char *referer;
    char *query_string;
    char *path_info;
    char *content_boundary; /* can be null */
    char **langlist;
................................................................................
      {
        char **qoo;
        for ( qoo = self->langlist; *qoo; ++qoo )
          free(*qoo);
        free(self->langlist);
      }
    
    free(self->document_root);
    free(self->script_name);
    free(self->server_software);
    free(self->server_name);
    free(self->request_uri);
    free(self->remote_addr);
    free(self->referer);
    free(self->query_string);
    free(self->path_info);
................................................................................
    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->script_name = Str_Copy_Npl(Cgi_Get_Env(env,"SCRIPT_NAME"),-1);    
    self->content_length = Str_To_Int_Dflt(Cgi_Get_Env(env,"CONTENT_LENGTH"),0);
    self->document_root = Str_Copy_Npl(Cgi_Get_Env(env,"DOCUMENT_ROOT"),-1);
    
    __Gogo
      {
        char *S = Cgi_Get_Env(env,"CONTENT_TYPE");
        self->content_type = Cgi_Recognize_Content_Type(S);
        if ( self->content_type == YO_CGI_MULTIPART )
          {
................................................................................
  }
#endif
  ;
  
void Cgi_Write_Out(YO_CGI *cgi, int out_status)
#ifdef _YO_CGI_BUILTIN
  {
    if ( out_status == 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 == 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 == 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: ");

Changes to httpx.hc.

421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
...
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
              {
                httpx->nline += L;
              }
          }
        else
          {
            int i = httpx->nline;
            byte_t *q = bf->at;
            while ( i < bf->count && q[i] != '\n' ) ++i;
            if ( q[i] == '\n' )
              {
                char *e = 0;
                ++i;
                if ( Isspace(*(q+httpx->nline)) ) 
                  {
................................................................................

    if ( eoh )
      {
        char *foo;
        Httpx_Analyze_Headers(httpx);
        httpx->left = 0;
        
        if ( foo = Dicto_Get(httpx->hdrs,"Connection",0) )
          {
            if ( !strcmp_I(foo,"Keep-Alive") )
              httpx->keepalive = 1;
          }

        if ( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) )
          {
            httpx->left = Yo_Minu(Str_To_Int(foo),httpx->maxcount);
          }
        else if ( strcmp_I(Dicto_Get(httpx->hdrs,"Transfer-Encoding",""),"chunked") == 0 )
          {
            httpx->chunked = 1;  
            httpx->nline = 0;
................................................................................
  ;
  
int Httpx_Content_Length(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    char *foo;
    if ( httpx->hdrs )
      if ( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) )
        {
          return Str_To_Int(foo);
        }
    __Raise(YO_ERROR_ILLFORMED,"httpx client doesn´t have required headers");
  }
#endif
  ;
  
void *Httpx_Content_Md5(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    char *foo;
    if ( httpx->hdrs )
      if ( foo = Dicto_Get(httpx->hdrs,"Content-MD5",0) )
        {
          return Str_Base64_Decode(foo,0);
        }
    return 0;
  }
#endif
  ;







|







 







|





|







 







|













|







421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
...
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
              {
                httpx->nline += L;
              }
          }
        else
          {
            int i = httpx->nline;
            char *q = bf->at;
            while ( i < bf->count && q[i] != '\n' ) ++i;
            if ( q[i] == '\n' )
              {
                char *e = 0;
                ++i;
                if ( Isspace(*(q+httpx->nline)) ) 
                  {
................................................................................

    if ( eoh )
      {
        char *foo;
        Httpx_Analyze_Headers(httpx);
        httpx->left = 0;
        
        if (( foo = Dicto_Get(httpx->hdrs,"Connection",0) ))
          {
            if ( !strcmp_I(foo,"Keep-Alive") )
              httpx->keepalive = 1;
          }

        if (( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) ))
          {
            httpx->left = Yo_Minu(Str_To_Int(foo),httpx->maxcount);
          }
        else if ( strcmp_I(Dicto_Get(httpx->hdrs,"Transfer-Encoding",""),"chunked") == 0 )
          {
            httpx->chunked = 1;  
            httpx->nline = 0;
................................................................................
  ;
  
int Httpx_Content_Length(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    char *foo;
    if ( httpx->hdrs )
      if (( foo = Dicto_Get(httpx->hdrs,"Content-Length",0) ))
        {
          return Str_To_Int(foo);
        }
    __Raise(YO_ERROR_ILLFORMED,"httpx client doesn´t have required headers");
  }
#endif
  ;
  
void *Httpx_Content_Md5(YO_HTTPX *httpx)
#ifdef _HTTPX_BUILTIN
  {
    char *foo;
    if ( httpx->hdrs )
      if (( foo = Dicto_Get(httpx->hdrs,"Content-MD5",0) ))
        {
          return Str_Base64_Decode(foo,0);
        }
    return 0;
  }
#endif
  ;

Changes to string.hc.

1329
1330
1331
1332
1333
1334
1335





















1336
1337
1338
1339
1340
1341
1342
    if ( !patt || !S ) return 0;

    while ( *patt )
      if ( *S++ != *patt++ )
        return 0;
    return 1;
  }





















#endif
  ;

int Str_Unicode_Starts_With(wchar_t *S, wchar_t *patt)
#ifdef _YO_STRING_BUILTIN
  {
    if ( !patt || !S ) return 0;







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







1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
    if ( !patt || !S ) return 0;

    while ( *patt )
      if ( *S++ != *patt++ )
        return 0;
    return 1;
  }
#endif
  ;

int Str_Ends_With(char *S, char *patt)
#ifdef _YO_STRING_BUILTIN
  {
    if ( patt && S )
      {
        int S_L = strlen(S);
        int patt_L = strlen(patt);
        if ( patt_L < S_L )
          {
            S += S_L-patt_L;
            while ( *patt )
              if ( *S++ != *patt++ )
                return 0;
            return 1;
          }
      }
    return 0;
  }
#endif
  ;

int Str_Unicode_Starts_With(wchar_t *S, wchar_t *patt)
#ifdef _YO_STRING_BUILTIN
  {
    if ( !patt || !S ) return 0;

Changes to tcpip.hc.

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
...
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
        ip->v4 = 0x0100007f;
        return 0;
      }
      
    ip->v4 = inet_addr(host);
    if ( ip->v4 != 0x0ffffffff ) return 0;
  l:
    if ( hstn = gethostbyname(host) )
      {
        memcpy(&ip->v4,hstn->h_addr,Yo_MAX(sizeof(ip->v4),hstn->h_length));
        return 0;
      }
    else
      {
        int err = Tcp_Errno();
................................................................................
  }
#endif
  ;
  
int Tcp_Read(YO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YO_TCPIP_BUILTIN  
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = recv(sok->skt,b,cc,0);
        if ( q < 0 )
          {
            int err = Tcp_Errno();
................................................................................
  }
#endif
  ;

int Tcp_Write(YO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YO_TCPIP_BUILTIN
  {
    byte_t *b = out;
    int cc = count;
    while ( cc )
      {
        int q = send(sok->skt,b,cc,0);
        if ( q < 0 )
          {
            int err = Tcp_Errno();
................................................................................
  }
#endif
  ;
  
void Wrk_Tcp_Send(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
  {
    byte_t *b = iot->dta;
    int count = iot->count;
    int cc = count;
    while ( cc )
      {
        int q = send(iot->skt,b,cc,0);
        if ( q < 0 )
          {
................................................................................
  }
#endif
  ;

void Wrk_Tcp_Recv(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
  {
    byte_t *b = iot->dta;
    int count = iot->count;
    int cc = count;
    while ( cc )
      {
        int q = recv(iot->skt,b,cc,0);
        if ( q < 0 )
          {
................................................................................
  }
#endif
  ;

YO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_TCPSOK *sok = Tcp_Socket(0);
    Tcp_Connect(sok,host,port);
    return sok;
  }
#endif
  ;

YO_TCPSOK *Tcp_Listen(char *host, int port, int listlen)
................................................................................
    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;







|







 







|







 







|







 







|







 







|







 







|







 







|







246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
...
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
...
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
...
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
...
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
...
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
...
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
        ip->v4 = 0x0100007f;
        return 0;
      }
      
    ip->v4 = inet_addr(host);
    if ( ip->v4 != 0x0ffffffff ) return 0;
  l:
    if ( !!(hstn = gethostbyname(host)) )
      {
        memcpy(&ip->v4,hstn->h_addr,Yo_MAX(sizeof(ip->v4),hstn->h_length));
        return 0;
      }
    else
      {
        int err = Tcp_Errno();
................................................................................
  }
#endif
  ;
  
int Tcp_Read(YO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YO_TCPIP_BUILTIN  
  {
    char *b = out;
    int cc = count;
    while ( cc )
      {
        int q = recv(sok->skt,b,cc,0);
        if ( q < 0 )
          {
            int err = Tcp_Errno();
................................................................................
  }
#endif
  ;

int Tcp_Write(YO_TCPSOK *sok, void *out, int count, int mincount)
#ifdef _YO_TCPIP_BUILTIN
  {
    char *b = out;
    int cc = count;
    while ( cc )
      {
        int q = send(sok->skt,b,cc,0);
        if ( q < 0 )
          {
            int err = Tcp_Errno();
................................................................................
  }
#endif
  ;
  
void Wrk_Tcp_Send(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
  {
    char *b = iot->dta;
    int count = iot->count;
    int cc = count;
    while ( cc )
      {
        int q = send(iot->skt,b,cc,0);
        if ( q < 0 )
          {
................................................................................
  }
#endif
  ;

void Wrk_Tcp_Recv(YO_TCP_IOT *iot)
#ifdef _YO_TCPIP_BUILTIN
  {
    char *b = iot->dta;
    int count = iot->count;
    int cc = count;
    while ( cc )
      {
        int q = recv(iot->skt,b,cc,0);
        if ( q < 0 )
          {
................................................................................
  }
#endif
  ;

YO_TCPSOK *Tcp_Open(char *host, int port)
#ifdef _YO_TCPIP_BUILTIN
  {
    YO_TCPSOK *sok = Tcp_Socket();
    Tcp_Connect(sok,host,port);
    return sok;
  }
#endif
  ;

YO_TCPSOK *Tcp_Listen(char *host, int port, int listlen)
................................................................................
    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());
    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;

Changes to threads.hc.

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

#ifdef _YO_THREADS_BUILTIN
typedef struct _YO_THREAD_COOKIE
  {
    thread_func_t func;
    void *o;    
  } YO_THREAD_COOKIE;
static _Thread_Release_Cookie(YO_THREAD_COOKIE *cookie) 
  {
    free(cookie);
  }
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;
................................................................................
      #endif
      }
  } 
#endif
  ;
     
#if defined __windoze && !defined _PTHREADS
#define _USECONDWAIT

typedef struct _YO_THRSYNC
  {
    CRITICAL_SECTION *cs;
  #ifdef _USECONDWAIT
    CONDITION_VARIABLE *condwait;
  #else







|







 







<







75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
243
244
245
246
247
248
249

250
251
252
253
254
255
256

#ifdef _YO_THREADS_BUILTIN
typedef struct _YO_THREAD_COOKIE
  {
    thread_func_t func;
    void *o;    
  } YO_THREAD_COOKIE;
static void _Thread_Release_Cookie(YO_THREAD_COOKIE *cookie) 
  {
    free(cookie);
  }
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;
................................................................................
      #endif
      }
  } 
#endif
  ;
     
#if defined __windoze && !defined _PTHREADS


typedef struct _YO_THRSYNC
  {
    CRITICAL_SECTION *cs;
  #ifdef _USECONDWAIT
    CONDITION_VARIABLE *condwait;
  #else

Changes to webhost.hc.

66
67
68
69
70
71
72

73
74
75
76
77
78
79
80

81
82
83
84
85
86
87
...
177
178
179
180
181
182
183























184
185
186
187
188
189
190
191
...
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
...
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
...
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
...
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

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
................................................................................
  {
    int code = Mime_Code_Of_Path(path,YO_MIME_OCTS);
    return Mime_String_Of(code);
  }
#endif
  ;
  























int Webhost_Is_Executable(YO_BUFFER *bf, int L, YO_ARRAY *extL)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i;
    
    for ( i = 0; i < extL->count; ++i )
      {
        char *ext = extL->at[i];
................................................................................
      }
    
    return 0;
  }
#endif
  ;
   
int Webhost_Find_Executable_1(YO_BUFFER *bf, int L, YO_ARRAY *extL)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i;
    YO_FILE_STATS stats = {0};
    
    bf->at[L] = 0;
    
    if ( File_Get_Stats(bf->at,&stats,1)->f.exists && !stats.f.is_directory )
      return Webhost_Is_Executable(bf,L,extL);
        

    for ( i = 0; i < extL->count; ++i )
      {
        char *ext = extL->at[i];
        bf->at[L] = 0;
        bf->count = L;
        Buffer_Append(bf,ext,-1); 
        if ( File_Get_Stats(bf->at,&stats,1)->f.exists )
          if ( !stats.f.is_directory )
            return 1;
      }
    
    return 0;
  }
#endif
  ;
  







































char *Webhost_Find_Executable(char *execdir, char *query, char **script, char **pathinfo)
#ifdef _YO_WEBHOST_BUILTIN
  {
    char *fpath = 0;
    
    __Auto_Ptr(fpath)
      {
        int i, L;

        YO_ARRAY *extL = Str_Split(getenv("PATHEXT"),";");
        YO_ARRAY *patL = Str_Split(query,"/");
        YO_BUFFER *bf  = Buffer_Acquire(Path_Normilize_Npl(execdir));
        
        L = bf->count;
        
        for ( i = 0; i < patL->count; ++i )



          {
            if ( *(char*)patL->at[i] == 0 ) continue;
            Buffer_Grow(bf,L+1);
            bf->at[L] = YO_PATH_SEPARATOR;
            Buffer_Append(bf,patL->at[i],-1);
            L = bf->count;

            if ( Webhost_Find_Executable_1(bf,L,extL) )

              {








                int j;
                L = bf->count+1;
                Buffer_Fill_Append(bf,'\0',1);
                for ( j = 0; j <= i ; ++j ) 
                  {
                    if ( *(char*)patL->at[j] == 0 ) continue;
                    Buffer_Fill_Append(bf,'/',1);
                    Buffer_Append(bf,patL->at[j],-1);



                  }






                j = bf->count+1;
                Buffer_Fill_Append(bf,'\0',1);
                for ( ++i; i < patL->count ; ++i ) 
                  {
                    Buffer_Fill_Append(bf,'/',1);
                    Buffer_Append(bf,patL->at[i],-1);



                  }


                
                fpath = Buffer_Take_Data(bf);
                *script = fpath+L;
                *pathinfo = fpath+j;





                break;
              }
          }
      }
      
    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;
................................................................................
#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);
................................................................................
            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
  {
    YO_WEBHOST_CGIST *st = __Object_Dtor(sizeof(YO_WEBHOST_CGIST),YO_WEBHOST_CGIST_Destruct);
................................................................................
            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 )







>








>







 







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







 







|








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





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










|




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






|







 







|







 







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







 







>
|












|




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







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
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
...
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
...
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
...
520
521
522
523
524
525
526




















527
528
529
530
531
532
533
...
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

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

typedef struct _YO_WEBHOST_SERVICE
  {
    char *prefix;
    char *ext;
    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);
    free(svc->ext);
    __Unrefe(svc->obj);
    __Destruct(svc);
  }
#endif
  ;
    
typedef struct _YO_WEBHOST
................................................................................
  {
    int code = Mime_Code_Of_Path(path,YO_MIME_OCTS);
    return Mime_String_Of(code);
  }
#endif
  ;
  
#define  Webhost_Register_Service(Host,Prefix,Callback,Obj) Webhost_Register_Service_(Host,Prefix,0,Callback,Obj)
#define  Webhost_Register_Filter(Host,Ext,Callback,Obj) Webhost_Register_Service_(Host,0,Ext,Callback,Obj)
void Webhost_Register_Service_(YO_WEBHOST *host, char *prefix, char *ext, 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 = prefix?Str_Copy_Npl(prefix,-1):0;
        svc->ext    = ext?Str_Copy_Npl(ext,-1):0;
        
        if ( !host->services ) 
          host->services = __Refe(Array_Refs());
        
        Array_Push(host->services,__Refe(svc));
      }
  }
#endif
  ;

int Webhost_File_Exists(YO_BUFFER *bf, int L, YO_ARRAY *extL,int executable)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i;
    
    for ( i = 0; i < extL->count; ++i )
      {
        char *ext = extL->at[i];
................................................................................
      }
    
    return 0;
  }
#endif
  ;
   
int Webhost_Find_File_1(YO_BUFFER *bf, int L, YO_ARRAY *extL, int executable)
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i;
    YO_FILE_STATS stats = {0};
    
    bf->at[L] = 0;
    
    if ( File_Get_Stats(bf->at,&stats,1)->f.exists && !stats.f.is_directory )
      return Webhost_File_Exists(bf,L,extL,executable);
    
    if ( executable )
      for ( i = 0; i < extL->count; ++i )
        {
          char *ext = extL->at[i];
          bf->at[L] = 0;
          bf->count = L;
          Buffer_Append(bf,ext,-1); 
          if ( File_Get_Stats(bf->at,&stats,1)->f.exists )
            if ( !stats.f.is_directory )
              return 1;
        }
    
    return 0;
  }
#endif
  ;

char *Webhost_Find_File(YO_BUFFER *bf, YO_ARRAY *patL, int i, int *L, YO_ARRAY *extL, char **script, char **pathinfo, int executable)    
#ifdef _YO_WEBHOST_BUILTIN
  {  
    char *fpath = 0;    
    if ( *(char*)patL->at[i] == 0 ) return 0;
    
    Buffer_Grow(bf,*L+1);
    bf->at[*L] = YO_PATH_SEPARATOR;
    Buffer_Append(bf,patL->at[i],-1);
    *L = bf->count;
    if ( Webhost_Find_File_1(bf,*L,extL,executable) )
      {
        int j;
        *L = bf->count+1;
        Buffer_Fill_Append(bf,'\0',1);
        for ( j = 0; j <= i ; ++j ) 
          {
            if ( *(char*)patL->at[j] == 0 ) continue;
            Buffer_Fill_Append(bf,'/',1);
            Buffer_Append(bf,patL->at[j],-1);
          }
        j = bf->count+1;
        Buffer_Fill_Append(bf,'\0',1);
        for ( ++i; i < patL->count ; ++i ) 
          {
            Buffer_Fill_Append(bf,'/',1);
            Buffer_Append(bf,patL->at[i],-1);
          }
        
        fpath = Buffer_Take_Data(bf);
        *script = fpath+*L;
        *pathinfo = fpath+j;
      }
    return fpath;
  }
#endif
  ;

  
char *Webhost_Find_Executable(YO_WEBHOST *host, char *query, char **script, char **pathinfo)
#ifdef _YO_WEBHOST_BUILTIN
  {
    char *fpath = 0;
    
    __Auto_Ptr(fpath)
      {
        int i, L;

        YO_ARRAY *extL = Str_Split(getenv("PATHEXT"),";");
        YO_ARRAY *patL = Str_Split(query,"/");
        YO_BUFFER *bf  = Buffer_Acquire(Path_Normilize_Npl(host->exec_root));
        
        L = bf->count;
        
        for ( i = 0; i < patL->count; ++i )
          if (( fpath = Webhost_Find_File(bf,patL,i,&L,extL,script,pathinfo,1) ))
              break;
      }
      



    return fpath;    

  }
#endif
  ;
    
char *Webhost_Find_Handler(YO_WEBHOST *host, char *query, char **script, char **pathinfo,YO_WEBHOST_SERVICE **h)
#ifdef _YO_WEBHOST_BUILTIN
  {
    char *fpath = 0;
    
    if ( !host->services ) return 0;
    __Auto_Ptr(fpath)
      {
        int i, L;







        YO_ARRAY *extL = Array_Init();
        YO_ARRAY *patL = Str_Split(query,"/");
        YO_BUFFER *bf  = Buffer_Acquire(Path_Normilize_Npl(host->doc_root));
        
        for ( i = 0; i < host->services->count; ++i )
          {
            YO_WEBHOST_SERVICE *svc = host->services->at[i];
            if ( svc->ext ) Array_Push(extL,svc->ext);
          }
                  
        L = bf->count;


        


        for ( i = 0; i < patL->count; ++i )
          if (( fpath = Webhost_Find_File(bf,patL,i,&L,extL,script,pathinfo,0) ))
              break;
      
        if ( fpath )
          for ( i = 0; i < host->services->count; ++i )
            {



              YO_WEBHOST_SERVICE *svc = host->services->at[i];
              if ( svc->ext )
                if ( Str_Ends_With(fpath,svc->ext) )
                  {
                    *h = svc; 
                    break;
                  }
            } 
      }
      
    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;
................................................................................
#ifdef _YO_WEBHOST_BUILTIN
  {
    int i,L;
    char c;
    for ( i = 0; i < services->count; ++i )
      {
        YO_WEBHOST_SERVICE * svc = services->at[i];
        if ( svc->prefix && 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);
................................................................................
            retcode = 500;
          }
      }
        
    if ( retcode )
      Httpd_Continue_Request(st->rqst,retcode);
  }




















#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
  {
    YO_WEBHOST_CGIST *st = __Object_Dtor(sizeof(YO_WEBHOST_CGIST),YO_WEBHOST_CGIST_Destruct);
................................................................................
            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;
            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,url->query,&script,&pathinfo);
            if ( path )
              return Webhost_Execute_CGI(Webhost_CGI_State(host, rqst, __Retain(path), script, pathinfo, url));
          }
        #endif
        
        if ( host->services )
          {
            YO_WEBHOST_SERVICE *svc = 0;
            path = Webhost_Find_Handler(host,url->query,&script,&pathinfo,&svc);
            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;
              }
          }
          
        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 )

Changes to xdata.hc.

1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
      {
        if ( i >= elm_size - 1 ) __Raise(YO_ERROR_OUT_OF_RANGE,0);
        if ( **query != '.' )
          {
            char c = *(*query)++;
            if ( c == '*' || c == '[' || c == '?' )
              patt = YO_XNODE_QUERY_MATCH;
            else if ( c == '@' && patt < YO_XNODE_QUERY_MATCH )
              patt = YO_XNODE_QUERY_NAMED;
            elm[i++] = c;
          }
        else
          {
            ++*query;
            break;







|







1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
      {
        if ( i >= elm_size - 1 ) __Raise(YO_ERROR_OUT_OF_RANGE,0);
        if ( **query != '.' )
          {
            char c = *(*query)++;
            if ( c == '*' || c == '[' || c == '?' )
              patt = YO_XNODE_QUERY_MATCH;
            else if ( c == '@' && patt < YO_XNODE_QUERY_MATCH && **query )
              patt = YO_XNODE_QUERY_NAMED;
            elm[i++] = c;
          }
        else
          {
            ++*query;
            break;

Changes to xdef.hc.

64
65
66
67
68
69
70

71
72
73
74
75
76
77
...
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
...
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
...
495
496
497
498
499
500
501
502


503
504
505
506
507
508
509
    XDEF_FORMAT_MASK = 0x300,
  };

typedef struct _YO_XDEF_STATE
  {
    char *text;
    int lineno;

  } YO_XDEF_STATE;

void Def_Parse_Skip_Spaces(YO_XDEF_STATE *st)
#ifdef _YO_XDEF_BUILTIN
  {
    while ( *st->text && Isspace(*st->text) )
      {
................................................................................
        default:
          __Raise(YO_ERROR_UNEXPECTED_VALUE,0);
      }
      
  }
#endif
  ;


void Def_Parse_In_Node( YO_XDEF_STATE *st, YO_XNODE *n )
#ifdef _YO_XDEF_BUILTIN
  {
    Def_Parse_Skip_Spaces(st);
    
    for ( ; *st->text && *st->text != '}' ; Def_Parse_Skip_Spaces(st) ) 
      {
................................................................................
        int go_deeper = 0;
        YO_XNODE *nn = 0;
        
        __Auto_Release
          {
            YO_XDEF_VALUE value = {0};
            char *name; 
            
            name = Def_Parse_Get_Literal(st);
            Def_Parse_Skip_Spaces(st);

            if ( *st->text == '(' )
              {
                char *dflt;
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                dflt = Def_Parse_Get_Node_Literal(st);
                Def_Parse_Skip_Spaces(st);
                if ( *st->text != ')' )
                  __Raise(YO_ERROR_ILLFORMED,__Format("expected ')' at line %d",st->lineno));
                ++st->text;
                Def_Parse_Skip_Spaces(st);

                nn = Xnode_Append(n,name);
                Xnode_Value_Set_Str(nn,"@",dflt);
                name = 0;
              }
            
            
            if ( *st->text == '=' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&value);
                Def_Parse_Skip_Spaces(st);
              }
            






























            if ( *st->text == '{' )
              {
                ++st->text;
                go_deeper = 1;
                if ( !nn )
                  nn = Xnode_Append(n,name);
                if ( value.type )
................................................................................
  }
#endif
  ;

YO_XDATA *Def_Parse_Str(char *text)
#ifdef _YO_XDEF_BUILTIN
  {
    YO_XDEF_STATE st = { text, 1 };
    YO_XDATA *doc = Xdata_Init();
    Def_Parse_In_Node(&st,&doc->root);
    return doc;
  }
#endif
  ;

................................................................................
#ifdef _YO_XDEF_BUILTIN
  {
    YO_XDATA *ret = 0;
    
    __Auto_Ptr(ret)
      {
        YO_BUFFER *bf = Oj_Read_All(Cfile_Open(filename,"rt"));
        ret = Def_Parse_Str((char*)bf->at);


      }
    return ret;
  }
#endif
  ;

void Def_Format_Node_In_Depth(YO_BUFFER *bf, YO_XNODE *r, int flags, int indent)







>







 







|
>







 







|





<


|





<
<
<
<











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







 







|







 







|
>
>







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
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
...
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
    XDEF_FORMAT_MASK = 0x300,
  };

typedef struct _YO_XDEF_STATE
  {
    char *text;
    int lineno;
    char *dirname;
  } YO_XDEF_STATE;

void Def_Parse_Skip_Spaces(YO_XDEF_STATE *st)
#ifdef _YO_XDEF_BUILTIN
  {
    while ( *st->text && Isspace(*st->text) )
      {
................................................................................
        default:
          __Raise(YO_ERROR_UNEXPECTED_VALUE,0);
      }
      
  }
#endif
  ;
  
YO_XDATA *Def_Parse_File(char *filename);
void Def_Parse_In_Node( YO_XDEF_STATE *st, YO_XNODE *n )
#ifdef _YO_XDEF_BUILTIN
  {
    Def_Parse_Skip_Spaces(st);
    
    for ( ; *st->text && *st->text != '}' ; Def_Parse_Skip_Spaces(st) ) 
      {
................................................................................
        int go_deeper = 0;
        YO_XNODE *nn = 0;
        
        __Auto_Release
          {
            YO_XDEF_VALUE value = {0};
            char *name; 
            char *nodename = 0;
            name = Def_Parse_Get_Literal(st);
            Def_Parse_Skip_Spaces(st);

            if ( *st->text == '(' )
              {

                ++st->text;
                Def_Parse_Skip_Spaces(st);
                nodename = Def_Parse_Get_Node_Literal(st);
                Def_Parse_Skip_Spaces(st);
                if ( *st->text != ')' )
                  __Raise(YO_ERROR_ILLFORMED,__Format("expected ')' at line %d",st->lineno));
                ++st->text;
                Def_Parse_Skip_Spaces(st);




              }
            
            
            if ( *st->text == '=' )
              {
                ++st->text;
                Def_Parse_Skip_Spaces(st);
                Def_Parse_Get_Value(st,&value);
                Def_Parse_Skip_Spaces(st);
              }
            
            if ( *st->text == '<' && st->text[1] == '=' )
              {
                YO_XDATA *xd = 0; 
                YO_XNODE *sn = 0;
                char *include_file;
                char *subnode = 0;
                st->text += 2;
                Def_Parse_Skip_Spaces(st);
                include_file = Def_Parse_Get_Literal(st);
                Def_Parse_Skip_Spaces(st);
                subnode = strchr(include_file,':');
                if ( subnode ) *subnode++ = 0;
                if ( st->dirname ) include_file = Path_Join(st->dirname,include_file);
                xd = Def_Parse_File(include_file);
                sn = subnode?Xnode_Query_Node(&xd->root,subnode):&xd->root;
                if ( sn )
                  {
                    nn = Xnode_Append_Refnode(n,name,sn);
                    name = 0;
                  }
              }
            
            if ( nodename )
              {
                if ( !nn )
                  nn = Xnode_Append(n,name);
                Xnode_Value_Set_Str(nn,"@",nodename);
                name = 0;
              }            

            if ( *st->text == '{' )
              {
                ++st->text;
                go_deeper = 1;
                if ( !nn )
                  nn = Xnode_Append(n,name);
                if ( value.type )
................................................................................
  }
#endif
  ;

YO_XDATA *Def_Parse_Str(char *text)
#ifdef _YO_XDEF_BUILTIN
  {
    YO_XDEF_STATE st = { text, 1, 0 };
    YO_XDATA *doc = Xdata_Init();
    Def_Parse_In_Node(&st,&doc->root);
    return doc;
  }
#endif
  ;

................................................................................
#ifdef _YO_XDEF_BUILTIN
  {
    YO_XDATA *ret = 0;
    
    __Auto_Ptr(ret)
      {
        YO_BUFFER *bf = Oj_Read_All(Cfile_Open(filename,"rt"));
        YO_XDEF_STATE st = { bf->at, 1, Path_Dirname(filename) };
        ret = Xdata_Init();
        Def_Parse_In_Node(&st,&ret->root);
      }
    return ret;
  }
#endif
  ;

void Def_Format_Node_In_Depth(YO_BUFFER *bf, YO_XNODE *r, int flags, int indent)

Added xtmpl.hc.



































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
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
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265

/*

Copyright © 2010-2012, Alexéy Sudachén, alexey@sudachen.name, Chile

In USA, UK, Japan and other countries allowing software patents:

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    http://www.gnu.org/licenses/

Otherwise:

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Except as contained in this notice, the name of a copyright holder shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization of the copyright holder.

*/

#ifndef C_once_40BDAC30_45A4_4FC1_810C_05757F2DC413
#define C_once_40BDAC30_45A4_4FC1_810C_05757F2DC413

#include "yoyo.hc"
#include "string.hc"
#include "buffer.hc"
#include "xdata.hc"
#include "xdef.hc"

#ifdef _LIBYOYO
#define _YO_XTMPL_BUILTIN
#endif

typedef struct _YO_XTMPL_UP
  {
    YO_XNODE *tmpl;
    struct _YO_XTMPL_UP *up;
  } YO_XTMPL_UP;

int Xtmpl_Macro_Template(YO_XNODE *n, char *source, char *close_tag);

int Xtmpl_Extends_Template(YO_XNODE *n, char *source)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *cnt = Xnode_Insert(n,"content");
        YO_XNODE *ext = Xnode_Append(n,"extends");
        __Auto_Release
          {
            YO_BUFFER *bf;
            void *file = 0;
            char *tmpl_home = Xnode_Value_Get_Str(&n->xdata->root,"home",".");
            char *tmpl_source = Str_Join_2('/',tmpl_home,Str_Copy_L(S_source,source-S_source));
            char *e = strrchr(tmpl_source,'.');
            if ( !e || (e && strcmp_I(e,".xtmpl")) ) tmpl_source = Str_Join_2('.',tmpl_source,"xtmpl");
            file = Cfile_Open(tmpl_source,"r");
            bf = Cfile_Read_All(file);
            Xnode_Value_Set_Str(ext,"source",tmpl_source);
            Xtmpl_Macro_Template(ext,bf->at,0);
          }
        ++source;
        return source - S_source + Xtmpl_Macro_Template(cnt,source,0);
      }

    return 0;
  }
#endif
  ;

int Xtmpl_Expand_Template(YO_XNODE *n, char *source)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,"expand");
        YO_XVALUE *val = Xnode_Value(ext,"$",1);
        Xvalue_Set_Str(val,S_source,source-S_source);
        ++source;
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Dump_Template(YO_XNODE *n, char *source)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,"dump");
        if ( source != S_source )
          {
            YO_XVALUE *val = Xnode_Value(ext,"$",1);
            Xvalue_Set_Str(val,S_source,source-S_source);
          }
        ++source;
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Liftup_Template(YO_XNODE *n, char *source)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,"liftup");
        YO_XVALUE *val = Xnode_Value(ext,"$",1);
        Xvalue_Set_Str(val,S_source,source-S_source);
        ++source;
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Oper_Template(YO_XNODE *n, char *source, char *tag)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,tag);
        YO_XVALUE *val = Xnode_Value(ext,"$",1);
        Xvalue_Set_Str(val,S_source,source-S_source);
        ++source;
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Requr_Template(YO_XNODE *n, char *source, char *tag, char *close_tag)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,tag);
        char * expr = Str_Trim_Copy_L(S_source,source-S_source);
        if ( expr && *expr )
          {
            YO_XVALUE *val = Xnode_Value(ext,"$",1);
            Xvalue_Set_Or_Put_Str(val,expr);
          }
        ++source;
        source += Xtmpl_Macro_Template(ext,source,close_tag);
        if ( *source == '$' )
          {
            if ( source[1] == '!' ) 
              source+=2;
            else if ( source[1] == '/' )
              {
                int L = strlen(close_tag);
                if ( !strncmp(source+2,close_tag,L) ) source += L+2;
              }
          }
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Alt_Template(YO_XNODE *n, char *source, char *tag, char *close_tag)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *S_source = source;
    for ( ; *source && *source != '}'; ) ++source;
    
    if ( *source == '}' )
      {
        YO_XNODE *ext = Xnode_Append(n,tag);
        char *expr = Str_Trim_Copy_L(S_source,source-S_source);
        if ( expr && *expr )
          {
            YO_XVALUE *val = Xnode_Value(ext,"$",1);
            Xvalue_Set_Or_Put_Str(val,expr);
          }
        source += Xtmpl_Macro_Template(ext,source+1,close_tag);
        return source - S_source;
      }
      
    return 0;
  }
#endif
  ;

int Xtmpl_Skip_Comment(YO_XNODE *n, char *source)
#ifdef _YO_XTMPL_BUILTIN 
  {
    int cmpx = 0;
    char *S_source = source;
    for ( ; *source && (cmpx = strncmp(source,"*/$",3)); ) ++source;
    if ( !cmpx )
      return source+3 - S_source;
    else
      return source - S_source;
  }
#endif
  ;

#ifdef _YO_XTMPL_BUILTIN 
void Xtmpl_Append_Text(YO_XNODE *n, char *S, int L)
  {
    int oldL = L;
    char *oldS = S;
    while ( L && Isspace(*S) ) { ++S; --L; }
    while ( L && Isspace(S[L-1]) ) { --L; }
    if ( L )
      {
        YO_XNODE *k = Xnode_Append(n,"text");
        YO_XVALUE *val = Xnode_Value(k,"$",1);
        Xvalue_Set_Str(val,oldS,oldL);
      }
    /*else if ( oldL != L )
      {
        Xnode_Append(n,"space");
      }*/
  }

int Xtmpl_Macro_Template(YO_XNODE *n, char *source, char *close_tag)
  {
    static char t_extends[]  = "extends{";
    static char t_liftup[]   = "liftup{";
    static char t_gadget[]   = "gadget{";
    static char t_seleref[]  = "seleref{";
    static char t_dump[]     = "dump{";
    static char t_ifvalue[]  = "ifvalue{";
    static char t_ifgroup[]  = "ifgroup{";
    static char t_switch[]   = "switch{";
    static char t_case[]     = "case{";
    static char t_foreach[]  = "foreach{";
    static char t_else[]     = "else{";
    static char t_match[]    = "match{";
    static char t_def[]      = "def{";
    static char t_set[]      = "set{";
    static char t_repeat[]   = "repeat{";
    static char t_at[]       = "at{";
    static char t_at1[]      = "at1{";
    static char t_inc[]      = "inc{";
    static char t_dec[]      = "dec{";
    static char t_expand[]   = "{";
    static char t_comment[]  = "/*";
    char *S_source = source;
    char *Q;
    int close_len = close_tag?strlen(close_tag):0;
    
    for ( Q = source; *source; )
      {
        if ( *source == '$' )
          {
            if ( Q != source )
              {
                Xtmpl_Append_Text(n,Q,source-Q);
                Q = source;
              }
            
            if ( close_tag )
              {
                if ( source[1] == '!' )
                  {
                    break;
                  }
                else if ( source[1] == '/' && !strncmp(source+2,close_tag,close_len) )
                  {
                    break;
                  }
              }
              
            if ( !strncmp(source+1,t_extends,sizeof(t_extends)-1) )
              {
                source += sizeof(t_extends); /* +1 ($) -1 (\0) */
                source += Xtmpl_Extends_Template(n,source);
                Q = source;
              }
            else if ( !strncmp(source+1,t_expand,sizeof(t_expand)-1) )
              {
                source += sizeof(t_expand);
                source += Xtmpl_Expand_Template(n,source);
                Q = source;
              }
            else if ( !strncmp(source+1,t_liftup,sizeof(t_liftup)-1) )
              {
                source += sizeof(t_liftup);
                source += Xtmpl_Liftup_Template(n,source);
                Q = source;
              }
            else if ( !strncmp(source+1,t_gadget,sizeof(t_gadget)-1) )
              {
                source += sizeof(t_gadget);
                source += Xtmpl_Requr_Template(n,source,"gadget","gadget");
                Q = source;
              }
            else if ( !strncmp(source+1,t_def,sizeof(t_def)-1) )
              {
                source += sizeof(t_def);
                source += Xtmpl_Requr_Template(n,source,"def","def");
                Q = source;
              }
            else if ( !strncmp(source+1,t_set,sizeof(t_set)-1) )
              {
                source += sizeof(t_set);
                source += Xtmpl_Requr_Template(n,source,"set","set");
                Q = source;
              }
            else if ( !strncmp(source+1,t_dump,sizeof(t_dump)-1) )
              {
                source += sizeof(t_dump);
                source += Xtmpl_Dump_Template(n,source);
                Q = source;
              }
            else if ( !strncmp(source+1,t_inc,sizeof(t_inc)-1) )
              {
                source += sizeof(t_inc);
                source += Xtmpl_Oper_Template(n,source,"inc");
                Q = source;
              }
            else if ( !strncmp(source+1,t_dec,sizeof(t_dec)-1) )
              {
                source += sizeof(t_dec);
                source += Xtmpl_Oper_Template(n,source,"dec");
                Q = source;
              }
            else if ( !strncmp(source+1,t_seleref,sizeof(t_seleref)-1) )
              {
                source += sizeof(t_seleref);
                source += Xtmpl_Requr_Template(n,source,"seleref","seleref");
                Q = source;
              }
            else if ( !strncmp(source+1,t_ifvalue,sizeof(t_ifvalue)-1) )
              {
                source += sizeof(t_ifvalue);
                source += Xtmpl_Requr_Template(n,source,"ifvalue","ifvalue");
                Q = source;
              }
            else if ( !strncmp(source+1,t_ifgroup,sizeof(t_ifgroup)-1) )
              {
                source += sizeof(t_ifgroup);
                source += Xtmpl_Requr_Template(n,source,"ifgroup","ifgroup");
                Q = source;
              }
            else if ( !strncmp(source+1,t_else,sizeof(t_else)-1) )
              {
                source += sizeof(t_else);
                source += Xtmpl_Alt_Template(n,source,"else",close_tag);
                Q = source;
              }
            else if ( !strncmp(source+1,t_switch,sizeof(t_switch)-1) )
              {
                source += sizeof(t_switch);
                source += Xtmpl_Requr_Template(n,source,"switch","switch");
                Q = source;
              }
            else if ( !strncmp(source+1,t_case,sizeof(t_case)-1) )
              {
                source += sizeof(t_case);
                source += Xtmpl_Alt_Template(n,source,"case",close_tag);
                Q = source;
              }
            else if ( !strncmp(source+1,t_match,sizeof(t_match)-1) )
              {
                source += sizeof(t_match);
                source += Xtmpl_Alt_Template(n,source,"match",close_tag);
                Q = source;
              }
            else if ( !strncmp(source+1,t_foreach,sizeof(t_foreach)-1) )
              {
                source += sizeof(t_foreach);
                source += Xtmpl_Requr_Template(n,source,"foreach","foreach");
                Q = source;
              }
            else if ( !strncmp(source+1,t_repeat,sizeof(t_repeat)-1) )
              {
                source += sizeof(t_repeat);
                source += Xtmpl_Requr_Template(n,source,"repeat","repeat");
                Q = source;
              }
            else if ( !strncmp(source+1,t_at,sizeof(t_at)-1) )
              {
                source += sizeof(t_at);
                source += Xtmpl_Requr_Template(n,source,"at","at");
                Q = source;
              }
            else if ( !strncmp(source+1,t_at1,sizeof(t_at1)-1) )
              {
                source += sizeof(t_at1);
                source += Xtmpl_Requr_Template(n,source,"at1","at1");
                Q = source;
              }
            else if ( !strncmp(source+1,t_comment,sizeof(t_comment)-1) )
              {
                source += sizeof(t_comment);
                source += Xtmpl_Skip_Comment(n,source);
                Q = source;
              }
            else
              {
                Q = source;
                ++source;
              }
          }
        else
          ++source;
      }
      
    if ( Q != source )
      Xtmpl_Append_Text(n,Q,source-Q);

    return source-S_source;
  }
#endif

YO_XDATA *Xtmpl_Load_Template(char *tmpl_home, char *tmpl_name)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XDATA *doc = Xdata_Init();
    
    __Auto_Release
      {
        YO_BUFFER *bf;
        char *tmpl_source = Str_Join_2('/',tmpl_home,tmpl_name);
        bf = Cfile_Read_All(Cfile_Open(tmpl_source,"r"));
        Xnode_Value_Set_Str(&doc->root,"home",tmpl_home);
        Xnode_Value_Set_Str(&doc->root,"source",tmpl_source);
        Xtmpl_Macro_Template(&doc->root,bf->at,0);
      }
      
    return doc;
  }
#endif
  ;

void Xtmpl_Handle_Error_Out(YO_BUFFER *bf, char *tag, char *text)
#ifdef _YO_XTMPL_BUILTIN 
  {
    Buffer_Append(bf,"<span id=\"xtmpl-error\">{",-1);
    Buffer_Append(bf,tag,-1);
    Buffer_Append(bf,":",1);
    Buffer_Html_Quote_Append(bf,text,-1);
    Buffer_Append(bf,"}</span>",-1);
  }
#endif
  ;
  
typedef struct _YO_XTMPL_CTX
  {
    YO_XDATA *model;
    YO_XNODE *each;
    YO_XNODE *tmpl;
    char *eachname;
    YO_DICTO *global;
  } YO_XTMPL_CTX;

void Xtmpl_Handle_Node_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx);

typedef struct _YO_XTMPL_MACRO
  {
    YO_XNODE *defnode;
    YO_XVALUE *value;
  } YO_XTMPL_MACRO;
  
void YO_XTMPL_MACRO_Destruct(YO_XTMPL_MACRO *self)
#ifdef _YO_XTMPL_BUILTIN 
  {
    if ( self->value )
      {
        Xvalue_Purge(self->value);
        free(self->value);
      }
    __Destruct(self);
  }
#endif
  ;

typedef struct _YO_XTMPL_VALUE
  {
    YO_XVALUE value;
  } YO_XTMPL_VALUE;

void YO_XTMPL_VALUE_Destruct(YO_XTMPL_VALUE *self)
#ifdef _YO_XTMPL_BUILTIN 
  {
    Xvalue_Purge(&self->value);
    __Destruct(self);
  }
#endif
  ;
  
char *YO_XTMPL_MACRO_Out(YO_XTMPL_MACRO *self, YO_XTMPL_CTX *ctx, YO_BUFFER *bf)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *t = Xnode_Down(self->defnode);
    int start = bf->count;
    while ( t )
      {
        Xtmpl_Handle_Node_Out(bf,t,ctx);
        t = Xnode_Next(t);
      }
    return bf->at+start;
  }
#endif
  ;
  
YO_XVALUE *YO_XTMPL_MACRO_Get(YO_XTMPL_MACRO *self, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    __Auto_Release
      {
        YO_BUFFER *bf = Buffer_Init(0);
        if ( !self->value )
          {
            self->value = __Malloc_Npl(sizeof(*self->value));
            self->value->opt = XVALUE_OPT_VALTYPE_NONE;
          }
        YO_XTMPL_MACRO_Out(self,ctx,bf);
        Xvalue_Set_Str(self->value,bf->at,bf->count);
      }
    return self->value;
  }
#endif
  ;

char *YO_XTMPL_VALUE_Out(YO_XTMPL_VALUE *self, YO_XTMPL_CTX *ctx, YO_BUFFER *bf)
#ifdef _YO_XTMPL_BUILTIN 
  {
    return Xvalue_Str_Bf(&self->value,bf);
  }
#endif
  ;
  
YO_XVALUE *YO_XTMPL_VALUE_Get(YO_XTMPL_VALUE *self, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    return &self->value;
  }
#endif
  ;
  
#ifdef _YO_XTMPL_BUILTIN 
  static char Xtmpl_Out_OjMID[] = "xtmpl$Out/@**";
  static char Xtmpl_Get_OjMID[] = "xtmpl$Get/@*";
#endif
  
YO_XTMPL_MACRO *YO_XTMPL_MACRO_Init(YO_XNODE *defnode)
#ifdef _YO_XTMPL_BUILTIN 
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID, YO_XTMPL_MACRO_Destruct},
        {Xtmpl_Out_OjMID,   YO_XTMPL_MACRO_Out},
        {Xtmpl_Get_OjMID,   YO_XTMPL_MACRO_Get},
        {0}};
    YO_XTMPL_MACRO *self = __Object(sizeof(YO_XTMPL_MACRO),funcs);
    self->defnode = defnode;
    return self;
  }
#endif
  ;
  
YO_XTMPL_VALUE *YO_XTMPL_VALUE_Init()
#ifdef _YO_XTMPL_BUILTIN 
  {
    static YO_FUNCTABLE funcs[] = 
      { {0},
        {Oj_Destruct_OjMID, YO_XTMPL_VALUE_Destruct},
        {Xtmpl_Out_OjMID,   YO_XTMPL_VALUE_Out},
        {Xtmpl_Get_OjMID,   YO_XTMPL_VALUE_Get},
        {0}};
    YO_XTMPL_VALUE *self = __Object(sizeof(YO_XTMPL_VALUE),funcs);
    self->value.opt = XVALUE_OPT_VALTYPE_NONE;
    return self;
  }
#endif
  ;
  
void Xtmpl_Call_Gadget(YO_XTMPL_CTX *ctx, char *operate, YO_BUFFER *bf)
#ifdef _YO_XTMPL_BUILTIN 
  {
    Xtmpl_Handle_Error_Out(bf,"gadget","unsupported");
  }
#endif
  ;
  
void Xtmpl_Def_Macro(YO_XTMPL_CTX *ctx, YO_XNODE *macro)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *name = Xnode_Value_Get_Str(macro,"$",0);
    if ( name )
      {
        void *m = YO_XTMPL_MACRO_Init(macro);
        Dicto_Put(ctx->global,name,__Refe(m));
      }
  }
#endif
  ;
  
void Xtmpl_Set_Value(YO_XTMPL_CTX *ctx, YO_XNODE *setval)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *name = Xnode_Value_Get_Str(setval,"$",0);
    if ( name ) __Auto_Release
      {
        YO_XNODE *t;
        YO_BUFFER *bf = Buffer_Init(0);
        YO_XTMPL_VALUE *m = YO_XTMPL_VALUE_Init();
        Dicto_Put(ctx->global,name,__Refe(m));
        t = Xnode_Down(setval);
        while ( t )
          {
            Xtmpl_Handle_Node_Out(bf,t,ctx);
            t = Xnode_Next(t);
          }
        Xvalue_Set_Str(&m->value,bf->at,bf->count);
      }
  }
#endif
  ;
  
void Xtmpl_Inc_Value(YO_XTMPL_CTX *ctx, YO_XNODE *incval)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *name = Xnode_Value_Get_Str(incval,"$",0);
    if ( name ) __Auto_Release
      {
        YO_XTMPL_VALUE *m = Dicto_Get(ctx->global,name,0);
        if ( m )
          {
            int q = Xvalue_Get_Int(&m->value,0);
            Xvalue_Set_Str(&m->value,Str_From_Int(q+1),-1);
          }
        else
          {
            YO_XTMPL_VALUE *m = YO_XTMPL_VALUE_Init();
            Dicto_Put(ctx->global,name,__Refe(m));
            Xvalue_Set_Str(&m->value,"0",1);
          }
      }
  }
#endif
  ;

  YO_XNODE *Xtmpl_Query_Node(YO_XTMPL_CTX *ctx, char *query)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *n = 0;
    if ( query && ((*query == '$' && (query[1] == '.' || !query[1])) || *query == '.' ) )
      {
        if ( ctx->each )
          {
            if ( *query == '.' ) 
              n = Xnode_Query_Node(ctx->each,query+1);
            else if ( query[1] == '.' ) 
              n = Xnode_Query_Node(ctx->each,query+2);
            else
              n = ctx->each;
          }
      }
    else if ( query )
      n = Xnode_Query_Node(&ctx->model->root,query);
    return n;
  }
#endif
  ;

YO_XVALUE *Xtmpl_Query_Value(YO_XTMPL_CTX *ctx, char *query)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XVALUE *v = 0;
    if ( query && ( (*query == '$' && (query[1] == '.' || !query[1])) || *query == '.' ) )
      {
        if ( ctx->each )
          {
            if ( *query == '.' ) 
              {
                if ( query[1] == '@' && !query[2] )
                  v = Xnode_Value(ctx->each,"@",0);
                else
                  v = Xnode_Query_Value(ctx->each,query+1);
              }
            else if ( query[1] == '.' ) 
              v = Xnode_Query_Value(ctx->each,query+2);
            else
              v = Xnode_Value(ctx->each,"$",0);
          }
      }
    else if ( query ) 
      {
        void *g = Dicto_Get(ctx->global,query,0);
        if ( g )
          {
            YO_XVALUE *(*getval)() = Yo_Find_Method_Of(&g,Xtmpl_Get_OjMID,0);
            return getval ? getval(g,ctx) : 0;
          }
        else
          v = Xnode_Query_Value(&ctx->model->root,query);
      }
    //fprintf(stderr,"Xtmpl_Query_Value '%s' => %p\n",query,v);
    return v;
  }
#endif
  ;
  
char *Xtmpl_Query_Str_Bf(YO_XTMPL_CTX *ctx, char *query, YO_BUFFER *bf)
#ifdef _YO_XTMPL_BUILTIN 
  {
    void *g = 0;
    if ( 0 != ( g = Dicto_Get(ctx->global,query,0)) )
      {
        char *(*outval)() = Yo_Find_Method_Of(&g,Xtmpl_Out_OjMID,0);
        return outval ? outval(g,ctx,bf) : 0;
      }
    else
      {
        YO_XVALUE *v = Xtmpl_Query_Value(ctx,query);
        return Xvalue_Str_Bf(v,bf);
      }
  }
#endif
  ;

void Xtmpl_Dump_Tmpl(YO_BUFFER *bf, YO_XNODE *tmpl)
#ifdef _YO_XTMPL_BUILTIN 
  {
    Buffer_Append(bf,"<pre style='text-align:left'>\n",-1);
    Def_Format_Into(bf,tmpl,0);
    Buffer_Append(bf,"</pre>\n",-1);
  }
#endif
  ;
  
void Xtmpl_Handle_Dump_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    char *query = Xnode_Value_Get_Str(n,"$",0);
    Buffer_Append(bf,"<pre style='text-align:left'>\n",-1);
    if ( query )
      {
        YO_XNODE *nn = 0;
        if ( !strcmp(query,"#xtmpl") )
          nn = ctx->tmpl;
        else
          nn = Xtmpl_Query_Node(ctx,query);
        
        if (nn) Buffer_Html_Quote_Append(bf,Def_Format(nn,0),-1);
      }
    else
      Def_Format_Into(bf,&ctx->model->root,0);
    Buffer_Append(bf,"</pre>\n",-1);
  }
#endif
  ;
  
void Xtmpl_Handle_Ifvalue_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    char *query = Xnode_Value_Get_Str(n,"$",0);
    if ( (query && Xtmpl_Query_Value(ctx,query)) || (!query && Xnode_Tag_Is(n,"else")) )
      {
        while ( down )
          {
            Xtmpl_Handle_Node_Out(bf,down,ctx);
            down = Xnode_Next(down);
          }
      }
    else
      {
        YO_XNODE *el = Xnode_Down_If(n,"else");
        if ( el )
          Xtmpl_Handle_Ifvalue_Out(bf,el,ctx);
      }
  }
#endif
  ;
  
void Xtmpl_Handle_Ifgroup_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    char *query = Xnode_Value_Get_Str(n,"$",0);
    YO_XNODE *node = query ? Xtmpl_Query_Node(ctx,query) : 0;
    if ( node || (!query && Xnode_Tag_Is(n,"else")) )
      {
        YO_XTMPL_CTX ctx1 = *ctx;
        if ( node ) ctx1.each = node;
        while ( down )
          {
            Xtmpl_Handle_Node_Out(bf,down,&ctx1);
            down = Xnode_Next(down);
          }
      }
    else
      {
        YO_XNODE *el = Xnode_Down_If(n,"else");
        if ( el )
          Xtmpl_Handle_Ifgroup_Out(bf,el,ctx);
      }
  }
#endif
  ;

void Xtmpl_Handle_Seleref_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    char *ref = Xnode_Value_Get_Str(n,"$",0);
    int is_it = ref && Str_Equal_Nocase(Xnode_Value_Get_Str(&ctx->model->root,"request-uri",0),ref);
    if ( is_it ) Buffer_Append(bf,"<span class=\"webfront-selected\">",-1);
    else 
      {
        Buffer_Append(bf,"<a href=\"",-1);
        Buffer_Append(bf,ref,-1);
        Buffer_Append(bf,"\">",-1);
      }
    while ( down )
      {
        Xtmpl_Handle_Node_Out(bf,down,ctx);
        down = Xnode_Next(down);
      }
    if ( is_it ) Buffer_Append(bf,"</span>",-1);
    else Buffer_Append(bf,"</a>",-1);
  }
#endif
  ;
  
void Xtmpl_Handle_Switch_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = n;
    YO_XVALUE *value = 0;
    char *S = 0;
    char *query = Xnode_Value_Get_Str(n,"$",0);
    if ( query ) 
      value = Xtmpl_Query_Value(ctx,query);
    if ( value )
      S = Xvalue_Get_Str(value,0);
    while ( down )
      {
        YO_XNODE *Match = 0, *Case;
        Case = Xnode_Down_If(down,"case");
        if ( !Case ) Match = Xnode_Down_If(down,"match");
        if ( Case || Match )
          {
            int ok = 0;
            if ( Case )
              {
                char *opt = Xnode_Value_Get_Str(Case,"$",0);
                ok = (!opt || !*opt) || (S && !strcmp_I(opt,S));
                down = Case;
              }
            else /* Match */
              {
                char *opt = Xnode_Value_Get_Str(Match,"$",0);
                ok = (!opt || !*opt) || (S && Str_Match_Nocase(S,opt));
                down = Match;
              }

            if ( ok )
              {
                down = Xnode_Down(down);
                while ( down )
                  {
                    Xtmpl_Handle_Node_Out(bf,down,ctx);
                    down = Xnode_Next(down);
                  }
                break;
              }
          }
        else
          down = 0;
      }
  }
#endif
  ;
  
void Xtmpl_Handle_Foreach_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    YO_XNODE *foo = 0;
    YO_XTMPL_CTX ctx1 = *ctx;
    char *tag;
    char *query = Xnode_Value_Get_Str(n,"$",0);
    char period = Str_Last(query) == ',' ? ',' : 0;
    if ( period ) query = Str_Copy_L(query,Str_Length(query)-1);
    if ( query )
      foo = Xtmpl_Query_Node(ctx,query);
    if ( foo ) tag = Xnode_Get_Tag(foo);
    while ( foo )
      {
        YO_XNODE *t = down;
        ctx1.each = foo;
        while ( t )
          {
            Xtmpl_Handle_Node_Out(bf,t,&ctx1);
            t = Xnode_Next(t);
          }
        foo = Xnode_Next_If(foo,tag);
        if ( period )
          {
            if ( foo ) Buffer_Append(bf,&period,1);
          } 
      }
  }
#endif
  ;
  
void Xtmpl_Handle_At_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx, int shift)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    YO_XNODE *foo = 0;
    YO_XTMPL_CTX ctx1 = *ctx;
    YO_ARRAY *q0 = 0;
    char *query = Xnode_Value_Get_Str(n,"$",0);
    if ( query )
      q0 = Str_Split(query,0);
    if ( q0 && q0->count == 2)
      foo = Xtmpl_Query_Node(ctx,q0->at[0]);
    if ( foo ) 
      {
        int i;
        char *tag;
        i = Str_To_Int(Xvalue_Get_Str(Xtmpl_Query_Value(ctx,q0->at[1]),"0")) - shift;
        tag = Xnode_Get_Tag(foo);
        for ( ; foo && i > 0; --i )
          foo = Xnode_Next_If(foo,tag);   
        if ( foo )
          {
            YO_XNODE *t = down;
            ctx1.each = foo;
            while ( t )
              {
                Xtmpl_Handle_Node_Out(bf,t,&ctx1);
                t = Xnode_Next(t);
              }
          }   
      }
  }
#endif
  ;

void Xtmpl_Handle_Repeat_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *down = Xnode_Down(n);
    YO_XTMPL_CTX ctx1 = *ctx;
    char *count_S = Xnode_Value_Get_Str(n,"$","0");
    int i, count = strtol(count_S,0,0);
    char period = Str_Last(count_S) == ',' ? ',' : 0;
    __Auto_Release 
      {
        YO_XNODE *foo  = Xdata_Init();

        for ( i = 0; i < count; ++i )
          {
            YO_XNODE *t = down;
            Xnode_Value_Set_Str(foo,"repno",Str_From_Int(i));
            ctx1.each = foo;

            while ( t )
              {
                Xtmpl_Handle_Node_Out(bf,t,&ctx1);
                t = Xnode_Next(t);
              }
              
            if ( period && i+1 < count )
              {
                Buffer_Append(bf,&period,1);
              } 
          }
      }
   }
#endif
  ;

void Xtmpl_Handle_Node_Out(YO_BUFFER *bf, YO_XNODE *n, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    if ( Xnode_Tag_Is(n,"text") )
      {
        char *value = Xnode_Value_Get_Str(n,"$",0);
        Buffer_Append(bf,value,-1);
      }
    else if ( Xnode_Tag_Is(n,"space") )
      {
        Buffer_Fill_Append(bf,' ',1);
      }
    else if ( Xnode_Tag_Is(n,"expand") )
      {
        int i;
        char *query = Xnode_Value_Get_Str(n,"$",0);
        YO_ARRAY *L = Str_Split(query,",");
        for ( i = 1; i < L->count; ++i )
          {
            YO_XTMPL_VALUE *m = YO_XTMPL_VALUE_Init();
            char S[3];
            sprintf(S,"%d",i);
            Dicto_Put(ctx->global,S,__Refe(m));
            Xvalue_Set_Str(&m->value,L->at[i],strlen(L->at[i]));
          }
        Xtmpl_Query_Str_Bf(ctx,L->at[0],bf);
      }
    else if ( Xnode_Tag_Is(n,"def") )
      {
        Xtmpl_Def_Macro(ctx,n);
      }
    else if ( Xnode_Tag_Is(n,"set") )
      {
        Xtmpl_Set_Value(ctx,n);
      }
    else if ( Xnode_Tag_Is(n,"inc") )
      {
        Xtmpl_Inc_Value(ctx,n);
      }
    else if ( Xnode_Tag_Is(n,"gadget") )
      {
        char *operate = Xnode_Value_Get_Str(n,"$",0);
        Xtmpl_Call_Gadget(ctx,operate,bf);
      }
    else if ( Xnode_Tag_Is(n,"dump") )
      Xtmpl_Handle_Dump_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"seleref") )
      Xtmpl_Handle_Seleref_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"ifvalue") )
      Xtmpl_Handle_Ifvalue_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"ifgroup") )
      Xtmpl_Handle_Ifgroup_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"switch") )
      Xtmpl_Handle_Switch_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"foreach") )
      Xtmpl_Handle_Foreach_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"repeat") )
      Xtmpl_Handle_Repeat_Out(bf,n,ctx);
    else if ( Xnode_Tag_Is(n,"at") )
      Xtmpl_Handle_At_Out(bf,n,ctx,0);
    else if ( Xnode_Tag_Is(n,"at1") )
      Xtmpl_Handle_At_Out(bf,n,ctx,1);
    else
      /* skip */
      ;
  }
#endif
  ;

void Xtmpl_Step_Up(YO_BUFFER *bf, YO_XTMPL_UP *up, char *content, YO_XTMPL_CTX *ctx)
#ifdef _YO_XTMPL_BUILTIN 
  {
    if ( up )
      {
        YO_XNODE *n = Xnode_Down_If(up->tmpl,"content");
        if ( !n ) n = up->tmpl;
        n = Xnode_Down(n);
        while ( n )
          {
            if ( Xnode_Tag_Is(n,"liftup") )
              {
                YO_XTMPL_CTX ctx1 = *ctx;
                Xtmpl_Step_Up(bf,up->up,Xnode_Value_Get_Str(n,"$","content"),&ctx1);
              }
            else
              {
                Xtmpl_Handle_Node_Out(bf,n,ctx);
              }
            n = Xnode_Next(n);
          }
      }
  }
#endif
  ;

void Xtmpl_Step_Down_Global_Set(YO_XNODE *tmpl, YO_XNODE *end, YO_DICTO *global, YO_XDATA *model)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XNODE *n;
    YO_BUFFER *bf = Buffer_Init(0);
    YO_XTMPL_CTX ctx = {0};
    ctx.model  = model;
    ctx.each   = &model->root;
    ctx.global = global;
    n = Xnode_Down(tmpl);
    while ( n != end )
      {
        if ( Xnode_Tag_Is(n,"liftup") )
          __Raise(YO_ERROR_ILLFORMED,"liftup is found before extends");
        else
          Xtmpl_Handle_Node_Out(bf,n,&ctx);
        n = Xnode_Next(n);
      }
  }
#endif
  ;
  
void Xtmpl_Step_Down(YO_BUFFER *bf, YO_XNODE *tmpl, YO_XDATA *model)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_DICTO *global = Dicto_Refs();
    YO_XTMPL_UP upstep_q[32] = { {tmpl, 0}, };
    YO_XNODE *down; 
    YO_XTMPL_UP *upstep = upstep_q;
    
    for ( down = Xnode_Down_If(tmpl,"extends"); down; )
      {
        Xtmpl_Step_Down_Global_Set(upstep->tmpl,down,global,model);
        upstep[1].up = upstep;
        ++upstep;
        upstep->tmpl = down;
        down = Xnode_Down_If(down,"extends");
      }
      
    fflush(stderr);
    
    __Gogo
      {
        YO_XTMPL_CTX ctx = {0};
        ctx.model  = model;
        ctx.each   = &model->root;
        ctx.global = global;
        ctx.tmpl   = tmpl;
        Xtmpl_Step_Up(bf,upstep,"content",&ctx);
      }
  }
#endif
  ;

char *Xtmpl_Produce_Out(YO_BUFFER *bf, YO_XDATA *tmpl, YO_XDATA *model)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_BUFFER *xbf = bf;
    if ( !xbf ) xbf = Buffer_Init(0);
    Buffer_Append(bf,"\n",1);
    Xtmpl_Step_Down(bf,&tmpl->root,model);
    return bf?xbf->at:Buffer_Take_Data(xbf);
  }
#endif
  ;

#ifdef _YO_XTMPL_BUILTIN 

static YO_DICTO *Xtmpl_Cache_Dicto = 0;

void Xtmpl_Release_Chache_Dicto()
  {
    __Unrefe(Xtmpl_Cache_Dicto);
  }
  
YO_DICTO *Xtmpl_Autoload_Dicto()
  {
    __Xchg_Interlock
      if ( !Xtmpl_Cache_Dicto )
        {
          Xtmpl_Cache_Dicto = __Refe(Dicto_Refs());
          atexit((void*)Xtmpl_Release_Chache_Dicto);
        }
    return Xtmpl_Cache_Dicto;
  }

YO_XDATA *Xtmpl_Cache_Template(char *root, char *S, char *lang)
  {
    YO_XDATA *doc = 0;
    YO_DICTO *dt = Xtmpl_Autoload_Dicto();
    void *reco = Dicto_Get(dt,S,&doc);
    if ( reco != &doc )
      doc = reco;
    else
      {
        __Try
          {
            YO_XNODE *n;
            doc = Xtmpl_Load_Template(root,S);
            if ( lang ) 
              {
                n = Xnode_Insert(&doc->root,"set");
                Xnode_Value_Set_Str(n,"$","#language");
                n = Xnode_Insert(n,"text");
                Xnode_Value_Set_Str(n,"$",lang);
              }
          }
        __Catch(YO_ERROR_DOESNT_EXIST) ;
        Dicto_Put(dt,S,__Refe(doc)); /* doc can be NULL */
      }
    return doc;
  }

#endif

YO_XDATA *Xtmpl_Autoload(char *root,  char *name, char **langlist /*zero terminated langiages list*/)
#ifdef _YO_XTMPL_BUILTIN 
  {
    YO_XDATA *doc = 0;
    __Auto_Release /* document is retaining by template cache */
      {
        if (langlist) for ( ; !doc && *langlist; ++langlist )
          {
            char *S = Str_Join_3('.',name,*langlist,"xtmpl");
            doc = Xtmpl_Cache_Template(root,S,*langlist);
          }
        if ( !doc )
          {
            char *S = Str_Join_2('.',name,"xtmpl");
            doc = Xtmpl_Cache_Template(root,S,0);
          }
      }
    if ( !doc )
      __Raise_Format(YO_ERROR_DOESNT_EXIST,("template %s doesn't exist at %s",name,root));
    return doc;
  }
#endif
  ;
  
#endif /* C_once_40BDAC30_45A4_4FC1_810C_05757F2DC413 */

Changes to yoyo.hc.

912
913
914
915
916
917
918

919
920
921
922
923
924
925
926
927
928
929
....
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
      Yo_Fatal(YO_ERROR_OUT_OF_MEMORY,0,0,0);
    memcpy(p,src,size);
    return p;
  }
#endif
  ;


typedef struct _YO_AUTORELEASE
  {
    void *ptr;
    void (*cleanup)(void *);
  }
  YO_AUTORELEASE;

enum { YO_MAX_ERROR_BTRACE = 25 };

typedef struct _YO_ERROR_INFO
  {
................................................................................

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

        if ( nfo->auto_top == nfo->auto_count )
          Yo_Extend_Csup_Autopool();
        nfo->auto_pool[nfo->auto_top].ptr = ptr;
        nfo->auto_pool[nfo->auto_top].cleanup = cleanup?cleanup:free;
      }
    return ptr;
  }
#endif
  ;

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







>



|







 







|







912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
....
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
      Yo_Fatal(YO_ERROR_OUT_OF_MEMORY,0,0,0);
    memcpy(p,src,size);
    return p;
  }
#endif
  ;

typedef void (*pool_cleanup_t)(void*); 
typedef struct _YO_AUTORELEASE
  {
    void *ptr;
    pool_cleanup_t cleanup;
  }
  YO_AUTORELEASE;

enum { YO_MAX_ERROR_BTRACE = 25 };

typedef struct _YO_ERROR_INFO
  {
................................................................................

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

        if ( nfo->auto_top == nfo->auto_count )
          Yo_Extend_Csup_Autopool();
        nfo->auto_pool[nfo->auto_top].ptr = ptr;
        nfo->auto_pool[nfo->auto_top].cleanup = cleanup?cleanup:(void*)free;
      }
    return ptr;
  }
#endif
  ;

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