Bontiv-Sourceer source code viewer
Root | Help
./web-crawler/src/tplcp/main.c
#include "main.h"

void addfile(ARGS arg, char* file)
{
  FILES new;
  char* lstr;

  lstr = strrchr(file, '.');
  new = malloc(sizeof(struct file_list_s));
  new->file = fopen(file, "r");
  if (new->file != 0)
    {
      new->next = arg->file;
      if (lstr != 0)
	lstr[0] = 0;
      new->name = file;
      arg->file = new;
    }
  else
    {
      free(new);
    }
}

ARGS parsearg(int argc, char* argv[])
{
  ARGS args;
  int pos;

  args = malloc(sizeof(struct args_s));
  args->flags = 0;
  args->file = 0;
  args->output = stdout;
  args->data = new_tpl();
  args->cflags = malloc(1);
  args->cflags[0] = 0;
  args->file = 0;
  pos = 1;

  while (pos < argc)
    {
      /* Set input file */
      if (0 == strcmp("-c", argv[pos]))
	{
	  args->flags = args->flags | COMPILE_FLAG;
	}
      else if (0 == strcmp("-i", argv[pos]))
	{
	  args->flags = args->flags | INTERPRET_FLAG;
	}
      else if (0 == strcmp("-debug", argv[pos]))
	{
	  args->flags = args->flags | DEBUG_FLAG;
	}
      else if (0 == strcmp("-o", argv[pos]) && pos + 1 < argc)
	{
	  pos++;
	  args->output = fopen(argv[pos], "w");
	}
      else if (0 == strcmp("-cflags", argv[pos]) && pos + 1 < argc)
	{
	  pos++;
	  args->cflags = realloc(args->cflags, strlen(argv[pos]) + 1);
	  strcpy(args->cflags, argv[pos]);
	}
      else if (0 == strcmp("-v", argv[pos]) && pos + 2 < argc)
	{
	  pos++;
	  tpl_updt(args->data, argv[pos], argv[pos + 1]);
	  pos++;
	}
      else
	{
	  addfile(args, argv[pos]);
	  args->flags = args->flags | FILE_FLAG;
	}
      pos++;
    }

  return args;
}

char* stream2buffer(FILE* stream, size_t* len)
{
  char* buff;
  size_t max;

  buff = malloc(100);
  *len = 0;
  max = 100;

  while (!feof(stream))
    {
      if (*len == max - 1)
	{
	  max += 100;
	  buff = realloc(buff, max);
	}
      buff[*len] = fgetc(stream);
      *len = *len + 1;
    }
  buff[*len] = 0;
  return buff;
}

void exec_page(ARGS args, FILE* input, char* page)
{
  char* str;
  size_t len;

  str = stream2buffer(input, &len);
  auto_t aut = start_auto(str, len);

  if (args->flags & DEBUG_FLAG)
    {
      fprintf(stderr, ">>>>>%s<<<<<\n", page);
      debug_auto(aut);
    }

  /* Interpretor mode */
  if (args->flags & INTERPRET_FLAG)
    {
      if (args->flags & DEBUG_FLAG)
	{
	  tpl_debug(args->data);
	}
      printf("<!-- PAGE : %s -->\n", page);
      fputs(flush(args->data, aut), args->output);
    }

  else
    {
      whead(args->output, page);
      wpage(args->output, aut);
      wend(args->output);
    }
}

void main(int argc, char* argv[])
{
  ARGS args;
  FILES flist;
  FILE* out;
  char buff[1];
  char* sys;

  args = parsearg(argc, argv);

  /* Start the file if we want to compile */
  if (args->flags & COMPILE_FLAG)
    {
      out = args->output;
      args->output = fopen("/tmp/code.c", "w");
    }

  if (~args->flags & INTERPRET_FLAG)
      wstart(args->output);

  /* Files in argument */
  if (args->flags & FILE_FLAG)
    {
      flist = args->file;
      while (flist != 0)
	{
	  exec_page(args, flist->file, flist->name);
	  flist = flist->next;
	}
    }
  else
    {
      exec_page(args, stdin, "index");
    }

  /* Close files */
  if ((args->flags & OUTPUT_FLAG) || (args->flags & COMPILE_FLAG))
    fclose(args->output);
  flist = args->file;

  /* End compilation */
  if (args->flags & COMPILE_FLAG)
    {
      sys = malloc(strlen(GCC) + strlen(args->cflags) + 1);
      sys = strcpy(sys, GCC);
      sys = strcat(sys, args->cflags);
      if (args->flags & DEBUG_FLAG)
	fprintf(stderr, "%s\n", sys);

      if (args->cflags != 0)
	{
	  //sys = realloc(sys, 500);
	  //strcat(sys, args->cflags);
	}
      system(sys);
      args->output = fopen("/tmp/code.o", "r");
      while (!feof(args->output))
	{
	  fread(buff, sizeof(char), 1, args->output);
	  fwrite(buff, sizeof(char), 1, out);
	}
      if (args->flags & OUTPUT_FLAG)
	fclose(out);
      fclose(args->output);
    }
}
Presented with Bontiv-Sourceer