sys_linux.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <unistd.h>
  16. #include <signal.h>
  17. #include <stdlib.h>
  18. #include <limits.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <stdarg.h>
  24. #include <stdio.h>
  25. #include <sys/ipc.h>
  26. #include <sys/shm.h>
  27. #include <sys/stat.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <sys/wait.h>
  31. #include <sys/mman.h>
  32. #include <errno.h>
  33. #include "quakedef.h"
  34. int noconinput = 0;
  35. int nostdout = 0;
  36. char *basedir = ".";
  37. char *cachedir = "/tmp";
  38. cvar_t sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
  39. // =======================================================================
  40. // General routines
  41. // =======================================================================
  42. void Sys_DebugNumber(int y, int val)
  43. {
  44. }
  45. /*
  46. void Sys_Printf (char *fmt, ...)
  47. {
  48. va_list argptr;
  49. char text[1024];
  50. va_start (argptr,fmt);
  51. vsprintf (text,fmt,argptr);
  52. va_end (argptr);
  53. fprintf(stderr, "%s", text);
  54. Con_Print (text);
  55. }
  56. void Sys_Printf (char *fmt, ...)
  57. {
  58. va_list argptr;
  59. char text[1024], *t_p;
  60. int l, r;
  61. if (nostdout)
  62. return;
  63. va_start (argptr,fmt);
  64. vsprintf (text,fmt,argptr);
  65. va_end (argptr);
  66. l = strlen(text);
  67. t_p = text;
  68. // make sure everything goes through, even though we are non-blocking
  69. while (l)
  70. {
  71. r = write (1, text, l);
  72. if (r != l)
  73. sleep (0);
  74. if (r > 0)
  75. {
  76. t_p += r;
  77. l -= r;
  78. }
  79. }
  80. }
  81. */
  82. void Sys_Printf (char *fmt, ...)
  83. {
  84. va_list argptr;
  85. char text[2048];
  86. unsigned char *p;
  87. va_start (argptr,fmt);
  88. vsprintf (text,fmt,argptr);
  89. va_end (argptr);
  90. if (strlen(text) > sizeof(text))
  91. Sys_Error("memory overwrite in Sys_Printf");
  92. if (nostdout)
  93. return;
  94. for (p = (unsigned char *)text; *p; p++)
  95. if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
  96. printf("[%02x]", *p);
  97. else
  98. putc(*p, stdout);
  99. }
  100. void Sys_Quit (void)
  101. {
  102. Host_Shutdown();
  103. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  104. exit(0);
  105. }
  106. void Sys_Init(void)
  107. {
  108. #if id386
  109. Sys_SetFPCW();
  110. #endif
  111. }
  112. void Sys_Error (char *error, ...)
  113. {
  114. va_list argptr;
  115. char string[1024];
  116. // change stdin to non blocking
  117. fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
  118. va_start (argptr,error);
  119. vsprintf (string,error,argptr);
  120. va_end (argptr);
  121. fprintf(stderr, "Error: %s\n", string);
  122. Host_Shutdown ();
  123. exit (1);
  124. }
  125. void Sys_Warn (char *warning, ...)
  126. {
  127. va_list argptr;
  128. char string[1024];
  129. va_start (argptr,warning);
  130. vsprintf (string,warning,argptr);
  131. va_end (argptr);
  132. fprintf(stderr, "Warning: %s", string);
  133. }
  134. /*
  135. ============
  136. Sys_FileTime
  137. returns -1 if not present
  138. ============
  139. */
  140. int Sys_FileTime (char *path)
  141. {
  142. struct stat buf;
  143. if (stat (path,&buf) == -1)
  144. return -1;
  145. return buf.st_mtime;
  146. }
  147. void Sys_mkdir (char *path)
  148. {
  149. mkdir (path, 0777);
  150. }
  151. int Sys_FileOpenRead (char *path, int *handle)
  152. {
  153. int h;
  154. struct stat fileinfo;
  155. h = open (path, O_RDONLY, 0666);
  156. *handle = h;
  157. if (h == -1)
  158. return -1;
  159. if (fstat (h,&fileinfo) == -1)
  160. Sys_Error ("Error fstating %s", path);
  161. return fileinfo.st_size;
  162. }
  163. int Sys_FileOpenWrite (char *path)
  164. {
  165. int handle;
  166. umask (0);
  167. handle = open(path,O_RDWR | O_CREAT | O_TRUNC
  168. , 0666);
  169. if (handle == -1)
  170. Sys_Error ("Error opening %s: %s", path,strerror(errno));
  171. return handle;
  172. }
  173. int Sys_FileWrite (int handle, void *src, int count)
  174. {
  175. return write (handle, src, count);
  176. }
  177. void Sys_FileClose (int handle)
  178. {
  179. close (handle);
  180. }
  181. void Sys_FileSeek (int handle, int position)
  182. {
  183. lseek (handle, position, SEEK_SET);
  184. }
  185. int Sys_FileRead (int handle, void *dest, int count)
  186. {
  187. return read (handle, dest, count);
  188. }
  189. void Sys_DebugLog(char *file, char *fmt, ...)
  190. {
  191. va_list argptr;
  192. static char data[1024];
  193. int fd;
  194. va_start(argptr, fmt);
  195. vsprintf(data, fmt, argptr);
  196. va_end(argptr);
  197. // fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
  198. fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
  199. write(fd, data, strlen(data));
  200. close(fd);
  201. }
  202. void Sys_EditFile(char *filename)
  203. {
  204. char cmd[256];
  205. char *term;
  206. char *editor;
  207. term = getenv("TERM");
  208. if (term && !strcmp(term, "xterm"))
  209. {
  210. editor = getenv("VISUAL");
  211. if (!editor)
  212. editor = getenv("EDITOR");
  213. if (!editor)
  214. editor = getenv("EDIT");
  215. if (!editor)
  216. editor = "vi";
  217. sprintf(cmd, "xterm -e %s %s", editor, filename);
  218. system(cmd);
  219. }
  220. }
  221. double Sys_DoubleTime (void)
  222. {
  223. struct timeval tp;
  224. struct timezone tzp;
  225. static int secbase;
  226. gettimeofday(&tp, &tzp);
  227. if (!secbase)
  228. {
  229. secbase = tp.tv_sec;
  230. return tp.tv_usec/1000000.0;
  231. }
  232. return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
  233. }
  234. // =======================================================================
  235. // Sleeps for microseconds
  236. // =======================================================================
  237. static volatile int oktogo;
  238. void alarm_handler(int x)
  239. {
  240. oktogo=1;
  241. }
  242. void Sys_LineRefresh(void)
  243. {
  244. }
  245. void floating_point_exception_handler(int whatever)
  246. {
  247. // Sys_Warn("floating point exception\n");
  248. signal(SIGFPE, floating_point_exception_handler);
  249. }
  250. char *Sys_ConsoleInput(void)
  251. {
  252. #if 0
  253. static char text[256];
  254. int len;
  255. if (cls.state == ca_dedicated) {
  256. len = read (0, text, sizeof(text));
  257. if (len < 1)
  258. return NULL;
  259. text[len-1] = 0; // rip off the /n and terminate
  260. return text;
  261. }
  262. #endif
  263. return NULL;
  264. }
  265. #if !id386
  266. void Sys_HighFPPrecision (void)
  267. {
  268. }
  269. void Sys_LowFPPrecision (void)
  270. {
  271. }
  272. #endif
  273. int skipframes;
  274. int main (int c, char **v)
  275. {
  276. double time, oldtime, newtime;
  277. quakeparms_t parms;
  278. int j;
  279. // static char cwd[1024];
  280. // signal(SIGFPE, floating_point_exception_handler);
  281. signal(SIGFPE, SIG_IGN);
  282. memset(&parms, 0, sizeof(parms));
  283. COM_InitArgv(c, v);
  284. parms.argc = com_argc;
  285. parms.argv = com_argv;
  286. parms.memsize = 16*1024*1024;
  287. j = COM_CheckParm("-mem");
  288. if (j)
  289. parms.memsize = (int) (Q_atof(com_argv[j+1]) * 1024 * 1024);
  290. parms.membase = malloc (parms.memsize);
  291. parms.basedir = basedir;
  292. // caching is disabled by default, use -cachedir to enable
  293. // parms.cachedir = cachedir;
  294. noconinput = COM_CheckParm("-noconinput");
  295. if (!noconinput)
  296. fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
  297. if (COM_CheckParm("-nostdout"))
  298. nostdout = 1;
  299. Sys_Init();
  300. Host_Init(&parms);
  301. oldtime = Sys_DoubleTime ();
  302. while (1)
  303. {
  304. // find time spent rendering last frame
  305. newtime = Sys_DoubleTime ();
  306. time = newtime - oldtime;
  307. Host_Frame(time);
  308. oldtime = newtime;
  309. }
  310. }
  311. /*
  312. ================
  313. Sys_MakeCodeWriteable
  314. ================
  315. */
  316. void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
  317. {
  318. int r;
  319. unsigned long addr;
  320. int psize = getpagesize();
  321. addr = (startaddr & ~(psize-1)) - psize;
  322. // fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
  323. // addr, startaddr+length, length);
  324. r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
  325. if (r < 0)
  326. Sys_Error("Protection change failed\n");
  327. }