Repository: jiejieTop/cmd-parser
Branch: master
Commit: 2fb348d44210
Files: 6
Total size: 17.3 KB
Directory structure:
gitextract_wra0t7qr/
├── .gitignore
├── LICENSE
├── README.md
├── cmd.c
├── cmd.h
└── test.c
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
.vscode
================================================
FILE: LICENSE
================================================
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
================================================
FILE: README.md
================================================

[](https://github.com/jiejieTop/cmd-parser/blob/master/LICENSE)

# cmd-parser
一个非常简单好用的命令解析器...
这个命令解析器有什么用??
> 简单来说,我希望我的开发板,可以通过命令执行一些处理,比如说我用串口发一个命令`A`,开发板就执行`A`的一些处理,或者,在调试某些`AT`模组的时候,当我收到模组返回的一些指令后,自动执行一些处理。当然,还有其他的地方可以用得上的,兄弟们自行挖掘!!欢迎提交**pr**与点个**star**~
## 问题
欢迎以 [GitHub Issues](https://github.com/jiejieTop/mqttclient/issues) 的形式提交问题和bug报告
## 版权和许可
cmd-parser 遵循 [Apache License v2.0](https://github.com/jiejieTop/mqttclient/blob/master/LICENSE) 开源协议。鼓励代码共享和尊重原作者的著作权,可以自由的使用、修改源代码,也可以将修改后的代码作为开源或闭源软件发布,**但必须保留原作者版权声明**。
## 使用方法
1. 注册命令
在工程中的任意位置均可调用(在函数外)
```c
REGISTER_CMD(test1, test1_cmd);
```
2. cmd初始化
```c
cmd_init();
```
3. 解析命令
```c
cmd_parsing("test1");
```
## 特色
- 用户无需关心命令的存储区域与大小,由编译器静态分配。
- 加入哈希算法超快速匹配命令,时间复杂度从O(n*m)变为O(n)。
- 命令支持忽略大小写。
- 非常易用与非常简洁的代码(不足150行)。
## 注意事项
- 本代码目前只支持`MDK`与`IAR`的编译器,对于`gcc`尚未移植,欢迎参与贡献
## test.c
```c
#include "cmd.h"
void test1_cmd(void)
{
printf("test1_cmd\n");
}
void test2_cmd(void)
{
printf("test2_cmd\n");
}
void test3_cmd(void)
{
printf("test3_cmd\n");
}
REGISTER_CMD(test1, test1_cmd);
REGISTER_CMD(test2, test2_cmd);
REGISTER_CMD(test3, test3_cmd);
int main(void)
{
cmd_init();
while (1)
{
cmd_parsing("test1");
cmd_parsing("test2");
cmd_parsing("test3");
}
return 0;
}
```
================================================
FILE: cmd.c
================================================
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-13 10:47:30
* @LastEditTime: 2020-05-16 15:53:35
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#include "cmd.h"
#include <stdio.h>
static cmd_t *_cmd_begin, *_cmd_end;
static int _cmd_to_lower(int c)
{
if ((c >= 'A') && (c <= 'Z'))
return c + ('a' - 'A');
return c;
}
static unsigned int _cmd_hash(const char* str)
{
int tmp, c = *str;
unsigned int seed = CMD_HASH; /* 'jiejie' string hash */
unsigned int hash = 0;
while(*str) {
tmp = _cmd_to_lower(c);
hash = (hash ^ seed) + tmp;
str++;
c = *str;
}
return hash;
}
static void _cmd_init(const void *begin, const void *end)
{
_cmd_begin = (cmd_t*) begin;
_cmd_end = (cmd_t*) end;
}
static cmd_t* _get_next_cmd(cmd_t *cmd)
{
unsigned int *ptr;
ptr = (unsigned int*) (cmd + 1);
while ((*ptr == 0) && ((unsigned int*)ptr < (unsigned int*) _cmd_end))
ptr ++;
return (cmd_t*) ptr;
}
static int _cmd_match(const char *str, const char *cmd)
{
int c1, c2;
do {
c1 = _cmd_to_lower(*str++);
c2 = _cmd_to_lower(*cmd++);
} while((c1 == c2) && c1);
return c1 - c2;
}
static void _list(void)
{
cmd_t *index;
for (index = _cmd_begin; index < _cmd_end; index = _get_next_cmd(index)) {
printf("%s -->%s\n",index->cmd,index->desc);
}
}
REGISTER_CMD(_list, _list,list all command);
void cmd_init(void)
{
cmd_t *index;
#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM C Compiler */
extern const int CMDS$$Base;
extern const int CMDS$$Limit;
_cmd_init(&CMDS$$Base, &CMDS$$Limit);
#elif defined (__ICCARM__) || defined(__ICCRX__) /* for IAR Compiler */
_cmd_init(__section_begin("CMDS"), __section_end("CMDS"));
#endif
for (index = _cmd_begin; index < _cmd_end; index = _get_next_cmd(index)) {
index->hash = _cmd_hash(index->cmd);
}
}
void cmd_parsing(char *str)
{
cmd_t *index;
unsigned int hash = _cmd_hash(str);
for (index = _cmd_begin; index < _cmd_end; index = _get_next_cmd(index)) {
if (hash == index->hash) {
if (_cmd_match(str, index->cmd) == 0) {
index->handler();
break;
}
}
}
}
================================================
FILE: cmd.h
================================================
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-13 10:48:52
* @LastEditTime: 2019-12-16 21:26:56
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#ifndef _CMD_H_
#define _CMD_H_
#define CMD_HASH 0xb433e5c6
#if defined(__CC_ARM) || defined(__CLANG_ARM) /* ARM Compiler */
#define SECTION(x) __attribute__((section(x)))
#define CMD_USED __attribute__((used))
#elif defined (__IAR_SYSTEMS_ICC__) /* IAR Compiler */
#define SECTION(x) @ x
#define CMD_USED __root
#else
#error "not supported tool chain..."
#endif
typedef void (*cmd_handler)(void);
typedef struct cmd {
const char* cmd;
const char* cmd_mess;
unsigned int hash;
cmd_handler handler;
} cmd_t;
#define REGISTER_CMD(cmd, handler,desc) \
const char _register_##cmd##_cmd[] = #cmd; \
const char _register_##cmd##_desc[] = #desc; \
CMD_USED cmd_t _register_##cmd SECTION("CMDS")= \
{ \
_register_##cmd##_cmd, \
_register_##cmd##_desc, \
(unsigned int)CMD_HASH, \
(cmd_handler)&handler \
};
void cmd_init(void);
void cmd_parsing(char *str);
#endif
================================================
FILE: test.c
================================================
/*
* @Author: jiejie
* @Github: https://github.com/jiejieTop
* @Date: 2019-12-13 19:15:30
* @LastEditTime: 2019-12-17 13:38:49
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
*/
#include "cmd.h"
void test1_cmd(void)
{
printf("test1_cmd\n");
}
void test2_cmd(void)
{
printf("test2_cmd\n");
}
void test3_cmd(void)
{
printf("test3_cmd\n");
}
REGISTER_CMD(test1, test1_cmd,test1 demo);
REGISTER_CMD(test2, test2_cmd,test2 demo);
REGISTER_CMD(test3, test3_cmd,test3 demo);
int main(void)
{
cmd_init();
while (1)
{
cmd_parsing("_list");
cmd_parsing("test1");
cmd_parsing("test2");
cmd_parsing("test3");
delay(1000);
}
return 0;
}
gitextract_wra0t7qr/ ├── .gitignore ├── LICENSE ├── README.md ├── cmd.c ├── cmd.h └── test.c
SYMBOL INDEX (13 symbols across 3 files)
FILE: cmd.c
function _cmd_to_lower (line 13) | static int _cmd_to_lower(int c)
function _cmd_hash (line 20) | static unsigned int _cmd_hash(const char* str)
function _cmd_init (line 35) | static void _cmd_init(const void *begin, const void *end)
function cmd_t (line 41) | static cmd_t* _get_next_cmd(cmd_t *cmd)
function _cmd_match (line 51) | static int _cmd_match(const char *str, const char *cmd)
function _list (line 63) | static void _list(void)
function cmd_init (line 72) | void cmd_init(void)
function cmd_parsing (line 90) | void cmd_parsing(char *str)
FILE: cmd.h
type cmd_t (line 27) | typedef struct cmd {
FILE: test.c
function test1_cmd (line 10) | void test1_cmd(void)
function test2_cmd (line 15) | void test2_cmd(void)
function test3_cmd (line 20) | void test3_cmd(void)
function main (line 29) | int main(void)
Condensed preview — 6 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (19K chars).
[
{
"path": ".gitignore",
"chars": 8,
"preview": ".vscode\n"
},
{
"path": "LICENSE",
"chars": 11357,
"preview": " Apache License\n Version 2.0, January 2004\n "
},
{
"path": "README.md",
"chars": 1514,
"preview": "\n[. The extraction includes 6 files (17.3 KB), approximately 4.4k tokens, and a symbol index with 13 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.