alias.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 <ncd/static_strings.h>
  46. #include <generated/blog_channel_ncd_alias.h>
  47. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  48. #define NUM_STATIC_NAMES 4
  49. struct instance {
  50. NCDModuleInst *i;
  51. NCD_string_id_t *dynamic_names;
  52. size_t num_names;
  53. NCD_string_id_t static_names[NUM_STATIC_NAMES];
  54. };
  55. static size_t count_names (const char *str, size_t str_len)
  56. {
  57. size_t count = 1;
  58. while (str_len > 0) {
  59. if (*str == '.') {
  60. count++;
  61. }
  62. str++;
  63. str_len--;
  64. }
  65. return count;
  66. }
  67. static int add_name (struct instance *o, const char *str, size_t str_len, const char *remain, size_t remain_len)
  68. {
  69. ASSERT(str)
  70. ASSERT(!!o->dynamic_names == (o->num_names > NUM_STATIC_NAMES))
  71. NCD_string_id_t id = NCDStringIndex_GetBin(o->i->params->iparams->string_index, str, str_len);
  72. if (id < 0) {
  73. return 0;
  74. }
  75. if (o->num_names < NUM_STATIC_NAMES) {
  76. o->static_names[o->num_names++] = id;
  77. return 1;
  78. }
  79. if (o->num_names == NUM_STATIC_NAMES) {
  80. size_t num_more = (!remain ? 0 : count_names(remain, remain_len));
  81. size_t num_all = o->num_names + 1 + num_more;
  82. if (!(o->dynamic_names = BAllocArray(num_all, sizeof(o->dynamic_names[0])))) {
  83. return 0;
  84. }
  85. memcpy(o->dynamic_names, o->static_names, NUM_STATIC_NAMES * sizeof(o->dynamic_names[0]));
  86. }
  87. o->dynamic_names[o->num_names++] = id;
  88. return 1;
  89. }
  90. static int make_names (struct instance *o, const char *str, size_t str_len)
  91. {
  92. ASSERT(str)
  93. o->num_names = 0;
  94. o->dynamic_names = NULL;
  95. size_t i = 0;
  96. while (i < str_len) {
  97. if (str[i] == '.') {
  98. if (!add_name(o, str, i, str + (i + 1), str_len - (i + 1))) {
  99. goto fail;
  100. }
  101. str += i + 1;
  102. str_len -= i + 1;
  103. i = 0;
  104. continue;
  105. }
  106. i++;
  107. }
  108. if (!add_name(o, str, i, NULL, 0)) {
  109. goto fail;
  110. }
  111. return 1;
  112. fail:
  113. BFree(o->dynamic_names);
  114. return 0;
  115. }
  116. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  117. {
  118. struct instance *o = vo;
  119. o->i = i;
  120. // read arguments
  121. NCDValRef target_arg;
  122. if (!NCDVal_ListRead(params->args, 1, &target_arg)) {
  123. ModuleLog(i, BLOG_ERROR, "wrong arity");
  124. goto fail0;
  125. }
  126. if (!NCDVal_IsString(target_arg)) {
  127. ModuleLog(i, BLOG_ERROR, "wrong type");
  128. goto fail0;
  129. }
  130. // parse name string
  131. if (!make_names(o, NCDVal_StringData(target_arg), NCDVal_StringLength(target_arg))) {
  132. ModuleLog(i, BLOG_ERROR, "make_names failed");
  133. goto fail0;
  134. }
  135. // signal up
  136. NCDModuleInst_Backend_Up(o->i);
  137. return;
  138. fail0:
  139. NCDModuleInst_Backend_DeadError(i);
  140. }
  141. static void func_die (void *vo)
  142. {
  143. struct instance *o = vo;
  144. BFree(o->dynamic_names);
  145. NCDModuleInst_Backend_Dead(o->i);
  146. }
  147. static int func_getobj (void *vo, NCD_string_id_t name, NCDObject *out_object)
  148. {
  149. struct instance *o = vo;
  150. ASSERT(o->num_names > 0)
  151. NCD_string_id_t *names = (o->dynamic_names ? o->dynamic_names : o->static_names);
  152. NCDObject object;
  153. if (!NCDModuleInst_Backend_GetObj(o->i, names[0], &object)) {
  154. return 0;
  155. }
  156. NCDObject obj2;
  157. if (!NCDObject_ResolveObjExprCompact(&object, names + 1, o->num_names - 1, &obj2)) {
  158. return 0;
  159. }
  160. if (name == NCD_STRING_EMPTY) {
  161. *out_object = obj2;
  162. return 1;
  163. }
  164. return NCDObject_GetObj(&obj2, name, out_object);
  165. }
  166. static struct NCDModule modules[] = {
  167. {
  168. .type = "alias",
  169. .func_new2 = func_new,
  170. .func_die = func_die,
  171. .func_getobj = func_getobj,
  172. .alloc_size = sizeof(struct instance)
  173. }, {
  174. .type = NULL
  175. }
  176. };
  177. const struct NCDModuleGroup ncdmodule_alias = {
  178. .modules = modules
  179. };