Annotation of quake1/sys_aix.c, revision 1.1

1.1     ! root        1: #include <unistd.h>
        !             2: #include <signal.h>
        !             3: #include <stdlib.h>
        !             4: #include <limits.h>
        !             5: #include <sys/time.h>
        !             6: #include <sys/types.h>
        !             7: #include <unistd.h>
        !             8: #include <fcntl.h>
        !             9: #include <stdarg.h>
        !            10: #include <stdio.h>
        !            11: #include <sys/ipc.h>
        !            12: #include <sys/shm.h>
        !            13: #include <sys/stat.h>
        !            14: #include <string.h>
        !            15: #include <ctype.h>
        !            16: #include <sys/wait.h>
        !            17: #include <sys/mman.h>
        !            18: #include <errno.h>
        !            19: 
        !            20: #include "quakedef.h"
        !            21: 
        !            22: int noconinput=0;
        !            23: 
        !            24: char *basedir = ".";
        !            25: char *cachedir = "/tmp";
        !            26: 
        !            27: cvar_t  sys_linerefresh = {"sys_linerefresh","0"};// set for entity display
        !            28: cvar_t  sys_nostdout = {"sys_nostdout","0"};
        !            29: 
        !            30: // =======================================================================
        !            31: // General routines
        !            32: // =======================================================================
        !            33: 
        !            34: void Sys_HighFPPrecision (void)
        !            35: {
        !            36: }
        !            37: 
        !            38: void Sys_LowFPPrecision (void)
        !            39: {
        !            40: }
        !            41: 
        !            42: void Sys_DebugNumber(int y, int val)
        !            43: {
        !            44: }
        !            45: 
        !            46: /*
        !            47: void Sys_Printf (char *fmt, ...)
        !            48: {
        !            49:        va_list         argptr;
        !            50:        char            text[1024];
        !            51:        
        !            52:        va_start (argptr,fmt);
        !            53:        vsprintf (text,fmt,argptr);
        !            54:        va_end (argptr);
        !            55:        fprintf(stderr, "%s", text);
        !            56:        
        !            57:        Con_Print (text);
        !            58: }
        !            59: */
        !            60: 
        !            61: void Sys_Printf (char *fmt, ...)
        !            62: {
        !            63: 
        !            64:     va_list     argptr;
        !            65:     char        text[1024], *t_p;
        !            66:     int         l, r;
        !            67: 
        !            68:     if (sys_nostdout.value)
        !            69:         return;
        !            70: 
        !            71:     va_start (argptr,fmt);
        !            72:     vsprintf (text,fmt,argptr);
        !            73:     va_end (argptr);
        !            74: 
        !            75:     l = strlen(text);
        !            76:     t_p = text;
        !            77: 
        !            78: // make sure everything goes through, even though we are non-blocking
        !            79:     while (l)
        !            80:     {
        !            81:         r = write (1, text, l);
        !            82:         if (r != l)
        !            83:             sleep (0);
        !            84:         if (r > 0)
        !            85:         {
        !            86:             t_p += r;
        !            87:             l -= r;
        !            88:         }
        !            89:     }
        !            90: 
        !            91: }
        !            92: 
        !            93: void Sys_Quit (void)
        !            94: {
        !            95:        Host_Shutdown();
        !            96:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
        !            97:        exit(0);
        !            98: }
        !            99: 
        !           100: void Sys_Init(void)
        !           101: {
        !           102: }
        !           103: 
        !           104: void Sys_Error (char *error, ...)
        !           105: { 
        !           106:     va_list     argptr;
        !           107:     char        string[1024];
        !           108: 
        !           109: // change stdin to non blocking
        !           110:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
        !           111:     
        !           112:     va_start (argptr,error);
        !           113:     vsprintf (string,error,argptr);
        !           114:     va_end (argptr);
        !           115:        fprintf(stderr, "Error: %s\n", string);
        !           116: 
        !           117:        Host_Shutdown ();
        !           118:        exit (1);
        !           119: 
        !           120: } 
        !           121: 
        !           122: void Sys_Warn (char *warning, ...)
        !           123: { 
        !           124:     va_list     argptr;
        !           125:     char        string[1024];
        !           126:     
        !           127:     va_start (argptr,warning);
        !           128:     vsprintf (string,warning,argptr);
        !           129:     va_end (argptr);
        !           130:        fprintf(stderr, "Warning: %s", string);
        !           131: } 
        !           132: 
        !           133: /*
        !           134: ============
        !           135: Sys_FileTime
        !           136: 
        !           137: returns -1 if not present
        !           138: ============
        !           139: */
        !           140: int    Sys_FileTime (char *path)
        !           141: {
        !           142:        struct  stat    buf;
        !           143:        
        !           144:        if (stat (path,&buf) == -1)
        !           145:                return -1;
        !           146:        
        !           147:        return buf.st_mtime;
        !           148: }
        !           149: 
        !           150: 
        !           151: void Sys_mkdir (char *path)
        !           152: {
        !           153:     if (mkdir (path, 0777) != -1)
        !           154:         return;
        !           155:     if (errno != EEXIST)
        !           156:         Sys_Error ("mkdir %s: %s",path, strerror(errno));
        !           157: }
        !           158: 
        !           159: int Sys_FileOpenRead (char *path, int *handle)
        !           160: {
        !           161:        int     h;
        !           162:        struct stat     fileinfo;
        !           163:     
        !           164:        
        !           165:        h = open (path, O_RDONLY, 0666);
        !           166:        *handle = h;
        !           167:        if (h == -1)
        !           168:                return -1;
        !           169:        
        !           170:        if (fstat (h,&fileinfo) == -1)
        !           171:                Sys_Error ("Error fstating %s", path);
        !           172: 
        !           173:        return fileinfo.st_size;
        !           174: }
        !           175: 
        !           176: int Sys_FileOpenWrite (char *path)
        !           177: {
        !           178:        int     handle;
        !           179: 
        !           180:        umask (0);
        !           181:        
        !           182:        handle = open(path,O_RDWR | O_CREAT | O_TRUNC
        !           183:        , 0666);
        !           184: 
        !           185:        if (handle == -1)
        !           186:                Sys_Error ("Error opening %s: %s", path,strerror(errno));
        !           187: 
        !           188:        return handle;
        !           189: }
        !           190: 
        !           191: int Sys_FileWrite (int handle, void *src, int count)
        !           192: {
        !           193:        return write (handle, src, count);
        !           194: }
        !           195: 
        !           196: void Sys_FileClose (int handle)
        !           197: {
        !           198:        close (handle);
        !           199: }
        !           200: 
        !           201: void Sys_FileSeek (int handle, int position)
        !           202: {
        !           203:        lseek (handle, position, SEEK_SET);
        !           204: }
        !           205: 
        !           206: int Sys_FileRead (int handle, void *dest, int count)
        !           207: {
        !           208:     return read (handle, dest, count);
        !           209: }
        !           210: 
        !           211: void Sys_DebugLog(char *file, char *fmt, ...)
        !           212: {
        !           213:     va_list argptr; 
        !           214:     static char data[1024];
        !           215:     int fd;
        !           216:     
        !           217:     va_start(argptr, fmt);
        !           218:     vsprintf(data, fmt, argptr);
        !           219:     va_end(argptr);
        !           220: //    fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
        !           221:     fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
        !           222:     write(fd, data, strlen(data));
        !           223:     close(fd);
        !           224: }
        !           225: 
        !           226: void Sys_EditFile(char *filename)
        !           227: {
        !           228: 
        !           229:        char cmd[256];
        !           230:        char *term;
        !           231:        char *editor;
        !           232: 
        !           233:        term = getenv("TERM");
        !           234:        if (term && !strcmp(term, "xterm"))
        !           235:        {
        !           236:                editor = getenv("VISUAL");
        !           237:                if (!editor)
        !           238:                        editor = getenv("EDITOR");
        !           239:                if (!editor)
        !           240:                        editor = getenv("EDIT");
        !           241:                if (!editor)
        !           242:                        editor = "vi";
        !           243:                sprintf(cmd, "xterm -e %s %s", editor, filename);
        !           244:                system(cmd);
        !           245:        }
        !           246: 
        !           247: }
        !           248: 
        !           249: double Sys_FloatTime (void)
        !           250: {
        !           251:     struct timeval tp;
        !           252:     struct timezone tzp; 
        !           253:     static int      secbase; 
        !           254:     
        !           255:     gettimeofday(&tp, &tzp);  
        !           256: 
        !           257:     if (!secbase)
        !           258:     {
        !           259:         secbase = tp.tv_sec;
        !           260:         return tp.tv_usec/1000000.0;
        !           261:     }
        !           262: 
        !           263:     return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
        !           264: }
        !           265: 
        !           266: // =======================================================================
        !           267: // Sleeps for microseconds
        !           268: // =======================================================================
        !           269: 
        !           270: static volatile int oktogo;
        !           271: 
        !           272: void alarm_handler(int x)
        !           273: {
        !           274:        oktogo=1;
        !           275: }
        !           276: 
        !           277: byte *Sys_ZoneBase (int *size)
        !           278: {
        !           279: 
        !           280:        char *QUAKEOPT = getenv("QUAKEOPT");
        !           281: 
        !           282:        *size = 0xc00000;
        !           283:        if (QUAKEOPT)
        !           284:        {
        !           285:                while (*QUAKEOPT)
        !           286:                        if (tolower(*QUAKEOPT++) == 'm')
        !           287:                        {
        !           288:                                *size = atof(QUAKEOPT) * 1024*1024;
        !           289:                                break;
        !           290:                        }
        !           291:        }
        !           292:        return malloc (*size);
        !           293: 
        !           294: }
        !           295: 
        !           296: void Sys_LineRefresh(void)
        !           297: {
        !           298: }
        !           299: 
        !           300: void Sys_Sleep(void)
        !           301: {
        !           302:        usleep(10);
        !           303: }
        !           304: 
        !           305: void Sys_GetMemory(quakeparms_t *parms)
        !           306: {
        !           307:     FILE *f;
        !           308:     char buffer[256];
        !           309:        char *procparse;
        !           310:     int freemem, buffermem, totalmem;
        !           311:     int rc, suggestion;
        !           312: 
        !           313: //     parms->memsize = 8*1024*1024;
        !           314:        parms->memsize = 5861376;
        !           315: 
        !           316:     f = fopen("/proc/meminfo", "r");
        !           317:     if (f)
        !           318:        {
        !           319:                fgets(buffer, sizeof buffer, f);
        !           320:                procparse = "%s %d %d %d %d %d";
        !           321:                rc = fscanf(f, procparse, buffer, &totalmem, buffer, &freemem,
        !           322:                  buffer, &buffermem);
        !           323: 
        !           324:                suggestion = (9*buffermem)/10 + freemem;
        !           325:                if (suggestion > totalmem)
        !           326:                        Sys_Printf("[%s] did not properly parse /proc/meminfo\n", procparse);
        !           327:                if (suggestion > parms->memsize && suggestion < totalmem)
        !           328:                        parms->memsize = suggestion;
        !           329: 
        !           330:                fclose(f);
        !           331:        }
        !           332:        else
        !           333:                Sys_Printf("Did you know /proc breaks up painful gas bubbles?\n");
        !           334: 
        !           335:        if (parms->memsize > 8*1024*1024) parms->memsize = 8*1024*1024;
        !           336: 
        !           337:        parms->membase = malloc (parms->memsize);
        !           338: 
        !           339: }
        !           340: 
        !           341: 
        !           342: void floating_point_exception_handler(int whatever)
        !           343: {
        !           344: //     Sys_Warn("floating point exception\n");
        !           345:        signal(SIGFPE, floating_point_exception_handler);
        !           346: }
        !           347: 
        !           348: /*
        !           349: char *Sys_ConsoleInput(void)
        !           350: {
        !           351:     static char text[256];
        !           352:     int     len;
        !           353: 
        !           354:     len = read (0, text, sizeof(text));
        !           355:     if (len < 1)
        !           356:         return NULL;
        !           357:     text[len-1] = 0;    // rip off the /n and terminate
        !           358: 
        !           359:     return text;
        !           360: }
        !           361: */
        !           362: 
        !           363: void moncontrol(int x)
        !           364: {
        !           365: }
        !           366: 
        !           367: int main (int c, char **v)
        !           368: {
        !           369: 
        !           370:        double          time, oldtime, newtime;
        !           371:        quakeparms_t parms;
        !           372:        extern int vcrFile;
        !           373:        extern int recording;
        !           374:        static int frame;
        !           375: 
        !           376: //     static char cwd[1024];
        !           377: 
        !           378:        moncontrol(0);
        !           379: 
        !           380: //     signal(SIGFPE, floating_point_exception_handler);
        !           381:        signal(SIGFPE, SIG_IGN);
        !           382: 
        !           383: //     getcwd(cwd);
        !           384: //     if (cwd[Q_strlen(cwd)-1] == '/') cwd[Q_strlen(cwd)-1] = 0;
        !           385: //     parms.basedir = cwd;
        !           386:        parms.memsize = 12 * 1024 * 1024;
        !           387:        parms.membase = malloc(parms.memsize);
        !           388:        parms.basedir = basedir;
        !           389:        parms.cachedir = cachedir;
        !           390: //     com_argc = parms.argc = c;
        !           391: //     com_argv = parms.argv = v;
        !           392: 
        !           393:        COM_InitArgv(c,v);
        !           394:        parms.argc = com_argc;
        !           395:        parms.argv = com_argv;
        !           396: 
        !           397:     Host_Init(&parms);
        !           398: 
        !           399:        noconinput = COM_CheckParm("-noconinput");
        !           400:        if (!noconinput)
        !           401:                fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
        !           402: 
        !           403:        Cvar_RegisterVariable (&sys_nostdout);
        !           404: 
        !           405:     oldtime = Sys_FloatTime () - 0.1;
        !           406:     while (1)
        !           407:     {
        !           408: // find time spent rendering last frame
        !           409:         newtime = Sys_FloatTime ();
        !           410:         time = newtime - oldtime;
        !           411: 
        !           412:         if (cls.state == ca_dedicated)
        !           413:         {   // play vcrfiles at max speed
        !           414:             if (time < sys_ticrate.value && (vcrFile == -1 || recording) )
        !           415:             {
        !           416:                 usleep (1);
        !           417:                 continue;       // not time to run a server only tic yet
        !           418:             }
        !           419:             time = sys_ticrate.value;
        !           420:         }
        !           421: 
        !           422:         if (time > sys_ticrate.value*2)
        !           423:             oldtime = newtime;
        !           424:         else
        !           425:             oldtime += time;
        !           426: 
        !           427:         if (++frame > 10)
        !           428:             moncontrol(1);      // profile only while we do each Quake frame
        !           429:         Host_Frame (time);
        !           430:         moncontrol(0);
        !           431: 
        !           432: // graphic debugging aids
        !           433:         if (sys_linerefresh.value)
        !           434:             Sys_LineRefresh ();
        !           435:     }
        !           436: 
        !           437: }
        !           438: 
        !           439: 
        !           440: /*
        !           441: ================
        !           442: Sys_MakeCodeWriteable
        !           443: ================
        !           444: */
        !           445: void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
        !           446: {
        !           447: 
        !           448:        int r;
        !           449:        unsigned int addr;
        !           450:        int psize = getpagesize();
        !           451: 
        !           452:        addr = startaddr & ~(psize-1);
        !           453: 
        !           454:        r = mprotect((char*)addr, length + startaddr - addr, 7);
        !           455: 
        !           456:        if (r < 0)
        !           457:                Sys_Error("Protection change failed\n");
        !           458: 
        !           459: }
        !           460: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.