home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-386-Vol-2of3.iso / b / bricon20.zip / bricons / parser.c < prev    next >
C/C++ Source or Header  |  1992-07-30  |  8KB  |  393 lines

  1. static char* sccs_buttons_c = "%W%%G%";
  2.  
  3. #include "gen.h"
  4.  
  5. static    int     ReadALine PROTO((FILE*, char**));
  6. static    int    CheckForComments PROTO((FILE*));
  7. static    int    KeyWordLength PROTO((char*));
  8. static  int    GetKeyword PROTO((char*));
  9. static    void    GetTitle PROTO((char*, char**));
  10. static    int    GetSymbol PROTO((char*));
  11. static    void    ConvertTitle PROTO((char*));
  12. extern    int    ParseFile PROTO((FILE*, Info*, char*));
  13. extern    void    DefaultButtons PROTO((Info *,int));
  14. extern    void    CreateSubButton PROTO((Info*, int, int, char*));
  15. extern    void    CreateMainMenuButton PROTO((Info*, int, int, char*, char*));
  16. extern  void    CreateSubMenuButton PROTO((int, char*, int, char*, int));
  17.  
  18.  
  19.  
  20. extern  int
  21. ProcessFile(data, prog_name)
  22. Info    *data;
  23. char    *prog_name;
  24. {
  25.         int     i = 0;
  26.         FILE    *fp;
  27.  
  28.         fp = fopen(data->icon_file,"r");
  29.         if(fp == NULL)
  30.         {
  31.         (void) fprintf(stderr,"\n%s: unable to open file: %s\n", prog_name, data->icon_file);
  32.                 fp = fopen(data->icon_file,"w");
  33.                 (void) fprintf(fp,"# Default .briconsrc file\n");
  34.                 (void) fprintf(fp,"# A line starting with a '#' is a comment line\n");
  35.                 (void) fprintf(fp,"# first line must have keyword %%icon or %%text\n");
  36.                 (void) fprintf(fp,"# to indicate if button is an icon or text\n");
  37.                 (void) fprintf(fp,"# for more information select the help button\n");
  38.                 (void) fprintf(fp,"\n%%text test1\n");
  39.                 (void) fprintf(fp,"xterm\n");
  40.                 fclose(fp);
  41.         }
  42.         else
  43.         {
  44.                 i = ParseFile(fp, data, prog_name);
  45.                 fclose(fp);
  46.         if(i == 0)
  47.         {
  48.             (void) fprintf(stderr,"\n%s: Very sorry cannot understand file: %s Is it corrupted?\n", prog_name, data->icon_file);
  49.             DefaultButtons(data,i);
  50.             return(0);
  51.         }
  52.         else if(data->default_buttons == True)
  53.         {
  54.             DefaultButtons(data,i); 
  55.             return(i);
  56.         }
  57.         else
  58.             return(--i);
  59.     }
  60.     DefaultButtons(data,i);
  61.     return(0);
  62. }
  63.  
  64. extern int
  65. ParseFile(fp, data, prog_name)
  66. FILE    *fp;
  67. Info    *data;
  68. char    *prog_name;
  69. {
  70.     int    line_no = 0;
  71.     int    prev_kword = UNKNOWN;
  72.     int    kword = UNKNOWN;
  73.     int    button_no = 0;
  74.     int    in_sub_menu = FALSE;
  75.     int    sub_menu_no = 0;
  76.     int    found_title = FALSE;
  77.     char    ch, *title = NULL;
  78.     char    *heading = NULL;
  79.     char    *exe_command = NULL;
  80.     char    path[MAXPATHLEN];
  81.  
  82.     GetPath(path);
  83.     ch = getc(fp);
  84.     while(ch != EOF)
  85.     {
  86.         ungetc(ch,fp);
  87.         line_no += ReadALine(fp, &heading);
  88.         if(heading == NULL)
  89.         {
  90.             fprintf(stderr,"\n%s: Very sorry cannot understand file: %s is it corrupted?\n", prog_name, data->icon_file);
  91.             exit(1);
  92.         }
  93.         if(*heading == '%')
  94.         {
  95.             prev_kword = kword;
  96.             kword = GetSymbol(heading);
  97.             switch(kword)
  98.             {
  99.                 case SUB_MENU:
  100.                     if(in_sub_menu)
  101.                     {
  102.                         fprintf(stderr,"%s: error: only one sub menu allowed per button.\n", prog_name);
  103.                         break;
  104.                     }
  105.                     else
  106.                     {
  107.                         in_sub_menu = TRUE;
  108.                         CreateSubButton(data, 
  109.                         button_no, prev_kword, title);
  110.                     }
  111.                     break;
  112.                 case END_SUB_MENU:
  113.                     if(in_sub_menu)
  114.                     {
  115.                         if(sub_menu_no == 0)
  116.                         {
  117.                             fprintf(stderr, "\n%s: \"%s\" line: %d cannot have a sub menu with no buttons.\n", prog_name, data->icon_file, line_no - 1);
  118.                             exit(1);
  119.                         }
  120.                         sub_menu_no = 0;
  121.                         button_no++;
  122.                         in_sub_menu = FALSE;
  123.                     }
  124.                     break;
  125.                 case ICON:
  126.                 case ICONPLUS:
  127.                 case TEXT:
  128.                 case TEXTPLUS:
  129.                     found_title = TRUE;
  130.                     GetTitle(heading, &title);
  131.                     ConvertTitle(title);
  132.                     break;
  133.                 case UNKNOWN:
  134.                 default:
  135.                     fprintf(stderr,"%s: syntax error: in %s line: %d\n", prog_name, data->icon_file, line_no);
  136.                     break;
  137.             }
  138.         }
  139.         else
  140.         {
  141.             if((found_title) && (GetCommand(path, heading, 
  142.                     data->icon_file, prog_name, line_no)))
  143.             {
  144.                 exe_command = malloc(strlen(heading) + 1);
  145.                 if(exe_command == NULL)
  146.                 {
  147.                     fprintf(stderr,"\nFATAL ERROR: malloc out of memory.\n");
  148.                     exit(1);
  149.                 }
  150.                 strcpy(exe_command, heading);
  151.                 if(in_sub_menu)
  152.                 {
  153.                     if(sub_menu_no > MAXSUBBUTTONS)
  154.                     {
  155.                         fprintf(stderr,"\n%s: only %d buttons allowed for each sub menu.\n", prog_name, MAXSUBBUTTONS + 1);
  156.                     }
  157.                     else
  158.                     {
  159.                         CreateSubMenuButton(kword, 
  160.                         title, sub_menu_no, 
  161.                         exe_command, data->sensitive);
  162.                         sub_menu_no++;
  163.                     }
  164.                 }
  165.                 else
  166.                 {
  167.                     CreateMainMenuButton(data, button_no, 
  168.                         kword, title, exe_command);
  169.                     button_no++;
  170.                 }
  171.                 found_title = FALSE;
  172.             }
  173.         }    
  174.         free(heading);
  175.         ch = getc(fp);
  176.     }
  177.     return(button_no);
  178. }
  179.  
  180. static void
  181. GetTitle(buffer, title)
  182. char    *buffer;
  183. char    **title;
  184. {
  185.     int    i = 0;
  186.     int    x = 0;
  187.     int    title_len = 0;
  188.     char    *c_ptr = NULL;
  189.  
  190.     i = KeyWordLength(buffer);
  191.     title_len = strlen(buffer) - i;
  192.     *title = malloc(title_len + 1);
  193.     if(title == NULL)
  194.     {
  195.         fprintf(stderr,"\nFATAL ERROR: malloc out of memory.\n");
  196.         exit(1);
  197.     }
  198.     c_ptr = buffer;
  199.     while(x <= i)
  200.     {
  201.         c_ptr++;
  202.         x++;
  203.     }
  204.     strncpy(*title, c_ptr, title_len);
  205. }
  206.  
  207. static int
  208. GetSymbol(buffer)
  209. char    *buffer;
  210. {
  211.     char    key_word[100];
  212.     int        i = 0;
  213.  
  214.     if((strcmp(buffer, "%sub_menu\n")) == 0)
  215.         return(SUB_MENU);
  216.     else if((strcmp(buffer, "%end_sub_menu\n")) == 0)
  217.         return(END_SUB_MENU);
  218.     else
  219.     {
  220.         i = KeyWordLength(buffer);
  221.         strncpy(key_word, buffer, i);
  222.         key_word[i++] = '\0';
  223.         return(GetKeyword(key_word));
  224.     }
  225. }
  226.  
  227. static int
  228. KeyWordLength(buffer)
  229. char    *buffer;
  230. {
  231.     char    *c_ptr = buffer;
  232.     int    i = 0;
  233.  
  234.     while((!isspace(*c_ptr)) && (*c_ptr != '\0'))
  235.     {
  236.         i++;
  237.         c_ptr++;
  238.     }
  239.     return(i);
  240. }
  241.  
  242. static int
  243. ReadALine(fp,command)
  244. FILE    *fp;
  245. char    **command;
  246. {
  247.     int    space_allocated = 20;
  248.     int    length = 0;
  249.     int    l_no = 0;
  250.     char    ch;
  251.     char    *c_line;
  252.  
  253.     c_line = malloc(space_allocated + 1);
  254.     if(c_line == NULL)
  255.     {
  256.         (void) fprintf(stderr,"\nFATAL ERROR: malloc out of memory.\n");
  257.         exit(1);
  258.     }
  259.     l_no += CheckForComments(fp);
  260.     while (((ch = getc(fp))!= EOF) && (ch != '\n'))
  261.     {
  262.         if(length == space_allocated)
  263.         {
  264.             space_allocated += 10;
  265.             c_line =  realloc(c_line, space_allocated + 1);
  266.         }
  267.         c_line[length++] = ch;
  268.     }
  269.     c_line[length] = '\0';
  270.     *command = malloc(strlen(c_line) +1);
  271.     if(*command == NULL)
  272.     {
  273.         (void) fprintf(stderr,"\nFATAL ERROR: malloc out of memory.\n");
  274.         exit(1);
  275.     }
  276.     strcpy(*command, c_line);
  277.     free(c_line);
  278.     l_no++;
  279.     return(l_no);
  280. }
  281.  
  282.  
  283. static int
  284. GetKeyword(label)
  285. char    *label;
  286. {
  287.     int        symbol = UNKNOWN;
  288.  
  289.     if((strcmp(label,"%icon")) == 0)
  290.         symbol = ICON;
  291.     else if((strcmp(label,"%icon+")) == 0)
  292.         symbol = ICONPLUS;
  293.     else if((strcmp(label,"%text")) == 0)
  294.         symbol = TEXT;
  295.     else if((strcmp(label,"%text+")) == 0)
  296.         symbol = TEXTPLUS;
  297.     else if((strcmp(label,"%sub_menu")) == 0)
  298.         symbol = SUB_MENU;
  299.     else if((strcmp(label,"%end_sub_menu")) == 0)
  300.         symbol = END_SUB_MENU;
  301.     else if((strcmp(label,"\0")) == 0)
  302.         symbol = END;
  303.     else
  304.         symbol = UNKNOWN;
  305.     return(symbol);
  306. }
  307.  
  308.  
  309. /*
  310.     Read the button title from file pointed at by file pointer fp. 
  311. */
  312. static void
  313. ConvertTitle(title)
  314. char    *title;
  315. {
  316.     int    i = 0;
  317.     int    j = 0;
  318.     char    *c_ptr = NULL;
  319.     char    *c_tmp = NULL;
  320.  
  321.     c_tmp = malloc(strlen(title) + 1);
  322.     if(c_tmp == NULL)
  323.     {
  324.         (void) fprintf(stderr,"\nFATAL ERROR: malloc out of memory.\n");
  325.         exit(1);
  326.     }
  327.     c_ptr = title;
  328.     while(c_ptr[i] != '\0')
  329.     {
  330.         if(c_ptr[i] == '\\')
  331.         {
  332.             i++;
  333.             if(c_ptr[i] == 'n')
  334.             {
  335.                 i--;
  336.                 c_tmp[j] = '\n';
  337.                 i += 2;
  338.                 j++;
  339.             }
  340.             else
  341.                 i--;
  342.         }
  343.         c_tmp[j] = c_ptr[i];
  344.         i++;
  345.         j++;
  346.     }
  347.     c_tmp[j++] = '\0';
  348.     *title = '\0';
  349.     strcpy(title, c_tmp);
  350.     free(c_tmp);
  351. }
  352.  
  353.  
  354. /*
  355.     Any line of text beginning with a "#" character is a comment line.
  356.     This function looks for comment lines and ignores them.
  357. */
  358. static int
  359. CheckForComments(fp)
  360. FILE    *fp;
  361. {
  362.     int    l_no = 0;
  363.     int    is_comment = TRUE;
  364.     char    ch;
  365.  
  366.     ch = getc(fp);
  367.     while(is_comment)
  368.     {
  369.         if(ch == '\n')
  370.             l_no++;
  371.         if(ch == '#')
  372.         {
  373.             while((ch != EOF) && (ch != '\n'))
  374.                 ch = getc(fp);
  375.         }
  376.         else if(isspace(ch))
  377.         {
  378.              while((ch != EOF) && (isspace(ch)))
  379.              {
  380.                 ch = getc(fp);
  381.                 if(ch == '\n')
  382.                     l_no++;
  383.              }
  384.         }
  385.         else
  386.         {
  387.             is_comment = FALSE;
  388.             ungetc(ch,fp);
  389.         }
  390.     }
  391.     return(l_no);
  392. }
  393.