您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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