C-Menu 0.2.9
A User Interface Toolkit
Loading...
Searching...
No Matches
Initializing View I/O

Populate the C-Menu View Struct and Connect Input. More...

Files

file  init_view.c
 Initialize C-Menu View Screen IO and Input.

Functions

int init_view_boxwin (Init *init)
 Initialize the C-Menu View in box window mode.
int init_view_full_screen (Init *init)
 Initialize C-Menu View in full screen mode.
void view_calc_boxwin_dimensions (Init *init)
 Calculate the dimensions and position of the box window for C-Menu View.
void view_calc_full_screen_dimensions (Init *init)
 Calculate the dimensions for full screen mode.
int view_init_input (Init *init, char *file_name)
 Initialize the input for the C-Menu View.

Detailed Description

Populate the C-Menu View Struct and Connect Input.

Function Documentation

◆ init_view_boxwin()

int init_view_boxwin ( Init * init)

Initialize the C-Menu View in box window mode.

Parameters
initPointer to the Init structure containing view settings.
Returns
0 on success, -1 on failure.

sets up the view structure for box window mode, adjusts dimensions based on screen size, and creates a new pad for the view. It also configures various parameters such as scroll lines, command line position, and tab size.

Definition at line 145 of file init_view.c.

145 {
146 View *view = init->view;
147 view->f_full_screen = false;
149 // -------------------> 1. BOX <-------------------
150 view->sfc = ui_surface_box(NULL, BOX, view->lines + 2, view->cols + 2, view->begy, view->begx, NULL);
151#ifdef DEBUG_Z_STACK
152 ui_bkgd(view->sfc, BOX, &cell_magenta_bg);
153 ui_bkgdset(view->sfc, BOX, &cell_magenta_bg);
154 ui_mvwaddstr(view->sfc, BOX, 1, 1, "-- BOX --");
155#endif
156#ifdef DEBUG_IMMED_UI
157 ui_render();
158#endif
159 // -------------------> 2. WIN <-------------------
160 ui_surface_addwin(view->sfc, WIN, BOX, view->lines, view->cols, 1, 1);
161 ui_bkgd(view->sfc, WIN, &cell_nt);
162 ui_werase(view->sfc, WIN);
163#ifdef DEBUG_Z_STACK
164 ui_bkgd(view->sfc, WIN, &cell_cyan_bg);
165 ui_mvwaddstr(view->sfc, WIN, 1, 1, "-- WIN --");
166#endif
167#ifdef DEBUG_IMMED_UI
168 ui_render();
169#endif
170 // -------------------> 3. LNNO <-------------------
171 if (view->f_ln) {
172 ui_surface_addwin(view->sfc, LNNO, WIN, view->lines - 1, view->ln_win_cols, 0, 0);
173 ui_bkgd(view->sfc, LNNO, &cell_ln);
174 ui_bkgdset(view->sfc, LNNO, &cell_ln);
175 ui_scrollok(view->sfc, LNNO, true);
176 ui_setscrreg(view->sfc, LNNO, 0, view->scroll_lines);
177#ifdef DEBUG_Z_STACK
178 ui_bkgd(view->sfc, LNNO, &cell_blue_bg);
179 ui_bkgdset(view->sfc, LNNO, &cell_blue_bg);
180 ui_mvwaddstr(view->sfc, LNNO, 1, 1, "-- LNNO --");
181#endif
182#ifdef DEBUG_IMMED_UI
183 ui_render();
184#endif
185 }
186 // -------------------> 4. CMDLN <-------------------
187 ui_surface_addwin(view->sfc, CMDLN, WIN, 1, view->cols, view->lines - 1, 0);
188 ui_bkgd(view->sfc, CMDLN, &cell_nt);
189 ui_keypad(view->sfc, CMDLN, true);
190 ui_idlok(view->sfc, CMDLN, false);
191 ui_idcok(view->sfc, CMDLN, false);
192 ui_scrollok(view->sfc, CMDLN, false);
193#ifdef DEBUG_Z_STACK
194 ui_bkgd(view->sfc, CMDLN, &cell_red_bg);
195 ui_bkgdset(view->sfc, CMDLN, &cell_red_bg);
196 ui_mvwaddstr(view->sfc, CMDLN, 1, 1, "-- CMDLN --");
197#endif
198#ifdef DEBUG_IMMED_UI
199 ui_render();
200#endif
201 // -------------------> 5. PAD <-------------------
202 ui_surface_addpad(view->sfc, PAD, WIN, view->lines - 1, PAD_COLS - 1, 0, view->ln_win_cols + 1, true);
203 ui_bkgd(view->sfc, PAD, &cell_nt);
204 UiCaps caps;
205 ui_get_caps(&caps);
206 if (caps.pad_relative_yx) {
207 view->sminrow = 0;
208 view->smincol = 0;
209 view->smaxrow = view->lines - 1;
210 view->smaxcol = view->cols - 1;
211 }
212 // ui_scrollok(view->sfc, PAD, true)
213 //
214#ifdef DEBUG_Z_STACK
215 ui_bkgd(view->sfc, PAD, &cell_yellow_bg);
216 ui_bkgdset(view->sfc, PAD, &cell_yellow_bg);
217 ui_mvwaddstr(view->sfc, PAD, 1, 1, "-- PAD --");
218#endif
219 ui_render();
220 return (0);
221}
UiSurface * ui_surface_box(UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x, const char *title)
Create a new UI surface with a border box and optional title.
Definition ui_ncurses.c:293
void ui_render()
Render the UI by updating panels and refreshing the display.
Definition ui_ncurses.c:931
int ui_setscrreg(UiSurface *s, ss_t w, uint top, uint bottom)
Set the scrolling region for a UiSurface.
Definition ui_ncurses.c:861
int ui_idcok(UiSurface *s, ss_t w, bool enable)
Enable or disable the use of the insert/delete character capabilities for a UiSurface.
Definition ui_ncurses.c:844
int ui_surface_addwin(UiSurface *s, ss_t w, ss_t p, uint lines, uint cols, uint y, uint x)
Add a new window to the specified UiSurface.
Definition ui_ncurses.c:423
int ui_surface_addpad(UiSurface *s, ss_t w, ss_t view_win, uint lines, uint cols, uint y, uint x, bool hidden)
Add a new pad to the specified UiSurface.
Definition ui_ncurses.c:381
int ui_bkgd(UiSurface *s, ss_t w, const UiCell *cell)
Set the background of a specific window within a UiSurface.
Definition ui_ncurses.c:974
int ui_idlok(UiSurface *s, ss_t w, bool enable)
Enable or disable the use of the insert/delete line capabilities for a UiSurface.
Definition ui_ncurses.c:828
int ui_scrollok(UiSurface *s, ss_t w, bool enable)
Enable or disable scrolling for a UiSurface.
Definition ui_ncurses.c:796
void ui_get_caps(UiCaps *caps)
Get the UI backend capabilities.
Definition ui_ncurses.c:62
int ui_bkgdset(UiSurface *s, ss_t w, const UiCell *cell)
Set the background for new content to be written to a specific window within a UiSurface.
Definition ui_ncurses.c:988
int ui_werase(UiSurface *s, ss_t w)
Erase the contents of a UiSurface or all surfaces.
Definition ui_ncurses.c:582
int ui_keypad(UiSurface *s, ss_t w, bool enable)
Enable or disable the keypad for a UiSurface.
Definition ui_ncurses.c:812
#define PAD_COLS
Definition view.h:39
UiCell cell_blue_bg
Definition ui_common.c:154
UiCell cell_nt
Definition ui_common.c:123
UiCell cell_cyan_bg
Definition ui_common.c:155
UiCell cell_magenta_bg
Definition ui_common.c:156
UiCell cell_ln
Definition ui_common.c:131
UiCell cell_yellow_bg
Definition ui_common.c:153
UiCell cell_red_bg
Definition ui_common.c:151
int ui_mvwaddstr(UiSurface *s, ss_t w, uint y, uint x, const char *text)
Draw a text string at a specific position in a surface window.
View * view
Definition mem.c:38
void view_calc_boxwin_dimensions(Init *)
Calculate the dimensions and position of the box window for C-Menu View.
Definition init_view.c:245
View * view
Definition common.h:193
Definition view.h:59
bool pad_relative_yx
Definition ui_backend.h:504

References View::begx, View::begy, cell_ln, cell_nt, View::cols, View::f_full_screen, View::f_ln, View::lines, View::ln_win_cols, UiCaps::pad_relative_yx, View::scroll_lines, View::sfc, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, ui_bkgd(), ui_bkgdset(), ui_get_caps(), ui_idcok(), ui_idlok(), ui_keypad(), ui_render(), ui_scrollok(), ui_setscrreg(), ui_surface_addpad(), ui_surface_addwin(), ui_surface_box(), ui_werase(), Init::view, and view_calc_boxwin_dimensions().

Referenced by main(), new_pick_view(), popup_view(), and view_boxwin_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_view_full_screen()

int init_view_full_screen ( Init * init)

Initialize C-Menu View in full screen mode.

Parameters
initPointer to the Init structure containing view settings.
Returns
0 on success, -1 on failure.

This function sets up the view structure for full screen mode and creates a new pad for the view.

The function creates the following windows:
1. view->win: Top Level Container
2. view->lnno: Line numbers
1. view->cmdln: Command Line
3. view->pad: Content

Definition at line 48 of file init_view.c.

48 {
49 View *view = init->view;
50 view->f_full_screen = true;
52 // -------------------> 1. WIN <-------------------
53 view->sfc = ui_surface_new(WIN, nullptr, 0, view->lines, view->cols, 0, 0);
54 ui_bkgd(view->sfc, WIN, &cell_nt);
55 ui_werase(view->sfc, WIN);
56 // -------------------> 2. LNNO <-------------------
57 if (view->f_ln) {
58 ui_surface_addwin(view->sfc, LNNO, WIN, view->scroll_lines, view->ln_win_cols, 0, 0);
59 ui_bkgd(view->sfc, LNNO, &cell_ln);
60 ui_bkgdset(view->sfc, LNNO, &cell_ln);
61 ui_scrollok(view->sfc, LNNO, true);
62 ui_setscrreg(view->sfc, LNNO, 0, view->scroll_lines);
63 }
64 // -------------------> 3. CMDLN <-------------------
65 ui_surface_addwin(view->sfc, CMDLN, WIN, 1, view->cols, view->scroll_lines, 0);
66 ui_bkgd(view->sfc, CMDLN, &cell_nt);
67 ui_keypad(view->sfc, CMDLN, true);
68 ui_idlok(view->sfc, CMDLN, false);
69 ui_idcok(view->sfc, CMDLN, false);
70 ui_scrollok(view->sfc, CMDLN, false);
71 // -------------------> 4. PAD <-------------------
72 ui_surface_addpad(view->sfc, PAD, WIN, view->lines - 1, PAD_COLS - 1, 0, view->ln_win_cols + 1, false);
73 // ui_scrollok(view->sfc, PAD, true);
74 // ui_setscrreg(view->sfc, PAD, 0, view->scroll_lines);
75 ui_keypad(view->sfc, PAD, true);
76 UiCaps caps;
77 ui_get_caps(&caps);
78 if (caps.pad_relative_yx) {
79 view->sminrow = 0;
80 view->smincol = 0;
81 }
82 // ------------------------------------------------
83 return 0;
84}
UiSurface * ui_surface_new(ss_t w, UiSurface *parent, ss_t p, uint lines, uint cols, uint y, uint x)
Create a new UI surface with the specified parameters.
Definition ui_ncurses.c:230
void view_calc_full_screen_dimensions(Init *)
Calculate the dimensions for full screen mode.
Definition init_view.c:110

References cell_ln, cell_nt, View::cols, View::f_full_screen, View::f_ln, View::lines, View::ln_win_cols, UiCaps::pad_relative_yx, View::scroll_lines, View::sfc, View::smincol, View::sminrow, ui_bkgd(), ui_bkgdset(), ui_get_caps(), ui_idcok(), ui_idlok(), ui_keypad(), ui_scrollok(), ui_setscrreg(), ui_surface_addpad(), ui_surface_addwin(), ui_surface_new(), ui_werase(), Init::view, and view_calc_full_screen_dimensions().

Referenced by main(), and view_full_screen_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_calc_boxwin_dimensions()

void view_calc_boxwin_dimensions ( Init * init)

Calculate the dimensions and position of the box window for C-Menu View.

This function calculates the dimensions and position of the box window for the C-Menu View based on the screen size and any specified parameters in the Init structure. It ensures that the box window fits within the screen dimensions and adjusts its size and position accordingly.

Parameters
initPointer to the Init structure containing view settings.

Use view->lines and view->cols if set, otherwise calculate based on screen size with some padding. Ensure the view fits within the screen dimensions.

Definition at line 245 of file init_view.c.

245 {
246 View *view = init->view;
247
251 uint scr_lines, scr_cols;
252 ui_get_screen_size(&scr_lines, &scr_cols);
253#ifdef DEBUG_RESIZE
254 ssnprintf(em0, MAXLEN - 1,
255 _("%s:%d=%d calc lines=%d, cols=%d, begy=%d, begx=%d"),
256 __FILE__, __LINE__, 526, view->lines, view->cols, view->begy, view->begx);
257 write_cmenu_log(em0);
258#endif
259 if (view->lines == 0 && view->cols == 0 && view->begy == 0 && view->begx == 0) {
260 view->lines = scr_lines - 3;
261 view->cols = scr_cols - 2;
262 view->begy = 0;
263 view->begx = 0;
264 }
265 uint lines = max(scr_lines / 10, 8);
266 view->lines = max(view->lines, lines);
267 if (view->lines > scr_lines - 3)
268 view->lines = scr_lines - 3;
269 if (view->lines + view->begy > scr_lines - 3)
270 view->begy = scr_lines - (view->lines + 3);
271
272 uint cols = max(scr_cols / 3, 40);
273 view->cols = max(view->cols, cols);
274 if (view->cols > scr_cols - 2)
275 view->cols = scr_cols - 2;
276 if (view->cols + view->begx > scr_cols - 2)
277 view->begx = scr_cols - (view->cols + 2);
278
279 view->h_shift = view->cols / 3;
280 view->ln_win_lines = view->lines - 1;
281 if (view->f_ln)
282 view->ln_win_cols = 8;
283 else
284 view->ln_win_cols = 0;
285 view->scroll_lines = view->lines - 1;
286 view->cmd_line = 0;
287
288 view->pminrow = 0;
289 view->pmincol = 0;
290 view->sminrow = view->begy + 1;
291 view->smincol = view->begx + view->ln_win_cols;
292 view->smaxrow = view->begy + view->lines;
293 view->smaxcol = view->begx + view->cols;
294
295 view->ln_no = view->page_top_ln_no + view->scroll_lines;
296 view->page_bot_ln_no = view->ln_no;
297#ifdef DEBUG_RESIZE
298 ssnprintf(em0, MAXLEN - 1,
299 _("%s:%d calc BOX lines=%d, cols=%d, begy=%d, begx=%d"),
300 __FILE__, __LINE__, view->lines + 2, view->cols + 2, view->begy, view->begx);
301 write_cmenu_log(em0);
302 ssnprintf(em0, MAXLEN - 1,
303 _("%s:%d calc WIN lines=%d, cols=%d, begy=%d, begx=%d"),
304 __FILE__, __LINE__, view->lines, view->cols, 1, 1);
305 write_cmenu_log(em0);
306 ssnprintf(em0, MAXLEN - 1,
307 _("%s:%d calc CMDLN lines=%d, cols=%d, begy=%d, begx=%d"),
308 __FILE__, __LINE__, 1, view->cols, view->lines - 1, 1);
309 write_cmenu_log(em0);
310 ssnprintf(em0, MAXLEN - 1,
311 _("%s:%d calc LNNO lines=%d, cols=%d, begy=%d, begx=%d"),
312 __FILE__, __LINE__, view->lines - 1, view->ln_win_cols, 0, 0);
313 write_cmenu_log(em0);
314 ssnprintf(em0, MAXLEN - 1,
315 _("%s:%d calc PAD lines=%d, cols=%d, begy=%d, begx=%d"),
316 __FILE__, __LINE__, view->lines - 1, view->cols - view->ln_win_cols, 0, view->ln_win_cols);
317 write_cmenu_log(em0);
318
319#endif
320}
void ui_get_screen_size(uint *lines, uint *cols)
Get the current screen size (number of lines and columns).
Definition ui_ncurses.c:754
#define MAXLEN
Definition lf.h:38
#define max(a, b)
Definition lf.h:152
char em0[MAXLEN]
Definition lf.c:42
#define _(String)
Definition amort.c:23
size_t ssnprintf(char *, size_t, const char *,...)
Custom safe string formatting utility (Variadic version).
Definition futil.c:398

References View::begx, View::begy, View::cmd_line, View::cols, View::f_ln, View::h_shift, View::lines, View::ln_no, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln_no, View::page_top_ln_no, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, ui_get_screen_size(), and Init::view.

Referenced by init_view_boxwin().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_calc_full_screen_dimensions()

void view_calc_full_screen_dimensions ( Init * init)

Calculate the dimensions for full screen mode.

This function calculates the dimensions for the full screen mode of the C-Menu View. It retrieves the maximum dimensions of the standard screen and sets the view parameters accordingly. It also resizes the line number window and command line window based on the new dimensions.

Parameters
initPointer to the Init structure containing view settings.

Definition at line 110 of file init_view.c.

110 {
111 View *view = init->view;
112 ui_get_screen_size(&view->lines, &view->cols);
113 view->ln_win_lines = view->lines;
114 view->ln_win_cols = 8;
115 view->scroll_lines = view->lines - 1;
116 view->h_shift = view->cols / 3;
117#ifdef DEBUG_RESIZE
118 char file[MAXLEN];
119 ssnprintf(
120 em0, MAXLEN - 1,
121 _("%s:%d view->lines=%d, view->cols=%d, view->maxrows=%d, view->maxcols=%d"),
122 __FILE__, __LINE__,
123 view->lines, view->cols, view->smaxrow, view->smaxcol);
124 write_cmenu_log(em0);
125#endif
126
127 view->pminrow = 0;
128 view->pmincol = 0;
129 view->sminrow = 0;
130 view->smincol = view->ln_win_cols;
131 view->smaxrow = view->lines - 1;
132 view->smaxcol = view->cols - 1;
133
134 view->ln_no = view->page_top_ln_no + view->scroll_lines;
135 view->page_bot_ln_no = view->ln_no;
136}

References View::cols, View::h_shift, View::lines, View::ln_no, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln_no, View::page_top_ln_no, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, ui_get_screen_size(), and Init::view.

Referenced by init_view_full_screen(), and view_full_screen_resize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ view_init_input()

int view_init_input ( Init * init,
char * file_name )

Initialize the input for the C-Menu View.

Parameters
initPointer to the Init structure containing view settings.
file_nameName of the input file or "-" for standard input.
Returns
0 on success, -1 on failure.

This function initializes the input for the C-Menu View. It handles both regular files and standard input, setting up pipes if necessary. It also memory-maps the input file for efficient access and sets up the view structure accordingly.

Prevent child process from writing to terminal

Definition at line 331 of file init_view.c.

331 {
332 struct stat sb;
333 int idx = 0;
334 pid_t pid = -1;
335 int pipe_fd[2];
336 int s_argc = 0;
337 char *s_argv[MAXARGS];
338 char tmp_str[MAXLEN];
339 View *view = init->view;
340 view->f_in_pipe = false;
341 if (strcmp(file_name, "-") == 0) {
342 file_name = _("/dev/stdin");
343 view->f_in_pipe = true;
344 }
345 if (view->provider_cmd[0] != '\0') {
346 s_argc = str_to_args(s_argv, view->provider_cmd, MAXARGS - 1);
347 if (pipe(pipe_fd) == -1) {
348 ui_perror(_("pipe(pipe_fd) failed in init_view"));
349 return -1;
350 }
351 // endwin();
352 if ((pid = fork()) == -1) {
353 ui_perror(_("fork() failed in init_view"));
354 return -1;
355 }
356 if (pid == 0) { // Child
358 int dev_null = open(_("/dev/null"), O_WRONLY);
359 if (dev_null == -1) {
360 ui_perror(_("open(/dev/null) failed in init_pick child process"));
361 exit(EXIT_FAILURE);
362 }
363 dup2(dev_null, STDERR_FILENO);
364 close(dev_null);
365 close(pipe_fd[P_READ]);
366 dup2(pipe_fd[P_WRITE], STDOUT_FILENO);
367 close(pipe_fd[P_WRITE]);
368 execvp(s_argv[0], s_argv);
369 strnz__cpy(tmp_str, _("Can't exec view start cmd: "), MAXLEN - 1);
370 strnz__cat(tmp_str, s_argv[0], MAXLEN - 1);
371 ui_perror(tmp_str);
372 exit(EXIT_FAILURE);
373 }
374 // Back to parent
375 destroy_argv(s_argc, s_argv);
378 close(pipe_fd[P_WRITE]);
379 view->in_fd = dup(pipe_fd[P_READ]);
380 view->f_in_pipe = true;
381 } else {
382 if (view->f_in_pipe)
383 view->in_fd = dup(STDIN_FILENO);
384 else {
385 /*----------------------------------------------------------------------*/
386 /* Open the input file for reading and get its size. */
387 expand_tilde(file_name, MAXLEN - 1);
388 view->in_fd = open(file_name, O_RDONLY);
389 if (view->in_fd == -1) {
390 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__,
391 __LINE__ - 3);
392 ssnprintf(em1, MAXLEN - 1, _("open %s"), file_name);
393 strerror__r(errno, em2, MAXLEN);
395 nullptr);
396 return -1;
397 }
398 if (fstat(view->in_fd, &sb) == -1) {
399 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__,
400 __LINE__ - 1);
401 ssnprintf(em1, MAXLEN - 1, _("fstat %s"), file_name);
402 strerror__r(errno, em2, MAXLEN);
403 ui_display_error(em0, em1, em2, nullptr);
404 close(view->in_fd);
405 return -1;
406 }
407 view->file_size = sb.st_size;
408 if (view->file_size == 0) {
409 close(view->in_fd);
410 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__,
411 __LINE__ - 1);
412 ssnprintf(em1, MAXLEN - 1, _("file %s is empty"), file_name);
413 strerror__r(errno, em2, MAXLEN);
414 ui_display_error(em0, em1, em2, nullptr);
415 return -1;
416 }
417 if (!S_ISREG(sb.st_mode))
418 view->f_in_pipe = true;
419 }
420 /*----------------------------------------------------------------------*/
421 }
422 if (view->f_in_pipe) {
423 errno = 0;
424 view->tmp_fd = memfd_create(_("view_input"), MFD_CLOEXEC);
425 if (view->in_fd < 0 || errno != 0) {
426 ssnprintf(em0, MAXLEN - 1, "memfd_create failed\n");
427 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
428 ssnprintf(em2, MAXLEN - 1, _("%s, line: %d, errno: %d"), __FILE__,
429 __LINE__ - 4, errno);
430 ui_display_error(em0, em1, em2, nullptr);
431 exit(EXIT_FAILURE);
432 }
433 // ------------------------------------------------------
434 // transfer view->in_fd to view->tmp_fd
435 // ------------------------------------------------------
436 char buf[VBUFSIZ];
437 ssize_t bytes_read = 0;
438 // ------------------------------------------------------
439 // countdown to timeout if view->in_fd not ready
440 // ------------------------------------------------------
443 int status = ui_poll_reg_read_fd(500);
444 if (status == 0) {
445 int timeout_ms = init->timeout_secs * 1000;
446 status = ui_timeout_prompt(_("Waiting for input..."), _("from view_input"), timeout_ms, init->timeout_secs);
448 if (status != UIKEY_STREAM_DATA) {
449 if (status == UIKEY_F09)
450 cmenu_abend(init, -1, _("view: user pressed F9"));
451 if (status == UIKEY_TIMEOUT)
452 cmenu_abend(init, -1, _("view: timed out waiting for input"));
453 cmenu_abend(init, -1, _("view: no input data"));
454 }
455 }
456 // ------------------------------------------------------
457 // end wait
458 // ------------------------------------------------------
459 bytes_read = read(view->in_fd, buf, sizeof(buf));
460 if (bytes_read > 0) {
461 if (write(view->tmp_fd, buf, bytes_read) != bytes_read)
462 cmenu_abend(init, EXIT_FAILURE, _("view: unable to write view->tmp_fd"));
463 } else if (bytes_read == 0)
464 cmenu_abend(init, EXIT_FAILURE, _("view: unable to read view->tmp_fd"));
465 // ------------------------------------------------------
466 // end transfer
467 // ------------------------------------------------------
468 if (fstat(view->tmp_fd, &sb) == -1) {
469 ssnprintf(em0, MAXLEN - 1, "fstat(view->in_fd) failed\n");
470 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
471 ssnprintf(em2, MAXLEN - 1, _("%s, line: %d, errno: %d"), __FILE__,
472 __LINE__ - 4, errno);
473 ui_display_error(em0, em1, em2, nullptr);
474 exit(EXIT_FAILURE);
475 }
476 view->file_size = sb.st_size;
477 if (view->file_size == 0) {
478 close(view->tmp_fd);
479 strnz__cpy(tmp_str, _("view: no standard input"), MAXLEN - 1);
480 ui_abend(-1, tmp_str);
481 exit(EXIT_FAILURE);
482 }
483 waitpid(-1, nullptr, 0);
484 view->in_fd = view->tmp_fd;
485 }
486 view->buf =
487 mmap(nullptr, view->file_size, PROT_READ, MAP_PRIVATE, view->in_fd, 0);
488 if (view->buf == MAP_FAILED) {
489 ssnprintf(em0, MAXLEN - 1, _("%s, line: %d"), __FILE__, __LINE__ - 2);
490 ssnprintf(em1, MAXLEN - 1, _("mmap %s"), file_name);
491 strerror__r(errno, em2, MAXLEN);
492 ui_display_error(em0, em1, em2, nullptr);
493 close(view->in_fd);
494 return -1;
495 }
496 close(view->in_fd);
497 stdio_fdnames(stdio_names_str, _("init_view.c 422"));
498 view->file_size = sb.st_size;
499 view->prev_file_pos = NULL_POSITION;
500 view->buf_curr_ptr = view->buf;
501 if (view->cmd_all[0] != '\0')
502 strnz__cpy(view->cmd, view->cmd_all, MAXLEN - 1);
503 for (idx = 0; idx < NMARKS; idx++)
504 view->mark_tbl[idx] = NULL_POSITION;
505 strnz__cpy(view->cur_file_str, file_name, MAXLEN - 1);
506 base_name(view->file_name, view->cur_file_str);
507 return 0;
508}
#define P_READ
Definition common.h:53
#define P_WRITE
Definition common.h:54
char stdio_names_str[4096]
Definition futil.c:121
#define MAXARGS
Definition cm.h:67
char * stdio_fdnames(char *, char *)
Definition futil.c:1130
void ui_restore_wins()
Restore the state of all windows in the UI. This function refreshes the standard screen and all surfa...
#define NMARKS
Definition view.h:33
#define VBUFSIZ
Definition view.h:37
#define NULL_POSITION
Definition view.h:36
#define UIKEY_TIMEOUT
#define UIKEY_STREAM_DATA
#define UIKEY_F09
void ui_flush_input()
Flush any pending input events from the NotCurses context. This function discards any input events th...
void ui_abend(int ec, char *s)
Convert multibyte string to complex character array.
Definition ui_common.c:404
int pipe_fd[2]
Definition pick_engine.c:46
void cmenu_abend(Init *init, int ec, char *s)
Definition menu.c:94
char em1[MAXLEN]
Definition lf.c:43
char em2[MAXLEN]
Definition lf.c:44
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition lf.c:2299
int str_to_args(char **, char *, uint)
Converts a string into an array of argument strings.
Definition futil.c:418
bool expand_tilde(char *, uint)
Expand a tilde (~) in a file path to the user's home directory.
Definition lf.c:2089
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition lf.c:2330
char * strerror__r(int errnum, char *buf, size_t buflen)
A thread-safe alternative to strerror() that writes the error message corresponding to errnum into th...
Definition futil.c:770
int destroy_argv(uint argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:474
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1154
void sig_prog_mode()
Set up signal handlers for interrupt signals.
Definition sig.c:61
int ui_perror(char *emsg_str)
Display a simple error message window or print to stderr.
Definition ui_common.c:1323
void ui_unregister_read_fd(int fd)
Unregister an external file descriptor from polling.
Definition ui_common.c:1195
void ui_register_read_fd(int fd, int token_id)
Register an external file descriptor for polling.
Definition ui_common.c:1178
int ui_timeout_prompt(char *msg0, char *msg1, uint timeout_ms, uint timeout_secs)
Display a timeout prompt window or print to stderr.
Definition ui_common.c:1094
int ui_poll_reg_read_fd(int timeout_ms)
Poll for input on registered external files.
Definition ui_common.c:1216
int ui_display_error(char *msg0, char *msg1, char *msg2, char *msg3)
Display an error message window or print to stderr.
Definition ui_common.c:1261
uint timeout_secs
Definition common.h:182

References base_name(), View::buf, View::buf_curr_ptr, View::cmd, View::cmd_all, cmenu_abend(), View::cur_file_str, destroy_argv(), em0, em1, em2, expand_tilde(), View::f_in_pipe, View::file_name, View::file_size, View::in_fd, View::mark_tbl, View::prev_file_pos, View::provider_cmd, sig_prog_mode(), ssnprintf(), stdio_fdnames(), stdio_names_str, str_to_args(), strerror__r(), strnz__cat(), strnz__cpy(), Init::timeout_secs, View::tmp_fd, ui_abend(), ui_display_error(), ui_flush_input(), ui_perror(), ui_poll_reg_read_fd(), ui_register_read_fd(), ui_restore_wins(), ui_timeout_prompt(), ui_unregister_read_fd(), and Init::view.

Referenced by new_view_file(), and view_file().

Here is the call graph for this function:
Here is the caller graph for this function: