]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sys_unix.c
cmd: Check for empty cbuf when inserting too
[xonotic/darkplaces.git] / sys_unix.c
1
2 #ifdef WIN32
3 #include <windows.h>
4 #include <mmsystem.h>
5 #include <io.h>
6 #include "conio.h"
7 #else
8 #include <sys/time.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #endif
12
13 #include <signal.h>
14
15 #include "darkplaces.h"
16
17 sys_t sys;
18
19 // =======================================================================
20 // General routines
21 // =======================================================================
22 void Sys_Shutdown (void)
23 {
24 #ifndef WIN32
25         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
26 #endif
27         fflush(stdout);
28 }
29
30 void Sys_Error (const char *error, ...)
31 {
32         va_list argptr;
33         char string[MAX_INPUTLINE];
34
35 // change stdin to non blocking
36 #ifndef WIN32
37         fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
38 #endif
39         va_start (argptr,error);
40         dpvsnprintf (string, sizeof (string), error, argptr);
41         va_end (argptr);
42
43         Con_Printf(CON_ERROR "Engine Error: %s\n", string);
44
45         //Host_Shutdown ();
46         exit (1);
47 }
48
49 void Sys_Print(const char *text)
50 {
51         if(sys.outfd < 0)
52                 return;
53         // BUG: for some reason, NDELAY also affects stdout (1) when used on stdin (0).
54         // this is because both go to /dev/tty by default!
55         {
56 #ifndef WIN32
57                 int origflags = fcntl (sys.outfd, F_GETFL, 0);
58                 fcntl (sys.outfd, F_SETFL, origflags & ~O_NONBLOCK);
59 #else
60 #define write _write
61 #endif
62                 while(*text)
63                 {
64                         fs_offset_t written = (fs_offset_t)write(sys.outfd, text, (int)strlen(text));
65                         if(written <= 0)
66                                 break; // sorry, I cannot do anything about this error - without an output
67                         text += written;
68                 }
69 #ifndef WIN32
70                 fcntl (sys.outfd, F_SETFL, origflags);
71 #endif
72         }
73         //fprintf(stdout, "%s", text);
74 }
75
76 char *Sys_ConsoleInput(void)
77 {
78         static char text[MAX_INPUTLINE];
79         static unsigned int len = 0;
80 #ifdef WIN32
81         int c;
82
83         // read a line out
84         while (_kbhit ())
85         {
86                 c = _getch ();
87                 if (c == '\r')
88                 {
89                         text[len] = '\0';
90                         _putch ('\n');
91                         len = 0;
92                         return text;
93                 }
94                 if (c == '\b')
95                 {
96                         if (len)
97                         {
98                                 _putch (c);
99                                 _putch (' ');
100                                 _putch (c);
101                                 len--;
102                         }
103                         continue;
104                 }
105                 if (len < sizeof (text) - 1)
106                 {
107                         _putch (c);
108                         text[len] = c;
109                         len++;
110                 }
111         }
112 #else
113         fd_set fdset;
114         struct timeval timeout;
115         FD_ZERO(&fdset);
116         FD_SET(0, &fdset); // stdin
117         timeout.tv_sec = 0;
118         timeout.tv_usec = 0;
119         if (select (1, &fdset, NULL, NULL, &timeout) != -1 && FD_ISSET(0, &fdset))
120         {
121                 len = read (0, text, sizeof(text) - 1);
122                 if (len >= 1)
123                 {
124                         // rip off the \n and terminate
125                         // div0: WHY? console code can deal with \n just fine
126                         // this caused problems with pasting stuff into a terminal window
127                         // so, not ripping off the \n, but STILL keeping a NUL terminator
128                         text[len] = 0;
129                         return text;
130                 }
131         }
132 #endif
133         return NULL;
134 }
135
136 char *Sys_GetClipboardData (void)
137 {
138         return NULL;
139 }
140
141 int main (int argc, char **argv)
142 {
143         signal(SIGFPE, SIG_IGN);
144         sys.selffd = -1;
145         sys.argc = argc;
146         sys.argv = (const char **)argv;
147         Sys_ProvideSelfFD();
148
149         // COMMANDLINEOPTION: sdl: -noterminal disables console output on stdout
150         if(Sys_CheckParm("-noterminal"))
151                 sys.outfd = -1;
152         // COMMANDLINEOPTION: sdl: -stderr moves console output to stderr
153         else if(Sys_CheckParm("-stderr"))
154                 sys.outfd = 2;
155         else
156                 sys.outfd = 1;
157 #ifndef WIN32
158         fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | O_NONBLOCK);
159 #endif
160
161         // used by everything
162         Memory_Init();
163
164         Host_Main();
165
166         Sys_Quit(0);
167
168         return 0;
169 }
170
171 qbool sys_supportsdlgetticks = false;
172 unsigned int Sys_SDL_GetTicks (void)
173 {
174         Sys_Error("Called Sys_SDL_GetTicks on non-SDL target");
175         return 0;
176 }
177 void Sys_SDL_Delay (unsigned int milliseconds)
178 {
179         Sys_Error("Called Sys_SDL_Delay on non-SDL target");
180 }