summaryrefslogtreecommitdiffstats
path: root/lib.c
Commit message (Collapse)AuthorAgeFilesLines
* Let some sequence functions work on structs.Kaz Kylheku2016-10-131-28/+32
| | | | | | * lib.c (in, sub, ref, search, rsearch, sel): These functions now accept struct objects that have the nullify, car and cdr methods.
* find-max and find-min support hashes.Kaz Kylheku2016-10-121-13/+55
| | | | | | | * lib.c (find_max): Restructured to implement separately for vectors and lists. Support hash tables. * txr.1: Document find-min and find-max for hashes.
* Support gmtoff and zone in time struct.Kaz Kylheku2016-10-121-2/+18
| | | | | | | | | | | | | | * lib.c (gmtoff_s, zone_s): New symbol variables. (tm_to_time_struct): Copy tm_gmtoff and tm_zone into Lisp struct from struct tm, if the platform has these. (time_fields_to_tm): Zero/null-out the tm_gmtoff and tm_zone fields of the target structure, if the platform has them. (time_init): Intern the gmtoff and zone symbols, initializing the gmtoff_s and zone_s variables. Add the gmtoff and zone slots to the time struct. * txr.1: Documented new slots.
* length and empty functions support ranges.Kaz Kylheku2016-10-041-0/+4
| | | | | | * lib.c (length, empty): Handle RNG in switch. * txr.1: Documented.
* Synchronize license comments with LICENSE.Kaz Kylheku2016-10-011-16/+17
| | | | | | | | | | | | | | | | | | | | * 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, ftw.c, ftw.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/awk.tl, share/txr/stdlib/build.tl, share/txr/stdlib/cadr.tl, share/txr/stdlib/conv.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/socket.tl, share/txr/stdlib/struct.tl, share/txr/stdlib/termios.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, socket.c, socket.h, stream.c, stream.h, struct.c, struct.h, sysif.c, sysif.h, syslog.c, syslog.h, termios.c, termios.h, txr.1, txr.c, txr.h, unwind.c, unwind.h, utf8.c, utf8.h: Revert to verbatim 2-Clause BSD.
* Bugfix: out-of-range negative indices into lists.Kaz Kylheku2016-09-231-2/+5
| | | | | | | | | | | | | | | | | | We have a problem not handling negative list indices which index beyond the beginning of the list. Such accesses are just storing and retrieving the first element! * lib.c (listref): If a negative index is still negative after the length of the list is added to it, then return nil. Do not return car(list)! (listref_l): Similary, do not return car_l(list) if the index is less than zero, so that an error occurs for an out-of-range negative index. * txr.1: Update the documentation for ref to describe these indexing constraints, and also to include hashes.
* Switch regex type from sys:regex to regex.Kaz Kylheku2016-09-231-1/+1
| | | | | | | | | | | | | | | | | The sys:regex internal symbol was historically used when derivative-based regexes had a S-exp representation headed by that symbol. It had to be a private symbol. Now the regex symbol is only used as the COBJ class type for regexes; it makes no sense for it to be in the sys package. We want user code to be able to refer to this type using a public symbol. * lib.c (obj_init): Intern the regex symbol stored in regex_s in user_package. * txr.1: Include regex in the type graph in the Object Type section, and in the type list under the typeof function.
* Regexes now callable as functions.Kaz Kylheku2016-09-221-0/+16
| | | | | | | | * lib.c (generic_funcall): Add case for regexes. Handle arguments in such a way that the string is always rightmost, with a view to convenient partial application. * txr.1: Documented in multiple places.
* Bugfix in tok-str: empty-match regexes.Kaz Kylheku2016-09-161-10/+7
| | | | | | | | | | | | | | * lib.c (tok_str): Only continue the loop if the new position isn't past the end of the string. This fixes the problem of recognizing an empty token past the last character in the string. Also, advance new_pos by one if there is a zero length match. Then don't advance pos by one later in that case. This fixes the bug that we collect empty separator pieces *and* empty tokens, and also prevents empty matches before the first character of the string. Logic in tok_str is now very similar to that in split_str_keep.
* Bugfix in split-str: empty-match regexes.Kaz Kylheku2016-09-161-4/+5
| | | | | | | | | | | * lib.c (split_str_keep): In the regex case, changing to an infinite loop. The do/while is no longer needed because the if statement includes a test of the position having reached the end of the string. This is done before it is incremented by len, so we avoid wrongly keeping a separator. * txr.1: Clarified that an empty regex match behaves like an sep which is an empty string,
* split-str gains ability to keep separating pieces.Kaz Kylheku2016-09-161-1/+14
| | | | | | | | | | | | | | | * eval.c (eval_init): Register split-str to split_str_keep, with optional argument. * lib.c (split_str_keep): New function, formed from split_str, with third argument. (split_str): Reduced to wrapper around split_str_keep. Thus we don't have to update umpteen existing calls with an extra nil parameter. * lib.h (split_str_keep): Declared. * txr.1: Documented new optional argument of split-str.
* key function argument on remq, remql and remqual.Kaz Kylheku2016-09-151-6/+20
| | | | | | | | | | | | | | | | | * eval.c (weave_while): Pass third arg to remq as nil. (eval_init): Update registrations of remq, remql and remqual. * lib.c (remq, remql, remqual): Implement key function argument. * lib.h (remq, remql, remqual): Declarations updated. * sysif.c (at_exit_call): Pass third arg to remq as nil. * debug.c (debug): Pass third argument to remqual as nil. * txr.1: Documentation updated.
* New keepq, keepql and keepqual functions.Kaz Kylheku2016-09-151-0/+61
| | | | | | | | | * eval.c (eval_init): Register keepq, keepql and keepqual intrinsic functions. * lib.c (keepq, keepql, keepqual): New functions. * lib.h (keepq, keepql, keepqual): Declared.
* Fix broken keep/remove functions.Kaz Kylheku2016-09-141-19/+10
| | | | | | | | * lib.c (remq, remql, remqual, remove_if, keep_if): Bug: functions don't work correctly for strings. Anyway, the ldiff-based algorithm is ugly on vectors and strings. Patching things upp by converting input to list with tolist.
* Semantics change in trim-str function.Kaz Kylheku2016-09-111-4/+12
| | | | | | | | | * lib.c (trim_str): Trim only newlines and blanks, not carriage returns, vertical tabs and form feeds. This is subject to the compatibility option * txr.1: Doc updated and compatibility note added.
* Adding time-parse method.Kaz Kylheku2016-08-251-17/+66
| | | | | | | | | | | | | | | | | | | | * lib.c (time_parse_s): New symbol variable. (tm_to_time_struct): New static function, based on code moved out of broken_time_struct. (broken_time_struct): Bulk of code replaced with call to tm_to_time_struct. (time_struct_to_tm): New parameter, strict. Indicates whether nils in time struct are converted to zeros, or whether they trigger errors. (time_string_meth): Specify strict conversion of argument time struct to to struct tm when calling time_struct_to_tm. (time_parse_meth): New static function. (time_init): Initialize time_parse_s symbol variable; add time-parse static slot to time struct; initialize slot with new method. * txr.1: Document time-parse method.
* New time-string method on time struct.Kaz Kylheku2016-08-221-16/+52
| | | | | | | | | | | | | * lib.c (time_string_s): New symbol variable. (time_fields_to_tm, time_struct_to_tm): New static functions. (make_time_impl): Replace bunch of code with call to time_fields_to_tm. (time_string_meth): New static function. (time_init): Initialize time_string_s. Add time-string static slot to time struct. Register time-string method. * txr.1: Documented.
* Adding time-parse function: wrapper for strptime.Kaz Kylheku2016-08-221-0/+18
| | | | | | | | | | | | * configure: Test for strptime. * eval.c (eval_init): register time-parse intrinsic. * lib.c (time_parse): New function. * lib.h (time_parse): Declared. * txr.1: Documented.
* Print notation for quasi-strings and quasi-word-lists.Kaz Kylheku2016-07-201-0/+64
| | | | | | | * lib.c (out_quasi_str): New static function. (obj_print_impl): Handle sys:quasi and sys:quasilist forms, reproducing a syntax similar to their read notation.
* Replace 0 constant with nil.Kaz Kylheku2016-07-191-1/+1
| | | | | * lib.c (split_str): Fourth argument of search_regex is Lisp boolean, so nil should be used to specify false.
* 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.