コード例 #1
0
static void process_ansi(FILE * fp)
{
	int i, c;
	char buf[MAX_ANSIBUF];
	char *numstart, *endptr;

	c = getc(fp);
	if (c != '[') {
		ungetc(c, fp);
		return;
	}
	for (i = 0; i < MAX_ANSIBUF; i++) {
		c = getc(fp);
		/* COLOUR SEQUENCE ENDS in 'm' */
		if (c == 'm') {
			buf[i] = '\0';
			break;
		}
		if (c < '0' && c > '9' && c != ';') {
			while (--i >= 0)
				ungetc(buf[i], fp);
			return;
		}
		buf[i] = (char)c;
	}
	/*
	 * buf now contains a semicolon-separated list of decimal integers,
	 * each indicating an attribute to apply.
	 * For example, buf might contain "0;1;31", derived from the color
	 * escape sequence "<ESC>[0;1;31m". There can be 1 or more
	 * attributes to apply, but typically there are between 1 and 3.
	 */

	if (*endptr == '\0') set_ansi_attribute(0); /* [m treated as [0m */

	for (endptr = numstart = buf; *endptr != '\0'; numstart = endptr + 1)
		set_ansi_attribute(strtol(numstart, &endptr, 10));
}
コード例 #2
0
ファイル: watch.c プロジェクト: whit537/watch
static void process_ansi(FILE *fp)
{
  int i,c, num1, num2;
  char buf[MAX_ANSIBUF];
  char *nextnum;


  c= getc(fp);
  if (c != '[') {
    ungetc(c, fp);
    return;
  }
  for(i=0; i<MAX_ANSIBUF; i++)
  {
    c = getc(fp);
    if (c == 'm') //COLOUR SEQUENCE ENDS in 'm'
    {
      buf[i] = '\0';
      break;
    }
    if (c < '0' && c > '9' && c != ';')
    {
      while(--i >= 0)
        ungetc(buf[i],fp);
      return;
    }
    buf[i] = (char)c;
  }
  num1 = strtol(buf, &nextnum, 10);
  if (nextnum != buf && nextnum[0] != '\0')
    num2 = strtol(nextnum+1, NULL, 10);
  else
    num2 = -1;
  set_ansi_attribute(num1);
  set_ansi_attribute(num2);
}