⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commandlist.txt

📁 lubbock是Intel官方为pxa255提供的bios
💻 TXT
字号:
(emacs users can use -*- outline -*- mode for this file)	The blob commandlist		or	How to add new commands to blobCopyright (C) 2001  Erik Mouw <J.A.K.Mouw@its.tudelft.nl>$Id: commandlist.txt,v 1.2 2002/01/05 20:24:07 erikm Exp $* Introduction==============All interactive programs have to deal with the problem of parsing thecommand line and calling the correct function to execute thecommand. Most programs use a simple if-then-else ladder for commandparsing, which works well for a small and/or fixed number ofcommands. When the number of commands becomes either large or isn'tfixed the if-then-else ladder solution becomes either too long, orbecomes cluttered up with ugly #ifdef/#endif statements to selectivelycompile the wanted commands. Another problem is that the actualimplementation of the command is not in the same compilation unit asthe command line parser, leaving room for discrepancies between thecaller and the actual command.To overcome those difficulties, a modular command parser wasintroduced in blob-2.0.5. This file documents that command parser.Note that although this document talks about "blob", the same commandparser is also used in the "diag" diagnostics utility.* Advantages============The modular command parser has the following advantages:- Modular (doh!)- Supports command abbreviation- Simple to use- Easy to extend the number of commands without changing the command  parser itself- Integrated help functionality* How it works==============** The commandlist------------------The key concept in the command parser is the "commandlist", which isdefined in src/lib/command.c as commandlist_t *commands. Early in theinitialisation of blob this pointer is initialised with the contentsof a special ELF section and transformed into a linked list ofcommands. The actual parser uses this linked list to compare thecommand on the command line with each of the individual commandlistentries.** Creating command list entries--------------------------------Command list entries are created with the __commandlist() macro asdefined in include/blob/command.h:	#define __commandlist(fn, nm, hlp) \	static commandlist_t __command_##fn __command = { \		magic:    COMMAND_MAGIC, \		name:     nm, \		help:     hlp, \		callback: fn }In other words: it fills out a commandlist_t entry with the callbackfunction, ASCII name, and help text. The __command attribute is againa macro:	#define __command __attribute__((unused, __section__(".commandlist")))This tells the compiler to put the commandlist_t entry in the.commandlist ELF section instead of the default .data section. The"unused" part tells the compiler not to discard the entry although itis unused in this compilation unit (remember the entry is declared asstatic to avoid name space clashes with other compilation units).** Fun with linker magic------------------------Without a proper linker script the linker would happily discard allELF sections it doesn't know about, so the carefully crafted commandlist entries would be lost. To avoid this, blob uses a linker scriptwhich (among other things) describes how to deal with the command listentries:	. = ALIGN(4);	.commandlist : {		__commandlist_start = .;		*(.commandlist)		__commandlist_end = .;	}This tells the linker to start an output section called .commandlistat a 4 byte aligned address, defines the __commandlist_start symbolwith that address, groups all .commandlist input sections (containingall commandlist_t entries) into the output section, and defines the__commandlist_end symbol with the current address.In this way we have created a list of commands nicely bounded by__commandlist_start and __commandlist_end. During the initialisationof blob those two addresses are used to find the correct number ofcommands. The advantage is clear: the command parser has been madeindependent on the number of commands.** Parsing and executing------------------------After all this linker magic, parsing the command list has been madevery easy: take the command from the command line, and compare it withcommandlist_t->name of all the commands in the command list. If thename matches, we have found our command and can start executingcommandlist_t->callback().* Usage=======Using this all is very simple. First of all, all commands should bedeclared as static functions (to avoid name space pollution) and havethis format:	static int command(int argc, char *argv[]);This looks pretty much like the main() function in normal C programs,and the argc and argv parameters have exactly the same meaning: argccontains the number of arguments to the function, argv is an arraywith pointers to each argument. The first pointer in argv points tothe name of the called command. The command should return 0 in case ofsuccess, or a negative error number otherwise (seeinclude/blob/errno.h for error number definitions).To get the function registered into the command list, simply write aline with a brief help text (again static) about the command, and usethe __commandlist() macro to register it:	static char commandhelp[] = "help on command\n";	__commandlist(command, "command", commandhelp);That's all there is.It's good practice to put both the help text and the __commandlist()definition just below the function that implements the command, sothere is less chance for them to get discrepancies.* Example=========Here is a longer example:static int example_command(int argc, char *argv[]){	int i;	for(i = 0; i < argc; i++) {		SerialOutputString("argv[");		SerialOutputDec(i);		SerialOutputString("] = \"");		SerialOutputString(argv[i]);		SerialOutputString("\"\n");	}	return 0;}static char examplehelp[] = "example command\n";__commandlist(example_command, "example", examplehelp);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -