home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gdb-4.16-base.tgz / gdb-4.16-base.tar / fsf / gdb / sim / ppc / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-18  |  6.2 KB  |  274 lines

  1. /*  This file is part of the program psim.
  2.  
  3.     Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18.  
  19.     */
  20.  
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <stdio.h>
  25. #include <fcntl.h>
  26. #include <ctype.h>
  27.  
  28. #include "config.h"
  29. #include "misc.h"
  30. #include "lf.h"
  31. #include "table.h"
  32.  
  33. #ifdef HAVE_UNISTD_H
  34. #include <unistd.h>
  35. #endif
  36.  
  37. #ifdef HAVE_STDLIB_H
  38. #include <stdlib.h>
  39. #endif
  40.  
  41. struct _table {
  42.   size_t size;
  43.   char *buffer;
  44.   char *pos;
  45.   int line_nr;
  46.   int nr_fields;
  47.   int nr_model_fields;
  48.   char *file_name;
  49. };
  50.  
  51. extern table *
  52. table_open(char *file_name,
  53.        int nr_fields,
  54.        int nr_model_fields)
  55. {
  56.   int fd;
  57.   struct stat stat_buf;
  58.   table *file;
  59.  
  60.   /* create a file descriptor */
  61.   file = ZALLOC(table);
  62.   ASSERT(file != NULL);
  63.   file->nr_fields = nr_fields;
  64.   file->nr_model_fields = nr_model_fields;
  65.  
  66.   /* save the file name */
  67.   file->file_name = (char*)zalloc(strlen(file_name) + 1);
  68.   ASSERT(file->file_name != NULL);
  69.   strcpy(file->file_name, file_name);
  70.  
  71.   /* open the file */
  72.   fd = open(file->file_name, O_RDONLY, 0);
  73.   ASSERT(fd >= 0);
  74.  
  75.   /* determine the size */
  76.   if (fstat(fd, &stat_buf) < 0) {
  77.     perror("table_open.fstat");
  78.     exit(1);
  79.   }
  80.   file->size = stat_buf.st_size;
  81.  
  82.   /* allocate this much memory */
  83.   file->buffer = (char*)zalloc(file->size+1);
  84.   if(file->buffer == NULL) {
  85.     perror("table_open.calloc.file->size+1");
  86.     exit(1);
  87.   }
  88.   file->pos = file->buffer;
  89.  
  90.   /* read it in */
  91.   if (read(fd, file->buffer, file->size) < file->size) {
  92.     perror("table_open.read");
  93.     exit(1);
  94.   }
  95.   file->buffer[file->size] = '\0';
  96.  
  97.   /* done */
  98.   close(fd);
  99.   return file;
  100. }
  101.  
  102.  
  103. extern table_entry *
  104. table_entry_read(table *file)
  105. {
  106.   int field;
  107.   table_entry *entry;
  108.  
  109.   /* skip comments/blanks */
  110.   while(1) {
  111.     /* leading white space */
  112.     while (*file->pos != '\0'
  113.        && *file->pos != '\n'
  114.        && isspace(*file->pos))
  115.       file->pos++;
  116.     /* comment */
  117.     if (*file->pos == '#') {
  118.       do {
  119.     file->pos++;
  120.       } while (*file->pos != '\0' && *file->pos != '\n');
  121.     }
  122.     /* end of line? */
  123.     if (*file->pos == '\n') {
  124.       file->pos++;
  125.       file->line_nr++;
  126.     }
  127.     else
  128.       break;
  129.   }
  130.   if (*file->pos == '\0')
  131.     return NULL;
  132.  
  133.   /* create this new entry */
  134.   entry = (table_entry*)zalloc(sizeof(table_entry)
  135.                    + (file->nr_fields + 1) * sizeof(char*));
  136.   ASSERT(entry != NULL);
  137.   entry->file_name = file->file_name;
  138.   entry->nr_fields = file->nr_fields;
  139.  
  140.   /* break the line into its colon delimitered fields */
  141.   for (field = 0; field < file->nr_fields-1; field++) {
  142.     entry->fields[field] = file->pos;
  143.     while(*file->pos && *file->pos != ':' && *file->pos != '\n')
  144.       file->pos++;
  145.     if (*file->pos == ':') {
  146.       *file->pos = '\0';
  147.       file->pos++;
  148.     }
  149.   }
  150.  
  151.   /* any trailing stuff not the last field */
  152.   ASSERT(field == file->nr_fields-1);
  153.   entry->fields[field] = file->pos;
  154.   while (*file->pos && *file->pos != '\n') {
  155.     file->pos++;
  156.   }
  157.   if (*file->pos == '\n') {
  158.     *file->pos = '\0';
  159.     file->pos++;
  160.   }
  161.   file->line_nr++;
  162.  
  163.   /* if following lines begin with a star, add them to the model
  164.      section.  */
  165.   while ((file->nr_model_fields > 0) && (*file->pos == '*')) {
  166.     table_model_entry *model = (table_model_entry*)zalloc(sizeof(table_model_entry)
  167.                               + (file->nr_model_fields + 1) * sizeof(char*));
  168.     if (entry->model_last)
  169.       entry->model_last->next = model;
  170.     else
  171.       entry->model_first = model;
  172.     entry->model_last = model;
  173.  
  174.     /* break the line into its colon delimitered fields */
  175.     file->pos++;
  176.     for (field = 0; field < file->nr_model_fields-1; field++) {
  177.       model->fields[field] = file->pos;
  178.       while(*file->pos && *file->pos != ':' && *file->pos != '\n')
  179.     file->pos++;
  180.       if (*file->pos == ':') {
  181.     *file->pos = '\0';
  182.     file->pos++;
  183.       }
  184.     }
  185.  
  186.     /* any trailing stuff not the last field */
  187.     ASSERT(field == file->nr_model_fields-1);
  188.     model->fields[field] = file->pos;
  189.     while (*file->pos && *file->pos != '\n') {
  190.       file->pos++;
  191.     }
  192.     if (*file->pos == '\n') {
  193.       *file->pos = '\0';
  194.       file->pos++;
  195.     }
  196.  
  197.     file->line_nr++;
  198.     model->line_nr = file->line_nr;
  199.   }
  200.  
  201.   entry->line_nr = file->line_nr;
  202.  
  203.   /* if following lines are tab indented, put in the annex */
  204.   if (*file->pos == '\t') {
  205.     entry->annex = file->pos;
  206.     do {
  207.       do {
  208.     file->pos++;
  209.       } while (*file->pos != '\0' && *file->pos != '\n');
  210.       if (*file->pos == '\n') {
  211.     char *save_pos = ++file->pos;
  212.     int extra_lines = 0;
  213.     file->line_nr++;
  214.     /* Allow tab indented to have blank lines */
  215.     while (*save_pos == '\n') {
  216.       save_pos++;
  217.       extra_lines++;
  218.     }
  219.     if (*save_pos == '\t') {
  220.       file->pos = save_pos;
  221.       file->line_nr += extra_lines;
  222.     }
  223.       }
  224.     } while (*file->pos != '\0' && *file->pos == '\t');
  225.     if (file->pos[-1] == '\n')
  226.       file->pos[-1] = '\0';
  227.   }
  228.   else
  229.     entry->annex = NULL;
  230.  
  231.   /* return it */
  232.   return entry;
  233.  
  234. }
  235.  
  236.  
  237. extern void
  238. dump_table_entry(table_entry *entry,
  239.          int indent)
  240. {
  241.   printf("(table_entry*)%p\n", entry);
  242.  
  243.   if (entry != NULL) {
  244.     int field;
  245.     char sep;
  246.  
  247.     sep = ' ';
  248.     dumpf(indent, "(fields");
  249.     for (field = 0; field < entry->nr_fields; field++) {
  250.       printf("%c%s", sep, entry->fields[field]);
  251.       sep = ':';
  252.     }
  253.     printf(")\n");
  254.  
  255.     dumpf(indent, "(line_nr %d)\n", entry->line_nr);
  256.  
  257.     dumpf(indent, "(file_name %s)\n", entry->file_name);
  258.  
  259.     dumpf(indent, "(annex\n%s\n", entry->annex);
  260.     dumpf(indent, " )\n");
  261.  
  262.   }
  263. }
  264.  
  265.  
  266. extern void
  267. table_entry_lf_c_line_nr(lf *file,
  268.              table_entry *entry)
  269. {
  270.   lf_print_c_line_nr(file, entry->line_nr, entry->file_name);
  271. }
  272.  
  273.  
  274.