"Fossies" - the Fresh Open Source Software Archive

Member "chrony-3.5/cmdparse.c" (10 May 2019, 8252 Bytes) of package /linux/misc/chrony-3.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "cmdparse.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3_vs_3.4.

    1 /*
    2   chronyd/chronyc - Programs for keeping computer clocks accurate.
    3 
    4  **********************************************************************
    5  * Copyright (C) Richard P. Curnow  1997-2003
    6  * Copyright (C) Miroslav Lichvar  2013-2014, 2016
    7  * 
    8  * This program is free software; you can redistribute it and/or modify
    9  * it under the terms of version 2 of the GNU General Public License as
   10  * published by the Free Software Foundation.
   11  * 
   12  * This program is distributed in the hope that it will be useful, but
   13  * WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   15  * General Public License for more details.
   16  * 
   17  * You should have received a copy of the GNU General Public License along
   18  * with this program; if not, write to the Free Software Foundation, Inc.,
   19  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   20  * 
   21  **********************************************************************
   22 
   23   =======================================================================
   24   
   25   Module for parsing various forms of directive and command lines that
   26   are common to the configuration file and to the command client.
   27 
   28   */
   29 
   30 #include "config.h"
   31 
   32 #include "sysincl.h"
   33 
   34 #include "cmdparse.h"
   35 #include "memory.h"
   36 #include "nameserv.h"
   37 #include "ntp.h"
   38 #include "util.h"
   39 
   40 /* ================================================== */
   41 
   42 int
   43 CPS_ParseNTPSourceAdd(char *line, CPS_NTP_Source *src)
   44 {
   45   char *hostname, *cmd;
   46   int n;
   47   
   48   src->port = SRC_DEFAULT_PORT;
   49   src->params.minpoll = SRC_DEFAULT_MINPOLL;
   50   src->params.maxpoll = SRC_DEFAULT_MAXPOLL;
   51   src->params.connectivity = SRC_ONLINE;
   52   src->params.auto_offline = 0;
   53   src->params.presend_minpoll = SRC_DEFAULT_PRESEND_MINPOLL;
   54   src->params.burst = 0;
   55   src->params.iburst = 0;
   56   src->params.min_stratum = SRC_DEFAULT_MINSTRATUM;
   57   src->params.poll_target = SRC_DEFAULT_POLLTARGET;
   58   src->params.version = 0;
   59   src->params.max_sources = SRC_DEFAULT_MAXSOURCES;
   60   src->params.min_samples = SRC_DEFAULT_MINSAMPLES;
   61   src->params.max_samples = SRC_DEFAULT_MAXSAMPLES;
   62   src->params.filter_length = 0;
   63   src->params.interleaved = 0;
   64   src->params.sel_options = 0;
   65   src->params.authkey = INACTIVE_AUTHKEY;
   66   src->params.max_delay = SRC_DEFAULT_MAXDELAY;
   67   src->params.max_delay_ratio = SRC_DEFAULT_MAXDELAYRATIO;
   68   src->params.max_delay_dev_ratio = SRC_DEFAULT_MAXDELAYDEVRATIO;
   69   src->params.min_delay = 0.0;
   70   src->params.asymmetry = SRC_DEFAULT_ASYMMETRY;
   71   src->params.offset = 0.0;
   72 
   73   hostname = line;
   74   line = CPS_SplitWord(line);
   75 
   76   if (!*hostname)
   77     return 0;
   78 
   79   src->name = hostname;
   80 
   81   /* Parse options */
   82   for (; *line; line += n) {
   83     cmd = line;
   84     line = CPS_SplitWord(line);
   85     n = 0;
   86 
   87     if (!strcasecmp(cmd, "auto_offline")) {
   88       src->params.auto_offline = 1;
   89     } else if (!strcasecmp(cmd, "burst")) {
   90       src->params.burst = 1;
   91     } else if (!strcasecmp(cmd, "iburst")) {
   92       src->params.iburst = 1;
   93     } else if (!strcasecmp(cmd, "offline")) {
   94       src->params.connectivity = SRC_OFFLINE;
   95     } else if (!strcasecmp(cmd, "noselect")) {
   96       src->params.sel_options |= SRC_SELECT_NOSELECT;
   97     } else if (!strcasecmp(cmd, "prefer")) {
   98       src->params.sel_options |= SRC_SELECT_PREFER;
   99     } else if (!strcasecmp(cmd, "require")) {
  100       src->params.sel_options |= SRC_SELECT_REQUIRE;
  101     } else if (!strcasecmp(cmd, "trust")) {
  102       src->params.sel_options |= SRC_SELECT_TRUST;
  103     } else if (!strcasecmp(cmd, "key")) {
  104       if (sscanf(line, "%"SCNu32"%n", &src->params.authkey, &n) != 1 ||
  105           src->params.authkey == INACTIVE_AUTHKEY)
  106         return 0;
  107     } else if (!strcasecmp(cmd, "asymmetry")) {
  108       if (sscanf(line, "%lf%n", &src->params.asymmetry, &n) != 1)
  109         return 0;
  110     } else if (!strcasecmp(cmd, "filter")) {
  111       if (sscanf(line, "%d%n", &src->params.filter_length, &n) != 1)
  112         return 0;
  113     } else if (!strcasecmp(cmd, "maxdelay")) {
  114       if (sscanf(line, "%lf%n", &src->params.max_delay, &n) != 1)
  115         return 0;
  116     } else if (!strcasecmp(cmd, "maxdelayratio")) {
  117       if (sscanf(line, "%lf%n", &src->params.max_delay_ratio, &n) != 1)
  118         return 0;
  119     } else if (!strcasecmp(cmd, "maxdelaydevratio")) {
  120       if (sscanf(line, "%lf%n", &src->params.max_delay_dev_ratio, &n) != 1)
  121         return 0;
  122     } else if (!strcasecmp(cmd, "maxpoll")) {
  123       if (sscanf(line, "%d%n", &src->params.maxpoll, &n) != 1)
  124         return 0;
  125     } else if (!strcasecmp(cmd, "maxsamples")) {
  126       if (sscanf(line, "%d%n", &src->params.max_samples, &n) != 1)
  127         return 0;
  128     } else if (!strcasecmp(cmd, "maxsources")) {
  129       if (sscanf(line, "%d%n", &src->params.max_sources, &n) != 1)
  130         return 0;
  131     } else if (!strcasecmp(cmd, "mindelay")) {
  132       if (sscanf(line, "%lf%n", &src->params.min_delay, &n) != 1)
  133         return 0;
  134     } else if (!strcasecmp(cmd, "minpoll")) {
  135       if (sscanf(line, "%d%n", &src->params.minpoll, &n) != 1)
  136         return 0;
  137     } else if (!strcasecmp(cmd, "minsamples")) {
  138       if (sscanf(line, "%d%n", &src->params.min_samples, &n) != 1)
  139         return 0;
  140     } else if (!strcasecmp(cmd, "minstratum")) {
  141       if (sscanf(line, "%d%n", &src->params.min_stratum, &n) != 1)
  142         return 0;
  143     } else if (!strcasecmp(cmd, "offset")) {
  144       if (sscanf(line, "%lf%n", &src->params.offset, &n) != 1)
  145         return 0;
  146     } else if (!strcasecmp(cmd, "port")) {
  147       if (sscanf(line, "%hu%n", &src->port, &n) != 1)
  148         return 0;
  149     } else if (!strcasecmp(cmd, "polltarget")) {
  150       if (sscanf(line, "%d%n", &src->params.poll_target, &n) != 1)
  151         return 0;
  152     } else if (!strcasecmp(cmd, "presend")) {
  153       if (sscanf(line, "%d%n", &src->params.presend_minpoll, &n) != 1)
  154         return 0;
  155     } else if (!strcasecmp(cmd, "version")) {
  156       if (sscanf(line, "%d%n", &src->params.version, &n) != 1)
  157         return 0;
  158     } else if (!strcasecmp(cmd, "xleave")) {
  159       src->params.interleaved = 1;
  160     } else {
  161       return 0;
  162     }
  163   }
  164 
  165   return 1;
  166 }
  167 
  168 /* ================================================== */
  169 
  170 int
  171 CPS_ParseLocal(char *line, int *stratum, int *orphan, double *distance)
  172 {
  173   int n;
  174   char *cmd;
  175 
  176   *stratum = 10;
  177   *distance = 1.0;
  178   *orphan = 0;
  179 
  180   while (*line) {
  181     cmd = line;
  182     line = CPS_SplitWord(line);
  183 
  184     if (!strcasecmp(cmd, "stratum")) {
  185       if (sscanf(line, "%d%n", stratum, &n) != 1 ||
  186           *stratum >= NTP_MAX_STRATUM || *stratum <= 0)
  187         return 0;
  188     } else if (!strcasecmp(cmd, "orphan")) {
  189       *orphan = 1;
  190       n = 0;
  191     } else if (!strcasecmp(cmd, "distance")) {
  192       if (sscanf(line, "%lf%n", distance, &n) != 1)
  193         return 0;
  194     } else {
  195       return 0;
  196     }
  197 
  198     line += n;
  199   }
  200 
  201   return 1;
  202 }
  203 
  204 /* ================================================== */
  205 
  206 void
  207 CPS_NormalizeLine(char *line)
  208 {
  209   char *p, *q;
  210   int space = 1, first = 1;
  211 
  212   /* Remove white-space at beginning and replace white-spaces with space char */
  213   for (p = q = line; *p; p++) {
  214     if (isspace((unsigned char)*p)) {
  215       if (!space)
  216         *q++ = ' ';
  217       space = 1;
  218       continue;
  219     }
  220 
  221     /* Discard comment lines */
  222     if (first && strchr("!;#%", *p))
  223       break;
  224 
  225     *q++ = *p;
  226     space = first = 0;
  227   }
  228 
  229   /* Strip trailing space */
  230   if (q > line && q[-1] == ' ')
  231     q--;
  232 
  233   *q = '\0';
  234 }
  235 
  236 /* ================================================== */
  237 
  238 char *
  239 CPS_SplitWord(char *line)
  240 {
  241   char *p = line, *q = line;
  242 
  243   /* Skip white-space before the word */
  244   while (*q && isspace((unsigned char)*q))
  245     q++;
  246 
  247   /* Move the word to the beginning */
  248   while (*q && !isspace((unsigned char)*q))
  249     *p++ = *q++;
  250 
  251   /* Find the next word */
  252   while (*q && isspace((unsigned char)*q))
  253     q++;
  254 
  255   *p = '\0';
  256 
  257   /* Return pointer to the next word or NUL */
  258   return q;
  259 }
  260 
  261 /* ================================================== */
  262 
  263 int
  264 CPS_ParseKey(char *line, uint32_t *id, const char **hash, char **key)
  265 {
  266   char *s1, *s2, *s3, *s4;
  267 
  268   s1 = line;
  269   s2 = CPS_SplitWord(s1);
  270   s3 = CPS_SplitWord(s2);
  271   s4 = CPS_SplitWord(s3);
  272 
  273   /* Require two or three words */
  274   if (!*s2 || *s4)
  275     return 0;
  276 
  277   if (sscanf(s1, "%"SCNu32, id) != 1)
  278     return 0;
  279 
  280   if (*s3) {
  281     *hash = s2;
  282     *key = s3;
  283   } else {
  284     *hash = "MD5";
  285     *key = s2;
  286   }
  287 
  288   return 1;
  289 }