int main(int argc, char **argv) { struct termios oldline; t_area ar; if (argc > 1) { non_canonical_mode(&oldline); init_term(&ar, argv, argc); display_arguments(&ar); while ((ar.len = read(0, ar.buff, sizeof(ar.buff)))) { x_read(ar.len); init_lines(&ar, &oldline); check_ctrl(&ar); check_keys(&ar, ar.len); check_select(&ar); if (my_exit(&oldline, &ar) == 1 || void_exit(&ar, &oldline) == 1) return (EXIT_SUCCESS); } restore_mode(&oldline, &ar); free_struct(&ar); } else my_put_error(ERR_ARGV); return (EXIT_SUCCESS); }
char *my_read(int fd, int *i, int *rd, char *buffer) { if (buffer != NULL) free(buffer); *i = 0; buffer = x_malloc(sizeof(buffer) * (BUF_SIZE + 1)); if ((*rd = x_read(fd, buffer, BUF_SIZE)) <= 0) { free(buffer); return (NULL); } buffer[*rd] = '\0'; return (buffer); }
static void populate(int fd, uint64_t *dest, unsigned long nr) { unsigned long i, pfn; uint64_t entry; for (i=0; i < nr; ++i) { entry = pagemap[i]; if (PMAP_PRESENT & entry) { pfn = PMAP_PFN & entry; x_lseek(fd, pfn*sizeof(uint64_t), SEEK_SET); x_read(fd, dest+i, sizeof(uint64_t)); } else { dest[i] = 0ULL; } } }
static int print_file(WINDOW *w, int item) { /* int handle, error = 0, i, key, result = 0, r; DjV 033 010203 */ int handle, error = 0, i, result = 0; /* DjV 033 010203 */ char *buffer; const char *name; long l; /* index in buffer[] */ int ll = 0; /* DjV 031 150203 line length counter */ boolean stop = FALSE; if ((name = itm_fullname(w, item)) == NULL) return XFATAL; buffer = x_alloc(PBUFSIZ); if (buffer != NULL) { graf_mouse(HOURGLASS, NULL); if ((handle = x_open(name, O_DENYW | O_RDONLY)) >= 0) { do { if ((l = x_read(handle, PBUFSIZ, buffer)) >= 0) { for (i = 0; i < (int) l; i++) { /* DjV 031 150203 ---vvv--- */ /* * line wrap & new line handling; */ ll++; if ( (buffer[i] == (char)13) || (buffer[i] == (char)10) || (buffer[i] == (char)12) ) ll = 0; /* reset linelength counter at CR, LF or FF */ else if ( ll >= plinelen ) { ll = 0; if (( stop = print_eol() ) == TRUE) break; } /* DjV 031 150203 ---^^^--- */ if ((stop = prtchar(buffer[i])) == TRUE) break; } /* DjV 033 010203 ---vvv--- */ /* if ((r = key_state(&key, TRUE)) > 0) { if (key == ESCAPE) stop = TRUE; } else if (r < 0) stop = TRUE; */ if ( escape_abort(TRUE) ) stop = TRUE; /* DjV 033 010203 ---^^^--- */ } else error = (int) l; } while ((l == PBUFSIZ) && (stop == FALSE)); x_close(handle); print_eol(); /* DjV 031 150203 print cr lf at end of file */ } else error = handle; if (stop == TRUE) result = XABORT; if (error != 0) result = xhndl_error(MEPRINT, error, itm_name(w, item)); graf_mouse(ARROW, NULL); x_free(buffer); } else { xform_error(ENSMEM); result = XFATAL; } free(name); return result; }
static void getsc_line(Source *s) { char *xp = Xstring(s->xs, xp); int interactive = Flag(FTALKING) && s->type == SSTDIN; int have_tty = interactive && (s->flags & SF_TTY); /* Done here to ensure nothing odd happens when a timeout occurs */ XcheckN(s->xs, xp, LINE); *xp = '\0'; s->start = s->str = xp; if (have_tty && ksh_tmout) { ksh_tmout_state = TMOUT_READING; alarm(ksh_tmout); } #ifdef EDIT if (have_tty && (0 # ifdef VI || Flag(FVI) # endif /* VI */ # ifdef EMACS || Flag(FEMACS) || Flag(FGMACS) # endif /* EMACS */ )) { int nread; nread = x_read(xp, LINE); if (nread < 0) /* read error */ nread = 0; xp[nread] = '\0'; xp += nread; } else #endif /* EDIT */ { if (interactive) { pprompt(prompt, 0); } else s->line++; while (1) { char *p = shf_getse(xp, Xnleft(s->xs, xp), s->u.shf); if (!p && shf_error(s->u.shf) && shf_errno(s->u.shf) == EINTR) { shf_clearerr(s->u.shf); if (trap) runtraps(0); continue; } if (!p || (xp = p, xp[-1] == '\n')) break; /* double buffer size */ xp++; /* move past null so doubling works... */ XcheckN(s->xs, xp, Xlength(s->xs, xp)); xp--; /* ...and move back again */ } /* flush any unwanted input so other programs/builtins * can read it. Not very optimal, but less error prone * than flushing else where, dealing with redirections, * etc.. * todo: reduce size of shf buffer (~128?) if SSTDIN */ if (s->type == SSTDIN) shf_flush(s->u.shf); } /* XXX: temporary kludge to restore source after a * trap may have been executed. */ source = s; if (have_tty && ksh_tmout) { ksh_tmout_state = TMOUT_EXECUTING; alarm(0); } s->start = s->str = Xstring(s->xs, xp); strip_nuls(Xstring(s->xs, xp), Xlength(s->xs, xp)); /* Note: if input is all nulls, this is not eof */ if (Xlength(s->xs, xp) == 0) { /* EOF */ if (s->type == SFILE) shf_fdclose(s->u.shf); s->str = NULL; } else if (interactive) { #ifdef HISTORY char *p = Xstring(s->xs, xp); if (cur_prompt == PS1) while (*p && ctype(*p, C_IFS) && ctype(*p, C_IFSWS)) p++; if (*p) { s->line++; histsave(s->line, s->str, 1); } #endif /* HISTORY */ } if (interactive) set_prompt(PS2, (Source *) 0); }
static void getsc_line(Source *s) { char *xp = Xstring(s->xs, xp), *cp; bool interactive = Flag(FTALKING) && s->type == SSTDIN; bool have_tty = tobool(interactive && (s->flags & SF_TTY)); /* Done here to ensure nothing odd happens when a timeout occurs */ XcheckN(s->xs, xp, LINE); *xp = '\0'; s->start = s->str = xp; if (have_tty && ksh_tmout) { ksh_tmout_state = TMOUT_READING; alarm(ksh_tmout); } if (interactive) change_winsz(); #ifndef MKSH_NO_CMDLINE_EDITING if (have_tty && ( #if !MKSH_S_NOVI Flag(FVI) || #endif Flag(FEMACS) || Flag(FGMACS))) { int nread; nread = x_read(xp); if (nread < 0) /* read error */ nread = 0; xp[nread] = '\0'; xp += nread; } else #endif { if (interactive) pprompt(prompt, 0); else s->line++; while (/* CONSTCOND */ 1) { char *p = shf_getse(xp, Xnleft(s->xs, xp), s->u.shf); if (!p && shf_error(s->u.shf) && shf_errno(s->u.shf) == EINTR) { shf_clearerr(s->u.shf); if (trap) runtraps(0); continue; } if (!p || (xp = p, xp[-1] == '\n')) break; /* double buffer size */ /* move past NUL so doubling works... */ xp++; XcheckN(s->xs, xp, Xlength(s->xs, xp)); /* ...and move back again */ xp--; } /* * flush any unwanted input so other programs/builtins * can read it. Not very optimal, but less error prone * than flushing else where, dealing with redirections, * etc. * TODO: reduce size of shf buffer (~128?) if SSTDIN */ if (s->type == SSTDIN) shf_flush(s->u.shf); } /* * XXX: temporary kludge to restore source after a * trap may have been executed. */ source = s; if (have_tty && ksh_tmout) { ksh_tmout_state = TMOUT_EXECUTING; alarm(0); } cp = Xstring(s->xs, xp); rndpush(cp); s->start = s->str = cp; strip_nuls(Xstring(s->xs, xp), Xlength(s->xs, xp)); /* Note: if input is all nulls, this is not eof */ if (Xlength(s->xs, xp) == 0) { /* EOF */ if (s->type == SFILE) shf_fdclose(s->u.shf); s->str = NULL; } else if (interactive && *s->str) { if (cur_prompt != PS1) histsave(&s->line, s->str, HIST_APPEND, true); else if (!ctype(*s->str, C_IFS | C_IFSWS)) histsave(&s->line, s->str, HIST_QUEUE, true); #if !defined(MKSH_SMALL) && HAVE_PERSISTENT_HISTORY else goto check_for_sole_return; } else if (interactive && cur_prompt == PS1) { check_for_sole_return: cp = Xstring(s->xs, xp); while (*cp && ctype(*cp, C_IFSWS)) ++cp; if (!*cp) { histsave(&s->line, NULL, HIST_FLUSH, true); histsync(); } #endif } if (interactive) set_prompt(PS2, NULL); }
int main(void) { int in[2]; int out[2]; pid_t pid; printf("type 'quit' for exit\n\n"); x_pipe(in); x_pipe(out); pid = x_fork(); if(IS_CHILD(pid)) { x_dup2(in[READ], STDIN_FD); x_dup2(out[WRITE], STDOUT_FD); x_close(in[READ]); x_close(in[WRITE]); x_close(out[READ]); x_close(out[WRITE]); execl("/usr/bin/bc", "runbc", NULL); perror("execl"); exit(1); } else if(IS_PARENT(pid)) { char buf[BUFSIZE + 1]; fd_set rfds; int count; int flag = 1; x_close(in[READ]); x_close(out[WRITE]); while(13) { FD_ZERO(&rfds); FD_SET(STDIN_FD, &rfds); FD_SET(out[READ], &rfds); count = 0; x_select(out[READ] + 1, &rfds, NULL, NULL, NULL); if(FD_ISSET(out[READ], &rfds)) { // read messages from BC count = x_read(out[READ], buf, BUFSIZE); if(count) { x_write(STDOUT_FD, buf, count); } else { break; } } if(FD_ISSET(STDIN_FD, &rfds) && flag) { // send commands to BC count = x_read(STDIN_FD, buf, BUFSIZE); if(count) { x_write(in[WRITE], buf, count); } else { x_write(in[WRITE], "quit\n", 6); flag = 0; } } } x_waitpid(pid, NULL, 0); x_close(in[WRITE]); x_close(out[READ]); } return 0; }
void x_vpx_enc_run(unsigned char *pixels, int w, int h, int sock, struct sockaddr_in *cli) { size_t len = 0; int p1x = 0; int p1y = 0; void *dat; vpx_codec_ctx_t _encoder; vpx_codec_ctx_t _decoder; vpx_image_t raw_yuv; const vpx_codec_cx_pkt_t *pkt; int frame_cnt = 0; int flags = 0; vpx_codec_iter_t iter; int outfd; int infd; int mem_fdmap; memset(&_encoder, 0, sizeof(_encoder)); x_vpx_encoder_init(&_encoder, 1, w, h); memset(&_decoder, 0, sizeof(_decoder)); x_vpx_decoder_init(&_decoder, 1); vpx_img_alloc(&raw_yuv, VPX_IMG_FMT_YV12, w, h, 0); memset(raw_yuv.planes[VPX_PLANE_Y], 0, raw_yuv.stride[VPX_PLANE_Y] * raw_yuv.d_h / 2); // outfd = _P(open)("x_vp8_out.yuv",_P(O_CREAT) | _P(O_RDWR)); infd = _P(open)("x_camera_in.yuv", _P(O_RDONLY)); for (;;) { #if 0 raw_yuv.planes[VPX_PLANE_Y][p1y * w + p1x] = 0x0; p1x++; if (p1x > w) { p1x = 0; p1y++; if (p1y > h) p1y = 0; } raw_yuv.planes[VPX_PLANE_Y][p1y * w + p1x] = 0xff; #endif // int rbytes; // int bufsiz = ycbcr[0].width*ycbcr[0].height // + ycbcr[1].width*ycbcr[1].height + ycbcr[2].width*ycbcr[2].height; /* read luminance */ x_read(infd, raw_yuv.planes[VPX_PLANE_Y], raw_yuv.w * raw_yuv.h); /* read chroma U */ x_read(infd, raw_yuv.planes[VPX_PLANE_U], (raw_yuv.w * raw_yuv.h) >> 2); /* read chroma V */ x_read(infd, raw_yuv.planes[VPX_PLANE_V], (raw_yuv.w * raw_yuv.h) >> 2); // x_vp8_write_frame(outfd,&raw_yuv); if (vpx_codec_encode(&_encoder, &raw_yuv, frame_cnt, 1, flags, VPX_DL_REALTIME )) { die_codec(&_encoder, "vpx_codec_enc_init()"); } iter = NULL; while ((pkt = vpx_codec_get_cx_data(&_encoder, &iter))) { switch (pkt->kind) { case VPX_CODEC_CX_FRAME_PKT: // __decode_vp8_pkt(&_decoder, pkt->data.frame.buf, pkt->data.frame.sz, outfd); x_vp8_rtp_send((int) sock, (void *) pkt->data.frame.buf, (int) pkt->data.frame.sz, (struct sockaddr *) cli, (int) sizeof(*cli), seqno * 90); break; default: break; } x_write(1, (pkt->kind == VPX_CODEC_CX_FRAME_PKT) && (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? "K" : (pkt->kind == VPX_CODEC_CX_FRAME_PKT) && (pkt->data.frame.flags & VPX_FRAME_IS_INVISIBLE) ? "I" : (pkt->kind == VPX_CODEC_CX_FRAME_PKT) && (pkt->data.frame.flags & VPX_FRAME_IS_DROPPABLE) ? "D" : (pkt->kind == VPX_CODEC_CX_FRAME_PKT) && (pkt->data.frame.flags & 0x3) ? "B" : ".", 1); #ifndef WIN32 fsync(1); #endif } frame_cnt++; #ifdef WIN32 Sleep(20); #else usleep(56000); #endif } }