ceptr
 All Data Structures Files Functions Variables Typedefs Macros Modules Pages
spec_utils.c
Go to the documentation of this file.
1 
7 #include "spec_utils.h"
8 
9 bool G_done = false;
10 
11 // write out a tree as json
12 void wjson(SemTable *sem,T *t,char *n,int i) {
13  char fn[100];
14  char json[100000] = {0};
15  _t2json(sem,t,-1,json);
16  if (i >= 0)
17  sprintf(fn,"web/%s_%d.json",n,i);
18  else
19  sprintf(fn,"web/%s.json",n);
20  writeFile(fn,json,strlen(json));
21 }
22 
23 void dump2json(SemTable *sem,T *t,char *n) {
24  char fn[100];
25  char json[1000000] = {0};
26  _t2rawjson(sem,t,-1,json);
27  sprintf(fn,"web/%s.json",n);
28  writeFile(fn,json,strlen(json));
29 }
30 
31 // create a TREE_DELTA tree
32 T *makeDelta(Symbol sym,int *path,T *t,int count) {
33  T *d = _t_new_root(sym);
34  _t_new(d,TREE_DELTA_PATH,path,sizeof(int)*(_t_path_depth(path)+1));
35  _t_add(_t_newr(d,TREE_DELTA_VALUE),_t_clone(t));
36  if (count)
37  _t_newi(d,TREE_DELTA_COUNT,count);
38  return d;
39 }
40 
41 char *G_visdump_fn = 0;
42 int G_visdump_count = 0;
43 
44 void _visdump(SemTable *sem,T *x,int *path) {
45  T *delta = makeDelta(TREE_DELTA_REPLACE,path,x,1);
46  wjson(sem,delta,G_visdump_fn,G_visdump_count++);
47  _t_free(delta);
48 }
49 
50 void visdump(SemTable *sem,T *x) {
51  if (G_visdump_count) {
52  int *path = _t_get_path(x);
53  _visdump(sem,x,path);
54  free(path);
55  }
56 }
57 
58 #include "../src/process.h"
59 #include "../src/receptor.h"
60 
61 void _test_reduce_signals(Receptor *r) {
62  while (r->q->contexts_count) {
63  _p_reduceq(r->q);
64 
65  // @todo fix this fake signal sending which only works here in this test because
66  // the signals are being sent to ourself!
67  T *signals = r->pending_signals;
68  while(_t_children(signals)>0) {
69  T *s = _t_detach_by_idx(signals,1);
70  Error err = _r_deliver(r,s);
71  if (err) raise_error("delivery error: %d",err);
72  }
73  }
74 }
75 
76 char *doSys(char *cmd) {
77  FILE *pipe = popen(cmd, "r");
78  if (!pipe) raise_error("Error running %s\n",cmd);
79  int sz = 1000;
80  int remaining = sz;
81  char *buffer = malloc(sz);
82  char buf[255];
83  while (fgets(buf, 255, pipe) != NULL) {
84  int l = strlen(buf);
85  if (l > remaining) {
86  int increase_by = l < sz ? sz : l*2;
87  remaining += increase_by;
88  sz += increase_by;
89  buffer = realloc(buffer,sz);
90  }
91  memcpy(&buffer[sz-remaining],buf,l);
92  remaining -= l;
93  }
94  buffer[sz-remaining] = 0;
95  pclose(pipe);
96  return buffer;
97 }
T * _t_new_root(Symbol symbol)
Definition: tree.c:160
Definition: ceptr_types.h:114
T * _t_detach_by_idx(T *t, int i)
Definition: tree.c:278
int _t_path_depth(int *p)
Definition: tree.c:1365
int * _t_get_path(T *t)
Definition: tree.c:1384
T * _t_clone(T *t)
Definition: tree.c:589
Error _p_reduceq(Q *q)
Definition: process.c:2126
Q * q
process queue
Definition: ceptr_types.h:250
int contexts_count
number of active processes
Definition: ceptr_types.h:208
char * _t2rawjson(SemTable *sem, T *t, int level, char *buf)
Definition: tree.c:1785
void _t_add(T *t, T *c)
Definition: tree.c:261
char * _t2json(SemTable *sem, T *t, int level, char *buf)
Definition: tree.c:1912
Error _r_deliver(Receptor *r, T *signal)
Definition: receptor.c:954
int _t_children(T *t)
Definition: tree.c:1205
void _t_free(T *t)
Definition: tree.c:526