1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555
|
/* sqUnixMain.c -- support for Unix.
*
* Copyright (C) 1996-2007 by Ian Piumarta and other authors/contributors
* listed elsewhere in this file.
* All rights reserved.
*
* This file is part of Unix Squeak.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* Author: Ian Piumarta <ian.piumarta@squeakland.org>
*
* Last edited: 2012-09-16 21:47:24 by piumarta on linux64
*/
#include "sq.h"
#include "sqMemoryAccess.h"
#include "sqaio.h"
#include "sqUnixCharConv.h"
#include "debug.h"
#ifdef ioMSecs
# undef ioMSecs
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#if defined(__alpha__) && defined(__osf__)
# include <sys/sysinfo.h>
# include <sys/proc.h>
#endif
#undef DEBUG_MODULES
#undef IMAGE_DUMP /* define to enable SIGHUP and SIGQUIT handling */
#define IMAGE_NAME_SIZE MAXPATHLEN
#define DefaultHeapSize 20 /* megabytes BEYOND actual image size */
#define DefaultMmapSize 1024 /* megabytes of virtual memory */
char *documentName= 0; /* name if launced from document */
char shortImageName[MAXPATHLEN+1]; /* image name */
char imageName[MAXPATHLEN+1]; /* full path to image */
static char vmName[MAXPATHLEN+1]; /* full path to vm */
char vmPath[MAXPATHLEN+1]; /* full path to image directory */
char *vmLibDir= VM_LIBDIR; /* full path to lib directory */
int argCnt= 0; /* global copies for access from plugins */
char **argVec= 0;
char **envVec= 0;
static int vmArgCnt= 0; /* for getAttributeIntoLength() */
static char **vmArgVec= 0;
static int squeakArgCnt= 0;
static char **squeakArgVec= 0;
static int extraMemory= 0;
int useMmap= DefaultMmapSize * 1024 * 1024;
static int useItimer= 1; /* 0 to disable itimer-based clock */
int noEvents= 0; /* 1 to disable new event handling */
int noSoundMixer= 0; /* 1 to disable writing sound mixer levels */
char *squeakPlugins= 0; /* plugin path */
int useJit= 0; /* use default */
int jitProcs= 0; /* use default */
int jitMaxPIC= 0; /* use default */
int withSpy= 0;
int uxDropFileCount= 0; /* number of dropped items */
char **uxDropFileNames= 0; /* dropped filenames */
int textEncodingUTF8= 0; /* 1 if copy from external selection uses UTF8 */
#if defined(IMAGE_DUMP)
static int dumpImageFile= 0; /* 1 after SIGHUP received */
#endif
#if defined(DARWIN)
int inModalLoop= 0;
#endif
int sqIgnorePluginErrors = 0;
int runInterpreter = 1;
#include "SqDisplay.h"
#include "SqSound.h"
struct SqDisplay *dpy= 0;
struct SqSound *snd= 0;
/*** timer support ***/
#define LOW_RES_TICK_MSECS 20 /* 1/50 second resolution */
static unsigned int lowResMSecs= 0;
static struct timeval startUpTime;
static void sigalrm(int signum)
{
lowResMSecs+= LOW_RES_TICK_MSECS;
}
static void initTimers(void)
{
/* set up the micro/millisecond clock */
gettimeofday(&startUpTime, 0);
if (useItimer)
{
/* set up the low-res (50th second) millisecond clock */
/* WARNING: all system calls must check for EINTR!!! */
{
struct sigaction sa;
sigset_t ss1, ss2;
sigemptyset(&ss1);
sigprocmask(SIG_BLOCK, &ss1, &ss2);
sa.sa_handler= sigalrm;
sa.sa_mask= ss2;
# ifdef SA_RESTART /* we're probably on Linux */
sa.sa_flags= SA_RESTART;
# else
sa.sa_flags= 0; /* assume we already have BSD behaviour */
# endif
# if defined(__linux__) && !defined(__ia64) && !defined(__alpha__)
sa.sa_restorer= 0;
# endif
sigaction(SIGALRM, &sa, 0);
}
{
struct itimerval iv;
iv.it_interval.tv_sec= 0;
iv.it_interval.tv_usec= LOW_RES_TICK_MSECS * 1000;
iv.it_value= iv.it_interval;
setitimer(ITIMER_REAL, &iv, 0);
}
}
}
sqInt ioLowResMSecs(void)
{
return (useItimer)
? lowResMSecs
: ioMSecs();
}
sqInt ioMSecs(void)
{
struct timeval now;
gettimeofday(&now, 0);
if ((now.tv_usec-= startUpTime.tv_usec) < 0)
{
now.tv_usec+= 1000000;
now.tv_sec-= 1;
}
now.tv_sec-= startUpTime.tv_sec;
return lowResMSecs= (now.tv_usec / 1000 + now.tv_sec * 1000);
}
sqInt ioMicroMSecs(void)
{
/* return the highest available resolution of the millisecond clock */
return ioMSecs(); /* this already to the nearest millisecond */
}
sqLong ioMicroSeconds(void)
{
struct timeval now;
sqLong microSecs;
gettimeofday(&now, 0);
if ((now.tv_usec -= startUpTime.tv_usec) < 0) {
now.tv_usec += 1000000;
now.tv_sec -= 1;
}
now.tv_sec -= startUpTime.tv_sec;
microSecs= now.tv_usec;
microSecs= microSecs + now.tv_sec * 1000000;
return microSecs;
}
/* implementation of ioUtcWithOffset(), defined in config.h to
/* override default definition in src/vm/interp.h
*/
sqInt sqUnixUtcWithOffset(sqLong *microSeconds, int *offset)
{
struct timeval timeval;
if (gettimeofday(&timeval, NULL) == -1) return -1;
time_t seconds= timeval.tv_sec;
suseconds_t usec= timeval.tv_usec;
*microSeconds= seconds * 1000000 + usec;
#if defined(HAVE_TM_GMTOFF)
*offset= localtime(&seconds)->tm_gmtoff;
#else
{
struct tm *local= localtime(&seconds);
struct tm *gmt= gmtime(&seconds);
int d= local->tm_yday - gmt->tm_yday;
int h= ((d < -1 ? 24 : 1 < d ? -24 : d * 24) + local->tm_hour - gmt->tm_hour);
int m= h * 60 + local->tm_min - gmt->tm_min;
*offset= m * 60;
}
#endif
return 0;
}
time_t convertToSqueakTime(time_t unixTime)
{
#ifdef HAVE_TM_GMTOFF
unixTime+= localtime(&unixTime)->tm_gmtoff;
#else
# ifdef HAVE_TIMEZONE
unixTime+= ((daylight) * 60*60) - timezone;
# else
# error: cannot determine timezone correction
# endif
#endif
/* Squeak epoch is Jan 1, 1901. Unix epoch is Jan 1, 1970: 17 leap years
and 52 non-leap years later than Squeak. */
return unixTime + ((52*365UL + 17*366UL) * 24*60*60UL);
}
/* returns the local wall clock time */
sqInt ioSeconds(void)
{
return convertToSqueakTime(time(0));
}
/*** VM & Image File Naming ***/
/* copy src filename to target, if src is not an absolute filename,
* prepend the cwd to make target absolute
*/
static void pathCopyAbs(char *target, const char *src, size_t targetSize)
{
if (src[0] == '/')
strcpy(target, src);
else
{
0 == getcwd(target, targetSize);
strcat(target, "/");
strcat(target, src);
}
}
static void recordFullPathForVmName(const char *localVmName)
{
#if defined(__linux__)
char name[MAXPATHLEN+1];
int len;
if ((len= readlink("/proc/self/exe", name, sizeof(name))) > 0)
{
struct stat st;
name[len]= '\0';
if (!stat(name, &st))
localVmName= name;
}
#endif
/* get canonical path to vm */
if (realpath(localVmName, vmPath) == 0)
pathCopyAbs(vmPath, localVmName, sizeof(vmPath));
/* truncate vmPath to dirname */
{
int i= 0;
for (i= strlen(vmPath); i >= 0; i--)
if ('/' == vmPath[i])
{
vmPath[i+1]= '\0';
break;
}
}
}
static void recordFullPathForImageName(const char *localImageName)
{
struct stat s;
/* get canonical path to image */
if ((stat(localImageName, &s) == -1) || (realpath(localImageName, imageName) == 0))
pathCopyAbs(imageName, localImageName, sizeof(imageName));
}
/* vm access */
sqInt imageNameSize(void)
{
return strlen(imageName);
}
sqInt imageNameGetLength(sqInt sqImageNameIndex, sqInt length)
{
char *sqImageName= pointerForOop(sqImageNameIndex);
int count, i;
count= strlen(imageName);
count= (length < count) ? length : count;
/* copy the file name into the Squeak string */
for (i= 0; i < count; i++)
sqImageName[i]= imageName[i];
return count;
}
sqInt imageNamePutLength(sqInt sqImageNameIndex, sqInt length)
{
char *sqImageName= pointerForOop(sqImageNameIndex);
int count, i;
count= (IMAGE_NAME_SIZE < length) ? IMAGE_NAME_SIZE : length;
/* copy the file name into a null-terminated C string */
for (i= 0; i < count; i++)
imageName[i]= sqImageName[i];
imageName[count]= 0;
dpy->winSetName(imageName);
return count;
}
char *getImageName(void)
{
return imageName;
}
/*** VM Home Directory Path ***/
sqInt vmPathSize(void)
{
return strlen(vmPath);
}
sqInt vmPathGetLength(sqInt sqVMPathIndex, sqInt length)
{
char *stVMPath= pointerForOop(sqVMPathIndex);
int count, i;
count= strlen(vmPath);
count= (length < count) ? length : count;
/* copy the file name into the Squeak string */
for (i= 0; i < count; i++)
stVMPath[i]= vmPath[i];
return count;
}
/*** Profiling ***/
sqInt clearProfile(void) { return 0; }
sqInt dumpProfile(void) { return 0; }
sqInt startProfiling(void) { return 0; }
sqInt stopProfiling(void) { return 0; }
/*** power management ***/
sqInt ioDisablePowerManager(sqInt disableIfNonZero)
{
return true;
}
/*** Access to system attributes and command-line arguments ***/
/* OS_TYPE may be set in configure.in and passed via the Makefile */
#ifndef OS_TYPE
# ifdef UNIX
# define OS_TYPE "unix"
# else
# define OS_TYPE "unknown"
# endif
#endif
static char *getAttribute(sqInt id)
{
if (id < 0) /* VM argument */
{
if (-id < vmArgCnt)
return vmArgVec[-id];
}
else
switch (id)
{
case 0:
return vmName[0] ? vmName : vmArgVec[0];
case 1:
return imageName;
case 1001:
/* OS type: "unix", "win32", "mac", ... */
return OS_TYPE;
case 1002:
/* OS name: "solaris2.5" on unix, "win95" on win32, ... */
return VM_HOST_OS;
case 1003:
/* processor architecture: "68k", "x86", "PowerPC", ... */
return VM_HOST_CPU;
case 1004:
/* Interpreter version string */
return (char *)interpreterVersion;
case 1005:
/* window system name */
return dpy->winSystemName();
case 1006:
/* vm build string */
return VM_BUILD_STRING;
case 1007:
/* default sources directory */
return "/usr/share/squeak";
default:
if ((id - 2) < squeakArgCnt)
return squeakArgVec[id - 2];
}
success(false);
return "";
}
sqInt attributeSize(sqInt id)
{
return strlen(getAttribute(id));
}
sqInt getAttributeIntoLength(sqInt id, sqInt byteArrayIndex, sqInt length)
{
if (length > 0)
strncpy(pointerForOop(byteArrayIndex), getAttribute(id), length);
return 0;
}
/*** event handling ***/
sqInt inputEventSemaIndex= 0;
/* set asynchronous input event semaphore */
sqInt ioSetInputSemaphore(sqInt semaIndex)
{
if ((semaIndex == 0) || (noEvents == 1))
success(false);
else
inputEventSemaIndex= semaIndex;
return true;
}
/*** display functions ***/
sqInt ioFormPrint(sqInt bitsAddr, sqInt width, sqInt height, sqInt depth, double hScale, double vScale, sqInt landscapeFlag)
{
return dpy->ioFormPrint(bitsAddr, width, height, depth, hScale, vScale, landscapeFlag);
}
static int lastInterruptCheck= 0;
sqInt ioRelinquishProcessorForMicroseconds(sqInt us)
{
int now;
dpy->ioRelinquishProcessorForMicroseconds(us);
now= ioLowResMSecs();
if (now - lastInterruptCheck > (1000/25)) /* avoid thrashing intr checks from 1ms loop in idle proc */
{
setInterruptCheckCounter(-1000); /* ensure timely poll for semaphore activity */
lastInterruptCheck= now;
}
return 0;
}
sqInt ioBeep(void) { return dpy->ioBeep(); }
#if defined(IMAGE_DUMP)
static void emergencyDump(int quit)
{
extern sqInt preSnapshot(void);
extern sqInt postSnapshot(void);
extern void writeImageFile(sqInt);
char savedName[MAXPATHLEN];
char baseName[MAXPATHLEN];
char *term;
int dataSize, i;
strncpy(savedName, imageName, MAXPATHLEN);
strncpy(baseName, imageName, MAXPATHLEN);
if ((term= strrchr(baseName, '.')))
*term= '\0';
for (i= 0; ++i;)
{
struct stat sb;
sprintf(imageName, "%s-emergency-dump-%d.image", baseName, i);
if (stat(imageName, &sb))
break;
}
dataSize= preSnapshot();
writeImageFile(dataSize);
fprintf(stderr, "\n");
printCallStack();
fprintf(stderr, "\nTo recover valuable content from this image:\n");
fprintf(stderr, " squeak %s\n", imageName);
fprintf(stderr, "and then evaluate\n");
fprintf(stderr, " Smalltalk processStartUpList: true\n");
fprintf(stderr, "in a workspace. DESTROY the dumped image after recovering content!");
if (quit) abort();
strncpy(imageName, savedName, sizeof(imageName));
}
#endif
sqInt ioProcessEvents(void)
{
#if defined(IMAGE_DUMP)
if (dumpImageFile)
{
emergencyDump(0);
dumpImageFile= 0;
}
#endif
return dpy->ioProcessEvents();
}
sqInt ioScreenDepth(void) { return dpy->ioScreenDepth(); }
sqInt ioScreenSize(void) { return dpy->ioScreenSize(); }
sqInt ioSetCursorWithMask(sqInt cursorBitsIndex, sqInt cursorMaskIndex, sqInt offsetX, sqInt offsetY)
{
return dpy->ioSetCursorWithMask(cursorBitsIndex, cursorMaskIndex, offsetX, offsetY);
}
sqInt ioSetCursorARGB(sqInt cursorBitsIndex, sqInt extentX, sqInt extentY, sqInt offsetX, sqInt offsetY)
{
return dpy->ioSetCursorARGB(cursorBitsIndex, extentX, extentY, offsetX, offsetY);
}
sqInt ioSetCursor(sqInt cursorBitsIndex, sqInt offsetX, sqInt offsetY)
{
return ioSetCursorWithMask(cursorBitsIndex, 0, offsetX, offsetY);
}
sqInt ioSetFullScreen(sqInt fullScreen) { return dpy->ioSetFullScreen(fullScreen); }
sqInt ioForceDisplayUpdate(void) { return dpy->ioForceDisplayUpdate(); }
sqInt ioShowDisplay(sqInt dispBitsIndex, sqInt width, sqInt height, sqInt depth, sqInt l, sqInt r, sqInt t, sqInt b)
{
return dpy->ioShowDisplay(dispBitsIndex, width, height, depth, l, r, t, b);
}
sqInt ioHasDisplayDepth(sqInt i) { return dpy->ioHasDisplayDepth(i); }
sqInt ioSetDisplayMode(sqInt width, sqInt height, sqInt depth, sqInt fullscreenFlag)
{
return dpy->ioSetDisplayMode(width, height, depth, fullscreenFlag);
}
sqInt clipboardSize(void)
{
return dpy->clipboardSize();
}
sqInt clipboardWriteFromAt(sqInt count, sqInt byteArrayIndex, sqInt startIndex)
{
return dpy->clipboardWriteFromAt(count, byteArrayIndex, startIndex);
}
sqInt clipboardReadIntoAt(sqInt count, sqInt byteArrayIndex, sqInt startIndex)
{
return dpy->clipboardReadIntoAt(count, byteArrayIndex, startIndex);
}
char **clipboardGetTypeNames(void)
{
return dpy->clipboardGetTypeNames();
}
sqInt clipboardSizeWithType(char *typeName, int ntypeName)
{
return dpy->clipboardSizeWithType(typeName, ntypeName);
}
void clipboardWriteWithType(char *data, size_t nData, char *typeName, size_t nTypeNames, int isDnd, int isClaiming)
{
dpy->clipboardWriteWithType(data, nData, typeName, nTypeNames, isDnd, isClaiming);
}
sqInt ioGetButtonState(void) { return dpy->ioGetButtonState(); }
sqInt ioPeekKeystroke(void) { return dpy->ioPeekKeystroke(); }
sqInt ioGetKeystroke(void) { return dpy->ioGetKeystroke(); }
sqInt ioGetNextEvent(sqInputEvent *evt) { return dpy->ioGetNextEvent(evt); }
sqInt ioMousePoint(void) { return dpy->ioMousePoint(); }
/*** Drag and Drop ***/
sqInt dndOutStart(char *types, int ntypes) { return dpy->dndOutStart(types, ntypes); }
sqInt dndOutAcceptedType(char *type, int ntype) { return dpy->dndOutAcceptedType(type, ntype); }
void dndOutSend(char *bytes, int nbytes) { dpy->dndOutSend(bytes, nbytes); }
/*** OpenGL ***/
int verboseLevel= 1;
struct SqDisplay *ioGetDisplayModule(void) { return dpy; }
void *ioGetDisplay(void) { return dpy->ioGetDisplay(); }
void *ioGetWindow(void) { return dpy->ioGetWindow(); }
sqInt ioGLinitialise(void) { return dpy->ioGLinitialise(); }
sqInt ioGLcreateRenderer(glRenderer *r, sqInt x, sqInt y, sqInt w, sqInt h, sqInt flags)
{
return dpy->ioGLcreateRenderer(r, x, y, w, h, flags);
}
sqInt ioGLmakeCurrentRenderer(glRenderer *r) { return dpy->ioGLmakeCurrentRenderer(r); }
void ioGLdestroyRenderer(glRenderer *r) { dpy->ioGLdestroyRenderer(r); }
void ioGLswapBuffers(glRenderer *r) { dpy->ioGLswapBuffers(r); }
void ioGLsetBufferRect(glRenderer *r, sqInt x, sqInt y, sqInt w, sqInt h)
{
dpy->ioGLsetBufferRect(r, x, y, w, h);
}
sqInt primitivePluginBrowserReady(void) { return dpy->primitivePluginBrowserReady(); }
sqInt primitivePluginRequestURLStream(void) { return dpy->primitivePluginRequestURLStream(); }
sqInt primitivePluginRequestURL(void) { return dpy->primitivePluginRequestURL(); }
sqInt primitivePluginPostURL(void) { return dpy->primitivePluginPostURL(); }
sqInt primitivePluginRequestFileHandle(void) { return dpy->primitivePluginRequestFileHandle(); }
sqInt primitivePluginDestroyRequest(void) { return dpy->primitivePluginDestroyRequest(); }
sqInt primitivePluginRequestState(void) { return dpy->primitivePluginRequestState(); }
/*** errors ***/
static void outOfMemory(void)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
static void sigsegv(int ignore)
{
/* error("Segmentation fault"); */
static int printingStack= 0;
printf("\nSegmentation fault\n\n");
if (!printingStack)
{
printingStack= 1;
printCallStack();
}
abort();
}
#if defined(IMAGE_DUMP)
static void sighup(int ignore)
{
dumpImageFile= 1;
}
static void sigquit(int ignore)
{
emergencyDump(1);
}
#endif
/*** modules ***/
#include "SqModule.h"
struct SqModule *displayModule= 0;
struct SqModule *soundModule= 0;
struct SqModule *modules= 0;
#define modulesDo(M) for (M= modules; M; M= M->next)
struct moduleDescription
{
struct SqModule **addr;
char *type;
char *name;
};
static struct moduleDescription moduleDescriptions[]=
{
{ &displayModule, "display", "X11" }, /*** NO DEFAULT ***/
{ &displayModule, "display", "fbdev" }, /*** NO DEFAULT ***/
{ &displayModule, "display", "null" }, /*** NO DEFAULT ***/
{ &displayModule, "display", "custom" }, /*** NO DEFAULT ***/
{ &soundModule, "sound", "NAS" }, /*** NO DEFAULT ***/
{ &soundModule, "sound", "custom" }, /*** NO DEFAULT ***/
/* when adding an entry above be sure to change the defaultModules offset below */
{ &displayModule, "display", "Quartz" }, /* defaults... */
{ &soundModule, "sound", "OSS" },
{ &soundModule, "sound", "MacOSX" },
{ &soundModule, "sound", "Sun" },
{ &soundModule, "sound", "pulse" },
{ &soundModule, "sound", "ALSA" },
{ &soundModule, "sound", "null" },
{ 0, 0, 0 }
};
static struct moduleDescription *defaultModules= moduleDescriptions + 6;
struct SqModule *queryLoadModule(char *type, char *name, int query)
{
char modName[MAXPATHLEN], itfName[32];
struct SqModule *module= 0;
void *itf= 0;
sprintf(modName, "vm-%s-%s", type, name);
#ifdef DEBUG_MODULES
printf("looking for module %s\n", modName);
#endif
modulesDo (module)
if (!strcmp(module->name, modName))
return module;
sprintf(itfName, "%s_%s", type, name);
itf= ioFindExternalFunctionIn(itfName, ioLoadModule(0));
if (!itf)
{
void *handle= ioLoadModule(modName);
if (handle)
itf= ioFindExternalFunctionIn(itfName, handle);
else
if (!query)
{
fprintf(stderr, "could not find module %s\n", modName);
return 0;
}
}
if (itf)
{
module= (struct SqModule *)itf;
if (SqModuleVersion != module->version)
{
fprintf(stderr, "module %s version %x does not have required version %x\n",
modName, module->version, SqModuleVersion);
abort();
}
module->next= modules;
modules= module;
module->name= strdup(modName);
module->parseEnvironment();
return module;
}
if (!query)
fprintf(stderr, "could not find interface %s in module %s\n", itfName, modName);
return 0;
}
struct SqModule *queryModule(char *type, char *name)
{
return queryLoadModule(type, name, 1);
}
struct SqModule *loadModule(char *type, char *name)
{
return queryLoadModule(type, name, 0);
}
struct SqModule *requireModule(char *type, char *name)
{
struct SqModule *m= loadModule(type, name);
if (!m) abort();
return m;
}
static char *canonicalModuleName(char *name)
{
struct moduleDescription *md;
for (md= moduleDescriptions; md->addr; ++md)
if (!strcasecmp(name, md->name))
return md->name;
if (!strcasecmp(name, "none"))
return "null";
return name;
}
static void requireModuleNamed(char *type) /*** NOTE: MODIFIES THE ARGUMENT! ***/
{
if (!strncmp(type, "vm-", 3)) type+= 3;
else if (!strncmp(type, "-vm-", 4)) type+= 4;
/* we would like to use strsep() here, but neither OSF1 nor Solaris have it */
{
char *name= type;
while (*name && ('-' != *name) && ('=' != *name))
++name;
if (*name) *name++= '\0';
# if defined(DEBUG_MODULES)
printf("type %s name %s\n", type, name);
# endif
{
struct SqModule **addr= 0, *module= 0;
if (!strcmp(type, "display")) addr= &displayModule;
else if (!strcmp(type, "sound")) addr= &soundModule;
/* let unknown types through to the following to generate a more informative diagnostic */
name= canonicalModuleName(name);
module= requireModule(type, name);
if (!addr)
{
fprintf(stderr, "this cannot happen\n");
abort();
}
*addr= module;
}
}
}
static void requireModulesNamed(char *specs)
{
char *vec= strdup(specs);
char *pos= vec;
while (*pos)
{
char *end= pos;
while (*end && (' ' <= *end) && (',' != *end))
++end;
if (*end) *end++= '\0';
requireModuleNamed(pos);
pos= end;
}
free(vec);
}
static void checkModuleVersion(struct SqModule *module, int required, int actual)
{
if (required != actual)
{
fprintf(stderr, "module %s interface version %x does not have required version %x\n",
module->name, actual, required);
abort();
}
}
static void loadImplicit(struct SqModule **addr, char *evar, char *type, char *name)
{
if ((!*addr) && getenv(evar) && !(*addr= queryModule(type, name)))
{
fprintf(stderr, "could not find %s driver vm-%s-%s; either:\n", type, type, name);
fprintf(stderr, " - check that %s/vm-%s-%s.so exists, or\n", vmLibDir, type, name);
fprintf(stderr, " - use the '-plugins <path>' option to tell me where it is, or\n");
fprintf(stderr, " - remove %s from your environment.\n", evar);
abort();
}
}
static void loadModules(void)
{
loadImplicit(&displayModule, "DISPLAY", "display", "X11");
loadImplicit(&soundModule, "AUDIOSERVER", "sound", "NAS");
{
struct moduleDescription *md;
for (md= defaultModules; md->addr; ++md)
if (!*md->addr)
if ((*md->addr= queryModule(md->type, md->name)))
# if defined(DEBUG_MODULES)
fprintf(stderr, "squeak: %s driver defaulting to vm-%s-%s\n", md->type, md->type, md->name)
# endif
;
}
if (!displayModule)
{
fprintf(stderr, "squeak: could not find any display driver\n");
abort();
}
if (!soundModule)
{
fprintf(stderr, "squeak: could not find any sound driver\n");
abort();
}
dpy= (struct SqDisplay *)displayModule->makeInterface();
snd= (struct SqSound *)soundModule ->makeInterface();
checkModuleVersion(displayModule, SqDisplayVersion, dpy->version);
checkModuleVersion(soundModule, SqSoundVersion, snd->version);
}
/* built-in main vm module */
static int strtobkm(const char *str)
{
char *suffix;
int value= strtol(str, &suffix, 10);
switch (*suffix)
{
case 'k': case 'K':
value*= 1024;
break;
case 'm': case 'M':
value*= 1024*1024;
break;
}
return value;
}
static int jitArgs(char *str)
{
char *endptr= str;
int args= 3; /* default JIT mode = fast compiler */
if (*str == '\0') return args;
if (*str != ',')
args= strtol(str, &endptr, 10); /* mode */
while (*endptr == ',') /* [,debugFlag]* */
args|= (1 << (strtol(endptr + 1, &endptr, 10) + 8));
return args;
}
# include <locale.h>
static void vm_parseEnvironment(void)
{
char *ev= setlocale(LC_CTYPE, "");
if (ev)
setLocaleEncoding(ev);
else
fprintf(stderr, "setlocale() failed (check values of LC_CTYPE, LANG and LC_ALL)\n");
if (documentName)
strcpy(shortImageName, documentName);
else if ((ev= getenv("SQUEAK_IMAGE")))
strcpy(shortImageName, ev);
else
strcpy(shortImageName, "squeak.image");
if ((ev= getenv("SQUEAK_MEMORY"))) extraMemory= strtobkm(ev);
if ((ev= getenv("SQUEAK_MMAP"))) useMmap= strtobkm(ev);
if ((ev= getenv("SQUEAK_PLUGINS"))) squeakPlugins= strdup(ev);
if ((ev= getenv("SQUEAK_NOEVENTS"))) noEvents= 1;
if ((ev= getenv("SQUEAK_NOTIMER"))) useItimer= 0;
if ((ev= getenv("SQUEAK_JIT"))) useJit= jitArgs(ev);
if ((ev= getenv("SQUEAK_PROCS"))) jitProcs= atoi(ev);
if ((ev= getenv("SQUEAK_MAXPIC"))) jitMaxPIC= atoi(ev);
if ((ev= getenv("SQUEAK_ENCODING"))) setEncoding(&sqTextEncoding, ev);
if ((ev= getenv("SQUEAK_PATHENC"))) setEncoding(&uxPathEncoding, ev);
if ((ev= getenv("SQUEAK_TEXTENC"))) setEncoding(&uxTextEncoding, ev);
if ((ev= getenv("SQUEAK_VM"))) requireModulesNamed(ev);
}
static void usage(void);
static void versionInfo(void);
static int parseModuleArgument(int argc, char **argv, struct SqModule **addr, char *type, char *name)
{
if (*addr)
{
fprintf(stderr, "option '%s' conflicts with previously-loaded module '%s'\n", *argv, (*addr)->name);
exit(1);
}
*addr= requireModule(type, name);
return (*addr)->parseArgument(argc, argv);
}
static int vm_parseArgument(int argc, char **argv)
{
/* deal with arguments that implicitly load modules */
if (!strncmp(argv[0], "-psn_", 5))
{
displayModule= requireModule("display", "Quartz");
return displayModule->parseArgument(argc, argv);
}
if ((!strcmp(argv[0], "-vm")) && (argc > 1))
{
requireModulesNamed(argv[1]);
return 2;
}
if (!strncmp(argv[0], "-vm-", 4))
{
requireModulesNamed(argv[0] + 4);
return 1;
}
/* legacy compatibility */ /*** XXX to be removed at some time ***/
# define moduleArg(arg, type, name) \
if (!strcmp(argv[0], arg)) \
return parseModuleArgument(argc, argv, &type##Module, #type, name);
moduleArg("-nodisplay", display, "null");
moduleArg("-display", display, "X11");
moduleArg("-headless", display, "X11");
moduleArg("-quartz", display, "Quartz");
moduleArg("-nosound", sound, "null");
# undef moduleArg
/* vm arguments */
if (!strcmp(argv[0], "-help")) { usage(); return 1; }
else if (!strcmp(argv[0], "-noevents")) { noEvents = 1; return 1; }
else if (!strcmp(argv[0], "-nomixer")) { noSoundMixer = 1; return 1; }
else if (!strcmp(argv[0], "-notimer")) { useItimer = 0; return 1; }
else if (!strncmp(argv[0],"-jit", 4)) { useJit = jitArgs(argv[0]+4); return 1; }
else if (!strcmp(argv[0], "-nojit")) { useJit = 0; return 1; }
else if (!strcmp(argv[0], "-spy")) { withSpy = 1; return 1; }
else if (!strcmp(argv[0], "-version")) { versionInfo(); return 1; }
/* option requires an argument */
else if (argc > 1)
{
if (!strcmp(argv[0], "-procs")) { jitProcs= atoi(argv[1]); return 2; }
else if (!strcmp(argv[0], "-maxpic")) { jitMaxPIC= atoi(argv[1]); return 2; }
else if (!strcmp(argv[0], "-memory")) { extraMemory= strtobkm(argv[1]); return 2; }
else if (!strcmp(argv[0], "-mmap")) { useMmap= strtobkm(argv[1]); return 2; }
else if (!strcmp(argv[0], "-plugins")) { squeakPlugins= strdup(argv[1]); return 2; }
else if (!strcmp(argv[0], "-encoding")) { setEncoding(&sqTextEncoding, argv[1]); return 2; }
else if (!strcmp(argv[0], "-pathenc")) { setEncoding(&uxPathEncoding, argv[1]); return 2; }
else if (!strcmp(argv[0], "-textenc"))
{
char *buf= (char *)malloc(strlen(argv[1]) + 1);
int len, i;
strcpy(buf, argv[1]);
len= strlen(buf);
for (i= 0; i < len; ++i)
buf[i]= toupper(buf[i]);
if ((!strcmp(buf, "UTF8")) || (!strcmp(buf, "UTF-8")))
textEncodingUTF8= 1;
else
setEncoding(&uxTextEncoding, buf);
free(buf);
return 2;
}
}
return 0; /* option not recognised */
}
static void vm_printUsage(void)
{
printf("\nCommon <option>s:\n");
printf(" -encoding <enc> set the internal character encoding (default: MacRoman)\n");
printf(" -help print this help message, then exit\n");
/*printf(" -jit enable the dynamic compiler (if available)\n");*/
printf(" -memory <size>[mk] use fixed heap size (added to image size)\n");
printf(" -mmap <size>[mk] limit dynamic heap size (default: %dm)\n", DefaultMmapSize);
printf(" -noevents disable event-driven input support\n");
printf(" -notimer disable interval timer for low-res clock \n");
printf(" -pathenc <enc> set encoding for pathnames (default: UTF-8)\n");
printf(" -plugins <path> specify alternative plugin location (see manpage)\n");
printf(" -textenc <enc> set encoding for external text (default: UTF-8)\n");
printf(" -version print version information, then exit\n");
printf(" -vm-<sys>-<dev> use the <dev> driver for <sys> (see below)\n");
#if 1
printf("Deprecated:\n");
printf(" -display <dpy> quivalent to '-vm-display-X11 -display <dpy>'\n");
printf(" -headless quivalent to '-vm-display-X11 -headless'\n");
printf(" -nodisplay quivalent to '-vm-display-null'\n");
printf(" -nosound quivalent to '-vm-sound-null'\n");
printf(" -quartz quivalent to '-vm-display-Quartz'\n");
#endif
}
static void vm_printUsageNotes(void)
{
printf(" If `-memory' is not specified then the heap will grow dynamically.\n");
printf(" <argument>s are ignored, but are processed by the Squeak image.\n");
printf(" The first <argument> normally names a Squeak `script' to execute.\n");
printf(" Precede <arguments> by `--' to use default image.\n");
}
static void *vm_makeInterface(void)
{
fprintf(stderr, "this cannot happen\n");
abort();
}
SqModuleDefine(vm, Module);
/*** options processing ***/
static void usage(void)
{
struct SqModule *m= 0;
printf("Usage: %s [<option>...] [<imageName> [<argument>...]]\n", argVec[0]);
printf(" %s [<option>...] -- [<argument>...]\n", argVec[0]);
sqIgnorePluginErrors= 1;
{
struct moduleDescription *md;
for (md= moduleDescriptions; md->addr; ++md)
queryModule(md->type, md->name);
}
modulesDo(m)
m->printUsage();
if (useJit)
{
printf("\njit <option>s:\n");
printf(" -align <n> align functions at <n>-byte boundaries\n");
printf(" -jit<o>[,<d>...] set optimisation [and debug] levels\n");
printf(" -maxpic <n> set maximum PIC size to <n> entries\n");
printf(" -procs <n> allow <n> concurrent volatile processes\n");
printf(" -spy enable the system spy\n");
}
printf("\nNotes:\n");
printf(" <imageName> defaults to `squeak.image'.\n");
modulesDo(m)
m->printUsageNotes();
printf("\nAvailable drivers:\n");
for (m= modules; m->next; m= m->next)
printf(" %s\n", m->name);
exit(1);
}
char *getVersionInfo(int verbose)
{
extern int vm_serial;
extern char *vm_date, *cc_version, *ux_version;
char *info= (char *)malloc(4096);
info[0]= '\0';
if (verbose)
sprintf(info+strlen(info), "Squeak VM version: ");
sprintf(info+strlen(info), "%s #%d", VM_VERSION_INFO, vm_serial);
#if defined(USE_XSHM)
sprintf(info+strlen(info), " XShm");
#endif
sprintf(info+strlen(info), " %s %s\n", vm_date, cc_version);
#if 0
if (verbose)
sprintf(info+strlen(info), "Built from: ");
sprintf(info+strlen(info), "%s\n", interpreterVersion);
#endif
if (verbose)
sprintf(info+strlen(info), "Build host: ");
sprintf(info+strlen(info), "%s\n", ux_version);
sprintf(info+strlen(info), "plugin path: %s [default: %s]\n", squeakPlugins, vmPath);
return info;
}
static void versionInfo(void)
{
printf("%s", getVersionInfo(0));
exit(0);
}
static void parseArguments(int argc, char **argv)
{
# define skipArg() (--argc, argv++)
# define saveArg() (vmArgVec[vmArgCnt++]= *skipArg())
saveArg(); /* vm name */
while ((argc > 0) && (**argv == '-')) /* more options to parse */
{
struct SqModule *m= 0;
int n= 0;
if (!strcmp(*argv, "--")) /* escape from option processing */
break;
modulesDo (m)
if ((n= m->parseArgument(argc, argv)))
break;
# ifdef DEBUG_IMAGE
printf("parseArgument n = %d\n", n);
# endif
if (n == 0) /* option not recognised */
{
fprintf(stderr, "unknown option: %s\n", argv[0]);
usage();
}
while (n--)
saveArg();
}
if (!argc)
return;
if (!strcmp(*argv, "--"))
skipArg();
else /* image name */
{
if (!documentName)
strcpy(shortImageName, saveArg());
if (!strstr(shortImageName, ".image"))
strcat(shortImageName, ".image");
}
/* save remaining arguments as Squeak arguments */
while (argc > 0)
squeakArgVec[squeakArgCnt++]= *skipArg();
# undef saveArg
# undef skipArg
}
/*** main ***/
static void imageNotFound(char *imageName)
{
/* image file is not found */
fprintf(stderr,
"Could not open the Squeak image file `%s'.\n"
"\n"
"There are three ways to open a Squeak image file. You can:\n"
" 1. Put copies of the default image and changes files in this directory.\n"
" 2. Put the name of the image file on the command line when you\n"
" run squeak (use the `-help' option for more information).\n"
" 3. Set the environment variable SQUEAK_IMAGE to the name of the image\n"
" that you want to use by default.\n"
"\n"
"For more information, type: `man squeak' (without the quote characters).\n",
imageName);
exit(1);
}
void imgInit(void)
{
/* read the image file and allocate memory for Squeak heap */
for (;;)
{
FILE *f= 0;
struct stat sb;
char imageName[MAXPATHLEN];
sq2uxPath(shortImageName, strlen(shortImageName), imageName, 1000, 1);
if (( (-1 == stat(imageName, &sb)))
|| ( 0 == (f= fopen(imageName, "r"))))
{
if (dpy->winImageFind(shortImageName, sizeof(shortImageName)))
continue;
dpy->winImageNotFound();
imageNotFound(shortImageName);
}
# if 0
{
int fd= open(imageName, O_RDONLY);
if (fd < 0) abort();
# ifdef DEBUG_IMAGE
printf("fstat(%d) => %d\n", fd, fstat(fd, &sb));
# endif
}
# endif
recordFullPathForImageName(shortImageName); /* full image path */
if (extraMemory)
useMmap= 0;
else
extraMemory= DefaultHeapSize * 1024 *1024;
# ifdef DEBUG_IMAGE
printf("image size %d + heap size %d (useMmap = %d)\n", (int)sb.st_size, extraMemory, useMmap);
# endif
extraMemory += (int)sb.st_size;
readImageFromFileHeapSize(f, extraMemory);
sqImageFileClose(f);
break;
}
}
#if defined(__GNUC__) && ( defined(i386) || defined(__i386) || defined(__i386__) \
|| defined(i486) || defined(__i486) || defined (__i486__) \
|| defined(intel) || defined(x86) || defined(i86pc) )
static void fldcw(unsigned int cw)
{
__asm__("fldcw %0" :: "m"(cw));
}
#else
# define fldcw(cw)
#endif
#if defined(__GNUC__) && ( defined(ppc) || defined(__ppc) || defined(__ppc__) \
|| defined(POWERPC) || defined(__POWERPC) || defined (__POWERPC__) )
void mtfsfi(unsigned long long fpscr)
{
__asm__("lfd f0, %0" :: "m"(fpscr));
__asm__("mtfsf 0xff, f0");
}
#else
# define mtfsfi(fpscr)
#endif
int main(int argc, char **argv, char **envp)
{
fldcw(0x12bf); /* signed infinity, round to nearest, REAL8, disable intrs, disable signals */
mtfsfi(0); /* disable signals, IEEE mode, round to nearest */
/* Make parameters global for access from plugins */
argCnt= argc;
argVec= argv;
envVec= envp;
#ifdef DEBUG_IMAGE
{
int i= argc;
char **p= argv;
while (i--)
printf("arg: %s\n", *p++);
}
#endif
/* Allocate arrays to store copies of pointers to command line
arguments. Used by getAttributeIntoLength(). */
if ((vmArgVec= calloc(argc + 1, sizeof(char *))) == 0)
outOfMemory();
if ((squeakArgVec= calloc(argc + 1, sizeof(char *))) == 0)
outOfMemory();
signal(SIGSEGV, sigsegv);
#if defined(__alpha__) && defined(__osf__)
/* disable printing of unaligned access exceptions */
{
int buf[2]= { SSIN_UACPROC, UAC_NOPRINT };
if (setsysinfo(SSI_NVPAIRS, buf, 1, 0, 0, 0) < 0)
{
perror("setsysinfo(UAC_NOPRINT)");
}
}
#endif
#if defined(HAVE_TZSET)
tzset(); /* should _not_ be necessary! */
#endif
recordFullPathForVmName(argv[0]); /* full vm path */
squeakPlugins= vmPath; /* default plugin location is VM directory */
sqIgnorePluginErrors= 1;
if (!modules)
modules= &vm_Module;
vm_Module.parseEnvironment();
parseArguments(argc, argv);
if ((!dpy) || (!snd))
loadModules();
sqIgnorePluginErrors= 0;
#if defined(DEBUG_MODULES)
printf("displayModule %p %s\n", displayModule, displayModule->name);
if (soundModule)
printf("soundModule %p %s\n", soundModule, soundModule->name);
#endif
if (!realpath(argv[0], vmName))
vmName[0]= 0; /* full VM name */
#ifdef DEBUG_IMAGE
printf("vmName: %s -> %s\n", argv[0], vmName);
printf("viName: %s\n", shortImageName);
printf("documentName: %s\n", documentName);
#endif
initTimers();
aioInit();
dpy->winInit();
imgInit();
dpy->winOpen();
#if defined(HAVE_DLOPEN)
if (useJit)
{
/* first try to find an internal dynamic compiler... */
void *handle= ioLoadModule(0);
void *comp= ioFindExternalFunctionIn("j_interpret", handle);
/* ...and if that fails... */
if (comp == 0)
{
/* ...try to find an external one */
handle= ioLoadModule("SqueakCompiler");
if (handle != 0)
comp= ioFindExternalFunctionIn("j_interpret", handle);
}
if (comp)
{
((void (*)(void))comp)();
fprintf(stderr, "handing control back to interpret() -- have a nice day\n");
}
else
printf("could not find j_interpret\n");
exit(1);
}
#endif
#if defined(IMAGE_DUMP)
signal(SIGHUP, sighup);
signal(SIGQUIT, sigquit);
#endif
/* run Squeak */
if (runInterpreter)
interpret();
/* we need these, even if not referenced from main executable */
(void)sq2uxPath;
(void)ux2sqPath;
sqDebugAnchor();
return 0;
}
sqInt ioExit(void)
{
dpy->winExit();
exit(0);
}
#if defined(DARWIN)
# include "mac-alias.c"
#endif
/* Copy aFilenameString to aCharBuffer and optionally resolveAlias (or
symbolic link) to the real path of the target. Answer 0 if
successful of -1 to indicate an error. Assume aCharBuffer is at
least MAXPATHLEN bytes long. Note that MAXSYMLINKS is a lower bound
on the (potentially unlimited) number of symlinks allowed in a
path, but calling sysconf() seems like overkill. */
sqInt sqGetFilenameFromString(char *aCharBuffer, char *aFilenameString, sqInt filenameLength, sqInt resolveAlias)
{
int numLinks= 0;
struct stat st;
sq2uxPath(aFilenameString, filenameLength, aCharBuffer, MAXPATHLEN, 1);
if (resolveAlias)
for (;;) /* aCharBuffer might refer to link or alias */
{
if (!lstat(aCharBuffer, &st) && S_ISLNK(st.st_mode)) /* symlink */
{
char linkBuffer[MAXPATHLEN], *linkPart= 0;
if (++numLinks > MAXSYMLINKS)
return -1; /* too many levels of indirection */
filenameLength= readlink(aCharBuffer, linkBuffer, MAXPATHLEN);
if ((filenameLength < 0) || (filenameLength >= MAXPATHLEN))
return -1; /* link unavailable or path too long */
linkBuffer[filenameLength]= 0;
if ((linkBuffer[0] == '/') || (!(linkPart= strrchr(aCharBuffer, '/'))))
linkPart= aCharBuffer; /* target is absolute or source is relative: target replaces entire source */
else
linkPart += 1; /* target is relative: target replaces basename component of source */
if (linkPart - aCharBuffer + 1 + filenameLength + 1 >= MAXPATHLEN)
return -1; /* result path too long */
strcpy(linkPart, linkBuffer);
continue;
}
# if defined(DARWIN)
if (isMacAlias(aCharBuffer))
{
if ((++numLinks > MAXSYMLINKS) || !resolveMacAlias(aCharBuffer, aCharBuffer, MAXPATHLEN))
return -1; /* too many levels or bad alias */
continue;
}
# endif
break; /* target is no longer a symlink or alias */
}
return 0;
}
sqInt ioGatherEntropy(char *buffer, sqInt bufSize)
{
int fd, count= 0;
if ((fd= open("/dev/urandom", O_RDONLY)) < 0)
return 0;
while (count < bufSize)
{
int n;
if ((n= read(fd, buffer + count, bufSize)) < 1)
break;
count += n;
}
close(fd);
return count == bufSize;
}
|