alias.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 <misc/concat_strings.h>
  41. #include <misc/balloc.h>
  42. #include <ncd/NCDModule.h>
  43. #include <generated/blog_channel_ncd_alias.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct instance {
  46. NCDModuleInst *i;
  47. char **target;
  48. };
  49. static char ** split_string (const char *str, char del)
  50. {
  51. size_t len = strlen(str);
  52. // count parts
  53. size_t num_parts = 0;
  54. size_t i = 0;
  55. while (1) {
  56. size_t j = i;
  57. while (j < len && str[j] != del) j++;
  58. if (j == i) {
  59. goto fail0;
  60. }
  61. num_parts++;
  62. if (j == len) {
  63. break;
  64. }
  65. i = j + 1;
  66. }
  67. // allocate array for part pointers
  68. char **result = BAllocArray(num_parts + 1, sizeof(*result));
  69. if (!result) {
  70. goto fail0;
  71. }
  72. num_parts = 0;
  73. i = 0;
  74. while (1) {
  75. size_t j = i;
  76. while (j < len && str[j] != del) j++;
  77. if (j == i) {
  78. goto fail1;
  79. }
  80. if (!(result[num_parts] = malloc(j - i + 1))) {
  81. goto fail1;
  82. }
  83. memcpy(result[num_parts], str + i, j - i);
  84. result[num_parts][j - i] = '\0';
  85. num_parts++;
  86. if (j == len) {
  87. break;
  88. }
  89. i = j + 1;
  90. }
  91. result[num_parts] = NULL;
  92. return result;
  93. fail1:
  94. while (num_parts-- > 0) {
  95. free(result[num_parts]);
  96. }
  97. BFree(result);
  98. fail0:
  99. return NULL;
  100. }
  101. static void func_new (NCDModuleInst *i)
  102. {
  103. // allocate instance
  104. struct instance *o = malloc(sizeof(*o));
  105. if (!o) {
  106. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  107. goto fail0;
  108. }
  109. o->i = i;
  110. NCDModuleInst_Backend_SetUser(i, o);
  111. // read arguments
  112. NCDValue *target_arg;
  113. if (!NCDValue_ListRead(i->args, 1, &target_arg)) {
  114. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  115. goto fail1;
  116. }
  117. if (!NCDValue_IsStringNoNulls(target_arg)) {
  118. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  119. goto fail1;
  120. }
  121. // split target into components
  122. if (!(o->target = split_string(NCDValue_StringValue(target_arg), '.'))) {
  123. ModuleLog(o->i, BLOG_ERROR, "bad target");
  124. goto fail1;
  125. }
  126. // signal up
  127. NCDModuleInst_Backend_Up(o->i);
  128. return;
  129. fail1:
  130. free(o);
  131. fail0:
  132. NCDModuleInst_Backend_SetError(i);
  133. NCDModuleInst_Backend_Dead(i);
  134. }
  135. static void func_die (void *vo)
  136. {
  137. struct instance *o = vo;
  138. NCDModuleInst *i = o->i;
  139. // free target
  140. char **str = o->target;
  141. while (*str) {
  142. free(*str);
  143. str++;
  144. }
  145. free(o->target);
  146. // free instance
  147. free(o);
  148. NCDModuleInst_Backend_Dead(i);
  149. }
  150. static int func_getobj (void *vo, const char *name, NCDObject *out_object)
  151. {
  152. struct instance *o = vo;
  153. NCDObject object;
  154. if (!NCDModuleInst_Backend_GetObj(o->i, o->target[0], &object)) {
  155. return 0;
  156. }
  157. NCDObject obj2;
  158. if (!NCDObject_ResolveObjExpr(&object, o->target + 1, &obj2)) {
  159. return 0;
  160. }
  161. if (!strcmp(name, "")) {
  162. *out_object = obj2;
  163. return 1;
  164. }
  165. return NCDObject_GetObj(&obj2, name, out_object);
  166. }
  167. static const struct NCDModule modules[] = {
  168. {
  169. .type = "alias",
  170. .func_new = func_new,
  171. .func_die = func_die,
  172. .func_getobj = func_getobj
  173. }, {
  174. .type = NULL
  175. }
  176. };
  177. const struct NCDModuleGroup ncdmodule_alias = {
  178. .modules = modules
  179. };