This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

reading command line arguments from a file


Here is the code that does what I was after when I first started this
thread. I need some help to get it integrated into gcc.


Mark,

Is there any chance this can go into the 3.0 branch?


Also, does anyone know how to get this code submitted for inclusion in
the binutils, i.e. linker?

To test the code compile parse.c and run it as follows

parse -I., -Wl, -@myCmdFile.txt

If there are better solutions I'll be happy to make some changes for
this to fit into the overall picture.

Thanks,
Robert



--
Robert Schweikert                      MAY THE SOURCE BE WITH YOU
rjschwei@mindspring.com                         LINUX


#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

int
parse_file (const char *cmdFile, char*** cmds, char** cmdsIndx, int size)
{
  FILE *cmd_fp;
  int bytes_read, i;
  struct stat s;
  char *newStr = 0;
  char *lines = 0;
  
  stat(cmdFile, &s);

  if ((cmd_fp = fopen(cmdFile, "r")) == NULL)
    {
      printf ("Cannot open command file %s", cmdFile);
      return 1;
    }

  lines = malloc(s.st_size);
  bytes_read = fread(lines, sizeof(char), s.st_size, cmd_fp);

  if (bytes_read != s.st_size || ferror(cmd_fp))
    {
      printf ("Error reading command file %s", cmdFile);
      return 1;
    }

   if (! ((lines+bytes_read-1) != '\n'))
    {
      printf ("Error reading command file %s, last character must be new-line",
              cmdFile);
      return 1;
    }

  i = 1;
  newStr = *cmdsIndx = lines;
  while (newStr - lines < s.st_size)
    {
      newStr = strchr(*cmdsIndx, '\n');
      *newStr = '\0';
      newStr++;
      cmdsIndx++;
      *cmdsIndx=newStr;
      i++;
      if (i == size)
        {
          *cmds = realloc(*cmds, size*sizeof(char *));
          i = 1;
        }
    }

  fclose(cmd_fp);
  return 0;
}


void
parse_args (int argc, char **argv)
{
  const int MAXCMDS = 1000;
  int i;
  char **cmds = 0;
  char **cmdsIndx = 0;

  cmdsIndx = cmds = malloc(MAXCMDS*sizeof(char *));

  for (i = 1; i < argc; i++)
    {
      if (! strncmp(argv[i], "-@", 2))
        {
          parse_file(&argv[i][2], &cmds, cmdsIndx, MAXCMDS);
          continue;
        }
      *cmdsIndx = argv[i];
      cmdsIndx++;
    }

  printf("Now\n");
  for (i = 0; i < 8; i++)
    {
      printf(cmds[i]);
      printf("\n");
    }
}

int main(int argc, char *argv[])
{
  int i;

  for (i = 1; i < argc; i++)
    {
      printf(argv[i]);
      printf("\n");
    }

  parse_args (argc, argv);

  return 0;
}
-u external_
-Wl
-fpic
-g
-I/home/rjschwei/tmp


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]