static int recv_cmd(int fd, NET_Fn fn) { static int nSize = 0; int nLen; char pBuff[1024]; char szResult[1024]; char *pHead; char *pTail; int ret = 0; memset(pBuff, 0, 1024); nLen = u_read(fd, pBuff, 1024); if (nLen > 0) { printf("+++ pbuff:%s +++\n", pBuff); memcpy(&szResult[nSize], pBuff, nLen); nSize += nLen; pHead = szResult; pTail = strstr(pHead, "\r\n"); while (pTail != NULL && nSize > 0) { char szMsg[1024] = {0}; int len = pTail - pHead + 2; memcpy(szMsg, pHead, len); nSize -= len; printf("+++ get message:nSize %d pHead %s %s +++\n",nSize, pHead, szMsg); ret = (*fn)(fd, szMsg, len); if (ret < 0) { nSize = 0; return ret; } pHead = pTail + 2; pTail = strstr(pHead, "\r\n"); } if (nSize > 0) { memmove(szResult, pHead, nSize); memset(&szResult[nSize], 0, 1024 - nSize); } } else { printf("+++ read error, errno = %d +++\n", errno); nSize = 0; return -1; } return ret; }
/* loads a file and returns its length */ int load_file(char *file, char *at) { unsigned int size; if (u_open(file, &size)) return 0; size = u_read(at, size); u_close(); return size; }
int main(void){ gc_init(); u_object obj; announce(); for ( ; ; ) { printf("\nunknown>"); //fflush(stdout); obj = u_read(stdin); if (u_eof_p(obj)) break; obj = u_eval(obj); u_write(stdout, obj); /* printf("\n"); */ } return (0); }
int process_cfg(char *cfg) { // buffer for config file reading static char s[BUFSIZE]; static int s_pos = 0; // second buffer static char str[BUFSIZE]; static int str_pos = 0; char *p, *line, *q; char f, g; unsigned int size, sz; unsigned int rd; int bytes_read = 0; int i; if (u_open(cfg, &size)) { printf("Cannot open config file!\r\n"); return 0; } u_close(); //grub_memset(s, 0, sizeof(s)); sz = size; while (sz) { // read new buffer if (u_open(cfg, &size)) { printf("Cannot open config file!\r\n"); return 0; } u_seek(bytes_read); rd = u_read(buf, sizeof(buf)); if (!rd) { printf("process_cfg(): read returned zero bytes\r\n"); return 0; } sz -= rd; bytes_read += rd; u_close(); if (sz && !rd) { printf("Can't read from config file!\r\n"); return 0; } f = 1; p = buf; while (*p && f) { // read new line from current buffer line = getline(&p, rd); i = grub_strlen(line); if (s_pos + i > BUFSIZE) panic("string s too long to fit in buffer!\r\n", cfg); grub_strcpy(s + s_pos, line); s_pos += i; f = (p - buf < rd); g = f || (!f && (buf[rd - 1] == '\n')); if (g) { // delete CR and LF symbols at the end line = strip(trim(s)); // skip comments ";" i = grub_index(';', line); if (i) line[i - 1] = '\0'; // skip comments "#" i = grub_index('#', line); if (i) line[i - 1] = '\0'; // delete leading and trailing spaces line = strip(line); // line continuation symbol ('^' or '&') support i = grub_strlen(line); if (line[i - 1] == '^' || line[i - 1] == '&') { // the current line continued line[i - 1] = '\0'; if (str_pos + i - 1 > BUFSIZE) panic("string str too long to fit in buffer!\r\n", cfg); grub_strcpy(str + str_pos, line); str_pos += i - 1; s[0] = '\0'; s_pos = 0; continue; } else { // the line ends if (str_pos + i > BUFSIZE) panic("string str too long to fit in buffer!\r\n", cfg); grub_strcpy(str + str_pos, line); str_pos = 0; s[0] = '\0'; s_pos = 0; } // expand variables //expand_vars(s, str); // process the line if (!process_cfg_line(str)) return -1; } } } return 1; }
/* called by both caller and callee, DONE */ static int call_run(int sock_fd, int p_rfd, call_fn_t *op, tmeo_t *tmeo) { char buf[64]; int ret = 0; char c; int maxfd; fd_set rset; struct timeval tv; while (1) { memset(buf, '\0', 64); FD_ZERO(&rset); FD_SET(sock_fd, &rset); FD_SET(p_rfd, &rset); maxfd = U_MAX(p_rfd, sock_fd) + 1; printf("--- timeout = %d ---\n", *(tmeo->tmeo)); if (*(tmeo->tmeo) > 0) { tv.tv_sec = *(tmeo->tmeo); tv.tv_usec = 0; ret = select(maxfd, &rset, NULL, NULL, &tv); } else if (*(tmeo->tmeo) == 0) { tv.tv_sec = tmeo->max_tmeo; tv.tv_usec = 0; ret = select(maxfd, &rset, NULL, NULL, &tv); } else if (*(tmeo->tmeo) < 0) { ret = select(maxfd, &rset, NULL, NULL, NULL); } if (ret < 0) { if (errno == EINTR) { printf("--- select interrupted ---\n"); continue; } else { printf("--- select error ---\n"); return -1; } } else if (ret > 0) { *(tmeo->tmeo) = 0; if (FD_ISSET(sock_fd, &rset)) { printf("--- received net cmd ---\n"); if ((ret = recv_cmd(sock_fd, op->nfn)) < 0) break; } if (FD_ISSET(p_rfd, &rset)) { printf("--- detect pipe cmd ---\n"); if (u_read(p_rfd, &c, 1)==1) { printf("--- received pipe cmd: %c ---\n", c); if ((ret = op->pfn(c)) < 0) break; } } } else if (ret == 0) { printf("--- timedout ---\n"); if ((ret = op->tfn()) < 0) break; } } return ret; }