免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 850 | 回复: 1
打印 上一主题 下一主题

如何实现setup中的简单色彩形式 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-08-22 21:01 |只看该作者 |倒序浏览
知道比如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, "\r\n#");
                  if (p != NULL) {
                     *p = '\0';
                  }

                  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 = '\0';
                        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] != '\0') &&
             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] = '\0';
         j--;
      }
      newtListboxAddEntry(lbox, setuptools, (void*) i);
#ifdef DEBUG
      fprintf(stderr, "%s = %s\n", 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");

论坛徽章:
0
2 [报告]
发表于 2006-08-22 21:10 |只看该作者
原帖由 stackman 于 2006-8-22 21:01 发表
知道比如less后底部的文字反白的是在printf中加入了[033等

但不知道是哪些系数控制了颜色。

下面附上setuptool的src。(rpm2cpio ...得到)

setup.c

#ifdef HAVE_CONFIG_H
#include "config.h& ...


>>
>>
>>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 "\033[40;32m"
>>    System V:       /usr/bin/echo "\033[40;32m"
>>    Generic Method: /usr/bin/echo "<ctrl-v><escape>[40;32m"
>>
>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP