-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.c
726 lines (682 loc) · 19.8 KB
/
session.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <wchar.h>
#include <locale.h>
#include <liba.h>
#include <tsdb.h>
#include <ace/lexicon.h>
#include <ace/hash.h>
#include <ace/tree.h>
#include <ace/reconstruct.h>
#include "treebank.h"
int allow_islands = 0;
char *dqescape(char *raw);
struct tree *duplicate_tree(struct tree *tin)
{
struct tree *tout = malloc(sizeof(*tin));
int i;
*tout = *tin;
tout->label = tin->label?strdup(tin->label):NULL;
tout->shortlabel = tin->shortlabel?strdup(tin->shortlabel):NULL;
tout->tokenids = malloc(sizeof(int)*tin->ntokens);
memcpy(tout->tokenids, tin->tokenids, sizeof(int)*tin->ntokens);
tout->tokens = malloc(sizeof(char*)*tin->ntokens);
for(i=0;i<tin->ntokens;i++)tout->tokens[i] = strdup(tin->tokens[i]);
tout->daughters = malloc(sizeof(struct tree*)*tin->ndaughters);
for(i=0;i<tin->ndaughters;i++)
{
tout->daughters[i] = duplicate_tree(tin->daughters[i]);
tout->daughters[i]->parent = tout;
}
tout->lexhead = NULL;
return tout;
}
int verify(struct tb_edge *e, struct reconstruction_error *error)
{
assert(e->unpackings==1);
struct tree *t = extract_tree(e, 0);
clear_slab();
void callback(struct tree *t, struct dg *d) { }
struct dg *d = reconstruct_tree_or_error(t, callback, error);
if(!d)error->node = duplicate_tree(error->node);
free_tree(t);
clear_slab();
return d?1:0;
}
void compute_forcings(struct parse *P, struct tb_edge **forced_edge, int max, long long N)
{
int i, j;
for(i=0;i<P->nedges;i++)
{
struct tb_edge *e = P->edges[i];
//printf("edge #%d had %lld solutions out of %lld\n", e->id, e->solutions, N);
if(e->solutions == N && e->unpackings == 1)
{
for(j=2*e->from+1;j<2*e->to;j++)
{
assert(j < max);
if(!forced_edge[j])
forced_edge[j] = e;
else if(e->from < forced_edge[j]->from || e->to > forced_edge[j]->to)
forced_edge[j] = e;
}
}
}
}
void get_discriminants(struct parse *P, int from, int to, long long ntrees, int *Nd, struct disc **D)
{
int i, j;
int have_span = (from == -1)?0:1;
int nd = 0;
struct disc *d = NULL;
for(i=0;i<P->nedges;i++)
{
struct tb_edge *e = P->edges[i];
if(have_span && (e->from != from || e->to != to))continue;
//printf("discriminant: edge #%d [%d - %d] %s has %lld trees\n", e->id, e->from, e->to, e->sign, e->solutions);
if(e->solutions == 0 || e->solutions == ntrees)continue;
for(j=0;j<nd;j++)
if(!strcmp(d[j].sign, e->sign) && e->from == d[j].from && e->to == d[j].to && ((e->ndaughters==0)?1:0) == d[j].lexical)break;
if(j==nd)
{
nd++;
d = realloc(d, sizeof(struct disc)*nd);
d[nd-1].sign = e->sign;
d[nd-1].count = 0;
d[nd-1].from = e->from;
d[nd-1].to = e->to;
d[nd-1].lexical = (e->ndaughters==0)?1:0;
}
d[j].count += e->solutions;
//printf("%s now has %d\n", d[j].sign, d[j].count);
}
for(i=0;i<nd;i++)
if(d[i].count == ntrees)
{
memmove(d+i, d+i+1, sizeof(struct disc)*(--nd - i));
i--;
}
*Nd = nd;
*D = d;
}
void find_discriminants(FILE *f, struct parse *P, int from, int to, long long ntrees)
{
int i;
int nd = 0;
struct disc *d = NULL;
get_discriminants(P, from, to, ntrees, &nd, &d);
for(i=0;i<nd;i++)
{
if(i > 0)fprintf(f, ",");
char *esc = dqescape(d[i].sign);
fprintf(f, "{sign:\"%s\",count:%lld,from:%d,to:%d}", esc, d[i].count, d[i].from, d[i].to);
free(esc);
}
if(d)free(d);
}
int has_unpacking(struct tb_edge *e)
{
int i;
if(e->unpackings)return 1;
for(i=0;i<e->npack;i++)
if(e->pack[i]->unpackings)return 1;
return 0;
}
char *get_uclabel(char *stack, int k)
{
while(k-- > 0 && stack)
{
stack = strchr(stack, '@');
if(stack)stack++;
}
if(!stack)return NULL;
char *rv = strdup(stack);
if(strchr(rv, '@'))
*strchr(rv, '@') = 0;
return rv;
}
struct tree *extract_tree(struct tb_edge *e, int ucdepth)
{
int i;
// 'e' and its packed edges together have exactly one unpacking amongst them
if(!e->unpackings)
{
for(i=0;i<e->npack;i++)
if(e->pack[i]->unpackings)break;
assert(i < e->npack);
e = e->pack[i];
}
//assert(e->unpackings == 1);
struct tree *t = calloc(sizeof(*t),1);
t->label = get_uclabel(e->sign_with_lexnames, ucdepth);
t->tfrom = e->from;
t->tto = e->to;
char *l2 = get_uclabel(e->sign_with_lexnames, ucdepth+1);
if(l2)
{
free(l2);
t->ndaughters = 1;
t->daughters = calloc(sizeof(struct tree*),1);
t->daughters[0] = extract_tree(e, ucdepth+1);
}
else
{
t->ndaughters = e->ndaughters;
if(!e->ndaughters)
{
// lexeme node
// need to supply an orthography daughter tree node, which has a few more fields
struct tree *orth = calloc(sizeof(struct tree),1);
t->ndaughters = 1;
t->daughters = calloc(sizeof(struct tree*),1);
t->daughters[0] = orth;
int wlen = 0;
for(i=0;i<e->ntokens;i++)
{
if(i)wlen++;
wlen += wcslen(e->tokens[i]->text);
}
wchar_t *orth_str = malloc(sizeof(wchar_t)*(wlen+4)), *wp=orth_str;
for(i=0;i<e->ntokens;i++)
{
if(i)wp += swprintf(wp, (orth_str+wlen+3-wp), L" ");
wp += swprintf(wp, (orth_str+wlen+3-wp), L"%S", e->tokens[i]->text);
}
int mlen = wcstombs(NULL, orth_str, 0) + 1;
char *mos = malloc(mlen+1);
wcstombs(mos, orth_str, mlen);
free(orth_str);
//orth->label = strdup("_"); // our use of the tree doesn't depend on this and it's inconvenient to get it here
orth->label = mos;
orth->tfrom = t->tfrom;
orth->tto = t->tto;
// ->ntokens ( = the lexeme's ->stemlen...)
// ->tokens (could be ""s for our purposes)
// ->cfrom, ->cto (could be phony for our purposes)
struct lexeme *lex = get_lex_by_name_hash(t->label);
assert(lex != NULL);
orth->ntokens = lex->stemlen;
assert(orth->ntokens == e->ntokens);
orth->tokens = calloc(sizeof(char*),orth->ntokens);
orth->tokenids = calloc(sizeof(int),orth->ntokens);
int j;
for(j=0;j<orth->ntokens;j++)
{
orth->tokenids[j] = e->tokens[j]->id;
orth->tokens[j] = strdup(e->tokens[j]->avmstr);
}
orth->cfrom = orth->cto = -1;
}
else
{
t->daughters = calloc(sizeof(struct tree*),t->ndaughters);
for(i=0;i<e->ndaughters;i++)
t->daughters[i] = extract_tree(e->daughter[i], 0);
}
}
return t;
}
void fsend_tree(FILE *f, struct tree *t)
{
int i;
int lexical = 0;
if(t->ndaughters == 1 && t->daughters[0]->ndaughters == 0)lexical = 1;
char *esc;
if(lexical)
{
// for whatever reason, grammarians prefer to see the lexical type as the full-form sign for a lexical node, rather than the lexeme identifier
struct lexeme *lex = get_lex_by_name_hash(t->label);
esc = dqescape(lex->lextype->name);
}
else esc = dqescape(t->label);
char *sesc = dqescape(t->shortlabel);
fprintf(f, "{label: \"%s\", shortlabel: \"%s\", from:%d, to:%d, daughters: [", esc, sesc, t->tfrom, t->tto);
free(esc);
free(sesc);
if(!lexical)for(i=0;i<t->ndaughters;i++)
{
if(i)fprintf(f, ",");
fsend_tree(f, t->daughters[i]);
}
fprintf(f, "]}");
}
int send_tree(FILE *f, struct tb_edge *e, struct reconstruction_error *error)
{
struct tree *t = extract_tree(e, 0);
assert(t != NULL);
void callback(struct tree *t, struct dg *d)
{ t->shortlabel = label_dag(d, "?"); }
struct dg *result = reconstruct_tree_or_error(t, callback, error);
if(result != NULL)
fsend_tree(f, t);
free_tree(t);
clear_slab();
if(result == NULL)return -1;
else return 0;
}
void send_mrs(FILE *f, struct tb_edge *e)
{
struct tree *t = extract_tree(e, 0);
assert(t != NULL);
struct dg *result = reconstruct_tree_or_error(t, NULL, NULL);
assert(result != NULL);
free_tree(t);
clear_mrs();
struct mrs *m = extract_mrs(result);
assert(m != NULL);
int i, j;
fprintf(f, "{ltop: %d, index: %d, rels: [\n", m->ltop->vnum, m->index->vnum);
for(i=0;i<m->nrels;i++)
{
if(i)fprintf(f, ",");
struct mrs_ep *e = &m->rels[i];
char *esc = dqescape(e->pred);
fprintf(f, "{pred:\"%s\",label:%d,args:[", esc, e->lbl->vnum);
free(esc);
for(j=0;j<e->nargs;j++)
fprintf(f, "%s{name:\"%s\",value:%d}", j?",":"", e->args[j].name, e->args[j].value->vnum);
fprintf(f, "]}");
}
fprintf(f, "], hcons: [");
for(i=0;i<m->nhcons;i++)
{
char *type = "??";
if(m->hcons[i].type == hcons_leq)type = "<=";
else if(m->hcons[i].type == hcons_geq)type = ">=";
else if(m->hcons[i].type == hcons_qeq)type = "=q";
fprintf(f, "%s{type:\"%s\",left:%d,right:%d}", i?",":"", type, m->hcons[i].hi->vnum, m->hcons[i].lo->vnum);
}
extern int mrs_enable_icons;
if(mrs_enable_icons)
{
fprintf(f, "], icons: [");
for(i=0;i<m->nicons;i++)
fprintf(f, "%s{type:\"%s\",left:%d,right:%d}",
i?",":"", m->icons[i].type, m->icons[i].left->vnum, m->icons[i].right->vnum);
}
fprintf(f, "], vars: [");
for(i=0;i<m->vlist.nvars;i++)
{
struct mrs_var *v = m->vlist.vars[i];
if(i)fprintf(f, ",");
if(v->is_const)
{
char *esc = dqescape(v->name);
fprintf(f, "\"%s\"", esc);
free(esc);
}
else
{
fprintf(f, "{type:\"%s\",num:%d,props:[", v->type, v->vnum);
for(j=0;j<v->nprops;j++)
fprintf(f, "%s{name:\"%s\",value:\"%s\"}",
j?",":"", v->props[j].name, v->props[j].value);
fprintf(f, "]}");
}
}
fprintf(f, "]}");
clear_slab();
}
void web_session(FILE *f, char *query)
{
int id = atoi(query);
struct session *S = get_session(id);
if(!S) { webreply(f, "404 no such session"); return; }
char *constraints = strchr(query, ';');
if(constraints)constraints++;
else { webreply(f, "500 bad syntax"); return; }
char *rqc = constraints;
constraints = strchr(constraints, ';');
int requesting_constraints = atoi(rqc);
if(constraints)constraints++;
else { webreply(f, "500 bad syntax"); return; }
char *span = constraints;
constraints = strchr(constraints, ';');
if(constraints)constraints++;
else { webreply(f, "500 bad syntax"); return; }
int from,to, have_span = 0;
if(2 == sscanf(span, "%d:%d;", &from, &to))
have_span = 1;
char *d;
int i, nc=0;
struct constraint *c=NULL;
char *olddecbits = NULL;
if(!requesting_constraints)
{
for(d=strtok(constraints,"&");d;d=strtok(NULL,"&"))
{
if(!strncmp(d, "_olddecs=", 9))
{
olddecbits = d + 9;
continue;
}
int from, to;
char sign[1024];
assert(strlen(d) < 1024);
int type = -1;
int inferred = 0;
if(4 == sscanf(d, "%[^:]:=:%d-%d:%d", sign, &from, &to, &inferred))
type = constraintExactly;
else if(4 == sscanf(d, "%[^:]:+:%d-%d:%d", sign, &from, &to, &inferred))
type = constraintPresent;
else if(4 == sscanf(d, "%[^:]:-:%d-%d:%d", sign, &from, &to, &inferred))
type = constraintAbsent;
if(type == -1)
{
printf("bad constraint: '%s'\n", d);
badconst:
webreply(f, "500 bad constraint");
if(c)
{
int i;
for(i=0;i<nc;i++)
if(c[i].sign)free(c[i].sign);
free(c);
}
return;
}
nc++;
c = realloc(c,sizeof(*c)*nc);
c[nc-1].sign = strcmp(sign, "span")?cgidecode(sign):NULL;
c[nc-1].from = from;
c[nc-1].to = to;
c[nc-1].type = type;
c[nc-1].inferred = inferred;
if(!strcmp(sign, "span"))
c[nc-1].type = constraintIsAConstituent;
printf("constraint: %s [%d-%d] type %d\n", sign, from, to, type);
}
// save the local_dec array
if(S->local_dec)
{
for(i=0;i<S->nlocal_dec;i++)
if(S->local_dec[i].sign)free(S->local_dec[i].sign);
free(S->local_dec);
}
S->local_dec = nc?malloc(sizeof(struct constraint)*nc):NULL;
S->nlocal_dec = nc;
memcpy(S->local_dec, c, sizeof(struct constraint)*nc);
for(i=0;i<S->nlocal_dec;i++)
if(S->local_dec[i].sign)
S->local_dec[i].sign = strdup(S->local_dec[i].sign);
}
else
{
// copy the local_dec array to c
c = S->nlocal_dec?malloc(sizeof(struct constraint)*S->nlocal_dec):NULL;
nc = S->nlocal_dec;
memcpy(c, S->local_dec, sizeof(struct constraint)*nc);
for(i=0;i<nc;i++)
if(c[i].sign)
c[i].sign = strdup(c[i].sign);
}
char *bits = olddecbits;
for(i=0;i<S->ngold_dec;i++)
{
if(bits && !bits[i])goto badconst;
if((!bits && (!requesting_constraints || S->gold_active[i]))
|| (bits && bits[i] == '1'))
{
// include this gold constraint
nc++;
c = realloc(c,sizeof(*c)*nc);
c[nc-1] = S->gold_dec[i];
c[nc-1].sign = strdup(c[nc-1].sign);
S->gold_active[i] = 1;
}
else S->gold_active[i] = 0;
}
fflush(stdout);
void send_escaped(char *key, char *value)
{
char *esc = dqescape(value);
fprintf(f, "%s: \"%s\",\n", key, esc);
free(esc);
}
http_headers(f, "text/javascript");
fprintf(f, "{");
send_escaped("profile_id", S->profile_id);
send_escaped("item_status", status_string(get_t_active(S->profile_id, S->parse_id)));
send_escaped("comment", get_t_comment(S->profile_id, S->parse_id));
send_escaped("item_id", S->item_id);
send_escaped("item", S->input);
//fprintf(f, "allow_islands: %d,\n", allow_islands);
long long ntrees = count_remaining_trees(S->parse, c, nc);
fprintf(f, "ntrees: %lld,\n", ntrees);
fprintf(f, "tokens: [");
int slen = 1;
for(i=0;i<S->parse->ntokens;i++)
{
struct tb_token *t = S->parse->tokens[i];
if(i)fprintf(f, ",");
char *foo = malloc(5 * wcslen(t->text));
wcstombs(foo, t->text, 5 * wcslen(t->text));
char *esc = dqescape(foo);
free(foo);
fprintf(f, "{from:%d,to:%d,cfrom:%d,cto:%d,text:\"%s\"}", t->from, t->to, t->cfrom, t->cto, esc);
free(esc);
if(t->to >= slen)slen = t->to+1;
}
fprintf(f, "],\n");
struct tb_edge *forced_edges[slen*2+1];
bzero(forced_edges, sizeof(struct tb_edge*)*(slen*2+1));
compute_forcings(S->parse, forced_edges, slen*2+1, ntrees);
fprintf(f, "chunks: [");
int nch = 0, l0 = 0, have_error = 0;
struct reconstruction_error first_error;
for(i=1;i<slen*2+1;i++)
{
if(forced_edges[l0] != forced_edges[i] || i == slen*2)
{
if(nch++)fprintf(f, ",");
int status = forced_edges[l0]?1:0;
struct reconstruction_error error;
int valid = status && verify(forced_edges[l0], &error);
if(status && !valid)
{
status = 2;
// inspect error!
if(!have_error)
{
first_error = error;
have_error = 1;
}
else
{
if(error.comment)free(error.comment);
if(error.path)free(error.path);
if(error.node)free_tree(error.node);
}
}
fprintf(f, "{from:%d,to:%d,state:%d}", l0, i, status);
//printf("%d - %d is %d\n", l0, i, status);
l0 = i;
}
}
fprintf(f, "],\n");
if(have_error)
{
char *lesc = dqescape(first_error.node->label);
fprintf(f, "recons_error: {");
switch(first_error.type)
{
case reconsNoSuchLexeme:
fprintf(f, "type:\"No such lexeme as %s\",", lesc);
break;
case reconsNoSuchRule:
fprintf(f, "type:\"No such rule as %s\",", lesc);
break;
case reconsIncompatibleToken:
fprintf(f, "type:\"Failed to unify token list into lexeme %s.\",", lesc);
break;
case reconsRuleGLB:
fprintf(f, "type:\"GLB failed for daughter %d [zero-based] into rule %s.\",", first_error.daughter, lesc);
send_escaped("path", first_error.path);
send_escaped("type1", first_error.type1);
send_escaped("type2", first_error.type2);
break;
case reconsRuleCycle:
fprintf(f, "type:\"Cycle detected with rule %s.\",", lesc);
break;
default: fprintf(f, "type:\"Unknown reconstruction error\","); break;
}
if(first_error.type == reconsRuleGLB)fprintf(f, "has_glb: 1,");
else fprintf(f, "has_glb: 0,");
send_escaped("comment", first_error.comment?:"");
fprintf(f, "node:{from:%d, to:%d, label:\"%s\"}}, ",
first_error.node->tfrom, first_error.node->tto, lesc);
if(first_error.comment) free(first_error.comment);
if(first_error.path) free(first_error.path);
if(first_error.node) free_tree(first_error.node);
free(lesc);
}
fprintf(f, "discriminants: [");
if(have_span)
find_discriminants(f, S->parse, from, to, ntrees);
else find_discriminants(f, S->parse, -1, -1, ntrees);
fprintf(f, "],");
if(S->pref_tree)
{
int in = tree_satisfies_constraints(S->pref_tree, c, nc);
fprintf(f, "oldgold:{in:%d},", in);
}
if(S->ngold_dec)
{
fprintf(f, "olddec:[");
for(i=0;i<S->ngold_dec;i++)
{
if(i)fprintf(f, ",");
struct constraint *C = &S->gold_dec[i];
fprintf(f, "{sign:\"%s\",type:\"%s\",from:%d,to:%d,inferred:%d,enabled:%d}",
C->sign, (C->type == constraintPresent)?"+":"-",
C->from, C->to, C->inferred, S->gold_active[i]);
}
fprintf(f, "],");
}
if(S->nlocal_dec)
{
fprintf(f, "dec:[");
for(i=0;i<S->nlocal_dec;i++)
{
if(i)fprintf(f, ",");
struct constraint *C = &S->local_dec[i];
char *ch = "=";
if(C->type == constraintPresent)ch = "+";
if(C->type == constraintAbsent)ch = "-";
if(C->type == constraintIsAConstituent)ch = "=";
fprintf(f, "{sign:\"%s\",type:\"%s\",from:%d,to:%d,inferred:%d,enabled:1}",
C->sign?:"span", ch, C->from, C->to, C->inferred);
}
fprintf(f, "],");
}
if(have_span)
{
for(i=0;i<S->parse->nedges;i++)
{
struct tb_edge *e = S->parse->edges[i];
if(e->from == from && e->to == to && e->solutions == ntrees && e->unpackings == 1)
break; // this is the root of the tree that we want to send back.
}
if(i<S->parse->nedges)
{
fprintf(f, "trees: [");
struct reconstruction_error error;
int res = send_tree(f, S->parse->edges[i], &error);
fprintf(f, "],");
if(res != 0)
fprintf(f, "error: 'Reconstruction failed on that tree.',"); // shouldn't happen unless we're also sending a detailed description of the error with the chunking code
else
{
fprintf(f, "mrses: [");
send_mrs(f, S->parse->edges[i]);
fprintf(f, "],");
}
}
else fprintf(f, "trees: [],");
}
else if(ntrees == 1)
{
fprintf(f, "trees: [");
for(i=0;i<S->parse->nroots;i++)
if(has_unpacking(S->parse->roots[i]))break;
assert(i < S->parse->nroots);
struct reconstruction_error error;
int res = send_tree(f, S->parse->roots[i], &error);
fprintf(f, "],");
if(res != 0)
fprintf(f, "error: 'Reconstruction failed on that tree.',"); // shouldn't happen unless we're also sending a detailed description of the error with the chunking code
else
{
fprintf(f, "mrses: [");
send_mrs(f, S->parse->roots[i]);
fprintf(f, "],");
}
}
else fprintf(f, "trees: [],\n");
fprintf(f, "end:0}\n");
fclose(f);
/*for(i=0;i<S->parse->nedges;i++)
{
struct tb_edge *e = S->parse->edges[i];
printf("edge #%d: [%d-%d] %s has %lld solutions with %lld unpackings\n", e->id, e->from, e->to, e->sign, e->solutions, e->unpackings);
}
for(i=0;i<S->parse->nroots;i++)
printf("root #%d\n", S->parse->roots[i]->id);*/
for(i=0;i<nc;i++)
if(c[i].sign)free(c[i].sign);
if(c)free(c);
}
void web_comment(FILE *f, char *query)
{
int id = atoi(query);
struct session *S = get_session(id);
if(!S) { webreply(f, "404 no such session"); return; }
char *rest = strchr(query, '&');
if(!rest) { webreply(f, "500 bad syntax"); return; }
rest = cgidecode(rest+1);
S->comment = strdup(rest);
char prof_path[10240];
sprintf(prof_path, "%s/%s", tsdb_home_path, S->profile_id);
char t_active[32];
sprintf(t_active, "%d", get_t_active(S->profile_id, S->parse_id));
int result = write_tree(prof_path, S->parse_id, "1", t_active, getenv("LOGNAME"), S->comment, get_t_start(S->profile_id, S->parse_id), get_t_end(S->profile_id, S->parse_id));
webreply(f, "200 ok");
}
/*
int edge_id_gen = 1000000000;
struct tb_edge *make_island(struct session *S, int from, int to)
{
}
struct tb_edge *make_island_root(struct parse *p, int nislands, struct tb_edge **islands)
{
struct tb_edge *r = calloc(sizeof(struct tb_edge),1);
r->sign = strdup("bridge");
r->sign_with_lexnames = strdup("bridge");
r->id = edge_id_gen++;
r->from = 99999;
r->to = 0;
int i;
return r;
}
void web_island(FILE *f, char *query)
{
int id, from, to;
if(3 != sscanf(query, "%d;%d,%d", &id, &from, &to)) { webreply(f, "500 bad syntax"); return; }
struct session *S = get_session(id);
if(!S) { webreply(f, "404 no such session"); return; }
S->parse->nroots = 0;
S->islands = realloc(S->islands, sizeof(struct tb_edge*)*(S->nislands+1));
S->islands[S->nislands++] = make_island(S->parse, from, to);
struct tb_edge *r = make_island_root(S->parse);
S->parse->nroots = 1;
S->parse->roots = realloc(S->parse->roots, sizeof(struct tb_edge*));
S->parse->roots[0] = r;
webreply(f, "200 ok");
}
*/