preserve.c 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. /* File preserve.c Copyright (c) Codemist Ltd, 1990-99 */
  2. #include <stdarg.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "machine.h"
  6. #include "version.h"
  7. #include "tags.h"
  8. #include "cslerror.h"
  9. #include "externs.h"
  10. #include "arith.h"
  11. #include "read.h"
  12. #include "stream.h"
  13. #ifdef TIMEOUT
  14. #include "timeout.h"
  15. #endif
  16. /* Signature: 7444bac7 07-Mar-2000 */
  17. /*
  18. * I perform file compression when making checkpoint images.
  19. * This is achieved by having procedure Cfwrite and Cfread which
  20. * are much like fwrite and fread but which are entitled to use a
  21. * squashed format on the external medium. It is fairly important that
  22. * Cfread should be reasonably fast - Cfwrite is just used by (preserve)
  23. * and is not so critical. The overall compression strategy implemented
  24. * here is a variant on LZ - the compressed file is made up of 12-bit
  25. * characters. The first 256 codes stand for bytes present in the original
  26. * data, while the remaining codes get allocated to stand for pairs of
  27. * characters found adjacent in the data. Initial experiments show that
  28. * the simple version of the idea implemented here squashes binary image
  29. * files to about 60% of their original size, while more elaborate
  30. * schemes can not do MUCH better.
  31. */
  32. int32 compression_worth_while = 128;
  33. #ifndef DEMO_MODE
  34. static void Cfwrite(char *a, int32 size)
  35. {
  36. /*
  37. * I keep a table showing how single 12-bit characters in the
  38. * compressed file map onto character-pairs in the original. The
  39. * field "where" in this table is notionally a quite separate
  40. * array, used to give hashed access to compressed codes. The table is
  41. * only needed at startup time and when I am dumping a checkpoint file -
  42. * in each case nothing else is one the stack, so since the table is
  43. * only 16/32 Kbytes or so I allocate it on the stack: this code is only
  44. * used when the stack is otherwise almost empty. Well actually
  45. * with the introduction of the (checkpoint) function that can be
  46. * used to dump images whatever else is going on the stack may not
  47. * be so empty after all. I will nevertheless continue to allocate
  48. * my buffers on it.
  49. */
  50. unsigned char pair_c[CODESIZE]; /* 4 Kbytes */
  51. unsigned short int pair_prev[CODESIZE], pair_where[CODESIZE];
  52. /* 16 Kbytes */
  53. unsigned char *p = (unsigned char *)a;
  54. int32 n = size, i;
  55. unsigned32 prev1;
  56. int hash, step, half;
  57. unsigned int next_code, prev, c;
  58. if (size < compression_worth_while)
  59. { if (size != 0) Iwrite(a, size);
  60. return;
  61. }
  62. /*
  63. * Clear the hash table and indicate that the next code to allocate is 256
  64. */
  65. memset(pair_where, 0, sizeof(pair_where));
  66. next_code = 256;
  67. /*
  68. * I deal with the first two characters by hand since they can not be
  69. * subject to compression. After these first two I can apply uniform
  70. * processing.
  71. */
  72. prev = *p++;
  73. c = *p++;
  74. /*
  75. * The hash function I use is not especially scientific, but a couple of
  76. * exclusive-or operations and a shift will be cheap to compute, and I
  77. * can eventually expect prev to be fairly evenly distributed, while the
  78. * distribution of c depends a lot on what sort of data is in the file.
  79. */
  80. hash = prev ^ c ^ (c << 5);
  81. prev1 = ((unsigned32)hash << 20) | ((unsigned32)prev << 8) | c;
  82. Iputc(prev >> 4);
  83. half = prev & 0xf;
  84. prev = c;
  85. for (i=2; i<n; i++)
  86. { c = *p++;
  87. hash = (prev ^ c ^ (c << 5)) & 0xfff;
  88. step = (prev - (c << 4)) | 1;
  89. /*
  90. * I compute a hash value, and also a secondary hash to be used when
  91. * making repeated probes. Since the table has size that is a power of
  92. * two I will be OK provided by step is an odd number. When I am finished
  93. * the table will have 4096-256 entries in it, i.e. it will be 94% full,
  94. * so access to it will take about 16 probes to discover that some
  95. * item is not present.
  96. */
  97. for (;;)
  98. { int where = pair_where[hash];
  99. if (where == 0) break;
  100. if (pair_prev[where] == prev &&
  101. pair_c[where] == c)
  102. { prev = where; /* squash 2 chars together */
  103. hash = -1; /* set a flag to indicate it was done */
  104. break;
  105. }
  106. hash = (hash + step) & 0xfff;
  107. }
  108. if (hash >= 0)
  109. {
  110. /*
  111. * There is a delicacy here - so that the uncompression process can
  112. * build its decoding tables on the fly I must delay entering items into
  113. * the compression tables by about one character of output. This is
  114. * achieved by keeping details of what is to be inserted stored in the
  115. * variable "prev1", which is activated here.
  116. * When all 4096 codes have been allocated I just flush out the
  117. * table and start afresh. A scheme that started with 9-bit chunks and
  118. * grew up to use longer ones up to (say) 15 or 16 bits could give
  119. * significantly better compression, but at the cost of both more
  120. * workspace here and (what is more to the point) seriously extra
  121. * overhead picking bit-fields of variable length out of the stream of
  122. * bytes in files.
  123. */
  124. if (next_code >= CODESIZE)
  125. { memset(pair_where, 0, sizeof(pair_where));
  126. next_code = 256;
  127. }
  128. else
  129. { pair_where[prev1 >> 20] = next_code;
  130. pair_prev[next_code] =
  131. (unsigned short int)(prev1 >> 8) & 0xfff;
  132. pair_c[next_code] = (unsigned char)prev1;
  133. next_code++;
  134. }
  135. /*
  136. * Now the mess of collecting 12 bit items and paching them into sequences
  137. * of 8 bit bytes.
  138. */
  139. if (half < 0)
  140. { Iputc(prev >> 4);
  141. half = prev & 0xf;
  142. }
  143. else
  144. { Iputc((half << 4) | ((prev >> 8) & 0xf));
  145. Iputc(prev);
  146. half = -1;
  147. }
  148. /*
  149. * record the information that the decoder will in due course see.
  150. */
  151. prev1 = ((unsigned32)hash << 20) | ((unsigned32)prev << 8) | c;
  152. prev = c;
  153. }
  154. }
  155. /*
  156. * Now I have to flush out the final buffered character
  157. */
  158. if (half < 0)
  159. { Iputc(prev >> 4);
  160. Iputc(prev << 4);
  161. }
  162. else
  163. { Iputc((half << 4) | ((prev >> 8) & 0xf));
  164. Iputc(prev);
  165. }
  166. }
  167. #endif /* DEMO_MODE */
  168. /*
  169. * These routines pack multiple binary files into one big one. The
  170. * good effect is that I expect fseek to be faster than fopen, and as
  171. * a result accessing fasl files will be faster. The bad news is that
  172. * when I update files I may need to compact them, and doing so will
  173. * be very tedious. In this model I do not permit arbitrary interleaving
  174. * of read and write operations.
  175. */
  176. static void set_dirused(directory_header *h, int v)
  177. {
  178. h->dirused = v & 0xff;
  179. h->dirext = (h->dirext & 0xf0) + ((v>>8) & 0x0f);
  180. }
  181. static directory empty_directory =
  182. {
  183. {'C', MIDDLE_INITIAL, 'L', IMAGE_FORMAT_VERSION,
  184. 0, 0, 0, 0, 0},
  185. NULL,
  186. "EmptyFile",
  187. {"\nEmpty ** *** not dated *** **"}
  188. };
  189. /*
  190. * In a way that may look clumsy I store file offsets and lengths as
  191. * sequences of three or four characters. The object of this
  192. * explicit control over memory layout is so that directories produced by
  193. * this code have a layout that is not sensitive to the byte-order used
  194. * by the computer involved. I also put a few newline characters into
  195. * my directory structure so that if one uses an ordinary text editor to
  196. * inspect an image file the set of modules and their datestamps should
  197. * be easily visible.
  198. */
  199. static int32 bits32(char *v)
  200. {
  201. int32 r = v[3] & 0xff;
  202. r = (r << 8) | (v[2] & 0xff);
  203. r = (r << 8) | (v[1] & 0xff);
  204. return (r << 8) | (v[0] & 0xff);
  205. }
  206. static int32 bits24(char *v)
  207. {
  208. int32 r = v[2] & 0xff;
  209. r = (r << 8) | (v[1] & 0xff);
  210. return (r << 8) | (v[0] & 0xff);
  211. }
  212. static void setbits32(char *v, int32 r)
  213. {
  214. *v++ = (char)r;
  215. *v++ = (char)(r >> 8);
  216. *v++ = (char)(r >> 16);
  217. *v = (char)(r >> 24);
  218. }
  219. static void setbits24(char *v, int32 r)
  220. {
  221. *v++ = (char)r;
  222. *v++ = (char)(r >> 8);
  223. *v = (char)(r >> 16);
  224. }
  225. static directory *current_input_directory;
  226. static directory_entry *current_output_entry;
  227. static directory *current_output_directory = NULL;
  228. static CSLbool any_output_request;
  229. static char would_be_output_directory[DIRNAME_LENGTH];
  230. #define I_INACTIVE 0
  231. #define I_READING 1
  232. #define I_WRITING 2
  233. static int Istatus = I_INACTIVE;
  234. FILE *binary_read_file;
  235. static FILE *binary_write_file;
  236. static unsigned32 subfile_checksum;
  237. static long int read_bytes_remaining, write_bytes_written;
  238. directory *fasl_files[MAX_FASL_PATHS];
  239. static directory *make_empty_directory(char *name)
  240. /*
  241. * The sole purpose of this empty directory is to carry with it the
  242. * name of the file that I had tried to open.
  243. */
  244. {
  245. directory *d;
  246. d = (directory *) malloc(sizeof(directory) - sizeof(directory_entry));
  247. if (d == NULL) return &empty_directory;
  248. d->h.C = 'C'; d->h.S = MIDDLE_INITIAL; d->h.L = 'L';
  249. d->h.version = IMAGE_FORMAT_VERSION;
  250. d->h.dirsize = 0;
  251. d->h.dirused = 0;
  252. d->h.dirext = 0;
  253. d->h.updated = 0; /* NB read-only */
  254. d->f = NULL;
  255. strncpy(d->filename, name, DIRNAME_LENGTH);
  256. d->filename[DIRNAME_LENGTH-1] = 0;
  257. memset(d->h.eof, 0, 4);
  258. return d;
  259. }
  260. static directory *make_pending_directory(char *name)
  261. /*
  262. * The sole purpose of this empty directory is to carry with it the
  263. * name of the file that I had tried to open.
  264. */
  265. {
  266. directory *d;
  267. int n = sizeof(directory) + (DIRECTORY_SIZE-1)*sizeof(directory_entry);
  268. int l = strlen(name) + 1 -
  269. DIRNAME_LENGTH -
  270. DIRECTORY_SIZE*sizeof(directory_entry);
  271. /*
  272. * Here I extend the directory header with enough extra bytes to hold the
  273. * full name of the file... Once the file has been opened the (potential)
  274. * extra data becomes unnecessary. However with room for DIRECTORY_SIZE
  275. * entries already it would seem bizarre if the path-name ever actually
  276. * overflowed here.
  277. */
  278. if (l > 0) n += l;
  279. d = (directory *)malloc(n);
  280. if (d == NULL) return &empty_directory;
  281. d->h.C = 'C'; d->h.S = MIDDLE_INITIAL; d->h.L = 'L';
  282. d->h.version = IMAGE_FORMAT_VERSION;
  283. d->h.dirsize = DIRECTORY_SIZE & 0xff;
  284. d->h.dirused = 0;
  285. d->h.dirext = (DIRECTORY_SIZE >> 4) & 0xf0;
  286. d->h.updated = D_PENDING | D_WRITE_OK;
  287. /* Well I HOPE that writing will be OK */
  288. d->f = NULL;
  289. strcpy(d->filename, name); /* guaranteed enough space here */
  290. memset(d->h.eof, 0, 4);
  291. return d;
  292. }
  293. static void clear_entry(directory_entry *d)
  294. {
  295. d->D_newline = NEWLINE_CHAR;
  296. memset(&d->D_name, ' ', name_size);
  297. memcpy(&d->D_name, "<Unused>", 8);
  298. memset(&d->D_date, ' ', date_size);
  299. (&d->D_date)[0] = '-';
  300. memset(&d->D_position, 0, 4);
  301. memset(&d->D_size, 0, 3);
  302. }
  303. static CSLbool version_moan(int v)
  304. {
  305. #if defined DEMO_MODE || defined DEMO_BUILD
  306. if (v == 'd') return NO;
  307. term_printf("\n");
  308. term_printf("+++++ This image file is either corrupted or was not\n");
  309. term_printf("+++++ built for use with the Demonstration version.\n");
  310. term_printf("+++++ Unable to proceed - sorry.\n");
  311. #else
  312. if (v == IMAGE_FORMAT_VERSION ||
  313. v == IMAGE_FORMAT_VERSION-1) return NO;
  314. term_printf("\n");
  315. if (v == 'd')
  316. { term_printf("+++++ This image file was built for use with the Demonstration\n");
  317. term_printf("+++++ version of this software and can not be used with the\n");
  318. term_printf("+++++ full product.\n");
  319. }
  320. else
  321. { term_printf("+++++ This image file seems to be from an old or incompatible\n");
  322. term_printf("+++++ version of the software. Please check it by re-installing\n");
  323. term_printf("+++++ or re-building.\n");
  324. }
  325. #endif
  326. return YES;
  327. }
  328. directory *open_pds(char *name, CSLbool forinput)
  329. /*
  330. * Given a file-name, open the associated file, make space for
  331. * a directory and return same.
  332. */
  333. {
  334. char expanded[LONGEST_LEGAL_FILENAME];
  335. directory hdr, *d;
  336. CSLbool write_OK = NO;
  337. FILE *f;
  338. int l, i, n;
  339. l = strlen(name);
  340. f = NULL;
  341. /*
  342. * If you are using "-z" for a cold start you may sometimes want to
  343. * delete the image file (by hand) before running CSL
  344. */
  345. if (!forinput)
  346. {
  347. #ifdef DEMO_MODE
  348. f = NULL;
  349. #else
  350. f = open_file(expanded, name, l, "r+b", NULL);
  351. any_output_request = YES;
  352. strncpy(would_be_output_directory, expanded, DIRNAME_LENGTH-1);
  353. if (f != NULL) write_OK = YES;
  354. else
  355. {
  356. /*
  357. * I first try to open in "r+" mode, which leaves data alone if there
  358. * is already some in the file. If that fails, I try "w+" which can
  359. * create a new file for me.
  360. */
  361. f = open_file(expanded, name, l, "w+b", NULL);
  362. if (f != NULL) write_OK = YES;
  363. }
  364. #endif /* DEMO_MODE */
  365. }
  366. /*
  367. * If I wanted the file for input or if I tried it for output and failed
  368. * then I open for input.
  369. */
  370. if (f == NULL) f = open_file(expanded, name, l, "rb", NULL);
  371. /*
  372. * If the file does not exist I will just hand back a directory that shows
  373. * no files in it. This seems as easy a thing to do at this stage as I can
  374. * think of. Maybe I should warn the user?
  375. */
  376. if (f == NULL) return make_empty_directory(expanded);
  377. #ifndef SIXTEEN_BIT
  378. #define BUFFER_SIZE 0x10000 /* Use 64 Kbyte buffers for extra speed? */
  379. { char *buffer = (char *)malloc(BUFFER_SIZE);
  380. if (buffer != NULL) setvbuf(f, buffer, _IOFBF, BUFFER_SIZE);
  381. }
  382. #endif
  383. fseek(f, 0, SEEK_SET); /* Ensure I am at start of the file */
  384. hdr.h.C = hdr.h.S = hdr.h.L = 0;
  385. if (fread(&hdr.h, sizeof(directory_header), 1, f) != 1 ||
  386. hdr.h.C != 'C' ||
  387. hdr.h.S != MIDDLE_INITIAL ||
  388. hdr.h.L != 'L' ||
  389. /*
  390. * Image format versions are somewhat delicate things. I will not change
  391. * this format often or lightly and the tests I make will then be set up to
  392. * cope with updates from the immediately previous version. The testing code
  393. * will need review each time I consider such a change. For the current
  394. * upgrade I will allow opening of files from version N-1, but I will
  395. * specifically lock out reading an initial heap-image from such. The issue
  396. * of people who start with an old file and then write a fresh image back into
  397. * it will be viewed as too messy to worry about in detail, but I hope that
  398. * I have made it so that writing a new base image (via PRESERVE) updates the
  399. * version info.
  400. */
  401. version_moan(hdr.h.version) ||
  402. get_dirused(hdr.h) > get_dirsize(hdr.h) ||
  403. bits32(hdr.h.eof) < sizeof(directory_header))
  404. {
  405. /*
  406. * Here I did not find a satisfactory header to the directory. If I wanted
  407. * to open the file for input I just return an empty directory, otherwise I
  408. * need to create a new one.
  409. */
  410. if (!write_OK) return make_empty_directory(expanded);
  411. fseek(f, 0, SEEK_SET);
  412. n = DIRECTORY_SIZE; /* Size for a directory */
  413. d = (directory *)
  414. malloc(sizeof(directory)+(n-1)*sizeof(directory_entry));
  415. if (d == NULL) return &empty_directory;
  416. d->h.C = 'C'; d->h.S = MIDDLE_INITIAL; d->h.L = 'L';
  417. d->h.version = IMAGE_FORMAT_VERSION;
  418. d->h.dirsize = n & 0xff;
  419. d->h.dirused = 0;
  420. d->h.dirext = (n >> 4) & 0xf0;
  421. d->h.updated = D_WRITE_OK | D_UPDATED;
  422. for (i=0; i<n; i++) clear_entry(&d->d[i]);
  423. if (fwrite(&d->h, sizeof(directory_header), 1, f) != 1)
  424. return make_empty_directory(expanded);
  425. if (fwrite(&d->d[0], sizeof(directory_entry), (size_t)n, f) != (size_t)n)
  426. return make_empty_directory(expanded);
  427. d->f = f;
  428. strncpy(d->filename, expanded, DIRNAME_LENGTH);
  429. d->filename[DIRNAME_LENGTH-1] = 0;
  430. if (fwrite(registration_data, REGISTRATION_SIZE, 1, f) != 1)
  431. return make_empty_directory(expanded);
  432. setbits32(d->h.eof, (int32)ftell(f));
  433. return d;
  434. }
  435. hdr.h.updated = write_OK ? D_WRITE_OK : 0;
  436. n = get_dirsize(hdr.h);
  437. d = (directory *)
  438. malloc(sizeof(directory)+(n-1)*sizeof(directory_entry));
  439. if (d == NULL) return &empty_directory;
  440. memcpy(&d->h, &hdr.h, sizeof(directory_header));
  441. if (fread(&d->d[0], sizeof(directory_entry), (size_t)n, f) != (size_t)n)
  442. return make_empty_directory(expanded);
  443. /*
  444. * Here the directory seemed OK
  445. */
  446. d->f = f;
  447. strncpy(d->filename, expanded, DIRNAME_LENGTH);
  448. d->filename[DIRNAME_LENGTH-1] = 0;
  449. /*
  450. * For binary files ANSI specify that the values used with fseek and ftell
  451. * are simple counts of the number of characters in the file, and hence
  452. * it is proper to save ftell() values from one run to the next.
  453. */
  454. return d;
  455. }
  456. directory *open_default_output_pds(char *name)
  457. /*
  458. * Given a file-name check if the file exists already. If so try to open
  459. * it writable, and if that fails fall back to opening it read-only.
  460. * if it does NOT exist yet then defer creating it until the first
  461. * write operation on it is attempted.
  462. */
  463. {
  464. char expanded[LONGEST_LEGAL_FILENAME];
  465. directory hdr, *d;
  466. CSLbool write_OK = NO;
  467. FILE *f;
  468. int l, i, n;
  469. l = strlen(name);
  470. f = NULL;
  471. #ifndef DEMO_MODE
  472. /*
  473. * See if I can read from the file. If so it must exist, so close it and
  474. * try again for output.
  475. */
  476. f = open_file(expanded, name, l, "r+b", NULL);
  477. any_output_request = YES;
  478. strncpy(would_be_output_directory, expanded, DIRNAME_LENGTH-1);
  479. if (f != NULL) write_OK = YES;
  480. else
  481. {
  482. /*
  483. * I first try to open in "r+" mode, which leaves data alone if there
  484. * is already some in the file. If that fails, I will hand back a special
  485. * variant on an empty directory.
  486. */
  487. f = open_file(expanded, name, l, "rb", NULL);
  488. if (f == NULL) return make_pending_directory(expanded);
  489. }
  490. #endif /* DEMO_MODE */
  491. /*
  492. * If the file exists but I could not open it for output then I will
  493. * use it read-only.
  494. */
  495. if (f == NULL) f = open_file(expanded, name, l, "rb", NULL);
  496. /*
  497. * If the file does not exist I will just hand back a directory that shows
  498. * no files in it. This seems as easy a thing to do at this stage as I can
  499. * think of. Maybe I should warn the user?
  500. */
  501. if (f == NULL) return make_empty_directory(expanded);
  502. #ifndef SIXTEEN_BIT
  503. #define BUFFER_SIZE 0x10000 /* Use 64 Kbyte buffers for extra speed? */
  504. { char *buffer = (char *)malloc(BUFFER_SIZE);
  505. if (buffer != NULL) setvbuf(f, buffer, _IOFBF, BUFFER_SIZE);
  506. }
  507. #endif
  508. fseek(f, 0, SEEK_SET); /* Ensure I am at start of the file */
  509. if (fread(&hdr.h, sizeof(directory_header), 1, f) != 1 ||
  510. hdr.h.C != 'C' ||
  511. hdr.h.S != MIDDLE_INITIAL ||
  512. hdr.h.L != 'L' ||
  513. version_moan(hdr.h.version) ||
  514. get_dirused(hdr.h) > get_dirsize(hdr.h) ||
  515. bits32(hdr.h.eof) < sizeof(directory_header))
  516. {
  517. /*
  518. * Here I did not find a satisfactory header to the directory. If I wanted
  519. * to open the file for input I just return an empty directory, otherwise I
  520. * need to create a new one.
  521. */
  522. if (!write_OK) return make_empty_directory(expanded);
  523. fseek(f, 0, SEEK_SET);
  524. n = DIRECTORY_SIZE; /* Size for a directory */
  525. d = (directory *)
  526. malloc(sizeof(directory)+(n-1)*sizeof(directory_entry));
  527. if (d == NULL) return &empty_directory;
  528. d->h.C = 'C'; d->h.S = MIDDLE_INITIAL; d->h.L = 'L';
  529. d->h.version = IMAGE_FORMAT_VERSION;
  530. d->h.dirsize = n & 0xff;
  531. d->h.dirused = 0;
  532. d->h.dirext = (n >> 4) & 0xf0;
  533. d->h.updated = D_WRITE_OK | D_UPDATED;
  534. for (i=0; i<n; i++) clear_entry(&d->d[i]);
  535. if (fwrite(&d->h, sizeof(directory_header), 1, f) != 1)
  536. return make_empty_directory(expanded);
  537. if (fwrite(&d->d[0], sizeof(directory_entry), (size_t)n, f) != (size_t)n)
  538. return make_empty_directory(expanded);
  539. d->f = f;
  540. strncpy(d->filename, expanded, DIRNAME_LENGTH);
  541. d->filename[DIRNAME_LENGTH-1] = 0;
  542. if (fwrite(registration_data, REGISTRATION_SIZE, 1, f) != 1)
  543. return make_empty_directory(expanded);
  544. setbits32(d->h.eof, (int32)ftell(f));
  545. return d;
  546. }
  547. hdr.h.updated = write_OK ? D_WRITE_OK : 0;
  548. n = get_dirsize(hdr.h);
  549. d = (directory *)
  550. malloc(sizeof(directory)+(n-1)*sizeof(directory_entry));
  551. if (d == NULL) return &empty_directory;
  552. memcpy(&d->h, &hdr.h, sizeof(directory_header));
  553. if (fread(&d->d[0], sizeof(directory_entry), (size_t)n, f) != (size_t)n)
  554. return make_empty_directory(expanded);
  555. /*
  556. * Here the directory seemed OK
  557. */
  558. d->f = f;
  559. strncpy(d->filename, expanded, DIRNAME_LENGTH);
  560. d->filename[DIRNAME_LENGTH-1] = 0;
  561. /*
  562. * For binary files ANSI specify that the values used with fseek and ftell
  563. * are simple counts of the number of characters in the file, and hence
  564. * it is proper to save ftell() values from one run to the next.
  565. */
  566. return d;
  567. }
  568. static int unpending(directory *d)
  569. {
  570. FILE *f = fopen(d->filename, "w+b");
  571. int32 i, n;
  572. if (f == NULL) return YES;
  573. d->f = f;
  574. d->filename[DIRNAME_LENGTH-1] = 0; /* truncate the name now */
  575. n = DIRECTORY_SIZE; /* Size for a directory */
  576. /* (the next bits were done when the pending directory was first created
  577. d->h.C = 'C'; d->h.S = MIDDLE_INITIAL; d->h.L = 'L';
  578. d->h.version = IMAGE_FORMAT_VERSION;
  579. d->h.dirsize = n & 0xff;
  580. d->h.dirused = 0;
  581. d->h.dirext = (n >> 4) & 0xf0;
  582. */
  583. d->h.updated = D_WRITE_OK | D_UPDATED;
  584. for (i=0; i<n; i++) clear_entry(&d->d[i]);
  585. if (fwrite(&d->h, sizeof(directory_header), 1, f) != 1)
  586. return YES;
  587. if (fwrite(&d->d[0], sizeof(directory_entry), (size_t)n, f) != (size_t)n)
  588. return YES;
  589. if (fwrite(registration_data, REGISTRATION_SIZE, 1, f) != 1)
  590. return YES;
  591. setbits32(d->h.eof, (int32)ftell(f));
  592. return NO;
  593. }
  594. void Iinit()
  595. {
  596. int i;
  597. Istatus = I_INACTIVE;
  598. current_input_directory = NULL;
  599. current_output_entry = NULL;
  600. current_output_directory = NULL;
  601. binary_read_file = binary_write_file = NULL;
  602. read_bytes_remaining = write_bytes_written = 0;
  603. any_output_request = NO;
  604. strcpy(would_be_output_directory, "<unknown>");
  605. for (i=0; i<number_of_fasl_paths; i++)
  606. { if (0x40000000+i == output_directory)
  607. fasl_files[i] = open_default_output_pds(fasl_paths[i]);
  608. else
  609. fasl_files[i] = open_pds(fasl_paths[i], i != output_directory);
  610. }
  611. MD5_Update((unsigned char *)"Copyright 1997 Codemist Ltd", 24);
  612. }
  613. void Icontext(Ihandle *where)
  614. /*
  615. * This and the next are used so that reading from binary files can be
  616. * nested, as may be needed while loading fasl files. An Ihandle should
  617. * be viewed as an abstract handle on the input stream. Beware however that
  618. * if input is from a regular Lisp stream (indicated by read_bytes_remaining
  619. * being negative) that standard_input is NOT saved here. Therefore in
  620. * some cases it needs to be stacked elsewhere. The reason I do not save
  621. * it here is that it is a Lisp_Object and needs garbage collection
  622. * protection, which is not easily provided here.
  623. */
  624. {
  625. switch (where->status = Istatus)
  626. {
  627. case I_INACTIVE:
  628. break;
  629. case I_READING:
  630. where->f = binary_read_file;
  631. if (read_bytes_remaining >= 0) where->o = ftell(binary_read_file);
  632. where->n = read_bytes_remaining;
  633. where->chk = subfile_checksum;
  634. break;
  635. case I_WRITING:
  636. where->f = binary_write_file;
  637. where->o = ftell(binary_write_file);
  638. where->n = write_bytes_written;
  639. where->chk = subfile_checksum;
  640. break;
  641. }
  642. Istatus = I_INACTIVE;
  643. }
  644. void Irestore_context(Ihandle x)
  645. {
  646. switch (Istatus = x.status)
  647. {
  648. case I_INACTIVE:
  649. return;
  650. case I_READING:
  651. binary_read_file = x.f;
  652. read_bytes_remaining = x.n;
  653. if (read_bytes_remaining >= 0) fseek(binary_read_file, x.o, SEEK_SET);
  654. subfile_checksum = x.chk;
  655. return;
  656. case I_WRITING:
  657. binary_write_file = x.f;
  658. fseek(binary_write_file, x.o, SEEK_SET);
  659. write_bytes_written = x.n;
  660. subfile_checksum = x.chk;
  661. return;
  662. }
  663. }
  664. #define IMAGE_CODE (-1000)
  665. #define HELP_CODE (-1001)
  666. #define BANNER_CODE (-1002)
  667. /*
  668. * The code here was originally written to support module names up to
  669. * 11 characters, but it has now been extended to support long names as
  670. * well.
  671. * The mechanism used is as follows:
  672. * The name field in a directory entry is 12 characters long. For system
  673. * special pseudo-modules all 12 characters are used for a name, and the
  674. * cases used at present are InitialImage and HelpDataFile. For all
  675. * user names the name is padded with blanks, and so user names of up
  676. * to 11 characters live in the field with the 12th character a blank.
  677. * To support long names I use values 0x80 and up in this 12th position.
  678. * (NB position 12 is at offset 11 because of zero-base counting!)
  679. * The first segment of a long name uses 11 characters of the user name
  680. * and puts 0x80 in the 12th. Subsequent directory entries are used
  681. * to hold more characters of the name. These hold 11 characters in the
  682. * name field and 24 in the date, and put values 0x81, 0x82 etc in
  683. * character 12. They will have a zero length field, but their position
  684. * field MUST match that of the first record. This requirement is so that
  685. * when I sort a directory the parts of a long name are kept both
  686. * together and in the correct order. The last part of a long name has
  687. * 0xff in position 12. The result is that I can distinguish the case
  688. * of
  689. * a regular username of up to 11 chars (blank in position 12)
  690. * a system special file (non-bloank, but under 0x80 in posn 12)
  691. * the start of a long name (0x80)
  692. * a middle part of a long name (0x81 ...)
  693. * the final part of a long name (0xff).
  694. * when I match names here I will only allow a long-name match if my
  695. * directory is pointing at the first part of a long name.
  696. * As a further minor usefulness here if I find a match the non-zero value I
  697. * return is the number of entries involved.
  698. */
  699. static int samename(char *n1, directory *d, int j, int len)
  700. /*
  701. * Compare the given names, given that n1 is of length len and n2 is
  702. * blank-padded to exactly name_size characters. The special cases
  703. * with n1 NULL allow len to encode what I am looking for.
  704. */
  705. {
  706. char *n2 = &d->d[j].D_name;
  707. int i, n, recs;
  708. if (len == IMAGE_CODE)
  709. return (memcmp(n2, "InitialImage", 12) == 0);
  710. if (len == HELP_CODE)
  711. return (memcmp(n2, "HelpDataFile", 12) == 0);
  712. if (len == BANNER_CODE)
  713. return (memcmp(n2, "Start-Banner", 12) == 0);
  714. if (len < 0)
  715. { char hard[16];
  716. sprintf(hard, "HardCode<%.2x>", (-len) & 0xff);
  717. return (memcmp(n2, hard, 12) == 0);
  718. }
  719. if ((n2[11] & 0xff) > 0x80) return 0;
  720. n = 0;
  721. #define next_char_of_name (n++ < len ? *n1++ : ' ')
  722. for (i=0; i<11; i++)
  723. if (n2[i] != next_char_of_name) return 0;
  724. if ((n2[11] & 0x80) == 0) return ((n >= len) ? 1 : 0);
  725. recs = 1;
  726. do
  727. { n2 = &d->d[++j].D_name;
  728. for (i=0; i<11; i++)
  729. if (n2[i] != next_char_of_name) return 0;
  730. for (i=12; i<36; i++)
  731. if (n2[i] != next_char_of_name) return 0;
  732. recs++;
  733. } while ((n2[11] & 0xff) != 0xff);
  734. #undef next_char_of_name
  735. if (n < len) return 0;
  736. else return recs;
  737. }
  738. static CSLbool open_input(directory *d, char *name, int len, int32 offset)
  739. /*
  740. * Set up binary_read_file to access the given module, returning YES
  741. * if it was not found in the given directory. I used to pass the
  742. * names "InitialImage" and "HelpDataFile" directly to this function, but
  743. * to allow for long module names I am changing things so that these special
  744. * cases are indicated by passing down a NULL string for the name and giving
  745. * an associated length of -1 or -2 (resp).
  746. */
  747. {
  748. int i;
  749. if (Istatus != I_INACTIVE || d == NULL) return YES;
  750. subfile_checksum = 0;
  751. /*
  752. * I use simple linear search to scan the directory - mainly because I
  753. * expect directories to be fairly small and once I have found a file
  754. * I will take a long while to process it, so any clumsiness here is
  755. * not critical. I will not allow myself to read from whichever file
  756. * is currently open for output.
  757. * Because samename() is careful to ensure it only reports a match when
  758. * pointed at the start of a long name it is OK to search in steps of 1
  759. * here.
  760. */
  761. for (i=0; i<get_dirused(d->h); i++)
  762. { if (samename(name, d, i, len) &&
  763. &d->d[i] != current_output_entry)
  764. { binary_read_file = d->f;
  765. read_bytes_remaining = bits24(&d->d[i].D_size);
  766. i = fseek(binary_read_file,
  767. bits32(&d->d[i].D_position)+offset, SEEK_SET);
  768. if (i == 0) /* If fseek succeeded it returned zero */
  769. { Istatus = I_READING;
  770. return NO;
  771. }
  772. else return YES;
  773. }
  774. }
  775. return YES;
  776. }
  777. void IreInit(void)
  778. {
  779. MD5_Update((unsigned char *)"Copyright 1997 Codemist Ltd", 24);
  780. MD5_Update((unsigned char *)"memory.u", 8);
  781. }
  782. static int MS_CDECL for_qsort(void const *aa, void const *bb)
  783. {
  784. directory_entry *a = (directory_entry *)aa,
  785. *b = (directory_entry *)bb;
  786. long int ap = bits32(&a->D_position), bp = bits32(&b->D_position);
  787. if (ap < bp) return -1;
  788. else if (ap > bp) return 1;
  789. /*
  790. * I make the position of the module in the image my primary sort key.
  791. * Over-long module names are coped with by giving each part of the
  792. * name the same position field, but marking the 12th character of the
  793. * name field (D_space) with 0x80, 0x81 ... in extension records. Note that
  794. * a regular short module name has a blank character there, while the special
  795. * cases of "InitialImage" and "HelpDataFile" each have 'e' there,
  796. * "Start-Banner" has 'r', while hard code has '>'.
  797. * So bytes 0x80 and up are clearly (if hackily!) distinguished.
  798. */
  799. ap = a->D_space & 0xff, bp = b->D_space & 0xff;
  800. if (ap < bp) return -1;
  801. else if (ap > bp) return 1;
  802. else return 0;
  803. }
  804. static void sort_directory(directory *d)
  805. {
  806. qsort((void *)d->d, (size_t)get_dirused(d->h),
  807. sizeof(directory_entry), for_qsort);
  808. }
  809. static directory *enlarge_directory(int current_size)
  810. {
  811. nil_as_base
  812. int n = (3*current_size)/2;
  813. int newsize = sizeof(directory)+(n-1)*sizeof(directory_entry);
  814. int newpos = sizeof(directory_header)+n*sizeof(directory_entry);
  815. /*
  816. * enlarge_directory() is only called when an output library is known
  817. * to exist, so I do not need to check for that here.
  818. */
  819. int dirno = library_number(qvalue(output_library));
  820. directory *d1 = fasl_files[dirno];
  821. if (n > current_size+20) n = current_size+20;
  822. for (;;)
  823. { directory_entry *first;
  824. FILE *f;
  825. char buffer[512]; /* I hope this is not done too often, since this */
  826. /* is not a very big buffer size for the copy. */
  827. int32 firstpos, firstlen, newfirst, eofpos;
  828. sort_directory(d1);
  829. first = &d1->d[0];
  830. firstpos = bits32(&first->D_position);
  831. if (firstpos >= newpos + REGISTRATION_SIZE) break;
  832. /*
  833. * Here I need to copy a module up to the end of the file to make room
  834. * for the enlarged directory
  835. */
  836. firstlen = bits24(&first->D_size);
  837. newfirst = eofpos = bits32(d1->h.eof);
  838. f = d1->f;
  839. firstlen += 4; /* Allow for the checksum */
  840. while (firstlen >= sizeof(buffer))
  841. { fseek(f, firstpos, SEEK_SET);
  842. if (fread(buffer, sizeof(buffer), 1, f) != 1) return NULL;
  843. fseek(f, eofpos, SEEK_SET);
  844. if (fwrite(buffer, sizeof(buffer), 1, f) != 1) return NULL;
  845. firstlen -= sizeof(buffer);
  846. firstpos += sizeof(buffer);
  847. eofpos += sizeof(buffer);
  848. }
  849. if (firstlen != 0)
  850. { fseek(f, firstpos, SEEK_SET);
  851. if (fread(buffer, firstlen, 1, f) != 1) return NULL;
  852. fseek(f, eofpos, SEEK_SET);
  853. if (fwrite(buffer, firstlen, 1, f) != 1) return NULL;
  854. eofpos += firstlen;
  855. }
  856. setbits32(&first->D_position, newfirst);
  857. if ((first->D_space & 0xff) == 0x80)
  858. { do
  859. { first++;
  860. setbits32(&first->D_position, newfirst);
  861. } while ((first->D_space & 0xff) != 0xff);
  862. }
  863. setbits32(d1->h.eof, eofpos);
  864. }
  865. fseek(d1->f, newpos, SEEK_SET);
  866. fwrite(registration_data, REGISTRATION_SIZE, 1, d1->f);
  867. d1 = (directory *)realloc((void *)d1, newsize);
  868. if (d1 == NULL) return NULL;
  869. d1->h.dirsize = (n & 0xff);
  870. d1->h.dirext = (d1->h.dirext & 0x0f) + ((n>>4) & 0xf0);
  871. d1->h.updated |= D_COMPACT | D_UPDATED;
  872. while (n>current_size) clear_entry(&d1->d[--n]);
  873. fasl_files[dirno] = d1;
  874. return d1;
  875. }
  876. static CSLbool open_output(char *name, int len)
  877. /*
  878. * Set up binary_write_file to access the given module, returning YES
  879. * if anything went wrong. Remember name==NULL for initial image & help
  880. * data.
  881. */
  882. {
  883. #ifdef DEMO_MODE
  884. return YES;
  885. #else
  886. nil_as_base
  887. int i, j, n;
  888. char *ct;
  889. char hard[16];
  890. directory *d;
  891. time_t t = time(NULL);
  892. Lisp_Object oo = qvalue(output_library);
  893. if (!is_library(oo)) return YES;
  894. d = fasl_files[library_number(oo)];
  895. if (d == NULL) return YES; /* closed handle, I guess */
  896. if ((d->h.updated & D_WRITE_OK) == 0) return YES;
  897. /*
  898. * The main effect of the next line will be to prohibit opening a new
  899. * FASL file while I am in the middle of reading one that already exists.
  900. * Indeed this is a restriction, but at present it seems a very reasonable
  901. * on for me to apply.
  902. */
  903. if (Istatus != I_INACTIVE) return YES;
  904. if (d->h.updated & D_PENDING)
  905. { if (unpending(d)) return YES;
  906. }
  907. subfile_checksum = 0;
  908. current_output_directory = d;
  909. /*
  910. * I use simple linear search to scan the directory - mainly because I
  911. * expect directories to be fairly small and once I have found a file
  912. * I will take a long while to process it, so any clumsiness here is
  913. * not critical. Again note it is OK to scan in steps of 1 despite the
  914. * fact that long-names are stored split across consecutive directory slots.
  915. */
  916. for (i=0; i<get_dirused(d->h); i++)
  917. { if (samename(name, d, i, len))
  918. { current_output_entry = &d->d[i];
  919. d->h.updated |= D_COMPACT | D_UPDATED;
  920. if (t == (time_t)(-1)) ct = "not dated";
  921. else ct = ctime(&t);
  922. /*
  923. * Note that I treat the result handed back by ctime() as delicate, in that
  924. * I do not do any library calls between calling ctime and copying the
  925. * string it returns to somewhere that is under my own control.
  926. */
  927. strncpy(&d->d[i].D_date, ct, date_size);
  928. binary_write_file = d->f;
  929. write_bytes_written = 0;
  930. memcpy(&d->d[i].D_position, d->h.eof, 4);
  931. /* For long names I must put the location in each record */
  932. if (d->d[i].D_space & 0x80)
  933. { j = 0;
  934. do
  935. { j++;
  936. memcpy(&d->d[i+j].D_position, d->h.eof, 4);
  937. } while ((d->d[i+j].D_space & 0xff) != 0xff);
  938. }
  939. i = fseek(binary_write_file, bits32(d->h.eof), SEEK_SET);
  940. if (i == 0) Istatus = I_WRITING;
  941. else current_output_directory = NULL;
  942. if (name == NULL && len == IMAGE_CODE)
  943. d->h.version = IMAGE_FORMAT_VERSION;
  944. return i;
  945. }
  946. }
  947. /*
  948. * Here the name did not already exist, and so I will need to enter it into
  949. * the directory. If I get here the variable i points to the first unused
  950. * directory entry.
  951. */
  952. if (len == IMAGE_CODE)
  953. { name = "InitialImage";
  954. n = 1;
  955. d->h.version = IMAGE_FORMAT_VERSION;
  956. }
  957. else if (len == HELP_CODE) name = "HelpDataFile", len = IMAGE_CODE, n = 1;
  958. else if (len == BANNER_CODE) name = "Start-Banner", len = IMAGE_CODE, n = 1;
  959. else if (len < 0)
  960. { sprintf(hard, "HardCode<%.2x>", (-len) & 0xff);
  961. name = hard, len = IMAGE_CODE, n = 1;
  962. }
  963. else if (len <= 11) n = 1;
  964. else if (len <= 11+11+24) n = 2;
  965. else if (len <= 11+11+11+24+24) n = 3;
  966. else return YES; /* Name longer than 81 chars not supported, sorry */
  967. while (i+n > (int)get_dirsize(d->h))
  968. { d = enlarge_directory(i);
  969. current_output_directory = d;
  970. if (d == NULL) return YES;
  971. }
  972. current_output_entry = &d->d[i];
  973. if (len == IMAGE_CODE)
  974. { d->d[i].D_newline = NEWLINE_CHAR;
  975. memcpy(&d->d[i].D_name, name, 12);
  976. memset(&d->d[i].D_date, ' ', date_size);
  977. memset(&d->d[i].D_size, 0, 3);
  978. memcpy(&d->d[i].D_position, d->h.eof, 4);
  979. }
  980. else
  981. { int np;
  982. char *p;
  983. /*
  984. * First I will clear all the relevant fields to blanks.
  985. */
  986. for (j=0; j<n; j++)
  987. { d->d[i+j].D_newline = '\n';
  988. memset(&d->d[i+j].D_name, ' ', name_size);
  989. memset(&d->d[i+j].D_date, ' ', date_size);
  990. memset(&d->d[i+j].D_size, 0, 3);
  991. memcpy(&d->d[i+j].D_position, d->h.eof, 4);
  992. }
  993. #define next_char_of_name (np++ >= len ? ' ' : *p++)
  994. np = 0;
  995. p = name;
  996. for (j=0; j<n; j++)
  997. { int k;
  998. for (k=0; k<11; k++) (&d->d[i+j].D_name)[k] = next_char_of_name;
  999. if (j != 0)
  1000. for (k=0; k<24; k++)
  1001. (&d->d[i+j].D_date)[k] = next_char_of_name;
  1002. if (j == 0 && n == 1) d->d[i+j].D_space = ' ';
  1003. else if (j == n-1) d->d[i+j].D_space = 0xff;
  1004. else d->d[i+j].D_space = 0x80+j;
  1005. #undef next_char_of_name
  1006. }
  1007. }
  1008. if (t == (time_t)(-1)) ct = "** *** not dated *** ** ";
  1009. else ct = ctime(&t);
  1010. strncpy(&d->d[i].D_date, ct, date_size);
  1011. set_dirused(&d->h, get_dirused(d->h)+n);
  1012. binary_write_file = d->f;
  1013. write_bytes_written = 0;
  1014. d->h.updated |= D_UPDATED;
  1015. i = fseek(binary_write_file, bits32(d->h.eof), SEEK_SET);
  1016. if (i == 0)
  1017. { Istatus = I_WRITING;
  1018. return NO;
  1019. }
  1020. else
  1021. { current_output_directory = NULL;
  1022. return YES;
  1023. }
  1024. #endif /* DEMO_MODE */
  1025. }
  1026. static void list_one_library(Lisp_Object oo, CSLbool out_only)
  1027. {
  1028. int j;
  1029. directory *d = fasl_files[library_number(oo)];
  1030. trace_printf("\nFile %s (dirsize %ld length %ld",
  1031. d->filename, (long)get_dirsize(d->h), (long)bits32(d->h.eof));
  1032. j = d->h.updated;
  1033. if (j != 0) trace_printf(",");
  1034. if (j & D_WRITE_OK) trace_printf(" Writable");
  1035. if (j & D_UPDATED) trace_printf(" Updated");
  1036. if (j & D_COMPACT) trace_printf(" NeedsCompaction");
  1037. if (j & D_PENDING) trace_printf(" Pending");
  1038. if (out_only) trace_printf(" OutputOnly");
  1039. trace_printf("):\n");
  1040. /*
  1041. * The format string used here will need adjustment if you ever change the
  1042. * number of characters used to store names or dates.
  1043. */
  1044. for (j=0; j<get_dirused(d->h); j++)
  1045. { int n = 0;
  1046. if (d->d[j].D_space & 0x80)
  1047. { trace_printf(" %.11s", &d->d[j].D_name);
  1048. do
  1049. { n++;
  1050. trace_printf("%.11s%.24s",
  1051. &d->d[j+n].D_name, &d->d[j+n].D_date);
  1052. } while ((d->d[j+n].D_space & 0xff) != 0xff);
  1053. trace_printf(
  1054. "\n %-24.24s position %-7ld size: %ld\n",
  1055. &d->d[j].D_date,
  1056. (long)bits32(&d->d[j].D_position),
  1057. (long)bits24(&d->d[j].D_size));
  1058. j += n;
  1059. }
  1060. else trace_printf(
  1061. " %-12.12s %-24.24s position %-7ld size: %ld\n",
  1062. &d->d[j].D_name, &d->d[j].D_date,
  1063. (long)bits32(&d->d[j].D_position),
  1064. (long)bits24(&d->d[j].D_size));
  1065. }
  1066. }
  1067. void Ilist()
  1068. {
  1069. Lisp_Object nil = C_nil;
  1070. Lisp_Object il = qvalue(input_libraries), w;
  1071. Lisp_Object ol = qvalue(output_library);
  1072. while (consp(il))
  1073. { w = qcar(il); il = qcdr(il);
  1074. if (!is_library(w)) continue;
  1075. if (w == ol) ol = nil;
  1076. list_one_library(w, NO);
  1077. }
  1078. if (is_library(ol)) list_one_library(ol, YES);
  1079. }
  1080. Lisp_Object Llibrary_members(Lisp_Object nil, Lisp_Object oo)
  1081. {
  1082. int i, j, k;
  1083. directory *d = fasl_files[library_number(oo)];
  1084. Lisp_Object v, r = C_nil;
  1085. char *p;
  1086. for (j=0; j<get_dirused(d->h); j++)
  1087. { int n = 0;
  1088. p = (char *)&celt(boffo, 0);
  1089. k = 0;
  1090. if (d->d[j].D_space & 0x80)
  1091. { for (i=0; i<11; i++)
  1092. { *p++ = (&d->d[j].D_name)[i];
  1093. k++;
  1094. }
  1095. do
  1096. { n++;
  1097. for (i=0; i<11; i++)
  1098. { *p++ = (&d->d[j+n].D_name)[i];
  1099. k++;
  1100. }
  1101. } while ((d->d[j+n].D_space & 0xff) != 0xff);
  1102. j += n;
  1103. }
  1104. else
  1105. { if (memcmp(&d->d[j].D_name, "InitialImage", 12) == 0 ||
  1106. memcmp(&d->d[j].D_name, "HelpDataFile", 12) == 0 ||
  1107. memcmp(&d->d[j].D_name, "Start-Banner", 12) == 0 ||
  1108. memcmp(&d->d[j].D_name, "HardCode<", 9) == 0 &&
  1109. (&d->d[j].D_name)[11] == '>')
  1110. continue; /* not user modules */
  1111. for (i=0; i<12; i++)
  1112. { *p++ = (&d->d[j].D_name)[i];
  1113. k++;
  1114. }
  1115. }
  1116. while (k>0 && p[-1] == ' ') k--, p--;
  1117. *p = 0;
  1118. push(r);
  1119. v = iintern(boffo, k, lisp_package, 0);
  1120. pop(r);
  1121. errexit();
  1122. r = cons(v, r);
  1123. errexit();
  1124. }
  1125. return onevalue(r);
  1126. }
  1127. Lisp_Object MS_CDECL Llibrary_members0(Lisp_Object nil, int nargs, ...)
  1128. /*
  1129. * This returns a list of the modules in the first library on the current
  1130. * search path.
  1131. */
  1132. {
  1133. Lisp_Object il = qvalue(input_libraries), w;
  1134. Lisp_Object ol = qvalue(output_library);
  1135. argcheck(nargs, 0, "library-members");
  1136. while (consp(il))
  1137. { w = qcar(il); il = qcdr(il);
  1138. if (!is_library(w)) continue;
  1139. return Llibrary_members(nil, w);
  1140. }
  1141. if (is_library(ol)) return Llibrary_members(nil, ol);
  1142. else return onevalue(nil);
  1143. }
  1144. CSLbool Imodulep(char *name, int len, char *datestamp, int32 *size,
  1145. char *expanded_name)
  1146. /*
  1147. * Hands back information about whether the given module exists, and
  1148. * if it does when it was written. Code should be very similar to
  1149. * that in Iopen.
  1150. */
  1151. {
  1152. int i;
  1153. Lisp_Object nil = C_nil;
  1154. Lisp_Object il = qvalue(input_libraries);
  1155. /*
  1156. * nil is conditionally needed for two reasons here:
  1157. * (a) if NILSEG_EXTERNS was not selected it is needed as a base register for
  1158. * access to input_libraries
  1159. * (b) if COMMON was selected it is needed for the expansion of the
  1160. * consp test.
  1161. * If neither of the above apply its is redundant, but not a very greate pain.
  1162. */
  1163. CSL_IGNORE(nil);
  1164. while (consp(il))
  1165. { int j;
  1166. directory *d;
  1167. Lisp_Object oo = qcar(il); il = qcdr(il);
  1168. if (!is_library(oo)) continue;
  1169. i = library_number(oo);
  1170. d = fasl_files[i];
  1171. if (d == NULL) continue;
  1172. for (j=0; j<get_dirused(d->h); j++)
  1173. { if (samename(name, d, j, len))
  1174. { char *n = fasl_files[i]->filename;
  1175. memcpy(datestamp, &d->d[j].D_date, date_size);
  1176. *size = bits24(&d->d[j].D_size);
  1177. if (name == NULL) sprintf(expanded_name, "%s(InitialImage)", n);
  1178. else sprintf(expanded_name, "%s(%.*s)", n, len, name);
  1179. return NO;
  1180. }
  1181. }
  1182. }
  1183. return YES;
  1184. }
  1185. CSLbool IopenRoot(char *expanded_name, int hard)
  1186. /*
  1187. * Opens the "InitialImage" file so that it can be loaded. Note that
  1188. * when I am about to do this I do not have a valid heap image loaded, and
  1189. * so it would NOT be possible to use the regular search-path mechanism for
  1190. * libraries. Therefore I will just use images as specified from the
  1191. * command line (or by default).
  1192. */
  1193. {
  1194. char *n;
  1195. int i;
  1196. if (hard == 0) hard = IMAGE_CODE;
  1197. for (i=0; i<number_of_fasl_paths; i++)
  1198. { CSLbool bad;
  1199. bad = open_input(fasl_files[i], NULL, hard, 0);
  1200. /*
  1201. * The name that I return (for possible display in error messages) will be
  1202. * either that of the file that was opened, or one relating to the last
  1203. * entry in the search path.
  1204. */
  1205. n = fasl_files[i]->filename;
  1206. if (expanded_name != NULL)
  1207. { if (hard == IMAGE_CODE)
  1208. { if (!bad)
  1209. { long int pos = ftell(binary_read_file);
  1210. directory *d = fasl_files[i];
  1211. unsigned char rr[REGISTRATION_SIZE];
  1212. int n = get_dirsize(d->h) * sizeof(directory_entry);
  1213. n += sizeof(directory_header);
  1214. fseek(binary_read_file, n, SEEK_SET);
  1215. fread(rr, REGISTRATION_SIZE,
  1216. 1, binary_read_file);
  1217. if (memcmp(rr, REGISTRATION_VERSION, 4) == 0)
  1218. memcpy(registration_data, rr, REGISTRATION_SIZE);
  1219. fseek(binary_read_file, pos, SEEK_SET);
  1220. }
  1221. sprintf(expanded_name, "%s(InitialImage)", n);
  1222. }
  1223. else if (hard == BANNER_CODE)
  1224. sprintf(expanded_name, "%s(InitialImage)", n);
  1225. else sprintf(expanded_name, "%s(HardCode<%.2x>)",
  1226. n, (-hard) & 0xff);
  1227. }
  1228. if (!bad) return NO;
  1229. }
  1230. return YES;
  1231. }
  1232. CSLbool Iopen(char *name, int len, CSLbool forinput, char *expanded_name)
  1233. /*
  1234. * Make file with the given name available through this package of
  1235. * routines. (name) is a pointer to a string (len characters valid) that
  1236. * names a fasl file. (forinput) specifies the direction of the transfer
  1237. * to set up. Returns YES if something failed.
  1238. * name can be NULL when a module is opened for output, and then output
  1239. * is sent to "InitialImage". The same is done for input, but it would be
  1240. * more sensible to use IopenRoot() to access the root image.
  1241. */
  1242. {
  1243. char *n;
  1244. Lisp_Object nil = C_nil;
  1245. CSL_IGNORE(nil);
  1246. if (name == NULL) len = IMAGE_CODE;
  1247. if (forinput)
  1248. { int i;
  1249. Lisp_Object il = qvalue(input_libraries);
  1250. while (consp(il))
  1251. { CSLbool bad;
  1252. Lisp_Object oo = qcar(il); il = qcdr(il);
  1253. if (!is_library(oo)) continue;
  1254. i = library_number(oo);
  1255. bad = open_input(fasl_files[i], name, len, 0);
  1256. /*
  1257. * The name that I return (for possible display in error messages) will be
  1258. * either that of the file that was opened, or one relating to the last
  1259. * entry in the search path.
  1260. */
  1261. n = fasl_files[i]->filename;
  1262. if (expanded_name != NULL)
  1263. sprintf(expanded_name, "%s(%.*s)", n, len, name);
  1264. if (!bad) return NO;
  1265. }
  1266. return YES;
  1267. }
  1268. #ifndef DEMO_MODE
  1269. if (!any_output_request)
  1270. #endif
  1271. { if (expanded_name != NULL)
  1272. strcpy(expanded_name, "<no output file specified>");
  1273. return YES;
  1274. }
  1275. #ifndef DEMO_MODE
  1276. n = would_be_output_directory;
  1277. if (expanded_name != NULL)
  1278. { if (len == IMAGE_CODE)
  1279. sprintf(expanded_name, "%s(InitialImage)", n);
  1280. else sprintf(expanded_name, "%s(%.*s)", n, len, name);
  1281. }
  1282. return open_output(name, len);
  1283. #endif
  1284. }
  1285. CSLbool Iwriterootp(char *expanded_name)
  1286. /*
  1287. * Test if it will be possible to write out an image file. Used
  1288. * by (preserve) so it can report that this would fail without actually
  1289. * doing anything too drastic.
  1290. */
  1291. {
  1292. #ifdef DEMO_MODE
  1293. strcpy(expanded_name, "<demo-system>");
  1294. return YES;
  1295. #else
  1296. Lisp_Object nil = C_nil;
  1297. directory *d;
  1298. Lisp_Object oo = qvalue(output_library);
  1299. CSL_IGNORE(nil);
  1300. if (!any_output_request)
  1301. { strcpy(expanded_name, "<no output file specified>");
  1302. return YES;
  1303. }
  1304. sprintf(expanded_name, "%s(InitialImage)", would_be_output_directory);
  1305. if (!is_library(oo)) return YES;
  1306. d = fasl_files[library_number(oo)];
  1307. if (d == NULL) return YES; /* closed handle, I guess */
  1308. if ((d->h.updated & D_WRITE_OK) == 0) return YES;
  1309. if (Istatus != I_INACTIVE) return YES;
  1310. return NO;
  1311. #endif /* DEMO_MODE */
  1312. }
  1313. CSLbool Iopen_help(int32 offset)
  1314. /*
  1315. * Get ready to handle the HELP subfile. offset >= 0 will open an
  1316. * existing help module for input and position at the given location.
  1317. * A negative offset indicates that the help module should be opened
  1318. * for writing.
  1319. */
  1320. {
  1321. Lisp_Object nil = C_nil;
  1322. CSL_IGNORE(nil);
  1323. if (offset >= 0)
  1324. { Lisp_Object il = qvalue(input_libraries);
  1325. while (consp(il))
  1326. { CSLbool bad;
  1327. Lisp_Object oo = qcar(il); il = qcdr(il);
  1328. if (!is_library(oo)) continue;
  1329. bad = open_input(fasl_files[library_number(oo)],
  1330. NULL, HELP_CODE, offset);
  1331. if (!bad) return NO;
  1332. }
  1333. return YES;
  1334. }
  1335. #ifdef DEMO_MODE
  1336. return YES;
  1337. #else
  1338. if (!any_output_request) return YES;
  1339. return open_output(NULL, HELP_CODE);
  1340. #endif
  1341. }
  1342. CSLbool Iopen_banner(int code)
  1343. /*
  1344. * Get ready to handle the startup banner.
  1345. * code = 0 open for reading
  1346. * code = -1 open for writing
  1347. * code = -2 delete banner file
  1348. */
  1349. {
  1350. Lisp_Object nil = C_nil;
  1351. CSL_IGNORE(nil);
  1352. if (code == -2) return Idelete(NULL, BANNER_CODE);
  1353. else if (code == 0)
  1354. { Lisp_Object il = qvalue(input_libraries);
  1355. while (consp(il))
  1356. { CSLbool bad;
  1357. Lisp_Object oo = qcar(il); il = qcdr(il);
  1358. if (!is_library(oo)) continue;
  1359. bad = open_input(fasl_files[library_number(oo)],
  1360. NULL, BANNER_CODE, 0);
  1361. if (!bad) return NO;
  1362. }
  1363. return YES;
  1364. }
  1365. #ifdef DEMO_MODE
  1366. return YES;
  1367. #else
  1368. if (!any_output_request) return YES;
  1369. return open_output(NULL, BANNER_CODE);
  1370. #endif
  1371. }
  1372. /*
  1373. * Set up binary_read_file to read from standard input. Return YES if
  1374. * things fail.
  1375. */
  1376. CSLbool Iopen_from_stdin()
  1377. {
  1378. if (Istatus != I_INACTIVE) return YES;
  1379. subfile_checksum = 0;
  1380. binary_read_file = NULL;
  1381. read_bytes_remaining = -1;
  1382. Istatus = I_READING;
  1383. return NO;
  1384. }
  1385. CSLbool Idelete(char *name, int len)
  1386. {
  1387. #ifdef DEMO_MODE
  1388. return YES;
  1389. #else
  1390. nil_as_base
  1391. int i, nrec;
  1392. directory *d;
  1393. Lisp_Object oo = qvalue(output_library);
  1394. if (!is_library(oo)) return YES;
  1395. d = fasl_files[library_number(oo)];
  1396. if (d == NULL ||
  1397. (d->h.updated && D_WRITE_OK) == 0 ||
  1398. Istatus != I_INACTIVE) return YES;
  1399. for (i=0; i<get_dirused(d->h); i++)
  1400. { if ((nrec = samename(name, d, i, len)) != 0)
  1401. { int j;
  1402. set_dirused(&d->h, get_dirused(d->h)-nrec);
  1403. for (j=i; j<get_dirused(d->h); j++)
  1404. d->d[j] = d->d[j+nrec];
  1405. /*
  1406. * I tidy up the now-unused entry - in some sense this is a redundant
  1407. * operation, but I think it makes the file seem neater, which may possibly
  1408. * help avoid confusion and ease debugging.
  1409. */
  1410. while (nrec-- != 0)
  1411. { memset(&d->d[j].D_name, ' ', name_size);
  1412. memcpy(&d->d[j].D_name, "<Unused>", 8);
  1413. memset(&d->d[j].D_date, ' ', date_size);
  1414. (&d->d[j].D_date)[0] = '-';
  1415. setbits32(&d->d[j].D_position, 0);
  1416. setbits24(&d->d[j].D_size, 0);
  1417. j++;
  1418. }
  1419. d->h.updated |= D_COMPACT | D_UPDATED;
  1420. return NO;
  1421. }
  1422. }
  1423. return YES;
  1424. #endif /* DEMO_MODE */
  1425. }
  1426. #define update_crc(chk, c) \
  1427. chk_temp = (chk << 7); \
  1428. chk = ((chk >> 25) ^ \
  1429. (chk_temp >> 1) ^ \
  1430. (chk_temp >> 4) ^ \
  1431. (0xff & (unsigned32)c)) & 0x7fffffff;
  1432. static int validate_checksum(FILE *f, unsigned32 chk1)
  1433. {
  1434. int c;
  1435. unsigned32 chk2;
  1436. if (read_bytes_remaining < 0)
  1437. { if ((c = Igetc()) == EOF) goto failed;
  1438. chk2 = c & 0xff;
  1439. if ((c = Igetc()) == EOF) goto failed;
  1440. chk2 = (chk2 << 8) | (c & 0xff);
  1441. if ((c = Igetc()) == EOF) goto failed;
  1442. chk2 = (chk2 << 8) | (c & 0xff);
  1443. if ((c = Igetc()) == EOF) goto failed;
  1444. chk2 = (chk2 << 8) | (c & 0xff);
  1445. if (chk1 == chk2) return NO; /* All went well */
  1446. }
  1447. else
  1448. { if ((c = getc(f)) == EOF) goto failed;
  1449. chk2 = c & 0xff;
  1450. if ((c = getc(f)) == EOF) goto failed;
  1451. chk2 = (chk2 << 8) | (c & 0xff);
  1452. if ((c = getc(f)) == EOF) goto failed;
  1453. chk2 = (chk2 << 8) | (c & 0xff);
  1454. if ((c = getc(f)) == EOF) goto failed;
  1455. chk2 = (chk2 << 8) | (c & 0xff);
  1456. if (chk1 == chk2) return NO; /* All went well */
  1457. }
  1458. failed:
  1459. err_printf("\n+++ FASL module checksum failure (%.8x instead of %.8x)\n",
  1460. chk2, chk1);
  1461. return YES;
  1462. }
  1463. #ifndef DEMO_MODE
  1464. static int put_checksum(FILE *f, unsigned32 chk)
  1465. {
  1466. Lisp_Object nil = C_nil;
  1467. /*
  1468. * NB that while I am writing out the root section of a checkpoint image
  1469. * I will have unadjusted all Lisp variables, and in particular this will
  1470. * mean that anything that used to have the value NIL will then be
  1471. * SPID_NIL instead. Part of what I should remember here is that
  1472. * in consequence I can not send a main image to a Lisp stream. But I
  1473. * think that is OK, since the only way I have of setting up fasl_stream
  1474. * is via the FASLOUT mechanism.
  1475. */
  1476. if (fasl_stream != nil && fasl_stream != SPID_NIL)
  1477. { putc_stream((int)(chk>>24), fasl_stream);
  1478. putc_stream((int)(chk>>16), fasl_stream);
  1479. putc_stream((int)(chk>>8), fasl_stream);
  1480. putc_stream((int)chk, fasl_stream);
  1481. return NO;
  1482. }
  1483. if (putc((int)(chk>>24), f) == EOF) return YES;
  1484. if (putc((int)(chk>>16), f) == EOF) return YES;
  1485. if (putc((int)(chk>>8), f) == EOF) return YES;
  1486. return (putc((int)chk, f) == EOF);
  1487. }
  1488. #endif /* DEMO_MODE */
  1489. CSLbool Icopy(char *name, int len)
  1490. /*
  1491. * Find the named module in one of the input files, and if the place that
  1492. * it is found is not already the output file copy it to the output.
  1493. */
  1494. {
  1495. #ifdef DEMO_MODE
  1496. return YES;
  1497. #else
  1498. int i, ii, j, n;
  1499. long int k, l, save = read_bytes_remaining;
  1500. unsigned32 chk1;
  1501. char hard[16];
  1502. directory *d, *id;
  1503. Lisp_Object nil = C_nil;
  1504. Lisp_Object il, oo = qvalue(output_library);
  1505. CSL_IGNORE(nil);
  1506. if (!is_library(oo)) return YES;
  1507. d = fasl_files[library_number(oo)];
  1508. /*
  1509. * Only valid if there is an output file and nothing else is going on.
  1510. */
  1511. if (d == NULL ||
  1512. (d->h.updated & D_WRITE_OK) == 0 ||
  1513. Istatus != I_INACTIVE) return YES;
  1514. if (d->h.updated & D_PENDING)
  1515. { if (unpending(d)) return YES;
  1516. }
  1517. /*
  1518. * Search for a suitable input module to copy...
  1519. */
  1520. for (il=qvalue(input_libraries); consp(il); il = qcdr(il))
  1521. { oo = qcar(il);
  1522. if (!is_library(oo)) continue;
  1523. i = library_number(oo);
  1524. id = fasl_files[i];
  1525. for (ii=0; ii<get_dirused(id->h); ii++)
  1526. if (samename(name, id, ii, len)) goto found;
  1527. }
  1528. return YES; /* Module to copy not found */
  1529. found:
  1530. /*
  1531. * If the potential input module found was in the output directory exit now.
  1532. */
  1533. if (id == d) return NO;
  1534. /*
  1535. * Now scan output directory to see where to put result
  1536. */
  1537. for (i=0; i<get_dirused(d->h); i++)
  1538. if (samename(name, d, i, len))
  1539. { d->h.updated |= D_UPDATED | D_COMPACT;
  1540. goto ofound;
  1541. }
  1542. /*
  1543. * The file was not previously present in the output directory, so
  1544. * I need to insert it. The code here is copies from open_output and is
  1545. * now messy enoug that I should really move it to a sub-function.
  1546. */
  1547. if (len == IMAGE_CODE)
  1548. name = "InitialImage", n = 1;
  1549. else if (len == HELP_CODE)
  1550. name = "HelpDataFile", len = IMAGE_CODE, n = 1;
  1551. else if (len == BANNER_CODE)
  1552. name = "Start-Banner", len = IMAGE_CODE, n = 1;
  1553. else if (len < 0)
  1554. { sprintf(hard, "HardCode<%.2x>", (-len) & 0xff);
  1555. name = hard, len = IMAGE_CODE, n = 1;
  1556. }
  1557. else if (len <= 11) n = 1;
  1558. else if (len <= 11+11+24) n = 2;
  1559. else if (len <= 11+11+11+24+24) n = 3;
  1560. else return YES; /* Name longer than 81 chars not supported, sorry */
  1561. while (i+n > (int)get_dirsize(d->h))
  1562. { d = enlarge_directory(i);
  1563. current_output_directory = d;
  1564. if (d == NULL) return YES;
  1565. }
  1566. current_output_entry = &d->d[i];
  1567. if (len == IMAGE_CODE)
  1568. { d->d[i].D_newline = NEWLINE_CHAR;
  1569. memcpy(&d->d[i].D_name, name, 12);
  1570. memset(&d->d[i].D_date, ' ', date_size);
  1571. memset(&d->d[i].D_size, 0, 3);
  1572. memcpy(&d->d[i].D_position, d->h.eof, 4);
  1573. }
  1574. else
  1575. { int np;
  1576. char *p;
  1577. /*
  1578. * First I will clear all the relevant fields to blanks.
  1579. */
  1580. for (j=0; j<n; j++)
  1581. { d->d[i+j].D_newline = '\n';
  1582. memset(&d->d[i+j].D_name, ' ', name_size);
  1583. memset(&d->d[i+j].D_date, ' ', date_size);
  1584. memset(&d->d[i+j].D_size, 0, 3);
  1585. memcpy(&d->d[i+j].D_position, d->h.eof, 4);
  1586. }
  1587. #define next_char_of_name (np++ >= len ? ' ' : *p++)
  1588. np = 0;
  1589. p = name;
  1590. for (j=0; j<n; j++)
  1591. { for (k=0; k<11; k++) (&d->d[i+j].D_name)[k] = next_char_of_name;
  1592. if (j != 0)
  1593. for (k=0; k<24; k++)
  1594. (&d->d[i+j].D_date)[k] = next_char_of_name;
  1595. if (j == 0 && n == 1) d->d[i+j].D_space = ' ';
  1596. else if (j == n-1) d->d[i+j].D_space = 0xff;
  1597. else d->d[i+j].D_space = 0x80+j;
  1598. #undef next_char_of_name
  1599. }
  1600. }
  1601. set_dirused(&d->h, get_dirused(d->h)+n);
  1602. ofound:
  1603. memcpy(&d->d[i].D_date, &id->d[ii].D_date, date_size);
  1604. trace_printf("\nCopy %.*s from %s to %s\n",
  1605. len, name, id->filename, d->filename);
  1606. memcpy(&d->d[i].D_position, d->h.eof, 4);
  1607. if (d->d[i].D_space & 0x80)
  1608. { n = 0;
  1609. do
  1610. { n++;
  1611. memcpy(&d->d[i+n].D_position, d->h.eof, 4);
  1612. } while ((d->d[i+n].D_space & 0xff) != 0xff);
  1613. }
  1614. /*
  1615. * I provisionally set the size to zero so that if something goes wrong
  1616. * I will still have a tolerably sensible image file.
  1617. */
  1618. memset(&d->d[i].D_size, 0, 3);
  1619. d->h.updated |= D_UPDATED;
  1620. if (fseek(d->f, bits32(&d->d[i].D_position), SEEK_SET) != 0 ||
  1621. fseek(id->f, bits32(&id->d[ii].D_position), SEEK_SET) != 0) return YES;
  1622. l = bits24(&id->d[ii].D_size);
  1623. chk1 = 0;
  1624. for (k=0; k<l; k++)
  1625. { int c = getc(id->f);
  1626. unsigned32 chk_temp;
  1627. /*
  1628. * I do not have to do anything special about encryption here...
  1629. */
  1630. update_crc(chk1, c);
  1631. if (c == EOF) return YES;
  1632. putc(c, d->f);
  1633. }
  1634. read_bytes_remaining = 0;
  1635. j = validate_checksum(id->f, chk1);
  1636. read_bytes_remaining = save;
  1637. if (j) return YES;
  1638. if (put_checksum(d->f, chk1)) return YES;
  1639. if (fflush(d->f) != 0) return YES;
  1640. setbits24(&d->d[i].D_size, (int32)l);
  1641. setbits32(d->h.eof, (int32)ftell(d->f));
  1642. return NO;
  1643. #endif /* DEMO_MODE */
  1644. }
  1645. CSLbool IcloseInput(int check_checksum)
  1646. /*
  1647. * Terminate processing one whatever subfile has been being processed.
  1648. * returns nonzero if there was trouble.
  1649. * read and verify checksum if arg is TRUE.
  1650. */
  1651. {
  1652. Istatus = I_INACTIVE;
  1653. if (check_checksum)
  1654. return validate_checksum(binary_read_file, subfile_checksum);
  1655. else return NO;
  1656. }
  1657. CSLbool IcloseOutput()
  1658. /*
  1659. * Terminate processing one whatever subfile has been being processed.
  1660. * returns nonzero if there was trouble. Write a checksum to the file.
  1661. * There is a jolly joke here! I MUST NOT try to pick up the identification
  1662. * of the output directory from the lisp-level variable output_directory
  1663. * because (preserve) calls this AFTER it has utterly mangled the heap (to
  1664. * put all pointers into relative form). To alloc for this the variable
  1665. * current_output_directory identifies the directory within which a file
  1666. * was most recently opened.
  1667. */
  1668. {
  1669. #ifdef DEMO_MODE
  1670. return YES;
  1671. #else
  1672. int r;
  1673. Lisp_Object nil = C_nil;
  1674. directory *d = current_output_directory;
  1675. Istatus = I_INACTIVE;
  1676. if (fasl_stream != nil && fasl_stream != SPID_NIL)
  1677. { put_checksum(NULL, subfile_checksum);
  1678. return NO;
  1679. }
  1680. current_output_directory = NULL;
  1681. /* Here I have to write a checksum to the current ouput dir */
  1682. if (d == NULL || (d->h.updated & D_WRITE_OK) == 0) return NO;
  1683. put_checksum(d->f, subfile_checksum);
  1684. setbits24(&current_output_entry->D_size, (int32)write_bytes_written);
  1685. r = fflush(d->f);
  1686. setbits32(d->h.eof, (int32)ftell(d->f));
  1687. /*
  1688. * I bring the directory at the start of the output file up to date at this
  1689. * stage - the effect is that if things crash somehow I have a better
  1690. * chance of resuming from where disaster hit.
  1691. */
  1692. fseek(d->f, 0, SEEK_SET);
  1693. if (fwrite(&d->h, sizeof(directory_header), 1, d->f) != 1) r = YES;
  1694. if (fwrite(&d->d[0], sizeof(directory_entry),
  1695. (size_t)get_dirsize(d->h), d->f) !=
  1696. (size_t)get_dirsize(d->h)) r = YES;
  1697. if (fflush(d->f) != 0) r = YES;
  1698. d->h.updated &= ~D_UPDATED;
  1699. current_output_entry = NULL;
  1700. return r;
  1701. #endif /* DEMO_MODE */
  1702. }
  1703. CSLbool finished_with(int j)
  1704. {
  1705. #ifdef DEMO_MODE
  1706. return YES;
  1707. #else
  1708. directory *d = fasl_files[j];
  1709. fasl_files[j] = NULL;
  1710. /*
  1711. * If the library concerned had been opened using (open-library ...) then
  1712. * the name stored in fasl_paths[] would have been allocated using malloc(),
  1713. * and just discarding it as here will represent a space-leak. Just for now
  1714. * I am going to accept that as an unimportant detail.
  1715. */
  1716. fasl_paths[j] = NULL;
  1717. if (d == NULL) return NO;
  1718. if (d->h.updated & D_COMPACT)
  1719. { int i;
  1720. long int hwm;
  1721. d->h.updated |= D_UPDATED;
  1722. sort_directory(d);
  1723. hwm = sizeof(directory_header) +
  1724. get_dirsize(d->h)*(long int)sizeof(directory_entry) +
  1725. REGISTRATION_SIZE;
  1726. for (i=0; i<get_dirused(d->h); i++)
  1727. { long int pos = bits32(&d->d[i].D_position);
  1728. if (pos != hwm)
  1729. { char *b = 16 + (char *)stack;
  1730. char small_buffer[64];
  1731. /* I add 4 to the length specified here to allow for checksums */
  1732. long int len = bits24(&d->d[i].D_size) + 4L;
  1733. long int newpos = hwm;
  1734. while (len != 0)
  1735. { size_t n =
  1736. (size_t)((CSL_PAGE_SIZE - 64 -
  1737. ((char *)stack - (char *)stackbase)) &
  1738. (~(int32)0xff));
  1739. /*
  1740. * I only perform compression of the file when I am in the process of stopping,
  1741. * and in that case the Lisp stack is not in use, so I use if as a buffer.
  1742. * WELL the above statement used to be true, but now it is not, since the
  1743. * function CLOSE-LIBRARY does exactly what I have declared is never
  1744. * possible. But all is not lost - I can afford to use that part of
  1745. * the stack that remains unused. In cases where CLOSE-LIBRARY is called
  1746. * just before a stack overflow was due the result will be utterly terrible
  1747. * (on speed) but it should still be correct. So what you will see is that
  1748. * I start my buffer 16 bytes above the active part of the stack, and
  1749. * let it run to within 48 bytes of the top of the stack page, but
  1750. * rounded down so I do transfers in multiples of 256 bytes. If there
  1751. * is really no (Lisp) stack free I use a 64 byte local buffer.
  1752. */
  1753. if (n == 0) b = small_buffer, n = sizeof(small_buffer);
  1754. if (len < (long int)n) n = (size_t)len;
  1755. fseek(d->f, pos, SEEK_SET);
  1756. fread(b, 1, n, d->f);
  1757. pos = ftell(d->f);
  1758. fseek(d->f, newpos, SEEK_SET);
  1759. fwrite(b, 1, n, d->f);
  1760. newpos = ftell(d->f);
  1761. len -= n;
  1762. }
  1763. setbits32(&d->d[i].D_position, (int32)hwm);
  1764. }
  1765. hwm += bits24(&d->d[i].D_size) + 4L;
  1766. }
  1767. fflush(d->f);
  1768. if (hwm != bits32(d->h.eof))
  1769. { truncate_file(d->f, hwm);
  1770. setbits32(d->h.eof, (int32)hwm);
  1771. }
  1772. }
  1773. if (d->h.updated & D_UPDATED)
  1774. {
  1775. if (fflush(d->f) != 0) return YES;
  1776. fseek(d->f, 0, SEEK_SET);
  1777. if (fwrite(&d->h, sizeof(directory_header), 1, d->f) != 1) return YES;
  1778. if (fwrite(&d->d[0], sizeof(directory_entry),
  1779. (size_t)get_dirsize(d->h), d->f) !=
  1780. (size_t)get_dirsize(d->h)) return YES;
  1781. if (fflush(d->f) != 0) return YES;
  1782. }
  1783. if (d->h.updated & D_PENDING) return NO;
  1784. else if (fclose(d->f) != 0) return YES;
  1785. else return NO;
  1786. #endif /* DEMO_MODE */
  1787. }
  1788. CSLbool Ifinished(void)
  1789. /*
  1790. * Indicates total completion of all work on image files, and so calls
  1791. * for things to be (finally) tidied up. Again returns YES of anything
  1792. * has gone wrong.
  1793. */
  1794. {
  1795. /*
  1796. * Need to close all files here... loads of calls to fflush and fclose.
  1797. * Actually only output files are a real issue here. And then only
  1798. * the ones that are flagged as needing compaction.
  1799. */
  1800. int j;
  1801. CSLbool failed = NO;
  1802. for (j=0; j<number_of_fasl_paths; j++)
  1803. if (finished_with(j)) failed = YES;
  1804. return failed;
  1805. }
  1806. int Igetc(void)
  1807. /*
  1808. * Returns next byte from current image sub-file, or EOF if either
  1809. * real end-of-file or on failure. As a special fudge here (ugh) I
  1810. * use a negative value of read_bytes_remaining to indicate that
  1811. * input should NOT be from the usual image-file mechanism, but from
  1812. * the currently selected standard input. Setting things up that way
  1813. * then supports processing of FASL files from almost arbitrary
  1814. * sources.
  1815. */
  1816. {
  1817. long int nn = read_bytes_remaining;
  1818. int c;
  1819. unsigned32 chk_temp;
  1820. if (nn <= 0)
  1821. { if (nn == 0) return EOF;
  1822. else
  1823. { Lisp_Object nil = C_nil;
  1824. Lisp_Object stream = qvalue(standard_input);
  1825. if (!is_stream(stream)) return EOF;
  1826. c = getc_stream(stream);
  1827. nil = C_nil;
  1828. if (exception_pending()) return EOF;
  1829. }
  1830. }
  1831. else
  1832. { read_bytes_remaining = nn - 1;
  1833. c = getc(binary_read_file);
  1834. }
  1835. if (c == EOF) return c;
  1836. update_crc(subfile_checksum, c);
  1837. if (crypt_active >= 0)
  1838. { if (crypt_count >= CRYPT_BLOCK)
  1839. { crypt_get_block(crypt_buffer);
  1840. crypt_count = 0;
  1841. }
  1842. c ^= crypt_buffer[crypt_count++];
  1843. }
  1844. return (c & 0xff);
  1845. }
  1846. #ifdef SIXTEEN_BIT
  1847. #define FREAD_CHUNK 0x4000
  1848. #endif
  1849. int32 Iread(void *buff, int32 size)
  1850. /*
  1851. * Reads (size) bytes into the indicated buffer. Returns number of
  1852. * bytes read. Decrypts if crypt_active >= 0.
  1853. */
  1854. {
  1855. unsigned char *p = (unsigned char *)buff;
  1856. int32 n = 0;
  1857. unsigned32 chk_temp;
  1858. int i;
  1859. size_t n1;
  1860. long int nn = read_bytes_remaining;
  1861. if (nn < 0)
  1862. { for (i=0; i<size; i++)
  1863. { int c = Igetc();
  1864. if (c == EOF) return i;
  1865. p[i] = c;
  1866. }
  1867. return i;
  1868. }
  1869. if (size > nn) size = (int32)nn; /* Do not go beyond end of file */
  1870. #ifdef FREAD_CHUNK
  1871. /*
  1872. * Iread can read a number of bytes that is specified by an int32, so on
  1873. * 16 bit implementations I will need to issue a sequence of calls to fread(),
  1874. * each transferring < 64Kbytes (in fact I do 16K at a time).
  1875. */
  1876. while (size >= FREAD_CHUNK)
  1877. { n1 = fread(p, 1, FREAD_CHUNK, binary_read_file);
  1878. for (i=0; i<(int)n1; i++)
  1879. { int c = p[i];
  1880. update_crc(subfile_checksum, c);
  1881. if (crypt_active >= 0)
  1882. { if (crypt_count >= CRYPT_BLOCK)
  1883. { crypt_get_block(crypt_buffer);
  1884. crypt_count = 0;
  1885. }
  1886. c ^= crypt_buffer[crypt_count++];
  1887. p[i] = c;
  1888. }
  1889. }
  1890. read_bytes_remaining -= n1;
  1891. if (n1 != FREAD_CHUNK) return n + n1;
  1892. p += n1;
  1893. size -= n1;
  1894. n += n1;
  1895. }
  1896. #endif
  1897. if (size == 0) return n;
  1898. n1 = fread(p, 1, (size_t)size, binary_read_file);
  1899. /*
  1900. * Updating the checksum here is probably a painful extra cost, but I count
  1901. * the security it gives me as worthwhile. I compute the checksum byte at a
  1902. * time so that it is not sensitive to the byte ordering of the machine used.
  1903. */
  1904. for (i=0; i<(int)n1; i++)
  1905. { int c = p[i];
  1906. update_crc(subfile_checksum, c);
  1907. if (crypt_active >= 0)
  1908. { if (crypt_count >= CRYPT_BLOCK)
  1909. { crypt_get_block(crypt_buffer);
  1910. crypt_count = 0;
  1911. }
  1912. c ^= crypt_buffer[crypt_count++];
  1913. p[i] = c;
  1914. }
  1915. }
  1916. read_bytes_remaining -= n1;
  1917. return n + n1;
  1918. }
  1919. long int Ioutsize()
  1920. {
  1921. return write_bytes_written;
  1922. }
  1923. CSLbool Iputc(int ch)
  1924. /*
  1925. * Puts one character into image system, returning YES if there
  1926. * was trouble.
  1927. */
  1928. {
  1929. #ifdef DEMO_MODE
  1930. return YES;
  1931. #else
  1932. unsigned32 chk_temp;
  1933. Lisp_Object nil = C_nil;
  1934. write_bytes_written++;
  1935. if (crypt_active >= 0)
  1936. { if (crypt_count >= CRYPT_BLOCK)
  1937. { crypt_get_block(crypt_buffer);
  1938. crypt_count = 0;
  1939. }
  1940. ch ^= crypt_buffer[crypt_count++];
  1941. }
  1942. update_crc(subfile_checksum, ch);
  1943. if (fasl_stream != nil && fasl_stream != SPID_NIL)
  1944. putc_stream(ch, fasl_stream);
  1945. else if (putc(ch, binary_write_file) == EOF) return YES;
  1946. return NO;
  1947. #endif /* DEMO_MODE */
  1948. }
  1949. #define FWRITE_CHUNK 0x4000
  1950. CSLbool Iwrite(void *buff, int32 size)
  1951. /*
  1952. * Writes (size) bytes from the given buffer, returning YES if trouble.
  1953. */
  1954. {
  1955. #ifdef DEMO_MODE
  1956. return YES;
  1957. #else
  1958. unsigned char *p = (unsigned char *)buff;
  1959. int32 i;
  1960. unsigned32 chk_temp;
  1961. Lisp_Object nil = C_nil;
  1962. if (crypt_active >= 0 ||
  1963. (fasl_stream != nil && fasl_stream != SPID_NIL))
  1964. {
  1965. /*
  1966. * Note that in this case the checksum is updated within Iputc() so I do
  1967. * not have to do anything special about it here.
  1968. */
  1969. for (i=0; i<size; i++)
  1970. if (Iputc(p[i])) return YES;
  1971. return NO;
  1972. }
  1973. /*
  1974. * If encrypted writing is active I will have gone through Iputc for
  1975. * every individual character and so will not get down to here. Thus the
  1976. * optimised calls to fwrite() can remain intact.
  1977. */
  1978. for (i=0; i<size; i++)
  1979. { /* Beware - update_crc is a macro and the {} block here is essential */
  1980. update_crc(subfile_checksum, p[i]);
  1981. }
  1982. write_bytes_written += size;
  1983. while (size >= FWRITE_CHUNK)
  1984. { if (fwrite(p, 1, FWRITE_CHUNK, binary_write_file) != FWRITE_CHUNK)
  1985. return YES;
  1986. p += FWRITE_CHUNK;
  1987. size -= FWRITE_CHUNK;
  1988. }
  1989. if (size == 0) return NO;
  1990. else return
  1991. (fwrite(p, 1, (size_t)size, binary_write_file) != (size_t)size);
  1992. #endif /* DEMO_MODE */
  1993. }
  1994. /*
  1995. * Now code that maps real pointers into references relative
  1996. * to page numbers. Here I will also go to the trouble of putting zero
  1997. * bytes in unused bits of memory - that will make checkpoint files
  1998. * compress better and will also make them independent of all actual
  1999. * addresses used on the host machine. Observe that the representation
  2000. * created has to depend a bit on the current page size.
  2001. */
  2002. #define PACK_PAGE_OFFSET(pg, of) ((pg << PAGE_BITS) + of)
  2003. static void unadjust(Lisp_Object *cp)
  2004. /*
  2005. * If p is a pointer to an object that has moved, unadjust it.
  2006. */
  2007. {
  2008. #ifndef DEMO_MODE
  2009. Lisp_Object nil = C_nil, p = (*cp); /* Beware "=*" anachronism! */
  2010. if (p == nil)
  2011. { *cp = SPID_NIL; /* Marks NIL in preserve files */
  2012. return;
  2013. }
  2014. else if (is_cons(p))
  2015. { int32 i;
  2016. for (i=0; i<heap_pages_count; i++)
  2017. { void *page = heap_pages[i];
  2018. char *base = (char *)doubleword_align_up((int32)page);
  2019. /*
  2020. * The next line is pretty dodgy - I want to decide which segment a
  2021. * pointer references, but pointer comparisons are only valid within
  2022. * single segments. I cast to int32 and cross my fingers!
  2023. */
  2024. if ((int32)base <= (int32)p &&
  2025. (int32)p <= (int32)(base+CSL_PAGE_SIZE))
  2026. { unsigned int offset = (unsigned int)((char *)p - base);
  2027. *cp = PACK_PAGE_OFFSET(i, offset);
  2028. return;
  2029. }
  2030. }
  2031. term_printf("\n[%.8lx] Cons address %.8lx not found in heap\n",
  2032. (long)cp, (long)p);
  2033. abort();
  2034. }
  2035. else if (!is_immed_or_cons(p))
  2036. { int32 i; /* vectors get relocated here */
  2037. for (i=0; i<vheap_pages_count; i++)
  2038. { void *page = vheap_pages[i];
  2039. char *base = (char *)doubleword_align_up((int32)page);
  2040. /* see comments above re the next line */
  2041. if ((int32)base <= (int32)p &&
  2042. (int32)p <= (int32)(base+CSL_PAGE_SIZE))
  2043. { unsigned int offset = (unsigned int)((char *)p - base);
  2044. *cp = PACK_PAGE_OFFSET(i, offset);
  2045. return;
  2046. }
  2047. }
  2048. term_printf("\n[%.8lx] Vector address %.8lx not found in heap\n",
  2049. (long)cp, (long)p);
  2050. abort();
  2051. }
  2052. #endif /* DEMO_MODE */
  2053. }
  2054. static void unadjust_consheap(void)
  2055. {
  2056. #ifndef DEMO_MODE
  2057. int32 page_number;
  2058. for (page_number = 0; page_number < heap_pages_count; page_number++)
  2059. { void *page = heap_pages[page_number];
  2060. char *low = (char *)doubleword_align_up((int32)page);
  2061. char *start = low + CSL_PAGE_SIZE;
  2062. char *fr = low + qcar(low);
  2063. /* The next line sets unused space in the page to be zero */
  2064. while ((fr -= sizeof(Lisp_Object)) != low) qcar(fr) = 0;
  2065. fr = low + qcar(low);
  2066. while (fr < start)
  2067. { unadjust((Lisp_Object *)fr);
  2068. fr += sizeof(Lisp_Object);
  2069. }
  2070. }
  2071. #endif /* DEMO_MODE */
  2072. }
  2073. static void convert_word_order(void *p)
  2074. {
  2075. if ((current_fp_rep & FP_WORD_ORDER) != 0)
  2076. { unsigned32 *f = (unsigned32 *)p;
  2077. unsigned32 w = f[0];
  2078. f[0] = f[1];
  2079. f[1] = w;
  2080. }
  2081. }
  2082. static struct entry_lookup
  2083. { int32 code;
  2084. int32 entry;
  2085. char *s;
  2086. } entry_lookup[entry_table_size];
  2087. static int MS_CDECL order_lookup_entries(void const *aa, void const *bb)
  2088. {
  2089. struct entry_lookup *a = (struct entry_lookup *)aa,
  2090. *b = (struct entry_lookup *)bb;
  2091. int32 ap = a->entry, bp = b->entry;
  2092. if (ap < bp) return -1;
  2093. else if (ap > bp) return 1;
  2094. else return 0;
  2095. }
  2096. static void set_up_entry_lookup()
  2097. /*
  2098. * This makes a sorted version of entries_table. Since the table is
  2099. * only a few dozen words long it hardly seems worth being too clever,
  2100. * but the C library provides qsort() for me so I use it.
  2101. */
  2102. {
  2103. int i;
  2104. for (i=0; i<entry_table_size; i++)
  2105. { entry_lookup[i].code = i;
  2106. entry_lookup[i].entry = (int32)entries_table[i].p;
  2107. entry_lookup[i].s = entries_table[i].s;
  2108. }
  2109. qsort((void *)entry_lookup,
  2110. entry_table_size, sizeof(struct entry_lookup),
  2111. order_lookup_entries);
  2112. }
  2113. static int32 code_up_fn(int32 e)
  2114. {
  2115. int low = 0, high = entry_table_size-1;
  2116. while (low < high)
  2117. { int mid = (high + low)/2;
  2118. int32 s = entry_lookup[mid].entry;
  2119. if (s == e) return entry_lookup[mid].code;
  2120. if (s < e) low = mid + 1;
  2121. else high = mid - 1;
  2122. }
  2123. if (low == high &&
  2124. entry_lookup[low].entry == e) return entry_lookup[low].code;
  2125. else return 0;
  2126. }
  2127. static void unadjust_vecheap(void)
  2128. {
  2129. #ifndef DEMO_MODE
  2130. int32 page_number, i;
  2131. for (page_number = 0; page_number < vheap_pages_count; page_number++)
  2132. { void *page = vheap_pages[page_number];
  2133. char *low = (char *)doubleword_align_up((int32)page);
  2134. char *high = low + (CSL_PAGE_SIZE - 8);
  2135. char *fr = low + qcar(low);
  2136. low += 8;
  2137. while (low < fr)
  2138. { Header h = *(Header *)low;
  2139. if (is_symbol_header(h))
  2140. { Lisp_Object s = (Lisp_Object)(low+TAG_SYMBOL);
  2141. ifn1(s) = code_up_fn(ifn1(s));
  2142. ifn2(s) = code_up_fn(ifn2(s));
  2143. ifnn(s) = code_up_fn(ifnn(s));
  2144. unadjust(&qvalue(s));
  2145. unadjust(&qenv(s));
  2146. unadjust(&qpname(s));
  2147. unadjust(&qplist(s));
  2148. unadjust(&qfastgets(s));
  2149. #ifdef COMMON
  2150. unadjust(&qpackage(s));
  2151. #endif
  2152. low += symhdr_length;
  2153. continue;
  2154. }
  2155. else switch (type_of_header(h))
  2156. {
  2157. #ifdef COMMON
  2158. case TYPE_RATNUM:
  2159. case TYPE_COMPLEX_NUM:
  2160. unadjust((Lisp_Object *)(low+4));
  2161. unadjust((Lisp_Object *)(low+8));
  2162. break;
  2163. #endif
  2164. case TYPE_HASH:
  2165. case TYPE_SIMPLE_VEC:
  2166. case TYPE_ARRAY:
  2167. case TYPE_STRUCTURE:
  2168. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  2169. unadjust((Lisp_Object *)(low+i));
  2170. break;
  2171. case TYPE_STREAM:
  2172. { Lisp_Object ss = (Lisp_Object)(low+TAG_VECTOR);
  2173. /*
  2174. * It might make rather good sense to close any file or pipe streams
  2175. * that I come across at this stage...
  2176. */
  2177. if (elt(ss, 4) == (int32)char_to_file &&
  2178. elt(ss, 3) != 0)
  2179. { fclose(stream_file(ss));
  2180. set_stream_write_fn(ss, char_to_illegal);
  2181. set_stream_write_other(ss, write_action_illegal);
  2182. set_stream_file(ss, NULL);
  2183. }
  2184. #ifdef PIPES
  2185. if (elt(ss, 4) == (int32)char_to_pipeout &&
  2186. elt(ss, 3) != 0)
  2187. { my_pclose(stream_file(ss));
  2188. set_stream_write_fn(ss, char_to_illegal);
  2189. set_stream_write_other(ss, write_action_illegal);
  2190. set_stream_file(ss, NULL);
  2191. }
  2192. #endif
  2193. if (elt(ss, 8) == (int32)char_from_file &&
  2194. elt(ss, 3) != 0)
  2195. { fclose(stream_file(ss));
  2196. set_stream_read_fn(ss, char_from_illegal);
  2197. set_stream_read_other(ss, read_action_illegal);
  2198. set_stream_file(ss, NULL);
  2199. }
  2200. elt(ss, 4) = code_up_fn(elt(ss, 4));
  2201. elt(ss, 5) = code_up_fn(elt(ss, 5));
  2202. elt(ss, 8) = code_up_fn(elt(ss, 8));
  2203. elt(ss, 9) = code_up_fn(elt(ss, 9));
  2204. }
  2205. case TYPE_MIXED1:
  2206. case TYPE_MIXED2:
  2207. case TYPE_MIXED3:
  2208. for (i=4; i<16; i+=4) unadjust((Lisp_Object *)(low+i));
  2209. break;
  2210. case TYPE_DOUBLE_FLOAT:
  2211. convert_word_order((void *)(low + 8));
  2212. break;
  2213. #ifdef COMMON
  2214. case TYPE_SINGLE_FLOAT:
  2215. break;
  2216. case TYPE_LONG_FLOAT:
  2217. /* If long floats were 3 words long I might need to adjust this code... */
  2218. convert_word_order((void *)(low + 8));
  2219. break;
  2220. #endif
  2221. default:
  2222. break;
  2223. }
  2224. low += doubleword_align_up(length_of_header(h));
  2225. }
  2226. /*
  2227. * Now clean up the unused space in the page...
  2228. */
  2229. while (low <= high)
  2230. { qcar(low) = 0;
  2231. qcdr(low) = 0;
  2232. low += 2*sizeof(Lisp_Object);
  2233. }
  2234. }
  2235. #endif /* DEMO_MODE */
  2236. }
  2237. static void unadjust_bpsheap(void)
  2238. {
  2239. #ifndef DEMO_MODE
  2240. int32 page_number;
  2241. for (page_number = 0; page_number < bps_pages_count; page_number++)
  2242. { void *page = bps_pages[page_number];
  2243. char *low = (char *)doubleword_align_up((int32)page);
  2244. char *fr = low + qcar(low);
  2245. /* Clean up unused space */
  2246. while ((fr -= sizeof(Lisp_Object)) != low) qcar(fr) = 0;
  2247. fr = low + qcar(low);
  2248. while (fr < low + CSL_PAGE_SIZE)
  2249. { Header h = *(Header *)fr;
  2250. #ifdef ENVIRONMENT_VECTORS_IN_BPS_HEAP
  2251. switch (type_of_header(h))
  2252. {
  2253. /* This option is not actually used at present... */
  2254. case TYPE_SIMPLE_VEC:
  2255. for (i=4; i<doubleword_align_up(length_of_header(h)); i+=4)
  2256. unadjust((Lisp_Object *)(fr+i));
  2257. break;
  2258. default:
  2259. break;
  2260. }
  2261. #endif
  2262. fr += doubleword_align_up(length_of_header(h));
  2263. }
  2264. }
  2265. #endif /* DEMO_MODE */
  2266. }
  2267. static void unadjust_all(void)
  2268. {
  2269. #ifndef DEMO_MODE
  2270. int32 i;
  2271. Lisp_Object nil = C_nil;
  2272. set_up_entry_lookup();
  2273. qheader(nil) = TAG_ODDS+TYPE_SYMBOL+SYM_SPECIAL_VAR;
  2274. qvalue(nil) = 0;
  2275. qenv(nil) = 0;
  2276. ifn1(nil) = 0;
  2277. ifn2(nil) = 0;
  2278. ifnn(nil) = 0;
  2279. unadjust(&(qpname(nil))); /* not a gensym */
  2280. unadjust(&(qplist(nil)));
  2281. unadjust(&(qfastgets(nil)));
  2282. #ifdef COMMON
  2283. unadjust(&(qpackage(nil)));
  2284. #endif
  2285. copy_into_nilseg(YES);
  2286. eq_hash_table_list = eq_hash_tables;
  2287. equal_hash_table_list = equal_hash_tables;
  2288. for (i = first_nil_offset; i<last_nil_offset; i++)
  2289. unadjust(&(((Lisp_Object *)nil)[i]));
  2290. copy_out_of_nilseg(YES);
  2291. unadjust_consheap();
  2292. unadjust_vecheap();
  2293. unadjust_bpsheap();
  2294. #endif /* DEMO_MODE */
  2295. }
  2296. void preserve_native_code()
  2297. {
  2298. #ifndef DEMO_MODE
  2299. /*
  2300. * I should maybe worry a little more here about IO errors...
  2301. */
  2302. int i;
  2303. if (!native_pages_changed) return;
  2304. if (open_output(NULL, -native_code_tag))
  2305. { term_printf("Failed to open module for native code storage\n");
  2306. return;
  2307. }
  2308. Iputc(native_pages_count & 0xff);
  2309. Iputc((native_pages_count>>8) & 0xff);
  2310. /*
  2311. * The FINAL native page will in general not be full, so I put a count of
  2312. * the number of bytes in it that are in use in its first word, and
  2313. * zero out the parts of it beyond there. Then the file compression that
  2314. * routinely use when writing into image files.
  2315. */
  2316. if (native_pages_count != 0)
  2317. { int32 p = (int32)native_pages[native_pages_count-1];
  2318. p = doubleword_align_up(p);
  2319. *(int32 *)p = native_fringe;
  2320. memset((char *)p+native_fringe, 0, CSL_PAGE_SIZE-native_fringe);
  2321. }
  2322. for (i=0; i<native_pages_count; i++)
  2323. { int32 p = (int32)native_pages[i];
  2324. p = doubleword_align_up(p);
  2325. Cfwrite((char *)p, CSL_PAGE_SIZE);
  2326. }
  2327. IcloseOutput();
  2328. #endif /* DEMO_MODE */
  2329. }
  2330. void preserve(char *banner)
  2331. {
  2332. #ifdef DEMO_MODE
  2333. err_printf("\nThe demo systen can not save a checkpoint file\n");
  2334. give_up();
  2335. return;
  2336. #else
  2337. int32 i;
  2338. CSLbool int_flag = NO;
  2339. Lisp_Object nil = C_nil;
  2340. /*
  2341. * I dump out any altered chunk of native code before I mangle the heap
  2342. * up.
  2343. */
  2344. preserve_native_code();
  2345. if (Iopen(NULL, 0, NO, NULL))
  2346. { err_printf("+++ PRESERVE failed to open image file\n");
  2347. return;
  2348. }
  2349. /*
  2350. * I set a whole bunch of things to NIL here. If spurious data is left over
  2351. * in global list-bases from a previous calculation it could clog up the
  2352. * heap and waste a lot of space...
  2353. */
  2354. #ifdef NILSEG_EXTERNS
  2355. for (i=0; i<=50; i++) workbase[i] = nil;
  2356. #else
  2357. for (i=work_0_offset; i<last_nil_offset; i++)
  2358. ((Lisp_Object *)nil)[i] = nil;
  2359. #endif
  2360. exit_tag = exit_value = catch_tags =
  2361. codevec = litvec = B_reg = faslvec = faslgensyms = nil;
  2362. reclaim(nil, "preserve", GC_PRESERVE, 0); /* FULL garbage collection */
  2363. nil = C_nil;
  2364. /*
  2365. * if the user generated a SIGINT this is where it gets noticed...
  2366. */
  2367. if (exception_pending())
  2368. { flip_exception();
  2369. int_flag = YES;
  2370. }
  2371. { char msg[128];
  2372. time_t t0 = time(0);
  2373. for (i=0; i<128; i++) msg[i] = ' ';
  2374. if (banner[0] == 0) msg[0] = 0;
  2375. else sprintf(msg, "%.60s", banner);
  2376. /* 26 bytes starting from byte 64 shows the time of the dump */
  2377. sprintf(msg+64, "%.25s\n", ctime(&t0));
  2378. /* 16 bytes starting at byte 90 are for a checksum of the u01.c etc checks */
  2379. get_user_files_checksum((unsigned char *)&msg[90]);
  2380. /* 106 to 109 free at present but available if checksum goes to 160 bits */
  2381. /* 1 byte at 110 marks an encrypted image (work in progress!) */
  2382. msg[110] = 0;
  2383. /* The final byte at 111 indicates whether compression is to be used */
  2384. { int32 cc = compression_worth_while;
  2385. int fg = 0;
  2386. while (cc > 128) fg++, cc >>= 1;
  2387. msg[111] = fg;
  2388. }
  2389. Cfwrite(msg, 112); /* Exactly 112 bytes in the header records */
  2390. }
  2391. unadjust_all(); /* Turn all pointers into base-offset form */
  2392. Cfwrite("\nNilseg:", 8);
  2393. copy_into_nilseg(YES);
  2394. { Lisp_Object saver[9];
  2395. for (i=0; i<9; i++)
  2396. saver[i] = ((Lisp_Object *)nil)[i+13],
  2397. ((Lisp_Object *)nil)[i+13] = 0;
  2398. /* codefringe */
  2399. /* codelimit */
  2400. /* stacklimit */
  2401. /* ... ditto */
  2402. /* ... ditto */
  2403. /* fringe */
  2404. /* heaplimit */
  2405. /* vheaplimit */
  2406. /* vfringe */
  2407. Cfwrite((char *)nil, sizeof(Lisp_Object)*last_nil_offset);
  2408. for (i=0; i<9; i++)
  2409. ((Lisp_Object *)nil)[i+13] = saver[i];
  2410. }
  2411. Cfwrite((char *)&heap_pages_count, sizeof(heap_pages_count));
  2412. Cfwrite((char *)&vheap_pages_count, sizeof(vheap_pages_count));
  2413. Cfwrite((char *)&bps_pages_count, sizeof(bps_pages_count));
  2414. Cfwrite("\nVecseg:", 8);
  2415. for (i=0; i<vheap_pages_count; i++)
  2416. { int32 p = (int32)vheap_pages[i];
  2417. Cfwrite((char *)doubleword_align_up(p), CSL_PAGE_SIZE);
  2418. }
  2419. Cfwrite("\nConsseg", 8);
  2420. for (i=0; i<heap_pages_count; i++)
  2421. { int32 p = (int32)heap_pages[i];
  2422. Cfwrite((char *)doubleword_align_up(p), CSL_PAGE_SIZE);
  2423. }
  2424. Cfwrite("\nCodeseg", 8);
  2425. for (i=0; i<bps_pages_count; i++)
  2426. { int32 p = (int32)bps_pages[i];
  2427. Cfwrite((char *)doubleword_align_up(p), CSL_PAGE_SIZE);
  2428. }
  2429. #ifndef COMMON
  2430. Cfwrite("\n\nEnd of CSL dump file\n\n", 24);
  2431. #else
  2432. Cfwrite("\n\nEnd of CCL dump file\n\n", 24);
  2433. #endif
  2434. /*
  2435. * Here I pad the image file to be a multiple of 4 bytes long. Since it is a
  2436. * binary file the '\n' characters I put in will always be just 1 byte each
  2437. * (for text files that might have expanded). See comments in fasl.c for
  2438. * a diatribe about why I do this.
  2439. */
  2440. { int k = (int)((-write_bytes_written) & 3);
  2441. while (k != 0) k--, Iputc(NEWLINE_CHAR);
  2442. }
  2443. /*
  2444. flip_needed = NO; Since I stop after (preserve) thses lines are unnecessary?
  2445. old_fp_rep = current_fp_rep;
  2446. */
  2447. /*
  2448. * I need to check for write errors here and moan if there were any...
  2449. */
  2450. if (IcloseOutput()) error(0, err_write_err);
  2451. if (int_flag) term_printf("\nInterrupt during (preserve) was ignored\n");
  2452. return;
  2453. #endif /* DEMO_MODE */
  2454. }
  2455. /* end of file preserve.c */