showmsg.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #include <time.h>
  7. #include <stdlib.h> // atexit
  8. #include "cbasetypes.h"
  9. #include "showmsg.h"
  10. #include "utils.h"
  11. #ifdef _WIN32
  12. #define WIN32_LEAN_AND_MEAN
  13. #include <windows.h>
  14. #ifdef DEBUGLOGMAP
  15. #define DEBUGLOGPATH "log\\map-server.log"
  16. #else
  17. #ifdef DEBUGLOGCHAR
  18. #define DEBUGLOGPATH "log\\char-server.log"
  19. #else
  20. #ifdef DEBUGLOGLOGIN
  21. #define DEBUGLOGPATH "log\\login-server.log"
  22. #endif
  23. #endif
  24. #endif
  25. #else
  26. #include <unistd.h>
  27. #include <ctype.h>
  28. #ifdef DEBUGLOGMAP
  29. #define DEBUGLOGPATH "log/map-server.log"
  30. #else
  31. #ifdef DEBUGLOGCHAR
  32. #define DEBUGLOGPATH "log/char-server.log"
  33. #else
  34. #ifdef DEBUGLOGLOGIN
  35. #define DEBUGLOGPATH "log/login-server.log"
  36. #endif
  37. #endif
  38. #endif
  39. #endif
  40. ///////////////////////////////////////////////////////////////////////////////
  41. /// behavioral parameter.
  42. /// when true, prints ansi sequences also when redirecting outputs to file
  43. /// otherwise remove them
  44. int stdout_with_ansisequence = 1;
  45. int msg_silent; //Specifies how silent the console is.
  46. ///////////////////////////////////////////////////////////////////////////////
  47. /// static/dynamic buffer for the messages
  48. #define SBUF_SIZE 2048 // never put less that what's required for the debug message
  49. #define NEWBUF(buf) \
  50. struct { \
  51. char s_[SBUF_SIZE]; \
  52. struct StringBuf *d_; \
  53. char *v_; \
  54. int l_; \
  55. } buf ={"",NULL,NULL,0}; \
  56. //define NEWBUF
  57. #define BUFVPRINTF(buf,fmt,args) \
  58. buf.l_ = vsnprintf(buf.s_, SBUF_SIZE, fmt, args); \
  59. if( buf.l_ >= 0 && buf.l_ < SBUF_SIZE ) \
  60. {/* static buffer */ \
  61. buf.v_ = buf.s_; \
  62. } \
  63. else \
  64. {/* dynamic buffer */ \
  65. buf.d_ = StringBuf_Malloc(); \
  66. buf.l_ = StringBuf_Vprintf(buf.d_, fmt, args); \
  67. buf.v_ = StringBuf_Value(buf.d_); \
  68. ShowDebug("showmsg: dynamic buffer used, increase the static buffer size to %d or more.", buf.l_+1);\
  69. } \
  70. //define BUFVPRINTF
  71. #define BUFVAL(buf) buf.v_
  72. #define BUFLEN(buf) buf.l_
  73. #define FREEBUF(buf) \
  74. if( buf.d_ ) \
  75. { \
  76. StringBuf_Free(buf.d_); \
  77. buf.d_ = NULL; \
  78. } \
  79. buf.v_ = NULL; \
  80. //define FREEBUF
  81. ///////////////////////////////////////////////////////////////////////////////
  82. #ifdef _WIN32
  83. // XXX adapted from eApp (comments are left untouched) [flaviojs]
  84. ///////////////////////////////////////////////////////////////////////////////
  85. // ansi compatible printf with control sequence parser for windows
  86. // fast hack, handle with care, not everything implemented
  87. //
  88. // \033[#;...;#m - Set Graphics Rendition (SGR)
  89. //
  90. // printf("\x1b[1;31;40m"); // Bright red on black
  91. // printf("\x1b[3;33;45m"); // Blinking yellow on magenta (blink not implemented)
  92. // printf("\x1b[1;30;47m"); // Bright black (grey) on dim white
  93. //
  94. // Style Foreground Background
  95. // 1st Digit 2nd Digit 3rd Digit RGB
  96. // 0 - Reset 30 - Black 40 - Black 000
  97. // 1 - FG Bright 31 - Red 41 - Red 100
  98. // 2 - Unknown 32 - Green 42 - Green 010
  99. // 3 - Blink 33 - Yellow 43 - Yellow 110
  100. // 4 - Underline 34 - Blue 44 - Blue 001
  101. // 5 - BG Bright 35 - Magenta 45 - Magenta 101
  102. // 6 - Unknown 36 - Cyan 46 - Cyan 011
  103. // 7 - Reverse 37 - White 47 - White 111
  104. // 8 - Concealed (invisible)
  105. //
  106. // \033[#A - Cursor Up (CUU)
  107. // Moves the cursor up by the specified number of lines without changing columns.
  108. // If the cursor is already on the top line, this sequence is ignored. \e[A is equivalent to \e[1A.
  109. //
  110. // \033[#B - Cursor Down (CUD)
  111. // Moves the cursor down by the specified number of lines without changing columns.
  112. // If the cursor is already on the bottom line, this sequence is ignored. \e[B is equivalent to \e[1B.
  113. //
  114. // \033[#C - Cursor Forward (CUF)
  115. // Moves the cursor forward by the specified number of columns without changing lines.
  116. // If the cursor is already in the rightmost column, this sequence is ignored. \e[C is equivalent to \e[1C.
  117. //
  118. // \033[#D - Cursor Backward (CUB)
  119. // Moves the cursor back by the specified number of columns without changing lines.
  120. // If the cursor is already in the leftmost column, this sequence is ignored. \e[D is equivalent to \e[1D.
  121. //
  122. // \033[#E - Cursor Next Line (CNL)
  123. // Moves the cursor down the indicated # of rows, to column 1. \e[E is equivalent to \e[1E.
  124. //
  125. // \033[#F - Cursor Preceding Line (CPL)
  126. // Moves the cursor up the indicated # of rows, to column 1. \e[F is equivalent to \e[1F.
  127. //
  128. // \033[#G - Cursor Horizontal Absolute (CHA)
  129. // Moves the cursor to indicated column in current row. \e[G is equivalent to \e[1G.
  130. //
  131. // \033[#;#H - Cursor Position (CUP)
  132. // Moves the cursor to the specified position. The first # specifies the line number,
  133. // the second # specifies the column. If you do not specify a position, the cursor moves to the home position:
  134. // the upper-left corner of the screen (line 1, column 1).
  135. //
  136. // \033[#;#f - Horizontal & Vertical Position
  137. // (same as \033[#;#H)
  138. //
  139. // \033[s - Save Cursor Position (SCP)
  140. // The current cursor position is saved.
  141. //
  142. // \033[u - Restore cursor position (RCP)
  143. // Restores the cursor position saved with the (SCP) sequence \033[s.
  144. // (addition, restore to 0,0 if nothinh was saved before)
  145. //
  146. // \033[#J - Erase Display (ED)
  147. // Clears the screen and moves to the home position
  148. // \033[0J - Clears the screen from cursor to end of display. The cursor position is unchanged. (default)
  149. // \033[1J - Clears the screen from start to cursor. The cursor position is unchanged.
  150. // \033[2J - Clears the screen and moves the cursor to the home position (line 1, column 1).
  151. //
  152. // \033[#K - Erase Line (EL)
  153. // Clears the current line from the cursor position
  154. // \033[0K - Clears all characters from the cursor position to the end of the line (including the character at the cursor position). The cursor position is unchanged. (default)
  155. // \033[1K - Clears all characters from start of line to the cursor position. (including the character at the cursor position). The cursor position is unchanged.
  156. // \033[2K - Clears all characters of the whole line. The cursor position is unchanged.
  157. /*
  158. not implemented
  159. \033[#L
  160. IL: Insert Lines: The cursor line and all lines below it move down # lines, leaving blank space. The cursor position is unchanged. The bottommost # lines are lost. \e[L is equivalent to \e[1L.
  161. \033[#M
  162. DL: Delete Line: The block of # lines at and below the cursor are deleted; all lines below them move up # lines to fill in the gap, leaving # blank lines at the bottom of the screen. The cursor position is unchanged. \e[M is equivalent to \e[1M.
  163. \033[#\@
  164. ICH: Insert CHaracter: The cursor character and all characters to the right of it move right # columns, leaving behind blank space. The cursor position is unchanged. The rightmost # characters on the line are lost. \e[\@ is equivalent to \e[1\@.
  165. \033[#P
  166. DCH: Delete CHaracter: The block of # characters at and to the right of the cursor are deleted; all characters to the right of it move left # columns, leaving behind blank space. The cursor position is unchanged. \e[P is equivalent to \e[1P.
  167. Escape sequences for Select Character Set
  168. */
  169. #define is_console(handle) (FILE_TYPE_CHAR==GetFileType(handle))
  170. ///////////////////////////////////////////////////////////////////////////////
  171. int VFPRINTF(HANDLE handle, const char *fmt, va_list argptr)
  172. {
  173. /////////////////////////////////////////////////////////////////
  174. /* XXX Two streams are being used. Disabled to avoid inconsistency [flaviojs]
  175. static COORD saveposition = {0,0};
  176. */
  177. /////////////////////////////////////////////////////////////////
  178. unsigned long written;
  179. char *p, *q;
  180. NEWBUF(tempbuf); // temporary buffer
  181. if(!fmt || !*fmt)
  182. return 0;
  183. // Print everything to the buffer
  184. BUFVPRINTF(tempbuf,fmt,argptr);
  185. if( !is_console(handle) && stdout_with_ansisequence )
  186. {
  187. WriteFile(handle, BUFVAL(tempbuf), BUFLEN(tempbuf), &written, 0);
  188. return 0;
  189. }
  190. // start with processing
  191. p = BUFVAL(tempbuf);
  192. while ((q = strchr(p, 0x1b)) != NULL)
  193. { // find the escape character
  194. if( 0==WriteConsole(handle, p, q-p, &written, 0) ) // write up to the escape
  195. WriteFile(handle, p, q-p, &written, 0);
  196. if( q[1]!='[' )
  197. { // write the escape char (whatever purpose it has)
  198. if(0==WriteConsole(handle, q, 1, &written, 0) )
  199. WriteFile(handle,q, 1, &written, 0);
  200. p=q+1; //and start searching again
  201. }
  202. else
  203. { // from here, we will skip the '\033['
  204. // we break at the first unprocessible position
  205. // assuming regular text is starting there
  206. uchar numbers[16], numpoint=0;
  207. CONSOLE_SCREEN_BUFFER_INFO info;
  208. // initialize
  209. GetConsoleScreenBufferInfo(handle, &info);
  210. memset(numbers,0,sizeof(numbers));
  211. // skip escape and bracket
  212. q=q+2;
  213. while(1)
  214. {
  215. if( isdigit((int)((unsigned char)*q)) )
  216. { // add number to number array, only accept 2digits, shift out the rest
  217. // so // \033[123456789m will become \033[89m
  218. numbers[numpoint] = (numbers[numpoint]<<4) | (*q-'0');
  219. ++q;
  220. // and next character
  221. continue;
  222. }
  223. else if( *q == ';' )
  224. { // delimiter
  225. if(numpoint<sizeof(numbers)/sizeof(*numbers))
  226. { // go to next array position
  227. numpoint++;
  228. }
  229. else
  230. { // array is full, so we 'forget' the first value
  231. memmove(numbers,numbers+1,sizeof(numbers)/sizeof(*numbers)-1);
  232. numbers[sizeof(numbers)/sizeof(*numbers)-1]=0;
  233. }
  234. ++q;
  235. // and next number
  236. continue;
  237. }
  238. else if( *q == 'm' )
  239. { // \033[#;...;#m - Set Graphics Rendition (SGR)
  240. uint i;
  241. for(i=0; i<= numpoint; ++i)
  242. {
  243. if( 0x00 == (0xF0 & numbers[i]) )
  244. { // upper nibble 0
  245. if( 0 == numbers[i] )
  246. { // reset
  247. info.wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  248. }
  249. else if( 1==numbers[i] )
  250. { // set foreground intensity
  251. info.wAttributes |= FOREGROUND_INTENSITY;
  252. }
  253. else if( 5==numbers[i] )
  254. { // set background intensity
  255. info.wAttributes |= BACKGROUND_INTENSITY;
  256. }
  257. else if( 7==numbers[i] )
  258. { // reverse colors (just xor them)
  259. info.wAttributes ^= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
  260. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
  261. }
  262. //case '2': // not existing
  263. //case '3': // blinking (not implemented)
  264. //case '4': // unterline (not implemented)
  265. //case '6': // not existing
  266. //case '8': // concealed (not implemented)
  267. //case '9': // not existing
  268. }
  269. else if( 0x20 == (0xF0 & numbers[i]) )
  270. { // off
  271. if( 1==numbers[i] )
  272. { // set foreground intensity off
  273. info.wAttributes &= ~FOREGROUND_INTENSITY;
  274. }
  275. else if( 5==numbers[i] )
  276. { // set background intensity off
  277. info.wAttributes &= ~BACKGROUND_INTENSITY;
  278. }
  279. else if( 7==numbers[i] )
  280. { // reverse colors (just xor them)
  281. info.wAttributes ^= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
  282. BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
  283. }
  284. }
  285. else if( 0x30 == (0xF0 & numbers[i]) )
  286. { // foreground
  287. uint num = numbers[i]&0x0F;
  288. if(num==9) info.wAttributes |= FOREGROUND_INTENSITY;
  289. if(num>7) num=7; // set white for 37, 38 and 39
  290. info.wAttributes &= ~(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
  291. if( (num & 0x01)>0 ) // lowest bit set = red
  292. info.wAttributes |= FOREGROUND_RED;
  293. if( (num & 0x02)>0 ) // second bit set = green
  294. info.wAttributes |= FOREGROUND_GREEN;
  295. if( (num & 0x04)>0 ) // third bit set = blue
  296. info.wAttributes |= FOREGROUND_BLUE;
  297. }
  298. else if( 0x40 == (0xF0 & numbers[i]) )
  299. { // background
  300. uint num = numbers[i]&0x0F;
  301. if(num==9) info.wAttributes |= BACKGROUND_INTENSITY;
  302. if(num>7) num=7; // set white for 47, 48 and 49
  303. info.wAttributes &= ~(BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE);
  304. if( (num & 0x01)>0 ) // lowest bit set = red
  305. info.wAttributes |= BACKGROUND_RED;
  306. if( (num & 0x02)>0 ) // second bit set = green
  307. info.wAttributes |= BACKGROUND_GREEN;
  308. if( (num & 0x04)>0 ) // third bit set = blue
  309. info.wAttributes |= BACKGROUND_BLUE;
  310. }
  311. }
  312. // set the attributes
  313. SetConsoleTextAttribute(handle, info.wAttributes);
  314. }
  315. else if( *q=='J' )
  316. { // \033[#J - Erase Display (ED)
  317. // \033[0J - Clears the screen from cursor to end of display. The cursor position is unchanged.
  318. // \033[1J - Clears the screen from start to cursor. The cursor position is unchanged.
  319. // \033[2J - Clears the screen and moves the cursor to the home position (line 1, column 1).
  320. uint num = (numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F);
  321. int cnt;
  322. COORD origin = {0,0};
  323. if(num==1)
  324. { // chars from start up to and including cursor
  325. cnt = info.dwSize.X * info.dwCursorPosition.Y + info.dwCursorPosition.X + 1;
  326. }
  327. else if(num==2)
  328. { // Number of chars on screen.
  329. cnt = info.dwSize.X * info.dwSize.Y;
  330. SetConsoleCursorPosition(handle, origin);
  331. }
  332. else// 0 and default
  333. { // number of chars from cursor to end
  334. origin = info.dwCursorPosition;
  335. cnt = info.dwSize.X * (info.dwSize.Y - info.dwCursorPosition.Y) - info.dwCursorPosition.X;
  336. }
  337. FillConsoleOutputAttribute(handle,info.wAttributes,cnt,origin,NULL);
  338. FillConsoleOutputCharacter(handle,' ', cnt,origin,NULL);
  339. }
  340. else if( *q=='K' )
  341. { // \033[K : clear line from actual position to end of the line
  342. // \033[0K - Clears all characters from the cursor position to the end of the line.
  343. // \033[1K - Clears all characters from start of line to the cursor position.
  344. // \033[2K - Clears all characters of the whole line.
  345. uint num = (numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F);
  346. COORD origin = {0,info.dwCursorPosition.Y};
  347. SHORT cnt;
  348. if(num==1)
  349. {
  350. cnt = info.dwCursorPosition.X + 1;
  351. }
  352. else if(num==2)
  353. {
  354. cnt = info.dwSize.X;
  355. }
  356. else// 0 and default
  357. {
  358. origin = info.dwCursorPosition;
  359. cnt = info.dwSize.X - info.dwCursorPosition.X; // how many spaces until line is full
  360. }
  361. FillConsoleOutputAttribute(handle, info.wAttributes, cnt, origin, NULL);
  362. FillConsoleOutputCharacter(handle, ' ', cnt, origin, NULL);
  363. }
  364. else if( *q == 'H' || *q == 'f' )
  365. { // \033[#;#H - Cursor Position (CUP)
  366. // \033[#;#f - Horizontal & Vertical Position
  367. // The first # specifies the line number, the second # specifies the column.
  368. // The default for both is 1
  369. info.dwCursorPosition.X = (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F-1):0;
  370. info.dwCursorPosition.Y = (numpoint && numbers[numpoint-1])?(numbers[numpoint-1]>>4)*10+(numbers[numpoint-1]&0x0F-1):0;
  371. if( info.dwCursorPosition.X >= info.dwSize.X ) info.dwCursorPosition.Y = info.dwSize.X-1;
  372. if( info.dwCursorPosition.Y >= info.dwSize.Y ) info.dwCursorPosition.Y = info.dwSize.Y-1;
  373. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  374. }
  375. else if( *q=='s' )
  376. { // \033[s - Save Cursor Position (SCP)
  377. /* XXX Two streams are being used. Disabled to avoid inconsistency [flaviojs]
  378. CONSOLE_SCREEN_BUFFER_INFO info;
  379. GetConsoleScreenBufferInfo(handle, &info);
  380. saveposition = info.dwCursorPosition;
  381. */
  382. }
  383. else if( *q=='u' )
  384. { // \033[u - Restore cursor position (RCP)
  385. /* XXX Two streams are being used. Disabled to avoid inconsistency [flaviojs]
  386. SetConsoleCursorPosition(handle, saveposition);
  387. */
  388. }
  389. else if( *q == 'A' )
  390. { // \033[#A - Cursor Up (CUU)
  391. // Moves the cursor UP # number of lines
  392. info.dwCursorPosition.Y -= (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  393. if( info.dwCursorPosition.Y < 0 )
  394. info.dwCursorPosition.Y = 0;
  395. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  396. }
  397. else if( *q == 'B' )
  398. { // \033[#B - Cursor Down (CUD)
  399. // Moves the cursor DOWN # number of lines
  400. info.dwCursorPosition.Y += (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  401. if( info.dwCursorPosition.Y >= info.dwSize.Y )
  402. info.dwCursorPosition.Y = info.dwSize.Y-1;
  403. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  404. }
  405. else if( *q == 'C' )
  406. { // \033[#C - Cursor Forward (CUF)
  407. // Moves the cursor RIGHT # number of columns
  408. info.dwCursorPosition.X += (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  409. if( info.dwCursorPosition.X >= info.dwSize.X )
  410. info.dwCursorPosition.X = info.dwSize.X-1;
  411. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  412. }
  413. else if( *q == 'D' )
  414. { // \033[#D - Cursor Backward (CUB)
  415. // Moves the cursor LEFT # number of columns
  416. info.dwCursorPosition.X -= (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  417. if( info.dwCursorPosition.X < 0 )
  418. info.dwCursorPosition.X = 0;
  419. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  420. }
  421. else if( *q == 'E' )
  422. { // \033[#E - Cursor Next Line (CNL)
  423. // Moves the cursor down the indicated # of rows, to column 1
  424. info.dwCursorPosition.Y += (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  425. info.dwCursorPosition.X = 0;
  426. if( info.dwCursorPosition.Y >= info.dwSize.Y )
  427. info.dwCursorPosition.Y = info.dwSize.Y-1;
  428. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  429. }
  430. else if( *q == 'F' )
  431. { // \033[#F - Cursor Preceding Line (CPL)
  432. // Moves the cursor up the indicated # of rows, to column 1.
  433. info.dwCursorPosition.Y -= (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F):1;
  434. info.dwCursorPosition.X = 0;
  435. if( info.dwCursorPosition.Y < 0 )
  436. info.dwCursorPosition.Y = 0;
  437. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  438. }
  439. else if( *q == 'G' )
  440. { // \033[#G - Cursor Horizontal Absolute (CHA)
  441. // Moves the cursor to indicated column in current row.
  442. info.dwCursorPosition.X = (numbers[numpoint])?(numbers[numpoint]>>4)*10+(numbers[numpoint]&0x0F-1):0;
  443. if( info.dwCursorPosition.X >= info.dwSize.X )
  444. info.dwCursorPosition.X = info.dwSize.X-1;
  445. SetConsoleCursorPosition(handle, info.dwCursorPosition);
  446. }
  447. else if( *q == 'L' || *q == 'M' || *q == '@' || *q == 'P')
  448. { // not implemented, just skip
  449. }
  450. else
  451. { // no number nor valid sequencer
  452. // something is fishy, we break and give the current char free
  453. --q;
  454. }
  455. // skip the sequencer and search again
  456. p = q+1;
  457. break;
  458. }// end while
  459. }
  460. }
  461. if (*p) // write the rest of the buffer
  462. if( 0==WriteConsole(handle, p, strlen(p), &written, 0) )
  463. WriteFile(handle,p, strlen(p), &written, 0);
  464. FREEBUF(tempbuf);
  465. return 0;
  466. }
  467. int FPRINTF(HANDLE handle, const char *fmt, ...)
  468. {
  469. int ret;
  470. va_list argptr;
  471. va_start(argptr, fmt);
  472. ret = VFPRINTF(handle,fmt,argptr);
  473. va_end(argptr);
  474. return ret;
  475. }
  476. #define FFLUSH(handle)
  477. #define STDOUT GetStdHandle(STD_OUTPUT_HANDLE)
  478. #define STDERR GetStdHandle(STD_ERROR_HANDLE)
  479. #else // not _WIN32
  480. //#define VPRINTF vprintf
  481. //#define PRINTF printf
  482. #define is_console(file) (0!=isatty(fileno(file)))
  483. //vprintf_without_ansiformats
  484. int VFPRINTF(FILE *file, const char *fmt, va_list argptr)
  485. {
  486. char *p, *q;
  487. NEWBUF(tempbuf); // temporary buffer
  488. if(!fmt || !*fmt)
  489. return 0;
  490. if( is_console(file) || stdout_with_ansisequence )
  491. {
  492. vfprintf(file, fmt, argptr);
  493. return 0;
  494. }
  495. // Print everything to the buffer
  496. BUFVPRINTF(tempbuf,fmt,argptr);
  497. // start with processing
  498. p = BUFVAL(tempbuf);
  499. while ((q = strchr(p, 0x1b)) != NULL)
  500. { // find the escape character
  501. fprintf(file, "%.*s", (int)(q-p), p); // write up to the escape
  502. if( q[1]!='[' )
  503. { // write the escape char (whatever purpose it has)
  504. fprintf(file, "%.*s", 1, q);
  505. p=q+1; //and start searching again
  506. }
  507. else
  508. { // from here, we will skip the '\033['
  509. // we break at the first unprocessible position
  510. // assuming regular text is starting there
  511. // skip escape and bracket
  512. q=q+2;
  513. while(1)
  514. {
  515. if( isdigit((int)((unsigned char)*q)) )
  516. {
  517. ++q;
  518. // and next character
  519. continue;
  520. }
  521. else if( *q == ';' )
  522. { // delimiter
  523. ++q;
  524. // and next number
  525. continue;
  526. }
  527. else if( *q == 'm' )
  528. { // \033[#;...;#m - Set Graphics Rendition (SGR)
  529. // set the attributes
  530. }
  531. else if( *q=='J' )
  532. { // \033[#J - Erase Display (ED)
  533. }
  534. else if( *q=='K' )
  535. { // \033[K : clear line from actual position to end of the line
  536. }
  537. else if( *q == 'H' || *q == 'f' )
  538. { // \033[#;#H - Cursor Position (CUP)
  539. // \033[#;#f - Horizontal & Vertical Position
  540. }
  541. else if( *q=='s' )
  542. { // \033[s - Save Cursor Position (SCP)
  543. }
  544. else if( *q=='u' )
  545. { // \033[u - Restore cursor position (RCP)
  546. }
  547. else if( *q == 'A' )
  548. { // \033[#A - Cursor Up (CUU)
  549. // Moves the cursor UP # number of lines
  550. }
  551. else if( *q == 'B' )
  552. { // \033[#B - Cursor Down (CUD)
  553. // Moves the cursor DOWN # number of lines
  554. }
  555. else if( *q == 'C' )
  556. { // \033[#C - Cursor Forward (CUF)
  557. // Moves the cursor RIGHT # number of columns
  558. }
  559. else if( *q == 'D' )
  560. { // \033[#D - Cursor Backward (CUB)
  561. // Moves the cursor LEFT # number of columns
  562. }
  563. else if( *q == 'E' )
  564. { // \033[#E - Cursor Next Line (CNL)
  565. // Moves the cursor down the indicated # of rows, to column 1
  566. }
  567. else if( *q == 'F' )
  568. { // \033[#F - Cursor Preceding Line (CPL)
  569. // Moves the cursor up the indicated # of rows, to column 1.
  570. }
  571. else if( *q == 'G' )
  572. { // \033[#G - Cursor Horizontal Absolute (CHA)
  573. // Moves the cursor to indicated column in current row.
  574. }
  575. else if( *q == 'L' || *q == 'M' || *q == '@' || *q == 'P')
  576. { // not implemented, just skip
  577. }
  578. else
  579. { // no number nor valid sequencer
  580. // something is fishy, we break and give the current char free
  581. --q;
  582. }
  583. // skip the sequencer and search again
  584. p = q+1;
  585. break;
  586. }// end while
  587. }
  588. }
  589. if (*p) // write the rest of the buffer
  590. fprintf(file, "%s", p);
  591. FREEBUF(tempbuf);
  592. return 0;
  593. }
  594. int FPRINTF(FILE *file, const char *fmt, ...)
  595. {
  596. int ret;
  597. va_list argptr;
  598. va_start(argptr, fmt);
  599. ret = VFPRINTF(file,fmt,argptr);
  600. va_end(argptr);
  601. return ret;
  602. }
  603. #define FFLUSH fflush
  604. #define STDOUT stdout
  605. #define STDERR stderr
  606. #endif// not _WIN32
  607. char timestamp_format[20] = ""; //For displaying Timestamps
  608. // by MC Cameri
  609. int _vShowMessage(enum msg_type flag, const char *string, va_list ap)
  610. {
  611. // _ShowMessage MUST be used instead of printf as of 10/24/2004.
  612. // Return: 0 = Successful, 1 = Failed.
  613. // int ret = 0;
  614. char prefix[100];
  615. #if defined(DEBUGLOGMAP) || defined(DEBUGLOGCHAR) || defined(DEBUGLOGLOGIN)
  616. FILE *fp;
  617. #endif
  618. if (!string || *string == '\0') {
  619. ShowError("Empty string passed to _vShowMessage().\n");
  620. return 1;
  621. }
  622. if ((flag == MSG_DEBUG && !SHOW_DEBUG_MSG) ||
  623. (flag == MSG_INFORMATION && msg_silent&1) ||
  624. (flag == MSG_STATUS && msg_silent&2) ||
  625. (flag == MSG_NOTICE && msg_silent&4) ||
  626. (flag == MSG_WARNING && msg_silent&8) ||
  627. (flag == MSG_ERROR && msg_silent&16) ||
  628. (flag == MSG_SQL && msg_silent&16))
  629. return 0; //Do not print it.
  630. if (timestamp_format[0])
  631. { //Display time format. [Skotlex]
  632. time_t t = time(NULL);
  633. strftime(prefix, 80, timestamp_format, localtime(&t));
  634. } else prefix[0]='\0';
  635. switch (flag) {
  636. case MSG_NONE: // direct printf replacement
  637. break;
  638. case MSG_STATUS: //Bright Green (To inform about good things)
  639. strcat(prefix,CL_GREEN"[Status]"CL_RESET":");
  640. break;
  641. case MSG_SQL: //Bright Violet (For dumping out anything related with SQL) <- Actually, this is mostly used for SQL errors with the database, as successes can as well just be anything else... [Skotlex]
  642. strcat(prefix,CL_MAGENTA"[SQL]"CL_RESET":");
  643. break;
  644. case MSG_INFORMATION: //Bright White (Variable information)
  645. strcat(prefix,CL_WHITE"[Info]"CL_RESET":");
  646. break;
  647. case MSG_NOTICE: //Bright White (Less than a warning)
  648. strcat(prefix,CL_WHITE"[Notice]"CL_RESET":");
  649. break;
  650. case MSG_WARNING: //Bright Yellow
  651. strcat(prefix,CL_YELLOW"[Warning]"CL_RESET":");
  652. break;
  653. case MSG_DEBUG: //Bright Cyan, important stuff!
  654. strcat(prefix,CL_CYAN"[Debug]"CL_RESET":");
  655. break;
  656. case MSG_ERROR: //Bright Red (Regular errors)
  657. strcat(prefix,CL_RED"[Error]"CL_RESET":");
  658. break;
  659. case MSG_FATALERROR: //Bright Red (Fatal errors, abort(); if possible)
  660. strcat(prefix,CL_RED"[Fatal Error]"CL_RESET":");
  661. break;
  662. default:
  663. ShowError("In function _vShowMessage() -> Invalid flag passed.\n");
  664. return 1;
  665. }
  666. if (flag == MSG_ERROR || flag == MSG_FATALERROR || flag == MSG_SQL)
  667. { //Send Errors to StdErr [Skotlex]
  668. FPRINTF(STDERR, "%s ", prefix);
  669. VFPRINTF(STDERR, string, ap);
  670. FFLUSH(STDERR);
  671. } else {
  672. if (flag != MSG_NONE)
  673. FPRINTF(STDOUT, "%s ", prefix);
  674. VFPRINTF(STDOUT, string, ap);
  675. FFLUSH(STDOUT);
  676. }
  677. #if defined(DEBUGLOGMAP) || defined(DEBUGLOGCHAR) || defined(DEBUGLOGLOGIN)
  678. if(strlen(DEBUGLOGPATH) > 0) {
  679. fp=fopen(DEBUGLOGPATH,"a");
  680. if (fp == NULL) {
  681. FPRINTF(STDERR, CL_RED"[ERROR]"CL_RESET": Could not open '"CL_WHITE"%s"CL_RESET"', access denied.\n", DEBUGLOGPATH);
  682. FFLUSH(STDERR);
  683. } else {
  684. fprintf(fp,"%s ", prefix);
  685. vfprintf(fp,string,ap);
  686. fclose(fp);
  687. }
  688. } else {
  689. FPRINTF(STDERR, CL_RED"[ERROR]"CL_RESET": DEBUGLOGPATH not defined!\n");
  690. FFLUSH(STDERR);
  691. }
  692. #endif
  693. va_end(ap);
  694. return 0;
  695. }
  696. void ClearScreen(void)
  697. {
  698. #ifndef _WIN32
  699. ShowMessage(CL_CLS); // to prevent empty string passed messages
  700. #endif
  701. }
  702. int _ShowMessage(enum msg_type flag, const char *string, ...)
  703. {
  704. int ret;
  705. va_list ap;
  706. va_start(ap, string);
  707. ret = _vShowMessage(flag, string, ap);
  708. va_end(ap);
  709. return ret;
  710. }
  711. // direct printf replacement
  712. int ShowMessage(const char *string, ...) {
  713. int ret;
  714. va_list ap;
  715. va_start(ap, string);
  716. ret = _vShowMessage(MSG_NONE, string, ap);
  717. va_end(ap);
  718. return ret;
  719. }
  720. int ShowStatus(const char *string, ...) {
  721. int ret;
  722. va_list ap;
  723. va_start(ap, string);
  724. ret = _vShowMessage(MSG_STATUS, string, ap);
  725. va_end(ap);
  726. return ret;
  727. }
  728. int ShowSQL(const char *string, ...) {
  729. int ret;
  730. va_list ap;
  731. va_start(ap, string);
  732. ret = _vShowMessage(MSG_SQL, string, ap);
  733. va_end(ap);
  734. return ret;
  735. }
  736. int ShowInfo(const char *string, ...) {
  737. int ret;
  738. va_list ap;
  739. va_start(ap, string);
  740. ret = _vShowMessage(MSG_INFORMATION, string, ap);
  741. va_end(ap);
  742. return ret;
  743. }
  744. int ShowNotice(const char *string, ...) {
  745. int ret;
  746. va_list ap;
  747. va_start(ap, string);
  748. ret = _vShowMessage(MSG_NOTICE, string, ap);
  749. va_end(ap);
  750. return ret;
  751. }
  752. int ShowWarning(const char *string, ...) {
  753. int ret;
  754. va_list ap;
  755. va_start(ap, string);
  756. ret = _vShowMessage(MSG_WARNING, string, ap);
  757. va_end(ap);
  758. return ret;
  759. }
  760. int ShowDebug(const char *string, ...) {
  761. int ret;
  762. va_list ap;
  763. va_start(ap, string);
  764. ret = _vShowMessage(MSG_DEBUG, string, ap);
  765. va_end(ap);
  766. return ret;
  767. }
  768. int ShowError(const char *string, ...) {
  769. int ret;
  770. va_list ap;
  771. va_start(ap, string);
  772. ret = _vShowMessage(MSG_ERROR, string, ap);
  773. va_end(ap);
  774. return ret;
  775. }
  776. int ShowFatalError(const char *string, ...) {
  777. int ret;
  778. va_list ap;
  779. va_start(ap, string);
  780. ret = _vShowMessage(MSG_FATALERROR, string, ap);
  781. va_end(ap);
  782. return ret;
  783. }