From patchwork Thu Nov 1 03:46:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 991802 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42lrjJ3pgMz9sPk for ; Thu, 1 Nov 2018 14:46:28 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="k2gfIVYJ"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="Vg0r7j02"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42lrjJ221bzF3L1 for ; Thu, 1 Nov 2018 14:46:28 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="k2gfIVYJ"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="Vg0r7j02"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.27; helo=out3-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="k2gfIVYJ"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="Vg0r7j02"; dkim-atps=neutral Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42lrj82G3ZzF3Jy for ; Thu, 1 Nov 2018 14:46:20 +1100 (AEDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 19259224B3; Wed, 31 Oct 2018 23:46:18 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 31 Oct 2018 23:46:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=from:to:cc:subject:date:message-id :mime-version:content-transfer-encoding; s=fm1; bh=1gLT94qW5vyEH NtoGrcylGf14DIVZT3Xljq9Lces6Bc=; b=k2gfIVYJVpxJpQs80oxxqhUVzPGRF 9/8u+e5KNwbYUZvq1L0EuH65z/WuFpG8hW32eecpOyE9S67auKJjh/aDIpaGtvOq STFwq7g/Gx9Q1HWAvcHeOan3GX758iulej5TjvRK9hREst5uYVtzefDVvyadBlHi X94pUzAe5il7DvPsdIhigxoypR3jR+sArM7i9GbDL3bOg4sLLX/qaKIZAsfyCRce xds0SLb0UI1VZ28/mR/VDIVjFRniEnkBNAGGyKv+Wzxsb8Aavl+cxZjI/wcP3O/h j9bWgnizugMlnwIcipZT2nSRw3rNUzQZislbr9yiI7HVeHyGQHcXV9mag== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :message-id:mime-version:subject:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=1gLT94qW5vyEHNtoG rcylGf14DIVZT3Xljq9Lces6Bc=; b=Vg0r7j02+5zTdEbjnD6ZFt9ndx2J674d7 NEer1cVfNSwy228Xj4YUPyxhH2u4vussh92rdw+kooMzPvxpEiDV2eTQ0Bqb8cCO mcGPhHMwJvKY/ZrZORsBEqvKXO80WyUyi6aDgZ6Nk6KL1BR8BnZexuQcj16q2ZYi 575mDC3lRvzIX3WuIv6nW8Tc6tZaBSOXQBxzghBVJbpIfllkknX/VnBFwAJCDNwk k9qhahkqbcRfvOiF4uzu7a0p3SG97qEg3ydQ0Ve6aj/qRVZ73k+pOnMQX529hfPK Ok6RcjZeBNP2AwMwFbg3/3qjrZG7pRg3C3KaDKV8GJsFFQ247iQkA== X-ME-Sender: X-ME-Proxy: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id C255DE454E; Wed, 31 Oct 2018 23:46:15 -0400 (EDT) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH 1/2] discover: Remove unused params.c Date: Thu, 1 Nov 2018 14:46:09 +1100 Message-Id: <20181101034610.10297-1-sam@mendozajonas.com> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" The discover/params code is an artifact from a long time ago, and isn't used by anything today: remove it. Signed-off-by: Samuel Mendoza-Jonas --- discover/Makefile.am | 2 - discover/params.c | 595 ------------------------------------------- discover/params.h | 6 - 3 files changed, 603 deletions(-) delete mode 100644 discover/params.c delete mode 100644 discover/params.h diff --git a/discover/Makefile.am b/discover/Makefile.am index f5b799dc..d98ebec6 100644 --- a/discover/Makefile.am +++ b/discover/Makefile.am @@ -28,8 +28,6 @@ discover_pb_discover_SOURCES = \ discover/devmapper.h \ discover/event.c \ discover/event.h \ - discover/params.c \ - discover/params.h \ discover/parser.c \ discover/parser.h \ discover/parser-conf.c \ diff --git a/discover/params.c b/discover/params.c deleted file mode 100644 index 09c2b0f6..00000000 --- a/discover/params.c +++ /dev/null @@ -1,595 +0,0 @@ -/* This modules is based on the params.c module from Samba, written by Karl Auer - and much modifed by Christopher Hertel. */ - -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include -#include -#include -#include - -#include - -#include "params.h" - -#define new_array(type, num) ((type *)_new_array(sizeof(type), (num))) -#define realloc_array(ptr, type, num) \ - ((type *)_realloc_array((ptr), sizeof(type), (num))) - -#define rprintf(x, ...) do {pb_log(__VA_ARGS__); pb_log("\n");} while (0) -#define rsyserr(x, y, ...) do {pb_log(__VA_ARGS__); pb_log("\n");} while (0) - -#define MALLOC_MAX 0x40000000 -#define False 0 -#define True 1 - -static void *_new_array(unsigned int size, unsigned long num) -{ - if (num >= MALLOC_MAX/size) - return NULL; - return malloc(size * num); -} - -static void *_realloc_array(void *ptr, unsigned int size, unsigned long num) -{ - if (num >= MALLOC_MAX/size) - return NULL; - /* No realloc should need this, but just in case... */ - if (!ptr) - return malloc(size * num); - return realloc(ptr, size * num); -} - - -/* -------------------------------------------------------------------------- ** - * - * Module name: params - * - * -------------------------------------------------------------------------- ** - * - * This module performs lexical analysis and initial parsing of a - * Windows-like parameter file. It recognizes and handles four token - * types: section-name, parameter-name, parameter-value, and - * end-of-file. Comments and line continuation are handled - * internally. - * - * The entry point to the module is function pm_process(). This - * function opens the source file, calls the Parse() function to parse - * the input, and then closes the file when either the EOF is reached - * or a fatal error is encountered. - * - * A sample parameter file might look like this: - * - * [section one] - * parameter one = value string - * parameter two = another value - * [section two] - * new parameter = some value or t'other - * - * The parameter file is divided into sections by section headers: - * section names enclosed in square brackets (eg. [section one]). - * Each section contains parameter lines, each of which consist of a - * parameter name and value delimited by an equal sign. Roughly, the - * syntax is: - * - * :== {
} EOF - * - *
:==
{ } - * - *
:== '[' NAME ']' - * - * :== NAME '=' VALUE '\n' - * - * Blank lines and comment lines are ignored. Comment lines are lines - * beginning with either a semicolon (';') or a pound sign ('#'). - * - * All whitespace in section names and parameter names is compressed - * to single spaces. Leading and trailing whitespace is stipped from - * both names and values. - * - * Only the first equals sign in a parameter line is significant. - * Parameter values may contain equals signs, square brackets and - * semicolons. Internal whitespace is retained in parameter values, - * with the exception of the '\r' character, which is stripped for - * historic reasons. Parameter names may not start with a left square - * bracket, an equal sign, a pound sign, or a semicolon, because these - * are used to identify other tokens. - * - * -------------------------------------------------------------------------- ** - */ - -/* -------------------------------------------------------------------------- ** - * Constants... - */ - -#define BUFR_INC 1024 - - -/* -------------------------------------------------------------------------- ** - * Variables... - * - * bufr - pointer to a global buffer. This is probably a kludge, - * but it was the nicest kludge I could think of (for now). - * bSize - The size of the global buffer . - */ - -static char *bufr = NULL; -static int bSize = 0; - -/* -------------------------------------------------------------------------- ** - * Functions... - */ - -static int EatWhitespace( FILE *InFile ) - /* ------------------------------------------------------------------------ ** - * Scan past whitespace (see ctype(3C)) and return the first non-whitespace - * character, or newline, or EOF. - * - * Input: InFile - Input source. - * - * Output: The next non-whitespace character in the input stream. - * - * Notes: Because the config files use a line-oriented grammar, we - * explicitly exclude the newline character from the list of - * whitespace characters. - * - Note that both EOF (-1) and the nul character ('\0') are - * considered end-of-file markers. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - - for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) ) - ; - return( c ); - } /* EatWhitespace */ - -static int EatComment( FILE *InFile ) - /* ------------------------------------------------------------------------ ** - * Scan to the end of a comment. - * - * Input: InFile - Input source. - * - * Output: The character that marks the end of the comment. Normally, - * this will be a newline, but it *might* be an EOF. - * - * Notes: Because the config files use a line-oriented grammar, we - * explicitly exclude the newline character from the list of - * whitespace characters. - * - Note that both EOF (-1) and the nul character ('\0') are - * considered end-of-file markers. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - - for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) ) - ; - return( c ); - } /* EatComment */ - -static int Continuation( char *line, int pos ) - /* ------------------------------------------------------------------------ ** - * Scan backards within a string to discover if the last non-whitespace - * character is a line-continuation character ('\\'). - * - * Input: line - A pointer to a buffer containing the string to be - * scanned. - * pos - This is taken to be the offset of the end of the - * string. This position is *not* scanned. - * - * Output: The offset of the '\\' character if it was found, or -1 to - * indicate that it was not. - * - * ------------------------------------------------------------------------ ** - */ - { - pos--; - while( (pos >= 0) && isspace(((unsigned char *)line)[pos]) ) - pos--; - - return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 ); - } /* Continuation */ - - -static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) ) - /* ------------------------------------------------------------------------ ** - * Scan a section name, and pass the name to function sfunc(). - * - * Input: InFile - Input source. - * sfunc - Pointer to the function to be called if the section - * name is successfully read. - * - * Output: True if the section name was read and True was returned from - * . False if failed or if a lexical error was - * encountered. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - int i; - int end; - char *func = "params.c:Section() -"; - - i = 0; /* is the offset of the next free byte in bufr[] and */ - end = 0; /* is the current "end of string" offset. In most */ - /* cases these will be the same, but if the last */ - /* character written to bufr[] is a space, then */ - /* will be one less than . */ - - c = EatWhitespace( InFile ); /* We've already got the '['. Scan */ - /* past initial white space. */ - - while( (EOF != c) && (c > 0) ) - { - - /* Check that the buffer is big enough for the next character. */ - if( i > (bSize - 2) ) - { - bSize += BUFR_INC; - bufr = realloc_array( bufr, char, bSize ); - if( NULL == bufr ) - { - rprintf(FERROR, "%s Memory re-allocation failure.", func); - return( False ); - } - } - - /* Handle a single character. */ - switch( c ) - { - case ']': /* Found the closing bracket. */ - bufr[end] = '\0'; - if( 0 == end ) /* Don't allow an empty name. */ - { - rprintf(FERROR, "%s Empty section name in configuration file.\n", func ); - return( False ); - } - if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */ - return( False ); - (void)EatComment( InFile ); /* Finish off the line. */ - return( True ); - - case '\n': /* Got newline before closing ']'. */ - i = Continuation( bufr, i ); /* Check for line continuation. */ - if( i < 0 ) - { - bufr[end] = '\0'; - rprintf(FERROR, "%s Badly formed line in configuration file: %s\n", - func, bufr ); - return( False ); - } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); - c = getc( InFile ); /* Continue with next line. */ - break; - - default: /* All else are a valid name chars. */ - if( isspace( c ) ) /* One space per whitespace region. */ - { - bufr[end] = ' '; - i = end + 1; - c = EatWhitespace( InFile ); - } - else /* All others copy verbatim. */ - { - bufr[i++] = c; - end = i; - c = getc( InFile ); - } - } - } - - /* We arrive here if we've met the EOF before the closing bracket. */ - rprintf(FERROR, "%s Unexpected EOF in the configuration file: %s\n", func, bufr ); - return( False ); - } /* Section */ - -static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c ) - /* ------------------------------------------------------------------------ ** - * Scan a parameter name and value, and pass these two fields to pfunc(). - * - * Input: InFile - The input source. - * pfunc - A pointer to the function that will be called to - * process the parameter, once it has been scanned. - * c - The first character of the parameter name, which - * would have been read by Parse(). Unlike a comment - * line or a section header, there is no lead-in - * character that can be discarded. - * - * Output: True if the parameter name and value were scanned and processed - * successfully, else False. - * - * Notes: This function is in two parts. The first loop scans the - * parameter name. Internal whitespace is compressed, and an - * equal sign (=) terminates the token. Leading and trailing - * whitespace is discarded. The second loop scans the parameter - * value. When both have been successfully identified, they are - * passed to pfunc() for processing. - * - * ------------------------------------------------------------------------ ** - */ - { - int i = 0; /* Position within bufr. */ - int end = 0; /* bufr[end] is current end-of-string. */ - int vstart = 0; /* Starting position of the parameter value. */ - char *func = "params.c:Parameter() -"; - - /* Read the parameter name. */ - while( 0 == vstart ) /* Loop until we've found the start of the value. */ - { - - if( i > (bSize - 2) ) /* Ensure there's space for next char. */ - { - bSize += BUFR_INC; - bufr = realloc_array( bufr, char, bSize ); - if( NULL == bufr ) - { - rprintf(FERROR, "%s Memory re-allocation failure.", func) ; - return( False ); - } - } - - switch( c ) - { - case '=': /* Equal sign marks end of param name. */ - if( 0 == end ) /* Don't allow an empty name. */ - { - rprintf(FERROR, "%s Invalid parameter name in config. file.\n", func ); - return( False ); - } - bufr[end++] = '\0'; /* Mark end of string & advance. */ - i = end; /* New string starts here. */ - vstart = end; /* New string is parameter value. */ - bufr[i] = '\0'; /* New string is nul, for now. */ - break; - - case '\n': /* Find continuation char, else error. */ - i = Continuation( bufr, i ); - if( i < 0 ) - { - bufr[end] = '\0'; - rprintf(FERROR, "%s Ignoring badly formed line in configuration file: %s\n", - func, bufr ); - return( True ); - } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); - c = getc( InFile ); /* Read past eoln. */ - break; - - case '\0': /* Shouldn't have EOF within param name. */ - case EOF: - bufr[i] = '\0'; - rprintf(FERROR, "%s Unexpected end-of-file at: %s\n", func, bufr ); - return( True ); - - default: - if( isspace( c ) ) /* One ' ' per whitespace region. */ - { - bufr[end] = ' '; - i = end + 1; - c = EatWhitespace( InFile ); - } - else /* All others verbatim. */ - { - bufr[i++] = c; - end = i; - c = getc( InFile ); - } - } - } - - /* Now parse the value. */ - c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */ - while( (EOF !=c) && (c > 0) ) - { - - if( i > (bSize - 2) ) /* Make sure there's enough room. */ - { - bSize += BUFR_INC; - bufr = realloc_array( bufr, char, bSize ); - if( NULL == bufr ) - { - rprintf(FERROR, "%s Memory re-allocation failure.", func) ; - return( False ); - } - } - - switch( c ) - { - case '\r': /* Explicitly remove '\r' because the older */ - c = getc( InFile ); /* version called fgets_slash() which also */ - break; /* removes them. */ - - case '\n': /* Marks end of value unless there's a '\'. */ - i = Continuation( bufr, i ); - if( i < 0 ) - c = 0; - else - { - for( end = i; (end >= 0) && isspace(((unsigned char *) bufr)[end]); end-- ) - ; - c = getc( InFile ); - } - break; - - default: /* All others verbatim. Note that spaces do */ - bufr[i++] = c; /* not advance . This allows trimming */ - if( !isspace( c ) ) /* of whitespace at the end of the line. */ - end = i; - c = getc( InFile ); - break; - } - } - bufr[end] = '\0'; /* End of value. */ - - return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ - } /* Parameter */ - -static BOOL Parse( FILE *InFile, - BOOL (*sfunc)(char *), - BOOL (*pfunc)(char *, char *) ) - /* ------------------------------------------------------------------------ ** - * Scan & parse the input. - * - * Input: InFile - Input source. - * sfunc - Function to be called when a section name is scanned. - * See Section(). - * pfunc - Function to be called when a parameter is scanned. - * See Parameter(). - * - * Output: True if the file was successfully scanned, else False. - * - * Notes: The input can be viewed in terms of 'lines'. There are four - * types of lines: - * Blank - May contain whitespace, otherwise empty. - * Comment - First non-whitespace character is a ';' or '#'. - * The remainder of the line is ignored. - * Section - First non-whitespace character is a '['. - * Parameter - The default case. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - - c = EatWhitespace( InFile ); - while( (EOF != c) && (c > 0) ) - { - switch( c ) - { - case '\n': /* Blank line. */ - c = EatWhitespace( InFile ); - break; - - case ';': /* Comment line. */ - case '#': - c = EatComment( InFile ); - break; - - case '[': /* Section Header. */ - if (!sfunc) return True; - if( !Section( InFile, sfunc ) ) - return( False ); - c = EatWhitespace( InFile ); - break; - - case '\\': /* Bogus backslash. */ - c = EatWhitespace( InFile ); - break; - - default: /* Parameter line. */ - if( !Parameter( InFile, pfunc, c ) ) - return( False ); - c = EatWhitespace( InFile ); - break; - } - } - return( True ); - } /* Parse */ - -static FILE *OpenConfFile( char *FileName ) - /* ------------------------------------------------------------------------ ** - * Open a configuration file. - * - * Input: FileName - The pathname of the config file to be opened. - * - * Output: A pointer of type (FILE *) to the opened file, or NULL if the - * file could not be opened. - * - * ------------------------------------------------------------------------ ** - */ - { - FILE *OpenedFile; - char *func = "params.c:OpenConfFile() -"; - - if( NULL == FileName || 0 == *FileName ) - { - rprintf(FERROR,"%s No configuration filename specified.\n", func); - return( NULL ); - } - - OpenedFile = fopen( FileName, "r" ); - if( NULL == OpenedFile ) - { - rsyserr(FERROR, errno, "unable to open configuration file \"%s\"", - FileName); - } - - return( OpenedFile ); - } /* OpenConfFile */ - -BOOL pm_process( char *FileName, - BOOL (*sfunc)(char *), - BOOL (*pfunc)(char *, char *) ) - /* ------------------------------------------------------------------------ ** - * Process the named parameter file. - * - * Input: FileName - The pathname of the parameter file to be opened. - * sfunc - A pointer to a function that will be called when - * a section name is discovered. - * pfunc - A pointer to a function that will be called when - * a parameter name and value are discovered. - * - * Output: TRUE if the file was successfully parsed, else FALSE. - * - * ------------------------------------------------------------------------ ** - */ - { - int result; - FILE *InFile; - char *func = "params.c:pm_process() -"; - - InFile = OpenConfFile( FileName ); /* Open the config file. */ - if( NULL == InFile ) - return( False ); - - if( NULL != bufr ) /* If we already have a buffer */ - result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ - /* use it. */ - - else /* If we don't have a buffer */ - { /* allocate one, then parse, */ - bSize = BUFR_INC; /* then free. */ - bufr = new_array( char, bSize ); - if( NULL == bufr ) - { - rprintf(FERROR,"%s memory allocation failure.\n", func); - fclose(InFile); - return( False ); - } - result = Parse( InFile, sfunc, pfunc ); - free( bufr ); - bufr = NULL; - bSize = 0; - } - - fclose(InFile); - - if( !result ) /* Generic failure. */ - { - rprintf(FERROR,"%s Failed. Error returned from params.c:parse().\n", func); - return( False ); - } - - return( True ); /* Generic success. */ - } /* pm_process */ - -/* -------------------------------------------------------------------------- */ - diff --git a/discover/params.h b/discover/params.h deleted file mode 100644 index 02a39c96..00000000 --- a/discover/params.h +++ /dev/null @@ -1,6 +0,0 @@ - -#define BOOL int - -BOOL pm_process( char *FileName, - BOOL (*sfunc)(char *), - BOOL (*pfunc)(char *, char *) ); From patchwork Thu Nov 1 03:46:10 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sam Mendoza-Jonas X-Patchwork-Id: 991803 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42lrjT5zNWz9sPj for ; Thu, 1 Nov 2018 14:46:37 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="wDndoxf8"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TIuuVadW"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 42lrjT49RlzF3L3 for ; Thu, 1 Nov 2018 14:46:37 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="wDndoxf8"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TIuuVadW"; dkim-atps=neutral X-Original-To: petitboot@lists.ozlabs.org Delivered-To: petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=mendozajonas.com (client-ip=66.111.4.27; helo=out3-smtp.messagingengine.com; envelope-from=sam@mendozajonas.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=mendozajonas.com Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=mendozajonas.com header.i=@mendozajonas.com header.b="wDndoxf8"; dkim=pass (2048-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b="TIuuVadW"; dkim-atps=neutral Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 42lrjB1YSBzF3Jy for ; Thu, 1 Nov 2018 14:46:22 +1100 (AEDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 0802D224CF; Wed, 31 Oct 2018 23:46:20 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 31 Oct 2018 23:46:20 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= mendozajonas.com; h=from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; s=fm1; bh=PVL6Xls0K8Eqj6GQ3my5/1X8+V1KyY64XQv0KQ6BdRQ=; b=wDndo xf8oRcy2sv+aHjErDPNgt5pvJvLDNlIIeaN6ILCFQNJSjtbLJ81Y10tIwemy8GLR wRA5fPP0+1k2B4av0jaFWLNpCDvcia+Rmwo+hug8B1oCdl1/GkVFmmBCkNvAPeC+ VfCBpfWq8xf+QmvAka/RN48P+T5EpoUg6aNrTMKqT6i6mWUevC6KqOgIG/0x4cBS Nn4bwxo5JbGCR8k2J3yxNbr7Oec0lc4Td5Z8FibNVE1bT1wm5pKC/zSJgntBdZwn ToJDOIy+FdoiRbX8d1xa5nJJtOUFIrxZpebMYRi4B6Dde2ljQNm1/+gl8su6DCbt U8aKbTU6iFEZf881w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm1; bh=PVL6Xls0K8Eqj6GQ3my5/1X8+V1KyY64XQv0KQ6BdRQ=; b=TIuuVadW Yl/PEhFMiwuy5hfeeevPXCMx7nL9WkbwUSGmnSSuUpCT6hNKqpcv4AlzLZ45vfvr k1cnRfSgAWNykuBDOei43GIyA4IVOyxKQjXMUHCpl+5Y6GkhdovhUX+4hNffS1UG /X97v0LP6GqInTnEdhWNwGWJNrKR8LLUUlfF8UR2l6e76SiglI3KNbBwOGd8BHqI Ht9ekWoqzkGIPBFe0+F3OqydRA625V8r+15t/UhCjUgfIk6nm79vuHhLoq6ej33+ feVFuekNLs8G3zkbZASfpmWxXMZ6JVWFS3Dh1Fq+ZPPHGRxitSxIul67bm+x8yhl auz1rbBKAC+z9g== X-ME-Sender: X-ME-Proxy: Received: from v4.ozlabs.ibm.com (unknown [122.99.82.10]) by mail.messagingengine.com (Postfix) with ESMTPA id 1B896E467A; Wed, 31 Oct 2018 23:46:17 -0400 (EDT) From: Samuel Mendoza-Jonas To: petitboot@lists.ozlabs.org Subject: [PATCH 2/2] discover: Reimplement native-parser as a Bison parser Date: Thu, 1 Nov 2018 14:46:10 +1100 Message-Id: <20181101034610.10297-2-sam@mendozajonas.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181101034610.10297-1-sam@mendozajonas.com> References: <20181101034610.10297-1-sam@mendozajonas.com> MIME-Version: 1.0 X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Mendoza-Jonas Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Occasionally you look at some code and realise that a) this never gets built, and b) even if it did it would never compile. Today's example is native-parser.c which we must have just assumed worked for quite a while. The native parser has bitrotted entirely and needs to be brought up to date. While we're here, lets take the change to implement a proper grammar for it. This helps us reason more effectively about the parser, lets us extend it easily in the future, and.. I wanted to write a Bison parser too. This implements most of the old functionality, but drops off some smaller details like settings icons which needs some separate attention to bring up to date. Signed-off-by: Samuel Mendoza-Jonas --- Makefile.am | 1 + discover/Makefile.am | 1 + discover/native-parser.c | 131 -------------------- discover/native/Makefile.am | 54 +++++++++ discover/native/native-lexer.l | 61 ++++++++++ discover/native/native-parser.y | 194 ++++++++++++++++++++++++++++++ discover/native/native.c | 55 +++++++++ discover/native/native.h | 27 +++++ test/parser/Makefile.am | 9 +- test/parser/test-native-globals.c | 45 +++++++ test/parser/test-native-simple.c | 31 +++++ 11 files changed, 476 insertions(+), 133 deletions(-) delete mode 100644 discover/native-parser.c create mode 100644 discover/native/Makefile.am create mode 100644 discover/native/native-lexer.l create mode 100644 discover/native/native-parser.y create mode 100644 discover/native/native.c create mode 100644 discover/native/native.h create mode 100644 test/parser/test-native-globals.c create mode 100644 test/parser/test-native-simple.c diff --git a/Makefile.am b/Makefile.am index c0ad8397..63456ca4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -50,6 +50,7 @@ CLEANFILES = include lib/Makefile.am include discover/grub2/Makefile.am +include discover/native/Makefile.am include discover/Makefile.am include test/Makefile.am include test/lib/Makefile.am diff --git a/discover/Makefile.am b/discover/Makefile.am index d98ebec6..bfe33fad 100644 --- a/discover/Makefile.am +++ b/discover/Makefile.am @@ -55,6 +55,7 @@ discover_pb_discover_SOURCES = \ discover_pb_discover_LDADD = \ discover/grub2/grub2-parser.ro \ + discover/native/native-parser.ro \ discover/platform.ro \ $(core_lib) \ $(UDEV_LIBS) diff --git a/discover/native-parser.c b/discover/native-parser.c deleted file mode 100644 index 08309d1b..00000000 --- a/discover/native-parser.c +++ /dev/null @@ -1,131 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -#include "parser.h" -#include "params.h" -#include "paths.h" - -#include -#include -#include - -static const char *conf_filename = "/boot/petitboot.conf"; - -static struct boot_option *cur_opt; -static struct device *dev; -static const char *devpath; -static int device_added; - -static int check_and_add_device(struct device *dev) -{ - if (!dev->icon_file) - dev->icon_file = strdup(generic_icon_file(guess_device_type())); - - return !add_device(dev); -} - -static int section(char *section_name) -{ - if (!device_added++ && !check_and_add_device(dev)) - return 0; - - if (cur_opt) { - add_boot_option(cur_opt); - free_boot_option(cur_opt); - } - - cur_opt = malloc(sizeof(*cur_opt)); - memset(cur_opt, 0, sizeof(*cur_opt)); - return 1; -} - - -static void set_boot_option_parameter(struct boot_option *opt, - const char *name, const char *value) -{ - if (streq(name, "name")) - opt->name = strdup(value); - - else if (streq(name, "description")) - opt->description = strdup(value); - - else if (streq(name, "image")) - opt->boot_image_file = resolve_path(value, devpath); - - else if (streq(name, "icon")) - opt->icon_file = resolve_path(value, devpath); - - else if (streq(name, "initrd")) - opt->initrd_file =resolve_path(value, devpath); - - else if (streq(name, "args")) - opt->boot_args = strdup(value); - - else - fprintf(stderr, "Unknown parameter %s\n", name); -} - -static void set_device_parameter(struct device *dev, - const char *name, const char *value) -{ - if (streq(name, "name")) - dev->name = strdup(value); - - else if (streq(name, "description")) - dev->description = strdup(value); - - else if (streq(name, "icon")) - dev->icon_file = resolve_path(value, devpath); -} - -static int parameter(char *param_name, char *param_value) -{ - if (cur_opt) - set_boot_option_parameter(cur_opt, param_name, param_value); - else - set_device_parameter(dev, param_name, param_value); - return 1; -} - - -static int native_parse(const char *device) -{ - char *filepath; - int rc; - - filepath = resolve_path(conf_filename, device); - - cur_opt = NULL; - dev = malloc(sizeof(*dev)); - memset(dev, 0, sizeof(*dev)); - dev->id = strdup(device); - - rc = pm_process(filepath, section, parameter); - if (!rc) - return 0; - - if (cur_opt) { - add_boot_option(cur_opt); - free_boot_option(cur_opt); - } - - cur_opt = NULL; - - free(filepath); - - return 1; -} - -define_parser(native, native_parse); diff --git a/discover/native/Makefile.am b/discover/native/Makefile.am new file mode 100644 index 00000000..f120e704 --- /dev/null +++ b/discover/native/Makefile.am @@ -0,0 +1,54 @@ +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +noinst_PROGRAMS += discover/native/native-parser.ro + +discover_native_native_parser_ro_SOURCES = \ + discover/native/native.h \ + discover/native/native.c \ + discover/native/native-lexer.l \ + discover/native/native-parser.y + +BUILT_SOURCES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.h \ + discover/native/native-lexer.c + +CLEANFILES += \ + discover/native/native-parser.c \ + discover/native/native-parser.h \ + discover/native/native-lexer.c \ + discover/native/native-lexer.h + +discover_native_native_parser_ro_CPPFLAGS = \ + $(AM_CPPFLAGS) \ + -I$(top_srcdir)/discover/native \ + -I$(top_builddir)/discover/native + +discover_native_native_parser_ro_LINK = \ + $(LD) -r -o $@ + +# ylwrap doesn't handle flex header files well; use our own rule here. +discover/native/native-lexer.h discover/native/native-lexer.c: \ + $(top_srcdir)/discover/native/native-lexer.l + $(AM_V_LEX)$(LEXCOMPILE) --header-file=discover/native/native-lexer.h \ + -o discover/native/native-lexer.c $^ + +# We need to loosen our warnings for the generated lexer code. +discover/native/%native-lexer.o discover/native/native-lexer.o: \ + AM_CFLAGS += -Wno-unused-parameter -Wno-missing-prototypes \ + -Wno-missing-declarations -Wno-sign-compare + +$(discover_native_native_parser_ro_OBJECTS): discover/native/native-parser.h diff --git a/discover/native/native-lexer.l b/discover/native/native-lexer.l new file mode 100644 index 00000000..50ad94cb --- /dev/null +++ b/discover/native/native-lexer.l @@ -0,0 +1,61 @@ +%{ +#include "native.h" +#include "native-parser.h" +#include + +#define YYSTYPE NSTYPE + +void yyerror(struct native_parser *parser, const char *fmt, ...); +%} + +%option nounput noinput +%option batch never-interactive +%option warn +%option noyywrap +%option reentrant +%option bison-bridge +%option yylineno +%option noyyalloc noyyfree noyyrealloc +%option extra-type="struct native_parser *" +%option prefix="n" + +DELIM [ \t]+ +NUMBER 0|[1-9][0-9]* +WORD [^{}|&$;<> \t\n'"#]+ +NEWLINE [\n]+ + +%% + +name { return TOKEN_NAME; } +image { return TOKEN_IMAGE; } +initrd { return TOKEN_INITRD; } +args { return TOKEN_ARGS; } +dtb { return TOKEN_DTB; } +description { return TOKEN_DESCRIPTION; } +default { return TOKEN_DEFAULT; } +dev_description { return TOKEN_DEV_DESCRIPTION; } +{DELIM} { ; } +{NUMBER} { yylval->num = atoi(yytext); return TOKEN_NUMBER; } +{WORD} { yylval->word = strdup(yytext); return TOKEN_WORD; } +{NEWLINE} { return TOKEN_NEWLINE; } + +%% + +struct native_parser; + +void *yyalloc(size_t bytes, void *yyscanner) +{ + struct native_parser *parser = yyget_extra(yyscanner); + return talloc_size(parser, bytes); +} + +void *yyrealloc(void *ptr, size_t bytes, void *yyscanner) +{ + struct native_parser *parser = yyget_extra(yyscanner); + return talloc_realloc_size(parser, ptr, bytes); +} + +void yyfree(void *ptr, void *yyscanner __attribute__((unused))) +{ + talloc_free(ptr); +} diff --git a/discover/native/native-parser.y b/discover/native/native-parser.y new file mode 100644 index 00000000..f0a547b8 --- /dev/null +++ b/discover/native/native-parser.y @@ -0,0 +1,194 @@ + +%pure-parser +%lex-param { nscan_t scanner } +%parse-param { struct native_parser *parser } +%parse-param { void *scanner } +%error-verbose + +%define api.prefix {n} +%{ +#include +#include +#include "discover/resource.h" +#include "discover/parser-utils.h" + +#include "native.h" + +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...); +%} + +%union { + char *word; + int num; +} + +%token TOKEN_WORD +%token TOKEN_NUMBER + +%token TOKEN_DEFAULT +%token TOKEN_DEV_DESCRIPTION + +%token TOKEN_NAME +%token TOKEN_IMAGE +%token TOKEN_INITRD +%token TOKEN_ARGS +%token TOKEN_DTB +%token TOKEN_DESCRIPTION +%token TOKEN_NEWLINE + +%{ +#include "native-lexer.h" +%} + +%% + +native: + globals boot_options { native_parser_finish(parser); } + | boot_options { native_parser_finish(parser); } + ; +globals: + globals global + | global + ; +global: + TOKEN_DEFAULT TOKEN_WORD { + if (parser->default_name) + pb_log_fn("Duplicate default option, ignoring\n"); + else + parser->default_name = talloc_strdup(parser, $2); + } + | TOKEN_DEV_DESCRIPTION TOKEN_WORD { + native_append_string(parser, + &parser->ctx->device->device->description, $2); + } + | TOKEN_NEWLINE + ; + +boot_options: + boot_options option + | option + ; +option: + label params + ; +label: + TOKEN_NAME TOKEN_WORD {native_parser_create_option(parser, $2);} + ; +params: + params line + | line + ; +line: + TOKEN_IMAGE TOKEN_WORD { + native_set_resource(parser, &parser->opt->boot_image, $2); + } + | TOKEN_INITRD TOKEN_WORD { + native_set_resource(parser, &parser->opt->initrd, $2); + } + | TOKEN_DTB TOKEN_WORD { + native_set_resource(parser, &parser->opt->dtb, $2); + } + | TOKEN_ARGS TOKEN_WORD { + native_append_string(parser, &parser->opt->option->boot_args, $2); + } + | TOKEN_DESCRIPTION TOKEN_WORD { + native_append_string(parser, &parser->opt->option->description, $2); + } + | TOKEN_NEWLINE + ; +%% + +void yyerror(struct native_parser *parser, void *scanner, const char *fmt, ...) +{ + const char *str; + va_list ap; + + va_start(ap, fmt); + str = talloc_vasprintf(parser, fmt, ap); + va_end(ap); + + pb_log("parse error: %d('%s'): %s\n", nget_lineno(scanner), + nget_text(scanner), str); +} + +void native_parser_finish(struct native_parser *parser) +{ + if (parser->opt) { + discover_context_add_boot_option(parser->ctx, parser->opt); + parser->opt = NULL; + } +} + +void native_set_resource(struct native_parser *parser, + struct resource ** resource, const char *path) +{ + if (*resource) { + pb_log_fn("Duplicate resource at line %d: %s\n", + nget_lineno(parser->scanner), path); + return; + } + + *resource = create_devpath_resource(parser->opt, parser->opt->device, + path); +} + +void native_append_string(struct native_parser *parser, + char **str, const char *append) +{ + if (*str) + *str = talloc_asprintf_append(*str, append); + else + *str = talloc_strdup(parser->opt, append); +} + +void native_parser_create_option(struct native_parser *parser, const char *name) +{ + struct discover_boot_option *opt = parser->opt; + + if (opt) + native_parser_finish(parser); + + opt = discover_boot_option_create(parser->ctx, parser->ctx->device); + opt->option->name = talloc_strdup(opt, name); + opt->option->id = talloc_asprintf(opt, "%s@%p", + parser->ctx->device->device->id, opt); + opt->option->type = DISCOVER_BOOT_OPTION; + opt->option->is_default = parser->default_name && + streq(parser->default_name, name); + parser->opt = opt; + return; +} + +struct native_parser *native_parser_create(struct discover_context *ctx) +{ + struct native_parser *parser; + + parser = talloc_zero(ctx, struct native_parser); + parser->ctx = ctx; + nlex_init_extra(parser, &parser->scanner); + + return parser; +} + +void native_parser_parse(struct native_parser *parser, const char *filename, + char *buf, int len) +{ + YY_BUFFER_STATE bufstate; + int rc; + + if (!len) + return; + + parser->filename = filename; + + bufstate = n_scan_bytes(buf, len - 1, parser->scanner); + nset_lineno(1, parser->scanner); + + rc = nparse(parser, parser->scanner); + + if (rc) + pb_log("Failed to parse %s\n", filename); + + n_delete_buffer(bufstate, parser->scanner); +} + diff --git a/discover/native/native.c b/discover/native/native.c new file mode 100644 index 00000000..964ad1d4 --- /dev/null +++ b/discover/native/native.c @@ -0,0 +1,55 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include "native.h" + +static const char *const native_conf_files[] = { + "/boot/petitboot.conf", + "/petitboot.conf", + NULL +}; + +static int native_parse(struct discover_context *dc) +{ + const char * const *filename; + struct native_parser *parser; + int len, rc; + char *buf; + + /* Support block device boot only at present */ + if (dc->event) + return -1; + + for (filename = native_conf_files; *filename; filename++) { + rc = parser_request_file(dc, dc->device, *filename, &buf, &len); + if (rc) + continue; + + parser = native_parser_create(dc); + native_parser_parse(parser, *filename, buf, len); + device_handler_status_dev_info(dc->handler, dc->device, + _("Parsed native configuration from %s"), + *filename); + talloc_free(buf); + talloc_free(parser); + break; + } + + return 0; +} + +static struct parser native_parser = { + .name = "native", + .parse = native_parse, + .resolve_resource = resolve_devpath_resource, +}; + +register_parser(native_parser); diff --git a/discover/native/native.h b/discover/native/native.h new file mode 100644 index 00000000..1cf7d6aa --- /dev/null +++ b/discover/native/native.h @@ -0,0 +1,27 @@ +#ifndef NATIVE_H +#define NATIVE_H + +#include + +struct native_parser { + struct discover_context *ctx; + struct discover_boot_option *opt; + void *scanner; + const char *filename; + char *default_name; +}; + +void native_parser_finish(struct native_parser *parser); +void native_set_resource(struct native_parser *parser, struct resource **, + const char *path); +void native_append_string(struct native_parser *parser, + char **str, const char *append); +void native_parser_create_option(struct native_parser *parser, + const char *name); + +/* external parser api */ +struct native_parser *native_parser_create(struct discover_context *ctx); +void native_parser_parse(struct native_parser *parser, const char *filename, + char *buf, int len); +#endif /* NATIVE_H */ + diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am index 363933e3..6e0da38c 100644 --- a/test/parser/Makefile.am +++ b/test/parser/Makefile.am @@ -87,7 +87,9 @@ parser_TESTS = \ test/parser/test-syslinux-single-yocto \ test/parser/test-syslinux-global-append \ test/parser/test-syslinux-explicit \ - test/parser/test-syslinux-nested-config + test/parser/test-syslinux-nested-config \ + test/parser/test-native-simple \ + test/parser/test-native-globals TESTS += $(parser_TESTS) @@ -133,13 +135,16 @@ test_parser_libtest_ro_SOURCES = \ discover/parser-conf.c \ discover/user-event.c \ discover/event.c \ - $(discover_grub2_grub2_parser_ro_SOURCES) + $(discover_grub2_grub2_parser_ro_SOURCES) \ + $(discover_native_native_parser_ro_SOURCES) test_parser_libtest_ro_CPPFLAGS = \ $(AM_CPPFLAGS) \ -I$(top_srcdir)/discover \ -I$(top_srcdir)/discover/grub2 \ -I$(top_builddir)/discover/grub2 \ + -I$(top_srcdir)/discover/native \ + -I$(top_builddir)/discover/native \ -DPETITBOOT_TEST \ -DLOCAL_STATE_DIR='"$(localstatedir)"' \ -DTEST_CONF_BASE='"$(top_srcdir)/test/parser/data"' diff --git a/test/parser/test-native-globals.c b/test/parser/test-native-globals.c new file mode 100644 index 00000000..0361e03b --- /dev/null +++ b/test/parser/test-native-globals.c @@ -0,0 +1,45 @@ +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ + +default first + +name first +image /vmlinuz +args console=hvc0 +initrd /initrd + +name second +image /boot/vmlinuz +args console=tty0 +initrd /boot/initrd + +#endif + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded(test, "/boot/petitboot.conf"); + + test_run_parser(test, "native"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 2); + + opt = get_boot_option(ctx, 0); + + check_name(opt, "first"); + check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz"); + check_args(opt, "console=hvc0"); + check_resolved_local_resource(opt->initrd, ctx->device, "/initrd"); + check_is_default(opt); + + opt = get_boot_option(ctx, 1); + check_name(opt, "second"); + check_resolved_local_resource(opt->boot_image, ctx->device, "/boot/vmlinuz"); + check_args(opt, "console=tty0"); + check_resolved_local_resource(opt->initrd, ctx->device, "/boot/initrd"); +} diff --git a/test/parser/test-native-simple.c b/test/parser/test-native-simple.c new file mode 100644 index 00000000..f817228e --- /dev/null +++ b/test/parser/test-native-simple.c @@ -0,0 +1,31 @@ +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ + +name native-option +image /vmlinuz +args console=hvc0 +initrd /initrd + +#endif + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded(test, "/boot/petitboot.conf"); + + test_run_parser(test, "native"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 1); + + opt = get_boot_option(ctx, 0); + + check_name(opt, "native-option"); + check_resolved_local_resource(opt->boot_image, ctx->device, "/vmlinuz"); + check_args(opt, "console=hvc0"); + check_resolved_local_resource(opt->initrd, ctx->device, "/initrd"); +}