deprecated.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /* Copyright 2003-2004,2006,2008-2018,2020
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #define SCM_BUILDING_DEPRECATED_CODE
  22. #include "alist.h"
  23. #include "boolean.h"
  24. #include "bitvectors.h"
  25. #include "deprecation.h"
  26. #include "eval.h"
  27. #include "gc.h"
  28. #include "gsubr.h"
  29. #include "modules.h"
  30. #include "procprop.h"
  31. #include "srcprop.h"
  32. #include "srfi-4.h"
  33. #include "strings.h"
  34. #include "symbols.h"
  35. #include "deprecated.h"
  36. #if (SCM_ENABLE_DEPRECATED == 1)
  37. #ifndef MAXPATHLEN
  38. #define MAXPATHLEN 80
  39. #endif /* ndef MAXPATHLEN */
  40. #ifndef X_OK
  41. #define X_OK 1
  42. #endif /* ndef X_OK */
  43. char *
  44. scm_find_executable (const char *name)
  45. {
  46. char tbuf[MAXPATHLEN];
  47. int i = 0, c;
  48. FILE *f;
  49. scm_c_issue_deprecation_warning ("scm_find_executable is deprecated.");
  50. /* fprintf(stderr, "s_f_e checking access %s ->%d\n", name, access(name, X_OK)); fflush(stderr); */
  51. if (access (name, X_OK))
  52. return 0L;
  53. f = fopen (name, "r");
  54. if (!f)
  55. return 0L;
  56. if ((fgetc (f) == '#') && (fgetc (f) == '!'))
  57. {
  58. while (1)
  59. switch (c = fgetc (f))
  60. {
  61. case /*WHITE_SPACES */ ' ':
  62. case '\t':
  63. case '\r':
  64. case '\f':
  65. case EOF:
  66. tbuf[i] = 0;
  67. fclose (f);
  68. return strdup (tbuf);
  69. default:
  70. tbuf[i++] = c;
  71. break;
  72. }
  73. }
  74. fclose (f);
  75. return strdup (name);
  76. }
  77. SCM
  78. scm_bitvector_p (SCM vec)
  79. {
  80. scm_c_issue_deprecation_warning
  81. ("scm_bitvector_p is deprecated. Use scm_is_bitvector instead.");
  82. return scm_from_bool (scm_is_bitvector (vec));
  83. }
  84. SCM
  85. scm_bitvector (SCM list)
  86. {
  87. scm_c_issue_deprecation_warning
  88. ("scm_bitvector is deprecated. Use scm_list_to_bitvector instead.");
  89. return scm_list_to_bitvector (list);
  90. }
  91. SCM
  92. scm_make_bitvector (SCM len, SCM fill)
  93. {
  94. scm_c_issue_deprecation_warning
  95. ("scm_make_bitvector is deprecated. Use scm_c_make_bitvector instead.");
  96. return scm_c_make_bitvector (scm_to_size_t (len), fill);
  97. }
  98. SCM
  99. scm_bitvector_length (SCM vec)
  100. {
  101. scm_c_issue_deprecation_warning
  102. ("scm_bitvector_length is deprecated. Use scm_c_bitvector_length "
  103. "instead.");
  104. return scm_from_size_t (scm_c_bitvector_length (vec));
  105. }
  106. SCM
  107. scm_c_bitvector_ref (SCM vec, size_t idx)
  108. {
  109. scm_c_issue_deprecation_warning
  110. ("bitvector-ref is deprecated. Use bitvector-bit-set? instead.");
  111. if (scm_is_bitvector (vec))
  112. return scm_from_bool (scm_c_bitvector_bit_is_set (vec, idx));
  113. SCM res;
  114. scm_t_array_handle handle;
  115. size_t len, off;
  116. ssize_t inc;
  117. const uint32_t *bits =
  118. scm_bitvector_elements (vec, &handle, &off, &len, &inc);
  119. if (idx >= len)
  120. scm_out_of_range (NULL, scm_from_size_t (idx));
  121. idx = idx*inc + off;
  122. res = scm_from_bool (bits[idx/32] & (1L << (idx%32)));
  123. scm_array_handle_release (&handle);
  124. return res;
  125. }
  126. SCM_DEFINE (scm_bitvector_ref, "bitvector-ref", 2, 0, 0,
  127. (SCM vec, SCM idx),
  128. "Return the element at index @var{idx} of the bitvector\n"
  129. "@var{vec}.")
  130. #define FUNC_NAME s_scm_bitvector_ref
  131. {
  132. return scm_c_bitvector_ref (vec, scm_to_size_t (idx));
  133. }
  134. #undef FUNC_NAME
  135. void
  136. scm_c_bitvector_set_x (SCM vec, size_t idx, SCM val)
  137. {
  138. scm_c_issue_deprecation_warning
  139. ("bitvector-set! is deprecated. Use bitvector-set-bit! or "
  140. "bitvector-clear-bit! instead.");
  141. if (scm_is_bitvector (vec))
  142. {
  143. if (scm_is_true (val))
  144. scm_c_bitvector_set_bit_x (vec, idx);
  145. else
  146. scm_c_bitvector_clear_bit_x (vec, idx);
  147. }
  148. else
  149. {
  150. scm_t_array_handle handle;
  151. uint32_t *bits, mask;
  152. size_t len, off;
  153. ssize_t inc;
  154. bits = scm_bitvector_writable_elements (vec, &handle, &off, &len, &inc);
  155. if (idx >= len)
  156. scm_out_of_range (NULL, scm_from_size_t (idx));
  157. idx = idx*inc + off;
  158. mask = 1L << (idx%32);
  159. if (scm_is_true (val))
  160. bits[idx/32] |= mask;
  161. else
  162. bits[idx/32] &= ~mask;
  163. scm_array_handle_release (&handle);
  164. }
  165. }
  166. SCM_DEFINE (scm_bitvector_set_x, "bitvector-set!", 3, 0, 0,
  167. (SCM vec, SCM idx, SCM val),
  168. "Set the element at index @var{idx} of the bitvector\n"
  169. "@var{vec} when @var{val} is true, else clear it.")
  170. #define FUNC_NAME s_scm_bitvector_set_x
  171. {
  172. scm_c_bitvector_set_x (vec, scm_to_size_t (idx), val);
  173. return SCM_UNSPECIFIED;
  174. }
  175. #undef FUNC_NAME
  176. SCM_DEFINE (scm_bitvector_fill_x, "bitvector-fill!", 2, 0, 0,
  177. (SCM vec, SCM val),
  178. "Set all elements of the bitvector\n"
  179. "@var{vec} when @var{val} is true, else clear them.")
  180. #define FUNC_NAME s_scm_bitvector_fill_x
  181. {
  182. scm_c_issue_deprecation_warning
  183. ("bitvector-fill! is deprecated. Use bitvector-set-all-bits! or "
  184. "bitvector-clear-all-bits! instead.");
  185. if (scm_is_bitvector (vec))
  186. {
  187. if (scm_is_true (val))
  188. scm_c_bitvector_set_all_bits_x (vec);
  189. else
  190. scm_c_bitvector_clear_all_bits_x (vec);
  191. return SCM_UNSPECIFIED;
  192. }
  193. scm_t_array_handle handle;
  194. size_t off, len;
  195. ssize_t inc;
  196. scm_bitvector_writable_elements (vec, &handle, &off, &len, &inc);
  197. size_t i;
  198. for (i = 0; i < len; i++)
  199. scm_array_handle_set (&handle, i*inc, val);
  200. scm_array_handle_release (&handle);
  201. return SCM_UNSPECIFIED;
  202. }
  203. #undef FUNC_NAME
  204. SCM_DEFINE (scm_bit_invert_x, "bit-invert!", 1, 0, 0,
  205. (SCM v),
  206. "Modify the bit vector @var{v} by replacing each element with\n"
  207. "its negation.")
  208. #define FUNC_NAME s_scm_bit_invert_x
  209. {
  210. scm_c_issue_deprecation_warning
  211. ("bit-invert! is deprecated. Use bitvector-flip-all-bits!, or "
  212. "scalar array accessors in a loop for generic arrays.");
  213. if (scm_is_bitvector (v))
  214. scm_c_bitvector_flip_all_bits_x (v);
  215. else
  216. {
  217. size_t off, len;
  218. ssize_t inc;
  219. scm_t_array_handle handle;
  220. scm_bitvector_writable_elements (v, &handle, &off, &len, &inc);
  221. for (size_t i = 0; i < len; i++)
  222. scm_array_handle_set (&handle, i*inc,
  223. scm_not (scm_array_handle_ref (&handle, i*inc)));
  224. scm_array_handle_release (&handle);
  225. }
  226. return SCM_UNSPECIFIED;
  227. }
  228. #undef FUNC_NAME
  229. SCM_DEFINE (scm_bit_count, "bit-count", 2, 0, 0,
  230. (SCM b, SCM bitvector),
  231. "Return the number of occurrences of the boolean @var{b} in\n"
  232. "@var{bitvector}.")
  233. #define FUNC_NAME s_scm_bit_count
  234. {
  235. int bit = scm_to_bool (b);
  236. size_t count = 0, len;
  237. scm_c_issue_deprecation_warning
  238. ("bit-count is deprecated. Use bitvector-count, or a loop over array-ref "
  239. "if array support is needed.");
  240. if (scm_is_bitvector (bitvector))
  241. {
  242. len = scm_to_size_t (scm_bitvector_length (bitvector));
  243. count = scm_c_bitvector_count (bitvector);
  244. }
  245. else
  246. {
  247. scm_t_array_handle handle;
  248. size_t off;
  249. ssize_t inc;
  250. scm_bitvector_elements (bitvector, &handle, &off, &len, &inc);
  251. for (size_t i = 0; i < len; i++)
  252. if (scm_is_true (scm_array_handle_ref (&handle, i*inc)))
  253. count++;
  254. scm_array_handle_release (&handle);
  255. }
  256. return scm_from_size_t (bit ? count : len-count);
  257. }
  258. #undef FUNC_NAME
  259. SCM_DEFINE (scm_bit_count_star, "bit-count*", 3, 0, 0,
  260. (SCM v, SCM kv, SCM obj),
  261. "Return a count of how many entries in bit vector @var{v} are\n"
  262. "equal to @var{obj}, with @var{kv} selecting the entries to\n"
  263. "consider.\n"
  264. "\n"
  265. "If @var{kv} is a bit vector, then those entries where it has\n"
  266. "@code{#t} are the ones in @var{v} which are considered.\n"
  267. "@var{kv} and @var{v} must be the same length.\n"
  268. "\n"
  269. "If @var{kv} is a u32vector, then it contains\n"
  270. "the indexes in @var{v} to consider.\n"
  271. "\n"
  272. "For example,\n"
  273. "\n"
  274. "@example\n"
  275. "(bit-count* #*01110111 #*11001101 #t) @result{} 3\n"
  276. "(bit-count* #*01110111 #u32(7 0 4) #f) @result{} 2\n"
  277. "@end example")
  278. #define FUNC_NAME s_scm_bit_count_star
  279. {
  280. size_t count = 0;
  281. scm_c_issue_deprecation_warning
  282. ("bit-count* is deprecated. Use bitvector-count-bits instead, and in the "
  283. "case of counting false bits, subtract from a bitvector-count on the "
  284. "selection bitvector.");
  285. /* Validate that OBJ is a boolean so this is done even if we don't
  286. need BIT.
  287. */
  288. int bit = scm_to_bool (obj);
  289. if (scm_is_bitvector (v) && scm_is_bitvector (kv))
  290. {
  291. count = scm_c_bitvector_count_bits (v, kv);
  292. if (count == 0)
  293. count = scm_c_bitvector_count (kv) - count;
  294. }
  295. else
  296. {
  297. scm_t_array_handle v_handle;
  298. size_t v_off, v_len;
  299. ssize_t v_inc;
  300. scm_bitvector_elements (v, &v_handle, &v_off, &v_len, &v_inc);
  301. if (scm_is_bitvector (kv))
  302. {
  303. size_t kv_len = scm_c_bitvector_length (kv);
  304. for (size_t i = 0; i < kv_len; i++)
  305. if (scm_c_bitvector_bit_is_set (kv, i))
  306. {
  307. SCM elt = scm_array_handle_ref (&v_handle, i*v_inc);
  308. if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt)))
  309. count++;
  310. }
  311. }
  312. else if (scm_is_true (scm_u32vector_p (kv)))
  313. {
  314. scm_t_array_handle kv_handle;
  315. size_t i, kv_len;
  316. ssize_t kv_inc;
  317. const uint32_t *kv_elts;
  318. kv_elts = scm_u32vector_elements (kv, &kv_handle, &kv_len, &kv_inc);
  319. for (i = 0; i < kv_len; i++, kv_elts += kv_inc)
  320. {
  321. SCM elt = scm_array_handle_ref (&v_handle, (*kv_elts)*v_inc);
  322. if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt)))
  323. count++;
  324. }
  325. scm_array_handle_release (&kv_handle);
  326. }
  327. else
  328. scm_wrong_type_arg_msg (NULL, 0, kv, "bitvector or u32vector");
  329. scm_array_handle_release (&v_handle);
  330. }
  331. return scm_from_size_t (count);
  332. }
  333. #undef FUNC_NAME
  334. SCM_DEFINE (scm_bit_position, "bit-position", 3, 0, 0,
  335. (SCM item, SCM v, SCM k),
  336. "Return the index of the first occurrence of @var{item} in bit\n"
  337. "vector @var{v}, starting from @var{k}. If there is no\n"
  338. "@var{item} entry between @var{k} and the end of\n"
  339. "@var{v}, then return @code{#f}. For example,\n"
  340. "\n"
  341. "@example\n"
  342. "(bit-position #t #*000101 0) @result{} 3\n"
  343. "(bit-position #f #*0001111 3) @result{} #f\n"
  344. "@end example")
  345. #define FUNC_NAME s_scm_bit_position
  346. {
  347. scm_c_issue_deprecation_warning
  348. ("bit-position is deprecated. Use bitvector-position, or "
  349. "array-ref in a loop if you need generic arrays instead.");
  350. if (scm_is_bitvector (v))
  351. return scm_bitvector_position (v, item, k);
  352. scm_t_array_handle handle;
  353. size_t off, len;
  354. ssize_t inc;
  355. scm_bitvector_elements (v, &handle, &off, &len, &inc);
  356. int bit = scm_to_bool (item);
  357. size_t first_bit = scm_to_unsigned_integer (k, 0, len);
  358. SCM res = SCM_BOOL_F;
  359. for (size_t i = first_bit; i < len; i++)
  360. {
  361. SCM elt = scm_array_handle_ref (&handle, i*inc);
  362. if ((bit && scm_is_true (elt)) || (!bit && scm_is_false (elt)))
  363. {
  364. res = scm_from_size_t (i);
  365. break;
  366. }
  367. }
  368. scm_array_handle_release (&handle);
  369. return res;
  370. }
  371. #undef FUNC_NAME
  372. SCM_DEFINE (scm_bit_set_star_x, "bit-set*!", 3, 0, 0,
  373. (SCM v, SCM kv, SCM obj),
  374. "Set entries of bit vector @var{v} to @var{obj}, with @var{kv}\n"
  375. "selecting the entries to change. The return value is\n"
  376. "unspecified.\n"
  377. "\n"
  378. "If @var{kv} is a bit vector, then those entries where it has\n"
  379. "@code{#t} are the ones in @var{v} which are set to @var{obj}.\n"
  380. "@var{v} must be at least as long as @var{kv}. When @var{obj}\n"
  381. "is @code{#t} it's like @var{kv} is OR'ed into @var{v}. Or when\n"
  382. "@var{obj} is @code{#f} it can be seen as an ANDNOT.\n"
  383. "\n"
  384. "@example\n"
  385. "(define bv #*01000010)\n"
  386. "(bit-set*! bv #*10010001 #t)\n"
  387. "bv\n"
  388. "@result{} #*11010011\n"
  389. "@end example\n"
  390. "\n"
  391. "If @var{kv} is a u32vector, then its elements are\n"
  392. "indices into @var{v} which are set to @var{obj}.\n"
  393. "\n"
  394. "@example\n"
  395. "(define bv #*01000010)\n"
  396. "(bit-set*! bv #u32(5 2 7) #t)\n"
  397. "bv\n"
  398. "@result{} #*01100111\n"
  399. "@end example")
  400. #define FUNC_NAME s_scm_bit_set_star_x
  401. {
  402. scm_c_issue_deprecation_warning
  403. ("bit-set*! is deprecated. Use bitvector-set-bits! or "
  404. "bitvector-clear-bits! on bitvectors, or array-set! in a loop "
  405. "if you need to work on generic arrays.");
  406. int bit = scm_to_bool (obj);
  407. if (scm_is_bitvector (v) && scm_is_bitvector (kv))
  408. {
  409. if (bit)
  410. scm_c_bitvector_set_bits_x (v, kv);
  411. else
  412. scm_c_bitvector_clear_bits_x (v, kv);
  413. return SCM_UNSPECIFIED;
  414. }
  415. scm_t_array_handle v_handle;
  416. size_t v_off, v_len;
  417. ssize_t v_inc;
  418. scm_bitvector_writable_elements (v, &v_handle, &v_off, &v_len, &v_inc);
  419. if (scm_is_bitvector (kv))
  420. {
  421. size_t kv_len = scm_c_bitvector_length (kv);
  422. if (v_len < kv_len)
  423. scm_misc_error (NULL,
  424. "selection bitvector longer than target bitvector",
  425. SCM_EOL);
  426. for (size_t i = 0; i < kv_len; i++)
  427. if (scm_is_true (scm_c_bitvector_ref (kv, i)))
  428. scm_array_handle_set (&v_handle, i*v_inc, obj);
  429. }
  430. else if (scm_is_true (scm_u32vector_p (kv)))
  431. {
  432. scm_t_array_handle kv_handle;
  433. size_t kv_len;
  434. ssize_t kv_inc;
  435. const uint32_t *kv_elts;
  436. kv_elts = scm_u32vector_elements (kv, &kv_handle, &kv_len, &kv_inc);
  437. for (size_t i = 0; i < kv_len; i++, kv_elts += kv_inc)
  438. scm_array_handle_set (&v_handle, (*kv_elts)*v_inc, obj);
  439. scm_array_handle_release (&kv_handle);
  440. }
  441. else
  442. scm_wrong_type_arg_msg (NULL, 0, kv, "bitvector or u32vector");
  443. scm_array_handle_release (&v_handle);
  444. return SCM_UNSPECIFIED;
  445. }
  446. #undef FUNC_NAME
  447. SCM
  448. scm_istr2bve (SCM str)
  449. {
  450. scm_t_array_handle handle;
  451. size_t len = scm_i_string_length (str);
  452. SCM vec = scm_c_make_bitvector (len, SCM_UNDEFINED);
  453. SCM res = vec;
  454. uint32_t mask;
  455. size_t k, j;
  456. const char *c_str;
  457. uint32_t *data;
  458. scm_c_issue_deprecation_warning
  459. ("scm_istr2bve is deprecated. "
  460. "Read from a string instead, prefixed with `#*'.");
  461. data = scm_bitvector_writable_elements (vec, &handle, NULL, NULL, NULL);
  462. c_str = scm_i_string_chars (str);
  463. for (k = 0; k < (len + 31) / 32; k++)
  464. {
  465. data[k] = 0L;
  466. j = len - k * 32;
  467. if (j > 32)
  468. j = 32;
  469. for (mask = 1L; j--; mask <<= 1)
  470. switch (*c_str++)
  471. {
  472. case '0':
  473. break;
  474. case '1':
  475. data[k] |= mask;
  476. break;
  477. default:
  478. res = SCM_BOOL_F;
  479. goto exit;
  480. }
  481. }
  482. exit:
  483. scm_array_handle_release (&handle);
  484. scm_remember_upto_here_1 (str);
  485. return res;
  486. }
  487. SCM_GLOBAL_SYMBOL (scm_sym_copy, "copy");
  488. SCM
  489. scm_make_srcprops (long line, int col, SCM filename, SCM copy, SCM alist)
  490. {
  491. scm_c_issue_deprecation_warning
  492. ("scm_make_srcprops is deprecated; use set-source-properties! instead");
  493. alist = SCM_UNBNDP (copy) ? alist : scm_acons (scm_sym_copy, copy, alist);
  494. return scm_i_make_srcprops (scm_from_long (line), scm_from_int (col),
  495. filename, alist);
  496. }
  497. SCM
  498. scm_copy_tree (SCM obj)
  499. {
  500. scm_c_issue_deprecation_warning
  501. ("scm_copy_tree is deprecated; use copy-tree from (ice-9 copy-tree) "
  502. "instead.");
  503. return scm_call_1 (scm_c_public_ref ("ice-9 copy-tree", "copy-tree"), obj);
  504. }
  505. void
  506. scm_i_init_deprecated ()
  507. {
  508. #include "deprecated.x"
  509. }
  510. #endif /* SCM_ENABLE_DEPRECATD == 1 */