int read_calibration_from_file(const char *calibration_file,
                               struct calibration_data *accel,
                               struct calibration_data *magn,
                               struct calibration_data *gyro,
                               double *magnetic_declination_mrad)
{
#define STR_BUFFER_SIZE 128
    int ret = 0;
    FILE *stream;
    int len;
    char buffer[STR_BUFFER_SIZE];

    stream = fopen(calibration_file, "r");
    if (stream == NULL)
    {
        ret = -errno;
        fprintf(stderr, "Error: cannot open %s\n", calibration_file);
        return ret;
    }

    while (fgets(buffer, sizeof(buffer), stream) != NULL)
    {
        len = strlen(buffer);
        // strip trailing '\n' if it exists
        if (buffer[len-1] == '\n')
        {
            buffer[len-1] = 0;
            len--;
        }
        else
        {
            // line is longer than our buffer, skip to the next line
            continue;
        }

        if (buffer[0] != '#')
        {
            // find the '=' sign
            char *equal = strstr(buffer, "=");
            if (equal)
            {
                char *key = buffer;
                char *value = equal+1;
                *equal = 0;

                consume_white_space(&key);
                process_key_value(key, value, accel, magn, gyro, magnetic_declination_mrad);
            }
        }
    }

    fclose(stream);
    return ret;
}
Beispiel #2
0
int fs_config_read_file(const char *path, int force) {
    if (!g_initialized) {
        initialize();
    }
    fs_log("\n");
    fs_log(LOG_LINE);
    fs_log("config (%s)\n", path);
    fs_log(LOG_LINE);
    fs_log("\n");

    // FIXME: support checking a config key "end_config", which causes
    // later calls to fs_config_read_config_file to be ignored
    if (!force && fs_config_get_boolean("end_config") == 1) {
        fs_log("end_config is set, ignoring this config file\n");
        return 1;
    }

    if (!fs_path_is_file(path)) {
        fs_log("config file %s does not exist\n", path);
        return 0;
    }

    fs_ini_file *ini_file = fs_ini_file_open(path);
    if (ini_file == NULL) {
        fs_log("error loading config file\n");
        return 0;
    }

    char **groups = fs_ini_file_get_groups(ini_file, NULL);
    for (char **group = groups; *group; group++) {
        const char *prefix = "";
        if (strcmp(*group, "theme") == 0) {
            prefix = "theme_";
        }
        char **keys = fs_ini_file_get_keys(ini_file, *group, NULL);
        for (char **key = keys; *key; key++) {
            char *value = fs_ini_file_get_value(ini_file, *group, *key);
            if (value) {
                char *key2 = g_strconcat(prefix, *key, NULL);
                process_key_value(key2, value, 0);
                g_free(key2);
            }
        }
        g_strfreev(keys);
    }
    g_strfreev(groups);
    fs_ini_file_destroy(ini_file);

    return 1;
}
Beispiel #3
0
/**
 * Helper function.  Reads up to BUFFER_SIZE from a file descriptor into a
 * buffer and calls process_key_value() when for each and every key-value
 * pair that is read from the file descriptor.
 *
 * Each key-value must be in a "Key: Value" format, identical to MP1, and
 * each pair must be terminated by a newline ('\n').
 *sorter
 * Each unique file descriptor must have a unisorterque buffer and the buffer
 * must be of size (BUFFER_SIZE + 1).  Therefore, if you have two
 * unique file descriptors, you must have two buffers that each have
 * been malloc()'d to size (BUFFER_SIZE + 1).
 *
 * Note that read_from_fd() makes a read() call and will block if the
 * fd does not have data ready to be read.  This function is complete
 * and does not need to be modified as part of this MP.
 *
 * @param fd
 *    File descriptor to read from.sorter
 * @param buffer
 *    A unique buffer associated with the fd.  This buffer may have
 *    a partial key-value pair between calls to read_from_fd() and
 *    must not be modified outside the context of read_from_fd().
 * @param mr
 *    Pass-through mapreduce_t structure (to process_key_value()).
 *
 * @retval 1
 *    Data was available and was read successfully.
 * @retval 0
 *    The file descriptor fd has been closed, no more data to read.
 * @retval -1
 *    The call to read() produced an error.
 */
static int read_from_fd(int fd, char *buffer, mapreduce_t *mr)
{
	/* Find the end of the string. */
	int offset = strlen(buffer);

	/* Read bytes from the underlying stream. */
	int bytes_read = read(fd, buffer + offset, BUFFER_SIZE - offset);
	if (bytes_read == 0)
		return 0;
	else if(bytes_read < 0)
	{
		fprintf(stderr, "error in read.\n");
		return -1;
	}

	buffer[offset + bytes_read] = '\0';

	/* Loop through each "key: value\n" line from the fd. */
	char *line;


	while ((line = strstr(buffer, "\n")) != NULL)
	{
		*line = '\0';

		/* Find the key/value split. */
		char *split = strstr(buffer, ": ");
		if (split == NULL)
			continue;

		/* Allocate and assign memory */
		char *key = malloc((split - buffer + 1) * sizeof(char));
		char *value = malloc((strlen(split) - 2 + 1) * sizeof(char));

		strncpy(key, buffer, split - buffer);
		key[split - buffer] = '\0';

		strcpy(value, split + 2);

		/* Process the key/value. */
		process_key_value(key, value, mr);

		/* Shift the contents of the buffer to remove the space used by the processed line. */
		memmove(buffer, line + 1, BUFFER_SIZE - ((line + 1) - buffer));
		buffer[BUFFER_SIZE - ((line + 1) - buffer)] = '\0';
	}

	return 1;
}
Beispiel #4
0
void fs_config_parse_options(int argc, char **argv) {
    if (!g_initialized) {
        initialize();
    }
    int first = 1;
    for (int i = 0; i < argc; i++) {
        char *arg = argv[i];
        if (!g_str_has_prefix(arg, "--")) {
            continue;
        }
        char *key = arg + 2;
        char *value = strchr(arg, '=');
        char *k, *v;
        if (value) {
            k = g_strndup(key, value - key);
            v = g_strdup(value + 1);
        }
        else {
            if (g_str_has_prefix(key, "no-")) {
                k = g_strdup(key + 3);
                v = g_strdup("0");
            }
            else {
                k = g_strdup(key);
                v = g_strdup("1");
            }
        }

        //if (value) {
        if (first) {
            fs_log("\n");
            fs_log(LOG_LINE);
            fs_log("config (command line arguments)\n");
            fs_log(LOG_LINE);
            fs_log("\n");
            first = 0;
        }
        process_key_value(k, v, 0);
        g_free(k);
        // v is owned by process_key_file, do not free here
    }
}
Beispiel #5
0
void fs_config_set_string(const char *key, const char *value) {
    process_key_value(key, g_strdup(value), 1);
}
Beispiel #6
0
static int parse_config_file(shadowvpn_args_t *args, const char *filename) {
  char buf[512];
  char *line;
  FILE *fp;
  size_t len = sizeof(buf);
  int lineno = 0;

  fp = fopen(filename, "rb");
  if (fp == NULL) {
    err("fopen");
    errf("Can't open config file: %s", filename);
    return -1;
  }
  while ((line = fgets(buf, len, fp))) {
    char *sp_pos;
    lineno++;
    sp_pos = strchr(line, '\r');
    if (sp_pos) *sp_pos = '\n';
    sp_pos = strchr(line, '\n');
    if (sp_pos) {
      *sp_pos = 0;
    } else {
      errf("line %d too long in %s", lineno, filename);
      return -1;
    }
    if (*line == 0 || *line == '#')
      continue;
    sp_pos = strchr(line, '=');
    if (!sp_pos) {
      errf("%s:%d: \"=\" is not found in this line: %s", filename, lineno,
           line);
      return -1;
    }
    *sp_pos = 0;
    sp_pos++;
    // line points to key and sp_pos points to value
    if (0 != process_key_value(args, line, sp_pos))
      return 1;
  }
  // check if every required arg is set
  if (!args->mode) {
    errf("mode not set in config file");
    return -1;
  }
  if (!args->server) {
    errf("server not set in config file");
    return -1;
  }
  if (!args->port) {
    errf("port not set in config file");
    return -1;
  }
  if (!args->password) {
    errf("password not set in config file");
    return -1;
  }
#ifdef TARGET_WIN32
  if (!args->tun_ip) {
    errf("tunip not set in config file");
    return -1;
  }
#endif
  return 0;
}
Beispiel #7
0
int main(void) 
{
	int i;
	char inbuf[80];
	char *key, *value;
	int status;
	employee temporary_record;
	int week;
	int blank_line = 1;
	int query = 0;
	// Store of all employee data
	employee employee_list[NUMBER_OF_EMPLOYEES];

	for( i=0; i<NUMBER_OF_EMPLOYEES; i++)
	{
		initialize_employee(&employee_list[i]);
	}

	initialize_employee(&temporary_record);
	week = 0;

	// Process input and respond to queries until a blank line is received
	while(1)
	{
		// Read input from network in the form of key-value pairs
		status = get_key_value((char *)&inbuf, sizeof(inbuf), &key, &value);
		switch (status)
		{
			case READ_ERROR:
				print("ERROR: on receive\n");
				_terminate(1);
				break;
			case NEWLINE_RECEIVED:
			// Process the input line by merging the temporary record with the global record
				if (blank_line == 1)
				{
					print("Exiting\n");
					_terminate(0);
				}
				if (query > 0)
				{
					process_query(query, (employee *)&employee_list, &temporary_record, week);
				}
				else if ((temporary_record.id >= 0) && (temporary_record.id < NUMBER_OF_EMPLOYEES))
				{
					merge_employee_records(&employee_list[temporary_record.id], &temporary_record);
				}
				// Get ready to process a new line
				initialize_employee(&temporary_record);
				week = 0;
				blank_line = 1;
				query = 0;
				break;			
			case KEY_VALUE_RECEIVED:
				// If this is a query line remember it for processing later
				if ((blank_line == 1) && (equals(key, "query")))
				{
					if (equals(value, "all"))
						query = QUERY_ALL;
					else if (equals(value, "one"))
						query = QUERY_ONE;
					else if (equals(value, "week"))
						query = QUERY_WEEK;
					else if (equals(value, "week_all"))
						query = QUERY_WEEK_ALL;
					else
						query = 0;
				}
				blank_line = 0;
				// Add the key_value information to a temporary record
				process_key_value(&temporary_record, key, value, &week);
				break;
			case OTHER_INPUT_RECEIVED:
			default:
				print("ERROR: invalid input\n");
				_terminate(1);
				break;
		}
	}
	
}
int
main(int argc, char **argv) {
	int rc = 0;
	bootenv_t env = NULL;
	uint32_t boot_volno;
	myargs args = {
		.action = ACT_NORMAL,
		.file_in  = NULL,
		.file_out = NULL,
		.side = -1,
		.x86 = 0,
		.both = 0,
		.env_in = NULL,

		.arg1 = NULL,
		.options = NULL,
	};

	rc = bootenv_create(&env);
	if (rc != 0) {
		err_msg("Cannot create bootenv handle. rc: %d", rc);
		goto err;
	}

	rc = bootenv_create(&(args.env_in));
	if (rc != 0) {
		err_msg("Cannot create bootenv handle. rc: %d", rc);
		goto err;
	}

	parse_opt(argc, argv, &args);
	if (args.action == ACT_ARGP_ERR) {
		rc = -1;
		goto err;
	}
	if (args.action == ACT_ARGP_ABORT) {
		rc = 0;
		goto out;
	}

	if ((args.side == 0) || (args.side == -1))
		boot_volno = EXAMPLE_BOOTENV_VOL_ID_1;
	else
		boot_volno = EXAMPLE_BOOTENV_VOL_ID_2;

	if( args.x86 )
		rc = read_bootenv(args.file_in, env);
	else
		rc = ubi_read_bootenv(EXAMPLE_UBI_DEVICE, boot_volno, env);
	if (rc != 0) {
		goto err;
	}

	if (args.action == ACT_LIST) {
		rc = list_bootenv(env);
		if (rc != 0) {
			goto err;
		}
		goto out;
	}

	rc = process_key_value(args.env_in, env);
	if (rc != 0) {
		goto err;
	}

	if( args.x86 )
		rc = write_bootenv(args.file_in, env);
	else
		rc = ubi_write_bootenv(EXAMPLE_UBI_DEVICE, boot_volno, env);
	if (rc != 0)
		goto err;

	if( args.both )		/* No side specified, update both */
		rc = do_mirror(boot_volno);

 out:
 err:
	bootenv_destroy(&env);
	bootenv_destroy(&(args.env_in));
	return rc;
}