cmdline.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/normal_menu.h>
  19. #include <grub/misc.h>
  20. #include <grub/term.h>
  21. #include <grub/err.h>
  22. #include <grub/types.h>
  23. #include <grub/mm.h>
  24. #include <grub/partition.h>
  25. #include <grub/disk.h>
  26. #include <grub/file.h>
  27. #include <grub/env.h>
  28. #include <grub/lib.h>
  29. #include <grub/i18n.h>
  30. #include <grub/charset.h>
  31. static grub_uint32_t *kill_buf;
  32. static grub_size_t
  33. strlen_ucs4 (const grub_uint32_t *s)
  34. {
  35. const grub_uint32_t *p = s;
  36. while (*p)
  37. p++;
  38. return p - s;
  39. }
  40. /* A completion hook to print items. */
  41. static void
  42. print_completion (const char *item, grub_completion_type_t type, int count)
  43. {
  44. if (count == 0)
  45. {
  46. /* If this is the first time, print a label. */
  47. grub_puts ("");
  48. switch (type)
  49. {
  50. case GRUB_COMPLETION_TYPE_COMMAND:
  51. grub_puts_ (N_("Possible commands are:"));
  52. break;
  53. case GRUB_COMPLETION_TYPE_DEVICE:
  54. grub_puts_ (N_("Possible devices are:"));
  55. break;
  56. case GRUB_COMPLETION_TYPE_FILE:
  57. grub_puts_ (N_("Possible files are:"));
  58. break;
  59. case GRUB_COMPLETION_TYPE_PARTITION:
  60. grub_puts_ (N_("Possible partitions are:"));
  61. break;
  62. case GRUB_COMPLETION_TYPE_ARGUMENT:
  63. grub_puts_ (N_("Possible arguments are:"));
  64. break;
  65. default:
  66. grub_puts_ (N_("Possible things are:"));
  67. break;
  68. }
  69. grub_puts ("");
  70. }
  71. if (type == GRUB_COMPLETION_TYPE_PARTITION)
  72. {
  73. grub_print_device_info (item);
  74. grub_errno = GRUB_ERR_NONE;
  75. }
  76. else
  77. grub_printf (" %s", item);
  78. }
  79. struct cmdline_term
  80. {
  81. unsigned xpos, ypos, ystart, width, height;
  82. struct grub_term_output *term;
  83. };
  84. struct grub_cmdline_get_closure
  85. {
  86. grub_size_t lpos, llen;
  87. grub_size_t plen;
  88. grub_uint32_t *buf;
  89. grub_size_t max_len;
  90. struct cmdline_term *cl_terms;
  91. unsigned nterms;
  92. };
  93. static void
  94. cl_set_pos (struct cmdline_term *cl_term, struct grub_cmdline_get_closure *c)
  95. {
  96. cl_term->xpos = (c->plen + c->lpos) % (cl_term->width - 1);
  97. cl_term->ypos = cl_term->ystart + (c->plen + c->lpos) / (cl_term->width - 1);
  98. grub_term_gotoxy (cl_term->term, cl_term->xpos, cl_term->ypos);
  99. }
  100. static void
  101. cl_set_pos_all (struct grub_cmdline_get_closure *c)
  102. {
  103. unsigned i;
  104. for (i = 0; i < c->nterms; i++)
  105. cl_set_pos (&c->cl_terms[i], c);
  106. }
  107. static inline void __attribute__ ((always_inline))
  108. cl_print (struct cmdline_term *cl_term, int pos, grub_uint32_t c,
  109. struct grub_cmdline_get_closure *cc)
  110. {
  111. grub_uint32_t *p;
  112. for (p = cc->buf + pos; p < cc->buf + cc->llen; p++)
  113. {
  114. if (c)
  115. grub_putcode (c, cl_term->term);
  116. else
  117. grub_putcode (*p, cl_term->term);
  118. cl_term->xpos++;
  119. if (cl_term->xpos >= cl_term->width - 1)
  120. {
  121. cl_term->xpos = 0;
  122. if (cl_term->ypos >= (unsigned) (cl_term->height - 1))
  123. cl_term->ystart--;
  124. else
  125. cl_term->ypos++;
  126. grub_putcode ('\n', cl_term->term);
  127. }
  128. }
  129. }
  130. static void
  131. cl_print_all (int pos, grub_uint32_t c, struct grub_cmdline_get_closure *cc)
  132. {
  133. unsigned i;
  134. for (i = 0; i < cc->nterms; i++)
  135. cl_print (&cc->cl_terms[i], pos, c, cc);
  136. }
  137. static void
  138. cl_insert (const grub_uint32_t *str, struct grub_cmdline_get_closure *c)
  139. {
  140. grub_size_t len = strlen_ucs4 (str);
  141. if (len + c->llen >= c->max_len)
  142. {
  143. grub_uint32_t *nbuf;
  144. c->max_len *= 2;
  145. nbuf = grub_realloc (c->buf, sizeof (grub_uint32_t) * c->max_len);
  146. if (nbuf)
  147. c->buf = nbuf;
  148. else
  149. {
  150. grub_print_error ();
  151. grub_errno = GRUB_ERR_NONE;
  152. c->max_len /= 2;
  153. }
  154. }
  155. if (len + c->llen < c->max_len)
  156. {
  157. grub_memmove (c->buf + c->lpos + len, c->buf + c->lpos,
  158. (c->llen - c->lpos + 1) * sizeof (grub_uint32_t));
  159. grub_memmove (c->buf + c->lpos, str, len * sizeof (grub_uint32_t));
  160. c->llen += len;
  161. cl_set_pos_all (c);
  162. cl_print_all (c->lpos, 0, c);
  163. c->lpos += len;
  164. cl_set_pos_all (c);
  165. }
  166. }
  167. static void
  168. cl_delete (unsigned len, struct grub_cmdline_get_closure *c)
  169. {
  170. if (c->lpos + len <= c->llen)
  171. {
  172. grub_size_t saved_lpos = c->lpos;
  173. c->lpos = c->llen - len;
  174. cl_set_pos_all (c);
  175. cl_print_all (c->lpos, ' ', c);
  176. c->lpos = saved_lpos;
  177. cl_set_pos_all (c);
  178. grub_memmove (c->buf + c->lpos, c->buf + c->lpos + len,
  179. sizeof (grub_uint32_t) * (c->llen - c->lpos + 1));
  180. c->llen -= len;
  181. cl_print_all (c->lpos, 0, c);
  182. cl_set_pos_all (c);
  183. }
  184. }
  185. static void
  186. init_clterm (struct cmdline_term *cl_term_cur, struct grub_cmdline_get_closure *c)
  187. {
  188. cl_term_cur->xpos = c->plen;
  189. cl_term_cur->ypos = (grub_term_getxy (cl_term_cur->term) & 0xFF);
  190. cl_term_cur->ystart = cl_term_cur->ypos;
  191. cl_term_cur->width = grub_term_width (cl_term_cur->term);
  192. cl_term_cur->height = grub_term_height (cl_term_cur->term);
  193. }
  194. static void
  195. init_clterm_all (struct grub_cmdline_get_closure *c)
  196. {
  197. unsigned i;
  198. for (i = 0; i < c->nterms; i++)
  199. init_clterm (&c->cl_terms[i], c);
  200. }
  201. /* Get a command-line. If ESC is pushed, return zero,
  202. otherwise return command line. */
  203. /* FIXME: The dumb interface is not supported yet. */
  204. char *
  205. grub_cmdline_get (const char *prompt)
  206. {
  207. int key;
  208. int histpos = 0;
  209. const char *prompt_translated = _(prompt);
  210. char *ret;
  211. struct grub_cmdline_get_closure c;
  212. c.max_len = 256;
  213. c.buf = grub_malloc (c.max_len * sizeof (grub_uint32_t));
  214. if (!c.buf)
  215. return 0;
  216. c.plen = grub_strlen (prompt_translated) + sizeof (" ") - 1;
  217. c.lpos = c.llen = 0;
  218. c.buf[0] = '\0';
  219. {
  220. grub_term_output_t term;
  221. FOR_ACTIVE_TERM_OUTPUTS(term)
  222. if ((grub_term_getxy (term) >> 8) != 0)
  223. grub_putcode ('\n', term);
  224. }
  225. grub_printf ("%s ", prompt_translated);
  226. {
  227. struct cmdline_term *cl_term_cur;
  228. struct grub_term_output *cur;
  229. c.nterms = 0;
  230. FOR_ACTIVE_TERM_OUTPUTS(cur)
  231. c.nterms++;
  232. c.cl_terms = grub_malloc (sizeof (c.cl_terms[0]) * c.nterms);
  233. if (!c.cl_terms)
  234. return 0;
  235. cl_term_cur = c.cl_terms;
  236. FOR_ACTIVE_TERM_OUTPUTS(cur)
  237. {
  238. cl_term_cur->term = cur;
  239. init_clterm (cl_term_cur, &c);
  240. cl_term_cur++;
  241. }
  242. }
  243. if (grub_history_used () == 0)
  244. grub_history_add (c.buf, c.llen);
  245. grub_refresh ();
  246. while ((key = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key != '\r')
  247. {
  248. switch (key)
  249. {
  250. case 1: /* Ctrl-a */
  251. c.lpos = 0;
  252. cl_set_pos_all (&c);
  253. break;
  254. case 2: /* Ctrl-b */
  255. if (c.lpos > 0)
  256. {
  257. c.lpos--;
  258. cl_set_pos_all (&c);
  259. }
  260. break;
  261. case 5: /* Ctrl-e */
  262. c.lpos = c.llen;
  263. cl_set_pos_all (&c);
  264. break;
  265. case 6: /* Ctrl-f */
  266. if (c.lpos < c.llen)
  267. {
  268. c.lpos++;
  269. cl_set_pos_all (&c);
  270. }
  271. break;
  272. case 9: /* Ctrl-i or TAB */
  273. {
  274. int restore;
  275. char *insertu8;
  276. char *bufu8;
  277. grub_uint32_t cc;
  278. cc = c.buf[c.lpos];
  279. c.buf[c.lpos] = '\0';
  280. bufu8 = grub_ucs4_to_utf8_alloc (c.buf, c.lpos);
  281. c.buf[c.lpos] = cc;
  282. if (!bufu8)
  283. {
  284. grub_print_error ();
  285. grub_errno = GRUB_ERR_NONE;
  286. break;
  287. }
  288. insertu8 = grub_complete (bufu8, &restore, print_completion);
  289. grub_free (bufu8);
  290. if (restore)
  291. {
  292. /* Restore the prompt. */
  293. grub_printf ("\n%s ", prompt_translated);
  294. init_clterm_all (&c);
  295. cl_print_all (0, 0, &c);
  296. }
  297. if (insertu8)
  298. {
  299. grub_size_t insertlen;
  300. grub_ssize_t t;
  301. grub_uint32_t *insert;
  302. insertlen = grub_strlen (insertu8);
  303. insert = grub_malloc ((insertlen + 1) * sizeof (grub_uint32_t));
  304. if (!insert)
  305. {
  306. grub_free (insertu8);
  307. grub_print_error ();
  308. grub_errno = GRUB_ERR_NONE;
  309. break;
  310. }
  311. t = grub_utf8_to_ucs4 (insert, insertlen,
  312. (grub_uint8_t *) insertu8,
  313. insertlen, 0);
  314. if (t > 0)
  315. {
  316. if (insert[t-1] == ' ' && c.buf[c.lpos] == ' ')
  317. {
  318. insert[t-1] = 0;
  319. if (t != 1)
  320. cl_insert (insert, &c);
  321. c.lpos++;
  322. }
  323. else
  324. {
  325. insert[t] = 0;
  326. cl_insert (insert, &c);
  327. }
  328. }
  329. grub_free (insertu8);
  330. grub_free (insert);
  331. }
  332. cl_set_pos_all (&c);
  333. }
  334. break;
  335. case 11: /* Ctrl-k */
  336. if (c.lpos < c.llen)
  337. {
  338. if (kill_buf)
  339. grub_free (kill_buf);
  340. kill_buf = grub_malloc ((c.llen - c.lpos + 1)
  341. * sizeof (grub_uint32_t));
  342. if (grub_errno)
  343. {
  344. grub_print_error ();
  345. grub_errno = GRUB_ERR_NONE;
  346. }
  347. else
  348. {
  349. grub_memcpy (kill_buf, c.buf + c.lpos,
  350. (c.llen - c.lpos + 1) * sizeof (grub_uint32_t));
  351. kill_buf[c.llen - c.lpos] = 0;
  352. }
  353. cl_delete (c.llen - c.lpos, &c);
  354. }
  355. break;
  356. case 14: /* Ctrl-n */
  357. case GRUB_TERM_DOWN:
  358. {
  359. grub_uint32_t *hist;
  360. c.lpos = 0;
  361. if (histpos > 0)
  362. {
  363. grub_history_replace (histpos, c.buf, c.llen);
  364. histpos--;
  365. }
  366. cl_delete (c.llen, &c);
  367. hist = grub_history_get (histpos);
  368. cl_insert (hist, &c);
  369. break;
  370. }
  371. case 16: /* Ctrl-p */
  372. case GRUB_TERM_UP:
  373. {
  374. grub_uint32_t *hist;
  375. c.lpos = 0;
  376. if (histpos < grub_history_used () - 1)
  377. {
  378. grub_history_replace (histpos, c.buf, c.llen);
  379. histpos++;
  380. }
  381. cl_delete (c.llen, &c);
  382. hist = grub_history_get (histpos);
  383. cl_insert (hist, &c);
  384. }
  385. break;
  386. case 21: /* Ctrl-u */
  387. if (c.lpos > 0)
  388. {
  389. grub_size_t n = c.lpos;
  390. if (kill_buf)
  391. grub_free (kill_buf);
  392. kill_buf = grub_malloc (n + 1);
  393. if (grub_errno)
  394. {
  395. grub_print_error ();
  396. grub_errno = GRUB_ERR_NONE;
  397. }
  398. if (kill_buf)
  399. {
  400. grub_memcpy (kill_buf, c.buf, n);
  401. kill_buf[n] = '\0';
  402. }
  403. c.lpos = 0;
  404. cl_set_pos_all (&c);
  405. cl_delete (n, &c);
  406. }
  407. break;
  408. case 25: /* Ctrl-y */
  409. if (kill_buf)
  410. cl_insert (kill_buf, &c);
  411. break;
  412. case '\e':
  413. return 0;
  414. case '\b':
  415. if (c.lpos > 0)
  416. {
  417. c.lpos--;
  418. cl_set_pos_all (&c);
  419. }
  420. else
  421. break;
  422. /* fall through */
  423. case 4: /* Ctrl-d */
  424. if (c.lpos < c.llen)
  425. cl_delete (1, &c);
  426. break;
  427. default:
  428. if (grub_isprint (key))
  429. {
  430. grub_uint32_t str[2];
  431. str[0] = key;
  432. str[1] = '\0';
  433. cl_insert (str, &c);
  434. }
  435. break;
  436. }
  437. grub_refresh ();
  438. }
  439. grub_putchar ('\n');
  440. grub_refresh ();
  441. /* Remove leading spaces. */
  442. c.lpos = 0;
  443. while (c.buf[c.lpos] == ' ')
  444. c.lpos++;
  445. histpos = 0;
  446. if (strlen_ucs4 (c.buf) > 0)
  447. {
  448. grub_uint32_t empty[] = { 0 };
  449. grub_history_replace (histpos, c.buf, c.llen);
  450. grub_history_add (empty, 0);
  451. }
  452. ret = grub_ucs4_to_utf8_alloc (c.buf + c.lpos, c.llen - c.lpos + 1);
  453. grub_free (c.buf);
  454. return ret;
  455. }