int EsdlCMDShell::run() { try { if (!parseCommandLineOptions(args)) return 1; finalizeOptions(globals); return processCMD(args); } catch (IException *E) { StringBuffer m; fputs(E->errorMessage(m).newline().str(), stderr); E->Release(); return 2; } #ifndef _DEBUG catch (...) { ERRLOG("Unexpected exception\n"); return 4; } #endif return 0; }
/* use: process a <command> or <list> args: cmd = root of command tree return: status of process */ int processCMD(CMD* cmd) { int status; // status of process // <and-or> ; <command> and <list> ; cases if (cmd->type == SEP_END) { // left child status = processCMD(cmd->left); // right child if (cmd->right) { status = processCMD(cmd->right); } } // <and-or> & <command> and <list> & cases else if (cmd->type == SEP_BG) { pid_t pID = fork(); // process ID of fork // error check if (pID < 0) { status = errno; errorExit(status); } // child process else if (pID == 0) { status = processCMD(cmd->left); exit(status); } // parent process else { status = 0; fprintf(stderr, "Backgrounded: %d\n", pID); if (cmd->right) { status = processCMD(cmd->right); } } } // <and-or> case else { status = processAndOr(cmd, 0, false); } return status; }
//MAIN----------------------------------------------------------------------------------- int main() { char usrInput[maxUsrIn]; //allocate space for a line of user input (static) //loop - read user input and tokenize //end loop and exit shell if user presses control-d //control-d = ^d = EOF printf("? "); while(fgets(usrInput, maxUsrIn, stdin) != NULL) { //tokenize user input tokenize(usrInput, " "); //pointer to an array of pointers processCMD(); printf("? "); } exitMsg(); return 0; }
void Connection::onControlSktReadyRead() { packetSize = 0; //get packet size QDataStream in(this); in.setVersion(QDataStream::Qt_4_8); if (packetSize == 0) { if (this->bytesAvailable() < (int)sizeof(quint16)){ qDebug() << "\t E: packet size wrong" << this->bytesAvailable() << "/" << (int)sizeof(quint16);; return; } in >> packetSize; } //ensure data size available if (this->bytesAvailable() < packetSize){ qDebug() << "\t E: not enough data bytes" << this->bytesAvailable() << "/need " << packetSize; return; } //read in data QByteArray payloadArrey; in >> payloadArrey; //analyze payload Packet p; if( p.fromPayload(payloadArrey)){ switch(p.getType()){ case PTYPE_CMD: processCMD(p); break; case PTYPE_DATA: qDebug() << "\t Connection: Data from CMD link"; break; default: qDebug() << "\t Connection: Unknown packet type"; } } }
/* use: process a list of commands args: cmdList = root of command tree return: status of last command executed */ int process(CMD *cmdList) { pid_t pID; // process ID int status; // status of process // reap zombies (void) signal(SIGINT, SIG_IGN); while((pID = waitpid(-1, &status, WNOHANG)) > 0) { status = (WIFEXITED(status) ? WEXITSTATUS(status) : 128+WTERMSIG(status)); fprintf(stderr, "Completed: %d (%d)\n", pID, status); } (void) signal(SIGINT, SIG_DFL); // process commands status = processCMD(cmdList); // update status return setStat(status); }
int EclCMDShell::run() { if (!parseCommandLineOptions(args)) return 1; if (!optIniFilename) { if (checkFileExists(INIFILE)) optIniFilename.set(INIFILE); else { StringBuffer fn(SYSTEMCONFDIR); fn.append(PATHSEPSTR).append(DEFAULTINIFILE); if (checkFileExists(fn)) optIniFilename.set(fn); } } globals.setown(createProperties(optIniFilename, true)); finalizeOptions(globals); try { return processCMD(args); } catch (IException *E) { StringBuffer m("Error: "); fputs(E->errorMessage(m).newline().str(), stderr); E->Release(); return 2; } #ifndef _DEBUG catch (...) { ERRLOG("Unexpected exception\n"); return 4; } #endif return 0; }
void udp_setup_thread_entry(void *p) { int sock; int bytes_read; struct sockaddr_in server_addr , client_addr; int optval = 1; fd_set readset; /* create socket */ if ((sock = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { rt_kprintf("Can not create udp setup socket.\n"); return; } lwip_setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&optval ,sizeof(optval)); /* init server socket address */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(SETUP_UDP_PORT); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); rt_memset(&(server_addr.sin_zero),0, sizeof(server_addr.sin_zero)); if (lwip_bind(sock,(struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { rt_kprintf("Bind error\n"); return; } rt_kprintf("UDPServer Waiting for client on port %d \n",SETUP_UDP_PORT); while (1) { int bytes_ret; rt_uint32_t addr_len = sizeof(struct sockaddr_in); FD_ZERO(&readset); FD_SET(sock, &readset); if( lwip_select(sock+1, &readset, 0, 0, 0) == 0 ) continue; rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); bytes_read = lwip_recvfrom(sock, setup_data_buf, DATA_BUF_SIZE, MSG_DONTWAIT, (struct sockaddr *)&client_addr, &addr_len); rt_mutex_release(&setup_data_buf_mutex); if( bytes_read < 0 ) { continue; } rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); setup_data_buf[bytes_read] = 0; bytes_ret = processCMD(setup_data_buf,bytes_read); rt_mutex_release(&setup_data_buf_mutex); if( bytes_ret > 0 ) { // command execute success, send reply in buffer. client_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST); rt_mutex_take(&setup_data_buf_mutex,RT_WAITING_FOREVER); lwip_sendto(sock,setup_data_buf,bytes_ret,0,(struct sockaddr*)&client_addr, sizeof(struct sockaddr)); rt_mutex_release(&setup_data_buf_mutex); } } // Should not reach here!. }
void tcp_setup_thread_entry(void *p) { int listenfd; struct sockaddr_in saddr; fd_set readset; listenfd = lwip_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if( listenfd == -1 ) { rt_kprintf("TCP setup can not create socket!\n"); return; } memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_addr.s_addr = htonl(INADDR_ANY); saddr.sin_port = htons(SETUP_TCP_PORT); if( lwip_bind(listenfd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1 ) { lwip_close(listenfd); rt_kprintf("TCP setup thread socket bind failed!\n"); return; } /* Put socket into listening mode */ if (lwip_listen(listenfd,2) == -1) { lwip_close(listenfd); rt_kprintf("Listen failed.\n"); return; } /* Wait for data or a new connection */ while(1) { /* Determine what sockets need to be in readset */ FD_ZERO(&readset); FD_SET(listenfd, &readset); // wait forever. if( lwip_select(listenfd+1, &readset, 0, 0, 0) == 0 ) continue; if (FD_ISSET(listenfd, &readset)) { int ret,optval = 1; struct sockaddr_in addrin; u32_t sockaddrLen = sizeof(addrin); int socket = lwip_accept(listenfd,(struct sockaddr*)&addrin,&sockaddrLen); if( socket < 0 ) // accept failed. { rt_kprintf("TCP setup accept failed.\n"); continue; } rt_kprintf("TCP setup accept connection.\n"); // set socket keep alive. lwip_setsockopt(socket,SOL_SOCKET,SO_KEEPALIVE,&optval ,sizeof(optval)); // begin to recv & send. while(1) { int dataLen,bytesRet; ret = blocking_lwip_recv(socket,setup_data_buf,P2X_HEADER_LEN,500); if( ret == 0 ) continue; if( ret != P2X_HEADER_LEN ) break; dataLen = P2X_GET_LENGTH(setup_data_buf); if( dataLen > 0 ) { int gotDataLen = blocking_lwip_recv(socket,setup_data_buf+P2X_HEADER_LEN,dataLen,500); if( gotDataLen != dataLen ) break; } bytesRet = processCMD(setup_data_buf,P2X_HEADER_LEN+dataLen); if( lwip_send(socket,setup_data_buf,bytesRet,0) < 0 ) break; } rt_kprintf("TCP setup disconnected.\n"); lwip_close(socket); } }// while(1) listen. }