json_script.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef __JSON_SCRIPT_H
  17. #define __JSON_SCRIPT_H
  18. #include "avl.h"
  19. #include "blob.h"
  20. #include "blobmsg.h"
  21. #include "utils.h"
  22. struct json_script_file;
  23. struct json_script_ctx {
  24. struct avl_tree files;
  25. struct blob_buf buf;
  26. uint32_t run_seq;
  27. bool abort;
  28. /*
  29. * handle_command: handle a command that was not recognized by the
  30. * json_script core (required)
  31. *
  32. * @cmd: blobmsg container of the processed command
  33. * @vars: blobmsg container of current run variables
  34. */
  35. void (*handle_command)(struct json_script_ctx *ctx, const char *name,
  36. struct blob_attr *cmd, struct blob_attr *vars);
  37. /*
  38. * handle_expr: handle an expression that was not recognized by the
  39. * json_script core (optional)
  40. *
  41. * @expr: blobmsg container of the processed expression
  42. * @vars: blobmsg container of current runtime variables
  43. */
  44. int (*handle_expr)(struct json_script_ctx *ctx, const char *name,
  45. struct blob_attr *expr, struct blob_attr *vars);
  46. /*
  47. * handle_var - look up a variable that's not part of the runtime
  48. * variable set (optional)
  49. */
  50. const char *(*handle_var)(struct json_script_ctx *ctx, const char *name,
  51. struct blob_attr *vars);
  52. /*
  53. * handle_file - load a file by filename (optional)
  54. *
  55. * in case of wildcards, it can return a chain of json_script files
  56. * linked via the ::next pointer. Only the first json_script file is
  57. * added to the tree.
  58. */
  59. struct json_script_file *(*handle_file)(struct json_script_ctx *ctx,
  60. const char *name);
  61. /*
  62. * handle_error - handle a processing error in a command or expression
  63. * (optional)
  64. *
  65. * @msg: error message
  66. * @context: source file context of the error (blobmsg container)
  67. */
  68. void (*handle_error)(struct json_script_ctx *ctx, const char *msg,
  69. struct blob_attr *context);
  70. };
  71. struct json_script_file {
  72. struct avl_node avl;
  73. struct json_script_file *next;
  74. unsigned int seq;
  75. struct blob_attr data[];
  76. };
  77. void json_script_init(struct json_script_ctx *ctx);
  78. void json_script_free(struct json_script_ctx *ctx);
  79. /*
  80. * json_script_run - run a json script with a set of runtime variables
  81. *
  82. * @filename: initial filename to run
  83. * @vars: blobmsg container of the current runtime variables
  84. */
  85. void json_script_run(struct json_script_ctx *ctx, const char *filename,
  86. struct blob_attr *vars);
  87. void json_script_run_file(struct json_script_ctx *ctx, struct json_script_file *file,
  88. struct blob_attr *vars);
  89. /*
  90. * json_script_abort - abort current json script run
  91. *
  92. * to be called from a script context callback
  93. */
  94. static inline void
  95. json_script_abort(struct json_script_ctx *ctx)
  96. {
  97. ctx->abort = true;
  98. }
  99. /*
  100. * json_script_eval_string - evaluate a string and store the result
  101. *
  102. * Can be used to process variable references outside of a script
  103. * in a same way that they would be interpreted in the script context.
  104. */
  105. int json_script_eval_string(struct json_script_ctx *ctx, struct blob_attr *vars,
  106. struct blob_buf *buf, const char *name,
  107. const char *pattern);
  108. struct json_script_file *
  109. json_script_file_from_blobmsg(const char *name, void *data, int len);
  110. /*
  111. * json_script_find_var - helper function to find a runtime variable from
  112. * the list passed by json_script user.
  113. * It is intended to be used by the .handle_var callback
  114. */
  115. const char *json_script_find_var(struct json_script_ctx *ctx, struct blob_attr *vars,
  116. const char *name);
  117. #endif