Move some various utility functions used by the config parser into
util.c

The main reason for this is to help extend phc2sys to read a
configuration file as well. It is easier to only use the generic
functions, as phc2sys does not use the exact same configuration settings
or structure as ptp4l.

Signed-off-by: Jacob Keller <jacob.e.kel...@intel.com>
---
 config.c | 65 ----------------------------------------------------------------
 util.c   | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 util.h   | 42 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 102 insertions(+), 65 deletions(-)

diff --git a/config.c b/config.c
index ec8c53801af0..7e783f3f7f88 100644
--- a/config.c
+++ b/config.c
@@ -27,30 +27,6 @@
 #include "print.h"
 #include "util.h"
 
-enum config_section {
-       GLOBAL_SECTION,
-       PORT_SECTION,
-       UNKNOWN_SECTION,
-};
-
-static enum parser_result parse_section_line(char *s, enum config_section 
*section)
-{
-       if (!strcasecmp(s, "[global]")) {
-               *section = GLOBAL_SECTION;
-       } else if (s[0] == '[') {
-               char c;
-               *section = PORT_SECTION;
-               /* Replace square brackets with white space. */
-               while (0 != (c = *s)) {
-                       if (c == '[' || c == ']')
-                               *s = ' ';
-                       s++;
-               }
-       } else
-               return NOT_PARSED;
-       return PARSED_OK;
-}
-
 static enum parser_result parse_pod_setting(const char *option,
                                            const char *value,
                                            struct port_defaults *pod)
@@ -592,47 +568,6 @@ static enum parser_result parse_global_setting(const char 
*option,
        return PARSED_OK;
 }
 
-static enum parser_result parse_setting_line(char *line,
-                                            const char **option,
-                                            const char **value)
-{
-       *option = line;
-
-       while (!isspace(line[0])) {
-               if (line[0] == '\0')
-                       return NOT_PARSED;
-               line++;
-       }
-
-       while (isspace(line[0])) {
-               line[0] = '\0';
-               line++;
-       }
-
-       *value = line;
-
-       return PARSED_OK;
-}
-
-static void check_deprecated_options(const char **option)
-{
-       const char *new_option = NULL;
-
-       if (!strcmp(*option, "pi_offset_const")) {
-               new_option = "step_threshold";
-       } else if (!strcmp(*option, "pi_f_offset_const")) {
-               new_option = "first_step_threshold";
-       } else if (!strcmp(*option, "pi_max_frequency")) {
-               new_option = "max_frequency";
-       }
-
-       if (new_option) {
-               fprintf(stderr, "option %s is deprecated, please use %s 
instead\n",
-                               *option, new_option);
-               *option = new_option;
-       }
-}
-
 int config_read(char *name, struct config *cfg)
 {
        enum config_section current_section = UNKNOWN_SECTION;
diff --git a/util.c b/util.c
index 06c3296cf189..861d1269bdd9 100644
--- a/util.c
+++ b/util.c
@@ -22,6 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 
 #include "address.h"
 #include "print.h"
@@ -216,6 +217,65 @@ int leap_second_status(uint64_t ts, int leap_set, int 
*leap, int *utc_offset)
        return leap_status;
 }
 
+enum parser_result parse_section_line(char *s, enum config_section *section)
+{
+       if (!strcasecmp(s, "[global]")) {
+               *section = GLOBAL_SECTION;
+       } else if (s[0] == '[') {
+               char c;
+               *section = PORT_SECTION;
+               /* Replace square brackets with white space. */
+               while (0 != (c = *s)) {
+                       if (c == '[' || c == ']')
+                               *s = ' ';
+                       s++;
+               }
+       } else
+               return NOT_PARSED;
+       return PARSED_OK;
+}
+
+enum parser_result parse_setting_line(char *line,
+                                     const char **option,
+                                     const char **value)
+{
+       *option = line;
+
+       while (!isspace(line[0])) {
+               if (line[0] == '\0')
+                       return NOT_PARSED;
+               line++;
+       }
+
+       while (isspace(line[0])) {
+               line[0] = '\0';
+               line++;
+       }
+
+       *value = line;
+
+       return PARSED_OK;
+}
+
+void check_deprecated_options(const char **option)
+{
+       const char *new_option = NULL;
+
+       if (!strcmp(*option, "pi_offset_const")) {
+               new_option = "step_threshold";
+       } else if (!strcmp(*option, "pi_f_offset_const")) {
+               new_option = "first_step_threshold";
+       } else if (!strcmp(*option, "pi_max_frequency")) {
+               new_option = "max_frequency";
+       }
+
+       if (new_option) {
+               fprintf(stderr, "option %s is deprecated, please use %s 
instead\n",
+                               *option, new_option);
+               *option = new_option;
+       }
+}
+
 enum parser_result get_ranged_int(const char *str_val, int *result,
                                  int min, int max)
 {
diff --git a/util.h b/util.h
index 98b395bce329..3f2863899b3d 100644
--- a/util.h
+++ b/util.h
@@ -136,6 +136,48 @@ enum parser_result {
 };
 
 /**
+ * Configuration sections used in the configuration file
+ */
+enum config_section {
+       GLOBAL_SECTION,
+       PORT_SECTION,
+       UNKNOWN_SECTION,
+};
+
+/**
+ * Parse config line for section heading
+ *
+ * @param s         String value line from configuration file
+ * @param section   the current section, modified if line is a section header
+ *
+ * @return          PARSED_OK on successful section found, otherwise returns
+ *                  NOT_PARSED for any non section line.
+ */
+enum parser_result parse_section_line(char *s, enum config_section *section);
+
+/**
+ * Parse a configuration line into option and value
+ *
+ * @line            String value line from configuration file
+ * @option          Contains option on return
+ * @value           Contains option value on return
+ *
+ * @return          PARSE_OK if line can be split into option and value,
+ *                  otherwise returns NOT_PARSED
+ */
+enum parser_result parse_setting_line(char *line,
+                                     const char **option,
+                                     const char **value);
+
+/**
+ * Convert deprecated option into new value, and warn user
+ *
+ * @option          Converted to new option name on return if matched a
+ *                  deprecated option
+ */
+void check_deprecated_options(const char **option);
+
+/**
  * Get an integer value from string with error checking and range
  * specification.
  *
-- 
2.1.2.555.gfbecd99


------------------------------------------------------------------------------
Dive into the World of Parallel Programming! The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net
_______________________________________________
Linuxptp-devel mailing list
Linuxptp-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel

Reply via email to