1 /*
2  *
3  * pipe to ansi .c
4  *
5  * Written by Kraig Amador and Joshua J. Drake
6  *
7  * convert |xx codes to ansi codes
8  * see colors.txt for more info.
9  */
10 
11 #include "irc.h"
12 #include "ircaux.h"
13 #include "p2a.h"
14 #include "ninja.h"
15 
16 u_char *
pipe_to_ansi(string)17 pipe_to_ansi(string)
18    u_char *string;
19 {
20    u_char *ptr, tmp[32], tmp2[32], tmp3[32], *fptr = UNULL;
21    static u_char final[BIG_BUFFER_SIZE];
22    int b, f, lastf = 7, bright = 0, blink = 0;
23 
24    if (!string || !*string)
25       return empty_string;
26 
27    final[0] = '\0';
28    fptr = final;
29    ptr = string;
30    while (*ptr && (fptr - final) < sizeof(final)-1)
31      {
32 	if (*ptr == '|' && *(ptr + 1) && *(ptr + 2) && isxdigit(ptr[1]) && isxdigit(ptr[2]))
33 	  {
34 	     sscanf(ptr, "|%1x%1x", &b, &f);
35 	     *tmp = '\0';
36 	     *tmp2 = '\0';
37 	     *tmp3 = '\0';
38 	     if (f == 0 && b == 0)
39 	       {
40 		  f = 7;
41 		  lastf = 0;
42 	       }
43 	     if (lastf != f)
44 	       {
45 		  if (f > 7)
46 		    {
47 		       if ((f == 15) && (lastf == 7))
48 			 my_strncpy(tmp2, "1;", sizeof(tmp2)-1);
49 		       else
50 			 snprintf(tmp2, sizeof(tmp2)-1, "%s3%d;", bright ? "" : "1;", (f - 8));
51 		    }
52 		  else if (f < 8)
53 		    {
54 		       if (f == 7)
55 			 my_strncpy(tmp2, "0;", sizeof(tmp2)-1);
56 		       else
57 			 snprintf(tmp2, sizeof(tmp2)-1, "%s3%d;", bright ? "0;" : "", f);
58 		    }
59 		  tmp2[sizeof(tmp2)-1] = '\0';
60 	       }
61 
62 	     if (b > 7)
63 	       snprintf(tmp3, sizeof(tmp3)-1, "5;4%d", (b - 8));
64 	     else if (b < 8)
65 	       snprintf(tmp3, sizeof(tmp3)-1, "4%d", b);
66 	     tmp3[sizeof(tmp3)-1] = '\0';
67 
68 	     snprintf(tmp, sizeof(tmp)-1, "[%s%sm", tmp2, tmp3);
69 	     tmp[sizeof(tmp)-1] = '\0';
70 	     lastf = f;
71 	     bright = (f > 7) ? 1 : 0;
72 	     blink = (b > 7) ? 1 : 0;
73 	     ptr += 3;
74 
75 	     strmcat(final, tmp, sizeof(final)-1);
76 	     fptr += my_strlen(tmp);
77 	  }
78 	else
79 	  *fptr++ = *ptr++;
80      }
81    /* terminate! */
82    if (fptr - final >= sizeof(final)-1)
83      fptr = final + sizeof(final) - 1;
84    *fptr = '\0';
85    return final;
86 }
87