getenv.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file getenv.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. * getenv(string name)
  33. *
  34. * Variables:
  35. * string (empty) - if the environment value exists, its value
  36. * string exists - "true" if the variable exists, "false" if not
  37. */
  38. #include <stdlib.h>
  39. #include <misc/strdup.h>
  40. #include <ncd/module_common.h>
  41. #include <generated/blog_channel_ncd_getenv.h>
  42. struct instance {
  43. NCDModuleInst *i;
  44. char *value;
  45. };
  46. enum {STRING_EXISTS};
  47. static const char *strings[] = {"exists", NULL};
  48. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  49. {
  50. struct instance *o = vo;
  51. o->i = i;
  52. // check arguments
  53. NCDValRef name_arg;
  54. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  55. ModuleLog(i, BLOG_ERROR, "wrong arity");
  56. goto fail0;
  57. }
  58. if (!NCDVal_IsString(name_arg)) {
  59. ModuleLog(i, BLOG_ERROR, "wrong type");
  60. goto fail0;
  61. }
  62. o->value = NULL;
  63. if (NCDVal_StringHasNulls(name_arg)) {
  64. goto out;
  65. }
  66. NCDValNullTermString nts;
  67. if (!NCDVal_StringNullTerminate(name_arg, &nts)) {
  68. ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
  69. goto fail0;
  70. }
  71. char *result = getenv(nts.data);
  72. NCDValNullTermString_Free(&nts);
  73. if (!result) {
  74. goto out;
  75. }
  76. o->value = b_strdup(result);
  77. if (!o->value) {
  78. ModuleLog(i, BLOG_ERROR, "b_strdup failed");
  79. goto fail0;
  80. }
  81. out:
  82. // signal up
  83. NCDModuleInst_Backend_Up(i);
  84. return;
  85. fail0:
  86. NCDModuleInst_Backend_DeadError(i);
  87. }
  88. static void func_die (void *vo)
  89. {
  90. struct instance *o = vo;
  91. // free value
  92. if (o->value) {
  93. BFree(o->value);
  94. }
  95. NCDModuleInst_Backend_Dead(o->i);
  96. }
  97. static int func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  98. {
  99. struct instance *o = vo;
  100. if (name == NCD_STRING_EMPTY && o->value) {
  101. *out = NCDVal_NewString(mem, o->value);
  102. return 1;
  103. }
  104. if (name == ModuleString(o->i, STRING_EXISTS)) {
  105. *out = ncd_make_boolean(mem, !!o->value);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static struct NCDModule modules[] = {
  111. {
  112. .type = "getenv",
  113. .func_new2 = func_new,
  114. .func_die = func_die,
  115. .func_getvar2 = func_getvar,
  116. .alloc_size = sizeof(struct instance)
  117. }, {
  118. .type = NULL
  119. }
  120. };
  121. const struct NCDModuleGroup ncdmodule_getenv = {
  122. .modules = modules,
  123. .strings = strings
  124. };