Annotation of researchv9/X11/src/X.V11R1/server/os/4.2bsd/utils.c, revision 1.1.1.1

1.1       root        1: /***********************************************************
                      2: Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
                      3: and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
                      4: 
                      5:                         All Rights Reserved
                      6: 
                      7: Permission to use, copy, modify, and distribute this software and its 
                      8: documentation for any purpose and without fee is hereby granted, 
                      9: provided that the above copyright notice appear in all copies and that
                     10: both that copyright notice and this permission notice appear in 
                     11: supporting documentation, and that the names of Digital or MIT not be
                     12: used in advertising or publicity pertaining to distribution of the
                     13: software without specific, written prior permission.  
                     14: 
                     15: DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
                     16: ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
                     17: DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
                     18: ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
                     19: WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
                     20: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
                     21: SOFTWARE.
                     22: 
                     23: ******************************************************************/
                     24: /* $Header: utils.c,v 1.34 87/09/14 02:09:06 toddb Exp $ */
                     25: #include <stdio.h>
                     26: #include <sys/time.h>
                     27: #include "misc.h"
                     28: #include "X.h"
                     29: #include "input.h"
                     30: #include "opaque.h"
                     31: #include "site.h"
                     32: #include <strings.h>
                     33: extern char *display;
                     34: 
                     35: extern long defaultScreenSaverTime;    /* for parsing command line */
                     36: extern long defaultScreenSaverInterval;
                     37: extern int defaultScreenSaverBlanking;
                     38: 
                     39: extern long ScreenSaverTime;           /* for forcing reset */
                     40: 
                     41: Bool clientsDoomed = FALSE;
                     42: extern void KillServerResources();
                     43: 
                     44: /* Force connections to close on SIGHUP from init */
                     45: 
                     46: AutoResetServer ()
                     47: {
                     48:     clientsDoomed = TRUE;
                     49:     /* force an immediate timeout (and exit) in WaitForSomething */
                     50:     ScreenSaverTime = TimeSinceLastInputEvent();
                     51: #ifdef GPROF
                     52:     chdir ("/tmp");
                     53:     exit (0);
                     54: #endif
                     55: }
                     56: 
                     57: /* Force connections to close and then exit on SIGTERM, SIGINT */
                     58: 
                     59: GiveUp()
                     60: {
                     61:     KillServerResources();
                     62:     exit(0);
                     63: }
                     64: 
                     65: 
                     66: void
                     67: ErrorF( f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9) /* limit of ten args */
                     68:     char *     f;
                     69:     char *s0, *s1, *s2, *s3, *s4, *s5, *s6, *s7, *s8, *s9;
                     70: {
                     71:     fprintf( stderr, f, s0, s1, s2, s3, s4, s5, s6, s7, s8, s9);
                     72: }
                     73: 
                     74: void
                     75: AbortServer()
                     76: {
                     77:     abort();
                     78: }
                     79: 
                     80: int
                     81: Error(str)
                     82:     char *str;
                     83: {
                     84:     perror(str);
                     85: }
                     86: 
                     87: int
                     88: Notice()
                     89: {
                     90: }
                     91: 
                     92: int
                     93: GetTimeInMillis()
                     94: {
                     95:     struct timeval  tp;
                     96: 
                     97:     gettimeofday(&tp, 0);
                     98:     return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
                     99: }
                    100: 
                    101: /*
                    102:  * This function parses the command line. Handles device-independent fields
                    103:  * only.  It is not allowed to modify argc or any of the strings pointed to
                    104:  * by argv.
                    105:  */
                    106: ProcessCommandLine ( argc, argv )
                    107: int    argc;
                    108: char   *argv[];
                    109: 
                    110: {
                    111:     int i;
                    112: 
                    113:     for ( i = 1; i < argc; i++ )
                    114:     {
                    115:        /* initialize display */
                    116:        if(argv[i][0] ==  ':')  
                    117:        {
                    118:            display = argv[i];
                    119:            display++;
                    120:        }
                    121:        else if ( strcmp( argv[i], "-a") == 0)
                    122:        {
                    123:            if(++i < argc)
                    124:                defaultPointerControl.num = atoi(argv[i]);
                    125:            else
                    126:                UseMsg();
                    127:        }
                    128:        else if ( strcmp( argv[i], "c") == 0)
                    129:        {
                    130:            if(++i < argc)
                    131:                defaultKeyboardControl.click = atoi(argv[i]);
                    132:            else
                    133:                UseMsg();
                    134:        }
                    135:        else if ( strcmp( argv[i], "-c") == 0)
                    136:        {
                    137:            defaultKeyboardControl.click = 0;
                    138:        }
                    139:        else if ( strcmp( argv[i], "-co") == 0)
                    140:        {
                    141:            if(++i < argc)
                    142:                rgbPath = argv[i];
                    143:            else
                    144:                UseMsg();
                    145:        }
                    146:        else if ( strcmp( argv[i], "-f") == 0)
                    147:        {
                    148:            if(++i < argc)
                    149:                defaultKeyboardControl.bell = atoi(argv[i]);
                    150:            else
                    151:                UseMsg();
                    152:        }
                    153:        else if ( strcmp( argv[i], "-fc") == 0)
                    154:        {
                    155:            if(++i < argc)
                    156:                defaultCursorFont = argv[i];
                    157:            else
                    158:                UseMsg();
                    159:        }
                    160:        else if ( strcmp( argv[i], "-fn") == 0)
                    161:        {
                    162:            if(++i < argc)
                    163:                defaultTextFont = argv[i];
                    164:            else
                    165:                UseMsg();
                    166:        }
                    167:        else if ( strcmp( argv[i], "-fp") == 0)
                    168:        {
                    169:            if(++i < argc)
                    170:                defaultFontPath = argv[i];
                    171:            else
                    172:                UseMsg();
                    173:        }
                    174:        else if ( strcmp( argv[i], "-help") == 0)
                    175:        {
                    176:            UseMsg();
                    177:            exit(0);
                    178:        }
                    179:        else if ( strcmp( argv[i], "-p") == 0)
                    180:        {
                    181:            if(++i < argc)
                    182:                defaultScreenSaverInterval = atoi(argv[i]) * MILLI_PER_MIN;
                    183:            else
                    184:                UseMsg();
                    185:        }
                    186:        else if ( strcmp( argv[i], "r") == 0)
                    187:            defaultKeyboardControl.autoRepeat = TRUE;
                    188:        else if ( strcmp( argv[i], "-r") == 0)
                    189:            defaultKeyboardControl.autoRepeat = FALSE;
                    190:        else if ( strcmp( argv[i], "-s") == 0)
                    191:        {
                    192:            if(++i < argc)
                    193:                defaultScreenSaverTime = atoi(argv[i]) * MILLI_PER_MIN;
                    194:            else
                    195:                UseMsg();
                    196:        }
                    197:        else if ( strcmp( argv[i], "-t") == 0)
                    198:        {
                    199:            if(++i < argc)
                    200:                defaultPointerControl.threshold = atoi(argv[i]);
                    201:            else
                    202:                UseMsg();
                    203:        }
                    204:        else if ( strcmp( argv[i], "-to") == 0)
                    205:        {
                    206:            if(++i < argc)
                    207:            {
                    208:                if((TimeOutValue = atoi(argv[i])) < 0)
                    209:                    TimeOutValue = DEFAULT_TIMEOUT;
                    210:            }
                    211:            else
                    212:                UseMsg();
                    213:        }
                    214:        else if ( strcmp( argv[i], "v") == 0)
                    215:            defaultScreenSaverBlanking = PreferBlanking;
                    216:        else if ( strcmp( argv[i], "-v") == 0)
                    217:            defaultScreenSaverBlanking = DontPreferBlanking;
                    218:        else if ( strcmp( argv[i], "-x") == 0)
                    219:        {
                    220:            if(++i >= argc)
                    221:                UseMsg();
                    222:            /* For U**x, which doesn't support dynamic loading, there's nothing
                    223:             * to do when we see a -x.  Either the extension is linked in or
                    224:             * it isn't */
                    225:        }
                    226:     }
                    227: }
                    228: 
                    229: UseMsg()
                    230: {
                    231:     ErrorF("use: X <display> [option] <tty>\n");
                    232:     ErrorF("-a #                   mouse acceleration (pixels)\n");
                    233:     ErrorF("-bp<:screen> color     BlackPixel for screen\n");
                    234:     ErrorF("-c                     turns off key-click\n");
                    235:     ErrorF("c #                    key-click volume (0-8)\n");
                    236:     ErrorF("-co string             color database file\n");
                    237:     ErrorF("-fc string             cursor font\n");
                    238:     ErrorF("-fn string             default text font name\n");
                    239:     ErrorF("-fp string             default text font path\n");
                    240:     ErrorF("-p #                   screen-saver pattern duration (seconds)\n");
                    241:     ErrorF("-r                     turns off auto-repeat\n");
                    242:     ErrorF("r                      turns on auto-repeat \n");
                    243:     ErrorF("-f #                   bell base (0-100)\n");
                    244:     ErrorF("-x string              loads named extension at init time \n");
                    245:     ErrorF("-help                  prints message with these options\n");
                    246:     ErrorF("-s #                   screen-saver timeout (seconds)\n");
                    247:     ErrorF("-t #                   mouse threshold (pixels)\n");
                    248:     ErrorF("-to #                  connection time out\n");
                    249:     ErrorF("v                      video blanking for screen-saver\n");
                    250:     ErrorF("-v                     screen-saver without video blanking\n");
                    251:     ErrorF("-wp<:screen> color     WhitePixel for screen\n");
                    252:     ErrorF("There may be other device-dependent options as well\n");
                    253: }
                    254: /*
                    255:  * malloc wrap-around, to take care of the "no memory" case, since
                    256:  * it would be difficult in many places to "back out" on failure.
                    257:  */
                    258: #ifdef DEBUG
                    259: #define FIRSTMAGIC 0x11aaaa11
                    260: #define SECONDMAGIC 0x22aaaa22
                    261: #define FREEDMAGIC  0x33aaaa33
                    262: #endif
                    263: 
                    264: /* XALLOC -- X's internal memory allocator.  Why does it return unsigned
                    265:  * int * instead of the more common char *?  Well, if you read K&R you'll
                    266:  * see they say that alloc must return a pointer "suitable for conversion"
                    267:  * to whatever type you really want.  In a full-blown generic allocator
                    268:  * there's no way to solve the alignment problems without potentially
                    269:  * wasting lots of space.  But we have a more limited problem. We know
                    270:  * we're only ever returning pointers to structures which will have to
                    271:  * be long word aligned.  So we are making a stronger guarantee.  It might
                    272:  * have made sense to make Xalloc return char * to conform with people's
                    273:  * expectations of malloc, but this makes lint happier.
                    274:  */
                    275: 
                    276: unsigned long * 
                    277: Xalloc (amount)
                    278:     int amount;
                    279: {
                    280:     char               *malloc();
                    281:     register pointer  ptr;
                    282:        
                    283:     if(amount == 0)
                    284:        return( (unsigned long *)NULL);
                    285: #ifdef DEBUG
                    286:         /* aligned extra on long word boundary */
                    287:     amount = (amount + 3) & ~3;  
                    288:     if (ptr =  (pointer) malloc( ((unsigned) amount) + 12))
                    289:     {
                    290:         *(unsigned long *)ptr = FIRSTMAGIC;
                    291:         *((unsigned long *)(ptr + 4)) = amount;
                    292:         *((unsigned long *)(ptr + 8 + amount)) = SECONDMAGIC;
                    293:        return (unsigned long *) (ptr + 8);
                    294:     }
                    295:     FatalError("Error in Xalloc\n");
                    296:     /* NOTREACHED */
                    297: #else
                    298:     ptr =  (pointer) malloc( ((unsigned) amount));
                    299:     return ((unsigned long *)ptr);
                    300: #endif /* DEBUG */
                    301:     /* NOTREACHED */
                    302: }
                    303: 
                    304: /*****************
                    305:  * Xrealloc
                    306:  *     realloc wrap-around, to take care of the "no memory" case, since
                    307:  *     it would be difficult in many places to "back out" on failure.
                    308:  *****************/
                    309: 
                    310: extern unsigned long *minfree;           /* DEBUG */
                    311: 
                    312: unsigned long *
                    313: Xrealloc (ptr, amount)
                    314: register pointer ptr;
                    315: int amount;
                    316: {
                    317:     char *malloc();
                    318:     char *realloc();
                    319:     char *foo;
                    320: 
                    321:     amount = (amount + 3) & ~3;  
                    322:     if (ptr)
                    323:     {
                    324:         if (ptr < (pointer) minfree)
                    325:         {
                    326:            ErrorF("Xrealloc: trying to free static storage\n");
                    327:            /* Force a core dump */
                    328:            AbortServer();
                    329:        }
                    330: #ifdef DEBUG
                    331:        if (!CheckNode(ptr - 8))
                    332:            AbortServer();
                    333:        ptr = (pointer) realloc ((ptr - 8), (unsigned) amount + 12);
                    334: #else
                    335:         ptr = (pointer) realloc (ptr, amount);
                    336: #endif /* DEBUG */
                    337:     }
                    338:     else
                    339:     {
                    340: #ifdef DEBUG
                    341:        ptr =  (pointer) malloc (((unsigned) amount) + 12);
                    342: #else
                    343:        ptr =  (pointer) malloc ((unsigned) amount);
                    344: #endif 
                    345:     }
                    346: #ifdef DEBUG
                    347:     if (ptr)
                    348:     {
                    349:         *(unsigned long *)ptr = FIRSTMAGIC;
                    350:        *((unsigned long *)(ptr + 4)) = amount;
                    351:        *((unsigned long *)(ptr + 8 + amount)) = SECONDMAGIC;
                    352:        return ((unsigned long *) (ptr + 8));
                    353:     }
                    354:     FatalError ("Error in Xrealloc\n");
                    355:     /*NOTREACHED*/
                    356: #else
                    357:         return((unsigned long *)ptr);
                    358: #endif /* DEBUG */
                    359: }
                    360:                     
                    361: /*****************
                    362:  *  Xfree
                    363:  *    calls free 
                    364:  *****************/    
                    365: 
                    366: void
                    367: Xfree(ptr)
                    368:     register pointer ptr;
                    369: {
                    370:     if (ptr == (pointer) NULL)
                    371:        return;
                    372: #ifdef DEBUG
                    373:     if (ptr < (pointer) minfree)
                    374:        ErrorF("Xfree: trying to free static storage\n");
                    375:     else if (CheckNode(ptr - 8))
                    376:     {
                    377:         *(unsigned long *)(ptr - 8) = FREEDMAGIC;
                    378:        free(ptr - 8); 
                    379:     }
                    380: #else
                    381:     if (ptr >= (pointer) minfree)
                    382:         free(ptr); 
                    383: #endif /* DEBUG */
                    384: }
                    385: 
                    386: #ifdef DEBUG
                    387: static Bool
                    388: CheckNode(ptr)
                    389:     pointer ptr;
                    390: {
                    391:     int    amount;
                    392: 
                    393:     amount = *((unsigned long *)(ptr + 4));
                    394:     if (*((unsigned long *) ptr) == FREEDMAGIC)
                    395:     {
                    396:         ErrorF("Freeing something already freed!\n");
                    397:        return FALSE;
                    398:     }
                    399:     if( *((unsigned long *) ptr) != FIRSTMAGIC ||
                    400:         *((unsigned long *) (ptr + amount + 8)) != SECONDMAGIC)
                    401:        FatalError("Heap bug!\n");
                    402:     return TRUE;
                    403: }
                    404: #endif /* DEBUG */

unix.superglobalmegacorp.com

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