Advertisement
hellionsoldier

Cisco Color 3/22/2012

Mar 22nd, 2012
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.82 KB | None | 0 0
  1. /*
  2. Copyright 2011 Thomas St.Pierre. All rights reserved.
  3.  
  4. Redistribution and use in source and binary forms, with or without modification, are
  5. permitted provided that the following conditions are met:
  6.  
  7.    1. Redistributions of source code must retain the above copyright notice, this list of
  8.       conditions and the following disclaimer.
  9.  
  10.    2. Redistributions in binary form must reproduce the above copyright notice, this list
  11.       of conditions and the following disclaimer in the documentation and/or other materials
  12.       provided with the distribution.
  13.  
  14. THIS SOFTWARE IS PROVIDED BY Thomas St.Pierre ''AS IS'' AND ANY EXPRESS OR IMPLIED
  15. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Thomas St.Pierre OR
  17. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  22. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23.  
  24. The views and conclusions contained in the software and documentation are those of the
  25. authors and should not be interpreted as representing official policies, either expressed
  26. or implied, of Thomas St.Pierre.
  27. */
  28.  
  29. #include <stdio.h>
  30. #include <regex.h>
  31. #include <string.h>
  32.  
  33. #define MAXMATCH 20
  34.  
  35. /* Terminal Color codes */
  36. // Normal
  37. #define TERM_RED            "\033[31m"
  38. #define TERM_GREEN          "\033[32m"
  39. #define TERM_YELLOW         "\033[33m"
  40. #define TERM_BLUE           "\033[34m"
  41. #define TERM_PURPLE         "\033[35m"
  42. #define TERM_CYAN           "\033[36m"
  43. #define TERM_WHITE          "\033[37m"
  44.  
  45. // Brights
  46. #define TERM_BRIGHT_RED     "\033[1;31m"
  47. #define TERM_BRIGHT_GREEN   "\033[1;32m"
  48. #define TERM_BRIGHT_YELLOW  "\033[1;33m"
  49. #define TERM_BRIGHT_BLUE    "\033[1;34m"
  50. #define TERM_BRIGHT_PURPLE  "\033[1;35m"
  51. #define TERM_BRIGHT_CYAN    "\033[1;36m"
  52. #define TERM_BRIGHT_WHITE   "\033[1;37m" // redundant to TERM_WHITE
  53.  
  54. // Darks
  55. // Using cygputty on windows xp 32bit to house cygwin these don't render. may render correctly on a true linux system.
  56. #define TERM_DARK_RED       "\033[2;31m" // doesn't render
  57. #define TERM_DARK_GREEN     "\033[2;32m" // doesn't render
  58. #define TERM_DARK_YELLOW    "\033[2;33m" // doesn't render
  59. #define TERM_DARK_BLUE      "\033[2;34m" // doesn't render
  60. #define TERM_DARK_PURPLE    "\033[2;35m" // doesn't render
  61. #define TERM_DARK_CYAN      "\033[2;36m" // doesn't render
  62. #define TERM_DARK_WHITE     "\033[2;37m" // doesn't render
  63.  
  64. // Backgrounds
  65. #define TERM_BG_GREEN       "\033[42m"
  66. #define TERM_WARNING        "\033[41m\033[1m"
  67.  
  68. // Reset
  69. #define TERM_NONE       "\033[0m"
  70.  
  71. /*
  72.     Define our rules!
  73.     Rules Are defined as a struct, first variable is a regex. The entire line is used to match
  74.     context of what you want to color. Within the regex you use sub-expressions to define
  75.     what to color. For example with the regex "This is (some) context", it will match the
  76.     entire line, but only the word 'some' will be colored. Second variable is an array which
  77.     contains strings that will be inserted right before every matched sub-expression. (usually
  78.     terminal escapes codes, but could be any string)
  79.  
  80.     Last variable is used internally to store compiled regex's
  81. */
  82.  
  83. struct color_match{
  84.                 const char *pattern;
  85.                 const char *insert_before[MAXMATCH];
  86.                 regex_t compiled;
  87.         } patterns [] = {
  88.  
  89. /* my edits */
  90. /* IP Address */
  91.  
  92. /* sh ip int brief highlights */
  93. {"(admin.*) *(down).*$", {TERM_BLUE, TERM_BLUE}},
  94. {"(up) *(up).*$",{TERM_BRIGHT_GREEN, TERM_BRIGHT_GREEN}},
  95. {"(up) *(down).*$",{TERM_YELLOW, TERM_RED}},
  96. {"(down) *(down).*$",{TERM_RED, TERM_RED}},
  97.  
  98. /* sh int output */
  99. {"([0-9]*) (input errors),", {TERM_RED, TERM_BRIGHT_CYAN}},
  100. {"([0-9]*) (output errors),", {TERM_RED, TERM_BRIGHT_CYAN}},
  101.  
  102. // ([A-Z][a-z]*[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps)$
  103. {"(Full[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps)$", {TERM_GREEN, TERM_YELLOW}},
  104. {"(Half[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps)$", {TERM_BRIGHT_PURPLE,TERM_YELLOW}},
  105. {"(Auto[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps)$", {TERM_BRIGHT_RED,TERM_YELLOW}},
  106. {"(Auto[- ]?[Dd]uplex), (Auto[- ]?[Ss]peed)$", {TERM_BRIGHT_RED,TERM_BRIGHT_RED}},
  107.  
  108. // ([A-Z][a-z]*[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)
  109. {"(Full[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_GREEN, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  110. {"(Half[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_PURPLE, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  111. {"(Auto[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_RED, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  112. {"(Auto[- ]?[Dd]uplex), (Auto[- ]?[Ss]peed), (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_RED, TERM_BRIGHT_RED, TERM_BRIGHT_GREEN}},
  113.  
  114. // ([A-Z][a-z]*[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)
  115. {"(Full[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_GREEN, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  116. {"(Half[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_PURPLE, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  117. {"(Auto[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_RED, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  118. {"(Auto[- ]?[Dd]uplex), (Auto[- ]?[Ss]peed), media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)", {TERM_BRIGHT_RED, TERM_BRIGHT_RED, TERM_BRIGHT_GREEN}},
  119.  
  120. // ([A-Z][a-z]*[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), link type is [a-z]*, media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP)
  121. // compile error for some reason - fix later
  122. //{"(Full[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), link type is [a-z]*, media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP", {TERM_GREEN, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  123. //{"(Half[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), link type is [a-z]*, media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP", {TERM_BRIGHT_PURPLE, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  124. //{"(Auto[- ]?[Dd]uplex), (1[0]{1,3}[GM]b/s|1[0]{0,1}[GM]bps), link type is [a-z]*, media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP", {TERM_BRIGHT_RED, TERM_YELLOW, TERM_BRIGHT_GREEN}},
  125. //{"(Auto[- ]?[Dd]uplex), (Auto[- ]?[Ss]peed), link type is [a-z]*, media type is (T|RJ45|SX|LH|BX10D|100BaseTX/FX|10/100/1000BaseTX|1000BaseSX SFP", {TERM_BRIGHT_RED, TERM_BRIGHT_RED, TERM_BRIGHT_GREEN}},
  126.  
  127. /* show int status */
  128. {"^[A-Z].*(connected)", {TERM_BRIGHT_GREEN}},
  129. {"^[A-Z].*(disabled)", {TERM_BRIGHT_RED}},
  130. {"^[A-Z].*(notconnect)", {TERM_YELLOW}},
  131. {"^[A-Z].*(err-disable)", {TERM_BRIGHT_RED}},
  132.  
  133. /* sh run interface - switch */
  134. {"switchport (mode) (trunk)", {TERM_BRIGHT_GREEN, TERM_CYAN}},
  135. {"switchport (mode) (access)", {TERM_BRIGHT_GREEN, TERM_CYAN}},
  136. {"switchport (voice vlan) ([0-9]*)", {TERM_BRIGHT_GREEN, TERM_CYAN}},
  137. {"switchport (access vlan) ([0-9]*)", {TERM_BRIGHT_GREEN, TERM_CYAN}},
  138. {"switchport (nonegotiate)", {TERM_BRIGHT_RED}},
  139. {"switchport trunk (native vlan) ([0-9])*", {TERM_BRIGHT_RED, TERM_CYAN}},
  140.  
  141.  /* access-lists */
  142. {"ip (access-list) extended (.*)", {TERM_CYAN, TERM_YELLOW}},
  143. {"ip (access-list) standard (.*)", {TERM_CYAN, TERM_YELLOW}},
  144. {"^ (remark) (.*)", {TERM_YELLOW, TERM_YELLOW}},
  145. {"^ (permit) ", {TERM_BRIGHT_GREEN}},
  146. {"^ (deny) ", {TERM_WARNING}},
  147.  
  148. /*trace times */
  149. {" ([0-9] msec)", {TERM_BRIGHT_GREEN}},
  150. {" ([1-9][0-9] msec)", {TERM_CYAN}},
  151. {" ([1-3][0-9]{2} msec)", {TERM_YELLOW}},
  152. {" (4-9][0-9]{2} msec)", {TERM_RED}},
  153. {" (1-9][0-9]{3} msec)", {TERM_RED}},
  154.  
  155.  /* sh cdp nei de */
  156.  {"Device ID: ([A-Za-z0-9\\._-]*)", {TERM_CYAN}},
  157.  {"IP address: ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})", {TERM_CYAN}},
  158.  
  159.  /* headers */
  160. {"(Port.*Name.*Status.*Vlan.*Duplex.*Speed Type)", {TERM_BRIGHT_BLUE}},
  161. {"(Interface.*IP-Address.*OK\\?.*Method Status.*Protocol)", {TERM_BRIGHT_BLUE}},
  162. {"(Interface.*Status.*Protocol.*Description)", {TERM_BRIGHT_BLUE}},
  163.  
  164. /* end my edits */
  165.  
  166. /* Logs */
  167. {"^\\*?([A-Z][a-z]{2}[ 0-9]{3}[0-9 ]{0,5} [0-9]{1,2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3} [A-Z]{3}): (%[A-Z0-9_-]+:) (.*)$", {TERM_CYAN, TERM_RED, TERM_BLUE}},
  168.  
  169. /* Interface Name */
  170. {"^(interface) ([a-zA-Z-]+)([0-9./]+)",{TERM_YELLOW, TERM_CYAN, TERM_GREEN}},
  171.  
  172. /* Interface parameters */
  173. {"^ (description) (.*)$", {TERM_YELLOW, TERM_BLUE}},
  174. {"^ (ip address) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (secondary)",
  175.         {TERM_YELLOW, TERM_PURPLE, TERM_PURPLE, TERM_BRIGHT_GREEN}},
  176. {"^ (ip address) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}).$",
  177.     {TERM_YELLOW, TERM_PURPLE, TERM_PURPLE}},
  178. {"^ (ip vrf forwarding) ([a-zA-Z0-9_-]+).$", {TERM_YELLOW,TERM_RED}},
  179. {"^ (ip nat) (inside|outside).$", {TERM_YELLOW,TERM_RED}},
  180. {"^ (encapsulation dot1Q) ([0-9]{1,4}).$", {TERM_YELLOW, TERM_PURPLE }},
  181. {"^ (shutdown).$", {TERM_RED}},
  182.  
  183. /* Interface Status */
  184. {"([a-zA-Z-]+)([0-9./]+) is (up), line protocol is (down)", {TERM_CYAN, TERM_GREEN, TERM_BRIGHT_GREEN, TERM_BRIGHT_RED}},
  185. {"([a-zA-Z-]+)([0-9./]+) is (down), line protocol is (down)", {TERM_CYAN, TERM_GREEN, TERM_BRIGHT_RED, TERM_BRIGHT_RED}},
  186. {"([a-zA-Z-]+)([0-9./]+) is (up), line protocol is (up)", {TERM_CYAN, TERM_GREEN, TERM_BRIGHT_GREEN, TERM_BRIGHT_GREEN}},
  187. {"([a-zA-Z-]+)([0-9./]+) is (administratively down), line protocol is (down)", {TERM_CYAN, TERM_GREEN, TERM_YELLOW, TERM_YELLOW}},
  188. {"([a-zA-Z-]+)([0-9./]+) is ([a-z]+), line protocol is ([a-z]+)", {TERM_CYAN, TERM_GREEN, TERM_WARNING, TERM_WARNING}},
  189.  
  190.  
  191. /* Interface speeds */
  192. {"^  [0-9]{1,2} [a-z]{6} ([a-z]{5,6} rate) ([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3}) bits.*$", {TERM_YELLOW, TERM_PURPLE, TERM_CYAN, TERM_BLUE, TERM_CYAN}},
  193. {"^  [0-9]{1,2} [a-z]{6} ([a-z]{5,6} rate) ([0-9]{1,3})([0-9]{3})([0-9]{3}) bits.*$", {TERM_YELLOW, TERM_CYAN, TERM_BLUE, TERM_CYAN}},
  194. {"^  [0-9]{1,2} [a-z]{6} ([a-z]{5,6} rate) ([0-9]{1,3})([0-9]{3}) bits.*$", {TERM_YELLOW, TERM_BLUE, TERM_CYAN}},
  195. {"^  [0-9]{1,2} [a-z]{6} ([a-z]{5,6} rate) ([0-9]{1,3}) bits.*$", {TERM_YELLOW, TERM_CYAN}},
  196.  
  197.  
  198.  
  199. /* Hostname / config prompts */
  200. {"^([a-zA-Z0-9_\\-]*)(\\(config)(-[a-z_-]+)(\\))#",{TERM_BRIGHT_BLUE,TERM_RED,TERM_PURPLE,TERM_RED}},
  201. {"^([a-zA-Z0-9_\\-]*)(\\(config\\))#",{TERM_BRIGHT_BLUE,TERM_RED}},
  202. {"^([a-zA-Z0-9_-]*)#",{TERM_BRIGHT_BLUE}},
  203. {"^([a-zA-Z0-9_-]*)>",{TERM_BRIGHT_BLUE}},
  204.  
  205. /* OSPF config */
  206. {"^(router ospf) ([0-9]+) (vrf) ([a-zA-Z_-]+).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
  207. {"^(router ospf) ([0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
  208. {"^( log-adjacency-changes).$", {TERM_YELLOW}},
  209. {"^ (redistribute) ([a-zA-Z0-9 ]+) (subnets).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW}},
  210. {"^ (network) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (area) ([0-9]+).$",
  211.                         {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
  212. {"^ (default-information) (originate)(.*)$", { TERM_YELLOW, TERM_RED, TERM_PURPLE}},
  213.  
  214. /* BGP Config */
  215. {"^(router bgp) ([0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
  216. {"^ (address-family) ([a-z0-9]+) (vrf) ([a-zA-Z0-9_-]+).$", {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
  217. {"^ (address-family) ([a-z0-9]+).$", {TERM_YELLOW, TERM_PURPLE}},
  218. {"^ {1,2}(no) (synchronization).$", {TERM_RED, TERM_YELLOW}},
  219. {"^ {1,2}(neighbor) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (remote-as) ([0-9]+).$",
  220.                     {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_RED}},
  221. {"^ {1,2}(neighbor) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) (description) ([a-zA-Z0-9 _-]+).$",
  222.                     {TERM_YELLOW, TERM_PURPLE, TERM_YELLOW, TERM_BLUE}},
  223.  
  224.  
  225.  
  226. /* Routing */
  227. {"^(Routing entry for )([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})/([0-9]{2}).$", {TERM_YELLOW,TERM_PURPLE,TERM_RED}},
  228. {"^  Known via \"([a-z0-9 ]+)\"", {TERM_CYAN}},
  229.  
  230. {"^(ip route vrf) ([a-zA-Z0-9_-]*) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$",
  231.             {TERM_YELLOW, TERM_RED, TERM_PURPLE, TERM_BRIGHT_GREEN}},
  232. {"^(ip route) ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3} [0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$",
  233. {TERM_YELLOW, TERM_PURPLE, TERM_BRIGHT_GREEN}},
  234.  
  235.  
  236. /* Big numbers */
  237. {" ([0-9]{1,3})([0-9]{3})([0-9]{3})([0-9]{3}) ", {TERM_PURPLE, TERM_CYAN, TERM_BRIGHT_GREEN, TERM_CYAN}},
  238. {" ([0-9]{1,3})([0-9]{3})([0-9]{3}) ", {TERM_CYAN, TERM_BRIGHT_GREEN, TERM_CYAN}},
  239. {" ([0-9]{1,3})([0-9]{3}) ", {TERM_BRIGHT_GREEN, TERM_CYAN}},
  240.  
  241.     };
  242.  
  243. /*  This function loops through each regex and compiles it so it's ready to use  */
  244. int init_patterns()
  245. {
  246.  
  247.  
  248.  
  249.     unsigned int i;
  250.         /* compile regexs */
  251.  
  252.         for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
  253.         {
  254.                 if (regcomp(&patterns[i].compiled, patterns[i].pattern, REG_EXTENDED|REG_NEWLINE))
  255.                 {
  256.                         fprintf(stderr, "Error compiling pattern: %s\n",
  257.                                 patterns[i].pattern);
  258.  
  259.                         return 1;
  260.                 }
  261.         }  
  262.     return 0;
  263. }
  264.  
  265.  
  266. /* This function is called for every incoming packet. It compares it to each regex and inserts
  267.    codes as appropriate */
  268. int process_patterns(char *data, int data_len)
  269. {
  270.         unsigned int i; /* main for loop, keeps track of which rule/pattern we're on */
  271.         int c; /* Used in inner loop to keep track of which sub-expression we're at */
  272.         char buffer[32767]; /* Buffer, we rebuild strings with color codes here */
  273.     char mydata[32767]; /* Keeps track of the data between loops */
  274.     int increased;  /* Keeps track of how much we lengthened data */
  275.     regmatch_t matches[MAXMATCH]; /* Used as results of regexec for what we matched */
  276.     regoff_t last_match; /* used to loop through the data incase we match the data more than once */
  277.  
  278.     increased = 0;
  279.  
  280.     /* Copy the string to the correct length. Data refrences the whole SSH buffer (I think) and has garbage */
  281.  
  282.     memset(mydata, '\0', sizeof(mydata));
  283.     strncat(mydata, data, data_len);
  284.  
  285.         /* execute regexs */
  286.         for (i = 0; i < sizeof(patterns)/sizeof(patterns[0]); i++)
  287.         {
  288.         last_match = 0;
  289.                 while (regexec(&patterns[i].compiled, mydata + last_match, MAXMATCH, matches, 0) == 0)
  290.                 {
  291.                         if ((int)matches[1].rm_eo > 0)
  292.                         {
  293.  
  294.                
  295.                                 /* Reset variables */
  296.                                 memset(buffer, '\0', sizeof(buffer));
  297.                 c = 1;
  298.  
  299.                                 /* Copy start of string */
  300.                                 strncat(buffer, mydata, (int)matches[c].rm_so + last_match);
  301.  
  302.                 while (((int)matches[c].rm_so >= 0) && (c < MAXMATCH) )
  303.                 {
  304.                                     /* Insert before match */
  305.                                     strcat(buffer, patterns[i].insert_before[c-1]);
  306.  
  307.                                     /*copy what matched */
  308.                                     strncat(buffer, mydata + (int)matches[c].rm_so + last_match,
  309.                             (int)matches[c].rm_eo - (int)matches[c].rm_so);
  310.  
  311.                                     /* Insert the after match */
  312.                                      strcat(buffer, TERM_NONE);
  313.    
  314.                     /* Are there any more matches for this pattern? */
  315.                     if ( (c+1 > MAXMATCH) || ((int)matches[c+1].rm_so < 0 ))
  316.                     {
  317.                                         /* Nope! Finish string! */
  318.                                         strncat(buffer, mydata + (int)matches[c].rm_eo + last_match,
  319.                             strlen(mydata) - (int)matches[c].rm_eo + last_match);
  320.                     } else
  321.                     {
  322.                         /* Copy up until next match */
  323.                         strncat(buffer, mydata + (int)matches[c].rm_eo + last_match,
  324.                             (int)matches[c+1].rm_so - (int)matches[c].rm_eo);
  325.                     }
  326.                    
  327.                     /* Increase the data length by what we added */
  328.                     increased += strlen(patterns[i].insert_before[c-1]) + strlen(TERM_NONE);
  329.                     c++;
  330.                 }
  331.                 /* Copy the buffer back to mydata */
  332.                 strcpy(mydata, buffer);
  333.             }
  334.        
  335.             last_match += (int)matches[0].rm_eo;
  336.         }
  337.     }
  338.  
  339.     /* Copy my data back to real data, and return increased length (used in channels.c) */
  340.     strcpy(data, mydata);
  341.     return increased;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement