]> icculus.org git repositories - divverent/darkplaces.git/blob - sys_shared.c
fix a signed/unsigned comparison warning
[divverent/darkplaces.git] / sys_shared.c
1
2 #include "quakedef.h"
3 # include <time.h>
4 #ifndef WIN32
5 # include <unistd.h>
6 # include <fcntl.h>
7 # include <dlfcn.h>
8 #endif
9
10 extern cvar_t   timestamps;
11 extern cvar_t   timeformat;
12
13 static int sys_nostdout = false;
14
15 /* The translation table between the graphical font and plain ASCII  --KB */
16 static char qfont_table[256] = {
17         '\0', '#',  '#',  '#',  '#',  '.',  '#',  '#',
18         '#',  9,    10,   '#',  ' ',  13,   '.',  '.',
19         '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
20         '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
21         ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
22         '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
23         '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
24         '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
25         '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
26         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
27         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
28         'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
29         '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
30         'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
31         'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
32         'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<',
33
34         '<',  '=',  '>',  '#',  '#',  '.',  '#',  '#',
35         '#',  '#',  ' ',  '#',  ' ',  '>',  '.',  '.',
36         '[',  ']',  '0',  '1',  '2',  '3',  '4',  '5',
37         '6',  '7',  '8',  '9',  '.',  '<',  '=',  '>',
38         ' ',  '!',  '"',  '#',  '$',  '%',  '&',  '\'',
39         '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
40         '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
41         '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
42         '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
43         'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
44         'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
45         'X',  'Y',  'Z',  '[',  '\\', ']',  '^',  '_',
46         '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
47         'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
48         'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
49         'x',  'y',  'z',  '{',  '|',  '}',  '~',  '<'
50 };
51
52 static char sys_timestring[128];
53 char *Sys_TimeString(const char *timeformat)
54 {
55         time_t mytime = time(NULL);
56         strftime(sys_timestring, sizeof(sys_timestring), timeformat, localtime(&mytime));
57         return sys_timestring;
58 }
59
60
61 #define MAXPRINTMSG 16384
62
63 void Sys_Print(const char *msg)
64 {
65         unsigned char *p;
66         // String we print
67         char final[MAXPRINTMSG];
68
69         if (sys_nostdout)
70                 return;
71
72         if (timestamps.integer)
73                 snprintf(final, sizeof(final), "%s%s", Sys_TimeString(timeformat.string), msg);
74         else
75                 strlcpy (final, msg, sizeof (final));
76
77         // LordHavoc: make sure the string is terminated
78         final[MAXPRINTMSG-1] = 0;
79         for (p = (unsigned char *) final;*p; p++)
80                 *p = qfont_table[*p];
81         Sys_PrintToTerminal(final);
82 }
83
84 void Sys_Printf(const char *fmt, ...)
85 {
86         va_list argptr;
87         char msg[MAXPRINTMSG];  // String we started with
88
89         va_start(argptr,fmt);
90         vsnprintf(msg,sizeof(msg),fmt,argptr);
91         va_end(argptr);
92
93         Sys_Print(msg);
94 }
95
96 extern qboolean host_shuttingdown;
97 void Sys_Quit (void)
98 {
99         host_shuttingdown = true;
100         Host_Shutdown();
101         exit(0);
102 }
103
104 char engineversion[128];
105
106 void Sys_Shared_EarlyInit(void)
107 {
108         const char* os;
109
110         Memory_Init ();
111
112         COM_InitArgv();
113         COM_InitGameType();
114
115 #if defined(__linux__)
116         os = "Linux";
117 #elif defined(WIN32)
118         os = "Windows";
119 #elif defined(__FreeBSD__)
120         os = "FreeBSD";
121 #elif defined(__NetBSD__)
122         os = "NetBSD";
123 #elif defined(__OpenBSD__)
124         os = "OpenBSD";
125 #else
126         os = "Unknown";
127 #endif
128         snprintf (engineversion, sizeof (engineversion), "%s %s %s", gamename, os, buildstring);
129
130 // COMMANDLINEOPTION: Console: -nostdout disables text output to the terminal the game was launched from
131         if (COM_CheckParm("-nostdout"))
132                 sys_nostdout = 1;
133         else
134                 Con_Printf("%s\n", engineversion);
135 }
136
137 void Sys_Shared_LateInit(void)
138 {
139 }
140
141 /*
142 ===============================================================================
143
144 DLL MANAGEMENT
145
146 ===============================================================================
147 */
148
149 qboolean Sys_LoadLibrary (const char* dllname, dllhandle_t* handle, const dllfunction_t *fcts)
150 {
151         const dllfunction_t *func;
152         dllhandle_t dllhandle;
153
154         if (handle == NULL)
155                 return false;
156
157         // Initializations
158         for (func = fcts; func && func->name != NULL; func++)
159                 *func->funcvariable = NULL;
160
161         // Load the DLL
162 #ifdef WIN32
163         dllhandle = LoadLibrary (dllname);
164 #else
165         dllhandle = dlopen (dllname, RTLD_LAZY);
166 #endif
167         if (! dllhandle)
168         {
169                 Con_Printf ("Can't load \"%s\".\n", dllname);
170                 return false;
171         }
172
173         // Get the function adresses
174         for (func = fcts; func && func->name != NULL; func++)
175                 if (!(*func->funcvariable = (void *) Sys_GetProcAddress (dllhandle, func->name)))
176                 {
177                         Con_Printf ("Missing function \"%s\" - broken library!\n", func->name);
178                         Sys_UnloadLibrary (&dllhandle);
179                         return false;
180                 }
181
182         *handle = dllhandle;
183         Con_DPrintf("\"%s\" loaded.\n", dllname);
184         return true;
185 }
186
187 void Sys_UnloadLibrary (dllhandle_t* handle)
188 {
189         if (handle == NULL || *handle == NULL)
190                 return;
191
192 #ifdef WIN32
193         FreeLibrary (*handle);
194 #else
195         dlclose (*handle);
196 #endif
197
198         *handle = NULL;
199 }
200
201 void* Sys_GetProcAddress (dllhandle_t handle, const char* name)
202 {
203 #ifdef WIN32
204         return (void *)GetProcAddress (handle, name);
205 #else
206         return (void *)dlsym (handle, name);
207 #endif
208 }
209