第一次实验

Scripts解析
sbj 8 months ago
parent ec30ba2a3e
commit 7a3266e2be

@ -3,166 +3,185 @@
* Released under the terms of the GNU GPL v2.0. * Released under the terms of the GNU GPL v2.0.
*/ */
#include <locale.h> #include <locale.h> // 引入本地化相关功能的头文件
#include <ctype.h> #include <ctype.h> // 引入字符处理函数的头文件,例如 isspace、isdigit 等
#include <stdio.h> #include <stdio.h> // 引入标准输入输出库
#include <stdlib.h> #include <stdlib.h> // 引入标准库,提供内存管理、程序退出等功能
#include <string.h> #include <string.h> // 引入字符串处理函数库
#include <time.h> #include <time.h> // 引入时间日期相关函数
#include <unistd.h> #include <unistd.h> // 引入 Unix 系统 API提供与系统交互的功能
#include <getopt.h> #include <getopt.h> // 引入命令行参数解析函数
#include <sys/stat.h> #include <sys/stat.h> // 引入文件状态函数
#include <sys/time.h> #include <sys/time.h> // 引入时间管理函数
#include <errno.h> #include <errno.h> // 引入错误码定义
#include "lkc.h" // 引入项目自定义的头文件
#include "lkc.h"
// 函数声明
static void conf(struct menu *menu); static void conf(struct menu *menu); // 配置函数
static void check_conf(struct menu *menu); static void check_conf(struct menu *menu); // 检查配置函数
static void xfgets(char *str, int size, FILE *in); static void xfgets(char *str, int size, FILE *in); // 自定义的 fgets 函数
// 定义输入模式枚举类型
enum input_mode { enum input_mode {
oldaskconfig, oldaskconfig, // 用户交互模式
silentoldconfig, silentoldconfig, // 无用户交互模式,使用默认值
oldconfig, oldconfig, // 使用旧的配置
allnoconfig, allnoconfig, // 所有选项都为 no
allyesconfig, allyesconfig, // 所有选项都为 yes
allmodconfig, allmodconfig, // 所有选项都为模块
alldefconfig, alldefconfig, // 所有选项都为默认值
randconfig, randconfig, // 随机配置
defconfig, defconfig, // 使用默认配置
savedefconfig, savedefconfig, // 保存默认配置
listnewconfig, listnewconfig, // 列出新的配置项
olddefconfig, olddefconfig, // 使用旧的默认配置
} input_mode = oldaskconfig; } input_mode = oldaskconfig; // 设置默认输入模式为 oldaskconfig
static int indent = 1; // 静态变量声明
static int tty_stdio; static int indent = 1; // 配置输出时的缩进级别
static int valid_stdin = 1; static int tty_stdio; // 标识标准输入输出是否是终端设备
static int sync_kconfig; static int valid_stdin = 1; // 标识标准输入是否有效
static int conf_cnt; static int sync_kconfig; // 同步配置标志
static char line[128]; static int conf_cnt; // 配置项计数器
static struct menu *rootEntry; static char line[128]; // 用于存储输入行的缓冲区
static struct menu *rootEntry; // 配置菜单的根节点指针
// 打印帮助信息
static void print_help(struct menu *menu) static void print_help(struct menu *menu)
{ {
struct gstr help = str_new(); struct gstr help = str_new(); // 创建一个新的字符串对象
menu_get_ext_help(menu, &help); menu_get_ext_help(menu, &help); // 获取菜单项的扩展帮助信息
printf("\n%s\n", str_get(&help)); printf("\n%s\n", str_get(&help)); // 打印帮助信息
str_free(&help); str_free(&help); // 释放帮助字符串资源
} }
// 去除字符串两端的空白字符
static void strip(char *str) static void strip(char *str)
{ {
char *p = str; char *p = str;
int l; int l;
while ((isspace(*p))) // 跳过前导空格
p++; while ((isspace(*p)))
l = strlen(p); p++;
if (p != str)
memmove(str, p, l + 1); l = strlen(p); // 获取字符串的长度
if (!l) if (p != str) // 如果有前导空格,移动字符串内容
return; memmove(str, p, l + 1);
p = str + l - 1;
while ((isspace(*p))) if (!l) // 如果字符串为空,返回
*p-- = 0; return;
// 去除尾部空格
p = str + l - 1;
while ((isspace(*p))) // 从字符串末尾向前查找空格并去除
*p-- = 0;
} }
// 检查标准输入是否有效
static void check_stdin(void) static void check_stdin(void)
{ {
if (!valid_stdin) { if (!valid_stdin) { // 如果标准输入无效
printf(_("aborted!\n\n")); printf(_("aborted!\n\n"));
printf(_("Console input/output is redirected. ")); printf(_("Console input/output is redirected. "));
printf(_("Run 'make oldconfig' to update configuration.\n\n")); printf(_("Run 'make oldconfig' to update configuration.\n\n"));
exit(1); exit(1); // 退出程序
} }
} }
// 配置选项的处理函数
static int conf_askvalue(struct symbol *sym, const char *def) static int conf_askvalue(struct symbol *sym, const char *def)
{ {
enum symbol_type type = sym_get_type(sym); enum symbol_type type = sym_get_type(sym); // 获取符号的类型
if (!sym_has_value(sym)) if (!sym_has_value(sym)) // 如果没有值,标记为新选项
printf(_("(NEW) ")); printf(_("(NEW) "));
line[0] = '\n'; line[0] = '\n';
line[1] = 0; line[1] = 0;
if (!sym_is_changable(sym)) { if (!sym_is_changable(sym)) { // 如果符号不可修改,直接输出默认值
printf("%s\n", def); printf("%s\n", def);
line[0] = '\n'; line[0] = '\n';
line[1] = 0; line[1] = 0;
return 0; return 0;
} }
switch (input_mode) { // 根据输入模式不同执行不同的操作
case oldconfig: switch (input_mode) {
case silentoldconfig: case oldconfig:
if (sym_has_value(sym)) { case silentoldconfig:
printf("%s\n", def); if (sym_has_value(sym)) { // 如果已有值,直接输出默认值
return 0; printf("%s\n", def);
} return 0;
check_stdin(); }
/* fall through */ check_stdin(); // 检查标准输入有效性
case oldaskconfig: /* fall through */
fflush(stdout); case oldaskconfig:
xfgets(line, 128, stdin); fflush(stdout); // 刷新标准输出缓冲区
if (!tty_stdio) xfgets(line, 128, stdin); // 获取用户输入
printf("\n"); if (!tty_stdio) // 如果标准输入不是终端,换行
return 1; printf("\n");
default: return 1;
break; default:
} break;
}
switch (type) {
case S_INT: // 根据符号类型进行处理
case S_HEX: switch (type) {
case S_STRING: case S_INT:
printf("%s\n", def); case S_HEX:
return 1; case S_STRING:
default: printf("%s\n", def);
; return 1;
} default:
printf("%s", line); ;
return 1; }
printf("%s", line); // 输出用户输入的内容
return 1;
} }
// 配置字符串类型的处理函数
static int conf_string(struct menu *menu) static int conf_string(struct menu *menu)
{ {
struct symbol *sym = menu->sym; struct symbol *sym = menu->sym;
const char *def; const char *def;
while (1) { while (1) {
printf("%*s%s ", indent - 1, "", _(menu->prompt->text)); printf("%*s%s ", indent - 1, "", _(menu->prompt->text)); // 打印菜单提示
printf("(%s) ", sym->name); printf("(%s) ", sym->name); // 打印符号名称
def = sym_get_string_value(sym); def = sym_get_string_value(sym); // 获取符号的默认字符串值
if (sym_get_string_value(sym)) if (def) // 如果有默认值,打印出来
printf("[%s] ", def); printf("[%s] ", def);
if (!conf_askvalue(sym, def)) if (!conf_askvalue(sym, def)) // 获取用户输入并处理
return 0; return 0;
switch (line[0]) {
case '\n': // 根据用户输入的内容进行处理
break; switch (line[0]) {
case '?': case '\n':
/* print help */ break; // 如果用户没有输入内容,继续
if (line[1] == '\n') { case '?':
print_help(menu); // 打印帮助信息
def = NULL; if (line[1] == '\n') {
break; print_help(menu); // 打印帮助
} def = NULL;
/* fall through */ break;
default: }
line[strlen(line)-1] = 0; /* fall through */
def = line; default:
} line[strlen(line) - 1] = 0; // 去掉换行符
if (def && sym_set_string_value(sym, def)) def = line; // 将用户输入的内容赋给 def
return 0; }
}
// 设置符号的字符串值
if (def && sym_set_string_value(sym, def))
return 0;
}
} }
static int conf_sym(struct menu *menu) static int conf_sym(struct menu *menu)
{ {
struct symbol *sym = menu->sym; struct symbol *sym = menu->sym;

Loading…
Cancel
Save