PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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