1 %%%----------------------------------------------------------------------
3 %%% Author : Claes Wikstrom <klacke@kaja.hemma.net>
5 %%% Created : 1 Dec 2000 by Claes Wikstrom <klacke@kaja.hemma.net>
6 %%%----------------------------------------------------------------------
9 -author('klacke@kaja.hemma.net').
13 -include ("slang.hrl").
15 -record(file_line
, {next
,
23 demolib_exit (Signal
) ->
26 slang:smg_reset_smg (),
32 io:format("Exiting on signal ~p\n", [Signal
]),
37 sigint_handler (Signal
) ->
38 demolib_exit (Signal
).
42 % static void init_signals (void)
45 % SLsignal (SIGTSTP, sigtstp_handler);
48 % SLsignal (SIGINT, sigint_handler);
52 exit_error_hook (Fmt
, Args
) ->
64 demolib_init_terminal () ->
66 %% SLang_Exit_Error_Hook = exit_error_hook
67 % It is wise to block the occurance of display related
72 %SLsig_block_signals (),
74 slang:tt_get_terminfo (),
76 %% SLkp_init assumes that SLtt_get_terminfo has been called.
78 case slang:kp_init() of
83 slang:init_tty (-1, 0, 1),
84 %slang:tty_set_suspend_state (1),
86 case slang:smg_init_smg () of
101 int
main (int argc
, char
**argv
)
107 else
if ((argc
!= 1) || (1 == isatty (fileno(stdin
))))
111 if (-1 == read_file (File_Name
))
113 fprintf (stderr
, "Unable to read %s\n", File_Name
);
117 /* This sets up the terminal
, signals
, screen management routines
, etc
... */
118 if (-1 == demolib_init_terminal (1, 1))
120 fprintf (stderr
, "Unable to initialize terminal.");
124 #define APP_KEY_EOB
0x1001
125 #define APP_KEY_BOB
0x1002
127 /* Add a few application defined keysyms
. 0x1000 and above are for
130 (void
) SLkp_define_keysym ("\033>", APP_KEY_EOB
);
131 (void
) SLkp_define_keysym ("\033<", APP_KEY_BOB
);
133 main_loop (); /* should not return
*/
138 /* The SLscroll routines will be used for pageup
/down commands
. They assume
139 * a linked
list of lines
. The first
element of the structure MUST point to
140 * the NEXT line
, the second MUST point to the PREVIOUS line
.
142 typedef struct _File_Line_Type
144 struct _File_Line_Type
*next
;
145 struct _File_Line_Type
*prev
;
146 char
*data
; /* pointer to line data
*/
150 static File_Line_Type
*File_Lines
;
152 /* The SLscroll routines will use this structure
. */
153 static SLscroll_Window_Type Line_Window
;
155 static void
free_lines (void
)
157 File_Line_Type
*line
, *next
;
163 if (line
->data
!= NULL
) free (line
->data
);
170 static File_Line_Type
*create_line (char
*buf
)
172 File_Line_Type
*line
;
174 line
= (File_Line_Type
*) malloc (sizeof (File_Line_Type
));
175 if (line
== NULL
) return NULL
;
177 memset ((char
*) line
, sizeof (File_Line_Type
), 0);
179 line
->data
= SLmake_string (buf
); /* use a slang routine
*/
180 if (line
->data
== NULL
)
190 static int
read_file (char
*file
)
194 File_Line_Type
*line
, *last_line
;
195 unsigned int num_lines
;
199 else fp
= fopen (file
, "r");
201 if (fp
== NULL
) return
-1;
206 while (NULL
!= fgets (buf
, sizeof(buf
), fp
))
210 if (NULL
== (line
= create_line (buf
)))
212 fprintf (stderr
, "Out of memory.");
217 if (last_line
== NULL
)
220 last_line
->next
= line
;
222 line
->prev
= last_line
;
228 memset ((char
*)&Line_Window
, 0, sizeof (SLscroll_Window_Type
));
230 Line_Window
.current_line
= (SLscroll_Type
*) File_Lines
;
231 Line_Window
.lines
= (SLscroll_Type
*) File_Lines
;
232 Line_Window
.line_num
= 1;
233 Line_Window
.num_lines
= num_lines
;
239 static void
update_display (void
)
241 unsigned int row
, nrows
;
242 File_Line_Type
*line
;
244 /* All well behaved applications should block signals that may affect
245 * the display while performing screen update
.
247 SLsig_block_signals ();
249 Line_Window
.nrows
= nrows
= SLtt_Screen_Rows
- 1;
251 /* Always make the current line equal to the top window line
. */
252 if (Line_Window
.top_window_line
!= NULL
)
253 Line_Window
.current_line
= Line_Window
.top_window_line
;
255 SLscroll_find_top (&Line_Window
);
258 line
= (File_Line_Type
*) Line_Window
.top_window_line
;
260 SLsmg_normal_video ();
262 while (row
< Line_Window
.nrows
)
264 SLsmg_gotorc (row
, 0);
268 SLsmg_write_string (line
->data
);
275 SLsmg_gotorc (row
, 0);
276 SLsmg_reverse_video ();
277 SLsmg_printf ("%s", (File_Name
== NULL
) ?
"<stdin>" : File_Name
);
281 SLsig_unblock_signals ();
284 static int Screen_Start
;
286 static void
main_loop (void
)
293 switch (SLkp_getkey ())
303 screen_start
= Screen_Start
;
304 SLsmg_set_screen_start (NULL
, &screen_start
);
309 if (Screen_Start
< 0) Screen_Start
= 0;
310 screen_start
= Screen_Start
;
311 SLsmg_set_screen_start (NULL
, &screen_start
);
315 SLscroll_prev_n (&Line_Window
, 1);
316 Line_Window
.top_window_line
= Line_Window
.current_line
;
321 SLscroll_next_n (&Line_Window
, 1);
322 Line_Window
.top_window_line
= Line_Window
.current_line
;
327 SLscroll_pagedown (&Line_Window
);
332 SLscroll_pageup (&Line_Window
);
336 while (-1 != SLscroll_pageup (&Line_Window
))
341 while (-1 != SLscroll_pagedown (&Line_Window
))