core.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // $Id: core.c,v 1.1.1.1 2004/09/10 17:44:49 MagicalTux Exp $
  2. // original : core.c 2003/02/26 18:03:12 Rev 1.7
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #ifndef _WIN32
  6. #include <unistd.h>
  7. #endif
  8. #include <signal.h>
  9. #include <string.h>
  10. #include "../common/mmo.h"
  11. #include "core.h"
  12. #include "socket.h"
  13. #include "timer.h"
  14. #include "version.h"
  15. #include "showmsg.h"
  16. #ifdef MEMWATCH
  17. #include "memwatch.h"
  18. #endif
  19. static void (*term_func)(void)=NULL;
  20. /*======================================
  21. * CORE : Set function
  22. *--------------------------------------
  23. */
  24. void set_termfunc(void (*termfunc)(void))
  25. {
  26. term_func = termfunc;
  27. }
  28. /*======================================
  29. * CORE : Signal Sub Function
  30. *--------------------------------------
  31. */
  32. static void sig_proc(int sn)
  33. {
  34. int i;
  35. switch(sn){
  36. case SIGINT:
  37. case SIGTERM:
  38. if(term_func)
  39. term_func();
  40. for(i=0;i<fd_max;i++){
  41. if(!session[i])
  42. continue;
  43. close(i);
  44. }
  45. exit(0);
  46. break;
  47. }
  48. }
  49. int get_svn_revision(char *svnentry) { // Warning: minor syntax checking
  50. char line[1024];
  51. int rev = 0;
  52. FILE *fp;
  53. if ((fp = fopen(svnentry, "r")) == NULL) {
  54. return 0;
  55. } else {
  56. while (fgets(line,1023,fp)) if (strstr(line,"revision=")) break;
  57. fclose(fp);
  58. if (sscanf(line," %*[^\"]\"%d%*[^\n]",&rev)==1)
  59. return rev;
  60. else
  61. return 0;
  62. }
  63. // return 0;
  64. }
  65. /*======================================
  66. * CORE : Display title
  67. *--------------------------------------
  68. */
  69. static void display_title(void)
  70. {
  71. int revision;
  72. // for help with the console colors look here:
  73. // http://www.edoceo.com/liberum/?doc=printf-with-color
  74. // some code explanation (used here):
  75. // \033[2J : clear screen and go up/left (0, 0 position)
  76. // \033[K : clear line from actual position to end of the line
  77. // \033[0m : reset color parameter
  78. // \033[1m : use bold for font
  79. printf("\033[2J"); // clear screen and go up/left (0, 0 position in text)
  80. printf("\033[37;44m (=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)\033[K\033[0m\n"); // white writing (37) on blue background (44), \033[K clean until end of file
  81. printf("\033[0;44m (\033[1;33m (c)2004 eAthena Development Team presents \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  82. printf("\033[0;44m (\033[1m ______ __ __ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  83. printf("\033[0;44m (\033[1m /\\ _ \\/\\ \\__/\\ \\ v%2d.%02d.%02d \033[0;44m)\033[K\033[0m\n", ATHENA_MAJOR_VERSION, ATHENA_MINOR_VERSION, ATHENA_REVISION); // 1: bold char, 0: normal char
  84. printf("\033[0;44m (\033[1m __\\ \\ \\_\\ \\ \\ ,_\\ \\ \\___ __ ___ __ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  85. printf("\033[0;44m (\033[1m /'__`\\ \\ __ \\ \\ \\/\\ \\ _ `\\ /'__`\\/' _ `\\ /'__`\\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  86. printf("\033[0;44m (\033[1m /\\ __/\\ \\ \\/\\ \\ \\ \\_\\ \\ \\ \\ \\/\\ __//\\ \\/\\ \\/\\ \\_\\.\\_ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  87. printf("\033[0;44m (\033[1m \\ \\____\\\\ \\_\\ \\_\\ \\__\\\\ \\_\\ \\_\\ \\____\\ \\_\\ \\_\\ \\__/.\\_\\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  88. printf("\033[0;44m (\033[1m \\/____/ \\/_/\\/_/\\/__/ \\/_/\\/_/\\/____/\\/_/\\/_/\\/__/\\/_/ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  89. printf("\033[0;44m (\033[1m _ _ _ _ _ _ _ _ _ _ _ _ _ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  90. printf("\033[0;44m (\033[1m / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ / \\ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  91. printf("\033[0;44m (\033[1m ( e | n | g | l | i | s | h ) ( A | t | h | e | n | a ) \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  92. printf("\033[0;44m (\033[1m \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \\_/ \033[0;44m)\033[K\033[0m\n"); // 1: bold char, 0: normal char
  93. printf("\033[0;44m (\033[1m \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  94. printf("\033[0;44m (\033[1;33m Advanced Fusion Maps (c) 2003-2004 The Fusion Project \033[0;44m)\033[K\033[0m\n"); // yellow writing (33)
  95. printf("\033[37;44m (=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=)\033[K\033[0m\n\n"); // reset color
  96. if ((revision = get_svn_revision(".svn\\entries"))>0) {
  97. snprintf(tmp_output,sizeof(tmp_output),"SVN Revision: '"CL_WHITE"%d"CL_RESET"'.\n",revision);
  98. ShowInfo(tmp_output);
  99. }
  100. }
  101. // Added by Gabuzomeu
  102. //
  103. // This is an implementation of signal() using sigaction() for portability.
  104. // (sigaction() is POSIX; signal() is not.) Taken from Stevens' _Advanced
  105. // Programming in the UNIX Environment_.
  106. //
  107. #ifndef SIGPIPE
  108. #define SIGPIPE SIGINT
  109. #endif
  110. #ifndef POSIX
  111. #define compat_signal(signo, func) signal(signo, func)
  112. #else
  113. sigfunc *compat_signal(int signo, sigfunc *func)
  114. {
  115. struct sigaction sact, oact;
  116. sact.sa_handler = func;
  117. sigemptyset(&sact.sa_mask);
  118. sact.sa_flags = 0;
  119. #ifdef SA_INTERRUPT
  120. sact.sa_flags |= SA_INTERRUPT; /* SunOS */
  121. #endif
  122. if (sigaction(signo, &sact, &oact) < 0)
  123. return (SIG_ERR);
  124. return (oact.sa_handler);
  125. }
  126. #endif
  127. /*======================================
  128. * CORE : MAINROUTINE
  129. *--------------------------------------
  130. */
  131. int runflag = 1;
  132. int main(int argc,char **argv)
  133. {
  134. int next;
  135. Net_Init();
  136. do_socket();
  137. compat_signal(SIGPIPE,SIG_IGN);
  138. compat_signal(SIGTERM,sig_proc);
  139. compat_signal(SIGINT,sig_proc);
  140. // Signal to create coredumps by system when necessary (crash)
  141. compat_signal(SIGSEGV, SIG_DFL);
  142. #ifndef _WIN32
  143. compat_signal(SIGBUS, SIG_DFL);
  144. compat_signal(SIGTRAP, SIG_DFL);
  145. #endif
  146. compat_signal(SIGILL, SIG_DFL);
  147. display_title();
  148. tick_ = time(0);
  149. do_init(argc,argv);
  150. while(runflag){
  151. next=do_timer(gettick_nocache());
  152. do_sendrecv(next);
  153. do_parsepacket();
  154. }
  155. return 0;
  156. }