1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  
     | 
    
      /****************************************************************************/
/*                                                                          */
/*                           GNAT COMPILER TOOLS                            */
/*                                                                          */
/*                              G N A T C H P                               */
/*                                                                          */
/*                          C Implementation File                           */
/*                                                                          */
/*                             $Revision: 1.32 $                            */
/*                                                                          */
/*          Copyright (C) 1992-1997, Free Software Foundation, Inc.         */
/*                                                                          */
/* GNAT is free software;  you can  redistribute it  and/or modify it under */
/* terms of the  GNU General Public License as published  by the Free Soft- */
/* ware  Foundation;  either version 2,  or (at your option) any later ver- */
/* sion.  GNAT is distributed in the hope that it will be useful, but WITH- */
/* OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY */
/* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License */
/* for  more details.  You should have  received  a copy of the GNU General */
/* Public License  distributed with GNAT;  see file COPYING.  If not, write */
/* to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, */
/* MA 02111-1307, USA.                                                      */
/*                                                                          */
/* GNAT was originally developed  by the GNAT team at  New York University. */
/* It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). */
/*                                                                          */
/****************************************************************************/
/* This is the utility that takes the unit list output from the GNAT
 * compiler and uses it to actually write the individual files. It is
 * called as part of the gnatchop script.
 */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef O_BINARY
#define O_BINARY 0
#endif
#ifndef DIR_SEPARATOR
#if defined (__EMX__)
#define DIR_SEPARATOR '\\'
#else
#define DIR_SEPARATOR '/'
#endif
#endif
int open_read(char *path)
{
    return open(path, O_RDONLY | O_BINARY);
}
int open_create(char *path)
{
    return open(path, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
#if defined (__EMX__)
                S_IREAD | S_IWRITE);
#else
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
#endif
}
long file_length(int fd)
{
    /* Return the number of bytes in the named file. */
    int ret;
    struct stat statbuf;
    ret = fstat(fd, &statbuf);
    if (ret || !S_ISREG(statbuf.st_mode)) return 0L;
    return (statbuf.st_size);
}
int is_regular_file (char *name)
{
  int ret;
  struct stat statbuf;
  ret = stat(name, &statbuf);
  return (!ret && S_ISREG(statbuf.st_mode));
}
int is_directory_file (char *name)
{
  int ret;
  struct stat statbuf;
  ret = stat(name, &statbuf);
  return (!ret && S_ISDIR(statbuf.st_mode));
}
char   *optarg;                         /* Global argument pointer. */
static char *scan = NULL;               /* Private scan pointer. */
#define MAX_UNITS 1000  /* Maximum compilation units per single file */
main (int argc, char *argv [])
{
  int  fd1;
  int  i, j, n, unit;
  char *p;
  int  linenum;
  int  overwrite_option = 0;
  int  source_ref_option = 0;
  int  script_option = 0;
  int  errors = 0;
  char *ptr;
  char name [10000];
  char *names [MAX_UNITS];
  int  linenums [MAX_UNITS];
  long offsets [MAX_UNITS];
  long bytes;
  char *directory = (char *) 0;
  int  directory_len = 0;
  char *source_name;
  char *script_name;
  FILE *script;
  int c;
  extern char *optarg;
#ifdef VMS
#define optind decc$gl_optind
#endif
  extern int optind;
  /* Scan the arguments list. The arguments for gnatchop would be
   * gnatchop [-ksw] filename [directory]
   */
  while ((c = getopt(argc, argv, "krsw")) != -1)
     switch (c) {
        case 'k':
            /* passed from k8 option to gnatchop, ignore it */
            break;
        case 's':
            script_option++;
            break;
        case 'w':
            overwrite_option++;
            break;
        case 'r':
            source_ref_option++;
            break;
        case '?':
            fprintf
              (stderr, "usage: gnatchop [-krsw] filename [directory]\n");
            exit (1);
     }
  source_name = argv [optind++];
  /* The optional last argument would be a directory name.  If it does not
   * correspond to an existing directory, issue message and abort.
   */
  if (optind < argc) {
    directory_len = strlen (argv[optind]) + 1;
    directory = malloc (directory_len);
    strcpy (directory, argv [optind]);
    directory [directory_len] = '\0';
    if (!is_directory_file (directory)) {
      fprintf(stderr, "%s is not a valid directory\n", directory);
      exit (1);
    }
  }
  /* It is assumed that gcc is called by "gcc -gnatu -gnats -c filename" on
   * a source file and the subsequent output is redirected as the standard
   * input to this program.  The strings are of the following forms:
   * "Config pragmas line 1, file offset 0, file name gnat.adc"
   * "Unit XXXXX (spec) line ddd, file offset ddd. file name XXXX.ads" or
   * "Unit XXXXX (body) line ddd, file offset ddd. file name XXXX.adb"
   * ...
   */
  fprintf(stdout, "splitting %s into: \n", source_name);
  {
    /* Declare a buffer big enough to hold the offset information and then
     * read it all in with a single read operation.
     */
    int buffer_size = file_length (0);
    char buffer [buffer_size+1];
    read (0, buffer, buffer_size);
    buffer [buffer_size] = 0;
    ptr = buffer;
    for (unit=1;;unit++) {
      /* Scan for the string " line", skip over it, and read the line number.
       * if the string does not appear, it indicates that the entire string
       * has been fully processed.
       */
       ptr = strstr (ptr, " line");
       if (ptr == (char *) 0) break;
       ptr = ptr + 6;
       sscanf(ptr, "%d", &n);
       linenums [unit] = n;
      /* Scan for the word "offset", skip over it and read the actual offset
       * must be found, since we just found a ) line before it
       */
      ptr = strstr (ptr, "offset");
      ptr = ptr + 6;
      sscanf(ptr, "%d", &n);
      offsets [unit] = n;
      /* Scan for the words "file name", skip over them and read in the actual
       * file name to be used when writing out this compilation unit.  If a
       * directory name was given as an argument, prepend it to the file name.
       */
      ptr = strstr (ptr, "file name");
      ptr = ptr + 9;
      sscanf(ptr, "%s", &name);
      names [unit] = malloc (strlen (name) + directory_len + 1);
      names [unit] [0] = '\0';
      if (directory) {
        strcpy (names [unit], directory);
        i = strlen (names [unit]);
        if (names [unit] [i-1] != DIR_SEPARATOR)
          names [unit] [i++] = DIR_SEPARATOR;
        names [unit] [i] = '\0';
      }
      strcat (names [unit], name);
      fprintf(stdout, "   %s \n", names [unit]);
      ptr = strstr (ptr, "\n");
    }
  }
  fprintf(stdout, "\n");
  fflush(stdout);
  /* If the overwrite option is not specified check to see if any file that
   * would be written is the name of an existing file and if so print it and
   * later signal an abort after checking all files.
   */
  if (!overwrite_option) {
    for (i = 1; i < unit; i++)
      if (is_regular_file (names [i])) {
        fprintf(stderr, "%s already exists, use -w to overwrite\n", names [i]);
        errors++;
      }
      if (errors) {
        fprintf(stderr, "no files have been written\n");
        exit (1);
      }
  }
  fd1 = open_read (source_name);
  bytes = file_length (fd1);
  offsets [unit] = bytes;
  {
    char buf2 [bytes];  /* large enough to read in entire source file. */
    int  fd2;
    int bytes_now;
    int bytes_read;
    int bytes_left;
    int has_no_corresponding_body (char *filename)
    {
       int i;
       char *fname = strcpy (malloc (strlen(filename)+1), filename);
       fname [strlen (fname) - 1] = 'b';
       for (i = 1; i < unit; i++) {
         if (!strcmp (fname, names [i]))
           return 0;
       }
       return 1;
    }
  /*
   * Read the source file in groups of bytes given by the offsets array
   * and write each group as a separate file using the string given by the
   * names array.
   */
    for (i = 1; i < unit; i++) {
      bytes = offsets [i+1] - offsets [i];
      bytes_read = 0;
      bytes_left = bytes;
      do {
        bytes_now = read (fd1, &buf2 [bytes_read], bytes_left);
        bytes_read += bytes_now;
        bytes_left -= bytes_now;
      } while ((bytes_read < bytes) && (bytes_now > 0));
      if (bytes_read < bytes)
        buf2 [bytes_read] = 0;
      fd2 = open_create (names [i]);
      if (fd2 == -1) {
         fprintf (stderr, "unable to open output file '%s'\n",
                  names [i]);
         errors++;
      } else {
         if (source_ref_option) {
            int j;
            char c;
            write (fd2, "pragma Source_Reference (", 25);
            j = 100000;
            while (j > 0) {
               c = (linenums[i]/j) % 10 + '0';
               write (fd2, &c, 1);
               j = j / 10;
            }
            write (fd2, ", \"", 3);
            write (fd2, source_name, strlen (source_name));
            write (fd2, "\");\n", 4);
         }
         write (fd2, buf2, bytes_read);
         close (fd2);
      }
    }
    if (script_option) {
       script_name = malloc (strlen (source_name) + 5);
       if (script_name == NULL) {
          fprintf (stderr, "Out of memory while attempting to create "
                  "the compilation script!\n");
          exit (1);
       }
       strcpy (script_name, source_name);
       /* Look for the last period in script_name  */
       ptr = strrchr (script_name, DIR_SEPARATOR);
       if (ptr == NULL)
          ptr = script_name;
       ptr = strrchr (ptr, '.');
       if (ptr == NULL) {
          ptr = script_name + strlen (script_name);
          *ptr = '.';
       }
#if defined (__EMX__)
       strcpy (++ptr, "cmd");
#elif defined (MSDOS)
       strcpy (++ptr, "bat");
#elif defined (VMS)
       strcpy (++ptr, "com");
#else
       strcpy (++ptr, "sh");
#endif
       ptr--;
       while (ptr > script_name && *(ptr - 1) != DIR_SEPARATOR) ptr--;
       script_name = ptr;
       script = fopen (script_name, "w");
#if !(defined (__EMX__) || defined (MSDOS) || defined (VMS))
       chmod (script_name, S_IRWXU);
#endif
       for (i = 1; i < unit; i++)
          if (strstr (names [i], ".adb")
                || has_no_corresponding_body (names [i]))
             fprintf (script,
#if defined (__EMX__) || defined (MSDOS)
               "gcc -c %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9 %s\n",
#elif defined (VMS)
               "$gnat compile 'p1' 'p2' 'p3' 'p4' 'p5' 'p6' 'p7' 'p8' %s\n",
#else
               "gnatgcc -c $* %s\n", /* eichin */
#endif
               names [i]);
       fprintf (stdout, "script %s written\n", script_name);
    }
  }
  exit (errors);
}
 
     |