/tags/rel-1-3-24/SWIG/Source/Modules/browser.cxx
C++ | 422 lines | 351 code | 46 blank | 25 comment | 60 complexity | 322363ac3fe6abfbf1a0843a239aea99 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1/* ----------------------------------------------------------------------------- 2 * browser.cxx 3 * 4 * A web-base parse tree browser using SWILL. This is an optional 5 * feature that's normally disabled. 6 * 7 * Author(s) : David Beazley (beazley@cs.uchicago.edu) 8 * 9 * Copyright (C) 2002. The University of Chicago 10 * See the file LICENSE for information on usage and redistribution. 11 * ----------------------------------------------------------------------------- */ 12 13char cvsroot_browser_cxx[] = "$Header$"; 14 15#include "swigmod.h" 16 17#ifdef SWIG_SWILL 18extern "C" { 19#include "swill.h" 20} 21 22static FILE *out = 0; 23static Node *view_top = 0; 24 25class Browser : public Dispatcher { 26 void show_checkbox(Node *t, Node *n) { 27 int v = 0; 28 if (Getmeta(n,"visible")) { 29 v = 1; 30 } 31 if (v) { 32 Printf(out,"<a name=\"n%x\"></a>[<a href=\"hide.html?node=0x%x&hn=0x%x#n%x\">-</a>] ", n, t, n,n); 33 } else { 34 Printf(out,"<a name=\"n%x\"></a>[<a href=\"show.html?node=0x%x&hn=0x%x#n%x\">+</a>] ", n, t, n,n); 35 } 36 } 37 void show_attributes(Node *obj) { 38 if (!Getmeta(obj,"visible")) return; 39 String *os = NewString(""); 40 String *k; 41 Iterator ki; 42 ki = First(obj); 43 while (ki.key) { 44 k = ki.key; 45 if ((Cmp(k,"nodeType") == 0) || (Cmp(k,"firstChild") == 0) || (Cmp(k,"lastChild") == 0) || 46 (Cmp(k,"parentNode") == 0) || (Cmp(k,"nextSibling") == 0) || 47 (Cmp(k,"previousSibling") == 0) || (*(Char(k)) == '$')) { 48 /* Do nothing */ 49 } else if (Cmp(k,"parms") == 0) { 50 String *o = NewString(""); 51 Printf(o,"%s", ParmList_protostr(Getattr(obj,k))); 52 Replaceall(o,"&","&"); 53 Replaceall(o,"<","<"); 54 Replaceall(o,">",">"); 55 Printf(os,"<a href=\"data.html?n=0x%x\">?</a> %-12s - %s\n", Getattr(obj,k), k, o); 56 Delete(o); 57 } else { 58 DOH *o; 59 char *trunc = ""; 60 if (DohIsString(Getattr(obj,k))) { 61 o = Str(Getattr(obj,k)); 62 if (Len(o) > 70) { 63 trunc = "..."; 64 } 65 Replaceall(o,"&","&"); 66 Replaceall(o,"<","<"); 67 Printf(os,"<a href=\"data.html?n=0x%x\">?</a> %-12s - \"%(escape)-0.70s%s\"\n", Getattr(obj,k), k, o, trunc); 68 Delete(o); 69 } else { 70 Printf(os,"<a href=\"data.html?n=0x%x\">?</a> %-12s - 0x%x\n", Getattr(obj,k), k, Getattr(obj,k)); 71 } 72 } 73 ki = Next(ki); 74 } 75 Printf(out,"<FONT color=\"#660000\"><pre>\n%s</pre></FONT>\n", Char(os)); 76 Delete(os); 77 } 78 79public: 80 virtual int emit_one(Node *n) { 81 char *tag = Char(nodeType(n)); 82 char *file = Char(Getfile(n)); 83 int line = Getline(n); 84 char *name = GetChar(n,"name"); 85 86 show_checkbox(view_top, n); 87 Printf(out,"<b><a href=\"index.html?node=0x%x\">%s</a></b>", n, tag); 88 if (name) { 89 Printf(out," (%s)", name); 90 } 91 Printf(out,". %s:%d\n", file, line); 92 Printf(out,"<br>"); 93 Dispatcher::emit_one(n); 94 return SWIG_OK; 95 } 96 virtual int emit_children(Node *n) { 97 if (Getmeta(n,"visible")) { 98 Printf(out,"<blockquote>\n"); 99 Dispatcher::emit_children(n); 100 Printf(out,"</blockquote>\n"); 101 } 102 return SWIG_OK; 103 } 104 virtual int defaultHandler(Node *n) { 105 show_attributes(n); 106 return SWIG_OK; 107 } 108 virtual int top(Node *n) { 109 show_attributes(n); 110 emit_children(n); 111 return SWIG_OK; 112 } 113 virtual int includeDirective(Node *n) { 114 show_attributes(n); 115 emit_children(n); 116 return SWIG_OK; 117 } 118 virtual int importDirective(Node *n) { 119 show_attributes(n); 120 emit_children(n); 121 return SWIG_OK; 122 } 123 124 virtual int extendDirective(Node *n) { 125 show_attributes(n); 126 emit_children(n); 127 return SWIG_OK; 128 } 129 virtual int classDeclaration(Node *n) { 130 show_attributes(n); 131 emit_children(n); 132 return SWIG_OK; 133 } 134 135 virtual int templateDeclaration(Node *n) { 136 show_attributes(n); 137 emit_children(n); 138 return SWIG_OK; 139 } 140 141 virtual int enumDeclaration(Node *n) { 142 show_attributes(n); 143 emit_children(n); 144 return SWIG_OK; 145 } 146 virtual int typemapDirective(Node *n) { 147 show_attributes(n); 148 emit_children(n); 149 return SWIG_OK; 150 } 151 virtual int namespaceDeclaration(Node *n) { 152 show_attributes(n); 153 emit_children(n); 154 return SWIG_OK; 155 } 156 virtual int usingDeclaration(Node *n) { 157 show_attributes(n); 158 emit_children(n); 159 return SWIG_OK; 160 } 161 162}; 163 164static int browser_exit = 0; 165static Node *tree_top = 0; 166static Browser *browse = 0; 167 168/* ---------------------------------------------------------------------- 169 * exit_handler() - Force the browser to exit 170 * ---------------------------------------------------------------------- */ 171 172void exit_handler(FILE *f) { 173 browser_exit = 1; 174 Printf(f,"Terminated.\n"); 175} 176 177/* ---------------------------------------------------------------------- 178 * node_handler() - Generate information about a specific node 179 * ---------------------------------------------------------------------- */ 180 181static void display(FILE *f, Node *n) { 182 /* Print standard HTML header */ 183 184 Printf(f,"<HTML><HEAD><TITLE>SWIG-%s</TITLE></HEAD><BODY BGCOLOR=\"#ffffff\">\n", PACKAGE_VERSION); 185 Printf(f,"<b>SWIG-%s</b><br>\n", PACKAGE_VERSION); 186 Printf(f,"[ <a href=\"exit.html\">Exit</a> ]"); 187 Printf(f," [ <a href=\"index.html?node=0x%x\">Top</a> ]", tree_top); 188 if (n != tree_top) { 189 Printf(f," [ <a href=\"index.html?node=0x%x\">Up</a> ]", parentNode(n)); 190 } 191 Printf(f," [ <a href=\"symbol.html\">Symbols</a> ]"); 192 Printf(f,"<br><hr><p>\n"); 193 194 out = f; 195 196 browse->emit_one(n); 197 198 /* Print standard footer */ 199 Printf(f,"<br><hr></BODY></HTML>\n"); 200 201} 202 203void node_handler(FILE *f) { 204 Node *n = 0; 205 if (!swill_getargs("p(node)", &n)) { 206 n = tree_top; 207 } 208 view_top = n; 209 display(f,n); 210} 211 212 213/* ---------------------------------------------------------------------- 214 * hide_handler() - Hide a node 215 * ---------------------------------------------------------------------- */ 216 217void hide_handler(FILE *f) { 218 Node *n = 0; 219 if (!swill_getargs("p(hn)", &n)) { 220 n = 0; 221 } 222 if (n) { 223 Delmeta(n,"visible"); 224 } 225 node_handler(f); 226} 227 228void show_handler(FILE *f) { 229 Node *n = 0; 230 if (!swill_getargs("p(hn)", &n)) { 231 n = 0; 232 } 233 if (n) { 234 Setmeta(n,"visible","1"); 235 } 236 node_handler(f); 237} 238 239void raw_data(FILE *out, Node *obj) { 240 if (!obj) return; 241 if (DohIsMapping(obj)) { 242 String *k; 243 Iterator ki; 244 String *os = NewString(""); 245 Printf(os,"Hash {\n"); 246 ki = First(obj); 247 while (ki.key) { 248 k = ki.key; 249 DOH *o; 250 const char *trunc = ""; 251 if (DohIsString(Getattr(obj,k))) { 252 o = Str(Getattr(obj,k)); 253 if (Len(o) > 70) { 254 trunc = "..."; 255 } 256 Replaceall(o,"<","<"); 257 Printf(os," <a href=\"data.html?n=0x%x\">?</a> %-12s - \"%(escape)-0.70s%s\"\n", Getattr(obj,k), k, o, trunc); 258 Delete(o); 259 } else { 260 Printf(os," <a href=\"data.html?n=0x%x\">?</a> %-12s - 0x%x\n", Getattr(obj,k), k, Getattr(obj,k)); 261 } 262 ki = Next(ki); 263 } 264 Printf(os,"}\n"); 265 Printf(out,"<FONT color=\"#660000\"><pre>\n%s</pre></FONT>\n", Char(os)); 266 Delete(os); 267 } else if (DohIsString(obj)) { 268 String *o = Str(obj); 269 Replaceall(o,"<","<"); 270 Printf(out,"<FONT color=\"#660000\"><pre>\n%s</pre></FONT>\n", Char(o)); 271 Delete(o); 272 } else if (DohIsSequence(obj)) { 273 int i; 274 String *os = NewString(""); 275 Printf(os,"List [\n"); 276 for (i = 0; i < Len(obj); i++) { 277 DOH *o = Getitem(obj,i); 278 const char *trunc = ""; 279 if (DohIsString(o)) { 280 String *s = Str(o); 281 if (Len(s) > 70) { 282 trunc = "..."; 283 } 284 Replaceall(o,"<","<"); 285 Printf(os," <a href=\"data.html?n=0x%x\">?</a> [%d] - \"%(escape)-0.70s%s\"\n", o,i,s, trunc); 286 Delete(s); 287 } else { 288 Printf(os," <a href=\"data.html?n=0x%x\">?</a> [%d] - 0x%x\n", o, i, o); 289 } 290 } 291 Printf(os,"\n]\n"); 292 Printf(out,"<FONT color=\"#660000\"><pre>\n%s</pre></FONT>\n", Char(os)); 293 Delete(os); 294 } 295} 296 297void data_handler(FILE *f) { 298 DOH *n = 0; 299 if (!swill_getargs("p(n)", &n)) { 300 n = 0; 301 } 302 Printf(f,"<HTML><HEAD><TITLE>SWIG-%s</TITLE></HEAD><BODY BGCOLOR=\"#ffffff\">\n", PACKAGE_VERSION); 303 Printf(f,"<b>SWIG-%s</b><br>\n", PACKAGE_VERSION); 304 Printf(f,"[ <a href=\"exit.html\">Exit</a> ]"); 305 Printf(f," [ <a href=\"index.html?node=0x%x\">Top</a> ]", tree_top); 306 Printf(f,"<br><hr><p>\n"); 307 if (n) { 308 raw_data(f,n); 309 } 310 /* Print standard footer */ 311 Printf(f,"<br><hr></BODY></HTML>\n"); 312} 313 314void symbol_handler(FILE *f) { 315 Symtab *sym; 316 char *name = 0; 317 318 Printf(f,"<HTML><HEAD><TITLE>SWIG-%s</TITLE></HEAD><BODY BGCOLOR=\"#ffffff\">\n", PACKAGE_VERSION); 319 Printf(f,"<b>SWIG-%s</b><br>\n", PACKAGE_VERSION); 320 Printf(f,"[ <a href=\"exit.html\">Exit</a> ]"); 321 Printf(f," [ <a href=\"index.html?node=0x%x\">Top</a> ]", tree_top); 322 Printf(f," [ <a href=\"symbol.html\">Symbols</a> ]"); 323 Printf(f,"<br><hr><p>\n"); 324 325 if (!swill_getargs("p(sym)|s(name)", &sym, &name)) { 326 sym = Swig_symbol_getscope(""); 327 name = 0; 328 } 329 if (!sym) { 330 Printf(f,"No symbol table specified!\n"); 331 return; 332 } 333 { 334 String *q = Swig_symbol_qualifiedscopename(sym); 335 if (!Len(q)) { 336 Printf(f,"<b>Symbol table: :: (global)</b><br>\n"); 337 } else { 338 Printf(f,"<b>Symbol table: %s</b><br>\n", q); 339 } 340 Delete(q); 341 } 342 343 fprintf(f,"<p><form action=\"symbol.html\" method=GET>\n"); 344 fprintf(f,"Symbol lookup: <input type=text name=name size=40></input><br>\n"); 345 fprintf(f,"<input type=hidden name=sym value=\"0x%x\">\n", sym); 346 fprintf(f,"Submit : <input type=submit></input>\n"); 347 fprintf(f,"</form>"); 348 349 if (name) { 350 Node *n = Swig_symbol_clookup(name,sym); 351 Printf(f,"Symbol '%s':\n", name); 352 Printf(f,"<blockquote>\n"); 353 if (!n) { 354 Printf(f,"Not defined!\n"); 355 } else { 356 raw_data(f,n); 357 } 358 Printf(f,"</blockquote>\n"); 359 } 360 361 Printf(f,"<p><b>Nested scopes</b><br>\n"); 362 Printf(f,"<blockquote><pre>\n"); 363 { 364 Hash *h; 365 h = firstChild(sym); 366 while (h) { 367 Printf(f,"<a href=\"symbol.html?sym=0x%x\">%s</a>\n", h, Getattr(h,"name")); 368 h = nextSibling(h); 369 } 370 } 371 Printf(f,"</pre></blockquote>\n"); 372 373 Printf(f,"<p><b>Symbol table contents</b></br>\n"); 374 raw_data(f,Getattr(sym,"symtab")); 375 Printf(f,"<br><hr></BODY></HTML>\n"); 376 377} 378#endif 379 380void 381Swig_browser(Node *top, int port) { 382#ifdef SWIG_SWILL 383 int sport; 384 browser_exit = 0; 385 386 /* Initialize the server */ 387 sport = swill_init(port); 388 if (sport < 0) { 389 Printf(stderr,"Couldn't open socket on port %d. Sorry.\n", port); 390 return; 391 } 392 browse = new Browser(); 393 Setmeta(top,"visible","1"); 394 tree_top = top; 395 396 Printf(stderr,"SWIG: Tree browser listening on port %d\n", sport); 397 398 swill_handle("exit.html", exit_handler,0); 399 swill_handle("index.html", node_handler, 0); 400 swill_handle("hide.html", hide_handler,0); 401 swill_handle("show.html", show_handler,0); 402 swill_handle("data.html", data_handler,0); 403 swill_handle("symbol.html", symbol_handler, 0); 404 swill_netscape("index.html"); 405 406 while (!browser_exit) { 407 swill_serve(); 408 } 409 Printf(stderr,"Browser terminated.\n"); 410 swill_close(); 411 delete browse; 412 return; 413#else 414 (void)top; 415 (void)port; 416#endif 417} 418 419 420 421 422