home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xcu16.zip / wlmCompiler / wlc.c < prev    next >
C/C++ Source or Header  |  1991-10-03  |  11KB  |  461 lines

  1. /*
  2.  * Copyright 1991 Cornell University
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software and its
  5.  * documentation for any purpose and without fee is hereby granted, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Cornell U. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Cornell U. makes no representations about the
  11.  * suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * CORNELL UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16.  * EVENT SHALL CORNELL UNIVERSITY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  18.  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  20.  * PERFORMANCE OF THIS SOFTWARE.
  21.  *
  22.  * Author:  Gene W. Dykes, Program of Computer Graphics
  23.  *          580 Theory Center, Cornell University, Ithaca, NY 14853
  24.  *          (607) 255-6713   gwd@graphics.cornell.edu
  25.  */
  26.  
  27.  
  28. /* *
  29.  * * This code is completely original.  Permission is granted to do with
  30.  * * it as one wishes.  I disclaim any responsibility whatsoever for
  31.  * * any bad things that may happen because of using it.
  32.  * */
  33.  
  34. #include <stdio.h>
  35. #include <X11/Xos.h>
  36. #include <sys/stat.h>
  37. #include <X11/IntrinsicP.h>
  38. #include <X11/StringDefs.h>
  39. #include <X11/Xresource.h>
  40. #include "CuStack.h"
  41. #include "wlc.h"
  42.  
  43. /* Private Definitions */
  44.  
  45. static char error_text[200] ;
  46.  
  47. /* Declarations of "private" calls */
  48.  
  49. static void    read_layout () ;
  50. static void    compile () ;
  51. static void    fetus_print () ;
  52. static void    managed_print () ;
  53. static void    resources_print () ;
  54. static void    directives_print () ;
  55. static void    pad_print () ;
  56.  
  57. static int    input () ;
  58. static void    unput () ;
  59. static void    yyerror () ;
  60. static int    yywrap () ;
  61.  
  62. /**
  63.  ***
  64.  **** Wlm Compiler
  65.  ***
  66.  **/
  67.  
  68. #define WLM_MAX_UNPUT 2000
  69. #define WLM_MAX_CONTEXT 2000
  70.  
  71. typedef struct
  72.     {
  73.     String input_buffer ;
  74.     Cardinal input_pointer ;
  75.     char unput_buffer[WLM_MAX_UNPUT] ;
  76.     Cardinal unput_pointer ;
  77.     char context_buffer[WLM_MAX_CONTEXT] ;
  78.     int context_ptr ; /* must not be Cardinal */
  79.     Cardinal line_number ;
  80.     String cur_file ;
  81.     } WlmInputRecord ;
  82.  
  83. static WlmInputRecord *layout_input ;
  84.  
  85. static WlmCompilerPart _ww, *ww = &_ww ;
  86.  
  87. main (argc, argv)
  88.     int argc ;
  89.     char *argv[] ;
  90. {
  91. char *input_file ;
  92. int argv_length ;
  93. if (argc < 2)
  94.     {
  95.     fprintf (stderr, "Usage: %s WLDL_file\n", argv[0]) ;
  96.     exit (1) ;
  97.     }
  98.  
  99. argv_length = strlen(argv[1]) ;
  100. if (strcmp(&argv[1][argv_length-3], ".wl") == 0)
  101.     {
  102.     /* This permits extensioned or extensionless input */
  103.     argv[1][argv_length-3] = '\0' ;
  104.     argv_length -= 3 ;
  105.     }
  106. ww->compiled_file = XtMalloc (argv_length + 4) ;
  107. ww->class_file = XtMalloc (argv_length + 4) ;
  108. ww->wldl_file = XtMalloc (argv_length + 4) ;
  109. input_file = XtMalloc (argv_length + 4) ;
  110. sprintf (ww->compiled_file, "%s.wc", argv[1]) ;
  111. sprintf (ww->class_file, "%s.cl", argv[1]) ;
  112. sprintf (ww->wldl_file, "%s.lw", argv[1]) ;
  113. sprintf (input_file, "%s.wl", argv[1]) ;
  114.  
  115. /*
  116.  * We'll read the file as one big string, then parse it.
  117.  */
  118.  
  119. layout_input = (WlmInputRecord *) XtMalloc (sizeof (WlmInputRecord)) ;
  120. read_layout (input_file, layout_input) ;
  121. yyparse () ;
  122. compile (ww->child_stack) ;
  123.  
  124. exit (0) ;
  125. }
  126.  
  127. /***** **** *** ** * read_layout * ** *** **** *****/
  128.  
  129. static void
  130. read_layout (file, layout_input)
  131.     String file ;
  132.     WlmInputRecord *layout_input ;
  133. {
  134. FILE *fd ;
  135. struct stat buf ;
  136.  
  137. fd = fopen (file, "r") ;
  138. if ((int)fd <= 0)
  139.     {
  140.     char *my_text = "Wlm: Couldn't open (%s) for reading\n" ;
  141.     char *etext = XtMalloc (strlen(my_text) + strlen(file)) ;
  142.     sprintf (etext, my_text, file) ;
  143.     XtError (etext) ;
  144.     }
  145.  
  146. /*
  147.  * Find out how big the layout file is.
  148.  * Read it in as one big chunk.
  149.  * Initialize some useful variables
  150.  */
  151.  
  152. fstat (fileno(fd), &buf) ;
  153. layout_input->input_buffer = XtMalloc (buf.st_size + 1) ;
  154. fread (layout_input->input_buffer, buf.st_size, 1, fd) ;
  155. layout_input->input_buffer[buf.st_size] = '\0' ;
  156.  
  157. Cu_copy_ds (&layout_input->cur_file, file) ; /* for error messages */
  158. layout_input->input_pointer = 0 ;
  159. layout_input->unput_pointer = 0 ;
  160. layout_input->line_number = 1 ;
  161. fclose (fd) ;
  162.  
  163. return ;
  164. }
  165.  
  166. typedef struct WlmClassnameList
  167.     {
  168.     String name ;
  169.     struct WlmClassnameList *next ;
  170.     } WlmClassnameList ;
  171.  
  172. typedef struct WlmWldlList
  173.     {
  174.     String name ;
  175.     struct WlmWldlList *next ;
  176.     } WlmWldlList ;
  177.  
  178. static WlmClassnameList *listC ;
  179. static WlmWldlList *listW ;
  180.  
  181. static void
  182. compile (stack)
  183.     Stack *stack ;
  184. {
  185. FILE *fd ;
  186. WlmClassnameList *chkC ;
  187. WlmWldlList *chkW ;
  188.  
  189. listC = NULL ;
  190. fd = fopen (ww->compiled_file, "w") ;
  191. fprintf (fd, "#WLDL\n") ;
  192. fprintf (fd, "%d\n", ww->max_depth) ;
  193. fetus_print (fd, (Fetus *)Cu_stack_peek(stack, STACK_NEXT_POP), 0) ;
  194. fclose (fd) ;
  195.  
  196. fd = fopen (ww->class_file, "w") ;
  197. for (chkC=listC;  chkC != NULL;  chkC = chkC->next)
  198.     {
  199.     fprintf (fd, "%s\n", chkC->name) ;
  200.     }
  201. fclose (fd) ;
  202.  
  203. fd = fopen (ww->wldl_file, "w") ;
  204. for (chkW=listW;  chkW != NULL;  chkW = chkW->next)
  205.     {
  206.     fprintf (fd, "%s\n", chkW->name) ;
  207.     }
  208. fclose (fd) ;
  209.  
  210. return ;
  211. }
  212.  
  213. #define LSTRNG(x) strlen(x), x
  214.  
  215. static void
  216. fetus_print (fd, fetus, pad)
  217.     FILE *fd ;
  218.     Fetus *fetus ;
  219.     int pad ;
  220. {
  221. Resource *resource ;
  222. Directive *directive ;
  223. WlmManageItem *managed ;
  224. Cardinal j ;
  225.  
  226. WlmClassnameList *chk ;
  227. for (chk=listC;  chk != NULL;  chk = chk->next)
  228.     {
  229.     if (strcmp (chk->name, fetus->class_name) == 0)
  230.     break ;
  231.     }
  232. if (chk == NULL)
  233.     {
  234.     chk = listC ;
  235.     listC = (WlmClassnameList *) XtMalloc (sizeof (WlmClassnameList)) ;
  236.     listC->name = fetus->class_name ;
  237.     listC->next = chk ;
  238.     }
  239.  
  240. fprintf (fd, "\n") ;
  241. pad_print (fd, pad) ;
  242. fprintf (fd, "%d %s\n", LSTRNG(fetus->class_name)) ;
  243.  
  244. pad_print (fd, pad) ;
  245. fprintf (fd, "%d\n", fetus->n_resources) ;
  246. for (resource=fetus->resource;  resource != NULL;  resource=resource->next)
  247.     {
  248.     resources_print (fd, resource, pad) ;
  249.     if (strcmp (fetus->class_name, "Wlm") == 0 &&
  250.         strcmp (resource->name, "file") == 0)
  251.     {
  252.     WlmWldlList *save = listW ;
  253.     listW = (WlmWldlList *) XtMalloc (sizeof (WlmWldlList)) ;
  254.     listW->name = resource->value ;
  255.     listW->next = save ;
  256.     }
  257.     }
  258.  
  259. pad_print (fd, pad) ;
  260. fprintf (fd, "%d\n", fetus->n_directives) ;
  261. for (directive=fetus->directives;  directive != NULL; directive=directive->next)
  262.     directives_print (fd, directive, pad) ;
  263.  
  264. pad_print (fd, pad) ;
  265. fprintf (fd, "%d\n", fetus->n_manage_list) ;
  266. for (managed=fetus->manage_list;  managed != NULL; managed=managed->next)
  267.     managed_print (fd, managed, pad) ;
  268.  
  269. pad_print (fd, pad) ;
  270. fprintf (fd, "%d\n", fetus->n_children) ;
  271. pad += 2 ;
  272. for (j=0;  j < fetus->n_children;  j++)
  273.     fetus_print (fd, fetus->children[j], pad) ;
  274.  
  275. return ;
  276. }
  277.  
  278. static void
  279. managed_print (fd, managed, pad)
  280.     FILE *fd ;
  281.     WlmManageItem *managed ;
  282.     int pad ;
  283. {
  284. pad_print (fd, pad) ;
  285. fprintf (fd, "%d (%s) %d %s %d (%s)\n",
  286.     LSTRNG(managed->widget), LSTRNG(managed->type), LSTRNG(managed->value)) ;
  287. return ;
  288. }
  289.  
  290. static void
  291. resources_print (fd, resource, pad)
  292.     FILE *fd ;
  293.     Resource *resource ;
  294.     int pad ;
  295. {
  296. pad_print (fd, pad) ;
  297. fprintf (fd, "%d %s %d (%s) %d %s\n",
  298.     LSTRNG(resource->name), LSTRNG(resource->value),
  299.     (resource->context == WlmLocalContext) ? strlen("Local") : strlen("Global"),
  300.     (resource->context == WlmLocalContext) ? "Local" : "Global") ;
  301. return ;
  302. }
  303.  
  304. static void
  305. directives_print (fd, directive, pad)
  306.     FILE *fd ;
  307.     Directive *directive ;
  308.     int pad ;
  309. {
  310. Cardinal i ;
  311. pad_print (fd, pad) ;
  312. fprintf (fd, "%d %s %d %s %d %s %d %d %s\n",
  313.     LSTRNG(directive->callback_name), LSTRNG(directive->target_class),
  314.     LSTRNG(directive->target_name), directive->n_call_comparisons,
  315.     directive->resource.name ?
  316.     strlen("XtSetValues") : strlen("PublicFunction"),
  317.     directive->resource.name ?
  318.     "XtSetValues" : "PublicFunction") ;
  319.  
  320. pad += 1 ;
  321. for (i=0;  i < directive->n_call_comparisons;  i++)
  322.     {
  323.     Cardinal j ;
  324.     static char *op_text[] = { "==", "!=", ">", "<", ">=", "<=" } ;
  325.  
  326.     pad_print (fd, pad) ;
  327.     fprintf (fd, "%d %s %d (%s) %d %d %s %d :",
  328.     LSTRNG(directive->call_data_converter[i]),
  329.     LSTRNG(directive->call_data[i]),
  330.     directive->call_data_operator[i],
  331.     LSTRNG(op_text[(int)directive->call_data_operator[i]]),
  332.     directive->n_call_indices[i]) ;
  333.  
  334.     for (j=0;  j < directive->n_call_indices[i];  j++)
  335.     fprintf (fd, " %d", directive->call_data_index[i][j]) ;
  336.     fprintf (fd, "\n") ;
  337.     }
  338. pad -= 1 ;
  339.  
  340. if (directive->resource.name)
  341.     {
  342.     pad_print (fd, pad) ;
  343.     fprintf (fd, "%d %s %d (%s) %d %s %d (%s)\n",
  344.     LSTRNG(directive->target_class), LSTRNG(directive->target_name),
  345.     LSTRNG(directive->resource.name), LSTRNG(directive->resource.value)
  346.     ) ; 
  347.     }
  348. else
  349.     {
  350.     Cardinal i ;
  351.     pad_print (fd, pad) ;
  352.     fprintf (fd, "%d %s %d\n", LSTRNG(directive->procedure),
  353.         directive->n_arguments) ;
  354.  
  355.     for (i=0;  i < directive->n_arguments;  i++)
  356.     {
  357.     pad_print (fd, pad) ;
  358.     fprintf (fd, "%d %s %d (%s)\n",
  359.         LSTRNG(directive->argument_converters[i]),
  360.         LSTRNG(directive->argument_strings[i])) ;
  361.     }
  362.     }
  363.  
  364. return ;
  365. }
  366.  
  367. static void
  368. pad_print (fd, pad)
  369.     FILE *fd ;
  370.     int pad ;
  371. {
  372. while (pad--)
  373.     fprintf (fd, " ") ;
  374. return ;
  375. }
  376.  
  377.  
  378. /**
  379.  ** The routines used to parse a wlm description
  380.  **/
  381.  
  382. /***** **** *** ** * "yyerror" * ** *** **** *****/
  383.  
  384. static void
  385. yyerror (s)
  386.     char *s ;
  387. {
  388. Cardinal length ;
  389. String my_text = "Wlm: Parsing error (%s)\nLine # %d in file '%s':\n" ;
  390. String etext ;
  391. length = strlen(my_text) + strlen(s) + WLM_MAX_CONTEXT + 20 ;
  392. etext = XtMalloc (length) ;
  393. sprintf (etext, my_text, s, layout_input->line_number, layout_input->cur_file) ;
  394. strcat (etext, layout_input->context_buffer) ;
  395. XtError (etext) ;
  396. }
  397.  
  398. /***** **** *** ** * the lex input routine * ** *** **** *****/
  399.  
  400. static int
  401. input ()
  402. {
  403. int t ;
  404. if (layout_input->unput_pointer)
  405.     {
  406.     t = layout_input->unput_buffer[layout_input->unput_pointer-1] ;
  407.     layout_input->unput_pointer-- ;
  408.     }
  409. else
  410.     {
  411.     t = layout_input->input_buffer[layout_input->input_pointer++] ;
  412.     }
  413.  
  414. if (t == '\n')
  415.     {
  416.     layout_input->line_number++ ;
  417.     layout_input->context_ptr = 0 ;
  418.     }
  419. else
  420. if (layout_input->context_ptr < WLM_MAX_CONTEXT)
  421.     {
  422.     layout_input->context_buffer[layout_input->context_ptr++] = t ;
  423.     layout_input->context_buffer[layout_input->context_ptr] = '\0' ;
  424.     }
  425. else
  426.     {
  427.     sprintf (error_text,
  428.         "Insufficient context buffer space for input character (%c)\n", t) ;
  429.     XtWarning (error_text) ;
  430.     }
  431.  
  432. return t ;
  433. }
  434.  
  435. /***** **** *** ** * the lex unput routine * ** *** **** *****/
  436.  
  437. static void
  438. unput (c)
  439.     int c ;
  440. {
  441. layout_input->unput_buffer[layout_input->unput_pointer++] = c ;
  442.  
  443. /*
  444.  * Just wipe out currrent line context characters.  Don't try to retrieve those
  445.  * already cycled out. (It seems highly unlikely that anything significant
  446.  * will ever be lost by this.)
  447.  */
  448.  
  449. if (layout_input->context_ptr > 0)
  450.     layout_input->context_ptr-- ;
  451.  
  452. if (c == '\n')
  453.     if (layout_input->line_number > 1)
  454.     layout_input->line_number-- ;
  455.  
  456. return ;
  457. }
  458.  
  459. #include "wlc.y.c"
  460.  
  461.