如何实现setup中的简单色彩形式
知道比如less后底部的文字反白的是在printf中加入了[033等
但不知道是哪些系数控制了颜色。
下面附上setuptool的src。(rpm2cpio ...得到)
setup.c
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <ctype.h>
#include <dirent.h>
#include <libintl.h>
#include <limits.h>
#include <locale.h>
#include <newt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define _(String) gettext(String)
struct tool {
char *name;
char *path;
char *namedomain;
char *namepath;
char *file;
};
static int comparetools(const void *t1, const void *t2)
{
const struct tool *tool1, *tool2;
int i;
tool1 = t1;
tool2 = t2;
i = strcoll(tool1->name, tool2->name);
if (i != 0) {
return i;
}
return strcoll(tool1->file, tool2->file);
}
int main(int argc, char **argv) {
newtComponent lbox, run, quit, form,answer;
newtGrid grid,grid2;
int rc, i, j, command;
char roottext[81], *p, *q, c;
struct tool *tools;
size_t n_tools;
DIR *setuptoold;
FILE *setuptoolf;
char setuptools[LINE_MAX];
struct dirent *dent;
setlocale(LC_ALL, "");
bindtextdomain(GETTEXT_PACKAGE, "/usr/share/locale");
textdomain(GETTEXT_PACKAGE);
if (getuid()) {
fprintf(stderr,
_("nERROR - You must be root to run setup.n"));
exit(1);
}
tools = NULL;
n_tools = 0;
if ((setuptoold = opendir(SETUPTOOLDDIR)) != NULL) {
while ((dent = readdir(setuptoold)) != NULL) {
snprintf(setuptools, sizeof(setuptools), "%s/%.*s", SETUPTOOLDDIR,
NAME_MAX, dent->d_name);
if ((strstr(setuptools, "/.") == NULL) &&
(strstr(setuptools, ".rpmorig") == NULL) &&
(strstr(setuptools, ".rpmsave") == NULL) &&
(strstr(setuptools, ".rpmnew") == NULL) &&
(strpbrk(setuptools, "!~#") == NULL)) {
if ((setuptoolf = fopen(setuptools, "r")) != NULL) {
while (fgets(setuptools, sizeof(setuptools),
setuptoolf) != NULL) {
p = strpbrk(setuptools, "rn#");
if (p != NULL) {
*p = '';
}
p = strtok(setuptools, "|");
if (strlen(p) == 0) {
continue;
}
if (access(p, X_OK) != 0) {
q = strpbrk(p, " t");
if ((q != NULL) && (q > p)) {
c = *q;
*q = '';
if (access(p, X_OK) != 0) {
continue;
}
*q = c;
} else {
continue;
}
}
tools = realloc(tools, sizeof(struct tool) * (n_tools + 1));
if (tools == NULL) {
continue;
}
memset(&tools[n_tools], 0, sizeof(tools[n_tools]));
tools[n_tools].path = strdup(p);
tools[n_tools].name = tools[n_tools].path;
tools[n_tools].namedomain = GETTEXT_PACKAGE;
tools[n_tools].namepath = "/usr/share/locale";
tools[n_tools].file = strdup(setuptools);
p = strtok(NULL, "|");
if (p == NULL) {
n_tools++;
continue;
}
tools[n_tools].name = strdup(p);
p = strtok(NULL, "|");
if (p == NULL) {
n_tools++;
continue;
}
if (strlen(p) > 0) {
tools[n_tools].namedomain = strdup(p);
}
p = strtok(NULL, "|");
if (p == NULL) {
n_tools++;
continue;
}
if (strlen(p) > 0) {
tools[n_tools].namepath = strdup(p);
}
n_tools++;
}
fclose(setuptoolf);
}
}
}
closedir(setuptoold);
}
for (i = 0; i < n_tools; i++) {
bindtextdomain(tools[i].namedomain, tools[i].namepath);
tools[i].name = dgettext(tools[i].namedomain, tools[i].name);
}
qsort(tools, n_tools, sizeof(tools[0]), comparetools);
for (i = 0; i < n_tools - 1; i++) {
while (i < n_tools - 1 &&
strcmp(tools[i].name, tools[i + 1].name) == 0) {
memmove(&tools[i + 1], &tools[i + 2],
sizeof(tools[i + 2]) * (n_tools - (i + 2)));
n_tools--;
}
}
lbox = newtListbox(2, 1, n_tools, NEWT_FLAG_RETURNEXIT);
for (i = 0; i < n_tools; i++) {
snprintf(setuptools, sizeof(setuptools), " %s ", tools[i].name);
while ((setuptools[1] != '') &&
isspace((unsigned char)setuptools[1])) {
memmove(setuptools + 1, setuptools + 2, strlen(setuptools + 1));
}
j = strlen(setuptools);
while ((j > 2) && isspace((unsigned char)setuptools[j - 2])) {
setuptools[j - 1] = '';
j--;
}
newtListboxAddEntry(lbox, setuptools, (void*) i);
#ifdef DEBUG
fprintf(stderr, "%s = %sn", setuptools, tools[i].path);
#endif
}
newtInit();
newtCls();
newtPushHelpLine(_(" <Tab>/<Alt-Tab> between elements |"
" Use <Enter> to edit a selection") );
snprintf(roottext, sizeof(roottext),
_("Text Mode Setup Utility %-28s (c) 1999-2004 Red Hat, Inc."),
VERSION);
newtDrawRootText(0, 0, roottext);
form = newtForm(NULL, NULL, 0);
grid = newtCreateGrid(1, 2);
grid2 = newtButtonBar(_("Run Tool"), &run, _("Quit"), &quit, NULL);
newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, lbox,
1, 0, 1, 0, NEWT_ANCHOR_TOP, 0);
newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, grid2,
0, 1, 0, 0, NEWT_ANCHOR_TOP, 0);
newtGridWrappedWindow(grid,_("Choose a Tool"));
newtFormAddComponents(form, lbox, run, quit, NULL);
do {
answer = newtRunForm(form);
if (answer != quit) {
command = (int)newtListboxGetCurrent(lbox);
rc = 0;
} else {
rc = -1;
}
if (rc != -1) {
newtSuspend();
#ifdef DEBUG
fprintf(stderr, "Running "%s".n", tools[command].path);
#endif
system(tools[command].path);
newtResume();
}
} while (rc!=-1);
newtPopWindow();
newtFormDestroy(form);
newtFinished();
return 0;
}
=============================================================
setup.h
const char *unnamed = N_("Authentication configuration");
const char *unnamed = N_("Firewall configuration");
const char *unnamed = N_("Keyboard configuration");
const char *unnamed = N_("Mouse configuration");
const char *unnamed = N_("Network configuration");
const char *unnamed = N_("Printer configuration");
const char *unnamed = N_("Sound card configuration");
const char *unnamed = N_("System services");
const char *unnamed = N_("Timezone configuration");
const char *unnamed = N_("X configuration");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
>>
>>
>>2. 前景 背景 颜色
>> ---------------------------------------
>> 30 40 黑色
>> 31 41 紅色
>> 32 42 綠色
>> 33 43 黃色
>> 34 44 藍色
>> 35 45 紫紅色
>> 36 46 青藍色
>> 37 47 白色
>>
>> 代码 意义
>> -------------------------
>> 0 OFF
>> 1 高亮显示
>> 4 underline
>> 5 闪烁
>> 7 反白显示
>> 8 不可见
>>
>>3. 产生颜色(黑色背景加绿色前景色):
>> sco: setcolor red; echo "abcd"; setcolor white
>> Linux/BSD: /usr/bin/echo -e "33[40;32m"
>> System V: /usr/bin/echo "33[40;32m"
>> Generic Method: /usr/bin/echo "<ctrl-v><escape>[40;32m"
>>
>>