Compare commits

...

451 Commits

Author SHA1 Message Date
ridiculousfish
b187538339 Bump OS X installer package version 2012-06-05 02:06:44 -07:00
ridiculousfish
93f6aabe12 Fix to stop identifying SnowLeopard's terminal as term256 2012-06-05 02:06:33 -07:00
ridiculousfish
10aa107380 Updated relnotes 2012-06-05 01:22:25 -07:00
ridiculousfish
53cba2a2e6 Improved webconfig.py's handling of combined term256 and classic colors 2012-06-05 01:19:59 -07:00
ridiculousfish
d871095d0b Updated "last run version" to __fish_init_1_50_0. Updated colors for non term-256 compatibility 2012-06-05 01:01:30 -07:00
ridiculousfish
9bf8a5e877 Updated release notes for new history syntax 2012-06-05 00:42:45 -07:00
ridiculousfish
3836bfe5a1 Added history completion file
Updated history function to assume --search as the default behavior
2012-06-05 00:40:42 -07:00
ridiculousfish
7698553c3a Made a bare history call end with a newline 2012-06-04 23:59:04 -07:00
ridiculousfish
be1d216b34 Relnoted history builtin
Tweaked validation regex to use extended regexs
2012-06-04 23:54:43 -07:00
ridiculousfish
85cbb943b5 Tweaks to the history function 2012-06-04 23:40:08 -07:00
Siteshwar Vashisht
ec34f2527a Adding history builtin 2012-06-05 09:54:42 +05:30
ridiculousfish
b877181e17 Fix for what I believe to be a bug in fish trunk where you can't always successfully control-C out of some while loops 2012-06-04 15:10:35 -07:00
ridiculousfish
69446be1ee Signal handling cleanup and improved safety
Fixes issue where you couldn't control-C out of a loop (https://github.com/ridiculousfish/fishfish/issues/13)
Also stops doing memory allocation in the signal handler (oops) https://github.com/ridiculousfish/fishfish/issues/27
2012-06-04 14:20:01 -07:00
ridiculousfish
cc90f9cf80 path_make_canonical mishandles empty string.
Fixes https://github.com/ridiculousfish/fishfish/issues/26
2012-06-04 12:00:59 -07:00
ridiculousfish
457e6a5f46 Updated some relnotes for beta r2 2012-06-03 17:58:25 -07:00
ridiculousfish
e7db18bae2 Added Debian description-pak file 2012-06-02 18:07:11 -07:00
ridiculousfish
f8e3e853aa Fix to disable VDSUSP, which allows control-Y to serve as yank on OS X 2012-06-02 15:43:18 -07:00
ridiculousfish
b7ba252965 Restore implicit cd for paths starting with ., .., or ~ 2012-06-02 14:04:25 -07:00
ridiculousfish
ae12e1b537 insert_tabs called wcstring::append with arguments in the wrong order
Should fix https://github.com/ridiculousfish/fishfish/issues/18
2012-06-01 14:38:41 -07:00
ridiculousfish
9255f625fa Disable internalized scripts 2012-06-01 14:00:04 -07:00
adisbladis
377d93d1cf Missing -s to which fixed 2012-05-30 21:08:44 +02:00
adisbladis
19d14c7c3a Python 3 compatibility fix in make_completions.py 2012-05-30 11:52:49 +02:00
adisbladis
6e3a823b7b Python 3 compatibility fix in internalize_scripts.py 2012-05-30 11:42:32 +02:00
ridiculousfish
51f9ff5389 Updated make_tarball.sh to stop dumping files in the extract directory 2012-05-30 01:06:25 -07:00
ridiculousfish
4986e6e3f9 Fix to set up 10.6 SDK correctly when building on 10.7 2012-05-30 00:27:50 -07:00
Peter Ammon
9d8ae184c9 Fix for annoying messages when launching on SnowLeopard
Fix for missing .o file in Makefile.in
2012-05-30 00:22:45 -07:00
ridiculousfish
4afdcf56fc Tweak a few default colors
Fix the make_pkg script to actually build an installable package that doesn't immediately barf
2012-05-29 21:14:19 -07:00
ridiculousfish
0fe43eb880 Fix to (really) no longer require Doxygen to build fish 2012-05-29 20:50:27 -07:00
ridiculousfish
4edfd6d9d6 Update set_color documentation 2012-05-25 02:14:26 -07:00
ridiculousfish
8351ec0afd Document some of the new commands 2012-05-25 02:07:21 -07:00
ridiculousfish
92ce84c26a More tweaks to Debian build script 2012-05-25 00:48:39 -07:00
ridiculousfish
9d31e52b2a Updated Debian package build script 2012-05-24 23:58:16 -07:00
ridiculousfish
dcd7d6769d Updated some build scripts 2012-05-24 23:56:07 -07:00
ridiculousfish
87c0e0e971 Tweaked make_deb script 2012-05-22 00:09:39 -07:00
ridiculousfish
6f9a523151 Some tweaks to build tools
Started make_deb.sh for Debian package
2012-05-22 00:05:41 -07:00
ridiculousfish
673faf7152 Fixed builtin_test to properly handle "just a strings" in combining expressions, like 'test foo -a bar'. This was causing error messages in the help function.
Fixed help function to know about OS X and use the open command to open web pages.
2012-05-20 12:58:03 -07:00
ridiculousfish
18ace555b0 Added release notes for beta 1 2012-05-19 17:04:45 -07:00
ridiculousfish
d09c9fba02 Added a -s option to echo to mean "don't output spaces"
Replaced default prompt to stop calling printf. I opened a bug to replace the default prompt entirely with one that's cheaper.
2012-05-19 16:59:56 -07:00
ridiculousfish
f1d22b6300 Got function.h and function.cpp off of shared_ptr (hooray) 2012-05-18 14:00:36 -07:00
ridiculousfish
da6295c428 More work towards getting function.h off of shared_ptr 2012-05-17 19:46:08 -07:00
ridiculousfish
86645b32e1 Work towards getting function.h off of shared_ptr 2012-05-17 19:38:08 -07:00
ridiculousfish
977c585f8b Made the OS X installer package flat 2012-05-17 19:37:26 -07:00
ridiculousfish
4c1ddab560 Tweaked Makefile.in to show off a few of our new features 2012-05-17 19:37:03 -07:00
ridiculousfish
f5442bdd4a Fixed cd autosuggestion for absolute paths
Removed some unnecessary path cleanup that is_potential_path now handles
2012-05-13 20:49:14 -07:00
ridiculousfish
129525af21 Fix for https://github.com/ridiculousfish/fishfish/issues/3
cd's special autosuggestion was broken, now fixed. Also, there's some tests for it now.
2012-05-13 20:19:02 -07:00
ridiculousfish
c15975113a Fix for https://github.com/ridiculousfish/fishfish/issues/2 2012-05-11 18:59:38 -07:00
ridiculousfish
2d3d6e1c17 Added PackageMaker file
Updated make_pkg.sh to use it
2012-05-11 17:26:08 -07:00
ridiculousfish
b0f4ff2879 Added build_tools folder so we can automate release builds 2012-05-10 02:11:28 -07:00
ridiculousfish
070b10689c Added .gitattributes to ignore new build_tools directory from archival 2012-05-10 02:10:39 -07:00
ridiculousfish
39863ce4d7 Fix for longstanding bug where set -e would fail to erase elements from an array.
This was introduced in 7b3377e78c
2012-05-10 01:04:18 -07:00
ridiculousfish
7cae1ae415 Make key_reader build and work 2012-05-09 03:30:09 -07:00
ridiculousfish
eca2100312 Cleanup SHLVL variable 2012-05-09 03:23:31 -07:00
ridiculousfish
175249b455 Adopt wcstring in env_set 2012-05-09 03:06:10 -07:00
ridiculousfish
294fbc8309 Replaced some calls to unescape with unescape_string 2012-05-09 02:55:36 -07:00
ridiculousfish
9bcc7df96f Lots of modernization changed
Removed wcsdupcat
2012-05-09 02:33:42 -07:00
ridiculousfish
4bd63020ca Fix to add a little explanatory text to colors in the web config interface 2012-05-08 17:10:38 -07:00
ridiculousfish
4885842ae8 Fix to do really nice syntax highlighting of cd command where it colors paths that cannot match any directory with error color 2012-05-07 17:52:02 -07:00
ridiculousfish
4df6b599b8 Eliminate the silly autosuggest_parsed_command_t class; make it just a function 2012-05-07 17:43:05 -07:00
ridiculousfish
0c79bb6e7c Factor is_potential_path to properly handle CDPATH
This will let us color cd commands better
2012-05-07 17:31:24 -07:00
ridiculousfish
1a264ab7c2 Made tests compile again
Renamed autosuggest_handle_special to autosuggest_special_validate_from_history
Began work to factor autosuggest_special_validate_from_history together with autosuggest_suggest_special
2012-05-07 12:55:13 -07:00
ridiculousfish
99b51ce6ea Fix to better support CDPATH 2012-05-06 14:53:19 -07:00
ridiculousfish
e0baab2722 Another effort to make the cd completion work without running command substitutions off of the main thread 2012-05-06 13:51:11 -07:00
ridiculousfish
71a8d39372 Fix to restore completions that use command substitution (like cd)
Fix to adopt the same escaping code for both completions and autosuggestions
2012-05-06 13:36:51 -07:00
ridiculousfish
90826e4031 Oops, had the wrong sense for when to call proc_push_interactive 2012-05-05 14:39:08 -07:00
ridiculousfish
0e4f2cca01 Fix to stop setting a default CDPATH 2012-05-05 14:34:59 -07:00
ridiculousfish
ac3dce38f0 Better fix to make cd completions work again 2012-05-05 14:33:24 -07:00
ridiculousfish
3be07cc645 Fix to start re-respecting CDPATH 2012-05-05 14:30:20 -07:00
ridiculousfish
07f5319472 Fix for failing to properly complete cd command 2012-05-05 14:21:21 -07:00
ridiculousfish
47019e315a Some work towards refactoring how completions are inserted to support escaping in autosuggestions 2012-05-05 13:34:09 -07:00
ridiculousfish
fe7fa46d57 Make dirh, nextd, prevd work on OS X 2012-05-04 18:53:38 -07:00
ridiculousfish
007c5bc9f0 Workaround difference between BSD and GNU seq in dirh
dirh still doesn't make any sense
2012-05-04 18:37:24 -07:00
ridiculousfish
8c423b56df Define a virtual destructor for lru.h 2012-05-04 18:37:12 -07:00
ridiculousfish
a868abb55a Minor cleanup of proc.cpp 2012-04-25 13:38:40 -07:00
ridiculousfish
ecab34c787 Make gettext function use echo instead of printf to save a fork 2012-04-25 13:37:41 -07:00
ridiculousfish
307ea6dce2 Removed extra newline from Goodbye output 2012-04-25 13:37:01 -07:00
ridiculousfish
d456b4f68e Removed useless deroff.py import from bash settings 2012-04-25 13:36:21 -07:00
ridiculousfish
17e815348b Fix to drain command output from quit commands before returning from job_continue
This should fix the bug where output from commands and builtins gets incorrectly interleaved.
I think this is a very long-standing bug that predates my work on the shell
2012-04-25 13:26:56 -07:00
ridiculousfish
953ab4b3cf Clean up __fish_config_interactive a little bit 2012-04-24 11:12:04 -07:00
ridiculousfish
9204780c1b Fix for a missing lock in complete_remove 2012-04-24 11:01:04 -07:00
ridiculousfish
fb2ed355ec Improve fork reporting
Save a couple of forks during init
2012-04-24 10:10:43 -07:00
ridiculousfish
675106cfa5 Include algorithm header so we can build on Linux 2012-04-23 19:29:44 -07:00
ridiculousfish
3aeadd9fb7 Fix for a deadlock when env_get_string ends up calling env_get_string 2012-04-23 11:08:29 -07:00
ridiculousfish
0e3eb38f11 Improved fork reporting
Made autosuggestion work properly for tilde expansion
2012-04-21 20:08:44 -07:00
Siteshwar Vashisht
dc23af6b32 Fixed setting PATH variable in import_bash_settings.py 2012-04-22 00:18:40 +05:30
ridiculousfish
b1bfa71338 Merge branch 'master' into CPlusPlus
Conflicts:
	builtin_commandline.cpp
	util.c
2012-04-18 13:41:39 -07:00
ridiculousfish
3c190374b4 Wrote tests for history merging, then made them pass. 2012-04-16 20:26:50 -07:00
ridiculousfish
d8428463d8 Fix to better handle merging histories between separate sessions 2012-04-15 22:49:27 -07:00
ridiculousfish
725982cc5b Fix to try to remove Python 2.7 dependency 2012-04-15 19:45:44 -07:00
ridiculousfish
f977dfcfe7 Deroffer optimizations 2012-04-15 19:22:30 -07:00
ridiculousfish
15079fa3bd Some more deroff optimizations 2012-04-15 04:41:20 -07:00
ridiculousfish
79a92120ef Speed improvements to deroffer 2012-04-15 01:15:10 -07:00
ridiculousfish
a34565ca0f Make screen.cpp properly compute the length of term256 escape codes 2012-04-13 14:40:16 -07:00
ridiculousfish
49ece586d5 Added a fish_update_completions function to run the manpage completion file
Added some pretty progress reporting to that script
Added some comments to deroff.py
2012-04-11 19:05:43 -07:00
ridiculousfish
2ec0778d78 Make create_manpage_completions output to ~/.config/fish/completions by default, and also discover man pages from reading the manpath 2012-04-11 18:26:26 -07:00
ridiculousfish
cccd1cefbe Make the web config page have a title and pass W3C validation 2012-04-11 18:26:10 -07:00
ridiculousfish
8f145fa7fa Fix for complete.cpp to print out completions in-order 2012-04-11 18:25:37 -07:00
ridiculousfish
ceb32f63e7 Deroffing completion work 2012-04-09 20:17:54 -07:00
ridiculousfish
576c12b184 Deroffer work 2012-04-09 20:17:54 -07:00
ridiculousfish
e2c3ca9950 Move completions from linked list to std::set 2012-04-09 20:17:06 -07:00
ridiculousfish
8585e0e9b8 Various improvements on completion tool 2012-04-04 12:43:12 -07:00
ridiculousfish
7394374a15 Work on improving the output of the manpage completion script 2012-04-03 18:38:25 -07:00
ridiculousfish
37defa739b Fix for an extra line at the end of the variable listing
Once again, fix the issue where some color cells aren't clickable
2012-04-01 01:31:38 -07:00
ridiculousfish
51ed3fbc4b More work to try to make webconfig's layout correct under firefox 2012-04-01 00:53:53 -07:00
ridiculousfish
ab78075d06 Default all xterms into term256 2012-04-01 00:53:47 -07:00
ridiculousfish
52070ea577 Fix to properly update colors for non-native term256 support 2012-03-31 17:24:11 -07:00
ridiculousfish
f930303d3e Fix for an issue where you can't select the top row of colors 2012-03-31 15:38:30 -07:00
ridiculousfish
268fb37517 Cleanup proc_had_barrier
Ensure we don't try to do a universal barrier off of the main thread
2012-03-31 15:33:34 -07:00
ridiculousfish
ff17101316 A little better support for non-term-256 colors in web config
Fix for a deadlock when autoloading a function triggers autoloading another function
2012-03-31 15:17:14 -07:00
ridiculousfish
ab536e5199 Fix for incorrectly highlighted parameters 2012-03-31 14:05:14 -07:00
ridiculousfish
c10b3017d6 Improve autosuggesting of cd command 2012-03-30 11:16:24 -07:00
ridiculousfish
0f63e1f988 Some work towards improving manpage completions 2012-03-30 10:00:01 -07:00
ridiculousfish
3bb537c183 Fix for failing to highlight last quote (or quoted string contents) 2012-03-30 09:58:25 -07:00
ridiculousfish
ff23bf6f0c Fix for bug where fish would not autosuggest certain executables 2012-03-27 19:22:33 -07:00
ridiculousfish
00d3e18780 Fix compile error (oops) 2012-03-27 19:10:21 -07:00
ridiculousfish
a62f300753 Make the color picker cell borders appear correctly in Firefox 2012-03-27 19:00:03 -07:00
ridiculousfish
0bc644abf0 Fix lots of bugs related to the static analyzer
Improved how screen.cpp interacts with output_set_writer()
2012-03-26 01:21:10 -07:00
ridiculousfish
31b7d076b7 Remove some dead variables.
Fix screwy output for invalid tilde expansion in expand.cpp
Some cleanup per clang static analyzer
2012-03-25 23:31:03 -07:00
ridiculousfish
a11687fc5c Make the functions builtin have a bit nicer output
Stop autosuggesting things with newlines
Make webconfig a little nicer
2012-03-25 22:41:22 -07:00
ridiculousfish
fa346cec3b Moved some Python scripts into new share/tools/ directory.
Added fish_config function to launch web config
2012-03-25 18:38:33 -07:00
ridiculousfish
0bdf7a598a More work on web_config 2012-03-25 16:00:17 -07:00
ridiculousfish
0c9a1a56c2 Lots of work on web config
Change to make fish immediately show color changes
2012-03-25 16:00:17 -07:00
ridiculousfish
c0655b6b08 Web config 2012-03-25 16:00:17 -07:00
Siteshwar Vashisht
dca9b83ec0 Enclose descriptions inside double quotes, and escape any enclosed double quotes. 2012-03-23 21:34:52 +05:30
Siteshwar Vashisht
7ded90ae6a Add trimmed descriptions with complete command while parsing man pages. 2012-03-22 23:41:36 +05:30
Peter Ammon
a6ab66080e Additional tweak to color order 2012-03-20 11:33:44 -07:00
Siteshwar Vashisht
89e1cad470 Append $argv only to the last command in an alias. 2012-03-20 22:07:34 +05:30
Peter Ammon
e55bc92d1f Tweak the color order 2012-03-20 03:05:00 -07:00
Peter Ammon
d6a59b538f Work on web config color ordering 2012-03-20 03:01:00 -07:00
ridiculousfish
1a87f44325 Re-implement $history variable
Added -L option to set to mean "don't abbreviate"
2012-03-19 11:52:18 -07:00
ridiculousfish
c8bc535f22 Big improvement to web config history speed
Nice dashed table seprators
2012-03-19 11:51:44 -07:00
Peter Ammon
1889db3263 web_config work 2012-03-19 00:03:38 -07:00
Siteshwar Vashisht
73b88f2304 Added method to find a character outside quotes in a string. 2012-03-18 15:14:34 +05:30
Siteshwar Vashisht
21b8e5a5c6 Some additions to make scripted aliases work 2012-03-18 15:00:27 +05:30
ridiculousfish
b1ff14162f More work on web config 2012-03-16 17:21:37 -07:00
Peter Ammon
c6c55823ac More work on web config 2012-03-16 03:05:51 -07:00
ridiculousfish
8b8a970098 Made builtin_test handle parenthesis 2012-03-15 20:40:57 -07:00
Siteshwar Vashisht
ebfcee6e67 Fixed a crash on inputs like ls x>| less, where x doesn't exist in current directory. 2012-03-16 00:34:09 +05:30
Peter Ammon
63b330439e Initial work towards web config UI 2012-03-15 03:43:45 -07:00
ridiculousfish
52d8fb301c Added term256 support to set_color tool 2012-03-13 14:23:10 -07:00
Siteshwar Vashisht
dfe159e719 Fixed a crash on Linux caused while inserting \ in quotes. 2012-03-13 05:39:17 +05:30
Siteshwar Vashisht
4e7e45a431 Adding scripts to import aliases, environment variables and prompt from bash. 2012-03-11 22:46:58 +05:30
ridiculousfish
d81bd296fc Tweak rgb_color_t back down to 4 bytes 2012-03-10 16:15:56 -08:00
ridiculousfish
6d3f257439 Shortened the "no matches found" error message 2012-03-10 13:44:35 -08:00
ridiculousfish
f92b24221a Made pwd a builtin
Fixed a thread error when autosuggesting with a command substitution
2012-03-09 20:16:26 -08:00
ridiculousfish
afd78f3f0b Allow g_log_forks to be set by fish_log_forks env var 2012-03-09 11:56:33 -08:00
ridiculousfish
44f3f51e0d Attempt to not fork just to output the result of simple builtins. I'm unsure of why this was necessary, if it ever was. 2012-03-09 11:49:29 -08:00
ridiculousfish
14a35e7c21 Flip the switch to stop draining threads before exec (!) 2012-03-09 11:28:24 -08:00
ridiculousfish
d173bb6e0a A bunch of changes working towards eliminating all memory allocation after fork() 2012-03-08 23:21:07 -08:00
ridiculousfish
ce859c9e92 Added some missing calls to setup_fork_guards() in utilities.
Made echo a builtin
2012-03-07 11:35:22 -08:00
ridiculousfish
8ed20f3c28 Removed pwd.fish, which made the home directory print out with ~ (annoying) 2012-03-07 10:53:08 -08:00
ridiculousfish
0ced7d8e56 Improve error reporting for builtin_test 2012-03-07 01:16:50 -08:00
ridiculousfish
143ead8d00 Fix builtin_test to properly handle last expression in a combining expression 2012-03-07 01:13:24 -08:00
ridiculousfish
6788c2710f Improved error reporting for builtin_test 2012-03-07 01:02:46 -08:00
ridiculousfish
ed7c10f366 Implemented test as a builtin (!) 2012-03-07 00:54:01 -08:00
ridiculousfish
68b93c624f Move special handling of DISPLAY environment variable from etc/config.fish to fish itself to reduce number of fork calls made at launch 2012-03-06 15:52:16 -08:00
ridiculousfish
36fe1e4a46 Some initial work towards cutting down the number of fork calls we do 2012-03-06 15:12:37 -08:00
ridiculousfish
9ee4e4e05c Tweak fork guards to use getpid() instead of pthread_atfork 2012-03-06 14:34:18 -08:00
ridiculousfish
ea65a0c519 Updated a comment 2012-03-06 14:32:00 -08:00
ridiculousfish
f35d2629d4 Notice when fish_term256 changes and react to it 2012-03-05 14:18:16 -08:00
ridiculousfish
0e5578204e Additional work towards "forcing" term256 mode on supported terms 2012-03-05 13:39:01 -08:00
ridiculousfish
063a465227 Begin to rework term256 support 2012-03-05 10:44:08 -08:00
ridiculousfish
230fb921ec We had identical format_val and to_string; standardize on to_string 2012-03-05 10:18:42 -08:00
ridiculousfish
82a93d8406 Fix for completions only printing one item 2012-03-05 10:03:26 -08:00
ridiculousfish
2dda8634e9 Fix to stop autosuggesting on whitespace 2012-03-05 09:34:54 -08:00
Siteshwar Vashisht
2f313b799a Made fish_tests.cpp compile on Linux. 2012-03-05 21:57:31 +05:30
ridiculousfish
d371256913 Fix for crash on "less test <tab>" 2012-03-04 18:11:21 -08:00
Siteshwar Vashisht
a7ec93b287 Fixed compilation on Linux. 2012-03-04 16:57:41 +05:30
ridiculousfish
2c5001a5b3 Removed buffer_t 2012-03-04 02:45:51 -08:00
ridiculousfish
191221eec5 Final cleanup of io_data_t now that I recognize the bizarre way in which it shares output buffers (not sure how this ever worked before) 2012-03-04 02:35:30 -08:00
ridiculousfish
27f374a38b Some initial work on removing buffer_t from io_data_t 2012-03-03 23:56:35 -08:00
ridiculousfish
baa813c46f Fix for history being saved too often 2012-03-03 23:10:15 -08:00
ridiculousfish
79a22b1f8d Replace some string_buffer_t 2012-03-03 23:01:42 -08:00
ridiculousfish
087940ec9e Fix a bug where wfopen would always fail
Removed some buffer_t
2012-03-03 22:48:21 -08:00
ridiculousfish
8a46931e34 Remove string_buffer_t (!) 2012-03-03 22:08:34 -08:00
ridiculousfish
a9313fc0c3 Cleanup reader.cpp's usage of string_buffer, remove some other unused data structures 2012-03-03 21:59:45 -08:00
ridiculousfish
dfba35aee8 Clean up the kill ring 2012-03-03 21:46:06 -08:00
ridiculousfish
5ae77b4317 For for crash when typing backslash c 2012-03-03 20:38:16 -08:00
ridiculousfish
a0bb2cdc6e removed some string_buffer 2012-03-03 19:37:55 -08:00
ridiculousfish
0a5680c3e8 Rewrite vformat_string to not use string_buffer 2012-03-03 19:12:06 -08:00
ridiculousfish
00764406d7 Fix to preserve errno across certain calls to stop printing EOVERFLOW messages in weird places 2012-03-03 15:28:16 -08:00
ridiculousfish
3ead99b088 Put fish on a diet. Tracked down the biggest memory hogs and fixed them. Total allocations down by a factor of 3 or so, live allocations a few KB. 2012-03-03 15:22:03 -08:00
Siteshwar Vashisht
8c0803e3c5 Setting cdpath to current directory in path.cpp. 2012-03-04 01:28:09 +05:30
ridiculousfish
2da8df6202 Rearrange wopen and stat to avoid a race and hopefully improve performance a little bit. 2012-03-02 11:12:08 -08:00
ridiculousfish
17a75a5aa5 Change to detect and use O_CLOEXEC 2012-03-02 09:58:29 -08:00
Peter Ammon
8b26d0104c Some initial changes to use CLO_EXEC, with an eye towards some day using it correctly. 2012-03-02 00:27:40 -08:00
ridiculousfish
36622c3578 Fix to properly handle case insensitive autosuggestions 2012-03-01 17:31:45 -08:00
ridiculousfish
fd4df6f9bb Changed iterators from iter++ to ++iter
Large set of changes to history to ensure that histories from other sessions don't "bleed" into the current session
2012-03-01 14:56:34 -08:00
ridiculousfish
5d18d60e3b Squash some leaks 2012-02-29 18:09:20 -08:00
ridiculousfish
e4d98597c7 Added some fork tests, fixed some bugs it found 2012-02-29 17:55:50 -08:00
ridiculousfish
72da14e414 Fix a leak 2012-02-29 17:55:28 -08:00
ridiculousfish
8ada404c5f More work towards improving relationship between multithreading and fork 2012-02-29 16:14:51 -08:00
ridiculousfish
ebba30d671 Fix a "class <-> struct" warning with clang++ 2012-02-29 16:14:51 -08:00
ridiculousfish
909d24cde6 More work on improving interaction between fork and pthreads. Added null_terminated_array_t class. 2012-02-29 16:14:51 -08:00
ridiculousfish
4e912ef83d Added postfork 2012-02-29 16:14:51 -08:00
ridiculousfish
b17dfff3fd Fix for out-of-bounds write in highlight_param 2012-02-29 16:14:03 -08:00
ridiculousfish
52dc415d0b Fix to add a generation count to threaded operations so they abort early 2012-02-28 12:40:59 -08:00
Siteshwar Vashisht
392232246e Fixed compilation on Linux with some minor changes. 2012-02-28 21:20:09 +05:30
ridiculousfish
cf54ad8242 Change to wait for all outstanding iothreads before calling fork(). This should prevent a whole host of threading/fork interactions, but may also compromise performance...we'll see. 2012-02-27 19:46:15 -08:00
ridiculousfish
fdfa5c0602 Some initial work towards resolving nasty fork/pthread issues, and to having a per-parser job list 2012-02-27 18:43:24 -08:00
ridiculousfish
50ee5d28cd Fix to finally turn on multithreaded completions for autosuggestion 2012-02-27 15:33:46 -08:00
ridiculousfish
f74a82776f Some more changes in preparation for turning on complete-based autosuggestions 2012-02-26 20:11:34 -08:00
ridiculousfish
3553e65089 Add some more locking and const correctness to completion_entry_t in preparation for supporting better multithreading 2012-02-26 14:32:06 -08:00
ridiculousfish
ad7c5886bc Fix a crash in complete -C 2012-02-26 13:46:21 -08:00
ridiculousfish
b30090f946 Some cleanup of completions in preparation for more multithreading 2012-02-26 13:27:31 -08:00
ridiculousfish
8f637975a4 Temporarily disable background completions 2012-02-26 12:16:22 -08:00
ridiculousfish
94a764d6ea Fix for error messages when loading completions 2012-02-26 01:15:53 -08:00
ridiculousfish
38e40862fe More work towards autosuggesting completions 2012-02-25 18:54:49 -08:00
ridiculousfish
5ea78f55f2 Some more work on threaded completions 2012-02-24 18:43:10 -08:00
ridiculousfish
a515db4aea Some work to allow completions to be evaluated off of the main thread 2012-02-24 12:19:31 -08:00
ridiculousfish
90e979d0d9 Added some const correctness 2012-02-24 09:33:30 -08:00
Siteshwar Vashisht
8232857d07 Modified env_get_string() to accept wcstring instead of wchar_t*. 2012-02-24 00:58:01 +05:30
ridiculousfish
2ae56564ef Changes to block signals for a smaller time interval during saving 2012-02-23 10:49:30 -08:00
ridiculousfish
f24a0170be Fix for failing to unescape history properly, resulting in a backslash explosion 2012-02-23 10:29:42 -08:00
Siteshwar Vashisht
fd56465931 Fixed initialization of va_list variable in debug() in common.cpp, and mismatched new/free() in io.cpp 2012-02-23 23:10:51 +05:30
ridiculousfish
376e199ebb Removed a lot of string_buffer_t 2012-02-22 12:00:02 -08:00
ridiculousfish
a837a27b34 Replace some string_buffer_t with wcstring 2012-02-22 11:07:34 -08:00
ridiculousfish
62bd43f17f Large set of changes to migrate sb_out and sb_err to wcstring 2012-02-22 10:51:06 -08:00
ridiculousfish
917b4ff8bc Fix for crash on redirections 2012-02-21 19:39:29 -08:00
ridiculousfish
5fe7c065dc Squash a bunch of leaks 2012-02-21 19:33:11 -08:00
ridiculousfish
e074ad4807 Removed a leaking wcsdup 2012-02-21 18:18:10 -08:00
ridiculousfish
5f3fe4acff Removed an optimization obviated by our use of wcstring from env.cpp 2012-02-21 18:12:51 -08:00
ridiculousfish
4ccc197451 Fix to change highlighting functions to take wcstring& instead of const wchar_t * 2012-02-21 17:55:56 -08:00
ridiculousfish
237cf19a77 Switch to using std::vector instead of int* for colors in highlight.cpp, for better range bounds checking 2012-02-21 11:45:13 -08:00
ridiculousfish
2206e221bd Removed array_list_t (!) 2012-02-21 10:47:21 -08:00
ridiculousfish
d9bb9b73ad Change to allow building without doxygen 2012-02-20 16:18:10 -08:00
ridiculousfish
d5c382bb1a Piling on more code to make autosuggestion try to guess directories even when they're not in the history 2012-02-20 02:13:31 -08:00
Peter Ammon
52daf6cf41 Fix to allow color.cpp to use fallback wcscasecmp 2012-02-19 11:07:02 -08:00
Siteshwar Vashisht
74a7303c23 Modified functions in function.h/.cpp to use wcstring instead wchar_t*, other files also modified to use wcstring. 2012-02-19 22:55:15 +05:30
ridiculousfish
d8a9991738 Fix to avoid calling signal_block off of hte main thread 2012-02-18 23:26:39 -08:00
ridiculousfish
ca1c8243c8 Fix to prevent autosuggesting cd'ing to the current working directory 2012-02-18 21:56:30 -08:00
ridiculousfish
14b3a5be56 Changes to make autosuggestion even smarter by specially recognizing the cd command. 2012-02-18 18:54:36 -08:00
ridiculousfish
ed89df7e9d Some minor cleanup of reader.cpp in preparation to unify highlighting with autosuggestion 2012-02-18 18:51:11 -08:00
ridiculousfish
1bedc16544 Enhanced directory detection in a way we don't actually need yet 2012-02-18 18:51:11 -08:00
ridiculousfish
ac0b97a571 Squash some warnings 2012-02-18 18:49:55 -08:00
Siteshwar Vashisht
7b3d670e23 Removing some unnecessary calls to c_str(), and added str2wcstring(std::string) in common.cpp. 2012-02-18 23:28:54 +05:30
Siteshwar Vashisht
c9f4e91df8 Modified most functions in wutil.h/.cpp to use wcstring instead of wchar_t*, removing calls to c_str() while calling these functions in other files. 2012-02-18 22:41:22 +05:30
Siteshwar Vashisht
c0ed169fdc Getting rid of hash_table_t and it's helper functions completely. Bye bye hash_table_t ! 2012-02-18 21:21:10 +05:30
Siteshwar Vashisht
412894bfc8 Removed tests for hash_table_t from fish_tests.cpp 2012-02-18 21:04:09 +05:30
Siteshwar Vashisht
aad6e5648b Fix for properly removing universal variable. 2012-02-18 14:42:02 +05:30
ridiculousfish
8f1423946f Fix a crash when using quotes due to wgettext thread safety issues. 2012-02-17 15:55:54 -08:00
ridiculousfish
51da4856e2 Squash a leak in LRU caches 2012-02-17 14:54:58 -08:00
ridiculousfish
3b56c58f00 Fix for crash in env.cpp where a deleted entry was not removed from the map 2012-02-17 12:23:30 -08:00
ridiculousfish
4fd2531302 Fix to properly null-terminate export list 2012-02-17 11:42:53 -08:00
ridiculousfish
fdef238a83 Fix to stop spamming console with event messages (oops) 2012-02-17 11:40:26 -08:00
ridiculousfish
9787901ddb Fix for removing too many event handlers (that's why "Goodbye" never got printed") 2012-02-17 11:37:25 -08:00
Siteshwar Vashisht
6e58c9f7c8 Switched to std::map instead of hash_table_t in env.cpp and env_universal_common.cpp. 2012-02-18 00:25:29 +05:30
ridiculousfish
a08450bcb6 Changes to make autosuggestion smarter about not suggesting commands that could never succeed. 2012-02-16 00:24:27 -08:00
ridiculousfish
a92d9d442b Initial work towards making autosuggestion smarter by recognizing paths 2012-02-15 11:33:41 -08:00
ridiculousfish
e2ff77b4ec Clean up make_path and some memory allocations 2012-02-13 22:44:29 -08:00
ridiculousfish
caf7a93382 Fix for busted completions (tab completion of directories was failing) 2012-02-13 22:37:09 -08:00
ridiculousfish
afcda2f724 Fix a leak 2012-02-13 19:39:20 -08:00
ridiculousfish
fcea723aa7 Fixed parenthesis causing a crash or bizarre layout. 2012-02-13 17:15:41 -08:00
ridiculousfish
faf80588ab Fix for busted directory check 2012-02-13 16:38:35 -08:00
ridiculousfish
fc589611e2 Attempt to make fish_pager.cpp compile on Linux 2012-02-13 14:25:07 -08:00
ridiculousfish
5ecd350311 Another attempt to make mimedb.cpp compile on Linux 2012-02-13 14:22:07 -08:00
ridiculousfish
4b6c6c59b5 Squash warnings in mimedb, hopefully get it compiling on Linux 2012-02-13 12:14:26 -08:00
ridiculousfish
0f8ffa6226 For for crash printing backtrace for command "eval cd sldkfjsdlfk" 2012-02-13 11:55:02 -08:00
ridiculousfish
6def61dfe9 A bit more debugging to parser class to try to track down a crash 2012-02-13 11:42:58 -08:00
ridiculousfish
b39bebee06 Fix for problem where we'd never use a cached placeholder function. Also upped the staleness interval from its testing value of 1 to 15 seconds. 2012-02-13 10:28:04 -08:00
ridiculousfish
dce189fc6d Support for setting both RGB and named colors on the same line, so the same config can work for multiple term types 2012-02-13 09:52:17 -08:00
ridiculousfish
0a4c72e78b Added color.h, color.cpp. Got term256 colors working. 2012-02-12 18:05:59 -08:00
ridiculousfish
d66700a0e4 Color work 2012-02-11 17:07:56 -08:00
ridiculousfish
b59a22bef0 Colors 2012-02-10 17:57:20 -08:00
ridiculousfish
9b1930588f Migrate errors from array_list_t to wcstring_list_t 2012-02-10 17:54:38 -08:00
Siteshwar Vashisht
18bb64cd43 Fixed compilation with recent changes on Linux. 2012-02-10 21:25:06 +05:30
ridiculousfish
d975187433 Removed some al_list functions 2012-02-10 01:37:30 -08:00
ridiculousfish
910863e9ea Final removal of halloc. It's like Christmas Morning. 2012-02-09 19:26:44 -08:00
ridiculousfish
a38490429b Removed a halloc_register_function_void 2012-02-09 19:00:05 -08:00
ridiculousfish
80e8f6a0d1 Removed halloc_wcsdup 2012-02-09 18:59:15 -08:00
ridiculousfish
e5ff5f7484 Some hopefully good changes to get IOs off of halloc 2012-02-09 18:43:36 -08:00
ridiculousfish
646240fc54 Changed the Makefile to no longer fail if man pages are missing, etc. 2012-02-09 16:36:35 -08:00
ridiculousfish
19cd7a248d Cleanup of wildcard.cpp, removing ad-hoc hash table and halloc 2012-02-09 16:06:24 -08:00
ridiculousfish
f1b1d1ca75 Get rid of some string buffer 2012-02-09 10:14:06 -08:00
ridiculousfish
e5bba2294d Removed some more halloc 2012-02-09 02:01:49 -08:00
ridiculousfish
19cfd9b5d6 Fix for pager regression 2012-02-09 01:54:46 -08:00
ridiculousfish
7e1b15a0cc More changes to fish pager to fix regressions. It didn't actually work. 2012-02-09 01:39:08 -08:00
ridiculousfish
19858c574d Removed halloc and al_list from fish_pager. paging still seems to work 2012-02-09 01:02:12 -08:00
ridiculousfish
f891ea5713 Fixed named_arguments to be wcstring_list_t instead of al_list 2012-02-08 23:53:23 -08:00
ridiculousfish
d3311c81e5 REmoved al_list from exec.cpp 2012-02-08 22:56:42 -08:00
ridiculousfish
b6bc4381fb Get event_t off of halloc (!) 2012-02-08 19:02:25 -08:00
ridiculousfish
b3d0561c09 Cleanup event_get_desc 2012-02-08 16:20:48 -08:00
ridiculousfish
1bbe901bcd Eliminated halloc usage from complete.cpp 2012-02-08 16:15:53 -08:00
ridiculousfish
4f8b4379f5 Cleanup of completion_entry in complete.cpp 2012-02-08 14:48:22 -08:00
Siteshwar Vashisht
bc8a288386 Got rid of multiple cd paths, only current directory will be searched while changing directories, implicit cd (entering directory just by typing it's name) is removed. 2012-02-09 01:18:51 +05:30
ridiculousfish
3dc56de0ae Excised some more halloc and array_list_t 2012-02-08 02:34:31 -08:00
ridiculousfish
b2e5809180 Removed halloc from highlight.cpp 2012-02-08 02:13:39 -08:00
ridiculousfish
7e52523541 Migrate mimedb off of al_init 2012-02-08 01:55:35 -08:00
ridiculousfish
8bc83c5967 Removed al_list from mimedb.cpp 2012-02-08 01:22:08 -08:00
ridiculousfish
029c8c06c2 Remove old env_get_names implementation 2012-02-08 00:59:46 -08:00
ridiculousfish
063fc0c48b Cleanup al_list from builtin_complete.cpp 2012-02-08 00:45:07 -08:00
ridiculousfish
191eeab589 Cleanup expand_escape_variable 2012-02-08 00:15:06 -08:00
ridiculousfish
3f8621e566 CLeanup the io_stack in builtin.cpp, other changes to migrate away from al_list 2012-02-07 23:53:34 -08:00
ridiculousfish
5f686ebb47 Clean up exec_subshell, removing al_list from it 2012-02-07 23:35:41 -08:00
ridiculousfish
6a31457c6d Really excise halloc from input.cpp 2012-02-07 23:17:20 -08:00
ridiculousfish
e6f7912a25 Removed halloc from input.cpp 2012-02-07 23:15:32 -08:00
ridiculousfish
8a2737566c Excised some more halloc 2012-02-07 22:44:10 -08:00
ridiculousfish
006523ac59 Replaced void*data with auto_ptr<function_data_t>, eliminating antoher halloc. 2012-02-07 22:10:35 -08:00
ridiculousfish
c0e783eb6e Excised some halloc 2012-02-07 21:23:12 -08:00
ridiculousfish
399c78fbf7 Fix event_block_t list from ad-hoc linked list to std::dequeue 2012-02-07 21:04:51 -08:00
ridiculousfish
a0a43046b3 Removed discriminated union from block_t type, allowing us to store wcstrings in it 2012-02-07 17:36:54 -08:00
Siteshwar Vashisht
e8af86017a Fixed a bug caused while highlighting double quoted strings. For e.g. ls "abc" shows up as invalid write for color of closing quote in valgrind. 2012-02-07 21:37:53 +05:30
ridiculousfish
382ffe9b6a Added autosuggestion color variable fish_color_autosuggestion
Fixed that nasty bug where fish would apply a color to both the foreground and background (yuck)
2012-02-06 20:14:19 -08:00
ridiculousfish
e5b34d5cd5 Suppress autosuggesting during backspacing like browsers do 2012-02-06 19:31:06 -08:00
ridiculousfish
0dba7b3313 Use right arrow to accept autosuggestions 2012-02-06 11:59:34 -08:00
ridiculousfish
27f2859258 Make history searching skip the autosuggestion 2012-02-06 11:52:24 -08:00
ridiculousfish
eeed45da0f Fix some bugs related to not updating our buffer size as our command line changes 2012-02-06 11:34:49 -08:00
ridiculousfish
067dff8489 Initial stab at autosuggestions 2012-02-06 10:52:13 -08:00
ridiculousfish
7d3151191d Removed builtin_scripts.h and builtin_scripts.cpp from Xcode project because they're annoying in searches 2012-02-06 10:47:55 -08:00
ridiculousfish
98fa1325f5 insert_str -> insert_string 2012-02-06 02:20:50 -08:00
ridiculousfish
bd45b79ed8 Removed buff_len field. One fewer field, hooray. 2012-02-06 01:53:03 -08:00
ridiculousfish
0a616a9dbb Large set of changes to migrate reader_data_t's buffer to std::wstring 2012-02-06 01:45:16 -08:00
ridiculousfish
a534c397f5 Const correctness changes 2012-02-06 00:57:43 -08:00
ridiculousfish
737589ec01 Removed lru.cpp (double oops) 2012-02-06 00:53:08 -08:00
ridiculousfish
a7f8ee90c0 Added LRU.cpp (oops) 2012-02-06 00:52:22 -08:00
ridiculousfish
b14683200a Change the history so that when you go forwards, it no longer highlights 2012-02-05 23:22:18 -08:00
ridiculousfish
c82410bfda Fixed history tests 2012-02-05 22:48:43 -08:00
ridiculousfish
00ad837eb4 Final cleanup of history 2012-02-05 22:30:42 -08:00
ridiculousfish
9ab54030b9 Moved LRU to its own file 2012-02-05 20:54:41 -08:00
ridiculousfish
5ad6849d4e Work on new history implementation 2012-02-05 16:42:24 -08:00
ridiculousfish
7fcf25a78f Removed the "don't highlight with stale data" code, that caused "flashing" as we typed 2012-02-03 20:06:48 -08:00
ridiculousfish
5c205c43d1 Fix to highlight search results range matching again 2012-02-03 20:05:50 -08:00
ridiculousfish
f20f49d872 Migrate search_buff to wcstring 2012-02-03 20:01:01 -08:00
ridiculousfish
fb63c3908c Minor cleanup of background_highlight_context_t 2012-02-03 18:39:41 -08:00
ridiculousfish
623eb42a6a CLean up current_filename, make it a std::stack 2012-02-02 15:05:08 -08:00
ridiculousfish
6afc06b97e Migrate some al_list to std::vector 2012-02-02 14:27:13 -08:00
ridiculousfish
cdd5b1ed26 Fixed a crash doing completion of commands 2012-02-02 14:11:20 -08:00
ridiculousfish
884b4026dd Cleanup complete_cmd_desc 2012-02-02 12:04:04 -08:00
ridiculousfish
62f49c55ce Clean up uses of completion_t 2012-02-01 16:27:14 -08:00
ridiculousfish
0b4b6c498d Migrated off of old variant of expand_one that used halloc 2012-01-31 21:30:09 -08:00
ridiculousfish
409f70c7f3 Remove some old functions from expand.cpp that now have modern replacements 2012-01-31 21:09:11 -08:00
ridiculousfish
bb19fe703a Fixup wsetlocale to use wcstring 2012-01-31 21:06:52 -08:00
ridiculousfish
eef1e3e77e More cleanup of static string buffers in favor of wcstring 2012-01-31 20:22:25 -08:00
ridiculousfish
562ba1291e Stop using placement new like a weirdo 2012-01-31 20:03:48 -08:00
ridiculousfish
3adf6d25f6 Cleanup of builtins. No more hashes. 2012-01-31 19:47:56 -08:00
ridiculousfish
beece6a828 Clean up how argv is stored in process_t 2012-01-31 18:06:20 -08:00
ridiculousfish
207ab2aa5b Squashed all known remaining leaks! 2012-01-31 17:06:47 -08:00
ridiculousfish
4ccc2550d0 Squash a bunch of warnings 2012-01-31 17:01:19 -08:00
ridiculousfish
0c9e398bef Change parser_t::parse_job's args array to be a local, rather than just leak it, which no longer seems to crash 2012-01-31 16:50:03 -08:00
ridiculousfish
af2d348543 Substantial modifications of expand_variables to modernize, normalize memory management, adopt C++ types 2012-01-30 21:33:15 -08:00
ridiculousfish
13f72decf4 Fix a leak 2012-01-30 11:23:09 -08:00
ridiculousfish
3ad7de0fcb Fix some leaks and clean up expand_pid to use wcstring 2012-01-30 11:15:06 -08:00
Siteshwar Vashisht
48408b0a0b Merge branch 'CPlusPlus' of gitorious.org:~ridiculousfish/fish-shell/fishfish into CPlusPlus 2012-01-31 00:04:15 +05:30
Siteshwar Vashisht
cd7d1f2bfe Fix to not show thread debugging warning when entering characters like "~%". expand_string2() now uses it's own parser instead of principal_parser. Added PARSER_TYPE_ERRORS_ONLY in parser_type_t. 2012-01-30 23:58:30 +05:30
ridiculousfish
ed66a46113 Fix for is_potential_path that caused all strings to be reported as paths 2012-01-30 09:59:48 -08:00
ridiculousfish
8e4e30d266 Migrate functions like parser_keywords_is_block to wcstring 2012-01-30 09:46:33 -08:00
Siteshwar Vashisht
f988dcd6f9 Fixed compilation errors caused by latest commits 2012-01-30 21:45:02 +05:30
ridiculousfish
143091edad More work to excise halloc 2012-01-30 02:45:55 -08:00
ridiculousfish
79e0405f6a Yet more un-hallocing 2012-01-30 02:23:58 -08:00
ridiculousfish
316f81119f Excised some more halloc 2012-01-29 23:22:42 -08:00
ridiculousfish
8d016040ab Back out removing a halloc_register to fix a leak 2012-01-29 22:32:01 -08:00
ridiculousfish
4da6986a68 Fix to finally make builtin_scripts.h and builtin_scripts.cpp be generated by the Makefile 2012-01-29 22:29:13 -08:00
ridiculousfish
3b8a4e56b0 A bunch of work to move towards a sane memory model in job_t 2012-01-29 22:06:58 -08:00
ridiculousfish
966cd6a8ca Began migration of job_t away from halloc 2012-01-29 18:25:54 -08:00
ridiculousfish
f243cd86c9 Convert jobs list to std::list 2012-01-29 16:36:21 -08:00
Siteshwar Vashisht
1a5d866a91 buggy-auto-complete is not so buggy now. Merged branch 'buggy-auto-complete' into CPlusPlus 2012-01-29 14:11:39 +05:30
Siteshwar Vashisht
a1d8ed83dd Putting a copyright statement on my script :) 2012-01-29 13:02:40 +05:30
Peter Ammon
7f284c404f Fixed for comment in create_manpage_completions.py to refer to new filename 2012-01-28 22:48:47 -08:00
ridiculousfish
d1b3b6a0ed Added create_manpage_completions.py file for generating completions from man pages 2012-01-28 22:25:45 -08:00
ridiculousfish
9b37bc667f Fix for issue where we treated all cached files as accessible for the purpose of highlighting, instead of using their cached accessible variable 2012-01-28 15:49:50 -08:00
ridiculousfish
4eea68b5a4 LRU work to load functions off of the main thread.
We'll have to reevaluate this after we fix function autocomplete
2012-01-28 14:56:13 -08:00
ridiculousfish
87429bc03c More work on LRU cache and adopting it in function and completion autoloading 2012-01-27 11:43:52 -08:00
ridiculousfish
6c28448e84 Added LRU tests, and made tests work again 2012-01-27 11:43:45 -08:00
ridiculousfish
843ba4ac2c Move autoload class into autoload.h and cpp 2012-01-25 18:59:35 -08:00
ridiculousfish
d6545588a3 Have to remember to evict nodes from our LRU cache 2012-01-25 18:51:26 -08:00
ridiculousfish
26b375a0de Removed access_tracker since we don't need it after all 2012-01-25 18:42:41 -08:00
ridiculousfish
2f1cac604d Implemented LRU cache for autoloading. 2012-01-25 18:40:08 -08:00
ridiculousfish
8e56763c98 LRU cache work 2012-01-25 11:47:45 -08:00
ridiculousfish
e94e1cc72f New file autoload.h that will ultimately handle autoloading completions and functions 2012-01-25 00:36:55 -08:00
ridiculousfish
4dfe36feb1 Migrate input_function_get_names to wcstring_list_t 2012-01-23 20:48:47 -08:00
ridiculousfish
0d1c2a3b2a Migrated function.cpp to use the new function_get() call. Lots of nice short code now. 2012-01-23 20:43:39 -08:00
ridiculousfish
378fd60756 Migrate function.cpp to scoped_lock and shared_ptr 2012-01-23 20:32:36 -08:00
ridiculousfish
fa569b32fb Made intern'd strings thread safe.
Removed intern_free_all().
2012-01-23 20:02:15 -08:00
ridiculousfish
78f8317de8 Typo fixes 2012-01-23 19:33:57 -08:00
ridiculousfish
6e8637fbc9 Move autoloading from a map of path names to a real object autoload_t.
Moved the various things we can autoload into static objects.
Next step is to make them thread safe.
2012-01-23 11:42:41 -08:00
ridiculousfish
8403aae928 Convert some cases where parsers are created to using PARSER_TYPE_GENERAL 2012-01-22 21:57:30 -08:00
ridiculousfish
b43c8da66b More work towards instanced parser. First successful compilation 2012-01-22 21:40:08 -08:00
ridiculousfish
da85bdc401 More work on the instanced parser 2012-01-22 20:47:13 -08:00
ridiculousfish
7e486e3b5c More work towards instanced parser 2012-01-20 11:24:43 -08:00
Siteshwar Vashisht
062e423125 Fixed more known auto completion bugs; auto completion calls working fine now 2012-01-20 22:20:31 +05:30
ridiculousfish
3d8face1f9 More work on instancing the parser 2012-01-19 10:28:44 -08:00
Siteshwar Vashisht
0c99fc5b4d Removed unnecessary const_cast in wildcard.cpp 2012-01-19 22:23:25 +05:30
Siteshwar Vashisht
51d7d5d0da Fixed calls to expand_variables2() in expand.cpp 2012-01-19 21:49:24 +05:30
Siteshwar Vashisht
c959584831 Fixed reader.cpp to not show garbage while showing help on auto completing (on pressin tab) for executables like "ls -" 2012-01-19 00:03:19 +05:30
ridiculousfish
fa796d668f Get some basic function signatures right for new instanced parser 2012-01-16 12:10:08 -08:00
ridiculousfish
e4ee4ec3d1 Some const correctness 2012-01-16 11:16:12 -08:00
ridiculousfish
0073a93079 Beginnings of instance parser work 2012-01-16 11:09:19 -08:00
Siteshwar Vashisht
7e124cf95e Added constructor to completion_t for guarranted initialzation of flags attribute. 2012-01-16 23:54:57 +05:30
ridiculousfish
c647bed9d5 Fix for bogus check for IFS environment variable 2012-01-16 09:43:34 -08:00
Siteshwar Vashisht
6f330f961b Merge branch 'CPlusPlus' of gitorious.org:~ridiculousfish/fish-shell/fishfish into CPlusPlus 2012-01-16 22:27:55 +05:30
Siteshwar Vashisht
140ead65b6 Converted all auto completion calls (on pressing tab) to use std::vector<completion_t>, bugs are yet to be fixed 2012-01-16 22:26:47 +05:30
ridiculousfish
55091d9deb Fix to prevent autoloading of completions on highlight thread 2012-01-15 14:24:58 -08:00
Peter Ammon
1b3427acd9 Changes to make completions internalized scripts.
This about doubles the size of fish. These things are huge. We may not want to ship it like this.
2012-01-15 00:25:21 -08:00
Peter Ammon
60d1ac4fec More const and signed correctness. Warnings now fit on one page! 2012-01-14 22:48:53 -08:00
Peter Ammon
9b133a978d Improve const and signed/unsigned correctness 2012-01-14 22:32:45 -08:00
Peter Ammon
203c749e6c Improved const-correctness, eliminating warnings.
Migrated some int to size_t
2012-01-14 22:00:00 -08:00
Peter Ammon
f3e2d2f68f Lots of warnings fixes and const correctness 2012-01-14 03:41:50 -08:00
Peter Ammon
a359f45df2 Redesign new environment variables to use env_var_t instead of wcstring
Migrate uses of empty() to missing() to distinguish between empty variable and unset variable
2012-01-14 02:42:17 -08:00
Peter Ammon
e8b6d48ad0 Introduce env_var_t to replace empty string as missing environment variable 2012-01-14 01:06:47 -08:00
ridiculousfish
8ba79d6ab9 Migrate function.cpp to STL types. 2012-01-13 23:54:49 -08:00
ridiculousfish
57c9eeeecb Changes related to supporting "intern scripts." These are scripts that used to be installed in /share/functions, but now are just compiled-in strings. This should reduce the amount of I/O done at launch time. 2012-01-13 23:54:16 -08:00
Siteshwar Vashisht
f78c4fa751 Removed multiple delcartions of path_list from autoload_names() in fuction.cpp 2012-01-14 12:06:16 +05:30
Siteshwar Vashisht
3ba6e9db5d Merge branch 'Use_env_get_string' into CPlusPlus
Conflicts:
	function.cpp
2012-01-14 11:59:48 +05:30
Siteshwar Vashisht
610246c48f Modified function.cpp to use env_get_string(). 2012-01-12 23:22:38 +05:30
Siteshwar Vashisht
d0e18e3d20 Modified highlight.cpp to use env_get_string(). 2012-01-12 23:19:05 +05:30
Siteshwar Vashisht
72cfdbbb4e Modified input.cpp, output.cpp to use env_get_string() 2012-01-12 23:11:07 +05:30
Siteshwar Vashisht
15296dedd5 Modified kill.cpp to use env_get_string(). 2012-01-12 23:07:10 +05:30
Siteshwar Vashisht
943cc68f54 Modified parse_util.cpp, parser.cpp, path.cpp to use env_get_string() 2012-01-12 22:56:53 +05:30
Siteshwar Vashisht
3bb4d0b276 Modified reader.cpp to use env_get_string() 2012-01-12 22:32:50 +05:30
Siteshwar Vashisht
9cc2217a26 Fixed connection error to fishd server in env.cpp 2012-01-12 21:58:42 +05:30
Siteshwar Vashisht
ee687ee433 Modified exec.cpp to use env_get_string(); rolled back changes to expand.cpp in last commit, because pressing ^D results into an error with the changes 2012-01-12 21:39:00 +05:30
Siteshwar Vashisht
9b56b67c0e Revert "Modified expand.cpp and exec.cpp to use env_get_string()"
This reverts commit 48655e882e.
2012-01-12 04:21:11 +05:30
Siteshwar Vashisht
48655e882e Modified expand.cpp and exec.cpp to use env_get_string() 2012-01-12 04:19:04 +05:30
Siteshwar Vashisht
7f49d37a51 Modified exec.cpp to use env_get_string() 2012-01-12 04:01:08 +05:30
Siteshwar Vashisht
9f8a1168e6 Modified env.cpp to use env_get_string() 2012-01-12 03:57:38 +05:30
ridiculousfish
c18d177b8c Additional migration to STL data structures 2012-01-10 12:55:22 -08:00
Siteshwar Vashisht
7bde2b5cbf Modified complete.cpp to use env_get_string(); Fixed env_get_string() return an empty wcstring instead of returning 0. 2012-01-10 01:40:03 +05:30
Siteshwar Vashisht
7b3377e78c Modified builtin_set.cpp to use env_get(), added functions like update_values2(), env_set2() etc. to support wcstring and vector instead of using array_list_t 2012-01-10 01:19:37 +05:30
Siteshwar Vashisht
feed900f3d Further changes in builtin.cpp to use env_get_string() 2012-01-10 00:05:51 +05:30
Siteshwar Vashisht
7530057c6e Modified builting.cpp to use env_get_string() 2012-01-10 00:00:54 +05:30
Siteshwar Vashisht
a6b3f6b5d4 Modified screen.cpp to compile on Linux. 2012-01-07 16:35:49 +05:30
ridiculousfish
76f870a30a Improved check for pthread library to build successfully on Linux 2012-01-06 11:50:17 -08:00
ridiculousfish
02092f315e More work towards incorporating C++ build steps into Makefile.in and configure.ac 2012-01-06 11:29:03 -08:00
ridiculousfish
9ef9284a34 Fix for bogus cast warning in configure script that was breaking local_cv_use__posix_c_source 2012-01-06 10:43:27 -08:00
ridiculousfish
820acb981b Fix to incorporate C++ build changes into configure.ac and Makefile.in 2012-01-06 10:23:38 -08:00
ridiculousfish
2516fd90eb Updated dependencies in Makefile.in 2012-01-05 14:10:02 -08:00
ridiculousfish
de23368d03 Updated Makefile.cpp with updated header dependencies 2012-01-05 14:06:09 -08:00
ridiculousfish
4df57fda09 Incorporate Siteshwar's patch for building on Linux 2012-01-05 13:58:48 -08:00
ridiculousfish
6f45832aed Switch from std::set to std::map in parse_util.cpp 2012-01-04 17:56:02 -08:00
ridiculousfish
fec0415d4e Rewrote parse_util.cpp data structures to use STL types and classes 2012-01-04 17:40:36 -08:00
ridiculousfish
04c7d87261 More work towards supporting builtin scripts 2012-01-03 10:54:06 -08:00
ridiculousfish
6abf3db13e Added internalize_scripts.py in preparation for ending reliance on "standard" scripts in /usr/local/share 2012-01-02 17:02:30 -08:00
ridiculousfish
99000e68b7 Fix for crash when unescaping a string with unbalanced quotes
Add Mac OS X's default term name to terms for which we know how to set a title
2012-01-02 13:40:03 -08:00
ridiculousfish
bef046a51a More work to migrate off of ad-hoc data structures 2011-12-31 15:57:30 -08:00
ridiculousfish
78322a6321 Migrate some more away from array_list_t 2011-12-28 12:36:47 -08:00
ridiculousfish
46fa2dd2f0 Migration of screen away from ad-hoc lists 2011-12-27 18:41:38 -08:00
ridiculousfish
451399b344 Migrate to std::vector in event class 2011-12-27 00:06:07 -08:00
ridiculousfish
22a8e57a57 More migration away from al_list 2011-12-26 23:29:57 -08:00
ridiculousfish
9cd279e0e9 Migrate some more off of al_list 2011-12-26 23:13:05 -08:00
ridiculousfish
b67eaa3abd More migration to the STL 2011-12-26 22:51:34 -08:00
ridiculousfish
046c9b56f5 Migrate blocked to std::vector 2011-12-26 22:27:58 -08:00
ridiculousfish
6ad80247ce Migrate killme to std::vector 2011-12-26 22:22:55 -08:00
ridiculousfish
0b744edfa4 Migrate events to std::vector 2011-12-26 22:18:16 -08:00
ridiculousfish
0d8bb78f66 Fixed a build failure
Added a thread assertion to function.cpp
2011-12-26 21:56:23 -08:00
ridiculousfish
28ecc68841 Migrated some more data structures to the STL. Removed some ad-hoc data structure implementations. 2011-12-26 21:50:23 -08:00
ridiculousfish
7c7aba1202 Fix to migrate the universal variable server off of dyn_queue_t 2011-12-26 21:39:08 -08:00
ridiculousfish
74a1d70b8a Added iothread 2011-12-26 21:21:12 -08:00
ridiculousfish
165a5aaa83 Merge branch 'master' into CPlusPlus 2011-12-26 21:09:26 -08:00
ridiculousfish
04856aded0 IO port 2011-12-26 21:05:25 -08:00
ridiculousfish
a9c238a1fc Removed a log 2011-12-26 21:05:07 -08:00
ridiculousfish
8d2f107d61 Some changes to migrate towards C++ and a multithreaded model 2011-12-26 19:18:46 -08:00
ridiculousfish
3f16ace678 Initial C++ conversion 2011-12-26 19:11:54 -08:00
167 changed files with 41155 additions and 27145 deletions

4
.gitattributes vendored Normal file
View File

@@ -0,0 +1,4 @@
.gitattributes export-ignore
.gitignore export-ignore
/build_tools export-ignore

View File

@@ -0,0 +1,356 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objects = {
/* Begin PBXFileReference section */
D03EE83814DF88B200FC7150 /* lru.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = lru.h; sourceTree = "<group>"; };
D09B1C1914FC7B5B00F91077 /* postfork.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = postfork.cpp; sourceTree = "<group>"; };
D09B1C1A14FC7B5B00F91077 /* postfork.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = postfork.h; sourceTree = "<group>"; };
D0A0850313B3ACEE0099B651 /* builtin.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = builtin.h; sourceTree = "<group>"; };
D0A0850413B3ACEE0099B651 /* common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = common.h; sourceTree = "<group>"; };
D0A0850513B3ACEE0099B651 /* complete.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = complete.h; sourceTree = "<group>"; };
D0A0850613B3ACEE0099B651 /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
D0A0850713B3ACEE0099B651 /* env_universal_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = env_universal_common.h; sourceTree = "<group>"; };
D0A0850813B3ACEE0099B651 /* env_universal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = env_universal.h; sourceTree = "<group>"; };
D0A0850913B3ACEE0099B651 /* env.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = env.h; sourceTree = "<group>"; };
D0A0850A13B3ACEE0099B651 /* event.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = event.h; sourceTree = "<group>"; };
D0A0850B13B3ACEE0099B651 /* exec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = exec.h; sourceTree = "<group>"; };
D0A0850C13B3ACEE0099B651 /* expand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = expand.h; sourceTree = "<group>"; };
D0A0850D13B3ACEE0099B651 /* fallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = fallback.h; sourceTree = "<group>"; };
D0A0850E13B3ACEE0099B651 /* function.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = function.h; sourceTree = "<group>"; };
D0A0851113B3ACEE0099B651 /* highlight.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = highlight.h; sourceTree = "<group>"; };
D0A0851213B3ACEE0099B651 /* history.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = history.h; sourceTree = "<group>"; };
D0A0851313B3ACEE0099B651 /* input_common.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = input_common.h; sourceTree = "<group>"; };
D0A0851413B3ACEE0099B651 /* input.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = input.h; sourceTree = "<group>"; };
D0A0851513B3ACEE0099B651 /* intern.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = intern.h; sourceTree = "<group>"; };
D0A0851613B3ACEE0099B651 /* io.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = io.h; sourceTree = "<group>"; };
D0A0851713B3ACEE0099B651 /* iothread.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = iothread.h; sourceTree = "<group>"; };
D0A0851813B3ACEE0099B651 /* kill.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = kill.h; sourceTree = "<group>"; };
D0A0851913B3ACEE0099B651 /* mimedb.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mimedb.h; sourceTree = "<group>"; };
D0A0851A13B3ACEE0099B651 /* output.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = output.h; sourceTree = "<group>"; };
D0A0851B13B3ACEE0099B651 /* parse_util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parse_util.h; sourceTree = "<group>"; };
D0A0851C13B3ACEE0099B651 /* parser_keywords.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parser_keywords.h; sourceTree = "<group>"; };
D0A0851D13B3ACEE0099B651 /* parser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = parser.h; sourceTree = "<group>"; };
D0A0851E13B3ACEE0099B651 /* path.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = path.h; sourceTree = "<group>"; };
D0A0851F13B3ACEE0099B651 /* print_help.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = print_help.h; sourceTree = "<group>"; };
D0A0852013B3ACEE0099B651 /* proc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = proc.h; sourceTree = "<group>"; };
D0A0852113B3ACEE0099B651 /* reader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = reader.h; sourceTree = "<group>"; };
D0A0852213B3ACEE0099B651 /* sanity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sanity.h; sourceTree = "<group>"; };
D0A0852313B3ACEE0099B651 /* screen.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = screen.h; sourceTree = "<group>"; };
D0A0852413B3ACEE0099B651 /* signal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = signal.h; sourceTree = "<group>"; };
D0A0852513B3ACEE0099B651 /* tokenizer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tokenizer.h; sourceTree = "<group>"; };
D0A0852613B3ACEE0099B651 /* util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = util.h; sourceTree = "<group>"; };
D0A0852713B3ACEE0099B651 /* wgetopt.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wgetopt.h; sourceTree = "<group>"; };
D0A0852813B3ACEE0099B651 /* wildcard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wildcard.h; sourceTree = "<group>"; };
D0A0852913B3ACEE0099B651 /* wutil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = wutil.h; sourceTree = "<group>"; };
D0A0852A13B3ACEE0099B651 /* xdgmime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmime.h; sourceTree = "<group>"; };
D0A0852B13B3ACEE0099B651 /* xdgmimealias.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmimealias.h; sourceTree = "<group>"; };
D0A0852C13B3ACEE0099B651 /* xdgmimeglob.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmimeglob.h; sourceTree = "<group>"; };
D0A0852D13B3ACEE0099B651 /* xdgmimeint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmimeint.h; sourceTree = "<group>"; };
D0A0852E13B3ACEE0099B651 /* xdgmimemagic.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmimemagic.h; sourceTree = "<group>"; };
D0A0852F13B3ACEE0099B651 /* xdgmimeparent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = xdgmimeparent.h; sourceTree = "<group>"; };
D0A0853013B3ACEE0099B651 /* builtin_commandline.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_commandline.cpp; sourceTree = "<group>"; };
D0A0853113B3ACEE0099B651 /* builtin_complete.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_complete.cpp; sourceTree = "<group>"; };
D0A0853213B3ACEE0099B651 /* builtin_jobs.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_jobs.cpp; sourceTree = "<group>"; };
D0A0853313B3ACEE0099B651 /* builtin_set.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_set.cpp; sourceTree = "<group>"; };
D0A0853413B3ACEE0099B651 /* builtin_ulimit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_ulimit.cpp; sourceTree = "<group>"; };
D0A0853513B3ACEE0099B651 /* builtin.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = builtin.cpp; sourceTree = "<group>"; };
D0A0853613B3ACEE0099B651 /* common.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = common.cpp; sourceTree = "<group>"; };
D0A0853713B3ACEE0099B651 /* complete.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = complete.cpp; sourceTree = "<group>"; };
D0A0853813B3ACEE0099B651 /* env_universal_common.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = env_universal_common.cpp; sourceTree = "<group>"; };
D0A0853913B3ACEE0099B651 /* env_universal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = env_universal.cpp; sourceTree = "<group>"; };
D0A0853A13B3ACEE0099B651 /* env.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = env.cpp; sourceTree = "<group>"; };
D0A0853B13B3ACEE0099B651 /* event.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = event.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
D0A0853C13B3ACEE0099B651 /* exec.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = exec.cpp; sourceTree = "<group>"; };
D0A0853D13B3ACEE0099B651 /* expand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = expand.cpp; sourceTree = "<group>"; };
D0A0853E13B3ACEE0099B651 /* fallback.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fallback.cpp; sourceTree = "<group>"; };
D0A0853F13B3ACEE0099B651 /* fish_indent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fish_indent.cpp; sourceTree = "<group>"; };
D0A0854013B3ACEE0099B651 /* fish_pager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fish_pager.cpp; sourceTree = "<group>"; };
D0A0854113B3ACEE0099B651 /* fish_tests.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fish_tests.cpp; sourceTree = "<group>"; };
D0A0854213B3ACEE0099B651 /* fish.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fish.cpp; sourceTree = "<group>"; };
D0A0854313B3ACEE0099B651 /* fishd.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = fishd.cpp; sourceTree = "<group>"; };
D0A0854413B3ACEE0099B651 /* function.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = function.cpp; sourceTree = "<group>"; };
D0A0854713B3ACEE0099B651 /* highlight.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = highlight.cpp; sourceTree = "<group>"; };
D0A0854813B3ACEE0099B651 /* history.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = history.cpp; sourceTree = "<group>"; };
D0A0854913B3ACEE0099B651 /* input_common.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = input_common.cpp; sourceTree = "<group>"; };
D0A0854A13B3ACEE0099B651 /* input.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = input.cpp; sourceTree = "<group>"; };
D0A0854B13B3ACEE0099B651 /* intern.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = intern.cpp; sourceTree = "<group>"; };
D0A0854C13B3ACEE0099B651 /* io.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = io.cpp; sourceTree = "<group>"; };
D0A0854D13B3ACEE0099B651 /* iothread.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = iothread.cpp; sourceTree = "<group>"; };
D0A0854E13B3ACEE0099B651 /* key_reader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = key_reader.cpp; sourceTree = "<group>"; };
D0A0854F13B3ACEE0099B651 /* kill.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = kill.cpp; sourceTree = "<group>"; };
D0A0855013B3ACEE0099B651 /* mimedb.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = mimedb.cpp; sourceTree = "<group>"; };
D0A0855113B3ACEE0099B651 /* output.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = output.cpp; sourceTree = "<group>"; };
D0A0855213B3ACEE0099B651 /* parse_util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = parse_util.cpp; sourceTree = "<group>"; };
D0A0855313B3ACEE0099B651 /* parser_keywords.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = parser_keywords.cpp; sourceTree = "<group>"; };
D0A0855413B3ACEE0099B651 /* parser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; lineEnding = 0; path = parser.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
D0A0855513B3ACEE0099B651 /* path.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = path.cpp; sourceTree = "<group>"; };
D0A0855613B3ACEE0099B651 /* print_help.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = print_help.cpp; sourceTree = "<group>"; };
D0A0855713B3ACEE0099B651 /* proc.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = proc.cpp; sourceTree = "<group>"; };
D0A0855813B3ACEE0099B651 /* reader.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = reader.cpp; sourceTree = "<group>"; };
D0A0855913B3ACEE0099B651 /* sanity.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sanity.cpp; sourceTree = "<group>"; };
D0A0855A13B3ACEE0099B651 /* screen.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = screen.cpp; sourceTree = "<group>"; };
D0A0855B13B3ACEE0099B651 /* set_color.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = set_color.cpp; sourceTree = "<group>"; };
D0A0855C13B3ACEE0099B651 /* signal.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = signal.cpp; sourceTree = "<group>"; };
D0A0855D13B3ACEE0099B651 /* tokenizer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tokenizer.cpp; sourceTree = "<group>"; };
D0A0855E13B3ACEE0099B651 /* util.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = util.cpp; sourceTree = "<group>"; };
D0A0855F13B3ACEE0099B651 /* wgetopt.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wgetopt.cpp; sourceTree = "<group>"; };
D0A0856013B3ACEE0099B651 /* wildcard.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wildcard.cpp; sourceTree = "<group>"; };
D0A0856113B3ACEE0099B651 /* wutil.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = wutil.cpp; sourceTree = "<group>"; };
D0A0856213B3ACEE0099B651 /* xdgmime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmime.cpp; sourceTree = "<group>"; };
D0A0856313B3ACEE0099B651 /* xdgmimealias.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmimealias.cpp; sourceTree = "<group>"; };
D0A0856413B3ACEE0099B651 /* xdgmimeglob.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmimeglob.cpp; sourceTree = "<group>"; };
D0A0856513B3ACEE0099B651 /* xdgmimeint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmimeint.cpp; sourceTree = "<group>"; };
D0A0856613B3ACEE0099B651 /* xdgmimemagic.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmimemagic.cpp; sourceTree = "<group>"; };
D0A0856713B3ACEE0099B651 /* xdgmimeparent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = xdgmimeparent.cpp; sourceTree = "<group>"; };
D0B6B0FE14E88BA400AD6C10 /* color.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = color.cpp; sourceTree = "<group>"; };
D0B6B0FF14E88BA400AD6C10 /* color.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = color.h; sourceTree = "<group>"; };
D0C6FCC914CFA4B0004CE8AD /* autoload.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = autoload.cpp; sourceTree = "<group>"; };
D0C6FCCB14CFA4B7004CE8AD /* autoload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = autoload.h; sourceTree = "<group>"; };
D0F3373A1506DE3C00ECEFC0 /* builtin_test.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = builtin_test.cpp; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXGroup section */
D0A084F013B3AC130099B651 = {
isa = PBXGroup;
children = (
D0C6FCCB14CFA4B7004CE8AD /* autoload.h */,
D0C6FCC914CFA4B0004CE8AD /* autoload.cpp */,
D0A0850313B3ACEE0099B651 /* builtin.h */,
D0A0853013B3ACEE0099B651 /* builtin_commandline.cpp */,
D0A0853113B3ACEE0099B651 /* builtin_complete.cpp */,
D0A0853213B3ACEE0099B651 /* builtin_jobs.cpp */,
D0A0853313B3ACEE0099B651 /* builtin_set.cpp */,
D0A0853413B3ACEE0099B651 /* builtin_ulimit.cpp */,
D0F3373A1506DE3C00ECEFC0 /* builtin_test.cpp */,
D0A0853513B3ACEE0099B651 /* builtin.cpp */,
D0B6B0FF14E88BA400AD6C10 /* color.h */,
D0B6B0FE14E88BA400AD6C10 /* color.cpp */,
D0A0850413B3ACEE0099B651 /* common.h */,
D0A0853613B3ACEE0099B651 /* common.cpp */,
D0A0850513B3ACEE0099B651 /* complete.h */,
D0A0853713B3ACEE0099B651 /* complete.cpp */,
D0A0850613B3ACEE0099B651 /* config.h */,
D0A0850713B3ACEE0099B651 /* env_universal_common.h */,
D0A0853813B3ACEE0099B651 /* env_universal_common.cpp */,
D0A0850813B3ACEE0099B651 /* env_universal.h */,
D0A0853913B3ACEE0099B651 /* env_universal.cpp */,
D0A0850913B3ACEE0099B651 /* env.h */,
D0A0853A13B3ACEE0099B651 /* env.cpp */,
D0A0850A13B3ACEE0099B651 /* event.h */,
D0A0853B13B3ACEE0099B651 /* event.cpp */,
D0A0850B13B3ACEE0099B651 /* exec.h */,
D0A0853C13B3ACEE0099B651 /* exec.cpp */,
D0A0850C13B3ACEE0099B651 /* expand.h */,
D0A0853D13B3ACEE0099B651 /* expand.cpp */,
D0A0850D13B3ACEE0099B651 /* fallback.h */,
D0A0853E13B3ACEE0099B651 /* fallback.cpp */,
D0A0850E13B3ACEE0099B651 /* function.h */,
D0A0854413B3ACEE0099B651 /* function.cpp */,
D0A0853F13B3ACEE0099B651 /* fish_indent.cpp */,
D0A0854013B3ACEE0099B651 /* fish_pager.cpp */,
D0A0854113B3ACEE0099B651 /* fish_tests.cpp */,
D0A0854213B3ACEE0099B651 /* fish.cpp */,
D0A0854313B3ACEE0099B651 /* fishd.cpp */,
D0A0851113B3ACEE0099B651 /* highlight.h */,
D0A0854713B3ACEE0099B651 /* highlight.cpp */,
D0A0851213B3ACEE0099B651 /* history.h */,
D0A0854813B3ACEE0099B651 /* history.cpp */,
D0A0851313B3ACEE0099B651 /* input_common.h */,
D0A0854913B3ACEE0099B651 /* input_common.cpp */,
D0A0851413B3ACEE0099B651 /* input.h */,
D0A0854A13B3ACEE0099B651 /* input.cpp */,
D0A0851513B3ACEE0099B651 /* intern.h */,
D0A0854B13B3ACEE0099B651 /* intern.cpp */,
D0A0851613B3ACEE0099B651 /* io.h */,
D0A0854C13B3ACEE0099B651 /* io.cpp */,
D0A0851713B3ACEE0099B651 /* iothread.h */,
D0A0854D13B3ACEE0099B651 /* iothread.cpp */,
D0A0851813B3ACEE0099B651 /* kill.h */,
D0A0854F13B3ACEE0099B651 /* kill.cpp */,
D0A0854E13B3ACEE0099B651 /* key_reader.cpp */,
D03EE83814DF88B200FC7150 /* lru.h */,
D0A0851913B3ACEE0099B651 /* mimedb.h */,
D0A0855013B3ACEE0099B651 /* mimedb.cpp */,
D0A0851A13B3ACEE0099B651 /* output.h */,
D0A0855113B3ACEE0099B651 /* output.cpp */,
D0A0851B13B3ACEE0099B651 /* parse_util.h */,
D0A0855213B3ACEE0099B651 /* parse_util.cpp */,
D0A0851C13B3ACEE0099B651 /* parser_keywords.h */,
D0A0855313B3ACEE0099B651 /* parser_keywords.cpp */,
D0A0851D13B3ACEE0099B651 /* parser.h */,
D0A0855413B3ACEE0099B651 /* parser.cpp */,
D0A0851E13B3ACEE0099B651 /* path.h */,
D0A0855513B3ACEE0099B651 /* path.cpp */,
D09B1C1A14FC7B5B00F91077 /* postfork.h */,
D09B1C1914FC7B5B00F91077 /* postfork.cpp */,
D0A0851F13B3ACEE0099B651 /* print_help.h */,
D0A0855613B3ACEE0099B651 /* print_help.cpp */,
D0A0852013B3ACEE0099B651 /* proc.h */,
D0A0855713B3ACEE0099B651 /* proc.cpp */,
D0A0852113B3ACEE0099B651 /* reader.h */,
D0A0855813B3ACEE0099B651 /* reader.cpp */,
D0A0852213B3ACEE0099B651 /* sanity.h */,
D0A0855913B3ACEE0099B651 /* sanity.cpp */,
D0A0852313B3ACEE0099B651 /* screen.h */,
D0A0855A13B3ACEE0099B651 /* screen.cpp */,
D0A0852413B3ACEE0099B651 /* signal.h */,
D0A0855C13B3ACEE0099B651 /* signal.cpp */,
D0A0855B13B3ACEE0099B651 /* set_color.cpp */,
D0A0852513B3ACEE0099B651 /* tokenizer.h */,
D0A0855D13B3ACEE0099B651 /* tokenizer.cpp */,
D0A0852613B3ACEE0099B651 /* util.h */,
D0A0855E13B3ACEE0099B651 /* util.cpp */,
D0A0852713B3ACEE0099B651 /* wgetopt.h */,
D0A0855F13B3ACEE0099B651 /* wgetopt.cpp */,
D0A0852813B3ACEE0099B651 /* wildcard.h */,
D0A0856013B3ACEE0099B651 /* wildcard.cpp */,
D0A0852913B3ACEE0099B651 /* wutil.h */,
D0A0856113B3ACEE0099B651 /* wutil.cpp */,
D0A0852A13B3ACEE0099B651 /* xdgmime.h */,
D0A0856213B3ACEE0099B651 /* xdgmime.cpp */,
D0A0852B13B3ACEE0099B651 /* xdgmimealias.h */,
D0A0856313B3ACEE0099B651 /* xdgmimealias.cpp */,
D0A0852C13B3ACEE0099B651 /* xdgmimeglob.h */,
D0A0856413B3ACEE0099B651 /* xdgmimeglob.cpp */,
D0A0852D13B3ACEE0099B651 /* xdgmimeint.h */,
D0A0856513B3ACEE0099B651 /* xdgmimeint.cpp */,
D0A0852E13B3ACEE0099B651 /* xdgmimemagic.h */,
D0A0856613B3ACEE0099B651 /* xdgmimemagic.cpp */,
D0A0852F13B3ACEE0099B651 /* xdgmimeparent.h */,
D0A0856713B3ACEE0099B651 /* xdgmimeparent.cpp */,
);
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXLegacyTarget section */
D0A084F713B3AC130099B651 /* FishsFish */ = {
isa = PBXLegacyTarget;
buildArgumentsString = "-k ${ACTION}";
buildConfigurationList = D0A084FA13B3AC130099B651 /* Build configuration list for PBXLegacyTarget "FishsFish" */;
buildPhases = (
);
buildToolPath = /usr/bin/make;
buildWorkingDirectory = "";
dependencies = (
);
name = FishsFish;
passBuildSettingsInEnvironment = 1;
productName = FishsFish;
};
/* End PBXLegacyTarget section */
/* Begin PBXProject section */
D0A084F213B3AC130099B651 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
};
buildConfigurationList = D0A084F513B3AC130099B651 /* Build configuration list for PBXProject "FishsFish" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = D0A084F013B3AC130099B651;
projectDirPath = "";
projectRoot = "";
targets = (
D0A084F713B3AC130099B651 /* FishsFish */,
);
};
/* End PBXProject section */
/* Begin XCBuildConfiguration section */
D0A084F813B3AC130099B651 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = DEBUG;
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.6;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
};
name = Debug;
};
D0A084F913B3AC130099B651 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.6;
SDKROOT = macosx;
};
name = Release;
};
D0A084FB13B3AC130099B651 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
COPY_PHASE_STRIP = NO;
DEBUGGING_SYMBOLS = YES;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_GENERATE_DEBUGGING_SYMBOLS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
OTHER_CFLAGS = "";
OTHER_LDFLAGS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
D0A084FC13B3AC130099B651 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
OTHER_CFLAGS = "";
OTHER_LDFLAGS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
D0A084F513B3AC130099B651 /* Build configuration list for PBXProject "FishsFish" */ = {
isa = XCConfigurationList;
buildConfigurations = (
D0A084F813B3AC130099B651 /* Debug */,
D0A084F913B3AC130099B651 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D0A084FA13B3AC130099B651 /* Build configuration list for PBXLegacyTarget "FishsFish" */ = {
isa = XCConfigurationList;
buildConfigurations = (
D0A084FB13B3AC130099B651 /* Debug */,
D0A084FC13B3AC130099B651 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = D0A084F213B3AC130099B651 /* Project object */;
}

View File

@@ -38,7 +38,7 @@ PACKAGE_TARNAME = @PACKAGE_TARNAME@
# Programs # Programs
# #
CC := @CC@ CXX := @CXX@
INSTALL:=@INSTALL@ INSTALL:=@INSTALL@
@@ -63,8 +63,7 @@ optbindirs = @optbindirs@
# #
MACROS = -DLOCALEDIR=\"$(localedir)\" -DPREFIX=L\"$(prefix)\" -DDATADIR=L\"$(datadir)\" -DSYSCONFDIR=L\"$(sysconfdir)\" MACROS = -DLOCALEDIR=\"$(localedir)\" -DPREFIX=L\"$(prefix)\" -DDATADIR=L\"$(datadir)\" -DSYSCONFDIR=L\"$(sysconfdir)\"
CFLAGS = @CFLAGS@ $(MACROS) $(EXTRA_CFLAGS) CXXFLAGS = @CXXFLAGS@ $(MACROS) $(EXTRA_CXXFLAGS)
CPPFLAGS = @CPPFLAGS@
LDFLAGS = @LIBS@ @LDFLAGS@ LDFLAGS = @LIBS@ @LDFLAGS@
LDFLAGS_FISH = ${LDFLAGS} @LIBS_FISH@ @LDFLAGS_FISH@ LDFLAGS_FISH = ${LDFLAGS} @LIBS_FISH@ @LDFLAGS_FISH@
LDFLAGS_FISH_INDENT = ${LDFLAGS} @LIBS_FISH_INDENT@ LDFLAGS_FISH_INDENT = ${LDFLAGS} @LIBS_FISH_INDENT@
@@ -81,11 +80,11 @@ HAVE_GETTEXT=@HAVE_GETTEXT@
# #
#Additional .c files used by common.o. These also have a corresponding #Additional .cpp files used by common.o. These also have a corresponding
#.h file. #.h file.
# #
COMMON_FILES := util.c halloc.c halloc_util.c fallback.c COMMON_FILES := util.cpp fallback.cpp
# #
@@ -96,8 +95,9 @@ FISH_OBJS := function.o builtin.o complete.o env.o exec.o expand.o \
highlight.o history.o kill.o parser.o proc.o reader.o sanity.o \ highlight.o history.o kill.o parser.o proc.o reader.o sanity.o \
tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \ tokenizer.o wildcard.o wgetopt.o wutil.o input.o output.o intern.o \
env_universal.o env_universal_common.o input_common.o event.o \ env_universal.o env_universal_common.o input_common.o event.o \
signal.o io.o parse_util.o common.o screen.o path.o \ signal.o io.o parse_util.o common.o screen.o path.o autoload.o \
parser_keywords.o parser_keywords.o iothread.o builtin_scripts.o color.o postfork.o \
builtin_test.o
FISH_INDENT_OBJS := fish_indent.o print_help.o common.o \ FISH_INDENT_OBJS := fish_indent.o print_help.o common.o \
parser_keywords.o wutil.o tokenizer.o parser_keywords.o wutil.o tokenizer.o
@@ -106,8 +106,8 @@ parser_keywords.o wutil.o tokenizer.o
# Additional files used by builtin.o # Additional files used by builtin.o
# #
BUILTIN_FILES := builtin_set.c builtin_commandline.c \ BUILTIN_FILES := builtin_set.cpp builtin_commandline.cpp \
builtin_ulimit.c builtin_complete.c builtin_jobs.c builtin_ulimit.cpp builtin_complete.cpp builtin_jobs.cpp
# #
@@ -116,7 +116,7 @@ BUILTIN_FILES := builtin_set.c builtin_commandline.c \
FISH_PAGER_OBJS := fish_pager.o output.o wutil.o \ FISH_PAGER_OBJS := fish_pager.o output.o wutil.o \
input_common.o env_universal.o env_universal_common.o common.o \ input_common.o env_universal.o env_universal_common.o common.o \
print_help.o print_help.o iothread.o color.o
# #
@@ -160,6 +160,24 @@ HDR_FILES_SRC := doc_src/index.hdr.in doc_src/commands.hdr.in doc_src/design.hdr
HDR_FILES := $(subst .hdr.in,.hdr,$(HDR_FILES_SRC)) HDR_FILES := $(subst .hdr.in,.hdr,$(HDR_FILES_SRC))
#
# Internalized scripts
#
GENERATED_INTERN_SCRIPT_FILES := builtin_scripts.h builtin_scripts.cpp
# Use a pattern rule so that Make knows to only issue one invocation
# per http://www.gnu.org/software/make/manual/make.html#Pattern-Intro
# Internalized scripts are currently disabled.
# For now, we just generate empty arrays.
# To generate them again, you would run this:
# ./internalize_scripts.py share/functions/*.fish share/completions/*.fish
builtin%scripts.h builtin%scripts.cpp: internalize_scripts.py
./internalize_scripts.py
# #
# Files containing documentation for external commands. # Files containing documentation for external commands.
# #
@@ -191,12 +209,12 @@ DOC_SRC_DIR_FILES := $(HDR_FILES_SRC) $(HELP_SRC)
MAIN_DIR_FILES_UNSORTED := Doxyfile Doxyfile.user Doxyfile.help.in \ MAIN_DIR_FILES_UNSORTED := Doxyfile Doxyfile.user Doxyfile.help.in \
Makefile.in configure configure.ac config.h.in install-sh \ Makefile.in configure configure.ac config.h.in install-sh \
set_color.c key_reader.c $(MIME_OBJS:.o=.h) \ set_color.cpp key_reader.cpp $(MIME_OBJS:.o=.h) \
$(MIME_OBJS:.o=.c) $(FISH_OBJS:.o=.h) $(BUILTIN_FILES) \ $(MIME_OBJS:.o=.cpp) $(FISH_OBJS:.o=.h) $(BUILTIN_FILES) \
$(COMMON_FILES) $(COMMON_FILES:.c=.h) $(FISH_OBJS:.o=.c) \ $(COMMON_FILES) $(COMMON_FILES:.cpp=.h) $(FISH_OBJS:.o=.cpp) \
fish.spec.in INSTALL README user_doc.head.html xsel-0.9.6.tar \ fish.spec.in INSTALL README user_doc.head.html xsel-0.9.6.tar \
ChangeLog config.sub config.guess fish_tests.c fish.c fish_pager.c \ ChangeLog config.sub config.guess fish_tests.cpp fish.cpp fish_pager.cpp \
fishd.c seq.in make_vcs_completions.fish $(FISH_INDENT_OBJS:.o=.c) fishd.cpp seq.in make_vcs_completions.fish $(FISH_INDENT_OBJS:.o=.cpp)
# #
# The sorting is not meaningful in itself, but it has the side effect # The sorting is not meaningful in itself, but it has the side effect
@@ -302,11 +320,11 @@ Makefile: Makefile.in configure
# #
debug: debug:
$(MAKE) all EXTRA_CFLAGS="-O0 -Wno-unused -Werror -g" $(MAKE) fish EXTRA_CXXFLAGS="-O0 -Wno-unused -g"
.PHONY: debug .PHONY: debug
prof: prof:
$(MAKE) all EXTRA_CFLAGS="-pg" LDFLAGS="-pg" $(MAKE) all EXTRA_CXXFLAGS="-pg" LDFLAGS="-pg"
.PHONY: prof .PHONY: prof
# #
@@ -315,10 +333,11 @@ prof:
# Depend on the sources (*.hdr.in) and manually make the # Depend on the sources (*.hdr.in) and manually make the
# intermediate *.hdr and doc.h files if needed # intermediate *.hdr and doc.h files if needed
# Allow doxygen to fail, e.g. if it does not exist
user_doc: $(HDR_FILES_SRC) Doxyfile.user user_doc.head.html $(HELP_SRC) user_doc: $(HDR_FILES_SRC) Doxyfile.user user_doc.head.html $(HELP_SRC)
$(MAKE) doc.h $(HDR_FILES) $(MAKE) doc.h $(HDR_FILES)
doxygen Doxyfile.user - doxygen Doxyfile.user
touch user_doc touch user_doc
@@ -326,7 +345,7 @@ user_doc: $(HDR_FILES_SRC) Doxyfile.user user_doc.head.html $(HELP_SRC)
# Source code documentation. Also includes user documentation. # Source code documentation. Also includes user documentation.
# #
doc: *.h *.c doc.h Doxyfile doc: *.h *.cpp doc.h Doxyfile
doxygen; doxygen;
@@ -453,9 +472,9 @@ doc.h: $(HDR_FILES)
# Create a template translation object # Create a template translation object
# #
messages.pot: *.c *.h etc/*.in share/*.in share/completions/*.fish share/functions/*.fish seq messages.pot: *.cpp *.h etc/*.in share/*.in share/completions/*.fish share/functions/*.fish seq
if test $(HAVE_GETTEXT) = 1;then \ if test $(HAVE_GETTEXT) = 1;then \
xgettext -k_ -kN_ *.c *.h -o messages.pot; \ xgettext -k_ -kN_ *.cpp *.h -o messages.pot; \
if xgettext -j -k_ -kN_ -k--description -LShell etc/*.in share/*.in share/completions/*.fish share/functions/*.fish seq -o messages.pot; then true; else \ if xgettext -j -k_ -kN_ -k--description -LShell etc/*.in share/*.in share/completions/*.fish share/functions/*.fish seq -o messages.pot; then true; else \
echo "Your xgettext version is too old to build the messages.pot file"\ echo "Your xgettext version is too old to build the messages.pot file"\
rm messages.pot\ rm messages.pot\
@@ -506,13 +525,15 @@ share/man: $(HELP_SRC)
cat $$i >>$$FILE; \ cat $$i >>$$FILE; \
echo "*/" >>$$FILE; \ echo "*/" >>$$FILE; \
done done
doxygen Doxyfile.help if command -v doxygen; then \
for i in $(HELP_SRC); do \ doxygen Doxyfile.help ; \
CMD_NAME=`basename $$i .txt`; \ for i in $(HELP_SRC); do \
sed -e "s/\(.\)\\.SH/\1/" -e "s/$$CMD_NAME *\\\\- *\"\(.*\)\"/\1/" <help_doc/man/man1/$$CMD_NAME.1 >share/man/$$CMD_NAME.1; \ CMD_NAME=`basename $$i .txt`; \
done sed -e "s/\(.\)\\.SH/\1/" -e "s/$$CMD_NAME *\\\\- *\"\(.*\)\"/\1/" <help_doc/man/man1/$$CMD_NAME.1 >share/man/$$CMD_NAME.1; \
done \
fi
rm doc_src/*.doxygen # Clean up intermediate files in doc_src/ rm doc_src/*.doxygen # Clean up intermediate files in doc_src/
rm -r help_doc # Clean up intermediate help_doc tree rm -rf help_doc # Clean up intermediate help_doc tree
# #
# The build rules for installing/uninstalling fish # The build rules for installing/uninstalling fish
@@ -572,29 +593,49 @@ install: all install-sh check-uninstall install-force
# #
# Force installation, even in presense of incompatible previous # Force installation, even in presense of incompatible previous
# version. This may fail. # version. This may fail.
# These 'true' lines are to prevent installs from failing for (e.g.) missing man pages.
# #
install-force: all install-translations install-force: all install-translations
$(INSTALL) -m 755 -d $(DESTDIR)$(bindir) $(INSTALL) -m 755 -d $(DESTDIR)$(bindir)
for i in $(PROGRAMS); do\ for i in $(PROGRAMS); do\
$(INSTALL) -m 755 $$i $(DESTDIR)$(bindir) ; \ $(INSTALL) -m 755 $$i $(DESTDIR)$(bindir) ; \
true ;\
done; done;
$(INSTALL) -m 755 -d $(DESTDIR)$(sysconfdir)/fish $(INSTALL) -m 755 -d $(DESTDIR)$(sysconfdir)/fish
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish $(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/completions $(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/completions
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/functions $(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/functions
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/man $(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/man
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/tools
$(INSTALL) -m 755 -d $(DESTDIR)$(datadir)/fish/tools/web_config
$(INSTALL) -m 644 etc/config.fish $(DESTDIR)$(sysconfdir)/fish/ $(INSTALL) -m 644 etc/config.fish $(DESTDIR)$(sysconfdir)/fish/
$(INSTALL) -m 644 share/config.fish $(DESTDIR)$(datadir)/fish/ $(INSTALL) -m 644 share/config.fish $(DESTDIR)$(datadir)/fish/
for i in $(COMPLETIONS_DIR_FILES); do \ for i in $(COMPLETIONS_DIR_FILES); do \
$(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/completions/; \ $(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/completions/; \
true; \
done; done;
for i in $(FUNCTIONS_DIR_FILES); do \ for i in $(FUNCTIONS_DIR_FILES); do \
$(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/functions/; \ $(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/functions/; \
true; \
done; done;
for i in share/man/*.1; do \ for i in share/man/*.1; do \
$(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/man/; \ $(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/man/; \
true; \
done; done;
for i in share/tools/*.py; do\
$(INSTALL) -m 755 $$i $(DESTDIR)$(datadir)/fish/tools/; \
true; \
done;
for i in share/tools/web_config/*; do\
$(INSTALL) -m 644 $$i $(DESTDIR)$(datadir)/fish/tools/web_config/; \
true; \
done;
for i in share/tools/web_config/*.py; do\
$(INSTALL) -m 755 $$i $(DESTDIR)$(datadir)/fish/tools/web_config/; \
true; \
done;
$(INSTALL) -m 755 -d $(DESTDIR)$(docdir) $(INSTALL) -m 755 -d $(DESTDIR)$(docdir)
for i in user_doc/html/* ChangeLog; do \ for i in user_doc/html/* ChangeLog; do \
if test -f $$i; then \ if test -f $$i; then \
@@ -604,6 +645,7 @@ install-force: all install-translations
$(INSTALL) -m 755 -d $(DESTDIR)$(mandir)/man1 $(INSTALL) -m 755 -d $(DESTDIR)$(mandir)/man1
for i in $(MANUALS); do \ for i in $(MANUALS); do \
$(INSTALL) -m 644 $$i $(DESTDIR)$(mandir)/man1/; \ $(INSTALL) -m 644 $$i $(DESTDIR)$(mandir)/man1/; \
true; \
done; done;
@echo fish is now installed on your system. @echo fish is now installed on your system.
@echo To run fish, type \'fish\' in your terminal. @echo To run fish, type \'fish\' in your terminal.
@@ -612,6 +654,9 @@ install-force: all install-translations
@echo \* add the line \'$(DESTDIR)$(bindir)/fish\' to the file \'/etc/shells\'. @echo \* add the line \'$(DESTDIR)$(bindir)/fish\' to the file \'/etc/shells\'.
@echo \* use the command \'chsh -s $(DESTDIR)$(bindir)/fish\'. @echo \* use the command \'chsh -s $(DESTDIR)$(bindir)/fish\'.
@echo @echo
@echo To set your colors, run \'fish_config\'
@echo To scan your man pages for completions, run \'fish_update_completions\'
@echo
@echo Have fun! @echo Have fun!
.PHONY: install-force .PHONY: install-force
@@ -693,7 +738,7 @@ uninstall-translations:
# #
fish: $(FISH_OBJS) fish.o fish: $(FISH_OBJS) fish.o
$(CC) $(FISH_OBJS) fish.o $(LDFLAGS_FISH) -o $@ $(CXX) $(FISH_OBJS) fish.o $(LDFLAGS_FISH) -o $@
# #
@@ -701,7 +746,7 @@ fish: $(FISH_OBJS) fish.o
# #
fish_pager: $(FISH_PAGER_OBJS) fish_pager: $(FISH_PAGER_OBJS)
$(CC) $(FISH_PAGER_OBJS) $(LDFLAGS_FISH_PAGER) -o $@ $(CXX) $(FISH_PAGER_OBJS) $(LDFLAGS_FISH_PAGER) -o $@
# #
@@ -709,7 +754,7 @@ fish_pager: $(FISH_PAGER_OBJS)
# #
fishd: $(FISHD_OBJS) fishd: $(FISHD_OBJS)
$(CC) $(FISHD_OBJS) $(LDFLAGS_FISHD) -o $@ $(CXX) $(FISHD_OBJS) $(LDFLAGS_FISHD) -o $@
# #
@@ -717,7 +762,7 @@ fishd: $(FISHD_OBJS)
# #
fish_tests: $(FISH_TESTS_OBJS) fish_tests: $(FISH_TESTS_OBJS)
$(CC) $(FISH_TESTS_OBJS) $(LDFLAGS_FISH) -o $@ $(CXX) $(FISH_TESTS_OBJS) $(LDFLAGS_FISH) -o $@
# #
@@ -727,23 +772,23 @@ fish_tests: $(FISH_TESTS_OBJS)
# #
mimedb: $(MIME_OBJS) mimedb: $(MIME_OBJS)
$(CC) $(MIME_OBJS) $(LDFLAGS_MIMEDB) -o $@ $(CXX) $(MIME_OBJS) $(LDFLAGS_MIMEDB) -o $@
# #
# Build the set_color program # Build the set_color program
# #
set_color: set_color.o print_help.o common.o set_color: set_color.o print_help.o common.o color.o wutil.o
$(CC) set_color.o print_help.o common.o wutil.o $(LDFLAGS_SET_COLOR) -o $@ $(CXX) set_color.o print_help.o common.o wutil.o color.o $(LDFLAGS_SET_COLOR) -o $@
# #
# Test program for the tokenizer library # Test program for the tokenizer library
# #
tokenizer_test: tokenizer.c tokenizer.h wutil.o common.o tokenizer_test: tokenizer.cpp tokenizer.h wutil.o common.o
$(CC) $(CFLAGS) tokenizer.c wutil.o common.o -D TOKENIZER_TEST $(LDFLAGS) -o $@ $(CXX) $(CXXFLAGS) tokenizer.cpp wutil.o common.o -D TOKENIZER_TEST $(LDFLAGS) -o $@
# #
@@ -751,25 +796,31 @@ tokenizer_test: tokenizer.c tokenizer.h wutil.o common.o
# #
fish_indent: $(FISH_INDENT_OBJS) fish_indent: $(FISH_INDENT_OBJS)
$(CC) $(FISH_INDENT_OBJS) $(LDFLAGS_FISH_INDENT) -o $@ $(CXX) $(FISH_INDENT_OBJS) $(LDFLAGS_FISH_INDENT) -o $@
# #
# Neat little program to show output from terminal # Neat little program to show output from terminal
# #
key_reader: key_reader.o input_common.o common.o env_universal.o env_universal_common.o wutil.o key_reader: key_reader.o input_common.o common.o env_universal.o env_universal_common.o wutil.o iothread.o
$(CC) key_reader.o input_common.o common.o env_universal.o env_universal_common.o wutil.o $(LDFLAGS_FISH) -o $@ $(CXX) key_reader.o input_common.o common.o env_universal.o env_universal_common.o wutil.o iothread.o $(LDFLAGS_FISH) -o $@
# #
# Update dependencies # Update dependencies
# #
depend: depend:
makedepend -fMakefile.in -Y *.c makedepend -fMakefile.in -Y *.cpp
./config.status ./config.status
.PHONY: depend .PHONY: depend
#
# Dependencies that makedepend misses
#
autoload.cpp function.cpp: $(GENERATED_INTERN_SCRIPT_FILES)
# #
# Copy all the source files into a new directory and use tar to create # Copy all the source files into a new directory and use tar to create
# an archive from it. Simplest way I could think of to make an archive # an archive from it. Simplest way I could think of to make an archive
@@ -877,7 +928,8 @@ distclean: clean
# #
clean: clean:
rm -f *.o doc.h doc.tmp doc_src/*.doxygen doc_src/*.c doc_src/*.o doc_src/commands.hdr rm -f *.o doc.h doc.tmp doc_src/*.doxygen doc_src/*.cpp doc_src/*.o doc_src/commands.hdr
rm -f $(GENERATED_INTERN_SCRIPT_FILES)
rm -f tests/tmp.err tests/tmp.out tests/tmp.status tests/foo.txt rm -f tests/tmp.err tests/tmp.out tests/tmp.status tests/foo.txt
rm -f $(PROGRAMS) fish_tests tokenizer_test key_reader rm -f $(PROGRAMS) fish_tests tokenizer_test key_reader
rm -f share/config.fish etc/config.fish doc_src/index.hdr doc_src/commands.hdr rm -f share/config.fish etc/config.fish doc_src/index.hdr doc_src/commands.hdr
@@ -893,122 +945,130 @@ clean:
# DO NOT DELETE THIS LINE -- make depend depends on it. # DO NOT DELETE THIS LINE -- make depend depends on it.
builtin.o: config.h fallback.h util.h wutil.h builtin.h io.h function.h autoload.o: config.h autoload.h common.h util.h lru.h wutil.h signal.h env.h
builtin.o: complete.h proc.h parser.h event.h reader.h env.h common.h autoload.o: builtin_scripts.h exec.h proc.h io.h
builtin.o: wgetopt.h sanity.h tokenizer.h wildcard.h input_common.h input.h builtin.o: config.h signal.h fallback.h util.h wutil.h builtin.h io.h
builtin.o: intern.h signal.h exec.h highlight.h halloc.h halloc_util.h builtin.o: common.h function.h event.h complete.h proc.h parser.h reader.h
builtin.o: parse_util.h parser_keywords.h expand.h path.h builtin_set.c builtin.o: env.h wgetopt.h sanity.h tokenizer.h wildcard.h input_common.h
builtin.o: builtin_commandline.c builtin_complete.c builtin_ulimit.c builtin.o: input.h intern.h exec.h highlight.h screen.h color.h parse_util.h
builtin.o: builtin_jobs.c builtin.o: autoload.h lru.h parser_keywords.h expand.h path.h builtin_set.cpp
builtin.o: builtin_commandline.cpp builtin_complete.cpp builtin_ulimit.cpp
builtin.o: builtin_jobs.cpp
builtin_commandline.o: config.h signal.h fallback.h util.h wutil.h builtin.h builtin_commandline.o: config.h signal.h fallback.h util.h wutil.h builtin.h
builtin_commandline.o: io.h common.h wgetopt.h reader.h proc.h parser.h builtin_commandline.o: io.h common.h wgetopt.h reader.h proc.h parser.h
builtin_commandline.o: event.h tokenizer.h input_common.h input.h builtin_commandline.o: event.h function.h tokenizer.h input_common.h input.h
builtin_commandline.o: parse_util.h builtin_commandline.o: parse_util.h autoload.h lru.h
builtin_complete.o: config.h signal.h fallback.h util.h wutil.h builtin.h builtin_complete.o: config.h signal.h fallback.h util.h wutil.h builtin.h
builtin_complete.o: io.h common.h complete.h wgetopt.h parser.h proc.h builtin_complete.o: io.h common.h complete.h wgetopt.h parser.h proc.h
builtin_complete.o: event.h reader.h builtin_complete.o: event.h function.h reader.h
builtin_jobs.o: config.h fallback.h util.h wutil.h builtin.h io.h proc.h builtin_jobs.o: config.h fallback.h signal.h util.h wutil.h builtin.h io.h
builtin_jobs.o: parser.h event.h common.h wgetopt.h builtin_jobs.o: common.h proc.h parser.h event.h function.h wgetopt.h
builtin_scripts.o: builtin_scripts.h
builtin_set.o: config.h signal.h fallback.h util.h wutil.h builtin.h io.h builtin_set.o: config.h signal.h fallback.h util.h wutil.h builtin.h io.h
builtin_set.o: env.h expand.h common.h wgetopt.h proc.h parser.h event.h builtin_set.o: common.h env.h expand.h wgetopt.h proc.h parser.h event.h
builtin_ulimit.o: config.h fallback.h util.h builtin.h io.h common.h builtin_set.o: function.h
builtin_ulimit.o: config.h fallback.h signal.h util.h builtin.h io.h common.h
builtin_ulimit.o: wgetopt.h builtin_ulimit.o: wgetopt.h
common.o: config.h fallback.h util.h wutil.h common.h expand.h proc.h io.h color.o: color.h config.h common.h util.h
common.o: wildcard.h parser.h event.h util.c halloc.c halloc.h halloc_util.c common.o: config.h fallback.h signal.h util.h wutil.h common.h expand.h
common.o: fallback.c common.o: proc.h io.h wildcard.h parser.h event.h function.h complete.h
complete.o: config.h signal.h fallback.h util.h tokenizer.h wildcard.h proc.h common.o: util.cpp fallback.cpp
complete.o: io.h parser.h event.h function.h complete.h builtin.h env.h complete.o: config.h signal.h fallback.h util.h tokenizer.h wildcard.h
complete.o: exec.h expand.h common.h reader.h history.h intern.h parse_util.h complete.o: common.h proc.h io.h parser.h event.h function.h complete.h
complete.o: parser_keywords.h halloc.h halloc_util.h wutil.h path.h complete.o: builtin.h env.h exec.h expand.h reader.h history.h intern.h
complete.o: parse_util.h autoload.h lru.h parser_keywords.h wutil.h path.h
complete.o: builtin_scripts.h
env.o: config.h signal.h fallback.h util.h wutil.h proc.h io.h common.h env.h env.o: config.h signal.h fallback.h util.h wutil.h proc.h io.h common.h env.h
env.o: sanity.h expand.h history.h reader.h parser.h event.h env_universal.h env.o: sanity.h expand.h history.h reader.h parser.h event.h function.h
env.o: env_universal_common.h input_common.h path.h halloc.h halloc_util.h env.o: env_universal.h env_universal_common.h input_common.h path.h
env.o: complete.h env.o: complete.h
env_universal.o: config.h signal.h fallback.h util.h common.h wutil.h env_universal.o: config.h signal.h fallback.h util.h common.h wutil.h
env_universal.o: env_universal_common.h env_universal.h env_universal.o: env_universal_common.h env_universal.h
env_universal_common.o: config.h signal.h fallback.h util.h common.h wutil.h env_universal_common.o: config.h signal.h fallback.h util.h common.h wutil.h
env_universal_common.o: env_universal_common.h env_universal_common.o: env_universal_common.h
event.o: config.h signal.h fallback.h util.h wutil.h function.h proc.h io.h event.o: config.h signal.h fallback.h util.h wutil.h function.h common.h
event.o: parser.h event.h common.h halloc_util.h event.o: event.h proc.h io.h parser.h
exec.o: config.h signal.h fallback.h util.h common.h wutil.h proc.h io.h exec.o: config.h signal.h fallback.h util.h common.h wutil.h proc.h io.h
exec.o: exec.h parser.h event.h builtin.h function.h env.h wildcard.h exec.o: exec.h parser.h event.h function.h builtin.h env.h wildcard.h
exec.o: sanity.h expand.h halloc.h halloc_util.h parse_util.h exec.o: sanity.h expand.h parse_util.h autoload.h lru.h
expand.o: config.h signal.h fallback.h util.h common.h wutil.h env.h proc.h expand.o: config.h signal.h fallback.h util.h common.h wutil.h env.h proc.h
expand.o: io.h parser.h event.h expand.h wildcard.h exec.h tokenizer.h expand.o: io.h parser.h event.h function.h expand.h wildcard.h exec.h
expand.o: complete.h parse_util.h halloc.h halloc_util.h expand.o: tokenizer.h complete.h parse_util.h autoload.h lru.h
fallback.o: config.h fallback.h util.h fallback.o: config.h fallback.h signal.h util.h
fish.o: config.h signal.h fallback.h util.h common.h reader.h io.h builtin.h fish.o: config.h signal.h fallback.h util.h common.h reader.h io.h builtin.h
fish.o: function.h complete.h wutil.h env.h sanity.h proc.h parser.h event.h fish.o: function.h event.h complete.h wutil.h env.h sanity.h proc.h parser.h
fish.o: expand.h intern.h exec.h output.h halloc.h halloc_util.h history.h fish.o: expand.h intern.h exec.h output.h screen.h color.h history.h path.h
fish.o: path.h fish_indent.o: config.h fallback.h signal.h util.h common.h wutil.h
fish_indent.o: config.h fallback.h util.h common.h wutil.h halloc.h fish_indent.o: tokenizer.h print_help.h parser_keywords.h
fish_indent.o: halloc_util.h tokenizer.h print_help.h parser_keywords.h
fish_pager.o: config.h signal.h fallback.h util.h wutil.h common.h complete.h fish_pager.o: config.h signal.h fallback.h util.h wutil.h common.h complete.h
fish_pager.o: output.h input_common.h env_universal.h env_universal_common.h fish_pager.o: output.h screen.h color.h input_common.h env_universal.h
fish_pager.o: halloc.h halloc_util.h print_help.h fish_pager.o: env_universal_common.h print_help.h
fish_tests.o: config.h signal.h fallback.h util.h common.h proc.h io.h fish_tests.o: config.h signal.h fallback.h util.h common.h proc.h io.h
fish_tests.o: reader.h builtin.h function.h complete.h wutil.h env.h expand.h fish_tests.o: reader.h builtin.h function.h event.h autoload.h lru.h
fish_tests.o: parser.h event.h tokenizer.h output.h exec.h path.h halloc.h fish_tests.o: complete.h wutil.h env.h expand.h parser.h tokenizer.h output.h
fish_tests.o: halloc_util.h fish_tests.o: screen.h color.h exec.h path.h history.h
fishd.o: config.h signal.h fallback.h util.h common.h wutil.h fishd.o: config.h signal.h fallback.h util.h common.h wutil.h
fishd.o: env_universal_common.h halloc.h halloc_util.h path.h print_help.h fishd.o: env_universal_common.h path.h print_help.h
function.o: config.h signal.h wutil.h fallback.h util.h function.h proc.h function.o: config.h signal.h wutil.h fallback.h util.h function.h common.h
function.o: io.h parser.h event.h common.h intern.h reader.h parse_util.h function.o: event.h proc.h io.h parser.h intern.h reader.h parse_util.h
function.o: parser_keywords.h env.h expand.h halloc.h halloc_util.h function.o: autoload.h lru.h parser_keywords.h env.h expand.h
halloc.o: config.h fallback.h util.h common.h halloc.h function.o: builtin_scripts.h
halloc_util.o: config.h fallback.h util.h common.h halloc.h highlight.o: config.h signal.h fallback.h util.h wutil.h highlight.h env.h
highlight.o: config.h signal.h fallback.h util.h wutil.h highlight.h highlight.o: common.h screen.h color.h tokenizer.h proc.h io.h parser.h
highlight.o: tokenizer.h proc.h io.h parser.h event.h parse_util.h highlight.o: event.h function.h parse_util.h autoload.h lru.h
highlight.o: parser_keywords.h builtin.h function.h env.h expand.h sanity.h highlight.o: parser_keywords.h builtin.h expand.h sanity.h complete.h
highlight.o: common.h complete.h output.h halloc.h halloc_util.h wildcard.h highlight.o: output.h wildcard.h path.h
highlight.o: path.h history.o: config.h fallback.h signal.h util.h sanity.h wutil.h history.h
history.o: config.h fallback.h util.h wutil.h history.h common.h halloc.h history.o: common.h intern.h path.h autoload.h lru.h
history.o: halloc_util.h intern.h path.h signal.h input.o: config.h signal.h fallback.h util.h wutil.h reader.h io.h common.h
input.o: config.h signal.h fallback.h util.h wutil.h reader.h io.h proc.h input.o: proc.h sanity.h input_common.h input.h parser.h event.h function.h
input.o: common.h sanity.h input_common.h input.h parser.h event.h env.h input.o: env.h expand.h output.h screen.h color.h intern.h
input.o: expand.h output.h intern.h halloc.h halloc_util.h input_common.o: config.h fallback.h signal.h util.h common.h wutil.h
input_common.o: config.h fallback.h util.h common.h wutil.h input_common.h input_common.o: input_common.h env_universal.h env_universal_common.h
input_common.o: env_universal.h env_universal_common.h input_common.o: iothread.h
intern.o: config.h fallback.h util.h wutil.h common.h intern.h intern.o: config.h fallback.h signal.h util.h wutil.h common.h intern.h
io.o: config.h fallback.h util.h wutil.h exec.h proc.h io.h common.h halloc.h io.o: config.h fallback.h signal.h util.h wutil.h exec.h proc.h io.h common.h
key_reader.o: config.h fallback.h input_common.h iothread.o: iothread.h signal.h
key_reader.o: config.h fallback.h signal.h input_common.h
kill.o: config.h signal.h fallback.h util.h wutil.h kill.h proc.h io.h kill.o: config.h signal.h fallback.h util.h wutil.h kill.h proc.h io.h
kill.o: sanity.h common.h env.h exec.h halloc.h path.h kill.o: common.h sanity.h env.h exec.h path.h
mimedb.o: config.h xdgmime.h fallback.h util.h print_help.h mimedb.o: config.h xdgmime.h fallback.h signal.h util.h print_help.h
output.o: config.h signal.h fallback.h util.h wutil.h expand.h common.h output.o: config.h signal.h fallback.h util.h wutil.h expand.h common.h
output.o: output.h halloc_util.h highlight.h output.o: output.h screen.h color.h highlight.h env.h
parse_util.o: config.h fallback.h util.h wutil.h common.h tokenizer.h parse_util.o: config.h fallback.h signal.h util.h wutil.h common.h
parse_util.o: parse_util.h expand.h intern.h exec.h proc.h io.h env.h parse_util.o: tokenizer.h parse_util.h autoload.h lru.h expand.h intern.h
parse_util.o: signal.h wildcard.h halloc_util.h parse_util.o: exec.h proc.h io.h env.h wildcard.h
parser.o: config.h signal.h fallback.h util.h common.h wutil.h proc.h io.h parser.o: config.h signal.h fallback.h util.h common.h wutil.h proc.h io.h
parser.o: parser.h event.h parser_keywords.h tokenizer.h exec.h wildcard.h parser.o: parser.h event.h function.h parser_keywords.h tokenizer.h exec.h
parser.o: function.h builtin.h env.h expand.h reader.h sanity.h parser.o: wildcard.h builtin.h env.h expand.h reader.h sanity.h
parser.o: env_universal.h env_universal_common.h intern.h parse_util.h parser.o: env_universal.h env_universal_common.h intern.h parse_util.h
parser.o: halloc.h halloc_util.h path.h parser.o: autoload.h lru.h path.h complete.h
parser_keywords.o: config.h fallback.h common.h util.h parser_keywords.h parser_keywords.o: config.h fallback.h signal.h common.h util.h
path.o: config.h fallback.h util.h common.h env.h wutil.h halloc.h parser_keywords.o: parser_keywords.h
path.o: halloc_util.h path.h expand.h path.o: config.h fallback.h signal.h util.h common.h env.h wutil.h path.h
path.o: expand.h
print_help.o: print_help.h print_help.o: print_help.h
proc.o: config.h signal.h fallback.h util.h wutil.h proc.h io.h common.h proc.o: config.h signal.h fallback.h util.h wutil.h proc.h io.h common.h
proc.o: reader.h sanity.h env.h parser.h event.h halloc.h halloc_util.h proc.o: reader.h sanity.h env.h parser.h event.h function.h output.h screen.h
proc.o: output.h proc.o: color.h
reader.o: config.h signal.h fallback.h util.h wutil.h highlight.h reader.h reader.o: config.h signal.h fallback.h util.h wutil.h highlight.h env.h
reader.o: io.h proc.h parser.h event.h complete.h history.h common.h sanity.h reader.o: common.h screen.h color.h reader.h io.h proc.h parser.h event.h
reader.o: env.h exec.h expand.h tokenizer.h kill.h input_common.h input.h reader.o: function.h complete.h history.h sanity.h exec.h expand.h
reader.o: function.h output.h screen.h halloc.h halloc_util.h parse_util.h reader.o: tokenizer.h kill.h input_common.h input.h output.h iothread.h
reader.o: intern.h parse_util.h autoload.h lru.h
sanity.o: config.h signal.h fallback.h util.h common.h sanity.h proc.h io.h sanity.o: config.h signal.h fallback.h util.h common.h sanity.h proc.h io.h
sanity.o: history.h reader.h kill.h wutil.h sanity.o: history.h reader.h kill.h wutil.h
screen.o: config.h fallback.h common.h util.h wutil.h output.h highlight.h screen.o: config.h fallback.h signal.h common.h util.h wutil.h output.h
screen.o: screen.h env.h screen.o: screen.h color.h highlight.h env.h
set_color.o: config.h fallback.h print_help.h set_color.o: config.h fallback.h signal.h print_help.h
signal.o: config.h signal.h common.h util.h fallback.h wutil.h event.h signal.o: config.h signal.h common.h util.h fallback.h wutil.h event.h
signal.o: reader.h io.h proc.h signal.o: reader.h io.h proc.h
tokenizer.o: config.h fallback.h util.h wutil.h tokenizer.h common.h tokenizer.o: config.h fallback.h signal.h util.h wutil.h tokenizer.h common.h
util.o: config.h fallback.h util.h common.h wutil.h util.o: config.h fallback.h signal.h util.h common.h wutil.h
wgetopt.o: config.h wgetopt.h wutil.h fallback.h wgetopt.o: config.h wgetopt.h wutil.h fallback.h signal.h
wildcard.o: config.h fallback.h util.h wutil.h complete.h common.h wildcard.h wildcard.o: config.h fallback.h signal.h util.h wutil.h complete.h common.h
wildcard.o: reader.h io.h expand.h exec.h proc.h halloc_util.h wildcard.o: wildcard.h reader.h io.h expand.h exec.h proc.h
wutil.o: config.h fallback.h util.h common.h wutil.h halloc.h halloc_util.h wutil.o: config.h fallback.h signal.h util.h common.h wutil.h
xdgmime.o: xdgmime.h xdgmimeint.h xdgmimeglob.h xdgmimemagic.h xdgmimealias.h xdgmime.o: xdgmime.h xdgmimeint.h xdgmimeglob.h xdgmimemagic.h xdgmimealias.h
xdgmime.o: xdgmimeparent.h xdgmime.o: xdgmimeparent.h
xdgmimealias.o: xdgmimealias.h xdgmime.h xdgmimeint.h xdgmimealias.o: xdgmimealias.h xdgmime.h xdgmimeint.h

342
autoload.cpp Normal file
View File

@@ -0,0 +1,342 @@
/** \file autoload.cpp
The classes responsible for autoloading functions and completions.
*/
#include "config.h"
#include "autoload.h"
#include "wutil.h"
#include "common.h"
#include "signal.h"
#include "env.h"
#include "builtin_scripts.h"
#include "exec.h"
#include <assert.h>
#include <algorithm>
/* The time before we'll recheck an autoloaded file */
static const int kAutoloadStalenessInterval = 15;
file_access_attempt_t access_file(const wcstring &path, int mode) {
//printf("Touch %ls\n", path.c_str());
file_access_attempt_t result = {0};
struct stat statbuf;
if (wstat(path, &statbuf)) {
result.error = errno;
} else {
result.mod_time = statbuf.st_mtime;
if (waccess(path, mode)) {
result.error = errno;
} else {
result.accessible = true;
}
}
// Note that we record the last checked time after the call, on the assumption that in a slow filesystem, the lag comes before the kernel check, not after.
result.stale = false;
result.last_checked = time(NULL);
return result;
}
autoload_t::autoload_t(const wcstring &env_var_name_var, const builtin_script_t * const scripts, size_t script_count) :
env_var_name(env_var_name_var),
builtin_scripts(scripts),
builtin_script_count(script_count)
{
pthread_mutex_init(&lock, NULL);
}
autoload_t::~autoload_t() {
pthread_mutex_destroy(&lock);
}
void autoload_t::node_was_evicted(autoload_function_t *node) {
// This should only ever happen on the main thread
ASSERT_IS_MAIN_THREAD();
// Tell ourselves that the command was removed if it was loaded
if (! node->is_loaded)
this->command_removed(node->key);
delete node;
}
int autoload_t::unload( const wcstring &cmd )
{
return this->evict_node(cmd);
}
int autoload_t::load( const wcstring &cmd, bool reload )
{
int res;
CHECK_BLOCK( 0 );
ASSERT_IS_MAIN_THREAD();
env_var_t path_var = env_get_string( env_var_name );
/*
Do we know where to look?
*/
if( path_var.empty() )
return 0;
/* Check if the lookup path has changed. If so, drop all loaded files. path_var may only be inspected on the main thread. */
if( path_var != this->last_path )
{
this->last_path = path_var;
scoped_lock locker(lock);
this->evict_all_nodes();
}
/** Warn and fail on infinite recursion. It's OK to do this because this function is only called on the main thread. */
if (this->is_loading(cmd))
{
debug( 0,
_( L"Could not autoload item '%ls', it is already being autoloaded. "
L"This is a circular dependency in the autoloading scripts, please remove it."),
cmd.c_str() );
return 1;
}
/* Mark that we're loading this */
is_loading_set.insert(cmd);
/* Get the list of paths from which we will try to load */
std::vector<wcstring> path_list;
tokenize_variable_array( path_var, path_list );
/* Try loading it */
res = this->locate_file_and_maybe_load_it( cmd, true, reload, path_list );
/* Clean up */
int erased = is_loading_set.erase(cmd);
assert(erased);
return res;
}
bool autoload_t::can_load( const wcstring &cmd, const env_vars &vars )
{
const wchar_t *path_var_ptr = vars.get(env_var_name.c_str());
if (! path_var_ptr || ! path_var_ptr[0])
return false;
const wcstring path_var(path_var_ptr);
std::vector<wcstring> path_list;
tokenize_variable_array( path_var, path_list );
return this->locate_file_and_maybe_load_it( cmd, false, false, path_list );
}
static bool script_name_precedes_script_name(const builtin_script_t &script1, const builtin_script_t &script2)
{
return wcscmp(script1.name, script2.name) < 0;
}
void autoload_t::unload_all(void) {
scoped_lock locker(lock);
this->evict_all_nodes();
}
/** Check whether the given command is loaded. */
bool autoload_t::has_tried_loading( const wcstring &cmd )
{
scoped_lock locker(lock);
autoload_function_t * func = this->get_node(cmd);
return func != NULL;
}
static bool is_stale(const autoload_function_t *func) {
/** Return whether this function is stale. Internalized functions can never be stale. */
return ! func->is_internalized && time(NULL) - func->access.last_checked > kAutoloadStalenessInterval;
}
autoload_function_t *autoload_t::get_autoloaded_function_with_creation(const wcstring &cmd, bool allow_eviction)
{
ASSERT_IS_LOCKED(lock);
autoload_function_t *func = this->get_node(cmd);
if (! func) {
func = new autoload_function_t(cmd);
if (allow_eviction) {
this->add_node(func);
} else {
this->add_node_without_eviction(func);
}
}
return func;
}
/**
This internal helper function does all the real work. By using two
functions, the internal function can return on various places in
the code, and the caller can take care of various cleanup work.
cmd: the command name ('grep')
really_load: whether to actually parse it as a function, or just check it it exists
reload: whether to reload it if it's already loaded
path_list: the set of paths to check
Result: if really_load is true, returns whether the function was loaded. Otherwise returns whether the function existed.
*/
bool autoload_t::locate_file_and_maybe_load_it( const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list )
{
/* Note that we are NOT locked in this function! */
size_t i;
bool reloaded = 0;
/* Try using a cached function. If we really want the function to be loaded, require that it be really loaded. If we're not reloading, allow stale functions. */
{
bool allow_stale_functions = ! reload;
/* Take a lock */
scoped_lock locker(lock);
/* Get the function */
autoload_function_t * func = this->get_node(cmd);
/* Determine if we can use this cached function */
bool use_cached;
if (! func) {
/* Can't use a function that doesn't exist */
use_cached = false;
} else if ( ! allow_stale_functions && is_stale(func)) {
/* Can't use a stale function */
use_cached = false;
} else if (really_load && ! func->is_placeholder && ! func->is_loaded) {
/* Can't use an unloaded function */
use_cached = false;
} else {
/* I guess we can use it */
use_cached = true;
}
/* If we can use this function, return whether we were able to access it */
if (use_cached) {
return func->is_internalized || func->access.accessible;
}
}
/* The source of the script will end up here */
wcstring script_source;
bool has_script_source = false;
/* Whether we found an accessible file */
bool found_file = false;
/* Look for built-in scripts via a binary search */
const builtin_script_t *matching_builtin_script = NULL;
if (builtin_script_count > 0)
{
const builtin_script_t test_script = {cmd.c_str(), NULL};
const builtin_script_t *array_end = builtin_scripts + builtin_script_count;
const builtin_script_t *found = std::lower_bound(builtin_scripts, array_end, test_script, script_name_precedes_script_name);
if (found != array_end && ! wcscmp(found->name, test_script.name))
{
/* We found it */
matching_builtin_script = found;
}
}
if (matching_builtin_script) {
has_script_source = true;
script_source = str2wcstring(matching_builtin_script->def);
/* Make a node representing this function */
scoped_lock locker(lock);
autoload_function_t *func = this->get_autoloaded_function_with_creation(cmd, really_load);
/* This function is internalized */
func->is_internalized = true;
/* It's a fiction to say the script is loaded at this point, but we're definitely going to load it down below. */
if (really_load) func->is_loaded = true;
}
if (! has_script_source)
{
/* Iterate over path searching for suitable completion files */
for( i=0; i<path_list.size(); i++ )
{
wcstring next = path_list.at(i);
wcstring path = next + L"/" + cmd + L".fish";
const file_access_attempt_t access = access_file(path, R_OK);
if (access.accessible) {
/* Found it! */
found_file = true;
/* Now we're actually going to take the lock. */
scoped_lock locker(lock);
autoload_function_t *func = this->get_node(cmd);
/* Generate the source if we need to load it */
bool need_to_load_function = really_load && (func == NULL || func->access.mod_time == access.mod_time || ! func->is_loaded);
if (need_to_load_function) {
/* Generate the script source */
wcstring esc = escape_string(path, 1);
script_source = L". " + esc;
has_script_source = true;
/* Remove any loaded command because we are going to reload it. Note that this will deadlock if command_removed calls back into us. */
if (func && func->is_loaded) {
command_removed(cmd);
func->is_placeholder = false;
}
/* Mark that we're reloading it */
reloaded = true;
}
/* Create the function if we haven't yet. This does not load it. Do not trigger eviction unless we are actually loading, because we don't want to evict off of the main thread. */
if (! func) {
func = get_autoloaded_function_with_creation(cmd, really_load);
}
/* It's a fiction to say the script is loaded at this point, but we're definitely going to load it down below. */
if (need_to_load_function) func->is_loaded = true;
/* Unconditionally record our access time */
func->access = access;
break;
}
}
/*
If no file or builtin script was found we insert a placeholder function.
Later we only research if the current time is at least five seconds later.
This way, the files won't be searched over and over again.
*/
if( ! found_file && ! has_script_source )
{
scoped_lock locker(lock);
/* Generate a placeholder */
autoload_function_t *func = this->get_node(cmd);
if (! func) {
func = new autoload_function_t(cmd);
func->is_placeholder = true;
if (really_load) {
this->add_node(func);
} else {
this->add_node_without_eviction(func);
}
}
func->access.last_checked = time(NULL);
}
}
/* If we have a script, either built-in or a file source, then run it */
if (really_load && has_script_source)
{
if( exec_subshell( script_source) == -1 )
{
/*
Do nothing on failiure
*/
}
}
if (really_load) {
return reloaded;
} else {
return found_file || has_script_source;
}
}

129
autoload.h Normal file
View File

@@ -0,0 +1,129 @@
/** \file autoload.h
The classes responsible for autoloading functions and completions.
*/
#ifndef FISH_AUTOLOAD_H
#define FISH_AUTOLOAD_H
#include <wchar.h>
#include <map>
#include <set>
#include <list>
#include "common.h"
#include "lru.h"
/** A struct responsible for recording an attempt to access a file. */
struct file_access_attempt_t {
time_t mod_time; /** The modification time of the file */
time_t last_checked; /** When we last checked the file */
bool accessible; /** Whether we believe we could access this file */
bool stale; /** Whether the access attempt is stale */
int error; /** If we could not access the file, the error code */
};
file_access_attempt_t access_file(const wcstring &path, int mode);
struct autoload_function_t : public lru_node_t
{
autoload_function_t(const wcstring &key) : lru_node_t(key), access(), is_loaded(false), is_placeholder(false), is_internalized(false) { }
file_access_attempt_t access; /** The last access attempt */
bool is_loaded; /** Whether we have actually loaded this function */
bool is_placeholder; /** Whether we are a placeholder that stands in for "no such function". If this is true, then is_loaded must be false. */
bool is_internalized; /** Whether this function came from a builtin "internalized" script */
};
struct builtin_script_t;
class env_vars;
/**
A class that represents a path from which we can autoload, and the autoloaded contents.
*/
class autoload_t : private lru_cache_t<autoload_function_t> {
private:
/** Lock for thread safety */
pthread_mutex_t lock;
/** The environment variable name */
const wcstring env_var_name;
/** Builtin script array */
const struct builtin_script_t *const builtin_scripts;
/** Builtin script count */
const size_t builtin_script_count;
/** The path from which we most recently autoloaded */
wcstring last_path;
/**
A table containing all the files that are currently being
loaded. This is here to help prevent recursion.
*/
std::set<wcstring> is_loading_set;
bool is_loading(const wcstring &name) const {
return is_loading_set.find(name) != is_loading_set.end();
}
void remove_all_functions(void) {
this->evict_all_nodes();
}
bool locate_file_and_maybe_load_it( const wcstring &cmd, bool really_load, bool reload, const wcstring_list_t &path_list );
virtual void node_was_evicted(autoload_function_t *node);
autoload_function_t *get_autoloaded_function_with_creation(const wcstring &cmd, bool allow_eviction);
protected:
/** Overridable callback for when a command is removed */
virtual void command_removed(const wcstring &cmd) { }
public:
/** Create an autoload_t for the given environment variable name */
autoload_t(const wcstring &env_var_name_var, const builtin_script_t *scripts, size_t script_count );
/** Destructor */
virtual ~autoload_t();
/**
Autoload the specified file, if it exists in the specified path. Do
not load it multiple times unless it's timestamp changes or
parse_util_unload is called.
Autoloading one file may unload another.
\param cmd the filename to search for. The suffix '.fish' is always added to this name
\param on_unload a callback function to run if a suitable file is found, which has not already been run. unload will also be called for old files which are unloaded.
\param reload wheter to recheck file timestamps on already loaded files
*/
int load( const wcstring &cmd, bool reload );
/** Check whether we have tried loading the given command. Does not do any I/O. */
bool has_tried_loading( const wcstring &cmd );
/**
Tell the autoloader that the specified file, in the specified path,
is no longer loaded.
\param cmd the filename to search for. The suffix '.fish' is always added to this name
\param on_unload a callback function which will be called before (re)loading a file, may be used to unload the previous file.
\return non-zero if the file was removed, zero if the file had not yet been loaded
*/
int unload( const wcstring &cmd );
/**
Unloads all files.
*/
void unload_all( );
/** Check whether the given command could be loaded, but do not load it. */
bool can_load( const wcstring &cmd, const env_vars &vars );
};
#endif

View File

@@ -0,0 +1,3 @@
This is the_ridiculous'fish s delightful fork of, fish friendly interactive shell. For more information, visit http://ridiculousfish.com/shell/ .
This installer will install fish, but will not modify your /etc/shells file or your default shell. I trust you know how to do that yourself if you care to!

View File

@@ -0,0 +1 @@
<pkg-contents spec="1.12"/>

View File

@@ -0,0 +1 @@
<pkgref spec="1.12" uuid="6A7E1ED5-E40E-4ACE-959B-B9D77555AD39"><config><identifier>com.ridiculousfish.fish-shell.fishShell.fish_pkg.pkg</identifier><version>1.0</version><description></description><post-install type="none"/><requireAuthorization/><installFrom>/private/tmp/fish_pkg</installFrom><installTo>/</installTo><flags><followSymbolicLinks/></flags><packageStore type="internal"></packageStore><mod>parent</mod><mod>installTo</mod></config></pkgref>

View File

@@ -0,0 +1,40 @@
<pkmkdoc spec="1.12"><properties><title>fish shell</title><organization>com.ridiculousfish.fish-shell</organization><userSees ui="easy"/><min-target os="3"/><domain anywhere="true"/></properties><distribution><versions min-spec="1.000000"/><scripts></scripts></distribution><contents><choice title="fish_pkg" id="fish shell" starts_selected="true" starts_enabled="true" starts_hidden="false"><pkgref id="com.ridiculousfish.fish-shell.fishShell.fish_pkg.pkg"/></choice></contents><resources bg-scale="proportional" bg-align="bottomleft"><locale lang="en"><resource mod="true" type="background">/Users/peter/Desktop/Untitled.pdf</resource><resource mime-type="text/rtf" kind="embedded" type="welcome"><![CDATA[{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs30 \cf0 This installs ridiculous_fish's fork of fish, a command line shell for Mac OS X, Linux, and the rest of the family. For more information on fish, visit {\field{\*\fldinst{HYPERLINK "http://ridiculousfish.com/shell/"}}{\fldrslt http://ridiculousfish.com/shell/}}\
\
The path to fish will be added to /etc/shells, but your default shell will not be modified. \
\
This release is beta r2.\
}]]></resource><resource mime-type="text/rtf" kind="embedded" type="conclusion"><![CDATA[{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf470
{\fonttbl\f0\fswiss\fcharset0 Helvetica;\f1\fnil\fcharset0 Monaco;}
{\colortbl;\red255\green255\blue255;}
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs26 \cf0 Run
\f1 fish
\f0 at the command line to start it up! Some useful commands:\
\
Interactively set your colors from a web page:\
\
\f1 fish_config
\f0 \
\
Update man-page completions:\
\
\f1 fish_update_completions
\f0 \
\
Make fish your default shell:\
\
\f1 chsh -s /usr/local/bin/fish\
\f0 \
\
Enjoy!\
}]]></resource></locale></resources><flags/><item type="file">01fish.xml</item><mod>properties.title</mod><mod>description</mod></pkmkdoc>

18
build_tools/make_deb.sh Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/sh
# Terminate on error
set -e
sudo rm -Rf /tmp/fishfish
mkdir /tmp/fishfish
git archive --format=tar fish_fish | tar -x -C /tmp/fishfish
mkdir /tmp/fishfish/doc-pak
cp README INSTALL CHANGELOG release_notes.html /tmp/fishfish/doc-pak/
cp build_tools/description-pak /tmp/fishfish/
cd /tmp/fishfish
autoconf
./configure
make -j 3
sudo checkinstall --default --pakdir ~/fish_built/ --pkgversion 0.9 make install
mv ~/fish_built/fishfish_0.9-1_i386.deb ~/fish_built/fishfish_0.9_i386.deb

31
build_tools/make_pkg.sh Executable file
View File

@@ -0,0 +1,31 @@
#!/bin/sh -x
make distclean
rm -rf /tmp/fish_pkg
mkdir -p /tmp/fish_pkg/
# Make sure what we build can run on SnowLeopard
export OSX_SDK="/Developer/SDKs/MacOSX10.6.sdk"
export MACOSX_DEPLOYMENT_TARGET="10.6"
export CC="clang -isysroot $OSX_SDK -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
export CCX="clang++ -isysroot $OSX_SDK -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
export CFLAGS="$CFLAGS -isysroot $OSX_SDK -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
export CXXFLAGS="$CXXFLAGS -isysroot $OSX_SDK -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
export LDFLAGS="$LDFLAGS -isysroot $OSX_SDK -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET"
autoconf
./configure --without-xsel
# Actually build it now
if make -j 4 DESTDIR=/tmp/fish_pkg install
then
echo "Root written to /tmp/fish_pkg/"
if /Developer/usr/bin/packagemaker --doc ./build_tools/fish_shell.pmdoc --out ~/fish_built/fishfish.pkg
then
echo "Package written to ~/fish_built/fishfish.pkg"
else
echo "Package could not be written"
fi
else
echo "Root could not be written"
fi

9
build_tools/make_tarball.sh Executable file
View File

@@ -0,0 +1,9 @@
#!/bin/sh
rm -f ~/fish_built/fishfish.tar.gz
if git archive --format=tar --prefix=fishfish/ fish_fish | gzip - > ~/fish_built/fishfish.tar.gz
then
echo "Tarball written to ~/fish_built/fishfish.tar.gz"
else
echo "Tarball could not be written"
fi

View File

@@ -0,0 +1,48 @@
#!/bin/sh -e
# Modified from Debian's add-shell to work on OS X
if test $# -eq 0
then
echo usage: $0 shellname [shellname ...]
exit 1
fi
scriptname=`basename "$0"`
if [[ $UID -ne 0 ]]; then
echo "${scriptname} must be run as root"
exit 1
fi
file=/etc/shells
# I want this to be GUARANTEED to be on the same filesystem as $file
tmpfile=${file}.tmp
set -o noclobber
trap "rm -f $tmpfile" EXIT
if ! cat $file > $tmpfile
then
cat 1>&2 <<EOF
Either another instance of $0 is running, or it was previously interrupted.
Please examine ${tmpfile} to see if it should be moved onto ${file}.
EOF
exit 1
fi
for i
do
if ! grep -q "^${i}$" $tmpfile
then
echo $i >> $tmpfile
fi
done
chmod 0644 $tmpfile
chown root:wheel $tmpfile
mv $tmpfile $file
trap "" EXIT
exit 0

View File

@@ -0,0 +1,3 @@
#!/bin/sh -x
./add-shell /usr/local/bin/fish > /tmp/fish_postinstall_output.log

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,9 @@
#include "util.h" #include "util.h"
#include "io.h" #include "io.h"
#include "common.h"
class parser_t;
enum enum
{ {
@@ -86,15 +89,13 @@ enum
#define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" ) #define BUILTIN_END_BLOCK_UNKNOWN _( L"%ls: Unknown block type '%ls'\n" )
#define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" ) #define BUILTIN_ERR_NOT_NUMBER _( L"%ls: Argument '%ls' is not a number\n" )
/**
Stringbuffer used to represent standard output
*/
extern string_buffer_t *sb_out;
/** /** Get the string used to represent stdout and stderr */
Stringbuffer used to represent standard error const wcstring &get_stdout_buffer();
*/ const wcstring &get_stderr_buffer();
extern string_buffer_t *sb_err;
/** Output an error */
void builtin_show_error(const wcstring &err);
/** /**
Kludge. Tells builtins if output is to screen Kludge. Tells builtins if output is to screen
@@ -120,11 +121,12 @@ void builtin_destroy();
/** /**
Is there a builtin command with the given name? Is there a builtin command with the given name?
*/ */
int builtin_exists( wchar_t *cmd ); int builtin_exists( const wcstring &cmd );
/** /**
Execute a builtin command Execute a builtin command
\param parser The parser being used
\param argv Array containing the command and parameters \param argv Array containing the command and parameters
of the builtin. The list is terminated by a of the builtin. The list is terminated by a
null pointer. This syntax resembles the syntax null pointer. This syntax resembles the syntax
@@ -133,28 +135,29 @@ int builtin_exists( wchar_t *cmd );
\return the exit status of the builtin command \return the exit status of the builtin command
*/ */
int builtin_run( wchar_t **argv, io_data_t *io ); int builtin_run( parser_t &parser, const wchar_t * const *argv, io_data_t *io );
/** Returns a list of all builtin names */
wcstring_list_t builtin_get_names(void);
/** Insert all builtin names into list. */
void builtin_get_names(std::vector<completion_t> &list);
/** /**
Insert all builtin names into l. These are not copies of the strings and should not be freed after use. Pushes a new set of input/output to the stack. The new stdin is supplied, a new set of output strings is created.
*/ */
void builtin_get_names( array_list_t *list ); void builtin_push_io( parser_t &parser, int stdin_fd );
/** /**
Pushes a new set of input/output to the stack. The new stdin is supplied, a new set of output string_buffer_ts is created. Pops a set of input/output from the stack. The output strings are destroued, but the input file is not closed.
*/ */
void builtin_push_io( int stdin_fd ); void builtin_pop_io(parser_t &parser);
/**
Pops a set of input/output from the stack. The output string_buffer_ts are destroued, but the input file is not closed.
*/
void builtin_pop_io();
/** /**
Return a one-line description of the specified builtin Return a one-line description of the specified builtin.
*/ */
const wchar_t *builtin_get_desc( const wchar_t *b ); wcstring builtin_get_desc( const wcstring &b );
/** /**
@@ -167,10 +170,9 @@ const wchar_t *builtin_complete_get_temporary_buffer();
/** /**
Run the __fish_print_help function to obtain the help information Run the __fish_print_help function to obtain the help information
for the specified command. The resulting string will be valid until for the specified command.
the next time this function is called, and must never be free'd manually.
*/ */
wchar_t *builtin_help_get( const wchar_t *cmd ); wcstring builtin_help_get( parser_t &parser, const wchar_t *cmd );
#endif #endif

View File

@@ -56,7 +56,7 @@ enum
Pointer to what the commandline builtin considers to be the current Pointer to what the commandline builtin considers to be the current
contents of the command line buffer. contents of the command line buffer.
*/ */
static wchar_t *current_buffer=0; static const wchar_t *current_buffer=0;
/** /**
What the commandline builtin considers to be the current cursor What the commandline builtin considers to be the current cursor
position. position.
@@ -66,7 +66,7 @@ static int current_cursor_pos = -1;
/** /**
Returns the current commandline buffer. Returns the current commandline buffer.
*/ */
static wchar_t *get_buffer() static const wchar_t *get_buffer()
{ {
return current_buffer; return current_buffer;
} }
@@ -90,46 +90,44 @@ static int get_cursor_pos()
*/ */
static void replace_part( const wchar_t *begin, static void replace_part( const wchar_t *begin,
const wchar_t *end, const wchar_t *end,
wchar_t *insert, const wchar_t *insert,
int append_mode ) int append_mode )
{ {
const wchar_t *buff = get_buffer(); const wchar_t *buff = get_buffer();
string_buffer_t out;
int out_pos=get_cursor_pos(); int out_pos=get_cursor_pos();
sb_init( &out ); wcstring out;
sb_append_substring( &out, buff, begin-buff ); out.append(buff, begin - buff);
switch( append_mode) switch( append_mode)
{ {
case REPLACE_MODE: case REPLACE_MODE:
{ {
sb_append( &out, insert ); out.append(insert);
out_pos = wcslen( insert ) + (begin-buff); out_pos = wcslen( insert ) + (begin-buff);
break; break;
} }
case APPEND_MODE: case APPEND_MODE:
{ {
sb_append_substring( &out, begin, end-begin ); out.append( begin, end-begin );
sb_append( &out, insert ); out.append( insert );
break; break;
} }
case INSERT_MODE: case INSERT_MODE:
{ {
int cursor = get_cursor_pos() -(begin-buff); int cursor = get_cursor_pos() -(begin-buff);
sb_append_substring( &out, begin, cursor ); out.append( begin, cursor );
sb_append( &out, insert ); out.append( insert );
sb_append_substring( &out, begin+cursor, end-begin-cursor ); out.append( begin+cursor, end-begin-cursor );
out_pos += wcslen( insert ); out_pos += wcslen( insert );
break; break;
} }
} }
sb_append( &out, end ); out.append( end );
reader_set_buffer( (wchar_t *)out.buff, out_pos ); reader_set_buffer( out, out_pos );
sb_destroy( &out );
} }
/** /**
@@ -146,9 +144,9 @@ static void write_part( const wchar_t *begin,
int tokenize ) int tokenize )
{ {
tokenizer tok; tokenizer tok;
string_buffer_t out; wcstring out;
wchar_t *buff; wchar_t *buff;
int pos; size_t pos;
pos = get_cursor_pos()-(begin-get_buffer()); pos = get_cursor_pos()-(begin-get_buffer());
@@ -156,7 +154,7 @@ static void write_part( const wchar_t *begin,
{ {
buff = wcsndup( begin, end-begin ); buff = wcsndup( begin, end-begin );
// fwprintf( stderr, L"Subshell: %ls, end char %lc\n", buff, *end ); // fwprintf( stderr, L"Subshell: %ls, end char %lc\n", buff, *end );
sb_init( &out ); out.clear();
for( tok_init( &tok, buff, TOK_ACCEPT_UNFINISHED ); for( tok_init( &tok, buff, TOK_ACCEPT_UNFINISHED );
tok_has_next( &tok ); tok_has_next( &tok );
@@ -170,39 +168,30 @@ static void write_part( const wchar_t *begin,
{ {
case TOK_STRING: case TOK_STRING:
{ {
wchar_t *tmp = unescape( tok_last( &tok ), UNESCAPE_INCOMPLETE ); out.append(escape_string(tok_last( &tok ), UNESCAPE_INCOMPLETE));
sb_append( &out, tmp, L"\n", (void *)0 ); out.push_back(L'\n');
free( tmp );
break; break;
} }
} }
} }
sb_append( sb_out, stdout_buffer.append(out);
(wchar_t *)out.buff );
free( buff ); free( buff );
tok_destroy( &tok ); tok_destroy( &tok );
sb_destroy( &out );
} }
else else
{ {
wchar_t *buff;
if( cut_at_cursor ) if( cut_at_cursor )
{ {
end = begin+pos; end = begin+pos;
} }
buff = wcsndup( begin, end-begin );
// debug( 0, L"woot2 %ls -> %ls", buff, esc ); // debug( 0, L"woot2 %ls -> %ls", buff, esc );
sb_append( sb_out, buff ); stdout_buffer.append(begin, end - begin);
sb_append( sb_out, L"\n" ); stdout_buffer.append(L"\n");
free( buff );
} }
} }
@@ -212,7 +201,7 @@ static void write_part( const wchar_t *begin,
The commandline builtin. It is used for specifying a new value for The commandline builtin. It is used for specifying a new value for
the commandline. the commandline.
*/ */
static int builtin_commandline( wchar_t **argv ) static int builtin_commandline( parser_t &parser, wchar_t **argv )
{ {
int buffer_part=0; int buffer_part=0;
@@ -228,7 +217,7 @@ static int builtin_commandline( wchar_t **argv )
int cursor_mode = 0; int cursor_mode = 0;
int line_mode = 0; int line_mode = 0;
int search_mode = 0; int search_mode = 0;
wchar_t *begin, *end; const wchar_t *begin, *end;
current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer(); current_buffer = (wchar_t *)builtin_complete_get_temporary_buffer();
if( current_buffer ) if( current_buffer )
@@ -253,11 +242,9 @@ static int builtin_commandline( wchar_t **argv )
return 1; return 1;
} }
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Can not set commandline in non-interactive mode\n");
L": Can not set commandline in non-interactive mode\n", builtin_print_help( parser, argv[0], stderr_buffer );
(void *)0 );
builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@@ -349,11 +336,11 @@ static int builtin_commandline( wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format( stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@@ -416,11 +403,11 @@ static int builtin_commandline( wchar_t **argv )
break; break;
case 'h': case 'h':
builtin_print_help( argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case L'?': case L'?':
builtin_unknown_option( argv[0], argv[woptind-1] ); builtin_unknown_option( parser, argv[0], argv[woptind-1] );
return 1; return 1;
} }
} }
@@ -434,22 +421,22 @@ static int builtin_commandline( wchar_t **argv )
*/ */
if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode ) if( buffer_part || cut_at_cursor || append_mode || tokenize || cursor_mode || line_mode || search_mode )
{ {
sb_printf(sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( argc == woptind ) if( argc == woptind )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_MISSING, BUILTIN_ERR_MISSING,
argv[0] ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
for( i=woptind; i<argc; i++ ) for( i=woptind; i<argc; i++ )
@@ -466,11 +453,11 @@ static int builtin_commandline( wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_(L"%ls: Unknown input function '%ls'\n"), _(L"%ls: Unknown input function '%ls'\n"),
argv[0], argv[0],
argv[i] ); argv[i] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
} }
@@ -484,45 +471,45 @@ static int builtin_commandline( wchar_t **argv )
if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) ) if( (search_mode || line_mode || cursor_mode) && (argc-woptind > 1) )
{ {
sb_append( sb_err, append_format(stderr_buffer,
argv[0], argv[0],
L": Too many arguments\n", L": Too many arguments\n",
(void *)0 ); NULL );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) ) if( (buffer_part || tokenize || cut_at_cursor) && (cursor_mode || line_mode || search_mode) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO, BUILTIN_ERR_COMBO,
argv[0] ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( (tokenize || cut_at_cursor) && (argc-woptind) ) if( (tokenize || cut_at_cursor) && (argc-woptind) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO2, BUILTIN_ERR_COMBO2,
argv[0], argv[0],
L"--cut-at-cursor and --tokenize can not be used when setting the commandline" ); L"--cut-at-cursor and --tokenize can not be used when setting the commandline" );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
if( append_mode && !(argc-woptind) ) if( append_mode && !(argc-woptind) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_COMBO2, BUILTIN_ERR_COMBO2,
argv[0], argv[0],
L"insertion mode switches can not be used when not in insertion mode" ); L"insertion mode switches can not be used when not in insertion mode" );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
@@ -550,11 +537,11 @@ static int builtin_commandline( wchar_t **argv )
new_pos = wcstol( argv[woptind], &endptr, 10 ); new_pos = wcstol( argv[woptind], &endptr, 10 );
if( *endptr || errno ) if( *endptr || errno )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_NOT_NUMBER, BUILTIN_ERR_NOT_NUMBER,
argv[0], argv[0],
argv[woptind] ); argv[woptind] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
} }
current_buffer = reader_get_buffer(); current_buffer = reader_get_buffer();
@@ -564,7 +551,7 @@ static int builtin_commandline( wchar_t **argv )
} }
else else
{ {
sb_printf( sb_out, L"%d\n", reader_get_cursor_pos() ); append_format(stdout_buffer, L"%d\n", reader_get_cursor_pos() );
return 0; return 0;
} }
@@ -573,8 +560,8 @@ static int builtin_commandline( wchar_t **argv )
if( line_mode ) if( line_mode )
{ {
int pos = reader_get_cursor_pos(); int pos = reader_get_cursor_pos();
wchar_t *buff = reader_get_buffer(); const wchar_t *buff = reader_get_buffer();
sb_printf( sb_out, L"%d\n", parse_util_lineno( buff, pos ) ); append_format(stdout_buffer, L"%d\n", parse_util_lineno( buff, pos ) );
return 0; return 0;
} }
@@ -640,21 +627,16 @@ static int builtin_commandline( wchar_t **argv )
default: default:
{ {
string_buffer_t sb; wcstring sb = argv[woptind];
int i; int i;
sb_init( &sb );
sb_append( &sb, argv[woptind] );
for( i=woptind+1; i<argc; i++ ) for( i=woptind+1; i<argc; i++ )
{ {
sb_append( &sb, L"\n" ); sb.push_back(L'\n');
sb_append( &sb, argv[i] ); sb.append(argv[i]);
} }
replace_part( begin, end, (wchar_t *)sb.buff, append_mode ); replace_part( begin, end, sb.c_str(), append_mode );
sb_destroy( &sb );
break; break;
} }

View File

@@ -26,7 +26,7 @@ Functions used for implementing the complete builtin.
/** /**
Internal storage for the builtin_get_temporary_buffer() function. Internal storage for the builtin_complete_get_temporary_buffer() function.
*/ */
static const wchar_t *temporary_buffer; static const wchar_t *temporary_buffer;
@@ -45,15 +45,15 @@ static const wchar_t *temporary_buffer;
static void builtin_complete_add2( const wchar_t *cmd, static void builtin_complete_add2( const wchar_t *cmd,
int cmd_type, int cmd_type,
const wchar_t *short_opt, const wchar_t *short_opt,
array_list_t *gnu_opt, const wcstring_list_t &gnu_opt,
array_list_t *old_opt, const wcstring_list_t &old_opt,
int result_mode, int result_mode,
const wchar_t *condition, const wchar_t *condition,
const wchar_t *comp, const wchar_t *comp,
const wchar_t *desc, const wchar_t *desc,
int flags ) int flags )
{ {
int i; size_t i;
const wchar_t *s; const wchar_t *s;
for( s=short_opt; *s; s++ ) for( s=short_opt; *s; s++ )
@@ -70,12 +70,12 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags ); flags );
} }
for( i=0; i<al_get_count( gnu_opt ); i++ ) for( i=0; i<gnu_opt.size(); i++ )
{ {
complete_add( cmd, complete_add( cmd,
cmd_type, cmd_type,
0, 0,
(wchar_t *)al_get(gnu_opt, i ), gnu_opt.at(i).c_str(),
0, 0,
result_mode, result_mode,
condition, condition,
@@ -84,12 +84,12 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags ); flags );
} }
for( i=0; i<al_get_count( old_opt ); i++ ) for( i=0; i<old_opt.size(); i++ )
{ {
complete_add( cmd, complete_add( cmd,
cmd_type, cmd_type,
0, 0,
(wchar_t *)al_get(old_opt, i ), old_opt.at(i).c_str(),
1, 1,
result_mode, result_mode,
condition, condition,
@@ -98,7 +98,7 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags ); flags );
} }
if( al_get_count( old_opt )+al_get_count( gnu_opt )+wcslen(short_opt) == 0 ) if( old_opt.size() == 0 && gnu_opt.size() == 0 && wcslen(short_opt) == 0 )
{ {
complete_add( cmd, complete_add( cmd,
cmd_type, cmd_type,
@@ -116,11 +116,11 @@ static void builtin_complete_add2( const wchar_t *cmd,
/** /**
Silly function Silly function
*/ */
static void builtin_complete_add( array_list_t *cmd, static void builtin_complete_add( const wcstring_list_t &cmd,
array_list_t *path, const wcstring_list_t &path,
const wchar_t *short_opt, const wchar_t *short_opt,
array_list_t *gnu_opt, wcstring_list_t &gnu_opt,
array_list_t *old_opt, wcstring_list_t &old_opt,
int result_mode, int result_mode,
int authoritative, int authoritative,
const wchar_t *condition, const wchar_t *condition,
@@ -128,11 +128,9 @@ static void builtin_complete_add( array_list_t *cmd,
const wchar_t *desc, const wchar_t *desc,
int flags ) int flags )
{ {
int i; for( size_t i=0; i<cmd.size(); i++ )
for( i=0; i<al_get_count( cmd ); i++ )
{ {
builtin_complete_add2( al_get( cmd, i ), builtin_complete_add2( cmd.at(i).c_str(),
COMMAND, COMMAND,
short_opt, short_opt,
gnu_opt, gnu_opt,
@@ -145,16 +143,16 @@ static void builtin_complete_add( array_list_t *cmd,
if( authoritative != -1 ) if( authoritative != -1 )
{ {
complete_set_authoritative( al_get( cmd, i ), complete_set_authoritative( cmd.at(i).c_str(),
COMMAND, COMMAND,
authoritative ); authoritative );
} }
} }
for( i=0; i<al_get_count( path ); i++ ) for( size_t i=0; i<path.size(); i++ )
{ {
builtin_complete_add2( al_get( path, i ), builtin_complete_add2( path.at(i).c_str(),
PATH, PATH,
short_opt, short_opt,
gnu_opt, gnu_opt,
@@ -167,7 +165,7 @@ static void builtin_complete_add( array_list_t *cmd,
if( authoritative != -1 ) if( authoritative != -1 )
{ {
complete_set_authoritative( al_get( path, i ), complete_set_authoritative( path.at(i).c_str(),
PATH, PATH,
authoritative ); authoritative );
} }
@@ -178,37 +176,35 @@ static void builtin_complete_add( array_list_t *cmd,
/** /**
Silly function Silly function
*/ */
static void builtin_complete_remove3( wchar_t *cmd, static void builtin_complete_remove3( const wchar_t *cmd,
int cmd_type, int cmd_type,
wchar_t short_opt, wchar_t short_opt,
array_list_t *long_opt ) const wcstring_list_t &long_opt )
{ {
int i; for( size_t i=0; i<long_opt.size(); i++ )
for( i=0; i<al_get_count( long_opt ); i++ )
{ {
complete_remove( cmd, complete_remove( cmd,
cmd_type, cmd_type,
short_opt, short_opt,
(wchar_t *)al_get( long_opt, i ) ); long_opt.at(i).c_str());
} }
} }
/** /**
Silly function Silly function
*/ */
static void builtin_complete_remove2( wchar_t *cmd, static void builtin_complete_remove2( const wchar_t *cmd,
int cmd_type, int cmd_type,
const wchar_t *short_opt, const wchar_t *short_opt,
array_list_t *gnu_opt, const wcstring_list_t &gnu_opt,
array_list_t *old_opt ) const wcstring_list_t &old_opt )
{ {
const wchar_t *s = (wchar_t *)short_opt; const wchar_t *s = (wchar_t *)short_opt;
if( *s ) if( *s )
{ {
for( ; *s; s++ ) for( ; *s; s++ )
{ {
if( al_get_count( old_opt) + al_get_count( gnu_opt ) == 0 ) if( old_opt.size() == 0 && gnu_opt.size() == 0 )
{ {
complete_remove(cmd, complete_remove(cmd,
cmd_type, cmd_type,
@@ -248,27 +244,24 @@ static void builtin_complete_remove2( wchar_t *cmd,
/** /**
Silly function Silly function
*/ */
static void builtin_complete_remove( array_list_t *cmd, static void builtin_complete_remove( const wcstring_list_t &cmd,
array_list_t *path, const wcstring_list_t &path,
const wchar_t *short_opt, const wchar_t *short_opt,
array_list_t *gnu_opt, const wcstring_list_t &gnu_opt,
array_list_t *old_opt ) const wcstring_list_t &old_opt )
{ {
for( size_t i=0; i<cmd.size(); i++ )
int i;
for( i=0; i<al_get_count( cmd ); i++ )
{ {
builtin_complete_remove2( (wchar_t *)al_get( cmd, i ), builtin_complete_remove2( cmd.at(i).c_str(),
COMMAND, COMMAND,
short_opt, short_opt,
gnu_opt, gnu_opt,
old_opt ); old_opt );
} }
for( i=0; i<al_get_count( path ); i++ ) for( size_t i=0; i<path.size(); i++ )
{ {
builtin_complete_remove2( (wchar_t *)al_get( path, i ), builtin_complete_remove2( path.at(i).c_str(),
PATH, PATH,
short_opt, short_opt,
gnu_opt, gnu_opt,
@@ -280,6 +273,7 @@ static void builtin_complete_remove( array_list_t *cmd,
const wchar_t *builtin_complete_get_temporary_buffer() const wchar_t *builtin_complete_get_temporary_buffer()
{ {
ASSERT_IS_MAIN_THREAD();
return temporary_buffer; return temporary_buffer;
} }
@@ -288,37 +282,33 @@ const wchar_t *builtin_complete_get_temporary_buffer()
tab-completions. Calls the functions in complete.c for any heavy tab-completions. Calls the functions in complete.c for any heavy
lifting. Defined in builtin_complete.c lifting. Defined in builtin_complete.c
*/ */
static int builtin_complete( wchar_t **argv ) static int builtin_complete( parser_t &parser, wchar_t **argv )
{ {
int res=0; ASSERT_IS_MAIN_THREAD();
bool res=false;
int argc=0; int argc=0;
int result_mode=SHARED; int result_mode=SHARED;
int remove = 0; int remove = 0;
int authoritative = -1; int authoritative = -1;
int flags = COMPLETE_AUTO_SPACE; int flags = COMPLETE_AUTO_SPACE;
string_buffer_t short_opt; wcstring short_opt;
array_list_t gnu_opt, old_opt; wcstring_list_t gnu_opt, old_opt;
wchar_t *comp=L"", *desc=L"", *condition=L""; const wchar_t *comp=L"", *desc=L"", *condition=L"";
wchar_t *do_complete = 0; bool do_complete = false;
wcstring do_complete_param;
array_list_t cmd; wcstring_list_t cmd;
array_list_t path; wcstring_list_t path;
static int recursion_level=0; static int recursion_level=0;
al_init( &cmd );
al_init( &path );
sb_init( &short_opt );
al_init( &gnu_opt );
al_init( &old_opt );
argc = builtin_count_args( argv ); argc = builtin_count_args( argv );
woptind=0; woptind=0;
while( res == 0 ) while( ! res )
{ {
static const struct woption static const struct woption
long_options[] = long_options[] =
@@ -408,14 +398,14 @@ static int builtin_complete( wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
res = 1; res = true;
break; break;
case 'x': case 'x':
@@ -433,15 +423,18 @@ static int builtin_complete( wchar_t **argv )
case 'p': case 'p':
case 'c': case 'c':
{ {
wchar_t *a = unescape( woptarg, 1); wcstring tmp = woptarg;
if( a ) if (unescape_string(tmp, 1))
{ {
al_push( (opt=='p'?&path:&cmd), a ); if (opt=='p')
path.push_back(tmp);
else
cmd.push_back(tmp);
} }
else else
{ {
sb_printf( sb_err, L"%ls: Invalid token '%ls'\n", argv[0], woptarg ); append_format(stderr_buffer, L"%ls: Invalid token '%ls'\n", argv[0], woptarg );
res = 1; res = true;
} }
break; break;
} }
@@ -459,15 +452,15 @@ static int builtin_complete( wchar_t **argv )
break; break;
case 's': case 's':
sb_append( &short_opt, woptarg ); short_opt.append(woptarg);
break; break;
case 'l': case 'l':
al_push( &gnu_opt, woptarg ); gnu_opt.push_back(woptarg);
break; break;
case 'o': case 'o':
al_push( &old_opt, woptarg ); old_opt.push_back(woptarg);
break; break;
case 'a': case 'a':
@@ -483,16 +476,17 @@ static int builtin_complete( wchar_t **argv )
break; break;
case 'C': case 'C':
do_complete = woptarg?woptarg:reader_get_buffer(); do_complete = true;
do_complete_param = woptarg ? woptarg : reader_get_buffer();
break; break;
case 'h': case 'h':
builtin_print_help( argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case '?': case '?':
builtin_unknown_option( argv[0], argv[woptind-1] ); builtin_unknown_option( parser, argv[0], argv[woptind-1] );
res = 1; res = true;
break; break;
} }
@@ -503,16 +497,16 @@ static int builtin_complete( wchar_t **argv )
{ {
if( condition && wcslen( condition ) ) if( condition && wcslen( condition ) )
{ {
if( parser_test( condition, 0, 0, 0 ) ) if( parser.test( condition, 0, 0, 0 ) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Condition '%ls' contained a syntax error\n", L"%ls: Condition '%ls' contained a syntax error\n",
argv[0], argv[0],
condition ); condition );
parser_test( condition, 0, sb_err, argv[0] ); parser.test( condition, 0, &stderr_buffer, argv[0] );
res = 1; res = true;
} }
} }
} }
@@ -521,16 +515,16 @@ static int builtin_complete( wchar_t **argv )
{ {
if( comp && wcslen( comp ) ) if( comp && wcslen( comp ) )
{ {
if( parser_test_args( comp, 0, 0 ) ) if( parser.test_args( comp, 0, 0 ) )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Completion '%ls' contained a syntax error\n", L"%ls: Completion '%ls' contained a syntax error\n",
argv[0], argv[0],
comp ); comp );
parser_test_args( comp, sb_err, argv[0] ); parser.test_args( comp, &stderr_buffer, argv[0] );
res = 1; res = true;
} }
} }
} }
@@ -539,31 +533,27 @@ static int builtin_complete( wchar_t **argv )
{ {
if( do_complete ) if( do_complete )
{ {
array_list_t *comp; const wchar_t *token;
int i;
parse_util_token_extent( do_complete_param.c_str(), do_complete_param.size(), &token, 0, 0, 0 );
const wchar_t *prev_temporary_buffer = temporary_buffer; const wchar_t *prev_temporary_buffer = temporary_buffer;
temporary_buffer = do_complete_param.c_str();
wchar_t *token;
parse_util_token_extent( do_complete, wcslen( do_complete ), &token, 0, 0, 0 );
temporary_buffer = do_complete;
if( recursion_level < 1 ) if( recursion_level < 1 )
{ {
recursion_level++; recursion_level++;
comp = al_halloc( 0 ); std::vector<completion_t> comp;
complete( do_complete_param, comp, COMPLETE_DEFAULT );
complete( do_complete, comp ); for( size_t i=0; i< comp.size() ; i++ )
for( i=0; i<al_get_count( comp ); i++ )
{ {
completion_t *next = (completion_t *)al_get( comp, i ); const completion_t &next = comp.at( i );
wchar_t *prepend;
if( next->flags & COMPLETE_NO_CASE ) const wchar_t *prepend;
if( next.flags & COMPLETE_NO_CASE )
{ {
prepend = L""; prepend = L"";
} }
@@ -573,17 +563,16 @@ static int builtin_complete( wchar_t **argv )
} }
if( next->description ) if( !(next.description).empty() )
{ {
sb_printf( sb_out, L"%ls%ls\t%ls\n", prepend, next->completion, next->description ); append_format(stdout_buffer, L"%ls%ls\t%ls\n", prepend, next.completion.c_str(), next.description.c_str() );
} }
else else
{ {
sb_printf( sb_out, L"%ls%ls\n", prepend, next->completion ); append_format(stdout_buffer, L"%ls%ls\n", prepend, next.completion.c_str() );
} }
} }
halloc_free( comp );
recursion_level--; recursion_level--;
} }
@@ -592,36 +581,36 @@ static int builtin_complete( wchar_t **argv )
} }
else if( woptind != argc ) else if( woptind != argc )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: Too many arguments\n" ), _( L"%ls: Too many arguments\n" ),
argv[0] ); argv[0] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
res = 1; res = true;
} }
else if( (al_get_count( &cmd) == 0 ) && (al_get_count( &path) == 0 ) ) else if( cmd.empty() && path.empty() )
{ {
/* No arguments specified, meaning we print the definitions of /* No arguments specified, meaning we print the definitions of
* all specified completions to stdout.*/ * all specified completions to stdout.*/
complete_print( sb_out ); complete_print( stdout_buffer );
} }
else else
{ {
if( remove ) if( remove )
{ {
builtin_complete_remove( &cmd, builtin_complete_remove( cmd,
&path, path,
(wchar_t *)short_opt.buff, short_opt.c_str(),
&gnu_opt, gnu_opt,
&old_opt ); old_opt );
} }
else else
{ {
builtin_complete_add( &cmd, builtin_complete_add( cmd,
&path, path,
(wchar_t *)short_opt.buff, short_opt.c_str(),
&gnu_opt, gnu_opt,
&old_opt, old_opt,
result_mode, result_mode,
authoritative, authoritative,
condition, condition,
@@ -633,14 +622,5 @@ static int builtin_complete( wchar_t **argv )
} }
} }
al_foreach( &cmd, &free ); return res ? 1 : 0;
al_foreach( &path, &free );
al_destroy( &cmd );
al_destroy( &path );
sb_destroy( &short_opt );
al_destroy( &gnu_opt );
al_destroy( &old_opt );
return res;
} }

View File

@@ -43,7 +43,7 @@ enum
/** /**
Calculates the cpu usage (in percent) of the specified job. Calculates the cpu usage (in percent) of the specified job.
*/ */
static int cpu_use( job_t *j ) static int cpu_use( const job_t *j )
{ {
double u=0; double u=0;
process_t *p; process_t *p;
@@ -71,7 +71,7 @@ static int cpu_use( job_t *j )
/** /**
Print information about the specified job Print information about the specified job
*/ */
static void builtin_jobs_print( job_t *j, int mode, int header ) static void builtin_jobs_print( const job_t *j, int mode, int header )
{ {
process_t *p; process_t *p;
switch( mode ) switch( mode )
@@ -84,24 +84,22 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Job\tGroup\t" )); stdout_buffer.append( _( L"Job\tGroup\t" ));
#ifdef HAVE__PROC_SELF_STAT #ifdef HAVE__PROC_SELF_STAT
sb_append( sb_out, _( L"CPU\t" ) ); stdout_buffer.append( _( L"CPU\t" ) );
#endif #endif
sb_append( sb_out, _( L"State\tCommand\n" ) ); stdout_buffer.append( _( L"State\tCommand\n" ) );
} }
sb_printf( sb_out, L"%d\t%d\t", j->job_id, j->pgid ); append_format(stdout_buffer, L"%d\t%d\t", j->job_id, j->pgid );
#ifdef HAVE__PROC_SELF_STAT #ifdef HAVE__PROC_SELF_STAT
sb_printf( sb_out, L"%d%%\t", cpu_use(j) ); append_format(stdout_buffer, L"%d%%\t", cpu_use(j) );
#endif #endif
sb_append( sb_out, stdout_buffer.append(job_is_stopped(j)?_(L"stopped"):_(L"running"));
job_is_stopped(j)?_(L"stopped"):_(L"running"), stdout_buffer.append(L"\t");
L"\t", stdout_buffer.append(j->command_wcstr());
j->command, stdout_buffer.append(L"\t");
L"\n",
(void *)0 );
break; break;
} }
@@ -112,9 +110,9 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Group\n" )); stdout_buffer.append( _( L"Group\n" ));
} }
sb_printf( sb_out, L"%d\n", j->pgid ); append_format(stdout_buffer, L"%d\n", j->pgid );
break; break;
} }
@@ -125,12 +123,12 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Procces\n" )); stdout_buffer.append( _( L"Procces\n" ));
} }
for( p=j->first_process; p; p=p->next ) for( p=j->first_process; p; p=p->next )
{ {
sb_printf( sb_out, L"%d\n", p->pid ); append_format(stdout_buffer, L"%d\n", p->pid );
} }
break; break;
} }
@@ -142,12 +140,12 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/* /*
Print table header before first job Print table header before first job
*/ */
sb_append( sb_out, _( L"Command\n" )); stdout_buffer.append( _( L"Command\n" ));
} }
for( p=j->first_process; p; p=p->next ) for( p=j->first_process; p; p=p->next )
{ {
sb_printf( sb_out, L"%ls\n", p->argv[0] ); append_format(stdout_buffer, L"%ls\n", p->argv0() );
} }
break; break;
} }
@@ -160,13 +158,13 @@ static void builtin_jobs_print( job_t *j, int mode, int header )
/** /**
The jobs builtin. Used fopr printing running jobs. Defined in builtin_jobs.c. The jobs builtin. Used fopr printing running jobs. Defined in builtin_jobs.c.
*/ */
static int builtin_jobs( wchar_t **argv ) static int builtin_jobs( parser_t &parser, wchar_t **argv )
{ {
int argc=0; int argc=0;
int found=0; int found=0;
int mode=JOBS_DEFAULT; int mode=JOBS_DEFAULT;
int print_last = 0; int print_last = 0;
job_t *j; const job_t *j;
argc = builtin_count_args( argv ); argc = builtin_count_args( argv );
woptind=0; woptind=0;
@@ -217,12 +215,12 @@ static int builtin_jobs( wchar_t **argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@@ -247,11 +245,11 @@ static int builtin_jobs( wchar_t **argv )
} }
case 'h': case 'h':
builtin_print_help( argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case '?': case '?':
builtin_unknown_option( argv[0], argv[woptind-1] ); builtin_unknown_option( parser, argv[0], argv[woptind-1] );
return 1; return 1;
} }
@@ -271,8 +269,11 @@ static int builtin_jobs( wchar_t **argv )
/* /*
Ignore unconstructed jobs, i.e. ourself. Ignore unconstructed jobs, i.e. ourself.
*/ */
for( j=first_job; j; j=j->next ) job_iterator_t jobs;
const job_t *j;
while ((j = jobs.next()))
{ {
if( (j->flags & JOB_CONSTRUCTED) && !job_is_completed(j) ) if( (j->flags & JOB_CONSTRUCTED) && !job_is_completed(j) )
{ {
builtin_jobs_print( j, mode, !found ); builtin_jobs_print( j, mode, !found );
@@ -297,7 +298,7 @@ static int builtin_jobs( wchar_t **argv )
pid=wcstol( argv[i], &end, 10 ); pid=wcstol( argv[i], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: '%ls' is not a job\n" ), _( L"%ls: '%ls' is not a job\n" ),
argv[0], argv[0],
argv[i] ); argv[i] );
@@ -312,7 +313,7 @@ static int builtin_jobs( wchar_t **argv )
} }
else else
{ {
sb_printf( sb_err, append_format(stderr_buffer,
_( L"%ls: No suitable job: %d\n" ), _( L"%ls: No suitable job: %d\n" ),
argv[0], argv[0],
pid ); pid );
@@ -322,8 +323,10 @@ static int builtin_jobs( wchar_t **argv )
} }
else else
{ {
for( j= first_job; j; j=j->next ) job_iterator_t jobs;
{ const job_t *j;
while ((j = jobs.next()))
{
/* /*
Ignore unconstructed jobs, i.e. ourself. Ignore unconstructed jobs, i.e. ourself.
*/ */
@@ -338,7 +341,7 @@ static int builtin_jobs( wchar_t **argv )
if( !found ) if( !found )
{ {
sb_printf( sb_out, append_format(stdout_buffer,
_( L"%ls: There are no jobs\n" ), _( L"%ls: There are no jobs\n" ),
argv[0] ); argv[0] );
} }

View File

@@ -1,879 +0,0 @@
/** \file builtin_set.c Functions defining the set builtin
Functions used for implementing the set builtin.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include <sys/types.h>
#include <termios.h>
#include <signal.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "builtin.h"
#include "env.h"
#include "expand.h"
#include "common.h"
#include "wgetopt.h"
#include "proc.h"
#include "parser.h"
/**
Error message for invalid path operations
*/
#define BUILTIN_SET_PATH_ERROR L"%ls: Could not add component %ls to %ls.\n"
/**
Hint for invalid path operation with a colon
*/
#define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n"
/**
Error for mismatch between index count and elements
*/
#define BUILTIN_SET_ARG_COUNT L"%ls: The number of variable indexes does not match the number of values\n"
/**
Test if the specified variable should be subject to path validation
*/
static int is_path_variable( const wchar_t *env )
{
return contains( env,
L"PATH",
L"CDPATH" );
}
/**
Call env_set. If this is a path variable, e.g. PATH, validate the
elements. On error, print a description of the problem to stderr.
*/
static int my_env_set( const wchar_t *key, array_list_t *val, int scope )
{
string_buffer_t sb;
int i;
int retcode = 0;
wchar_t *val_str=0;
if( is_path_variable( key ) )
{
int error = 0;
for( i=0; i<al_get_count( val ); i++ )
{
int show_perror = 0;
int show_hint = 0;
struct stat buff;
wchar_t *dir = (wchar_t *)al_get( val, i );
if( wstat( dir, &buff ) )
{
error = 1;
show_perror = 1;
}
if( !( S_ISDIR(buff.st_mode) ) )
{
error = 1;
}
if( error )
{
wchar_t *colon;
sb_printf( sb_err,
_(BUILTIN_SET_PATH_ERROR),
L"set",
dir,
key );
colon = wcschr( dir, L':' );
if( colon && *(colon+1) )
{
show_hint = 1;
}
}
if( show_perror )
{
builtin_wperror( L"set" );
}
if( show_hint )
{
sb_printf( sb_err,
_(BUILTIN_SET_PATH_HINT),
L"set",
key,
key,
wcschr( dir, L':' )+1);
}
if( error )
{
break;
}
}
if( error )
{
return 1;
}
}
sb_init( &sb );
if( al_get_count( val ) )
{
for( i=0; i<al_get_count( val ); i++ )
{
wchar_t *next =(wchar_t *)al_get( val, i );
sb_append( &sb, next?next:L"" );
if( i<al_get_count( val )-1 )
{
sb_append( &sb, ARRAY_SEP_STR );
}
}
val_str = (wchar_t *)sb.buff;
}
switch( env_set( key, val_str, scope | ENV_USER ) )
{
case ENV_PERM:
{
sb_printf( sb_err, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key );
retcode=1;
break;
}
case ENV_INVALID:
{
sb_printf( sb_err, _(L"%ls: Unknown error"), L"set" );
retcode=1;
break;
}
}
sb_destroy( &sb );
return retcode;
}
/**
Extract indexes from a destination argument of the form name[index1 index2...]
\param indexes the list to insert the new indexes into
\param src the source string to parse
\param name the name of the element. Return null if the name in \c src does not match this name
\param var_count the number of elements in the array to parse.
\return the total number of indexes parsed, or -1 on error
*/
static int parse_index( array_list_t *indexes,
const wchar_t *src,
const wchar_t *name,
int var_count )
{
int len;
int count = 0;
const wchar_t *src_orig = src;
if (src == 0)
{
return 0;
}
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
{
src++;
}
if (*src != L'[')
{
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), L"set" );
return 0;
}
len = src-src_orig;
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
{
sb_printf( sb_err,
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
L"set",
name,
len,
src_orig);
return 0;
}
src++;
while (iswspace(*src))
{
src++;
}
while (*src != L']')
{
wchar_t *end;
long l_ind;
errno = 0;
l_ind = wcstol(src, &end, 10);
if( end==src || errno )
{
sb_printf(sb_err, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
return 0;
}
if( l_ind < 0 )
{
l_ind = var_count+l_ind+1;
}
al_push_long(indexes, l_ind);
src = end;
count++;
while (iswspace(*src)) src++;
}
return count;
}
/**
Update a list \c list by writing copies (using wcsdup) of the
values specified by \c values to the indexes specified by \c
indexes. The previous entries at the specidied position will be
free'd.
\return 0 if the operation was successfull, non-zero otherwise
*/
static int update_values( array_list_t *list,
array_list_t *indexes,
array_list_t *values )
{
int i;
/* Replace values where needed */
for( i = 0; i < al_get_count(indexes); i++ )
{
/*
The '- 1' below is because the indices in fish are
one-based, but the array_list_t uses zero-based indices
*/
long ind = al_get_long(indexes, i) - 1;
void *new = (void *) al_get(values, i);
if( ind < 0 )
{
return 1;
}
free((void *) al_get(list, ind));
al_set(list, ind, new != 0 ? wcsdup(new) : wcsdup(L""));
}
return 0;
}
/**
Return 1 if an array list of longs contains the specified
value, 0 otherwise
*/
static int al_contains_long( array_list_t *list,
long val)
{
int i;
for (i = 0; i < al_get_count(list); i++)
{
long current = al_get_long(list, i);
if( current != 0 && current == val )
{
return 1;
}
}
return 0;
}
/**
Erase from a list values at specified indexes
*/
static void erase_values(array_list_t *list, array_list_t *indexes)
{
long i;
array_list_t result;
al_init(&result);
for (i = 0; i < al_get_count(list); i++)
{
if (!al_contains_long(indexes, i + 1))
{
al_push(&result, al_get(list, i));
}
else
{
free( (void *)al_get(list, i));
}
}
al_truncate(list,0);
al_push_all( list, &result );
al_destroy(&result);
}
/**
Print the names of all environment variables in the scope, with or without values,
with or without escaping
*/
static void print_variables(int include_values, int esc, int scope)
{
array_list_t names;
int i;
al_init( &names );
env_get_names( &names, scope );
sort_list( &names );
for( i = 0; i < al_get_count(&names); i++ )
{
wchar_t *key = (wchar_t *)al_get( &names, i );
wchar_t *e_key = esc ? escape(key, 0) : wcsdup(key);
sb_append(sb_out, e_key);
if( include_values )
{
wchar_t *value = env_get(key);
wchar_t *e_value;
if( value )
{
int shorten = 0;
if( wcslen( value ) > 64 )
{
shorten = 1;
value = wcsndup( value, 60 );
if( !value )
{
DIE_MEM();
}
}
e_value = esc ? expand_escape_variable(value) : wcsdup(value);
sb_append(sb_out, L" ", e_value, (void *)0);
free(e_value);
if( shorten )
{
sb_append(sb_out, L"\u2026");
free( value );
}
}
}
sb_append(sb_out, L"\n");
free(e_key);
}
al_destroy(&names);
}
/**
The set builtin. Creates, updates and erases environment variables
and environemnt variable arrays.
*/
static int builtin_set( wchar_t **argv )
{
/**
Variables used for parsing the argument list
*/
static const struct woption
long_options[] =
{
{
L"export", no_argument, 0, 'x'
}
,
{
L"global", no_argument, 0, 'g'
}
,
{
L"local", no_argument, 0, 'l'
}
,
{
L"erase", no_argument, 0, 'e'
}
,
{
L"names", no_argument, 0, 'n'
}
,
{
L"unexport", no_argument, 0, 'u'
}
,
{
L"universal", no_argument, 0, 'U'
}
,
{
L"query", no_argument, 0, 'q'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
}
}
;
const wchar_t *short_options = L"+xglenuUqh";
int argc = builtin_count_args(argv);
/*
Flags to set the work mode
*/
int local = 0, global = 0, export = 0;
int erase = 0, list = 0, unexport=0;
int universal = 0, query=0;
/*
Variables used for performing the actual work
*/
wchar_t *dest = 0;
int retcode=0;
int scope;
int slice=0;
int i;
wchar_t *bad_char;
/* Parse options to obtain the requested operation and the modifiers */
woptind = 0;
while (1)
{
int c = wgetopt_long(argc, argv, short_options, long_options, 0);
if (c == -1)
{
break;
}
switch(c)
{
case 0:
break;
case 'e':
erase = 1;
break;
case 'n':
list = 1;
break;
case 'x':
export = 1;
break;
case 'l':
local = 1;
break;
case 'g':
global = 1;
break;
case 'u':
unexport = 1;
break;
case 'U':
universal = 1;
break;
case 'q':
query = 1;
break;
case 'h':
builtin_print_help( argv[0], sb_out );
return 0;
case '?':
builtin_unknown_option( argv[0], argv[woptind-1] );
return 1;
default:
break;
}
}
/*
Ok, all arguments have been parsed, let's validate them
*/
/*
If we are checking the existance of a variable (-q) we can not
also specify scope
*/
if( query && (erase || list || global || local || universal || export || unexport ) )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/* We can't both list and erase varaibles */
if( erase && list )
{
sb_printf(sb_err,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/*
Variables can only have one scope
*/
if( local + global + universal > 1 )
{
sb_printf( sb_err,
BUILTIN_ERR_GLOCAL,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/*
Variables can only have one export status
*/
if( export && unexport )
{
sb_printf( sb_err,
BUILTIN_ERR_EXPUNEXP,
argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/*
Calculate the scope value for variable assignement
*/
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (export ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
if( query )
{
/*
Query mode. Return the number of variables that do not exist
out of the specified variables.
*/
int i;
for( i=woptind; i<argc; i++ )
{
wchar_t *arg = argv[i];
int slice=0;
if( !(dest = wcsdup(arg)))
{
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
{
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( slice )
{
array_list_t indexes;
array_list_t result;
int j;
al_init( &result );
al_init( &indexes );
tokenize_variable_array( env_get( dest ), &result );
if( !parse_index( &indexes, arg, dest, al_get_count( &result ) ) )
{
builtin_print_help( argv[0], sb_err );
retcode = 1;
break;
}
for( j=0; j<al_get_count( &indexes ); j++ )
{
long idx = al_get_long( &indexes, j );
if( idx < 1 || idx > al_get_count( &result ) )
{
retcode++;
}
}
}
else
{
if( !env_exist( arg, scope ) )
{
retcode++;
}
}
free( dest );
}
return retcode;
}
if( list )
{
/* Maybe we should issue an error if there are any other arguments? */
print_variables(0, 0, scope);
return 0;
}
if( woptind == argc )
{
/*
Print values of variables
*/
if( erase )
{
sb_printf( sb_err,
_(L"%ls: Erase needs a variable name\n%ls\n"),
argv[0] );
builtin_print_help( argv[0], sb_err );
retcode = 1;
}
else
{
print_variables( 1, 1, scope );
}
return retcode;
}
if( !(dest = wcsdup(argv[woptind])))
{
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
{
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( !wcslen( dest ) )
{
free( dest );
sb_printf( sb_err, BUILTIN_ERR_VARNAME_ZERO, argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
if( (bad_char = wcsvarname( dest ) ) )
{
sb_printf( sb_err, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
builtin_print_help( argv[0], sb_err );
free( dest );
return 1;
}
if( slice && erase && (scope != ENV_USER) )
{
free( dest );
sb_printf( sb_err, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] );
builtin_print_help( argv[0], sb_err );
return 1;
}
/*
set assignment can work in two modes, either using slices or
using the whole array. We detect which mode is used here.
*/
if( slice )
{
/*
Slice mode
*/
int idx_count, val_count;
array_list_t values;
array_list_t indexes;
array_list_t result;
al_init(&values);
al_init(&indexes);
al_init(&result);
tokenize_variable_array( env_get(dest), &result );
for( ; woptind<argc; woptind++ )
{
if( !parse_index( &indexes, argv[woptind], dest, al_get_count( &result ) ) )
{
builtin_print_help( argv[0], sb_err );
retcode = 1;
break;
}
val_count = argc-woptind-1;
idx_count = al_get_count( &indexes );
if( !erase )
{
if( val_count < idx_count )
{
sb_printf( sb_err, _(BUILTIN_SET_ARG_COUNT), argv[0] );
builtin_print_help( argv[0], sb_err );
retcode=1;
break;
}
if( val_count == idx_count )
{
woptind++;
break;
}
}
}
if( !retcode )
{
/*
Slice indexes have been calculated, do the actual work
*/
if( erase )
{
erase_values(&result, &indexes);
my_env_set( dest, &result, scope);
}
else
{
array_list_t value;
al_init(&value);
while( woptind < argc )
{
al_push(&value, argv[woptind++]);
}
if( update_values( &result,
&indexes,
&value ) )
{
sb_printf( sb_err, L"%ls: ", argv[0] );
sb_printf( sb_err, ARRAY_BOUNDS_ERR );
sb_append( sb_err, L"\n" );
}
my_env_set(dest,
&result,
scope);
al_destroy( &value );
}
}
al_foreach( &result, &free );
al_destroy( &result );
al_destroy(&indexes);
al_destroy(&values);
}
else
{
woptind++;
/*
No slicing
*/
if( erase )
{
if( woptind != argc )
{
sb_printf( sb_err,
_(L"%ls: Values cannot be specfied with erase\n"),
argv[0] );
builtin_print_help( argv[0], sb_err );
retcode=1;
}
else
{
retcode = env_remove( dest, scope );
}
}
else
{
array_list_t val;
al_init( &val );
for( i=woptind; i<argc; i++ )
{
al_push( &val, argv[i] );
}
retcode = my_env_set( dest, &val, scope );
al_destroy( &val );
}
}
free( dest );
return retcode;
}

808
builtin_set.cpp Normal file
View File

@@ -0,0 +1,808 @@
/** \file builtin_set.c Functions defining the set builtin
Functions used for implementing the set builtin.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <wctype.h>
#include <sys/types.h>
#include <termios.h>
#include <signal.h>
#include <vector>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "builtin.h"
#include "env.h"
#include "expand.h"
#include "common.h"
#include "wgetopt.h"
#include "proc.h"
#include "parser.h"
/* We know about these buffers */
extern wcstring stdout_buffer, stderr_buffer;
/**
Error message for invalid path operations
*/
#define BUILTIN_SET_PATH_ERROR L"%ls: Could not add component %ls to %ls.\n"
/**
Hint for invalid path operation with a colon
*/
#define BUILTIN_SET_PATH_HINT L"%ls: Did you mean 'set %ls $%ls %ls'?\n"
/**
Error for mismatch between index count and elements
*/
#define BUILTIN_SET_ARG_COUNT L"%ls: The number of variable indexes does not match the number of values\n"
/**
Test if the specified variable should be subject to path validation
*/
static int is_path_variable( const wchar_t *env )
{
return contains(env, L"PATH", L"CDPATH" );
}
/**
Call env_set. If this is a path variable, e.g. PATH, validate the
elements. On error, print a description of the problem to stderr.
*/
static int my_env_set( const wchar_t *key, wcstring_list_t &val, int scope )
{
size_t i;
int retcode = 0;
const wchar_t *val_str=NULL;
if( is_path_variable( key ) )
{
int error = 0;
for( i=0; i< val.size() ; i++ )
{
int show_perror = 0;
int show_hint = 0;
struct stat buff;
const wchar_t *dir = val[i].c_str();
if( wstat( dir, &buff ) )
{
error = 1;
show_perror = 1;
}
if( !( S_ISDIR(buff.st_mode) ) )
{
error = 1;
}
if( error )
{
const wchar_t *colon;
append_format(stderr_buffer, _(BUILTIN_SET_PATH_ERROR), L"set", dir, key);
colon = wcschr( dir, L':' );
if( colon && *(colon+1) )
{
show_hint = 1;
}
}
if( show_perror )
{
builtin_wperror( L"set" );
}
if( show_hint )
{
append_format(stderr_buffer, _(BUILTIN_SET_PATH_HINT), L"set", key, key, wcschr( dir, L':' )+1);
}
if( error )
{
break;
}
}
if( error )
{
return 1;
}
}
wcstring sb;
if( val.size() )
{
for( i=0; i< val.size() ; i++ )
{
sb.append(val[i]);
if( i<val.size() - 1 )
{
sb.append( ARRAY_SEP_STR );
}
}
val_str = sb.c_str();
}
switch( env_set( key, val_str, scope | ENV_USER ) )
{
case ENV_PERM:
{
append_format(stderr_buffer, _(L"%ls: Tried to change the read-only variable '%ls'\n"), L"set", key);
retcode=1;
break;
}
case ENV_INVALID:
{
append_format(stderr_buffer, _(L"%ls: Unknown error"), L"set" );
retcode=1;
break;
}
}
return retcode;
}
/**
Extract indexes from a destination argument of the form name[index1 index2...]
\param indexes the list to insert the new indexes into
\param src the source string to parse
\param name the name of the element. Return null if the name in \c src does not match this name
\param var_count the number of elements in the array to parse.
\return the total number of indexes parsed, or -1 on error
*/
static int parse_index( std::vector<long> &indexes,
const wchar_t *src,
const wchar_t *name,
int var_count )
{
size_t len;
int count = 0;
const wchar_t *src_orig = src;
if (src == 0)
{
return 0;
}
while (*src != L'\0' && (iswalnum(*src) || *src == L'_'))
{
src++;
}
if (*src != L'[')
{
append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), L"set" );
return 0;
}
len = src-src_orig;
if( (wcsncmp( src_orig, name, len )!=0) || (wcslen(name) != (len)) )
{
append_format(stderr_buffer,
_(L"%ls: Multiple variable names specified in single call (%ls and %.*ls)\n"),
L"set",
name,
len,
src_orig);
return 0;
}
src++;
while (iswspace(*src))
{
src++;
}
while (*src != L']')
{
wchar_t *end;
long l_ind;
errno = 0;
l_ind = wcstol(src, &end, 10);
if( end==src || errno )
{
append_format(stderr_buffer, _(L"%ls: Invalid index starting at '%ls'\n"), L"set", src);
return 0;
}
if( l_ind < 0 )
{
l_ind = var_count+l_ind+1;
}
indexes.push_back( l_ind );
src = end;
count++;
while (iswspace(*src)) src++;
}
return count;
}
static int update_values( wcstring_list_t &list,
std::vector<long> &indexes,
wcstring_list_t &values )
{
size_t i;
/* Replace values where needed */
for( i = 0; i < indexes.size(); i++ )
{
/*
The '- 1' below is because the indices in fish are
one-based, but the vector uses zero-based indices
*/
long ind = indexes[i] - 1;
const wcstring newv = values[ i ];
if( ind < 0 )
{
return 1;
}
// free((void *) al_get(list, ind));
list[ ind ] = newv;
}
return 0;
}
/**
Erase from a list of wcstring values at specified indexes
*/
static void erase_values(wcstring_list_t &list, const std::vector<long> &indexes)
{
// Make a set of indexes.
// This both sorts them into ascending order and removes duplicates.
const std::set<long> indexes_set(indexes.begin(), indexes.end());
// Now walk the set backwards, so we encounter larger indexes first, and remove elements at the given (1-based) indexes.
std::set<long>::const_reverse_iterator iter;
for (iter = indexes_set.rbegin(); iter != indexes_set.rend(); iter++) {
long val = *iter;
if (val > 0 && val <= list.size()) {
// One-based indexing!
list.erase(list.begin() + val - 1);
}
}
}
/**
Print the names of all environment variables in the scope, with or without shortening,
with or without values, with or without escaping
*/
static void print_variables(int include_values, int esc, bool shorten_ok, int scope)
{
wcstring_list_t names = env_get_names(scope);
sort(names.begin(), names.end());
for( size_t i = 0; i < names.size(); i++ )
{
const wcstring key = names.at(i);
const wcstring e_key = escape_string(key, 0);
stdout_buffer.append(e_key);
if( include_values )
{
env_var_t value = env_get_string(key);
if( !value.missing() )
{
int shorten = 0;
if( shorten_ok && value.length() > 64 )
{
shorten = 1;
value.resize(60);
}
wcstring e_value = esc ? expand_escape_variable(value) : value;
stdout_buffer.append(L" ");
stdout_buffer.append(e_value);
if( shorten )
{
stdout_buffer.append(L"\u2026");
}
}
}
stdout_buffer.append(L"\n");
}
}
/**
The set builtin. Creates, updates and erases environment variables
and environemnt variable arrays.
*/
static int builtin_set( parser_t &parser, wchar_t **argv )
{
/**
Variables used for parsing the argument list
*/
static const struct woption
long_options[] =
{
{
L"export", no_argument, 0, 'x'
}
,
{
L"global", no_argument, 0, 'g'
}
,
{
L"local", no_argument, 0, 'l'
}
,
{
L"erase", no_argument, 0, 'e'
}
,
{
L"names", no_argument, 0, 'n'
}
,
{
L"unexport", no_argument, 0, 'u'
}
,
{
L"universal", no_argument, 0, 'U'
}
,
{
L"long", no_argument, 0, 'L'
}
,
{
L"query", no_argument, 0, 'q'
}
,
{
L"help", no_argument, 0, 'h'
}
,
{
0, 0, 0, 0
}
}
;
const wchar_t *short_options = L"+xglenuULqh";
int argc = builtin_count_args(argv);
/*
Flags to set the work mode
*/
int local = 0, global = 0, exportv = 0;
int erase = 0, list = 0, unexport=0;
int universal = 0, query=0;
bool shorten_ok = true;
/*
Variables used for performing the actual work
*/
wchar_t *dest = 0;
int retcode=0;
int scope;
int slice=0;
int i;
wchar_t *bad_char;
/* Parse options to obtain the requested operation and the modifiers */
woptind = 0;
while (1)
{
int c = wgetopt_long(argc, argv, short_options, long_options, 0);
if (c == -1)
{
break;
}
switch(c)
{
case 0:
break;
case 'e':
erase = 1;
break;
case 'n':
list = 1;
break;
case 'x':
exportv = 1;
break;
case 'l':
local = 1;
break;
case 'g':
global = 1;
break;
case 'u':
unexport = 1;
break;
case 'U':
universal = 1;
break;
case 'L':
shorten_ok = false;
break;
case 'q':
query = 1;
break;
case 'h':
builtin_print_help( parser, argv[0], stdout_buffer );
return 0;
case '?':
builtin_unknown_option( parser, argv[0], argv[woptind-1] );
return 1;
default:
break;
}
}
/*
Ok, all arguments have been parsed, let's validate them
*/
/*
If we are checking the existance of a variable (-q) we can not
also specify scope
*/
if( query && (erase || list || global || local || universal || exportv || unexport ) )
{
append_format(stderr_buffer,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
/* We can't both list and erase varaibles */
if( erase && list )
{
append_format(stderr_buffer,
BUILTIN_ERR_COMBO,
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
/*
Variables can only have one scope
*/
if( local + global + universal > 1 )
{
append_format(stderr_buffer,
BUILTIN_ERR_GLOCAL,
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
/*
Variables can only have one export status
*/
if( exportv && unexport )
{
append_format(stderr_buffer,
BUILTIN_ERR_EXPUNEXP,
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
/*
Calculate the scope value for variable assignement
*/
scope = (local ? ENV_LOCAL : 0) | (global ? ENV_GLOBAL : 0) | (exportv ? ENV_EXPORT : 0) | (unexport ? ENV_UNEXPORT : 0) | (universal ? ENV_UNIVERSAL:0) | ENV_USER;
if( query )
{
/*
Query mode. Return the number of variables that do not exist
out of the specified variables.
*/
int i;
for( i=woptind; i<argc; i++ )
{
wchar_t *arg = argv[i];
int slice=0;
if( !(dest = wcsdup(arg)))
{
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
{
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( slice )
{
std::vector<long> indexes;
wcstring_list_t result;
size_t j;
env_var_t dest_str = env_get_string(dest);
if (! dest_str.missing())
tokenize_variable_array( dest_str, result );
if( !parse_index( indexes, arg, dest, result.size() ) )
{
builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1;
break;
}
for( j=0; j < indexes.size() ; j++ )
{
long idx = indexes[j];
if( idx < 1 || (size_t)idx > result.size() )
{
retcode++;
}
}
}
else
{
if( !env_exist( arg, scope ) )
{
retcode++;
}
}
free( dest );
}
return retcode;
}
if( list )
{
/* Maybe we should issue an error if there are any other arguments? */
print_variables(0, 0, shorten_ok, scope);
return 0;
}
if( woptind == argc )
{
/*
Print values of variables
*/
if( erase )
{
append_format(stderr_buffer,
_(L"%ls: Erase needs a variable name\n%ls\n"),
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1;
}
else
{
print_variables( 1, 1, shorten_ok, scope );
}
return retcode;
}
if( !(dest = wcsdup(argv[woptind])))
{
DIE_MEM();
}
if( wcschr( dest, L'[' ) )
{
slice = 1;
*wcschr( dest, L'[' )=0;
}
if( !wcslen( dest ) )
{
free( dest );
append_format(stderr_buffer, BUILTIN_ERR_VARNAME_ZERO, argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
if( (bad_char = wcsvarname( dest ) ) )
{
append_format(stderr_buffer, BUILTIN_ERR_VARCHAR, argv[0], *bad_char );
builtin_print_help( parser, argv[0], stderr_buffer );
free( dest );
return 1;
}
if( slice && erase && (scope != ENV_USER) )
{
free( dest );
append_format(stderr_buffer, _(L"%ls: Can not specify scope when erasing array slice\n"), argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
return 1;
}
/*
set assignment can work in two modes, either using slices or
using the whole array. We detect which mode is used here.
*/
if( slice )
{
/*
Slice mode
*/
int idx_count, val_count;
wcstring_list_t values;
std::vector<long> indexes;
wcstring_list_t result;
const env_var_t dest_str = env_get_string(dest);
if (! dest_str.missing())
tokenize_variable_array( dest_str, result );
for( ; woptind<argc; woptind++ )
{
if( !parse_index( indexes, argv[woptind], dest, result.size() ) )
{
builtin_print_help( parser, argv[0], stderr_buffer );
retcode = 1;
break;
}
val_count = argc-woptind-1;
idx_count = indexes.size();
if( !erase )
{
if( val_count < idx_count )
{
append_format(stderr_buffer, _(BUILTIN_SET_ARG_COUNT), argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
retcode=1;
break;
}
if( val_count == idx_count )
{
woptind++;
break;
}
}
}
if( !retcode )
{
/*
Slice indexes have been calculated, do the actual work
*/
if( erase )
{
erase_values(result, indexes);
my_env_set( dest, result, scope);
}
else
{
wcstring_list_t value;
// al_init(&value);
while( woptind < argc )
{
value.push_back( argv[woptind++] );
}
if( update_values( result,
indexes,
value ) )
{
append_format(stderr_buffer, L"%ls: ", argv[0] );
append_format(stderr_buffer, ARRAY_BOUNDS_ERR );
stderr_buffer.push_back(L'\n');
}
my_env_set(dest, result, scope);
// al_destroy( &value );
}
}
// al_foreach( &result, &free );
// al_destroy( &result );
// al_destroy(&indexes);
// al_destroy(&values);
}
else
{
woptind++;
/*
No slicing
*/
if( erase )
{
if( woptind != argc )
{
append_format(stderr_buffer,
_(L"%ls: Values cannot be specfied with erase\n"),
argv[0] );
builtin_print_help( parser, argv[0], stderr_buffer );
retcode=1;
}
else
{
retcode = env_remove( dest, scope );
}
}
else
{
wcstring_list_t val;
for( i=woptind; i<argc; i++ )
val.push_back(argv[i]);
retcode = my_env_set( dest, val, scope );
}
}
free( dest );
return retcode;
}

745
builtin_test.cpp Normal file
View File

@@ -0,0 +1,745 @@
/** \file builtin_test.cpp Functions defining the test builtin
Functions used for implementing the test builtin.
Implemented from scratch (yes, really) by way of IEEE 1003.1 as reference.
*/
#include "config.h"
#include "common.h"
#include "builtin.h"
#include "wutil.h"
#include "proc.h"
#include <sys/stat.h>
#include <memory>
enum {
BUILTIN_TEST_SUCCESS = STATUS_BUILTIN_OK,
BUILTIN_TEST_FAIL = STATUS_BUILTIN_ERROR
};
int builtin_test( parser_t &parser, wchar_t **argv );
static const wchar_t * const condstr[] = {
L"!", L"&&", L"||", L"==", L"!=", L"<", L">", L"-nt", L"-ot", L"-ef", L"-eq",
L"-ne", L"-lt", L"-gt", L"-le", L"-ge", L"=~"
};
namespace test_expressions {
enum token_t {
test_unknown, // arbitrary string
test_bang, // "!", inverts sense
test_filetype_b, // "-b", for block special files
test_filetype_c, // "-c" for character special files
test_filetype_d, // "-d" for directories
test_filetype_e, // "-e" for files that exist
test_filetype_f, // "-f" for for regular files
test_filetype_g, // "-g" for set-group-id
test_filetype_h, // "-h" for symbolic links
test_filetype_L, // "-L", same as -h
test_filetype_p, // "-p", for FIFO
test_filetype_S, // "-S", socket
test_filesize_s, // "-s", size greater than zero
test_filedesc_t, // "-t", whether the fd is associated with a terminal
test_fileperm_r, // "-r", read permission
test_fileperm_u, // "-u", whether file is setuid
test_fileperm_w, // "-w", whether file write permission is allowed
test_fileperm_x, // "-x", whether file execute/search is allowed
test_string_n, // "-n", non-empty string
test_string_z, // "-z", true if length of string is 0
test_string_equal, // "=", true if strings are identical
test_string_not_equal, // "!=", true if strings are not identical
test_number_equal, // "-eq", true if numbers are equal
test_number_not_equal, // "-ne", true if numbers are not equal
test_number_greater, // "-gt", true if first number is larger than second
test_number_greater_equal, // "-ge", true if first number is at least second
test_number_lesser, // "-lt", true if first number is smaller than second
test_number_lesser_equal, // "-le", true if first number is at most second
test_combine_and, // "-a", true if left and right are both true
test_combine_or, // "-o", true if either left or right is true
test_paren_open, // "(", open paren
test_paren_close, // ")", close paren
};
static bool binary_primary_evaluate(test_expressions::token_t token, const wcstring &left, const wcstring &right, wcstring_list_t &errors);
static bool unary_primary_evaluate(test_expressions::token_t token, const wcstring &arg, wcstring_list_t &errors);
enum {
UNARY_PRIMARY = 1 << 0,
BINARY_PRIMARY = 1 << 1
};
static const struct token_info_t { token_t tok; const wchar_t *string; unsigned int flags; } token_infos[] =
{
{test_unknown, L"", 0},
{test_bang, L"!", 0},
{test_filetype_b, L"-b", UNARY_PRIMARY},
{test_filetype_c, L"-c", UNARY_PRIMARY},
{test_filetype_d, L"-d", UNARY_PRIMARY},
{test_filetype_e, L"-e", UNARY_PRIMARY},
{test_filetype_f, L"-f", UNARY_PRIMARY},
{test_filetype_g, L"-g", UNARY_PRIMARY},
{test_filetype_h, L"-h", UNARY_PRIMARY},
{test_filetype_L, L"-L", UNARY_PRIMARY},
{test_filetype_p, L"-p", UNARY_PRIMARY},
{test_filetype_S, L"-S", UNARY_PRIMARY},
{test_filesize_s, L"-s", UNARY_PRIMARY},
{test_filedesc_t, L"-t", UNARY_PRIMARY},
{test_fileperm_r, L"-r", UNARY_PRIMARY},
{test_fileperm_u, L"-u", UNARY_PRIMARY},
{test_fileperm_w, L"-w", UNARY_PRIMARY},
{test_fileperm_x, L"-x", UNARY_PRIMARY},
{test_string_n, L"-n", UNARY_PRIMARY},
{test_string_z, L"-z", UNARY_PRIMARY},
{test_string_equal, L"=", BINARY_PRIMARY},
{test_string_not_equal, L"!=", BINARY_PRIMARY},
{test_number_equal, L"-eq", BINARY_PRIMARY},
{test_number_not_equal, L"-ne", BINARY_PRIMARY},
{test_number_greater, L"-gt", BINARY_PRIMARY},
{test_number_greater_equal, L"-ge", BINARY_PRIMARY},
{test_number_lesser, L"-lt", BINARY_PRIMARY},
{test_number_lesser_equal, L"-le", BINARY_PRIMARY},
{test_combine_and, L"-a", 0},
{test_combine_or, L"-o", 0},
{test_paren_open, L"(", 0},
{test_paren_close, L")", 0}
};
const token_info_t *token_for_string(const wcstring &str) {
for (size_t i=0; i < sizeof token_infos / sizeof *token_infos; i++) {
if (str == token_infos[i].string) {
return &token_infos[i];
}
}
return &token_infos[0]; //unknown
}
/* Grammar.
<expr> = <combining_expr>
<combining_expr> = <unary_expr> and/or <combining_expr> |
<unary_expr>
<unary_expr> = bang <unary_expr> |
<primary>
<primary> = <unary_primary> arg |
arg <binary_primary> arg |
'(' <expr> ')'
*/
class expression;
class test_parser {
private:
wcstring_list_t strings;
wcstring_list_t errors;
expression *error(const wchar_t *fmt, ...);
void add_error(const wchar_t *fmt, ...);
const wcstring &arg(unsigned int idx) { return strings.at(idx); }
public:
test_parser(const wcstring_list_t &val) : strings(val)
{ }
expression *parse_expression(unsigned int start, unsigned int end);
expression *parse_combining_expression(unsigned int start, unsigned int end);
expression *parse_unary_expression(unsigned int start, unsigned int end);
expression *parse_primary(unsigned int start, unsigned int end);
expression *parse_parenthentical(unsigned int start, unsigned int end);
expression *parse_unary_primary(unsigned int start, unsigned int end);
expression *parse_binary_primary(unsigned int start, unsigned int end);
expression *parse_just_a_string(unsigned int start, unsigned int end);
static expression *parse_args(const wcstring_list_t &args, wcstring &err);
};
struct range_t {
unsigned int start;
unsigned int end;
range_t(unsigned s, unsigned e) : start(s), end(e) { }
};
/* Base class for expressions */
class expression {
protected:
expression(token_t what, range_t where) : token(what), range(where) { }
public:
const token_t token;
range_t range;
virtual ~expression() { }
// evaluate returns true if the expression is true (i.e. BUILTIN_TEST_SUCCESS)
virtual bool evaluate(wcstring_list_t &errors) = 0;
};
typedef std::auto_ptr<expression> expr_ref_t;
/* Single argument like -n foo or "just a string" */
class unary_primary : public expression {
public:
wcstring arg;
unary_primary(token_t tok, range_t where, const wcstring &what) : expression(tok, where), arg(what) { }
bool evaluate(wcstring_list_t &errors);
};
/* Two argument primary like foo != bar */
class binary_primary : public expression {
public:
wcstring arg_left;
wcstring arg_right;
binary_primary(token_t tok, range_t where, const wcstring &left, const wcstring &right) : expression(tok, where), arg_left(left), arg_right(right)
{ }
bool evaluate(wcstring_list_t &errors);
};
/* Unary operator like bang */
class unary_operator : public expression {
public:
expr_ref_t subject;
unary_operator(token_t tok, range_t where, expr_ref_t &exp) : expression(tok, where), subject(exp) { }
bool evaluate(wcstring_list_t &errors);
};
/* Combining expression. Contains a list of AND or OR expressions. It takes more than two so that we don't have to worry about precedence in the parser. */
class combining_expression : public expression {
public:
const std::vector<expression *> subjects;
const std::vector<token_t> combiners;
combining_expression(token_t tok, range_t where, const std::vector<expression *> &exprs, const std::vector<token_t> &combs) : expression(tok, where), subjects(exprs), combiners(combs)
{
/* We should have one more subject than combiner */
assert(subjects.size() == combiners.size() + 1);
}
/* We are responsible for destroying our expressions */
virtual ~combining_expression() {
for (size_t i=0; i < subjects.size(); i++) {
delete subjects[i];
}
}
bool evaluate(wcstring_list_t &errors);
};
/* Parenthetical expression */
class parenthetical_expression : public expression {
public:
expr_ref_t contents;
parenthetical_expression(token_t tok, range_t where, expr_ref_t &expr) : expression(tok, where), contents(expr) { }
virtual bool evaluate(wcstring_list_t &errors);
};
void test_parser::add_error(const wchar_t *fmt, ...) {
assert(fmt != NULL);
va_list va;
va_start(va, fmt);
this->errors.push_back(vformat_string(fmt, va));
va_end(va);
}
expression *test_parser::error(const wchar_t *fmt, ...) {
assert(fmt != NULL);
va_list va;
va_start(va, fmt);
this->errors.push_back(vformat_string(fmt, va));
va_end(va);
return NULL;
}
expression *test_parser::parse_unary_expression(unsigned int start, unsigned int end) {
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
token_t tok = token_for_string(arg(start))->tok;
if (tok == test_bang) {
expr_ref_t subject(parse_unary_expression(start + 1, end));
if (subject.get()) {
return new unary_operator(tok, range_t(start, subject->range.end), subject);
} else {
return NULL;
}
} else {
return parse_primary(start, end);
}
}
/* Parse a combining expression (AND, OR) */
expression *test_parser::parse_combining_expression(unsigned int start, unsigned int end) {
if (start >= end)
return NULL;
std::vector<expression *> subjects;
std::vector<token_t> combiners;
unsigned int idx = start;
while (idx < end) {
if (! subjects.empty()) {
/* This is not the first expression, so we expect a combiner. */
token_t combiner = token_for_string(arg(idx))->tok;
if (combiner != test_combine_and && combiner != test_combine_or) {
/* Not a combiner, we're done */
break;
}
combiners.push_back(combiner);
idx++;
}
/* Parse another expression */
expression *expr = parse_unary_expression(idx, end);
if (! expr) {
add_error(L"Missing argument at index %u", idx);
break;
}
/* Go to the end of this expression */
idx = expr->range.end;
subjects.push_back(expr);
}
if (! subjects.empty()) {
/* Our new expression takes ownership of all expressions we created. The token we pass is irrelevant. */
return new combining_expression(test_combine_and, range_t(start, idx), subjects, combiners);
} else {
/* No subjects */
return NULL;
}
}
expression *test_parser::parse_unary_primary(unsigned int start, unsigned int end) {
/* We need two arguments */
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
if (start + 1 >= end) {
return error(L"Missing argument at index %u", start + 1);
}
/* All our unary primaries are prefix, so the operator is at start. */
const token_info_t *info = token_for_string(arg(start));
if (! (info->flags & UNARY_PRIMARY))
return NULL;
return new unary_primary(info->tok, range_t(start, start + 2), arg(start + 1));
}
expression *test_parser::parse_just_a_string(unsigned int start, unsigned int end) {
/* Handle a string as a unary primary that is not a token of any other type.
e.g. 'test foo -a bar' should evaluate to true
We handle this with a unary primary of test_string_n
*/
/* We need one arguments */
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
const token_info_t *info = token_for_string(arg(start));
if (info->tok != test_unknown) {
return error(L"Unexpected argument type at index %u", start);
}
/* This is hackish; a nicer way to implement this would be with a "just a string" expression type */
return new unary_primary(test_string_n, range_t(start, start + 1), arg(start));
}
#if 0
expression *test_parser::parse_unary_primary(unsigned int start, unsigned int end) {
/* We need either one or two arguments */
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
/* The index of the argument to the unary primary */
unsigned int arg_idx;
/* All our unary primaries are prefix, so any operator is at start. But it also may just be a string, with no operator. */
const token_info_t *info = token_for_string(arg(start));
if (info->flags & UNARY_PRIMARY) {
/* We have an operator. Skip the operator argument */
arg_idx = start + 1;
/* We have some freedom here...do we allow other tokens for the argument to operate on?
For example, should 'test -n =' work? I say yes. So no typechecking on the next token. */
} else if (info->tok == test_unknown) {
/* "Just a string. */
arg_idx = start;
} else {
/* Here we don't allow arbitrary tokens as "just a string." I.e. 'test = -a =' should have a parse error. We could relax this at some point. */
return error(L"Parse error at argument index %u", start);
}
/* Verify we have the argument we want, i.e. test -n should fail to parse */
if (arg_idx >= end) {
return error(L"Missing argument at index %u", arg_idx);
}
return new unary_primary(info->tok, range_t(start, arg_idx + 1), arg(arg_idx));
}
#endif
expression *test_parser::parse_binary_primary(unsigned int start, unsigned int end) {
/* We need three arguments */
for (unsigned int idx = start; idx < start + 3; idx++) {
if (idx >= end) {
return error(L"Missing argument at index %u", idx);
}
}
/* All our binary primaries are infix, so the operator is at start + 1. */
const token_info_t *info = token_for_string(arg(start + 1));
if (! (info->flags & BINARY_PRIMARY))
return NULL;
return new binary_primary(info->tok, range_t(start, start + 3), arg(start), arg(start + 2));
}
expression *test_parser::parse_parenthentical(unsigned int start, unsigned int end) {
/* We need at least three arguments: open paren, argument, close paren */
if (start + 3 >= end)
return NULL;
/* Must start with an open expression */
const token_info_t *open_paren = token_for_string(arg(start));
if (open_paren->tok != test_paren_open)
return NULL;
/* Parse a subexpression */
expression *subexr_ptr = parse_expression(start + 1, end);
if (! subexr_ptr)
return NULL;
expr_ref_t subexpr(subexr_ptr);
/* Parse a close paren */
unsigned close_index = subexpr->range.end;
assert(close_index <= end);
if (close_index == end) {
return error(L"Missing close paren at index %u", close_index);
}
const token_info_t *close_paren = token_for_string(arg(close_index));
if (close_paren->tok != test_paren_close) {
return error(L"Expected close paren at index %u", close_index);
}
/* Success */
return new parenthetical_expression(test_paren_open, range_t(start, close_index+1), subexpr);
}
expression *test_parser::parse_primary(unsigned int start, unsigned int end) {
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
expression *expr = NULL;
if (! expr) expr = parse_parenthentical(start, end);
if (! expr) expr = parse_unary_primary(start, end);
if (! expr) expr = parse_binary_primary(start, end);
if (! expr) expr = parse_just_a_string(start, end);
return expr;
}
expression *test_parser::parse_expression(unsigned int start, unsigned int end) {
if (start >= end) {
return error(L"Missing argument at index %u", start);
}
return parse_combining_expression(start, end);
}
expression *test_parser::parse_args(const wcstring_list_t &args, wcstring &err) {
/* Empty list and one-arg list should be handled by caller */
assert(args.size() > 1);
test_parser parser(args);
expression *result = parser.parse_expression(0, (unsigned int)args.size());
/* Handle errors */
bool errored = false;
for (size_t i = 0; i < parser.errors.size(); i++) {
err.append(L"test: ");
err.append(parser.errors.at(i));
err.push_back(L'\n');
errored = true;
// For now we only show the first error
break;
}
if (! errored && result) {
/* It's also an error if there are any unused arguments. This is not detected by parse_expression() */
assert(result->range.end <= args.size());
if (result->range.end < args.size()) {
append_format(err, L"test: unexpected argument at index %lu: '%ls'\n", (unsigned long)result->range.end, args.at(result->range.end).c_str());
delete result;
result = NULL;
errored = true;
}
}
return result;
}
bool unary_primary::evaluate(wcstring_list_t &errors) {
return unary_primary_evaluate(token, arg, errors);
}
bool binary_primary::evaluate(wcstring_list_t &errors) {
return binary_primary_evaluate(token, arg_left, arg_right, errors);
}
bool unary_operator::evaluate(wcstring_list_t &errors) {
switch (token) {
case test_bang:
assert(subject.get());
return ! subject->evaluate(errors);
default:
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
}
bool combining_expression::evaluate(wcstring_list_t &errors) {
switch (token) {
case test_combine_and:
case test_combine_or:
{
/* One-element case */
if (subjects.size() == 1)
return subjects.at(0)->evaluate(errors);
/* Evaluate our lists, remembering that AND has higher precedence than OR. We can visualize this as a sequence of OR expressions of AND expressions. */
assert(combiners.size() + 1 == subjects.size());
assert(! subjects.empty());
size_t idx = 0, max = subjects.size();
bool or_result = false;
while (idx < max) {
if (or_result) {
/* Short circuit */
break;
}
/* Evaluate a stream of AND starting at given subject index. It may only have one element. */
bool and_result = true;
for (; idx < max; idx++) {
/* Evaluate it, short-circuiting */
and_result = and_result && subjects.at(idx)->evaluate(errors);
/* If the combiner at this index (which corresponding to how we combine with the next subject) is not AND, then exit the loop */
if (idx + 1 < max && combiners.at(idx) != test_combine_and) {
idx++;
break;
}
}
/* OR it in */
or_result = or_result || and_result;
}
return or_result;
}
default:
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return BUILTIN_TEST_FAIL;
}
}
bool parenthetical_expression::evaluate(wcstring_list_t &errors) {
return contents->evaluate(errors);
}
/* IEEE 1003.1 says nothing about what it means for two strings to be "algebraically equal". For example, should we interpret 0x10 as 0, 10, or 16? Here we use only base 10 and use wcstoll, which allows for leading + and -, and leading whitespace. This matches bash. */
static bool parse_number(const wcstring &arg, long long *out) {
const wchar_t *str = arg.c_str();
wchar_t *endptr = NULL;
*out = wcstoll(str, &endptr, 10);
return endptr && *endptr == L'\0';
}
static bool binary_primary_evaluate(test_expressions::token_t token, const wcstring &left, const wcstring &right, wcstring_list_t &errors) {
using namespace test_expressions;
long long left_num, right_num;
switch (token) {
case test_string_equal:
return left == right;
case test_string_not_equal:
return left != right;
case test_number_equal:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num == right_num;
case test_number_not_equal:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num != right_num;
case test_number_greater:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num > right_num;
case test_number_greater_equal:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num >= right_num;
case test_number_lesser:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num < right_num;
case test_number_lesser_equal:
return parse_number(left, &left_num) && parse_number(right, &right_num) && left_num <= right_num;
default:
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
}
static bool unary_primary_evaluate(test_expressions::token_t token, const wcstring &arg, wcstring_list_t &errors) {
using namespace test_expressions;
struct stat buf;
long long num;
switch (token) {
case test_filetype_b: // "-b", for block special files
return !wstat(arg, &buf) && S_ISBLK(buf.st_mode);
case test_filetype_c: // "-c" for character special files
return !wstat(arg, &buf) && S_ISCHR(buf.st_mode);
case test_filetype_d: // "-d" for directories
return !wstat(arg, &buf) && S_ISDIR(buf.st_mode);
case test_filetype_e: // "-e" for files that exist
return !wstat(arg, &buf);
case test_filetype_f: // "-f" for for regular files
return !wstat(arg, &buf) && S_ISREG(buf.st_mode);
case test_filetype_g: // "-g" for set-group-id
return !wstat(arg, &buf) && (S_ISGID & buf.st_mode);
case test_filetype_h: // "-h" for symbolic links
case test_filetype_L: // "-L", same as -h
return !lwstat(arg, &buf) && S_ISLNK(buf.st_mode);
case test_filetype_p: // "-p", for FIFO
return !wstat(arg, &buf) && S_ISFIFO(buf.st_mode);
case test_filetype_S: // "-S", socket
return !wstat(arg, &buf) && S_ISSOCK(buf.st_mode);
case test_filesize_s: // "-s", size greater than zero
return !wstat(arg, &buf) && buf.st_size > 0;
case test_filedesc_t: // "-t", whether the fd is associated with a terminal
return parse_number(arg, &num) && num == (int)num && isatty((int)num);
case test_fileperm_r: // "-r", read permission
return !waccess(arg, R_OK);
case test_fileperm_u: // "-u", whether file is setuid
return !wstat(arg, &buf) && (S_ISUID & buf.st_mode);
case test_fileperm_w: // "-w", whether file write permission is allowed
return !waccess(arg, W_OK);
case test_fileperm_x: // "-x", whether file execute/search is allowed
return !waccess(arg, X_OK);
case test_string_n: // "-n", non-empty string
return ! arg.empty();
case test_string_z: // "-z", true if length of string is 0
return arg.empty();
default:
errors.push_back(format_string(L"Unknown token type in %s", __func__));
return false;
}
}
};
/*
* Evaluate a conditional expression given the arguments.
* If fromtest is set, the caller is the test or [ builtin;
* with the pointer giving the name of the command.
* for POSIX conformance this supports a more limited range
* of functionality.
*
* Return status is the final shell status, i.e. 0 for true,
* 1 for false and 2 for error.
*/
int builtin_test( parser_t &parser, wchar_t **argv )
{
using namespace test_expressions;
/* The first argument should be the name of the command ('test') */
if (! argv[0])
return BUILTIN_TEST_FAIL;
size_t argc = 0;
while (argv[argc + 1])
argc++;
const wcstring_list_t args(argv + 1, argv + 1 + argc);
if (argc == 0) {
// Per 1003.1, exit false
return BUILTIN_TEST_FAIL;
} else if (argc == 1) {
// Per 1003.1, exit true if the arg is non-empty
return args.at(0).empty() ? BUILTIN_TEST_FAIL : BUILTIN_TEST_SUCCESS;
} else {
// Try parsing. If expr is not nil, we are responsible for deleting it.
wcstring err;
expression *expr = test_parser::parse_args(args, err);
if (! expr) {
#if 0
printf("Oops! test was given args:\n");
for (size_t i=0; i < argc; i++) {
printf("\t%ls\n", args.at(i).c_str());
}
printf("and returned parse error: %ls\n", err.c_str());
#endif
builtin_show_error(err);
return BUILTIN_TEST_FAIL;
} else {
wcstring_list_t eval_errors;
bool result = expr->evaluate(eval_errors);
if (! eval_errors.empty()) {
printf("test returned eval errors:\n");
for (size_t i=0; i < eval_errors.size(); i++) {
printf("\t%ls\n", eval_errors.at(i).c_str());
}
}
delete expr;
return result ? BUILTIN_TEST_SUCCESS : BUILTIN_TEST_FAIL;
}
}
return 1;
}

View File

@@ -144,9 +144,9 @@ static void print( int resource, int hard )
rlim_t l = get( resource, hard ); rlim_t l = get( resource, hard );
if( l == RLIM_INFINITY ) if( l == RLIM_INFINITY )
sb_append( sb_out, L"unlimited\n" ); stdout_buffer.append( L"unlimited\n" );
else else
sb_printf( sb_out, L"%d\n", l / get_multiplier( resource ) ); append_format(stdout_buffer, L"%d\n", l / get_multiplier( resource ) );
} }
@@ -170,9 +170,9 @@ static void print_all( int hard )
getrlimit( resource_arr[i].resource, &ls ); getrlimit( resource_arr[i].resource, &ls );
l = hard ? ls.rlim_max:ls.rlim_cur; l = hard ? ls.rlim_max:ls.rlim_cur;
wchar_t *unit = ((resource_arr[i].resource==RLIMIT_CPU)?L"(seconds, ":(get_multiplier(resource_arr[i].resource)==1?L"(":L"(kB, ")); const wchar_t *unit = ((resource_arr[i].resource==RLIMIT_CPU)?L"(seconds, ":(get_multiplier(resource_arr[i].resource)==1?L"(":L"(kB, "));
sb_printf( sb_out, append_format(stdout_buffer,
L"%-*ls %10ls-%lc) ", L"%-*ls %10ls-%lc) ",
w, w,
resource_arr[i].desc, resource_arr[i].desc,
@@ -181,11 +181,11 @@ static void print_all( int hard )
if( l == RLIM_INFINITY ) if( l == RLIM_INFINITY )
{ {
sb_append( sb_out, L"unlimited\n" ); stdout_buffer.append( L"unlimited\n" );
} }
else else
{ {
sb_printf( sb_out, L"%d\n", l/get_multiplier(resource_arr[i].resource) ); append_format(stdout_buffer, L"%d\n", l/get_multiplier(resource_arr[i].resource) );
} }
} }
@@ -240,7 +240,7 @@ static int set( int resource, int hard, int soft, rlim_t value )
if( setrlimit( resource, &ls ) ) if( setrlimit( resource, &ls ) )
{ {
if( errno == EPERM ) if( errno == EPERM )
sb_printf( sb_err, L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc( resource ) ); append_format(stderr_buffer, L"ulimit: Permission denied when changing resource of type '%ls'\n", get_desc( resource ) );
else else
builtin_wperror( L"ulimit" ); builtin_wperror( L"ulimit" );
return 1; return 1;
@@ -252,7 +252,7 @@ static int set( int resource, int hard, int soft, rlim_t value )
The ulimit builtin, used for setting resource limits. Defined in The ulimit builtin, used for setting resource limits. Defined in
builtin_ulimit.c. builtin_ulimit.c.
*/ */
static int builtin_ulimit( wchar_t ** argv ) static int builtin_ulimit( parser_t &parser, wchar_t ** argv )
{ {
int hard=0; int hard=0;
int soft=0; int soft=0;
@@ -347,11 +347,11 @@ static int builtin_ulimit( wchar_t ** argv )
case 0: case 0:
if(long_options[opt_index].flag != 0) if(long_options[opt_index].flag != 0)
break; break;
sb_printf( sb_err, append_format(stderr_buffer,
BUILTIN_ERR_UNKNOWN, BUILTIN_ERR_UNKNOWN,
argv[0], argv[0],
long_options[opt_index].name ); long_options[opt_index].name );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
@@ -415,11 +415,11 @@ static int builtin_ulimit( wchar_t ** argv )
#endif #endif
case L'h': case L'h':
builtin_print_help( argv[0], sb_out ); builtin_print_help( parser, argv[0], stdout_buffer );
return 0; return 0;
case L'?': case L'?':
builtin_unknown_option( argv[0], argv[woptind-1] ); builtin_unknown_option( parser, argv[0], argv[woptind-1] );
return 1; return 1;
} }
} }
@@ -432,11 +432,9 @@ static int builtin_ulimit( wchar_t ** argv )
} }
else else
{ {
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Too many arguments\n");
L": Too many arguments\n", builtin_print_help( parser, argv[0], stderr_buffer );
(void *)0 );
builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }
@@ -488,11 +486,11 @@ static int builtin_ulimit( wchar_t ** argv )
new_limit = wcstol( argv[woptind], &end, 10 ); new_limit = wcstol( argv[woptind], &end, 10 );
if( errno || *end ) if( errno || *end )
{ {
sb_printf( sb_err, append_format(stderr_buffer,
L"%ls: Invalid limit '%ls'\n", L"%ls: Invalid limit '%ls'\n",
argv[0], argv[0],
argv[woptind] ); argv[woptind] );
builtin_print_help( argv[0], sb_err ); builtin_print_help( parser, argv[0], stderr_buffer );
return 1; return 1;
} }
new_limit *= get_multiplier( what ); new_limit *= get_multiplier( what );
@@ -503,11 +501,9 @@ static int builtin_ulimit( wchar_t ** argv )
default: default:
{ {
sb_append( sb_err, stderr_buffer.append(argv[0]);
argv[0], stderr_buffer.append(L": Too many arguments\n");
L": Too many arguments\n", builtin_print_help( parser, argv[0], stderr_buffer );
(void *)0 );
builtin_print_help( argv[0], sb_err );
return 1; return 1;
} }

263
color.cpp Normal file
View File

@@ -0,0 +1,263 @@
/** \file color.cpp Color class implementation
*/
#include "color.h"
#include "fallback.h"
bool rgb_color_t::try_parse_special(const wcstring &special) {
bzero(&data, sizeof data);
const wchar_t *name = special.c_str();
if (! wcscasecmp(name, L"normal")) {
this->type = type_normal;
} else if (! wcscasecmp(name, L"reset")) {
this->type = type_reset;
} else if (! wcscasecmp(name, L"ignore")) {
this->type = type_ignore;
} else {
this->type = type_none;
}
return this->type != type_none;
}
static int parse_hex_digit(wchar_t x) {
switch (x) {
case L'0': return 0x0;
case L'1': return 0x1;
case L'2': return 0x2;
case L'3': return 0x3;
case L'4': return 0x4;
case L'5': return 0x5;
case L'6': return 0x6;
case L'7': return 0x7;
case L'8': return 0x8;
case L'9': return 0x9;
case L'a':case L'A': return 0xA;
case L'b':case L'B': return 0xB;
case L'c':case L'C': return 0xC;
case L'd':case L'D': return 0xD;
case L'e':case L'E': return 0xE;
case L'f':case L'F': return 0xF;
default: return -1;
}
}
static unsigned char convert_color(const unsigned char rgb[3], const uint32_t *colors, size_t color_count) {
long r = rgb[0], g = rgb[1], b = rgb[2];
unsigned long best_distance = (unsigned long)(-1);
unsigned char best_index = (unsigned char)(-1);
for (unsigned char idx = 0; idx < color_count; idx++) {
uint32_t color = colors[idx];
long test_r = (color >> 16) & 0xFF, test_g = (color >> 8) & 0xFF, test_b = (color >> 0) & 0xFF;
unsigned long distance = 0;
distance += (r - test_r) * (r - test_r);
distance += (g - test_g) * (g - test_g);
distance += (b - test_b) * (b - test_b);
if (distance <= best_distance) {
best_index = idx;
best_distance = distance;
}
}
return best_index;
}
bool rgb_color_t::try_parse_rgb(const wcstring &name) {
bzero(&data, sizeof data);
/* We support the following style of rgb formats (case insensitive):
#FA3
#F3A035
FA3
F3A035
*/
size_t digit_idx = 0, len = name.size();
/* Skip any leading # */
if (len > 0 && name.at(0) == L'#')
digit_idx++;
bool success = false;
size_t i;
if (len - digit_idx == 3) {
// type FA3
for (i=0; i < 3; i++) {
int val = parse_hex_digit(name.at(digit_idx++));
if (val < 0) break;
data.rgb[i] = val*16+val;
}
success = (i == 3);
} else if (len - digit_idx == 6) {
// type F3A035
for (i=0; i < 3; i++) {
int hi = parse_hex_digit(name.at(digit_idx++));
int lo = parse_hex_digit(name.at(digit_idx++));
if (lo < 0 || hi < 0) break;
data.rgb[i] = hi*16+lo;
}
success = (i == 3);
}
if (success) {
this->type = type_rgb;
}
return success;
}
struct named_color_t {
const wchar_t * name;
unsigned char idx;
unsigned char rgb[3];
};
static const named_color_t named_colors[11] = {
{L"black", 0, {0, 0, 0}},
{L"red", 1, {0xFF, 0, 0}},
{L"green", 2, {0, 0xFF, 0}},
{L"brown", 3, {0x72, 0x50, 0}},
{L"yellow", 3, {0xFF, 0xFF, 0}},
{L"blue", 4, {0, 0, 0xFF}},
{L"magenta", 5, {0xFF, 0, 0xFF}},
{L"purple", 5, {0xFF, 0, 0xFF}},
{L"cyan", 6, {0, 0xFF, 0xFF}},
{L"white", 7, {0xFF, 0xFF, 0xFF}},
{L"normal", 8, {0xFF, 0xFF, 0XFF}}
};
bool rgb_color_t::try_parse_named(const wcstring &str) {
bzero(&data, sizeof data);
size_t max = sizeof named_colors / sizeof *named_colors;
for (size_t idx=0; idx < max; idx++) {
if (0 == wcscasecmp(str.c_str(), named_colors[idx].name)) {
data.name_idx = named_colors[idx].idx;
this->type = type_named;
return true;
}
}
return false;
}
static const wchar_t *name_for_color_idx(unsigned char idx) {
size_t max = sizeof named_colors / sizeof *named_colors;
for (size_t i=0; i < max; i++) {
if (named_colors[i].idx == idx) {
return named_colors[i].name;
}
}
return L"unknown";
}
rgb_color_t::rgb_color_t(unsigned char t, unsigned char i) : type(t), flags(), data() {
data.name_idx = i;
}
rgb_color_t rgb_color_t::normal() { return rgb_color_t(type_normal); }
rgb_color_t rgb_color_t::reset() { return rgb_color_t(type_reset); }
rgb_color_t rgb_color_t::ignore() { return rgb_color_t(type_ignore); }
rgb_color_t rgb_color_t::none() { return rgb_color_t(type_none); }
rgb_color_t rgb_color_t::white() { return rgb_color_t(type_named, 7); }
rgb_color_t rgb_color_t::black() { return rgb_color_t(type_named, 0); }
static unsigned char term8_color_for_rgb(const unsigned char rgb[3]) {
const uint32_t kColors[] = {
0x000000, //Black
0xFF0000, //Red
0x00FF00, //Green
0x725000, //Brown
0xFFFF00, //Yellow
0x0000FF, //Blue
0xFF00FF, //Magenta
0xFF00FF, //Purple
0x00FFFF, //Cyan
0xFFFFFF, //White
};
return convert_color(rgb, kColors, sizeof kColors / sizeof *kColors);
}
static unsigned char term256_color_for_rgb(const unsigned char rgb[3]) {
const uint32_t kColors[240] = {
0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f,
0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af,
0x0087d7, 0x0087ff, 0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff,
0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f,
0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af,
0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff,
0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f,
0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff, 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af,
0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff,
0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f,
0x875f87, 0x875faf, 0x875fd7, 0x875fff, 0x878700, 0x87875f, 0x878787, 0x8787af,
0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff,
0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f,
0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af,
0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff,
0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f,
0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 0xafd700, 0xafd75f, 0xafd787, 0xafd7af,
0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff,
0xd70000, 0xd7005f, 0xd70087, 0xd700af, 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f,
0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 0xd78700, 0xd7875f, 0xd78787, 0xd787af,
0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff,
0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f,
0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff, 0xff0000, 0xff005f, 0xff0087, 0xff00af,
0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff,
0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f,
0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 0xffd700, 0xffd75f, 0xffd787, 0xffd7af,
0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff,
0x080808, 0x121212, 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e,
0x585858, 0x626262, 0x6c6c6c, 0x767676, 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e,
0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee
};
return 16 + convert_color(rgb, kColors, sizeof kColors / sizeof *kColors);
}
unsigned char rgb_color_t::to_term256_index() const {
assert(type == type_rgb);
return term256_color_for_rgb(data.rgb);
}
unsigned char rgb_color_t::to_name_index() const {
if (type == type_named) {
return data.name_idx;
} else if (type == type_rgb) {
return term8_color_for_rgb(data.rgb);
} else {
throw "Bad type for to_name_index";
}
}
void rgb_color_t::parse(const wcstring &str) {
bool success = false;
if (! success) success = try_parse_special(str);
if (! success) success = try_parse_named(str);
if (! success) success = try_parse_rgb(str);
if (! success) {
bzero(this->data.rgb, sizeof this->data.rgb);
this->type = type_none;
}
}
rgb_color_t::rgb_color_t(const wcstring &str) {
this->parse(str);
}
rgb_color_t::rgb_color_t(const std::string &str) {
this->parse(str2wcstring(str));
}
wcstring rgb_color_t::description() const {
switch (type) {
case type_none:
return L"none";
case type_named:
return format_string(L"named(%d: %ls)", (int)data.name_idx, name_for_color_idx(data.name_idx));
case type_rgb:
return format_string(L"rgb(0x%02x%02x%02x)", data.rgb[0], data.rgb[1], data.rgb[2]);
case type_reset:
return L"reset";
case type_normal:
return L"normal";
case type_ignore:
return L"ignore";
default:
abort();
return L"";
}
}

133
color.h Normal file
View File

@@ -0,0 +1,133 @@
/** \file color.h Color class.
*/
#ifndef FISH_COLOR_H
#define FISH_COLOR_H
#include <stdint.h>
#include <cstddef>
#include "config.h"
#include "common.h"
/* A type that represents a color. We work hard to keep it at a size of 4 bytes. */
class rgb_color_t {
/* Types */
enum {
type_none,
type_named,
type_rgb,
type_normal,
type_reset,
type_ignore
};
unsigned char type:4;
/* Flags */
enum {
flag_bold = 1 << 0,
flag_underline = 1 << 1
};
unsigned char flags:4;
union {
unsigned char name_idx; //0-10
unsigned char rgb[3];
} data;
/** Try parsing a special color name like "normal" */
bool try_parse_special(const wcstring &str);
/** Try parsing an rgb color like "#F0A030" */
bool try_parse_rgb(const wcstring &str);
/** Try parsing an explicit color name like "magenta" */
bool try_parse_named(const wcstring &str);
/* Parsing entry point */
void parse(const wcstring &str);
/** Private constructor */
explicit rgb_color_t(unsigned char t, unsigned char i=0);
public:
/** Default constructor of type none */
explicit rgb_color_t() : type(type_none), flags(), data() {}
/** Parse a color from a string */
explicit rgb_color_t(const wcstring &str);
explicit rgb_color_t(const std::string &str);
/** Returns white */
static rgb_color_t white();
/** Returns black */
static rgb_color_t black();
/** Returns the reset special color */
static rgb_color_t reset();
/** Returns the normal special color */
static rgb_color_t normal();
/** Returns the ignore special color */
static rgb_color_t ignore();
/** Returns the none special color */
static rgb_color_t none();
/** Returns whether the color is the ignore special color */
bool is_ignore(void) const { return type == type_ignore; }
/** Returns whether the color is the normal special color */
bool is_normal(void) const { return type == type_normal; }
/** Returns whether the color is the reset special color */
bool is_reset(void) const { return type == type_reset; }
/** Returns whether the color is the none special color */
bool is_none(void) const { return type == type_none; }
/** Returns whether the color is a named color (like "magenta") */
bool is_named(void) const { return type == type_named; }
/** Returns whether the color is specified via RGB components */
bool is_rgb(void) const { return type == type_rgb; }
/** Returns whether the color is special, that is, not rgb or named */
bool is_special(void) const { return type != type_named && type != type_rgb; }
/** Returns a description of the color */
wcstring description() const;
/** Returns the name index for the given color. Requires that the color be named or RGB. */
unsigned char to_name_index() const;
/** Returns the term256 index for the given color. Requires that the color be named or RGB. */
unsigned char to_term256_index() const;
/** Returns whether the color is bold */
bool is_bold() const { return flags & flag_bold; }
/** Set whether the color is bold */
void set_bold(bool x) { if (x) flags |= flag_bold; else flags &= ~flag_bold; }
/** Returns whether the color is underlined */
bool is_underline() const { return !! (flags & flag_underline); }
/** Set whether the color is underlined */
void set_underline(bool x) { if (x) flags |= flag_underline; else flags &= ~flag_underline; }
/** Compare two colors for equality */
bool operator==(const rgb_color_t &other) const {
return type == other.type && ! memcmp(&data, &other.data, sizeof data);
}
/** Compare two colors for inequality */
bool operator!=(const rgb_color_t &other) const {
return !(*this == other);
}
};
#endif

1859
common.c

File diff suppressed because it is too large Load Diff

2021
common.cpp Normal file

File diff suppressed because it is too large Load Diff

409
common.h
View File

@@ -12,9 +12,22 @@
#include <stdio.h> #include <stdio.h>
#include <wchar.h> #include <wchar.h>
#include <termios.h> #include <termios.h>
#include <string>
#include <sstream>
#include <vector>
#include <pthread.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include "util.h" #include "util.h"
class completion_t;
/* Common string type */
typedef std::wstring wcstring;
typedef std::vector<wcstring> wcstring_list_t;
/** /**
Maximum number of bytes used by a single utf-8 character Maximum number of bytes used by a single utf-8 character
*/ */
@@ -59,6 +72,13 @@
*/ */
#define ESCAPE_NO_QUOTED 2 #define ESCAPE_NO_QUOTED 2
/**
Helper macro for errors
*/
#define VOMIT_ON_FAILURE(a) do { if (0 != (a)) { int err = errno; fprintf(stderr, "%s failed on line %d in file %s: %d (%s)\n", #a, __LINE__, __FILE__, err, strerror(err)); abort(); }} while (0)
/** Exits without invoking destructors (via _exit), useful for code after fork. */
void exit_without_destructors(int code) __attribute__ ((noreturn));
/** /**
Save the shell mode on startup so we can restore them on exit Save the shell mode on startup so we can restore them on exit
@@ -86,7 +106,7 @@ extern char *profile;
Name of the current program. Should be set at startup. Used by the Name of the current program. Should be set at startup. Used by the
debug function. debug function.
*/ */
extern wchar_t *program_name; extern const wchar_t *program_name;
/** /**
This macro is used to check that an input argument is not null. It This macro is used to check that an input argument is not null. It
@@ -114,7 +134,7 @@ extern wchar_t *program_name;
int exit_read_count;char exit_read_buff; \ int exit_read_count;char exit_read_buff; \
show_stackframe(); \ show_stackframe(); \
exit_read_count=read( 0, &exit_read_buff, 1 ); \ exit_read_count=read( 0, &exit_read_buff, 1 ); \
exit( 1 ); \ exit_without_destructors( 1 ); \
} \ } \
@@ -148,7 +168,7 @@ extern wchar_t *program_name;
/** /**
Shorthand for wgettext call Shorthand for wgettext call
*/ */
#define _(wstr) wgettext(wstr) #define _(wstr) wgettext((const wchar_t *)wstr)
/** /**
Noop, used to tell xgettext that a string should be translated, Noop, used to tell xgettext that a string should be translated,
@@ -159,23 +179,15 @@ extern wchar_t *program_name;
/** /**
Check if the specified stringelement is a part of the specified string list Check if the specified stringelement is a part of the specified string list
*/ */
#define contains( str,... ) contains_internal( str, __VA_ARGS__, (void *)0 ) #define contains( str,... ) contains_internal( str, __VA_ARGS__, NULL )
/**
Concatenate all the specified strings into a single newly allocated one
*/
#define wcsdupcat( str,... ) wcsdupcat_internal( str, __VA_ARGS__, (void *)0 )
/** /**
Print a stack trace to stderr Print a stack trace to stderr
*/ */
void show_stackframe(); void show_stackframe();
/**
Take an array_list_t containing wide strings and converts them to a wcstring_list_t completions_to_wcstring_list( const std::vector<completion_t> &completions );
single null-terminated wchar_t **. The array is allocated using
malloc, and needs to be fred's by the caller.
*/
wchar_t **list_to_char_arr( array_list_t *l );
/** /**
Read a line from the stream f into the buffer buff of length len. If Read a line from the stream f into the buffer buff of length len. If
@@ -190,11 +202,9 @@ wchar_t **list_to_char_arr( array_list_t *l );
*/ */
int fgetws2( wchar_t **buff, int *len, FILE *f ); int fgetws2( wchar_t **buff, int *len, FILE *f );
/** void sort_strings( std::vector<wcstring> &strings);
Sorts an array_list of wide strings according to the
wcsfilecmp-function from the util library void sort_completions( std::vector<completion_t> &strings);
*/
void sort_list( array_list_t *comp );
/** /**
Returns a newly allocated wide character string equivalent of the Returns a newly allocated wide character string equivalent of the
@@ -205,6 +215,16 @@ void sort_list( array_list_t *comp );
*/ */
wchar_t *str2wcs( const char *in ); wchar_t *str2wcs( const char *in );
/**
Returns a newly allocated wide character string equivalent of the
specified multibyte character string
This function encodes illegal character sequences in a reversible
way using the private use area.
*/
wcstring str2wcstring( const char *in );
wcstring str2wcstring( const std::string &in );
/** /**
Converts the narrow character string \c in into it's wide Converts the narrow character string \c in into it's wide
equivalent, stored in \c out. \c out must have enough space to fit equivalent, stored in \c out. \c out must have enough space to fit
@@ -223,6 +243,35 @@ wchar_t *str2wcs_internal( const char *in, wchar_t *out );
way using the private use area. way using the private use area.
*/ */
char *wcs2str( const wchar_t *in ); char *wcs2str( const wchar_t *in );
std::string wcs2string(const wcstring &input);
/** Test if a string prefixes another. Returns true if a is a prefix of b */
bool string_prefixes_string(const wcstring &proposed_prefix, const wcstring &value);
bool string_prefixes_string(const wchar_t *proposed_prefix, const wcstring &value);
/** Test if a string is a suffix of another */
bool string_suffixes_string(const wcstring &proposed_suffix, const wcstring &value);
bool string_suffixes_string(const wchar_t *proposed_suffix, const wcstring &value);
/** Test if a string prefixes another without regard to case. Returns true if a is a prefix of b */
bool string_prefixes_string_case_insensitive(const wcstring &proposed_prefix, const wcstring &value);
/** Test if a list contains a string using a linear search. */
bool list_contains_string(const wcstring_list_t &list, const wcstring &str);
void assert_is_main_thread(const char *who);
#define ASSERT_IS_MAIN_THREAD_TRAMPOLINE(x) assert_is_main_thread(x)
#define ASSERT_IS_MAIN_THREAD() ASSERT_IS_MAIN_THREAD_TRAMPOLINE(__FUNCTION__)
void assert_is_background_thread(const char *who);
#define ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(x) assert_is_background_thread(x)
#define ASSERT_IS_BACKGROUND_THREAD() ASSERT_IS_BACKGROUND_THREAD_TRAMPOLINE(__FUNCTION__)
/* Useful macro for asserting that a lock is locked. This doesn't check whether this thread locked it, which it would be nice if it did, but here it is anyways. */
void assert_is_locked(void *mutex, const char *who, const char *caller);
#define ASSERT_IS_LOCKED(x) assert_is_locked((void *)(&x), #x, __FUNCTION__)
/** /**
Converts the wide character string \c in into it's narrow Converts the wide character string \c in into it's narrow
@@ -234,28 +283,244 @@ char *wcs2str( const wchar_t *in );
*/ */
char *wcs2str_internal( const wchar_t *in, char *out ); char *wcs2str_internal( const wchar_t *in, char *out );
/** Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer. */
wcstring format_size(long long sz);
/** Version of format_size that does not allocate memory. */
void format_size_safe(char buff[128], unsigned long long sz);
/** Our crappier versions of debug which is guaranteed to not allocate any memory, or do anything other than call write(). This is useful after a call to fork() with threads. */
void debug_safe(int level, const char *msg, const char *param1 = NULL, const char *param2 = NULL, const char *param3 = NULL, const char *param4 = NULL, const char *param5 = NULL, const char *param6 = NULL, const char *param7 = NULL, const char *param8 = NULL, const char *param9 = NULL, const char *param10 = NULL, const char *param11 = NULL, const char *param12 = NULL);
/** Writes out a long safely */
void format_long_safe(char buff[128], long val);
void format_long_safe(wchar_t buff[128], long val);
template<typename T>
T from_string(const wcstring &x) {
T result;
std::wstringstream stream(x);
stream >> result;
return result;
}
template<typename T>
T from_string(const std::string &x) {
T result = T();
std::stringstream stream(x);
stream >> result;
return result;
}
template<typename T>
wcstring to_string(const T &x) {
std::wstringstream stream;
stream << x;
return stream.str();
}
/* wstringstream is a huge memory pig. Let's provide some specializations where we can. */
template<>
inline wcstring to_string(const long &x) {
wchar_t buff[128];
format_long_safe(buff, x);
return wcstring(buff);
}
template<>
inline bool from_string(const std::string &x) {
return ! x.empty() && strchr("YTyt1", x.at(0));
}
template<>
inline bool from_string(const wcstring &x) {
return ! x.empty() && wcschr(L"YTyt1", x.at(0));
}
template<>
inline wcstring to_string(const int &x) {
return to_string(static_cast<long>(x));
}
/* Helper class for managing a null-terminated array of null-terminated strings (of some char type) */
template <typename CharType_t>
class null_terminated_array_t {
CharType_t **array;
typedef std::basic_string<CharType_t> string_t;
typedef std::vector<string_t> string_list_t;
void swap(null_terminated_array_t<CharType_t> &him) { std::swap(array, him.array); }
/* Silly function to get the length of a null terminated array of...something */
template <typename T>
static size_t count_not_null(const T *arr) {
size_t len;
for (len=0; arr[len] != T(0); len++)
;
return len;
}
size_t size() const {
return count_not_null(array);
}
void free(void) {
if (array != NULL) {
for (size_t i = 0; array[i] != NULL; i++) {
delete [] array[i];
}
delete [] array;
array = NULL;
}
}
public:
null_terminated_array_t() : array(NULL) { }
null_terminated_array_t(const string_list_t &argv) : array(NULL) { this->set(argv); }
~null_terminated_array_t() { this->free(); }
/** operator=. Notice the pass-by-value parameter. */
null_terminated_array_t& operator=(null_terminated_array_t rhs) {
if (this != &rhs)
this->swap(rhs);
return *this;
}
/* Copy constructor. */
null_terminated_array_t(const null_terminated_array_t &him) : array(NULL) {
this->set(him.array);
}
void set(const string_list_t &argv) {
/* Get rid of the old argv */
this->free();
/* Allocate our null-terminated array of null-terminated strings */
size_t i, count = argv.size();
this->array = new CharType_t * [count + 1];
for (i=0; i < count; i++) {
const string_t &str = argv.at(i);
this->array[i] = new CharType_t [1 + str.size()];
std::copy(str.begin(), str.end(), this->array[i]);
this->array[i][str.size()] = CharType_t(0);
}
this->array[count] = NULL;
}
void set(const CharType_t * const *new_array) {
if (new_array == array)
return;
/* Get rid of the old argv */
this->free();
/* Copy the new one */
if (new_array) {
size_t i, count = count_not_null(new_array);
this->array = new CharType_t * [count + 1];
for (i=0; i < count; i++) {
size_t len = count_not_null(new_array[i]);
this->array[i] = new CharType_t [1 + len];
std::copy(new_array[i], new_array[i] + len, this->array[i]);
this->array[i][len] = CharType_t(0);
}
this->array[count] = NULL;
}
}
CharType_t **get() { return array; }
const CharType_t * const *get() const { return array; }
string_list_t to_list() const {
string_list_t lst;
if (array != NULL) {
size_t count = this->size();
lst.reserve(count);
lst.insert(lst.end(), array, array + count);
}
return lst;
}
};
/* Helper function to convert from a null_terminated_array_t<wchar_t> to a null_terminated_array_t<char_t> */
null_terminated_array_t<char> convert_wide_array_to_narrow(const null_terminated_array_t<wchar_t> &arr);
/* Helper class to cache a narrow version of a wcstring in a malloc'd buffer, so that we can read it after fork() */
class narrow_string_rep_t {
private:
const char *str;
public:
~narrow_string_rep_t() {
free((void *)str);
}
narrow_string_rep_t() : str(NULL) {}
void set(const wcstring &s) {
free((void *)str);
str = wcs2str(s.c_str());
}
const char *get() const {
return str;
}
};
bool is_forked_child();
/* Basic scoped lock class */
class scoped_lock {
pthread_mutex_t *lock_obj;
bool locked;
public:
void lock(void);
void unlock(void);
scoped_lock(pthread_mutex_t &mutex);
~scoped_lock();
};
class wcstokenizer {
wchar_t *buffer, *str, *state;
const wcstring sep;
public:
wcstokenizer(const wcstring &s, const wcstring &separator) : sep(separator) {
wchar_t *wcsdup(const wchar_t *s);
buffer = wcsdup(s.c_str());
str = buffer;
state = NULL;
}
bool next(wcstring &result) {
wchar_t *tmp = wcstok(str, sep.c_str(), &state);
str = NULL;
if (tmp) result = tmp;
return tmp != NULL;
}
~wcstokenizer() {
free(buffer);
}
};
/**
Appends a path component, with a / if necessary
*/
void append_path_component(wcstring &path, const wcstring &component);
wcstring format_string(const wchar_t *format, ...);
wcstring vformat_string(const wchar_t *format, va_list va_orig);
void append_format(wcstring &str, const wchar_t *format, ...);
/** /**
Returns a newly allocated wide character string array equivalent of Returns a newly allocated wide character string array equivalent of
the specified multibyte character string array the specified multibyte character string array
*/ */
char **wcsv2strv( const wchar_t **in ); char **wcsv2strv( const wchar_t * const *in );
/**
Returns a newly allocated multibyte character string array equivalent of the specified wide character string array
*/
wchar_t **strv2wcsv( const char **in );
/**
Returns a newly allocated concatenation of the specified wide
character strings. The last argument must be a null pointer.
*/
__sentinel wchar_t *wcsdupcat_internal( const wchar_t *a, ... );
/**
Test if the given string is a valid variable name
*/
/** /**
Test if the given string is a valid variable name. Test if the given string is a valid variable name.
@@ -272,7 +537,7 @@ wchar_t *wcsvarname( const wchar_t *str );
\return null if this is a valid name, and a pointer to the first invalid character otherwise \return null if this is a valid name, and a pointer to the first invalid character otherwise
*/ */
wchar_t *wcsfuncname( const wchar_t *str ); const wchar_t *wcsfuncname( const wchar_t *str );
/** /**
Test if the given string is valid in a variable name Test if the given string is valid in a variable name
@@ -313,7 +578,7 @@ void error_reset();
the user is using a Unicode character set, and if so, use the the user is using a Unicode character set, and if so, use the
unicode ellipsis character as ellipsis, instead of '$'. unicode ellipsis character as ellipsis, instead of '$'.
*/ */
const wchar_t *wsetlocale( int category, const wchar_t *locale ); wcstring wsetlocale( int category, const wchar_t *locale );
/** /**
Checks if \c needle is included in the list of strings specified. A warning is printed if needle is zero. Checks if \c needle is included in the list of strings specified. A warning is printed if needle is zero.
@@ -322,7 +587,8 @@ const wchar_t *wsetlocale( int category, const wchar_t *locale );
\return zero if needle is not found, of if needle is null, non-zero otherwise \return zero if needle is not found, of if needle is null, non-zero otherwise
*/ */
__sentinel int contains_internal( const wchar_t *needle, ... ); __sentinel bool contains_internal( const wchar_t *needle, ... );
__sentinel bool contains_internal( const wcstring &needle, ... );
/** /**
Call read while blocking the SIGCHLD signal. Should only be called Call read while blocking the SIGCHLD signal. Should only be called
@@ -335,7 +601,13 @@ int read_blocked(int fd, void *buf, size_t count);
Loop a write request while failiure is non-critical. Return -1 and set errno Loop a write request while failiure is non-critical. Return -1 and set errno
in case of critical error. in case of critical error.
*/ */
ssize_t write_loop(int fd, char *buff, size_t count); ssize_t write_loop(int fd, const char *buff, size_t count);
/**
Loop a read request while failiure is non-critical. Return -1 and set errno
in case of critical error.
*/
ssize_t read_loop(int fd, void *buff, size_t count);
/** /**
@@ -368,6 +640,7 @@ void debug( int level, const wchar_t *msg, ... );
*/ */
wchar_t *escape( const wchar_t *in, int escape_all ); wchar_t *escape( const wchar_t *in, int escape_all );
wcstring escape_string( const wcstring &in, int escape_all );
/** /**
Expand backslashed escapes and substitute them with their unescaped Expand backslashed escapes and substitute them with their unescaped
@@ -383,16 +656,9 @@ wchar_t *escape( const wchar_t *in, int escape_all );
wchar_t *unescape( const wchar_t * in, wchar_t *unescape( const wchar_t * in,
int escape_special ); int escape_special );
/** bool unescape_string( wcstring &str,
Attempt to acquire a lock based on a lockfile, waiting LOCKPOLLINTERVAL int escape_special );
milliseconds between polls and timing out after timeout seconds,
thereafter forcibly attempting to obtain the lock if force is non-zero.
Returns 1 on success, 0 on failure.
To release the lock the lockfile must be unlinked.
A unique temporary file named by appending characters to the lockfile name
is used; any pre-existing file of the same name is subject to deletion.
*/
int acquire_lock_file( const char *lockfile, const int timeout, int force );
/** /**
Returns the width of the terminal window, so that not all Returns the width of the terminal window, so that not all
@@ -422,17 +688,14 @@ void common_handle_winch( int signal );
Write paragraph of output to the specified stringbuffer, and redo Write paragraph of output to the specified stringbuffer, and redo
the linebreaks to fit the current screen. the linebreaks to fit the current screen.
*/ */
void write_screen( const wchar_t *msg, string_buffer_t *buff ); void write_screen( const wcstring &msg, wcstring &buff );
/** /**
Tokenize the specified string into the specified array_list_t. Tokenize the specified string into the specified wcstring_list_t.
Each new element is allocated using malloc and must be freed by the
caller.
\param val the input string. The contents of this string is not changed. \param val the input string. The contents of this string is not changed.
\param out the list in which to place the elements. \param out the list in which to place the elements.
*/ */
void tokenize_variable_array( const wchar_t *val, array_list_t *out ); void tokenize_variable_array( const wcstring &val, wcstring_list_t &out);
/** /**
Make sure the specified direcotry exists. If needed, try to create Make sure the specified direcotry exists. If needed, try to create
@@ -440,19 +703,13 @@ void tokenize_variable_array( const wchar_t *val, array_list_t *out );
\return 0 if, at the time of function return the directory exists, -1 otherwise. \return 0 if, at the time of function return the directory exists, -1 otherwise.
*/ */
int create_directory( wchar_t *d ); int create_directory( const wcstring &d );
/** /**
Print a short message about how to file a bug report to stderr Print a short message about how to file a bug report to stderr
*/ */
void bugreport(); void bugreport();
/**
Format the specified size (in bytes, kilobytes, etc.) into the specified stringbuffer.
*/
void sb_format_size( string_buffer_t *sb,
long long sz );
/** /**
Return the number of seconds from the UNIX epoch, with subsecond Return the number of seconds from the UNIX epoch, with subsecond
precision. This function uses the gettimeofday function, and will precision. This function uses the gettimeofday function, and will
@@ -462,6 +719,28 @@ void sb_format_size( string_buffer_t *sb,
*/ */
double timef(); double timef();
/**
Call the following function early in main to set the main thread.
This is our replacement for pthread_main_np().
*/
void set_main_thread();
bool is_main_thread();
/** Configures thread assertions for testing */
void configure_thread_assertions_for_testing();
/** Set up a guard to complain if we try to do certain things (like take a lock) after calling fork */
void setup_fork_guards(void);
/** Return whether we are the child of a fork */
bool is_forked_child(void);
void assert_is_not_forked_child(const char *who);
#define ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(x) assert_is_not_forked_child(x)
#define ASSERT_IS_NOT_FORKED_CHILD() ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(__FUNCTION__)
extern "C" {
__attribute__((noinline)) void debug_thread_error(void);
}
#endif #endif

2158
complete.c

File diff suppressed because it is too large Load Diff

2004
complete.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -12,10 +12,11 @@
*/ */
#define FISH_COMPLETE_H #define FISH_COMPLETE_H
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
#include "common.h"
/** /**
Use all completions Use all completions
*/ */
@@ -66,53 +67,60 @@
*/ */
#define PROG_COMPLETE_SEP L'\t' #define PROG_COMPLETE_SEP L'\t'
/** enum {
Do not insert space afterwards if this is the only completion. (The /**
default is to try insert a space) Do not insert space afterwards if this is the only completion. (The
*/ default is to try insert a space)
#define COMPLETE_NO_SPACE 1 */
COMPLETE_NO_SPACE = 1 << 0,
/** /**
This compeltion is case insensitive. This compeltion is case insensitive.
Warning: The contents of the completion_t structure is actually Warning: The contents of the completion_t structure is actually
different if this flag is set! Specifically, the completion string different if this flag is set! Specifically, the completion string
contains the _entire_ completion token, not only the current contains the _entire_ completion token, not only the current
*/ */
#define COMPLETE_NO_CASE 2 COMPLETE_NO_CASE = 1 << 1,
/** /**
This compeltion is the whole argument, not just the remainder. This This compeltion is the whole argument, not just the remainder. This
flag must never be set on completions returned from the complete() flag must never be set on completions returned from the complete()
function. It is strictly for internal use in the completion code. function. It is strictly for internal use in the completion code.
*/ */
#define COMPLETE_WHOLE_ARGUMENT 4 COMPLETE_WHOLE_ARGUMENT = 1 << 2,
/** /**
This completion may or may not want a space at the end - guess by This completion may or may not want a space at the end - guess by
checking the last character of the completion. checking the last character of the completion.
*/ */
#define COMPLETE_AUTO_SPACE 8 COMPLETE_AUTO_SPACE = 1 << 3,
/** /**
This completion should be inserted as-is, without escaping. This completion should be inserted as-is, without escaping.
*/ */
#define COMPLETE_DONT_ESCAPE 16 COMPLETE_DONT_ESCAPE = 1 << 4
};
typedef int complete_flags_t;
class completion_t
typedef struct
{ {
private:
/* No public default constructor */
completion_t(){ }
public:
/** /**
The completion string The completion string
*/ */
const wchar_t *completion; wcstring completion;
/** /**
The description for this completion The description for this completion
*/ */
const wchar_t *description; wcstring description;
/** /**
Flags determining the completion behaviour. Flags determining the completion behaviour.
@@ -126,9 +134,26 @@ typedef struct
*/ */
int flags; int flags;
} completion_t(const wcstring &comp, const wcstring &desc = L"", int flags_val = 0) : completion(comp), description(desc), flags(flags_val) {
completion_t; if( flags & COMPLETE_AUTO_SPACE )
{
flags = flags & ~COMPLETE_AUTO_SPACE;
size_t len = completion.size();
if (len > 0 && ( wcschr( L"/=@:", comp.at(len-1)) != 0 ))
flags |= COMPLETE_NO_SPACE;
}
}
bool operator < (const completion_t& rhs) const { return this->completion < rhs.completion; }
bool operator == (const completion_t& rhs) const { return this->completion == rhs.completion; }
bool operator != (const completion_t& rhs) const { return ! (*this == rhs); }
};
enum complete_type_t {
COMPLETE_DEFAULT,
COMPLETE_AUTOSUGGEST
};
/** /**
@@ -173,7 +198,7 @@ typedef struct
\param flags A set of completion flags \param flags A set of completion flags
*/ */
void complete_add( const wchar_t *cmd, void complete_add( const wchar_t *cmd,
int cmd_type, bool cmd_is_path,
wchar_t short_opt, wchar_t short_opt,
const wchar_t *long_opt, const wchar_t *long_opt,
int long_mode, int long_mode,
@@ -187,43 +212,34 @@ void complete_add( const wchar_t *cmd,
true, any options not matching one of the provided options will be true, any options not matching one of the provided options will be
flagged as an error by syntax highlighting. flagged as an error by syntax highlighting.
*/ */
void complete_set_authoritative( const wchar_t *cmd, void complete_set_authoritative( const wchar_t *cmd, bool cmd_type, bool authoritative );
int cmd_type,
int authoritative );
/** /**
Remove a previously defined completion Remove a previously defined completion
*/ */
void complete_remove( const wchar_t *cmd, void complete_remove( const wchar_t *cmd,
int cmd_type, bool cmd_is_path,
wchar_t short_opt, wchar_t short_opt,
const wchar_t *long_opt ); const wchar_t *long_opt );
/**
Find all completions of the command cmd, insert them into out. The
caller must free the variables returned in out. The results are
returned in the array_list_t 'out', in the format of wide character
strings, with each element consisting of a suggested completion and
a description of what kind of object this completion represents,
separated by a separator of type COMPLETE_SEP.
Values returned by this function should be freed by the caller. /** Find all completions of the command cmd, insert them into out. If to_load is not NULL, append all commands that we would autoload, but did not (presumably because this is not the main thread) */
*/ void complete( const wcstring &cmd, std::vector<completion_t> &comp, complete_type_t type, wcstring_list_t *to_load = NULL );
void complete( const wchar_t *cmd, array_list_t *out );
/** /**
Print a list of all current completions into the string_buffer_t. Print a list of all current completions into the string.
\param out The string_buffer_t to write completions to \param out The string to write completions to
*/ */
void complete_print( string_buffer_t *out ); void complete_print( wcstring &out );
/** /**
Tests if the specified option is defined for the specified command Tests if the specified option is defined for the specified command
*/ */
int complete_is_valid_option( const wchar_t *str, int complete_is_valid_option( const wchar_t *str,
const wchar_t *opt, const wchar_t *opt,
array_list_t *errors ); wcstring_list_t *inErrorsOrNull,
bool allow_autoload );
/** /**
Tests if the specified argument is valid for the specified option Tests if the specified argument is valid for the specified option
@@ -243,20 +259,18 @@ int complete_is_valid_argument( const wchar_t *str,
\param cmd the command for which to load command-specific completions \param cmd the command for which to load command-specific completions
\param reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells) \param reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells)
*/ */
void complete_load( const wchar_t *cmd, int reload ); void complete_load( const wcstring &cmd, bool reload );
/** /**
Create a new completion entry Create a new completion entry
\param context The halloc context to use for allocating new memory \param completions The array of completions to append to
\param comp The completion string \param comp The completion string
\param desc The description of the completion \param desc The description of the completion
\param flags completion flags \param flags completion flags
*/ */
void completion_allocate( array_list_t *context, void completion_allocate(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc, int flags);
const wchar_t *comp,
const wchar_t *desc,
int flags );
#endif #endif

View File

@@ -110,7 +110,7 @@ for i in /usr/pkg /sw /opt /opt/local; do
AC_MSG_CHECKING([for $i/include include directory]) AC_MSG_CHECKING([for $i/include include directory])
if test -d $i/include; then if test -d $i/include; then
AC_MSG_RESULT(yes) AC_MSG_RESULT(yes)
CPPFLAGS="$CPPFLAGS -I$i/include/" CXXFLAGS="$CXXFLAGS -I$i/include/"
CFLAGS="$CFLAGS -I$i/include/" CFLAGS="$CFLAGS -I$i/include/"
else else
AC_MSG_RESULT(no) AC_MSG_RESULT(no)
@@ -159,10 +159,7 @@ AH_BOTTOM([#if __GNUC__ >= 3
# Set up various programs needed for install # Set up various programs needed for install
# #
# Here we look for c99 before cc as Sun Studio compiler supports c99 AC_PROG_CXX([g++ c++])
# through the c99 binary.
AC_PROG_CC([gcc c99 cc])
AC_PROG_CPP AC_PROG_CPP
AC_PROG_INSTALL AC_PROG_INSTALL
@@ -239,55 +236,13 @@ if test x$local_gettext != xno; then
fi fi
#
# Test if the compiler accepts the -std=c99 flag. If so, using it
# increases the odds of correct compilation, since we want to use the
# *wprintf functions, which where defined in C99.
#
# NOTE: Never versions of autoconf has AC_CHECK_PROG_CC_C99
#
if test "$CC" != "c99"; then
XCFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -std=c99"
XCPPFLAGS="$CPPFLAGS"
CPPFLAGS="$CPPFLAGS -std=c99"
AC_MSG_CHECKING(if -std=c99 works)
AC_CACHE_VAL(
local_cv_has__std_c99,
[
AC_TRY_RUN(
[
#include <stdlib.h>
#include <stdio.h>
int main()
{
return 0;
}
],
local_cv_has__std_c99=yes,
local_cv_has__std_c99=no,
)
]
)
AC_MSG_RESULT($local_cv_has__std_c99)
case x$local_cv_has__std_c99 in
xno)
CFLAGS="$XCFLAGS"
CPPFLAGS="$XCPPFLAGS" ;;
esac
fi
# #
# Try to enable large file support. This will make sure that on systems # Try to enable large file support. This will make sure that on systems
# where off_t can be either 32 or 64 bit, the latter size is used. On # where off_t can be either 32 or 64 bit, the latter size is used. On
# other systems, this should do nothing. (Hopefully) # other systems, this should do nothing. (Hopefully)
# #
CFLAGS="$CFLAGS -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64" CXXFLAGS="$CXXFLAGS -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64"
# #
# If we are using gcc, set some flags that increase the odds of the # If we are using gcc, set some flags that increase the odds of the
@@ -306,14 +261,15 @@ if test "$GCC" = yes; then
# bug has been verified to not exist on Linux using GCC 3.3.3. # bug has been verified to not exist on Linux using GCC 3.3.3.
# #
CFLAGS="$CFLAGS -fno-optimize-sibling-calls" CXXFLAGS="$CXXFLAGS -fno-optimize-sibling-calls"
# #
# -Wall is there to keep me on my toes # -Wall is there to keep me on my toes
# #
CFLAGS="$CFLAGS -Wall" # Some day...
CXXFLAGS="$CXXFLAGS -Wall"
# #
# This is needed in order to get the really cool backtraces # This is needed in order to get the really cool backtraces
@@ -512,6 +468,7 @@ LIBS_COMMON=$LIBS
LIBS="" LIBS=""
AC_SEARCH_LIBS( connect, socket, , [AC_MSG_ERROR([Cannot find the socket library, needed to build this package.] )] ) AC_SEARCH_LIBS( connect, socket, , [AC_MSG_ERROR([Cannot find the socket library, needed to build this package.] )] )
AC_SEARCH_LIBS( nanosleep, rt, , [AC_MSG_ERROR([Cannot find the rt library, needed to build this package.] )] ) AC_SEARCH_LIBS( nanosleep, rt, , [AC_MSG_ERROR([Cannot find the rt library, needed to build this package.] )] )
AC_SEARCH_LIBS( pthread_create, pthread, , [AC_MSG_ERROR([Cannot find the pthread library, needed to build this package.] )] )
AC_SEARCH_LIBS( setupterm, [ncurses curses], , [AC_MSG_ERROR([Could not find a curses implementation, needed to build fish])] ) AC_SEARCH_LIBS( setupterm, [ncurses curses], , [AC_MSG_ERROR([Could not find a curses implementation, needed to build fish])] )
AC_SEARCH_LIBS( [nan], [m], [AC_DEFINE( [HAVE_NAN], [1], [Define to 1 if you have the nan function])] ) AC_SEARCH_LIBS( [nan], [m], [AC_DEFINE( [HAVE_NAN], [1], [Define to 1 if you have the nan function])] )
LIBS_SHARED=$LIBS LIBS_SHARED=$LIBS
@@ -645,7 +602,7 @@ AC_CHECK_HEADER(
# conditional definition of __EXTENSIONS__, to avoid redundant tests. # conditional definition of __EXTENSIONS__, to avoid redundant tests.
# #
XCFLAGS="$CFLAGS" XCFLAGS="$CXXFLAGS"
echo Checking how to use -D_XOPEN_SOURCE=600 and -D_POSIX_C_SOURCE=200112L... echo Checking how to use -D_XOPEN_SOURCE=600 and -D_POSIX_C_SOURCE=200112L...
local_found_posix_switch=no local_found_posix_switch=no
@@ -695,19 +652,19 @@ for i in "" "-D_POSIX_C_SOURCE=200112L" "-D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=2
[ [
/* Avert high-level optimisation, by making the program's /* Avert high-level optimisation, by making the program's
* return value depend on all tested identifiers. */ * return value depend on all tested identifiers. */
int ret = 0; long ret = 0;
/* POSIX only: might be unhidden by _POSIX_C_SOURCE. */ /* POSIX only: might be unhidden by _POSIX_C_SOURCE. */
struct sigaction sa; struct sigaction sa;
sigset_t ss; sigset_t ss;
siginfo_t info; siginfo_t info;
ret += (int)(void *)&info + kill( 0, 0 ) + ret += (long)(void *)&info + kill( 0, 0 ) +
sigaction( 0, &sa, 0 ) + sigemptyset( &ss ); sigaction( 0, &sa, 0 ) + sigemptyset( &ss );
/* Extended-POSIX: might be unhidden by _XOPEN_SOURCE. */ /* Extended-POSIX: might be unhidden by _XOPEN_SOURCE. */
ret += killpg( 0, 0 ); ret += killpg( 0, 0 );
/* Non-standard: might be hidden by the macros. */ /* Non-standard: might be hidden by the macros. */
{ {
struct winsize termsize; struct winsize termsize;
ret += (int)(void *)&termsize; ret += (long)(void *)&termsize;
ret += SIGWINCH + TIOCGWINSZ + SIGIO; ret += SIGWINCH + TIOCGWINSZ + SIGIO;
} }
return ret; return ret;

5
doc_src/fish_config.txt Normal file
View File

@@ -0,0 +1,5 @@
\section fish_config fish_config - Start up the web-based configuration interface
\subsection fish_config-description Description
This command starts up the web-based configuration interface, which allows you to edit your colors and view your functions, variables, and history.

View File

@@ -0,0 +1,6 @@
\section fish_update_completions fish_update_completions - Update man-page completions
\subsection fish_update_completions-description Description
This command parses your installed man pages and writes completion files to the fish config directory. This does not overwrite custom completions.

36
doc_src/history.txt Normal file
View File

@@ -0,0 +1,36 @@
\section history history - Show and manipulate user's command history
\subsection history-synopsis Synopsis
<pre>
history (--save | --clear)
history (--search | --delete ) (--prefix "prefix string" | --search "search string")
</pre>
\subsection history-description Description
history is used to list, search and delete user's command history.
\subsection history-examples Example
<pre>
history --save
Save all changes in history file.
history --clear
Delete all history items.
history --search --contains "foo"
Searches commands containing "foo" string.
history --search --prefix "foo"
Searches for commands with prefix "foo".
history --delete --contains "foo"
Interactively delete commands containing string "foo".
history --delete --prefix "foo"
Interactively delete commands with prefix "foo".
history --delete "foo"
Delete command "foo" from history.
<pre>

View File

@@ -9,6 +9,10 @@ Change the foreground and/or background color of the terminal.
COLOR is one of black, red, green, brown, yellow, blue, magenta, COLOR is one of black, red, green, brown, yellow, blue, magenta,
purple, cyan, white and normal. purple, cyan, white and normal.
If your terminal supports term256 (modern xterms and OS X Lion),
you can specify an RGB value with three or six hex digits, such
as A0FF33 or f2f. fish will choose the closest supported color.
- \c -b, \c --background Set the background color - \c -b, \c --background Set the background color
- \c -c, \c --print-colors Prints a list of all valid color names - \c -c, \c --print-colors Prints a list of all valid color names
- \c -h, \c --help Display help message and exit - \c -h, \c --help Display help message and exit

1608
env.c

File diff suppressed because it is too large Load Diff

1508
env.cpp Normal file

File diff suppressed because it is too large Load Diff

68
env.h
View File

@@ -6,8 +6,10 @@
#define FISH_ENV_H #define FISH_ENV_H
#include <wchar.h> #include <wchar.h>
#include <map>
#include "util.h" #include "util.h"
#include "common.h"
/** /**
Flag for local (to the current block) variable Flag for local (to the current block) variable
@@ -79,9 +81,7 @@ void env_destroy();
* ENV_INVALID, the variable name or mode was invalid * ENV_INVALID, the variable name or mode was invalid
*/ */
int env_set( const wchar_t *key, int env_set(const wcstring &key, const wchar_t *val, int mode);
const wchar_t *val,
int mode );
/** /**
@@ -91,7 +91,40 @@ int env_set( const wchar_t *key,
valid until the next call to env_get(), env_set(), env_push() or valid until the next call to env_get(), env_set(), env_push() or
env_pop() takes place. env_pop() takes place.
*/ */
wchar_t *env_get( const wchar_t *key ); //const wchar_t *env_get( const wchar_t *key );
class env_var_t : public wcstring {
private:
bool is_missing;
public:
static env_var_t missing_var(void);
env_var_t(const env_var_t &x) : wcstring(x), is_missing(x.is_missing) { }
env_var_t(const wcstring & x) : wcstring(x), is_missing(false) { }
env_var_t(const wchar_t *x) : wcstring(x), is_missing(false) { }
env_var_t() : wcstring(L""), is_missing(false) { }
bool missing(void) const { return is_missing; }
bool missing_or_empty(void) const { return missing() || empty(); }
const wchar_t *c_str(void) const;
env_var_t &operator=(const env_var_t &s) {
is_missing = s.is_missing;
wcstring::operator=(s);
return *this;
}
bool operator==(const env_var_t &s) const {
if (is_missing && s.is_missing)
return true;
else if (s.is_missing || s.is_missing)
return false;
else
return *static_cast<const wcstring *>(this) == *static_cast<const wcstring *>(&s);
}
};
/**
Gets the variable with the specified name, or an empty string if it does not exist.
*/
env_var_t env_get_string( const wcstring &key );
/** /**
Returns 1 if the specified key exists. This can't be reliably done Returns 1 if the specified key exists. This can't be reliably done
@@ -110,7 +143,7 @@ int env_exist( const wchar_t *key, int mode );
\return zero if the variable existed, and non-zero if the variable did not exist \return zero if the variable existed, and non-zero if the variable did not exist
*/ */
int env_remove( const wchar_t *key, int mode ); int env_remove( const wcstring &key, int mode );
/** /**
Push the variable stack. Used for implementing local variables for functions and for-loops. Push the variable stack. Used for implementing local variables for functions and for-loops.
@@ -122,15 +155,14 @@ void env_push( int new_scope );
*/ */
void env_pop(); void env_pop();
/** /** Returns an array containing all exported variables in a format suitable for execv. */
Returns an array containing all exported variables in a format suitable for execv.
*/
char **env_export_arr( int recalc ); char **env_export_arr( int recalc );
void env_export_arr(bool recalc, null_terminated_array_t<char> &result);
/** /**
Insert all variable names into l. These are not copies of the strings and should not be freed after use. Returns all variable names.
*/ */
void env_get_names( array_list_t *l, int flags ); wcstring_list_t env_get_names( int flags );
/** /**
Update the PWD variable Update the PWD variable
@@ -138,5 +170,21 @@ void env_get_names( array_list_t *l, int flags );
*/ */
int env_set_pwd(); int env_set_pwd();
class env_vars {
std::map<wcstring, wcstring> vars;
public:
env_vars(const wchar_t * const * keys);
env_vars(void);
const wchar_t *get(const wchar_t *key) const;
// vars necessary for highlighting
static const wchar_t * const highlighting_keys[];
};
extern bool g_log_forks;
extern int g_fork_count;
#endif #endif

View File

@@ -113,7 +113,7 @@ static int get_socket( int fork_ok )
uname = strdup( pw->pw_name ); uname = strdup( pw->pw_name );
} }
name = malloc( strlen(dir) + name = (char *)malloc( strlen(dir) +
strlen(uname) + strlen(uname) +
strlen(SOCK_FILENAME) + strlen(SOCK_FILENAME) +
2 ); 2 );
@@ -198,7 +198,7 @@ static void check_connection()
env_universal_server.fd = -1; env_universal_server.fd = -1;
env_universal_server.killme=0; env_universal_server.killme=0;
env_universal_server.input.used=0; env_universal_server.input.clear();
env_universal_read_all(); env_universal_read_all();
} }
} }
@@ -208,23 +208,18 @@ static void check_connection()
*/ */
static void env_universal_remove_all() static void env_universal_remove_all()
{ {
array_list_t lst; size_t i;
int i;
al_init( &lst ); wcstring_list_t lst;
env_universal_common_get_names( lst,
env_universal_common_get_names( &lst,
1, 1,
1 ); 1 );
for( i=0; i<lst.size(); i++ )
for( i=0; i<al_get_count( &lst ); i++ )
{ {
wchar_t *key = (wchar_t *)al_get( &lst, i ); const wcstring &key = lst.at(i);
env_universal_common_remove( key ); env_universal_common_remove( key );
} }
al_destroy( &lst );
} }
@@ -323,28 +318,25 @@ int env_universal_read_all()
} }
} }
wchar_t *env_universal_get( const wchar_t *name ) wchar_t *env_universal_get( const wcstring &name )
{ {
if( !init) if( !init)
return 0; return 0;
CHECK( name, 0 );
return env_universal_common_get( name ); return env_universal_common_get( name );
} }
int env_universal_get_export( const wchar_t *name ) int env_universal_get_export( const wcstring &name )
{ {
if( !init) if( !init)
return 0; return 0;
CHECK( name, 0 );
return env_universal_common_get_export( name ); return env_universal_common_get_export( name );
} }
void env_universal_barrier() void env_universal_barrier()
{ {
ASSERT_IS_MAIN_THREAD();
message_t *msg; message_t *msg;
fd_set fds; fd_set fds;
@@ -358,7 +350,7 @@ void env_universal_barrier()
*/ */
msg= create_message( BARRIER, 0, 0); msg= create_message( BARRIER, 0, 0);
msg->count=1; msg->count=1;
q_put( &env_universal_server.unsent, msg ); env_universal_server.unsent->push(msg);
/* /*
Wait until barrier request has been sent Wait until barrier request has been sent
@@ -369,7 +361,7 @@ void env_universal_barrier()
try_send_all( &env_universal_server ); try_send_all( &env_universal_server );
check_connection(); check_connection();
if( q_empty( &env_universal_server.unsent ) ) if( env_universal_server.unsent->empty() )
break; break;
if( env_universal_server.fd == -1 ) if( env_universal_server.fd == -1 )
@@ -405,26 +397,24 @@ void env_universal_barrier()
} }
void env_universal_set( const wchar_t *name, const wchar_t *value, int export ) void env_universal_set( const wcstring &name, const wcstring &value, int exportv )
{ {
message_t *msg; message_t *msg;
if( !init ) if( !init )
return; return;
CHECK( name, ); debug( 3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str() );
debug( 3, L"env_universal_set( \"%ls\", \"%ls\" )", name, value );
if( is_dead() ) if( is_dead() )
{ {
env_universal_common_set( name, value, export ); env_universal_common_set( name.c_str(), value.c_str(), exportv );
} }
else else
{ {
msg = create_message( export?SET_EXPORT:SET, msg = create_message( exportv?SET_EXPORT:SET,
name, name.c_str(),
value); value.c_str());
if( !msg ) if( !msg )
{ {
@@ -433,7 +423,7 @@ void env_universal_set( const wchar_t *name, const wchar_t *value, int export )
} }
msg->count=1; msg->count=1;
q_put( &env_universal_server.unsent, msg ); env_universal_server.unsent->push(msg);
env_universal_barrier(); env_universal_barrier();
} }
} }
@@ -455,29 +445,27 @@ int env_universal_remove( const wchar_t *name )
if( is_dead() ) if( is_dead() )
{ {
env_universal_common_remove( name ); env_universal_common_remove( wcstring(name) );
} }
else else
{ {
msg= create_message( ERASE, name, 0); msg= create_message( ERASE, name, 0);
msg->count=1; msg->count=1;
q_put( &env_universal_server.unsent, msg ); env_universal_server.unsent->push(msg);
env_universal_barrier(); env_universal_barrier();
} }
return res; return res;
} }
void env_universal_get_names( array_list_t *l, void env_universal_get_names2( wcstring_list_t &lst,
int show_exported, int show_exported,
int show_unexported ) int show_unexported )
{ {
if( !init ) if( !init )
return; return;
CHECK( l, ); env_universal_common_get_names( lst,
env_universal_common_get_names( l,
show_exported, show_exported,
show_unexported ); show_unexported );
} }

View File

@@ -17,7 +17,10 @@ extern connection_t env_universal_server;
/** /**
Initialize the envuni library Initialize the envuni library
*/ */
void env_universal_init(); void env_universal_init( wchar_t * p,
wchar_t *u,
void (*sf)(),
void (*cb)( int type, const wchar_t *name, const wchar_t *val ));
/** /**
Free memory used by envuni Free memory used by envuni
*/ */
@@ -26,18 +29,18 @@ void env_universal_destroy();
/** /**
Get the value of a universal variable Get the value of a universal variable
*/ */
wchar_t *env_universal_get( const wchar_t *name ); wchar_t *env_universal_get( const wcstring &name );
/** /**
Get the export flag of the variable with the specified Get the export flag of the variable with the specified
name. Returns 0 if the variable doesn't exist. name. Returns 0 if the variable doesn't exist.
*/ */
int env_universal_get_export( const wchar_t *name ); int env_universal_get_export( const wcstring &name );
/** /**
Set the value of a universal variable Set the value of a universal variable
*/ */
void env_universal_set( const wchar_t *name, const wchar_t *val, int export ); void env_universal_set( const wcstring &name, const wcstring &val, int exportv );
/** /**
Erase a universal variable Erase a universal variable
@@ -57,7 +60,7 @@ int env_universal_read_all();
\param show_exported whether exported variables should be shown \param show_exported whether exported variables should be shown
\param show_unexported whether unexported variables should be shown \param show_unexported whether unexported variables should be shown
*/ */
void env_universal_get_names( array_list_t *l, void env_universal_get_names2( wcstring_list_t &list,
int show_exported, int show_exported,
int show_unexported ); int show_unexported );

View File

@@ -28,6 +28,7 @@
#include <dirent.h> #include <dirent.h>
#include <signal.h> #include <signal.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <map>
#ifdef HAVE_SYS_SELECT_H #ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> #include <sys/select.h>
@@ -92,8 +93,9 @@
*/ */
typedef struct var_uni_entry typedef struct var_uni_entry
{ {
int export; /**< Whether the variable should be exported */ int exportv; /**< Whether the variable should be exported */
wchar_t val[1]; /**< The value of the variable */ wcstring val; /**< The value of the variable */
var_uni_entry():exportv(0) { }
} }
var_uni_entry_t; var_uni_entry_t;
@@ -104,7 +106,7 @@ static void parse_message( wchar_t *msg,
/** /**
The table of all universal variables The table of all universal variables
*/ */
hash_table_t env_universal_var; std::map<wcstring, var_uni_entry_t*> env_universal_var;
/** /**
Callback function, should be called on all events Callback function, should be called on all events
@@ -113,21 +115,10 @@ void (*callback)( int type,
const wchar_t *key, const wchar_t *key,
const wchar_t *val ); const wchar_t *val );
/**
Variable used by env_get_names to communicate auxiliary information
to add_key_to_hash
*/
static int get_names_show_exported;
/**
Variable used by env_get_names to communicate auxiliary information
to add_key_to_hash
*/
static int get_names_show_unexported;
/** /**
List of names for the UTF-8 character set. List of names for the UTF-8 character set.
*/ */
static char *iconv_utf8_names[]= static const char *iconv_utf8_names[]=
{ {
"utf-8", "UTF-8", "utf-8", "UTF-8",
"utf8", "UTF8", "utf8", "UTF8",
@@ -138,7 +129,7 @@ static char *iconv_utf8_names[]=
/** /**
List of wide character names, undefined byte length. List of wide character names, undefined byte length.
*/ */
static char *iconv_wide_names_unknown[]= static const char *iconv_wide_names_unknown[]=
{ {
"wchar_t", "WCHAR_T", "wchar_t", "WCHAR_T",
"wchar", "WCHAR", "wchar", "WCHAR",
@@ -149,7 +140,7 @@ static char *iconv_wide_names_unknown[]=
/** /**
List of wide character names, 4 bytes long. List of wide character names, 4 bytes long.
*/ */
static char *iconv_wide_names_4[]= static const char *iconv_wide_names_4[]=
{ {
"wchar_t", "WCHAR_T", "wchar_t", "WCHAR_T",
"wchar", "WCHAR", "wchar", "WCHAR",
@@ -164,7 +155,7 @@ static char *iconv_wide_names_4[]=
/** /**
List of wide character names, 2 bytes long. List of wide character names, 2 bytes long.
*/ */
static char *iconv_wide_names_2[]= static const char *iconv_wide_names_2[]=
{ {
"wchar_t", "WCHAR_T", "wchar_t", "WCHAR_T",
"wchar", "WCHAR", "wchar", "WCHAR",
@@ -192,7 +183,7 @@ static wchar_t *utf2wcs( const char *in )
really the character set used by wchar_t, but it is the best really the character set used by wchar_t, but it is the best
assumption we can make. assumption we can make.
*/ */
char **to_name=0; const char **to_name=0;
switch (sizeof (wchar_t)) switch (sizeof (wchar_t))
{ {
@@ -214,14 +205,14 @@ static wchar_t *utf2wcs( const char *in )
/* /*
The line protocol fish uses is always utf-8. The line protocol fish uses is always utf-8.
*/ */
char **from_name = iconv_utf8_names; const char **from_name = iconv_utf8_names;
size_t in_len = strlen( in ); size_t in_len = strlen( in );
size_t out_len = sizeof( wchar_t )*(in_len+2); size_t out_len = sizeof( wchar_t )*(in_len+2);
size_t nconv; size_t nconv;
char *nout; char *nout;
out = malloc( out_len ); out = (wchar_t *)malloc( out_len );
nout = (char *)out; nout = (char *)out;
if( !out ) if( !out )
@@ -306,7 +297,7 @@ static char *wcs2utf( const wchar_t *in )
really the character set used by wchar_t, but it is the best really the character set used by wchar_t, but it is the best
assumption we can make. assumption we can make.
*/ */
char **from_name=0; const char **from_name=0;
switch (sizeof (wchar_t)) switch (sizeof (wchar_t))
{ {
@@ -324,14 +315,14 @@ static char *wcs2utf( const wchar_t *in )
break; break;
} }
char **to_name = iconv_utf8_names; const char **to_name = iconv_utf8_names;
size_t in_len = wcslen( in ); size_t in_len = wcslen( in );
size_t out_len = sizeof( char )*( (MAX_UTF8_BYTES*in_len)+1); size_t out_len = sizeof( char )*( (MAX_UTF8_BYTES*in_len)+1);
size_t nconv; size_t nconv;
char *nout; char *nout;
out = malloc( out_len ); out = (char *)malloc( out_len );
nout = (char *)out; nout = (char *)out;
in_len *= sizeof( wchar_t ); in_len *= sizeof( wchar_t );
@@ -389,24 +380,18 @@ static char *wcs2utf( const wchar_t *in )
void env_universal_common_init( void (*cb)(int type, const wchar_t *key, const wchar_t *val ) ) void env_universal_common_init( void (*cb)(int type, const wchar_t *key, const wchar_t *val ) )
{ {
callback = cb; callback = cb;
hash_init( &env_universal_var, &hash_wcs_func, &hash_wcs_cmp );
}
/**
Free both key and data
*/
static void erase( void *key,
void *data )
{
free( (void *)key );
free( (void *)data );
} }
void env_universal_common_destroy() void env_universal_common_destroy()
{ {
hash_foreach( &env_universal_var, &erase ); std::map<wcstring, var_uni_entry_t*>::iterator iter;
hash_destroy( &env_universal_var );
for(iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
{
var_uni_entry_t* value = iter->second;
delete value;
}
} }
/** /**
@@ -479,13 +464,13 @@ void read_message( connection_t *src )
{ {
src->killme = 1; src->killme = 1;
debug( 3, L"Fd %d has reached eof, set killme flag", src->fd ); debug( 3, L"Fd %d has reached eof, set killme flag", src->fd );
if( src->input.used > 0 ) if( src->input.size() > 0 )
{ {
char c = 0; char c = 0;
b_append( &src->input, &c, 1 ); src->input.push_back(c);
debug( 1, debug( 1,
L"Universal variable connection closed while reading command. Partial command recieved: '%s'", L"Universal variable connection closed while reading command. Partial command recieved: '%s'",
(wchar_t *)src->input.buff ); &src->input.at(0));
} }
return; return;
} }
@@ -498,16 +483,16 @@ void read_message( connection_t *src )
wchar_t *msg; wchar_t *msg;
b = 0; b = 0;
b_append( &src->input, &b, 1 ); src->input.push_back(b);
msg = utf2wcs( src->input.buff ); msg = utf2wcs( &src->input.at(0) );
/* /*
Before calling parse_message, we must empty reset Before calling parse_message, we must empty reset
everything, since the callback function could everything, since the callback function could
potentially call read_message. potentially call read_message.
*/ */
src->input.used=0; src->input.clear();
if( msg ) if( msg )
{ {
@@ -515,7 +500,7 @@ void read_message( connection_t *src )
} }
else else
{ {
debug( 0, _(L"Could not convert message '%s' to wide character string"), src->input.buff ); debug( 0, _(L"Could not convert message '%s' to wide character string"), &src->input.at(0) );
} }
free( msg ); free( msg );
@@ -523,7 +508,7 @@ void read_message( connection_t *src )
} }
else else
{ {
b_append( &src->input, &b, 1 ); src->input.push_back(b);
} }
} }
} }
@@ -531,15 +516,15 @@ void read_message( connection_t *src )
/** /**
Remove variable with specified name Remove variable with specified name
*/ */
void env_universal_common_remove( const wchar_t *name ) void env_universal_common_remove( const wcstring &name )
{ {
void *k, *v; std::map<wcstring, var_uni_entry_t*>::iterator result = env_universal_var.find(name);
hash_remove( &env_universal_var, if (result != env_universal_var.end())
name, {
&k, var_uni_entry_t* v = result->second;
&v ); env_universal_var.erase(result);
free( k ); delete v;
free( v ); }
} }
/** /**
@@ -557,30 +542,23 @@ static int match( const wchar_t *msg, const wchar_t *cmd )
return 1; return 1;
} }
void env_universal_common_set( const wchar_t *key, const wchar_t *val, int export ) void env_universal_common_set( const wchar_t *key, const wchar_t *val, int exportv )
{ {
var_uni_entry_t *entry; var_uni_entry_t *entry;
wchar_t *name;
CHECK( key, ); CHECK( key, );
CHECK( val, ); CHECK( val, );
entry = malloc( sizeof(var_uni_entry_t) + sizeof(wchar_t)*(wcslen(val)+1) ); entry = new var_uni_entry_t;
name = wcsdup(key);
if( !entry || !name ) entry->exportv=exportv;
DIE_MEM(); entry->val = val;
env_universal_common_remove( key );
entry->export=export;
wcscpy( entry->val, val );
env_universal_common_remove( name );
hash_put( &env_universal_var, name, entry );
env_universal_var.insert( std::pair<wcstring, var_uni_entry_t*>(key, entry));
if( callback ) if( callback )
{ {
callback( export?SET_EXPORT:SET, name, val ); callback( exportv?SET_EXPORT:SET, key, val );
} }
} }
@@ -599,9 +577,9 @@ static void parse_message( wchar_t *msg,
if( match( msg, SET_STR ) || match( msg, SET_EXPORT_STR )) if( match( msg, SET_STR ) || match( msg, SET_EXPORT_STR ))
{ {
wchar_t *name, *tmp; wchar_t *name, *tmp;
int export = match( msg, SET_EXPORT_STR ); int exportv = match( msg, SET_EXPORT_STR );
name = msg+(export?wcslen(SET_EXPORT_STR):wcslen(SET_STR)); name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
while( wcschr( L"\t ", *name ) ) while( wcschr( L"\t ", *name ) )
name++; name++;
@@ -611,14 +589,14 @@ static void parse_message( wchar_t *msg,
wchar_t *key; wchar_t *key;
wchar_t *val; wchar_t *val;
key = malloc( sizeof( wchar_t)*(tmp-name+1)); key = (wchar_t *)malloc( sizeof( wchar_t)*(tmp-name+1));
memcpy( key, name, sizeof( wchar_t)*(tmp-name)); memcpy( key, name, sizeof( wchar_t)*(tmp-name));
key[tmp-name]=0; key[tmp-name]=0;
val = tmp+1; val = tmp+1;
val = unescape( val, 0 ); val = unescape( val, 0 );
env_universal_common_set( key, val, export ); env_universal_common_set( key, val, exportv );
free( val ); free( val );
free( key ); free( key );
@@ -658,7 +636,7 @@ static void parse_message( wchar_t *msg,
{ {
message_t *msg = create_message( BARRIER_REPLY, 0, 0 ); message_t *msg = create_message( BARRIER_REPLY, 0, 0 );
msg->count = 1; msg->count = 1;
q_put( &src->unsent, msg ); src->unsent->push(msg);
try_send_all( src ); try_send_all( src );
} }
else if( match( msg, BARRIER_REPLY_STR ) ) else if( match( msg, BARRIER_REPLY_STR ) )
@@ -728,12 +706,12 @@ void try_send_all( connection_t *c )
/* debug( 3, /* debug( 3,
L"Send all updates to connection on fd %d", L"Send all updates to connection on fd %d",
c->fd );*/ c->fd );*/
while( !q_empty( &c->unsent) ) while( !c->unsent->empty() )
{ {
switch( try_send( (message_t *)q_peek( &c->unsent), c->fd ) ) switch( try_send( c->unsent->front(), c->fd ) )
{ {
case 1: case 1:
q_get( &c->unsent); c->unsent->pop();
break; break;
case 0: case 0:
@@ -751,30 +729,29 @@ void try_send_all( connection_t *c )
/** /**
Escape specified string Escape specified string
*/ */
static wchar_t *full_escape( const wchar_t *in ) static wcstring full_escape( const wchar_t *in )
{ {
string_buffer_t out; wcstring out;
sb_init( &out );
for( ; *in; in++ ) for( ; *in; in++ )
{ {
if( *in < 32 ) if( *in < 32 )
{ {
sb_printf( &out, L"\\x%.2x", *in ); append_format( out, L"\\x%.2x", *in );
} }
else if( *in < 128 ) else if( *in < 128 )
{ {
sb_append_char( &out, *in ); out.push_back(*in);
} }
else if( *in < 65536 ) else if( *in < 65536 )
{ {
sb_printf( &out, L"\\u%.4x", *in ); append_format( out, L"\\u%.4x", *in );
} }
else else
{ {
sb_printf( &out, L"\\U%.8x", *in ); append_format( out, L"\\U%.8x", *in );
} }
} }
return (wchar_t *)out.buff; return out;
} }
@@ -818,15 +795,11 @@ message_t *create_message( int type,
val_in=L""; val_in=L"";
} }
wchar_t *esc = full_escape( val_in ); wcstring esc = full_escape( val_in );
if( !esc ) char *val = wcs2utf(esc.c_str());
break;
char *val = wcs2utf(esc );
free(esc);
sz = strlen(type==SET?SET_MBS:SET_EXPORT_MBS) + strlen(key) + strlen(val) + 4; sz = strlen(type==SET?SET_MBS:SET_EXPORT_MBS) + strlen(key) + strlen(val) + 4;
msg = malloc( sizeof( message_t ) + sz ); msg = (message_t *)malloc( sizeof( message_t ) + sz );
if( !msg ) if( !msg )
DIE_MEM(); DIE_MEM();
@@ -846,7 +819,7 @@ message_t *create_message( int type,
case ERASE: case ERASE:
{ {
sz = strlen(ERASE_MBS) + strlen(key) + 3; sz = strlen(ERASE_MBS) + strlen(key) + 3;
msg = malloc( sizeof( message_t ) + sz ); msg = (message_t *)malloc( sizeof( message_t ) + sz );
if( !msg ) if( !msg )
DIE_MEM(); DIE_MEM();
@@ -859,7 +832,7 @@ message_t *create_message( int type,
case BARRIER: case BARRIER:
{ {
msg = malloc( sizeof( message_t ) + msg = (message_t *)malloc( sizeof( message_t ) +
strlen( BARRIER_MBS ) +2); strlen( BARRIER_MBS ) +2);
if( !msg ) if( !msg )
DIE_MEM(); DIE_MEM();
@@ -869,7 +842,7 @@ message_t *create_message( int type,
case BARRIER_REPLY: case BARRIER_REPLY:
{ {
msg = malloc( sizeof( message_t ) + msg = (message_t *)malloc( sizeof( message_t ) +
strlen( BARRIER_REPLY_MBS ) +2); strlen( BARRIER_REPLY_MBS ) +2);
if( !msg ) if( !msg )
DIE_MEM(); DIE_MEM();
@@ -894,76 +867,68 @@ message_t *create_message( int type,
} }
/** /**
Function used with hash_foreach to insert keys of one table into Put exported or unexported variables in a string list
another
*/ */
static void add_key_to_hash( void *key, void env_universal_common_get_names( wcstring_list_t &lst,
void *data,
void *aux )
{
var_uni_entry_t *e = (var_uni_entry_t *)data;
if( ( e->export && get_names_show_exported) ||
( !e->export && get_names_show_unexported) )
al_push( (array_list_t *)aux, key );
}
void env_universal_common_get_names( array_list_t *l,
int show_exported, int show_exported,
int show_unexported ) int show_unexported )
{ {
get_names_show_exported = show_exported; std::map<wcstring, var_uni_entry_t*>::const_iterator iter;
get_names_show_unexported = show_unexported; for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
{
const wcstring& key = iter->first;
const var_uni_entry_t *e = iter->second;
if( ( e->exportv && show_exported) ||
( !e->exportv && show_unexported) )
{
lst.push_back(key);
}
}
hash_foreach2( &env_universal_var,
add_key_to_hash,
l );
} }
wchar_t *env_universal_common_get( const wchar_t *name )
wchar_t *env_universal_common_get( const wcstring &name )
{ {
var_uni_entry_t *e = (var_uni_entry_t *)hash_get( &env_universal_var, name ); std::map<wcstring, var_uni_entry_t*>::const_iterator result = env_universal_var.find(name);
if( e )
return e->val; if (result != env_universal_var.end() )
{
const var_uni_entry_t *e = result->second;
if( e )
return const_cast<wchar_t*>(e->val.c_str());
}
return 0; return 0;
} }
int env_universal_common_get_export( const wchar_t *name ) int env_universal_common_get_export( const wcstring &name )
{ {
var_uni_entry_t *e = (var_uni_entry_t *)hash_get( &env_universal_var, name ); std::map<wcstring, var_uni_entry_t*>::const_iterator result = env_universal_var.find(name);
if( e ) if (result != env_universal_var.end() )
return e->export; {
const var_uni_entry_t *e = result->second;
if (e != NULL)
return e->exportv;
}
return 0; return 0;
} }
/**
Adds a variable creation message about the specified variable to
the specified queue. The function signature is non-obvious since
this function is used together with hash_foreach2, which requires
the specified function signature.
\param k the variable name
\param v the variable value
\param q the queue to add the message to
*/
static void enqueue( void *k,
void *v,
void *q)
{
const wchar_t *key = (const wchar_t *)k;
const var_uni_entry_t *val = (const var_uni_entry_t *)v;
dyn_queue_t *queue = (dyn_queue_t *)q;
message_t *msg = create_message( val->export?SET_EXPORT:SET, key, val->val );
msg->count=1;
q_put( queue, msg );
}
void enqueue_all( connection_t *c ) void enqueue_all( connection_t *c )
{ {
hash_foreach2( &env_universal_var, std::map<wcstring, var_uni_entry_t*>::const_iterator iter;
&enqueue,
(void *)&c->unsent ); for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
{
const wcstring &key = iter->first;
const var_uni_entry_t *val = iter->second;
message_t *msg = create_message( val->exportv?SET_EXPORT:SET, key.c_str(), val->val.c_str() );
msg->count=1;
c->unsent->push(msg);
}
try_send_all( c ); try_send_all( c );
} }
@@ -972,15 +937,13 @@ void connection_init( connection_t *c, int fd )
{ {
memset (c, 0, sizeof (connection_t)); memset (c, 0, sizeof (connection_t));
c->fd = fd; c->fd = fd;
b_init( &c->input ); c->unsent = new std::queue<message_t *>;
q_init( &c->unsent );
c->buffer_consumed = c->buffer_used = 0; c->buffer_consumed = c->buffer_used = 0;
} }
void connection_destroy( connection_t *c) void connection_destroy( connection_t *c)
{ {
q_destroy( &c->unsent ); if (c->unsent) delete c->unsent;
b_destroy( &c->input );
/* /*
A connection need not always be open - we only try to close it A connection need not always be open - we only try to close it

View File

@@ -2,7 +2,7 @@
#define FISH_ENV_UNIVERSAL_COMMON_H #define FISH_ENV_UNIVERSAL_COMMON_H
#include <wchar.h> #include <wchar.h>
#include <queue>
#include "util.h" #include "util.h"
/** /**
@@ -54,6 +54,24 @@ enum
*/ */
#define ENV_UNIVERSAL_BUFFER_SIZE 1024 #define ENV_UNIVERSAL_BUFFER_SIZE 1024
/**
A struct representing a message to be sent between client and server
*/
typedef struct
{
/**
Number of queues that contain this message. Once this reaches zero, the message should be deleted
*/
int count;
/**
Message body. The message must be allocated using enough memory to actually contain the message.
*/
char body[1];
}
message_t;
typedef std::queue<message_t *> message_queue_t;
/** /**
This struct represents a connection between a universal variable server/client This struct represents a connection between a universal variable server/client
*/ */
@@ -66,7 +84,7 @@ typedef struct connection
/** /**
Queue of onsent messages Queue of onsent messages
*/ */
dyn_queue_t unsent; message_queue_t *unsent;
/** /**
Set to one when this connection should be killed Set to one when this connection should be killed
*/ */
@@ -75,7 +93,7 @@ typedef struct connection
The input string. Input from the socket goes here. When a The input string. Input from the socket goes here. When a
newline is encountered, the buffer is parsed and cleared. newline is encountered, the buffer is parsed and cleared.
*/ */
buffer_t input; std::vector<char> input;
/** /**
The read buffer. The read buffer.
@@ -100,22 +118,6 @@ typedef struct connection
} }
connection_t; connection_t;
/**
A struct representing a message to be sent between client and server
*/
typedef struct
{
/**
Number of queues that contain this message. Once this reaches zero, the message should be deleted
*/
int count;
/**
Message body. The message must be allocated using enough memory to actually contain the message.
*/
char body[1];
}
message_t;
/** /**
Read all available messages on this connection Read all available messages on this connection
*/ */
@@ -147,7 +149,7 @@ void env_universal_common_destroy();
This function operate agains the local copy of all universal This function operate agains the local copy of all universal
variables, it does not communicate with any other process. variables, it does not communicate with any other process.
*/ */
void env_universal_common_get_names( array_list_t *l, void env_universal_common_get_names( wcstring_list_t &lst,
int show_exported, int show_exported,
int show_unexported ); int show_unexported );
@@ -160,7 +162,7 @@ void env_universal_common_get_names( array_list_t *l,
Do not call this function. Create a message to do it. This function Do not call this function. Create a message to do it. This function
is only to be used when fishd is dead. is only to be used when fishd is dead.
*/ */
void env_universal_common_set( const wchar_t *key, const wchar_t *val, int export ); void env_universal_common_set( const wchar_t *key, const wchar_t *val, int exportv );
/** /**
Remove the specified variable. Remove the specified variable.
@@ -171,7 +173,7 @@ void env_universal_common_set( const wchar_t *key, const wchar_t *val, int expor
Do not call this function. Create a message to do it. This function Do not call this function. Create a message to do it. This function
is only to be used when fishd is dead. is only to be used when fishd is dead.
*/ */
void env_universal_common_remove( const wchar_t *key ); void env_universal_common_remove( const wcstring &key );
/** /**
Get the value of the variable with the specified name Get the value of the variable with the specified name
@@ -179,7 +181,7 @@ void env_universal_common_remove( const wchar_t *key );
This function operate agains the local copy of all universal This function operate agains the local copy of all universal
variables, it does not communicate with any other process. variables, it does not communicate with any other process.
*/ */
wchar_t *env_universal_common_get( const wchar_t *name ); wchar_t *env_universal_common_get( const wcstring &name );
/** /**
Get the export flag of the variable with the specified Get the export flag of the variable with the specified
@@ -188,7 +190,7 @@ wchar_t *env_universal_common_get( const wchar_t *name );
This function operate agains the local copy of all universal This function operate agains the local copy of all universal
variables, it does not communicate with any other process. variables, it does not communicate with any other process.
*/ */
int env_universal_common_get_export( const wchar_t *name ); int env_universal_common_get_export( const wcstring &name );
/** /**
Add messages about all existing variables to the specified connection Add messages about all existing variables to the specified connection

View File

@@ -38,17 +38,6 @@ if status --is-login
end end
end end
#
# There are variables that contain colons that are not arrays. This
# reverts them back to regular strings.
#
for i in DISPLAY
if set -q $i
set -- $i (printf ":%s" $$i|cut -c 2-)
end
end
# #
# Load additional initialization files # Load additional initialization files
# #

731
event.c
View File

@@ -1,731 +0,0 @@
/** \file event.c
Functions for handling event triggers
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <string.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "event.h"
#include "signal.h"
#include "halloc_util.h"
/**
Number of signals that can be queued before an overflow occurs
*/
#define SIG_UNHANDLED_MAX 64
/**
This struct contains a list of generated signals waiting to be
dispatched
*/
typedef struct
{
/**
Number of delivered signals
*/
int count;
/**
Whether signals have been skipped
*/
int overflow;
/**
Array of signal events
*/
int signal[SIG_UNHANDLED_MAX];
}
signal_list_t;
/**
The signal event list. Actually two separate lists. One which is
active, which is the one that new events is written to. The inactive
one contains the events that are currently beeing performed.
*/
static signal_list_t sig_list[]={{0,0},{0,0}};
/**
The index of sig_list that is the list of signals currently written to
*/
static int active_list=0;
/**
List of event handlers
*/
static array_list_t *events;
/**
List of event handlers that should be removed
*/
static array_list_t *killme;
/**
List of events that have been sent but have not yet been delivered because they are blocked.
*/
static array_list_t *blocked;
/**
Tests if one event instance matches the definition of a event
class. If both the class and the instance name a function,
they must name the same function.
*/
static int event_match( event_t *class, event_t *instance )
{
if( class->function_name && instance->function_name )
{
if( wcscmp( class->function_name, instance->function_name ) != 0 )
return 0;
}
if( class->type == EVENT_ANY )
return 1;
if( class->type != instance->type )
return 0;
switch( class->type )
{
case EVENT_SIGNAL:
if( class->param1.signal == EVENT_ANY_SIGNAL )
return 1;
return class->param1.signal == instance->param1.signal;
case EVENT_VARIABLE:
return wcscmp( instance->param1.variable,
class->param1.variable )==0;
case EVENT_EXIT:
if( class->param1.pid == EVENT_ANY_PID )
return 1;
return class->param1.pid == instance->param1.pid;
case EVENT_JOB_ID:
return class->param1.job_id == instance->param1.job_id;
case EVENT_GENERIC:
return wcscmp( instance->param1.param,
class->param1.param )==0;
}
/**
This should never be reached
*/
return 0;
}
/**
Create an identical copy of an event. Use deep copying, i.e. make
duplicates of any strings used as well.
*/
static event_t *event_copy( event_t *event, int copy_arguments )
{
event_t *e = malloc( sizeof( event_t ) );
if( !e )
DIE_MEM();
memcpy( e, event, sizeof(event_t));
if( e->function_name )
e->function_name = wcsdup( e->function_name );
if( e->type == EVENT_VARIABLE )
e->param1.variable = wcsdup( e->param1.variable );
else if( e->type == EVENT_GENERIC )
e->param1.param = wcsdup( e->param1.param );
al_init( &e->arguments );
if( copy_arguments )
{
int i;
for( i=0; i<al_get_count( &event->arguments ); i++ )
{
al_push( &e->arguments, wcsdup( (wchar_t *)al_get( &event->arguments, i ) ) );
}
}
return e;
}
/**
Test if specified event is blocked
*/
static int event_is_blocked( event_t *e )
{
block_t *block;
event_block_t *eb;
for( block = current_block; block; block = block->outer )
{
for( eb = block->first_event_block; eb; eb=eb->next )
{
if( eb->type & (1<<EVENT_ANY ) )
return 1;
if( eb->type & (1<<e->type) )
return 1;
}
}
for( eb = global_event_block; eb; eb=eb->next )
{
if( eb->type & (1<<EVENT_ANY ) )
return 1;
if( eb->type & (1<<e->type) )
return 1;
return 1;
}
return 0;
}
const wchar_t *event_get_desc( event_t *e )
{
/*
String buffer used for formating event descriptions in event_get_desc()
*/
static string_buffer_t *get_desc_buff=0;
CHECK( e, 0 );
if( !get_desc_buff )
{
get_desc_buff=sb_halloc( global_context );
}
else
{
sb_clear( get_desc_buff );
}
switch( e->type )
{
case EVENT_SIGNAL:
sb_printf( get_desc_buff, _(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal ), signal_get_desc( e->param1.signal ) );
break;
case EVENT_VARIABLE:
sb_printf( get_desc_buff, _(L"handler for variable '%ls'"), e->param1.variable );
break;
case EVENT_EXIT:
if( e->param1.pid > 0 )
{
sb_printf( get_desc_buff, _(L"exit handler for process %d"), e->param1.pid );
}
else
{
job_t *j = job_get_from_pid( -e->param1.pid );
if( j )
sb_printf( get_desc_buff, _(L"exit handler for job %d, '%ls'"), j->job_id, j->command );
else
sb_printf( get_desc_buff, _(L"exit handler for job with process group %d"), -e->param1.pid );
}
break;
case EVENT_JOB_ID:
{
job_t *j = job_get( e->param1.job_id );
if( j )
sb_printf( get_desc_buff, _(L"exit handler for job %d, '%ls'"), j->job_id, j->command );
else
sb_printf( get_desc_buff, _(L"exit handler for job with job id %d"), j->job_id );
break;
}
case EVENT_GENERIC:
sb_printf( get_desc_buff, _(L"handler for generic event '%ls'"), e->param1.param );
break;
default:
sb_printf( get_desc_buff, _(L"Unknown event type") );
break;
}
return (const wchar_t *)get_desc_buff->buff;
}
void event_add_handler( event_t *event )
{
event_t *e;
CHECK( event, );
e = event_copy( event, 0 );
if( !events )
events = al_new();
if( e->type == EVENT_SIGNAL )
{
signal_handle( e->param1.signal, 1 );
}
al_push( events, e );
}
void event_remove( event_t *criterion )
{
int i;
array_list_t *new_list=0;
event_t e;
CHECK( criterion, );
/*
Because of concurrency issues (env_remove could remove an event
that is currently being executed), env_remove does not actually
free any events - instead it simply moves all events that should
be removed from the event list to the killme list, and the ones
that shouldn't be killed to new_list, and then drops the empty
events-list.
*/
if( !events )
return;
for( i=0; i<al_get_count( events); i++ )
{
event_t *n = (event_t *)al_get( events, i );
if( event_match( criterion, n ) )
{
if( !killme )
killme = al_new();
al_push( killme, n );
/*
If this event was a signal handler and no other handler handles
the specified signal type, do not handle that type of signal any
more.
*/
if( n->type == EVENT_SIGNAL )
{
e.type = EVENT_SIGNAL;
e.param1.signal = n->param1.signal;
e.function_name = 0;
if( event_get( &e, 0 ) == 1 )
{
signal_handle( e.param1.signal, 0 );
}
}
}
else
{
if( !new_list )
new_list = al_new();
al_push( new_list, n );
}
}
al_destroy( events );
free( events );
events = new_list;
}
int event_get( event_t *criterion, array_list_t *out )
{
int i;
int found = 0;
if( !events )
return 0;
CHECK( criterion, 0 );
for( i=0; i<al_get_count( events); i++ )
{
event_t *n = (event_t *)al_get( events, i );
if( event_match(criterion, n ) )
{
found++;
if( out )
al_push( out, n );
}
}
return found;
}
/**
Free all events in the kill list
*/
static void event_free_kills()
{
int i;
if( !killme )
return;
for( i=0; i<al_get_count( killme ); i++ )
{
event_t *roadkill = (event_t *)al_get( killme, i );
event_free( roadkill );
}
al_truncate( killme, 0 );
}
/**
Test if the specified event is waiting to be killed
*/
static int event_is_killed( event_t *e )
{
int i;
if( !killme )
return 0;
for( i=0; i<al_get_count( killme ); i++ )
{
event_t *roadkill = (event_t *)al_get( events, i );
if( roadkill ==e )
return 1;
}
return 0;
}
/**
Perform the specified event. Since almost all event firings will
not be matched by even a single event handler, we make sure to
optimize the 'no matches' path. This means that nothing is
allocated/initialized unless needed.
*/
static void event_fire_internal( event_t *event )
{
int i, j;
string_buffer_t *b=0;
array_list_t *fire=0;
/*
First we free all events that have been removed
*/
event_free_kills();
if( !events )
return;
/*
Then we iterate over all events, adding events that should be
fired to a second list. We need to do this in a separate step
since an event handler might call event_remove or
event_add_handler, which will change the contents of the \c
events list.
*/
for( i=0; i<al_get_count( events ); i++ )
{
event_t *criterion = (event_t *)al_get( events, i );
/*
Check if this event is a match
*/
if(event_match( criterion, event ) )
{
if( !fire )
fire = al_new();
al_push( fire, criterion );
}
}
/*
No matches. Time to return.
*/
if( !fire )
return;
/*
Iterate over our list of matching events
*/
for( i=0; i<al_get_count( fire ); i++ )
{
event_t *criterion = (event_t *)al_get( fire, i );
int prev_status;
/*
Check if this event has been removed, if so, dont fire it
*/
if( event_is_killed( criterion ) )
continue;
/*
Fire event
*/
if( !b )
b = sb_new();
else
sb_clear( b );
sb_append( b, criterion->function_name );
for( j=0; j<al_get_count(&event->arguments); j++ )
{
wchar_t *arg_esc = escape( (wchar_t *)al_get( &event->arguments, j), 1 );
sb_append( b, L" " );
sb_append( b, arg_esc );
free( arg_esc );
}
// debug( 1, L"Event handler fires command '%ls'", (wchar_t *)b->buff );
/*
Event handlers are not part of the main flow of code, so
they are marked as non-interactive
*/
proc_push_interactive(0);
prev_status = proc_get_last_status();
parser_push_block( EVENT );
current_block->param1.event = event;
eval( (wchar_t *)b->buff, 0, TOP );
parser_pop_block();
proc_pop_interactive();
proc_set_last_status( prev_status );
}
if( b )
{
sb_destroy( b );
free( b );
}
if( fire )
{
al_destroy( fire );
free( fire );
}
/*
Free killed events
*/
event_free_kills();
}
/**
Handle all pending signal events
*/
static void event_fire_delayed()
{
int i;
/*
If is_event is one, we are running the event-handler non-recursively.
When the event handler has called a piece of code that triggers
another event, we do not want to fire delayed events because of
concurrency problems.
*/
if( blocked && is_event==1)
{
array_list_t *new_blocked = 0;
for( i=0; i<al_get_count( blocked ); i++ )
{
event_t *e = (event_t *)al_get( blocked, i );
if( event_is_blocked( e ) )
{
if( !new_blocked )
new_blocked = al_new();
al_push( new_blocked, e );
}
else
{
event_fire_internal( e );
event_free( e );
}
}
al_destroy( blocked );
free( blocked );
blocked = new_blocked;
}
while( sig_list[active_list].count > 0 )
{
signal_list_t *lst;
event_t e;
al_init( &e.arguments );
/*
Switch signal lists
*/
sig_list[1-active_list].count=0;
sig_list[1-active_list].overflow=0;
active_list=1-active_list;
/*
Set up
*/
e.type=EVENT_SIGNAL;
e.function_name=0;
lst = &sig_list[1-active_list];
if( lst->overflow )
{
debug( 0, _( L"Signal list overflow. Signals have been ignored." ) );
}
/*
Send all signals in our private list
*/
for( i=0; i<lst->count; i++ )
{
e.param1.signal = lst->signal[i];
al_set( &e.arguments, 0, sig2wcs( e.param1.signal ) );
if( event_is_blocked( &e ) )
{
if( !blocked )
blocked = al_new();
al_push( blocked, event_copy(&e, 1) );
}
else
{
event_fire_internal( &e );
}
}
al_destroy( &e.arguments );
}
}
void event_fire( event_t *event )
{
is_event++;
if( event && (event->type == EVENT_SIGNAL) )
{
/*
This means we are in a signal handler. We must be very
careful not do do anything that could cause a memory
allocation or something else that might be bad when in a
signal handler.
*/
if( sig_list[active_list].count < SIG_UNHANDLED_MAX )
sig_list[active_list].signal[sig_list[active_list].count++]=event->param1.signal;
else
sig_list[active_list].overflow=1;
}
else
{
/*
Fire events triggered by signals
*/
event_fire_delayed();
if( event )
{
if( event_is_blocked( event ) )
{
if( !blocked )
blocked = al_new();
al_push( blocked, event_copy(event, 1) );
}
else
{
event_fire_internal( event );
}
}
}
is_event--;
}
void event_init()
{
}
void event_destroy()
{
if( events )
{
al_foreach( events, (void (*)(void *))&event_free );
al_destroy( events );
free( events );
events=0;
}
if( killme )
{
al_foreach( killme, (void (*)(void *))&event_free );
al_destroy( killme );
free( killme );
killme=0;
}
}
void event_free( event_t *e )
{
CHECK( e, );
/*
When apropriate, we clear the argument vector
*/
al_foreach( &e->arguments, &free );
al_destroy( &e->arguments );
free( (void *)e->function_name );
if( e->type == EVENT_VARIABLE )
{
free( (void *)e->param1.variable );
}
else if( e->type == EVENT_GENERIC )
{
free( (void *)e->param1.param );
}
free( e );
}
void event_fire_generic_internal(const wchar_t *name, ...)
{
event_t ev;
va_list va;
wchar_t *arg;
CHECK( name, );
ev.type = EVENT_GENERIC;
ev.param1.param = name;
ev.function_name=0;
al_init( &ev.arguments );
va_start( va, name );
while( (arg=va_arg(va, wchar_t *) )!= 0 )
{
al_push( &ev.arguments, arg );
}
va_end( va );
event_fire( &ev );
}

666
event.cpp Normal file
View File

@@ -0,0 +1,666 @@
/** \file event.c
Functions for handling event triggers
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <string.h>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "event.h"
#include "signal.h"
/**
Number of signals that can be queued before an overflow occurs
*/
#define SIG_UNHANDLED_MAX 64
/**
This struct contains a list of generated signals waiting to be
dispatched
*/
typedef struct
{
/**
Number of delivered signals
*/
int count;
/**
Whether signals have been skipped
*/
int overflow;
/**
Array of signal events
*/
int signal[SIG_UNHANDLED_MAX];
}
signal_list_t;
/**
The signal event list. Actually two separate lists. One which is
active, which is the one that new events is written to. The inactive
one contains the events that are currently beeing performed.
*/
static signal_list_t sig_list[]={{0,0},{0,0}};
/**
The index of sig_list that is the list of signals currently written to
*/
static int active_list=0;
typedef std::vector<event_t *> event_list_t;
/**
List of event handlers.
Note this is inspected by our signal handler, so we must block signals around manipulating it.
*/
static event_list_t events;
/**
List of event handlers that should be removed
*/
static event_list_t killme;
/**
List of events that have been sent but have not yet been delivered because they are blocked.
*/
static event_list_t blocked;
/**
Tests if one event instance matches the definition of a event
class. If both the class and the instance name a function,
they must name the same function.
*/
static int event_match( const event_t *classv, const event_t *instance )
{
/* If the function names are both non-empty and different, then it's not a match */
if( ! classv->function_name.empty() &&
! instance->function_name.empty() &&
classv->function_name != instance->function_name)
{
return 0;
}
if( classv->type == EVENT_ANY )
return 1;
if( classv->type != instance->type )
return 0;
switch( classv->type )
{
case EVENT_SIGNAL:
if( classv->param1.signal == EVENT_ANY_SIGNAL )
return 1;
return classv->param1.signal == instance->param1.signal;
case EVENT_VARIABLE:
return instance->str_param1 == classv->str_param1;
case EVENT_EXIT:
if( classv->param1.pid == EVENT_ANY_PID )
return 1;
return classv->param1.pid == instance->param1.pid;
case EVENT_JOB_ID:
return classv->param1.job_id == instance->param1.job_id;
case EVENT_GENERIC:
return instance->str_param1 == classv->str_param1;
}
/**
This should never be reached
*/
return 0;
}
/**
Create an identical copy of an event. Use deep copying, i.e. make
duplicates of any strings used as well.
*/
static event_t *event_copy( const event_t *event, int copy_arguments )
{
event_t *e = new event_t(*event);
e->arguments.reset(new wcstring_list_t);
if( copy_arguments && event->arguments.get() != NULL )
{
*(e->arguments) = *(event->arguments);
}
return e;
}
/**
Test if specified event is blocked
*/
static int event_is_blocked( event_t *e )
{
block_t *block;
parser_t &parser = parser_t::principal_parser();
for( block = parser.current_block; block; block = block->outer )
{
if (event_block_list_blocks_type(block->event_blocks, e->type))
return true;
}
return event_block_list_blocks_type(parser.global_event_blocks, e->type);
}
wcstring event_get_desc( const event_t *e )
{
CHECK( e, 0 );
wcstring result;
switch( e->type )
{
case EVENT_SIGNAL:
result = format_string(_(L"signal handler for %ls (%ls)"), sig2wcs(e->param1.signal ), signal_get_desc( e->param1.signal ));
break;
case EVENT_VARIABLE:
result = format_string(_(L"handler for variable '%ls'"), e->str_param1.c_str() );
break;
case EVENT_EXIT:
if( e->param1.pid > 0 )
{
result = format_string(_(L"exit handler for process %d"), e->param1.pid );
}
else
{
job_t *j = job_get_from_pid( -e->param1.pid );
if( j )
result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr() );
else
result = format_string(_(L"exit handler for job with process group %d"), -e->param1.pid );
}
break;
case EVENT_JOB_ID:
{
job_t *j = job_get( e->param1.job_id );
if( j )
result = format_string(_(L"exit handler for job %d, '%ls'"), j->job_id, j->command_wcstr() );
else
result = format_string(_(L"exit handler for job with job id %d"), e->param1.job_id );
break;
}
case EVENT_GENERIC:
result = format_string(_(L"handler for generic event '%ls'"), e->str_param1.c_str() );
break;
default:
result = format_string(_(L"Unknown event type") );
break;
}
return result;
}
#if 0
static void show_all_handlers(void) {
puts("event handlers:");
for (event_list_t::const_iterator iter = events.begin(); iter != events.end(); ++iter) {
const event_t *foo = *iter;
wcstring tmp = event_get_desc(foo);
printf(" handler now %ls\n", tmp.c_str());
}
}
#endif
void event_add_handler( const event_t *event )
{
event_t *e;
CHECK( event, );
e = event_copy( event, 0 );
if( e->type == EVENT_SIGNAL )
{
signal_handle( e->param1.signal, 1 );
}
// Block around updating the events vector
signal_block();
events.push_back(e);
signal_unblock();
}
void event_remove( event_t *criterion )
{
size_t i;
event_list_t new_list;
CHECK( criterion, );
/*
Because of concurrency issues (env_remove could remove an event
that is currently being executed), env_remove does not actually
free any events - instead it simply moves all events that should
be removed from the event list to the killme list, and the ones
that shouldn't be killed to new_list, and then drops the empty
events-list.
*/
if( events.empty() )
return;
for( i=0; i<events.size(); i++ )
{
event_t *n = events.at(i);
if( event_match( criterion, n ) )
{
killme.push_back(n);
/*
If this event was a signal handler and no other handler handles
the specified signal type, do not handle that type of signal any
more.
*/
if( n->type == EVENT_SIGNAL )
{
event_t e = event_t::signal_event(n->param1.signal);
if( event_get( &e, 0 ) == 1 )
{
signal_handle( e.param1.signal, 0 );
}
}
}
else
{
new_list.push_back(n);
}
}
signal_block();
events.swap(new_list);
signal_unblock();
}
int event_get( event_t *criterion, std::vector<event_t *> *out )
{
size_t i;
int found = 0;
if( events.empty() )
return 0;
CHECK( criterion, 0 );
for( i=0; i<events.size(); i++ )
{
event_t *n = events.at(i);
if( event_match(criterion, n ) )
{
found++;
if( out )
out->push_back(n);
}
}
return found;
}
bool event_is_signal_observed(int sig)
{
/* We are in a signal handler! Don't allocate memory, etc.
This does what event_match does, except it doesn't require passing in an event_t.
*/
size_t i, max = events.size();
for (i=0; i < max; i++)
{
const event_t *event = events[i];
if (event->type == EVENT_ANY)
{
return true;
}
else if (event->type == EVENT_SIGNAL)
{
if( event->param1.signal == EVENT_ANY_SIGNAL || event->param1.signal == sig)
return true;
}
}
return false;
}
/**
Free all events in the kill list
*/
static void event_free_kills()
{
for_each(killme.begin(), killme.end(), event_free);
killme.resize(0);
}
/**
Test if the specified event is waiting to be killed
*/
static int event_is_killed( event_t *e )
{
return std::find(killme.begin(), killme.end(), e) != killme.end();
}
/**
Perform the specified event. Since almost all event firings will
not be matched by even a single event handler, we make sure to
optimize the 'no matches' path. This means that nothing is
allocated/initialized unless needed.
*/
static void event_fire_internal( const event_t *event )
{
size_t i, j;
event_list_t fire;
/*
First we free all events that have been removed
*/
event_free_kills();
if( events.empty() )
return;
/*
Then we iterate over all events, adding events that should be
fired to a second list. We need to do this in a separate step
since an event handler might call event_remove or
event_add_handler, which will change the contents of the \c
events list.
*/
for( i=0; i<events.size(); i++ )
{
event_t *criterion = events.at(i);
/*
Check if this event is a match
*/
if(event_match( criterion, event ) )
{
fire.push_back(criterion);
}
}
/*
No matches. Time to return.
*/
if( fire.empty() )
return;
/*
Iterate over our list of matching events
*/
for( i=0; i<fire.size(); i++ )
{
event_t *criterion = fire.at(i);
int prev_status;
/*
Check if this event has been removed, if so, dont fire it
*/
if( event_is_killed( criterion ) )
continue;
/*
Fire event
*/
wcstring buffer = criterion->function_name;
if (event->arguments.get())
{
for( j=0; j< event->arguments->size(); j++ )
{
wcstring arg_esc = escape_string( event->arguments->at(j), 1 );
buffer += L" ";
buffer += arg_esc;
}
}
// debug( 1, L"Event handler fires command '%ls'", (wchar_t *)b->buff );
/*
Event handlers are not part of the main flow of code, so
they are marked as non-interactive
*/
proc_push_interactive(0);
prev_status = proc_get_last_status();
parser_t &parser = parser_t::principal_parser();
parser.push_block( EVENT );
parser.current_block->state1<const event_t *>() = event;
parser.eval( buffer.c_str(), 0, TOP );
parser.pop_block();
proc_pop_interactive();
proc_set_last_status( prev_status );
}
/*
Free killed events
*/
event_free_kills();
}
/**
Handle all pending signal events
*/
static void event_fire_delayed()
{
size_t i;
/*
If is_event is one, we are running the event-handler non-recursively.
When the event handler has called a piece of code that triggers
another event, we do not want to fire delayed events because of
concurrency problems.
*/
if( ! blocked.empty() && is_event==1)
{
event_list_t new_blocked;
for( i=0; i<blocked.size(); i++ )
{
event_t *e = blocked.at(i);
if( event_is_blocked( e ) )
{
new_blocked.push_back(e);
}
else
{
event_fire_internal( e );
event_free( e );
}
}
blocked.swap(new_blocked);
}
while( sig_list[active_list].count > 0 )
{
signal_list_t *lst;
/*
Switch signal lists
*/
sig_list[1-active_list].count=0;
sig_list[1-active_list].overflow=0;
active_list=1-active_list;
/*
Set up
*/
event_t e = event_t::signal_event(0);
e.arguments.reset(new wcstring_list_t(1)); //one element
lst = &sig_list[1-active_list];
if( lst->overflow )
{
debug( 0, _( L"Signal list overflow. Signals have been ignored." ) );
}
/*
Send all signals in our private list
*/
for( i=0; i<(size_t)lst->count; i++ )
{
e.param1.signal = lst->signal[i];
e.arguments->at(0) = sig2wcs( e.param1.signal );
if( event_is_blocked( &e ) )
{
blocked.push_back(event_copy(&e, 1));
}
else
{
event_fire_internal( &e );
}
}
e.arguments.reset(NULL);
}
}
void event_fire_signal(int signal)
{
/*
This means we are in a signal handler. We must be very
careful not do do anything that could cause a memory
allocation or something else that might be bad when in a
signal handler.
*/
if( sig_list[active_list].count < SIG_UNHANDLED_MAX )
sig_list[active_list].signal[sig_list[active_list].count++]=signal;
else
sig_list[active_list].overflow=1;
}
void event_fire( event_t *event )
{
if( event && (event->type == EVENT_SIGNAL) )
{
event_fire_signal(event->param1.signal);
}
else
{
is_event++;
/*
Fire events triggered by signals
*/
event_fire_delayed();
if( event )
{
if( event_is_blocked( event ) )
{
blocked.push_back(event_copy(event, 1));
}
else
{
event_fire_internal( event );
}
}
is_event--;
}
}
void event_init()
{
}
void event_destroy()
{
for_each(events.begin(), events.end(), event_free);
events.clear();
for_each(killme.begin(), killme.end(), event_free);
killme.clear();
}
void event_free( event_t *e )
{
CHECK( e, );
delete e;
}
void event_fire_generic_internal(const wchar_t *name, ...)
{
va_list va;
wchar_t *arg;
CHECK( name, );
event_t ev(EVENT_GENERIC);
ev.str_param1 = name;
ev.arguments.reset(new wcstring_list_t);
va_start( va, name );
while( (arg=va_arg(va, wchar_t *) )!= 0 )
{
ev.arguments->push_back(arg);
}
va_end( va );
event_fire( &ev );
ev.arguments.reset(NULL);
}
event_t event_t::signal_event(int sig) {
event_t event(EVENT_SIGNAL);
event.param1.signal = sig;
return event;
}
event_t event_t::variable_event(const wcstring &str) {
event_t event(EVENT_VARIABLE);
event.str_param1 = str;
return event;
}
event_t event_t::generic_event(const wcstring &str) {
event_t event(EVENT_GENERIC);
event.str_param1 = str;
return event;
}
event_t::event_t(const event_t &x) :
type(x.type),
param1(x.param1),
str_param1(x.str_param1),
function_name(x.function_name)
{
const wcstring_list_t *ptr = x.arguments.get();
if (ptr)
arguments.reset(new wcstring_list_t(*ptr));
}

86
event.h
View File

@@ -12,6 +12,9 @@
#ifndef FISH_EVENT_H #ifndef FISH_EVENT_H
#define FISH_EVENT_H #define FISH_EVENT_H
#include <memory>
#include "common.h"
/** /**
The signal number that is used to match any signal The signal number that is used to match any signal
@@ -44,63 +47,60 @@ enum
- When used as a parameter to event_add, it represents a class of events, and function_name is the name of the function which will be called whenever an event matching the specified class occurs. This is also how events are stored internally. - When used as a parameter to event_add, it represents a class of events, and function_name is the name of the function which will be called whenever an event matching the specified class occurs. This is also how events are stored internally.
- When used as a parameter to event_get, event_remove and event_fire, it represents a class of events, and if the function_name field is non-zero, only events which call the specified function will be returned. - When used as a parameter to event_get, event_remove and event_fire, it represents a class of events, and if the function_name field is non-zero, only events which call the specified function will be returned.
*/ */
typedef struct struct event_t
{ {
/** /**
Type of event Type of event
*/ */
int type; int type;
/** /** The type-specific parameter. The int types are one of the following:
The type-specific parameter
*/
union
{
/**
Signal number for signal-type events.Use EVENT_ANY_SIGNAL
to match any signal
*/
int signal;
/**
Variable name for variable-type events.
*/
const wchar_t *variable;
/**
Process id for process-type events. Use EVENT_ANY_PID to
match any pid.
*/
pid_t pid;
/**
Job id for EVENT_JOB_ID type events
*/
int job_id;
/**
The parameter describing this generic event
*/
const wchar_t *param;
} param1; signal: Signal number for signal-type events.Use EVENT_ANY_SIGNAL to match any signal
pid: Process id for process-type events. Use EVENT_ANY_PID to match any pid.
job_id: Job id for EVENT_JOB_ID type events
*/
union {
int signal;
int job_id;
pid_t pid;
} param1;
/** The string types are one of the following:
variable: Variable name for variable-type events.
param: The parameter describing this generic event.
*/
wcstring str_param1;
/** /**
The name of the event handler function The name of the event handler function
*/ */
const wchar_t *function_name; wcstring function_name;
/** /**
The argument list. Only used when sending a new event using The argument list. Only used when sending a new event using
event_fire. In all other situations, the value of this variable event_fire. In all other situations, the value of this variable
is ignored. is ignored.
*/ */
array_list_t arguments; std::auto_ptr<wcstring_list_t> arguments;
}
event_t; event_t(int t) : type(t), param1() { }
/** Copy constructor */
event_t(const event_t &x);
static event_t signal_event(int sig);
static event_t variable_event(const wcstring &str);
static event_t generic_event(const wcstring &str);
};
/** /**
Add an event handler Add an event handler
May not be called by a signal handler, since it may allocate new memory. May not be called by a signal handler, since it may allocate new memory.
*/ */
void event_add_handler( event_t *event ); void event_add_handler( const event_t *event );
/** /**
Remove all events matching the specified criterion. Remove all events matching the specified criterion.
@@ -120,7 +120,13 @@ void event_remove( event_t *event );
\return the number of found matches \return the number of found matches
*/ */
int event_get( event_t *criterion, array_list_t *out ); int event_get( event_t *criterion, std::vector<event_t *> *out );
/**
Returns whether an event listener is registered for the given signal.
This is safe to call from a signal handler.
*/
bool event_is_signal_observed(int signal);
/** /**
Fire the specified event. The function_name field of the event must Fire the specified event. The function_name field of the event must
@@ -140,6 +146,9 @@ int event_get( event_t *criterion, array_list_t *out );
*/ */
void event_fire( event_t *event ); void event_fire( event_t *event );
/** Like event_fire, but takes a signal directly. */
void event_fire_signal(int signal);
/** /**
Initialize the event-handling library Initialize the event-handling library
*/ */
@@ -156,15 +165,14 @@ void event_destroy();
void event_free( event_t *e ); void event_free( event_t *e );
/** /**
Returns a string describing the specified event. The string should Returns a string describing the specified event.
not be freed.
*/ */
const wchar_t *event_get_desc( event_t *e ); wcstring event_get_desc( const event_t *e );
/** /**
Fire a generic event with the specified name Fire a generic event with the specified name
*/ */
#define event_fire_generic( ... ) event_fire_generic_internal( __VA_ARGS__, (void *)0 ) #define event_fire_generic( ... ) event_fire_generic_internal( __VA_ARGS__, NULL )
void event_fire_generic_internal(const wchar_t *name,...); void event_fire_generic_internal(const wchar_t *name,...);

File diff suppressed because it is too large Load Diff

18
exec.h
View File

@@ -9,9 +9,11 @@
#define FISH_EXEC_H #define FISH_EXEC_H
#include <wchar.h> #include <wchar.h>
#include <vector>
#include "proc.h" #include "proc.h"
#include "util.h" #include "util.h"
#include "common.h"
/** /**
pipe redirection error message pipe redirection error message
@@ -39,7 +41,8 @@
*/ */
void exec( job_t *j ); class parser_t;
void exec( parser_t &parser, job_t *j );
/** /**
Evaluate the expression cmd in a subshell, add the outputs into the Evaluate the expression cmd in a subshell, add the outputs into the
@@ -47,17 +50,17 @@ void exec( job_t *j );
proc_gfet_last_status will not be changed. proc_gfet_last_status will not be changed.
\param cmd the command to execute \param cmd the command to execute
\param l The list to insert output into.If \c l is zero, the output will be discarded. \param outputs The list to insert output into.
\return the status of the last job to exit, or -1 if en error was encountered. \return the status of the last job to exit, or -1 if en error was encountered.
*/ */
__warn_unused int exec_subshell( const wchar_t *cmd, __warn_unused int exec_subshell(const wcstring &cmd, std::vector<wcstring> &outputs );
array_list_t *l ); __warn_unused int exec_subshell(const wcstring &cmd );
/** /**
Loops over close until thesyscall was run without beeing Loops over close until the syscall was run without being
interrupted. Thenremoves the fd from the open_fds list. interrupted. Then removes the fd from the open_fds list.
*/ */
void exec_close( int fd ); void exec_close( int fd );
@@ -67,4 +70,7 @@ void exec_close( int fd );
*/ */
int exec_pipe( int fd[2]); int exec_pipe( int fd[2]);
/* Close all fds in open_fds. This is called from postfork.cpp */
void close_unused_internal_pipes( io_data_t *io );
#endif #endif

1870
expand.c

File diff suppressed because it is too large Load Diff

1629
expand.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -18,41 +18,37 @@
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
#include "common.h"
#include <list>
/** enum {
Flag specifying that cmdsubst expansion should be skipped /** Flag specifying that cmdsubst expansion should be skipped */
*/ EXPAND_SKIP_CMDSUBST = 1 << 0,
#define EXPAND_SKIP_CMDSUBST 1
/** /** Flag specifying that variable expansion should be skipped */
Flag specifying that variable expansion should be skipped EXPAND_SKIP_VARIABLES = 1 << 1,
*/
#define EXPAND_SKIP_VARIABLES 2
/** /** Flag specifying that wildcard expansion should be skipped */
Flag specifying that wildcard expansion should be skipped EXPAND_SKIP_WILDCARDS = 1 << 2,
*/
#define EXPAND_SKIP_WILDCARDS 4
/** /**
Incomplete matches in the last segment are ok (for tab Incomplete matches in the last segment are ok (for tab
completion). An incomplete match is a wildcard that matches a completion). An incomplete match is a wildcard that matches a
prefix of the filename. If accept_incomplete is true, only the prefix of the filename. If accept_incomplete is true, only the
remainder of the string is returned. remainder of the string is returned.
*/ */
#define ACCEPT_INCOMPLETE 8 ACCEPT_INCOMPLETE = 1 << 3,
/** /** Only match files that are executable by the current user. Only applicable together with ACCEPT_INCOMPLETE. */
Only match files that are executable by the current user. Only applicable together with ACCEPT_INCOMPLETE. EXECUTABLES_ONLY = 1 << 4,
*/
#define EXECUTABLES_ONLY 16 /** Only match directories. Only applicable together with ACCEPT_INCOMPLETE. */
DIRECTORIES_ONLY = 1 << 5,
/** /** Don't generate descriptions */
Only match directories. Only applicable together with ACCEPT_INCOMPLETE. EXPAND_NO_DESCRIPTIONS = 1 << 6
*/ };
typedef int expand_flags_t;
#define DIRECTORIES_ONLY 32
/** /**
Use unencoded private-use keycodes for internal characters Use unencoded private-use keycodes for internal characters
@@ -63,6 +59,8 @@
*/ */
#define EXPAND_RESERVED_END 0xf000f #define EXPAND_RESERVED_END 0xf000f
class completion_t;
enum enum
{ {
/** Character represeting a home directory */ /** Character represeting a home directory */
@@ -107,8 +105,7 @@ enum
EXPAND_WILDCARD_NO_MATCH, EXPAND_WILDCARD_NO_MATCH,
/* Ok, a wildcard in the string matched a file */ /* Ok, a wildcard in the string matched a file */
EXPAND_WILDCARD_MATCH EXPAND_WILDCARD_MATCH
} };
;
/** Character for separating two array elements. We use 30, i.e. the ascii record separator since that seems logical. */ /** Character for separating two array elements. We use 30, i.e. the ascii record separator since that seems logical. */
#define ARRAY_SEP 0x1e #define ARRAY_SEP 0x1e
@@ -121,6 +118,7 @@ enum
*/ */
#define ARRAY_BOUNDS_ERR _(L"Array index out of bounds") #define ARRAY_BOUNDS_ERR _(L"Array index out of bounds")
class parser_t;
/** /**
Perform various forms of expansion on in, such as tilde expansion Perform various forms of expansion on in, such as tilde expansion
@@ -130,52 +128,40 @@ enum
into the list out. into the list out.
If the parameter does not need expansion, it is copied into the list If the parameter does not need expansion, it is copied into the list
out. If expansion is performed, the original parameter is freed and out.
newly allocated strings are inserted into the list out.
If \c context is non-null, all the strings contained in the \param input The parameter to expand
array_list_t \c out will be registered to be free'd when context is \param output The list to which the result will be appended.
free'd.
\param context the halloc context to use for automatic deallocation
\param in The parameter to expand
\param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS \param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
\param out The list to which the result will be appended.
\return One of EXPAND_OK, EXPAND_ERROR, EXPAND_WILDCARD_MATCH and EXPAND_WILDCARD_NO_MATCH. EXPAND_WILDCARD_NO_MATCH and EXPAND_WILDCARD_MATCH are normal exit conditions used only on strings containing wildcards to tell if the wildcard produced any matches. \return One of EXPAND_OK, EXPAND_ERROR, EXPAND_WILDCARD_MATCH and EXPAND_WILDCARD_NO_MATCH. EXPAND_WILDCARD_NO_MATCH and EXPAND_WILDCARD_MATCH are normal exit conditions used only on strings containing wildcards to tell if the wildcard produced any matches.
*/ */
__warn_unused int expand_string( void *context, wchar_t *in, array_list_t *out, int flag ); __warn_unused int expand_string( const wcstring &input, std::vector<completion_t> &output, expand_flags_t flags );
/** /**
expand_one is identical to expand_string, except it will fail if in expand_one is identical to expand_string, except it will fail if in
expands to more than one string. This is used for expanding command expands to more than one string. This is used for expanding command
names. names.
If \c context is non-null, the returning string ill be registered \param inout_str The parameter to expand in-place
to be free'd when context is free'd.
\param context the halloc context to use for automatic deallocation
\param in The parameter to expand
\param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS \param flag Specifies if any expansion pass should be skipped. Legal values are any combination of EXPAND_SKIP_CMDSUBST EXPAND_SKIP_VARIABLES and EXPAND_SKIP_WILDCARDS
\return The expanded parameter, or 0 on failiure \return Whether expansion succeded
*/ */
wchar_t *expand_one( void *context, wchar_t *in, int flag ); bool expand_one( wcstring &inout_str, expand_flags_t flags );
/** /**
Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc. Suitable for pretty-printing. Convert the variable value to a human readable form, i.e. escape things, handle arrays, etc. Suitable for pretty-printing. The result must be free'd!
\param in the value to escape \param in the value to escape
*/ */
wchar_t *expand_escape_variable( const wchar_t *in ); wcstring expand_escape_variable( const wcstring &in );
/** /**
Perform tilde expansion and nothing else on the specified string. Perform tilde expansion and nothing else on the specified string, which is modified in place.
If tilde expansion is needed, the original string is freed and a \param input the string to tilde expand
new string, allocated using malloc, is returned.
\param in the string to tilde expand
*/ */
wchar_t *expand_tilde(wchar_t *in); void expand_tilde(wcstring &input);
/** /**
@@ -201,7 +187,7 @@ int expand_is_clean( const wchar_t *in );
\param token_pos The position where the expansion begins \param token_pos The position where the expansion begins
\param error_pos The position on the line to report to the error function. \param error_pos The position on the line to report to the error function.
*/ */
void expand_variable_error( const wchar_t *token, int token_pos, int error_pos ); void expand_variable_error( parser_t &parser, const wchar_t *token, int token_pos, int error_pos );
#endif #endif

View File

@@ -807,7 +807,7 @@ wchar_t *wcstok(wchar_t *wcs, const wchar_t *delim, wchar_t **save_ptr)
wchar_t *wcsdup( const wchar_t *in ) wchar_t *wcsdup( const wchar_t *in )
{ {
size_t len=wcslen(in); size_t len=wcslen(in);
wchar_t *out = malloc( sizeof( wchar_t)*(len+1)); wchar_t *out = (wchar_t *)malloc( sizeof( wchar_t)*(len+1));
if( out == 0 ) if( out == 0 )
{ {
return 0; return 0;
@@ -886,7 +886,7 @@ int wcwidth( wchar_t c )
#ifndef HAVE_WCSNDUP #ifndef HAVE_WCSNDUP
wchar_t *wcsndup( const wchar_t *in, int c ) wchar_t *wcsndup( const wchar_t *in, int c )
{ {
wchar_t *res = malloc( sizeof(wchar_t)*(c+1) ); wchar_t *res = (wchar_t *)malloc( sizeof(wchar_t)*(c+1) );
if( res == 0 ) if( res == 0 )
{ {
return 0; return 0;

View File

@@ -58,8 +58,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "exec.h" #include "exec.h"
#include "event.h" #include "event.h"
#include "output.h" #include "output.h"
#include "halloc.h"
#include "halloc_util.h"
#include "history.h" #include "history.h"
#include "path.h" #include "path.h"
@@ -73,35 +71,27 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
static int read_init() static int read_init()
{ {
wchar_t *config_dir; parser_t &parser = parser_t::principal_parser();
wchar_t *config_dir_escaped;
void *context;
string_buffer_t *eval_buff;
eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP ); parser.eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP );
eval( L"builtin . " SYSCONFDIR L"/fish/config.fish 2>/dev/null", 0, TOP ); parser.eval( L"builtin . " SYSCONFDIR L"/fish/config.fish 2>/dev/null", 0, TOP );
/* /*
We need to get the configuration directory before we can source the user configuration file We need to get the configuration directory before we can source the user configuration file
*/ */
context = halloc( 0, 0 ); wcstring config_dir;
eval_buff = sb_halloc( context );
config_dir = path_get_config( context );
/* /*
If config_dir is null then we have no configuration directory If path_get_config returns false then we have no configuration directory
and no custom config to load. and no custom config to load.
*/ */
if( config_dir ) if (path_get_config(config_dir))
{ {
config_dir_escaped = escape( config_dir, 1 ); wcstring config_dir_escaped = escape_string( config_dir, 1 );
sb_printf( eval_buff, L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped ); wcstring eval_buff = format_string(L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped.c_str());
eval( (wchar_t *)eval_buff->buff, 0, TOP ); parser.eval( eval_buff, 0, TOP );
free( config_dir_escaped );
} }
halloc_free( context );
return 1; return 1;
} }
@@ -110,7 +100,7 @@ static int read_init()
Parse the argument list, return the index of the first non-switch Parse the argument list, return the index of the first non-switch
arguments. arguments.
*/ */
static int fish_parse_opt( int argc, char **argv, char **cmd_ptr ) static int fish_parse_opt( int argc, char **argv, const char **cmd_ptr )
{ {
int my_optind; int my_optind;
int force_interactive=0; int force_interactive=0;
@@ -198,7 +188,7 @@ static int fish_parse_opt( int argc, char **argv, char **cmd_ptr )
else else
{ {
debug( 0, _(L"Invalid value '%s' for debug level switch"), optarg ); debug( 0, _(L"Invalid value '%s' for debug level switch"), optarg );
exit(1); exit_without_destructors(1);
} }
break; break;
} }
@@ -239,12 +229,12 @@ static int fish_parse_opt( int argc, char **argv, char **cmd_ptr )
_(L"%s, version %s\n"), _(L"%s, version %s\n"),
PACKAGE_NAME, PACKAGE_NAME,
PACKAGE_VERSION ); PACKAGE_VERSION );
exit( 0 ); exit_without_destructors( 0 );
} }
case '?': case '?':
{ {
exit( 1 ); exit_without_destructors( 1 );
} }
} }
@@ -270,24 +260,27 @@ static int fish_parse_opt( int argc, char **argv, char **cmd_ptr )
return my_optind; return my_optind;
} }
/** /**
Calls a bunch of init functions, parses the init files and then Calls a bunch of init functions, parses the init files and then
parses commands from stdin or files, depending on arguments parses commands from stdin or files, depending on arguments
*/ */
extern int g_fork_count;
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
struct stat tmp;
int res=1; int res=1;
char *cmd=0; const char *cmd=0;
int my_optind=0; int my_optind=0;
halloc_util_init(); set_main_thread();
setup_fork_guards();
wsetlocale( LC_ALL, L"" ); wsetlocale( LC_ALL, L"" );
is_interactive_session=1; is_interactive_session=1;
program_name=L"fish"; program_name=L"fish";
stat("----------FISH_HIT_MAIN----------", &tmp);
my_optind = fish_parse_opt( argc, argv, &cmd ); my_optind = fish_parse_opt( argc, argv, &cmd );
@@ -303,19 +296,24 @@ int main( int argc, char **argv )
proc_init(); proc_init();
event_init(); event_init();
wutil_init(); wutil_init();
parser_init(); //parser_init();
builtin_init(); builtin_init();
function_init(); function_init();
env_init(); env_init();
reader_init(); reader_init();
history_init(); history_init();
parser_t &parser = parser_t::principal_parser();
if (g_log_forks)
printf("%d: g_fork_count: %d\n", __LINE__, g_fork_count);
if( read_init() ) if( read_init() )
{ {
if( cmd != 0 ) if( cmd != 0 )
{ {
wchar_t *cmd_wcs = str2wcs( cmd ); wchar_t *cmd_wcs = str2wcs( cmd );
res = eval( cmd_wcs, 0, TOP ); res = parser.eval( cmd_wcs, 0, TOP );
free(cmd_wcs); free(cmd_wcs);
reader_exit(0, 0); reader_exit(0, 0);
} }
@@ -330,31 +328,30 @@ int main( int argc, char **argv )
char **ptr; char **ptr;
char *file = *(argv+(my_optind++)); char *file = *(argv+(my_optind++));
int i; int i;
string_buffer_t sb;
int fd; int fd;
wchar_t *rel_filename, *abs_filename; wchar_t *rel_filename, *abs_filename;
if( ( fd = open(file, O_RDONLY) ) == -1 ) if( ( fd = open(file, O_RDONLY) ) == -1 )
{ {
wperror( L"open" ); wperror( L"open" );
return 1; return 1;
} }
// OK to not do this atomically since we cannot have gone multithreaded yet
set_cloexec(fd);
if( *(argv+my_optind)) if( *(argv+my_optind))
{ {
sb_init( &sb ); wcstring sb;
for( i=1,ptr = argv+my_optind; *ptr; i++, ptr++ ) for( i=1,ptr = argv+my_optind; *ptr; i++, ptr++ )
{ {
if( i != 1 ) if( i != 1 )
sb_append( &sb, ARRAY_SEP_STR ); sb.append( ARRAY_SEP_STR );
wchar_t *val = str2wcs( *ptr ); sb.append( str2wcstring( *ptr ));
sb_append( &sb, val );
free( val );
} }
env_set( L"argv", (wchar_t *)sb.buff, 0 ); env_set( L"argv", sb.c_str(), 0 );
sb_destroy( &sb );
} }
rel_filename = str2wcs( file ); rel_filename = str2wcs( file );
@@ -387,17 +384,15 @@ int main( int argc, char **argv )
history_destroy(); history_destroy();
proc_destroy(); proc_destroy();
builtin_destroy(); builtin_destroy();
function_destroy();
reader_destroy(); reader_destroy();
parser_destroy(); parser.destroy();
wutil_destroy(); wutil_destroy();
event_destroy(); event_destroy();
halloc_util_destroy();
env_destroy(); env_destroy();
intern_free_all(); if (g_log_forks)
printf("%d: g_fork_count: %d\n", __LINE__, g_fork_count);
return res?STATUS_UNKNOWN_COMMAND:proc_get_last_status(); return res?STATUS_UNKNOWN_COMMAND:proc_get_last_status();
} }

View File

@@ -16,7 +16,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/ */
/** \file fish_indent.c /** \file fish_indent.cpp
The fish_indent proegram. The fish_indent proegram.
*/ */
@@ -37,8 +37,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "util.h" #include "util.h"
#include "common.h" #include "common.h"
#include "wutil.h" #include "wutil.h"
#include "halloc.h"
#include "halloc_util.h"
#include "tokenizer.h" #include "tokenizer.h"
#include "print_help.h" #include "print_help.h"
#include "parser_keywords.h" #include "parser_keywords.h"
@@ -49,9 +47,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define GETOPT_STRING "hvi" #define GETOPT_STRING "hvi"
/** /**
Read the entire contents of a file into the specified string_Buffer_t Read the entire contents of a file into the specified string
*/ */
static void read_file( FILE *f, string_buffer_t *b ) static void read_file( FILE *f, wcstring &b )
{ {
while( 1 ) while( 1 )
{ {
@@ -67,29 +65,23 @@ static void read_file( FILE *f, string_buffer_t *b )
break; break;
} }
b.push_back((wchar_t)c);
sb_append_char( b, c );
} }
} }
/** /**
Insert the specified number of tabe into the output buffer Insert the specified number of tabs into the output buffer
*/ */
static void insert_tabs( string_buffer_t *out, int indent ) static void insert_tabs( wcstring &out, int indent )
{ {
int i; out.append(indent, L'\t');
for( i=0; i<indent; i++ )
{
sb_append( out, L"\t" );
}
} }
/** /**
Indent the specified input Indent the specified input
*/ */
static int indent( string_buffer_t *out, wchar_t *in, int flags ) static int indent( wcstring &out, const wcstring &in, int flags )
{ {
tokenizer tok; tokenizer tok;
int res=0; int res=0;
@@ -99,7 +91,7 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
int prev_type = 0; int prev_type = 0;
int prev_prev_type = 0; int prev_prev_type = 0;
tok_init( &tok, in, TOK_SHOW_COMMENTS ); tok_init( &tok, in.c_str(), TOK_SHOW_COMMENTS );
for( ; tok_has_next( &tok ); tok_next( &tok ) ) for( ; tok_has_next( &tok ); tok_next( &tok ) )
{ {
@@ -115,22 +107,23 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
int next_indent = indent; int next_indent = indent;
is_command = 0; is_command = 0;
wchar_t *unesc = unescape( last, UNESCAPE_SPECIAL ); wcstring unesc = last;
unescape_string(unesc, UNESCAPE_SPECIAL);
if( parser_keywords_is_block( unesc ) ) if( parser_keywords_is_block(unesc))
{ {
next_indent++; next_indent++;
} }
else if( wcscmp( unesc, L"else" ) == 0 ) else if (unesc == L"else")
{ {
indent--; indent--;
} }
/* case should have the same indent level as switch*/ /* case should have the same indent level as switch*/
else if( wcscmp( unesc, L"case" ) == 0 ) else if (unesc == L"case")
{ {
indent--; indent--;
} }
else if( wcscmp( unesc, L"end" ) == 0 ) else if (unesc == L"end")
{ {
indent--; indent--;
next_indent--; next_indent--;
@@ -142,7 +135,7 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
insert_tabs( out, indent ); insert_tabs( out, indent );
} }
sb_printf( out, L"%ls", last ); append_format(out, L"%ls", last );
indent = next_indent; indent = next_indent;
@@ -150,8 +143,8 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
else else
{ {
if ( prev_type != TOK_REDIRECT_FD ) if ( prev_type != TOK_REDIRECT_FD )
sb_append( out, L" " ); out.append( L" " );
sb_append( out, last ); out.append( last );
} }
break; break;
@@ -160,7 +153,7 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_END: case TOK_END:
{ {
if( prev_type != TOK_END || prev_prev_type != TOK_END ) if( prev_type != TOK_END || prev_prev_type != TOK_END )
sb_append( out, L"\n" ); out.append( L"\n" );
do_indent = 1; do_indent = 1;
is_command = 1; is_command = 1;
break; break;
@@ -168,64 +161,65 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_PIPE: case TOK_PIPE:
{ {
sb_append( out, L" " ); out.append( L" " );
if ( last[0] == '2' && !last[1] ) { if ( last[0] == '2' && !last[1] ) {
sb_append( out, L"^" ); out.append( L"^" );
} else if ( last[0] != '1' || last[1] ) { } else if ( last[0] != '1' || last[1] ) {
sb_append( out, last, L">" ); out.append( last);
out.append( L">" );
} }
sb_append( out, L"| " ); out.append( L" | " );
is_command = 1; is_command = 1;
break; break;
} }
case TOK_REDIRECT_OUT: case TOK_REDIRECT_OUT:
{ {
sb_append( out, L" " ); out.append( L" " );
if ( wcscmp( last, L"2" ) == 0 ) { if ( wcscmp( last, L"2" ) == 0 ) {
sb_append( out, L"^" ); out.append( L"^" );
} else { } else {
if ( wcscmp( last, L"1" ) != 0 ) if ( wcscmp( last, L"1" ) != 0 )
sb_append( out, last ); out.append( last );
sb_append( out, L">" ); out.append( L"> " );
} }
break; break;
} }
case TOK_REDIRECT_APPEND: case TOK_REDIRECT_APPEND:
{ {
sb_append( out, L" " ); out.append( L" " );
if ( wcscmp( last, L"2" ) == 0 ) { if ( wcscmp( last, L"2" ) == 0 ) {
sb_append( out, L"^^" ); out.append( L"^^" );
} else { } else {
if ( wcscmp( last, L"1" ) != 0 ) if ( wcscmp( last, L"1" ) != 0 )
sb_append( out, last ); out.append( last );
sb_append( out, L">>" ); out.append( L">> " );
} }
break; break;
} }
case TOK_REDIRECT_IN: case TOK_REDIRECT_IN:
{ {
sb_append( out, L" " ); out.append( L" " );
if ( wcscmp( last, L"0" ) != 0 ) if ( wcscmp( last, L"0" ) != 0 )
sb_append( out, last ); out.append( last );
sb_append( out, L"<" ); out.append( L"< " );
break; break;
} }
case TOK_REDIRECT_FD: case TOK_REDIRECT_FD:
{ {
sb_append( out, L" " ); out.append( L" " );
if ( wcscmp( last, L"1" ) != 0 ) if ( wcscmp( last, L"1" ) != 0 )
sb_append( out, last ); out.append( last );
sb_append( out, L">&" ); out.append( L">& " );
break; break;
} }
case TOK_BACKGROUND: case TOK_BACKGROUND:
{ {
sb_append( out, L"&\n" ); out.append( L"&\n" );
do_indent = 1; do_indent = 1;
is_command = 1; is_command = 1;
break; break;
@@ -238,7 +232,7 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
insert_tabs( out, indent ); insert_tabs( out, indent );
} }
sb_printf( out, L"%ls", last ); append_format( out, L"%ls", last );
do_indent = 1; do_indent = 1;
break; break;
} }
@@ -262,34 +256,20 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
/** /**
Remove any prefix and suffix newlines from the specified Remove any prefix and suffix newlines from the specified
string. Does not allocete a new string, edits the string in place string.
and returns a pointer somewhere into the string.
*/ */
static wchar_t *trim( wchar_t *in ) static void trim( wcstring &str )
{ {
wchar_t *end; if (str.empty())
return;
while( *in == L'\n' ) size_t pos = str.find_first_not_of(L" \n");
{ if (pos > 0)
in++; str.erase(0, pos);
}
end = in + wcslen(in); pos = str.find_last_not_of(L" \n");
if (pos != wcstring::npos && pos + 1 < str.length())
while( 1 ) str.erase(pos + 1);
{
if( end < in+2 )
break;
end--;
if( (*end == L'\n' ) && ( *(end-1) == L'\n' ) )
*end=0;
else
break;
}
return in;
} }
@@ -298,10 +278,9 @@ static wchar_t *trim( wchar_t *in )
*/ */
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
string_buffer_t sb_in;
string_buffer_t sb_out;
int do_indent=1; int do_indent=1;
set_main_thread();
setup_fork_guards();
wsetlocale( LC_ALL, L"" ); wsetlocale( LC_ALL, L"" );
program_name=L"fish_indent"; program_name=L"fish_indent";
@@ -378,31 +357,26 @@ int main( int argc, char **argv )
} }
} }
halloc_util_init(); wcstring sb_in, sb_out;
read_file( stdin, sb_in );
sb_init( &sb_in );
sb_init( &sb_out );
read_file( stdin, &sb_in );
wutil_init(); wutil_init();
if( !indent( &sb_out, (wchar_t *)sb_in.buff, do_indent ) ) if( !indent( sb_out, sb_in, do_indent ) )
{ {
fwprintf( stdout, L"%ls", trim( (wchar_t *)sb_out.buff) ); trim(sb_out);
fwprintf( stdout, L"%ls", sb_out.c_str() );
} }
else else
{ {
/* /*
Indenting failed - print original input Indenting failed - print original input
*/ */
fwprintf( stdout, L"%ls", (wchar_t *)sb_in.buff ); fwprintf( stdout, L"%ls", sb_in.c_str() );
} }
wutil_destroy(); wutil_destroy();
halloc_util_destroy();
return 0; return 0;
} }

View File

@@ -6,6 +6,8 @@
#include <unistd.h> #include <unistd.h>
#include <termios.h> #include <termios.h>
#include <string.h> #include <string.h>
#include <map>
#include <algorithm>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
@@ -47,6 +49,7 @@
#endif #endif
#include <errno.h> #include <errno.h>
#include <vector>
#include "fallback.h" #include "fallback.h"
#include "util.h" #include "util.h"
@@ -57,8 +60,6 @@
#include "output.h" #include "output.h"
#include "input_common.h" #include "input_common.h"
#include "env_universal.h" #include "env_universal.h"
#include "halloc.h"
#include "halloc_util.h"
#include "print_help.h" #include "print_help.h"
enum enum
@@ -136,17 +137,17 @@ static struct termios saved_modes;
static int is_ca_mode = 0; static int is_ca_mode = 0;
/** /**
This buffer_t is used to buffer the output of the pager to improve This buffer is used to buffer the output of the pager to improve
screen redraw performance bu cutting down the number of write() screen redraw performance bu cutting down the number of write()
calls to only one. calls to only one.
*/ */
static buffer_t *pager_buffer; static std::vector<char> pager_buffer;
/** /**
The environment variables used to specify the color of different The environment variables used to specify the color of different
tokens. tokens.
*/ */
static wchar_t *hightlight_var[] = static const wchar_t *hightlight_var[] =
{ {
L"fish_pager_color_prefix", L"fish_pager_color_prefix",
L"fish_pager_color_completion", L"fish_pager_color_completion",
@@ -156,9 +157,9 @@ static wchar_t *hightlight_var[] =
; ;
/** /**
This string_buffer_t contains the text that should be sent back to the calling program This string contains the text that should be sent back to the calling program
*/ */
static string_buffer_t out_buff; static wcstring out_buff;
/** /**
This is the file to which the output text should be sent. It is really a pipe. This is the file to which the output text should be sent. It is really a pipe.
*/ */
@@ -167,16 +168,16 @@ static FILE *out_file;
/** /**
Data structure describing one or a group of related completions Data structure describing one or a group of related completions
*/ */
typedef struct struct comp_t
{ {
/** /**
The list of all completin strings this entry applies to The list of all completin strings this entry applies to
*/ */
array_list_t *comp; wcstring_list_t comp;
/** /**
The description The description
*/ */
wchar_t *desc; wcstring desc;
/** /**
On-screen width of the completion string On-screen width of the completion string
*/ */
@@ -193,21 +194,20 @@ typedef struct
Minimum acceptable width Minimum acceptable width
*/ */
int min_width; int min_width;
} };
comp_t;
/** /**
This function translates from a highlight code to a specific color This function translates from a highlight code to a specific color
by check invironement variables by check invironement variables
*/ */
static int get_color( int highlight ) static rgb_color_t get_color( int highlight )
{ {
wchar_t *val; const wchar_t *val;
if( highlight < 0 ) if( highlight < 0 )
return FISH_COLOR_NORMAL; return rgb_color_t::normal();
if( highlight >= (4) ) if( highlight >= (4) )
return FISH_COLOR_NORMAL; return rgb_color_t::normal();
val = wgetenv( hightlight_var[highlight]); val = wgetenv( hightlight_var[highlight]);
@@ -218,10 +218,10 @@ static int get_color( int highlight )
if( !val ) if( !val )
{ {
return FISH_COLOR_NORMAL; return rgb_color_t::normal();
} }
return output_color_code( val ); return parse_color( val, false );
} }
/** /**
@@ -230,12 +230,11 @@ static int get_color( int highlight )
terminal size, so this function should be called when the terminal terminal size, so this function should be called when the terminal
changes size. changes size.
*/ */
static void recalc_width( array_list_t *l, const wchar_t *prefix ) static void recalc_width( std::vector<comp_t *> &lst, const wchar_t *prefix )
{ {
int i; for( size_t i=0; i<lst.size(); i++ )
for( i=0; i<al_get_count( l ); i++ )
{ {
comp_t *c = (comp_t *)al_get( l, i ); comp_t *c = lst.at(i);
c->min_width = mini( c->desc_width, maxi(0,termsize.ws_col/3 - 2)) + c->min_width = mini( c->desc_width, maxi(0,termsize.ws_col/3 - 2)) +
mini( c->desc_width, maxi(0,termsize.ws_col/5 - 4)) +4; mini( c->desc_width, maxi(0,termsize.ws_col/5 - 4)) +4;
@@ -247,7 +246,7 @@ static void recalc_width( array_list_t *l, const wchar_t *prefix )
Test if the specified character sequence has been entered on the Test if the specified character sequence has been entered on the
keyboard keyboard
*/ */
static int try_sequence( char *seq ) static int try_sequence( const char *seq )
{ {
int j, k; int j, k;
wint_t c=0; wint_t c=0;
@@ -277,7 +276,7 @@ static wint_t readch()
{ {
struct mapping struct mapping
{ {
char *seq; const char *seq;
wint_t bnd; wint_t bnd;
} }
; ;
@@ -342,7 +341,7 @@ static wint_t readch()
*/ */
static int pager_buffered_writer( char c) static int pager_buffered_writer( char c)
{ {
b_append( pager_buffer, &c, 1 ); pager_buffer.push_back(c);
return 0; return 0;
} }
@@ -351,8 +350,10 @@ static int pager_buffered_writer( char c)
*/ */
static void pager_flush() static void pager_flush()
{ {
write_loop( 1, pager_buffer->buff, pager_buffer->used ); if (! pager_buffer.empty()) {
pager_buffer->used = 0; write_loop( 1, & pager_buffer.at(0), pager_buffer.size() * sizeof(char) );
pager_buffer.clear();
}
} }
/** /**
@@ -391,7 +392,6 @@ static int print_max( const wchar_t *str, int max, int has_more )
static void completion_print_item( const wchar_t *prefix, comp_t *c, int width ) static void completion_print_item( const wchar_t *prefix, comp_t *c, int width )
{ {
int comp_width=0, desc_width=0; int comp_width=0, desc_width=0;
int i;
int written=0; int written=0;
if( c->pref_width <= width ) if( c->pref_width <= width )
@@ -422,15 +422,15 @@ static void completion_print_item( const wchar_t *prefix, comp_t *c, int width )
} }
for( i=0; i<al_get_count( c->comp ); i++ ) for( size_t i=0; i<c->comp.size(); i++ )
{ {
const wchar_t *comp = (const wchar_t *)al_get( c->comp, i ); const wcstring &comp = c->comp.at(i);
if( i != 0 ) if( i != 0 )
written += print_max( L" ", comp_width - written, 2 ); written += print_max( L" ", comp_width - written, 2 );
set_color( get_color(HIGHLIGHT_PAGER_PREFIX),FISH_COLOR_NORMAL ); set_color( get_color(HIGHLIGHT_PAGER_PREFIX),rgb_color_t::normal() );
written += print_max( prefix, comp_width - written, comp[0]?1:0 ); written += print_max( prefix, comp_width - written, comp.empty()?0:1 );
set_color( get_color(HIGHLIGHT_PAGER_COMPLETION),FISH_COLOR_IGNORE ); set_color( get_color(HIGHLIGHT_PAGER_COMPLETION),rgb_color_t::ignore() );
written += print_max( comp, comp_width - written, i!=(al_get_count(c->comp)-1) ); written += print_max( comp.c_str(), comp_width - written, i!=(c->comp.size()-1) );
} }
@@ -442,9 +442,8 @@ static void completion_print_item( const wchar_t *prefix, comp_t *c, int width )
writech( L' '); writech( L' ');
} }
written += print_max( L"(", 1, 0 ); written += print_max( L"(", 1, 0 );
set_color( get_color( HIGHLIGHT_PAGER_DESCRIPTION ), set_color( get_color( HIGHLIGHT_PAGER_DESCRIPTION ), rgb_color_t::ignore() );
FISH_COLOR_IGNORE ); written += print_max( c->desc.c_str(), desc_width, 0 );
written += print_max( c->desc, desc_width, 0 );
written += print_max( L")", 1, 0 ); written += print_max( L")", 1, 0 );
} }
else else
@@ -477,10 +476,10 @@ static void completion_print( int cols,
int row_stop, int row_stop,
wchar_t *prefix, wchar_t *prefix,
int is_quoted, int is_quoted,
array_list_t *l) std::vector<comp_t *> &lst )
{ {
int rows = (al_get_count( l )-1)/cols+1; int rows = (lst.size()-1)/cols+1;
int i, j; int i, j;
for( i = row_start; i<row_stop; i++ ) for( i = row_start; i<row_stop; i++ )
@@ -491,10 +490,10 @@ static void completion_print( int cols,
int is_last = (j==(cols-1)); int is_last = (j==(cols-1));
if( al_get_count( l ) <= j*rows + i ) if( (int)lst.size() <= j*rows + i )
continue; continue;
el = (comp_t *)al_get( l, j*rows + i ); el = lst.at(j*rows + i );
completion_print_item( prefix, el, width[j] - (is_last?0:2) ); completion_print_item( prefix, el, width[j] - (is_last?0:2) );
@@ -527,7 +526,7 @@ static void completion_print( int cols,
static int completion_try_print( int cols, static int completion_try_print( int cols,
wchar_t *prefix, wchar_t *prefix,
int is_quoted, int is_quoted,
array_list_t *l ) std::vector<comp_t *> &lst )
{ {
/* /*
The calculated preferred width of each column The calculated preferred width of each column
@@ -548,7 +547,7 @@ static int completion_try_print( int cols,
int i, j; int i, j;
int rows = (al_get_count( l )-1)/cols+1; int rows = (lst.size()-1)/cols+1;
int pref_tot_width=0; int pref_tot_width=0;
int min_tot_width = 0; int min_tot_width = 0;
@@ -570,10 +569,10 @@ static int completion_try_print( int cols,
{ {
int pref,min; int pref,min;
comp_t *c; comp_t *c;
if( al_get_count( l ) <= j*rows + i ) if( (int)lst.size() <= j*rows + i )
continue; continue;
c = (comp_t *)al_get( l, j*rows + i ); c = lst.at(j*rows + i );
pref = c->pref_width; pref = c->pref_width;
min = c->min_width; min = c->min_width;
@@ -610,7 +609,7 @@ static int completion_try_print( int cols,
} }
else else
{ {
int next_rows = (al_get_count( l )-1)/(cols-1)+1; int next_rows = (lst.size()-1)/(cols-1)+1;
/* fwprintf( stderr, /* fwprintf( stderr,
L"cols %d, min_tot %d, term %d, rows=%d, nextrows %d, termrows %d, diff %d\n", L"cols %d, min_tot %d, term %d, rows=%d, nextrows %d, termrows %d, diff %d\n",
cols, cols,
@@ -669,7 +668,7 @@ static int completion_try_print( int cols,
writembs(exit_ca_mode); writembs(exit_ca_mode);
} }
completion_print( cols, width, 0, rows, prefix, is_quoted, l); completion_print( cols, width, 0, rows, prefix, is_quoted, lst);
pager_flush(); pager_flush();
} }
else else
@@ -695,30 +694,19 @@ static int completion_try_print( int cols,
termsize.ws_row-1, termsize.ws_row-1,
prefix, prefix,
is_quoted, is_quoted,
l); lst);
/* /*
List does not fit on screen. Print one screenfull and List does not fit on screen. Print one screenfull and
leave a scrollable interface leave a scrollable interface
*/ */
while(do_loop) while(do_loop)
{ {
string_buffer_t msg; set_color( rgb_color_t::black(), get_color(HIGHLIGHT_PAGER_PROGRESS) );
sb_init( &msg ); wcstring msg = format_string(_(L" %d to %d of %d"), pos, pos+termsize.ws_row-1, rows );
msg.append(L" \r" );
set_color( FISH_COLOR_BLACK, writestr(msg.c_str());
get_color(HIGHLIGHT_PAGER_PROGRESS) ); set_color( rgb_color_t::normal(), rgb_color_t::normal() );
sb_printf( &msg,
_(L" %d to %d of %d"),
pos,
pos+termsize.ws_row-1,
rows );
sb_printf( &msg,
L" \r" );
writestr((wchar_t *)msg.buff);
sb_destroy( &msg );
set_color( FISH_COLOR_NORMAL, FISH_COLOR_NORMAL );
pager_flush(); pager_flush();
int c = readch(); int c = readch();
@@ -737,7 +725,7 @@ static int completion_try_print( int cols,
pos+1, pos+1,
prefix, prefix,
is_quoted, is_quoted,
l ); lst );
writembs( tparm( cursor_address, writembs( tparm( cursor_address,
termsize.ws_row-1, 0) ); termsize.ws_row-1, 0) );
writembs(clr_eol ); writembs(clr_eol );
@@ -758,7 +746,7 @@ static int completion_try_print( int cols,
pos+termsize.ws_row-1, pos+termsize.ws_row-1,
prefix, prefix,
is_quoted, is_quoted,
l ); lst );
} }
break; break;
} }
@@ -777,7 +765,7 @@ static int completion_try_print( int cols,
pos+termsize.ws_row-1, pos+termsize.ws_row-1,
prefix, prefix,
is_quoted, is_quoted,
l ); lst );
} }
else else
{ {
@@ -802,7 +790,7 @@ static int completion_try_print( int cols,
pos+termsize.ws_row-1, pos+termsize.ws_row-1,
prefix, prefix,
is_quoted, is_quoted,
l ); lst );
} }
else else
{ {
@@ -822,7 +810,7 @@ static int completion_try_print( int cols,
default: default:
{ {
sb_append_char( &out_buff, c ); out_buff.push_back( c );
do_loop = 0; do_loop = 0;
break; break;
} }
@@ -839,139 +827,121 @@ static int completion_try_print( int cols,
inside completion descriptions. Remove all whitespace from inside completion descriptions. Remove all whitespace from
beginning/end of completion descriptions. beginning/end of completion descriptions.
*/ */
static void mangle_descriptions( array_list_t *l ) static void mangle_descriptions( wcstring_list_t &lst )
{ {
int i, skip; int skip;
for( i=0; i<al_get_count( l ); i++ ) for( size_t i=0; i<lst.size(); i++ )
{ {
wchar_t *next = (wchar_t *)al_get(l, i); wcstring &next = lst.at(i);
wchar_t *in, *out; size_t in, out;
skip=1; skip=1;
while( *next != COMPLETE_SEP && *next ) size_t next_idx = 0;
next++; while( next_idx < next.size() && next[next_idx] != COMPLETE_SEP )
next_idx++;
if( !*next ) if( next_idx == next.size() )
continue; continue;
in=out=(next+1); in=out=next_idx + 1;
while( *in != 0 ) while( in < next.size() )
{ {
if( *in == L' ' || *in==L'\t' || *in<32 ) if( next[in] == L' ' || next[in]==L'\t' || next[in]<32 )
{ {
if( !skip ) if( !skip )
*out++=L' '; next[out++]=L' ';
skip=1; skip=1;
} }
else else
{ {
*out++ = *in; next[out++] = next[in];
skip=0; skip=0;
} }
in++; in++;
} }
*out=0; next.resize(out);
} }
} }
/** /**
Merge multiple completions with the same description to the same line Merge multiple completions with the same description to the same line
*/ */
static void join_completions( array_list_t *l ) static void join_completions( wcstring_list_t lst )
{ {
long i; long i;
int in, out;
hash_table_t desc_table;
hash_init( &desc_table, &hash_wcs_func, &hash_wcs_cmp ); std::map<wcstring, long> desc_table;
for( i=0; i<al_get_count(l); i++ ) for( i=0; i<(long)lst.size(); i++ )
{ {
wchar_t *item = (wchar_t *)al_get( l, i ); const wchar_t *item = lst.at(i).c_str();
wchar_t *desc = wcschr( item, COMPLETE_SEP ); const wchar_t *desc = wcschr( item, COMPLETE_SEP );
long prev_idx; long prev_idx;
if( !desc ) if( !desc )
continue; continue;
desc++; desc++;
prev_idx = ((long)hash_get( &desc_table, desc) )-1; prev_idx = desc_table[desc] - 1;
if( prev_idx == -1 ) if( prev_idx == -1 )
{ {
hash_put( &desc_table, halloc_wcsdup( global_context, desc), (void *)(i+1)); desc_table[desc] = i+1;
} }
else else
{ {
string_buffer_t foo; const wchar_t *old = lst.at(i).c_str();
wchar_t *old = (wchar_t *)al_get( l, prev_idx ); const wchar_t *old_end = wcschr( old, COMPLETE_SEP );
wchar_t *old_end = wcschr( old, COMPLETE_SEP );
if( old_end ) if( old_end )
{ {
*old_end = 0;
sb_init( &foo ); wcstring foo;
sb_append( &foo, old ); foo.append(old, old_end - old);
sb_append_char( &foo, COMPLETE_ITEM_SEP ); foo.push_back(COMPLETE_ITEM_SEP);
sb_append( &foo, item ); foo.append(item);
free( (void *)al_get( l, prev_idx ) ); lst.at(prev_idx) = foo;
al_set( l, prev_idx, foo.buff ); lst.at(i).clear();
free( (void *)al_get( l, i ) );
al_set( l, i, 0 );
} }
} }
} }
hash_destroy( &desc_table );
out=0; /* Remove empty strings */
for( in=0; in < al_get_count(l); in++ ) lst.erase(remove(lst.begin(), lst.end(), wcstring()), lst.end());
{
const void * d = al_get( l, in );
if( d )
{
al_set( l, out++, d );
}
}
al_truncate( l, out );
} }
/** /**
Replace completion strings with a comp_t structure Replace completion strings with a comp_t structure
*/ */
static void mangle_completions( array_list_t *l, const wchar_t *prefix ) static std::vector<comp_t *> mangle_completions( wcstring_list_t &lst, const wchar_t *prefix )
{ {
int i; std::vector<comp_t *> result;
for( size_t i=0; i<lst.size(); i++ )
for( i=0; i<al_get_count( l ); i++ )
{ {
wchar_t *next = (wchar_t *)al_get( l, i ); wcstring &next = lst.at(i);
wchar_t *start, *end; size_t start, end;
comp_t *comp = halloc( global_context, sizeof( comp_t ) );
comp->comp = al_halloc( global_context );
for( start=end=next; 1; end++ ) comp_t zerod = {};
comp_t *comp = new comp_t(zerod);
for( start=end=0; 1; end++ )
{ {
wchar_t c = *end; wchar_t c = next.c_str()[end];
if( (c == COMPLETE_ITEM_SEP) || (c==COMPLETE_SEP) || !c) if( (c == COMPLETE_ITEM_SEP) || (c==COMPLETE_SEP) || !c)
{ {
*end = 0; wcstring start2 = wcstring(next, start, end - start);
wchar_t * str = escape( start, ESCAPE_ALL | ESCAPE_NO_QUOTED ); wcstring str = escape_string(start2, ESCAPE_ALL | ESCAPE_NO_QUOTED);
comp->comp_width += my_wcswidth( str.c_str() );
comp->comp_width += my_wcswidth( str ); comp->comp.push_back(str);
halloc_register( global_context, str );
al_push( comp->comp, str );
start = end+1; start = end+1;
} }
if( c == COMPLETE_SEP ) if( c == COMPLETE_SEP )
{ {
comp->desc = halloc_wcsdup( global_context, start ); comp->desc = next.c_str() + start;
break; break;
} }
@@ -980,16 +950,16 @@ static void mangle_completions( array_list_t *l, const wchar_t *prefix )
} }
comp->comp_width += my_wcswidth(prefix)*al_get_count(comp->comp) + 2*(al_get_count(comp->comp)-1); comp->comp_width += my_wcswidth(prefix)*comp->comp.size() + 2*(comp->comp.size()-1);
comp->desc_width = comp->desc?my_wcswidth( comp->desc ):0; comp->desc_width = comp->desc.empty()?0:my_wcswidth( comp->desc.c_str() );
comp->pref_width = comp->comp_width + comp->desc_width + (comp->desc_width?4:0); comp->pref_width = comp->comp_width + comp->desc_width + (comp->desc_width?4:0);
free( next ); result.push_back(comp);
al_set( l, i, comp );
} }
recalc_width( l, prefix ); recalc_width( result, prefix );
return result;
} }
@@ -1039,6 +1009,7 @@ static void init( int mangle_descriptors, int out )
close(1); close(1);
close(0); close(0);
/* OK to not use CLO_EXEC here because fish_pager is single threaded */
if( (in = open( ttyname(2), O_RDWR )) != -1 ) if( (in = open( ttyname(2), O_RDWR )) != -1 )
{ {
if( dup2( 2, 1 ) == -1 ) if( dup2( 2, 1 ) == -1 )
@@ -1067,16 +1038,9 @@ static void init( int mangle_descriptors, int out )
} }
/**
Init the stringbuffer used to keep any output in
*/
sb_init( &out_buff );
env_universal_init( 0, 0, 0, 0); env_universal_init( 0, 0, 0, 0);
input_common_init( &interrupt_handler ); input_common_init( &interrupt_handler );
output_set_writer( &pager_buffered_writer ); output_set_writer( &pager_buffered_writer );
pager_buffer = halloc( global_context, sizeof( buffer_t ) );
halloc_register_function( global_context, (void (*)(void *))&b_destroy, pager_buffer );
sigemptyset( & act.sa_mask ); sigemptyset( & act.sa_mask );
act.sa_flags=0; act.sa_flags=0;
@@ -1125,6 +1089,15 @@ static void init( int mangle_descriptors, int out )
free( wterm ); free( wterm );
} }
/* Infer term256 support */
char *fish_term256 = getenv("fish_term256");
bool support_term256;
if (fish_term256) {
support_term256 = from_string<bool>(fish_term256);
} else {
support_term256 = term && strstr(term, "256color");
}
output_set_supports_term256(support_term256);
} }
/** /**
@@ -1140,7 +1113,6 @@ static void destroy()
debug( 0, _(L"Error while closing terminfo") ); debug( 0, _(L"Error while closing terminfo") );
} }
sb_destroy( &out_buff );
fclose( out_file ); fclose( out_file );
} }
@@ -1148,18 +1120,15 @@ static void destroy()
Read lines of input from the specified file, unescape them and Read lines of input from the specified file, unescape them and
insert them into the specified list. insert them into the specified list.
*/ */
static void read_array( FILE* file, array_list_t *comp ) static void read_array( FILE* file, wcstring_list_t &comp )
{ {
buffer_t buffer; std::vector<char> buffer;
int c; int c;
char cc; wchar_t *wcs;
wchar_t *wcs, *unescaped;
b_init( &buffer );
while( !feof( file ) ) while( !feof( file ) )
{ {
buffer.used=0; buffer.clear();
while( 1 ) while( 1 )
{ {
@@ -1174,31 +1143,26 @@ static void read_array( FILE* file, array_list_t *comp )
break; break;
} }
cc=c; buffer.push_back(static_cast<char>(c));
b_append( &buffer, &cc, 1 );
} }
if( buffer.used ) if( ! buffer.empty() )
{ {
cc=0; buffer.push_back(0);
b_append( &buffer, &cc, 1 );
wcs = str2wcs( buffer.buff ); wcs = str2wcs( &buffer.at(0) );
if( wcs ) if( wcs )
{ {
unescaped = unescape( wcs, 0 ); wcstring tmp = wcs;
if( unescaped ) if (unescape_string(tmp, 0))
{ {
al_push( comp, unescaped ); comp.push_back(tmp);
} }
free( wcs ); free( wcs );
} }
} }
} }
b_destroy( &buffer );
} }
static int get_fd( const char *str ) static int get_fd( const char *str )
@@ -1221,22 +1185,22 @@ int main( int argc, char **argv )
{ {
int i; int i;
int is_quoted=0; int is_quoted=0;
array_list_t *comp; wcstring_list_t comp;
wchar_t *prefix = 0; wchar_t *prefix = 0;
int mangle_descriptors = 0; int mangle_descriptors = 0;
int result_fd = -1; int result_fd = -1;
set_main_thread();
setup_fork_guards();
/* /*
This initialization is made early, so that the other init code This initialization is made early, so that the other init code
can use global_context for memory managment can use global_context for memory managment
*/ */
halloc_util_init();
program_name = L"fish_pager"; program_name = L"fish_pager";
wsetlocale( LC_ALL, L"" ); wsetlocale( LC_ALL, L"" );
comp = al_halloc( global_context );
/* /*
The call signature for fish_pager is a mess. Because we want The call signature for fish_pager is a mess. Because we want
@@ -1405,11 +1369,8 @@ int main( int argc, char **argv )
*/ */
for( i=3; i<argc; i++ ) for( i=3; i<argc; i++ )
{ {
wchar_t *wcs = str2wcs( argv[i] ); wcstring wcs = str2wcstring( argv[i] );
if( wcs ) comp.push_back(wcs);
{
al_push( comp, wcs );
}
} }
} }
else else
@@ -1432,7 +1393,7 @@ int main( int argc, char **argv )
if( wcscmp( prefix, L"-" ) == 0 ) if( wcscmp( prefix, L"-" ) == 0 )
join_completions( comp ); join_completions( comp );
mangle_completions( comp, prefix ); std::vector<comp_t *> completions = mangle_completions( comp, prefix );
/** /**
Try to print the completions. Start by trying to print the Try to print the completions. Start by trying to print the
@@ -1442,7 +1403,7 @@ int main( int argc, char **argv )
*/ */
for( i = PAGER_MAX_COLS; i>0; i-- ) for( i = PAGER_MAX_COLS; i>0; i-- )
{ {
switch( completion_try_print( i, prefix, is_quoted, comp ) ) switch( completion_try_print( i, prefix, is_quoted, completions ) )
{ {
case PAGER_RETRY: case PAGER_RETRY:
@@ -1467,7 +1428,7 @@ int main( int argc, char **argv )
free(prefix ); free(prefix );
fwprintf( out_file, L"%ls", (wchar_t *)out_buff.buff ); fwprintf( out_file, L"%ls", out_buff.c_str() );
if( is_ca_mode ) if( is_ca_mode )
{ {
writembs(exit_ca_mode); writembs(exit_ca_mode);
@@ -1475,7 +1436,5 @@ int main( int argc, char **argv )
} }
destroy(); destroy();
halloc_util_destroy();
} }

View File

@@ -1,900 +0,0 @@
/** \file fish_tests.c
Various bug and feature tests. Compiled and run by make test.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <signal.h>
#include <locale.h>
#include <dirent.h>
#include <time.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "proc.h"
#include "reader.h"
#include "builtin.h"
#include "function.h"
#include "complete.h"
#include "wutil.h"
#include "env.h"
#include "expand.h"
#include "parser.h"
#include "tokenizer.h"
#include "output.h"
#include "exec.h"
#include "event.h"
#include "path.h"
#include "halloc.h"
#include "halloc_util.h"
/**
The number of tests to run
*/
#define ESCAPE_TEST_COUNT 1000000
/**
The average length of strings to unescape
*/
#define ESCAPE_TEST_LENGTH 100
/**
The higest character number of character to try and escape
*/
#define ESCAPE_TEST_CHAR 4000
/**
Number of laps to run performance testing loop
*/
#define LAPS 50
/**
The result of one of the test passes
*/
#define NUM_ANS L"-7 99999999 1234567 deadbeef DEADBEEFDEADBEEF"
/**
Number of encountered errors
*/
static int err_count=0;
/**
Print formatted output
*/
static void say( wchar_t *blah, ... )
{
va_list va;
va_start( va, blah );
vwprintf( blah, va );
va_end( va );
wprintf( L"\n" );
}
/**
Print formatted error string
*/
static void err( wchar_t *blah, ... )
{
va_list va;
va_start( va, blah );
err_count++;
wprintf( L"Error: " );
vwprintf( blah, va );
va_end( va );
wprintf( L"\n" );
}
/**
Compare two pointers
*/
static int pq_compare( void *e1, void *e2 )
{
return e1-e2;
}
/**
Test priority queue functionality
*/
static void pq_test( int elements )
{
int i;
int prev;
int *count = calloc( sizeof(int), 100 );
priority_queue_t q;
pq_init( &q, pq_compare );
for( i=0; i<elements; i++ )
{
long foo = rand() % 100;
// printf( "Adding %d\n", foo );
pq_put( &q, (void *)foo );
count[foo]++;
}
prev = 100;
for( i=0; i<elements; i++ )
{
long pos = (long)pq_get( &q );
count[ pos ]--;
if( pos > prev )
err( L"Wrong order of elements in priority_queue_t" );
prev = pos;
}
for( i=0; i<100; i++ )
{
if( count[i] != 0 )
{
err( L"Wrong number of elements in priority_queue_t" );
}
}
}
/**
Test stack functionality
*/
static int stack_test( int elements )
{
long i;
int res=1;
array_list_t s;
al_init( &s );
for( i=0; i<elements; i++ )
{
long foo;
al_push_long( &s, i);
al_push_long( &s, i);
if( (foo=al_pop_long( &s )) != i )
{
err( L"Unexpected data" );
res = 0;
break;
}
}
for( i=0; i<elements; i++ )
{
long foo;
if( (foo=al_pop_long( &s )) != (elements-i-1) )
{
err( L"Unexpected data" );
res = 0;
break;
}
}
al_destroy( &s );
return res;
}
/**
Hash function for pointers
*/
static int hash_func( void *data )
{
/* srand( (int)data );
return rand();
*/
int foo = (int)(long)data;
return 127*((foo^0xefc7e214)) ^(foo<<11);
}
/**
Pointer hash comparison function
*/
static int compare_func( void *key1, void *key2 )
{
return key1==key2;
}
/**
Hashtable test
*/
static int hash_test( long elements )
{
long i;
int res=1;
hash_table_t h;
hash_init( &h, hash_func, compare_func );
for( i=1; i< elements+1; i++ )
{
hash_put( &h, (void*)i, (void*)100l-i );
}
for( i=1; i< elements+1; i++ )
{
if( (long)hash_get( &h, (void*)i ) != (100l-i) )
{
err( L"Key %d gave data %d, expected data %d",
i,
(long)hash_get( &h, (void*)i ),
100l-i );
res = 0;
break;
}
}
if( hash_get_count( &h ) != elements )
{
err( L"Table holds %d elements, should hold %d elements",
hash_get_count( &h ),
elements );
res = 0;
}
for( i=1; i<elements+1; i+=2 )
{
hash_remove( &h, (void*)i, 0, 0 );
}
if( hash_get_count( &h ) != ((elements)/2) )
{
err( L"Table contains %d elements, should contain %d elements",
hash_get_count( &h ),
elements/2 );
res = 0;
}
for( i=1; i<elements+1; i++ )
{
if( hash_contains( &h, (void*)i) != (i+1l)%2l )
{
if( i%2 )
err( L"Key %d remains, should be deleted",
i );
else
err( L"Key %d does not exist",
i );
res = 0;
break;
}
}
hash_destroy( &h );
return res;
}
/**
Arraylist test
*/
static void al_test( int sz)
{
long i;
array_list_t l;
al_init( &l );
al_set_long( &l, 1, 7L );
al_set_long( &l, sz, 7L );
if( al_get_count( &l ) != maxi( sz+1, 2 ) )
err( L"Wrong number of elements in array list" );
for( i=0; i<al_get_count( &l ); i++ )
{
long val = al_get_long( &l, i );
if( (i == 1) || (i==sz))
{
if( val != 7 )
err( L"Canary changed to %d at index %d", val, i );
}
else
{
if( val != 0 )
err( L"False canary %d found at index %d", val, i );
}
}
}
/**
Stringbuffer test
*/
static void sb_test()
{
string_buffer_t b;
int res;
sb_init( &b );
if( (res=sb_printf( &b, L"%ls%s", L"Testing ", "string_buffer_t " )) == -1 )
{
err( L"Error %d while testing stringbuffers", res );
}
if( (res=sb_printf( &b, L"%ls", L"functionality" ))==-1)
{
err( L"Error %d while testing stringbuffers", res );
}
say( (wchar_t *)b.buff );
sb_clear( &b );
sb_printf( &b, L"%d %u %o %x %llX", -7, 99999999, 01234567, 0xdeadbeef, 0xdeadbeefdeadbeefll );
if( wcscmp( (wchar_t *)b.buff, NUM_ANS) != 0 )
{
err( L"numerical formating is broken, '%ls' != '%ls'", (wchar_t *)b.buff, NUM_ANS );
}
else
say( L"numerical formating works" );
}
/**
Performs all tests of the util library
*/
static void test_util()
{
int i;
say( L"Testing utility library" );
for( i=0; i<18; i++ )
{
long t1, t2;
pq_test( 1<<i );
stack_test( 1<<i );
t1 = get_time();
hash_test( 1<<i );
t2 = get_time();
if( i > 8 )
say( L"Hashtable uses %f microseconds per element at size %d",
((double)(t2-t1))/(1<<i),
1<<i );
al_test( 1<<i );
}
sb_test();
/*
int i;
for( i=2; i<10000000; i*=2 )
{
printf( "%d", i );
t1 = get_time();
if(!hash_test(i))
exit(0);
t2 = get_time();
printf( " %d\n", (t2-t1)/i );
}
*/
}
/**
Test the escaping/unescaping code by escaping/unescaping random
strings and verifying that the original string comes back.
*/
static void test_escape()
{
int i;
string_buffer_t sb;
say( L"Testing escaping and unescaping" );
sb_init( &sb );
for( i=0; i<ESCAPE_TEST_COUNT; i++ )
{
wchar_t *o, *e, *u;
sb_clear( &sb );
while( rand() % ESCAPE_TEST_LENGTH )
{
sb_append_char( &sb, (rand() %ESCAPE_TEST_CHAR) +1 );
}
o = (wchar_t *)sb.buff;
e = escape(o, 1);
u = unescape( e, 0 );
if( !o || !e || !u )
{
err( L"Escaping cycle of string %ls produced null pointer on %ls", o, e?L"unescaping":L"escaping" );
}
if( wcscmp(o, u) )
{
err( L"Escaping cycle of string %ls produced different string %ls", o, u );
}
free( e );
free( u );
}
}
/**
Test wide/narrow conversion by creating random strings and
verifying that the original string comes back thorugh double
conversion.
*/
static void test_convert()
{
/* char o[] =
{
-17, -128, -121, -68, 0
}
;
wchar_t *w = str2wcs(o);
char *n = wcs2str(w);
int i;
for( i=0; o[i]; i++ )
{
bitprint(o[i]);;
//wprintf(L"%d ", o[i]);
}
wprintf(L"\n");
for( i=0; w[i]; i++ )
{
wbitprint(w[i]);;
//wprintf(L"%d ", w[i]);
}
wprintf(L"\n");
for( i=0; n[i]; i++ )
{
bitprint(n[i]);;
//wprintf(L"%d ", n[i]);
}
wprintf(L"\n");
return;
*/
int i;
buffer_t sb;
say( L"Testing wide/narrow string conversion" );
b_init( &sb );
for( i=0; i<ESCAPE_TEST_COUNT; i++ )
{
wchar_t *w;
char *o, *n;
char c;
sb.used=0;
while( rand() % ESCAPE_TEST_LENGTH )
{
c = rand ();
b_append( &sb, &c, 1 );
}
c = 0;
b_append( &sb, &c, 1 );
o = (char *)sb.buff;
w = str2wcs(o);
n = wcs2str(w);
if( !o || !w || !n )
{
err( L"Conversion cycle of string %s produced null pointer on %s", o, w?L"str2wcs":L"wcs2str" );
}
if( strcmp(o, n) )
{
err( L"%d: Conversion cycle of string %s produced different string %s", i, o, n );
}
free( w );
free( n );
}
}
/**
Test the tokenizer
*/
static void test_tok()
{
tokenizer t;
say( L"Testing tokenizer" );
say( L"Testing invalid input" );
tok_init( &t, 0, 0 );
if( tok_last_type( &t ) != TOK_ERROR )
{
err(L"Invalid input to tokenizer was undetected" );
}
say( L"Testing use of broken tokenizer" );
if( !tok_has_next( &t ) )
{
err( L"tok_has_next() should return 1 once on broken tokenizer" );
}
tok_next( &t );
if( tok_last_type( &t ) != TOK_ERROR )
{
err(L"Invalid input to tokenizer was undetected" );
}
/*
This should crash if there is a bug. No reliable way to detect otherwise.
*/
say( L"Test destruction of broken tokenizer" );
tok_destroy( &t );
{
wchar_t *str = L"string <redirection 2>&1 'nested \"quoted\" '(string containing subshells ){and,brackets}$as[$well (as variable arrays)]";
const int types[] =
{
TOK_STRING, TOK_REDIRECT_IN, TOK_STRING, TOK_REDIRECT_FD, TOK_STRING, TOK_STRING, TOK_END
}
;
int i;
say( L"Test correct tokenization" );
for( i=0, tok_init( &t, str, 0 ); i<(sizeof(types)/sizeof(int)); i++,tok_next( &t ) )
{
if( types[i] != tok_last_type( &t ) )
{
err( L"Tokenization error:");
wprintf( L"Token number %d of string \n'%ls'\n, expected token type %ls, got token '%ls' of type %ls\n",
i+1,
str,
tok_get_desc(types[i]),
tok_last(&t),
tok_get_desc(tok_last_type( &t )) );
}
}
}
}
/**
Test the parser
*/
static void test_parser()
{
say( L"Testing parser" );
say( L"Testing null input to parser" );
if( !parser_test( 0, 0, 0, 0 ) )
{
err( L"Null input to parser_test undetected" );
}
say( L"Testing block nesting" );
if( !parser_test( L"if; end", 0, 0, 0 ) )
{
err( L"Incomplete if statement undetected" );
}
if( !parser_test( L"if test; echo", 0, 0, 0 ) )
{
err( L"Missing end undetected" );
}
if( !parser_test( L"if test; end; end", 0, 0, 0 ) )
{
err( L"Unbalanced end undetected" );
}
say( L"Testing detection of invalid use of builtin commands" );
if( !parser_test( L"case foo", 0, 0, 0 ) )
{
err( L"'case' command outside of block context undetected" );
}
if( !parser_test( L"switch ggg; if true; case foo;end;end", 0, 0, 0 ) )
{
err( L"'case' command outside of switch block context undetected" );
}
if( !parser_test( L"else", 0, 0, 0 ) )
{
err( L"'else' command outside of conditional block context undetected" );
}
if( !parser_test( L"break", 0, 0, 0 ) )
{
err( L"'break' command outside of loop block context undetected" );
}
if( !parser_test( L"exec ls|less", 0, 0, 0 ) || !parser_test( L"echo|return", 0, 0, 0 ))
{
err( L"Invalid pipe command undetected" );
}
say( L"Testing basic evaluation" );
if( !eval( 0, 0, TOP ) )
{
err( L"Null input when evaluating undetected" );
}
if( !eval( L"ls", 0, WHILE ) )
{
err( L"Invalid block mode when evaluating undetected" );
}
}
/**
Perform parameter expansion and test if the output equals the zero-terminated parameter list supplied.
\param in the string to expand
\param flags the flags to send to expand_string
*/
static int expand_test( const wchar_t *in, int flags, ... )
{
array_list_t out;
va_list va;
int i=0;
int res=1;
wchar_t *arg;
al_init( &out );
if( expand_string( 0, wcsdup(in), &out, flags) )
{
}
va_start( va, flags );
while( (arg=va_arg(va, wchar_t *) )!= 0 )
{
if( al_get_count( &out ) == i )
{
res=0;
break;
}
if( wcscmp( al_get( &out, i ),arg) != 0 )
{
res=0;
break;
}
i++;
}
va_end( va );
al_foreach( &out, &free );
return res;
}
/**
Test globbing and other parameter expansion
*/
static void test_expand()
{
say( L"Testing parameter expansion" );
if( !expand_test( L"foo", 0, L"foo", 0 ))
{
err( L"Strings do not expand to themselves" );
}
if( !expand_test( L"a{b,c,d}e", 0, L"abe", L"ace", L"ade", 0 ) )
{
err( L"Bracket expansion is broken" );
}
if( !expand_test( L"a*", EXPAND_SKIP_WILDCARDS, L"a*", 0 ) )
{
err( L"Cannot skip wildcard expansion" );
}
}
/**
Test path functions
*/
static void test_path()
{
say( L"Testing path functions" );
void *context = halloc( 0, 0 );
wchar_t *can = path_make_canonical( context, L"//foo//////bar/" );
if( wcscmp( can, L"/foo/bar" ) )
{
err( L"Bug in canonical PATH code" );
}
halloc_free( context );
}
/**
Test speed of completion calculations
*/
void perf_complete()
{
wchar_t c;
array_list_t out;
long long t1, t2;
int matches=0;
double t;
wchar_t str[3]=
{
0, 0, 0
}
;
int i;
say( L"Testing completion performance" );
al_init( &out );
reader_push(L"");
say( L"Here we go" );
t1 = get_time();
for( c=L'a'; c<=L'z'; c++ )
{
str[0]=c;
reader_set_buffer( str, 0 );
complete( str, &out );
matches += al_get_count( &out );
al_foreach( &out, &free );
al_truncate( &out, 0 );
}
t2=get_time();
t = (double)(t2-t1)/(1000000*26);
say( L"One letter command completion took %f seconds per completion, %f microseconds/match", t, (double)(t2-t1)/matches );
matches=0;
t1 = get_time();
for( i=0; i<LAPS; i++ )
{
str[0]='a'+(rand()%26);
str[1]='a'+(rand()%26);
reader_set_buffer( str, 0 );
complete( str, &out );
matches += al_get_count( &out );
al_foreach( &out, &free );
al_truncate( &out, 0 );
}
t2=get_time();
t = (double)(t2-t1)/(1000000*LAPS);
say( L"Two letter command completion took %f seconds per completion, %f microseconds/match", t, (double)(t2-t1)/matches );
al_destroy( &out );
reader_pop();
}
/**
Main test
*/
int main( int argc, char **argv )
{
setlocale( LC_ALL, "" );
srand( time( 0 ) );
program_name=L"(ignore)";
say( L"Testing low-level functionality");
say( L"Lines beginning with '(ignore):' are not errors, they are warning messages\ngenerated by the fish parser library when given broken input, and can be\nignored. All actual errors begin with 'Error:'." );
proc_init();
halloc_util_init();
event_init();
parser_init();
function_init();
builtin_init();
reader_init();
env_init();
test_util();
test_escape();
test_convert();
test_tok();
test_parser();
test_expand();
test_path();
say( L"Encountered %d errors in low-level tests", err_count );
/*
Skip performance tests for now, since they seem to hang when running from inside make (?)
*/
// say( L"Testing performance" );
// perf_complete();
env_destroy();
reader_destroy();
parser_destroy();
function_destroy();
builtin_destroy();
wutil_destroy();
event_destroy();
proc_destroy();
halloc_util_destroy();
}

1032
fish_tests.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -70,11 +70,18 @@ time the original barrier request was sent have been received.
#include "common.h" #include "common.h"
#include "wutil.h" #include "wutil.h"
#include "env_universal_common.h" #include "env_universal_common.h"
#include "halloc.h"
#include "halloc_util.h"
#include "path.h" #include "path.h"
#include "print_help.h" #include "print_help.h"
#ifndef HOST_NAME_MAX
/**
Maximum length of hostname return. It is ok if this is too short,
getting the actual hostname is not critical, so long as the string
is unique in the filesystem namespace.
*/
#define HOST_NAME_MAX 255
#endif
/** /**
Maximum length of socket filename Maximum length of socket filename
*/ */
@@ -147,7 +154,7 @@ static int quit=0;
static char *get_socket_filename() static char *get_socket_filename()
{ {
char *name; char *name;
char *dir = getenv( "FISHD_SOCKET_DIR" ); const char *dir = getenv( "FISHD_SOCKET_DIR" );
char *uname = getenv( "USER" ); char *uname = getenv( "USER" );
if( dir == NULL ) if( dir == NULL )
@@ -162,7 +169,7 @@ static char *get_socket_filename()
uname = strdup( pw->pw_name ); uname = strdup( pw->pw_name );
} }
name = malloc( strlen(dir)+ strlen(uname)+ strlen(SOCK_FILENAME) + 2 ); name = (char *)malloc( strlen(dir)+ strlen(uname)+ strlen(SOCK_FILENAME) + 2 );
if( name == NULL ) if( name == NULL )
{ {
wperror( L"get_socket_filename" ); wperror( L"get_socket_filename" );
@@ -190,6 +197,252 @@ static void handle_term( int signal )
} }
/**
Writes a pid_t in decimal representation to str.
str must contain sufficient space.
The conservatively approximate maximum number of characters a pid_t will
represent is given by: (int)(0.31 * sizeof(pid_t) + CHAR_BIT + 1)
Returns the length of the string
*/
static int sprint_pid_t( pid_t pid, char *str )
{
int len, i = 0;
int dig;
/* Store digits in reverse order into string */
while( pid != 0 )
{
dig = pid % 10;
str[i] = '0' + dig;
pid = ( pid - dig ) / 10;
i++;
}
len = i;
/* Reverse digits */
i /= 2;
while( i )
{
i--;
dig = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = dig;
}
return len;
}
/**
Writes a pseudo-random number (between one and maxlen) of pseudo-random
digits into str.
str must point to an allocated buffer of size of at least maxlen chars.
Returns the number of digits written.
Since the randomness in part depends on machine time it has _some_ extra
strength but still not enough for use in concurrent locking schemes on a
single machine because gettimeofday may not return a different value on
consecutive calls when:
a) the OS does not support fine enough resolution
b) the OS is running on an SMP machine.
Additionally, gettimeofday errors are ignored.
Excludes chars other than digits since ANSI C only guarantees that digits
are consecutive.
*/
static int sprint_rand_digits( char *str, int maxlen )
{
int i, max;
struct timeval tv;
/*
Seed the pseudo-random generator based on time - this assumes
that consecutive calls to gettimeofday will return different values
and ignores errors returned by gettimeofday.
Cast to unsigned so that wrapping occurs on overflow as per ANSI C.
*/
(void)gettimeofday( &tv, NULL );
srand( (unsigned int)tv.tv_sec + (unsigned int)tv.tv_usec * 1000000UL );
max = 1 + (maxlen - 1) * (rand() / (RAND_MAX + 1.0));
for( i = 0; i < max; i++ )
{
str[i] = '0' + 10 * (rand() / (RAND_MAX + 1.0));
}
return i;
}
/**
Generate a filename unique in an NFS namespace by creating a copy of str and
appending .{hostname}.{pid} to it. If gethostname() fails then a pseudo-
random string is substituted for {hostname} - the randomness of the string
should be strong enough across different machines. The main assumption
though is that gethostname will not fail and this is just a "safe enough"
fallback.
The memory returned should be freed using free().
*/
static char *gen_unique_nfs_filename( const char *filename )
{
int pidlen, hnlen, orglen = strlen( filename );
char hostname[HOST_NAME_MAX + 1];
char *newname;
if ( gethostname( hostname, HOST_NAME_MAX + 1 ) == 0 )
{
hnlen = strlen( hostname );
}
else
{
hnlen = sprint_rand_digits( hostname, HOST_NAME_MAX );
hostname[hnlen] = '\0';
}
newname = (char *)malloc( orglen + 1 /* period */ + hnlen + 1 /* period */ +
/* max possible pid size: 0.31 ~= log(10)2 */
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1)
+ 1 /* '\0' */ );
if ( newname == NULL )
{
debug( 1, L"gen_unique_nfs_filename: %s", strerror( errno ) );
return newname;
}
memcpy( newname, filename, orglen );
newname[orglen] = '.';
memcpy( newname + orglen + 1, hostname, hnlen );
newname[orglen + 1 + hnlen] = '.';
pidlen = sprint_pid_t( getpid(), newname + orglen + 1 + hnlen + 1 );
newname[orglen + 1 + hnlen + 1 + pidlen] = '\0';
/* debug( 1, L"gen_unique_nfs_filename returning with: newname = \"%s\"; "
L"HOST_NAME_MAX = %d; hnlen = %d; orglen = %d; "
L"sizeof(pid_t) = %d; maxpiddigits = %d; malloc'd size: %d",
newname, (int)HOST_NAME_MAX, hnlen, orglen,
(int)sizeof(pid_t),
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1),
(int)(orglen + 1 + hnlen + 1 +
(int)(0.31 * sizeof(pid_t) * CHAR_BIT + 1) + 1) ); */
return newname;
}
/**
The number of milliseconds to wait between polls when attempting to acquire
a lockfile
*/
#define LOCKPOLLINTERVAL 10
/**
Attempt to acquire a lock based on a lockfile, waiting LOCKPOLLINTERVAL
milliseconds between polls and timing out after timeout seconds,
thereafter forcibly attempting to obtain the lock if force is non-zero.
Returns 1 on success, 0 on failure.
To release the lock the lockfile must be unlinked.
A unique temporary file named by appending characters to the lockfile name
is used; any pre-existing file of the same name is subject to deletion.
*/
static int acquire_lock_file( const char *lockfile, const int timeout, int force )
{
int fd, timed_out = 0;
int ret = 0; /* early exit returns failure */
struct timespec pollint;
struct timeval start, end;
double elapsed;
struct stat statbuf;
/*
(Re)create a unique file and check that it has one only link.
*/
char *linkfile = gen_unique_nfs_filename( lockfile );
if( linkfile == NULL )
{
goto done;
}
(void)unlink( linkfile );
/* OK to not use CLO_EXEC here because fishd is single threaded */
if( ( fd = open( linkfile, O_CREAT|O_RDONLY, 0600 ) ) == -1 )
{
debug( 1, L"acquire_lock_file: open: %s", strerror( errno ) );
goto done;
}
/*
Don't need to check exit status of close on read-only file descriptors
*/
close( fd );
if( stat( linkfile, &statbuf ) != 0 )
{
debug( 1, L"acquire_lock_file: stat: %s", strerror( errno ) );
goto done;
}
if ( statbuf.st_nlink != 1 )
{
debug( 1, L"acquire_lock_file: number of hardlinks on unique "
L"tmpfile is %d instead of 1.", (int)statbuf.st_nlink );
goto done;
}
if( gettimeofday( &start, NULL ) != 0 )
{
debug( 1, L"acquire_lock_file: gettimeofday: %s", strerror( errno ) );
goto done;
}
end = start;
pollint.tv_sec = 0;
pollint.tv_nsec = LOCKPOLLINTERVAL * 1000000;
do
{
/*
Try to create a hard link to the unique file from the
lockfile. This will only succeed if the lockfile does not
already exist. It is guaranteed to provide race-free
semantics over NFS which the alternative of calling
open(O_EXCL|O_CREAT) on the lockfile is not. The lock
succeeds if the call to link returns 0 or the link count on
the unique file increases to 2.
*/
if( link( linkfile, lockfile ) == 0 ||
( stat( linkfile, &statbuf ) == 0 &&
statbuf.st_nlink == 2 ) )
{
/* Successful lock */
ret = 1;
break;
}
elapsed = end.tv_sec + end.tv_usec/1000000.0 -
( start.tv_sec + start.tv_usec/1000000.0 );
/*
The check for elapsed < 0 is to deal with the unlikely event
that after the loop is entered the system time is set forward
past the loop's end time. This would otherwise result in a
(practically) infinite loop.
*/
if( timed_out || elapsed >= timeout || elapsed < 0 )
{
if ( timed_out == 0 && force )
{
/*
Timed out and force was specified - attempt to
remove stale lock and try a final time
*/
(void)unlink( lockfile );
timed_out = 1;
continue;
}
else
{
/*
Timed out and final try was unsuccessful or
force was not specified
*/
debug( 1, L"acquire_lock_file: timed out "
L"trying to obtain lockfile %s using "
L"linkfile %s", lockfile, linkfile );
break;
}
}
nanosleep( &pollint, NULL );
} while( gettimeofday( &end, NULL ) == 0 );
done:
/* The linkfile is not needed once the lockfile has been created */
(void)unlink( linkfile );
free( linkfile );
return ret;
}
/** /**
Acquire the lock for the socket Acquire the lock for the socket
Returns the name of the lock file if successful or Returns the name of the lock file if successful or
@@ -200,7 +453,7 @@ static void handle_term( int signal )
static char *acquire_socket_lock( const char *sock_name ) static char *acquire_socket_lock( const char *sock_name )
{ {
int len = strlen( sock_name ); int len = strlen( sock_name );
char *lockfile = malloc( len + strlen( LOCKPOSTFIX ) + 1 ); char *lockfile = (char *)malloc( len + strlen( LOCKPOSTFIX ) + 1 );
if( lockfile == NULL ) if( lockfile == NULL )
{ {
@@ -322,7 +575,7 @@ static void broadcast( int type, const wchar_t *key, const wchar_t *val )
for( c = conn; c; c=c->next ) for( c = conn; c; c=c->next )
{ {
msg->count++; msg->count++;
q_put( &c->unsent, msg ); c->unsent->push(msg);
} }
for( c = conn; c; c=c->next ) for( c = conn; c; c=c->next )
@@ -390,7 +643,7 @@ static void daemonize()
/** /**
Get environment variable value. The resulting string needs to be free'd. Get environment variable value. The resulting string needs to be free'd.
*/ */
static wchar_t *fishd_env_get( wchar_t *key ) static wchar_t *fishd_env_get( const wchar_t *key )
{ {
char *nres, *nkey; char *nres, *nkey;
wchar_t *res; wchar_t *res;
@@ -407,8 +660,7 @@ static wchar_t *fishd_env_get( wchar_t *key )
res = env_universal_common_get( key ); res = env_universal_common_get( key );
if( res ) if( res )
res = wcsdup( res ); res = wcsdup( res );
return res;
return env_universal_common_get( key );
} }
} }
@@ -418,54 +670,45 @@ static wchar_t *fishd_env_get( wchar_t *key )
to be rewritten to avoid dragging in additional library to be rewritten to avoid dragging in additional library
dependencies. dependencies.
*/ */
static wchar_t *fishd_get_config() static wcstring fishd_get_config()
{ {
wchar_t *xdg_dir, *home; wchar_t *xdg_dir, *home;
int done = 0; bool done = false;
wchar_t *res = 0; wcstring result;
xdg_dir = fishd_env_get( L"XDG_CONFIG_HOME" ); xdg_dir = fishd_env_get( L"XDG_CONFIG_HOME" );
if( xdg_dir ) if (xdg_dir)
{ {
res = wcsdupcat( xdg_dir, L"/fish" ); result = xdg_dir;
if( !create_directory( res ) ) append_path_component(result, L"/fish");
if (!create_directory(result))
{ {
done = 1; done = true;
} }
else free(xdg_dir);
{
free( res );
}
free( xdg_dir );
} }
else else
{ {
home = fishd_env_get( L"HOME" ); home = fishd_env_get( L"HOME" );
if( home ) if( home )
{ {
res = wcsdupcat( home, L"/.config/fish" ); result = home;
if( !create_directory( res ) ) append_path_component(result, L"/.config/fish");
if (!create_directory(result))
{ {
done = 1; done = 1;
} }
else
{
free( res );
}
free( home ); free( home );
} }
} }
if( done ) if (! done) {
{ /* Bad juju */
return res;
}
else
{
debug( 0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access." )); debug( 0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access." ));
return 0; result.clear();
} }
return result;
} }
/** /**
@@ -473,40 +716,30 @@ static wchar_t *fishd_get_config()
*/ */
static void load_or_save( int save) static void load_or_save( int save)
{ {
char *name; const wcstring wdir = fishd_get_config();
wchar_t *wdir = fishd_get_config();
char *dir;
char hostname[HOSTNAME_LEN]; char hostname[HOSTNAME_LEN];
connection_t c; connection_t c;
int fd; int fd;
if( !wdir ) if (wdir.empty())
{
return; return;
}
dir = wcs2str( wdir ); std::string dir = wcs2string( wdir );
free( wdir );
gethostname( hostname, HOSTNAME_LEN ); gethostname( hostname, HOSTNAME_LEN );
name = malloc( strlen(dir)+ strlen(FILE)+ strlen(hostname) + 2 ); std::string name;
strcpy( name, dir ); name.append(dir);
strcat( name, "/" ); name.append("/");
strcat( name, FILE ); name.append(FILE);
strcat( name, hostname ); name.append(hostname);
free( dir );
debug( 4, L"Open file for %s: '%s'", debug( 4, L"Open file for %s: '%s'",
save?"saving":"loading", save?"saving":"loading",
name ); name.c_str() );
fd = open( name, save?(O_CREAT | O_TRUNC | O_WRONLY):O_RDONLY, 0600); /* OK to not use CLO_EXEC here because fishd is single threaded */
fd = open(name.c_str(), save?(O_CREAT | O_TRUNC | O_WRONLY):O_RDONLY, 0600);
free( name );
if( fd == -1 ) if( fd == -1 )
{ {
@@ -572,7 +805,8 @@ int main( int argc, char ** argv )
fd_set read_fd, write_fd; fd_set read_fd, write_fd;
halloc_util_init(); set_main_thread();
setup_fork_guards();
program_name=L"fishd"; program_name=L"fishd";
wsetlocale( LC_ALL, L"" ); wsetlocale( LC_ALL, L"" );
@@ -646,7 +880,7 @@ int main( int argc, char ** argv )
FD_SET( c->fd, &read_fd ); FD_SET( c->fd, &read_fd );
max_fd = maxi( max_fd, c->fd+1); max_fd = maxi( max_fd, c->fd+1);
if( ! q_empty( &c->unsent ) ) if( ! c->unsent->empty() )
{ {
FD_SET( c->fd, &write_fd ); FD_SET( c->fd, &write_fd );
} }
@@ -692,12 +926,12 @@ int main( int argc, char ** argv )
} }
else else
{ {
connection_t *new = malloc( sizeof(connection_t)); connection_t *newc = (connection_t *)malloc( sizeof(connection_t));
connection_init( new, child_socket ); connection_init( newc, child_socket );
new->next = conn; newc->next = conn;
send( new->fd, GREETING, strlen(GREETING), MSG_DONTWAIT ); send( newc->fd, GREETING, strlen(GREETING), MSG_DONTWAIT );
enqueue_all( new ); enqueue_all( newc );
conn=new; conn=newc;
} }
} }
} }
@@ -738,9 +972,10 @@ int main( int argc, char ** argv )
{ {
debug( 4, L"Close connection %d", c->fd ); debug( 4, L"Close connection %d", c->fd );
while( !q_empty( &c->unsent ) ) while( ! c->unsent->empty() )
{ {
message_t *msg = (message_t *)q_get( &c->unsent ); message_t *msg = c->unsent->front();
c->unsent->pop();
msg->count--; msg->count--;
if( !msg->count ) if( !msg->count )
free( msg ); free( msg );
@@ -777,6 +1012,5 @@ int main( int argc, char ** argv )
} }
} }
halloc_util_destroy();
} }

View File

@@ -1,467 +0,0 @@
/** \file function.c
Prototypes for functions for storing and retrieving function
information. These functions also take care of autoloading
functions in the $fish_function_path. Actual function evaluation
is taken care of by the parser and to some degree the builtin
handling library.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include "wutil.h"
#include "fallback.h"
#include "util.h"
#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "intern.h"
#include "event.h"
#include "reader.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "env.h"
#include "expand.h"
#include "halloc.h"
#include "halloc_util.h"
/**
Struct describing a function
*/
typedef struct
{
/** Function definition */
wchar_t *definition;
/** Function description */
wchar_t *description;
/**
File where this function was defined
*/
const wchar_t *definition_file;
/**
Line where definition started
*/
int definition_offset;
/**
List of all named arguments for this function
*/
array_list_t *named_arguments;
/**
Flag for specifying that this function was automatically loaded
*/
int is_autoload;
/**
Set to non-zero if invoking this function shadows the variables
of the underlying function.
*/
int shadows;
}
function_internal_data_t;
/**
Table containing all functions
*/
static hash_table_t function;
/**
Kludgy flag set by the load function in order to tell function_add
that the function being defined is autoloaded. There should be a
better way to do this...
*/
static int is_autoload = 0;
/**
Make sure that if the specified function is a dynamically loaded
function, it has been fully loaded.
*/
static int load( const wchar_t *name )
{
int was_autoload = is_autoload;
int res;
function_internal_data_t *data;
data = (function_internal_data_t *)hash_get( &function, name );
if( data && !data->is_autoload )
return 0;
is_autoload = 1;
res = parse_util_load( name,
L"fish_function_path",
&function_remove,
1 );
is_autoload = was_autoload;
return res;
}
/**
Insert a list of all dynamically loaded functions into the
specified list.
*/
static void autoload_names( array_list_t *out, int get_hidden )
{
int i;
array_list_t path_list;
const wchar_t *path_var = env_get( L"fish_function_path" );
if( ! path_var )
return;
al_init( &path_list );
tokenize_variable_array( path_var, &path_list );
for( i=0; i<al_get_count( &path_list ); i++ )
{
wchar_t *ndir = (wchar_t *)al_get( &path_list, i );
DIR *dir = wopendir( ndir );
if( !dir )
continue;
struct wdirent *next;
while( (next=wreaddir(dir))!=0 )
{
wchar_t *fn = next->d_name;
wchar_t *suffix;
if( !get_hidden && fn[0] == L'_' )
continue;
suffix = wcsrchr( fn, L'.' );
if( suffix && (wcscmp( suffix, L".fish" ) == 0 ) )
{
const wchar_t *dup;
*suffix = 0;
dup = intern( fn );
if( !dup )
DIE_MEM();
al_push( out, dup );
}
}
closedir(dir);
}
al_foreach( &path_list, &free );
al_destroy( &path_list );
}
void function_init()
{
hash_init( &function,
&hash_wcs_func,
&hash_wcs_cmp );
}
/**
Clear specified value, but not key
*/
static void clear_entry( void *key, void *value )
{
halloc_free( value );
}
void function_destroy()
{
hash_foreach( &function, &clear_entry );
hash_destroy( &function );
}
void function_add( function_data_t *data )
{
int i;
wchar_t *cmd_end;
function_internal_data_t *d;
CHECK( data->name, );
CHECK( data->definition, );
function_remove( data->name );
d = halloc( 0, sizeof( function_internal_data_t ) );
d->definition_offset = parse_util_lineno( parser_get_buffer(), current_block->tok_pos )-1;
d->definition = halloc_wcsdup( d, data->definition );
if( data->named_arguments )
{
d->named_arguments = al_halloc( d );
for( i=0; i<al_get_count( data->named_arguments ); i++ )
{
al_push( d->named_arguments, halloc_wcsdup( d, (wchar_t *)al_get( data->named_arguments, i ) ) );
}
}
cmd_end = d->definition + wcslen(d->definition)-1;
d->description = data->description?halloc_wcsdup( d, data->description ):0;
d->definition_file = intern(reader_current_filename());
d->is_autoload = is_autoload;
d->shadows = data->shadows;
hash_put( &function, intern(data->name), d );
for( i=0; i<al_get_count( data->events ); i++ )
{
event_add_handler( (event_t *)al_get( data->events, i ) );
}
}
int function_copy( const wchar_t *name, const wchar_t *new_name )
{
int i;
function_internal_data_t *d, *orig_d;
CHECK( name, 0 );
CHECK( new_name, 0 );
orig_d = (function_internal_data_t *)hash_get(&function, name);
if( !orig_d )
return 0;
d = halloc(0, sizeof( function_internal_data_t ) );
d->definition_offset = orig_d->definition_offset;
d->definition = halloc_wcsdup( d, orig_d->definition );
if( orig_d->named_arguments )
{
d->named_arguments = al_halloc( d );
for( i=0; i<al_get_count( orig_d->named_arguments ); i++ )
{
al_push( d->named_arguments, halloc_wcsdup( d, (wchar_t *)al_get( orig_d->named_arguments, i ) ) );
}
d->description = orig_d->description?halloc_wcsdup(d, orig_d->description):0;
d->shadows = orig_d->shadows;
// This new instance of the function shouldn't be tied to the def
// file of the original.
d->definition_file = 0;
d->is_autoload = 0;
}
hash_put( &function, intern(new_name), d );
return 1;
}
int function_exists( const wchar_t *cmd )
{
CHECK( cmd, 0 );
if( parser_keywords_is_reserved(cmd) )
return 0;
load( cmd );
return (hash_get(&function, cmd) != 0 );
}
void function_remove( const wchar_t *name )
{
void *key;
void *dv;
function_internal_data_t *d;
event_t ev;
CHECK( name, );
hash_remove( &function,
name,
&key,
&dv );
d=(function_internal_data_t *)dv;
if( !key )
return;
ev.type=EVENT_ANY;
ev.function_name=name;
event_remove( &ev );
halloc_free( d );
/*
Notify the autoloader that the specified function is erased, but
only if this call to fish_remove is not made by the autoloader
itself.
*/
if( !is_autoload )
{
parse_util_unload( name, L"fish_function_path", 0 );
}
}
const wchar_t *function_get_definition( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, 0 );
load( name );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return 0;
return data->definition;
}
array_list_t *function_get_named_arguments( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, 0 );
load( name );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return 0;
return data->named_arguments;
}
int function_get_shadows( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, 0 );
load( name );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return 0;
return data->shadows;
}
const wchar_t *function_get_desc( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, 0 );
load( name );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return 0;
return _(data->description);
}
void function_set_desc( const wchar_t *name, const wchar_t *desc )
{
function_internal_data_t *data;
CHECK( name, );
CHECK( desc, );
load( name );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return;
data->description = halloc_wcsdup( data, desc );
}
/**
Search arraylist of strings for specified string
*/
static int al_contains_str( array_list_t *list, const wchar_t * str )
{
int i;
CHECK( list, 0 );
CHECK( str, 0 );
for( i=0; i<al_get_count( list ); i++ )
{
if( wcscmp( al_get( list, i ), str) == 0 )
{
return 1;
}
}
return 0;
}
/**
Helper function for removing hidden functions
*/
static void get_names_internal( void *key,
void *val,
void *aux )
{
wchar_t *name = (wchar_t *)key;
if( name[0] != L'_' && !al_contains_str( (array_list_t *)aux, name ) )
{
al_push( (array_list_t *)aux, name );
}
}
/**
Helper function for removing hidden functions
*/
static void get_names_internal_all( void *key,
void *val,
void *aux )
{
wchar_t *name = (wchar_t *)key;
if( !al_contains_str( (array_list_t *)aux, name ) )
{
al_push( (array_list_t *)aux, name );
}
}
void function_get_names( array_list_t *list, int get_hidden )
{
CHECK( list, );
autoload_names( list, get_hidden );
if( get_hidden )
{
hash_foreach2( &function, &get_names_internal_all, list );
}
else
{
hash_foreach2( &function, &get_names_internal, list );
}
}
const wchar_t *function_get_definition_file( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, 0 );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return 0;
return data->definition_file;
}
int function_get_definition_offset( const wchar_t *name )
{
function_internal_data_t *data;
CHECK( name, -1 );
data = (function_internal_data_t *)hash_get( &function, name );
if( data == 0 )
return -1;
return data->definition_offset;
}

350
function.cpp Normal file
View File

@@ -0,0 +1,350 @@
/** \file function.c
Prototypes for functions for storing and retrieving function
information. These functions also take care of autoloading
functions in the $fish_function_path. Actual function evaluation
is taken care of by the parser and to some degree the builtin
handling library.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <map>
#include <set>
#include "wutil.h"
#include "fallback.h"
#include "util.h"
#include "function.h"
#include "proc.h"
#include "parser.h"
#include "common.h"
#include "intern.h"
#include "event.h"
#include "reader.h"
#include "parse_util.h"
#include "parser_keywords.h"
#include "env.h"
#include "expand.h"
#include "builtin_scripts.h"
/**
Table containing all functions
*/
typedef std::map<wcstring, function_info_t> function_map_t;
static function_map_t loaded_functions;
/* Lock for functions */
static pthread_mutex_t functions_lock;
/* Autoloader for functions */
class function_autoload_t : public autoload_t {
public:
function_autoload_t();
virtual void command_removed(const wcstring &cmd);
};
static function_autoload_t function_autoloader;
/** Constructor */
function_autoload_t::function_autoload_t() : autoload_t(L"fish_function_path",
internal_function_scripts,
sizeof internal_function_scripts / sizeof *internal_function_scripts)
{
}
/** Removes a function from our internal table, returning true if it was found and false if not */
static bool function_remove_ignore_autoload(const wcstring &name);
/** Callback when an autoloaded function is removed */
void function_autoload_t::command_removed(const wcstring &cmd) {
function_remove_ignore_autoload(cmd);
}
/* Helper macro for vomiting */
#define VOMIT_ON_FAILURE(a) do { if (0 != (a)) { int err = errno; fprintf(stderr, "%s failed on line %d in file %s: %d (%s)\n", #a, __LINE__, __FILE__, err, strerror(err)); abort(); }} while (0)
/**
Kludgy flag set by the load function in order to tell function_add
that the function being defined is autoloaded. There should be a
better way to do this...
*/
static bool is_autoload = false;
/**
Make sure that if the specified function is a dynamically loaded
function, it has been fully loaded.
*/
static int load( const wcstring &name )
{
ASSERT_IS_MAIN_THREAD();
scoped_lock lock(functions_lock);
bool was_autoload = is_autoload;
int res;
function_map_t::iterator iter = loaded_functions.find(name);
if( iter != loaded_functions.end() && !iter->second.is_autoload ) {
/* We have a non-autoload version already */
return 0;
}
is_autoload = true;
res = function_autoloader.load( name, true );
is_autoload = was_autoload;
return res;
}
/**
Insert a list of all dynamically loaded functions into the
specified list.
*/
static void autoload_names( std::set<wcstring> &names, int get_hidden )
{
size_t i;
const env_var_t path_var_wstr = env_get_string( L"fish_function_path" );
if (path_var_wstr.missing())
return;
const wchar_t *path_var = path_var_wstr.c_str();
wcstring_list_t path_list;
tokenize_variable_array( path_var, path_list );
for( i=0; i<path_list.size(); i++ )
{
const wcstring &ndir_str = path_list.at(i);
const wchar_t *ndir = (wchar_t *)ndir_str.c_str();
DIR *dir = wopendir( ndir );
if( !dir )
continue;
wcstring name;
while (wreaddir(dir, name))
{
const wchar_t *fn = name.c_str();
const wchar_t *suffix;
if( !get_hidden && fn[0] == L'_' )
continue;
suffix = wcsrchr( fn, L'.' );
if( suffix && (wcscmp( suffix, L".fish" ) == 0 ) )
{
wcstring name(fn, suffix - fn);
names.insert(name);
}
}
closedir(dir);
}
}
void function_init()
{
/* PCA: This recursive lock was introduced early in my work. I would like to make this a non-recursive lock but I haven't fully investigated all the call paths (for autoloading functions, etc.) */
pthread_mutexattr_t a;
VOMIT_ON_FAILURE(pthread_mutexattr_init(&a));
VOMIT_ON_FAILURE(pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE));
VOMIT_ON_FAILURE(pthread_mutex_init(&functions_lock, &a));
VOMIT_ON_FAILURE(pthread_mutexattr_destroy(&a));
}
function_info_t::function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload) :
definition(data.definition),
description(data.description),
definition_file(intern(filename)),
definition_offset(def_offset),
named_arguments(data.named_arguments),
is_autoload(autoload),
shadows(data.shadows)
{
}
function_info_t::function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload) :
definition(data.definition),
description(data.description),
definition_file(intern(filename)),
definition_offset(def_offset),
named_arguments(data.named_arguments),
is_autoload(autoload),
shadows(data.shadows)
{
}
void function_add( const function_data_t &data, const parser_t &parser )
{
ASSERT_IS_MAIN_THREAD();
CHECK( ! data.name.empty(), );
CHECK( data.definition, );
scoped_lock lock(functions_lock);
/* Remove the old function */
function_remove( data.name );
/* Create and store a new function */
const wchar_t *filename = reader_current_filename();
int def_offset = parse_util_lineno( parser.get_buffer(), parser.current_block->tok_pos )-1;
const function_map_t::value_type new_pair(data.name, function_info_t(data, filename, def_offset, is_autoload));
loaded_functions.insert(new_pair);
/* Add event handlers */
for( std::vector<event_t>::const_iterator iter = data.events.begin(); iter != data.events.end(); ++iter )
{
event_add_handler( &*iter );
}
}
int function_exists( const wcstring &cmd )
{
if( parser_keywords_is_reserved(cmd) )
return 0;
scoped_lock lock(functions_lock);
load(cmd);
return loaded_functions.find(cmd) != loaded_functions.end();
}
int function_exists_no_autoload( const wcstring &cmd, const env_vars &vars )
{
if( parser_keywords_is_reserved(cmd) )
return 0;
scoped_lock lock(functions_lock);
return loaded_functions.find(cmd) != loaded_functions.end() || function_autoloader.can_load(cmd, vars);
}
static bool function_remove_ignore_autoload(const wcstring &name)
{
scoped_lock lock(functions_lock);
bool erased = (loaded_functions.erase(name) > 0);
if (erased) {
event_t ev(EVENT_ANY);
ev.function_name=name;
event_remove( &ev );
}
return erased;
}
void function_remove( const wcstring &name )
{
if (function_remove_ignore_autoload(name))
function_autoloader.unload( name );
}
static const function_info_t *function_get(const wcstring &name)
{
// The caller must lock the functions_lock before calling this; however our mutex is currently recursive, so trylock will never fail
// We need a way to correctly check if a lock is locked (or better yet, make our lock non-recursive)
//ASSERT_IS_LOCKED(functions_lock);
function_map_t::iterator iter = loaded_functions.find(name);
if (iter == loaded_functions.end()) {
return NULL;
} else {
return &iter->second;
}
}
bool function_get_definition(const wcstring &name, wcstring *out_definition)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
if (func && out_definition) {
out_definition->assign(func->definition);
}
return func != NULL;
}
wcstring_list_t function_get_named_arguments(const wcstring &name)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->named_arguments : wcstring_list_t();
}
int function_get_shadows(const wcstring &name)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->shadows : false;
}
bool function_get_desc(const wcstring &name, wcstring *out_desc)
{
/* Empty length string goes to NULL */
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
if (out_desc && func && ! func->description.empty()) {
out_desc->assign(_(func->description.c_str()));
return true;
} else {
return false;
}
}
void function_set_desc(const wcstring &name, const wcstring &desc)
{
load(name);
scoped_lock lock(functions_lock);
function_map_t::iterator iter = loaded_functions.find(name);
if (iter != loaded_functions.end()) {
iter->second.description = desc;
}
}
bool function_copy(const wcstring &name, const wcstring &new_name)
{
bool result = false;
scoped_lock lock(functions_lock);
function_map_t::const_iterator iter = loaded_functions.find(name);
if (iter != loaded_functions.end()) {
// This new instance of the function shouldn't be tied to the definition file of the original, so pass NULL filename, etc.
const function_map_t::value_type new_pair(new_name, function_info_t(iter->second, NULL, 0, false));
loaded_functions.insert(new_pair);
result = true;
}
return result;
}
wcstring_list_t function_get_names(int get_hidden)
{
std::set<wcstring> names;
scoped_lock lock(functions_lock);
autoload_names(names, get_hidden);
function_map_t::const_iterator iter;
for (iter = loaded_functions.begin(); iter != loaded_functions.end(); ++iter) {
const wcstring &name = iter->first;
/* Maybe skip hidden */
if (! get_hidden) {
if (name.empty() || name.at(0) == L'_') continue;
}
names.insert(name);
}
return wcstring_list_t(names.begin(), names.end());
}
const wchar_t *function_get_definition_file(const wcstring &name)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->definition_file : NULL;
}
int function_get_definition_offset(const wcstring &name)
{
scoped_lock lock(functions_lock);
const function_info_t *func = function_get(name);
return func ? func->definition_offset : -1;
}

View File

@@ -13,6 +13,11 @@
#include <wchar.h> #include <wchar.h>
#include "util.h" #include "util.h"
#include "common.h"
#include "event.h"
class parser_t;
class env_vars;
/** /**
Structure describing a function. This is used by the parser to Structure describing a function. This is used by the parser to
@@ -21,16 +26,16 @@
structure is used for that purpose. Parhaps these two should be structure is used for that purpose. Parhaps these two should be
merged. merged.
*/ */
typedef struct function_data struct function_data_t
{ {
/** /**
Name of function Name of function
*/ */
wchar_t *name; wcstring name;
/** /**
Description of function Description of function
*/ */
wchar_t *description; wcstring description;
/** /**
Function definition Function definition
*/ */
@@ -38,18 +43,47 @@ typedef struct function_data
/** /**
List of all event handlers for this function List of all event handlers for this function
*/ */
array_list_t *events; std::vector<event_t> events;
/** /**
List of all named arguments for this function List of all named arguments for this function
*/ */
array_list_t *named_arguments; wcstring_list_t named_arguments;
/** /**
Set to non-zero if invoking this function shadows the variables Set to non-zero if invoking this function shadows the variables
of the underlying function. of the underlying function.
*/ */
int shadows; int shadows;
} };
function_data_t;
class function_info_t {
public:
/** Constructs relevant information from the function_data */
function_info_t(const function_data_t &data, const wchar_t *filename, int def_offset, bool autoload);
/** Used by function_copy */
function_info_t(const function_info_t &data, const wchar_t *filename, int def_offset, bool autoload);
/** Function definition */
const wcstring definition;
/** Function description. Only the description may be changed after the function is created. */
wcstring description;
/** File where this function was defined (intern'd string) */
const wchar_t * const definition_file;
/** Line where definition started */
const int definition_offset;
/** List of all named arguments for this function */
const wcstring_list_t named_arguments;
/** Flag for specifying that this function was automatically loaded */
const bool is_autoload;
/** Set to true if invoking this function shadows the variables of the underlying function. */
const bool shadows;
};
/** /**
@@ -57,51 +91,47 @@ typedef struct function_data
*/ */
void function_init(); void function_init();
/** /** Add a function. */
Destroy function data void function_add( const function_data_t &data, const parser_t &parser );
*/
void function_destroy();
/**
Add an function. The parameters values are copied and should be
freed by the caller.
*/
void function_add( function_data_t *data );
/** /**
Remove the function with the specified name. Remove the function with the specified name.
*/ */
void function_remove( const wchar_t *name ); void function_remove( const wcstring &name );
/** /**
Returns the definition of the function with the name \c name. Returns by reference the definition of the function with the name \c name.
Returns true if successful, false if no function with the given name exists.
*/ */
const wchar_t *function_get_definition( const wchar_t *name ); bool function_get_definition( const wcstring &name, wcstring *out_definition );
/** /**
Returns the description of the function with the name \c name. Returns by reference the description of the function with the name \c name.
Returns true if the function exists and has a nonempty description, false if it does not.
*/ */
const wchar_t *function_get_desc( const wchar_t *name ); bool function_get_desc( const wcstring &name, wcstring *out_desc );
/** /**
Sets the description of the function with the name \c name. Sets the description of the function with the name \c name.
*/ */
void function_set_desc( const wchar_t *name, const wchar_t *desc ); void function_set_desc( const wcstring &name, const wcstring &desc );
/** /**
Returns true if the function with the name name exists. Returns true if the function with the name name exists.
*/ */
int function_exists( const wchar_t *name ); int function_exists( const wcstring &name );
/** /**
Insert all function names into l. These are not copies of the Returns true if the function with the name name exists, without triggering autoload.
strings and should not be freed after use. */
int function_exists_no_autoload( const wcstring &name, const env_vars &vars );
/**
Returns all function names.
\param list the list to add the names to
\param get_hidden whether to include hidden functions, i.e. ones starting with an underscore \param get_hidden whether to include hidden functions, i.e. ones starting with an underscore
*/ */
void function_get_names( array_list_t *list, wcstring_list_t function_get_names( int get_hidden );
int get_hidden );
/** /**
Returns tha absolute path of the file where the specified function Returns tha absolute path of the file where the specified function
@@ -109,8 +139,10 @@ void function_get_names( array_list_t *list,
This function does not autoload functions, it will only work on This function does not autoload functions, it will only work on
functions that have already been defined. functions that have already been defined.
This returns an intern'd string.
*/ */
const wchar_t *function_get_definition_file( const wchar_t *name ); const wchar_t *function_get_definition_file( const wcstring &name );
/** /**
Returns the linenumber where the definition of the specified Returns the linenumber where the definition of the specified
@@ -119,22 +151,23 @@ const wchar_t *function_get_definition_file( const wchar_t *name );
This function does not autoload functions, it will only work on This function does not autoload functions, it will only work on
functions that have already been defined. functions that have already been defined.
*/ */
int function_get_definition_offset( const wchar_t *name ); int function_get_definition_offset( const wcstring &name );
/** /**
Returns a list of all named arguments of the specified function. Returns a list of all named arguments of the specified function.
*/ */
array_list_t *function_get_named_arguments( const wchar_t *name ); wcstring_list_t function_get_named_arguments( const wcstring &name );
/**
Creates a new function using the same definition as the specified function.
Returns true if copy is successful.
*/
bool function_copy( const wcstring &name, const wcstring &new_name );
/** /**
Returns whether this function shadows variables of the underlying function Returns whether this function shadows variables of the underlying function
*/ */
int function_get_shadows( const wchar_t *name ); int function_get_shadows( const wcstring &name );
/**
Creates a new function using the same definition as the specified function.
Returns non-zero if copy is successful.
*/
int function_copy( const wchar_t *name, const wchar_t *new_name );
#endif #endif

264
halloc.c
View File

@@ -1,264 +0,0 @@
/** \file halloc.c
A hierarchical memory allocation system. Works just like talloc
used in Samba, except that an arbitrary block allocated with
malloc() can be registered to be freed by halloc_free.
*/
#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "halloc.h"
/**
Extra size to allocate whenever doing a halloc, in order to fill uyp smaller halloc calls
*/
#define HALLOC_BLOCK_SIZE 128
/**
Maximum size of trailing halloc space to refuse to discard. This is
set to be larger on 64-bit platforms, since we don't want to get
'stuck' with an unusably small slice of memory, and what is
unusably small often depends on pointer size.
*/
#define HALLOC_SCRAP_SIZE (4*sizeof(void *))
#ifdef HALLOC_DEBUG
/**
Debug statistic parameter
*/
static int child_count=0;
/**
Debug statistic parameter
*/
static int child_size=0;
/**
Debug statistic parameter
*/
static int alloc_count =0;
/**
Debug statistic parameter
*/
static int alloc_spill = 0;
/**
Debug statistic parameter
*/
static pid_t pid=0;
/**
Debug statistic parameter
*/
static int parent_count=0;
#endif
/**
The main datastructure for a main halloc context
*/
typedef struct halloc
{
/**
List of all addresses and functions to call on them
*/
array_list_t children;
/**
Memory scratch area used to fullfil smaller memory allocations
*/
char *scratch;
/**
Amount of free space in the scratch area
*/
ssize_t scratch_free;
}
halloc_t;
/**
Allign the specified pointer
*/
static char *align_ptr( char *in )
{
unsigned long step = maxi(sizeof(double),sizeof(void *));
unsigned long inc = step-1;
unsigned long long_in = (long)in;
unsigned long long_out = ((long_in+inc)/step)*step;
return (char *)long_out;
}
/**
Allign specifies size_t
*/
static size_t align_sz( size_t in )
{
size_t step = maxi(sizeof(double),sizeof(void *));
size_t inc = step-1;
return ((in+inc)/step)*step;
}
/**
Get the offset of the halloc structure before a data block
*/
static halloc_t *halloc_from_data( void *data )
{
return (halloc_t *)(((char *)data) - align_sz(sizeof( halloc_t ) ));
}
/**
A function that does nothing
*/
static void late_free( void *data)
{
}
#ifdef HALLOC_DEBUG
/**
Debug function, called at exit when in debug mode. Prints usage
statistics, like number of allocations and number of internal calls
to malloc.
*/
static void halloc_report()
{
if( getpid() == pid )
{
debug( 1, L"%d parents, %d children with average child size of %.2f bytes caused %d allocs, average spill of %.2f bytes",
parent_count, child_count, (double)child_size/child_count,
parent_count+alloc_count, (double)alloc_spill/(parent_count+alloc_count) );
}
}
#endif
void *halloc( void *context, size_t size )
{
halloc_t *me, *parent;
if( context )
{
char *res;
char *aligned;
#ifdef HALLOC_DEBUG
if( !child_count )
{
pid = getpid();
atexit( &halloc_report );
}
child_count++;
child_size += size;
#endif
parent = halloc_from_data( context );
/*
Align memory address
*/
aligned = align_ptr( parent->scratch );
parent->scratch_free -= (aligned-parent->scratch);
if( parent->scratch_free < 0 )
parent->scratch_free=0;
parent->scratch = aligned;
if( size <= parent->scratch_free )
{
res = parent->scratch;
parent->scratch_free -= size;
parent->scratch = ((char *)parent->scratch)+size;
}
else
{
#ifdef HALLOC_DEBUG
alloc_count++;
#endif
if( parent->scratch_free < HALLOC_SCRAP_SIZE )
{
#ifdef HALLOC_DEBUG
alloc_spill += parent->scratch_free;
#endif
res = calloc( 1, size + HALLOC_BLOCK_SIZE );
if( !res )
DIE_MEM();
parent->scratch = (char *)res + size;
parent->scratch_free = HALLOC_BLOCK_SIZE;
}
else
{
res = calloc( 1, size );
if( !res )
DIE_MEM();
}
al_push_func( &parent->children, &late_free );
al_push( &parent->children, res );
}
return res;
}
else
{
me = (halloc_t *)calloc( 1, align_sz(sizeof(halloc_t)) + align_sz(size) + HALLOC_BLOCK_SIZE );
if( !me )
DIE_MEM();
#ifdef HALLOC_DEBUG
parent_count++;
#endif
me->scratch = ((char *)me) + align_sz(sizeof(halloc_t)) + align_sz(size);
me->scratch_free = HALLOC_BLOCK_SIZE;
al_init( &me->children );
return ((char *)me) + align_sz(sizeof(halloc_t));
}
}
void halloc_register_function( void *context, void (*func)(void *), void *data )
{
halloc_t *me;
if( !context )
return;
me = halloc_from_data( context );
al_push_func( &me->children, func );
al_push( &me->children, data );
}
void halloc_free( void *context )
{
halloc_t *me;
int i;
if( !context )
return;
me = halloc_from_data( context );
#ifdef HALLOC_DEBUG
alloc_spill += me->scratch_free;
#endif
for( i=0; i<al_get_count(&me->children); i+=2 )
{
void (*func)(void *) = (void (*)(void *))al_get( &me->children, i );
void * data = (void *)al_get( &me->children, i+1 );
if( func != &late_free )
func( data );
}
for( i=0; i<al_get_count(&me->children); i+=2 )
{
void (*func)(void *) = (void (*)(void *))al_get_func( &me->children, i );
void * data = (void *)al_get( &me->children, i+1 );
if( func == &late_free )
free( data );
}
al_destroy( &me->children );
free(me);
}

View File

@@ -1,49 +0,0 @@
/** \file halloc.h
A hierarchical memory allocation system. Works mostly like talloc
used in Samba, except that an arbitrary block allocated with
malloc() can be registered to be freed by halloc_free.
*/
#ifndef FISH_HALLOC_H
#define FISH_HALLOC_H
/**
Allocate new memory using specified parent memory context. Context
_must_ be either 0 or the result of a previous call to halloc. The
resulting memory is set to zero.
If \c context is null, the resulting block is a root block, and
must be freed with a call to halloc_free().
If \c context is not null, context must be a halloc root block. the
resulting memory block is a child context, and must never be
explicitly freed, it will be automatically freed whenever the
parent context is freed. Child blocks can also never be used as the
context in calls to halloc_register_function, halloc_free, etc.
*/
void *halloc( void *context, size_t size );
/**
Make the specified function run whenever context is free'd, using data as argument.
\c context a halloc root block
*/
void halloc_register_function( void *context, void (*func)(void *), void *data );
/**
Free memory context and all children contexts. Only root contexts
may be freed explicitly.
All functions registered with halloc_register_function are run in
the order they where added. Afterwards, all memory allocated using
halloc itself is free'd.
\c context a halloc root block
*/
void halloc_free( void *context );
#endif

View File

@@ -1,100 +0,0 @@
/** \file halloc_util.c
A hierarchical memory allocation system. Works just like talloc
used in Samba, except that an arbitrary block allocated with
malloc() can be registered to be freed by halloc_free.
*/
#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "fallback.h"
#include "util.h"
#include "common.h"
#include "halloc.h"
void *global_context=0;
void halloc_util_init()
{
global_context = halloc( 0, 0 );
}
void halloc_util_destroy()
{
halloc_free( global_context );
}
array_list_t *al_halloc( void *context )
{
array_list_t *res = halloc( context, sizeof( array_list_t ) );
if( !res )
DIE_MEM();
al_init( res );
halloc_register_function( context?context:res, (void (*)(void *)) &al_destroy, res );
return res;
}
string_buffer_t *sb_halloc( void *context )
{
string_buffer_t *res = halloc( context, sizeof( string_buffer_t ) );
if( !res )
DIE_MEM();
sb_init( res );
halloc_register_function( context?context:res, (void (*)(void *)) &sb_destroy, res );
return res;
}
/**
A function that takes a single parameter, which is a function pointer, and calls it.
*/
static void halloc_passthrough( void *f )
{
void (*func)() = (void (*)() )f;
func();
}
void halloc_register_function_void( void *context, void (*func)() )
{
halloc_register_function( context, &halloc_passthrough, (void *)func );
}
void *halloc_register( void *context, void *data )
{
if( !data )
return 0;
halloc_register_function( context, &free, data );
return data;
}
wchar_t *halloc_wcsdup( void *context, const wchar_t *in )
{
size_t len=wcslen(in);
wchar_t *out = halloc( context, sizeof( wchar_t)*(len+1));
if( out == 0 )
{
DIE_MEM();
}
memcpy( out, in, sizeof( wchar_t)*(len+1));
return out;
}
wchar_t *halloc_wcsndup( void * context, const wchar_t *in, int c )
{
wchar_t *res = halloc( context, sizeof(wchar_t)*(c+1) );
if( res == 0 )
{
DIE_MEM();
}
wcslcpy( res, in, c+1 );
res[c] = L'\0';
return res;
}

View File

@@ -1,66 +0,0 @@
/**
\file halloc_util.h
Various halloc-related utility functions.
*/
#ifndef FISH_HALLOC_UTIL_H
#define FISH_HALLOC_UTIL_H
/**
This pointer is a valid halloc context that will be freed right
before program shutdown. It may be used to allocate memory that
should be freed when the program shuts down.
*/
extern void *global_context;
/**
Create the global_context halloc object
*/
void halloc_util_init();
/**
Free the global_context halloc object
*/
void halloc_util_destroy();
/**
Allocate a array_list_t that will be automatically disposed of when
the specified context is free'd
*/
array_list_t *al_halloc( void *context );
/**
Allocate a string_buffer_t that will be automatically disposed of
when the specified context is free'd
*/
string_buffer_t *sb_halloc( void *context );
/**
Register the specified function to run when the specified context
is free'd. This function is related to halloc_register_function,
but the specified function dowes not take an argument.
*/
void halloc_register_function_void( void *context, void (*func)() );
/**
Free the memory pointed to by \c data when the memory pointed to by
\c context is free:d. Note that this will _not_ turn the specified
memory area into a valid halloc context. Only memory areas created
using a call to halloc( 0, size ) can be used as a context.
*/
void *halloc_register( void *context, void *data );
/**
Make a copy of the specified string using memory allocated using
halloc and the specified context
*/
wchar_t *halloc_wcsdup( void *context, const wchar_t *str );
/**
Make a copy of the specified substring using memory allocated using
halloc and the specified context
*/
wchar_t *halloc_wcsndup( void * context, const wchar_t *in, int c );
#endif

File diff suppressed because it is too large Load Diff

1430
highlight.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,10 @@
#include <wchar.h> #include <wchar.h>
#include "env.h"
#include "util.h" #include "util.h"
#include "screen.h"
#include "color.h"
/** /**
Internal value representing highlighting of normal text Internal value representing highlighting of normal text
@@ -62,6 +65,11 @@
*/ */
#define HIGHLIGHT_VALID_PATH 0x1000 #define HIGHLIGHT_VALID_PATH 0x1000
/**
Internal value representing highlighting an autosuggestion
*/
#define HIGHLIGHT_AUTOSUGGESTION 0x2000
/** /**
Perform syntax highlighting for the shell commands in buff. The result is Perform syntax highlighting for the shell commands in buff. The result is
stored in the color array as a color_code from the HIGHLIGHT_ enum stored in the color array as a color_code from the HIGHLIGHT_ enum
@@ -72,7 +80,7 @@
\param pos the cursor position. Used for quote matching, etc. \param pos the cursor position. Used for quote matching, etc.
\param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated. \param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated.
*/ */
void highlight_shell( wchar_t * buff, int *color, int pos, array_list_t *error ); void highlight_shell( const wcstring &buffstr, std::vector<int> &color, int pos, wcstring_list_t *error, const env_vars &vars );
/** /**
Perform syntax highlighting for the text in buff. Matching quotes and paranthesis are highlighted. The result is Perform syntax highlighting for the text in buff. Matching quotes and paranthesis are highlighted. The result is
@@ -84,7 +92,7 @@ void highlight_shell( wchar_t * buff, int *color, int pos, array_list_t *error )
\param pos the cursor position. Used for quote matching, etc. \param pos the cursor position. Used for quote matching, etc.
\param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated. \param error a list in which a description of each error will be inserted. May be 0, in whcich case no error descriptions will be generated.
*/ */
void highlight_universal( wchar_t * buff, int *color, int pos, array_list_t *error ); void highlight_universal( const wcstring &buffstr, std::vector<int> &color, int pos, wcstring_list_t *error, const env_vars &vars );
/** /**
Translate from HIGHLIGHT_* to FISH_COLOR_* according to environment Translate from HIGHLIGHT_* to FISH_COLOR_* according to environment
@@ -96,6 +104,22 @@ void highlight_universal( wchar_t * buff, int *color, int pos, array_list_t *err
call to highlight_get_color( HIGHLIGHT_ERROR) will return call to highlight_get_color( HIGHLIGHT_ERROR) will return
FISH_COLOR_RED. FISH_COLOR_RED.
*/ */
int highlight_get_color( int highlight ); rgb_color_t highlight_get_color( int highlight, bool is_background );
/** Given a command 'str' from the history, try to determine whether we ought to suggest it by specially recognizing the command.
Returns true if we validated the command. If so, returns by reference whether the suggestion is valid or not.
*/
bool autosuggest_special_validate_from_history(const wcstring &str, const wcstring &working_directory, bool *outSuggestionOK);
/** Given the command line contents 'str', return via reference a suggestion by specially recognizing the command. Returns true if we recognized the command (even if we couldn't think of a suggestion for it).
*/
bool autosuggest_suggest_special(const wcstring &str, const wcstring &working_directory, wcstring &outString);
/* Tests whether the specified string cpath is the prefix of anything we could cd to. directories is a list of possible parent directories (typically either the working directory, or the cdpath). This does I/O!
This is used only internally to this file, and is exposed only for testing.
*/
bool is_potential_path(const wcstring &const_path, const wcstring_list_t &directories, bool require_dir = false, wcstring *out_path = NULL);
#endif #endif

995
history.c
View File

@@ -1,995 +0,0 @@
/** \file history.c
History functions, part of the user interface.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <errno.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "history.h"
#include "common.h"
#include "halloc.h"
#include "halloc_util.h"
#include "intern.h"
#include "path.h"
#include "signal.h"
/**
Interval in seconds between automatic history save
*/
#define SAVE_INTERVAL (5*60)
/**
Number of new history entries to add before automatic history save
*/
#define SAVE_COUNT 5
/**
A struct representiong a history list
*/
typedef struct
{
/**
The name of this list. Used for picking a suitable filename and for switching modes.
*/
const wchar_t *name;
/**
The items of the list. Each entry may be either a pointer to an
item_t struct or a pointer to an mmaped memory region where a
multibyte string representing the item is stored. Use the
item_get function to always get an item_t.
*/
array_list_t item;
/**
A hash table containing all the items created by the current
session as keys. This can be used as a lookup when loading the
history list to ignore the on-file version of an entry from
this session.
*/
hash_table_t session_item;
/**
The current history position
*/
int pos;
/**
This flag is set nonzero if the file containing earlier saves has ben mmaped in
*/
int has_loaded;
/**
The mmaped region for the history file
*/
char *mmap_start;
/**
The size of the mmaped region
*/
size_t mmap_length;
/**
A list of indices of all previous search maches. This is used to eliminate duplicate search results.
*/
array_list_t used;
/**
Timestamp of last save
*/
time_t save_timestamp;
/**
Number of entries that have been added since last save
*/
int new_count;
/**
A halloc context. This context is free'd every time the history
is saved. Therefore it is very well suited for use as the
context for history item data.
*/
void *item_context;
}
history_mode_t;
/**
This struct represents a history item
*/
typedef struct
{
/**
The actual contents of the entry
*/
wchar_t *data;
/**
Original creation time for the entry
*/
time_t timestamp;
}
item_t;
/**
Table of all history modes
*/
static hash_table_t *mode_table=0;
/**
The surrent history mode
*/
static history_mode_t *current_mode=0;
/**
Hash function for item_t struct
*/
static int hash_item_func( void *v )
{
item_t *i = (item_t *)v;
return i->timestamp ^ hash_wcs_func( i->data );
}
/**
Comparison function for item_t struct
*/
static int hash_item_cmp( void *v1, void *v2 )
{
item_t *i1 = (item_t *)v1;
item_t *i2 = (item_t *)v2;
return (i1->timestamp == i2->timestamp) && (wcscmp( i1->data, i2->data )==0);
}
/**
Add backslashes to all newlines, so that the returning string is
suitable for writing to the history file. The memory for the return
value will be reused by subsequent calls to this function.
*/
static wchar_t *history_escape_newlines( wchar_t *in )
{
static string_buffer_t *out = 0;
if( !out )
{
out = sb_halloc( global_context );
if( !out )
{
DIE_MEM();
}
}
else
{
sb_clear( out );
}
for( ; *in; in++ )
{
if( *in == L'\\' )
{
sb_append_char( out, *in );
if( *(in+1) )
{
in++;
sb_append_char( out, *in );
}
else
{
/*
This is a weird special case. When we are trying to
save a string that ends with a backslash, we need to
handle it specially, otherwise this command would be
combined with the one following it. We hack around
this by adding an additional newline.
*/
sb_append_char( out, L'\n' );
}
}
else if( *in == L'\n' )
{
sb_append_char( out, L'\\' );
sb_append_char( out, *in );
}
else
{
sb_append_char( out, *in );
}
}
return (wchar_t *)out->buff;
}
/**
Remove backslashes from all newlines. This makes a string from the
history file better formated for on screen display. The memory for
the return value will be reused by subsequent calls to this
function.
*/
static wchar_t *history_unescape_newlines( wchar_t *in )
{
static string_buffer_t *out = 0;
if( !out )
{
out = sb_halloc( global_context );
if( !out )
{
DIE_MEM();
}
}
else
{
sb_clear( out );
}
for( ; *in; in++ )
{
if( *in == L'\\' )
{
if( *(in+1)!= L'\n')
{
sb_append_char( out, *in );
}
}
else
{
sb_append_char( out, *in );
}
}
return (wchar_t *)out->buff;
}
/**
Check if the specified item is already loaded
*/
static int item_is_new( history_mode_t *m, void *d )
{
char *begin = (char *)d;
if( !m->has_loaded || !m->mmap_start || (m->mmap_start == MAP_FAILED ) )
{
return 1;
}
if( (begin < m->mmap_start) || (begin > (m->mmap_start+m->mmap_length) ) )
{
return 1;
}
return 0;
}
/**
Returns an item_t for the specified adress. The adress must come from the item list of the specified mode.
Later calls to this function may erase the output of a previous call to this function.
*/
static item_t *item_get( history_mode_t *m, void *d )
{
char *begin = (char *)d;
if( item_is_new( m, d ) )
{
return (item_t *)d;
}
else
{
char *end = m->mmap_start + m->mmap_length;
char *pos=begin;
int was_backslash = 0;
static string_buffer_t *out = 0;
static item_t narrow_item;
int first_char = 1;
int timestamp_mode = 0;
narrow_item.timestamp = 0;
if( !out )
{
out = sb_halloc( global_context );
if( !out )
{
DIE_MEM();
}
}
else
{
sb_clear( out );
}
while( 1 )
{
wchar_t c;
mbstate_t state;
size_t res;
memset( &state, 0, sizeof(state) );
res = mbrtowc( &c, pos, end-pos, &state );
if( res == (size_t)-1 )
{
pos++;
continue;
}
else if( res == (size_t)-2 )
{
break;
}
else if( res == (size_t)0 )
{
pos++;
continue;
}
pos += res;
if( c == L'\n' )
{
if( timestamp_mode )
{
wchar_t *time_string = (wchar_t *)out->buff;
while( *time_string && !iswdigit(*time_string))
time_string++;
errno=0;
if( *time_string )
{
time_t tm;
wchar_t *end;
errno = 0;
tm = (time_t)wcstol( time_string, &end, 10 );
if( tm && !errno && !*end )
{
narrow_item.timestamp = tm;
}
}
sb_clear( out );
timestamp_mode = 0;
continue;
}
if( !was_backslash )
break;
}
if( first_char )
{
if( c == L'#' )
timestamp_mode = 1;
}
first_char = 0;
sb_append_char( out, c );
was_backslash = ( (c == L'\\') && !was_backslash);
}
narrow_item.data = history_unescape_newlines((wchar_t *)out->buff);
return &narrow_item;
}
}
/**
Write the specified item to the specified file.
*/
static int item_write( FILE *f, history_mode_t *m, void *v )
{
item_t *i = item_get( m, v );
return fwprintf( f, L"# %d\n%ls\n", i->timestamp, history_escape_newlines( i->data ) );
}
/**
Release all memory used by the specified history mode.
*/
static void history_destroy_mode( history_mode_t *m )
{
halloc_free( m->item_context );
if( m->mmap_start && (m->mmap_start != MAP_FAILED ))
{
munmap( m->mmap_start, m->mmap_length);
}
}
/**
Free all memory used by specified mistory mode
*/
static void history_destroy_mode_wrapper( void *n, history_mode_t *m )
{
halloc_free( m );
}
/**
Create a new empty mode with the specified name.
The mode is a halloc context, and the entire mode can be destroyed using halloc_free().
*/
static history_mode_t *history_create_mode( const wchar_t *name )
{
history_mode_t *new_mode = halloc( 0, sizeof( history_mode_t ));
new_mode->name = intern(name);
al_init( &new_mode->item );
al_init( &new_mode->used );
halloc_register_function( new_mode, (void (*)(void *))&al_destroy, &new_mode->item );
halloc_register_function( new_mode, (void (*)(void *))&al_destroy, &new_mode->used );
hash_init( &new_mode->session_item, &hash_item_func, &hash_item_cmp );
halloc_register_function( new_mode, (void (*)(void *))&hash_destroy, &new_mode->session_item );
new_mode->save_timestamp=time(0);
new_mode->item_context = halloc( 0,0 );
halloc_register_function( new_mode, (void (*)(void *))&history_destroy_mode, new_mode );
return new_mode;
}
/**
This function tests if the search string is a match for the given string
*/
static int history_test( const wchar_t *needle, const wchar_t *haystack )
{
return !!wcsstr( haystack, needle );
}
/**
Returns the name of the save file for a mode.
\param context a halloc context used to allocate memory
\param name the name of the hstory mode
\param suffix an optional file suffix
*/
static wchar_t *history_filename( void *context, const wchar_t *name, const wchar_t *suffix )
{
wchar_t *path;
wchar_t *res;
if( !current_mode )
return 0;
path = path_get_config( context );
if( !path )
return 0;
res = wcsdupcat( path, L"/", name, L"_history", suffix?suffix:(void *)0);
halloc_register_function( context, &free, res );
return res;
}
/**
Go through the mmaped region and insert pointers to suitable loacations into the item list
*/
static void history_populate_from_mmap( history_mode_t *m )
{
char *begin = m->mmap_start;
char *end = begin + m->mmap_length;
char *pos;
array_list_t old_item;
array_list_t session_item_list;
int ignore_newline = 0;
int do_push = 1;
al_init( &old_item );
al_init( &session_item_list );
al_push_all( &old_item, &m->item );
al_truncate( &m->item, 0 );
for( pos = begin; pos <end; pos++ )
{
if( do_push )
{
item_t *i;
item_t *i_orig;
ignore_newline = *pos == '#';
i = item_get( m, pos );
if( (i_orig=hash_get( &current_mode->session_item, i ) ) )
{
/*
This item comes from this session. Insert the
original item at the end of the item list.
*/
al_push( &session_item_list, i_orig );
}
else
{
/*
Old item. Insert pointer directly to the item list
*/
al_push( &m->item, pos );
}
do_push = 0;
}
switch( *pos )
{
case '\\':
{
pos++;
break;
}
case '\n':
{
if( ignore_newline )
{
ignore_newline = 0;
}
else
{
do_push = 1;
}
break;
}
}
}
al_push_all( &m->item, &session_item_list );
m->pos += al_get_count( &m->item );
al_push_all( &m->item, &old_item );
al_destroy( &session_item_list );
al_destroy( &old_item );
}
/**
Load contents of the backing file to memory
*/
static void history_load( history_mode_t *m )
{
int fd;
int ok=0;
void *context;
wchar_t *filename;
if( !m )
return;
m->has_loaded=1;
signal_block();
context = halloc( 0, 0 );
filename = history_filename( context, m->name, 0 );
if( filename )
{
if( ( fd = wopen( filename, O_RDONLY ) ) > 0 )
{
off_t len = lseek( fd, 0, SEEK_END );
if( len != (off_t)-1)
{
m->mmap_length = (size_t)len;
if( lseek( fd, 0, SEEK_SET ) == 0 )
{
if( (m->mmap_start = mmap( 0, m->mmap_length, PROT_READ, MAP_PRIVATE, fd, 0 )) != MAP_FAILED )
{
ok = 1;
history_populate_from_mmap( m );
}
}
}
close( fd );
}
}
halloc_free( context );
signal_unblock();
}
/**
Save the specified mode to file
*/
static void history_save_mode( void *n, history_mode_t *m )
{
FILE *out;
history_mode_t *on_disk;
int i;
int has_new=0;
wchar_t *tmp_name;
int ok = 1;
/*
First check if there are any new entries to save. If not, then
we can just return
*/
for( i=0; i<al_get_count(&m->item); i++ )
{
void *ptr = al_get( &m->item, i );
has_new = item_is_new( m, ptr );
if( has_new )
{
break;
}
}
if( !has_new )
{
return;
}
signal_block();
/*
Set up on_disk variable to describe the current contents of the
history file
*/
on_disk = history_create_mode( m->name );
history_load( on_disk );
tmp_name = history_filename( on_disk, m->name, L".tmp" );
if( tmp_name )
{
tmp_name = wcsdup(tmp_name );
if( (out=wfopen( tmp_name, "w" ) ) )
{
hash_table_t mine;
hash_init( &mine, &hash_item_func, &hash_item_cmp );
for( i=0; i<al_get_count(&m->item); i++ )
{
void *ptr = al_get( &m->item, i );
int is_new = item_is_new( m, ptr );
if( is_new )
{
hash_put( &mine, item_get( m, ptr ), L"" );
}
}
/*
Re-save the old history
*/
for( i=0; ok && (i<al_get_count(&on_disk->item)); i++ )
{
void *ptr = al_get( &on_disk->item, i );
item_t *i = item_get( on_disk, ptr );
if( !hash_get( &mine, i ) )
{
if( item_write( out, on_disk, ptr ) == -1 )
{
ok = 0;
break;
}
}
}
hash_destroy( &mine );
/*
Add our own items last
*/
for( i=0; ok && (i<al_get_count(&m->item)); i++ )
{
void *ptr = al_get( &m->item, i );
int is_new = item_is_new( m, ptr );
if( is_new )
{
if( item_write( out, m, ptr ) == -1 )
{
ok = 0;
}
}
}
if( fclose( out ) || !ok )
{
/*
This message does not have high enough priority to
be shown by default.
*/
debug( 2, L"Error when writing history file" );
}
else
{
wrename( tmp_name, history_filename( on_disk, m->name, 0 ) );
}
}
free( tmp_name );
}
halloc_free( on_disk);
if( ok )
{
/*
Reset the history. The item_t entries created in this session
are not lost or dropped, they are stored in the session_item
hash table. On reload, they will be automatically inserted at
the end of the history list.
*/
if( m->mmap_start && (m->mmap_start != MAP_FAILED ) )
{
munmap( m->mmap_start, m->mmap_length );
}
al_truncate( &m->item, 0 );
al_truncate( &m->used, 0 );
m->pos = 0;
m->has_loaded = 0;
m->mmap_start=0;
m->mmap_length=0;
m->save_timestamp=time(0);
m->new_count = 0;
}
signal_unblock();
}
void history_add( const wchar_t *str )
{
item_t *i;
if( !current_mode )
return;
i = halloc( current_mode->item_context, sizeof(item_t));
i->data = (wchar_t *)halloc_wcsdup( current_mode->item_context, str );
i->timestamp = time(0);
al_push( &current_mode->item, i );
hash_put( &current_mode->session_item, i, i );
al_truncate( &current_mode->used, 0 );
current_mode->pos = al_get_count( &current_mode->item );
current_mode->new_count++;
if( (time(0) > current_mode->save_timestamp+SAVE_INTERVAL) || (current_mode->new_count >= SAVE_COUNT) )
{
history_save_mode( 0, current_mode );
}
}
/**
Check if the specified string has already been used a s a search match
*/
static int history_is_used( const wchar_t *str )
{
int i;
int res =0;
item_t *it = 0;
for( i=0; i<al_get_count( &current_mode->used); i++ )
{
long idx = al_get_long( &current_mode->used, i );
it = item_get( current_mode, al_get( &current_mode->item, (int)idx ) );
if( wcscmp( it->data, str ) == 0 )
{
res = 1;
break;
}
}
return res;
}
const wchar_t *history_prev_match( const wchar_t *needle )
{
if( current_mode )
{
if( current_mode->pos > 0 )
{
for( current_mode->pos--; current_mode->pos>=0; current_mode->pos-- )
{
item_t *i = item_get( current_mode, al_get( &current_mode->item, current_mode->pos ) );
wchar_t *haystack = (wchar_t *)i->data;
if( history_test( needle, haystack ) )
{
int is_used;
/*
This is ugly. Whenever we call item_get(),
there is a chance that the return value of any
previous call to item_get will become
invalid. The history_is_used function uses the
item_get() function. Therefore, we must create
a copy of the haystack string, and if the string
is unused, we must call item_get anew.
*/
haystack = wcsdup(haystack );
is_used = history_is_used( haystack );
free( haystack );
if( !is_used )
{
i = item_get( current_mode, al_get( &current_mode->item, current_mode->pos ) );
al_push_long( &current_mode->used, current_mode->pos );
return i->data;
}
}
}
}
if( !current_mode->has_loaded )
{
/*
We found no match in the list, try loading the history
file and continue the search
*/
history_load( current_mode );
return history_prev_match( needle );
}
else
{
/*
We found no match in the list, and the file is already
loaded. Set poition before first element and return
original search string.
*/
current_mode->pos=-1;
if( al_peek_long( &current_mode->used ) != -1 )
al_push_long( &current_mode->used, -1 );
}
}
return needle;
}
wchar_t *history_get( int idx )
{
int len;
if( !current_mode )
return 0;
len = al_get_count( &current_mode->item );
if( (idx >= len ) && !current_mode->has_loaded )
{
history_load( current_mode );
len = al_get_count( &current_mode->item );
}
if( idx < 0 )
return 0;
if( idx >= len )
return 0;
return item_get( current_mode, al_get( &current_mode->item, len - 1 - idx ) )->data;
}
void history_first()
{
if( current_mode )
{
if( !current_mode->has_loaded )
{
history_load( current_mode );
}
current_mode->pos = 0;
}
}
void history_reset()
{
if( current_mode )
{
current_mode->pos = al_get_count( &current_mode->item );
/*
Clear list of search matches
*/
al_truncate( &current_mode->used, 0 );
}
}
const wchar_t *history_next_match( const wchar_t *needle)
{
if( current_mode )
{
/*
The index of previous search matches are saved in the 'used'
list. We just need to pop the top item and set the new
position. Easy!
*/
if( al_get_count( &current_mode->used ) )
{
al_pop( &current_mode->used );
if( al_get_count( &current_mode->used ) )
{
current_mode->pos = (int) al_peek_long( &current_mode->used );
item_t *i = item_get( current_mode, al_get( &current_mode->item, current_mode->pos ) );
return i->data;
}
}
/*
The used-list is empty. Set position to 'past end of list'
and return the search string.
*/
current_mode->pos = al_get_count( &current_mode->item );
}
return needle;
}
void history_set_mode( const wchar_t *name )
{
if( !mode_table )
{
mode_table = malloc( sizeof(hash_table_t ));
hash_init( mode_table, &hash_wcs_func, &hash_wcs_cmp );
}
current_mode = (history_mode_t *)hash_get( mode_table, name );
if( !current_mode )
{
current_mode = history_create_mode( name );
hash_put( mode_table, name, current_mode );
}
}
void history_init()
{
}
void history_destroy()
{
if( mode_table )
{
hash_foreach( mode_table, (void (*)(void *, void *))&history_save_mode );
hash_foreach( mode_table, (void (*)(void *, void *))&history_destroy_mode_wrapper );
hash_destroy( mode_table );
free( mode_table );
mode_table=0;
}
}
void history_sanity_check()
{
/*
No sanity checking implemented yet...
*/
}

1052
history.cpp Normal file

File diff suppressed because it is too large Load Diff

312
history.h
View File

@@ -6,6 +6,236 @@
#define FISH_HISTORY_H #define FISH_HISTORY_H
#include <wchar.h> #include <wchar.h>
#include "common.h"
#include "pthread.h"
#include <vector>
#include <deque>
#include <utility>
#include <list>
#include <tr1/memory>
#include <set>
typedef std::list<wcstring> path_list_t;
enum history_search_type_t {
/** The history searches for strings containing the given string */
HISTORY_SEARCH_TYPE_CONTAINS,
/** The history searches for strings starting with the given string */
HISTORY_SEARCH_TYPE_PREFIX
};
class history_item_t {
friend class history_t;
friend class history_lru_node_t;
friend class history_tests_t;
private:
explicit history_item_t(const wcstring &);
explicit history_item_t(const wcstring &, time_t, const path_list_t &paths = path_list_t());
/** Attempts to merge two compatible history items together */
bool merge(const history_item_t &item);
/** The actual contents of the entry */
wcstring contents;
/** Original creation time for the entry */
time_t creation_timestamp;
/** Paths that we require to be valid for this item to be autosuggested */
path_list_t required_paths;
public:
const wcstring &str() const { return contents; }
bool empty() const { return contents.empty(); }
/* Whether our contents matches a search term. */
bool matches_search(const wcstring &term, enum history_search_type_t type) const;
time_t timestamp() const { return creation_timestamp; }
const path_list_t &get_required_paths() const { return required_paths; }
bool write_to_file(FILE *f) const;
bool operator==(const history_item_t &other) const {
return contents == other.contents &&
creation_timestamp == other.creation_timestamp &&
required_paths == other.required_paths;
}
bool match_contents(const history_item_t &other) const {
return contents == other.contents;
}
/* Functions for testing only */
};
class history_t {
friend class history_tests_t;
private:
/** No copying */
history_t(const history_t&);
history_t &operator=(const history_t&);
/** Private creator */
history_t(const wcstring &pname);
/** Privately add an item */
void add(const history_item_t &item);
/** Destructor */
~history_t();
/** Lock for thread safety */
pthread_mutex_t lock;
/** Internal function */
void clear_file_state();
/** The name of this list. Used for picking a suitable filename and for switching modes. */
const wcstring name;
/** New items. */
std::vector<history_item_t> new_items;
/** Deleted items. */
std::vector<history_item_t> deleted_items;
/** How many items we've added without saving */
size_t unsaved_item_count;
/** The mmaped region for the history file */
const char *mmap_start;
/** The size of the mmaped region */
size_t mmap_length;
/** Timestamp of when this history was created */
const time_t birth_timestamp;
/** Timestamp of last save */
time_t save_timestamp;
static history_item_t decode_item(const char *ptr, size_t len);
void populate_from_mmap(void);
/** List of old items, as offsets into out mmap data */
std::deque<size_t> old_item_offsets;
/** Whether we've loaded old items */
bool loaded_old;
/** Loads old if necessary */
bool load_old_if_needed(void);
/** Deletes duplicates in new_items. */
void compact_new_items();
/** Saves history */
void save_internal();
public:
/** Returns history with the given name, creating it if necessary */
static history_t & history_with_name(const wcstring &name);
/** Add a new history item to the end */
void add(const wcstring &str, const path_list_t &valid_paths = path_list_t());
/** Remove a history item */
void remove(const wcstring &str);
/** Add a new history item to the end */
void add_with_file_detection(const wcstring &str);
/** Saves history */
void save();
/** Irreversibly clears history */
void clear();
/* Gets all the history into a string with ARRAY_SEP_STR. This is intended for the $history environment variable. This may be long! */
void get_string_representation(wcstring &str, const wcstring &separator);
/** Return the specified history at the specified index. 0 is the index of the current commandline. (So the most recent item is at index 1.) */
history_item_t item_at_index(size_t idx);
bool is_deleted(const history_item_t &item) const;
};
class history_search_t {
/** The history in which we are searching */
history_t * history;
/** Our type */
enum history_search_type_t search_type;
/** Our list of previous matches as index, value. The end is the current match. */
typedef std::pair<size_t, history_item_t> prev_match_t;
std::deque<prev_match_t> prev_matches;
/** Returns yes if a given term is in prev_matches. */
bool match_already_made(const wcstring &match) const;
/** The search term */
wcstring term;
/** Additional strings to skip (sorted) */
wcstring_list_t external_skips;
bool should_skip_match(const wcstring &str) const;
public:
/** Gets the search term */
const wcstring &get_term() const { return term; }
/** Sets additional string matches to skip */
void skip_matches(const wcstring_list_t &skips);
/** Finds the next search term (forwards in time). Returns true if one was found. */
bool go_forwards(void);
/** Finds the previous search result (backwards in time). Returns true if one was found. */
bool go_backwards(void);
/** Goes to the end (forwards) */
void go_to_end(void);
/** Returns if we are at the end. We start out at the end. */
bool is_at_end(void) const;
/** Goes to the beginning (backwards) */
void go_to_beginning(void);
/** Returns the current search result item. asserts if there is no current item. */
history_item_t current_item(void) const;
/** Returns the current search result item contents. asserts if there is no current item. */
wcstring current_string(void) const;
/** Constructor */
history_search_t(history_t &hist, const wcstring &str, enum history_search_type_t type = HISTORY_SEARCH_TYPE_CONTAINS) :
history(&hist),
search_type(type),
term(str)
{}
/* Default constructor */
history_search_t() :
history(),
search_type(HISTORY_SEARCH_TYPE_CONTAINS),
term()
{}
};
/** /**
Init history library. The history file won't actually be loaded Init history library. The history file won't actually be loaded
@@ -14,59 +244,47 @@
void history_init(); void history_init();
/** /**
Saves the new history to disc and frees all memory used by the history. Saves the new history to disc.
*/ */
void history_destroy(); void history_destroy();
/**
Add a new history item to the bottom of the history, containing a
copy of str. Remove any duplicates. Moves the current item past the
end of the history list.
*/
void history_add( const wchar_t *str );
/**
Find previous history item starting with str. If this moves before
the start of the history, str is returned.
*/
const wchar_t *history_prev_match( const wchar_t *str );
/**
Return the specified history at the specified index, or 0 if out of bounds. 0 is the index of the current commandline.
*/
wchar_t *history_get( int idx );
/**
Move to first history item
*/
void history_first();
/**
Make current point to last history item
*/
void history_reset();
/**
Find next history item starting with str. If this moves past
the end of the history, str is returned.
*/
const wchar_t *history_next_match( const wchar_t *str);
/**
Set the current mode name for history. Each application that uses
the history has it's own mode. This must be called prior to any use
of the history.
*/
void history_set_mode( const wchar_t *name );
/** /**
Perform sanity checks Perform sanity checks
*/ */
void history_sanity_check(); void history_sanity_check();
/* A helper class for threaded detection of paths */
struct file_detection_context_t {
/* Constructor */
file_detection_context_t(history_t *hist, const wcstring &cmd);
/* Determine which of potential_paths are valid, and put them in valid_paths */
int perform_file_detection();
/* The history associated with this context */
history_t *history;
/* The command */
wcstring command;
/* When the command was issued */
time_t when;
/* The working directory at the time the command was issued */
wcstring working_directory;
/* Paths to test */
path_list_t potential_paths;
/* Paths that were found to be valid */
path_list_t valid_paths;
/* Performs file detection. Returns 1 if every path in potential_paths is valid, 0 otherwise. If test_all is true, tests every path; otherwise stops as soon as it reaches an invalid path. */
int perform_file_detection(bool test_all);
/* Determine whether the given paths are all valid */
bool paths_are_valid(const path_list_t &paths);
};
#endif #endif

View File

@@ -8,6 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <termios.h> #include <termios.h>
@@ -66,42 +67,29 @@
#include "output.h" #include "output.h"
#include "intern.h" #include "intern.h"
#include "halloc.h" #include <vector>
#include "halloc_util.h"
/**
Add a new terminfo mapping
*/
#define TERMINFO_ADD(key) \
{ \
terminfo_mapping_t *m = halloc( terminfo_mappings, sizeof( terminfo_mapping_t ) ); \
m->name = halloc_wcsdup( terminfo_mappings, (L ## #key)+4 ); \
m->seq = key; \
al_push( terminfo_mappings, m ); \
}
/** /**
Struct representing a keybinding. Returned by input_get_mappings. Struct representing a keybinding. Returned by input_get_mappings.
*/ */
typedef struct struct input_mapping_t
{ {
const wchar_t *seq; /**< Character sequence which generates this event */ wcstring seq; /**< Character sequence which generates this event */
const wchar_t *command; /**< command that should be evaluated by this mapping */ wcstring command; /**< command that should be evaluated by this mapping */
}
input_mapping_t; input_mapping_t(const wcstring &s, const wcstring &c) : seq(s), command(c) {}
};
/** /**
A struct representing the mapping from a terminfo key name to a terminfo character sequence A struct representing the mapping from a terminfo key name to a terminfo character sequence
*/ */
typedef struct struct terminfo_mapping_t
{ {
const wchar_t *name; /**< Name of key */ const wchar_t *name; /**< Name of key */
const char *seq; /**< Character sequence generated on keypress */ const char *seq; /**< Character sequence generated on keypress. Constant string. */
} };
terminfo_mapping_t;
/** /**
@@ -226,16 +214,26 @@ static const wchar_t code_arr[] =
} }
; ;
/** Mappings for the current input mode */
static std::vector<input_mapping_t> mapping_list;
/* Terminfo map list */
static std::vector<terminfo_mapping_t> terminfo_mappings;
/** Add a new terminfo mapping */
static inline void terminfo_add(const wchar_t *name, const char *seq)
{
terminfo_mapping_t mapping = {name, seq};
terminfo_mappings.push_back(mapping);
}
#define TERMINFO_ADD(key) do { terminfo_add((L ## #key)+4, key); } while (0)
/**
Mappings for the current input mode
*/
static array_list_t mappings = {0,0,0};
/** /**
List of all terminfo mappings List of all terminfo mappings
*/ */
static array_list_t *terminfo_mappings = 0; static std::vector<terminfo_mapping_t> mappings;
/** /**
@@ -247,10 +245,7 @@ static int is_init = 0;
Initialize terminfo. Initialize terminfo.
*/ */
static void input_terminfo_init(); static void input_terminfo_init();
/**
Deallocate memory used by terminfo. Or at least try to. Terminfo leaks.
*/
static void input_terminfo_destroy();
/** /**
Returns the function description for the given function code. Returns the function description for the given function code.
@@ -259,7 +254,7 @@ static void input_terminfo_destroy();
void input_mapping_add( const wchar_t *sequence, void input_mapping_add( const wchar_t *sequence,
const wchar_t *command ) const wchar_t *command )
{ {
int i; size_t i;
CHECK( sequence, ); CHECK( sequence, );
CHECK( command, ); CHECK( command, );
@@ -267,21 +262,16 @@ void input_mapping_add( const wchar_t *sequence,
// debug( 0, L"Add mapping from %ls to %ls", escape(sequence, 1), escape(command, 1 ) ); // debug( 0, L"Add mapping from %ls to %ls", escape(sequence, 1), escape(command, 1 ) );
for( i=0; i<al_get_count( &mappings); i++ ) for( i=0; i<mapping_list.size(); i++ )
{ {
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i ); input_mapping_t &m = mapping_list.at(i);
if( wcscmp( m->seq, sequence ) == 0 ) if( m.seq == sequence )
{ {
m->command = intern(command); m.command = command;
return; return;
} }
} }
mapping_list.push_back(input_mapping_t(sequence, command));
input_mapping_t *m = malloc( sizeof( input_mapping_t ) );
m->seq = intern( sequence );
m->command = intern(command);
al_push( &mappings, m );
} }
/** /**
@@ -315,6 +305,32 @@ static int interrupt_handler()
return R_NULL; return R_NULL;
} }
void update_fish_term256(void)
{
/* Infer term256 support. If fish_term256 is set, we respect it; otherwise try to detect it from the TERM variable */
env_var_t fish_term256 = env_get_string(L"fish_term256");
bool support_term256;
if (! fish_term256.missing_or_empty()) {
support_term256 = from_string<bool>(fish_term256);
} else {
env_var_t term = env_get_string(L"TERM");
if (term.missing()) {
support_term256 = false;
} else if (term.find(L"256color") != wcstring::npos) {
/* Explicitly supported */
support_term256 = true;
} else if (term.find(L"xterm") != wcstring::npos) {
// assume that all xterms are 256, except for OS X SnowLeopard
env_var_t prog = env_get_string(L"TERM_PROGRAM");
support_term256 = (prog != L"Apple_Terminal");
} else {
// Don't know, default to false
support_term256 = false;
}
}
output_set_supports_term256(support_term256);
}
int input_init() int input_init()
{ {
if( is_init ) if( is_init )
@@ -327,16 +343,18 @@ int input_init()
if( setupterm( 0, STDOUT_FILENO, 0) == ERR ) if( setupterm( 0, STDOUT_FILENO, 0) == ERR )
{ {
debug( 0, _( L"Could not set up terminal" ) ); debug( 0, _( L"Could not set up terminal" ) );
exit(1); exit_without_destructors(1);
} }
output_set_term( env_get( L"TERM" ) ); const env_var_t term = env_get_string( L"TERM" );
assert(! term.missing());
output_set_term( term.c_str() );
input_terminfo_init(); input_terminfo_init();
/* update_fish_term256();
If we have no keybindings, add a few simple defaults
*/ /* If we have no keybindings, add a few simple defaults */
if( !al_get_count( &mappings ) ) if( mapping_list.size() )
{ {
input_mapping_add( L"", L"self-insert" ); input_mapping_add( L"", L"self-insert" );
input_mapping_add( L"\n", L"execute" ); input_mapping_add( L"\n", L"execute" );
@@ -357,26 +375,20 @@ void input_destroy()
is_init=0; is_init=0;
al_foreach( &mappings, &free );
al_destroy( &mappings );
input_common_destroy(); input_common_destroy();
if( del_curterm( cur_term ) == ERR ) if( del_curterm( cur_term ) == ERR )
{ {
debug( 0, _(L"Error while closing terminfo") ); debug( 0, _(L"Error while closing terminfo") );
} }
input_terminfo_destroy();
} }
/** /**
Perform the action of the specified binding Perform the action of the specified binding
*/ */
static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq ) static wint_t input_exec_binding( const input_mapping_t &m, const wcstring &seq )
{ {
wchar_t code = input_function_get_code( m->command ); wchar_t code = input_function_get_code( m.command );
if( code != -1 ) if( code != -1 )
{ {
switch( code ) switch( code )
@@ -403,7 +415,7 @@ static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq )
*/ */
int last_status = proc_get_last_status(); int last_status = proc_get_last_status();
eval( m->command, 0, TOP ); parser_t::principal_parser().eval( m.command.c_str(), 0, TOP );
proc_set_last_status( last_status ); proc_set_last_status( last_status );
@@ -427,44 +439,47 @@ static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq )
/** /**
Try reading the specified function mapping Try reading the specified function mapping
*/ */
static wint_t input_try_mapping( input_mapping_t *m) static wint_t input_try_mapping( const input_mapping_t &m)
{ {
int j, k;
wint_t c=0; wint_t c=0;
int j;
/* /*
Check if the actual function code of this mapping is on the stack Check if the actual function code of this mapping is on the stack
*/ */
c = input_common_readch( 0 ); c = input_common_readch( 0 );
if( c == input_function_get_code( m->command ) ) if( c == input_function_get_code( m.command ) )
{ {
return input_exec_binding( m, m->seq ); return input_exec_binding( m, m.seq );
} }
input_unreadch( c ); input_unreadch( c );
if( m->seq != 0 ) const wchar_t *str = m.seq.c_str();
{ for (j=0; str[j] != L'\0'; j++)
{
bool timed = (j > 0);
c = input_common_readch(timed);
if (str[j] != c)
break;
}
for( j=0; m->seq[j] != L'\0' && if( str[j] == L'\0' )
m->seq[j] == (c=input_common_readch( j>0 )); j++ ) {
; /* We matched the entire sequence */
return input_exec_binding( m, m.seq );
if( m->seq[j] == L'\0' ) }
{ else
return input_exec_binding( m, m->seq ); {
} int k;
else /*
{ Return the read characters
/* */
Return the read characters input_unreadch(c);
*/ for( k=j-1; k>=0; k-- )
input_unreadch(c); {
for( k=j-1; k>=0; k-- ) input_unreadch( m.seq[k] );
{ }
input_unreadch( m->seq[k] ); }
}
}
}
return 0; return 0;
} }
@@ -477,7 +492,7 @@ void input_unreadch( wint_t ch )
wint_t input_readch() wint_t input_readch()
{ {
int i; size_t i;
CHECK_BLOCK( R_NULL ); CHECK_BLOCK( R_NULL );
@@ -492,17 +507,17 @@ wint_t input_readch()
while( 1 ) while( 1 )
{ {
input_mapping_t *generic = 0; const input_mapping_t *generic = 0;
for( i=0; i<al_get_count( &mappings); i++ ) for( i=0; i<mapping_list.size(); i++ )
{ {
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i ); const input_mapping_t &m = mapping_list.at(i);
wint_t res = input_try_mapping( m ); wint_t res = input_try_mapping( m );
if( res ) if( res )
return res; return res;
if( wcslen( m->seq) == 0 ) if( m.seq.length() == 0 )
{ {
generic = m; generic = &m;
} }
} }
@@ -521,7 +536,7 @@ wint_t input_readch()
; ;
arr[0] = input_common_readch(0); arr[0] = input_common_readch(0);
return input_exec_binding( generic, arr ); return input_exec_binding( *generic, arr );
} }
/* /*
@@ -531,63 +546,57 @@ wint_t input_readch()
input_common_readch( 0 ); } input_common_readch( 0 ); }
} }
void input_mapping_get_names( array_list_t *list ) void input_mapping_get_names( wcstring_list_t &lst )
{ {
int i; size_t i;
for( i=0; i<al_get_count( &mappings ); i++ ) for( i=0; i<mapping_list.size(); i++ )
{ {
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i ); const input_mapping_t &m = mapping_list.at(i);
al_push( list, m->seq ); lst.push_back(wcstring(m.seq));
} }
} }
int input_mapping_erase( const wchar_t *sequence ) bool input_mapping_erase( const wchar_t *sequence )
{ {
int ok = 0; ASSERT_IS_MAIN_THREAD();
int i; bool result = false;
size_t sz = al_get_count( &mappings ); size_t i, sz = mapping_list.size();
for( i=0; i<sz; i++ ) for( i=0; i<sz; i++ )
{ {
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i ); const input_mapping_t &m = mapping_list.at(i);
if( !wcscmp( sequence, m->seq ) ) if( sequence == m.seq )
{ {
if( i != (sz-1 ) ) if( i != (sz-1 ) )
{ {
al_set( &mappings, i, al_get( &mappings, sz -1 ) ); mapping_list[i] = mapping_list[sz-1];
} }
al_truncate( &mappings, sz-1 ); mapping_list.pop_back();
ok = 1; result = true;
free( m );
break; break;
} }
} }
return result;
return ok;
} }
const wchar_t *input_mapping_get( const wchar_t *sequence ) bool input_mapping_get( const wcstring &sequence, wcstring &cmd )
{ {
int i; size_t i, sz = mapping_list.size();
size_t sz = al_get_count( &mappings );
for( i=0; i<sz; i++ ) for( i=0; i<sz; i++ )
{ {
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i ); const input_mapping_t &m = mapping_list.at(i);
if( !wcscmp( sequence, m->seq ) ) if( sequence == m.seq )
{ {
return m->command; cmd = m.command;
return true;
} }
} }
return 0; return false;
} }
/** /**
@@ -595,9 +604,6 @@ const wchar_t *input_mapping_get( const wchar_t *sequence )
*/ */
static void input_terminfo_init() static void input_terminfo_init()
{ {
terminfo_mappings = al_halloc( 0 );
TERMINFO_ADD(key_a1); TERMINFO_ADD(key_a1);
TERMINFO_ADD(key_a3); TERMINFO_ADD(key_a3);
TERMINFO_ADD(key_b2); TERMINFO_ADD(key_b2);
@@ -756,32 +762,23 @@ static void input_terminfo_init()
TERMINFO_ADD(key_up); TERMINFO_ADD(key_up);
} }
static void input_terminfo_destroy()
{
if( terminfo_mappings )
{
halloc_free( terminfo_mappings );
}
}
const wchar_t *input_terminfo_get_sequence( const wchar_t *name ) const wchar_t *input_terminfo_get_sequence( const wchar_t *name )
{ {
ASSERT_IS_MAIN_THREAD();
const char *res = 0; const char *res = 0;
int i; static wcstring buff;
static string_buffer_t *buff = 0;
int err = ENOENT; int err = ENOENT;
CHECK( name, 0 ); CHECK( name, 0 );
input_init(); input_init();
for( i=0; i<al_get_count( terminfo_mappings ); i++ ) for( size_t i=0; i<terminfo_mappings.size(); i++ )
{ {
terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i ); const terminfo_mapping_t &m = terminfo_mappings.at(i);
if( !wcscmp( name, m.name ) )
if( !wcscmp( name, m->name ) )
{ {
res = m->seq; res = m.seq;
err = EILSEQ; err = EILSEQ;
break; break;
} }
@@ -793,91 +790,67 @@ const wchar_t *input_terminfo_get_sequence( const wchar_t *name )
return 0; return 0;
} }
if( !buff ) buff = format_string(L"%s", res);
{ return buff.c_str();
buff = sb_halloc( global_context );
}
sb_clear( buff );
sb_printf( buff, L"%s", res );
return (wchar_t *)buff->buff;
} }
const wchar_t *input_terminfo_get_name( const wchar_t *seq ) bool input_terminfo_get_name( const wcstring &seq, wcstring &name )
{ {
int i;
static string_buffer_t *buff = 0;
CHECK( seq, 0 );
input_init(); input_init();
if( !buff ) for( size_t i=0; i<terminfo_mappings.size(); i++ )
{ {
buff = sb_halloc( global_context ); terminfo_mapping_t &m = terminfo_mappings.at(i);
}
for( i=0; i<al_get_count( terminfo_mappings ); i++ ) if( !m.seq )
{
terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i );
if( !m->seq )
{ {
continue; continue;
} }
sb_clear( buff ); const wcstring map_buf = format_string(L"%s", m.seq);
sb_printf( buff, L"%s", m->seq ); if (map_buf == seq) {
name = m.name;
if( !wcscmp( seq, (wchar_t *)buff->buff ) ) return true;
{ }
return m->name; }
}
}
return 0;
return false;
} }
void input_terminfo_get_names( array_list_t *lst, int skip_null ) wcstring_list_t input_terminfo_get_names( bool skip_null )
{ {
int i; wcstring_list_t result;
result.reserve(terminfo_mappings.size());
CHECK( lst, );
input_init(); input_init();
for( i=0; i<al_get_count( terminfo_mappings ); i++ ) for( size_t i=0; i<terminfo_mappings.size(); i++ )
{ {
terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i ); terminfo_mapping_t &m = terminfo_mappings.at(i);
if( skip_null && !m->seq ) if( skip_null && !m.seq )
{ {
continue; continue;
} }
al_push( lst, m->name ); result.push_back(wcstring(m.name));
} }
return result;
} }
void input_function_get_names( array_list_t *lst ) wcstring_list_t input_function_get_names( void )
{ {
int i; size_t count = sizeof name_arr / sizeof *name_arr;
return wcstring_list_t(name_arr, name_arr + count);
CHECK( lst, );
for( i=0; i<(sizeof(name_arr)/sizeof(wchar_t *)); i++ )
{
al_push( lst, name_arr[i] );
}
} }
wchar_t input_function_get_code( const wchar_t *name ) wchar_t input_function_get_code( const wcstring &name )
{ {
int i; size_t i;
for( i = 0; i<(sizeof( code_arr )/sizeof(wchar_t)) ; i++ ) for( i = 0; i<(sizeof( code_arr )/sizeof(wchar_t)) ; i++ )
{ {
if( wcscmp( name, name_arr[i] ) == 0 ) if( name == name_arr[i] )
{ {
return code_arr[i]; return code_arr[i];
} }

37
input.h
View File

@@ -97,19 +97,19 @@ void input_unreadch( wint_t ch );
void input_mapping_add( const wchar_t *sequence, const wchar_t *command ); void input_mapping_add( const wchar_t *sequence, const wchar_t *command );
/** /**
Insert all mapping names into the specified array_list_t Insert all mapping names into the specified wcstring_list_t
*/ */
void input_mapping_get_names( array_list_t *list ); void input_mapping_get_names( wcstring_list_t &lst );
/** /**
Erase binding for specified key sequence Erase binding for specified key sequence
*/ */
int input_mapping_erase( const wchar_t *sequence ); bool input_mapping_erase( const wchar_t *sequence );
/** /**
Return the command bound to the specified key sequence Gets the command bound to the specified key sequence. Returns true if it exists, false if not.
*/ */
const wchar_t *input_mapping_get( const wchar_t *sequence ); bool input_mapping_get( const wcstring &sequence, wcstring &cmd );
/** /**
Return the sequence for the terminfo variable of the specified name. Return the sequence for the terminfo variable of the specified name.
@@ -119,26 +119,21 @@ const wchar_t *input_mapping_get( const wchar_t *sequence );
*/ */
const wchar_t *input_terminfo_get_sequence( const wchar_t *name ); const wchar_t *input_terminfo_get_sequence( const wchar_t *name );
/** /** Return the name of the terminfo variable with the specified sequence */
Return the name of the terminfo variable with the specified sequence bool input_terminfo_get_name( const wcstring &seq, wcstring &name );
*/
const wchar_t *input_terminfo_get_name( const wchar_t *seq );
/** /** Return a list of all known terminfo names */
Return a list of all known terminfo names wcstring_list_t input_terminfo_get_names( bool skip_null );
*/
void input_terminfo_get_names( array_list_t *lst, int skip_null );
/** /** Returns the input function code for the given input function name. */
Returns the input function code for the given input function name. wchar_t input_function_get_code( const wcstring &name );
*/
wchar_t input_function_get_code( const wchar_t *name );
/** /** Returns a list of all existing input function names */
Returns a list of all existing input function names wcstring_list_t input_function_get_names( void );
*/
void input_function_get_names( array_list_t *lst ); /** Updates our idea of whether we support term256 */
void update_fish_term256();
#endif #endif

View File

@@ -25,6 +25,7 @@ Implementation file for the low level input library
#include "wutil.h" #include "wutil.h"
#include "input_common.h" #include "input_common.h"
#include "env_universal.h" #include "env_universal.h"
#include "iothread.h"
/** /**
Time in milliseconds to wait for another byte to be available for Time in milliseconds to wait for another byte to be available for
@@ -43,16 +44,23 @@ static wint_t lookahead_arr[1024];
*/ */
static int lookahead_count = 0; static int lookahead_count = 0;
/** /** Callback function for handling interrupts on reading */
Callback function for handling interrupts on reading
*/
static int (*interrupt_handler)(); static int (*interrupt_handler)();
/** Callback function to be invoked before reading each byte */
static void (*poll_handler)();
void input_common_init( int (*ih)() ) void input_common_init( int (*ih)() )
{ {
interrupt_handler = ih; interrupt_handler = ih;
} }
void input_common_set_poll_callback(void (*handler)(void))
{
poll_handler = handler;
}
void input_common_destroy() void input_common_destroy()
{ {
@@ -64,26 +72,34 @@ void input_common_destroy()
*/ */
static wint_t readb() static wint_t readb()
{ {
/* do_loop must be set on every path through the loop; leaving it uninitialized allows the static analyzer to assist in catching mistakes. */
unsigned char arr[1]; unsigned char arr[1];
int do_loop = 0; bool do_loop;
do do
{ {
fd_set fd; /* Invoke any poll handler */
int fd_max=1; if (poll_handler)
poll_handler();
fd_set fdset;
int fd_max=0;
int ioport = iothread_port();
int res; int res;
FD_ZERO( &fd ); FD_ZERO( &fdset );
FD_SET( 0, &fd ); FD_SET( 0, &fdset );
if( env_universal_server.fd > 0 ) if( env_universal_server.fd > 0 )
{ {
FD_SET( env_universal_server.fd, &fd ); FD_SET( env_universal_server.fd, &fdset );
fd_max = env_universal_server.fd+1; if (fd_max < env_universal_server.fd) fd_max = env_universal_server.fd;
}
if (ioport > 0) {
FD_SET( ioport, &fdset );
if (fd_max < ioport) fd_max = ioport;
} }
do_loop = 0; res = select( fd_max + 1, &fdset, 0, 0, 0 );
res = select( fd_max, &fd, 0, 0, 0 );
if( res==-1 ) if( res==-1 )
{ {
switch( errno ) switch( errno )
@@ -106,7 +122,7 @@ static wint_t readb()
} }
do_loop = 1; do_loop = true;
break; break;
} }
default: default:
@@ -120,30 +136,38 @@ static wint_t readb()
} }
else else
{ {
if( env_universal_server.fd > 0 ) /* Assume we loop unless we see a character in stdin */
{ do_loop = true;
if( FD_ISSET( env_universal_server.fd, &fd ) )
{
debug( 3, L"Wake up on universal variable event" );
env_universal_read_all();
do_loop = 1;
if( lookahead_count ) if( env_universal_server.fd > 0 && FD_ISSET( env_universal_server.fd, &fdset ) )
{ {
return lookahead_arr[--lookahead_count]; debug( 3, L"Wake up on universal variable event" );
} env_universal_read_all();
} if( lookahead_count )
{
return lookahead_arr[--lookahead_count];
}
}
if ( ioport > 0 && FD_ISSET(ioport, &fdset))
{
iothread_service_completion();
if( lookahead_count )
{
return lookahead_arr[--lookahead_count];
}
} }
if( FD_ISSET( 0, &fd ) )
if( FD_ISSET( STDIN_FILENO, &fdset ) )
{ {
if( read_blocked( 0, arr, 1 ) != 1 ) if( read_blocked( 0, arr, 1 ) != 1 )
{ {
/* /* The teminal has been closed. Save and exit. */
The teminal has been closed. Save and exit.
*/
return R_EOF; return R_EOF;
} }
do_loop = 0;
/* We read from stdin, so don't loop */
do_loop = false;
} }
} }
} }
@@ -152,7 +176,6 @@ static wint_t readb()
return arr[0]; return arr[0];
} }
wchar_t input_common_readch( int timed ) wchar_t input_common_readch( int timed )
{ {
if( lookahead_count == 0 ) if( lookahead_count == 0 )

View File

@@ -30,6 +30,9 @@ enum
*/ */
void input_common_init( int (*ih)() ); void input_common_init( int (*ih)() );
/* Sets a callback to be invoked every time a byte is read */
void input_common_set_poll_callback(void (*handler)(void));
/** /**
Free memory used by the library Free memory used by the library
*/ */

128
intern.c
View File

@@ -1,128 +0,0 @@
/** \file intern.c
Library for pooling common strings
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#include "intern.h"
/**
Table of interned strings
*/
static hash_table_t *intern_table=0;
/**
Table of static interned strings
*/
static hash_table_t *intern_static_table=0;
const wchar_t *intern( const wchar_t *in )
{
const wchar_t *res=0;
// debug( 0, L"intern %ls", in );
if( !in )
return 0;
if( !intern_table )
{
intern_table = malloc( sizeof( hash_table_t ) );
if( !intern_table )
{
DIE_MEM();
}
hash_init( intern_table, &hash_wcs_func, &hash_wcs_cmp );
}
if( intern_static_table )
{
res = hash_get( intern_static_table, in );
}
if( !res )
{
res = hash_get( intern_table, in );
if( !res )
{
res = wcsdup( in );
if( !res )
{
DIE_MEM();
}
hash_put( intern_table, res, res );
}
}
return res;
}
const wchar_t *intern_static( const wchar_t *in )
{
const wchar_t *res=0;
if( !in )
return 0;
if( !intern_static_table )
{
intern_static_table = malloc( sizeof( hash_table_t ) );
if( !intern_static_table )
{
DIE_MEM();
}
hash_init( intern_static_table, &hash_wcs_func, &hash_wcs_cmp );
}
res = hash_get( intern_static_table, in );
if( !res )
{
res = in;
hash_put( intern_static_table, res, res );
}
return res;
}
/**
Free the specified key/value pair. Should only be called by intern_free_all at shutdown
*/
static void clear_value( void *key, void *data )
{
debug( 3, L"interned string: '%ls'", data );
free( (void *)data );
}
void intern_free_all()
{
if( intern_table )
{
hash_foreach( intern_table, &clear_value );
hash_destroy( intern_table );
free( intern_table );
intern_table=0;
}
if( intern_static_table )
{
hash_destroy( intern_static_table );
free( intern_static_table );
intern_static_table=0;
}
}

85
intern.cpp Normal file
View File

@@ -0,0 +1,85 @@
/** \file intern.c
Library for pooling common strings
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <set>
#include <deque>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#include "intern.h"
/** Comparison function for intern'd strings */
class string_table_compare_t {
public:
bool operator()(const wchar_t *a, const wchar_t *b) const {
return wcscmp(a, b) < 0;
}
};
/* A sorted deque ends up being a little more memory efficient than a std::set for the intern'd string table */
#define USE_SET 0
#if USE_SET
/** The table of intern'd strings */
typedef std::set<const wchar_t *, string_table_compare_t> string_table_t;
#else
/** The table of intern'd strings */
typedef std::deque<const wchar_t *> string_table_t;
#endif
static string_table_t string_table;
/** The lock to provide thread safety for intern'd strings */
static pthread_mutex_t intern_lock = PTHREAD_MUTEX_INITIALIZER;
static const wchar_t *intern_with_dup( const wchar_t *in, bool dup )
{
if( !in )
return NULL;
// debug( 0, L"intern %ls", in );
scoped_lock lock(intern_lock);
const wchar_t *result;
#if USE_SET
string_table_t::const_iterator iter = string_table.find(in);
if (iter != string_table.end()) {
result = *iter;
} else {
result = dup ? wcsdup(in) : in;
string_table.insert(result);
}
#else
string_table_t::iterator iter = std::lower_bound(string_table.begin(), string_table.end(), in, string_table_compare_t());
if (iter != string_table.end() && wcscmp(*iter, in) == 0) {
result = *iter;
} else {
result = dup ? wcsdup(in) : in;
string_table.insert(iter, result);
}
#endif
return result;
}
const wchar_t *intern( const wchar_t *in )
{
return intern_with_dup(in, true);
}
const wchar_t *intern_static( const wchar_t *in )
{
return intern_with_dup(in, false);
}

View File

@@ -25,9 +25,4 @@ const wchar_t *intern( const wchar_t *in );
*/ */
const wchar_t *intern_static( const wchar_t *in ); const wchar_t *intern_static( const wchar_t *in );
/**
Free all interned strings. Only call this at shutdown.
*/
void intern_free_all();
#endif #endif

125
internalize_scripts.py Executable file
View File

@@ -0,0 +1,125 @@
#!/usr/bin/env python
import string, sys, os.path
escapes = {}
escapes['\a'] = r'\a'
escapes['\b'] = r'\b'
escapes['\f'] = r'\f'
escapes['\n'] = r'\n'
escapes['\r'] = r'\r'
#escapes['\t'] = r'\t'
# Let's replace tabs with four spaces
# so the text looks nicely indented in the C source
escapes['\t'] = r' '
escapes['\v'] = r'\v'
# escapes['\''] = r'\''
escapes['\"'] = r'\"'
escapes['\\'] = r'\\'
def escape(c):
if c in escapes:
return (escapes[c], False)
elif c not in string.printable:
return ("\\x%x" % ord(c), True)
else:
return (c, False)
def stringize(line):
newline = '"'
was_escape = False
for c in line:
# Avoid an issue where characters after a hexadecimal escape are treated as part of that escape
# by adding two quotes
if was_escape and c in string.hexdigits:
newline += '""'
chars, was_escape = escape(c)
newline += chars
newline += '"'
return newline
class cfunc:
def __init__(self, type, name, lines):
self.type = type
self.name = name
self.lines = lines
def cdef(self):
result = ""
result += "static const char * const {0} = \n\t".format(self.cfunc_name())
result += '\n\t'.join(self.lines)
result += ';\n'
return result
def cfunc_name(self):
# Translate - and . to underscore
try: #Python 2
translator = string.maketrans('-.', '__')
munged_name = string.translate(self.name, translator)
except AttributeError: #Python 3
translator = "".maketrans('-.', '__')
munged_name = self.name.translate(translator)
return "{0}_{1}".format(self.type, munged_name)
TYPES = ['function', 'completion']
type_to_funcs = dict((t, []) for t in TYPES)
for file in sys.argv[1:]:
fd = open(file, 'r')
newlines = []
for line in fd:
newlines.append(stringize(line))
fd.close()
dirname = os.path.dirname(file)
# Try to figure out the file type (completion or function)
matches = [dir in dirname for dir in TYPES]
if matches.count(True) is not 1:
print("Cannot determine the type of the file at path {0}".format(file))
sys.exit(-1)
type = TYPES[matches.index(True)]
name = os.path.basename(file)
name, ext = os.path.splitext(name)
newfunc = cfunc(type, name, newlines)
type_to_funcs[type].append(newfunc)
# Sort our functions by name
for funcs in type_to_funcs.values():
funcs.sort(key=cfunc.cfunc_name)
# Output our header
fd = open('builtin_scripts.h', 'w')
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
fd.write("""struct builtin_script_t {
const wchar_t *name;
const char *def;
};""")
fd.write('\n')
for type in TYPES:
funcs = type_to_funcs[type]
fd.write('\n')
fd.write('extern const struct builtin_script_t internal_{0}_scripts[{1}];'.format(type, len(funcs)))
fd.write('\n')
fd.close()
# Output the function definitions
fd = open('builtin_scripts.cpp', 'w')
fd.write('/* This file is generated by internalize_scripts.py */\n\n')
fd.write('#include "builtin_scripts.h"\n\n')
for type in TYPES:
for func in type_to_funcs[type]:
fd.write(func.cdef())
fd.write('\n')
# Output the refs
for type in TYPES:
funcs = type_to_funcs[type]
func_refs = ["{0}L{1}, {2}{3}".format("{", stringize(func.name), func.cfunc_name(), "}") for func in funcs]
fd.write('const struct builtin_script_t internal_{0}_scripts[{1}] =\n'.format(type, len(funcs)))
fd.write('{\n\t')
fd.write(',\n\t'.join(func_refs))
fd.write('\n};\n')
fd.close()

View File

@@ -48,8 +48,6 @@ Utilities for io redirection.
#include "common.h" #include "common.h"
#include "io.h" #include "io.h"
#include "halloc.h"
void io_buffer_read( io_data_t *d ) void io_buffer_read( io_data_t *d )
{ {
@@ -93,7 +91,7 @@ void io_buffer_read( io_data_t *d )
} }
else else
{ {
b_append( d->param2.out_buffer, b, l ); d->out_buffer_append( b, l );
} }
} }
} }
@@ -102,22 +100,18 @@ void io_buffer_read( io_data_t *d )
io_data_t *io_buffer_create( int is_input ) io_data_t *io_buffer_create( int is_input )
{ {
io_data_t *buffer_redirect = malloc( sizeof( io_data_t )); std::auto_ptr<io_data_t> buffer_redirect(new io_data_t);
buffer_redirect->out_buffer_create();
buffer_redirect->io_mode=IO_BUFFER; buffer_redirect->io_mode=IO_BUFFER;
buffer_redirect->next=0; buffer_redirect->next=0;
buffer_redirect->param2.out_buffer= malloc( sizeof(buffer_t));
buffer_redirect->is_input = is_input; buffer_redirect->is_input = is_input;
b_init( buffer_redirect->param2.out_buffer );
buffer_redirect->fd=is_input?0:1; buffer_redirect->fd=is_input?0:1;
if( exec_pipe( buffer_redirect->param1.pipe_fd ) == -1 ) if( exec_pipe( buffer_redirect->param1.pipe_fd ) == -1 )
{ {
debug( 1, PIPE_ERROR ); debug( 1, PIPE_ERROR );
wperror (L"pipe"); wperror (L"pipe");
free( buffer_redirect->param2.out_buffer ); return NULL;
free( buffer_redirect );
return 0;
} }
else if( fcntl( buffer_redirect->param1.pipe_fd[0], else if( fcntl( buffer_redirect->param1.pipe_fd[0],
F_SETFL, F_SETFL,
@@ -125,11 +119,9 @@ io_data_t *io_buffer_create( int is_input )
{ {
debug( 1, PIPE_ERROR ); debug( 1, PIPE_ERROR );
wperror( L"fcntl" ); wperror( L"fcntl" );
free( buffer_redirect->param2.out_buffer ); return NULL;
free( buffer_redirect );
return 0;
} }
return buffer_redirect; return buffer_redirect.release();
} }
void io_buffer_destroy( io_data_t *io_buffer ) void io_buffer_destroy( io_data_t *io_buffer )
@@ -150,11 +142,7 @@ void io_buffer_destroy( io_data_t *io_buffer )
Dont free fd for writing. This should already be free'd before Dont free fd for writing. This should already be free'd before
calling exec_read_io_buffer on the buffer calling exec_read_io_buffer on the buffer
*/ */
delete io_buffer;
b_destroy( io_buffer->param2.out_buffer );
free( io_buffer->param2.out_buffer );
free( io_buffer );
} }
@@ -195,29 +183,21 @@ io_data_t *io_remove( io_data_t *list, io_data_t *element )
return list; return list;
} }
io_data_t *io_duplicate( void *context, io_data_t *l ) io_data_t *io_duplicate( io_data_t *l )
{ {
io_data_t *res; io_data_t *res;
if( l == 0 ) if( l == 0 )
return 0; return 0;
res = halloc( context, sizeof( io_data_t) ); res = new io_data_t(*l);
res->next=io_duplicate(l->next );
if( !res )
{
DIE_MEM();
}
memcpy( res, l, sizeof(io_data_t ));
res->next=io_duplicate( context, l->next );
return res; return res;
} }
io_data_t *io_get( io_data_t *io, int fd ) io_data_t *io_get( io_data_t *io, int fd )
{ {
if( io == 0 ) if( io == NULL )
return 0; return 0;
io_data_t *res = io_get( io->next, fd ); io_data_t *res = io_get( io->next, fd );

104
io.h
View File

@@ -1,22 +1,34 @@
#ifndef FISH_IO_H #ifndef FISH_IO_H
#define FISH_IO_H #define FISH_IO_H
#include <vector>
#include <tr1/memory>
using std::tr1::shared_ptr;
/** /**
Describes what type of IO operation an io_data_t represents Describes what type of IO operation an io_data_t represents
*/ */
enum io_mode enum io_mode
{ {
IO_FILE, IO_PIPE, IO_FD, IO_BUFFER, IO_CLOSE IO_FILE, IO_PIPE, IO_FD, IO_BUFFER, IO_CLOSE
} };
;
/** Represents an FD redirection */ /** Represents an FD redirection */
typedef struct io_data class io_data_t
{ {
private:
/** buffer to save output in for IO_BUFFER. Note that in the original fish, the buffer was a pointer to a buffer_t stored in the param2 union down below, and when an io_data_t was duplicated the pointer was copied so that two io_data_ts referenced the same buffer. It's not clear to me how this was ever cleaned up correctly. But it's important that they share the same buffer for reasons I don't yet understand either. We can get correct sharing and cleanup with shared_ptr. */
shared_ptr<std::vector<char> > out_buffer;
/* No assignment allowed */
void operator=(const io_data_t &rhs) { assert(0); }
public:
/** Type of redirect */ /** Type of redirect */
int io_mode; int io_mode;
/** FD to redirect */ /** FD to redirect */
int fd; int fd;
/** /**
Type-specific parameter for redirection Type-specific parameter for redirection
*/ */
@@ -24,35 +36,79 @@ typedef struct io_data
{ {
/** Fds for IO_PIPE and for IO_BUFFER */ /** Fds for IO_PIPE and for IO_BUFFER */
int pipe_fd[2]; int pipe_fd[2];
/** Filename IO_FILE */ /** fd to redirect specified fd to, for IO_FD */
wchar_t *filename;
/** fd to redirect specified fd to, for IO_FD*/
int old_fd; int old_fd;
} param1 } param1;
;
/**
Second type-specific paramter for redirection /** Second type-specific paramter for redirection */
*/
union union
{ {
/** file creation flags to send to open for IO_FILE */ /** file creation flags to send to open for IO_FILE */
int flags; int flags;
/** buffer to save output in for IO_BUFFER */
buffer_t *out_buffer;
/** Whether to close old_fd for IO_FD */ /** Whether to close old_fd for IO_FD */
int close_old; int close_old;
} param2 } param2;
;
/** /** Filename IO_FILE. malloc'd. This needs to be used after fork, so don't use wcstring here. */
Set to true if this is an input io redirection const char *filename_cstr;
*/
/** Convenience to set filename_cstr via wcstring */
void set_filename(const wcstring &str) {
free((void *)filename_cstr);
filename_cstr = wcs2str(str.c_str());
}
/** Function to create the output buffer */
void out_buffer_create() {
out_buffer.reset(new std::vector<char>);
}
/** Function to append to the buffer */
void out_buffer_append(const char *ptr, size_t count) {
assert(out_buffer.get() != NULL);
out_buffer->insert(out_buffer->end(), ptr, ptr + count);
}
/** Function to get a pointer to the buffer */
char *out_buffer_ptr(void) {
assert(out_buffer.get() != NULL);
return (out_buffer->size() == 0) ? NULL : &out_buffer->at(0);
}
/** Function to get the size of the buffer */
size_t out_buffer_size(void) const {
assert(out_buffer.get() != NULL);
return out_buffer->size();
}
/** Set to true if this is an input io redirection */
int is_input; int is_input;
/** Pointer to the next IO redirection */ /** Pointer to the next IO redirection */
struct io_data *next; io_data_t *next;
}
io_data_t; io_data_t() : filename_cstr(NULL), next(NULL)
{
}
io_data_t(const io_data_t &rhs) :
out_buffer(rhs.out_buffer),
io_mode(rhs.io_mode),
fd(rhs.fd),
param1(rhs.param1),
param2(rhs.param2),
filename_cstr(rhs.filename_cstr ? strdup(rhs.filename_cstr) : NULL),
is_input(rhs.is_input),
next(rhs.next)
{
}
~io_data_t() {
free((void *)filename_cstr);
}
};
/** /**
@@ -66,9 +122,9 @@ io_data_t *io_add( io_data_t *first_chain, io_data_t *decond_chain );
io_data_t *io_remove( io_data_t *list, io_data_t *element ); io_data_t *io_remove( io_data_t *list, io_data_t *element );
/** /**
Make a copy of the specified chain of redirections. Uses halloc. Make a copy of the specified chain of redirections. Uses operator new.
*/ */
io_data_t *io_duplicate( void *context, io_data_t *l ); io_data_t *io_duplicate( io_data_t *l );
/** /**
Return the last io redirection in the chain for the specified file descriptor. Return the last io redirection in the chain for the specified file descriptor.
@@ -84,7 +140,7 @@ void io_buffer_destroy( io_data_t *io_buffer );
/** /**
Create a IO_BUFFER type io redirection, complete with a pipe and a Create a IO_BUFFER type io redirection, complete with a pipe and a
buffer_t for output. The default file descriptor used is 1 for vector<char> for output. The default file descriptor used is 1 for
output buffering and 0 for input buffering. output buffering and 0 for input buffering.
\param is_input set this parameter to zero if the buffer should be \param is_input set this parameter to zero if the buffer should be

217
iothread.cpp Normal file
View File

@@ -0,0 +1,217 @@
#include "config.h"
#include "iothread.h"
#include "common.h"
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <queue>
#ifdef _POSIX_THREAD_THREADS_MAX
#if _POSIX_THREAD_THREADS_MAX < 64
#define IO_MAX_THREADS _POSIX_THREAD_THREADS_MAX
#endif
#endif
#ifndef IO_MAX_THREADS
#define IO_MAX_THREADS 64
#endif
static int s_active_thread_count;
typedef unsigned char ThreadIndex_t;
static struct WorkerThread_t {
ThreadIndex_t idx;
pthread_t thread;
} threads[IO_MAX_THREADS];
struct ThreadedRequest_t {
int sequenceNumber;
int (*handler)(void *);
void (*completionCallback)(void *, int);
void *context;
int handlerResult;
};
static struct WorkerThread_t *next_vacant_thread_slot(void) {
for (ThreadIndex_t i=0; i < IO_MAX_THREADS; i++) {
if (! threads[i].thread) return &threads[i];
}
return NULL;
}
static pthread_mutex_t s_request_queue_lock;
static std::queue<ThreadedRequest_t *> s_request_queue;
static int s_last_sequence_number;
static int s_read_pipe, s_write_pipe;
static void iothread_init(void) {
static bool inited = false;
if (! inited) {
inited = true;
/* Initialize the queue lock */
VOMIT_ON_FAILURE(pthread_mutex_init(&s_request_queue_lock, NULL));
/* Initialize the completion pipes */
int pipes[2] = {0, 0};
VOMIT_ON_FAILURE(pipe(pipes));
s_read_pipe = pipes[0];
s_write_pipe = pipes[1];
// 0 means success to VOMIT_ON_FAILURE. Arrange to pass 0 if fcntl returns anything other than -1.
VOMIT_ON_FAILURE(-1 == fcntl(s_read_pipe, F_SETFD, FD_CLOEXEC));
VOMIT_ON_FAILURE(-1 == fcntl(s_write_pipe, F_SETFD, FD_CLOEXEC));
/* Tell each thread its index */
for (ThreadIndex_t i=0; i < IO_MAX_THREADS; i++) {
threads[i].idx = i;
}
}
}
static void add_to_queue(struct ThreadedRequest_t *req) {
ASSERT_IS_LOCKED(s_request_queue_lock);
s_request_queue.push(req);
}
static ThreadedRequest_t *dequeue_request(void) {
ThreadedRequest_t *result = NULL;
scoped_lock lock(s_request_queue_lock);
if (! s_request_queue.empty()) {
result = s_request_queue.front();
s_request_queue.pop();
}
return result;
}
/* The function that does thread work. */
static void *iothread_worker(void *threadPtr) {
assert(threadPtr != NULL);
struct WorkerThread_t *thread = (struct WorkerThread_t *)threadPtr;
// We don't want to receive signals on this thread
sigset_t set;
sigfillset(&set);
VOMIT_ON_FAILURE(pthread_sigmask(SIG_SETMASK, &set, NULL));
/* Grab a request off of the queue */
struct ThreadedRequest_t *req = dequeue_request();
/* Run the handler and store the result */
if (req) {
req->handlerResult = req->handler(req->context);
}
/* Write our index to wake up the main thread */
VOMIT_ON_FAILURE(! write_loop(s_write_pipe, (const char *)&thread->idx, sizeof thread->idx));
/* We're done */
return req;
}
/* Spawn another thread if there's work to be done. */
static void iothread_spawn_if_needed(void) {
ASSERT_IS_LOCKED(s_request_queue_lock);
if (! s_request_queue.empty() && s_active_thread_count < IO_MAX_THREADS) {
struct WorkerThread_t *thread = next_vacant_thread_slot();
assert(thread != NULL);
/* Spawn a thread */
int err;
do {
err = 0;
if (pthread_create(&thread->thread, NULL, iothread_worker, thread)) {
err = errno;
}
} while (err == EAGAIN);
assert(err == 0);
/* Note that we are spawned another thread */
s_active_thread_count += 1;
}
}
int iothread_perform_base(int (*handler)(void *), void (*completionCallback)(void *, int), void *context) {
ASSERT_IS_MAIN_THREAD();
ASSERT_IS_NOT_FORKED_CHILD();
iothread_init();
/* Create and initialize a request. */
struct ThreadedRequest_t *req = new ThreadedRequest_t();
req->handler = handler;
req->completionCallback = completionCallback;
req->context = context;
req->sequenceNumber = ++s_last_sequence_number;
/* Take our lock */
scoped_lock lock(s_request_queue_lock);
/* Add to the queue */
add_to_queue(req);
/* Spawn a thread if necessary */
iothread_spawn_if_needed();
return 0;
}
int iothread_port(void) {
iothread_init();
return s_read_pipe;
}
void iothread_service_completion(void) {
ASSERT_IS_MAIN_THREAD();
ThreadIndex_t threadIdx = (ThreadIndex_t)-1;
VOMIT_ON_FAILURE(1 != read_loop(iothread_port(), &threadIdx, sizeof threadIdx));
assert(threadIdx < IO_MAX_THREADS);
struct WorkerThread_t *thread = &threads[threadIdx];
assert(thread->thread != 0);
struct ThreadedRequest_t *req = NULL;
VOMIT_ON_FAILURE(pthread_join(thread->thread, (void **)&req));
/* Free up this thread */
thread->thread = 0;
assert(s_active_thread_count > 0);
s_active_thread_count -= 1;
/* Handle the request */
if (req) {
if (req->completionCallback)
req->completionCallback(req->context, req->handlerResult);
delete req;
}
/* Maybe spawn another thread, if there's more work to be done. */
VOMIT_ON_FAILURE(pthread_mutex_lock(&s_request_queue_lock));
iothread_spawn_if_needed();
VOMIT_ON_FAILURE(pthread_mutex_unlock(&s_request_queue_lock));
}
void iothread_drain_all(void) {
ASSERT_IS_MAIN_THREAD();
ASSERT_IS_NOT_FORKED_CHILD();
if (s_active_thread_count == 0)
return;
#define TIME_DRAIN 0
#if TIME_DRAIN
int thread_count = s_active_thread_count;
double now = timef();
#endif
while (s_active_thread_count > 0) {
iothread_service_completion();
}
#if TIME_DRAIN
double after = timef();
printf("(Waited %.02f msec for %d thread(s) to drain)\n", 1000 * (after - now), thread_count);
#endif
}

37
iothread.h Normal file
View File

@@ -0,0 +1,37 @@
/** \file iothread.h
Handles IO that may hang.
*/
#ifndef FISH_IOTHREAD_H
#define FISH_IOTHREAD_H
/**
Runs a command on a thread.
\param handler The function to execute on a background thread. Accepts an arbitrary context pointer, and returns an int, which is passed to the completionCallback.
\param completionCallback The function to execute on the main thread once the background thread is complete. Accepts an int (the return value of handler) and the context.
\param context A arbitary context pointer to pass to the handler and completion callback.
\return A sequence number, currently not very useful.
*/
int iothread_perform_base(int (*handler)(void *), void (*completionCallback)(void *, int), void *context);
/**
Gets the fd on which to listen for completion callbacks.
\return A file descriptor on which to listen for completion callbacks.
*/
int iothread_port(void);
/** Services one iothread competion callback. */
void iothread_service_completion(void);
/** Waits for all iothreads to terminate. */
void iothread_drain_all(void);
/** Helper template */
template<typename T>
int iothread_perform(int (*handler)(T *), void (*completionCallback)(T *, int), T *context) {
return iothread_perform_base((int (*)(void *))handler, (void (*)(void *, int))completionCallback, static_cast<void *>(context));
}
#endif

View File

@@ -15,6 +15,7 @@
#include <locale.h> #include <locale.h>
#include <termcap.h> #include <termcap.h>
#include "common.h"
#include "fallback.h" #include "fallback.h"
#include "input_common.h" #include "input_common.h"
@@ -27,7 +28,8 @@ int writestr( char *str )
int main( int argc, char **argv) int main( int argc, char **argv)
{ {
set_main_thread();
setup_fork_guards();
setlocale( LC_ALL, "" ); setlocale( LC_ALL, "" );
@@ -35,7 +37,7 @@ int main( int argc, char **argv)
{ {
static char term_buffer[2048]; static char term_buffer[2048];
char *termtype = getenv ("TERM"); char *termtype = getenv ("TERM");
char *tbuff = malloc( sizeof(char)*9999); char *tbuff = new char[9999];
char *res; char *res;
tgetent( term_buffer, termtype ); tgetent( term_buffer, termtype );

353
kill.c
View File

@@ -1,353 +0,0 @@
/** \file kill.c
The killring.
Works like the killring in emacs and readline. The killring is cut
and paste with a memory of previous cuts. It supports integration
with the X clipboard.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "kill.h"
#include "proc.h"
#include "sanity.h"
#include "common.h"
#include "env.h"
#include "exec.h"
#include "halloc.h"
#include "path.h"
/**
Maximum entries in killring
*/
#define KILL_MAX 8192
static ll_node_t /** Last kill string */*kill_last=0, /** Current kill string */*kill_current =0;
/**
Contents of the X clipboard, at last time we checked it
*/
static wchar_t *cut_buffer=0;
/**
Test if the xsel command is installed. Since this is called often,
cache the result.
*/
static int has_xsel()
{
static int called=0;
static int res = 0;
if (!called) {
void *context = halloc(0, 0);
wchar_t *path = path_get_path( context, L"xsel" );
res = !!path;
halloc_free( context );
called = 1;
}
return res;
}
/**
Add the string to the internal killring
*/
static void kill_add_internal( wchar_t *str )
{
if( wcslen( str ) == 0 )
return;
if( kill_last == 0 )
{
kill_current = kill_last=malloc( sizeof( ll_node_t ) );
kill_current->data = wcsdup(str);
kill_current->prev = kill_current;
}
else
{
kill_current = malloc( sizeof( ll_node_t ) );
kill_current->data = kill_last->data;
kill_last->data = wcsdup(str);
kill_current->prev = kill_last->prev;
kill_last->prev = kill_current;
kill_current = kill_last;
}
}
void kill_add( wchar_t *str )
{
wchar_t *cmd = NULL;
wchar_t *escaped_str;
kill_add_internal(str);
/*
Check to see if user has set the FISH_CLIPBOARD_CMD variable,
and, if so, use it instead of checking the display, etc.
I couldn't think of a safe way to allow overide of the echo
command too, so, the command used must accept the input via stdin.
*/
wchar_t *clipboard;
if( (clipboard = env_get(L"FISH_CLIPBOARD_CMD")) )
{
escaped_str = escape( str, 1 );
cmd = wcsdupcat(L"echo -n ", escaped_str, clipboard);
}
else
{
/* This is for sending the kill to the X copy-and-paste buffer */
if( !has_xsel() ) {
return;
}
wchar_t *disp;
if( (disp = env_get( L"DISPLAY" )) )
{
escaped_str = escape( str, 1 );
cmd = wcsdupcat(L"echo ", escaped_str, L"|xsel -b" );
}
}
if (cmd != NULL)
{
if( exec_subshell( cmd, 0 ) == -1 )
{
/*
Do nothing on failiure
*/
}
free( cut_buffer );
free( cmd );
cut_buffer = escaped_str;
}
}
/**
Remove the specified node from the circular list
*/
static void kill_remove_node( ll_node_t *n )
{
if( n->prev == n )
{
kill_last=kill_current = 0;
}
else
{
ll_node_t *nxt = n->prev;
while( nxt->prev != n )
{
nxt=nxt->prev;
}
nxt->prev = n->prev;
if( kill_last == n )
{
kill_last = n->prev;
}
kill_current=kill_last;
free( n->data );
free( n );
}
}
/**
Remove first match for specified string from circular list
*/
static void kill_remove( wchar_t *s )
{
ll_node_t *n, *next=0;
if( !kill_last )
{
return;
}
for( n=kill_last;
n!=kill_last || next == 0 ;
n=n->prev )
{
if( wcscmp( (wchar_t *)n->data, s ) == 0 )
{
kill_remove_node( n );
break;
}
next = n;
}
}
void kill_replace( wchar_t *old, wchar_t *new )
{
kill_remove( old );
kill_add( new );
}
wchar_t *kill_yank_rotate()
{
if( kill_current == 0 )
return L"";
kill_current = kill_current->prev;
return (wchar_t *)kill_current->data;
}
/**
Check the X clipboard. If it has been changed, add the new
clipboard contents to the fish killring.
*/
static void kill_check_x_buffer()
{
wchar_t *disp;
if( !has_xsel() )
return;
if( (disp = env_get( L"DISPLAY" )) )
{
int i;
wchar_t *cmd = L"xsel -t 500 -b";
wchar_t *new_cut_buffer=0;
array_list_t list;
al_init( &list );
if( exec_subshell( cmd, &list ) != -1 )
{
for( i=0; i<al_get_count( &list ); i++ )
{
wchar_t *next_line = escape( (wchar_t *)al_get( &list, i ), 0 );
if( i==0 )
{
new_cut_buffer = next_line;
}
else
{
wchar_t *old = new_cut_buffer;
new_cut_buffer= wcsdupcat( new_cut_buffer, L"\\n", next_line );
free( old );
free( next_line );
}
}
if( new_cut_buffer )
{
/*
The buffer is inserted with backslash escapes,
since we don't really like tabs, newlines,
etc. anyway.
*/
if( cut_buffer != 0 )
{
if( wcscmp( new_cut_buffer, cut_buffer ) == 0 )
{
free( new_cut_buffer );
new_cut_buffer = 0;
}
else
{
free( cut_buffer );
cut_buffer = 0;
}
}
if( cut_buffer == 0 )
{
cut_buffer = new_cut_buffer;
kill_add_internal( cut_buffer );
}
}
}
al_foreach( &list, &free );
al_destroy( &list );
}
}
wchar_t *kill_yank()
{
kill_check_x_buffer();
if( kill_current == 0 )
return L"";
kill_current=kill_last;
return (wchar_t *)kill_current->data;
}
void kill_sanity_check()
{
int i;
if( is_interactive )
{
/* Test that the kill-ring is consistent */
if( kill_current != 0 )
{
int kill_ok = 0;
ll_node_t *tmp = kill_current->prev;
for( i=0; i<KILL_MAX; i++ )
{
if( tmp == 0 )
break;
if( tmp->data == 0 )
break;
if( tmp == kill_current )
{
kill_ok = 1;
break;
}
tmp = tmp->prev;
}
if( !kill_ok )
{
debug( 0,
L"Killring inconsistent" );
sanity_lose();
}
}
}
}
void kill_init()
{
}
void kill_destroy()
{
if( cut_buffer )
free( cut_buffer );
if( kill_current != 0 )
{
kill_current = kill_last->prev;
kill_last->prev = 0;
while( kill_current )
{
ll_node_t *tmp = kill_current;
kill_current = kill_current->prev;
free( tmp->data );
free( tmp );
}
}
}

231
kill.cpp Normal file
View File

@@ -0,0 +1,231 @@
/** \file kill.c
The killring.
Works like the killring in emacs and readline. The killring is cut
and paste with a memory of previous cuts. It supports integration
with the X clipboard.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <algorithm>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "kill.h"
#include "proc.h"
#include "sanity.h"
#include "common.h"
#include "env.h"
#include "exec.h"
#include "path.h"
/**
Maximum entries in killring
*/
#define KILL_MAX 8192
/** Last kill string */
//static ll_node_t *kill_last=0;
/** Current kill string */
//static ll_node_t *kill_current=0;
/** Kill ring */
typedef std::list<wcstring> kill_list_t;
static kill_list_t kill_list;
/**
Contents of the X clipboard, at last time we checked it
*/
static wchar_t *cut_buffer=0;
/**
Test if the xsel command is installed. Since this is called often,
cache the result.
*/
static int has_xsel()
{
static int called=0;
static int res = 0;
if (!called) {
wchar_t *path = path_get_path( L"xsel" );
res = !!path;
free(path);
called = 1;
}
return res;
}
void kill_add( const wcstring &str )
{
ASSERT_IS_MAIN_THREAD();
if (str.empty())
return;
wcstring cmd;
wchar_t *escaped_str = NULL;
kill_list.push_front(str);
/*
Check to see if user has set the FISH_CLIPBOARD_CMD variable,
and, if so, use it instead of checking the display, etc.
I couldn't think of a safe way to allow overide of the echo
command too, so, the command used must accept the input via stdin.
*/
const env_var_t clipboard_wstr = env_get_string(L"FISH_CLIPBOARD_CMD");
if( !clipboard_wstr.missing() )
{
escaped_str = escape( str.c_str(), 1 );
cmd.assign(L"echo -n ");
cmd.append(escaped_str);
cmd.append(clipboard_wstr);
}
else
{
/* This is for sending the kill to the X copy-and-paste buffer */
if( !has_xsel() ) {
return;
}
const env_var_t disp_wstr = env_get_string( L"DISPLAY" );
if( !disp_wstr.missing() )
{
escaped_str = escape( str.c_str(), 1 );
cmd.assign(L"echo ");
cmd.append(escaped_str);
cmd.append(L"|xsel -b" );
}
}
if (! cmd.empty())
{
if( exec_subshell(cmd) == -1 )
{
/*
Do nothing on failiure
*/
}
free( cut_buffer );
cut_buffer = escaped_str;
}
}
/**
Remove first match for specified string from circular list
*/
static void kill_remove( const wcstring &s )
{
ASSERT_IS_MAIN_THREAD();
kill_list_t::iterator iter = std::find(kill_list.begin(), kill_list.end(), s);
if (iter != kill_list.end())
kill_list.erase(iter);
}
void kill_replace( const wcstring &old, const wcstring &newv )
{
kill_remove( old );
kill_add( newv );
}
const wchar_t *kill_yank_rotate()
{
ASSERT_IS_MAIN_THREAD();
// Move the first element to the end
if (kill_list.empty()) {
return NULL;
} else {
kill_list.splice(kill_list.end(), kill_list, kill_list.begin());
return kill_list.front().c_str();
}
}
/**
Check the X clipboard. If it has been changed, add the new
clipboard contents to the fish killring.
*/
static void kill_check_x_buffer()
{
if( !has_xsel() )
return;
const env_var_t disp = env_get_string(L"DISPLAY");
if( ! disp.missing())
{
size_t i;
wcstring cmd = L"xsel -t 500 -b";
wcstring new_cut_buffer=L"";
wcstring_list_t list;
if( exec_subshell( cmd, list ) != -1 )
{
for( i=0; i<list.size(); i++ )
{
wcstring next_line = escape_string( list.at(i), 0 );
if (i > 0) new_cut_buffer += L"\\n";
new_cut_buffer += next_line;
}
if( new_cut_buffer.size() > 0 )
{
/*
The buffer is inserted with backslash escapes,
since we don't really like tabs, newlines,
etc. anyway.
*/
if (cut_buffer == NULL || cut_buffer != new_cut_buffer)
{
free(cut_buffer);
cut_buffer = wcsdup(new_cut_buffer.c_str());
kill_list.push_front( new_cut_buffer );
}
}
}
}
}
const wchar_t *kill_yank()
{
kill_check_x_buffer();
if (kill_list.empty()) {
return L"";
} else {
return kill_list.front().c_str();
}
}
void kill_sanity_check()
{
}
void kill_init()
{
}
void kill_destroy()
{
if( cut_buffer )
free( cut_buffer );
}

37
kill.h
View File

@@ -12,32 +12,25 @@
/** /**
Replace the specified string in the killring Replace the specified string in the killring
*/ */
void kill_replace( wchar_t *old, wchar_t *new ); void kill_replace( const wcstring &old, const wcstring &newv );
/** /** Add a string to the top of the killring */
Add a string to the top of the killring void kill_add( const wcstring &str );
*/
void kill_add( wchar_t *str ); /** Rotate the killring */
/** const wchar_t *kill_yank_rotate();
Rotate the killring
*/ /** Paste from the killring */
wchar_t *kill_yank_rotate(); const wchar_t *kill_yank();
/**
Paste from the killring /** Sanity check */
*/
wchar_t *kill_yank();
/**
Sanity check
*/
void kill_sanity_check(); void kill_sanity_check();
/**
Initialize the killring /** Initialize the killring */
*/
void kill_init(); void kill_init();
/**
Destroy the killring /** Destroy the killring */
*/
void kill_destroy(); void kill_destroy();
#endif #endif

209
lru.h Normal file
View File

@@ -0,0 +1,209 @@
/** \file lru.h
Least-recently-used cache implementation
*/
#ifndef FISH_LRU_H
#define FISH_LRU_H
#include <wchar.h>
#include <map>
#include <set>
#include <list>
#include "common.h"
/** A predicate to compare dereferenced pointers */
struct dereference_less_t {
template <typename ptr_t>
bool operator()(ptr_t p1, ptr_t p2) const { return *p1 < *p2; }
};
class lru_node_t {
template<class T> friend class lru_cache_t;
/** Our linked list pointer */
lru_node_t *prev, *next;
public:
/** The key used to look up in the cache */
const wcstring key;
/** Constructor */
lru_node_t(const wcstring &pkey) : prev(NULL), next(NULL), key(pkey) { }
/** operator< for std::set */
bool operator<(const lru_node_t &other) const { return key < other.key; }
};
template<class node_type_t>
class lru_cache_t {
private:
/** Max node count */
const size_t max_node_count;
/** Count of nodes */
size_t node_count;
/** The set of nodes */
typedef std::set<lru_node_t *, dereference_less_t> node_set_t;
node_set_t node_set;
void promote_node(node_type_t *node) {
/* We should never promote the mouth */
assert(node != &mouth);
/* First unhook us */
node->prev->next = node->next;
node->next->prev = node->prev;
/* Put us after the mouth */
node->next = mouth.next;
node->next->prev = node;
node->prev = &mouth;
mouth.next = node;
}
void evict_node(node_type_t *condemned_node) {
/* We should never evict the mouth */
assert(condemned_node != NULL && condemned_node != &mouth);
/* Remove it from the linked list */
condemned_node->prev->next = condemned_node->next;
condemned_node->next->prev = condemned_node->prev;
/* Remove us from the set */
node_set.erase(condemned_node);
node_count--;
/* Tell ourselves */
this->node_was_evicted(condemned_node);
}
void evict_last_node(void) {
/* Simple */
evict_node((node_type_t *)mouth.prev);
}
protected:
/** Head of the linked list */
lru_node_t mouth;
/** Overridable callback for when a node is evicted */
virtual void node_was_evicted(node_type_t *node) { }
public:
/** Constructor */
lru_cache_t(size_t max_size = 1024 ) : max_node_count(max_size), node_count(0), mouth(wcstring()) {
/* Hook up the mouth to itself: a one node circularly linked list! */
mouth.prev = mouth.next = &mouth;
}
/** Note that we do not evict nodes in our destructor (even though they typically need to be deleted by their creator). */
virtual ~lru_cache_t() { }
/** Returns the node for a given key, or NULL */
node_type_t *get_node(const wcstring &key) {
node_type_t *result = NULL;
/* Construct a fake node as our key */
lru_node_t node_key(key);
/* Look for it in the set */
node_set_t::iterator iter = node_set.find(&node_key);
/* If we found a node, promote and return it */
if (iter != node_set.end()) {
result = static_cast<node_type_t*>(*iter);
promote_node(result);
}
return result;
}
/** Evicts the node for a given key, returning true if a node was evicted. */
bool evict_node(const wcstring &key) {
/* Construct a fake node as our key */
lru_node_t node_key(key);
/* Look for it in the set */
node_set_t::iterator iter = node_set.find(&node_key);
if (iter == node_set.end())
return false;
/* Evict the given node */
evict_node(static_cast<node_type_t*>(*iter));
return true;
}
/** Adds a node under the given key. Returns true if the node was added, false if the node was not because a node with that key is already in the set. */
bool add_node(node_type_t *node) {
/* Add our node without eviction */
if (! this->add_node_without_eviction(node))
return false;
/* Evict */
while (node_count > max_node_count)
evict_last_node();
/* Success */
return true;
}
/** Adds a node under the given key without triggering eviction. Returns true if the node was added, false if the node was not because a node with that key is already in the set. */
bool add_node_without_eviction(node_type_t *node) {
assert(node != NULL && node != &mouth);
/* Try inserting; return false if it was already in the set */
if (! node_set.insert(node).second)
return false;
/* Add the node after the mouth */
node->next = mouth.next;
node->next->prev = node;
node->prev = &mouth;
mouth.next = node;
/* Update the count */
node_count++;
/* Evict */
while (node_count > max_node_count)
evict_last_node();
/* Success */
return true;
}
/** Counts nodes */
size_t size(void) {
return node_count;
}
/** Evicts all nodes */
void evict_all_nodes(void) {
while (node_count > 0) {
evict_last_node();
}
}
/** Iterator for walking nodes, from least recently used to most */
class iterator {
lru_node_t *node;
public:
iterator(lru_node_t *val) : node(val) { }
void operator++() { node = node->prev; }
void operator++(int x) { node = node->prev; }
bool operator==(const iterator &other) { return node == other.node; }
bool operator!=(const iterator &other) { return !(*this == other); }
node_type_t *operator*() { return static_cast<node_type_t *>(node); }
};
iterator begin() { return iterator(mouth.prev); }
iterator end() { return iterator(&mouth); }
};
#endif

View File

@@ -1,7 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys import sys
import commands try:
import commands
except ImportError:
import subprocess
import re import re
# Regexes for performing cleanup # Regexes for performing cleanup
@@ -12,13 +15,13 @@ cl = { re.compile(r"-[ \t]*\n[ \t\r]+" ):"",
re.compile(r"[ \n\t\r]$"):"" } re.compile(r"[ \n\t\r]$"):"" }
def header(cmd): def header(cmd):
print '''# print('''#
# Command specific completions for the %s command. # Command specific completions for the {0} command.
# These completions where generated from the commands # These completions were generated from the commands
# man page by the make_completions.py script, but may # man page by the make_completions.py script, but may
# have been hand edited since. # have been hand edited since.
# #
''' % (cmd) '''.format(cmd))
def up_first(s): def up_first(s):
return s[0].upper() + s[1:] return s[0].upper() + s[1:]
@@ -57,13 +60,16 @@ def print_completion( cmd, switch_arr, arg, desc ):
res += " --description '%s'" % (up_first(escape_quotes(clean(desc)))) res += " --description '%s'" % (up_first(escape_quotes(clean(desc))))
print res print(res)
cmd = sys.argv[1] cmd = sys.argv[1]
header(cmd) header(cmd)
man = commands.getoutput( "man %s | col -b" % cmd ) try:
man = commands.getoutput( "man %s | col -b".format(cmd))
except NameError:
man = subprocess.getoutput( "man %s | col -b".format(cmd))
remainder = man remainder = man
@@ -152,7 +158,7 @@ for c in man:
desc += c desc += c
else: else:
print "Unknown mode", mode print("Unknown mode {0}".format(mode))
pc = c pc = c

View File

@@ -33,6 +33,10 @@ license. Read the source code of the library for more information.
#include <errno.h> #include <errno.h>
#include <regex.h> #include <regex.h>
#include <locale.h> #include <locale.h>
#include <vector>
#include <string>
#include <map>
#ifdef HAVE_GETOPT_H #ifdef HAVE_GETOPT_H
@@ -48,6 +52,7 @@ license. Read the source code of the library for more information.
#include "util.h" #include "util.h"
#include "print_help.h" #include "print_help.h"
typedef std::vector<std::string> string_list_t;
/** /**
Location of the applications .desktop file, relative to a base mime directory Location of the applications .desktop file, relative to a base mime directory
@@ -173,7 +178,7 @@ void *my_malloc( size_t s )
/** /**
Duplicate string, set error flag and print message on failure Duplicate string, set error flag and print message on failure
*/ */
char *my_strdup( char *s ) char *my_strdup( const char *s )
{ {
char *res = strdup( s ); char *res = strdup( s );
if( !s ) if( !s )
@@ -189,8 +194,9 @@ char *my_strdup( char *s )
Search the file \c filename for the first line starting with \c Search the file \c filename for the first line starting with \c
match, which is returned in a newly allocated string. match, which is returned in a newly allocated string.
*/ */
static char * search_ini( const char *filename, const char *match ) static const char * search_ini( const char *filename, const char *match )
{ {
/* OK to not use CLO_EXEC here because mimedb is single threaded */
FILE *f = fopen( filename, "r" ); FILE *f = fopen( filename, "r" );
char buf[4096]; char buf[4096];
int len=strlen(match); int len=strlen(match);
@@ -242,8 +248,8 @@ static char *file_exists( const char *dir, const char *in )
{ {
int dir_len = strlen( dir ); int dir_len = strlen( dir );
int need_sep = dir[dir_len - 1] != '/'; int need_sep = dir[dir_len - 1] != '/';
char *filename = my_malloc( dir_len + need_sep + strlen( in ) + 1 ); char *filename = (char *)my_malloc( dir_len + need_sep + strlen( in ) + 1 );
char *replaceme; char *replaceme;
struct stat buf; struct stat buf;
// fprintf( stderr, "Check %s%s\n", dir, in ); // fprintf( stderr, "Check %s%s\n", dir, in );
@@ -267,7 +273,7 @@ static char *file_exists( const char *dir, const char *in )
a slash in the name as a directory separator. We try to replace a slash in the name as a directory separator. We try to replace
a dash with a slash and try again. a dash with a slash and try again.
*/ */
replaceme = strchr( in, '-' ); replaceme = const_cast<char*>(strchr( in, '-' ));
if( replaceme ) if( replaceme )
{ {
char *res; char *res;
@@ -295,9 +301,9 @@ static char *file_exists( const char *dir, const char *in )
\param all If zero, then stop after the first filename. \param all If zero, then stop after the first filename.
\return The number of filenames added to the list. \return The number of filenames added to the list.
*/ */
static int append_filenames( array_list_t *list, char *f, int all ) static int append_filenames( string_list_t &list, const char *f, int all )
{ {
int prev_count = al_get_count( list ); size_t prev_count = list.size();
char *result; char *result;
const char *xdg_data_home; const char *xdg_data_home;
const char *xdg_data_dirs; const char *xdg_data_dirs;
@@ -307,9 +313,9 @@ static int append_filenames( array_list_t *list, char *f, int all )
if (xdg_data_home) if (xdg_data_home)
{ {
result = file_exists( xdg_data_home, f ); result = file_exists( xdg_data_home, f );
if ( result ) if (result)
{ {
al_push( list, result ); list.push_back(result);
if ( !all ) if ( !all )
return 1; return 1;
} }
@@ -323,7 +329,7 @@ static int append_filenames( array_list_t *list, char *f, int all )
{ {
char *guessed_xdg_home; char *guessed_xdg_home;
guessed_xdg_home = my_malloc (strlen (home) + strlen ("/.local/share") + 1); guessed_xdg_home = (char *)my_malloc (strlen (home) + strlen ("/.local/share") + 1);
if( !guessed_xdg_home ) if( !guessed_xdg_home )
return 0; return 0;
@@ -332,9 +338,9 @@ static int append_filenames( array_list_t *list, char *f, int all )
result = file_exists( guessed_xdg_home, f ); result = file_exists( guessed_xdg_home, f );
free (guessed_xdg_home); free (guessed_xdg_home);
if ( result ) if (result)
{ {
al_push( list, result ); list.push_back(result);
if ( !all ) if ( !all )
return 1; return 1;
} }
@@ -363,8 +369,8 @@ static int append_filenames( array_list_t *list, char *f, int all )
continue; continue;
} }
len = end_ptr - ptr; len = end_ptr - ptr;
dir = my_malloc (len + 1); dir = (char *)my_malloc (len + 1);
if( !dir ) if( !dir )
return 0; return 0;
@@ -374,9 +380,9 @@ static int append_filenames( array_list_t *list, char *f, int all )
free (dir); free (dir);
if ( result ) if (result)
{ {
al_push( list, result ); list.push_back(result);
if ( !all ) { if ( !all ) {
return 1; return 1;
} }
@@ -384,22 +390,22 @@ static int append_filenames( array_list_t *list, char *f, int all )
ptr = end_ptr; ptr = end_ptr;
} }
return al_get_count( list ) - prev_count; return list.size() - prev_count;
} }
/** /**
Find at most one file relative to the XDG data directories. Find at most one file relative to the XDG data directories; returns the empty string on failure
*/ */
static char *get_filename( char *f ) static std::string get_filename( char *f )
{ {
array_list_t list; string_list_t list;
char *first = NULL;
al_init( &list ); append_filenames( list, f, 0 );
append_filenames( &list, f, 0 ); if (list.empty()) {
first = al_pop( &list ); return "";
al_destroy( &list ); } else {
return first; return list.back();
}
} }
/** /**
@@ -409,7 +415,7 @@ static char *get_filename( char *f )
*/ */
static char *munge( char *in ) static char *munge( char *in )
{ {
char *out = my_malloc( strlen( in )+1 ); char *out = (char *)my_malloc( strlen( in )+1 );
char *p=out; char *p=out;
int had_whitespace = 0; int had_whitespace = 0;
int printed = 0; int printed = 0;
@@ -512,7 +518,7 @@ static char *get_description( const char *mimetype )
{ {
char *fn_part; char *fn_part;
char *fn; std::string fn;
int fd; int fd;
struct stat st; struct stat st;
char *contents; char *contents;
@@ -529,8 +535,8 @@ static char *get_description( const char *mimetype )
snprintf( buff, BUFF_SIZE, START_TAG, lang, lang ); snprintf( buff, BUFF_SIZE, START_TAG, lang, lang );
start_re = my_malloc( sizeof(regex_t)); start_re = (regex_t *)my_malloc( sizeof(regex_t));
stop_re = my_malloc( sizeof(regex_t)); stop_re = (regex_t *)my_malloc( sizeof(regex_t));
int reg_status; int reg_status;
if( ( reg_status = regcomp( start_re, buff, REG_EXTENDED ) ) ) if( ( reg_status = regcomp( start_re, buff, REG_EXTENDED ) ) )
@@ -560,7 +566,7 @@ static char *get_description( const char *mimetype )
} }
} }
fn_part = my_malloc( strlen(MIME_DIR) + strlen( mimetype) + strlen(MIME_SUFFIX) + 1 ); fn_part = (char *)my_malloc( strlen(MIME_DIR) + strlen( mimetype) + strlen(MIME_SUFFIX) + 1 );
if( !fn_part ) if( !fn_part )
{ {
@@ -574,12 +580,13 @@ static char *get_description( const char *mimetype )
fn = get_filename(fn_part); //malloc( strlen(MIME_DIR) +strlen( MIME_SUFFIX)+ strlen( mimetype ) + 1 ); fn = get_filename(fn_part); //malloc( strlen(MIME_DIR) +strlen( MIME_SUFFIX)+ strlen( mimetype ) + 1 );
free(fn_part ); free(fn_part );
if( !fn ) if( fn.empty() )
{ {
return 0; return 0;
} }
fd = open( fn, O_RDONLY ); /* OK to not use CLO_EXEC here because mimedb is single threaded */
fd = open( fn.c_str(), O_RDONLY );
// fprintf( stderr, "%s\n", fn ); // fprintf( stderr, "%s\n", fn );
@@ -590,14 +597,14 @@ static char *get_description( const char *mimetype )
return 0; return 0;
} }
if( stat( fn, &st) ) if( stat( fn.c_str(), &st) )
{ {
perror( "stat" ); perror( "stat" );
error=1; error=1;
return 0; return 0;
} }
contents = my_malloc( st.st_size + 1 ); contents = (char *)my_malloc( st.st_size + 1 );
if( !contents ) if( !contents )
{ {
return 0; return 0;
@@ -607,6 +614,7 @@ static char *get_description( const char *mimetype )
{ {
perror( "read" ); perror( "read" );
error=1; error=1;
free((void *)contents);
return 0; return 0;
} }
@@ -614,7 +622,6 @@ static char *get_description( const char *mimetype )
Don't need to check exit status of close on read-only file descriptors Don't need to check exit status of close on read-only file descriptors
*/ */
close( fd ); close( fd );
free( fn );
contents[st.st_size]=0; contents[st.st_size]=0;
regmatch_t match[1]; regmatch_t match[1];
@@ -673,32 +680,26 @@ static char *get_description( const char *mimetype )
static char *get_action( const char *mimetype ) static char *get_action( const char *mimetype )
{ {
char *res=0; char *res=0;
int i;
char *launcher; const char *launcher, *end;
char *end; string_list_t mime_filenames;
array_list_t mime_filenames;
char *launcher_str = NULL; const char *launcher_str = NULL;
char *launcher_filename, *launcher_command_str, *launcher_command; const char *launcher_command_str, *launcher_command;
char *launcher_full; char *launcher_full;
al_init( &mime_filenames ); if( !append_filenames( mime_filenames, DESKTOP_DEFAULT, 1 ) )
if( !append_filenames( &mime_filenames, DESKTOP_DEFAULT, 1 ) )
{ {
al_destroy( &mime_filenames );
return 0; return 0;
} }
for ( i = 0; i < al_get_count( &mime_filenames ); i++ ) for ( size_t i = 0; i < mime_filenames.size(); i++ )
{ {
launcher_str = search_ini( al_get( &mime_filenames, i ), mimetype ); launcher_str = search_ini( mime_filenames.at(i).c_str(), mimetype );
if ( launcher_str ) if ( launcher_str )
break; break;
} }
al_foreach( &mime_filenames, free );
al_destroy( &mime_filenames );
if( !launcher_str ) if( !launcher_str )
{ {
@@ -730,7 +731,7 @@ static char *get_action( const char *mimetype )
} }
// fprintf( stderr, "WOOT %s\n", launcher_str ); // fprintf( stderr, "WOOT %s\n", launcher_str );
launcher = strchr( launcher_str, '=' ); launcher = const_cast<char*>(strchr( launcher_str, '=' ));
if( !launcher ) if( !launcher )
{ {
@@ -742,45 +743,45 @@ static char *get_action( const char *mimetype )
/* Skip the = */ /* Skip the = */
launcher++; launcher++;
/* Make one we can change */
std::string mut_launcher = launcher;
/* Only use first launcher */ /* Only use first launcher */
end = strchr( launcher, ';' ); end = strchr( launcher, ';' );
if( end ) if( end )
*end = '\0'; mut_launcher.resize(end - launcher);
launcher_full = my_malloc( strlen( launcher) + strlen( APPLICATIONS_DIR)+1 ); launcher_full = (char *)my_malloc( mut_launcher.size() + strlen( APPLICATIONS_DIR)+1 );
if( !launcher_full ) if( !launcher_full )
{ {
free( launcher_str ); free( (void *)launcher_str );
return 0; return 0;
} }
strcpy( launcher_full, APPLICATIONS_DIR ); strcpy( launcher_full, APPLICATIONS_DIR );
strcat( launcher_full, launcher ); strcat( launcher_full, mut_launcher.c_str() );
free( launcher_str ); free( (void *)launcher_str );
launcher_filename = get_filename( launcher_full ); std::string launcher_filename = get_filename( launcher_full );
free( launcher_full ); free( launcher_full );
launcher_command_str = search_ini( launcher_filename, "Exec" ); launcher_command_str = search_ini( launcher_filename.c_str(), "Exec" );
if( !launcher_command_str ) if( !launcher_command_str )
{ {
fprintf( stderr, fprintf( stderr,
_( "%s: Default launcher '%s' does not specify how to start\n"), MIMEDB, _( "%s: Default launcher '%s' does not specify how to start\n"), MIMEDB,
launcher_filename ); launcher_filename.c_str() );
free( launcher_filename );
return 0; return 0;
} }
free( launcher_filename );
launcher_command = strchr( launcher_command_str, '=' ); launcher_command = strchr( launcher_command_str, '=' );
launcher_command++; launcher_command++;
res = my_strdup( launcher_command ); res = my_strdup( launcher_command );
free( launcher_command_str ); free( (void *)launcher_command_str );
return res; return res;
} }
@@ -797,7 +798,7 @@ static void writer( char c )
if( launch_len <= launch_pos ) if( launch_len <= launch_pos )
{ {
int new_len = launch_len?2*launch_len:256; int new_len = launch_len?2*launch_len:256;
char *new_buff = realloc( launch_buff, new_len ); char *new_buff = (char *)realloc( launch_buff, new_len );
if( !new_buff ) if( !new_buff )
{ {
free( launch_buff ); free( launch_buff );
@@ -846,9 +847,9 @@ static char *my_getcwd ()
/** /**
Return absolute filename of specified file Return absolute filename of specified file
*/ */
static char *get_fullfile( char *file ) static const char *get_fullfile( const char *file )
{ {
char *fullfile; const char *fullfile;
if( file[0] == '/' ) if( file[0] == '/' )
{ {
@@ -866,18 +867,19 @@ static char *get_fullfile( char *file )
int l = strlen(cwd); int l = strlen(cwd);
fullfile = my_malloc( l + strlen(file)+2 ); char *tmp = (char *)my_malloc( l + strlen(file)+2 );
if( !fullfile ) if( !tmp )
{ {
free(cwd); free(cwd);
return 0; return 0;
} }
strcpy( fullfile, cwd ); strcpy( tmp, cwd );
if( cwd[l-1] != '/' ) if( cwd[l-1] != '/' )
strcat(fullfile, "/" ); strcat(tmp, "/" );
strcat( fullfile, file ); strcat( tmp, file );
free(cwd); free(cwd);
fullfile = tmp;
} }
return fullfile; return fullfile;
} }
@@ -886,10 +888,10 @@ static char *get_fullfile( char *file )
/** /**
Write specified file as an URL Write specified file as an URL
*/ */
static void write_url( char *file ) static void write_url( const char *file )
{ {
char *fullfile = get_fullfile( file ); const char *fullfile = get_fullfile( file );
char *str = fullfile; const char *str = fullfile;
if( str == 0 ) if( str == 0 )
{ {
@@ -926,17 +928,17 @@ static void write_url( char *file )
str++; str++;
} }
if( fullfile != file ) if( fullfile != file )
free( fullfile ); free( (void *)fullfile );
} }
/** /**
Write specified file Write specified file
*/ */
static void write_file( char *file, int print_path ) static void write_file( const char *file, int print_path )
{ {
char *fullfile; const char *fullfile;
char *str; const char *str;
if( print_path ) if( print_path )
{ {
fullfile = get_fullfile( file ); fullfile = get_fullfile( file );
@@ -944,12 +946,13 @@ static void write_file( char *file, int print_path )
} }
else else
{ {
fullfile = my_strdup( file ); char *tmp = my_strdup( file );
if( !fullfile ) if( !tmp )
{ {
return; return;
} }
str = basename( fullfile ); str = basename( tmp );
fullfile = tmp;
} }
if( !str ) if( !str )
@@ -1020,7 +1023,7 @@ static void write_file( char *file, int print_path )
} }
if( fullfile != file ) if( fullfile != file )
free( fullfile ); free( (void *)fullfile );
} }
/** /**
@@ -1030,13 +1033,13 @@ static void write_file( char *file, int print_path )
\param files the list of files for which to perform the action \param files the list of files for which to perform the action
\param fileno an internal value. Should always be set to zero. \param fileno an internal value. Should always be set to zero.
*/ */
static void launch( char *filter, array_list_t *files, int fileno ) static void launch( char *filter, const string_list_t &files, int fileno )
{ {
char *filter_org=filter; char *filter_org=filter;
int count=0; int count=0;
int launch_again=0; int launch_again=0;
if( al_get_count( files ) <= fileno ) if( (int)files.size() <= fileno )
return; return;
@@ -1052,17 +1055,16 @@ static void launch( char *filter, array_list_t *files, int fileno )
case 'u': case 'u':
{ {
launch_again = 1; launch_again = 1;
write_url( (char *)al_get( files, fileno ) ); write_url( files.at(fileno).c_str() );
break; break;
} }
case 'U': case 'U':
{ {
int i; for( size_t i=0; i<files.size(); i++ )
for( i=0; i<al_get_count( files ); i++ )
{ {
if( i != 0 ) if( i != 0 )
writer( ' ' ); writer( ' ' );
write_url( (char *)al_get( files, i ) ); write_url( files.at(i).c_str() );
if( error ) if( error )
break; break;
} }
@@ -1074,19 +1076,18 @@ static void launch( char *filter, array_list_t *files, int fileno )
case 'n': case 'n':
{ {
launch_again = 1; launch_again = 1;
write_file( (char *)al_get( files, fileno ), *filter == 'f' ); write_file( files.at(fileno).c_str(), *filter == 'f' );
break; break;
} }
case 'F': case 'F':
case 'N': case 'N':
{ {
int i; for( size_t i=0; i<files.size(); i++ )
for( i=0; i<al_get_count( files ); i++ )
{ {
if( i != 0 ) if( i != 0 )
writer( ' ' ); writer( ' ' );
write_file( (char *)al_get( files, i ), *filter == 'F' ); write_file( files.at(i).c_str(), *filter == 'F' );
if( error ) if( error )
break; break;
} }
@@ -1096,52 +1097,52 @@ static void launch( char *filter, array_list_t *files, int fileno )
case 'd': case 'd':
{ {
char *cpy = get_fullfile( (char *)al_get( files, fileno ) ); const char *cpy = get_fullfile( files.at(fileno).c_str() );
char *dir; char *dir;
launch_again=1; launch_again=1;
/* /*
We wish to modify this string, make sure it is only a copy We wish to modify this string, make sure it is only a copy
*/ */
if( cpy == al_get( files, fileno ) ) if( cpy == files.at(fileno).c_str())
cpy = my_strdup( cpy ); cpy = my_strdup( cpy );
if( cpy == 0 ) if( cpy == 0 )
{ {
break; break;
} }
dir=dirname( cpy );
dir=dirname( (char *)cpy );
write_file( dir, 1 ); write_file( dir, 1 );
free( cpy ); free( (void *)cpy );
break; break;
} }
case 'D': case 'D':
{ {
int i; for( size_t i=0; i<files.size(); i++ )
for( i=0; i<al_get_count( files ); i++ )
{ {
char *cpy = get_fullfile( (char *)al_get( files, i ) ); const char *cpy = get_fullfile( files.at(i).c_str() );
char *dir; char *dir;
/* /*
We wish to modify this string, make sure it is only a copy We wish to modify this string, make sure it is only a copy
*/ */
if( cpy == al_get( files, i ) ) if( cpy == files.at(i).c_str() )
cpy = my_strdup( cpy ); cpy = my_strdup( cpy );
if( cpy == 0 ) if( cpy == 0 )
{ {
break; break;
} }
dir=dirname( cpy ); dir=dirname( (char *)cpy );
if( i != 0 ) if( i != 0 )
writer( ' ' ); writer( ' ' );
write_file( dir, 1 ); write_file( dir, 1 );
free( cpy ); free( (void *)cpy );
} }
break; break;
@@ -1199,25 +1200,6 @@ static void launch( char *filter, array_list_t *files, int fileno )
} }
/**
Clean up one entry from the hash table of launch files
*/
static void clear_entry( void *key, void *val )
{
/*
The key is a mime value, either from the libraries internal hash
table of mime types or from the command line. Either way, it
should not be freed.
The value is an array_list_t of filenames. The filenames com from
the argument list and should not be freed. The arraylist,
however, should be destroyed and freed.
*/
array_list_t *l = (array_list_t *)val;
al_destroy( l );
free( l );
}
/** /**
Do locale specific init Do locale specific init
*/ */
@@ -1242,7 +1224,8 @@ int main (int argc, char *argv[])
int i; int i;
hash_table_t launch_hash; typedef std::map<std::string, string_list_t> launch_hash_t;
launch_hash_t launch_hash;
locale_init(); locale_init();
@@ -1361,10 +1344,6 @@ int main (int argc, char *argv[])
exit(1); exit(1);
} }
if( output_type == LAUNCH )
hash_init( &launch_hash, &hash_str_func, &hash_str_cmp );
/* /*
Loop over all non option arguments and do the specified lookup Loop over all non option arguments and do the specified lookup
*/ */
@@ -1425,20 +1404,9 @@ int main (int argc, char *argv[])
them together after all the arguments have been them together after all the arguments have been
parsed. parsed.
*/ */
array_list_t *l= (array_list_t *)hash_get( &launch_hash, mimetype );
output = 0; output = 0;
string_list_t &l = launch_hash[mimetype];
if( !l ) l.push_back(argv[i]);
{
l = my_malloc( sizeof( array_list_t ) );
if( l == 0 )
{
break;
}
al_init( l );
hash_put( &launch_hash, mimetype, l );
}
al_push( l, argv[i] );
} }
} }
@@ -1459,20 +1427,10 @@ int main (int argc, char *argv[])
*/ */
if( output_type == LAUNCH && !error ) if( output_type == LAUNCH && !error )
{ {
int i; for( launch_hash_t::iterator iter = launch_hash.begin(); iter != launch_hash.end(); ++iter)
array_list_t mimes;
al_init( &mimes );
hash_get_keys( &launch_hash, &mimes );
for( i=0; i<al_get_count( &mimes ); i++ )
{ {
char *mimetype = (char *)al_get( &mimes, i ); const char *mimetype = iter->first.c_str();
array_list_t *files = (array_list_t *)hash_get( &launch_hash, mimetype ); string_list_t &files = iter->second;
if( !files )
{
fprintf( stderr, _( "%s: Unknown error\n"), MIMEDB );
error=1;
break;
}
char *launcher = get_action( mimetype ); char *launcher = get_action( mimetype );
@@ -1482,9 +1440,6 @@ int main (int argc, char *argv[])
free( launcher ); free( launcher );
} }
} }
hash_foreach( &launch_hash, &clear_entry );
hash_destroy( &launch_hash );
al_destroy( &mimes );
} }
if( launch_buff ) if( launch_buff )

600
output.c
View File

@@ -1,600 +0,0 @@
/** \file output.c
Generic output functions
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <sys/time.h>
#include <unistd.h>
#include <wctype.h>
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERMIO_H
#include <termio.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <wchar.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "expand.h"
#include "common.h"
#include "output.h"
#include "halloc_util.h"
#include "highlight.h"
/**
Number of color names in the col array
*/
#define COLORS (sizeof(col)/sizeof(wchar_t *))
static int writeb_internal( char c );
/**
Names of different colors.
*/
static wchar_t *col[]=
{
L"black",
L"red",
L"green",
L"brown",
L"yellow",
L"blue",
L"magenta",
L"purple",
L"cyan",
L"white"
L"normal"
}
;
/**
Mapping from color name (the 'col' array) to color index as used in
ANSI color terminals, and also the fish_color_* constants defined
in highlight.h. Non-ANSI terminals will display the wrong colors,
since they use a different mapping.
*/
static int col_idx[]=
{
0,
1,
2,
3,
3,
4,
5,
5,
6,
7,
FISH_COLOR_NORMAL,
}
;
/**
Size of writestr_buff
*/
static size_t writestr_buff_sz=0;
/**
Temp buffer used for converting from wide to narrow strings
*/
static char *writestr_buff = 0;
/**
The function used for output
*/
static int (*out)(char c) = &writeb_internal;
/**
Name of terminal
*/
static wchar_t *current_term = 0;
/**
Cleanup function. Run automatically through halloc
*/
static void output_destroy()
{
free( writestr_buff );
}
void output_set_writer( int (*writer)(char) )
{
CHECK( writer, );
out = writer;
}
int (*output_get_writer())(char)
{
return out;
}
void set_color( int c, int c2 )
{
static int last_color = FISH_COLOR_NORMAL;
static int last_color2 = FISH_COLOR_NORMAL;
static int was_bold=0;
static int was_underline=0;
int bg_set=0, last_bg_set=0;
char *fg = 0, *bg=0;
int is_bold = 0;
int is_underline = 0;
/*
Test if we have at least basic support for setting fonts, colors
and related bits - otherwise just give up...
*/
if( !exit_attribute_mode )
{
return;
}
is_bold |= (c&FISH_COLOR_BOLD)!=0;
is_bold |= (c2&FISH_COLOR_BOLD)!=0;
is_underline |= (c&FISH_COLOR_UNDERLINE)!=0;
is_underline |= (c2&FISH_COLOR_UNDERLINE)!=0;
c = c&(~(FISH_COLOR_BOLD|FISH_COLOR_UNDERLINE));
c2 = c2&(~(FISH_COLOR_BOLD|FISH_COLOR_UNDERLINE));
if( (set_a_foreground != 0) && (strlen( set_a_foreground) != 0 ) )
{
fg = set_a_foreground;
bg = set_a_background;
}
else if( (set_foreground != 0) && (strlen( set_foreground) != 0 ) )
{
fg = set_foreground;
bg = set_background;
}
if( (c == FISH_COLOR_RESET) || (c2 == FISH_COLOR_RESET))
{
c = c2 = FISH_COLOR_NORMAL;
was_bold=0;
was_underline=0;
if( fg )
{
/*
If we exit attibute mode, we must first set a color, or
previously coloured text might lose it's
color. Terminals are weird...
*/
writembs( tparm( fg, 0 ) );
}
writembs( exit_attribute_mode );
return;
}
if( was_bold && !is_bold )
{
/*
Only way to exit bold mode is a reset of all attributes.
*/
writembs( exit_attribute_mode );
last_color = FISH_COLOR_NORMAL;
last_color2 = FISH_COLOR_NORMAL;
was_bold=0;
was_underline=0;
}
if( last_color2 != FISH_COLOR_NORMAL &&
last_color2 != FISH_COLOR_RESET &&
last_color2 != FISH_COLOR_IGNORE )
{
/*
Background was set
*/
last_bg_set=1;
}
if( c2 != FISH_COLOR_NORMAL &&
c2 != FISH_COLOR_IGNORE )
{
/*
Background is set
*/
bg_set=1;
c = (c2==FISH_COLOR_WHITE)?FISH_COLOR_BLACK:FISH_COLOR_WHITE;
}
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
{
if(bg_set && !last_bg_set)
{
/*
Background color changed and is set, so we enter bold
mode to make reading easier. This means bold mode is
_always_ on when the background color is set.
*/
writembs( enter_bold_mode );
}
if(!bg_set && last_bg_set)
{
/*
Background color changed and is no longer set, so we
exit bold mode
*/
writembs( exit_attribute_mode );
was_bold=0;
was_underline=0;
/*
We don't know if exit_attribute_mode resets colors, so
we set it to something known.
*/
if( fg )
{
writembs( tparm( fg, 0 ) );
last_color=0;
}
}
}
if( last_color != c )
{
if( c==FISH_COLOR_NORMAL )
{
if( fg )
{
writembs( tparm( fg, 0 ) );
}
writembs( exit_attribute_mode );
last_color2 = FISH_COLOR_NORMAL;
was_bold=0;
was_underline=0;
}
else if( ( c >= 0 ) && ( c < FISH_COLOR_NORMAL ) )
{
if( fg )
{
writembs( tparm( fg, c ) );
}
}
}
last_color = c;
if( last_color2 != c2 )
{
if( c2 == FISH_COLOR_NORMAL )
{
if( bg )
{
writembs( tparm( bg, 0 ) );
}
writembs( exit_attribute_mode );
if( ( last_color != FISH_COLOR_NORMAL ) && fg )
{
if( fg )
{
writembs( tparm( fg, last_color ) );
}
}
was_bold=0;
was_underline=0;
last_color2 = c2;
}
else if ( ( c2 >= 0 ) && ( c2 < FISH_COLOR_NORMAL ) )
{
if( bg )
{
writembs( tparm( bg, c2 ) );
}
last_color2 = c2;
}
}
/*
Lastly, we set bold mode and underline mode correctly
*/
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set )
{
if( is_bold && !was_bold )
{
if( enter_bold_mode )
{
writembs( tparm( enter_bold_mode ) );
}
}
was_bold = is_bold;
}
if( was_underline && !is_underline )
{
writembs( exit_underline_mode );
}
if( !was_underline && is_underline )
{
writembs( enter_underline_mode );
}
was_underline = is_underline;
}
/**
Default output method, simply calls write() on stdout
*/
static int writeb_internal( char c )
{
write_loop( 1, &c, 1 );
return 0;
}
int writeb( tputs_arg_t b )
{
out( b );
return 0;
}
int writembs_internal( char *str )
{
CHECK( str, 1 );
return tputs(str,1,&writeb)==ERR?1:0;
}
int writech( wint_t ch )
{
mbstate_t state;
int i;
char buff[MB_LEN_MAX+1];
size_t bytes;
if( ( ch >= ENCODE_DIRECT_BASE) &&
( ch < ENCODE_DIRECT_BASE+256) )
{
buff[0] = ch - ENCODE_DIRECT_BASE;
bytes=1;
}
else
{
memset( &state, 0, sizeof(state) );
bytes= wcrtomb( buff, ch, &state );
switch( bytes )
{
case (size_t)(-1):
{
return 1;
}
}
}
for( i=0; i<bytes; i++ )
{
out( buff[i] );
}
return 0;
}
void writestr( const wchar_t *str )
{
char *pos;
CHECK( str, );
// while( *str )
// writech( *str++ );
/*
Check amount of needed space
*/
size_t len = wcstombs( 0, str, 0 );
if( len == (size_t)-1 )
{
debug( 1, L"Tried to print invalid wide character string" );
return;
}
len++;
/*
Reallocate if needed
*/
if( writestr_buff_sz < len )
{
if( !writestr_buff )
{
halloc_register_function_void( global_context, &output_destroy );
}
writestr_buff = realloc( writestr_buff, len );
if( !writestr_buff )
{
DIE_MEM();
}
writestr_buff_sz = len;
}
/*
Convert
*/
wcstombs( writestr_buff,
str,
writestr_buff_sz );
/*
Write
*/
for( pos = writestr_buff; *pos; pos++ )
{
out( *pos );
}
}
void writestr_ellipsis( const wchar_t *str, int max_width )
{
int written=0;
int tot;
CHECK( str, );
tot = my_wcswidth(str);
if( tot <= max_width )
{
writestr( str );
return;
}
while( *str != 0 )
{
int w = wcwidth( *str );
if( written+w+wcwidth( ellipsis_char )>max_width )
{
break;
}
written+=w;
writech( *(str++) );
}
written += wcwidth( ellipsis_char );
writech( ellipsis_char );
while( written < max_width )
{
written++;
writestr( L" " );
}
}
int write_escaped_str( const wchar_t *str, int max_len )
{
wchar_t *out;
int i;
int len;
int written=0;
CHECK( str, 0 );
out = escape( str, 1 );
len = my_wcswidth( out );
if( max_len && (max_len < len))
{
for( i=0; (written+wcwidth(out[i]))<=(max_len-1); i++ )
{
writech( out[i] );
written += wcwidth( out[i] );
}
writech( ellipsis_char );
written += wcwidth( ellipsis_char );
for( i=written; i<max_len; i++ )
{
writech( L' ' );
written++;
}
}
else
{
written = len;
writestr( out );
}
free( out );
return written;
}
int output_color_code( const wchar_t *val )
{
int j, i, color=FISH_COLOR_NORMAL;
array_list_t el;
int is_bold=0;
int is_underline=0;
if( !val )
return FISH_COLOR_NORMAL;
al_init( &el );
tokenize_variable_array( val, &el );
for( j=0; j<al_get_count( &el ); j++ )
{
wchar_t *next = (wchar_t *)al_get( &el, j );
is_bold |= (wcsncmp( next, L"--bold", wcslen(next) ) == 0 ) && wcslen(next)>=3;
is_bold |= wcscmp( next, L"-o" ) == 0;
is_underline |= (wcsncmp( next, L"--underline", wcslen(next) ) == 0 ) && wcslen(next)>=3;
is_underline |= wcscmp( next, L"-u" ) == 0;
for( i=0; i<COLORS; i++ )
{
if( wcscasecmp( col[i], next ) == 0 )
{
color = col_idx[i];
break;
}
}
}
al_foreach( &el, &free );
al_destroy( &el );
return color | (is_bold?FISH_COLOR_BOLD:0) | (is_underline?FISH_COLOR_UNDERLINE:0);
}
void output_set_term( wchar_t *term )
{
current_term = halloc_wcsdup(global_context, term);
}
wchar_t *output_get_term()
{
return current_term ? current_term : L"<unknown>";
}

690
output.cpp Normal file
View File

@@ -0,0 +1,690 @@
/** \file output.c
Generic output functions
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_TERMIOS_H
#include <sys/termios.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#include <sys/time.h>
#include <unistd.h>
#include <wctype.h>
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERMIO_H
#include <termio.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <signal.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <wchar.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "expand.h"
#include "common.h"
#include "output.h"
#include "highlight.h"
#include "env.h"
/**
Number of color names in the col array
*/
#define COLORS (sizeof(col)/sizeof(wchar_t *))
static int writeb_internal( char c );
/**
Names of different colors.
*/
static const wchar_t *col[]=
{
L"black",
L"red",
L"green",
L"brown",
L"yellow",
L"blue",
L"magenta",
L"purple",
L"cyan",
L"white"
L"normal"
}
;
/**
Mapping from color name (the 'col' array) to color index as used in
ANSI color terminals, and also the fish_color_* constants defined
in highlight.h. Non-ANSI terminals will display the wrong colors,
since they use a different mapping.
*/
static const int col_idx[]=
{
0,
1,
2,
3,
3,
4,
5,
5,
6,
7,
FISH_COLOR_NORMAL,
};
/**
The function used for output
*/
static int (*out)(char c) = &writeb_internal;
/**
Name of terminal
*/
static wcstring current_term;
/* Whether term256 is supported */
static bool support_term256 = false;
void output_set_writer( int (*writer)(char) )
{
CHECK( writer, );
out = writer;
}
int (*output_get_writer())(char)
{
return out;
}
static bool term256_support_is_native(void) {
/* Return YES if we think the term256 support is "native" as opposed to forced. */
return max_colors == 256;
}
bool output_get_supports_term256() {
return support_term256;
}
void output_set_supports_term256(bool val) {
support_term256 = val;
}
static unsigned char index_for_color(rgb_color_t c) {
if (c.is_named() || ! output_get_supports_term256()) {
return c.to_name_index();
} else {
return c.to_term256_index();
}
}
static bool write_color(char *todo, unsigned char idx, bool is_fg) {
bool result = false;
if (idx < 16 || term256_support_is_native()) {
/* Use tparm */
writembs( tparm( todo, idx ) );
result = true;
} else {
/* We are attempting to bypass the term here. Generate the ANSI escape sequence ourself. */
char stridx[128];
format_long_safe(stridx, (long)idx);
char buff[128] = "\x1b[";
strcat(buff, is_fg ? "38;5;" : "48;5;");
strcat(buff, stridx);
strcat(buff, "m");
int (*writer)(char) = output_get_writer();
if (writer) {
for (size_t i=0; buff[i]; i++) {
writer(buff[i]);
}
}
result = true;
}
return result;
}
static bool write_foreground_color(unsigned char idx) {
if (set_a_foreground && set_a_foreground[0]) {
return write_color(set_a_foreground, idx, true);
} else if (set_foreground && set_foreground[0]) {
return write_color(set_foreground, idx, true);
} else {
return false;
}
}
static bool write_background_color(unsigned char idx) {
if (set_a_background && set_a_background[0]) {
return write_color(set_a_background, idx, false);
} else if (set_background && set_background[0]) {
return write_color(set_background, idx, false);
} else {
return false;
}
}
void set_color(rgb_color_t c, rgb_color_t c2)
{
#if 0
wcstring tmp = c.description();
wcstring tmp2 = c2.description();
printf("set_color %ls : %ls\n", tmp.c_str(), tmp2.c_str());
#endif
ASSERT_IS_MAIN_THREAD();
const rgb_color_t normal = rgb_color_t::normal();
static rgb_color_t last_color = rgb_color_t::normal();
static rgb_color_t last_color2 = rgb_color_t::normal();
static int was_bold=0;
static int was_underline=0;
int bg_set=0, last_bg_set=0;
int is_bold = 0;
int is_underline = 0;
/*
Test if we have at least basic support for setting fonts, colors
and related bits - otherwise just give up...
*/
if( !exit_attribute_mode )
{
return;
}
is_bold |= c.is_bold();
is_bold |= c2.is_bold();
is_underline |= c.is_underline();
is_underline |= c2.is_underline();
if( c.is_reset() || c2.is_reset())
{
c = c2 = normal;
was_bold=0;
was_underline=0;
/*
If we exit attibute mode, we must first set a color, or
previously coloured text might lose it's
color. Terminals are weird...
*/
write_foreground_color(0);
writembs( exit_attribute_mode );
return;
}
if( was_bold && !is_bold )
{
/*
Only way to exit bold mode is a reset of all attributes.
*/
writembs( exit_attribute_mode );
last_color = normal;
last_color2 = normal;
was_bold=0;
was_underline=0;
}
if( ! last_color2.is_normal() &&
! last_color2.is_reset() &&
! last_color2.is_ignore() )
{
/*
Background was set
*/
last_bg_set=1;
}
if( ! c2.is_normal() &&
! c2.is_ignore())
{
/*
Background is set
*/
bg_set=1;
c = (c2==rgb_color_t::white())?rgb_color_t::black():rgb_color_t::white();
}
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0))
{
if(bg_set && !last_bg_set)
{
/*
Background color changed and is set, so we enter bold
mode to make reading easier. This means bold mode is
_always_ on when the background color is set.
*/
writembs( enter_bold_mode );
}
if(!bg_set && last_bg_set)
{
/*
Background color changed and is no longer set, so we
exit bold mode
*/
writembs( exit_attribute_mode );
was_bold=0;
was_underline=0;
/*
We don't know if exit_attribute_mode resets colors, so
we set it to something known.
*/
if( write_foreground_color(0))
{
last_color=rgb_color_t::black();
}
}
}
if( last_color != c )
{
if( c.is_normal() )
{
write_foreground_color(0);
writembs( exit_attribute_mode );
last_color2 = rgb_color_t::normal();
was_bold=0;
was_underline=0;
}
else if( ! c.is_special() )
{
write_foreground_color(index_for_color(c));
}
}
last_color = c;
if( last_color2 != c2 )
{
if( c2.is_normal() )
{
write_background_color(0);
writembs( exit_attribute_mode );
if( ! last_color.is_normal())
{
write_foreground_color(index_for_color(last_color));
}
was_bold=0;
was_underline=0;
last_color2 = c2;
}
else if ( ! c2.is_special() )
{
write_background_color(index_for_color(c2));
last_color2 = c2;
}
}
/*
Lastly, we set bold mode and underline mode correctly
*/
if( (enter_bold_mode != 0) && (strlen(enter_bold_mode) > 0) && !bg_set )
{
if( is_bold && !was_bold )
{
if( enter_bold_mode )
{
writembs( tparm( enter_bold_mode ) );
}
}
was_bold = is_bold;
}
if( was_underline && !is_underline )
{
writembs( exit_underline_mode );
}
if( !was_underline && is_underline )
{
writembs( enter_underline_mode );
}
was_underline = is_underline;
}
/**
Default output method, simply calls write() on stdout
*/
static int writeb_internal( char c )
{
write_loop( 1, &c, 1 );
return 0;
}
int writeb( tputs_arg_t b )
{
out( b );
return 0;
}
int writembs_internal( char *str )
{
CHECK( str, 1 );
return tputs(str,1,&writeb)==ERR?1:0;
}
int writech( wint_t ch )
{
mbstate_t state;
size_t i;
char buff[MB_LEN_MAX+1];
size_t bytes;
if( ( ch >= ENCODE_DIRECT_BASE) &&
( ch < ENCODE_DIRECT_BASE+256) )
{
buff[0] = ch - ENCODE_DIRECT_BASE;
bytes=1;
}
else
{
memset( &state, 0, sizeof(state) );
bytes= wcrtomb( buff, ch, &state );
switch( bytes )
{
case (size_t)(-1):
{
return 1;
}
}
}
for( i=0; i<bytes; i++ )
{
out( buff[i] );
}
return 0;
}
void writestr( const wchar_t *str )
{
char *pos;
CHECK( str, );
// while( *str )
// writech( *str++ );
/*
Check amount of needed space
*/
size_t len = wcstombs( 0, str, 0 );
if( len == (size_t)-1 )
{
debug( 1, L"Tried to print invalid wide character string" );
return;
}
len++;
/*
Convert
*/
char *buffer, static_buffer[256];
if (len <= sizeof static_buffer)
buffer = static_buffer;
else
buffer = new char[len];
wcstombs( buffer,
str,
len );
/*
Write
*/
for( pos = buffer; *pos; pos++ )
{
out( *pos );
}
if (buffer != static_buffer)
delete[] buffer;
}
void writestr_ellipsis( const wchar_t *str, int max_width )
{
int written=0;
int tot;
CHECK( str, );
tot = my_wcswidth(str);
if( tot <= max_width )
{
writestr( str );
return;
}
while( *str != 0 )
{
int w = wcwidth( *str );
if( written+w+wcwidth( ellipsis_char )>max_width )
{
break;
}
written+=w;
writech( *(str++) );
}
written += wcwidth( ellipsis_char );
writech( ellipsis_char );
while( written < max_width )
{
written++;
writestr( L" " );
}
}
int write_escaped_str( const wchar_t *str, int max_len )
{
wchar_t *out;
int i;
int len;
int written=0;
CHECK( str, 0 );
out = escape( str, 1 );
len = my_wcswidth( out );
if( max_len && (max_len < len))
{
for( i=0; (written+wcwidth(out[i]))<=(max_len-1); i++ )
{
writech( out[i] );
written += wcwidth( out[i] );
}
writech( ellipsis_char );
written += wcwidth( ellipsis_char );
for( i=written; i<max_len; i++ )
{
writech( L' ' );
written++;
}
}
else
{
written = len;
writestr( out );
}
free( out );
return written;
}
int output_color_code( const wcstring &val, bool is_background ) {
size_t i;
int color=FISH_COLOR_NORMAL;
int is_bold=0;
int is_underline=0;
if (val.empty())
return FISH_COLOR_NORMAL;
wcstring_list_t el;
tokenize_variable_array( val, el );
for(size_t j=0; j < el.size(); j++ ) {
const wcstring &next = el.at(j);
wcstring color_name;
if (is_background) {
// look for something like "--background=red"
const wcstring prefix = L"--background=";
if (string_prefixes_string(prefix, next)) {
color_name = wcstring(next, prefix.size());
}
} else {
if (next == L"--bold" || next == L"-o")
is_bold = true;
else if (next == L"--underline" || next == L"-u")
is_underline = true;
else
color_name = next;
}
if (! color_name.empty()) {
for( i=0; i<COLORS; i++ )
{
if( wcscasecmp( col[i], color_name.c_str() ) == 0 )
{
color = col_idx[i];
break;
}
}
}
}
return color | (is_bold?FISH_COLOR_BOLD:0) | (is_underline?FISH_COLOR_UNDERLINE:0);
}
rgb_color_t parse_color( const wcstring &val, bool is_background ) {
int is_bold=0;
int is_underline=0;
std::vector<rgb_color_t> candidates;
wcstring_list_t el;
tokenize_variable_array( val, el );
for(size_t j=0; j < el.size(); j++ ) {
const wcstring &next = el.at(j);
wcstring color_name;
if (is_background) {
// look for something like "--background=red"
const wcstring prefix = L"--background=";
if (string_prefixes_string(prefix, next)) {
color_name = wcstring(next, prefix.size());
}
} else {
if (next == L"--bold" || next == L"-o")
is_bold = true;
else if (next == L"--underline" || next == L"-u")
is_underline = true;
else
color_name = next;
}
if (! color_name.empty()) {
rgb_color_t color = rgb_color_t(color_name);
if (! color.is_none()) {
candidates.push_back(color);
}
}
}
// Pick the best candidate
rgb_color_t first_rgb = rgb_color_t::none(), first_named = rgb_color_t::none();
for (size_t i=0; i < candidates.size(); i++) {
const rgb_color_t &color = candidates.at(i);
if (color.is_rgb() && first_rgb.is_none())
first_rgb = color;
if (color.is_named() && first_named.is_none())
first_named = color;
}
// If we have both RGB and named colors, then prefer rgb if term256 is supported
rgb_color_t result;
if ((!first_rgb.is_none() && output_get_supports_term256()) || first_named.is_none()) {
result = first_rgb;
} else {
result = first_named;
}
if (result.is_none())
result = rgb_color_t::normal();
result.set_bold(is_bold);
result.set_underline(is_underline);
#if 0
wcstring desc = result.description();
printf("Parsed %ls from %ls (%s)\n", desc.c_str(), val.c_str(), is_background ? "background" : "foreground");
#endif
return result;
}
void output_set_term( const wchar_t *term )
{
current_term = term;
}
const wchar_t *output_get_term()
{
return current_term.empty() ? L"<unknown>" : current_term.c_str();
}

View File

@@ -9,6 +9,8 @@
#define FISH_OUTPUT_H #define FISH_OUTPUT_H
#include <wchar.h> #include <wchar.h>
#include "screen.h"
#include "color.h"
/** /**
Constants for various colors as used by the set_color function. Constants for various colors as used by the set_color function.
@@ -71,7 +73,7 @@ enum
*/ */
void set_color( int c, int c2 ); void set_color(rgb_color_t c, rgb_color_t c2);
/** /**
Write specified multibyte string Write specified multibyte string
@@ -128,7 +130,8 @@ int write_escaped_str( const wchar_t *str, int max_len );
/** /**
Return the internal color code representing the specified color Return the internal color code representing the specified color
*/ */
int output_color_code( const wchar_t *val ); int output_color_code( const wcstring &val, bool is_background );
rgb_color_t parse_color( const wcstring &val, bool is_background );
/** /**
This is for writing process notification messages. Has to write to This is for writing process notification messages. Has to write to
@@ -145,19 +148,19 @@ int writeb( tputs_arg_t b );
*/ */
void output_set_writer( int (*writer)(char) ); void output_set_writer( int (*writer)(char) );
//typedef int (*func_ptr_t)(char);
/** /**
Return the current output writer Return the current output writer
*/ */
int (*output_get_writer())(char) ; int (*output_get_writer())(char) ;
/** /** Set the terminal name */
Set the terminal name void output_set_term( const wchar_t *term );
*/
void output_set_term( wchar_t *term ); /** Return the terminal name */
/** const wchar_t *output_get_term();
Return the terminal name
*/ /** Sets whether term256 colors are supported */
wchar_t *output_get_term(); bool output_get_supports_term256();
void output_set_supports_term256(bool val);
#endif #endif

File diff suppressed because it is too large Load Diff

693
parse_util.cpp Normal file
View File

@@ -0,0 +1,693 @@
/** \file parse_util.c
Various mostly unrelated utility functions related to parsing,
loading and evaluating fish code.
This library can be seen as a 'toolbox' for functions that are
used in many places in fish and that are somehow related to
parsing the code.
*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <wctype.h>
#include <wchar.h>
#include <map>
#include <set>
#include <algorithm>
#include <time.h>
#include <assert.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "common.h"
#include "tokenizer.h"
#include "parse_util.h"
#include "expand.h"
#include "intern.h"
#include "exec.h"
#include "env.h"
#include "signal.h"
#include "wildcard.h"
/**
Maximum number of autoloaded items opf a specific type to keep in
memory at a time.
*/
#define AUTOLOAD_MAX 10
/**
Minimum time, in seconds, before an autoloaded item will be
unloaded
*/
#define AUTOLOAD_MIN_AGE 60
int parse_util_lineno( const wchar_t *str, int len )
{
/**
First cached state
*/
static wchar_t *prev_str = 0;
static int i=0;
static int res = 1;
/**
Second cached state
*/
static wchar_t *prev_str2 = 0;
static int i2 = 0;
static int res2 = 1;
CHECK( str, 0 );
if( str != prev_str || i>len )
{
if( prev_str2 == str && i2 <= len )
{
wchar_t *tmp_str = prev_str;
int tmp_i = i;
int tmp_res = res;
prev_str = prev_str2;
i=i2;
res=res2;
prev_str2 = tmp_str;
i2 = tmp_i;
res2 = tmp_res;
}
else
{
prev_str2 = prev_str;
i2 = i;
res2=res;
prev_str = (wchar_t *)str;
i=0;
res=1;
}
}
for( ; str[i] && i<len; i++ )
{
if( str[i] == L'\n' )
{
res++;
}
}
return res;
}
int parse_util_get_line_from_offset( const wcstring &str, int pos )
{
// return parse_util_lineno( buff, pos );
const wchar_t *buff = str.c_str();
int i;
int count = 0;
if( pos < 0 )
{
return -1;
}
for( i=0; i<pos; i++ )
{
if( !buff[i] )
{
return -1;
}
if( buff[i] == L'\n' )
{
count++;
}
}
return count;
}
int parse_util_get_offset_from_line( const wcstring &str, int line )
{
const wchar_t *buff = str.c_str();
int i;
int count = 0;
if( line < 0 )
{
return -1;
}
if( line == 0 )
return 0;
for( i=0;; i++ )
{
if( !buff[i] )
{
return -1;
}
if( buff[i] == L'\n' )
{
count++;
if( count == line )
{
return i+1;
}
}
}
}
int parse_util_get_offset( const wcstring &str, int line, int line_offset )
{
const wchar_t *buff = str.c_str();
int off = parse_util_get_offset_from_line( buff, line );
int off2 = parse_util_get_offset_from_line( buff, line+1 );
int line_offset2 = line_offset;
if( off < 0 )
{
return -1;
}
if( off2 < 0 )
{
off2 = wcslen( buff )+1;
}
if( line_offset2 < 0 )
{
line_offset2 = 0;
}
if( line_offset2 >= off2-off-1 )
{
line_offset2 = off2-off-1;
}
return off + line_offset2;
}
int parse_util_locate_cmdsubst( const wchar_t *in,
wchar_t **begin,
wchar_t **end,
int allow_incomplete )
{
wchar_t *pos;
wchar_t prev=0;
int syntax_error=0;
int paran_count=0;
wchar_t *paran_begin=0, *paran_end=0;
CHECK( in, 0 );
for( pos = (wchar_t *)in; *pos; pos++ )
{
if( prev != '\\' )
{
if( wcschr( L"\'\"", *pos ) )
{
wchar_t *q_end = quote_end( pos );
if( q_end && *q_end)
{
pos=q_end;
}
else
{
break;
}
}
else
{
if( *pos == '(' )
{
if(( paran_count == 0)&&(paran_begin==0))
{
paran_begin = pos;
}
paran_count++;
}
else if( *pos == ')' )
{
paran_count--;
if( (paran_count == 0) && (paran_end == 0) )
{
paran_end = pos;
break;
}
if( paran_count < 0 )
{
syntax_error = 1;
break;
}
}
}
}
prev = *pos;
}
syntax_error |= (paran_count < 0 );
syntax_error |= ((paran_count>0)&&(!allow_incomplete));
if( syntax_error )
{
return -1;
}
if( paran_begin == 0 )
{
return 0;
}
if( begin )
{
*begin = paran_begin;
}
if( end )
{
*end = paran_count?(wchar_t *)in+wcslen(in):paran_end;
}
return 1;
}
void parse_util_cmdsubst_extent( const wchar_t *buff,
int cursor_pos,
const wchar_t **a,
const wchar_t **b )
{
wchar_t *begin, *end;
wchar_t *pos;
const wchar_t *cursor = buff + cursor_pos;
CHECK( buff, );
if( a )
{
*a = (wchar_t *)buff;
}
if( b )
{
*b = (wchar_t *)buff+wcslen(buff);
}
pos = (wchar_t *)buff;
while( 1 )
{
if( parse_util_locate_cmdsubst( pos,
&begin,
&end,
1 ) <= 0)
{
/*
No subshell found
*/
break;
}
if( !end )
{
end = (wchar_t *)buff + wcslen(buff);
}
if(( begin < cursor ) && (end >= cursor) )
{
begin++;
if( a )
{
*a = begin;
}
if( b )
{
*b = end;
}
break;
}
if( !*end )
{
break;
}
pos = end+1;
}
}
/**
Get the beginning and end of the job or process definition under the cursor
*/
static void job_or_process_extent( const wchar_t *buff,
int cursor_pos,
const wchar_t **a,
const wchar_t **b,
int process )
{
const wchar_t *begin, *end;
int pos;
wchar_t *buffcpy;
int finished=0;
tokenizer tok;
CHECK( buff, );
if( a )
{
*a=0;
}
if( b )
{
*b = 0;
}
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
if( !end || !begin )
{
return;
}
pos = cursor_pos - (begin - buff);
if( a )
{
*a = begin;
}
if( b )
{
*b = end;
}
buffcpy = wcsndup( begin, end-begin );
if( !buffcpy )
{
DIE_MEM();
}
for( tok_init( &tok, buffcpy, TOK_ACCEPT_UNFINISHED );
tok_has_next( &tok ) && !finished;
tok_next( &tok ) )
{
int tok_begin = tok_get_pos( &tok );
switch( tok_last_type( &tok ) )
{
case TOK_PIPE:
{
if( !process )
{
break;
}
}
case TOK_END:
case TOK_BACKGROUND:
{
if( tok_begin >= pos )
{
finished=1;
if( b )
{
*b = (wchar_t *)buff + tok_begin;
}
}
else
{
if( a )
{
*a = (wchar_t *)buff + tok_begin+1;
}
}
break;
}
}
}
free( buffcpy);
tok_destroy( &tok );
}
void parse_util_process_extent( const wchar_t *buff,
int pos,
const wchar_t **a,
const wchar_t **b )
{
job_or_process_extent( buff, pos, a, b, 1 );
}
void parse_util_job_extent( const wchar_t *buff,
int pos,
const wchar_t **a,
const wchar_t **b )
{
job_or_process_extent( buff,pos,a, b, 0 );
}
void parse_util_token_extent( const wchar_t *buff,
int cursor_pos,
const wchar_t **tok_begin,
const wchar_t **tok_end,
const wchar_t **prev_begin,
const wchar_t **prev_end )
{
const wchar_t *begin, *end;
int pos;
wchar_t *buffcpy;
tokenizer tok;
const wchar_t *a = NULL, *b = NULL, *pa = NULL, *pb = NULL;
CHECK( buff, );
assert( cursor_pos >= 0 );
parse_util_cmdsubst_extent( buff, cursor_pos, &begin, &end );
if( !end || !begin )
{
return;
}
pos = cursor_pos - (begin - buff);
a = buff + pos;
b = a;
pa = buff + pos;
pb = pa;
assert( begin >= buff );
assert( begin <= (buff+wcslen(buff) ) );
assert( end >= begin );
assert( end <= (buff+wcslen(buff) ) );
buffcpy = wcsndup( begin, end-begin );
if( !buffcpy )
{
DIE_MEM();
}
for( tok_init( &tok, buffcpy, TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS );
tok_has_next( &tok );
tok_next( &tok ) )
{
int tok_begin = tok_get_pos( &tok );
int tok_end=tok_begin;
/*
Calculate end of token
*/
if( tok_last_type( &tok ) == TOK_STRING )
{
tok_end +=wcslen(tok_last(&tok));
}
/*
Cursor was before beginning of this token, means that the
cursor is between two tokens, so we set it to a zero element
string and break
*/
if( tok_begin > pos )
{
a = b = (wchar_t *)buff + pos;
break;
}
/*
If cursor is inside the token, this is the token we are
looking for. If so, set a and b and break
*/
if( (tok_last_type( &tok ) == TOK_STRING) && (tok_end >= pos ) )
{
a = begin + tok_get_pos( &tok );
b = a + wcslen(tok_last(&tok));
break;
}
/*
Remember previous string token
*/
if( tok_last_type( &tok ) == TOK_STRING )
{
pa = begin + tok_get_pos( &tok );
pb = pa + wcslen(tok_last(&tok));
}
}
free( buffcpy);
tok_destroy( &tok );
if( tok_begin )
{
*tok_begin = a;
}
if( tok_end )
{
*tok_end = b;
}
if( prev_begin )
{
*prev_begin = pa;
}
if( prev_end )
{
*prev_end = pb;
}
assert( pa >= buff );
assert( pa <= (buff+wcslen(buff) ) );
assert( pb >= pa );
assert( pb <= (buff+wcslen(buff) ) );
}
void parse_util_set_argv( const wchar_t * const *argv, const wcstring_list_t &named_arguments )
{
if( *argv )
{
const wchar_t * const *arg;
wcstring sb;
for( arg=argv; *arg; arg++ )
{
if( arg != argv )
{
sb.append(ARRAY_SEP_STR);
}
sb.append(*arg);
}
env_set( L"argv", sb.c_str(), ENV_LOCAL );
}
else
{
env_set( L"argv", 0, ENV_LOCAL );
}
if( named_arguments.size() )
{
const wchar_t * const *arg;
size_t i;
for( i=0, arg=argv; i < named_arguments.size(); i++ )
{
env_set( named_arguments.at(i).c_str(), *arg, ENV_LOCAL );
if( *arg )
arg++;
}
}
}
wchar_t *parse_util_unescape_wildcards( const wchar_t *str )
{
wchar_t *in, *out;
wchar_t *unescaped;
CHECK( str, 0 );
unescaped = wcsdup(str);
if( !unescaped )
{
DIE_MEM();
}
for( in=out=unescaped; *in; in++ )
{
switch( *in )
{
case L'\\':
{
if( *(in+1) )
{
in++;
*(out++)=*in;
}
*(out++)=*in;
break;
}
case L'*':
{
*(out++)=ANY_STRING;
break;
}
case L'?':
{
*(out++)=ANY_CHAR;
break;
}
default:
{
*(out++)=*in;
break;
}
}
}
return unescaped;
}

View File

@@ -7,7 +7,10 @@
#ifndef FISH_PARSE_UTIL_H #ifndef FISH_PARSE_UTIL_H
#define FISH_PARSE_UTIL_H #define FISH_PARSE_UTIL_H
#include "autoload.h"
#include <wchar.h> #include <wchar.h>
#include <map>
#include <set>
/** /**
Find the beginning and end of the first subshell in the specified string. Find the beginning and end of the first subshell in the specified string.
@@ -38,8 +41,8 @@ int parse_util_locate_cmdsubst( const wchar_t *in,
*/ */
void parse_util_cmdsubst_extent( const wchar_t *buff, void parse_util_cmdsubst_extent( const wchar_t *buff,
int cursor_pos, int cursor_pos,
wchar_t **a, const wchar_t **a,
wchar_t **b ); const wchar_t **b );
/** /**
Find the beginning and end of the process definition under the cursor Find the beginning and end of the process definition under the cursor
@@ -51,8 +54,8 @@ void parse_util_cmdsubst_extent( const wchar_t *buff,
*/ */
void parse_util_process_extent( const wchar_t *buff, void parse_util_process_extent( const wchar_t *buff,
int cursor_pos, int cursor_pos,
wchar_t **a, const wchar_t **a,
wchar_t **b ); const wchar_t **b );
/** /**
@@ -65,12 +68,12 @@ void parse_util_process_extent( const wchar_t *buff,
*/ */
void parse_util_job_extent( const wchar_t *buff, void parse_util_job_extent( const wchar_t *buff,
int cursor_pos, int cursor_pos,
wchar_t **a, const wchar_t **a,
wchar_t **b ); const wchar_t **b );
/** /**
Find the beginning and end of the token under the cursor and the Find the beginning and end of the token under the cursor and the
toekn before the current token. Any combination of tok_begin, token before the current token. Any combination of tok_begin,
tok_end, prev_begin and prev_end may be null. tok_end, prev_begin and prev_end may be null.
\param buff the string to search for subshells \param buff the string to search for subshells
@@ -82,10 +85,10 @@ void parse_util_job_extent( const wchar_t *buff,
*/ */
void parse_util_token_extent( const wchar_t *buff, void parse_util_token_extent( const wchar_t *buff,
int cursor_pos, int cursor_pos,
wchar_t **tok_begin, const wchar_t **tok_begin,
wchar_t **tok_end, const wchar_t **tok_end,
wchar_t **prev_begin, const wchar_t **prev_begin,
wchar_t **prev_end ); const wchar_t **prev_end );
/** /**
@@ -96,66 +99,24 @@ int parse_util_lineno( const wchar_t *str, int len );
/** /**
Calculate the line number of the specified cursor position Calculate the line number of the specified cursor position
*/ */
int parse_util_get_line_from_offset( wchar_t *buff, int pos ); int parse_util_get_line_from_offset( const wcstring &str, int pos );
/** /**
Get the offset of the first character on the specified line Get the offset of the first character on the specified line
*/ */
int parse_util_get_offset_from_line( wchar_t *buff, int line ); int parse_util_get_offset_from_line( const wcstring &str, int line );
/** /**
Return the total offset of the buffer for the cursor position nearest to the specified poition Return the total offset of the buffer for the cursor position nearest to the specified poition
*/ */
int parse_util_get_offset( wchar_t *buff, int line, int line_offset ); int parse_util_get_offset( const wcstring &str, int line, int line_offset );
/**
Autoload the specified file, if it exists in the specified path. Do
not load it multiple times unless it's timestamp changes or
parse_util_unload is called.
Autoloading one file may unload another.
\param cmd the filename to search for. The suffix '.fish' is always added to this name
\param path_var_name the environment variable giving the search path
\param on_unload a callback function to run if a suitable file is found, which has not already been run. unload will also be called for old files which are unloaded.
\param reload wheter to recheck file timestamps on already loaded files
*/
int parse_util_load( const wchar_t *cmd,
const wchar_t *path_var_name,
void (*on_unload)(const wchar_t *cmd),
int reload );
/**
Reset the loader for the specified path variable. This will cause
all information on loaded files in the specified directory to be
reset.
\param path_var_name the environment variable giving the search path
\param on_unload a callback function which will be called before (re)loading a file, may be used to unload the previous file.
*/
void parse_util_load_reset( const wchar_t *path_var_name,
void (*on_unload)(const wchar_t *cmd) );
/**
Tell the autoloader that the specified file, in the specified path,
is no longer loaded.
\param cmd the filename to search for. The suffix '.fish' is always added to this name
\param path_var_name the environment variable giving the search path
\param on_unload a callback function which will be called before (re)loading a file, may be used to unload the previous file.
\return non-zero if the file was removed, zero if the file had not yet been loaded
*/
int parse_util_unload( const wchar_t *cmd,
const wchar_t *path_var_name,
void (*on_unload)(const wchar_t *cmd) );
/** /**
Set the argv environment variable to the specified null-terminated Set the argv environment variable to the specified null-terminated
array of strings. array of strings.
*/ */
void parse_util_set_argv( wchar_t **argv, array_list_t *named_arguments ); void parse_util_set_argv( const wchar_t * const *argv, const wcstring_list_t &named_arguments );
/** /**
Make a duplicate of the specified string, unescape wildcard Make a duplicate of the specified string, unescape wildcard

File diff suppressed because it is too large Load Diff

466
parser.h
View File

@@ -9,8 +9,10 @@
#include "proc.h" #include "proc.h"
#include "util.h" #include "util.h"
#include "parser.h"
#include "event.h" #include "event.h"
#include "function.h"
#include <vector>
#include <memory>
#define PARSER_TEST_ERROR 1 #define PARSER_TEST_ERROR 1
#define PARSER_TEST_INCOMPLETE 2 #define PARSER_TEST_INCOMPLETE 2
@@ -18,7 +20,7 @@
/** /**
event_block_t represents a block on events of the specified type event_block_t represents a block on events of the specified type
*/ */
typedef struct event_block struct event_block_t
{ {
/** /**
The types of events to block. This is interpreted as a bitset The types of events to block. This is interpreted as a bitset
@@ -28,16 +30,33 @@ typedef struct event_block
Note that EVENT_ANY can be used to specify any event. Note that EVENT_ANY can be used to specify any event.
*/ */
int type; unsigned int typemask;
};
/** typedef std::list<event_block_t> event_block_list_t;
The next event_block struct
*/ inline bool event_block_list_blocks_type(const event_block_list_t &ebls, int type) {
struct event_block *next; for (event_block_list_t::const_iterator iter = ebls.begin(); iter != ebls.end(); ++iter) {
if( iter->typemask & (1<<EVENT_ANY ) )
return true;
if( iter->typemask & (1<<type) )
return true;
}
return false;
} }
event_block_t;
/** Block state template, to replace the discriminated union */
struct block_state_base_t {
public:
virtual ~block_state_base_t() {}
};
template<typename T>
struct block_state_t : public block_state_base_t {
T value;
block_state_t() : value() {}
};
/** /**
block_t represents a block of commands. block_t represents a block of commands.
@@ -62,11 +81,9 @@ typedef struct block
/** /**
Block type-specific data Block type-specific data
*/ */
void *data; std::auto_ptr<function_data_t> function_data;
/** #if 0
First block type specific variable
*/
union union
{ {
int while_state; /**< True if the loop condition has not yet been evaluated*/ int while_state; /**< True if the loop condition has not yet been evaluated*/
@@ -77,17 +94,42 @@ typedef struct block
event_t *event; /**<The event that triggered this block */ event_t *event; /**<The event that triggered this block */
wchar_t *function_call_name; wchar_t *function_call_name;
} param1; } param1;
#endif
/** /** First block type specific variable */
Second block type specific variable block_state_base_t *state1_ptr;
*/
union
{
array_list_t for_vars; /**< List of values for a for block */
int switch_taken; /**< Whether a switch match has already been found */
process_t *function_call_process; /**< The process representing this function call */
} param2;
template<typename T>
T& state1(void) {
block_state_t<T> *state;
if (state1_ptr == NULL) {
state = new block_state_t<T>();
state1_ptr = state;
} else {
state = dynamic_cast<block_state_t<T> *>(state1_ptr);
if (state == NULL) {
printf("Expected type %s, but instead got type %s\n", typeid(T).name(), typeid(*state1_ptr).name());
abort();
}
}
return state->value;
}
/** Second block type specific variable */
block_state_base_t *state2_ptr;
template<typename T>
T& state2(void) {
block_state_t<T> *state;
if (state2_ptr == NULL) {
state = new block_state_t<T>();
state2_ptr = state;
} else {
state = dynamic_cast<block_state_t<T> *>(state2_ptr);
assert(state != NULL);
}
return state->value;
}
/** /**
Name of file that created this block Name of file that created this block
@@ -99,21 +141,31 @@ typedef struct block
*/ */
int src_lineno; int src_lineno;
/** /** Whether we should pop the environment variable stack when we're popped off of the block stack */
Some naming confusion. This is a pointer to the first element in the list of all event blocks. bool wants_pop_env;
*/
event_block_t *first_event_block; /** List of event blocks. */
event_block_list_t event_blocks;
/** /**
Next outer block Next outer block
*/ */
struct block *outer; struct block *outer;
/** Destructor */
~block()
{
if (state1_ptr != NULL)
delete state1_ptr;
if (state2_ptr != NULL)
delete state2_ptr;
}
} block_t; } block_t;
/** /**
Types of blocks Types of blocks
*/ */
enum block_type enum block_type_t
{ {
WHILE, /**< While loop block */ WHILE, /**< While loop block */
FOR, /**< For loop block */ FOR, /**< For loop block */
@@ -177,165 +229,275 @@ enum parser_error
Error while evaluating cmdsubst Error while evaluating cmdsubst
*/ */
CMDSUBST_ERROR, CMDSUBST_ERROR,
} };
;
/** The current innermost block */ enum parser_type_t {
extern block_t *current_block; PARSER_TYPE_NONE,
PARSER_TYPE_GENERAL,
PARSER_TYPE_FUNCTIONS_ONLY,
PARSER_TYPE_COMPLETIONS_ONLY,
PARSER_TYPE_ERRORS_ONLY
};
/** Global event blocks */ struct profile_item_t {
extern event_block_t *global_event_block; /**
Time spent executing the specified command, including parse time for nested blocks.
*/
int exec;
/**
Time spent parsing the specified command, including execution time for command substitutions.
*/
int parse;
/**
The block level of the specified command. nested blocks and command substitutions both increase the block level.
*/
size_t level;
/**
If the execution of this command was skipped.
*/
int skipped;
/**
The command string.
*/
wcstring cmd;
};
/** struct tokenizer;
Current block level io redirections
*/
extern io_data_t *block_io;
/** class parser_t {
Evaluate the expressions contained in cmd. private:
enum parser_type_t parser_type;
std::vector<block_t> blocks;
\param cmd the string to evaluate /** Whether or not we output errors */
\param io io redirections to perform on all started jobs const bool show_errors;
\param block_type The type of block to push on the block stack
\return 0 on success, 1 otherwise /** Last error code */
*/ int error_code;
int eval( const wchar_t *cmd, io_data_t *io, int block_type );
/** /** Position of last error */
Evaluate line as a list of parameters, i.e. tokenize it and perform parameter expansion and cmdsubst execution on the tokens. int err_pos;
The output is inserted into output, and should be freed by the caller.
\param line Line to evaluate /** Description of last error */
\param output List to insert output to wcstring err_buff;
*/
int eval_args( const wchar_t *line,
array_list_t *output );
/** /** Pointer to the current tokenizer */
Sets the current evaluation error. This function should only be used by libraries that are called by tokenizer *current_tokenizer;
\param ec The new error code /** String for representing the current line */
\param p The character offset at which the error occured wcstring lineinfo;
\param str The printf-style error message filter
*/
void error( int ec, int p, const wchar_t *str, ... );
/** This is the position of the beginning of the currently parsed command */
int current_tokenizer_pos;
/** /** List of called functions, used to help prevent infinite recursion */
Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'. wcstring_list_t forbidden_function;
Example:
init.fish (line 127): ls|grep pancake /** String index where the current job started. */
*/ int job_start_pos;
wchar_t *parser_current_line();
/** /** The jobs associated with this parser */
Returns the current line number job_list_t my_job_list;
*/
int parser_get_lineno();
/** /**
Returns the current position in the latest string of the tokenizer. Keeps track of how many recursive eval calls have been made. Eval
*/ doesn't call itself directly, recursion happens on blocks and on
int parser_get_pos(); command substitutions.
*/
int eval_level;
/** /* No copying allowed */
Returns the position where the current job started in the latest string of the tokenizer. parser_t(const parser_t&);
*/ parser_t& operator=(const parser_t&);
int parser_get_job_pos();
/** void parse_job_argument_list( process_t *p, job_t *j, tokenizer *tok, std::vector<completion_t>& );
Set the current position in the latest string of the tokenizer. int parse_job( process_t *p, job_t *j, tokenizer *tok );
*/ void skipped_exec( job_t * j );
void parser_set_pos( int p); void eval_job( tokenizer *tok );
int parser_test_argument( const wchar_t *arg, wcstring *out, const wchar_t *prefix, int offset );
void print_errors( wcstring &target, const wchar_t *prefix );
void print_errors_stderr();
/** public:
Get the string currently parsed std::vector<profile_item_t> profile_items;
*/
const wchar_t *parser_get_buffer();
/** /**
Create block of specified type Returns the name of the currently evaluated function if we are
*/ currently evaluating a function, null otherwise. This is tested by
void parser_push_block( int type); moving down the block-scope-stack, checking every block if it is of
type FUNCTION_CALL.
*/
const wchar_t *is_function() const;
/** /** Get the "principal" parser, whatever that is */
Remove the outermost block namespace static parser_t &principal_parser();
*/
void parser_pop_block();
/** /** Indicates that execution of all blocks in the principal parser should stop.
Return a description of the given blocktype This is called from signal handlers!
*/ */
const wchar_t *parser_get_block_desc( int block ); static void skip_all_blocks();
/** Create a parser of the given type */
parser_t(enum parser_type_t type, bool show_errors);
/** /** The current innermost block, allocated with new */
Test if the specified string can be parsed, or if more bytes need block_t *current_block;
to be read first. The result will have the PARSER_TEST_ERROR bit
set if there is a syntax error in the code, and the
PARSER_TEST_INCOMPLETE bit set if the code contains unclosed
blocks.
\param buff the text buffer to test /** Global event blocks */
\param block_level if non-null, the block nesting level will be filled out into this array event_block_list_t global_event_blocks;
\param out if non-null, any errors in the command will be filled out into this buffer
\param prefix the prefix string to prepend to each error message written to the \c out buffer
*/
int parser_test( const wchar_t * buff, int *block_level, string_buffer_t *out, const wchar_t *prefix );
/** /** Current block level io redirections */
Test if the specified string can be parsed as an argument list, io_data_t *block_io;
e.g. sent to eval_args. The result has the first bit set if the
string contains errors, and the second bit is set if the string
contains an unclosed block.
*/
int parser_test_args( const wchar_t * buff, string_buffer_t *out, const wchar_t *prefix );
/** /**
Tell the parser that the specified function may not be run if not Evaluate the expressions contained in cmd.
inside of a conditional block. This is to remove some possibilities
of infinite recursion.
*/
void parser_forbid_function( wchar_t *function );
/**
Undo last call to parser_forbid_function().
*/
void parser_allow_function();
/** \param cmd the string to evaluate
Initialize static parser data \param io io redirections to perform on all started jobs
*/ \param block_type The type of block to push on the block stack
void parser_init();
/** \return 0 on success, 1 otherwise
Destroy static parser data */
*/ int eval( const wcstring &cmd, io_data_t *io, enum block_type_t block_type );
void parser_destroy();
/** /**
This function checks if the specified string is a help option. Evaluate line as a list of parameters, i.e. tokenize it and perform parameter expansion and cmdsubst execution on the tokens.
The output is inserted into output, and should be freed by the caller.
\param s the string to test \param line Line to evaluate
\param min_match is the minimum number of characters that must match in a long style option, i.e. the longest common prefix between --help and any other option. If less than 3, 3 will be assumed. \param output List to insert output to
*/ */
int parser_is_help( wchar_t *s, int min_match ); /**
\param line Line to evaluate
\param output List to insert output to
*/
int eval_args( const wchar_t *line, std::vector<completion_t> &output );
/** /**
Returns the file currently evaluated by the parser. This can be Sets the current evaluation error. This function should only be used by libraries that are called by
different than reader_current_filename, e.g. if we are evaulating a
function defined in a different file than the one curently read.
*/
const wchar_t *parser_current_filename();
/** \param ec The new error code
Write a stack trace starting at the specified block to the specified string_buffer_t \param p The character offset at which the error occured
*/ \param str The printf-style error message filter
void parser_stack_trace( block_t *b, string_buffer_t *buff); */
void error( int ec, int p, const wchar_t *str, ... );
int parser_get_block_type( const wchar_t *cmd ); /**
const wchar_t *parser_get_block_command( int type ); Returns a string describing the current parser pisition in the format 'FILENAME (line LINE_NUMBER): LINE'.
Example:
init.fish (line 127): ls|grep pancake
*/
const wchar_t *current_line();
/** Returns the current line number */
int get_lineno() const;
/** Returns the current position in the latest string of the tokenizer. */
int get_pos() const;
/** Returns the position where the current job started in the latest string of the tokenizer. */
int get_job_pos() const;
/** Set the current position in the latest string of the tokenizer. */
void set_pos( int p);
/** Get the string currently parsed */
const wchar_t *get_buffer() const;
/** Get the list of jobs */
job_list_t &job_list() { return my_job_list; }
/** Create block of specified type */
void push_block( int type);
/** Remove the outermost block namespace */
void pop_block();
/** Return a description of the given blocktype */
const wchar_t *get_block_desc( int block ) const;
/** Create a job */
job_t *job_create();
/** Removes a job */
bool job_remove(job_t *job);
/** Promotes a job to the front of the list */
void job_promote(job_t *job);
/** Return the job with the specified job id. If id is 0 or less, return the last job used. */
job_t *job_get(int job_id);
/** Returns the job with the given pid */
job_t *job_get_from_pid( int pid );
/**
Test if the specified string can be parsed, or if more bytes need
to be read first. The result will have the PARSER_TEST_ERROR bit
set if there is a syntax error in the code, and the
PARSER_TEST_INCOMPLETE bit set if the code contains unclosed
blocks.
\param buff the text buffer to test
\param block_level if non-null, the block nesting level will be filled out into this array
\param out if non-null, any errors in the command will be filled out into this buffer
\param prefix the prefix string to prepend to each error message written to the \c out buffer
*/
int test( const wchar_t * buff, int *block_level, wcstring *out, const wchar_t *prefix );
/**
Test if the specified string can be parsed as an argument list,
e.g. sent to eval_args. The result has the first bit set if the
string contains errors, and the second bit is set if the string
contains an unclosed block.
*/
int test_args( const wchar_t * buff, wcstring *out, const wchar_t *prefix );
/**
Tell the parser that the specified function may not be run if not
inside of a conditional block. This is to remove some possibilities
of infinite recursion.
*/
void forbid_function( const wcstring &function );
/**
Undo last call to parser_forbid_function().
*/
void allow_function();
/**
Initialize static parser data
*/
void init();
/**
Destroy static parser data
*/
void destroy();
/**
This function checks if the specified string is a help option.
\param s the string to test
\param min_match is the minimum number of characters that must match in a long style option, i.e. the longest common prefix between --help and any other option. If less than 3, 3 will be assumed.
*/
int is_help( const wchar_t *s, int min_match ) const;
/**
Returns the file currently evaluated by the parser. This can be
different than reader_current_filename, e.g. if we are evaulating a
function defined in a different file than the one curently read.
*/
const wchar_t *current_filename() const;
/**
Write a stack trace starting at the specified block to the specified wcstring
*/
void stack_trace( block_t *b, wcstring &buff);
int get_block_type( const wchar_t *cmd ) const;
const wchar_t *get_block_command( int type ) const;
};
#endif #endif

View File

@@ -14,15 +14,18 @@ Functions having to do with parser keywords, like testing if a function is a blo
#include "parser_keywords.h" #include "parser_keywords.h"
int parser_keywords_is_switch( const wchar_t *cmd ) bool parser_keywords_is_switch( const wcstring &cmd )
{ {
if( wcscmp( cmd, L"--" ) == 0 ) if (cmd == L"--") {
return ARG_SKIP; return ARG_SKIP;
else } else if (! cmd.empty() && cmd.at(0) == L'-') {
return cmd[0] == L'-'; return ARG_SWITCH;
} else {
return ARG_NON_SWITCH;
}
} }
int parser_keywords_skip_arguments( const wchar_t *cmd ) bool parser_keywords_skip_arguments( const wcstring &cmd )
{ {
return contains( cmd, return contains( cmd,
L"else", L"else",
@@ -30,7 +33,7 @@ int parser_keywords_skip_arguments( const wchar_t *cmd )
} }
int parser_keywords_is_subcommand( const wchar_t *cmd ) bool parser_keywords_is_subcommand( const wcstring &cmd )
{ {
return parser_keywords_skip_arguments( cmd ) || return parser_keywords_skip_arguments( cmd ) ||
@@ -46,7 +49,7 @@ int parser_keywords_is_subcommand( const wchar_t *cmd )
} }
int parser_keywords_is_block( const wchar_t *word) bool parser_keywords_is_block( const wcstring &word)
{ {
return contains( word, return contains( word,
L"for", L"for",
@@ -57,7 +60,7 @@ int parser_keywords_is_block( const wchar_t *word)
L"begin" ); L"begin" );
} }
int parser_keywords_is_reserved( const wchar_t *word) bool parser_keywords_is_reserved( const wcstring &word)
{ {
return parser_keywords_is_block(word) || return parser_keywords_is_block(word) ||
parser_keywords_is_subcommand( word ) || parser_keywords_is_subcommand( word ) ||

Some files were not shown because too many files have changed in this diff Show More