alias.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @file alias.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * alias(string target)
  33. *
  34. * Variables and objects:
  35. * - empty name - resolves target
  36. * - nonempty name N - resolves target.N
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stdint.h>
  41. #include <limits.h>
  42. #include <misc/debug.h>
  43. #include <misc/balloc.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_alias.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct instance {
  48. NCDModuleInst *i;
  49. int num_extra_parts;
  50. char strings[];
  51. };
  52. static int split_string_inplace (char *str, char del)
  53. {
  54. ASSERT(str)
  55. int num_extra_parts = 0;
  56. while (*str) {
  57. if (*str == del) {
  58. if (num_extra_parts == INT_MAX) {
  59. return -1;
  60. }
  61. *str = '\0';
  62. num_extra_parts++;
  63. }
  64. str++;
  65. }
  66. return num_extra_parts;
  67. }
  68. static void func_new (NCDModuleInst *i)
  69. {
  70. // read arguments
  71. NCDValue *target_arg;
  72. if (!NCDValue_ListRead(i->args, 1, &target_arg)) {
  73. ModuleLog(i, BLOG_ERROR, "wrong arity");
  74. goto fail0;
  75. }
  76. if (!NCDValue_IsStringNoNulls(target_arg)) {
  77. ModuleLog(i, BLOG_ERROR, "wrong type");
  78. goto fail0;
  79. }
  80. const char *target = NCDValue_StringValue(target_arg);
  81. size_t target_len = strlen(target);
  82. // calculate size
  83. bsize_t size = bsize_add(bsize_fromsize(sizeof(struct instance)), bsize_fromsize(target_len + 1));
  84. // allocate instance
  85. struct instance *o = BAllocSize(size);
  86. if (!o) {
  87. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  88. goto fail0;
  89. }
  90. o->i = i;
  91. NCDModuleInst_Backend_SetUser(i, o);
  92. // copy target
  93. memcpy(o->strings, target, target_len + 1);
  94. // split target into components
  95. if ((o->num_extra_parts = split_string_inplace(o->strings, '.')) < 0) {
  96. ModuleLog(o->i, BLOG_ERROR, "split_string_inplace failed");
  97. goto fail1;
  98. }
  99. // signal up
  100. NCDModuleInst_Backend_Up(o->i);
  101. return;
  102. fail1:
  103. BFree(o);
  104. fail0:
  105. NCDModuleInst_Backend_SetError(i);
  106. NCDModuleInst_Backend_Dead(i);
  107. }
  108. static void func_die (void *vo)
  109. {
  110. struct instance *o = vo;
  111. NCDModuleInst *i = o->i;
  112. // free instance
  113. BFree(o);
  114. NCDModuleInst_Backend_Dead(i);
  115. }
  116. static int func_getobj (void *vo, const char *name, NCDObject *out_object)
  117. {
  118. struct instance *o = vo;
  119. NCDObject object;
  120. if (!NCDModuleInst_Backend_GetObj(o->i, o->strings, &object)) {
  121. return 0;
  122. }
  123. NCDObject obj2;
  124. if (!NCDObject_ResolveObjExprCompact(&object, o->strings + strlen(o->strings) + 1, o->num_extra_parts, &obj2)) {
  125. return 0;
  126. }
  127. if (!strcmp(name, "")) {
  128. *out_object = obj2;
  129. return 1;
  130. }
  131. return NCDObject_GetObj(&obj2, name, out_object);
  132. }
  133. static const struct NCDModule modules[] = {
  134. {
  135. .type = "alias",
  136. .func_new = func_new,
  137. .func_die = func_die,
  138. .func_getobj = func_getobj
  139. }, {
  140. .type = NULL
  141. }
  142. };
  143. const struct NCDModuleGroup ncdmodule_alias = {
  144. .modules = modules
  145. };