📄 xesps.c
字号:
pid_table[i].client_data = data;}/***********************************************************************/caddr_t get_esps_callback_data(pid) int pid;{ int i = pid_get_table_index(pid); if(i >= 0) return(pid_table[i].client_data); else return(NULL);}/***********************************************************************/static char *pid_add_graphics_name(i,name) char *name; int i;{ addstr(savestring(name), &(pid_table[i].out_files)); return((pid_table[i].out_files)[strlistlen(pid_table[i].out_files) - 1]);}/***********************************************************************/static char *pid_add_text_name(i,name) char *name; int i;{ addstr(savestring(name), &(pid_table[i].text_files)); return((pid_table[i].text_files)[strlistlen(pid_table[i].text_files) - 1]);}/***********************************************************************/static char *pid_add_command(i,command) char *command; int i;{ pid_table[i].command = savestring(command); return(pid_table[i].command);}/***********************************************************************/static char *make_output_name(name, ext, type) char *name, *ext, *type;{ static char newname[NAMELEN]; if(name) { if( ! strncmp(type, "out.", 4)) { if(ext && *ext) sprintf(newname, "%s.%s", name, ext); else sprintf(newname, "%s.out", name); strcpy(newname, inc_esps_name(newname)); } else strcpy(newname, ext); if (strlen(remote_output_path) > 0) build_filename(newname, basename2(newname), remote_output_path); else if (strlen(remote_path) > 0) build_filename(newname, basename2(newname), remote_path); else setup_output_dir(newname); return(newname); } return(NULL);}/***********************************************************************/char *get_only_ascii(out,in, waswhite) char *out, *in; int *waswhite;{ char *c, *get_next_item(); int i; sscanf(in,"%s",out); for(i=0, c = out; *c && (((*c >= '0') && (*c <= '9')) || ((*c >= 'A') && (*c <= 'Z')) || ((*c >= 'a') && (*c <= 'z')) || (*c == '_') || (*c == '/') || (*c == '.')); ) { c++; i++; } if(*c && i) { *waswhite = FALSE; while(*in) { if(*in == *c) { break; } in++; } } else { in = get_next_item(in); *waswhite = TRUE; } *c = 0; return(in);}/***************************************************************************/static run_any_prog(command_line, pid_slot) char *command_line; int pid_slot;{ extern char default_header[]; int i; char *args[4]; int pid; int esps_w_done(); set_default_header(); /* Check to see if remote command rsh and in debug mode, then issue warning about -n option on rsh. */ if (debug_level && (strlen(command_line) >4) && !strncmp(command_line,"rsh",3)) { show_notice(0,"You have used rsh in an external command.\nYou might need the -n option \n on rsh (eg. rsh hostname -n command)."); } pid_add_command(pid_slot,command_line); /* set execvp args */ args[0] = "/bin/sh"; args[1] = "-c"; args[2] = command_line; args[3] = NULL; if (w_verbose || debug_level) fprintf(stderr, "Starting shell command:\n%s %s %s", args[0], args[1], args[2]);/* Fork a new process to run the command. */#if defined(SUN4) && !defined(OS5) || defined(DS3100) switch (pid = vfork()) {#else switch (pid = fork()) {#endif case -1: /* fork failed */ perror("run_any_prog(); fork failed"); return (0); case 0: /* child process */ setpgrp(0,getpid()); execvp(args[0], args); perror("run_any_prog: execvp failed"); return (0); default: /* parent */ /* Save the pid of the child in the table. */ { extern Frame daddy; void *in_a_ipc_dispatch(); pid_table[pid_slot].pid = pid; (void)notify_set_wait3_func(daddy,esps_w_done,pid); pid_table[pid_slot].ipc_client_info = in_a_ipc_dispatch(); indicate_pending_process(pid_slot); } } if (w_verbose) fprintf(stderr,"(pid: %d, request: %d)\n",pid,pid_slot); return(pid);}/* Examine a character string to determine if it has one or more"arithmetic" operators as a prefix. If so, return the prefix as astring and a number indicating how many characters are in the prefix.***********************************************************************/char *is_arith_operator(cp, nops) char *cp; int *nops;{ static char aops[] ="*/+-=:%^", opstr[50]; *nops = 0; while(*cp && strchr(aops,(int)(*cp))) { opstr[*nops] = *cp++; (*nops)++; } if(*nops) { opstr[*nops] = 0; return(opstr); } else return(NULL);}/***********************************************************************//* This event routine is called when an external operator is selected from view-window menus. A program is introduced onto the menu via a call to meth_add_operator(), which in turn is called by issuing the add_op command to xwaves. */void call_operator(canvas, event, arg) Canvas canvas; Event *event; caddr_t arg;{ SINGLE_HIT { char *out_names = NULL; int out_size = 0; Signal *s; View *v; static char command[5000]; char *cp, *outname, *current_object_name(); int i, pidi, internal; Object *o; v = (View *) xv_get(canvas, WIN_CLIENT_DATA); s = v->sig; if((pidi = init_pid_table()) < 0) { fprintf(stderr," No more free pid table slots\n"); return; } if((o = (Object*)(s->obj))) cp = o->name; else cp = current_object_name(); strcpy(pid_table[pidi].oname, cp); if (s->file == SIG_NEW) { if (debug_level) fprintf(stderr,"call_operator: signal and file differ; writing signal.\n"); put_signal(s); } *command = 0; if(!(cp = (char*)arg) || !(*cp) ) { if(debug_level) fprintf(stderr, "NULL command passed to call_operator()\n"); return; } internal = FALSE; if(*cp == '#') { while((*cp == '#') || (*cp == ' ') || (*cp == ' ')) cp++; internal = TRUE; } if(debug_level) fprintf(stderr,"call_operator building command from:\n|%s|\n",cp); do { char item[NAMELEN], *get_next_item(), *aop = NULL; int noc = 0; if((aop = is_arith_operator(cp,&noc)) && (cp[noc] == '_')) cp += noc; if(*cp == '_') { /* decode the symbol */ int waswhite; cp = get_only_ascii(item, cp+1, &waswhite); if(!strncmp("out.",item,4) || !strncmp("nom.",item,4)) { /* Generate an output file name. */ if(!strncmp("g.",&item[4],2)) { outname = pid_add_graphics_name(pidi,make_output_name(view_get_value(v,"file"),&item[6],item)); } else if(!strncmp("t.",&item[4],2)) { outname = pid_add_text_name(pidi,make_output_name(view_get_value(v,"file"),&item[6],item)); } else if(!strncmp("n.",&item[4],2)) { outname = make_output_name(view_get_value(v,"file"),&item[6],item); } else outname = make_output_name(view_get_value(v,"file"),&item[4],item); if(!outname) { fprintf(stderr,"Problems parsing output spec %s in call_operator()\n",item); return; } sprintf(command+strlen(command),"%s", outname); } else { if(aop) { char *tcp = view_get_value(v,item); noc--; if(*tcp == '-') { if(aop[noc] == '-') { tcp++; aop[noc] = '+'; } else if(aop[noc] == '+') { tcp++; aop[noc] = '-'; } } sprintf(command+strlen(command),"%s%s",aop,tcp); } else sprintf(command+strlen(command),"%s",view_get_value(v,item)); } if(waswhite) strcat(command," "); } else { sscanf(cp,"%s",item); sprintf(command+strlen(command),"%s ",item); cp = get_next_item(cp); } } while(*cp); if(internal) { pid_clear_out_files(pidi); pid_reset_entry(pidi); exec_waves(command); } else if ( ! run_any_prog(command, pidi)) fprintf(stderr, "call_operator: couldn't run command"); }} /***********************************************************************//* run an external ESPS program, given the command string, input file name, and output file names. The command can be run either in the foreground or background. If defined, the remote_path string is prepended to the basename of input file and, if num_esps_out > 0, the output files. If remote_input_path is defined, it overrides remote_path for input files. If remote_output_path is defined, it overrides remote_path for output files. It also overrides output_dir (for external calls) if that global is defined. This overall behavior is usseful for running in a remote NFS directory. Note that the remote_path globals should not terminate with a /, which is added here. The number of file arguments can be reduced to 1 or 0 by calling run_esps_prog with output "" or input and output both "". */intrun_esps_prog(command, input, output, num_esps_out, display, func) char *command, *input, *output; int num_esps_out; int (*func)(); /* if !NULL, then call this function when done */ int display; /* YES, display output files when done */{if(run_esps_prog_get_pid(command, input, output, num_esps_out, display, func)) return(0); else return(-1);}/* Return the PID of the external process on success, zero on failure. */intrun_esps_prog_get_pid(command, input, output, num_esps_out, display, func) char *command, *input, *output; int num_esps_out; int (*func)(); /* if !NULL, then call this function when done */ int display; /* YES, display output files when done */{ char command_line[2000]; char tmpfile[MES_BUF_SIZE]; extern char default_header[]; int ret; int i; char *args[4]; int pid; int pid_slot; int esps_w_done(); char *output_wdir; int out_size; char *output_files = output; if(*default_header) { static char tch[NAMELEN];#if defined(SONY_RISC) || defined(CONVEX) setenv("DEF_HEADER",default_header,1);#else sprintf(tch,"DEF_HEADER=%s",default_header); putenv(tch);#endif } if((pid_slot = init_pid_table()) < 0) return(0); /* check to see if remote command rsh and in debug mode, then issue warning about -n option on rsh */ if (debug_level && (strlen(command) >4) && !strncmp(command,"rsh",3)) show_notice(1, "You have used rsh in an external command.\nYou might need the -n option \n on rsh (eg. rsh hostname -n command)."); if (*output) { /* Output has one or more output file names with null */ /* chars as separators and terminator. */ /* first, let's fix them up if output_dir or other globals defined */ output_wdir = savestring(""); out_size = 0; for (i = 0; i < num_esps_out; i++) { int old_size = out_size; if (strlen(remote_output_path) > 0) build_filename(tmpfile, basename(output), remote_output_path); else if (strlen(remote_path) > 0) build_filename(tmpfile, basename(output), remote_path); else { strcpy(tmpfile, output); if(not_explicitly_named(tmpfile)) setup_output_dir(tmpfile); } if(debug_level > 1) fprintf(stderr, "out #%d:%s\n",i,tmpfile); out_size += strlen(tmpfile) + 1; output_wdir = realloc(output_wdir, out_size); strcpy(output_wdir + old_size, tmpfile); output += strlen(output) + 1; } output = output_files = output_wdir; } if(*output && debug_level) { int nuts = strlen(output) + 1, kk; fprintf(stderr, "outputs: "); for(kk=0; kk < num_esps_out; kk++) fprintf(stderr,"%s ",output+(kk*nuts)); fprintf(stderr,"\n"); } if (input && *input) { if (strlen(remote_input_path) > 0) build_filename(tmpfile, basename(input), remote_input_path); else if (strlen(remote_path) > 0) build_filename(tmpfile, basename(input), remote_path); else strcpy(tmpfile, input); sprintf(command_line, "%s %s ", command, tmpfile); } else sprintf(command_line, "%s ", command); if (*output) { if (num_esps_out == 0) { sprintf(tmpfile, " %s", output); strcat(command_line, tmpfile); } else /* Output has one or more output file names with null */ /* chars as separators and terminator. */ { for (i = 0; i < num_esps_out; i++) { strcpy(tmpfile, output); strcat(command_line, tmpfile); strcat(command_line, " "); output += strlen(output) + 1; } } } strcat(command_line, "\n"); if (w_verbose || debug_level) fprintf(stderr,"Starting shell command:\n%s", command_line);/* set execvp args */ args[0] = "/bin/sh"; args[1] = "-c";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -