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 194 of file init_view.c.

194 {
195 View *view = init->view;
196 if (view->tab_stop <= 0)
197 view->tab_stop = TABSIZE;
198 set_tabsize(view->tab_stop);
199 view->f_full_screen = false;
201 // -------------------> 1. BOX <-------------------
202 view->box_win = newwin(view->lines + 2, view->cols + 2, view->begy, view->begx);
203 if (view->box_win == nullptr) {
204 ssnprintf(em0, MAXLEN - 1, "view->box_win: lines=%d, cols=%d, begy=%d, begx=%d",
205 view->lines + 2, view->cols + 2, view->begy, view->begx);
206 Perror(em0);
207 return -1;
208 }
209 view->box_pan = new_panel(view->box_win);
210 wbkgrnd(view->box_win, &CC_BOX);
211 wbkgrndset(view->box_win, &CC_BOX);
212#ifdef DEBUG_RESIZE
213 ssnprintf(em0, MAXLEN - 1,
214 "%s:%d init BOX: lines=%d, cols=%d, begy=%d, begx=%d",
215 __FILE__, __LINE__, view->lines + 2, view->cols + 2, view->begy, view->begx);
217#endif
218 wborder_set(view->box_win, &ls, &rs, &ts, &bs, &tl, &tr, &bl, &br);
219
220 // -------------------> 2. WIN <-------------------
221 view->win_win = derwin(view->box_win, view->lines, view->cols, 1, 1);
222 if (view->win_win == nullptr) {
223 ssnprintf(em0, MAXLEN - 1, "%s:%d WIN: lines=%d, cols=%d, begy=%d, begx=%d",
224 __FILE__, __LINE__, view->lines, view->cols, 1, 1);
225 Perror(em0);
226 return -1;
227 }
228 view->win_pan = new_panel(view->win_win);
229 wbkgrnd(view->win_win, &CC_NT);
230 wbkgrndset(view->win_win, &CC_NT);
231 // -------------------> 3. CMDLN <-------------------
232 view->cmdln_win = derwin(view->win_win, 1, view->cols, view->lines - 1, 0);
233 if (view->cmdln_win == nullptr) {
234 ssnprintf(em0, MAXLEN - 1, "%s:%d CMDLN: lines=%d, cols=%d, begy=%d, begx=%d",
235 __FILE__, __LINE__, 1, view->cols, view->lines - 1, 0);
236 Perror(em0);
237 return -1;
238 }
239 view->cmdln_pan = new_panel(view->cmdln_win);
240 wbkgrnd(view->cmdln_win, &CC_NT);
241 wbkgrndset(view->cmdln_win, &CC_NT);
242 keypad(view->cmdln_win, true);
243 idlok(view->cmdln_win, false);
244 idcok(view->cmdln_win, false);
245 scrollok(view->cmdln_win, false);
246
247 // -------------------> 4. LNNO <-------------------
248 view->lnno_win = derwin(view->win_win, view->lines - 1, view->ln_win_cols, 0, 0);
249 if (view->lnno_win == nullptr) {
250 ssnprintf(em0, MAXLEN - 1, "%s:%d LNNO: lines=%d, cols=%d, begy=%d, begx=%d",
251 __FILE__, __LINE__, view->lines - 1, view->ln_win_cols, 0, 0);
252 Perror(em0);
253 return -1;
254 }
255 view->lnno_pan = new_panel(view->lnno_win);
256 wbkgrnd(view->lnno_win, &CC_LN);
257 wbkgrndset(view->lnno_win, &CC_LN);
258 keypad(view->lnno_win, false);
259 idlok(view->lnno_win, false);
260 idcok(view->lnno_win, false);
261 scrollok(view->lnno_win, true);
262 wsetscrreg(view->lnno_win, 0, view->scroll_lines);
263
264 // -------------------> 5. PAD CONTAINER <---------
265 // view->pad_container_win = derwin(view->win_win, view->lines - 1,
266 // view->cols - view->ln_win_cols, 0, view->ln_win_cols);
267 // if (view->pad_container_win == nullptr) {
268 // ssnprintf(em0, MAXLEN - 1,
269 // "derwin(view->win_win, view->lines - 1, view->cols -
270 // view->ln_win_cols, 0, view->ln_win_cols) failed in
271 // init_view_full_screen");
272 // Perror(em0);
273 // return -1;
274 // }
275 // view->pad_container_pan = new_panel(view->pad_container_win);
276
277 // -------------------> 5. PAD <-------------------
278 view->pad = newpad(view->lines - 1, PAD_COLS - 1);
279 if (view->pad == nullptr) {
280 ssnprintf(em0, MAXLEN - 1, "newpad(view->lines - 1, PAD_COLS - 1) failed in init_view_full_screen");
281 Perror(em0);
282 return -1;
283 }
284 // view->pad = newpad(view->lines - 1, PAD_COLS - 1);
285
286 // -------------------> 5. PAD_VIEW <--------------
287 view->pad_view_win = subpad(view->pad,
288 view->lines - 1,
289 PAD_COLS - 1,
290 0,
291 0);
292 if (view->pad_view_win == nullptr) {
293 ssnprintf(em0, MAXLEN - 1,
294 "%s:%d PAD: lines=%d, cols=%d, begy=%d, begx=%d",
295 __FILE__, __LINE__,
296 view->lines - 1,
297 view->cols - view->ln_win_cols,
298 0,
299 0);
300 Perror(em0);
301 return -1;
302 }
303 view->pad_view_pan = new_panel(view->pad_view_win);
304
305 // -----------------------------
306#ifdef DEBUG_LAYOUT
307 wbkgrnd(view->pad, &CC_GREEN);
308 wbkgrndset(view->pad, &CC_GREEN);
309#else
310 wbkgrnd(view->pad, &CC_NT);
311 wbkgrndset(view->pad, &CC_NT);
312#endif
313 scrollok(view->pad, true);
314 wsetscrreg(view->pad, 0, view->scroll_lines - 1);
315 return (0);
316}
cchar_t tl
Definition cm.h:501
cchar_t br
Definition cm.h:501
cchar_t CC_NT
Definition dwin.c:204
cchar_t rs
Definition cm.h:501
cchar_t CC_GREEN
Definition dwin.c:215
cchar_t CC_BOX
Definition dwin.c:199
cchar_t bl
Definition cm.h:501
char em0[MAXLEN]
Definition dwin.c:175
cchar_t ts
Definition cm.h:501
cchar_t tr
Definition cm.h:501
cchar_t ls
Definition cm.h:631
cchar_t bs
Definition cm.h:501
cchar_t CC_LN
Definition dwin.c:201
#define PAD_COLS
Definition view.h:32
View * view
Definition mem.c:49
#define MAXLEN
Definition curskeys.c:15
int Perror(char *)
Display a simple error message window or print to stderr.
Definition dwin.c:1526
size_t ssnprintf(char *, size_t, const char *,...)
ssnprintf was designed to be a safer alternative to snprintf.
Definition futil.c:420
void write_cmenu_log_nt(char *)
Write message to C-Menu log file without timestamp.
Definition futil.c:1701
void view_calc_boxwin_dimensions(Init *)
Calculate the dimensions and position of the box window for C-Menu View.
Definition init_view.c:362
View * view
Definition common.h:188
Definition view.h:42

References View::begx, View::begy, bl, View::box_pan, View::box_win, br, bs, CC_BOX, CC_LN, CC_NT, View::cmdln_pan, View::cmdln_win, View::cols, em0, View::f_full_screen, View::lines, View::ln_win_cols, View::lnno_pan, View::lnno_win, ls, View::pad, View::pad_view_pan, View::pad_view_win, Perror(), rs, View::scroll_lines, ssnprintf(), View::tab_stop, tl, tr, ts, Init::view, view_calc_boxwin_dimensions(), View::win_pan, and View::win_win.

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->cmdln_win: Status or Command Line
2. view->lnno_win: Line Number Window
3. view->pad: Main Content Pad

Definition at line 49 of file init_view.c.

49 {
50 View *view = init->view;
51
52 if (view->tab_stop <= 0)
53 view->tab_stop = TABSIZE;
54 set_tabsize(view->tab_stop);
55 view->f_full_screen = true;
56
57 // -------------------> 1. WIN <-------------------
59 view->win_win = newwin(LINES, COLS, 0, 0);
60 if (view->win_win == nullptr) {
61 ssnprintf(em0, MAXLEN - 1, "newwin(LINES, COLS, 0, 0) failed in init_view_full_screen");
62 Perror(em0);
63 return -1;
64 }
65 view->win_pan = new_panel(view->win_win);
66 wbkgrnd(view->win_win, &CC_NT);
67
68 // -------------------> 2. LNNO <-------------------
69 view->lnno_win = derwin(view->win_win, LINES - 1, COLS, 0, 0);
70 if (view->lnno_win == nullptr) {
71 ssnprintf(em0, MAXLEN - 1, "derwin(view->win_win, LINES - 1, COLS, 0, 0) failed in init_view_full_screen");
72 Perror(em0);
73 return -1;
74 }
75 view->lnno_pan = new_panel(view->lnno_win);
76 wbkgrnd(view->lnno_win, &CC_LN);
77 keypad(view->lnno_win, false);
78 idlok(view->lnno_win, false);
79 idcok(view->lnno_win, false);
80 scrollok(view->lnno_win, true);
81 wsetscrreg(view->lnno_win, 0, view->scroll_lines - 1);
82
83 // -------------------> 3. CMDLN <-------------------
84 view->cmdln_win = derwin(view->win_win, 1, COLS, LINES - 1, 0);
85 if (view->cmdln_win == nullptr) {
86 ssnprintf(em0, MAXLEN - 1, "derwin(view->win_win, 1, COLS, LINES - 1, 0) failed in init_view_full_screen");
87 Perror(em0);
88 return -1;
89 }
90 view->cmdln_pan = new_panel(view->cmdln_win);
91 wbkgrnd(view->cmdln_win, &CC_NT);
92 keypad(view->cmdln_win, true);
93 idlok(view->cmdln_win, false);
94 idcok(view->cmdln_win, false);
95 scrollok(view->cmdln_win, false);
96
97 // view->pad_container_win = derwin(view->win_win, LINES - 1, COLS -
98 // view->ln_win_cols, 0, view->ln_win_cols);
99 // if (view->pad_container_win == nullptr) {
100 // ssnprintf(em0, MAXLEN - 1,
101 // "derwin(view->win_win, LINES - 1, COLS - view->ln_win_cols,
102 // 0, view->ln_win_cols) failed in init_view_full_screen");
103 // Perror(em0);
104 // return -1;
105 // }
106 // view->pad_container_pan = new_panel(view->pad_container_win);
107 // -------------------> 4. PAD <-------------------
108 view->pad = newpad(LINES - 1, PAD_COLS - 1);
109 if (view->pad == nullptr) {
110 ssnprintf(em0, MAXLEN - 1, "newpad(LINES - 1, PAD_COLS - 1) failed in init_view_full_screen");
111 Perror(em0);
112 return -1;
113 }
114 view->pad_view_win = subpad(view->pad, LINES - 1, PAD_COLS - 1, 0, 0);
115 immedok(view->pad, true);
116 if (view->pad_view_win == nullptr) {
117 ssnprintf(em0, MAXLEN - 1,
118 "subpad(view->pad, LINES - 1, COLS - view->ln_win_cols, 0, 0) failed in init_view_full_screen");
119 Perror(em0);
120 return -1;
121 }
122 view->pad_view_pan = new_panel(view->pad_view_win);
123 wbkgrnd(view->pad, &CC_NT);
124 keypad(view->pad, true);
125 keypad(view->pad, true);
126 idlok(view->pad, false);
127 idcok(view->pad, false);
128 scrollok(view->pad, true);
129 wsetscrreg(view->pad, 0, view->scroll_lines - 1);
130 return 0;
131}
void view_calc_full_screen_dimensions(Init *)
Calculate the dimensions for full screen mode.
Definition init_view.c:159

References CC_LN, CC_NT, View::cmdln_pan, View::cmdln_win, em0, View::f_full_screen, View::lnno_pan, View::lnno_win, View::pad, View::pad_view_pan, View::pad_view_win, Perror(), View::scroll_lines, ssnprintf(), View::tab_stop, Init::view, view_calc_full_screen_dimensions(), View::win_pan, and View::win_win.

Referenced by main().

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 362 of file init_view.c.

362 {
363 int scr_lines, scr_cols;
364 View *view = init->view;
365
369
370 getmaxyx(stdscr, scr_lines, scr_cols);
371#ifdef DEBUG_RESIZE
372 ssnprintf(em0, MAXLEN - 1,
373 "%s:%d=%d calc lines=%d, cols=%d, begy=%d, begx=%d",
374 __FILE__, __LINE__, 526, view->lines, view->cols, view->begy, view->begx);
376#endif
377 if (view->lines == 0 && view->cols == 0 && view->begy == 0 && view->begx == 0) {
378 view->lines = scr_lines - 3;
379 view->cols = scr_cols - 2;
380 view->begy = 0;
381 view->begx = 0;
382 }
383 int lines = max(scr_lines / 10, 8);
384 view->lines = max(view->lines, lines);
385 if (view->lines > scr_lines - 3)
386 view->lines = scr_lines - 3;
387 if (view->lines + view->begy > scr_lines - 3)
388 view->begy = scr_lines - (view->lines + 3);
389
390 int cols = max(scr_cols / 3, 40);
391 view->cols = max(view->cols, cols);
392 if (view->cols > scr_cols - 2)
393 view->cols = scr_cols - 2;
394 if (view->cols + view->begx > scr_cols - 2)
395 view->begx = scr_cols - (view->cols + 2);
396
397 view->h_shift = view->cols / 3;
398 view->ln_win_lines = view->lines - 1;
399 if (view->f_ln)
400 view->ln_win_cols = 8;
401 else
402 view->ln_win_cols = 0;
403 view->scroll_lines = view->lines - 1;
404 view->cmd_line = 0;
405 view->pminrow = 0;
406 view->pmincol = 0;
407 view->sminrow = view->begy + 1;
408 view->smincol = view->begx + view->ln_win_cols + 1;
409 view->smaxrow = view->begy + view->lines;
410 view->smaxcol = view->begx + view->cols;
411 view->ln = view->page_top_ln + view->scroll_lines;
412 view->page_bot_ln = view->ln;
413#ifdef DEBUG_RESIZE
414 ssnprintf(em0, MAXLEN - 1,
415 "%s:%d calc BOX lines=%d, cols=%d, begy=%d, begx=%d",
416 __FILE__, __LINE__, view->lines + 2, view->cols + 2, view->begy, view->begx);
418 ssnprintf(em0, MAXLEN - 1,
419 "%s:%d calc WIN lines=%d, cols=%d, begy=%d, begx=%d",
420 __FILE__, __LINE__, view->lines, view->cols, 1, 1);
422 ssnprintf(em0, MAXLEN - 1,
423 "%s:%d calc CMDLN lines=%d, cols=%d, begy=%d, begx=%d",
424 __FILE__, __LINE__, 1, view->cols, view->lines - 1, 1);
426 ssnprintf(em0, MAXLEN - 1,
427 "%s:%d calc LNNO lines=%d, cols=%d, begy=%d, begx=%d",
428 __FILE__, __LINE__, view->lines - 1, view->ln_win_cols, 0, 0);
430 ssnprintf(em0, MAXLEN - 1,
431 "%s:%d calc PAD lines=%d, cols=%d, begy=%d, begx=%d",
432 __FILE__, __LINE__, view->lines - 1, view->cols - view->ln_win_cols, 0, view->ln_win_cols);
434
435#endif
436}
#define max(a, b)
max macro evaluates two expressions, returning greatest result.
Definition cm.h:82

References View::begx, View::begy, View::cmd_line, View::cols, View::f_ln, View::h_shift, View::lines, View::ln, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln, View::page_top_ln, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, and Init::view.

Referenced by init_view_boxwin().

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 159 of file init_view.c.

159 {
160 View *view = init->view;
161 getmaxyx(stdscr, view->lines, view->cols);
162 view->ln_win_lines = view->lines;
163 view->ln_win_cols = 8;
164 view->scroll_lines = view->lines - 1;
165 view->h_shift = view->cols / 3;
166#ifdef DEBUG_RESIZE
167 char file[MAXLEN];
168 ssnprintf(
169 em0, MAXLEN - 1,
170 "%s:%d view->lines=%d, view->cols=%d, view->maxrows=%d, view->maxcols=%d",
171 __FILE__, __LINE__,
172 view->lines, view->cols, view->smaxrow, view->smaxcol);
174#endif
175 view->cmd_line = 0;
176 view->pminrow = 0;
177 view->pmincol = 0;
178 view->sminrow = 0;
179 view->smincol = view->ln_win_cols;
180 view->smaxrow = view->lines - 1;
181 view->smaxcol = view->cols - 1;
182 view->ln = view->page_top_ln + view->scroll_lines;
183 view->page_bot_ln = view->ln;
184}

References View::cmd_line, View::cols, View::h_shift, View::lines, View::ln, View::ln_win_cols, View::ln_win_lines, View::page_bot_ln, View::page_top_ln, View::pmincol, View::pminrow, View::scroll_lines, View::smaxcol, View::smaxrow, View::smincol, View::sminrow, and Init::view.

Referenced by init_view_full_screen(), and view_full_screen_resize().

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 447 of file init_view.c.

447 {
448 struct stat sb;
449 int idx = 0;
450 pid_t pid = -1;
451 int pipe_fd[2];
452 int s_argc = 0;
453 char *s_argv[MAXARGS];
454 char tmp_str[MAXLEN];
455 View *view = init->view;
456 view->f_in_pipe = false;
457 if (strcmp(file_name, "-") == 0) {
458 file_name = "/dev/stdin";
459 view->f_in_pipe = true;
460 }
461 if (view->provider_cmd[0] != '\0') {
462 s_argc = str_to_args(s_argv, view->provider_cmd, MAXARGS - 1);
463 if (pipe(pipe_fd) == -1) {
464 Perror("pipe(pipe_fd) failed in init_view");
465 return -1;
466 }
467 // endwin();
468 if ((pid = fork()) == -1) {
469 Perror("fork() failed in init_view");
470 return -1;
471 }
472 if (pid == 0) { // Child
474 int dev_null = open("/dev/null", O_WRONLY);
475 if (dev_null == -1) {
476 Perror("open(/dev/null) failed in init_pick child process");
477 exit(EXIT_FAILURE);
478 }
479 dup2(dev_null, STDERR_FILENO);
480 close(dev_null);
481 close(pipe_fd[P_READ]);
482 dup2(pipe_fd[P_WRITE], STDOUT_FILENO);
483 close(pipe_fd[P_WRITE]);
484 execvp(s_argv[0], s_argv);
485 strnz__cpy(tmp_str, "Can't exec view start cmd: ", MAXLEN - 1);
486 strnz__cat(tmp_str, s_argv[0], MAXLEN - 1);
487 Perror(tmp_str);
488 exit(EXIT_FAILURE);
489 }
490 // Back to parent
491 destroy_argv(s_argc, s_argv);
492 reset_prog_mode();
493 restore_wins();
494 close(pipe_fd[P_WRITE]);
495 dup2(pipe_fd[P_READ], STDIN_FILENO);
496 view->in_fd = dup(STDIN_FILENO);
497 view->f_in_pipe = true;
498 } else {
499 if (view->f_in_pipe)
500 view->in_fd = dup(STDIN_FILENO);
501 else {
502 /*----------------------------------------------------------------------*/
503 /* Open the input file for reading and get its size. */
504 expand_tilde(file_name, MAXLEN - 1);
505 view->in_fd = open(file_name, O_RDONLY);
506 if (view->in_fd == -1) {
507 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
508 __LINE__ - 3);
509 ssnprintf(em1, MAXLEN - 1, "open %s", file_name);
510 strerror_r(errno, em2, MAXLEN);
511 display_error(em0, em1, em2, nullptr);
512 return -1;
513 }
514 if (fstat(view->in_fd, &sb) == -1) {
515 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
516 __LINE__ - 1);
517 ssnprintf(em1, MAXLEN - 1, "fstat %s", file_name);
518 strerror_r(errno, em2, MAXLEN);
519 display_error(em0, em1, em2, nullptr);
520 close(view->in_fd);
521 return -1;
522 }
523 view->file_size = sb.st_size;
524 if (view->file_size == 0) {
525 close(view->in_fd);
526 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__,
527 __LINE__ - 1);
528 ssnprintf(em1, MAXLEN - 1, "file %s is empty", file_name);
529 strerror_r(errno, em2, MAXLEN);
530 display_error(em0, em1, em2, nullptr);
531 return -1;
532 }
533 if (!S_ISREG(sb.st_mode))
534 view->f_in_pipe = true;
535 }
536 /*----------------------------------------------------------------------*/
537 }
538 if (view->f_in_pipe) {
539 close(view->in_fd);
540 errno = 0;
541 view->in_fd = memfd_create("view_input", MFD_CLOEXEC);
542 if (view->in_fd < 0 || errno != 0) {
543 ssnprintf(em0, MAXLEN - 1, "memfd_create failed\n");
544 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
545 ssnprintf(em2, MAXLEN - 1, "%s, line: %d, errno: %d", __FILE__,
546 __LINE__ - 4, errno);
547 display_error(em0, em1, em2, nullptr);
548 exit(EXIT_FAILURE);
549 }
550 char buf[VBUFSIZ];
551 ssize_t bytes_read = 0;
552 ssize_t bytes_written = 0;
553 while ((bytes_read = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
554 if ((bytes_written = write(view->in_fd, buf, bytes_read)) != bytes_read) {
555 abend(-1, "unable to write view->in_fd");
556 exit(EXIT_FAILURE);
557 }
558 }
559 if (fstat(view->in_fd, &sb) == -1) {
560 ssnprintf(em0, MAXLEN - 1, "fstat(view->in_fd) failed\n");
561 ssnprintf(em1, MAXLEN - 1, "%s", strerror(errno));
562 ssnprintf(em2, MAXLEN - 1, "%s, line: %d, errno: %d", __FILE__,
563 __LINE__ - 4, errno);
564 display_error(em0, em1, em2, nullptr);
565 exit(EXIT_FAILURE);
566 }
567 view->file_size = sb.st_size;
568 if (view->file_size == 0) {
569 close(view->in_fd);
570 strnz__cpy(tmp_str, "no standard input", MAXLEN - 1);
571 abend(-1, tmp_str);
572 exit(EXIT_FAILURE);
573 }
574 waitpid(-1, nullptr, 0);
575 }
576 view->buf =
577 mmap(nullptr, view->file_size, PROT_READ, MAP_PRIVATE, view->in_fd, 0);
578 if (view->buf == MAP_FAILED) {
579 ssnprintf(em0, MAXLEN - 1, "%s, line: %d", __FILE__, __LINE__ - 2);
580 ssnprintf(em1, MAXLEN - 1, "mmap %s", file_name);
581 strerror_r(errno, em2, MAXLEN);
582 display_error(em0, em1, em2, nullptr);
583 close(view->in_fd);
584 return -1;
585 }
586 close(view->in_fd);
587 SIO *sio = init->sio;
588 stdio_names(stdio_names_str, "init_view.c 673");
589 stdio_fdnames(stdio_names_str, "init_view.c 674");
590 dup2(sio->stdin_fd, STDIN_FILENO);
591 stdio_names(stdio_names_str, "init_view.c 673");
592 stdio_fdnames(stdio_names_str, "init_view.c 674");
593 view->file_size = sb.st_size;
594 view->prev_file_pos = NULL_POSITION;
595 view->buf_curr_ptr = view->buf;
596 if (view->cmd_all[0] != '\0')
597 strnz__cpy(view->cmd, view->cmd_all, MAXLEN - 1);
598 for (idx = 0; idx < NMARKS; idx++)
599 view->mark_tbl[idx] = NULL_POSITION;
600 strnz__cpy(view->cur_file_str, file_name, MAXLEN - 1);
601 base_name(view->file_name, view->cur_file_str);
602 return 0;
603}
#define P_READ
Definition common.h:50
#define P_WRITE
Definition common.h:51
char em1[MAXLEN]
Definition dwin.c:176
#define MAXARGS
Definition cm.h:48
char * stdio_names(char *, char *)
Definition futil.c:1064
char stdio_names_str[MAXLEN]
Definition futil.c:134
char * stdio_fdnames(char *, char *)
Definition futil.c:1089
char em2[MAXLEN]
Definition dwin.c:177
#define NMARKS
Definition view.h:26
#define VBUFSIZ
Definition view.h:30
#define NULL_POSITION
Definition view.h:29
int pipe_fd[2]
Definition pick_engine.c:46
void restore_wins()
Restore all windows after a screen resize.
Definition dwin.c:1225
void abend(int, char *)
Abnormal program termination.
Definition dwin.c:2018
int display_error(char *, char *, char *, char *)
Display an error message window or print to stderr.
Definition dwin.c:1467
size_t strnz__cpy(char *, const char *, size_t)
safer alternative to strncpy
Definition futil.c:544
int destroy_argv(int argc, char **argv)
Deallocates memory allocated for argument strings in argv.
Definition futil.c:494
bool expand_tilde(char *, int)
Replaces "~/" in string with the user's home directory.
Definition futil.c:970
size_t strnz__cat(char *, const char *, size_t)
safer alternative to strncat
Definition futil.c:573
bool base_name(char *, char *)
Returns the base name of a file specification.
Definition futil.c:1123
int str_to_args(char **, char *, int)
Converts a string into an array of argument strings.
Definition futil.c:440
SIO * sio
Definition common.h:114
The SIO structure encapsulates various aspects of the terminal's state and configuration,...
Definition cm.h:805
int stdin_fd
Definition cm.h:860

References abend(), base_name(), View::buf, View::buf_curr_ptr, View::cmd, View::cmd_all, View::cur_file_str, destroy_argv(), display_error(), em0, em1, em2, expand_tilde(), View::f_in_pipe, View::file_name, View::file_size, View::in_fd, View::mark_tbl, Perror(), View::prev_file_pos, View::provider_cmd, restore_wins(), Init::sio, ssnprintf(), SIO::stdin_fd, stdio_fdnames(), stdio_names(), stdio_names_str, str_to_args(), strnz__cat(), strnz__cpy(), 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: