/** polling of child status @returns 0 - no signal */ static int child_waitpid(int hc) { struct child_stat *child = mls(CLIST, hc); int status; if( child->stat != CHILD_RUNNING ) return child->stat; int err = waitpid(child->pid,&status,WNOHANG ); if( err == 0 ) return 0; if( err == -1 ) { TRACE(trace_child,"error in waitpid\n"); child->stat = CHILD_EXIT_FAILURE; child->error = CHILD_ERR_PROCESS_NOT_FOUND; return -3; } if (WIFEXITED(status)) { child->exit_value = WEXITSTATUS(status); TRACE(trace_child,"Child exited, status=%d\n", WEXITSTATUS(status)); child->stat = WEXITSTATUS(status) ? CHILD_EXIT_FAILURE : CHILD_EXIT_SUCCESS; return WEXITSTATUS(status) ? 2 : 1; } return 0; }
/** vergleiche die ersten n zeichen des arrays m und des strings s */ int m_isequal( int m, const char *s, int n) { if( m > 0 && m_len(m) >= n && s && *s ) { return ( strncmp( s, (char*)mls(m,0), n ) == 0 ); } return 0; }
/** create a child process and connect a pipe to child stdout */ static void child_exec(int hc) { char *null_string = NULL; struct child_stat *child = mls(CLIST, hc); if( pipe2( child->fd, O_NONBLOCK ) ) ERR("pipe2"); if( pipe2( child->fd+2, O_NONBLOCK ) ) ERR("pipe2"); pid_t cpid = fork(); if (cpid == -1) ERR("fork"); if (cpid == 0) { /* Child reads from pipe */ dup2(child->fd[1],1); /* make STDOUT==1 same as WRITE-TO==1 end of pipe-A */ dup2(child->fd[2],0); /* make STDIN==0 sama es READ-FROM==0 end of pipe-B */ xclose(child->fd+0); /* Close unused read end pipe-A */ xclose(child->fd+3); /* Close unused write end pipe-B */ m_put(child->args,&null_string); // execlp( child->filename, child->filename, NULL ); execvp( child->filename, m_buf(child->args) ); m_drop(child->args); perror( "can not exec child process" ); _exit(EXIT_FAILURE); /* never reached */ } xclose( child->fd+1 ); /* close unused WRITE end of pipe-A */ xclose( child->fd+2 ); /* close unused READ end of pipe-B */ child->stat = CHILD_RUNNING; child->pid = cpid; }
// from the resample demo int main (int argc, char** argv) { std::string infile = "../../recordings/mark1_pcd/1316652689.744386960.pcd"; std::string smoothfile = "scan-mls.pcd"; std::string outmesh = "scan.vtk"; // there's a ply export in pcl 1.2, but ... we don't have that if(argc > 1) { if(strcmp(argv[1], "-s") == 0) { if(access(infile.c_str(), R_OK) >= 0) { std::cout << "running Moving Least Squares smoothing, this takes about 30s\n"; mls(infile, smoothfile); } else { print_usage(); std::cout << " ! unable to find " << infile << std::endl; } } else if(strcmp(argv[1], "-v") == 0) { if(access(smoothfile.c_str(), R_OK) >= 0) { viz(smoothfile); } else { print_usage(); std::cout << " ! unable to find " << smoothfile << std::endl; } } else if(strcmp(argv[1], "-r") == 0) { if(access(smoothfile.c_str(), R_OK) >= 0) { std::cout << "running greedy projection, this takes about 4s\n"; recons(smoothfile, outmesh); } else { print_usage(); std::cout << " ! unable to find " << smoothfile << std::endl; } } else { print_usage(); } } else { print_usage(); } }
int execute(int argc, char** argv) { if(argc < 0) { fprintf(stderr, "Erreur : pas assez d'argument dans la fonction d'execution\n"); return 0; } if(strcmp(argv[0], "cd") == 0) { return mcd(argc, argv); } else if(strcmp(argv[0], "mkdir") == 0) { return mmkdir(argc, argv); } else if(strcmp(argv[0], "cat") == 0) { return mcat(argc, argv); } else if(strcmp(argv[0], "ls") == 0) { return mls(argc, argv); } else if(strcmp(argv[0], "vim") == 0) { return mvim(argc, argv); } else { printf("Commande non reconnu\n"); return 0; } }
static struct mcu_comm *mcu_ptr(uint ctx) { if(MCU_COMM<1 || ctx == 0 || ctx > m_len(MCU_COMM)) { ERR("mcu ctx undefined %d", ctx); exit(1); } return mls(MCU_COMM, ctx-1 ); }
static void new_packet_cb(int err, int msg, int sln, void *ctx) { if( err ) return; m_putc(msg,0); TRACE(1,"packet received: %s", (char*)mls(msg,0) ); cp_func_t fn = cp_lookup(msg); if(fn) { err= fn( (void*)(intptr_t)msg, (void*)(intptr_t)sln ); if( err ) sln_set_exit_flag(1); else sln_printf(sln,"OK"); } else { TRACE(1,"unknow command: %s", (char*)mls(msg,0) ); sln_printf(sln,"ERROR"); } }
/** run child process "name", returns handle for process returns: <=0 : error, no process forked >0 : child process handle */ static struct child_stat *child_new(int *pos) { struct child_stat *child; *pos=m_len(CLIST); while( (*pos)-- ) { child = mls(CLIST,*pos); if( child->stat == CHILD_NOT_INIT ) return child; } return m_add2( CLIST, pos ); }
int child_start(Widget top, char *cmd, void (*cb)(int) ) { int hc = child_init(cmd, 0); struct child_stat *child = mls(CLIST, hc); child->cb = cb; child_exec(hc); child->id = XtAppAddInput(XtWidgetToApplicationContext(top), child_read_fd(hc), (XtPointer) (XtInputReadMask), child_input_cb,(XtPointer) hc ); return hc; }
void child_close(int hc) { struct child_stat *child = mls(CLIST, hc); if( child->stat == CHILD_NOT_INIT ) return; child_close_handle(hc); /* get exit status */ XtRemoveInput(child->id); /* remove listener */ if(child->cb) child->cb(hc); /* tell app. */ m_free(child->line); /* free mem */ mrb_destroy(child->qin); free( child->filename ); child->stat = CHILD_NOT_INIT; m_free_strings(child->args,0); }
/** kill child process and close file handles */ static int child_close_handle( int hc ) { struct child_stat *child = mls(CLIST, hc); if( child->stat == CHILD_RUNNING ) { kill( child->pid, SIGKILL ); child_get_exit_value(child); } xclose( child->fd+0 ); xclose( child->fd+1 ); return child->error; }
/** * this function is called if some data has arrived * sln_client_select -> sln_input_cb -> slop_put which calls this function */ static void process_new_packet(int error, int msg, int sln, void *ctx) { if( error ) return; m_putc(msg,0); TRACE(1,"Msg: %s\n", (char*)mls(msg,0)); int err; cp_func_t fn = cp_lookup(msg); if(fn) { err= fn( (void*)(intptr_t)msg, (void*)(intptr_t)sln ); if( err ) sln_set_exit_flag(1); } }
int child_startv(Widget top, int cmd_m, void (*cb)(int) ) { int l = m_len(cmd_m); int i; int hc = child_init(STR(cmd_m,0), 0); struct child_stat *child = mls(CLIST, hc); child->cb = cb; for(i=1;i<l;i++) v_kset(child->args, STR(cmd_m, i), -1 ); child_exec(hc); child->id = XtAppAddInput(XtWidgetToApplicationContext(top), child_read_fd(hc), (XtPointer) (XtInputReadMask), child_input_cb,(XtPointer) hc ); return hc; }
static void child_input_cb( XtPointer p, int *n, XtInputId *id ) { int hc = (int) p; struct child_stat *child = mls(CLIST, hc); TRACE(trace_child,""); if( child_read2(hc) ) { child_close(hc); return; } while( mrb_get_line(child->qin,child->line)==1) { if( child->cb ) child->cb(hc); m_clear(child->line); }; }
int cmd_rect(int msg,void *ctx) { TRACE(1,""); int x,y,wi,h; sscanf( mls(msg,5), "%d %d %d %d", &x, &y, &wi, &h ); Widget w = CWNET.widget_draw1; if( w ) { Drawable d = XtWindow( w ); Display *dpy = XtDisplay(w); GC gc = DefaultGC(dpy,DefaultScreen(dpy)); XSetForeground(dpy, gc, 0xffffff); XDrawRectangle( dpy, d, gc, x,y,wi,h ); } TRACE(1,"server told us to draw a rectange"); return 0; }
int cmd_circle(int msg,void *ctx) { TRACE(1,""); int x,y,r; sscanf( mls(msg,7), "%d %d %d", &x, &y, &r ); /* lgfx_circle(lgfx, x,y,r); */ Widget w = CWNET.widget_draw1; if( w ) { Drawable d = XtWindow( w ); Display *dpy = XtDisplay(w); GC gc = DefaultGC(dpy,DefaultScreen(dpy)); XSetForeground(dpy, gc, 0xffffff); XDrawArc( dpy, d, gc, x,y,r,r,0,360*64 ); } TRACE(1,"server told us to draw a circle %d %d %d",x,y,r); return 0; }
int main(int argc,char *argv[]) //该怎麽实现 { int i,j,flag=1,k[20]={0}; char ml[50]; getcwd(ml,50); //获取当前目录路径 //命令解析表 char a[][5]={"-a","-l","-R","-al","-la","-aR", "-Ra","-lR","-Rl","-alR","-aRl", "-lRa","-laR","-Ral","-Rla"}; for(i=1;i<argc;i++) { flag=0; for(j=0;j<15;j++) if(!strcmp(a[j],argv[i])) k[i-1]=j+1; } if(flag) k[0]=0; switch(k) // 命令映射表 { case 0:mls(ml,0,0,0);break; case 1:mls(ml,1,0,0);break; // a l R case 2:mls(ml,0,1,0);break; case 3:mls(ml,0,0,1);break; case 4: case 5:mls(ml,1,1,0);break; case 6: case 7:mls(ml,1,0,1);break; case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15:mls(ml,1,1,1);break; default:printf("你输入的参数有误");break; } }
/** fehlerprüfung und daten einlesen */ static int child_read2(int hc) { int err; struct child_stat *child = mls(CLIST, hc); TRACE(1,""); /* einlesen ist non-blocking */ err = mrb_read_max(child->qin, child_read_fd(hc) ); if( ! err ) return 0; /* irgendetwas geht fürchterlich schief was ist mit dem subprocess? */ if( child_waitpid(hc) != 0 ) return 1; /* subprocess läuft, wie sieht es mit errno aus? */ TRACE(1,"err %s", strerror(err) ); if( (err == EINTR) || (err == EWOULDBLOCK) ) return 0; /* errno sagt nichts gutes, besser die verbindung beenden */ return 1; }
/* GETTERS */ enum child_proc_state child_status(int hc) { struct child_stat *child = mls(CLIST, hc); return child->stat; }
int main (int argc, char** argv){ //greedy_proj(); mls(); return 0; }
int child_read_fd(int hc) { struct child_stat *child = mls(CLIST, hc); return child->fd[0]; }
int child_exit_status(int hc) { struct child_stat *child = mls(CLIST, hc); return child->exit_value; }
char* child_data(int hc) { struct child_stat *child = mls(CLIST, hc); return m_buf(child->line); }
static void* m_add2(int list, int *pos) { *pos = m_new(list,1); return mls(list,*pos); }
/** returns index of child argument list */ int child_args(int hc) { struct child_stat *child = mls(CLIST, hc); return child->args; }