選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

exec.c 13KB

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