static int loadfile(struct sr_input *in, const char *filename)
{
int res;
struct context *ctx;
struct sr_datafeed_packet packet;
struct sr_datafeed_meta meta;
struct sr_config *cfg;
GIOStatus status;
gboolean read_new_line;
gsize term_pos;
char **columns;
gsize num_columns;
int max_columns;
(void)filename;
ctx = in->internal;
/* Send header packet to the session bus. */
std_session_send_df_header(in->sdi, LOG_PREFIX);
if (ctx->samplerate) {
packet.type = SR_DF_META;
packet.payload = &meta;
cfg = sr_config_new(SR_CONF_SAMPLERATE,
g_variant_new_uint64(ctx->samplerate));
meta.config = g_slist_append(NULL, cfg);
sr_session_send(in->sdi, &packet);
sr_config_free(cfg);
}
read_new_line = FALSE;
/* Limit the number of columns to parse. */
if (ctx->multi_column_mode)
max_columns = ctx->num_probes;
else
max_columns = 1;
while (TRUE) {
/*
* Skip reading a new line for the first time if the last read
* line was not a header because the sample data is not parsed
* yet.
*/
if (read_new_line || ctx->header) {
ctx->line_number++;
status = g_io_channel_read_line_string(ctx->channel,
ctx->buffer, &term_pos, NULL);
if (status == G_IO_STATUS_EOF)
break;
if (status != G_IO_STATUS_NORMAL) {
sr_err("Error while reading line %zu.",
ctx->line_number);
free_context(ctx);
return SR_ERR;
}
/* Remove line termination character(s). */
g_string_truncate(ctx->buffer, term_pos);
}
read_new_line = TRUE;
if (!ctx->buffer->len) {
sr_spew("Blank line %zu skipped.", ctx->line_number);
continue;
}
/* Remove trailing comment. */
strip_comment(ctx->buffer, ctx->comment);
if (!ctx->buffer->len) {
sr_spew("Comment-only line %zu skipped.",
ctx->line_number);
continue;
}
if (!(columns = parse_line(ctx, max_columns))) {
sr_err("Error while parsing line %zu.",
ctx->line_number);
free_context(ctx);
return SR_ERR;
}
num_columns = g_strv_length(columns);
/* Ensure that the first column is not out of bounds. */
if (!num_columns) {
sr_err("Column %zu in line %zu is out of bounds.",
ctx->first_column, ctx->line_number);
g_strfreev(columns);
free_context(ctx);
return SR_ERR;
}
/*
* Ensure that the number of probes does not exceed the number
//.........这里部分代码省略.........
开发者ID:russdill,项目名称:libsigrok,代码行数:101,代码来源:csv.c
示例11: parse_contents
static void
parse_contents(void)
{
int cnt;
pkgentry_t *ent, *e2;
avl_index_t where;
int num = 0;
struct stat stb;
ptrdiff_t off;
char *p, *q, *map;
pkgentry_t *lastentry = NULL;
int d;
int cntserrs = 0;
cnt = open(CONTENTS, O_RDONLY);
cind = 0;
if (cnt == -1) {
if (errno == ENOENT)
return;
exit(99);
}
if (fstat(cnt, &stb) != 0) {
(void) close(cnt);
exit(99);
}
if (stb.st_size == 0) {
(void) close(cnt);
return;
}
map = mmap(0, stb.st_size, PROT_READ, MAP_PRIVATE, cnt, 0);
(void) close(cnt);
if (map == (char *)-1)
return;
(void) madvise(map, stb.st_size, MADV_WILLNEED);
for (off = 0; off < stb.st_size; off += q - p) {
p = map + off;
q = memchr(p, '\n', stb.st_size - off);
if (q == NULL)
break;
q++;
num++;
if (p[0] == '#' || p[0] == '\n') {
handle_comments(p, q - p);
continue;
}
ent = parse_line(p, q - p - 1, B_TRUE);
if (ent == NULL) {
cntserrs++;
continue;
}
/*
* We save time by assuming the database is sorted; by
* using avl_insert_here(), building the tree is nearly free.
* lastentry always contains the last entry in the AVL tree.
*/
if (lastentry == NULL) {
avl_add(list, ent);
lastentry = ent;
} else if ((d = avlcmp(ent, lastentry)) == 1) {
avl_insert_here(list, ent, lastentry, AVL_AFTER);
lastentry = ent;
} else if (d == 0 ||
(e2 = avl_find(list, ent, &where)) != NULL) {
/*
* This can only happen if the contents file is bad;
* this can, e.g., happen with the old SQL contents DB,
* it didn't sort properly. Assume the first one
* is the correct one, but who knows?
*/
if (d == 0)
e2 = lastentry;
if (strcmp(ent->line, e2->line) != 0) {
progerr(gettext("two entries for %.*s"),
ent->pathlen, ent->line);
cntserrs++;
}
freeentry(ent);
} else {
/* Out of order: not an error for us, really. */
progerr(gettext("bad read of contents file"));
logerr(gettext("pathname: Unknown"));
logerr(gettext(
"problem: unable to read pathname field"));
if (one_shot)
exit(2);
avl_insert(list, ent, where);
}
}
cind = 0;
//.........这里部分代码省略.........
//.........这里部分代码省略.........
if (status != G_IO_STATUS_NORMAL) {
sr_err("Error while reading line %zu.",
ctx->line_number);
free_context(ctx);
return SR_ERR;
}
if (ctx->start_line > ctx->line_number) {
sr_spew("Line %zu skipped.", ctx->line_number);
continue;
}
/* Remove line termination character(s). */
g_string_truncate(ctx->buffer, term_pos);
if (!ctx->buffer->len) {
sr_spew("Blank line %zu skipped.", ctx->line_number);
continue;
}
/* Remove trailing comment. */
strip_comment(ctx->buffer, ctx->comment);
if (ctx->buffer->len)
break;
sr_spew("Comment-only line %zu skipped.", ctx->line_number);
}
/*
* In order to determine the number of columns parse the current line
* without limiting the number of columns.
*/
if (!(columns = parse_line(ctx, -1))) {
sr_err("Error while parsing line %zu.", ctx->line_number);
free_context(ctx);
return SR_ERR;
}
num_columns = g_strv_length(columns);
/* Ensure that the first column is not out of bounds. */
if (!num_columns) {
sr_err("Column %zu in line %zu is out of bounds.",
ctx->first_column, ctx->line_number);
g_strfreev(columns);
free_context(ctx);
return SR_ERR;
}
if (ctx->multi_column_mode) {
/*
* Detect the number of probes in multi column mode
* automatically if not specified.
*/
if (!ctx->num_probes) {
ctx->num_probes = num_columns;
sr_info("Number of auto-detected probes: %zu.",
ctx->num_probes);
}
/*
* Ensure that the number of probes does not exceed the number
* of columns in multi column mode.
*/
if (num_columns < ctx->num_probes) {
开发者ID:russdill,项目名称:libsigrok,代码行数:67,代码来源:csv.c
示例13: builtin_run_command
/****************************************************************************
* returns:
* 1 - command executed, repeatable
* 0 - command executed but not repeatable, interrupted commands are
* always considered not repeatable
* -1 - not executed (unrecognized, bootd recursion or too many args)
* (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
* considered unrecognized)
*
* WARNING:
*
* We must create a temporary copy of the command since the command we get
* may be the result from getenv(), which returns a pointer directly to
* the environment data, which may change magicly when the command we run
* creates or modifies environment variables (like "bootp" does).
*/
static int builtin_run_command(const char *cmd, int flag)
{
char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
char *token; /* start of token in cmdbuf */
char *sep; /* end of token (separator) in cmdbuf */
char finaltoken[CONFIG_SYS_CBSIZE];
char *str = cmdbuf;
char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
int argc, inquotes;
int repeatable = 1;
int rc = 0;
debug_parser("[RUN_COMMAND] cmd[%p]=\"", cmd);
if (DEBUG_PARSER) {
/* use printf - string may be loooong */
printf(cmd ? cmd : "NULL");
printf("\"\n");
}
//clear_ctrlc(); /* forget any previous Control C */
if (!cmd || !*cmd) {
return -1; /* empty command */
}
if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
printf ("## Command too long!\n");
return -1;
}
strcpy (cmdbuf, cmd);
/* Process separators and check for invalid
* repeatable commands
*/
debug_parser("[PROCESS_SEPARATORS] %s\n", cmd);
while (*str) {
/*
* Find separator, or string end
* Allow simple escape of ';' by writing "\;"
*/
for (inquotes = 0, sep = str; *sep; sep++) {
if ((*sep=='\'') &&
(*(sep-1) != '\\'))
inquotes=!inquotes;
if (!inquotes &&
(*sep == ';') && /* separator */
( sep != str) && /* past string start */
(*(sep-1) != '\\')) /* and NOT escaped */
break;
}
/*
* Limit the token to data between separators
*/
token = str;
if (*sep) {
str = sep + 1; /* start of command for next pass */
*sep = '\0';
}
else
str = sep; /* no more commands for next pass */
debug_parser("token: \"%s\"\n", token);
/* find macros in this token and replace them */
process_macros (token, finaltoken);
/* Extract arguments */
if ((argc = parse_line (finaltoken, argv)) == 0) {
rc = -1; /* no command at all */
continue;
}
if (cmd_process(flag, argc, argv, &repeatable, NULL))
rc = -1;
/* Did the user stop this? */
//if (had_ctrlc ())
// return -1; /* if stopped then not repeatable */
}
return rc ? rc : repeatable;
//.........这里部分代码省略.........
/*
* Parse a single file
*/
static int parse_file(char *filename)
{
int val;
int ret = OMPI_SUCCESS;
bool showed_no_section_warning = false;
bool showed_unexpected_tokens_warning = false;
parsed_section_values_t section;
reset_section(false, §ion);
/* Open the file */
ini_filename = filename;
btl_openib_ini_yyin = fopen(filename, "r");
if (NULL == btl_openib_ini_yyin) {
orte_show_help("help-mpi-btl-openib.txt", "ini file:file not found",
true, filename);
ret = OMPI_ERR_NOT_FOUND;
goto cleanup;
}
/* Do the parsing */
btl_openib_ini_parse_done = false;
btl_openib_ini_yynewlines = 1;
btl_openib_ini_init_buffer(btl_openib_ini_yyin);
while (!btl_openib_ini_parse_done) {
val = btl_openib_ini_yylex();
switch (val) {
case BTL_OPENIB_INI_PARSE_DONE:
/* This will also set btl_openib_ini_parse_done to true, so just
break here */
break;
case BTL_OPENIB_INI_PARSE_NEWLINE:
/* blank line! ignore it */
break;
case BTL_OPENIB_INI_PARSE_SECTION:
/* We're starting a new section; if we have previously
parsed a section, go see if we can use its values. */
save_section(§ion);
reset_section(true, §ion);
section.name = strdup(btl_openib_ini_yytext);
break;
case BTL_OPENIB_INI_PARSE_SINGLE_WORD:
if (NULL == section.name) {
/* Warn that there is no current section, and ignore
this parameter */
if (!showed_no_section_warning) {
show_help("ini file:not in a section");
showed_no_section_warning = true;
}
/* Parse it and then dump it */
parse_line(§ion);
reset_section(true, §ion);
} else {
parse_line(§ion);
}
break;
default:
/* anything else is an error */
if (!showed_unexpected_tokens_warning) {
show_help("ini file:unexpected token");
showed_unexpected_tokens_warning = true;
}
break;
}
}
save_section(§ion);
fclose(btl_openib_ini_yyin);
btl_openib_ini_yylex_destroy ();
cleanup:
reset_section(true, §ion);
if (NULL != key_buffer) {
free(key_buffer);
key_buffer = NULL;
key_buffer_len = 0;
}
return ret;
}
请发表评论