Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <ctype.h>
  24. #include <unistd.h>
  25. #include <getopt.h>
  26. #include <errno.h>
  27. #include <assert.h>
  28. #include <ipxe/tables.h>
  29. #include <ipxe/command.h>
  30. #include <ipxe/parseopt.h>
  31. #include <ipxe/settings.h>
  32. #include <ipxe/shell.h>
  33. /** @file
  34. *
  35. * Command execution
  36. *
  37. */
  38. /** Shell stop state */
  39. static int stop_state;
  40. /**
  41. * Execute command
  42. *
  43. * @v command Command name
  44. * @v argv Argument list
  45. * @ret rc Return status code
  46. *
  47. * Execute the named command. Unlike a traditional POSIX execv(),
  48. * this function returns the exit status of the command.
  49. */
  50. int execv ( const char *command, char * const argv[] ) {
  51. struct command *cmd;
  52. int argc;
  53. /* Count number of arguments */
  54. for ( argc = 0 ; argv[argc] ; argc++ ) {}
  55. /* An empty command is deemed to do nothing, successfully */
  56. if ( command == NULL )
  57. return 0;
  58. /* Sanity checks */
  59. if ( argc == 0 ) {
  60. DBG ( "%s: empty argument list\n", command );
  61. return -EINVAL;
  62. }
  63. /* Reset getopt() library ready for use by the command. This
  64. * is an artefact of the POSIX getopt() API within the context
  65. * of Etherboot; see the documentation for reset_getopt() for
  66. * details.
  67. */
  68. reset_getopt();
  69. /* Hand off to command implementation */
  70. for_each_table_entry ( cmd, COMMANDS ) {
  71. if ( strcmp ( command, cmd->name ) == 0 )
  72. return cmd->exec ( argc, ( char ** ) argv );
  73. }
  74. printf ( "%s: command not found\n", command );
  75. return -ENOEXEC;
  76. }
  77. /**
  78. * Split command line into tokens
  79. *
  80. * @v command Command line
  81. * @v tokens Token list to populate, or NULL
  82. * @ret count Number of tokens
  83. *
  84. * Splits the command line into whitespace-delimited tokens. If @c
  85. * tokens is non-NULL, any whitespace in the command line will be
  86. * replaced with NULs.
  87. */
  88. static int split_command ( char *command, char **tokens ) {
  89. int count = 0;
  90. while ( 1 ) {
  91. /* Skip over any whitespace / convert to NUL */
  92. while ( isspace ( *command ) ) {
  93. if ( tokens )
  94. *command = '\0';
  95. command++;
  96. }
  97. /* Check for end of line */
  98. if ( ! *command )
  99. break;
  100. /* We have found the start of the next argument */
  101. if ( tokens )
  102. tokens[count] = command;
  103. count++;
  104. /* Skip to start of next whitespace, if any */
  105. while ( *command && ! isspace ( *command ) ) {
  106. command++;
  107. }
  108. }
  109. return count;
  110. }
  111. /**
  112. * Process next command only if previous command succeeded
  113. *
  114. * @v rc Status of previous command
  115. * @ret process Process next command
  116. */
  117. static int process_on_success ( int rc ) {
  118. return ( rc == 0 );
  119. }
  120. /**
  121. * Process next command only if previous command failed
  122. *
  123. * @v rc Status of previous command
  124. * @ret process Process next command
  125. */
  126. static int process_on_failure ( int rc ) {
  127. return ( rc != 0 );
  128. }
  129. /**
  130. * Process next command regardless of status from previous command
  131. *
  132. * @v rc Status of previous command
  133. * @ret process Process next command
  134. */
  135. static int process_always ( int rc __unused ) {
  136. return 1;
  137. }
  138. /**
  139. * Find command terminator
  140. *
  141. * @v tokens Token list
  142. * @ret process_next "Should next command be processed?" function
  143. * @ret argc Argument count
  144. */
  145. static int command_terminator ( char **tokens,
  146. int ( **process_next ) ( int rc ) ) {
  147. unsigned int i;
  148. /* Find first terminating token */
  149. for ( i = 0 ; tokens[i] ; i++ ) {
  150. if ( tokens[i][0] == '#' ) {
  151. /* Start of a comment */
  152. break;
  153. } else if ( strcmp ( tokens[i], "||" ) == 0 ) {
  154. /* Short-circuit logical OR */
  155. *process_next = process_on_failure;
  156. return i;
  157. } else if ( strcmp ( tokens[i], "&&" ) == 0 ) {
  158. /* Short-circuit logical AND */
  159. *process_next = process_on_success;
  160. return i;
  161. } else if ( strcmp ( tokens[i], ";" ) == 0 ) {
  162. /* Process next command unconditionally */
  163. *process_next = process_always;
  164. return i;
  165. }
  166. }
  167. /* End of token list */
  168. *process_next = NULL;
  169. return i;
  170. }
  171. /**
  172. * Set shell stop state
  173. *
  174. * @v stop Shell stop state
  175. */
  176. void shell_stop ( int stop ) {
  177. stop_state = stop;
  178. }
  179. /**
  180. * Test and consume shell stop state
  181. *
  182. * @v stop Shell stop state to consume
  183. * @v stopped Shell had been stopped
  184. */
  185. int shell_stopped ( int stop ) {
  186. int stopped;
  187. /* Test to see if we need to stop */
  188. stopped = ( stop_state >= stop );
  189. /* Consume stop state */
  190. if ( stop_state <= stop )
  191. stop_state = 0;
  192. return stopped;
  193. }
  194. /**
  195. * Expand settings within a token list
  196. *
  197. * @v argc Argument count
  198. * @v tokens Token list
  199. * @v argv Argument list to fill in
  200. * @ret rc Return status code
  201. */
  202. static int expand_tokens ( int argc, char **tokens, char **argv ) {
  203. int i;
  204. /* Expand each token in turn */
  205. for ( i = 0 ; i < argc ; i++ ) {
  206. argv[i] = expand_settings ( tokens[i] );
  207. if ( ! argv[i] )
  208. goto err_expand_settings;
  209. }
  210. return 0;
  211. err_expand_settings:
  212. assert ( argv[i] == NULL );
  213. for ( ; i >= 0 ; i-- )
  214. free ( argv[i] );
  215. return -ENOMEM;
  216. }
  217. /**
  218. * Free an expanded token list
  219. *
  220. * @v argv Argument list
  221. */
  222. static void free_tokens ( char **argv ) {
  223. /* Free each expanded argument */
  224. while ( *argv )
  225. free ( *(argv++) );
  226. }
  227. /**
  228. * Execute command line
  229. *
  230. * @v command Command line
  231. * @ret rc Return status code
  232. *
  233. * Execute the named command and arguments.
  234. */
  235. int system ( const char *command ) {
  236. int count = split_command ( ( char * ) command, NULL );
  237. char *all_tokens[ count + 1 ];
  238. int ( * process_next ) ( int rc );
  239. char *command_copy;
  240. char **tokens;
  241. int argc;
  242. int process;
  243. int rc = 0;
  244. /* Create modifiable copy of command */
  245. command_copy = strdup ( command );
  246. if ( ! command_copy )
  247. return -ENOMEM;
  248. /* Split command into tokens */
  249. split_command ( command_copy, all_tokens );
  250. all_tokens[count] = NULL;
  251. /* Process individual commands */
  252. process = 1;
  253. for ( tokens = all_tokens ; ; tokens += ( argc + 1 ) ) {
  254. /* Find command terminator */
  255. argc = command_terminator ( tokens, &process_next );
  256. /* Expand tokens and execute command */
  257. if ( process ) {
  258. char *argv[ argc + 1 ];
  259. /* Expand tokens */
  260. if ( ( rc = expand_tokens ( argc, tokens, argv ) ) != 0)
  261. break;
  262. argv[argc] = NULL;
  263. /* Execute command */
  264. rc = execv ( argv[0], argv );
  265. /* Free tokens */
  266. free_tokens ( argv );
  267. }
  268. /* Stop processing, if applicable */
  269. if ( shell_stopped ( SHELL_STOP_COMMAND ) )
  270. break;
  271. /* Stop processing if we have reached the end of the
  272. * command.
  273. */
  274. if ( ! process_next )
  275. break;
  276. /* Determine whether or not to process next command */
  277. process = process_next ( rc );
  278. }
  279. /* Free modified copy of command */
  280. free ( command_copy );
  281. return rc;
  282. }
  283. /**
  284. * Concatenate arguments
  285. *
  286. * @v args Argument list (NULL-terminated)
  287. * @ret string Concatenated arguments
  288. *
  289. * The returned string is allocated with malloc(). The caller is
  290. * responsible for eventually free()ing this string.
  291. */
  292. char * concat_args ( char **args ) {
  293. char **arg;
  294. size_t len;
  295. char *string;
  296. char *ptr;
  297. /* Calculate total string length */
  298. len = 1 /* NUL */;
  299. for ( arg = args ; *arg ; arg++ )
  300. len += ( 1 /* possible space */ + strlen ( *arg ) );
  301. /* Allocate string */
  302. string = zalloc ( len );
  303. if ( ! string )
  304. return NULL;
  305. /* Populate string */
  306. ptr = string;
  307. for ( arg = args ; *arg ; arg++ ) {
  308. ptr += sprintf ( ptr, "%s%s",
  309. ( ( ptr == string ) ? "" : " " ), *arg );
  310. }
  311. assert ( ptr < ( string + len ) );
  312. return string;
  313. }
  314. /** "echo" options */
  315. struct echo_options {
  316. /** Do not print trailing newline */
  317. int no_newline;
  318. };
  319. /** "echo" option list */
  320. static struct option_descriptor echo_opts[] = {
  321. OPTION_DESC ( "n", 'n', no_argument,
  322. struct echo_options, no_newline, parse_flag ),
  323. };
  324. /** "echo" command descriptor */
  325. static struct command_descriptor echo_cmd =
  326. COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
  327. "[-n] [...]" );
  328. /**
  329. * "echo" command
  330. *
  331. * @v argc Argument count
  332. * @v argv Argument list
  333. * @ret rc Return status code
  334. */
  335. static int echo_exec ( int argc, char **argv ) {
  336. struct echo_options opts;
  337. char *text;
  338. int rc;
  339. /* Parse options */
  340. if ( ( rc = parse_options ( argc, argv, &echo_cmd, &opts ) ) != 0 )
  341. return rc;
  342. /* Parse text */
  343. text = concat_args ( &argv[optind] );
  344. if ( ! text )
  345. return -ENOMEM;
  346. /* Print text */
  347. printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
  348. free ( text );
  349. return 0;
  350. }
  351. /** "echo" command */
  352. struct command echo_command __command = {
  353. .name = "echo",
  354. .exec = echo_exec,
  355. };
  356. /** "exit" options */
  357. struct exit_options {};
  358. /** "exit" option list */
  359. static struct option_descriptor exit_opts[] = {};
  360. /** "exit" command descriptor */
  361. static struct command_descriptor exit_cmd =
  362. COMMAND_DESC ( struct exit_options, exit_opts, 0, 1, "[<status>]" );
  363. /**
  364. * "exit" command
  365. *
  366. * @v argc Argument count
  367. * @v argv Argument list
  368. * @ret rc Return status code
  369. */
  370. static int exit_exec ( int argc, char **argv ) {
  371. struct exit_options opts;
  372. unsigned int exit_code = 0;
  373. int rc;
  374. /* Parse options */
  375. if ( ( rc = parse_options ( argc, argv, &exit_cmd, &opts ) ) != 0 )
  376. return rc;
  377. /* Parse exit status, if present */
  378. if ( optind != argc ) {
  379. if ( ( rc = parse_integer ( argv[optind], &exit_code ) ) != 0 )
  380. return rc;
  381. }
  382. /* Stop shell processing */
  383. shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );
  384. return exit_code;
  385. }
  386. /** "exit" command */
  387. struct command exit_command __command = {
  388. .name = "exit",
  389. .exec = exit_exec,
  390. };
  391. /** "isset" options */
  392. struct isset_options {};
  393. /** "isset" option list */
  394. static struct option_descriptor isset_opts[] = {};
  395. /** "isset" command descriptor */
  396. static struct command_descriptor isset_cmd =
  397. COMMAND_DESC ( struct isset_options, isset_opts, 1, 1, "<value>" );
  398. /**
  399. * "isset" command
  400. *
  401. * @v argc Argument count
  402. * @v argv Argument list
  403. * @ret rc Return status code
  404. */
  405. static int isset_exec ( int argc, char **argv ) {
  406. struct isset_options opts;
  407. int rc;
  408. /* Parse options */
  409. if ( ( rc = parse_options ( argc, argv, &isset_cmd, &opts ) ) != 0 )
  410. return rc;
  411. /* Return success iff argument is non-empty */
  412. return ( argv[optind][0] ? 0 : -ENOENT );
  413. }
  414. /** "isset" command */
  415. struct command isset_command __command = {
  416. .name = "isset",
  417. .exec = isset_exec,
  418. };
  419. /** "iseq" options */
  420. struct iseq_options {};
  421. /** "iseq" option list */
  422. static struct option_descriptor iseq_opts[] = {};
  423. /** "iseq" command descriptor */
  424. static struct command_descriptor iseq_cmd =
  425. COMMAND_DESC ( struct iseq_options, iseq_opts, 2, 2,
  426. "<value1> <value2>" );
  427. /**
  428. * "iseq" command
  429. *
  430. * @v argc Argument count
  431. * @v argv Argument list
  432. * @ret rc Return status code
  433. */
  434. static int iseq_exec ( int argc, char **argv ) {
  435. struct iseq_options opts;
  436. int rc;
  437. /* Parse options */
  438. if ( ( rc = parse_options ( argc, argv, &iseq_cmd, &opts ) ) != 0 )
  439. return rc;
  440. /* Return success iff arguments are equal */
  441. return ( ( strcmp ( argv[optind], argv[ optind + 1 ] ) == 0 ) ?
  442. 0 : -ERANGE );
  443. }
  444. /** "iseq" command */
  445. struct command iseq_command __command = {
  446. .name = "iseq",
  447. .exec = iseq_exec,
  448. };