File: bold.c

package info (click to toggle)
esh 0.8-7
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 508 kB
  • ctags: 542
  • sloc: ansic: 3,608; lisp: 166; makefile: 72
file content (91 lines) | stat: -rw-r--r-- 1,549 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* 
 * esh, the Unix shell with Lisp-like syntax. 
 * Copyright (C) 1999  Ivan Tkatchev
 * This source code is under the GPL.
 */


#include <stdlib.h>
#include <stdio.h>

char colorspec[10];

char* colors[] = {
  "black",   "30",
  "red",     "31",
  "green",   "32",
  "yellow",  "33",
  "blue",    "34",
  "magenta", "35",
  "cyan",    "36",
  "white",   "37",
  NULL, NULL
};

char* flags[] = {
  "none",       "00",
  "bold",       "01",
  "underscore", "04",
  "blink",      "05",
  "reverse",    "07",
  "concealed",  "08",
  NULL, NULL
};

int main(int argc, char** argv) {
  char* ss = "\033[%s;%sm";
  char* flag = NULL;
  char* color = NULL;
  char* se = "\033[00m";
  int chr;
  int i = 0;

  if (argc != 3) {
    printf("Usage: %s <flag> <color>\n"
	   "Where <color> is one of:\n"
	   "black red green yellow blue magenta cyan white\n"
	   "And <flag> is one of:\n"
	   "none bold underscore blink reverse concealed\n",
	   argv[0]);
    exit(EXIT_FAILURE);
  }

  while (flags[i]) {
    if (strcmp(flags[i], argv[1]) == 0) {
      flag = flags[i+1];
    }
    
    i += 2;
  }

  i = 0;

  while (colors[i]) {
    if (strcmp(colors[i], argv[2]) == 0) {
      color = colors[i+1];
    }
    
    i += 2;
  }

  if (!flag || !color) {
    fprintf(stderr, "bold: either color or flag argument is wrong.\n");
    return EXIT_FAILURE;
  }

  sprintf(colorspec, ss, flag, color);


  while (1) {
    chr = getchar();

    if (chr == EOF) break;

    printf("%s%c%s", colorspec, chr, se);
    fflush(stdout);
  }

  return EXIT_SUCCESS;
}