summaryrefslogtreecommitdiffstats
path: root/lib.c
Commit message (Collapse)AuthorAgeFilesLines
* Tidy up some vector object initializations.Kaz Kylheku2016-06-171-7/+7
| | | | | | | * lib.c (vector, copy_vec, sub_vec, cat_vec): Set the type field of the object sooner. Do not malloc between obtaining the object, and initializing it, because that could throw, leaving an uninitialized object in the heap space.
* New --free-all option for freeing memory on exit.Kaz Kylheku2016-06-071-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Although we are garbage-collected, being able to clean up on shutdown is nevertheless useful for uncovering leaks. Leaks can occur, for instance, due to neglect to free out-of-heap satellite data from objects that are reclaimed by gc. This feature is long overdue. * arith.c, arith.h (arith_free_all): New function. * gc.c, gc.h (gc_free_all): New function. * lib.c (init): Remove program name parameter and redundant initialization of progname globl variable. * lib.h (progname): Superfluous declaration removed. This is already declared in txr.h. (init): Declaration updated. * regex.c (char_set_destroy): Do not check the static allocation flag here; just destroy the object. Do check for a null pointer, though. (char_set_cobj_destroy): This cobj destructor now checks the static flag of the char set object and avoids freeing it. Thus our char set singletons are left alone by gc, but our global freeing function takes care of them. (wide_cs): New static variable moved out of wide_display_char_p to static scope. (regex_free_all): New function. * regex.h (regex_free_all): Declared. * txr.c (progname): const qualifier and initializer removed. (main): Ensure progname is always dynamically allocated, even in the argv[0] == 0 case. Do not pass progname to init; it doesn't take that argument any more. (free_all): New static function. (txr_main): Implement --free-all option. * txr.h (progname): Declaration updated.
* Handle sequence structs in length.Kaz Kylheku2016-06-061-0/+2
| | | | | * lib.c (length): In COBJ case, handle structures which have a car slot.
* Handle sequence structs in toseq.Kaz Kylheku2016-06-061-0/+4
| | | | | | * lib.c (toseq): Handle OBJP case, and implement behavior for struct objects that have a car method.
* Handle sequence better in index list case of replace.Kaz Kylheku2016-06-061-14/+9
| | | | | | | | | * lib.c (replace_list): Simplify treatment of items on entry using toseq. (replace_str, replace_vec): When a list or vector of indices is given, use the itseq result of passing items items through toseq function rather than original items, for consistency with range assignment.
* Handle structs in list collection operations.Kaz Kylheku2016-06-061-0/+10
| | | | | | | | | | | | | | | This eliminates the error which occurs when a sequence implemented as a struct is nconced or appended onto. The struct is converted to a list, and so is the object on the right hand side. As a result, more generic sequence code works on structs. * lib.c (list_collect_nconc, list_collect_append): Handle tail of type COBJ by converting to a list, storing that list back in the tail and then destructively tacking onto that the obj operand, also turned into a list.
* Use local var in list_collect_{nconc,append}.Kaz Kylheku2016-06-061-10/+12
| | | | | | | * lib.c (list_collect_nconc, list_collect_append): Capture deref(ptail) subexpression in local var and refer to that in all code where ptail isn't modified from the original value.
* Fix useless tolist over COBJ objects.Kaz Kylheku2016-06-061-1/+1
| | | | | | * lib.c (tolist): Use mapcar_listout, to avoid the conversion of the resulting list to the object's type, making this whole operation useless!
* Fix error messages in internal function scat.Kaz Kylheku2016-06-051-2/+2
| | | | | * lib.c (vscat): Replace "cat-str" and "vcat" with correct name "scat".
* Adding special function from-list.Kaz Kylheku2016-06-011-1/+9
| | | | | | | | | | * lib.c (from_list_s): New symbol variable. (make_like): Handle a COBJ. If it's a structure with a from-list method, then use it, otherwise the default handling applies of returning the list. (obj_init): Initialize from_list_s. * txr.1: Documented.
* ldiff uses equal for all non-list arguments.Kaz Kylheku2016-06-011-6/+5
| | | | | | | | | | | * lib.c (ldiff): Rather than checking specifically for strings and vectors to apply the special case behavior (comparison using equal), test specifically for lists and apply the traditional behavior. Every other object, not just strings and vectors, gets the altered behavior. * txr.1: Changed text in TXR Lisp introduction which touches on ldiff, and wording fix under ldiff.
* Methods for turning objects into sequences.Kaz Kylheku2016-06-011-2/+20
| | | | | | | | | | | | | | | | | | | | | | | | Struct objects can now define methods car, cdr and nullify. With these, they can participate in operations on sequences. * eval.h (car_s, cdr_s): Declared. * lib.c (nullify_s): New symbol variable. (car, cdr): Implement for struct objects via, respectively, their car and cdr methods. (tolist): Handle objects by mapping through identity. (nullify): Implement for objects optionally: if an object is a struct with a nullify method, use it, otherwise go through default case of just returning the object. (empty): Implement for objects that have nullify method. (obj_init): Initialize nullify_s. * struct.c (maybe_slot): New function. * struct.h (maybe_slot): Declared. * txr.1: Documented car, cdr and nullify method convention.
* Remove superfluous arg defaulting in drop functions.Kaz Kylheku2016-05-261-4/+2
| | | | | | * lib.c (drop_while, drop_until): No need to do default_arg on keyfun, since it is just passed to pos_if, which takes care of it.
* Bugfix: argument defaulting in take-until.Kaz Kylheku2016-05-261-2/+2
| | | | | | * lib.c (take_until): keyfun not defaulted in list cases. Needless defaulting of key in vector/string cases, since pos function already defaults.
* 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.