示例#1
0
int
main(int argc, const char *const argv[]) {
  lagopus_result_t n_tokens;
  char *tokens[1024];
  ssize_t i;

  const char *a =
    "\\\\"
    "'that\\'s what I want.'"
    "\"that's what I want, too\""
    "\"who is the \\\"bad\\\"\""
    ;
  char *x;
  char *y;
  lagopus_result_t len;

  (void)argc;
  (void)argv;

  x = strdup(a);
  n_tokens = lagopus_str_tokenize_quote(x, tokens, 1024, "\t\r\n ", "\"'");
  if (n_tokens > 0) {

    for (i = 0; i < n_tokens; i++) {
      fprintf(stderr, "<%s>\n", tokens[i]);
    }

    fprintf(stderr, "\n");

    for (i = 0; i < n_tokens; i++) {
      if ((len = lagopus_str_unescape(tokens[i], "\"'", &y)) >= 0) {
        fprintf(stderr, "[%s]\n", y);
        free((void *)y);
      } else {
        lagopus_perror(len);
      }
    }

  } else {
    lagopus_perror(n_tokens);
  }

  fprintf(stderr, "\n");

  x = strdup(a);
  n_tokens = lagopus_str_tokenize(x, tokens, 1024, "\t\r\n ");
  if (n_tokens > 0) {
    for (i = 0; i < n_tokens; i++) {
      fprintf(stderr, "<%s>\n", tokens[i]);
    }
  } else {
    lagopus_perror(n_tokens);
  }

  if ((len = lagopus_str_unescape("\"aaa nnn\"", "\"'", &y)) >= 0) {
    fprintf(stderr, "(%s)\n", y);
    free((void *)y);
  } else {
    lagopus_perror(len);
  }

  return 0;
}
示例#2
0
static inline lagopus_result_t
s_parse_load(datastore_interp_t *iptr,
             datastore_interp_state_t state,
             size_t argc, const char *const argv[],
             lagopus_hashmap_t *hptr,
             datastore_update_proc_t u_proc,
             datastore_enable_proc_t e_proc,
             datastore_serialize_proc_t s_proc,
             datastore_destroy_proc_t d_proc,
             lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  size_t i;
  const char *cname = NULL;
  char *filepath = NULL;

  (void)state;
  (void)argc;
  (void)hptr;
  (void)u_proc;
  (void)e_proc;
  (void)s_proc;
  (void)d_proc;

  for (i = 0; i < argc; i++) {
    lagopus_msg_debug(1, "argv[" PFSZS(4, u) "]:\t'%s'\n", i, argv[i]);
  }

  argv++;

  if (IS_VALID_STRING(*argv) == false) {
    ret = datastore_json_result_set(result,
                                    LAGOPUS_RESULT_OK,
                                    NULL);
  } else {
    if (IS_VALID_STRING(*argv) == true) {
      if ((ret = lagopus_str_unescape(*argv, "\"'", &filepath)) > 0) {
        if (s_is_readable(filepath) == LAGOPUS_RESULT_OK) {
          ret = datastore_interp_get_current_configurater(iptr, &cname);
          if (ret == LAGOPUS_RESULT_OK && IS_VALID_STRING(cname) == true) {
            ret = datastore_interp_load_file(iptr, cname, filepath, result);
            if (ret == LAGOPUS_RESULT_OK) {
              ret = datastore_json_result_set(result, ret, NULL);
            }
          } else {
            ret = datastore_json_result_string_setf(result,
                                                    ret,
                                                    CMD_ERR_MSG_LOAD_FAILED,
                                                    "internal error.");
          }
        } else {
          ret = datastore_json_result_string_setf(result,
                                                  LAGOPUS_RESULT_INVALID_ARGS,
                                                  CMD_ERR_MSG_UNREADABLE,
                                                  filepath);
        }
      } else {
        ret = datastore_json_result_string_setf(result,
                                                LAGOPUS_RESULT_INVALID_ARGS,
                                                CMD_ERR_MSG_INVALID_OPT_VALUE,
                                                *argv);
      }
    } else {
      ret = datastore_json_result_string_setf(result,
                                              LAGOPUS_RESULT_INVALID_ARGS,
                                              CMD_ERR_MSG_INVALID_OPT,
                                              *argv);
    }
  }

  free((void *) filepath);
  return ret;
}
示例#3
0
STATIC lagopus_result_t
flow_cmd_parse(datastore_interp_t *iptr,
               datastore_interp_state_t state,
               size_t argc, const char *const argv[],
               lagopus_hashmap_t *hptr,
               datastore_update_proc_t u_proc,
               datastore_enable_proc_t e_proc,
               datastore_serialize_proc_t s_proc,
               datastore_destroy_proc_t d_proc,
               lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  lagopus_result_t ret_for_json = LAGOPUS_RESULT_ANY_FAILURES;
  size_t i;
  void *sub_cmd_proc;
  char *name = NULL;
  char *fullname = NULL;
  char *str = NULL;
  flow_conf_t conf = {NULL, OFPTT_ALL};
  configs_t out_configs = {false, false};
  lagopus_dstring_t conf_result = NULL;
  (void) u_proc;
  (void) e_proc;
  (void) s_proc;
  (void) d_proc;

  for (i = 0; i < argc; i++) {
    lagopus_msg_debug(1, "argv[" PFSZS(4, u) "]:\t'%s'\n", i, argv[i]);
  }

  if (iptr != NULL && argv != NULL && result != NULL) {
    if ((ret = lagopus_dstring_create(&conf_result)) == LAGOPUS_RESULT_OK) {
      argv++;

      if (IS_VALID_STRING(*argv) == true) {
        if ((ret = lagopus_hashmap_find(
                     &sub_cmd_not_name_table,
                     (void *)(*argv),
                     &sub_cmd_proc)) == LAGOPUS_RESULT_OK) {
          /* parse sub cmd. */
          if (sub_cmd_proc != NULL) {
            ret_for_json =
              ((sub_cmd_proc_t) sub_cmd_proc)(iptr, state,
                                              argc, argv,
                                              NULL, hptr,
                                              u_proc,
                                              (void *) &out_configs,
                                              result);
          }
        }

        if (ret == LAGOPUS_RESULT_NOT_FOUND &&
            ret_for_json != LAGOPUS_RESULT_OK) {
          if ((ret = lagopus_str_unescape(*argv, "\"'", &name)) < 0) {
            goto done;
          } else {
            argv++;

            if ((ret = namespace_get_fullname(name, &fullname))
                == LAGOPUS_RESULT_OK) {
              if (IS_VALID_STRING(*argv) == true) {
                if ((ret = lagopus_hashmap_find(
                             &sub_cmd_table,
                             (void *)(*argv),
                             &sub_cmd_proc)) == LAGOPUS_RESULT_OK) {
                  /* parse sub cmd. */
                  if (sub_cmd_proc != NULL) {
                    ret_for_json =
                      ((sub_cmd_proc_t) sub_cmd_proc)(iptr, state,
                                                      argc, argv,
                                                      fullname, hptr,
                                                      u_proc,
                                                      (void *) &out_configs,
                                                      result);
                  } else {
                    ret = LAGOPUS_RESULT_NOT_FOUND;
                    lagopus_perror(ret);
                    goto done;
                  }
                } else if (ret == LAGOPUS_RESULT_NOT_FOUND) {
                  /* parse dump cmd. */
                  ret_for_json = dump_sub_cmd_parse(iptr, argv, fullname, &conf,
                                                    &out_configs, result);
                } else {
                  ret_for_json = datastore_json_result_string_setf(
                                   result, LAGOPUS_RESULT_INVALID_ARGS,
                                   "sub_cmd = %s.", *argv);
                }
              } else {
                /* parse dump cmd. */
                ret_for_json = dump_sub_cmd_parse(iptr, argv, fullname, &conf,
                                                  &out_configs, result);
              }
            } else {
              ret_for_json = datastore_json_result_string_setf(
                               result, ret, "Can't get fullname %s.", name);
            }
          }
        }
      } else {
        /* parse dump all cmd. */
        ret_for_json = dump_sub_cmd_parse(iptr, argv, NULL, &conf,
                                          &out_configs, result);
      }

      /* create jsno for LAGOPUS_RESULT_OK. */
      if (ret_for_json == LAGOPUS_RESULT_OK &&
          out_configs.is_dump == false) {
        ret = lagopus_dstring_str_get(result, &str);
        if (ret != LAGOPUS_RESULT_OK) {
          goto done;
        }
        if (IS_VALID_STRING(str) == true) {
          ret = datastore_json_result_set(result, LAGOPUS_RESULT_OK, str);
        } else {
          ret = datastore_json_result_set(result, LAGOPUS_RESULT_OK, NULL);
        }
      } else {
        ret = ret_for_json;
      }
    }

  done:
    /* free. */
    free(name);
    free(fullname);
    free(str);
    lagopus_dstring_destroy(&conf_result);
  } else {
    ret = LAGOPUS_RESULT_INVALID_ARGS;
  }

  return ret;
}
示例#4
0
static inline lagopus_result_t
s_parse_snmp_internal(const char *const argv[],
                      lagopus_dstring_t *result) {
  lagopus_result_t ret = LAGOPUS_RESULT_ANY_FAILURES;
  char *agentx_sock = NULL;
  char *esc_agentx = NULL;
  char *ping = NULL;

  while (IS_VALID_STRING(*argv) == true) {
    if (strcmp(*argv, "-master-agentx-socket") == 0) {
      if (IS_VALID_STRING(*(argv + 1)) == true) {
        argv++;
        if ((ret = lagopus_str_unescape(*argv, "\"'", &agentx_sock))
            > 0) {

          if ((ret = lagopus_snmp_set_agentx_sock(agentx_sock))
              != LAGOPUS_RESULT_OK) {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't set %s.",
                                                    agentx_sock);
            goto done;
          }

        } else if (ret == 0) {
          ret = datastore_json_result_string_setf(result,
                                                  LAGOPUS_RESULT_INVALID_ARGS,
                                                  "Bad opt val = %s ",
                                                  *argv);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't convert to an "
                                                  "unescaped strings.");
          goto done;
        }
      } else {

        if ((ret = lagopus_snmp_get_agentx_sock(&agentx_sock))
            == LAGOPUS_RESULT_OK) {
          ret = datastore_json_string_escape(agentx_sock,
                                             (char **)&esc_agentx);
          if (ret == LAGOPUS_RESULT_OK) {
            ret = datastore_json_result_setf(result, ret, "[{"
                                             "\"master-agentx-socket\":\"%s\""
                                             "}]", esc_agentx);
            goto done;
          } else {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't convert to "
                                                    "an escaped strings.");
            goto done;
          }
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't get "
                                                  "master-agentx-socket.");
          goto done;
        }
      }

    } else if (strcmp(*argv, "-ping-interval-second") == 0) {

      if (IS_VALID_STRING(*(argv + 1)) == true) {
        argv++;
        ret = lagopus_str_unescape(*argv, "\"'", &ping);
        if (ret > 0) {
          uint16_t new_ping;
          ret = lagopus_str_parse_uint16(ping, &new_ping);
          if (ret == LAGOPUS_RESULT_OK) {
            if ((ret = lagopus_snmp_set_ping_interval(new_ping))
                != LAGOPUS_RESULT_OK) {
              ret = datastore_json_result_string_setf(result, ret,
                                                      "Can't set "
                                                      "ping-interval.");

              goto done;
            }
          } else {
            ret = datastore_json_result_string_setf(result, ret,
                                                    "Can't parse string "
                                                    "to uint16_t.");
            goto done;
          }
        } else if (ret == 0) {
          ret = datastore_json_result_string_setf(result,
                                                  LAGOPUS_RESULT_INVALID_ARGS,
                                                  "Bad opt val = %s ",
                                                  *argv);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't convert to an "
                                                  "unescaped strings.");
          goto done;
        }
      } else {
        uint16_t ping_interval;
        ret = lagopus_snmp_get_ping_interval(&ping_interval);
        if (ret == LAGOPUS_RESULT_OK) {
          ret = datastore_json_result_setf(result, ret, "[{"
                                           "\"ping-interval-second\":%d"
                                           "}]", ping_interval);
          goto done;
        } else {
          ret = datastore_json_result_string_setf(result, ret,
                                                  "Can't get "
                                                  "ping-interval-second.");
          goto done;
        }
      }

    } else if (strcmp(*argv, "enable") == 0) {
      if ((ret = lagopus_snmp_set_enable(true))
          != LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result, ret,
                                                "Can't set enable.");
        goto done;
      }
    } else {
      char *esc_str = NULL;
      ret = datastore_json_string_escape(*argv, (char **)&esc_str);
      if (ret == LAGOPUS_RESULT_OK) {
        ret = datastore_json_result_string_setf(result, LAGOPUS_RESULT_NOT_FOUND,
                                                "option = %s",
                                                esc_str);
      }
      free((void *)esc_str);
    }

 done:
    free((void *)agentx_sock);
    free((void *)esc_agentx);
    free((void *)ping);
    agentx_sock = NULL;
    esc_agentx = NULL;
    ping = NULL;

    if (ret != LAGOPUS_RESULT_OK) {
      break;
    }
    argv++;
  }

  if (ret == LAGOPUS_RESULT_OK &&
      lagopus_dstring_empty(result)) {
    ret = lagopus_dstring_appendf(result, "{\"ret\":\"OK\"}");
    if (ret != LAGOPUS_RESULT_OK) {
      ret = datastore_json_result_string_setf(result, ret,
                                              "Can't add strings "
                                              "to dstring.");
    }
  }

  return ret;
}