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