From patchwork Wed Nov 28 01:31:41 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Troy Kisky X-Patchwork-Id: 202343 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 D0BF62C0082 for ; Wed, 28 Nov 2012 12:33:32 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 4219E4A0AE; Wed, 28 Nov 2012 02:33:04 +0100 (CET) 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 bNEd3oC++Df4; Wed, 28 Nov 2012 02:33:04 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 71BD54A0F8; Wed, 28 Nov 2012 02:32:11 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A10394A095 for ; Wed, 28 Nov 2012 02:32:02 +0100 (CET) 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 AMWq+yRJFb0M for ; Wed, 28 Nov 2012 02:32:01 +0100 (CET) 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-pb0-f44.google.com (mail-pb0-f44.google.com [209.85.160.44]) by theia.denx.de (Postfix) with ESMTPS id 174194A0A6 for ; Wed, 28 Nov 2012 02:31:53 +0100 (CET) Received: by mail-pb0-f44.google.com with SMTP id uo1so8838979pbc.3 for ; Tue, 27 Nov 2012 17:31:51 -0800 (PST) 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=/vlMi/61XnPUHXTlc84HCy3YDeDb5HWACk59cdJ7usY=; b=SDkw7yrqk3DFFoTY5nOXNJahyKl6dZ8blS5idek5TtQI8EL4Yo2QJ1n/6gNlF1YHMk kqcLOykPoCg4xvqSixMhjjDK7y1F+sX3lZSDmfUCmCjWtUpWCizDrf2/d5wJl2JTGibf UKPhYLAgf83tcm4VHQzpX1bPwjDZF4jdKDDZWBJSiNPdFMhP3tNOD7qRzUrVQiqDZJ02 23HyZWmvpNiBM4T5WHpriuzQYK+RP4z3sVliZIuktr5oAedSyPpS1Bn38WFF5Qg4SaDg 78FFtZusLA3muTWFOJh/6Q/SuWc2gjtSLL0T7Pn/8C6jArAMyfFyK++kjkbFc9EpFSu8 td6w== Received: by 10.66.82.73 with SMTP id g9mr48082967pay.5.1354066311571; Tue, 27 Nov 2012 17:31:51 -0800 (PST) Received: from officeserver-2 ([70.96.116.236]) by mx.google.com with ESMTPS id qp6sm11501506pbc.25.2012.11.27.17.31.48 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 27 Nov 2012 17:31:50 -0800 (PST) Received: from tkisky by officeserver-2 with local (Exim 4.76) (envelope-from ) id 1TdWVC-0007l4-Da; Tue, 27 Nov 2012 18:31:50 -0700 From: Troy Kisky To: sbabic@denx.de Date: Tue, 27 Nov 2012 18:31:41 -0700 Message-Id: <1354066303-29762-10-git-send-email-troy.kisky@boundarydevices.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1354066303-29762-1-git-send-email-troy.kisky@boundarydevices.com> References: <1349315254-21151-9-git-send-email-troy.kisky@boundarydevices.com> <1354066303-29762-1-git-send-email-troy.kisky@boundarydevices.com> X-Gm-Message-State: ALoCoQnkSBXz00TI10ZNjfRLpekRhUhbtdo4ORCbuWn/THAXZ68YWYIOosN8RY+/g1DUCR/fXjmd Cc: u-boot@lists.denx.de Subject: [U-Boot] [PATCH V4 09/11] 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 Acked-by: Jason Liu --- 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 686840a..db3b247 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