1 ! Copyright (C) 2013 John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors assocs combinators formatting fry grouping hashtables
5 io io.binary io.directories io.encodings.binary io.files
6 io.files.types io.pathnames kernel math math.parser memoize pack
7 sequences sequences.generalizations splitting strings system ;
11 ! Reads compiled terminfo files
12 ! typically located in any of the directories below.
13 CONSTANT: TERMINFO-DIRS {
26 : check-magic ( n -- )
27 MAGIC = [ bad-magic ] unless ;
29 TUPLE: terminfo-header names-bytes boolean-bytes #numbers
30 #strings string-bytes ;
32 C: <terminfo-header> terminfo-header
34 : read-header ( -- header )
35 12 read "ssssss" unpack-le unclip check-magic
36 5 firstn <terminfo-header> ;
38 : read-names ( header -- names )
39 names-bytes>> read but-last "|" split [ >string ] map ;
41 : read-booleans ( header -- booleans )
42 boolean-bytes>> read [ 1 = ] { } map-as ;
44 : read-shorts ( n -- seq' )
45 2 * read 2 <groups> [ signed-le> dup 0 < [ drop f ] when ] map ;
47 : align-even-bytes ( header -- )
48 [ names-bytes>> ] [ boolean-bytes>> ] bi + odd?
51 : read-numbers ( header -- numbers )
52 [ align-even-bytes ] [ #numbers>> read-shorts ] bi ;
54 : string-offset ( from seq -- str )
55 0 2over index-from swap subseq >string ;
57 : read-strings ( header -- strings )
58 [ #strings>> read-shorts ] [ string-bytes>> read ] bi
59 '[ [ _ string-offset ] [ f ] if* ] map ;
61 TUPLE: terminfo names booleans numbers strings ;
63 C: <terminfo> terminfo
65 : read-terminfo ( -- terminfo )
75 : file>terminfo ( path -- terminfo )
76 binary [ read-terminfo ] with-file-reader ;
78 HOOK: terminfo-relative-path os ( name -- path )
80 M: macosx terminfo-relative-path ( name -- path )
81 [ first >hex ] keep "%s/%s" sprintf ;
83 M: linux terminfo-relative-path ( name -- path )
84 [ first ] keep "%c/%s" sprintf ;
86 : terminfo-path ( name -- path )
87 terminfo-relative-path TERMINFO-DIRS [ swap append-path ] with map
88 [ exists? ] find nip ;
90 : terminfo-names-for-path ( path -- names )
92 [ type>> +directory+ = ] filter
93 [ name>> directory-files ] map concat
94 ] with-directory-entries ;
96 MEMO: terminfo-names ( -- names )
97 TERMINFO-DIRS [ exists? ] filter
98 [ terminfo-names-for-path ] map concat ;
102 CONSTANT: boolean-names {
103 "auto_left_margin" "auto_right_margin" "no_esc_ctlc"
104 "ceol_standout_glitch" "eat_newline_glitch"
105 "erase_overstrike" "generic_type" "hard_copy" "has_meta_key"
106 "has_status_line" "insert_null_glitch" "memory_above"
107 "memory_below" "move_insert_mode" "move_standout_mode"
108 "over_strike" "status_line_esc_ok" "dest_tabs_magic_smso"
109 "tilde_glitch" "transparent_underline" "xon_xoff"
110 "needs_xon_xoff" "prtr_silent" "hard_cursor" "non_rev_rmcup"
111 "no_pad_char" "non_dest_scroll_region" "can_change"
112 "back_color_erase" "hue_lightness_saturation"
113 "col_addr_glitch" "cr_cancels_micro_mode" "has_print_wheel"
114 "row_addr_glitch" "semi_auto_right_margin" "cpi_changes_res"
115 "lpi_changes_res" "backspaces_with_bs" "crt_no_scrolling"
116 "no_correctly_working_cr" "gnu_has_meta_key"
117 "linefeed_is_newline" "has_hardware_tabs"
118 "return_does_clr_eol"
121 CONSTANT: number-names {
122 "columns" "init_tabs" "lines" "lines_of_memory"
123 "magic_cookie_glitch" "padding_baud_rate" "virtual_terminal"
124 "width_status_line" "num_labels" "label_height"
125 "label_width" "max_attributes" "maximum_windows"
126 "max_colors" "max_pairs" "no_color_video" "buffer_capacity"
127 "dot_vert_spacing" "dot_horz_spacing" "max_micro_address"
128 "max_micro_jump" "micro_col_size" "micro_line_size"
129 "number_of_pins" "output_res_char" "output_res_line"
130 "output_res_horz_inch" "output_res_vert_inch" "print_rate"
131 "wide_char_size" "buttons" "bit_image_entwining"
132 "bit_image_type" "magic_cookie_glitch_ul"
133 "carriage_return_delay" "new_line_delay" "backspace_delay"
134 "horizontal_tab_delay" "number_of_function_keys"
137 CONSTANT: string-names {
138 "back_tab" "bell" "carriage_return" "change_scroll_region"
139 "clear_all_tabs" "clear_screen" "clr_eol" "clr_eos"
140 "column_address" "command_character" "cursor_address"
141 "cursor_down" "cursor_home" "cursor_invisible" "cursor_left"
142 "cursor_mem_address" "cursor_normal" "cursor_right"
143 "cursor_to_ll" "cursor_up" "cursor_visible"
144 "delete_character" "delete_line" "dis_status_line"
145 "down_half_line" "enter_alt_charset_mode" "enter_blink_mode"
146 "enter_bold_mode" "enter_ca_mode" "enter_delete_mode"
147 "enter_dim_mode" "enter_insert_mode" "enter_secure_mode"
148 "enter_protected_mode" "enter_reverse_mode"
149 "enter_standout_mode" "enter_underline_mode" "erase_chars"
150 "exit_alt_charset_mode" "exit_attribute_mode" "exit_ca_mode"
151 "exit_delete_mode" "exit_insert_mode" "exit_standout_mode"
152 "exit_underline_mode" "flash_screen" "form_feed"
153 "from_status_line" "init_1string" "init_2string"
154 "init_3string" "init_file" "insert_character" "insert_line"
155 "insert_padding" "key_backspace" "key_catab" "key_clear"
156 "key_ctab" "key_dc" "key_dl" "key_down" "key_eic" "key_eol"
157 "key_eos" "key_f0" "key_f1" "key_f10" "key_f2" "key_f3"
158 "key_f4" "key_f5" "key_f6" "key_f7" "key_f8" "key_f9"
159 "key_home" "key_ic" "key_il" "key_left" "key_ll" "key_npage"
160 "key_ppage" "key_right" "key_sf" "key_sr" "key_stab"
161 "key_up" "keypad_local" "keypad_xmit" "lab_f0" "lab_f1"
162 "lab_f10" "lab_f2" "lab_f3" "lab_f4" "lab_f5" "lab_f6"
163 "lab_f7" "lab_f8" "lab_f9" "meta_off" "meta_on" "newline"
164 "pad_char" "parm_dch" "parm_delete_line" "parm_down_cursor"
165 "parm_ich" "parm_index" "parm_insert_line"
166 "parm_left_cursor" "parm_right_cursor" "parm_rindex"
167 "parm_up_cursor" "pkey_key" "pkey_local" "pkey_xmit"
168 "print_screen" "prtr_off" "prtr_on" "repeat_char"
169 "reset_1string" "reset_2string" "reset_3string" "reset_file"
170 "restore_cursor" "row_address" "save_cursor"
171 "scroll_forward" "scroll_reverse" "set_attributes" "set_tab"
172 "set_window" "tab" "to_status_line" "underline_char"
173 "up_half_line" "init_prog" "key_a1" "key_a3" "key_b2"
174 "key_c1" "key_c3" "prtr_non" "char_padding" "acs_chars"
175 "plab_norm" "key_btab" "enter_xon_mode" "exit_xon_mode"
176 "enter_am_mode" "exit_am_mode" "xon_character"
177 "xoff_character" "ena_acs" "label_on" "label_off" "key_beg"
178 "key_cancel" "key_close" "key_command" "key_copy"
179 "key_create" "key_end" "key_enter" "key_exit" "key_find"
180 "key_help" "key_mark" "key_message" "key_move" "key_next"
181 "key_open" "key_options" "key_previous" "key_print"
182 "key_redo" "key_reference" "key_refresh" "key_replace"
183 "key_restart" "key_resume" "key_save" "key_suspend"
184 "key_undo" "key_sbeg" "key_scancel" "key_scommand"
185 "key_scopy" "key_screate" "key_sdc" "key_sdl" "key_select"
186 "key_send" "key_seol" "key_sexit" "key_sfind" "key_shelp"
187 "key_shome" "key_sic" "key_sleft" "key_smessage" "key_smove"
188 "key_snext" "key_soptions" "key_sprevious" "key_sprint"
189 "key_sredo" "key_sreplace" "key_sright" "key_srsume"
190 "key_ssave" "key_ssuspend" "key_sundo" "req_for_input"
191 "key_f11" "key_f12" "key_f13" "key_f14" "key_f15" "key_f16"
192 "key_f17" "key_f18" "key_f19" "key_f20" "key_f21" "key_f22"
193 "key_f23" "key_f24" "key_f25" "key_f26" "key_f27" "key_f28"
194 "key_f29" "key_f30" "key_f31" "key_f32" "key_f33" "key_f34"
195 "key_f35" "key_f36" "key_f37" "key_f38" "key_f39" "key_f40"
196 "key_f41" "key_f42" "key_f43" "key_f44" "key_f45" "key_f46"
197 "key_f47" "key_f48" "key_f49" "key_f50" "key_f51" "key_f52"
198 "key_f53" "key_f54" "key_f55" "key_f56" "key_f57" "key_f58"
199 "key_f59" "key_f60" "key_f61" "key_f62" "key_f63" "clr_bol"
200 "clear_margins" "set_left_margin" "set_right_margin"
201 "label_format" "set_clock" "display_clock" "remove_clock"
202 "create_window" "goto_window" "hangup" "dial_phone"
203 "quick_dial" "tone" "pulse" "flash_hook" "fixed_pause"
204 "wait_tone" "user0" "user1" "user2" "user3" "user4" "user5"
205 "user6" "user7" "user8" "user9" "orig_pair" "orig_colors"
206 "initialize_color" "initialize_pair" "set_color_pair"
207 "set_foreground" "set_background" "change_char_pitch"
208 "change_line_pitch" "change_res_horz" "change_res_vert"
209 "define_char" "enter_doublewide_mode" "enter_draft_quality"
210 "enter_italics_mode" "enter_leftward_mode"
211 "enter_micro_mode" "enter_near_letter_quality"
212 "enter_normal_quality" "enter_shadow_mode"
213 "enter_subscript_mode" "enter_superscript_mode"
214 "enter_upward_mode" "exit_doublewide_mode"
215 "exit_italics_mode" "exit_leftward_mode" "exit_micro_mode"
216 "exit_shadow_mode" "exit_subscript_mode"
217 "exit_superscript_mode" "exit_upward_mode"
218 "micro_column_address" "micro_down" "micro_left"
219 "micro_right" "micro_row_address" "micro_up" "order_of_pins"
220 "parm_down_micro" "parm_left_micro" "parm_right_micro"
221 "parm_up_micro" "select_char_set" "set_bottom_margin"
222 "set_bottom_margin_parm" "set_left_margin_parm"
223 "set_right_margin_parm" "set_top_margin"
224 "set_top_margin_parm" "start_bit_image" "start_char_set_def"
225 "stop_bit_image" "stop_char_set_def" "subscript_characters"
226 "superscript_characters" "these_cause_cr" "zero_motion"
227 "char_set_names" "key_mouse" "mouse_info" "req_mouse_pos"
228 "get_mouse" "set_a_foreground" "set_a_background"
229 "pkey_plab" "device_type" "code_set_init" "set0_des_seq"
230 "set1_des_seq" "set2_des_seq" "set3_des_seq" "set_lr_margin"
231 "set_tb_margin" "bit_image_repeat" "bit_image_newline"
232 "bit_image_carriage_return" "color_names"
233 "define_bit_image_region" "end_bit_image_region"
234 "set_color_band" "set_page_length" "display_pc_char"
235 "enter_pc_charset_mode" "exit_pc_charset_mode"
236 "enter_scancode_mode" "exit_scancode_mode" "pc_term_options"
237 "scancode_escape" "alt_scancode_esc"
238 "enter_horizontal_hl_mode" "enter_left_hl_mode"
239 "enter_low_hl_mode" "enter_right_hl_mode"
240 "enter_top_hl_mode" "enter_vertical_hl_mode"
241 "set_a_attributes" "set_pglen_inch" "termcap_init2"
242 "termcap_reset" "linefeed_if_not_lf" "backspace_if_not_bs"
243 "other_non_function_keys" "arrow_key_map" "acs_ulcorner"
244 "acs_ll_corner" "acs_urcorner" "acs_lrcorner" "acs_ltee"
245 "acs_rtee" "acs_btee" "acs_ttee" "acs_hline" "acs_vline"
246 "acs_plus" "memory_lock" "memory_unlock" "box_chars_1"
249 : zip-names ( seq names -- assoc )
250 swap 2dup [ length ] bi@ - f <repetition> append zip ;
254 : term-capabilities ( name -- assoc )
255 terminfo-path file>terminfo {
256 [ booleans>> boolean-names zip-names ]
257 [ numbers>> number-names zip-names ]
258 [ strings>> string-names zip-names ]
259 } cleave 3append >hashtable ;