日常分享 一个小C 调试工具

编程入门 行业动态 更新时间:2024-10-04 03:23:41

<a href=https://www.elefans.com/category/jswz/34/1770030.html style=日常分享 一个小C 调试工具"/>

日常分享 一个小C 调试工具

同时也自定义了自己的shell

没啥事儿,鼓捣,交互机的命令行不就是这么封装出来的,开机进入的是定义好的交换机,shell界面

上个虚拟机测试下

#include "cmd_list.h"
#include "list.h"
#include <arpa/inet.h>
#include <netinet/in.h>
#include <readline/history.h>
#include <readline/readline.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>
#include <sys/select.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>char *dupstr(char *s) {char *r;r = (char *)calloc(strlen(s) + 1, sizeof(char));strcpy(r, s);return (r);
}char *cmd_generator(const char *text, int state) {static int i, len;char *name;if (!state) {i = 0;len = (int)strlen(text);}while ((name = cmd_table[i].name)) { // 检索命令行并进行补全;i++;if (strncmp(name, text, len) == 0) {return dupstr(name);}}return NULL;
}char **debug_cli_completion(const char *text, int start, int end) {char **matches;matches = (char **)NULL;if (start == 0) {matches = rl_completion_matches(text, cmd_generator);}return matches;
}void init_readline(void) {rl_readline_name = "oran@debug # ";rl_attempted_completion_function = debug_cli_completion;
}char *stripwhite(char *string) {register char *s, *t;for (s = string; whitespace(*s); s++) {;}if (*s == 0) {return s;}t = s + strlen(s) - 1;while (t > s && whitespace(*t)) {t--;}*++t = 0;return s;
}int32_t parse2cmd(int32_t argc, char *argv[]) {char *name = NULL;int32_t i = 0;if (strstr(argv[0], ".lua") != NULL) { //使用LUA 解释器 源码执行的关键部分printf("[ exec lua scripts ]: %s ... ...\n", argv[0]);return 0;}for (i = 0; i < CMD_COUNTS - 1; i++) {if (!strcmp(argv[0], cmd_table[i].name)) {cmd_table[i].exec(--argc, ++argv);break;}}return 0;
}void parse_cmd(char *s) {if (s == NULL || strlen(s) == 0) {return;}char *tmp;char *argv[256] = {NULL};int32_t i = 0;while ((tmp = strsep(&s, " "))) {if (i >= sizeof(argv) / sizeof(argv[0])) {printf("cmd and args over: cmd's args max 255 ... ...\n");break;}argv[i++] = tmp;}parse2cmd(i, argv);
}void cmdline() {char *line = NULL;char *s = NULL;init_readline();while (1) {const char display[] = "oran@debug # ";line = readline(display);if (!line)break;s = stripwhite(line);if (*s) {add_history(s);if (!strcmp(s, "quit") || !strcmp(s, "q") || !strcmp(s, "exit")) {exit(0);}parse_cmd(s);}free(line);line = NULL;}exit(0);
}int main(int argc, char **argv) {if (argc > 1) {parse2cmd(--argc, ++argv);} else {cmdline();}return 0;
}

更多推荐

日常分享 一个小C 调试工具

本文发布于:2024-02-27 20:40:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1766295.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日常   调试工具

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!