buildgcc: Fix colors for dash
[coreboot.git] / util / nvramtool / input_file.c
index eafea4ca026e8de6a8374891995333c65c490705..1520930175c8250d58af15dca4e8105eeb14435d 100644 (file)
@@ -1,6 +1,5 @@
 /*****************************************************************************\
  * input_file.c
- * $Id$
  *****************************************************************************
  *  Copyright (C) 2002-2005 The Regents of the University of California.
  *  Produced at the Lawrence Livermore National Laboratory.
@@ -26,7 +25,7 @@
  *
  *  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.
+ *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 \*****************************************************************************/
 
 #include "common.h"
 #include "cmos_lowlevel.h"
 #include "reg_expr.h"
 
-static int get_input_file_line (FILE *f, char line[], int line_buf_size);
-static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
-                                                  const char value_str[]);
+static int get_input_file_line(FILE * f, char line[], int line_buf_size);
+static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e,
+                                                const char value_str[]);
 
 /* matches either a blank line or a comment line */
 static const char blank_or_comment_regex[] =
-   /* a blank line */
-   "(^[[:space:]]+$)"
-
-   "|"  /* or ... */
-
-   /* a line consisting of: optional whitespace followed by */
-   "(^[[:space:]]*"
-   /* a '#' character and optionally, additional characters */
-   "#.*$)";
+    /* a blank line */
+    "(^[[:space:]]+$)" "|"     /* or ... */
+    /* a line consisting of: optional whitespace followed by */
+    "(^[[:space:]]*"
+    /* a '#' character and optionally, additional characters */
+    "#.*$)";
 
 /* matches an assignment line */
 const char assignment_regex[] =
-   /* optional whitespace */
-   "^[[:space:]]*"
-   /* followed by a coreboot parameter name */
-   "([^[:space:]]+)"
-   /* followed by optional whitespace */
-   "[[:space:]]*"
-   /* followed by an '=' character */
-   "="
-   /* followed by optional whitespace */
-   "[[:space:]]*"
-   /* followed by a value that may contain embedded whitespace */
-   "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
-   /* followed by optional whitespace */
-   "[[:space:]]*$";
+    /* optional whitespace */
+    "^[[:space:]]*"
+    /* followed by a coreboot parameter name */
+    "([^[:space:]]+)"
+    /* followed by optional whitespace */
+    "[[:space:]]*"
+    /* followed by an '=' character */
+    "="
+    /* followed by optional whitespace */
+    "[[:space:]]*"
+    /* followed by a value that may contain embedded whitespace */
+    "([^[:space:]]+([[:space:]]+[^[:space:]]+)*)+"
+    /* followed by optional whitespace */
+    "[[:space:]]*$";
 
 static int line_num;
 
@@ -78,76 +74,78 @@ static int line_num;
  * write operations.  Perform sanity checking on all write operations and
  * exit with an error message if there is a problem.
  ****************************************************************************/
-cmos_write_t * process_input_file (FILE *f)
- {
-   static const int LINE_BUF_SIZE = 256;
-   static const size_t N_MATCHES = 4;
-   char line[LINE_BUF_SIZE];
-   const char *name, *value;
-   cmos_write_t *list, *item, **p;
-   regex_t blank_or_comment, assignment;
-   regmatch_t match[N_MATCHES];
-   const cmos_entry_t *e;
-
-   list = NULL;
-   p = &list;
-
-   compile_reg_exprs(REG_EXTENDED | REG_NEWLINE, 2, blank_or_comment_regex,
-                     &blank_or_comment, assignment_regex, &assignment);
-
-   /* each iteration processes one line from input file */
-   for (line_num = 1;
-        get_input_file_line(f, line, LINE_BUF_SIZE) == OK;
-        line_num++)
-    { /* skip comments and blank lines */
-      if (!regexec(&blank_or_comment, line, 0, NULL, 0))
-         continue;
-
-      /* Is this a valid assignment line?  If not, then it's a syntax
-       * error.
-       */
-      if (regexec(&assignment, line, N_MATCHES, match, 0))
-       { fprintf(stderr, "%s: Syntax error on line %d of input file.\n",
-                 prog_name, line_num);
-         exit(1);
-       }
-
-      /* OK, we found an assignment.  Break the line into substrings
-       * representing the lefthand and righthand sides of the assignment.
-       */
-      line[match[1].rm_eo] = '\0';
-      line[match[2].rm_eo] = '\0';
-      name = &line[match[1].rm_so];
-      value = &line[match[2].rm_so];
-
-      /* now look up the coreboot parameter name */
-      if (is_checksum_name(name) || (e = find_cmos_entry(name)) == NULL)
-       { fprintf(stderr, "%s: Error on line %d of input file: CMOS parameter "
-                 "%s not found.\n", prog_name, line_num, name);
-         exit(1);
-       }
-
-      /* At this point, we figure out what numeric value needs to be written
-       * to which location.  At the same time, we perform sanity checking on
-       * the write operation.
-       */
-
-      if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
-         out_of_memory();
-
-      item->bit = e->bit;
-      item->length = e->length;
-      item->value = try_prepare_cmos_write(e, value);
-
-      /* Append write operation to pending write list. */
-      item->next = NULL;
-      *p = item;
-      p = &item->next;
-    }
-
-   free_reg_exprs(2, &blank_or_comment, &assignment);
-   return list;
- }
+cmos_write_t *process_input_file(FILE * f)
+{
+       static const int LINE_BUF_SIZE = 256;
+       static const size_t N_MATCHES = 4;
+       char line[LINE_BUF_SIZE];
+       const char *name, *value;
+       cmos_write_t *list, *item, **p;
+       regex_t blank_or_comment, assignment;
+       regmatch_t match[N_MATCHES];
+       const cmos_entry_t *e;
+
+       list = NULL;
+       p = &list;
+
+       compile_reg_expr(REG_EXTENDED | REG_NEWLINE, blank_or_comment_regex, &blank_or_comment);
+       compile_reg_expr(REG_EXTENDED | REG_NEWLINE, assignment_regex, &assignment);
+
+       /* each iteration processes one line from input file */
+       for (line_num = 1; get_input_file_line(f, line, LINE_BUF_SIZE) == OK; line_num++) {     /* skip comments and blank lines */
+               if (!regexec(&blank_or_comment, line, 0, NULL, 0))
+                       continue;
+
+               /* Is this a valid assignment line?  If not, then it's a syntax
+                * error.
+                */
+               if (regexec(&assignment, line, N_MATCHES, match, 0)) {
+                       fprintf(stderr,
+                               "%s: Syntax error on line %d of input file.\n",
+                               prog_name, line_num);
+                       exit(1);
+               }
+
+               /* OK, we found an assignment.  Break the line into substrings
+                * representing the lefthand and righthand sides of the assignment.
+                */
+               line[match[1].rm_eo] = '\0';
+               line[match[2].rm_eo] = '\0';
+               name = &line[match[1].rm_so];
+               value = &line[match[2].rm_so];
+
+               /* now look up the coreboot parameter name */
+               if (is_checksum_name(name)
+                   || (e = find_cmos_entry(name)) == NULL) {
+                       fprintf(stderr,
+                               "%s: Error on line %d of input file: CMOS parameter "
+                               "%s not found.\n", prog_name, line_num, name);
+                       exit(1);
+               }
+
+               /* At this point, we figure out what numeric value needs to be written
+                * to which location.  At the same time, we perform sanity checking on
+                * the write operation.
+                */
+
+               if ((item = (cmos_write_t *) malloc(sizeof(*item))) == NULL)
+                       out_of_memory();
+
+               item->bit = e->bit;
+               item->length = e->length;
+               item->config = e->config;
+               item->value = try_prepare_cmos_write(e, value);
+
+               /* Append write operation to pending write list. */
+               item->next = NULL;
+               *p = item;
+               p = &item->next;
+       }
+
+       regfree(&blank_or_comment);
+       regfree(&assignment);
+       return list;
+}
 
 /****************************************************************************
  * do_cmos_writes
@@ -156,21 +154,26 @@ cmos_write_t * process_input_file (FILE *f)
  * all sanity checks.  Perform all write operations, destroying the list as
  * we go.
  ****************************************************************************/
-void do_cmos_writes (cmos_write_t *list)
- { cmos_write_t *item;
-
-   set_iopl(3);
-
-   while (list != NULL)
-    { item = list;
-      list = item->next;
-      cmos_write(item->bit, item->length, item->value);
-      free(item);
-    }
-
-   cmos_checksum_write(cmos_checksum_compute());
-   set_iopl(0);
- }
+void do_cmos_writes(cmos_write_t * list)
+{
+       cmos_write_t *item;
+
+       set_iopl(3);
+
+       while (list != NULL) {
+               cmos_entry_t e;
+               item = list;
+               e.bit = item->bit;
+               e.length = item->length;
+               e.config = item->config;
+               list = item->next;
+               cmos_write(&e, item->value);
+               free(item);
+       }
+
+       cmos_checksum_write(cmos_checksum_compute());
+       set_iopl(0);
+}
 
 /****************************************************************************
  * get_input_file_line
@@ -179,27 +182,29 @@ void do_cmos_writes (cmos_write_t *list)
  * array of 'line_buf_size' bytes.  Return OK on success or an error code on
  * error.
  ****************************************************************************/
-static int get_input_file_line (FILE *f, char line[], int line_buf_size)
- { switch (get_line_from_file(f, line, line_buf_size))
-    { case OK:
-         return OK;
-
-      case LINE_EOF:
-         return LINE_EOF;
-
-      case LINE_TOO_LONG:
-         fprintf(stderr, "%s: Error on line %d of input file: Maximum line "
-                 "length exceeded.  Max is %d characters.\n", prog_name,
-                 line_num, line_buf_size - 2);
-         break;
-
-      default:
-         BUG();
-    }
-
-   exit(1);
-   return 1;  /* keep compiler happy */
- }
+static int get_input_file_line(FILE * f, char line[], int line_buf_size)
+{
+       switch (get_line_from_file(f, line, line_buf_size)) {
+       case OK:
+               return OK;
+
+       case LINE_EOF:
+               return LINE_EOF;
+
+       case LINE_TOO_LONG:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: Maximum line "
+                       "length exceeded.  Max is %d characters.\n", prog_name,
+                       line_num, line_buf_size - 2);
+               break;
+
+       default:
+               BUG();
+       }
+
+       exit(1);
+       return 1;               /* keep compiler happy */
+}
 
 /****************************************************************************
  * try_prepare_cmos_write
@@ -208,73 +213,83 @@ static int get_input_file_line (FILE *f, char line[], int line_buf_size)
  * CMOS memory.  On success, return the converted value.  On error, exit with
  * an error message.
  ****************************************************************************/
-static unsigned long long try_prepare_cmos_write (const cmos_entry_t *e,
-                                                  const char value_str[])
- { unsigned long long value;
-
-   switch (prepare_cmos_write(e, value_str, &value))
-    { case OK:
-         return value;
-
-      case CMOS_OP_BAD_ENUM_VALUE:
-         fprintf(stderr, "%s: Error on line %d of input file: Bad value for "
-                 "parameter %s.", prog_name, line_num, e->name);
-         break;
-
-      case CMOS_OP_NEGATIVE_INT:
-         fprintf(stderr, "%s: Error on line %d of input file: This program "
-                 "does not support assignment of negative numbers to "
-                 "coreboot parameters.", prog_name, line_num);
-         break;
-
-      case CMOS_OP_INVALID_INT:
-         fprintf(stderr, "%s: Error on line %d of input file: %s is not a "
-                 "valid integer.", prog_name, line_num, value_str);
-         break;
-
-      case CMOS_OP_RESERVED:
-         fprintf(stderr, "%s: Error on line %d of input file: Can not modify "
-                 "reserved coreboot parameter %s.", prog_name, line_num,
-                 e->name);
-         break;
-
-      case CMOS_OP_VALUE_TOO_WIDE:
-         fprintf(stderr, "%s: Error on line %d of input file: Can not write "
-                 "value %s to CMOS parameter %s that is only %d bits wide.",
-                 prog_name, line_num, value_str, e->name, e->length);
-         break;
-
-      case CMOS_OP_NO_MATCHING_ENUM:
-         fprintf(stderr, "%s: coreboot parameter %s has no matching enums.",
-                 prog_name, e->name);
-         break;
-
-      case CMOS_AREA_OUT_OF_RANGE:
-         fprintf(stderr, "%s: The CMOS area specified by the layout info for "
-                 "coreboot parameter %s is out of range.", prog_name,
-                 e->name);
-         break;
-
-      case CMOS_AREA_OVERLAPS_RTC:
-         fprintf(stderr, "%s: The CMOS area specified by the layout info for "
-                 "coreboot parameter %s overlaps the realtime clock area.",
-                 prog_name, e->name);
-         break;
-
-      case CMOS_AREA_TOO_WIDE:
-         fprintf(stderr, "%s: The CMOS area specified by the layout info for "
-                 "coreboot parameter %s is too wide.",
-                 prog_name, e->name);
-         break;
-
-      default:
-         fprintf(stderr,
-                 "%s: Unknown error encountered while attempting to modify "
-                 "coreboot parameter %s.", prog_name, e->name);
-         break;
-    }
-
-   fprintf(stderr, "  No CMOS writes performed.\n");
-   exit(1);
-   return 0;  /* keep compiler happy */
- }
+static unsigned long long try_prepare_cmos_write(const cmos_entry_t * e,
+                                                const char value_str[])
+{
+       unsigned long long value;
+
+       switch (prepare_cmos_write(e, value_str, &value)) {
+       case OK:
+               return value;
+
+       case CMOS_OP_BAD_ENUM_VALUE:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: Bad value for "
+                       "parameter %s.", prog_name, line_num, e->name);
+               break;
+
+       case CMOS_OP_NEGATIVE_INT:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: This program "
+                       "does not support assignment of negative numbers to "
+                       "coreboot parameters.", prog_name, line_num);
+               break;
+
+       case CMOS_OP_INVALID_INT:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: %s is not a "
+                       "valid integer.", prog_name, line_num, value_str);
+               break;
+
+       case CMOS_OP_RESERVED:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: Can not modify "
+                       "reserved coreboot parameter %s.", prog_name, line_num,
+                       e->name);
+               break;
+
+       case CMOS_OP_VALUE_TOO_WIDE:
+               fprintf(stderr,
+                       "%s: Error on line %d of input file: Can not write "
+                       "value %s to CMOS parameter %s that is only %d bits wide.",
+                       prog_name, line_num, value_str, e->name, e->length);
+               break;
+
+       case CMOS_OP_NO_MATCHING_ENUM:
+               fprintf(stderr,
+                       "%s: coreboot parameter %s has no matching enums.",
+                       prog_name, e->name);
+               break;
+
+       case CMOS_AREA_OUT_OF_RANGE:
+               fprintf(stderr,
+                       "%s: The CMOS area specified by the layout info for "
+                       "coreboot parameter %s is out of range.", prog_name,
+                       e->name);
+               break;
+
+       case CMOS_AREA_OVERLAPS_RTC:
+               fprintf(stderr,
+                       "%s: The CMOS area specified by the layout info for "
+                       "coreboot parameter %s overlaps the realtime clock area.",
+                       prog_name, e->name);
+               break;
+
+       case CMOS_AREA_TOO_WIDE:
+               fprintf(stderr,
+                       "%s: The CMOS area specified by the layout info for "
+                       "coreboot parameter %s is too wide.", prog_name,
+                       e->name);
+               break;
+
+       default:
+               fprintf(stderr,
+                       "%s: Unknown error encountered while attempting to modify "
+                       "coreboot parameter %s.", prog_name, e->name);
+               break;
+       }
+
+       fprintf(stderr, "  No CMOS writes performed.\n");
+       exit(1);
+       return 0;               /* keep compiler happy */
+}