substitute.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. // color subs
  26. char *c256to16_fg[256] =
  27. {
  28. "\e[22;30m", "\e[22;31m", "\e[22;32m", "\e[22;33m", "\e[22;34m", "\e[22;35m", "\e[22;36m", "\e[22;37m",
  29. "\e[1;30m", "\e[1;31m", "\e[1;32m", "\e[1;33m", "\e[1;34m", "\e[1;35m", "\e[1;36m", "\e[1;37m",
  30. "\e[22;30m", "\e[22;34m", "\e[22;34m", "\e[22;34m", "\e[1;34m", "\e[1;34m",
  31. "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[22;34m", "\e[1;34m", "\e[1;34m",
  32. "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[22;36m", "\e[1;34m", "\e[1;34m",
  33. "\e[22;32m", "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[22;36m", "\e[1;36m",
  34. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[22;36m", "\e[1;36m", "\e[1;36m",
  35. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[1;36m", "\e[1;36m", "\e[1;36m",
  36. "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[22;34m", "\e[1;34m", "\e[1;34m",
  37. "\e[22;33m", "\e[1;30m", "\e[22;34m", "\e[22;34m", "\e[1;34m", "\e[1;34m",
  38. "\e[22;33m", "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[1;34m", "\e[1;34m",
  39. "\e[22;32m", "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[22;36m", "\e[1;36m",
  40. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[22;36m", "\e[1;36m", "\e[1;36m",
  41. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[1;36m", "\e[1;36m", "\e[1;36m",
  42. "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[22;35m", "\e[1;34m", "\e[1;34m",
  43. "\e[22;33m", "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[1;34m", "\e[1;34m",
  44. "\e[22;33m", "\e[22;33m", "\e[22;37m", "\e[22;34m", "\e[1;34m", "\e[1;34m",
  45. "\e[22;33m", "\e[22;33m", "\e[22;32m", "\e[22;36m", "\e[22;36m", "\e[1;34m",
  46. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[22;36m", "\e[1;36m", "\e[1;36m",
  47. "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[1;32m", "\e[1;36m", "\e[1;36m",
  48. "\e[22;31m", "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[22;35m", "\e[1;35m",
  49. "\e[22;31m", "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[22;35m", "\e[1;35m",
  50. "\e[22;33m", "\e[22;33m", "\e[22;31m", "\e[22;35m", "\e[22;35m", "\e[1;34m",
  51. "\e[22;33m", "\e[22;33m", "\e[22;33m", "\e[22;37m", "\e[1;34m", "\e[1;34m",
  52. "\e[22;33m", "\e[22;33m", "\e[22;33m", "\e[1;32m", "\e[1;36m", "\e[1;36m",
  53. "\e[1;33m", "\e[1;33m", "\e[1;32m", "\e[1;32m", "\e[1;36m", "\e[1;36m",
  54. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[22;35m", "\e[1;35m", "\e[1;35m",
  55. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[22;35m", "\e[1;35m", "\e[1;35m",
  56. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[22;35m", "\e[1;35m", "\e[1;35m",
  57. "\e[22;33m", "\e[22;33m", "\e[22;33m", "\e[1;31m", "\e[1;35m", "\e[1;35m",
  58. "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;37m", "\e[1;37m",
  59. "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;37m", "\e[1;37m",
  60. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[1;35m", "\e[1;35m", "\e[1;35m",
  61. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[1;35m", "\e[1;35m", "\e[1;35m",
  62. "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[1;31m", "\e[1;35m", "\e[1;35m",
  63. "\e[1;33m", "\e[1;33m", "\e[1;31m", "\e[1;31m", "\e[1;35m", "\e[1;35m",
  64. "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;37m", "\e[1;37m",
  65. "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;33m", "\e[1;37m", "\e[1;37m",
  66. "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m",
  67. "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m", "\e[1;30m",
  68. "\e[22;37m", "\e[22;37m", "\e[22;37m", "\e[22;37m", "\e[22;37m", "\e[22;37m",
  69. "\e[1;37m", "\e[1;37m", "\e[1;37m", "\e[1;37m", "\e[1;37m", "\e[1;37m"
  70. };
  71. char *c256to16_bg[256] =
  72. {
  73. "\e[22;40m", "\e[22;41m", "\e[22;42m", "\e[22;43m", "\e[22;44m", "\e[22;45m", "\e[22;46m", "\e[22;47m",
  74. "\e[1;40m", "\e[1;41m", "\e[1;42m", "\e[1;43m", "\e[1;44m", "\e[1;45m", "\e[1;46m", "\e[1;47m",
  75. "\e[22;40m", "\e[22;44m", "\e[22;44m", "\e[22;44m", "\e[1;44m", "\e[1;44m",
  76. "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[22;44m", "\e[1;44m", "\e[1;44m",
  77. "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[22;46m", "\e[1;44m", "\e[1;44m",
  78. "\e[22;42m", "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[22;46m", "\e[1;46m",
  79. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[22;46m", "\e[1;46m", "\e[1;46m",
  80. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[1;46m", "\e[1;46m", "\e[1;46m",
  81. "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[22;44m", "\e[1;44m", "\e[1;44m",
  82. "\e[22;43m", "\e[1;40m", "\e[22;44m", "\e[22;44m", "\e[1;44m", "\e[1;44m",
  83. "\e[22;43m", "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[1;44m", "\e[1;44m",
  84. "\e[22;42m", "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[22;46m", "\e[1;46m",
  85. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[22;46m", "\e[1;46m", "\e[1;46m",
  86. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[1;46m", "\e[1;46m", "\e[1;46m",
  87. "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[22;45m", "\e[1;44m", "\e[1;44m",
  88. "\e[22;43m", "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[1;44m", "\e[1;44m",
  89. "\e[22;43m", "\e[22;43m", "\e[22;47m", "\e[22;44m", "\e[1;44m", "\e[1;44m",
  90. "\e[22;43m", "\e[22;43m", "\e[22;42m", "\e[22;46m", "\e[22;46m", "\e[1;44m",
  91. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[22;46m", "\e[1;46m", "\e[1;46m",
  92. "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[1;42m", "\e[1;46m", "\e[1;46m",
  93. "\e[22;41m", "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[22;45m", "\e[1;45m",
  94. "\e[22;41m", "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[22;45m", "\e[1;45m",
  95. "\e[22;43m", "\e[22;43m", "\e[22;41m", "\e[22;45m", "\e[22;45m", "\e[1;44m",
  96. "\e[22;43m", "\e[22;43m", "\e[22;43m", "\e[22;47m", "\e[1;44m", "\e[1;44m",
  97. "\e[22;43m", "\e[22;43m", "\e[22;43m", "\e[1;42m", "\e[1;46m", "\e[1;46m",
  98. "\e[1;43m", "\e[1;43m", "\e[1;42m", "\e[1;42m", "\e[1;46m", "\e[1;46m",
  99. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[22;45m", "\e[1;45m", "\e[1;45m",
  100. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[22;45m", "\e[1;45m", "\e[1;45m",
  101. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[22;45m", "\e[1;45m", "\e[1;45m",
  102. "\e[22;43m", "\e[22;43m", "\e[22;43m", "\e[1;41m", "\e[1;45m", "\e[1;45m",
  103. "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;47m", "\e[1;47m",
  104. "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;47m", "\e[1;47m",
  105. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[1;45m", "\e[1;45m", "\e[1;45m",
  106. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[1;45m", "\e[1;45m", "\e[1;45m",
  107. "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[1;41m", "\e[1;45m", "\e[1;45m",
  108. "\e[1;43m", "\e[1;43m", "\e[1;41m", "\e[1;41m", "\e[1;45m", "\e[1;45m",
  109. "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;47m", "\e[1;47m",
  110. "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;43m", "\e[1;47m", "\e[1;47m",
  111. "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m",
  112. "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m", "\e[1;40m",
  113. "\e[22;47m", "\e[22;47m", "\e[22;47m", "\e[22;47m", "\e[22;47m", "\e[22;47m",
  114. "\e[1;47m", "\e[1;47m", "\e[1;47m", "\e[1;47m", "\e[1;47m", "\e[1;47m"
  115. };
  116. // input A to F
  117. int c256_val(char chr)
  118. {
  119. static unsigned char c256_val[256] =
  120. {
  121. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  122. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  123. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  124. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  125. 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  126. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  127. 0, 0, 1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  128. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  129. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  130. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  131. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  132. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  133. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  134. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  135. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  136. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  137. };
  138. return (int) c256_val[(unsigned char) chr];
  139. }
  140. // input 00 to FF
  141. int c4096_val(char chr1, char chr2)
  142. {
  143. static unsigned char c4096_val[256] =
  144. {
  145. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  146. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  147. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  148. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
  149. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  150. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  151. 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  152. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  155. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  156. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  157. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  158. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  159. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  160. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  161. };
  162. return (int) c4096_val[(unsigned char) chr1] * 16 + c4096_val[(unsigned char) chr2];
  163. }
  164. // input 00 to FF
  165. int c4096_to_256_val(char chr1, char chr2)
  166. {
  167. static unsigned char c4096_to_256[256] =
  168. {
  169. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  170. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  171. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  172. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  173. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  174. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  175. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  176. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  177. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  178. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  179. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  180. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  181. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  182. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  183. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  184. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  185. };
  186. return (int) c4096_to_256[c4096_val(chr1, chr2)];
  187. }
  188. char int_to_hex[16] =
  189. {
  190. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  191. };
  192. char int_to_256[6] =
  193. {
  194. 'A', 'B', 'C', 'D', 'E', 'F'
  195. };
  196. void c4096_rnd(struct session *ses, char *str)
  197. {
  198. sprintf(str, "%c%c%c", int_to_hex[generate_rand(ses) % 16], int_to_hex[generate_rand(ses) % 16], int_to_hex[generate_rand(ses) % 16]);
  199. }
  200. int is_c32(char chr)
  201. {
  202. static unsigned char c32_lookup[256] =
  203. {
  204. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  205. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  206. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  207. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '?',
  208. 0, 'A', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  209. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 'Z', 0, 0, 0, 0, 0,
  210. 0, 'a', 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  211. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 'z', 0, 0, 0, 0, 0,
  212. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  213. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  214. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  215. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  216. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  217. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  218. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  219. };
  220. return (int) c32_lookup[(unsigned char) chr];
  221. }
  222. char *c32_fg_dark[26] =
  223. {
  224. "<f06b>", "<f00b>", "<f0bb>", "", "<f000>", "", "<f0b0>", "", "", "<f0b6>", "", "<f6b0>", "<fb0b>",
  225. "", "<fb60>", "<fb06>", "", "<fb00>", "<f888>", "<f860>", "", "<f60b>", "<fbbb>", "", "<fbb0>", ""
  226. };
  227. char *c32_fg_bold[26] =
  228. {
  229. "<f08f>", "<f00f>", "<f0ff>", "", "<f666>", "", "<f0f0>", "", "", "<f0f8>", "", "<f8f0>", "<ff0f>",
  230. "", "<ff80>", "<ff08>", "", "<ff00>", "<fddd>", "<fdb0>", "", "<f80f>", "<ffff>", "", "<fff0>", ""
  231. };
  232. int is_variable(struct session *ses, char *str)
  233. {
  234. struct listroot *root;
  235. char temp[BUFFER_SIZE], *ptt;
  236. int i = 1;
  237. while (str[i] == str[0])
  238. {
  239. i++;
  240. }
  241. if (str[i] == DEFAULT_OPEN)
  242. {
  243. return TRUE;
  244. }
  245. if (str[i] != '_' && isalpha((int) str[i]) == 0)
  246. {
  247. return FALSE;
  248. }
  249. ptt = temp;
  250. while (isalnum((int) str[i]) || str[i] == '_')
  251. {
  252. *ptt++ = str[i];
  253. i++;
  254. }
  255. *ptt = 0;
  256. root = local_list(ses);
  257. if (search_node_list(root, temp) == NULL)
  258. {
  259. root = ses->list[LIST_VARIABLE];
  260. if (search_node_list(root, temp) == NULL)
  261. {
  262. return FALSE;
  263. }
  264. }
  265. return TRUE;
  266. }
  267. int is_function(struct session *ses, char *str)
  268. {
  269. char temp[BUFFER_SIZE], *ptt;
  270. int i = 1;
  271. while (str[i] == str[0])
  272. {
  273. i++;
  274. }
  275. if (str[i] != '_' && isalpha((int) str[i]) == 0)
  276. {
  277. return FALSE;
  278. }
  279. ptt = temp;
  280. while (isalnum((int) str[i]) || str[i] == '_')
  281. {
  282. *ptt++ = str[i];
  283. i++;
  284. }
  285. *ptt = 0;
  286. if (str[i] != DEFAULT_OPEN)
  287. {
  288. return FALSE;
  289. }
  290. if (search_node_list(ses->list[LIST_FUNCTION], temp) == NULL)
  291. {
  292. if (find_session(temp) == NULL)
  293. {
  294. return FALSE;
  295. }
  296. }
  297. return TRUE;
  298. }
  299. int is_color_code(char *pti)
  300. {
  301. if (pti[0] == '<')
  302. {
  303. if (pti[1] == 0 || pti[2] == 0 || pti[3] == 0 || pti[4] == 0)
  304. {
  305. return 0;
  306. }
  307. if (pti[4] == '>')
  308. {
  309. if (isdigit(pti[1]) && isdigit(pti[2]) && isdigit(pti[3]))
  310. {
  311. return 5;
  312. }
  313. if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f')
  314. {
  315. return 5;
  316. }
  317. if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F')
  318. {
  319. return 5;
  320. }
  321. if (pti[1] == 'g' || pti[1] == 'G')
  322. {
  323. if (isdigit((int) pti[2]) && isdigit((int) pti[3]))
  324. {
  325. return 5;
  326. }
  327. return 0;
  328. }
  329. return 0;
  330. }
  331. if (pti[5] == 0)
  332. {
  333. return 0;
  334. }
  335. if (toupper((int) pti[1]) == 'F')
  336. {
  337. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  338. {
  339. return 6;
  340. }
  341. else if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  342. {
  343. return 6;
  344. }
  345. else if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  346. {
  347. return 9;
  348. }
  349. return 0;
  350. }
  351. if (toupper(pti[1]) == 'B')
  352. {
  353. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  354. {
  355. return 6;
  356. }
  357. if (toupper(pti[1]) == 'B' && pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  358. {
  359. return 6;
  360. }
  361. if (toupper(pti[1]) == 'B' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  362. {
  363. return 9;
  364. }
  365. return 0;
  366. }
  367. }
  368. return 0;
  369. }
  370. char *fuzzy_char(struct session *ses, char val1, char val2, int mode)
  371. {
  372. static char out[10][3];
  373. static int cnt;
  374. int tmp;
  375. cnt = (cnt + 1) % 10;
  376. switch (mode)
  377. {
  378. case 8:
  379. tmp = c256_val(val2) - 1 + generate_rand(ses) % 3;
  380. tmp = URANGE(0, tmp, 5);
  381. if (isupper(val2))
  382. {
  383. sprintf(out[cnt], "%c", toupper(int_to_256[tmp]));
  384. }
  385. else
  386. {
  387. sprintf(out[cnt], "%c", tolower(int_to_256[tmp]));
  388. }
  389. break;
  390. case 12:
  391. tmp = c4096_val(0, val2);
  392. tmp = tmp - 2 + generate_rand(ses) % 4;
  393. tmp = URANGE(0, tmp, 15);
  394. sprintf(out[cnt], "%c", int_to_hex[tmp]);
  395. break;
  396. case 24:
  397. tmp = c4096_val(val1, val2);
  398. tmp = (tmp - 12 + generate_rand(ses) % 24);
  399. tmp = URANGE(0, tmp, 255);
  400. sprintf(out[cnt], "%c%c", int_to_hex[tmp / 16], int_to_hex[tmp % 16]);
  401. break;
  402. default:
  403. tintin_printf2(ses, "dim_char: invalid mode");
  404. break;
  405. }
  406. return out[cnt];
  407. }
  408. char *fuzzy_color_code(struct session *ses, char *in)
  409. {
  410. static char *pto, out[10][100];
  411. static int cnt;
  412. char *pti, buf[100];
  413. int tmp;
  414. if (*in == 0 || strlen(in) > 50)
  415. {
  416. return "";
  417. }
  418. strcpy(buf, in);
  419. pti = buf;
  420. cnt = (cnt + 1) % 10;
  421. pto = out[cnt];
  422. while (*pti)
  423. {
  424. if (pti[0] == '<')
  425. {
  426. if (pti[1] == 0 || pti[2] == 0 || pti[3] == 0 || pti[4] == 0)
  427. {
  428. return out[cnt];
  429. }
  430. if (pti[4] == '>')
  431. {
  432. if (isdigit(pti[1]) && isdigit(pti[2]) && isdigit(pti[3]))
  433. {
  434. pto += sprintf(pto, "<%c%c%c>", pti[1], pti[2], pti[3]);
  435. }
  436. else if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f')
  437. {
  438. pto += sprintf(pto, "<%s%s%s>", fuzzy_char(ses, 0, pti[1], 8), fuzzy_char(ses, 0, pti[2], 8), fuzzy_char(ses, 0, pti[3], 8));
  439. }
  440. else if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F')
  441. {
  442. pto += sprintf(pto, "<%s%s%s>", fuzzy_char(ses, 0, pti[1], 8), fuzzy_char(ses, 0, pti[2], 8), fuzzy_char(ses, 0, pti[3], 8));
  443. }
  444. else if ((pti[1] == 'g' || pti[1] == 'G') && isdigit((int) pti[2]) && isdigit((int) pti[3]))
  445. {
  446. tmp = (pti[2] - '0') * 10 + (pti[3] - '0') - 3 + generate_rand(ses) % 7;
  447. pto += sprintf(pto, "<%c%02d>", pti[1], URANGE(0, tmp, 23));
  448. }
  449. else
  450. {
  451. return out[cnt];
  452. }
  453. pti += 5;
  454. }
  455. else if (toupper((int) pti[1]) == 'F')
  456. {
  457. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  458. {
  459. pto += sprintf(pto, "<F%s%s%s>", fuzzy_char(ses, 0, pti[2], 12), fuzzy_char(ses, 0, pti[3], 12), fuzzy_char(ses, 0, pti[4], 12));
  460. pti += 6;
  461. }
  462. else if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  463. {
  464. c4096_rnd(ses, &pti[2]);
  465. pto += sprintf(pto, "<F%c%c%c>", pti[2], pti[3], pti[4]);
  466. pti += 6;
  467. }
  468. else if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  469. {
  470. pto += sprintf(pto, "<F%s%s%s>", fuzzy_char(ses, pti[3], pti[4], 24), fuzzy_char(ses, pti[4], pti[5], 24), fuzzy_char(ses, pti[7], pti[7], 24));
  471. pti += 9;
  472. }
  473. else
  474. {
  475. return out[cnt];
  476. }
  477. }
  478. else if (toupper(pti[1]) == 'B')
  479. {
  480. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  481. {
  482. pto += sprintf(pto, "<B%s%s%s>", fuzzy_char(ses, 0, pti[2], 12), fuzzy_char(ses, 0, pti[3], 12), fuzzy_char(ses, 0, pti[4], 12));
  483. pti += 6;
  484. }
  485. else if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  486. {
  487. c4096_rnd(ses, &pti[2]);
  488. pto += sprintf(pto, "<B%c%c%c>", pti[2], pti[3], pti[4]);
  489. pti += 6;
  490. }
  491. else if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  492. {
  493. pto += sprintf(pto, "<B%s%s%s>", fuzzy_char(ses, pti[2], pti[3], 24), fuzzy_char(ses, pti[4], pti[5], 24), fuzzy_char(ses, pti[6], pti[7], 24));
  494. pti += 9;
  495. }
  496. else
  497. {
  498. return out[cnt];
  499. }
  500. }
  501. else
  502. {
  503. return out[cnt];
  504. }
  505. }
  506. else
  507. {
  508. return out[cnt];
  509. }
  510. }
  511. return out[cnt];
  512. }
  513. char *dim_char(struct session *ses, char val1, char val2, int mod, int mode)
  514. {
  515. static char out[10][3];
  516. static int cnt;
  517. int tmp;
  518. cnt = (cnt + 1) % 10;
  519. switch (mode)
  520. {
  521. case 8:
  522. tmp = c256_val(val2) - mod;
  523. tmp = URANGE(0, tmp, 5);
  524. if (isupper(val2))
  525. {
  526. sprintf(out[cnt], "%c", toupper(int_to_256[tmp]));
  527. }
  528. else
  529. {
  530. sprintf(out[cnt], "%c", tolower(int_to_256[tmp]));
  531. }
  532. break;
  533. case 12:
  534. tmp = c4096_val(0, val2);
  535. tmp = tmp - mod - generate_rand(ses) % 3;
  536. tmp = URANGE(0, tmp, 15);
  537. sprintf(out[cnt], "%c", int_to_hex[tmp]);
  538. break;
  539. case 24:
  540. tmp = c4096_val(val1, val2);
  541. tmp = tmp - mod - generate_rand(ses) % 2;
  542. tmp = URANGE(0, tmp, 255);
  543. sprintf(out[cnt], "%c%c", int_to_hex[tmp / 16], int_to_hex[tmp % 16]);
  544. break;
  545. default:
  546. tintin_printf2(ses, "dim_char: invalid mode");
  547. break;
  548. }
  549. return out[cnt];
  550. }
  551. char *dim_color_code(struct session *ses, char *in, int mod)
  552. {
  553. static char *pto, out[10][60], buf[60];
  554. char *pti;
  555. static int cnt;
  556. int tmp;
  557. if (*in == 0 || strlen(in) > 40)
  558. {
  559. return "";
  560. }
  561. strcpy(buf, in);
  562. pti = buf;
  563. cnt = (cnt + 1) % 10;
  564. pto = out[cnt];
  565. if (mod < 0)
  566. {
  567. strcpy(pto, buf);
  568. return out[cnt];
  569. }
  570. *pto = 0;
  571. while (*pti)
  572. {
  573. if (pti[0] == '<')
  574. {
  575. if (pti[1] == 0 || pti[2] == 0 || pti[3] == 0 || pti[4] == 0)
  576. {
  577. return out[cnt];
  578. }
  579. if (pti[4] == '>')
  580. {
  581. if (isdigit(pti[1]) && isdigit(pti[2]) && isdigit(pti[3]))
  582. {
  583. pto += sprintf(pto, "<%c%c%c>", pti[1], pti[2], pti[3]);
  584. }
  585. else if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f')
  586. {
  587. pto += sprintf(pto, "<%s%s%s>", dim_char(ses, 0, pti[1], mod, 8), dim_char(ses, 0, pti[2], mod, 8), dim_char(ses, 0, pti[3], mod, 8));
  588. }
  589. else if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F')
  590. {
  591. pto += sprintf(pto, "<%s%s%s>", dim_char(ses, 0, pti[1], mod, 8), dim_char(ses, 0, pti[2], mod, 8), dim_char(ses, 0, pti[3], mod, 8));
  592. }
  593. else if ((pti[1] == 'g' || pti[1] == 'G') && isdigit((int) pti[2]) && isdigit((int) pti[3]))
  594. {
  595. tmp = (pti[2] - '0') * 10 + (pti[3] - '0') - mod;
  596. pto += sprintf(pto, "<%c%02d>", pti[1], URANGE(0, tmp, 23));
  597. }
  598. else
  599. {
  600. return out[cnt];
  601. }
  602. pti += 5;
  603. }
  604. else if (toupper((int) pti[1]) == 'F')
  605. {
  606. if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  607. {
  608. c4096_rnd(ses, &pti[2]);
  609. }
  610. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  611. {
  612. pto += sprintf(pto, "<F%s%s%s>", dim_char(ses, 0, pti[2], mod, 12), dim_char(ses, 0, pti[3], mod, 12), dim_char(ses, 0, pti[4], mod, 12));
  613. pti += 6;
  614. }
  615. else if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  616. {
  617. pto += sprintf(pto, "<F%s%s%s>", dim_char(ses, pti[2], pti[3], mod, 24), dim_char(ses, pti[4], pti[5], mod, 24), dim_char(ses, mod, pti[6], pti[7], 24));
  618. pti += 9;
  619. }
  620. else
  621. {
  622. return out[cnt];
  623. }
  624. }
  625. else if (toupper(pti[1]) == 'B')
  626. {
  627. if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  628. {
  629. c4096_rnd(ses, &pti[2]);
  630. }
  631. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  632. {
  633. pto += sprintf(pto, "<B%s%s%s>", dim_char(ses, 0, pti[2], mod, 12), dim_char(ses, 0, pti[3], mod, 12), dim_char(ses, 0, pti[4], mod, 12));
  634. pti += 6;
  635. }
  636. else if (toupper(pti[1]) == 'B' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  637. {
  638. pto += sprintf(pto, "<B%s%s%s>", dim_char(ses, pti[2], pti[3], mod, 24), dim_char(ses, pti[4], pti[5], mod, 24), dim_char(ses, pti[6], pti[7], mod, 24));
  639. pti += 9;
  640. }
  641. else
  642. {
  643. return out[cnt];
  644. }
  645. }
  646. else
  647. {
  648. return out[cnt];
  649. }
  650. }
  651. }
  652. return out[cnt];
  653. }
  654. char lit_char(struct session *ses, char max, char val, int mod, int mode)
  655. {
  656. int tmp;
  657. switch (mode)
  658. {
  659. case 8:
  660. tmp = val + mod;
  661. return UMIN(max, tmp);
  662. case 12:
  663. tmp = c4096_val(0, val);
  664. tmp = URANGE(0, tmp + mod, 15);
  665. return int_to_hex[tmp];
  666. }
  667. return val;
  668. }
  669. char *lit_color_code(struct session *ses, char *pti, int mod)
  670. {
  671. static char fuzzy[10][20];
  672. static int cnt;
  673. int tmp;
  674. cnt = (cnt + 1) % 10;
  675. if (pti[0] == '<')
  676. {
  677. if (pti[1] == 0 || pti[2] == 0 || pti[3] == 0 || pti[4] == 0)
  678. {
  679. return "";
  680. }
  681. if (pti[4] == '>')
  682. {
  683. if (isdigit(pti[1]) && isdigit(pti[2]) && isdigit(pti[3]))
  684. {
  685. sprintf(fuzzy[cnt], "<%c%c%c>%.9s", pti[1], pti[2], pti[3], &pti[5]);
  686. return fuzzy[cnt];
  687. }
  688. if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f')
  689. {
  690. sprintf(fuzzy[cnt], "<%c%c%c>%.9s", lit_char(ses, 'f', pti[1], mod, 8), lit_char(ses, 'f', pti[2], mod, 8), lit_char(ses, 'f', pti[3], mod, 8), &pti[5]);
  691. return fuzzy[cnt];
  692. }
  693. if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F')
  694. {
  695. sprintf(fuzzy[cnt], "<%c%c%c>%.9s", lit_char(ses, 'F', pti[1], mod, 8), lit_char(ses, 'F', pti[2], mod, 8), lit_char(ses, 'F', pti[3], mod, 8), &pti[5]);
  696. return fuzzy[cnt];
  697. }
  698. }
  699. {
  700. if (pti[1] == 'g' || pti[1] == 'G')
  701. {
  702. if (isdigit((int) pti[2]) && isdigit((int) pti[3]))
  703. {
  704. tmp = (pti[2] - '0') * 10 + (pti[3] - '0');
  705. tmp -= mod;
  706. sprintf(fuzzy[cnt], "<%c%02d>%.9s", pti[1], URANGE(0, tmp, 23), &pti[5]);
  707. return fuzzy[cnt];
  708. }
  709. return "";
  710. }
  711. }
  712. if (pti[5] == 0)
  713. {
  714. return "";
  715. }
  716. if (toupper((int) pti[1]) == 'F')
  717. {
  718. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  719. {
  720. sprintf(fuzzy[cnt], "<F%c%c%c>%.9s", lit_char(ses, 'F', pti[2], mod, 12), lit_char(ses, 'F', pti[3], mod, 12), lit_char(ses, 'F', pti[4], mod, 12), &pti[6]);
  721. return fuzzy[cnt];
  722. }
  723. else if (pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  724. {
  725. sprintf(fuzzy[cnt], "<F??%c>%.9s", '?', &pti[6]);
  726. c4096_rnd(ses, &fuzzy[cnt][2]);
  727. return dim_color_code(ses, fuzzy[cnt], mod);
  728. }
  729. else if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  730. {
  731. sprintf(fuzzy[cnt], "<F%c%c%c>%.9s", lit_char(ses, 'F', pti[2], mod, 12), lit_char(ses, 'F', pti[4], mod, 12), lit_char(ses, mod, pti[6], 'F', 12), &pti[9]);
  732. return fuzzy[cnt];
  733. }
  734. return "";
  735. }
  736. if (toupper(pti[1]) == 'B')
  737. {
  738. if (isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  739. {
  740. sprintf(fuzzy[cnt], "<B%c%c%c>%.9s", lit_char(ses, 'F', pti[2], mod, 12), lit_char(ses, 'F', pti[3], mod, 12), lit_char(ses, 'F', pti[4], mod, 12), &pti[6]);
  741. return fuzzy[cnt];
  742. }
  743. if (toupper(pti[1]) == 'B' && pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  744. {
  745. sprintf(fuzzy[cnt], "<B??%c>%.9s", '?', &pti[6]);
  746. c4096_rnd(ses, &fuzzy[cnt][2]);
  747. return dim_color_code(ses, fuzzy[cnt], mod);
  748. }
  749. if (toupper(pti[1]) == 'B' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  750. {
  751. sprintf(fuzzy[cnt], "<B%c%c%c>%.9s", lit_char(ses, 'F', pti[2], mod, 12), lit_char(ses, 'F', pti[4], mod, 12), lit_char(ses, mod, pti[6], 'F', 12), &pti[9]);
  752. return fuzzy[cnt];
  753. }
  754. return "";
  755. }
  756. }
  757. return "";
  758. }
  759. int substitute(struct session *ses, char *string, char *result, int flags)
  760. {
  761. struct listnode *node;
  762. struct listroot *root;
  763. struct session *sesptr;
  764. char temp[BUFFER_SIZE], buf[BUFFER_SIZE], buffer[BUFFER_SIZE], *pti, *pto, *ptt, *str;
  765. char *pte, old[10] = { 0 };
  766. int i, skip, cnt, escape = FALSE, flags_neol = flags;
  767. push_call("substitute(%p,%p,%p,%d)",ses,string,result,flags);
  768. pti = string;
  769. pto = (string == result) ? buffer : result;
  770. DEL_BIT(flags_neol, SUB_EOL|SUB_LNF);
  771. while (TRUE)
  772. {
  773. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  774. {
  775. *pto++ = *pti++;
  776. *pto++ = *pti++;
  777. continue;
  778. }
  779. switch (*pti)
  780. {
  781. case '\0':
  782. if (HAS_BIT(flags, SUB_EOL))
  783. {
  784. if (HAS_BIT(ses->flags, SES_FLAG_RUN))
  785. {
  786. *pto++ = '\r';
  787. }
  788. else
  789. {
  790. *pto++ = '\r';
  791. *pto++ = '\n';
  792. }
  793. }
  794. if (HAS_BIT(flags, SUB_LNF))
  795. {
  796. *pto++ = '\n';
  797. }
  798. *pto = 0;
  799. if (string == result)
  800. {
  801. strcpy(result, buffer);
  802. pop_call();
  803. return pto - buffer;
  804. }
  805. else
  806. {
  807. pop_call();
  808. return pto - result;
  809. }
  810. break;
  811. case '@':
  812. if (HAS_BIT(flags, SUB_FUN) && !HAS_BIT(ses->list[LIST_FUNCTION]->flags, LIST_FLAG_IGNORE))
  813. {
  814. i = 1;
  815. escape = FALSE;
  816. sesptr = NULL;
  817. while (pti[i] == '@')
  818. {
  819. escape = TRUE;
  820. i++;
  821. }
  822. for (ptt = temp ; isalnum((int) pti[i]) || pti[i] == '_' ; i++)
  823. {
  824. *ptt++ = pti[i];
  825. }
  826. *ptt = 0;
  827. if (pti[i] != DEFAULT_OPEN)
  828. {
  829. while (*pti == '@')
  830. {
  831. *pto++ = *pti++;
  832. }
  833. continue;
  834. }
  835. node = search_node_list(ses->list[LIST_FUNCTION], temp);
  836. if (node == NULL)
  837. {
  838. sesptr = find_session(temp);
  839. }
  840. if (sesptr == NULL && node == NULL)
  841. {
  842. while (*pti == '@')
  843. {
  844. *pto++ = *pti++;
  845. }
  846. continue;
  847. }
  848. if (escape)
  849. {
  850. pti++;
  851. while (*pti == '@')
  852. {
  853. *pto++ = *pti++;
  854. }
  855. continue;
  856. }
  857. pti = get_arg_in_braces(ses, &pti[i], temp, GET_ONE);
  858. if (sesptr)
  859. {
  860. substitute(sesptr, temp, pto, flags_neol);
  861. pto += strlen(pto);
  862. continue;
  863. }
  864. else
  865. {
  866. substitute(ses, temp, buf, flags_neol);
  867. }
  868. show_debug(ses, LIST_FUNCTION, "#DEBUG FUNCTION {%s}", node->arg1);
  869. RESTRING(gtd->vars[0], buf);
  870. pte = buf;
  871. for (i = 1 ; i < 100 ; i++)
  872. {
  873. pte = get_arg_in_braces(ses, pte, temp, GET_ALL);
  874. RESTRING(gtd->vars[i], temp);
  875. if (*pte == 0)
  876. {
  877. while (++i < 100)
  878. {
  879. if (*gtd->vars[i])
  880. {
  881. RESTRING(gtd->vars[i], "");
  882. }
  883. }
  884. break;
  885. }
  886. if (*pte == COMMAND_SEPARATOR)
  887. {
  888. pte++;
  889. }
  890. }
  891. substitute(ses, node->arg2, buf, SUB_ARG);
  892. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  893. {
  894. delete_node_list(ses, LIST_FUNCTION, node);
  895. }
  896. script_driver(ses, LIST_FUNCTION, buf);
  897. substitute(ses, "$result", pto, flags_neol|SUB_VAR);
  898. pto += strlen(pto);
  899. }
  900. else
  901. {
  902. if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG) && is_function(ses, pti))
  903. {
  904. *pto++ = '\\';
  905. }
  906. *pto++ = *pti++;
  907. }
  908. break;
  909. case '*':
  910. if (HAS_BIT(flags, SUB_VAR) && !HAS_BIT(ses->list[LIST_VARIABLE]->flags, LIST_FLAG_IGNORE) && pti[1])
  911. {
  912. int brace = FALSE;
  913. i = 1;
  914. escape = FALSE;
  915. while (pti[i] == '*')
  916. {
  917. escape = TRUE;
  918. i++;
  919. }
  920. if (pti[i] == DEFAULT_OPEN)
  921. {
  922. brace = TRUE;
  923. ptt = get_arg_in_braces(ses, &pti[i], buf, GET_ALL);
  924. i += strlen(buf) + 2;
  925. substitute(ses, buf, temp, flags_neol);
  926. }
  927. else
  928. {
  929. ptt = temp;
  930. while (isalnum((int) pti[i]) || pti[i] == '_')
  931. {
  932. *ptt++ = pti[i];
  933. i++;
  934. }
  935. *ptt = 0;
  936. }
  937. if (*temp)
  938. {
  939. root = local_list(ses);
  940. if ((node = search_node_list(root, temp)) == NULL)
  941. {
  942. root = ses->list[LIST_VARIABLE];
  943. node = search_node_list(root, temp);
  944. }
  945. }
  946. else
  947. {
  948. root = ses->list[LIST_VARIABLE];
  949. node = NULL;
  950. }
  951. if (brace == FALSE && node == NULL)
  952. {
  953. while (*pti == '*')
  954. {
  955. *pto++ = *pti++;
  956. }
  957. continue;
  958. }
  959. if (escape)
  960. {
  961. pti++;
  962. while (*pti == '*')
  963. {
  964. *pto++ = *pti++;
  965. }
  966. continue;
  967. }
  968. if (brace == FALSE)
  969. {
  970. pti = get_arg_at_brackets(ses, &pti[i], temp + strlen(temp));
  971. }
  972. else
  973. {
  974. pti = ptt;
  975. }
  976. substitute(ses, temp, buf, flags_neol);
  977. str = str_dup("");
  978. get_nest_node_key(root, buf, &str, brace);
  979. substitute(ses, str, pto, flags_neol - SUB_VAR);
  980. pto += strlen(pto);
  981. str_free(str);
  982. }
  983. else
  984. {
  985. if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG) && is_variable(ses, pti))
  986. {
  987. *pto++ = '\\';
  988. }
  989. *pto++ = *pti++;
  990. }
  991. break;
  992. case '$':
  993. if (HAS_BIT(flags, SUB_VAR) && !HAS_BIT(ses->list[LIST_VARIABLE]->flags, LIST_FLAG_IGNORE) && pti[1])
  994. {
  995. int brace = FALSE;
  996. i = 1;
  997. escape = FALSE;
  998. while (pti[i] == '$')
  999. {
  1000. escape = TRUE;
  1001. i++;
  1002. }
  1003. if (pti[i] == DEFAULT_OPEN)
  1004. {
  1005. brace = TRUE;
  1006. ptt = get_arg_in_braces(ses, &pti[i], buf, GET_ALL);
  1007. i += strlen(buf) + 2;
  1008. substitute(ses, buf, temp, flags_neol);
  1009. }
  1010. else
  1011. {
  1012. ptt = temp;
  1013. while (isalnum((int) pti[i]) || pti[i] == '_')
  1014. {
  1015. *ptt++ = pti[i];
  1016. i++;
  1017. }
  1018. *ptt = 0;
  1019. }
  1020. if (*temp)
  1021. {
  1022. root = search_nest_base_ses(ses, temp);
  1023. if (root)
  1024. {
  1025. node = search_node_list(root, temp);
  1026. }
  1027. else
  1028. {
  1029. root = ses->list[LIST_VARIABLE];
  1030. node = NULL;
  1031. }
  1032. }
  1033. else
  1034. {
  1035. root = ses->list[LIST_VARIABLE];
  1036. node = NULL;
  1037. }
  1038. if (brace == FALSE && node == NULL)
  1039. {
  1040. while (*pti == '$')
  1041. {
  1042. *pto++ = *pti++;
  1043. }
  1044. continue;
  1045. }
  1046. if (escape)
  1047. {
  1048. pti++;
  1049. while (*pti == '$')
  1050. {
  1051. *pto++ = *pti++;
  1052. }
  1053. continue;
  1054. }
  1055. if (brace == FALSE)
  1056. {
  1057. pti = get_arg_at_brackets(ses, &pti[i], temp + strlen(temp));
  1058. }
  1059. else
  1060. {
  1061. pti = ptt;
  1062. }
  1063. substitute(ses, temp, buf, flags_neol);
  1064. str = str_dup("");
  1065. get_nest_node_val(root, buf, &str, brace);
  1066. substitute(ses, str, pto, flags_neol - SUB_VAR);
  1067. pto += strlen(pto);
  1068. str_free(str);
  1069. }
  1070. else
  1071. {
  1072. if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG) && is_variable(ses, pti))
  1073. {
  1074. *pto++ = '\\';
  1075. }
  1076. *pto++ = *pti++;
  1077. }
  1078. break;
  1079. case '&':
  1080. if (HAS_BIT(flags, SUB_CMD) && (isdigit((int) pti[1]) || pti[1] == '&'))
  1081. {
  1082. if (pti[1] == '&')
  1083. {
  1084. while (pti[1] == '&')
  1085. {
  1086. *pto++ = *pti++;
  1087. }
  1088. if (isdigit((int) pti[1]))
  1089. {
  1090. pti++;
  1091. }
  1092. else
  1093. {
  1094. *pto++ = *pti++;
  1095. }
  1096. }
  1097. else
  1098. {
  1099. i = isdigit((int) pti[2]) ? (pti[1] - '0') * 10 + pti[2] - '0' : pti[1] - '0';
  1100. for (cnt = 0 ; gtd->cmds[i][cnt] ; cnt++)
  1101. {
  1102. *pto++ = gtd->cmds[i][cnt];
  1103. }
  1104. pti += isdigit((int) pti[2]) ? 3 : 2;
  1105. }
  1106. }
  1107. else if (HAS_BIT(flags, SUB_VAR) && !HAS_BIT(ses->list[LIST_VARIABLE]->flags, LIST_FLAG_IGNORE))
  1108. {
  1109. int brace = FALSE;
  1110. i = 1;
  1111. escape = FALSE;
  1112. while (pti[i] == '&')
  1113. {
  1114. escape = TRUE;
  1115. i++;
  1116. }
  1117. if (pti[i] == DEFAULT_OPEN)
  1118. {
  1119. brace = TRUE;
  1120. ptt = get_arg_in_braces(ses, &pti[i], buf, GET_ALL);
  1121. i += strlen(buf) + 2;
  1122. substitute(ses, buf, temp, flags_neol);
  1123. }
  1124. else
  1125. {
  1126. ptt = temp;
  1127. while (isalnum((int) pti[i]) || pti[i] == '_')
  1128. {
  1129. *ptt++ = pti[i];
  1130. i++;
  1131. }
  1132. *ptt = 0;
  1133. }
  1134. if (*temp)
  1135. {
  1136. root = local_list(ses);
  1137. if ((node = search_node_list(root, temp)) == NULL)
  1138. {
  1139. root = ses->list[LIST_VARIABLE];
  1140. node = search_node_list(root, temp);
  1141. }
  1142. }
  1143. else
  1144. {
  1145. root = ses->list[LIST_VARIABLE];
  1146. node = NULL;
  1147. }
  1148. if (brace == FALSE && node == NULL)
  1149. {
  1150. while (*pti == '&')
  1151. {
  1152. *pto++ = *pti++;
  1153. }
  1154. continue;
  1155. }
  1156. if (escape)
  1157. {
  1158. pti++;
  1159. while (*pti == '&')
  1160. {
  1161. *pto++ = *pti++;
  1162. }
  1163. continue;
  1164. }
  1165. if (brace == FALSE)
  1166. {
  1167. pti = get_arg_at_brackets(ses, &pti[i], temp + strlen(temp));
  1168. }
  1169. else
  1170. {
  1171. pti = ptt;
  1172. }
  1173. substitute(ses, temp, buf, flags_neol);
  1174. str = str_dup("");
  1175. get_nest_index(root, buf, &str, brace);
  1176. substitute(ses, str, pto, flags_neol - SUB_VAR);
  1177. pto += strlen(pto);
  1178. str_free(str);
  1179. }
  1180. else
  1181. {
  1182. if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG) && is_variable(ses, pti))
  1183. {
  1184. *pto++ = '\\';
  1185. }
  1186. *pto++ = *pti++;
  1187. }
  1188. break;
  1189. case '%':
  1190. if (HAS_BIT(flags, SUB_ARG) && (isdigit((int) pti[1]) || pti[1] == '%'))
  1191. {
  1192. if (pti[1] == '%')
  1193. {
  1194. while (pti[1] == '%')
  1195. {
  1196. *pto++ = *pti++;
  1197. }
  1198. pti++;
  1199. }
  1200. else
  1201. {
  1202. i = isdigit((int) pti[2]) ? (pti[1] - '0') * 10 + pti[2] - '0' : pti[1] - '0';
  1203. ptt = gtd->vars[i];
  1204. while (*ptt)
  1205. {
  1206. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, ptt))
  1207. {
  1208. *pto++ = *ptt++;
  1209. *pto++ = *ptt++;
  1210. continue;
  1211. }
  1212. if (HAS_BIT(flags, SUB_SEC))
  1213. {
  1214. switch (*ptt)
  1215. {
  1216. case '\\':
  1217. *pto++ = '\\';
  1218. *pto++ = '\\';
  1219. break;
  1220. case '{':
  1221. *pto++ = '\\';
  1222. *pto++ = 'x';
  1223. *pto++ = '7';
  1224. *pto++ = 'B';
  1225. break;
  1226. case '}':
  1227. *pto++ = '\\';
  1228. *pto++ = 'x';
  1229. *pto++ = '7';
  1230. *pto++ = 'D';
  1231. break;
  1232. case '$':
  1233. case '&':
  1234. case '*':
  1235. if (is_variable(ses, ptt))
  1236. {
  1237. *pto++ = '\\';
  1238. *pto++ = *ptt;
  1239. }
  1240. else
  1241. {
  1242. *pto++ = *ptt;
  1243. }
  1244. break;
  1245. case '@':
  1246. if (is_function(ses, ptt))
  1247. {
  1248. *pto++ = '\\';
  1249. *pto++ = *ptt;
  1250. }
  1251. else
  1252. {
  1253. *pto++ = *ptt;
  1254. }
  1255. break;
  1256. case COMMAND_SEPARATOR:
  1257. *pto++ = '\\';
  1258. *pto++ = COMMAND_SEPARATOR;
  1259. break;
  1260. default:
  1261. *pto++ = *ptt;
  1262. break;
  1263. }
  1264. ptt++;
  1265. }
  1266. else
  1267. {
  1268. *pto++ = *ptt++;
  1269. }
  1270. }
  1271. pti += isdigit((int) pti[2]) ? 3 : 2;
  1272. }
  1273. }
  1274. else if (pti[1] == '*') // avoid %*variable triggers
  1275. {
  1276. *pto++ = *pti++;
  1277. *pto++ = *pti++;
  1278. }
  1279. else
  1280. {
  1281. *pto++ = *pti++;
  1282. }
  1283. break;
  1284. case '<':
  1285. if (HAS_BIT(flags, SUB_COL))
  1286. {
  1287. if (HAS_BIT(flags, SUB_CMP) && old[0] && !strncmp(old, pti, strlen(old)))
  1288. {
  1289. pti += strlen(old);
  1290. }
  1291. else if (isdigit(pti[1]) && isdigit(pti[2]) && isdigit(pti[3]) && pti[4] == '>')
  1292. {
  1293. if (pti[1] != '8' || pti[2] != '8' || pti[3] != '8')
  1294. {
  1295. *pto++ = ASCII_ESC;
  1296. *pto++ = '[';
  1297. switch (pti[1])
  1298. {
  1299. case '2':
  1300. *pto++ = '2';
  1301. *pto++ = '2';
  1302. *pto++ = ';';
  1303. break;
  1304. case '8':
  1305. break;
  1306. default:
  1307. *pto++ = pti[1];
  1308. *pto++ = ';';
  1309. }
  1310. switch (pti[2])
  1311. {
  1312. case '8':
  1313. break;
  1314. default:
  1315. *pto++ = '3';
  1316. *pto++ = pti[2];
  1317. *pto++ = ';';
  1318. break;
  1319. }
  1320. switch (pti[3])
  1321. {
  1322. case '8':
  1323. break;
  1324. default:
  1325. *pto++ = '4';
  1326. *pto++ = pti[3];
  1327. *pto++ = ';';
  1328. break;
  1329. }
  1330. pto--;
  1331. *pto++ = 'm';
  1332. }
  1333. pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
  1334. }
  1335. else if (pti[1] >= 'a' && pti[1] <= 'f' && pti[2] >= 'a' && pti[2] <= 'f' && pti[3] >= 'a' && pti[3] <= 'f' && pti[4] == '>')
  1336. {
  1337. cnt = 16 + (pti[1] - 'a') * 36 + (pti[2] - 'a') * 6 + (pti[3] - 'a');
  1338. if (ses->color >= 256)
  1339. {
  1340. pto += sprintf(pto, "\e[38;5;%dm", cnt);
  1341. }
  1342. else if (ses->color == 16)
  1343. {
  1344. pto += sprintf(pto, "%s", c256to16_fg[cnt]);
  1345. }
  1346. pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
  1347. }
  1348. else if (pti[1] >= 'A' && pti[1] <= 'F' && pti[2] >= 'A' && pti[2] <= 'F' && pti[3] >= 'A' && pti[3] <= 'F' && pti[4] == '>')
  1349. {
  1350. cnt = 16 + (pti[1] - 'A') * 36 + (pti[2] - 'A') * 6 + (pti[3] - 'A');
  1351. if (ses->color >= 256)
  1352. {
  1353. pto += sprintf(pto, "\e[48;5;%dm", cnt);
  1354. }
  1355. else if (ses->color == 16)
  1356. {
  1357. pto += sprintf(pto, "%s", c256to16_bg[cnt]);
  1358. }
  1359. pti += sprintf(old, "<%c%c%c>", pti[1], pti[2], pti[3]);
  1360. }
  1361. else if (pti[1] == 'g' && isdigit((int) pti[2]) && isdigit((int) pti[3]) && pti[4] == '>')
  1362. {
  1363. cnt = 232 + (pti[2] - '0') * 10 + (pti[3] - '0');
  1364. if (ses->color >= 256)
  1365. {
  1366. pto += sprintf(pto, "\e[38;5;%dm", cnt);
  1367. }
  1368. else if (ses->color == 16)
  1369. {
  1370. pto += sprintf(pto, "%s", c256to16_fg[cnt]);
  1371. }
  1372. pti += sprintf(old, "<g%c%c>", pti[2], pti[3]);
  1373. }
  1374. else if (pti[1] == 'G' && isdigit((int) pti[2]) && isdigit((int) pti[3]) && pti[4] == '>')
  1375. {
  1376. cnt = 232 + (pti[2] - '0') * 10 + (pti[3] - '0');
  1377. if (ses->color >= 256)
  1378. {
  1379. pto += sprintf(pto, "\e[48;5;%dm", cnt);
  1380. }
  1381. else if (ses->color == 16)
  1382. {
  1383. pto += sprintf(pto, "%s", c256to16_bg[cnt]);
  1384. }
  1385. pti += sprintf(old, "<G%c%c>", pti[2], pti[3]);
  1386. }
  1387. else if (toupper((int) pti[1]) == 'F' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  1388. {
  1389. if (ses->color == 4096)
  1390. {
  1391. pto += sprintf(pto, "\e[38;2;%d;%d;%dm", c4096_val(pti[2], pti[2]), c4096_val(pti[3], pti[3]), c4096_val(pti[4], pti[4]));
  1392. }
  1393. else if (ses->color == 256)
  1394. {
  1395. pto += sprintf(pto, "\033[38;5;%dm", 16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4]));
  1396. }
  1397. else if (ses->color == 16)
  1398. {
  1399. pto += sprintf(pto, "%s", c256to16_fg[16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4])]);
  1400. }
  1401. pti += sprintf(old, "<F%c%c%c>", pti[2], pti[3], pti[4]);
  1402. }
  1403. else if (toupper(pti[1]) == 'F' && pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  1404. {
  1405. c4096_rnd(ses, &pti[2]);
  1406. if (ses->color == 4096)
  1407. {
  1408. pto += sprintf(pto, "\e[38;2;%d;%d;%dm", c4096_val(pti[2], pti[2]), c4096_val(pti[3], pti[3]), c4096_val(pti[4], pti[4]));
  1409. }
  1410. else if (ses->color == 256)
  1411. {
  1412. pto += sprintf(pto, "\033[38;5;%dm", 16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4]));
  1413. }
  1414. else if (ses->color == 16)
  1415. {
  1416. pto += sprintf(pto, "%s", c256to16_fg[16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4])]);
  1417. }
  1418. pti += sprintf(old, "<F%c%c%c>", pti[2], pti[3], pti[4]);
  1419. }
  1420. else if (toupper((int) pti[1]) == 'F' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  1421. {
  1422. if (ses->color == 4096)
  1423. {
  1424. pto += sprintf(pto, "\e[38;2;%d;%d;%dm", c4096_val(pti[2], pti[3]), c4096_val(pti[4], pti[5]), c4096_val(pti[6], pti[7]));
  1425. }
  1426. else if (ses->color == 256)
  1427. {
  1428. pto += sprintf(pto, "\033[38;5;%dm", 16 + c4096_to_256_val(pti[2], pti[3]) * 36 + c4096_to_256_val(pti[4], pti[5]) * 6 + c4096_to_256_val(pti[6], pti[7]));
  1429. }
  1430. else if (ses->color == 16)
  1431. {
  1432. pto += sprintf(pto, "%s", c256to16_fg[16 + c4096_to_256_val(pti[2], pti[3]) * 36 + c4096_to_256_val(pti[4], pti[5]) * 6 + c4096_to_256_val(pti[6], pti[7])]);
  1433. }
  1434. pti += sprintf(old, "<F%c%c%c%c%c%c>", pti[2], pti[3], pti[4], pti[5], pti[6], pti[7]);
  1435. }
  1436. else if (toupper(pti[1]) == 'B' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && pti[5] == '>')
  1437. {
  1438. if (ses->color == 4096)
  1439. {
  1440. pto += sprintf(pto, "\e[48;2;%d;%d;%dm", c4096_val(pti[2], pti[2]), c4096_val(pti[3], pti[3]), c4096_val(pti[4], pti[4]));
  1441. }
  1442. else if (ses->color == 256)
  1443. {
  1444. pto += sprintf(pto, "\033[48;5;%dm", 16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4]));
  1445. }
  1446. else if (ses->color == 16)
  1447. {
  1448. pto += sprintf(pto, "%s", c256to16_bg[16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4])]);
  1449. }
  1450. pti += sprintf(old, "<B%c%c%c>", pti[2], pti[3], pti[4]);
  1451. }
  1452. else if (toupper(pti[1]) == 'B' && pti[2] == '?' && pti[3] == '?' && pti[4] == '?' && pti[5] == '>')
  1453. {
  1454. c4096_rnd(ses, &pti[2]);
  1455. if (ses->color == 4096)
  1456. {
  1457. pto += sprintf(pto, "\e[48;2;%d;%d;%dm", c4096_val(pti[2], pti[2]), c4096_val(pti[3], pti[3]), c4096_val(pti[4], pti[4]));
  1458. }
  1459. else if (ses->color == 256)
  1460. {
  1461. pto += sprintf(pto, "\033[48;5;%dm", 16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4]));
  1462. }
  1463. else if (ses->color == 16)
  1464. {
  1465. pto += sprintf(pto, "%s", c256to16_bg[16 + c4096_to_256_val(pti[2], pti[2]) * 36 + c4096_to_256_val(pti[3], pti[3]) * 6 + c4096_to_256_val(pti[4], pti[4])]);
  1466. }
  1467. pti += sprintf(old, "<F%c%c%c>", pti[2], pti[3], pti[4]);
  1468. }
  1469. else if (toupper(pti[1]) == 'B' && isxdigit(pti[2]) && isxdigit(pti[3]) && isxdigit(pti[4]) && isxdigit(pti[5]) && isxdigit(pti[6]) && isxdigit(pti[7]) && pti[8] == '>')
  1470. {
  1471. if (ses->color == 4096)
  1472. {
  1473. pto += sprintf(pto, "\e[48;2;%d;%d;%dm", c4096_val(pti[2], pti[3]), c4096_val(pti[4], pti[5]), c4096_val(pti[6], pti[7]));
  1474. }
  1475. else if (ses->color == 256)
  1476. {
  1477. pto += sprintf(pto, "\033[48;5;%dm", 16 + c4096_to_256_val(pti[2], pti[3]) * 36 + c4096_to_256_val(pti[4], pti[5]) * 6 + c4096_to_256_val(pti[6], pti[7]));
  1478. }
  1479. else if (ses->color == 16)
  1480. {
  1481. pto += sprintf(pto, "%s", c256to16_bg[16 + c4096_to_256_val(pti[2], pti[3]) * 36 + c4096_to_256_val(pti[4], pti[5]) * 6 + c4096_to_256_val(pti[6], pti[7])]);
  1482. }
  1483. pti += sprintf(old, "<B%c%c%c%c%c%c>", pti[2], pti[3], pti[4], pti[5], pti[6], pti[7]);
  1484. }
  1485. else
  1486. {
  1487. *pto++ = *pti++;
  1488. }
  1489. }
  1490. else
  1491. {
  1492. *pto++ = *pti++;
  1493. }
  1494. break;
  1495. case '\\':
  1496. if (HAS_BIT(flags, SUB_ESC))
  1497. {
  1498. pti++;
  1499. switch (*pti)
  1500. {
  1501. case 'a':
  1502. *pto++ = '\a';
  1503. break;
  1504. case 'b':
  1505. *pto++ = '\b';
  1506. break;
  1507. case 'c':
  1508. if (pti[1])
  1509. {
  1510. pti++;
  1511. *pto++ = *pti % 32;
  1512. }
  1513. break;
  1514. case 'e':
  1515. *pto++ = '\e';
  1516. break;
  1517. case 'f':
  1518. *pto++ = '\f';
  1519. break;
  1520. case 'n':
  1521. *pto++ = '\n';
  1522. break;
  1523. case 'r':
  1524. *pto++ = '\r';
  1525. break;
  1526. case 't':
  1527. *pto++ = '\t';
  1528. break;
  1529. case 'x':
  1530. if (pti[1] && pti[2])
  1531. {
  1532. if (pti[1] == '0' && pti[2] == '0' && pti[3] == 0)
  1533. {
  1534. pti += 2;
  1535. DEL_BIT(flags, SUB_EOL);
  1536. DEL_BIT(flags, SUB_LNF);
  1537. }
  1538. else
  1539. {
  1540. pti++;
  1541. *pto++ = hex_number_8bit(pti);
  1542. pti++;
  1543. }
  1544. }
  1545. break;
  1546. case 'u':
  1547. if (pti[1] && pti[2] && pti[3] && pti[4])
  1548. {
  1549. pto += unicode_16_bit(&pti[1], pto);
  1550. pti += 4;
  1551. }
  1552. break;
  1553. case 'U':
  1554. if (pti[1] && pti[2] && pti[3] && pti[4] && pti[5] && pti[6])
  1555. {
  1556. pto += unicode_21_bit(&pti[1], pto);
  1557. pti += 6;
  1558. }
  1559. break;
  1560. case 'v':
  1561. *pto++ = '\v';
  1562. break;
  1563. case '0':
  1564. if (pti[1] == 0)
  1565. {
  1566. DEL_BIT(flags, SUB_EOL);
  1567. DEL_BIT(flags, SUB_LNF);
  1568. }
  1569. else if (pti[1] && pti[2])
  1570. {
  1571. pti++;
  1572. *pto++ = oct_number(pti);
  1573. pti++;
  1574. }
  1575. break;
  1576. case '\0':
  1577. DEL_BIT(flags, SUB_EOL);
  1578. DEL_BIT(flags, SUB_LNF);
  1579. continue;
  1580. default:
  1581. *pto++ = *pti;
  1582. break;
  1583. }
  1584. pti++;
  1585. }
  1586. else if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG))
  1587. {
  1588. *pto++ = '\\';
  1589. *pto++ = *pti++;
  1590. }
  1591. else if (HAS_BIT(flags, SUB_LIT))
  1592. {
  1593. *pto++ = *pti++;
  1594. }
  1595. else
  1596. {
  1597. *pto++ = *pti++;
  1598. if (*pti)
  1599. {
  1600. *pto++ = *pti++;
  1601. }
  1602. }
  1603. break;
  1604. case ASCII_ESC:
  1605. if (HAS_BIT(flags, SUB_COL) && ses->color == 0)
  1606. {
  1607. skip = find_color_code(pti);
  1608. if (skip)
  1609. {
  1610. pti += skip;
  1611. }
  1612. else
  1613. {
  1614. *pto++ = *pti++;
  1615. }
  1616. }
  1617. else
  1618. {
  1619. *pto++ = *pti++;
  1620. }
  1621. break;
  1622. default:
  1623. if (HAS_BIT(flags, SUB_SEC) && !HAS_BIT(flags, SUB_ARG))
  1624. {
  1625. switch (*pti)
  1626. {
  1627. case '{':
  1628. *pto++ = '\\';
  1629. *pto++ = 'x';
  1630. *pto++ = '7';
  1631. *pto++ = 'B';
  1632. break;
  1633. case '}':
  1634. *pto++ = '\\';
  1635. *pto++ = 'x';
  1636. *pto++ = '7';
  1637. *pto++ = 'D';
  1638. break;
  1639. case COMMAND_SEPARATOR:
  1640. *pto++ = '\\';
  1641. *pto++ = COMMAND_SEPARATOR;
  1642. break;
  1643. default:
  1644. *pto++ = *pti;
  1645. break;
  1646. }
  1647. pti++;
  1648. }
  1649. else
  1650. {
  1651. *pto++ = *pti++;
  1652. }
  1653. break;
  1654. }
  1655. }
  1656. pop_call();
  1657. return 0;
  1658. }