command.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * command.c - command execution support
  4. *
  5. * Copyright © 2010-2012 Guillem Jover <guillem@debian.org>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <stdarg.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <dpkg/dpkg.h>
  26. #include <dpkg/i18n.h>
  27. #include <dpkg/string.h>
  28. #include <dpkg/path.h>
  29. #include <dpkg/command.h>
  30. /**
  31. * Initialize a command structure.
  32. *
  33. * If name is NULL, then the last component of the filename path will be
  34. * used to initialize the name member.
  35. *
  36. * @param cmd The command structure to initialize.
  37. * @param filename The filename of the command to execute.
  38. * @param name The description of the command to execute.
  39. */
  40. void
  41. command_init(struct command *cmd, const char *filename, const char *name)
  42. {
  43. cmd->filename = filename;
  44. if (name == NULL)
  45. cmd->name = path_basename(filename);
  46. else
  47. cmd->name = name;
  48. cmd->argc = 0;
  49. cmd->argv_size = 10;
  50. cmd->argv = m_malloc(cmd->argv_size * sizeof(const char *));
  51. cmd->argv[0] = NULL;
  52. }
  53. /**
  54. * Destroy a command structure.
  55. *
  56. * Free the members managed by the command functions (i.e. the argv pointer
  57. * array), and zero all members of a command structure.
  58. *
  59. * @param cmd The command structure to free.
  60. */
  61. void
  62. command_destroy(struct command *cmd)
  63. {
  64. cmd->filename = NULL;
  65. cmd->name = NULL;
  66. cmd->argc = 0;
  67. cmd->argv_size = 0;
  68. free(cmd->argv);
  69. cmd->argv = NULL;
  70. }
  71. static void
  72. command_grow_argv(struct command *cmd, int need)
  73. {
  74. /* We need a ghost byte for the NUL character. */
  75. need++;
  76. /* Check if we already have enough room. */
  77. if ((cmd->argv_size - cmd->argc) >= need)
  78. return;
  79. cmd->argv_size = (cmd->argv_size + need) * 2;
  80. cmd->argv = m_realloc(cmd->argv, cmd->argv_size * sizeof(const char *));
  81. }
  82. /**
  83. * Append an argument to the command's argv.
  84. *
  85. * @param cmd The command structure to act on.
  86. * @param arg The argument to append to argv.
  87. */
  88. void
  89. command_add_arg(struct command *cmd, const char *arg)
  90. {
  91. command_grow_argv(cmd, 1);
  92. cmd->argv[cmd->argc++] = arg;
  93. cmd->argv[cmd->argc] = NULL;
  94. }
  95. /**
  96. * Append an argument array to the command's argv.
  97. *
  98. * @param cmd The command structure to act on.
  99. * @param argv The NULL terminated argument array to append to argv.
  100. */
  101. void
  102. command_add_argl(struct command *cmd, const char **argv)
  103. {
  104. int i, add_argc = 0;
  105. while (argv[add_argc] != NULL)
  106. add_argc++;
  107. command_grow_argv(cmd, add_argc);
  108. for (i = 0; i < add_argc; i++)
  109. cmd->argv[cmd->argc++] = argv[i];
  110. cmd->argv[cmd->argc] = NULL;
  111. }
  112. /**
  113. * Append a va_list of argument to the command's argv.
  114. *
  115. * @param cmd The command structure to act on.
  116. * @param args The NULL terminated va_list of argument array to append to argv.
  117. */
  118. void
  119. command_add_argv(struct command *cmd, va_list args)
  120. {
  121. va_list args_copy;
  122. int i, add_argc = 0;
  123. va_copy(args_copy, args);
  124. while (va_arg(args_copy, const char *) != NULL)
  125. add_argc++;
  126. va_end(args_copy);
  127. command_grow_argv(cmd, add_argc);
  128. for (i = 0; i < add_argc; i++)
  129. cmd->argv[cmd->argc++] = va_arg(args, const char *);
  130. cmd->argv[cmd->argc] = NULL;
  131. }
  132. /**
  133. * Append a variable list of argument to the command's argv.
  134. *
  135. * @param cmd The command structure to act on.
  136. * @param ... The NULL terminated variable list of argument to append to argv.
  137. */
  138. void
  139. command_add_args(struct command *cmd, ...)
  140. {
  141. va_list args;
  142. va_start(args, cmd);
  143. command_add_argv(cmd, args);
  144. va_end(args);
  145. }
  146. /**
  147. * Execute the command specified.
  148. *
  149. * The command is executed searching the PATH if the filename does not
  150. * contain any slashes, or using the full path if it's either a relative or
  151. * absolute pathname. This functions does not return.
  152. *
  153. * @param cmd The command structure to act on.
  154. */
  155. void
  156. command_exec(struct command *cmd)
  157. {
  158. execvp(cmd->filename, (char * const *)cmd->argv);
  159. ohshite(_("unable to execute %s (%s)"), cmd->name, cmd->filename);
  160. }
  161. /**
  162. * Get a suitable pager.
  163. *
  164. * @return A string representing a pager.
  165. */
  166. const char *
  167. command_get_pager(void)
  168. {
  169. const char *pager;
  170. if (!isatty(1))
  171. return CAT;
  172. pager = getenv("PAGER");
  173. if (str_is_unset(pager))
  174. pager = DEFAULTPAGER;
  175. return pager;
  176. }
  177. /**
  178. * Execute a shell with a possible command.
  179. *
  180. * @param cmd The command string to execute, if it's NULL an interactive
  181. * shell will be executed instead.
  182. * @param name The description of the command to execute.
  183. */
  184. void
  185. command_shell(const char *cmd, const char *name)
  186. {
  187. const char *shell;
  188. const char *mode;
  189. shell = getenv("SHELL");
  190. if (str_is_unset(shell))
  191. shell = DEFAULTSHELL;
  192. if (cmd == NULL)
  193. mode = "-i";
  194. else
  195. mode = "-c";
  196. execlp(shell, shell, mode, cmd, NULL);
  197. ohshite(_("unable to execute %s (%s)"), name, cmd);
  198. }