Пример #1
0
/* loads a file and returns its length */
int load_file(char *file, char *at)
{
  unsigned int size;

  if (u_open(file, &size))
    return 0;

  size = u_read(at, size);
  u_close();

  return size;
}
Пример #2
0
int process_cfg(char *cfg)
{
  // buffer for config file reading
  static char s[BUFSIZE];
  static int  s_pos = 0;
  // second buffer
  static char str[BUFSIZE];
  static int  str_pos = 0;
  char *p, *line, *q;
  char f, g;
  unsigned int size, sz;
  unsigned int rd;
  int bytes_read = 0;
  int i;

  if (u_open(cfg, &size)) {
    printf("Cannot open config file!\r\n");
    return 0;
  }

  u_close();

  //grub_memset(s, 0, sizeof(s));

  sz = size;
  while (sz)
  {
    // read new buffer
    if (u_open(cfg, &size)) {
      printf("Cannot open config file!\r\n");
      return 0;
    }

    u_seek(bytes_read);

    rd = u_read(buf, sizeof(buf));
    if (!rd)
    {
      printf("process_cfg(): read returned zero bytes\r\n");
      return 0;
    }

    sz -= rd;
    bytes_read += rd;

    u_close();

    if (sz && !rd)
    {
      printf("Can't read from config file!\r\n");
      return 0;
    }

    f = 1;
    p = buf;
    while (*p && f)
    {
      // read new line from current buffer
      line = getline(&p, rd);
      i = grub_strlen(line);
      if (s_pos + i > BUFSIZE)
        panic("string s too long to fit in buffer!\r\n", cfg);
      grub_strcpy(s + s_pos, line);
      s_pos += i;
      f = (p - buf < rd);
      g = f || (!f && (buf[rd - 1] == '\n'));
      if (g)
      {
        // delete CR and LF symbols at the end
        line = strip(trim(s));
        // skip comments ";"
        i = grub_index(';', line);
        if (i) line[i - 1] = '\0';
        // skip comments "#"
        i = grub_index('#', line);
        if (i) line[i - 1] = '\0';
        // delete leading and trailing spaces
        line = strip(line);

        // line continuation symbol ('^' or '&') support
        i = grub_strlen(line);
        if (line[i - 1] == '^' || line[i - 1] == '&')
        {
          // the current line continued
          line[i - 1] = '\0';
          if (str_pos + i - 1 > BUFSIZE)
            panic("string str too long to fit in buffer!\r\n", cfg);
          grub_strcpy(str + str_pos, line);
          str_pos += i - 1;
          s[0] = '\0';
          s_pos = 0;

          continue;
        }
        else
        {
          // the line ends
          if (str_pos + i > BUFSIZE)
            panic("string str too long to fit in buffer!\r\n", cfg);
          grub_strcpy(str + str_pos, line);
          str_pos = 0;
          s[0] = '\0';
          s_pos = 0;
        }

        // expand variables
        //expand_vars(s, str);

        // process the line
        if (!process_cfg_line(str))
          return -1;
      }
    }
  }

  return 1;
}