fns1.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /* fns1.c Copyright (C) 1989-96 Codemist Ltd */
  2. /*
  3. * Basic functions part 1.
  4. */
  5. /* Signature: 4e5258ab 07-Mar-2000 */
  6. #include <stdarg.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "machine.h"
  10. #include "tags.h"
  11. #include "cslerror.h"
  12. #include "externs.h"
  13. #include "entries.h"
  14. #include "arith.h"
  15. #ifdef TIMEOUT
  16. #include "timeout.h"
  17. #endif
  18. /*****************************************************************************/
  19. /* Some basic functions */
  20. /*****************************************************************************/
  21. Lisp_Object integerp(Lisp_Object p)
  22. {
  23. Lisp_Object nil = C_nil;
  24. int tag = ((int)p) & TAG_BITS;
  25. if (tag == TAG_FIXNUM) return lisp_true;
  26. if (tag == TAG_NUMBERS)
  27. { Header h = *(Header *)((char *)p - TAG_NUMBERS);
  28. if (type_of_header(h) == TYPE_BIGNUM) return lisp_true;
  29. }
  30. return nil;
  31. }
  32. /*****************************************************************************/
  33. /* Storage allocation. */
  34. /*****************************************************************************/
  35. Lisp_Object cons(Lisp_Object a, Lisp_Object b)
  36. {
  37. nil_as_base
  38. Lisp_Object r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  39. qcar(r) = a;
  40. qcdr(r) = b;
  41. fringe = r;
  42. if ((char *)r <= (char *)heaplimit)
  43. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  44. "internal cons", GC_CONS, 0);
  45. else return (Lisp_Object)((char *)r + TAG_CONS);
  46. }
  47. Lisp_Object cons_no_gc(Lisp_Object a, Lisp_Object b)
  48. {
  49. nil_as_base
  50. Lisp_Object r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  51. qcar(r) = a;
  52. qcdr(r) = b;
  53. fringe = r;
  54. return (Lisp_Object)((char *)r + TAG_CONS);
  55. }
  56. /*
  57. * cons_gc_test() MUST be called after any sequence of cons_no_gc() calls.
  58. */
  59. Lisp_Object cons_gc_test(Lisp_Object p)
  60. {
  61. nil_as_base
  62. if ((char *)fringe <= (char *)heaplimit)
  63. return reclaim(p, "cons gc test", GC_CONS, 0);
  64. else return p;
  65. }
  66. Lisp_Object ncons(Lisp_Object a)
  67. {
  68. Lisp_Object nil = C_nil;
  69. Lisp_Object r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  70. qcar(r) = a;
  71. qcdr(r) = nil;
  72. fringe = r;
  73. if ((char *)r <= (char *)heaplimit)
  74. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  75. "internal ncons", GC_CONS, 0);
  76. else return (Lisp_Object)((char *)r + TAG_CONS);
  77. }
  78. Lisp_Object list2(Lisp_Object a, Lisp_Object b)
  79. {
  80. /* Note that building two cons cells at once saves some overhead here */
  81. Lisp_Object nil = C_nil;
  82. Lisp_Object r = (Lisp_Object)((char *)fringe - 2*sizeof(Cons_Cell));
  83. qcar(r) = a;
  84. qcdr(r) = (Lisp_Object)((char *)r + sizeof(Cons_Cell) + TAG_CONS);
  85. qcar((char *)r+sizeof(Cons_Cell)) = b;
  86. qcdr((char *)r+sizeof(Cons_Cell)) = nil;
  87. fringe = r;
  88. if ((char *)r <= (char *)heaplimit)
  89. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  90. "internal list2", GC_CONS, 0);
  91. else return (Lisp_Object)((char *)r + TAG_CONS);
  92. }
  93. Lisp_Object list2star(Lisp_Object a, Lisp_Object b, Lisp_Object c)
  94. {
  95. nil_as_base
  96. Lisp_Object r = (Lisp_Object)((char *)fringe - 2*sizeof(Cons_Cell));
  97. qcar(r) = a;
  98. qcdr(r) = (Lisp_Object)((char *)r + sizeof(Cons_Cell) + TAG_CONS);
  99. qcar((char *)r+sizeof(Cons_Cell)) = b;
  100. qcdr((char *)r+sizeof(Cons_Cell)) = c;
  101. fringe = r;
  102. if ((char *)r <= (char *)heaplimit)
  103. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  104. "internal list2*", GC_CONS, 0);
  105. else return (Lisp_Object)((char *)r + TAG_CONS);
  106. }
  107. Lisp_Object acons(Lisp_Object a, Lisp_Object b, Lisp_Object c)
  108. {
  109. nil_as_base
  110. Lisp_Object r = (Lisp_Object)((char *)fringe - 2*sizeof(Cons_Cell));
  111. qcar(r) = (Lisp_Object)((char *)r + sizeof(Cons_Cell) + TAG_CONS);
  112. qcdr(r) = c;
  113. qcar((char *)r+sizeof(Cons_Cell)) = a;
  114. qcdr((char *)r+sizeof(Cons_Cell)) = b;
  115. fringe = r;
  116. if ((char *)r <= (char *)heaplimit)
  117. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  118. "internal acons", GC_CONS, 0);
  119. else return (Lisp_Object)((char *)r + TAG_CONS);
  120. }
  121. Lisp_Object list3(Lisp_Object a, Lisp_Object b, Lisp_Object c)
  122. {
  123. Lisp_Object nil = C_nil;
  124. Lisp_Object r = (Lisp_Object)((char *)fringe - 3*sizeof(Cons_Cell));
  125. qcar(r) = a;
  126. qcdr(r) = (Lisp_Object)((char *)r + sizeof(Cons_Cell) + TAG_CONS);
  127. qcar((char *)r+sizeof(Cons_Cell)) = b;
  128. qcdr((char *)r+sizeof(Cons_Cell)) =
  129. (Lisp_Object)((char *)r + 2*sizeof(Cons_Cell) + TAG_CONS);
  130. qcar((char *)r+2*sizeof(Cons_Cell)) = c;
  131. qcdr((char *)r+2*sizeof(Cons_Cell)) = nil;
  132. fringe = r;
  133. if ((char *)r <= (char *)heaplimit)
  134. return reclaim((Lisp_Object)((char *)r + TAG_CONS),
  135. "internal list3", GC_CONS, 0);
  136. else return (Lisp_Object)((char *)r + TAG_CONS);
  137. }
  138. /*****************************************************************************/
  139. /*****************************************************************************/
  140. /*** Lisp-callable versions of all the above ***/
  141. /*****************************************************************************/
  142. /*****************************************************************************/
  143. /*
  144. * The set of car/cdr combinations here seem pretty dull, but they
  145. * are fairly important for performance...
  146. */
  147. Lisp_Object Lcar(Lisp_Object nil, Lisp_Object a)
  148. {
  149. CSL_IGNORE(nil);
  150. if (!car_legal(a)) return error(1, err_bad_car, a);
  151. else return onevalue(qcar(a));
  152. }
  153. /*
  154. * (car* a) = (car a) if a is non-atomic, but just a otherwise.
  155. */
  156. Lisp_Object Lcar_star(Lisp_Object nil, Lisp_Object a)
  157. {
  158. CSL_IGNORE(nil);
  159. if (!car_legal(a)) return onevalue(a);
  160. else return onevalue(qcar(a));
  161. }
  162. Lisp_Object Lcdr(Lisp_Object nil, Lisp_Object a)
  163. {
  164. CSL_IGNORE(nil);
  165. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  166. else return onevalue(qcdr(a));
  167. }
  168. Lisp_Object Lcaar(Lisp_Object nil, Lisp_Object a)
  169. {
  170. CSL_IGNORE(nil);
  171. if (!car_legal(a)) return error(1, err_bad_car, a);
  172. else a = qcar(a);
  173. if (!car_legal(a)) return error(1, err_bad_car, a);
  174. else return onevalue(qcar(a));
  175. }
  176. Lisp_Object Lcadr(Lisp_Object nil, Lisp_Object a)
  177. {
  178. CSL_IGNORE(nil);
  179. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  180. else a = qcdr(a);
  181. if (!car_legal(a)) return error(1, err_bad_car, a);
  182. else return onevalue(qcar(a));
  183. }
  184. Lisp_Object Lcdar(Lisp_Object nil, Lisp_Object a)
  185. {
  186. CSL_IGNORE(nil);
  187. if (!car_legal(a)) return error(1, err_bad_car, a);
  188. else a = qcar(a);
  189. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  190. else return onevalue(qcdr(a));
  191. }
  192. Lisp_Object Lcddr(Lisp_Object nil, Lisp_Object a)
  193. {
  194. CSL_IGNORE(nil);
  195. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  196. else a = qcdr(a);
  197. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  198. else return onevalue(qcdr(a));
  199. }
  200. Lisp_Object Lcaaar(Lisp_Object nil, Lisp_Object a)
  201. {
  202. CSL_IGNORE(nil);
  203. if (!car_legal(a)) return error(1, err_bad_car, a);
  204. else a = qcar(a);
  205. if (!car_legal(a)) return error(1, err_bad_car, a);
  206. else a = qcar(a);
  207. if (!car_legal(a)) return error(1, err_bad_car, a);
  208. else return onevalue(qcar(a));
  209. }
  210. Lisp_Object Lcaadr(Lisp_Object nil, Lisp_Object a)
  211. {
  212. CSL_IGNORE(nil);
  213. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  214. else a = qcdr(a);
  215. if (!car_legal(a)) return error(1, err_bad_car, a);
  216. else a = qcar(a);
  217. if (!car_legal(a)) return error(1, err_bad_car, a);
  218. else return onevalue(qcar(a));
  219. }
  220. Lisp_Object Lcadar(Lisp_Object nil, Lisp_Object a)
  221. {
  222. CSL_IGNORE(nil);
  223. if (!car_legal(a)) return error(1, err_bad_car, a);
  224. else a = qcar(a);
  225. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  226. else a = qcdr(a);
  227. if (!car_legal(a)) return error(1, err_bad_car, a);
  228. else return onevalue(qcar(a));
  229. }
  230. Lisp_Object Lcaddr(Lisp_Object nil, Lisp_Object a)
  231. {
  232. CSL_IGNORE(nil);
  233. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  234. else a = qcdr(a);
  235. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  236. else a = qcdr(a);
  237. if (!car_legal(a)) return error(1, err_bad_car, a);
  238. else return onevalue(qcar(a));
  239. }
  240. Lisp_Object Lcdaar(Lisp_Object nil, Lisp_Object a)
  241. {
  242. CSL_IGNORE(nil);
  243. if (!car_legal(a)) return error(1, err_bad_car, a);
  244. else a = qcar(a);
  245. if (!car_legal(a)) return error(1, err_bad_car, a);
  246. else a = qcar(a);
  247. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  248. else return onevalue(qcdr(a));
  249. }
  250. Lisp_Object Lcdadr(Lisp_Object nil, Lisp_Object a)
  251. {
  252. CSL_IGNORE(nil);
  253. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  254. else a = qcdr(a);
  255. if (!car_legal(a)) return error(1, err_bad_car, a);
  256. else a = qcar(a);
  257. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  258. else return onevalue(qcdr(a));
  259. }
  260. Lisp_Object Lcddar(Lisp_Object nil, Lisp_Object a)
  261. {
  262. CSL_IGNORE(nil);
  263. if (!car_legal(a)) return error(1, err_bad_car, a);
  264. else a = qcar(a);
  265. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  266. else a = qcdr(a);
  267. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  268. else return onevalue(qcdr(a));
  269. }
  270. Lisp_Object Lcdddr(Lisp_Object nil, Lisp_Object a)
  271. {
  272. CSL_IGNORE(nil);
  273. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  274. else a = qcdr(a);
  275. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  276. else a = qcdr(a);
  277. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  278. else return onevalue(qcdr(a));
  279. }
  280. Lisp_Object Lcaaaar(Lisp_Object nil, Lisp_Object a)
  281. {
  282. CSL_IGNORE(nil);
  283. if (!car_legal(a)) return error(1, err_bad_car, a);
  284. else a = qcar(a);
  285. if (!car_legal(a)) return error(1, err_bad_car, a);
  286. else a = qcar(a);
  287. if (!car_legal(a)) return error(1, err_bad_car, a);
  288. else a = qcar(a);
  289. if (!car_legal(a)) return error(1, err_bad_car, a);
  290. else return onevalue(qcar(a));
  291. }
  292. Lisp_Object Lcaaadr(Lisp_Object nil, Lisp_Object a)
  293. {
  294. CSL_IGNORE(nil);
  295. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  296. else a = qcdr(a);
  297. if (!car_legal(a)) return error(1, err_bad_car, a);
  298. else a = qcar(a);
  299. if (!car_legal(a)) return error(1, err_bad_car, a);
  300. else a = qcar(a);
  301. if (!car_legal(a)) return error(1, err_bad_car, a);
  302. else return onevalue(qcar(a));
  303. }
  304. Lisp_Object Lcaadar(Lisp_Object nil, Lisp_Object a)
  305. {
  306. CSL_IGNORE(nil);
  307. if (!car_legal(a)) return error(1, err_bad_car, a);
  308. else a = qcar(a);
  309. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  310. else a = qcdr(a);
  311. if (!car_legal(a)) return error(1, err_bad_car, a);
  312. else a = qcar(a);
  313. if (!car_legal(a)) return error(1, err_bad_car, a);
  314. else return onevalue(qcar(a));
  315. }
  316. Lisp_Object Lcaaddr(Lisp_Object nil, Lisp_Object a)
  317. {
  318. CSL_IGNORE(nil);
  319. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  320. else a = qcdr(a);
  321. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  322. else a = qcdr(a);
  323. if (!car_legal(a)) return error(1, err_bad_car, a);
  324. else a = qcar(a);
  325. if (!car_legal(a)) return error(1, err_bad_car, a);
  326. else return onevalue(qcar(a));
  327. }
  328. Lisp_Object Lcadaar(Lisp_Object nil, Lisp_Object a)
  329. {
  330. CSL_IGNORE(nil);
  331. if (!car_legal(a)) return error(1, err_bad_car, a);
  332. else a = qcar(a);
  333. if (!car_legal(a)) return error(1, err_bad_car, a);
  334. else a = qcar(a);
  335. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  336. else a = qcdr(a);
  337. if (!car_legal(a)) return error(1, err_bad_car, a);
  338. else return onevalue(qcar(a));
  339. }
  340. Lisp_Object Lcadadr(Lisp_Object nil, Lisp_Object a)
  341. {
  342. CSL_IGNORE(nil);
  343. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  344. else a = qcdr(a);
  345. if (!car_legal(a)) return error(1, err_bad_car, a);
  346. else a = qcar(a);
  347. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  348. else a = qcdr(a);
  349. if (!car_legal(a)) return error(1, err_bad_car, a);
  350. else return onevalue(qcar(a));
  351. }
  352. Lisp_Object Lcaddar(Lisp_Object nil, Lisp_Object a)
  353. {
  354. CSL_IGNORE(nil);
  355. if (!car_legal(a)) return error(1, err_bad_car, a);
  356. else a = qcar(a);
  357. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  358. else a = qcdr(a);
  359. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  360. else a = qcdr(a);
  361. if (!car_legal(a)) return error(1, err_bad_car, a);
  362. else return onevalue(qcar(a));
  363. }
  364. Lisp_Object Lcadddr(Lisp_Object nil, Lisp_Object a)
  365. {
  366. CSL_IGNORE(nil);
  367. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  368. else a = qcdr(a);
  369. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  370. else a = qcdr(a);
  371. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  372. else a = qcdr(a);
  373. if (!car_legal(a)) return error(1, err_bad_car, a);
  374. else return onevalue(qcar(a));
  375. }
  376. Lisp_Object Lcdaaar(Lisp_Object nil, Lisp_Object a)
  377. {
  378. CSL_IGNORE(nil);
  379. if (!car_legal(a)) return error(1, err_bad_car, a);
  380. else a = qcar(a);
  381. if (!car_legal(a)) return error(1, err_bad_car, a);
  382. else a = qcar(a);
  383. if (!car_legal(a)) return error(1, err_bad_car, a);
  384. else a = qcar(a);
  385. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  386. else return onevalue(qcdr(a));
  387. }
  388. Lisp_Object Lcdaadr(Lisp_Object nil, Lisp_Object a)
  389. {
  390. CSL_IGNORE(nil);
  391. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  392. else a = qcdr(a);
  393. if (!car_legal(a)) return error(1, err_bad_car, a);
  394. else a = qcar(a);
  395. if (!car_legal(a)) return error(1, err_bad_car, a);
  396. else a = qcar(a);
  397. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  398. else return onevalue(qcdr(a));
  399. }
  400. Lisp_Object Lcdadar(Lisp_Object nil, Lisp_Object a)
  401. {
  402. CSL_IGNORE(nil);
  403. if (!car_legal(a)) return error(1, err_bad_car, a);
  404. else a = qcar(a);
  405. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  406. else a = qcdr(a);
  407. if (!car_legal(a)) return error(1, err_bad_car, a);
  408. else a = qcar(a);
  409. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  410. else return onevalue(qcdr(a));
  411. }
  412. Lisp_Object Lcdaddr(Lisp_Object nil, Lisp_Object a)
  413. {
  414. CSL_IGNORE(nil);
  415. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  416. else a = qcdr(a);
  417. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  418. else a = qcdr(a);
  419. if (!car_legal(a)) return error(1, err_bad_car, a);
  420. else a = qcar(a);
  421. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  422. else return onevalue(qcdr(a));
  423. }
  424. Lisp_Object Lcddaar(Lisp_Object nil, Lisp_Object a)
  425. {
  426. CSL_IGNORE(nil);
  427. if (!car_legal(a)) return error(1, err_bad_car, a);
  428. else a = qcar(a);
  429. if (!car_legal(a)) return error(1, err_bad_car, a);
  430. else a = qcar(a);
  431. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  432. else a = qcdr(a);
  433. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  434. else return onevalue(qcdr(a));
  435. }
  436. Lisp_Object Lcddadr(Lisp_Object nil, Lisp_Object a)
  437. {
  438. CSL_IGNORE(nil);
  439. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  440. else a = qcdr(a);
  441. if (!car_legal(a)) return error(1, err_bad_car, a);
  442. else a = qcar(a);
  443. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  444. else a = qcdr(a);
  445. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  446. else return onevalue(qcdr(a));
  447. }
  448. Lisp_Object Lcdddar(Lisp_Object nil, Lisp_Object a)
  449. {
  450. CSL_IGNORE(nil);
  451. if (!car_legal(a)) return error(1, err_bad_car, a);
  452. else a = qcar(a);
  453. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  454. else a = qcdr(a);
  455. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  456. else a = qcdr(a);
  457. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  458. else return onevalue(qcdr(a));
  459. }
  460. Lisp_Object Lcddddr(Lisp_Object nil, Lisp_Object a)
  461. {
  462. CSL_IGNORE(nil);
  463. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  464. else a = qcdr(a);
  465. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  466. else a = qcdr(a);
  467. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  468. else a = qcdr(a);
  469. if (!car_legal(a)) return error(1, err_bad_cdr, a);
  470. else return onevalue(qcdr(a));
  471. }
  472. Lisp_Object Lrplaca(Lisp_Object nil,
  473. Lisp_Object a, Lisp_Object b)
  474. {
  475. CSL_IGNORE(nil);
  476. if (!consp(a)) return error(1, err_bad_rplac, a);
  477. qcar(a) = b;
  478. return onevalue(a);
  479. }
  480. Lisp_Object Lrplacd(Lisp_Object nil,
  481. Lisp_Object a, Lisp_Object b)
  482. {
  483. CSL_IGNORE(nil);
  484. if (!consp(a)) return error(1, err_bad_rplac, a);
  485. qcdr(a) = b;
  486. return onevalue(a);
  487. }
  488. Lisp_Object Lsymbolp(Lisp_Object nil, Lisp_Object a)
  489. {
  490. return onevalue(Lispify_predicate(symbolp(a)));
  491. }
  492. Lisp_Object Latom(Lisp_Object nil, Lisp_Object a)
  493. {
  494. return onevalue(Lispify_predicate(!consp(a)));
  495. }
  496. Lisp_Object Lconsp(Lisp_Object nil, Lisp_Object a)
  497. {
  498. return onevalue(Lispify_predicate(consp(a)));
  499. }
  500. Lisp_Object Lconstantp(Lisp_Object nil, Lisp_Object a)
  501. /*
  502. * This version is as required for Standard Lisp - it is inadequate
  503. * for Common Lisp.
  504. */
  505. {
  506. /*
  507. * Standard Lisp requires that I report that "Function Pointers" are
  508. * "constant" here. It is not at all clear that I have a way of
  509. * doing that. I will go some way my ensuring that code-vectors are.
  510. */
  511. #ifdef COMMON
  512. return onevalue(Lispify_predicate(
  513. a == nil || a == lisp_true ||
  514. is_char(a) ||
  515. is_number(a) ||
  516. is_vector(a) ||
  517. is_bps(a)));
  518. #else
  519. return onevalue(Lispify_predicate(
  520. is_number(a) ||
  521. is_vector(a) || /* Vectors include strings here */
  522. is_bps(a)));
  523. #endif
  524. }
  525. Lisp_Object Lidentity(Lisp_Object nil, Lisp_Object a)
  526. {
  527. CSL_IGNORE(nil);
  528. return onevalue(a);
  529. }
  530. #ifdef COMMON
  531. Lisp_Object Llistp(Lisp_Object nil, Lisp_Object a)
  532. {
  533. return onevalue(Lispify_predicate(is_cons(a)));
  534. }
  535. #endif
  536. Lisp_Object Lnumberp(Lisp_Object nil, Lisp_Object a)
  537. {
  538. return onevalue(Lispify_predicate(is_number(a)));
  539. }
  540. Lisp_Object Lintegerp(Lisp_Object nil, Lisp_Object a)
  541. {
  542. CSL_IGNORE(nil);
  543. return onevalue(integerp(a));
  544. }
  545. Lisp_Object Leq_safe(Lisp_Object nil, Lisp_Object a)
  546. {
  547. /*
  548. * True if you can safely use EQ tests to check equality. Thus true for
  549. * things that are represented in "immediate" form...
  550. */
  551. #ifdef COMMON
  552. return onevalue(is_fixnum(a) ||
  553. is_sfloat(a) ||
  554. is_odds(a) ? lisp_true : nil);
  555. #else
  556. return onevalue(is_fixnum(a) ||
  557. is_odds(a) ? lisp_true : nil);
  558. #endif
  559. }
  560. Lisp_Object Lfixp(Lisp_Object nil, Lisp_Object a)
  561. {
  562. #ifdef COMMON
  563. return onevalue(is_fixnum(a) ? lisp_true : nil);
  564. #else
  565. /*
  566. * Standard Lisp defines fixp to say yes to bignums as well as
  567. * fixnums.
  568. */
  569. CSL_IGNORE(nil);
  570. return onevalue(integerp(a));
  571. #endif
  572. }
  573. Lisp_Object Lfloatp(Lisp_Object nil, Lisp_Object p)
  574. {
  575. int tag = TAG_BITS & (int)p;
  576. #ifdef COMMON
  577. if (tag == TAG_SFLOAT) return onevalue(lisp_true);
  578. #endif
  579. if (tag == TAG_BOXFLOAT) return onevalue(lisp_true);
  580. else return onevalue(nil);
  581. }
  582. #ifdef COMMON
  583. static Lisp_Object Lshort_floatp(Lisp_Object nil, Lisp_Object p)
  584. {
  585. int tag = TAG_BITS & (int)p;
  586. if (tag == TAG_SFLOAT) return onevalue(lisp_true);
  587. else return onevalue(nil);
  588. }
  589. static Lisp_Object Lsingle_floatp(Lisp_Object nil, Lisp_Object p)
  590. {
  591. int tag = TAG_BITS & (int)p;
  592. if (tag == TAG_BOXFLOAT &&
  593. type_of_header(flthdr(p)) == TYPE_SINGLE_FLOAT)
  594. return onevalue(lisp_true);
  595. else return onevalue(nil);
  596. }
  597. static Lisp_Object Ldouble_floatp(Lisp_Object nil, Lisp_Object p)
  598. {
  599. int tag = TAG_BITS & (int)p;
  600. if (tag == TAG_BOXFLOAT &&
  601. type_of_header(flthdr(p)) == TYPE_DOUBLE_FLOAT)
  602. return onevalue(lisp_true);
  603. else return onevalue(nil);
  604. }
  605. static Lisp_Object Llong_floatp(Lisp_Object nil, Lisp_Object p)
  606. {
  607. int tag = TAG_BITS & (int)p;
  608. if (tag == TAG_BOXFLOAT &&
  609. type_of_header(flthdr(p)) == TYPE_LONG_FLOAT)
  610. return onevalue(lisp_true);
  611. else return onevalue(nil);
  612. }
  613. Lisp_Object Lrationalp(Lisp_Object nil, Lisp_Object a)
  614. {
  615. CSL_IGNORE(nil);
  616. return onevalue(
  617. Lispify_predicate(
  618. is_fixnum(a) ||
  619. (is_numbers(a) &&
  620. (is_bignum(a) || is_ratio(a)))));
  621. }
  622. Lisp_Object Lcomplexp(Lisp_Object nil, Lisp_Object a)
  623. {
  624. CSL_IGNORE(nil);
  625. return onevalue(Lispify_predicate(is_numbers(a) && is_complex(a)));
  626. }
  627. CSLbool complex_stringp(Lisp_Object a)
  628. /*
  629. * true if the arg is a string, but NOT a simple string. In general
  630. * when this is true simplify_string() will then be called to do
  631. * an adjustment.
  632. */
  633. {
  634. Header h;
  635. Lisp_Object w, nil = C_nil;
  636. if (!is_vector(a)) return NO;
  637. h = vechdr(a);
  638. if (type_of_header(h) != TYPE_ARRAY) return NO;
  639. /*
  640. * Note that the cheery Common Lisp Committee decided the abolish the
  641. * separate type 'string-char, so the test here is maybe dubious...
  642. */
  643. else if (elt(a, 0) != string_char_sym) return NO;
  644. w = elt(a, 1);
  645. if (!consp(w) || consp(qcdr(w))) return NO;
  646. else return YES;
  647. }
  648. #endif
  649. CSLbool stringp(Lisp_Object a)
  650. /*
  651. * True if arg is a simple OR a general string
  652. */
  653. {
  654. Header h;
  655. #ifdef COMMON
  656. Lisp_Object w, nil = C_nil;
  657. #endif
  658. if (!is_vector(a)) return NO;
  659. h = vechdr(a);
  660. if (type_of_header(h) == TYPE_STRING) return YES;
  661. #ifdef COMMON
  662. else if (type_of_header(h) != TYPE_ARRAY) return NO;
  663. /*
  664. * Beware abolition of 'string-char
  665. */
  666. else if (elt(a, 0) != string_char_sym) return NO;
  667. w = elt(a, 1);
  668. if (!consp(w) || consp(qcdr(w))) return NO;
  669. else return YES;
  670. #else
  671. else return NO;
  672. #endif
  673. }
  674. Lisp_Object Lstringp(Lisp_Object nil, Lisp_Object a)
  675. /*
  676. * simple-string-p
  677. */
  678. {
  679. if (!(is_vector(a)) || type_of_header(vechdr(a)) != TYPE_STRING)
  680. return onevalue(nil);
  681. else return onevalue(lisp_true);
  682. }
  683. #ifdef COMMON
  684. static Lisp_Object Lc_stringp(Lisp_Object nil, Lisp_Object a)
  685. {
  686. return onevalue(Lispify_predicate(stringp(a)));
  687. }
  688. #endif
  689. Lisp_Object Lhash_table_p(Lisp_Object nil, Lisp_Object a)
  690. /*
  691. * hash-table-p
  692. */
  693. {
  694. if (!(is_vector(a)) || type_of_header(vechdr(a)) != TYPE_HASH)
  695. return onevalue(nil);
  696. else return onevalue(lisp_true);
  697. }
  698. #ifdef COMMON
  699. static Lisp_Object Lsimple_bit_vector_p(Lisp_Object nil,
  700. Lisp_Object a)
  701. /*
  702. * simple-bit-vector-p
  703. */
  704. {
  705. if (!(is_vector(a))) return onevalue(nil);
  706. else return onevalue(Lispify_predicate(header_of_bitvector(vechdr(a))));
  707. }
  708. #endif
  709. Lisp_Object Lsimple_vectorp(Lisp_Object nil, Lisp_Object a)
  710. /*
  711. * simple-vector-p
  712. */
  713. {
  714. if (!(is_vector(a))) return onevalue(nil);
  715. else return onevalue(Lispify_predicate(
  716. type_of_header(vechdr(a))==TYPE_SIMPLE_VEC));
  717. }
  718. Lisp_Object Lbpsp(Lisp_Object nil, Lisp_Object a)
  719. {
  720. if (!(is_bps(a))) return onevalue(nil);
  721. else return onevalue(lisp_true);
  722. }
  723. Lisp_Object Lthreevectorp(Lisp_Object nil, Lisp_Object a)
  724. /*
  725. * This is useful for REDUCE - it checks if something is a vector
  726. * of size 3!
  727. */
  728. {
  729. if (!(is_vector(a))) return onevalue(nil);
  730. /*
  731. * With 3 elements a vector has 12 bytes of data plus 4 of header - hence
  732. * the number 16 used here.
  733. */
  734. return onevalue(Lispify_predicate(
  735. vechdr(a) == (TAG_ODDS + TYPE_SIMPLE_VEC + (16<<10))));
  736. }
  737. #ifdef COMMON
  738. static Lisp_Object Larrayp(Lisp_Object nil, Lisp_Object a)
  739. {
  740. Header h;
  741. if (!(is_vector(a))) return onevalue(nil);
  742. h = vechdr(a);
  743. /*
  744. * I could consider accepting TYPE_VEC16 and TYPE_VEC32 etc here...
  745. */
  746. if (type_of_header(h)==TYPE_ARRAY ||
  747. type_of_header(h)==TYPE_STRING ||
  748. type_of_header(h)==TYPE_SIMPLE_VEC ||
  749. header_of_bitvector(h)) return onevalue(lisp_true);
  750. else return onevalue(nil);
  751. }
  752. static Lisp_Object Lcomplex_arrayp(Lisp_Object nil, Lisp_Object a)
  753. {
  754. if (!(is_vector(a))) return onevalue(nil);
  755. else return onevalue(Lispify_predicate(
  756. type_of_header(vechdr(a))==TYPE_ARRAY));
  757. }
  758. static Lisp_Object Lconvert_to_array(Lisp_Object nil, Lisp_Object a)
  759. {
  760. if (!(is_vector(a))) return onevalue(nil);
  761. vechdr(a) = TYPE_ARRAY + (vechdr(a) & ~header_mask);
  762. return onevalue(a);
  763. }
  764. #endif
  765. static Lisp_Object Lstructp(Lisp_Object nil, Lisp_Object a)
  766. /*
  767. * structp
  768. */
  769. {
  770. if (!(is_vector(a))) return onevalue(nil);
  771. else return onevalue(Lispify_predicate(
  772. type_of_header(vechdr(a))==TYPE_STRUCTURE));
  773. }
  774. static Lisp_Object Lconvert_to_struct(Lisp_Object nil, Lisp_Object a)
  775. {
  776. if (!(is_vector(a))) return onevalue(nil);
  777. vechdr(a) = TYPE_STRUCTURE + (vechdr(a) & ~header_mask);
  778. return onevalue(a);
  779. }
  780. Lisp_Object Lcons(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  781. {
  782. Lisp_Object r;
  783. CSL_IGNORE(nil);
  784. r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  785. qcar(r) = a;
  786. qcdr(r) = b;
  787. fringe = r;
  788. if ((char *)r <= (char *)heaplimit)
  789. return onevalue(reclaim((Lisp_Object)((char *)r + TAG_CONS),
  790. "cons", GC_CONS, 0));
  791. else return onevalue((Lisp_Object)((char *)r + TAG_CONS));
  792. }
  793. Lisp_Object Lxcons(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  794. {
  795. Lisp_Object r;
  796. CSL_IGNORE(nil);
  797. r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  798. qcar(r) = b;
  799. qcdr(r) = a;
  800. fringe = r;
  801. if ((char *)r <= (char *)heaplimit)
  802. return onevalue(reclaim((Lisp_Object)((char *)r + TAG_CONS),
  803. "xcons", GC_CONS, 0));
  804. else return onevalue((Lisp_Object)((char *)r + TAG_CONS));
  805. }
  806. Lisp_Object Lncons(Lisp_Object nil, Lisp_Object a)
  807. {
  808. Lisp_Object r;
  809. r = (Lisp_Object)((char *)fringe - sizeof(Cons_Cell));
  810. qcar(r) = a;
  811. qcdr(r) = nil;
  812. fringe = r;
  813. if ((char *)r <= (char *)heaplimit)
  814. return onevalue(reclaim((Lisp_Object)((char *)r + TAG_CONS),
  815. "ncons", GC_CONS, 0));
  816. else return onevalue((Lisp_Object)((char *)r + TAG_CONS));
  817. }
  818. Lisp_Object Llist2(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  819. {
  820. a = list2(a, b);
  821. errexit();
  822. return onevalue(a);
  823. }
  824. Lisp_Object Lmkquote(Lisp_Object nil, Lisp_Object a)
  825. {
  826. a = list2(quote_symbol, a);
  827. errexit();
  828. return onevalue(a);
  829. }
  830. Lisp_Object MS_CDECL Llist2star(Lisp_Object nil, int nargs, ...)
  831. {
  832. va_list aa;
  833. Lisp_Object a, b, c;
  834. argcheck(nargs, 3, "list2*");
  835. va_start(aa, nargs);
  836. a = va_arg(aa, Lisp_Object);
  837. b = va_arg(aa, Lisp_Object);
  838. c = va_arg(aa, Lisp_Object);
  839. va_end(aa);
  840. a = list2star(a,b,c);
  841. errexit();
  842. return onevalue(a);
  843. }
  844. Lisp_Object MS_CDECL Lacons(Lisp_Object nil, int nargs, ...)
  845. {
  846. va_list aa;
  847. Lisp_Object a, b, c;
  848. argcheck(nargs, 3, "acons");
  849. va_start(aa, nargs);
  850. a = va_arg(aa, Lisp_Object);
  851. b = va_arg(aa, Lisp_Object);
  852. c = va_arg(aa, Lisp_Object);
  853. va_end(aa);
  854. a = acons(a,b,c);
  855. errexit();
  856. return onevalue(a);
  857. }
  858. Lisp_Object MS_CDECL Llist3(Lisp_Object nil, int nargs, ...)
  859. {
  860. va_list aa;
  861. Lisp_Object a, b, c;
  862. argcheck(nargs, 3, "list3");
  863. va_start(aa, nargs);
  864. a = va_arg(aa, Lisp_Object);
  865. b = va_arg(aa, Lisp_Object);
  866. c = va_arg(aa, Lisp_Object);
  867. va_end(aa);
  868. a = list3(a,b,c);
  869. errexit();
  870. return onevalue(a);
  871. }
  872. #ifdef COMMON
  873. /*
  874. * In non-COMMON mode I implement list and list* as special forms
  875. * rather than as functions, guessing that that will be more efficient.
  876. */
  877. Lisp_Object MS_CDECL Llist(Lisp_Object nil, int nargs, ...)
  878. {
  879. Lisp_Object r = nil, w, w1;
  880. va_list a;
  881. va_start(a, nargs);
  882. push_args(a, nargs);
  883. while (nargs > 1)
  884. { pop2(w, w1);
  885. nargs-=2;
  886. r = list2star(w1, w, r);
  887. errexitn(nargs);
  888. }
  889. while (nargs > 0)
  890. { pop(w);
  891. nargs--;
  892. r = cons(w, r);
  893. errexitn(nargs);
  894. }
  895. return onevalue(r);
  896. }
  897. static Lisp_Object MS_CDECL Lliststar(Lisp_Object nil, int nargs, ...)
  898. {
  899. Lisp_Object r, w, w1;
  900. va_list a;
  901. if (nargs == 0) return onevalue(nil);
  902. va_start(a, nargs);
  903. push_args(a, nargs);
  904. pop(r);
  905. nargs--;
  906. while (nargs > 1)
  907. { pop2(w, w1);
  908. nargs-=2;
  909. r = list2star(w1, w, r);
  910. errexitn(nargs);
  911. }
  912. while (nargs > 0)
  913. { pop(w);
  914. nargs--;
  915. r = cons(w, r);
  916. errexitn(nargs);
  917. }
  918. return onevalue(r);
  919. }
  920. /*
  921. * fill-vector is used for open-compilation of (vector ...) to avoid
  922. * passing grossly unreasonable numbers of arguments. The expansion of
  923. * (vector e1 ... en) should be
  924. * (let ((v (mkvect <n-1>)) (i 0))
  925. * (setq i (fill-vector v i e1 e2 ... e10))
  926. * (setq i (fill-vector v i e11 e12 ... ))
  927. * ...
  928. * v)
  929. */
  930. static Lisp_Object MS_CDECL Lfill_vector(Lisp_Object nil, int nargs, ...)
  931. {
  932. va_list a;
  933. Lisp_Object v, il;
  934. int32 i;
  935. CSL_IGNORE(nil);
  936. if (nargs < 3) return aerror("fill-vector");
  937. va_start(a, nargs);
  938. v = va_arg(a, Lisp_Object);
  939. il = va_arg(a, Lisp_Object);
  940. if (!is_vector(v) || !is_fixnum(il)) return aerror("fill-vector");
  941. i = int_of_fixnum(il);
  942. nargs -= 2;
  943. while (nargs != 0)
  944. { elt(v, i++) = va_arg(a, Lisp_Object);
  945. nargs--;
  946. }
  947. return onevalue(fixnum_of_int(i));
  948. }
  949. #endif
  950. Lisp_Object Lpair(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  951. {
  952. Lisp_Object r = nil;
  953. while (consp(a) && consp(b))
  954. { push2(a, b);
  955. r = acons(qcar(a), qcar(b), r);
  956. pop2(b, a);
  957. errexit();
  958. a = qcdr(a);
  959. b = qcdr(b);
  960. }
  961. a = nil;
  962. while (r != nil)
  963. { b = qcdr(r);
  964. qcdr(r) = a;
  965. a = r;
  966. r = b;
  967. }
  968. return onevalue(a);
  969. }
  970. static int32 membercount(Lisp_Object a, Lisp_Object b)
  971. /*
  972. * Counts how many times a is a member of the list b
  973. */
  974. {
  975. int32 r = 0;
  976. #ifdef COMMON
  977. Lisp_Object nil = C_nil;
  978. #endif
  979. if (is_symbol(a) || is_fixnum(a))
  980. { while (consp(b))
  981. { if (a == qcar(b)) r++;
  982. b = qcdr(b);
  983. }
  984. return r;
  985. }
  986. while (consp(b))
  987. { Lisp_Object cb = qcar(b);
  988. if (equal(a, cb)) r++;
  989. b = qcdr(b);
  990. }
  991. return r;
  992. }
  993. /*
  994. * INTERSECTION(A,B)
  995. * The result will have its items in the order that they occur in A.
  996. * If lists A and B contain duplicate items these will appear in the
  997. * output if and only if the items involved are duplicated in both
  998. * input lists.
  999. */
  1000. Lisp_Object Lintersect(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1001. {
  1002. Lisp_Object r = nil, w;
  1003. push(b);
  1004. while (consp(a))
  1005. { push2(a, r);
  1006. w = Lmember(nil, qcar(a), stack[-2]);
  1007. errexitn(3);
  1008. /* Here I ignore any item in a that is not also in b */
  1009. if (w != nil)
  1010. { int32 n1 = membercount(qcar(stack[-1]), stack[0]);
  1011. errexitn(3);
  1012. /*
  1013. * Here I want to arrange that items only appear in the result list multiple
  1014. * times if they occur multipl times in BOTH the input lists.
  1015. */
  1016. if (n1 != 0)
  1017. { int32 n2 = membercount(qcar(stack[-1]), stack[-2]);
  1018. errexitn(3);
  1019. if (n2 > n1) n1 = 0;
  1020. }
  1021. if (n1 == 0)
  1022. { pop(r);
  1023. a = stack[0];
  1024. r = cons(qcar(a), r);
  1025. errexitn(2);
  1026. pop(a);
  1027. }
  1028. else pop2(r, a);
  1029. }
  1030. else pop2(r, a);
  1031. a = qcdr(a);
  1032. }
  1033. popv(1);
  1034. a = nil;
  1035. while (consp(r))
  1036. { b = r;
  1037. r = qcdr(r);
  1038. qcdr(b) = a;
  1039. a = b;
  1040. }
  1041. return onevalue(a);
  1042. }
  1043. /*
  1044. * UNION(A, B)
  1045. * This works by consing onto the front of B each element of A that
  1046. * is not already in B. Thus items in A (but not already in B) get
  1047. * added in reversed order. Duplicates in B remain there, and but
  1048. * duplicates in A are dropped.
  1049. */
  1050. Lisp_Object Lunion(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1051. {
  1052. while (consp(a))
  1053. { Lisp_Object c;
  1054. push2(a, b);
  1055. c = Lmember(nil, qcar(a), b);
  1056. errexitn(2);
  1057. pop(b);
  1058. if (c == nil)
  1059. { b = cons(qcar(stack[0]), b);
  1060. errexitn(1);
  1061. }
  1062. pop(a);
  1063. a = qcdr(a);
  1064. }
  1065. return onevalue(b);
  1066. }
  1067. Lisp_Object Lenable_backtrace(Lisp_Object nil, Lisp_Object a)
  1068. {
  1069. int32 n = miscflags;
  1070. if (a == nil) miscflags &= ~MESSAGES_FLAG;
  1071. else miscflags |= MESSAGES_FLAG;
  1072. return onevalue(Lispify_predicate((n & MESSAGES_FLAG) != 0));
  1073. }
  1074. #ifdef NAG
  1075. Lisp_Object MS_CDECL Lunwind(Lisp_Object nil, int nargs, ...)
  1076. {
  1077. argcheck(nargs, 0, "unwind");
  1078. exit_reason = (miscflags & MESSAGES_FLAG) ? UNWIND_ERROR : UNWIND_UNWIND;
  1079. exit_count = 0;
  1080. exit_tag = nil;
  1081. flip_exception();
  1082. return nil;
  1083. ]
  1084. #endif
  1085. /*
  1086. * If the variable *break-function* has as its value a symbol, and that
  1087. * symbol names a function, then the function concerned will be called
  1088. * with one argument after the headline for the diagnostic. When it returns
  1089. * the system will unwind in the usual manner.
  1090. */
  1091. Lisp_Object MS_CDECL Lerror(Lisp_Object nil, int nargs, ...)
  1092. {
  1093. va_list a;
  1094. Lisp_Object w;
  1095. #ifdef COMMON
  1096. Lisp_Object r = nil, w1;
  1097. #else
  1098. int i;
  1099. #endif
  1100. if (nargs == 0) return aerror("error");
  1101. va_start(a, nargs);
  1102. push_args(a, nargs);
  1103. #ifdef COMMON
  1104. while (nargs > 1)
  1105. { pop2(w, w1);
  1106. nargs -= 2;
  1107. w = list2star(w1, w, r);
  1108. nil = C_nil;
  1109. if (exception_pending()) flip_exception();
  1110. else r = w;
  1111. }
  1112. while (nargs > 0)
  1113. { pop(w);
  1114. nargs--;
  1115. w = cons(w, r);
  1116. nil = C_nil;
  1117. if (exception_pending()) flip_exception();
  1118. else r = w;
  1119. }
  1120. if (miscflags & HEADLINE_FLAG)
  1121. { push(r);
  1122. err_printf("\n+++ error: ");
  1123. /*
  1124. * I will use FORMAT to handle error messages provided the first arg
  1125. * to error had been a string and also provided (for bootstrapping) that
  1126. * the function FORMAT seems to be defined.
  1127. */
  1128. if (qfn1(format_symbol) == undefined1 ||
  1129. !consp(r) ||
  1130. !stringp(qcar(r))) loop_print_error(r);
  1131. else Lapply_n(nil, 3, format_symbol, qvalue(error_output), r);
  1132. ignore_exception();
  1133. err_printf("\n");
  1134. pop(r);
  1135. ignore_exception();
  1136. }
  1137. qvalue(emsg_star) = r; /* "Error message" in CL world */
  1138. exit_value = fixnum_of_int(0); /* "Error number" in CL world */
  1139. #else
  1140. if (miscflags & HEADLINE_FLAG)
  1141. { err_printf("\n+++ error: ");
  1142. loop_print_error(stack[1-nargs]);
  1143. for (i=1; i<nargs; i++)
  1144. { err_printf(" ");
  1145. loop_print_error(stack[1+i-nargs]);
  1146. }
  1147. err_printf("\n");
  1148. }
  1149. if (nargs == 1)
  1150. { push(nil);
  1151. nargs++;
  1152. }
  1153. qvalue(emsg_star) = stack[2-nargs]; /* "Error message" in SL world */
  1154. exit_value = stack[1-nargs]; /* "Error number" in SL world */
  1155. popv(nargs);
  1156. #endif
  1157. if ((w = qvalue(break_function)) != nil &&
  1158. symbolp(w) &&
  1159. qfn1(w) != undefined1)
  1160. { (*qfn1(w))(qenv(w), qvalue(emsg_star));
  1161. ignore_exception();
  1162. }
  1163. exit_reason = (miscflags & MESSAGES_FLAG) ? UNWIND_ERROR : UNWIND_UNWIND;
  1164. exit_count = 0;
  1165. exit_tag = nil;
  1166. flip_exception();
  1167. return nil;
  1168. }
  1169. Lisp_Object Lerror1(Lisp_Object nil, Lisp_Object a1)
  1170. {
  1171. return Lerror(nil, 1, a1);
  1172. }
  1173. Lisp_Object Lerror2(Lisp_Object nil, Lisp_Object a1, Lisp_Object a2)
  1174. {
  1175. return Lerror(nil, 2, a1, a2);
  1176. }
  1177. Lisp_Object MS_CDECL Lerror0(Lisp_Object nil, int nargs, ...)
  1178. {
  1179. /*
  1180. * Silently provoked error - unwind to surrounding errorset level. Note that
  1181. * this will NEVER enter a user-provided break loop...
  1182. */
  1183. argcheck(nargs, 0, "error0");
  1184. miscflags &= ~(MESSAGES_FLAG | HEADLINE_FLAG);
  1185. exit_reason = UNWIND_UNWIND;
  1186. exit_value = exit_tag = nil;
  1187. exit_count = 0;
  1188. flip_exception();
  1189. return nil;
  1190. }
  1191. Lisp_Object Lstop(Lisp_Object env, Lisp_Object code)
  1192. {
  1193. /*
  1194. * I ignore "env" and set up nil for myself here to make it easier to call
  1195. * this function from random places in my interface code...
  1196. */
  1197. Lisp_Object nil = C_nil;
  1198. CSL_IGNORE(env);
  1199. if (!is_fixnum(code)) return aerror("stop");
  1200. exit_value = code;
  1201. exit_tag = fixnum_of_int(0); /* Flag to say "stop" */
  1202. exit_reason = UNWIND_RESTART;
  1203. exit_count = 1;
  1204. flip_exception();
  1205. return nil;
  1206. }
  1207. Lisp_Object Lmake_special(Lisp_Object nil, Lisp_Object a)
  1208. {
  1209. CSL_IGNORE(nil);
  1210. if (!symbolp(a)) return aerror1("make-special", a);
  1211. qheader(a) |= SYM_SPECIAL_VAR;
  1212. return onevalue(a);
  1213. }
  1214. Lisp_Object Lmake_global(Lisp_Object nil, Lisp_Object a)
  1215. {
  1216. CSL_IGNORE(nil);
  1217. if (!symbolp(a)) return aerror("make-global");
  1218. qheader(a) |= (SYM_SPECIAL_VAR | SYM_GLOBAL_VAR);
  1219. return onevalue(a);
  1220. }
  1221. Lisp_Object Lunmake_special(Lisp_Object nil, Lisp_Object a)
  1222. {
  1223. if (!symbolp(a)) return onevalue(nil);
  1224. qheader(a) &= ~(SYM_SPECIAL_VAR | SYM_GLOBAL_VAR);
  1225. return onevalue(a);
  1226. }
  1227. Lisp_Object Lunmake_global(Lisp_Object nil, Lisp_Object a)
  1228. {
  1229. if (!symbolp(a)) return onevalue(nil);
  1230. qheader(a) &= ~(SYM_SPECIAL_VAR | SYM_GLOBAL_VAR);
  1231. return onevalue(a);
  1232. }
  1233. Lisp_Object Lsymbol_specialp(Lisp_Object nil, Lisp_Object a)
  1234. {
  1235. if (!symbolp(a)) return onevalue(nil);
  1236. else if ((qheader(a) & (SYM_SPECIAL_VAR | SYM_GLOBAL_VAR)) ==
  1237. SYM_SPECIAL_VAR) return onevalue(lisp_true);
  1238. else return onevalue(nil);
  1239. }
  1240. Lisp_Object Lsymbol_globalp(Lisp_Object nil, Lisp_Object a)
  1241. {
  1242. if (!symbolp(a)) return onevalue(nil);
  1243. else if ((qheader(a) & SYM_GLOBAL_VAR) != 0) return onevalue(lisp_true);
  1244. else return onevalue(nil);
  1245. }
  1246. Lisp_Object Lboundp(Lisp_Object nil, Lisp_Object a)
  1247. {
  1248. if (!symbolp(a)) return onevalue(nil);
  1249. #ifndef COMMON
  1250. /*
  1251. * In COMMON Lisp it seems that this is intended to just check if the
  1252. * value cell in a shallow-bound implementation contains some marker value
  1253. * that stands for "junk". In Standard Lisp mode I deem that variables
  1254. * that have not been declared fluid are unbound. Seems to me like a
  1255. * classical mix-up between the concept of binding and of having some
  1256. * particular value... Oh well.
  1257. */
  1258. else if ((qheader(a) & SYM_SPECIAL_VAR) == 0) return onevalue(nil);
  1259. #endif
  1260. else if (qvalue(a) == unset_var) return onevalue(nil); /* no value yet */
  1261. else return onevalue(lisp_true);
  1262. }
  1263. Lisp_Object Lsymbol_value(Lisp_Object nil, Lisp_Object a)
  1264. {
  1265. CSL_IGNORE(nil);
  1266. if (!symbolp(a)) return onevalue(a);
  1267. else return onevalue(qvalue(a));
  1268. }
  1269. Lisp_Object Lset(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1270. {
  1271. if (!symbolp(a) || a == nil || a == lisp_true) return aerror("set");
  1272. qvalue(a) = b;
  1273. return onevalue(b);
  1274. }
  1275. Lisp_Object Lsymbol_function(Lisp_Object nil, Lisp_Object a)
  1276. {
  1277. one_args *f1;
  1278. two_args *f2;
  1279. n_args *fn;
  1280. if (!symbolp(a)) return onevalue(nil);
  1281. f1 = qfn1(a); f2 = qfn2(a); fn = qfnn(a);
  1282. if ((qheader(a) & (SYM_SPECIAL_FORM | SYM_MACRO)) != 0 ||
  1283. (f1 == undefined1 && f2 == undefined2 &&
  1284. fn == undefinedn)) return onevalue(nil);
  1285. else if (f1 == interpreted1 ||
  1286. f2 == interpreted2 ||
  1287. fn == interpretedn)
  1288. /* I wonder if onevalue(cons(...)) is really valid here. It is OK in SL mode */
  1289. return onevalue(cons(lambda, qenv(a)));
  1290. else if (f1 == funarged1 ||
  1291. f2 == funarged2 ||
  1292. fn == funargedn)
  1293. return onevalue(cons(funarg, qenv(a)));
  1294. else if (f1 == traceinterpreted1 ||
  1295. f2 == traceinterpreted2 ||
  1296. fn == traceinterpretedn)
  1297. return onevalue(cons(lambda, qcdr(qenv(a))));
  1298. else if (f1 == tracefunarged1 ||
  1299. f2 == tracefunarged2 ||
  1300. fn == tracefunargedn)
  1301. return onevalue(cons(funarg, qcdr(qenv(a))));
  1302. else
  1303. {
  1304. #ifdef COMMON
  1305. Lisp_Object b = get(a, work_symbol, nil);
  1306. #else
  1307. Lisp_Object b = get(a, work_symbol);
  1308. #endif
  1309. /*
  1310. * If I have already manufactured a code pointer for this function I
  1311. * can find it on the property list - in that case I will re-use it.
  1312. */
  1313. while (b != nil)
  1314. { Lisp_Object c = qcar(b);
  1315. if ((qheader(c) & (SYM_C_DEF | SYM_CODEPTR)) == SYM_CODEPTR)
  1316. return onevalue(c);
  1317. b = qcdr(b);
  1318. }
  1319. push(a);
  1320. /*
  1321. * To carry a code-pointer I manufacture a sort of gensym, flagging
  1322. * it in its header as a "code pointer object" and sticking the required
  1323. * definition in with it. I need to link this to the originating
  1324. * definition in some cases to allow for preserve/restart problems wrt
  1325. * the initialisation of function addresses that refer to C code.
  1326. * I make the carrier using GENSYM1, but need to clear the gensym flag bit
  1327. * to show I have a regular name for the object, and that I will not need
  1328. * to append a serial number later on. In Common Lisp mode I let the name
  1329. * of the gensym be just the name of the function, while in Standard Lisp
  1330. * mode I will append a numeric suffix. I do this because in Common Lisp
  1331. * mode the thing will print as (say) #:apply which is visibly different
  1332. * from the name 'apply of the base function, while in Standard Lisp a name
  1333. * like apply775 is needed to make the distinction (easily) visible.
  1334. */
  1335. #ifdef COMMON
  1336. b = Lgensym2(nil, a);
  1337. #else
  1338. b = Lgensym1(nil, a);
  1339. #endif
  1340. pop(a);
  1341. errexit();
  1342. set_fns(b, f1, f2, fn);
  1343. qenv(b) = qenv(a);
  1344. #ifdef COMMON
  1345. /* in Common Lisp mode gensyms that are "unprinted" are not special */
  1346. qheader(b) ^= (SYM_ANY_GENSYM | SYM_CODEPTR);
  1347. #else
  1348. qheader(b) ^= (SYM_UNPRINTED_GENSYM | SYM_ANY_GENSYM | SYM_CODEPTR);
  1349. #endif
  1350. if ((qheader(a) & SYM_C_DEF) != 0)
  1351. { Lisp_Object c, w;
  1352. #ifdef COMMON
  1353. c = get(a, unset_var, nil);
  1354. #else
  1355. c = get(a, unset_var);
  1356. #endif
  1357. if (c == nil) c = a;
  1358. push3(a, b, c);
  1359. qheader(b) |= SYM_C_DEF;
  1360. putprop(b, unset_var, c);
  1361. errexitn(3);
  1362. c = stack[0]; b = stack[-1];
  1363. #ifdef COMMON
  1364. w = get(c, work_symbol, nil);
  1365. #else
  1366. w = get(c, work_symbol);
  1367. #endif
  1368. w = cons(b, w);
  1369. pop(c);
  1370. errexitn(2);
  1371. putprop(c, work_symbol, w);
  1372. pop2(b, a);
  1373. errexit();
  1374. }
  1375. return onevalue(b);
  1376. }
  1377. }
  1378. Lisp_Object Lspecial_form_p(Lisp_Object nil, Lisp_Object a)
  1379. {
  1380. if (!symbolp(a)) return onevalue(nil);
  1381. else if ((qheader(a) & SYM_SPECIAL_FORM) != 0) return onevalue(lisp_true);
  1382. else return onevalue(nil);
  1383. }
  1384. Lisp_Object Lcodep(Lisp_Object nil, Lisp_Object a)
  1385. /*
  1386. * This responds TRUE for the special pseudo-symbols that are used to
  1387. * carry compiled code objects. It returns NIL on the symbols that
  1388. * are normally used by the user.
  1389. */
  1390. {
  1391. if (!symbolp(a)) return onevalue(nil);
  1392. if ((qheader(a) & (SYM_CODEPTR | SYM_C_DEF)) == SYM_CODEPTR)
  1393. return onevalue(lisp_true);
  1394. else return onevalue(nil);
  1395. }
  1396. Lisp_Object getvector(int tag, int32 type, int32 size)
  1397. {
  1398. /*
  1399. * tag is the value (e.g. TAG_SYMBOL) that will go in the low order
  1400. * 3 bits of the pointer result.
  1401. * type is the code (e.g. TYPE_SYMBOL) that gets packed, together with
  1402. * the size, into a header word.
  1403. * size is measured in bytes and must allow space for the header word.
  1404. */
  1405. Lisp_Object nil = C_nil;
  1406. #ifdef CHECK_FOR_CORRUPT_HEAP
  1407. validate_all();
  1408. #endif
  1409. for (;;)
  1410. { char *r = (char *)vfringe;
  1411. unsigned int free = (unsigned int)((char *)vheaplimit - r);
  1412. int32 alloc_size = (int32)doubleword_align_up(size);
  1413. /*
  1414. * There is a real NASTY here - it is quite possible that I ought to implement
  1415. * a scheme whereby large vectors can be allocated as a series of chunks so as
  1416. * to avoid the current absolute limit on size. Remember that the page size
  1417. * is about 64 Kbytes for small machines but on larger ones I can have bigger
  1418. * pages (typically 256K) and hence bigger vectors.
  1419. */
  1420. if (alloc_size > CSL_PAGE_SIZE - 32)
  1421. return aerror("vector request too big");
  1422. if (alloc_size > (int32)free)
  1423. { char msg[40];
  1424. /*
  1425. * I go to a whole load of trouble here to tell the user what sort of
  1426. * vector request provoked this garbage collection. I wonder if the user
  1427. * really cares - but I do very much when I am chasing after GC bugs!
  1428. */
  1429. switch (tag)
  1430. {
  1431. case TAG_SYMBOL:
  1432. sprintf(msg, "symbol header");
  1433. break;
  1434. case TAG_NUMBERS:
  1435. switch (type)
  1436. {
  1437. case TYPE_BIGNUM:
  1438. sprintf(msg, "bignum(%ld)", (long)size);
  1439. break;
  1440. default:
  1441. sprintf(msg, "numbers(%lx,%ld)", (long)type, (long)size);
  1442. break;
  1443. }
  1444. break;
  1445. case TAG_VECTOR:
  1446. switch (type)
  1447. {
  1448. case TYPE_STRING:
  1449. sprintf(msg, "string(%ld)", (long)size);
  1450. break;
  1451. case TYPE_BPS:
  1452. sprintf(msg, "BPS(%ld)", (long)size);
  1453. break;
  1454. case TYPE_SIMPLE_VEC:
  1455. sprintf(msg, "simple vector(%ld)", (long)size);
  1456. break;
  1457. case TYPE_HASH:
  1458. sprintf(msg, "hash table(%ld)", (long)size);
  1459. break;
  1460. default:
  1461. sprintf(msg, "vector(%lx,%ld)", (long)type, (long)size);
  1462. break;
  1463. }
  1464. break;
  1465. case TAG_BOXFLOAT:
  1466. sprintf(msg, "float(%ld)", (long)size);
  1467. break;
  1468. default:
  1469. sprintf(msg, "getvector(%lx,%ld)", (long)tag, (long)size);
  1470. break;
  1471. }
  1472. reclaim(nil, msg, GC_VEC, alloc_size);
  1473. errexit();
  1474. continue;
  1475. }
  1476. vfringe = (Lisp_Object)(r + alloc_size);
  1477. *((Header *)r) = type + (size << 10) + TAG_ODDS;
  1478. /*
  1479. * DANGER: the vector allocated here is left uninitialised at this stage.
  1480. * This is OK if the vector will contain binary information, but if it
  1481. * will hold any Lisp_Objects it needs safe values put in PDQ.
  1482. */
  1483. return (Lisp_Object)(r + tag);
  1484. }
  1485. }
  1486. Lisp_Object getvector_init(int32 n, Lisp_Object k)
  1487. {
  1488. Lisp_Object p, nil;
  1489. push(k);
  1490. p = getvector(TAG_VECTOR, TYPE_SIMPLE_VEC, n);
  1491. pop(k);
  1492. errexit();
  1493. if ((n & 4) != 0) n += 4; /* Ensure last doubleword is tidy */
  1494. while (n > 4)
  1495. { n -= 4;
  1496. *(Lisp_Object *)((char *)p - TAG_VECTOR + n) = k;
  1497. }
  1498. return p;
  1499. }
  1500. clock_t base_time;
  1501. double *clock_stack, consolidated_time[10], gc_time;
  1502. void push_clock()
  1503. {
  1504. clock_t t0 = read_clock();
  1505. /*
  1506. * Provided that I do this often enough I will not suffer clock
  1507. * wrap-around or overflow.
  1508. */
  1509. double delta = (double)(t0 - base_time)/(double)CLOCKS_PER_SEC;
  1510. base_time = t0;
  1511. *clock_stack += delta;
  1512. *++clock_stack = 0.0;
  1513. }
  1514. double pop_clock()
  1515. {
  1516. clock_t t0 = read_clock();
  1517. double delta = (double)(t0 - base_time)/(double)CLOCKS_PER_SEC;
  1518. base_time = t0;
  1519. return delta + *clock_stack--;
  1520. }
  1521. Lisp_Object MS_CDECL Ltime(Lisp_Object nil, int nargs, ...)
  1522. {
  1523. unsigned32 tt;
  1524. Lisp_Object r;
  1525. if (clock_stack == &consolidated_time[0])
  1526. { clock_t t0 = read_clock();
  1527. double delta = (double)(t0 - base_time)/(double)CLOCKS_PER_SEC;
  1528. base_time = t0;
  1529. consolidated_time[0] += delta;
  1530. }
  1531. argcheck(nargs, 0, "time");
  1532. CSL_IGNORE(nil);
  1533. tt = (unsigned32)(1000.0 * consolidated_time[0]);
  1534. /*
  1535. * Overflow here is around 49 days. I suppose that that is almost a
  1536. * thinkable amount of CPU time to use.... Oh dear!
  1537. */
  1538. if ((tt & fix_mask) == 0) return onevalue(fixnum_of_int(tt));
  1539. if (!signed_overflow(tt)) r = make_one_word_bignum(tt);
  1540. else r = make_two_word_bignum((tt>>31) & 1, tt & 0x7fffffff);
  1541. errexit();
  1542. return onevalue(r);
  1543. }
  1544. Lisp_Object MS_CDECL Lgctime(Lisp_Object nil, int nargs, ...)
  1545. {
  1546. argcheck(nargs, 0, "gctime");
  1547. CSL_IGNORE(nil);
  1548. return onevalue(fixnum_of_int((int32)(1000.0 * gc_time)));
  1549. }
  1550. #ifdef COMMON
  1551. Lisp_Object MS_CDECL Ldecoded_time(Lisp_Object nil, int nargs, ...)
  1552. {
  1553. time_t t0 = time(NULL);
  1554. /*
  1555. * tm_sec -- seconds 0..59
  1556. * tm_min -- minutes 0..59
  1557. * tm_hour -- hour of day 0..23
  1558. * tm_mday -- day of month 1..31
  1559. * tm_mon -- month 0..11
  1560. * tm_year -- years since 1900
  1561. * tm_wday -- day of week, 0..6 (Sunday..Saturday)
  1562. * tm_yday -- day of year, 0..365
  1563. * tm_isdst -- >0 if daylight savings time
  1564. * -- ==0 if not DST
  1565. * -- <0 if don't know
  1566. */
  1567. struct tm *tbuf = localtime(&t0);
  1568. Lisp_Object r, *p = &mv_2;
  1569. int w;
  1570. argcheck(nargs, 0, "get-decoded-time");
  1571. r = fixnum_of_int(tbuf->tm_sec);
  1572. *p++ = fixnum_of_int(tbuf->tm_min);
  1573. *p++ = fixnum_of_int(tbuf->tm_hour);
  1574. *p++ = fixnum_of_int(tbuf->tm_mday);
  1575. *p++ = fixnum_of_int(tbuf->tm_mon+1);
  1576. *p++ = fixnum_of_int(tbuf->tm_year+1900);
  1577. w = tbuf->tm_wday;
  1578. *p++ = fixnum_of_int(w == 0 ? 6 : w-1);
  1579. *p++ = tbuf->tm_isdst > 0 ? lisp_true : nil;
  1580. *p++ = fixnum_of_int(0); /* Time zone info not available? */
  1581. return nvalues(r, 9);
  1582. }
  1583. #endif
  1584. Lisp_Object MS_CDECL Ldate(Lisp_Object nil, int nargs, ...)
  1585. {
  1586. Lisp_Object w;
  1587. time_t t = time(NULL);
  1588. char today[32];
  1589. argcheck(nargs, 0, "date");
  1590. CSL_IGNORE(nil);
  1591. strcpy(today, ctime(&t)); /* e.g. "Sun Sep 16 01:03:52 1973\n" */
  1592. today[24] = 0; /* loses final '\n' */
  1593. w = make_string(today);
  1594. errexit();
  1595. return onevalue(w);
  1596. }
  1597. Lisp_Object MS_CDECL Ldatestamp(Lisp_Object nil, int nargs, ...)
  1598. /*
  1599. * Returns date-stamp integer, which on many systems will be the
  1600. * number of seconds between 1970.0.0 and now, but which could be
  1601. * pretty-well other things, as per the C "time_t" type.
  1602. */
  1603. {
  1604. Lisp_Object w;
  1605. time_t t = time(NULL);
  1606. unsigned32 n = (unsigned32)t; /* NON-PORTABLE assumption about time_t */
  1607. argcheck(nargs, 0, "datestamp");
  1608. CSL_IGNORE(nil);
  1609. if ((n & fix_mask) == 0) w = fixnum_of_int(n);
  1610. else if ((n & 0xc0000000U) == 0) w = make_one_word_bignum(n);
  1611. else w = make_two_word_bignum((n >> 31) & 1, n & 0x7fffffff);
  1612. errexit();
  1613. return onevalue(w);
  1614. }
  1615. #define STR24HDR (TAG_ODDS+TYPE_STRING+((24+4)<<10))
  1616. static int getint(char *p, int len)
  1617. {
  1618. int r = 0;
  1619. while (len-- != 0)
  1620. { int c = *p++;
  1621. if (c == ' ') c = '0';
  1622. r = 10*r + (c - '0');
  1623. }
  1624. return r;
  1625. }
  1626. static int getmon(char *s)
  1627. {
  1628. int c1 = s[0], c2 = s[1], c3 = s[2], r = -1, w;
  1629. char *m = "janfebmaraprmayjunjulaugsepoctnovdec";
  1630. if (isupper(c1)) c1 = tolower(c1);
  1631. if (isupper(c2)) c2 = tolower(c2);
  1632. if (isupper(c3)) c3 = tolower(c3);
  1633. for (w=0; w<12; w++)
  1634. { if (c1==m[0] && c2==m[1] && c3==m[2])
  1635. { r = w;
  1636. break;
  1637. }
  1638. m += 3;
  1639. }
  1640. return r;
  1641. }
  1642. static Lisp_Object Ldatelessp(Lisp_Object nil, Lisp_Object a, Lisp_Object b)
  1643. /*
  1644. * This is maybe a bit of an abomination! The functions (date) and
  1645. * (filedate "filename") [and also (modulep 'modulename)] return times
  1646. * as strings of 24 characters. This function decodes these and
  1647. * sorts out which time is earlier. The alternative would be to provide
  1648. * a collection of functions that returned coded times (as in C "time_t"),
  1649. * but I have greater doubts about making those utterly portable, while the
  1650. * textual arrangement used here seems fairly robust (until you start
  1651. * worrying about carrying a portable machine across time zones or switching
  1652. * to daylight savings time).
  1653. */
  1654. {
  1655. char *aa, *bb;
  1656. CSLbool res;
  1657. int wa, wb;
  1658. if (!is_vector(a) || !is_vector(b) ||
  1659. vechdr(a) != STR24HDR ||
  1660. vechdr(b) != STR24HDR) return aerror2("datelessp", a, b);
  1661. aa = (char *)a + (4 - TAG_VECTOR);
  1662. bb = (char *)b + (4 - TAG_VECTOR);
  1663. /*
  1664. * Layout is eg. "Wed May 12 15:50:23 1993"
  1665. * 012345678901234567890123
  1666. * Note that the year is 4 digits so that the year 2000 should hold
  1667. * no special terrors JUST here.
  1668. */
  1669. if ((wa = getint(aa+20, 4)) != (wb = getint(bb+20, 4))) res = wa < wb;
  1670. else if ((wa = getmon(aa+4)) != (wb = getmon(bb+4))) res = wa < wb;
  1671. else if ((wa = getint(aa+8, 2)) != (wb = getint(bb+8, 2))) res = wa < wb;
  1672. else if ((wa = getint(aa+11, 2)) != (wb = getint(bb+11, 2))) res = wa < wb;
  1673. else if ((wa = getint(aa+14, 2)) != (wb = getint(bb+14, 2))) res = wa < wb;
  1674. else if ((wa = getint(aa+17, 2)) != (wb = getint(bb+17, 2))) res = wa < wb;
  1675. else res = NO;
  1676. return onevalue(Lispify_predicate(res));
  1677. }
  1678. static Lisp_Object Lrepresentation1(Lisp_Object nil, Lisp_Object a)
  1679. /*
  1680. * Intended for debugging, and use with indirect (q.v.)
  1681. */
  1682. {
  1683. int32 top = (int32)a & 0xf8000000U;
  1684. CSL_IGNORE(nil);
  1685. if (top == 0 || top == 0xf8000000U)
  1686. return onevalue(fixnum_of_int((int32)a));
  1687. a = make_one_word_bignum((int32)a);
  1688. errexit();
  1689. return onevalue(a);
  1690. }
  1691. static Lisp_Object Lrepresentation2(Lisp_Object nil,
  1692. Lisp_Object a, Lisp_Object b)
  1693. /*
  1694. * Intended for debugging, and use with indirect (q.v.). arg2, if
  1695. * present and non-nil makes this more verbose.
  1696. */
  1697. {
  1698. int32 top = (int32)a & 0xf8000000U;
  1699. CSL_IGNORE(nil);
  1700. if (b != nil) trace_printf(" %.8lx ", (long)(unsigned32)a);
  1701. if (top == 0 || top == 0xf8000000U)
  1702. return onevalue(fixnum_of_int((int32)a));
  1703. a = make_one_word_bignum((int32)a);
  1704. errexit();
  1705. return onevalue(a);
  1706. }
  1707. Lisp_Object Lindirect(Lisp_Object nil, Lisp_Object a)
  1708. {
  1709. CSL_IGNORE(nil);
  1710. return onevalue(*(Lisp_Object *)thirty_two_bits(a));
  1711. }
  1712. setup_type const funcs1_setup[] =
  1713. {
  1714. {"acons", wrong_no_na, wrong_no_nb, Lacons},
  1715. {"atom", Latom, too_many_1, wrong_no_1},
  1716. {"boundp", Lboundp, too_many_1, wrong_no_1},
  1717. {"car", Lcar, too_many_1, wrong_no_1},
  1718. {"car*", Lcar_star, too_many_1, wrong_no_1},
  1719. {"cdr", Lcdr, too_many_1, wrong_no_1},
  1720. {"caar", Lcaar, too_many_1, wrong_no_1},
  1721. {"cadr", Lcadr, too_many_1, wrong_no_1},
  1722. {"cdar", Lcdar, too_many_1, wrong_no_1},
  1723. {"cddr", Lcddr, too_many_1, wrong_no_1},
  1724. {"caaar", Lcaaar, too_many_1, wrong_no_1},
  1725. {"caadr", Lcaadr, too_many_1, wrong_no_1},
  1726. {"cadar", Lcadar, too_many_1, wrong_no_1},
  1727. {"caddr", Lcaddr, too_many_1, wrong_no_1},
  1728. {"cdaar", Lcdaar, too_many_1, wrong_no_1},
  1729. {"cdadr", Lcdadr, too_many_1, wrong_no_1},
  1730. {"cddar", Lcddar, too_many_1, wrong_no_1},
  1731. {"cdddr", Lcdddr, too_many_1, wrong_no_1},
  1732. {"caaaar", Lcaaaar, too_many_1, wrong_no_1},
  1733. {"caaadr", Lcaaadr, too_many_1, wrong_no_1},
  1734. {"caadar", Lcaadar, too_many_1, wrong_no_1},
  1735. {"caaddr", Lcaaddr, too_many_1, wrong_no_1},
  1736. {"cadaar", Lcadaar, too_many_1, wrong_no_1},
  1737. {"cadadr", Lcadadr, too_many_1, wrong_no_1},
  1738. {"caddar", Lcaddar, too_many_1, wrong_no_1},
  1739. {"cadddr", Lcadddr, too_many_1, wrong_no_1},
  1740. {"cdaaar", Lcdaaar, too_many_1, wrong_no_1},
  1741. {"cdaadr", Lcdaadr, too_many_1, wrong_no_1},
  1742. {"cdadar", Lcdadar, too_many_1, wrong_no_1},
  1743. {"cdaddr", Lcdaddr, too_many_1, wrong_no_1},
  1744. {"cddaar", Lcddaar, too_many_1, wrong_no_1},
  1745. {"cddadr", Lcddadr, too_many_1, wrong_no_1},
  1746. {"cdddar", Lcdddar, too_many_1, wrong_no_1},
  1747. {"cddddr", Lcddddr, too_many_1, wrong_no_1},
  1748. {"qcar", Lcar, too_many_1, wrong_no_1},
  1749. {"qcdr", Lcdr, too_many_1, wrong_no_1},
  1750. {"qcaar", Lcaar, too_many_1, wrong_no_1},
  1751. {"qcadr", Lcadr, too_many_1, wrong_no_1},
  1752. {"qcdar", Lcdar, too_many_1, wrong_no_1},
  1753. {"qcddr", Lcddr, too_many_1, wrong_no_1},
  1754. {"bpsp", Lbpsp, too_many_1, wrong_no_1},
  1755. {"codep", Lcodep, too_many_1, wrong_no_1},
  1756. {"constantp", Lconstantp, too_many_1, wrong_no_1},
  1757. {"date", wrong_no_na, wrong_no_nb, Ldate},
  1758. {"datestamp", wrong_no_na, wrong_no_nb, Ldatestamp},
  1759. {"enable-backtrace", Lenable_backtrace, too_many_1, wrong_no_1},
  1760. {"error", Lerror1, Lerror2, Lerror},
  1761. {"error1", wrong_no_na, wrong_no_nb, Lerror0},
  1762. #ifdef NAG
  1763. {"unwind", wrong_no_na, wrong_no_nb, Lunwind},
  1764. #endif
  1765. {"eq-safe", Leq_safe, too_many_1, wrong_no_1},
  1766. {"fixp", Lfixp, too_many_1, wrong_no_1},
  1767. {"floatp", Lfloatp, too_many_1, wrong_no_1},
  1768. {"fluidp", Lsymbol_specialp, too_many_1, wrong_no_1},
  1769. {"gctime", wrong_no_na, wrong_no_nb, Lgctime},
  1770. {"globalp", Lsymbol_globalp, too_many_1, wrong_no_1},
  1771. {"hash-table-p", Lhash_table_p, too_many_1, wrong_no_1},
  1772. {"indirect", Lindirect, too_many_1, wrong_no_1},
  1773. {"integerp", Lintegerp, too_many_1, wrong_no_1},
  1774. {"intersection", too_few_2, Lintersect, wrong_no_2},
  1775. {"list2", too_few_2, Llist2, wrong_no_2},
  1776. {"list2*", wrong_no_na, wrong_no_nb, Llist2star},
  1777. {"list3", wrong_no_na, wrong_no_nb, Llist3},
  1778. {"make-global", Lmake_global, too_many_1, wrong_no_1},
  1779. {"make-special", Lmake_special, too_many_1, wrong_no_1},
  1780. {"mkquote", Lmkquote, too_many_1, wrong_no_1},
  1781. {"ncons", Lncons, too_many_1, wrong_no_1},
  1782. {"numberp", Lnumberp, too_many_1, wrong_no_1},
  1783. {"pair", too_few_2, Lpair, wrong_no_2},
  1784. {"put", wrong_no_na, wrong_no_nb, Lputprop},
  1785. {"remprop", too_few_2, Lremprop, wrong_no_2},
  1786. {"representation", Lrepresentation1, too_many_1, wrong_no_1},
  1787. {"representation", too_few_2, Lrepresentation2, wrong_no_2},
  1788. {"rplaca", too_few_2, Lrplaca, wrong_no_2},
  1789. {"rplacd", too_few_2, Lrplacd, wrong_no_2},
  1790. {"set", too_few_2, Lset, wrong_no_2},
  1791. {"special-form-p", Lspecial_form_p, too_many_1, wrong_no_1},
  1792. {"stop", Lstop, too_many_1, wrong_no_1},
  1793. {"symbol-function", Lsymbol_function, too_many_1, wrong_no_1},
  1794. {"symbol-value", Lsymbol_value, too_many_1, wrong_no_1},
  1795. {"time", wrong_no_na, wrong_no_nb, Ltime},
  1796. {"datelessp", too_few_2, Ldatelessp, wrong_no_2},
  1797. {"union", too_few_2, Lunion, wrong_no_2},
  1798. {"unmake-global", Lunmake_global, too_many_1, wrong_no_1},
  1799. {"unmake-special", Lunmake_special, too_many_1, wrong_no_1},
  1800. {"xcons", too_few_2, Lxcons, wrong_no_2},
  1801. /* I provide both IDP and SYMBOLP in both modes... */
  1802. {"symbolp", Lsymbolp, too_many_1, wrong_no_1},
  1803. {"idp", Lsymbolp, too_many_1, wrong_no_1},
  1804. /* I support the Common Lisp names here in both modes */
  1805. {"simple-string-p", Lstringp, too_many_1, wrong_no_1},
  1806. {"simple-vector-p", Lsimple_vectorp, too_many_1, wrong_no_1},
  1807. #ifdef COMMON
  1808. {"fill-vector", wrong_no_na, wrong_no_nb, Lfill_vector},
  1809. {"get", too_few_2, Lget, Lget_3},
  1810. {"get-decoded-time", wrong_no_0a, wrong_no_0b, Ldecoded_time},
  1811. {"arrayp", Larrayp, too_many_1, wrong_no_1},
  1812. {"complex-arrayp", Lcomplex_arrayp, too_many_1, wrong_no_1},
  1813. {"short-floatp", Lshort_floatp, too_many_1, wrong_no_1},
  1814. {"single-floatp", Lsingle_floatp, too_many_1, wrong_no_1},
  1815. {"double-floatp", Ldouble_floatp, too_many_1, wrong_no_1},
  1816. {"long-floatp", Llong_floatp, too_many_1, wrong_no_1},
  1817. {"rationalp", Lrationalp, too_many_1, wrong_no_1},
  1818. {"complexp", Lcomplexp, too_many_1, wrong_no_1},
  1819. {"consp", Lconsp, too_many_1, wrong_no_1},
  1820. {"convert-to-array", Lconvert_to_array, too_many_1, wrong_no_1},
  1821. {"convert-to-struct", Lconvert_to_struct, too_many_1, wrong_no_1},
  1822. {"identity", Lidentity, too_many_1, wrong_no_1},
  1823. {"list", Lncons, Llist2, Llist},
  1824. {"list*", Lidentity, Lcons, Lliststar},
  1825. {"listp", Llistp, too_many_1, wrong_no_1},
  1826. {"bit-vector-p", Lsimple_bit_vector_p, too_many_1, wrong_no_1},
  1827. {"simple-bit-vector-p", Lsimple_bit_vector_p, too_many_1, wrong_no_1},
  1828. {"stringp", Lc_stringp, too_many_1, wrong_no_1},
  1829. {"structp", Lstructp, too_many_1, wrong_no_1},
  1830. {"flag", too_few_2, Lflag, wrong_no_2},
  1831. {"flagp", too_few_2, Lflagp, wrong_no_2},
  1832. {"flagpcar", too_few_2, Lflagpcar, wrong_no_2},
  1833. {"remflag", too_few_2, Lremflag, wrong_no_2},
  1834. {"time*", wrong_no_na, wrong_no_nb, Ltime},
  1835. #else
  1836. {"get", too_few_2, Lget, wrong_no_2},
  1837. {"convert-to-evector", Lconvert_to_struct, too_many_1, wrong_no_1},
  1838. {"evectorp", Lstructp, too_many_1, wrong_no_1},
  1839. {"get*", too_few_2, Lget, wrong_no_2},
  1840. {"pairp", Lconsp, too_many_1, wrong_no_1},
  1841. /* I provide CONSP as well as PAIRP since otherwise I get muddled */
  1842. {"consp", Lconsp, too_many_1, wrong_no_1},
  1843. {"flag", too_few_2, Lflag, wrong_no_2},
  1844. {"flagp", too_few_2, Lflagp, wrong_no_2},
  1845. {"flagpcar", too_few_2, Lflagpcar, wrong_no_2},
  1846. {"flagp**", too_few_2, Lflagp, wrong_no_2},
  1847. {"remflag", too_few_2, Lremflag, wrong_no_2},
  1848. {"stringp", Lstringp, too_many_1, wrong_no_1},
  1849. {"threevectorp", Lthreevectorp, too_many_1, wrong_no_1},
  1850. {"vectorp", Lsimple_vectorp, too_many_1, wrong_no_1},
  1851. #endif
  1852. {NULL, 0, 0, 0}
  1853. };
  1854. /* end of fns1.c */