build_cmdline.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @file build_cmdline.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. #include <stdlib.h>
  30. #include <misc/debug.h>
  31. #include <ncd/extra/value_utils.h>
  32. #include "build_cmdline.h"
  33. int ncd_build_cmdline (NCDModuleInst *i, int log_channel, NCDValRef cmd_arg, char **out_exec, CmdLine *out_cl)
  34. {
  35. ASSERT(!NCDVal_IsInvalid(cmd_arg))
  36. ASSERT(out_exec)
  37. ASSERT(out_cl)
  38. if (!NCDVal_IsList(cmd_arg)) {
  39. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
  40. goto fail0;
  41. }
  42. size_t count = NCDVal_ListCount(cmd_arg);
  43. // read exec
  44. if (count == 0) {
  45. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "missing executable name");
  46. goto fail0;
  47. }
  48. NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
  49. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  50. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
  51. goto fail0;
  52. }
  53. char *exec = ncd_strdup(exec_arg);
  54. if (!exec) {
  55. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "ncd_strdup failed");
  56. goto fail0;
  57. }
  58. // start cmdline
  59. CmdLine cl;
  60. if (!CmdLine_Init(&cl)) {
  61. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Init failed");
  62. goto fail1;
  63. }
  64. // add header
  65. if (!CmdLine_Append(&cl, exec)) {
  66. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Append failed");
  67. goto fail2;
  68. }
  69. // add additional arguments
  70. for (size_t j = 1; j < count; j++) {
  71. NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
  72. if (!NCDVal_IsStringNoNulls(arg)) {
  73. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "wrong type");
  74. goto fail2;
  75. }
  76. if (!CmdLine_AppendNoNullMr(&cl, NCDVal_StringMemRef(arg))) {
  77. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
  78. goto fail2;
  79. }
  80. }
  81. // finish
  82. if (!CmdLine_Finish(&cl)) {
  83. NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_Finish failed");
  84. goto fail2;
  85. }
  86. *out_exec = exec;
  87. *out_cl = cl;
  88. return 1;
  89. fail2:
  90. CmdLine_Free(&cl);
  91. fail1:
  92. free(exec);
  93. fail0:
  94. return 0;
  95. }