summaryrefslogtreecommitdiffstats
path: root/lib.c
Commit message (Collapse)AuthorAgeFilesLines
* Handle integer overflow in length_list.Kaz Kylheku2016-05-181-2/+14
| | | | | | * lib.c (length_list): Count using cnum only up to INT_PTR_MAX, then switch to bignums.
* Adding termios support.Kaz Kylheku2016-05-181-0/+4
| | | | | | | | | | | | | | | | * Makefile (termios.o): New object file. * lib.c (init): Call termios_init. * lisplib.c (termios_set_entries, termios_instantiate): New functions. (lisplib_init): Register new functions in autoload table. * share/txr/stdlib/termios.tl: New file. * termios.c, termios.h: New files. * txr.1: Documented termios.
* Some streamlining in the cons recycling.Kaz Kylheku2016-05-151-10/+8
| | | | | | | | | | | * lib.c (rcyc_pop): Just assume that *plist points to a cons and access the fields directly. (rcyc_cons): Don't bother with rplacd. (rcyc_list): Don't bother with set macro. * regex.c (read_until_match): Defensive coding: locally ensure that rcyc_pop won't be called on a nil stack, which will now segfault.
* Bugfix: recycled conses play nice with gen GC.Kaz Kylheku2016-05-151-0/+3
| | | | | | | | | | * lib.c (cons): When we recycle a cons, it could be already in the mature generation. In that case, baby objects stored in its car or cdr violate the integrity of the heap. We must hint about this to the garbage collector. Test case: ./txr --gc-debug -p "(list* 'foo (zip '(a) (repeat '((gensym)))))"
* Bugfix in - and / functions.Kaz Kylheku2016-05-131-4/+4
| | | | | | | | | Improper handling of arguments in certain cases. Test case: [[flipargs -] 2 1]. Should yield -1; actual behavior is error. * lib.c (minusv, divv): Correct test for more arguments after extracting the leftmost one.
* Avoid unnecessary evaluation in argument defaulting.Kaz Kylheku2016-05-071-1/+0
| | | | | | | | * lib.h (default_arg): Inline function becomes macro, so we can avoid evaluating the default value expression when it is not needed. * lib.c (default_arg): Declaration removed.
* Adding panic macro, which throws a panic exception.Kaz Kylheku2016-05-071-1/+2
| | | | | | | | | | | * lib.c (panic_s): New symbol variable. (obj_init): Initialize panic_s. * lib.h (panic_s): Declared. * unwind.c (uw_init): Register panic exception. * unwind.h (panic): New macro.
* Clean-up rand code; implement configurable warmup.Kaz Kylheku2016-04-281-1/+1
| | | | | | | | | | | | | | | | | | | | | * lib.c (copy): Pass new second argument to make_random_state. * rand.c (random_warmup): New macro. (random_warmup_s): New symbol variable. (make_random_state): New warmup argument. Code restructured to avoid copy flag. Fill code and initialization of r->cur made common. (rand_compat_fixup): Pass second argument to make_random_state. (rand_init): Pass second argument to make_random_state. Initialize random_warmup_s with interned symbol. Register *random-warmup* special variable. Fix registration of make-random-state to two-argument function type. * rand.h (make_random_state): Declaration updated. * txr.1: Document *random-warmup* variable and warup argument of make-random-state.
* Rename misleadingly named out_str_pretty function.Kaz Kylheku2016-04-211-5/+5
| | | | | | | | * lib.c (out_str_pretty): Rename to out_str_readable. This function is called from obj_print_impl precisely when the pretty flag is false: pretty means output textual things as the text they denote, rather than machine-readable Lisp. (out_lazy_str, obj_print_impl): Calls updated.
* Harmonize rules for string and character printing.Kaz Kylheku2016-04-211-9/+24
| | | | | | | | | | | | | | | | | In this patch we change which characters objects are printed using hex escapes, and which characters are printed as hex when printing string literals. * lib.c (obj_print_impl): Add DEL (U+7F) to the list of character objects which are printed as hex. In a string literal, it's already printed as \x7F. Use upper case hex rather than lower case. (out_str_char): Copy the rules used by obj_print_impl for deciding what string constituents to print as hex and how to print it. So for instance, U+80 to U+A0 will now print in hex as well as the U+D800 to U+DFFF range, rather than just U+DC00 to U+DCFF, and the BOM code U+FFFE, U+FFFF and anything higher.
* Mechanism for recycling conses outside of GC.Kaz Kylheku2016-04-201-2/+47
| | | | | | | | | | | | * lib.c (recycled_conses): New static variable. (rcyc_pop, rcyc_cons, rcyc_list, rcyc_empty): New functions. (cons): Take a recycled cons, if available. * lib.h (rcyc_pop, rcyc_cons, rcyc_list, rcyc_empty): Declared. * gc.c (gc): Call rcyc_empty to make recycle_list unreachable.
* Adding ftw function.Kaz Kylheku2016-04-151-0/+4
| | | | | | | | | | | | | | * Makefile (ftw.o): Add to OBJS-y conditionally. * configure (have_ftw): New variable. New configure test for nftw. (gen_config_make): Set up have_ftw make variable. * ftw.c, ftw.h: New files. * lib.c (init): Call ftw_init, if compiled in. * txr.1: Documented.
* Fix proper-listp to proper-list-p.Kaz Kylheku2016-04-141-1/+1
| | | | | | | | | | | | | | | | | | | | This is really a gratuitous incompatibility with Common Lisp and other dialects. Let's fix it internally also, but keep the proper-listp function binding for backwards compatibility. * eval.c (dot_to_apply, me_op): Update proper_listp call to proper_list_p. (eval_init): Register proper-list-p to the same C function as proper-listp, and that C function is now called proper_list_p. * lib.c (proper_listp): Renamed to proper_list_p. * lib.h (proper_listp): Declaration updated. * parser.y (define_transform): Update proper_listp call. * txr.1: Replace all occurrences of proper-listp with proper-list-p. Add note explaining the rename situation.
* UTF-8 API overhaul: security, and other concerns.Kaz Kylheku2016-03-311-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The main aim here is to pave the way for conversion between arbitrary buffers of bytes (that may include embedded NUL characters) and a wide string. Also, a potential security hole is closed. When we convert a TXR string to UTF-8 for use with some C library API, any embedded pnul characters (U+DC00) turn into NUL bytes which effectively cut the UTF-8 string short, and silently so. The C library function receives a shortened string. This could be exploitable in some situations. * lib.c (int_str): Use utf8_dup_to_buf instead of utf8_dup_to_uc. Pass 1 to have the buffer null-terminated, since mp_read_radix depends on it. * stream.c (make_string_byte_input_stream): Use utf8_dup_to_buf. This gives us the size, soo we don't have to call strlen. The buffer is no longer null terminated, but the byte input stream implementation never relied on this. * utf8.c (utf8_from_buf): Replacement fors utf8_from_uc which doesn't assume that the buffer of bytes is null-terminated. It can produce a wide string containing U+DC00 characters corresponding to embedded nulls in the original buffer. (utf8_from): Calculate length of null-terminated string and use utf8_from_buf. (utf8_to_buf): Replacement for utf8_to_uc. Can produce a buffer which is or is not null-terminated, based on new argument. (utf8_to): Use utf8_to_buf, and ask it to null-terminate, thus preserving behavior. (utf8_dup_from_uc): This function was not used anywhere and is removed. (utf8_dup_to_buf): Replacement for utf8_dup_to_uc which takes an extra agrgument, whether to null-terminate or not. (utf8_dup_to): Apply security check here: is the resulting string as long as utf8_to says it should be? If not, it contains embedded nulls. Throw an exception. * utf.h (utf8_from_uc, utf8_to_uc, utf8_dup_from_uc, utf8_dup_to_uc): Declarations removed. (utf8_from_buf, utf8_to_buf, utf8_dup_to_buf): Declared.
* Adding rightmost item search functions.Kaz Kylheku2016-03-271-1/+267
| | | | | | | | | | | | | | | | | | * eval.c (eval_init): Registered intrinsics rmemq, rmemql, rmemqual, rmember, rmember-if, rposqual, rposql, rposq, rpos, rpos-if, rfind, rfind-if and rsearch. * lib.c (rmemq, rmemql, rmemqual, rmember, rmember-if, rposqual, rposql, rposq, rpos, rpos-if, rfind, rfind-if, rsearch): New functions. (rsearch_list): New static function. (search): Omit unreachable return statement. * lib.h (rmemq, rmemql, rmemqual, rmember, rmember-if, rposqual, rposql, rposq, rpos, rpos-if, rfind, rfind-if, rsearch): Declared. * txr.1: Documented.
* Methods in time struct: time-local and time-utc.Kaz Kylheku2016-03-241-4/+28
| | | | | | | | | | | * lib.c (time_local_s, time_utc_s): New symbol variables. (time_meth): New static function. (time_init): Initialize new symbol variables. Create the time struct with two static slots, and initialize those static slots to be methods. * txr.1: Introduce "the epoch" term. Document the new methods.
* Bugfix: lazy str printing not observing limit.Kaz Kylheku2016-03-241-0/+4
| | | | | * lib.c (lazy_str_put, out_lazy_str): Decrement lim variable in loop body.
* Implement socket timeouts.Kaz Kylheku2016-03-151-1/+2
| | | | | | | | | | | | | | | | | * lib.c (timeout_error_s): New symbol variable. (obj_init): Intern timeout-error, init new variable. * lib.h (timeout_error_s): Declared. * socket.c (sock_timeout, sock_send_timeout, sock_recv_timeout): New static functions. (sock_load_init): Register sock-send-timeout and sock-recv-timeout intrinsics. * stream.c (stdio_maybe_read_error, stdio_maybe_error): Convert EAGAIN into timeout_error_s. * txr.1: Documented.
* Fix in chk_manage_vec resizing from zero.Kaz Kylheku2016-03-061-1/+1
| | | | | | | * lib.c (bounding_pow_two): Eliminate bogus zero versus non-zero case check in return which causes the function to wrongly map 1 to 0, so that chk_manage_vec is then not able to adjust a vector to size 1.
* Place C standard I/O based streams into subtype.Kaz Kylheku2016-02-261-1/+3
| | | | | | | | | | | | | | | | | | | * lib.c (subtypep): Handle subtype check here between stream and stdio-stream as a special case, since streams aren't structures related by inheritance, but built-ins. (class_check): If the type of obj doesn't match the class exactly, use a subtypep check. We need this because stream functions use this check, and stdio streams are not of the stream type now. * stream.c (stdio_stream_s): New global symbol variable. (make_stdio_stream_common): Use stdio_stream_s symbol for the type of stdio streams. (stream_init): Intern the stdio-stream symbol, and store in stdio_stream_s variable. (streamp): Replace exact check with typep. * stream.h (stdio_stream_s): Declared.
* Relax input type constraint in vec-list.Kaz Kylheku2016-01-181-4/+1
| | | | | | | * lib.c (vec_list): Don't require input to be specifically a list, just any sequence that can be marched with cdr until nil. This commit also fixes the problem that the error message was still referring to the function as vector-list.
* New function, split*.Kaz Kylheku2016-01-171-5/+46
| | | | | | | | | | | | | | | | | | * eval.c (eval_init): Register split*. * lib.c (split_star_func): New static function. (partition_split_common): Take pointer-to-function argument instead of boolean. Hoist this C function into the lazy cons. (partition): Pass pointer to partition_func ito partition_split_common, intsead of a flag requesting the use of partition_func. (split): Pass apointer to split_func into partition_split_common. (split_star): New function. * lib.h (split_star): Declared. * txr.1: Documented split*.
* partition* bugfix: ignore negative indices consistently.Kaz Kylheku2016-01-171-9/+14
| | | | | * lib.c (partition_star): Eliminate strange behaviors when a negative index is given as an argument.
* partition* bugfix: incorrect behavior for 0.Kaz Kylheku2016-01-171-1/+1
| | | | | | | lib.c (partition_star): In the special case that the indices argument is given as the integer zero, the function wrongly returned just the remaining piece of the sequence after zero, rather than a list containing this piece.
* Apply partition/split bugfix to partition*.Kaz Kylheku2016-01-171-4/+4
| | | | | | * lib.c (partition_star): The same issues that were addressed in TXR 126 in 2015-11-29 commits and 2015-11-24 commits must also be addressed here.
* Bugfix: *print-flo-format* used for integer output.Kaz Kylheku2016-01-141-0/+2
| | | | | * lib.c (obj_print_impl): Handle NUM and BGNUM separately from FLNUM, thorugh a hard-coded "~s" format.
* Improve n-ary minus like newly added division.Kaz Kylheku2016-01-141-3/+13
| | | | | | * lib.c (minusv): Avoid reduce_left overhead in common two-argument case, and just call binary minus function directly.
* Optimization in n-ary numeric functions.Kaz Kylheku2016-01-141-5/+12
| | | | | * lib.c (nary_op): Avoid the overhead of reduce_left in the two-argument case and just call the binary function.
* Fix omission: the / function becomes n-ary.Kaz Kylheku2016-01-131-0/+17
| | | | | | | | | | * eval.c (eval_init): Register / function to divv instead of divi. * lib.c (divv): New function. * lib.h (divv): Declared. * txr.1: Documented.
* Fix broken print of semicolon after ctrl char.Kaz Kylheku2016-01-121-1/+1
| | | | | | | | * lib.c (out_str_char): If semi-flag is true, we must issue a semicolon not only when the next character is a hex digit, but also when it is a semicolon, so that "\x1F;;" isn't rendered as "\x1F;" where the semicolon character has disappeared.
* Print control chars in regexes using \x.Kaz Kylheku2016-01-121-1/+1
| | | | | | | | | | | | | | | | | | | | * lib.c (out_str_char): Static function becomes extern. * lib.h (out_str_char): Declared. * regex.c (puts_clear_flag, putc_clear_flag): New static functions. (print_class_char): Take semicolon flag argument. Use out_str_char to render characters not escaped locally. Clear the semicolon flag. (paren_print_rec): Take semicolon flag argument, and pass it down. Clear it when printing parentheses. (print_rec): Take semicolon flag argument, and pass down to lower level functions. Use putc_clear_flag and puts_clear_flag instead of put_string and put_char. Use out_str_char for char object not esaped locally. (regex_print): define semi_flag and pass it down to print_rec.
* Pretty-print DEL and U+DCxx str char using \xHEX.Kaz Kylheku2016-01-121-1/+1
| | | | | | * lib.c (out_str_char): Don't print all characters above space as themselves. Treat 7F and U+DCxx as control chars to be printed using hex escape.
* Remove redundant assert in mem allocation functions.Kaz Kylheku2016-01-101-3/+0
| | | | | | * lib.c (chk_strdup, chk_strdup_utf8, chk_copy_obj): No need to assert !async_sig_enabled since the lower level chk_malloc or chk_malloc does it.
* Reduce scope of variable in cat_str.Kaz Kylheku2016-01-101-2/+2
| | | | | * lib.c (cat_str): Unnecessarily scoped len variable moves to inner scope.
* New internal function scat: variable arg cat_str.Kaz Kylheku2016-01-101-2/+82
| | | | | | | | | | | | | | Avoids consing up list of strings. * lib.c (vscat): New static function. (scat): New function. (lazy_str): Use scat instead of cat_str. * lib.h (scat): Declared. * eval.c (format_field): Use scat instead of cat_str. * parser.c (open_txr_file, read_eval_stream): Likewise.
* GC-correct order in copy_lazy_str.Kaz Kylheku2016-01-101-2/+2
| | | | | | * lib.c (copy_lazy_str): Eliminate pointless initialization of ls.list and to nil. Make ls.prefix initialization the last step since it's the only operation which conses.
* Rework lazy string optimization done in TXR 118.Kaz Kylheku2016-01-091-26/+10
| | | | | | | | | | | | | This is also a bugfix. Padding up the index to be at least 1024 characters longer than the existing prefix was dumb and wrong; it changes the semantics of code which restores the list from the lazy string, like the @(freeform) directive. How much of the string is forced is visible to the caller! * lib.c (lazy_str_force, lazy_str_force_upto): Don't collect pieces from the lazy list and then catenate them in one pass. Instead, use the existing function string_extend, which grows the string exponentially.
* Use struct instead of cons for lazy string fields.Kaz Kylheku2016-01-091-17/+24
| | | | | | | | | | | | | | | | * gc.c (finalize): Must free the dynamic structure attached to the LSTR type now. (mark_obj): Must mark interior of LSTR type's props structure. * lib.c (lazy_sub_str, copy_lazy_str): Copy props structure. (lazy_str): Allocate and initialize props structure. (lazy_str_force, lazy_str_put, lazy_str_force_upto, lazy_str_get_trailing_list, out_lazy_str): Follow representation change. * lib.h (struct lazy_string_props): New struct type. (strut lazy_string): Member opts replaced with props pointer to struct lazy_string_props.
* New internal function for copying malloced object.Kaz Kylheku2016-01-091-0/+8
| | | | | | * lib.c (chk_copy_obj): New function. * lib.h (chk_copy_obj): Declared.
* Revert chr-isdigit/isxdigit, provide new functions.Kaz Kylheku2016-01-041-0/+10
| | | | | | | | | | | | | | | | | | | It was a mistake to change the semantics of the return value of chr-isdigit and chr-isdigit. It breaks code like [partition-by chr-isdigit ...]. The behavior of chr-isdigit and chr-isxdigit is restored to returning t and nil. New chr-digit and chr-xdigit functions are introduced for returning the digit value or nil. * eval.c (eval_init): Register chr-digit and chr-xdigit intrinsics. * lib.c (chr_isdigit, chr_isxdigit): Restore old behavior. (chr_digit, chr_xdigit): New functions. * lib.h (chr_digit, chr_xdigit): Declared. * txr.1: Everything documented.
* Copyright year bump.Kaz Kylheku2015-12-311-1/+1
| | | | | | | | | | | | | | | | | | | | | | | * LICENSE, METALICENSE, Makefile, args.c, args.h, arith.c, arith.h, cadr.c, cadr.h, combi.c, combi.h, configure, debug.c, debug.h, eval.c, eval.h, filter.c, filter.h, gc.c, gc.h, glob.c, glob.h, hash.c, hash.h, jmp.S, lib.c, lib.h, lisplib.c, lisplib.h, match.c, match.h, parser.c, parser.h, parser.l, parser.y, rand.c, rand.h, regex.c, regex.h, share/txr/stdlib/cadr.tl, share/txr/stdlib/except.tl, share/txr/stdlib/hash.tl, share/txr/stdlib/ifa.tl, share/txr/stdlib/path-test.tl, share/txr/stdlib/place.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/txr-case.tl, share/txr/stdlib/type.tl, share/txr/stdlib/with-resources.tl, share/txr/stdlib/with-stream.tl, share/txr/stdlib/yield.tl, signal.c, signal.h, stream.c, stream.h, struct.c, struct.h, sysif.c, sysif.h, syslog.c, syslog.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h: Add 2016 copyright. * linenoise/LICENSE, linenoise/linenoise.c, linenoise/linenoise.h: Bump one principal author's copyright from 2014 to 2015. The code is based on a snapshot of 2015 upstream work.
* C++ breakage: multiple definition of name_s.Kaz Kylheku2015-12-301-1/+2
| | | | | | | | | | | | | * lib.c (name_s): Defined here now. (obj_init): name_s initialized here. * lib.h (name_s): Declared. * match.c (name_s): Definition removed. (syms_init): Initialization of name_s removed. * sysif.c (name_s): Definition removed. (sysif_init): Initialization of name_s removed.
* New variables for floating-point printing control.Kaz Kylheku2015-12-281-1/+4
| | | | | | | | | | | | | | | | | * lib.c (obj_print_impl): Instead of hard-coded "~s", obtain the format string for floats from the *print-flo-format* special variable, whose default value is "~s". * stream.c (print_flo_digits_s, print_flo_format_s): New symbol variables. (formatv): Use *print-flo-digits* value for default precision for ~f and ~e, rather than hard-coded 3. (stream_init): Initialize print_flo_digits_s and print_flo_format_s, and register special variables under those symbols. * stream.h (print_flo_digits_s, print_flo_format_s): Declared. * txr.1: Documented new specials.
* Whitespace in lazy_str_force.Kaz Kylheku2015-12-281-1/+1
| | | | * lib.c (lazy_str_force): Remove trailing whitespace.
* Overhaul printing of lazy strings.Kaz Kylheku2015-12-281-30/+91
| | | | | | | | | | | | | | | | | | The #<lazy-string ...> print syntax is gone. Lazy strings are now printed by traversing their structure, without forcing them to the flat representation. * lib.c (lazy_str_put): New function. (out_str_char, out_str_pretty, out_lazy_str): New static functions. (obj_print_impl): Use out_str_pretty for standard-printing regular strings. Use lazy_put_str for pretty-printing lazy strings, and out_lazy_str for standard-printing them. * lib.h (lazy_str_put): Declared. * stream.c (put_string): Check for a lazy string and route to lazy_str_put, so the string doesn't get forced (though of course the underlying list does, if it is lazy).
* Fix exception in @(freeform) over interactive input.Kaz Kylheku2015-12-271-1/+6
| | | | | | | | | | | This is caused by the ("str0" "str1" .. "strn" nil) representation put out by the interactive style lazy list. The lazy string force function doesn't like the nil. Quick and dirty fix applied here. * lib.c (lazy_str_force, lazy_str_force_upto): If a nil comes out of lstr->ls.list, break out of the loop as if the list ended.
* chr-isdigit and chr-isxdigit return value.Kaz Kylheku2015-12-231-2/+13
| | | | | | | | * lib.c (chr_isdigit, chr_isxdigit): Return the integer value rather than the symbol t, which can be exploited to write more compact scanning code. * txr.1: Documented.
* Print some instances of qref as dot notation.Kaz Kylheku2015-12-181-0/+24
| | | | | | | | | We must preserve ambiguity-free read-print consistency. * lib.c (simple_qref_args_p): New static function. (obj_print_impl): Check for qref and print as dot notation if its argument list satisfies simple_qref_args_p.
* Useful feature: object post-initialization.Kaz Kylheku2015-12-161-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Structs can now have code which executes after an object is initialized, which is useful for doing work like registering objects in global lists and whatever, when those actions need access to the initialized slots of the object. * share/txr/stdlib/struct.tl (defstruct): Handle :posinit syntax, by generating lambda as eighth argument of sys:make-struct call. * struct.c (struct struct_type): New member, postinitfun. (struct_init): Adjust registrations of make_struct_type to account for new parameter. The user visible make-struct-type is registered as having one optional argument, for backward compat. (make_struct_type): New argument, postinitfun. Store this in the structure. For backward compatibility, the argument is defaulted. (struct_type_mark): Mark the new postinitfun member. (call_postinitfun_chain): New static function. (make_struct, lazy_struct_init): Call call_postinitfun_chain after slots are initialized, and after the boa function is called. * struct.h (make_struct_type): Declaration updated. * lib.c (time_init): Pass eighth argument to make_struct type. * sysif.c (sysif_init): Likewise. * unwind.c (uw_late_init): Likewise. * tests/012/struct.tl: Update defstruct expansion test case. * txr.1: Document new argument of make-struct-type, and clarify ordering of initfun with regard to other actions. Likewise, document :postinit, and clarify ordering of :init actions with regard to other actions.
* Support eight-argument intrinsic functions.Kaz Kylheku2015-12-161-0/+46
| | | | | | | | | | * lib.h (enum functype): New member, N8. (struct func): New members n8 and n8v in embedded f struct. (func_n8, func_n8v, func_n70, func_n8o): Declared. * lib.c (equal): Handle N8 in switch. (func_n8, func_n8v, func_n70, func_n8o): New functions. (generic_funcall): Handle N8 in two switches.