Mercurial > emacs
annotate src/fns.c @ 399:21aa17a1560d
*** empty log message ***
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sat, 17 Aug 1991 17:54:46 +0000 |
parents | 80a890dbbeb5 |
children | 24b63d6679b6 |
rev | line source |
---|---|
211 | 1 /* Random utility Lisp functions. |
2 Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc. | |
3 | |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 1, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
21 #include "config.h" | |
22 | |
23 #ifdef LOAD_AVE_TYPE | |
24 #ifdef BSD | |
25 /* It appears param.h defines BSD and BSD4_3 in 4.3 | |
26 and is not considerate enough to avoid bombing out | |
27 if they are already defined. */ | |
28 #undef BSD | |
29 #ifdef BSD4_3 | |
30 #undef BSD4_3 | |
31 #define XBSD4_3 /* XBSD4_3 says BSD4_3 is supposed to be defined. */ | |
32 #endif | |
33 #include <sys/param.h> | |
34 /* Now if BSD or BSD4_3 was defined and is no longer, | |
35 define it again. */ | |
36 #ifndef BSD | |
37 #define BSD | |
38 #endif | |
39 #ifdef XBSD4_3 | |
40 #ifndef BSD4_3 | |
41 #define BSD4_3 | |
42 #endif | |
43 #endif /* XBSD4_3 */ | |
44 #endif /* BSD */ | |
45 #ifndef VMS | |
46 #ifndef NLIST_STRUCT | |
47 #include <a.out.h> | |
48 #else /* NLIST_STRUCT */ | |
49 #include <nlist.h> | |
50 #endif /* NLIST_STRUCT */ | |
51 #endif /* not VMS */ | |
52 #endif /* LOAD_AVE_TYPE */ | |
53 | |
350 | 54 #ifdef DGUX |
55 #include <sys/dg_sys_info.h> /* for load average info - DJB */ | |
56 #endif | |
57 | |
211 | 58 /* Note on some machines this defines `vector' as a typedef, |
59 so make sure we don't use that name in this file. */ | |
60 #undef vector | |
61 #define vector ***** | |
62 | |
63 #ifdef NULL | |
64 #undef NULL | |
65 #endif | |
66 #include "lisp.h" | |
67 #include "commands.h" | |
68 | |
69 #ifdef MULTI_SCREEN | |
70 #include "screen.h" | |
71 #endif | |
72 | |
73 #include "buffer.h" | |
74 | |
75 Lisp_Object Qstring_lessp; | |
76 | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
77 static Lisp_Object internal_equal (); |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
78 |
211 | 79 DEFUN ("identity", Fidentity, Sidentity, 1, 1, 0, |
80 "Return the argument unchanged.") | |
81 (arg) | |
82 Lisp_Object arg; | |
83 { | |
84 return arg; | |
85 } | |
86 | |
87 DEFUN ("random", Frandom, Srandom, 0, 1, 0, | |
88 "Return a pseudo-random number.\n\ | |
89 On most systems all integers representable in Lisp are equally likely.\n\ | |
90 This is 24 bits' worth.\n\ | |
91 With argument N, return random number in interval [0,N).\n\ | |
92 With argument t, set the random number seed from the current time and pid.") | |
93 (arg) | |
94 Lisp_Object arg; | |
95 { | |
96 int val; | |
97 extern long random (); | |
98 extern srandom (); | |
99 extern long time (); | |
100 | |
101 if (EQ (arg, Qt)) | |
102 srandom (getpid () + time (0)); | |
103 val = random (); | |
104 if (XTYPE (arg) == Lisp_Int && XINT (arg) != 0) | |
105 { | |
106 /* Try to take our random number from the higher bits of VAL, | |
107 not the lower, since (says Gentzel) the low bits of `random' | |
108 are less random than the higher ones. */ | |
109 val &= 0xfffffff; /* Ensure positive. */ | |
110 val >>= 5; | |
111 if (XINT (arg) < 10000) | |
112 val >>= 6; | |
113 val %= XINT (arg); | |
114 } | |
115 return make_number (val); | |
116 } | |
117 | |
118 /* Random data-structure functions */ | |
119 | |
120 DEFUN ("length", Flength, Slength, 1, 1, 0, | |
121 "Return the length of vector, list or string SEQUENCE.\n\ | |
122 A byte-code function object is also allowed.") | |
123 (obj) | |
124 register Lisp_Object obj; | |
125 { | |
126 register Lisp_Object tail, val; | |
127 register int i; | |
128 | |
129 retry: | |
130 if (XTYPE (obj) == Lisp_Vector || XTYPE (obj) == Lisp_String | |
131 || XTYPE (obj) == Lisp_Compiled) | |
132 return Farray_length (obj); | |
133 else if (CONSP (obj)) | |
134 { | |
135 for (i = 0, tail = obj; !NULL(tail); i++) | |
136 { | |
137 QUIT; | |
138 tail = Fcdr (tail); | |
139 } | |
140 | |
141 XFASTINT (val) = i; | |
142 return val; | |
143 } | |
144 else if (NULL(obj)) | |
145 { | |
146 XFASTINT (val) = 0; | |
147 return val; | |
148 } | |
149 else | |
150 { | |
151 obj = wrong_type_argument (Qsequencep, obj); | |
152 goto retry; | |
153 } | |
154 } | |
155 | |
156 DEFUN ("string-equal", Fstring_equal, Sstring_equal, 2, 2, 0, | |
157 "T if two strings have identical contents.\n\ | |
158 Case is significant.\n\ | |
159 Symbols are also allowed; their print names are used instead.") | |
160 (s1, s2) | |
161 register Lisp_Object s1, s2; | |
162 { | |
163 if (XTYPE (s1) == Lisp_Symbol) | |
164 XSETSTRING (s1, XSYMBOL (s1)->name), XSETTYPE (s1, Lisp_String); | |
165 if (XTYPE (s2) == Lisp_Symbol) | |
166 XSETSTRING (s2, XSYMBOL (s2)->name), XSETTYPE (s2, Lisp_String); | |
167 CHECK_STRING (s1, 0); | |
168 CHECK_STRING (s2, 1); | |
169 | |
170 if (XSTRING (s1)->size != XSTRING (s2)->size || | |
171 bcmp (XSTRING (s1)->data, XSTRING (s2)->data, XSTRING (s1)->size)) | |
172 return Qnil; | |
173 return Qt; | |
174 } | |
175 | |
176 DEFUN ("string-lessp", Fstring_lessp, Sstring_lessp, 2, 2, 0, | |
177 "T if first arg string is less than second in lexicographic order.\n\ | |
178 Case is significant.\n\ | |
179 Symbols are also allowed; their print names are used instead.") | |
180 (s1, s2) | |
181 register Lisp_Object s1, s2; | |
182 { | |
183 register int i; | |
184 register unsigned char *p1, *p2; | |
185 register int end; | |
186 | |
187 if (XTYPE (s1) == Lisp_Symbol) | |
188 XSETSTRING (s1, XSYMBOL (s1)->name), XSETTYPE (s1, Lisp_String); | |
189 if (XTYPE (s2) == Lisp_Symbol) | |
190 XSETSTRING (s2, XSYMBOL (s2)->name), XSETTYPE (s2, Lisp_String); | |
191 CHECK_STRING (s1, 0); | |
192 CHECK_STRING (s2, 1); | |
193 | |
194 p1 = XSTRING (s1)->data; | |
195 p2 = XSTRING (s2)->data; | |
196 end = XSTRING (s1)->size; | |
197 if (end > XSTRING (s2)->size) | |
198 end = XSTRING (s2)->size; | |
199 | |
200 for (i = 0; i < end; i++) | |
201 { | |
202 if (p1[i] != p2[i]) | |
203 return p1[i] < p2[i] ? Qt : Qnil; | |
204 } | |
205 return i < XSTRING (s2)->size ? Qt : Qnil; | |
206 } | |
207 | |
208 static Lisp_Object concat (); | |
209 | |
210 /* ARGSUSED */ | |
211 Lisp_Object | |
212 concat2 (s1, s2) | |
213 Lisp_Object s1, s2; | |
214 { | |
215 #ifdef NO_ARG_ARRAY | |
216 Lisp_Object args[2]; | |
217 args[0] = s1; | |
218 args[1] = s2; | |
219 return concat (2, args, Lisp_String, 0); | |
220 #else | |
221 return concat (2, &s1, Lisp_String, 0); | |
222 #endif /* NO_ARG_ARRAY */ | |
223 } | |
224 | |
225 DEFUN ("append", Fappend, Sappend, 0, MANY, 0, | |
226 "Concatenate all the arguments and make the result a list.\n\ | |
227 The result is a list whose elements are the elements of all the arguments.\n\ | |
228 Each argument may be a list, vector or string.\n\ | |
229 The last argument is not copied if it is a list.") | |
230 (nargs, args) | |
231 int nargs; | |
232 Lisp_Object *args; | |
233 { | |
234 return concat (nargs, args, Lisp_Cons, 1); | |
235 } | |
236 | |
237 DEFUN ("concat", Fconcat, Sconcat, 0, MANY, 0, | |
238 "Concatenate all the arguments and make the result a string.\n\ | |
239 The result is a string whose elements are the elements of all the arguments.\n\ | |
240 Each argument may be a string, a list of numbers, or a vector of numbers.") | |
241 (nargs, args) | |
242 int nargs; | |
243 Lisp_Object *args; | |
244 { | |
245 return concat (nargs, args, Lisp_String, 0); | |
246 } | |
247 | |
248 DEFUN ("vconcat", Fvconcat, Svconcat, 0, MANY, 0, | |
249 "Concatenate all the arguments and make the result a vector.\n\ | |
250 The result is a vector whose elements are the elements of all the arguments.\n\ | |
251 Each argument may be a list, vector or string.") | |
252 (nargs, args) | |
253 int nargs; | |
254 Lisp_Object *args; | |
255 { | |
256 return concat (nargs, args, Lisp_Vector, 0); | |
257 } | |
258 | |
259 DEFUN ("copy-sequence", Fcopy_sequence, Scopy_sequence, 1, 1, 0, | |
260 "Return a copy of a list, vector or string.\n\ | |
261 The elements of a list or vector are not copied; they are shared\n\ | |
262 with the original.") | |
263 (arg) | |
264 Lisp_Object arg; | |
265 { | |
266 if (NULL (arg)) return arg; | |
267 if (!CONSP (arg) && XTYPE (arg) != Lisp_Vector && XTYPE (arg) != Lisp_String) | |
268 arg = wrong_type_argument (Qsequencep, arg); | |
269 return concat (1, &arg, CONSP (arg) ? Lisp_Cons : XTYPE (arg), 0); | |
270 } | |
271 | |
272 static Lisp_Object | |
273 concat (nargs, args, target_type, last_special) | |
274 int nargs; | |
275 Lisp_Object *args; | |
276 enum Lisp_Type target_type; | |
277 int last_special; | |
278 { | |
279 Lisp_Object val; | |
280 Lisp_Object len; | |
281 register Lisp_Object tail; | |
282 register Lisp_Object this; | |
283 int toindex; | |
284 register int leni; | |
285 register int argnum; | |
286 Lisp_Object last_tail; | |
287 Lisp_Object prev; | |
288 | |
289 /* In append, the last arg isn't treated like the others */ | |
290 if (last_special && nargs > 0) | |
291 { | |
292 nargs--; | |
293 last_tail = args[nargs]; | |
294 } | |
295 else | |
296 last_tail = Qnil; | |
297 | |
298 for (argnum = 0; argnum < nargs; argnum++) | |
299 { | |
300 this = args[argnum]; | |
301 if (!(CONSP (this) || NULL (this) | |
302 || XTYPE (this) == Lisp_Vector || XTYPE (this) == Lisp_String | |
303 || XTYPE (this) == Lisp_Compiled)) | |
304 { | |
305 if (XTYPE (this) == Lisp_Int) | |
306 args[argnum] = Fint_to_string (this); | |
307 else | |
308 args[argnum] = wrong_type_argument (Qsequencep, this); | |
309 } | |
310 } | |
311 | |
312 for (argnum = 0, leni = 0; argnum < nargs; argnum++) | |
313 { | |
314 this = args[argnum]; | |
315 len = Flength (this); | |
316 leni += XFASTINT (len); | |
317 } | |
318 | |
319 XFASTINT (len) = leni; | |
320 | |
321 if (target_type == Lisp_Cons) | |
322 val = Fmake_list (len, Qnil); | |
323 else if (target_type == Lisp_Vector) | |
324 val = Fmake_vector (len, Qnil); | |
325 else | |
326 val = Fmake_string (len, len); | |
327 | |
328 /* In append, if all but last arg are nil, return last arg */ | |
329 if (target_type == Lisp_Cons && EQ (val, Qnil)) | |
330 return last_tail; | |
331 | |
332 if (CONSP (val)) | |
333 tail = val, toindex = -1; /* -1 in toindex is flag we are making a list */ | |
334 else | |
335 toindex = 0; | |
336 | |
337 prev = Qnil; | |
338 | |
339 for (argnum = 0; argnum < nargs; argnum++) | |
340 { | |
341 Lisp_Object thislen; | |
342 int thisleni; | |
343 register int thisindex = 0; | |
344 | |
345 this = args[argnum]; | |
346 if (!CONSP (this)) | |
347 thislen = Flength (this), thisleni = XINT (thislen); | |
348 | |
349 while (1) | |
350 { | |
351 register Lisp_Object elt; | |
352 | |
353 /* Fetch next element of `this' arg into `elt', or break if `this' is exhausted. */ | |
354 if (NULL (this)) break; | |
355 if (CONSP (this)) | |
356 elt = Fcar (this), this = Fcdr (this); | |
357 else | |
358 { | |
359 if (thisindex >= thisleni) break; | |
360 if (XTYPE (this) == Lisp_String) | |
361 XFASTINT (elt) = XSTRING (this)->data[thisindex++]; | |
362 else | |
363 elt = XVECTOR (this)->contents[thisindex++]; | |
364 } | |
365 | |
366 /* Store into result */ | |
367 if (toindex < 0) | |
368 { | |
369 XCONS (tail)->car = elt; | |
370 prev = tail; | |
371 tail = XCONS (tail)->cdr; | |
372 } | |
373 else if (XTYPE (val) == Lisp_Vector) | |
374 XVECTOR (val)->contents[toindex++] = elt; | |
375 else | |
376 { | |
377 while (XTYPE (elt) != Lisp_Int) | |
378 elt = wrong_type_argument (Qintegerp, elt); | |
379 { | |
380 #ifdef MASSC_REGISTER_BUG | |
381 /* Even removing all "register"s doesn't disable this bug! | |
382 Nothing simpler than this seems to work. */ | |
383 unsigned char *p = & XSTRING (val)->data[toindex++]; | |
384 *p = XINT (elt); | |
385 #else | |
386 XSTRING (val)->data[toindex++] = XINT (elt); | |
387 #endif | |
388 } | |
389 } | |
390 } | |
391 } | |
392 if (!NULL (prev)) | |
393 XCONS (prev)->cdr = last_tail; | |
394 | |
395 return val; | |
396 } | |
397 | |
398 DEFUN ("copy-alist", Fcopy_alist, Scopy_alist, 1, 1, 0, | |
399 "Return a copy of ALIST.\n\ | |
400 This is an alist which represents the same mapping from objects to objects,\n\ | |
401 but does not share the alist structure with ALIST.\n\ | |
402 The objects mapped (cars and cdrs of elements of the alist)\n\ | |
403 are shared, however.\n\ | |
404 Elements of ALIST that are not conses are also shared.") | |
405 (alist) | |
406 Lisp_Object alist; | |
407 { | |
408 register Lisp_Object tem; | |
409 | |
410 CHECK_LIST (alist, 0); | |
411 if (NULL (alist)) | |
412 return alist; | |
413 alist = concat (1, &alist, Lisp_Cons, 0); | |
414 for (tem = alist; CONSP (tem); tem = XCONS (tem)->cdr) | |
415 { | |
416 register Lisp_Object car; | |
417 car = XCONS (tem)->car; | |
418 | |
419 if (CONSP (car)) | |
420 XCONS (tem)->car = Fcons (XCONS (car)->car, XCONS (car)->cdr); | |
421 } | |
422 return alist; | |
423 } | |
424 | |
425 DEFUN ("substring", Fsubstring, Ssubstring, 2, 3, 0, | |
426 "Return a substring of STRING, starting at index FROM and ending before TO.\n\ | |
427 TO may be nil or omitted; then the substring runs to the end of STRING.\n\ | |
428 If FROM or TO is negative, it counts from the end.") | |
429 (string, from, to) | |
430 Lisp_Object string; | |
431 register Lisp_Object from, to; | |
432 { | |
433 CHECK_STRING (string, 0); | |
434 CHECK_NUMBER (from, 1); | |
435 if (NULL (to)) | |
436 to = Flength (string); | |
437 else | |
438 CHECK_NUMBER (to, 2); | |
439 | |
440 if (XINT (from) < 0) | |
441 XSETINT (from, XINT (from) + XSTRING (string)->size); | |
442 if (XINT (to) < 0) | |
443 XSETINT (to, XINT (to) + XSTRING (string)->size); | |
444 if (!(0 <= XINT (from) && XINT (from) <= XINT (to) | |
445 && XINT (to) <= XSTRING (string)->size)) | |
446 args_out_of_range_3 (string, from, to); | |
447 | |
448 return make_string (XSTRING (string)->data + XINT (from), | |
449 XINT (to) - XINT (from)); | |
450 } | |
451 | |
452 DEFUN ("nthcdr", Fnthcdr, Snthcdr, 2, 2, 0, | |
453 "Take cdr N times on LIST, returns the result.") | |
454 (n, list) | |
455 Lisp_Object n; | |
456 register Lisp_Object list; | |
457 { | |
458 register int i, num; | |
459 CHECK_NUMBER (n, 0); | |
460 num = XINT (n); | |
461 for (i = 0; i < num && !NULL (list); i++) | |
462 { | |
463 QUIT; | |
464 list = Fcdr (list); | |
465 } | |
466 return list; | |
467 } | |
468 | |
469 DEFUN ("nth", Fnth, Snth, 2, 2, 0, | |
470 "Return the Nth element of LIST.\n\ | |
471 N counts from zero. If LIST is not that long, nil is returned.") | |
472 (n, list) | |
473 Lisp_Object n, list; | |
474 { | |
475 return Fcar (Fnthcdr (n, list)); | |
476 } | |
477 | |
478 DEFUN ("elt", Felt, Selt, 2, 2, 0, | |
479 "Return element of SEQUENCE at index N.") | |
480 (seq, n) | |
481 register Lisp_Object seq, n; | |
482 { | |
483 CHECK_NUMBER (n, 0); | |
484 while (1) | |
485 { | |
486 if (XTYPE (seq) == Lisp_Cons || NULL (seq)) | |
487 return Fcar (Fnthcdr (n, seq)); | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
488 else if (XTYPE (seq) == Lisp_String |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
489 || XTYPE (seq) == Lisp_Vector) |
211 | 490 return Faref (seq, n); |
491 else | |
492 seq = wrong_type_argument (Qsequencep, seq); | |
493 } | |
494 } | |
495 | |
496 DEFUN ("member", Fmember, Smember, 2, 2, 0, | |
497 "Return non-nil if ELT is an element of LIST. Comparison done with EQUAL.\n\ | |
498 The value is actually the tail of LIST whose car is ELT.") | |
499 (elt, list) | |
500 register Lisp_Object elt; | |
501 Lisp_Object list; | |
502 { | |
503 register Lisp_Object tail; | |
504 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
505 { | |
506 register Lisp_Object tem; | |
507 tem = Fcar (tail); | |
508 if (! NULL (Fequal (elt, tem))) | |
509 return tail; | |
510 QUIT; | |
511 } | |
512 return Qnil; | |
513 } | |
514 | |
515 DEFUN ("memq", Fmemq, Smemq, 2, 2, 0, | |
516 "Return non-nil if ELT is an element of LIST. Comparison done with EQ.\n\ | |
517 The value is actually the tail of LIST whose car is ELT.") | |
518 (elt, list) | |
519 register Lisp_Object elt; | |
520 Lisp_Object list; | |
521 { | |
522 register Lisp_Object tail; | |
523 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
524 { | |
525 register Lisp_Object tem; | |
526 tem = Fcar (tail); | |
527 if (EQ (elt, tem)) return tail; | |
528 QUIT; | |
529 } | |
530 return Qnil; | |
531 } | |
532 | |
533 DEFUN ("assq", Fassq, Sassq, 2, 2, 0, | |
534 "Return non-nil if ELT is `eq' to the car of an element of LIST.\n\ | |
535 The value is actually the element of LIST whose car is ELT.\n\ | |
536 Elements of LIST that are not conses are ignored.") | |
537 (key, list) | |
538 register Lisp_Object key; | |
539 Lisp_Object list; | |
540 { | |
541 register Lisp_Object tail; | |
542 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
543 { | |
544 register Lisp_Object elt, tem; | |
545 elt = Fcar (tail); | |
546 if (!CONSP (elt)) continue; | |
547 tem = Fcar (elt); | |
548 if (EQ (key, tem)) return elt; | |
549 QUIT; | |
550 } | |
551 return Qnil; | |
552 } | |
553 | |
554 /* Like Fassq but never report an error and do not allow quits. | |
555 Use only on lists known never to be circular. */ | |
556 | |
557 Lisp_Object | |
558 assq_no_quit (key, list) | |
559 register Lisp_Object key; | |
560 Lisp_Object list; | |
561 { | |
562 register Lisp_Object tail; | |
563 for (tail = list; CONSP (tail); tail = Fcdr (tail)) | |
564 { | |
565 register Lisp_Object elt, tem; | |
566 elt = Fcar (tail); | |
567 if (!CONSP (elt)) continue; | |
568 tem = Fcar (elt); | |
569 if (EQ (key, tem)) return elt; | |
570 } | |
571 return Qnil; | |
572 } | |
573 | |
574 DEFUN ("assoc", Fassoc, Sassoc, 2, 2, 0, | |
575 "Return non-nil if ELT is `equal' to the car of an element of LIST.\n\ | |
576 The value is actually the element of LIST whose car is ELT.") | |
577 (key, list) | |
578 register Lisp_Object key; | |
579 Lisp_Object list; | |
580 { | |
581 register Lisp_Object tail; | |
582 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
583 { | |
584 register Lisp_Object elt, tem; | |
585 elt = Fcar (tail); | |
586 if (!CONSP (elt)) continue; | |
587 tem = Fequal (Fcar (elt), key); | |
588 if (!NULL (tem)) return elt; | |
589 QUIT; | |
590 } | |
591 return Qnil; | |
592 } | |
593 | |
594 DEFUN ("rassq", Frassq, Srassq, 2, 2, 0, | |
595 "Return non-nil if ELT is `eq' to the cdr of an element of LIST.\n\ | |
596 The value is actually the element of LIST whose cdr is ELT.") | |
597 (key, list) | |
598 register Lisp_Object key; | |
599 Lisp_Object list; | |
600 { | |
601 register Lisp_Object tail; | |
602 for (tail = list; !NULL (tail); tail = Fcdr (tail)) | |
603 { | |
604 register Lisp_Object elt, tem; | |
605 elt = Fcar (tail); | |
606 if (!CONSP (elt)) continue; | |
607 tem = Fcdr (elt); | |
608 if (EQ (key, tem)) return elt; | |
609 QUIT; | |
610 } | |
611 return Qnil; | |
612 } | |
613 | |
614 DEFUN ("delq", Fdelq, Sdelq, 2, 2, 0, | |
615 "Delete by side effect any occurrences of ELT as a member of LIST.\n\ | |
616 The modified LIST is returned. Comparison is done with `eq'.\n\ | |
617 If the first member of LIST is ELT, there is no way to remove it by side effect;\n\ | |
618 therefore, write `(setq foo (delq element foo))'\n\ | |
619 to be sure of changing the value of `foo'.") | |
620 (elt, list) | |
621 register Lisp_Object elt; | |
622 Lisp_Object list; | |
623 { | |
624 register Lisp_Object tail, prev; | |
625 register Lisp_Object tem; | |
626 | |
627 tail = list; | |
628 prev = Qnil; | |
629 while (!NULL (tail)) | |
630 { | |
631 tem = Fcar (tail); | |
632 if (EQ (elt, tem)) | |
633 { | |
634 if (NULL (prev)) | |
635 list = Fcdr (tail); | |
636 else | |
637 Fsetcdr (prev, Fcdr (tail)); | |
638 } | |
639 else | |
640 prev = tail; | |
641 tail = Fcdr (tail); | |
642 QUIT; | |
643 } | |
644 return list; | |
645 } | |
646 | |
647 DEFUN ("nreverse", Fnreverse, Snreverse, 1, 1, 0, | |
648 "Reverse LIST by modifying cdr pointers.\n\ | |
649 Returns the beginning of the reversed list.") | |
650 (list) | |
651 Lisp_Object list; | |
652 { | |
653 register Lisp_Object prev, tail, next; | |
654 | |
655 if (NULL (list)) return list; | |
656 prev = Qnil; | |
657 tail = list; | |
658 while (!NULL (tail)) | |
659 { | |
660 QUIT; | |
661 next = Fcdr (tail); | |
662 Fsetcdr (tail, prev); | |
663 prev = tail; | |
664 tail = next; | |
665 } | |
666 return prev; | |
667 } | |
668 | |
669 DEFUN ("reverse", Freverse, Sreverse, 1, 1, 0, | |
670 "Reverse LIST, copying. Returns the beginning of the reversed list.\n\ | |
671 See also the function `nreverse', which is used more often.") | |
672 (list) | |
673 Lisp_Object list; | |
674 { | |
675 Lisp_Object length; | |
676 register Lisp_Object *vec; | |
677 register Lisp_Object tail; | |
678 register int i; | |
679 | |
680 length = Flength (list); | |
681 vec = (Lisp_Object *) alloca (XINT (length) * sizeof (Lisp_Object)); | |
682 for (i = XINT (length) - 1, tail = list; i >= 0; i--, tail = Fcdr (tail)) | |
683 vec[i] = Fcar (tail); | |
684 | |
685 return Flist (XINT (length), vec); | |
686 } | |
687 | |
688 Lisp_Object merge (); | |
689 | |
690 DEFUN ("sort", Fsort, Ssort, 2, 2, 0, | |
691 "Sort LIST, stably, comparing elements using PREDICATE.\n\ | |
692 Returns the sorted list. LIST is modified by side effects.\n\ | |
693 PREDICATE is called with two elements of LIST, and should return T\n\ | |
694 if the first element is \"less\" than the second.") | |
695 (list, pred) | |
696 Lisp_Object list, pred; | |
697 { | |
698 Lisp_Object front, back; | |
699 register Lisp_Object len, tem; | |
700 struct gcpro gcpro1, gcpro2; | |
701 register int length; | |
702 | |
703 front = list; | |
704 len = Flength (list); | |
705 length = XINT (len); | |
706 if (length < 2) | |
707 return list; | |
708 | |
709 XSETINT (len, (length / 2) - 1); | |
710 tem = Fnthcdr (len, list); | |
711 back = Fcdr (tem); | |
712 Fsetcdr (tem, Qnil); | |
713 | |
714 GCPRO2 (front, back); | |
715 front = Fsort (front, pred); | |
716 back = Fsort (back, pred); | |
717 UNGCPRO; | |
718 return merge (front, back, pred); | |
719 } | |
720 | |
721 Lisp_Object | |
722 merge (org_l1, org_l2, pred) | |
723 Lisp_Object org_l1, org_l2; | |
724 Lisp_Object pred; | |
725 { | |
726 Lisp_Object value; | |
727 register Lisp_Object tail; | |
728 Lisp_Object tem; | |
729 register Lisp_Object l1, l2; | |
730 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4; | |
731 | |
732 l1 = org_l1; | |
733 l2 = org_l2; | |
734 tail = Qnil; | |
735 value = Qnil; | |
736 | |
737 /* It is sufficient to protect org_l1 and org_l2. | |
738 When l1 and l2 are updated, we copy the new values | |
739 back into the org_ vars. */ | |
740 GCPRO4 (org_l1, org_l2, pred, value); | |
741 | |
742 while (1) | |
743 { | |
744 if (NULL (l1)) | |
745 { | |
746 UNGCPRO; | |
747 if (NULL (tail)) | |
748 return l2; | |
749 Fsetcdr (tail, l2); | |
750 return value; | |
751 } | |
752 if (NULL (l2)) | |
753 { | |
754 UNGCPRO; | |
755 if (NULL (tail)) | |
756 return l1; | |
757 Fsetcdr (tail, l1); | |
758 return value; | |
759 } | |
760 tem = call2 (pred, Fcar (l2), Fcar (l1)); | |
761 if (NULL (tem)) | |
762 { | |
763 tem = l1; | |
764 l1 = Fcdr (l1); | |
765 org_l1 = l1; | |
766 } | |
767 else | |
768 { | |
769 tem = l2; | |
770 l2 = Fcdr (l2); | |
771 org_l2 = l2; | |
772 } | |
773 if (NULL (tail)) | |
774 value = tem; | |
775 else | |
776 Fsetcdr (tail, tem); | |
777 tail = tem; | |
778 } | |
779 } | |
780 | |
781 DEFUN ("get", Fget, Sget, 2, 2, 0, | |
782 "Return the value of SYMBOL's PROPNAME property.\n\ | |
783 This is the last VALUE stored with `(put SYMBOL PROPNAME VALUE)'.") | |
784 (sym, prop) | |
785 Lisp_Object sym; | |
786 register Lisp_Object prop; | |
787 { | |
788 register Lisp_Object tail; | |
789 for (tail = Fsymbol_plist (sym); !NULL (tail); tail = Fcdr (Fcdr (tail))) | |
790 { | |
791 register Lisp_Object tem; | |
792 tem = Fcar (tail); | |
793 if (EQ (prop, tem)) | |
794 return Fcar (Fcdr (tail)); | |
795 } | |
796 return Qnil; | |
797 } | |
798 | |
799 DEFUN ("put", Fput, Sput, 3, 3, 0, | |
800 "Store SYMBOL's PROPNAME property with value VALUE.\n\ | |
801 It can be retrieved with `(get SYMBOL PROPNAME)'.") | |
802 (sym, prop, val) | |
803 Lisp_Object sym; | |
804 register Lisp_Object prop; | |
805 Lisp_Object val; | |
806 { | |
807 register Lisp_Object tail, prev; | |
808 Lisp_Object newcell; | |
809 prev = Qnil; | |
810 for (tail = Fsymbol_plist (sym); !NULL (tail); tail = Fcdr (Fcdr (tail))) | |
811 { | |
812 register Lisp_Object tem; | |
813 tem = Fcar (tail); | |
814 if (EQ (prop, tem)) | |
815 return Fsetcar (Fcdr (tail), val); | |
816 prev = tail; | |
817 } | |
818 newcell = Fcons (prop, Fcons (val, Qnil)); | |
819 if (NULL (prev)) | |
820 Fsetplist (sym, newcell); | |
821 else | |
822 Fsetcdr (Fcdr (prev), newcell); | |
823 return val; | |
824 } | |
825 | |
826 DEFUN ("equal", Fequal, Sequal, 2, 2, 0, | |
827 "T if two Lisp objects have similar structure and contents.\n\ | |
828 They must have the same data type.\n\ | |
829 Conses are compared by comparing the cars and the cdrs.\n\ | |
830 Vectors and strings are compared element by element.\n\ | |
831 Numbers are compared by value. Symbols must match exactly.") | |
832 (o1, o2) | |
833 register Lisp_Object o1, o2; | |
834 { | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
835 return internal_equal (o1, o2, 0); |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
836 } |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
837 |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
838 static Lisp_Object |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
839 internal_equal (o1, o2, depth) |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
840 register Lisp_Object o1, o2; |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
841 int depth; |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
842 { |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
843 if (depth > 200) |
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
844 error ("Stack overflow in equal"); |
211 | 845 do_cdr: |
846 QUIT; | |
847 if (XTYPE (o1) != XTYPE (o2)) return Qnil; | |
848 if (XINT (o1) == XINT (o2)) return Qt; | |
849 if (XTYPE (o1) == Lisp_Cons) | |
850 { | |
851 Lisp_Object v1; | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
852 v1 = Fequal (Fcar (o1), Fcar (o2), depth + 1); |
211 | 853 if (NULL (v1)) |
854 return v1; | |
855 o1 = Fcdr (o1), o2 = Fcdr (o2); | |
856 goto do_cdr; | |
857 } | |
858 if (XTYPE (o1) == Lisp_Marker) | |
859 { | |
860 return (XMARKER (o1)->buffer == XMARKER (o2)->buffer | |
861 && XMARKER (o1)->bufpos == XMARKER (o2)->bufpos) | |
862 ? Qt : Qnil; | |
863 } | |
864 if (XTYPE (o1) == Lisp_Vector) | |
865 { | |
866 register int index; | |
867 if (XVECTOR (o1)->size != XVECTOR (o2)->size) | |
868 return Qnil; | |
869 for (index = 0; index < XVECTOR (o1)->size; index++) | |
870 { | |
871 Lisp_Object v, v1, v2; | |
872 v1 = XVECTOR (o1)->contents [index]; | |
873 v2 = XVECTOR (o2)->contents [index]; | |
399
21aa17a1560d
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
350
diff
changeset
|
874 v = Fequal (v1, v2, depth + 1); |
211 | 875 if (NULL (v)) return v; |
876 } | |
877 return Qt; | |
878 } | |
879 if (XTYPE (o1) == Lisp_String) | |
880 { | |
881 if (XSTRING (o1)->size != XSTRING (o2)->size) | |
882 return Qnil; | |
883 if (bcmp (XSTRING (o1)->data, XSTRING (o2)->data, XSTRING (o1)->size)) | |
884 return Qnil; | |
885 return Qt; | |
886 } | |
887 return Qnil; | |
888 } | |
889 | |
890 DEFUN ("fillarray", Ffillarray, Sfillarray, 2, 2, 0, | |
891 "Store each element of ARRAY with ITEM. ARRAY is a vector or string.") | |
892 (array, item) | |
893 Lisp_Object array, item; | |
894 { | |
895 register int size, index, charval; | |
896 retry: | |
897 if (XTYPE (array) == Lisp_Vector) | |
898 { | |
899 register Lisp_Object *p = XVECTOR (array)->contents; | |
900 size = XVECTOR (array)->size; | |
901 for (index = 0; index < size; index++) | |
902 p[index] = item; | |
903 } | |
904 else if (XTYPE (array) == Lisp_String) | |
905 { | |
906 register unsigned char *p = XSTRING (array)->data; | |
907 CHECK_NUMBER (item, 1); | |
908 charval = XINT (item); | |
909 size = XSTRING (array)->size; | |
910 for (index = 0; index < size; index++) | |
911 p[index] = charval; | |
912 } | |
913 else | |
914 { | |
915 array = wrong_type_argument (Qarrayp, array); | |
916 goto retry; | |
917 } | |
918 return array; | |
919 } | |
920 | |
921 /* ARGSUSED */ | |
922 Lisp_Object | |
923 nconc2 (s1, s2) | |
924 Lisp_Object s1, s2; | |
925 { | |
926 #ifdef NO_ARG_ARRAY | |
927 Lisp_Object args[2]; | |
928 args[0] = s1; | |
929 args[1] = s2; | |
930 return Fnconc (2, args); | |
931 #else | |
932 return Fnconc (2, &s1); | |
933 #endif /* NO_ARG_ARRAY */ | |
934 } | |
935 | |
936 DEFUN ("nconc", Fnconc, Snconc, 0, MANY, 0, | |
937 "Concatenate any number of lists by altering them.\n\ | |
938 Only the last argument is not altered, and need not be a list.") | |
939 (nargs, args) | |
940 int nargs; | |
941 Lisp_Object *args; | |
942 { | |
943 register int argnum; | |
944 register Lisp_Object tail, tem, val; | |
945 | |
946 val = Qnil; | |
947 | |
948 for (argnum = 0; argnum < nargs; argnum++) | |
949 { | |
950 tem = args[argnum]; | |
951 if (NULL (tem)) continue; | |
952 | |
953 if (NULL (val)) | |
954 val = tem; | |
955 | |
956 if (argnum + 1 == nargs) break; | |
957 | |
958 if (!CONSP (tem)) | |
959 tem = wrong_type_argument (Qlistp, tem); | |
960 | |
961 while (CONSP (tem)) | |
962 { | |
963 tail = tem; | |
964 tem = Fcdr (tail); | |
965 QUIT; | |
966 } | |
967 | |
968 tem = args[argnum + 1]; | |
969 Fsetcdr (tail, tem); | |
970 if (NULL (tem)) | |
971 args[argnum + 1] = tail; | |
972 } | |
973 | |
974 return val; | |
975 } | |
976 | |
977 /* This is the guts of all mapping functions. | |
978 Apply fn to each element of seq, one by one, | |
979 storing the results into elements of vals, a C vector of Lisp_Objects. | |
980 leni is the length of vals, which should also be the length of seq. */ | |
981 | |
982 static void | |
983 mapcar1 (leni, vals, fn, seq) | |
984 int leni; | |
985 Lisp_Object *vals; | |
986 Lisp_Object fn, seq; | |
987 { | |
988 register Lisp_Object tail; | |
989 Lisp_Object dummy; | |
990 register int i; | |
991 struct gcpro gcpro1, gcpro2, gcpro3; | |
992 | |
993 /* Don't let vals contain any garbage when GC happens. */ | |
994 for (i = 0; i < leni; i++) | |
995 vals[i] = Qnil; | |
996 | |
997 GCPRO3 (dummy, fn, seq); | |
998 gcpro1.var = vals; | |
999 gcpro1.nvars = leni; | |
1000 /* We need not explicitly protect `tail' because it is used only on lists, and | |
1001 1) lists are not relocated and 2) the list is marked via `seq' so will not be freed */ | |
1002 | |
1003 if (XTYPE (seq) == Lisp_Vector) | |
1004 { | |
1005 for (i = 0; i < leni; i++) | |
1006 { | |
1007 dummy = XVECTOR (seq)->contents[i]; | |
1008 vals[i] = call1 (fn, dummy); | |
1009 } | |
1010 } | |
1011 else if (XTYPE (seq) == Lisp_String) | |
1012 { | |
1013 for (i = 0; i < leni; i++) | |
1014 { | |
1015 XFASTINT (dummy) = XSTRING (seq)->data[i]; | |
1016 vals[i] = call1 (fn, dummy); | |
1017 } | |
1018 } | |
1019 else /* Must be a list, since Flength did not get an error */ | |
1020 { | |
1021 tail = seq; | |
1022 for (i = 0; i < leni; i++) | |
1023 { | |
1024 vals[i] = call1 (fn, Fcar (tail)); | |
1025 tail = Fcdr (tail); | |
1026 } | |
1027 } | |
1028 | |
1029 UNGCPRO; | |
1030 } | |
1031 | |
1032 DEFUN ("mapconcat", Fmapconcat, Smapconcat, 3, 3, 0, | |
1033 "Apply FN to each element of SEQ, and concat the results as strings.\n\ | |
1034 In between each pair of results, stick in SEP.\n\ | |
1035 Thus, \" \" as SEP results in spaces between the values return by FN.") | |
1036 (fn, seq, sep) | |
1037 Lisp_Object fn, seq, sep; | |
1038 { | |
1039 Lisp_Object len; | |
1040 register int leni; | |
1041 int nargs; | |
1042 register Lisp_Object *args; | |
1043 register int i; | |
1044 struct gcpro gcpro1; | |
1045 | |
1046 len = Flength (seq); | |
1047 leni = XINT (len); | |
1048 nargs = leni + leni - 1; | |
1049 if (nargs < 0) return build_string (""); | |
1050 | |
1051 args = (Lisp_Object *) alloca (nargs * sizeof (Lisp_Object)); | |
1052 | |
1053 GCPRO1 (sep); | |
1054 mapcar1 (leni, args, fn, seq); | |
1055 UNGCPRO; | |
1056 | |
1057 for (i = leni - 1; i >= 0; i--) | |
1058 args[i + i] = args[i]; | |
1059 | |
1060 for (i = 1; i < nargs; i += 2) | |
1061 args[i] = sep; | |
1062 | |
1063 return Fconcat (nargs, args); | |
1064 } | |
1065 | |
1066 DEFUN ("mapcar", Fmapcar, Smapcar, 2, 2, 0, | |
1067 "Apply FUNCTION to each element of SEQUENCE, and make a list of the results.\n\ | |
1068 The result is a list just as long as SEQUENCE.\n\ | |
1069 SEQUENCE may be a list, a vector or a string.") | |
1070 (fn, seq) | |
1071 Lisp_Object fn, seq; | |
1072 { | |
1073 register Lisp_Object len; | |
1074 register int leni; | |
1075 register Lisp_Object *args; | |
1076 | |
1077 len = Flength (seq); | |
1078 leni = XFASTINT (len); | |
1079 args = (Lisp_Object *) alloca (leni * sizeof (Lisp_Object)); | |
1080 | |
1081 mapcar1 (leni, args, fn, seq); | |
1082 | |
1083 return Flist (leni, args); | |
1084 } | |
1085 | |
1086 /* Anything that calls this function must protect from GC! */ | |
1087 | |
1088 DEFUN ("y-or-n-p", Fy_or_n_p, Sy_or_n_p, 1, 1, 0, | |
1089 "Ask user a \"y or n\" question. Return t if answer is \"y\".\n\ | |
1090 No confirmation of the answer is requested; a single character is enough.\n\ | |
1091 Also accepts Space to mean yes, or Delete to mean no.") | |
1092 (prompt) | |
1093 Lisp_Object prompt; | |
1094 { | |
1095 register Lisp_Object obj; | |
1096 register int ans; | |
1097 Lisp_Object xprompt; | |
1098 Lisp_Object args[2]; | |
1099 int ocech = cursor_in_echo_area; | |
1100 struct gcpro gcpro1, gcpro2; | |
1101 | |
1102 CHECK_STRING (prompt, 0); | |
1103 xprompt = prompt; | |
1104 GCPRO2 (prompt, xprompt); | |
1105 | |
1106 while (1) | |
1107 { | |
1108 message ("%s(y or n) ", XSTRING (xprompt)->data); | |
1109 cursor_in_echo_area = 1; | |
1110 | |
1111 obj = read_char (0); | |
1112 if (XTYPE (obj) == Lisp_Int) | |
1113 ans = XINT (obj); | |
1114 else | |
1115 continue; | |
1116 | |
1117 cursor_in_echo_area = -1; | |
1118 message ("%s(y or n) %c", XSTRING (xprompt)->data, ans); | |
1119 cursor_in_echo_area = ocech; | |
1120 /* Accept a C-g or C-] (abort-recursive-edit) as quit requests. */ | |
1121 if (ans == 7 || ans == '\035') | |
1122 Vquit_flag = Qt; | |
1123 QUIT; | |
1124 if (ans >= 0) | |
1125 ans = DOWNCASE (ans); | |
1126 if (ans == 'y' || ans == ' ') | |
1127 { ans = 'y'; break; } | |
1128 if (ans == 'n' || ans == 127) | |
1129 break; | |
1130 | |
1131 Fding (Qnil); | |
1132 Fdiscard_input (); | |
1133 if (EQ (xprompt, prompt)) | |
1134 { | |
1135 args[0] = build_string ("Please answer y or n. "); | |
1136 args[1] = prompt; | |
1137 xprompt = Fconcat (2, args); | |
1138 } | |
1139 } | |
1140 UNGCPRO; | |
1141 return (ans == 'y' ? Qt : Qnil); | |
1142 } | |
1143 | |
1144 /* This is how C code calls `yes-or-no-p' and allows the user | |
1145 to redefined it. | |
1146 | |
1147 Anything that calls this function must protect from GC! */ | |
1148 | |
1149 Lisp_Object | |
1150 do_yes_or_no_p (prompt) | |
1151 Lisp_Object prompt; | |
1152 { | |
1153 return call1 (intern ("yes-or-no-p"), prompt); | |
1154 } | |
1155 | |
1156 /* Anything that calls this function must protect from GC! */ | |
1157 | |
1158 DEFUN ("yes-or-no-p", Fyes_or_no_p, Syes_or_no_p, 1, 1, 0, | |
1159 "Ask user a yes or no question. Return t if answer is yes.\n\ | |
1160 The user must confirm the answer with a newline,\n\ | |
1161 and can rub it out if not confirmed.") | |
1162 (prompt) | |
1163 Lisp_Object prompt; | |
1164 { | |
1165 register Lisp_Object ans; | |
1166 Lisp_Object args[2]; | |
1167 struct gcpro gcpro1; | |
1168 | |
1169 CHECK_STRING (prompt, 0); | |
1170 | |
1171 args[0] = prompt; | |
1172 args[1] = build_string ("(yes or no) "); | |
1173 prompt = Fconcat (2, args); | |
1174 | |
1175 GCPRO1 (prompt); | |
1176 while (1) | |
1177 { | |
1178 ans = Fdowncase (read_minibuf (Vminibuffer_local_map, | |
1179 Qnil, prompt, Qnil, 0)); | |
1180 if (XSTRING (ans)->size == 3 && !strcmp (XSTRING (ans)->data, "yes")) | |
1181 { | |
1182 UNGCPRO; | |
1183 return Qt; | |
1184 } | |
1185 if (XSTRING (ans)->size == 2 && !strcmp (XSTRING (ans)->data, "no")) | |
1186 { | |
1187 UNGCPRO; | |
1188 return Qnil; | |
1189 } | |
1190 | |
1191 Fding (Qnil); | |
1192 Fdiscard_input (); | |
1193 message ("Please answer yes or no."); | |
1194 Fsleep_for (make_number (2)); | |
1195 } | |
1196 UNGCPRO; | |
1197 } | |
1198 | |
1199 /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
350 | 1200 #ifdef DGUX |
1201 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */ | |
1202 | |
1203 #else /* Not DGUX */ | |
1204 | |
211 | 1205 static int ldav_initialized; |
1206 static int ldav_channel; | |
1207 #ifdef LOAD_AVE_TYPE | |
1208 #ifndef VMS | |
1209 static struct nlist ldav_nl[2]; | |
1210 #endif /* VMS */ | |
1211 #endif /* LOAD_AVE_TYPE */ | |
1212 | |
1213 #define channel ldav_channel | |
1214 #define initialized ldav_initialized | |
1215 #define nl ldav_nl | |
350 | 1216 #endif /* Not DGUX */ |
211 | 1217 |
1218 DEFUN ("load-average", Fload_average, Sload_average, 0, 0, 0, | |
1219 "Return list of 1 minute, 5 minute and 15 minute load averages.\n\ | |
1220 Each of the three load averages is multiplied by 100,\n\ | |
1221 then converted to integer.") | |
1222 () | |
1223 { | |
350 | 1224 #ifdef DGUX |
1225 /* perhaps there should be a "sys_load_avg" call in sysdep.c?! - DJB */ | |
1226 load_info.one_minute = 0.0; /* just in case there is an error */ | |
1227 load_info.five_minute = 0.0; | |
1228 load_info.fifteen_minute = 0.0; | |
1229 dg_sys_info (&load_info, DG_SYS_INFO_LOAD_INFO_TYPE, | |
1230 DG_SYS_INFO_LOAD_VERSION_0); | |
1231 | |
1232 return Fcons (make_number ((int)(load_info.one_minute * 100.0)), | |
1233 Fcons (make_number ((int)(load_info.five_minute * 100.0)), | |
1234 Fcons (make_number ((int)(load_info.fifteen_minute * 100.0)), | |
1235 Qnil))); | |
1236 #else /* not DGUX */ | |
211 | 1237 #ifndef LOAD_AVE_TYPE |
1238 error ("load-average not implemented for this operating system"); | |
1239 | |
1240 #else /* LOAD_AVE_TYPE defined */ | |
1241 | |
1242 LOAD_AVE_TYPE load_ave[3]; | |
1243 #ifdef VMS | |
1244 #ifndef eunice | |
1245 #include <iodef.h> | |
1246 #include <descrip.h> | |
1247 #else | |
1248 #include <vms/iodef.h> | |
1249 struct {int dsc$w_length; char *dsc$a_pointer;} descriptor; | |
1250 #endif /* eunice */ | |
1251 #endif /* VMS */ | |
1252 | |
1253 /* If this fails for any reason, we can return (0 0 0) */ | |
1254 load_ave[0] = 0.0; load_ave[1] = 0.0; load_ave[2] = 0.0; | |
1255 | |
1256 #ifdef VMS | |
1257 /* | |
1258 * VMS specific code -- read from the Load Ave driver | |
1259 */ | |
1260 | |
1261 /* | |
1262 * Ensure that there is a channel open to the load ave device | |
1263 */ | |
1264 if (initialized == 0) | |
1265 { | |
1266 /* Attempt to open the channel */ | |
1267 #ifdef eunice | |
1268 descriptor.size = 18; | |
1269 descriptor.ptr = "$$VMS_LOAD_AVERAGE"; | |
1270 #else | |
1271 $DESCRIPTOR(descriptor, "LAV0:"); | |
1272 #endif | |
1273 if (sys$assign (&descriptor, &channel, 0, 0) & 1) | |
1274 initialized = 1; | |
1275 } | |
1276 /* | |
1277 * Read the load average vector | |
1278 */ | |
1279 if (initialized) | |
1280 { | |
1281 if (!(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0, | |
1282 load_ave, 12, 0, 0, 0, 0) | |
1283 & 1)) | |
1284 { | |
1285 sys$dassgn (channel); | |
1286 initialized = 0; | |
1287 } | |
1288 } | |
1289 #else /* not VMS */ | |
1290 /* | |
1291 * 4.2BSD UNIX-specific code -- read _avenrun from /dev/kmem | |
1292 */ | |
1293 | |
1294 /* | |
1295 * Make sure we have the address of _avenrun | |
1296 */ | |
1297 if (nl[0].n_value == 0) | |
1298 { | |
1299 /* | |
1300 * Get the address of _avenrun | |
1301 */ | |
1302 #ifndef NLIST_STRUCT | |
1303 strcpy (nl[0].n_name, LDAV_SYMBOL); | |
1304 nl[1].n_zeroes = 0; | |
1305 #else /* NLIST_STRUCT */ | |
1306 #ifdef convex | |
1307 nl[0].n_un.n_name = LDAV_SYMBOL; | |
1308 nl[1].n_un.n_name = 0; | |
1309 #else /* not convex */ | |
1310 nl[0].n_name = LDAV_SYMBOL; | |
1311 nl[1].n_name = 0; | |
1312 #endif /* not convex */ | |
1313 #endif /* NLIST_STRUCT */ | |
1314 | |
1315 nlist (KERNEL_FILE, nl); | |
1316 | |
1317 #ifdef FIXUP_KERNEL_SYMBOL_ADDR | |
1318 FIXUP_KERNEL_SYMBOL_ADDR (nl); | |
1319 #endif /* FIXUP_KERNEL_SYMBOL_ADDR */ | |
1320 } | |
1321 /* | |
1322 * Make sure we have /dev/kmem open | |
1323 */ | |
1324 if (initialized == 0) | |
1325 { | |
1326 /* | |
1327 * Open /dev/kmem | |
1328 */ | |
1329 channel = open ("/dev/kmem", 0); | |
1330 if (channel >= 0) initialized = 1; | |
1331 } | |
1332 /* | |
1333 * If we can, get the load ave values | |
1334 */ | |
1335 if ((nl[0].n_value != 0) && (initialized != 0)) | |
1336 { | |
1337 /* | |
1338 * Seek to the correct address | |
1339 */ | |
1340 lseek (channel, (long) nl[0].n_value, 0); | |
1341 if (read (channel, load_ave, sizeof load_ave) | |
1342 != sizeof(load_ave)) | |
1343 { | |
1344 close (channel); | |
1345 initialized = 0; | |
1346 } | |
1347 } | |
1348 #endif /* not VMS */ | |
1349 | |
1350 /* | |
1351 * Return the list of load average values | |
1352 */ | |
1353 return Fcons (make_number (LOAD_AVE_CVT (load_ave[0])), | |
1354 Fcons (make_number (LOAD_AVE_CVT (load_ave[1])), | |
1355 Fcons (make_number (LOAD_AVE_CVT (load_ave[2])), | |
1356 Qnil))); | |
1357 #endif /* LOAD_AVE_TYPE */ | |
350 | 1358 #endif /* not DGUX */ |
211 | 1359 } |
1360 | |
1361 #undef channel | |
1362 #undef initialized | |
1363 #undef nl | |
1364 | |
1365 Lisp_Object Vfeatures; | |
1366 | |
1367 DEFUN ("featurep", Ffeaturep, Sfeaturep, 1, 1, 0, | |
1368 "Returns t if FEATURE is present in this Emacs.\n\ | |
1369 Use this to conditionalize execution of lisp code based on the presence or\n\ | |
1370 absence of emacs or environment extensions.\n\ | |
1371 Use `provide' to declare that a feature is available.\n\ | |
1372 This function looks at the value of the variable `features'.") | |
1373 (feature) | |
1374 Lisp_Object feature; | |
1375 { | |
1376 register Lisp_Object tem; | |
1377 CHECK_SYMBOL (feature, 0); | |
1378 tem = Fmemq (feature, Vfeatures); | |
1379 return (NULL (tem)) ? Qnil : Qt; | |
1380 } | |
1381 | |
1382 DEFUN ("provide", Fprovide, Sprovide, 1, 1, 0, | |
1383 "Announce that FEATURE is a feature of the current Emacs.") | |
1384 (feature) | |
1385 Lisp_Object feature; | |
1386 { | |
1387 register Lisp_Object tem; | |
1388 CHECK_SYMBOL (feature, 0); | |
1389 if (!NULL (Vautoload_queue)) | |
1390 Vautoload_queue = Fcons (Fcons (Vfeatures, Qnil), Vautoload_queue); | |
1391 tem = Fmemq (feature, Vfeatures); | |
1392 if (NULL (tem)) | |
1393 Vfeatures = Fcons (feature, Vfeatures); | |
1394 return feature; | |
1395 } | |
1396 | |
1397 DEFUN ("require", Frequire, Srequire, 1, 2, 0, | |
1398 "If feature FEATURE is not loaded, load it from FILENAME.\n\ | |
1399 If FEATURE is not a member of the list `features', then the feature\n\ | |
1400 is not loaded; so load the file FILENAME.\n\ | |
1401 If FILENAME is omitted, the printname of FEATURE is used as the file name.") | |
1402 (feature, file_name) | |
1403 Lisp_Object feature, file_name; | |
1404 { | |
1405 register Lisp_Object tem; | |
1406 CHECK_SYMBOL (feature, 0); | |
1407 tem = Fmemq (feature, Vfeatures); | |
1408 if (NULL (tem)) | |
1409 { | |
1410 int count = specpdl_ptr - specpdl; | |
1411 | |
1412 /* Value saved here is to be restored into Vautoload_queue */ | |
1413 record_unwind_protect (un_autoload, Vautoload_queue); | |
1414 Vautoload_queue = Qt; | |
1415 | |
1416 Fload (NULL (file_name) ? Fsymbol_name (feature) : file_name, | |
1417 Qnil, Qt, Qnil); | |
1418 | |
1419 tem = Fmemq (feature, Vfeatures); | |
1420 if (NULL (tem)) | |
1421 error ("Required feature %s was not provided", | |
1422 XSYMBOL (feature)->name->data ); | |
1423 | |
1424 /* Once loading finishes, don't undo it. */ | |
1425 Vautoload_queue = Qt; | |
1426 feature = unbind_to (count, feature); | |
1427 } | |
1428 return feature; | |
1429 } | |
1430 | |
1431 syms_of_fns () | |
1432 { | |
1433 Qstring_lessp = intern ("string-lessp"); | |
1434 staticpro (&Qstring_lessp); | |
1435 | |
1436 DEFVAR_LISP ("features", &Vfeatures, | |
1437 "A list of symbols which are the features of the executing emacs.\n\ | |
1438 Used by `featurep' and `require', and altered by `provide'."); | |
1439 Vfeatures = Qnil; | |
1440 | |
1441 defsubr (&Sidentity); | |
1442 defsubr (&Srandom); | |
1443 defsubr (&Slength); | |
1444 defsubr (&Sstring_equal); | |
1445 defsubr (&Sstring_lessp); | |
1446 defsubr (&Sappend); | |
1447 defsubr (&Sconcat); | |
1448 defsubr (&Svconcat); | |
1449 defsubr (&Scopy_sequence); | |
1450 defsubr (&Scopy_alist); | |
1451 defsubr (&Ssubstring); | |
1452 defsubr (&Snthcdr); | |
1453 defsubr (&Snth); | |
1454 defsubr (&Selt); | |
1455 defsubr (&Smember); | |
1456 defsubr (&Smemq); | |
1457 defsubr (&Sassq); | |
1458 defsubr (&Sassoc); | |
1459 defsubr (&Srassq); | |
1460 defsubr (&Sdelq); | |
1461 defsubr (&Snreverse); | |
1462 defsubr (&Sreverse); | |
1463 defsubr (&Ssort); | |
1464 defsubr (&Sget); | |
1465 defsubr (&Sput); | |
1466 defsubr (&Sequal); | |
1467 defsubr (&Sfillarray); | |
1468 defsubr (&Snconc); | |
1469 defsubr (&Smapcar); | |
1470 defsubr (&Smapconcat); | |
1471 defsubr (&Sy_or_n_p); | |
1472 defsubr (&Syes_or_no_p); | |
1473 defsubr (&Sload_average); | |
1474 defsubr (&Sfeaturep); | |
1475 defsubr (&Srequire); | |
1476 defsubr (&Sprovide); | |
1477 } |