From patchwork Thu Oct 4 01:47:17 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Troy Kisky X-Patchwork-Id: 189003 X-Patchwork-Delegate: sbabic@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id AD7DF2C0336 for ; Thu, 4 Oct 2012 11:57:14 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BAE6F281FA; Thu, 4 Oct 2012 03:57:12 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id jP6ERKps3eHl; Thu, 4 Oct 2012 03:57:12 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9236E28201; Thu, 4 Oct 2012 03:57:11 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 7AB0528201 for ; Thu, 4 Oct 2012 03:57:08 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PQ0RZ46xNxSN for ; Thu, 4 Oct 2012 03:57:08 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-pa0-f44.google.com (mail-pa0-f44.google.com [209.85.220.44]) by theia.denx.de (Postfix) with ESMTPS id 9A005281FA for ; Thu, 4 Oct 2012 03:57:05 +0200 (CEST) Received: by padfb11 with SMTP id fb11so7093781pad.3 for ; Wed, 03 Oct 2012 18:57:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :x-gm-message-state; bh=dnUErniY60WvhQ/IkUxZGGPxkvml7CSpZuDY2gnR5Vg=; b=OlMEMRPjYWKKiobm7eJ6wBA7oVHctkIc1h/IX2N5MTpc135w8UHlQezRkfUscdkwX2 6tV68z5WNYcaIMih6R3b8AXOQRsrALFZFkhfYxUpz0f763YzGjtKIbW66BEb6bzsmvYb O/cmt5v984TLxADK+4e8Xw1WoGqGozEmJ/ll7U5idgo+CzmDmcpP5N4Cb63HRrqSY1aw 8nc9vOGNwbuxpbqBy3BEIfIaw75k46+uJ1gSks7HWEpJPhs5T+QGHnHHxcb5MM/6Ru0O oQgR7izjoE+Uw98KU0zQ3OkutukFtWj7EfyXkdMsNEjik8KNLE2DZOFtdGacMR1vh904 Yv9g== Received: by 10.68.130.198 with SMTP id og6mr17399739pbb.163.1349315824312; Wed, 03 Oct 2012 18:57:04 -0700 (PDT) Received: from officeserver-2 ([70.96.116.236]) by mx.google.com with ESMTPS id te6sm3480328pbc.29.2012.10.03.18.57.02 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 03 Oct 2012 18:57:03 -0700 (PDT) Received: from tkisky by officeserver-2 with local (Exim 4.76) (envelope-from ) id 1TJaXd-0005Wp-7y; Wed, 03 Oct 2012 18:47:57 -0700 From: Troy Kisky To: sbabic@denx.de Date: Wed, 3 Oct 2012 18:47:17 -0700 Message-Id: <1349315254-21151-16-git-send-email-troy.kisky@boundarydevices.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1349315254-21151-1-git-send-email-troy.kisky@boundarydevices.com> References: <1348281558-19520-1-git-send-email-troy.kisky@boundarydevices.com> <1349315254-21151-1-git-send-email-troy.kisky@boundarydevices.com> X-Gm-Message-State: ALoCoQnxQemCOBylhJqLB4YDp3gPdzA1hAXtC8fRwTMgMrylF8VSlnvfpccr4HEoao+DFWkNyBc7 Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH V3 15/32] tools: add parse_helper file X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de This file can help you parse configuration files. Signed-off-by: Troy Kisky --- tools/Makefile | 2 + tools/parse_helper.c | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/parse_helper.h | 28 ++++++++ 3 files changed, 203 insertions(+) create mode 100644 tools/parse_helper.c create mode 100644 tools/parse_helper.h diff --git a/tools/Makefile b/tools/Makefile index c31437e..a540274 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -94,6 +94,7 @@ NOPED_OBJ_FILES-y += aisimage.o NOPED_OBJ_FILES-y += kwbimage.o NOPED_OBJ_FILES-y += pblimage.o NOPED_OBJ_FILES-y += imximage.o +NOPED_OBJ_FILES-y += parse_helper.o NOPED_OBJ_FILES-y += omapimage.o NOPED_OBJ_FILES-y += mkenvimage.o NOPED_OBJ_FILES-y += mkimage.o @@ -208,6 +209,7 @@ $(obj)mkimage$(SFX): $(obj)aisimage.o \ $(obj)fit_image.o \ $(obj)image.o \ $(obj)imximage.o \ + $(obj)parse_helper.o \ $(obj)kwbimage.o \ $(obj)pblimage.o \ $(obj)md5.o \ diff --git a/tools/parse_helper.c b/tools/parse_helper.c new file mode 100644 index 0000000..0a5c5f6 --- /dev/null +++ b/tools/parse_helper.c @@ -0,0 +1,173 @@ +/* + * (C) Copyright 20012 Boundary Devices Inc, troy.kisky@boundarydevices.com + * + * Licensed under the GPL-2 or later. + */ + +/* Required to obtain the getline prototype from stdio.h */ +#define _GNU_SOURCE + +#include "mkimage.h" +#include +#include "parse_helper.h" + +int ph_open(struct parse_helper *ph, char *filename) +{ + ph->line = NULL; + ph->len = 0; + ph->fd = fopen(filename, "r"); + ph->lineno = 0; + ph->cmd_started = 0; + ph->filename = filename; + ph->p = NULL; + return (!ph->fd) ? -1 : 0; +} + +void ph_close(struct parse_helper *ph) +{ + fclose(ph->fd); + ph->fd = NULL; +} + +int ph_skip_separators(struct parse_helper *ph) +{ + int line_no = ph->lineno; + char *p = ph->p; + + for (;;) { + char c; + if (!p) { + if (getline(&ph->line, &ph->len, ph->fd) <= 0) + return -1; + ph->lineno++; + p = ph->line; + if (ph->cmd_started) { + fprintf(stderr, "warning: continuing command on" + " next line, line %s[%d](%s)\n", + ph->filename, ph->lineno, p); + } + } + c = *p; + if ((c == ' ') || (c == '\t')) { + p++; + continue; + } + /* Drop all text starting with '#' as comments */ + if ((c == '#') || (c == '\r') || (c == '\n') + || !c) { + p = NULL; + continue; + } + if (c == ';') { + if (ph->cmd_started) { + fprintf(stderr, "Error: command not " + "finished:%s[%d](%s)\n", + ph->filename, ph->lineno, p); + exit(EXIT_FAILURE); + } + p++; + continue; + } + if (!ph->cmd_started && line_no == ph->lineno) { + fprintf(stderr, "Error: extra data at end " + "of line %s[%d](%s)\n", + ph->filename, ph->lineno, p); + exit(EXIT_FAILURE); + } + ph->p = p; + return 0; + } +} + +int ph_skip_comma(struct parse_helper *ph) +{ + char *p = ph->p; + + for (;;) { + char c = *p++; + if ((c == '#') || (c == '\r') || (c == '\n') || !c) + return 0; + if (c == ',') { + ph->p = p; + ph_skip_separators(ph); + return 1; + } + if ((c != ' ') && (c == '\t')) + return 0; + } +} + +int ph_get_value(struct parse_helper *ph, uint32_t *pval) +{ + char *endptr; + uint32_t value; + + if (ph_skip_separators(ph)) + return -1; + errno = 0; + value = strtoul(ph->p, &endptr, 16); + if (errno || (ph->p == endptr)) + return -1; + *pval = value; + ph->p = endptr; + return 0; +} + +/* + * Comma separator optional + * Input: + * ph - input source + * data - array to fill in + * cnt - exact number of elements to parse + * Return: number of elements parsed, or error + */ +int ph_get_array(struct parse_helper *ph, uint32_t *data, int cnt) +{ + int i = 0; + + for (;;) { + int ret = ph_get_value(ph, &data[i++]); + if (ret) + return ret; + if (i >= cnt) + break; + ph_skip_comma(ph); /* comma is optional */ + } + return i; +} + +static char *grab_token(char *dest, int size, char *src) +{ + while (size) { + char c = *src; + if ((c == ' ') || (c == '\t') || (c == '\r') || (c == '\n') + || (c == '#') || !c) + break; + *dest++ = c; + size--; + src++; + } + if (!size) + return NULL; + *dest = 0; + return src; +} + +int ph_get_table_entry_id(struct parse_helper *ph, + const table_entry_t *table, const char *table_name) +{ + int val; + char token[16]; + char *p; + + if (ph_skip_separators(ph)) + return -1; + p = grab_token(token, sizeof(token), ph->p); + if (!p) + return -1; + val = get_table_entry_id(table, table_name, token); + if (val != -1) + ph->p = p; + return val; +} + diff --git a/tools/parse_helper.h b/tools/parse_helper.h new file mode 100644 index 0000000..1ff98a3 --- /dev/null +++ b/tools/parse_helper.h @@ -0,0 +1,28 @@ +/* + * (C) Copyright 20012 Boundary Devices Inc, troy.kisky@boundarydevices.com + * + * Licensed under the GPL-2 or later. + */ + +#ifndef _PARSE_HELPER_H_ +#define _PARSE_HELPER_H_ + +struct parse_helper { + char *line; + size_t len; + FILE *fd; + int lineno; + char cmd_started; + char *filename; + char *p; +}; + +int ph_open(struct parse_helper *ph, char *filename); +void ph_close(struct parse_helper *ph); +int ph_skip_separators(struct parse_helper *ph); +int ph_skip_comma(struct parse_helper *ph); +int ph_get_value(struct parse_helper *ph, uint32_t *pval); +int ph_get_array(struct parse_helper *ph, uint32_t *data, int cnt); +int ph_get_table_entry_id(struct parse_helper *ph, + const table_entry_t *table, const char *table_name); +#endif