Esempio n. 1
0
board_config*
libsoc_board_init()
{
  int rc;
  FILE *fp;
  char line[256];
  board_config *bc;
  pin_mapping *cur = NULL;
  pin_mapping *ptr;
  char *tmp;
  const char *conf = _get_conf_file();

  bc = calloc(sizeof(board_config), 1);

  fp = fopen(conf, "r");
  if (fp)
    {
      while(fgets(line, sizeof(line), fp))
        {
          if (*line == '#' || *line == '\0' || *line == '\n') continue;
          ptr = calloc(sizeof(pin_mapping), 1);
          rc = sscanf(line, "%15[^=]=%d", ptr->pin, &ptr->gpio);
          if (rc != 2)
            {
              libsoc_warn("Invalid mapping line in %s:\n%s\n", conf, line);
              goto fail_close;
            }
          rtrim(ptr->pin);

          if (!cur)
            {
              bc->pin_mappings = cur = ptr;
            }
          else
            {
              cur->next = ptr;
              cur = cur->next;
            }
        }
    }
  else
    {
      libsoc_warn("Unable to read pin mapping file: %s\n", conf);
      goto fail;
    }
  return bc;

fail_close:
  fclose(fp);
fail:
  free(bc);
  return NULL;
}
Esempio n. 2
0
static keyval*
_create_keyval(const char *path, char *line)
{
  keyval *kv;
  char *key, *val, *save;
  key = strtok_r(line, "=", &save);
  val = strtok_r(NULL, "\n", &save);
  if (!val)
    {
      libsoc_warn("Invalid key = value in %s:\n%s\n", path, line);
      return NULL;
    }
  key = trim(key);
  val = trim(val);
  kv = calloc(1, sizeof(keyval));
  kv->key = strdup(key);
  kv->val = strdup(val);
  return kv;
}
Esempio n. 3
0
int
conffile_get_int(conffile *conf, const char *sectname, const char *key, int defval)
{
  long val = defval;
  const char *strval = conffile_get(conf, sectname, key, NULL);
  if (strval)
    {
      char *endptr;
      val = strtol(strval, &endptr, 10);
      if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
            || (errno != 0 && val == 0) || endptr == strval || *endptr != '\0'
            || val > INT_MAX)
        {
          libsoc_warn("Invalid number: %s\n", strval);
          val = defval;
        }
    }
  return val;
}
Esempio n. 4
0
conffile *
conffile_load(const char *path)
{
  int rc;
  FILE *fp;
  char line[256];
  conffile *conf = calloc(1, sizeof(conffile));
  section *stmp = NULL;
  keyval *ktmp = NULL;

  fp = fopen (path, "r");
  if (fp)
    {
      while(fgets(line, sizeof(line), fp))
        {
          if (*line == '#' || *line == '\0' || *line == '\n')
	    continue;
          if (line[0] == '[')
            {
              // new section
              stmp = calloc(1, sizeof(section));
              stmp->next = conf->sections;
              conf->sections = stmp;

              rc = strlen(line);
              if (line[rc-2] != ']' || (rc-2) > sizeof(stmp->name))
                {
                  libsoc_warn("Invalid section line in %s:\n%s\n", path, line);
                  goto cleanup;
                }
              line[rc-2] = '\0';
              strcpy(stmp->name, trim(&line[1]));
            }
          else
            {
              if (!conf->sections)
                {
                  libsoc_warn("Section must be declared in %s before line: %s\n",
                              path, line);
                }
              ktmp = _create_keyval(path, line);
              if (ktmp)
                {
                  ktmp->next = conf->sections->settings;
                  conf->sections->settings = ktmp;
                }
              else
                {
                  goto cleanup;
                }
            }
        }
      fclose(fp);
    }
  else
    {
        libsoc_warn("Unable to open board config: %s\n", path);
    }

  return conf;

cleanup:
  if (fp)
    fclose(fp);
  conffile_free(conf);
  return NULL;
}