neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

validate.c (2478B)


      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #include "nvim/api/private/defs.h"
      6 #include "nvim/api/private/helpers.h"
      7 #include "nvim/api/private/validate.h"
      8 #include "nvim/ascii_defs.h"
      9 #include "nvim/globals.h"
     10 
     11 /// Creates "Invalid …" message and sets it on `err`.
     12 void api_err_invalid(Error *err, const char *name, const char *val_s, int64_t val_n, bool quote_val)
     13 {
     14  ErrorType errtype = kErrorTypeValidation;
     15  // Treat `name` without whitespace as a parameter (surround in quotes).
     16  // Treat `name` with whitespace as a description (no quotes).
     17  char *has_space = strchr(name, ' ');
     18 
     19  // No value.
     20  if (val_s && val_s[0] == NUL) {
     21    api_set_error(err, errtype, has_space ? "Invalid %s" : "Invalid '%s'", name);
     22    return;
     23  }
     24 
     25  // Number value.
     26  if (val_s == NULL) {
     27    api_set_error(err, errtype, has_space ? "Invalid %s: %" PRId64 : "Invalid '%s': %" PRId64,
     28                  name, val_n);
     29    return;
     30  }
     31 
     32  // String value.
     33  if (has_space) {
     34    api_set_error(err, errtype, quote_val ? "Invalid %s: '%s'" : "Invalid %s: %s", name, val_s);
     35  } else {
     36    api_set_error(err, errtype, quote_val ? "Invalid '%s': '%s'" : "Invalid '%s': %s", name, val_s);
     37  }
     38 }
     39 
     40 /// Creates "Invalid …: expected …" message and sets it on `err`.
     41 void api_err_exp(Error *err, const char *name, const char *expected, const char *actual)
     42 {
     43  ErrorType errtype = kErrorTypeValidation;
     44  // Treat `name` without whitespace as a parameter (surround in quotes).
     45  // Treat `name` with whitespace as a description (no quotes).
     46  char *has_space = strchr(name, ' ');
     47 
     48  if (!actual) {
     49    api_set_error(err, errtype,
     50                  has_space ? "Invalid %s: expected %s" : "Invalid '%s': expected %s",
     51                  name, expected);
     52    return;
     53  }
     54 
     55  api_set_error(err, errtype,
     56                has_space ? "Invalid %s: expected %s, got %s" : "Invalid '%s': expected %s, got %s",
     57                name, expected, actual);
     58 }
     59 
     60 bool check_string_array(Array arr, char *name, bool disallow_nl, Error *err)
     61 {
     62  snprintf(IObuff, sizeof(IObuff), "'%s' item", name);
     63  for (size_t i = 0; i < arr.size; i++) {
     64    VALIDATE_T(IObuff, kObjectTypeString, arr.items[i].type, {
     65      return false;
     66    });
     67    // Disallow newlines in the middle of the line.
     68    if (disallow_nl) {
     69      const String l = arr.items[i].data.string;
     70      VALIDATE(!memchr(l.data, NL, l.size), "'%s' item contains newlines", name, {
     71        return false;
     72      });
     73    }
     74  }
     75  return true;
     76 }