2009-11-21 Samuel Thibault <samuel.thibault@ens-lyon.org>
[grub2.git] / commands / handler.c
blob2070c391cfa9e7a98e67585cb09f92d77495c681
1 /* handler.c - commands to list or select handlers */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/dl.h>
21 #include <grub/err.h>
22 #include <grub/misc.h>
23 #include <grub/term.h>
24 #include <grub/handler.h>
25 #include <grub/command.h>
27 static grub_err_t
28 grub_cmd_handler (struct grub_command *cmd,
29 int argc, char **args)
31 char *class_name;
32 void *curr_item = 0;
33 grub_handler_class_t head;
35 auto int list_item (grub_named_list_t item);
36 int list_item (grub_named_list_t item)
38 if (item == curr_item)
39 grub_putchar ('*');
41 grub_printf ("%s\n", item->name);
43 return 0;
46 class_name = (grub_strcmp (cmd->name, "handler")) ? (char *) cmd->name : 0;
48 head = grub_handler_class_list;
49 if ((argc == 0) && (class_name == 0))
51 grub_list_iterate (GRUB_AS_LIST (head), (grub_list_hook_t) list_item);
53 else
55 grub_handler_class_t class;
57 if (class_name == 0)
59 class_name = args[0];
60 argc--;
61 args++;
64 class = grub_named_list_find (GRUB_AS_NAMED_LIST (head), class_name);
65 if (! class)
66 return grub_error (GRUB_ERR_FILE_NOT_FOUND, "class not found");
68 if (argc == 0)
70 curr_item = class->cur_handler;
71 grub_list_iterate (GRUB_AS_LIST (class->handler_list),
72 (grub_list_hook_t) list_item);
74 else
76 grub_handler_t handler;
78 handler =
79 grub_named_list_find (GRUB_AS_NAMED_LIST (class->handler_list),
80 args[0]);
82 if (! handler)
83 return grub_error (GRUB_ERR_FILE_NOT_FOUND, "handler not found");
85 grub_handler_set_current (class, handler);
89 return 0;
92 static grub_command_t cmd_handler, cmd_terminal_input, cmd_terminal_output;
94 GRUB_MOD_INIT(handler)
96 cmd_handler =
97 grub_register_command ("handler", grub_cmd_handler,
98 "handler [class [handler]]",
99 "List or select a handler");
100 cmd_terminal_input =
101 grub_register_command ("terminal_input", grub_cmd_handler,
102 "terminal_input [handler]",
103 "List or select a handler");
104 cmd_terminal_output =
105 grub_register_command ("terminal_output", grub_cmd_handler,
106 "terminal_output [handler]",
107 "List or select a handler");
110 GRUB_MOD_FINI(handler)
112 grub_unregister_command (cmd_handler);
113 grub_unregister_command (cmd_terminal_input);
114 grub_unregister_command (cmd_terminal_output);