📄 io.c
字号:
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1986-1990 Entropic Speech, Inc. * "Copyright (c) 1990-1996 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: Joseph T. Buck, modified for select by Alan Parker * Checked by: * Revised by: * * Brief description: * */char *io_sccs = "@(#)io.c 3.9 1/20/97 ESI/ERL";#include "select.h"#include <signal.h>#include <esps/esignal_fea.h>#define SCREEN_HEIGHT 23extern char *get_sphere_hdr();extern char *ifile[], *ofile;extern FILE *istrm[], *ostrm;extern struct header *ihd[];extern struct fea_data *irec[];extern int nflag, n_ifiles, debug_level;extern int cfr_flag1, cfr_flag2;extern char *select_sccs, *io_sccs, *parser_sccs, *lexan_sccs;int n_wfields=0, n_wofields=0;char *with_fields[MAXWFIELDS], *wo_fields[MAXWFIELDS];extern int lin_search2(), copy_fea_fld();char *savestring();void open_buff(), copy_fea_rec(), set_header();char msgbuf[200], *strcpy();long data_ptr;long undone_count=0;static char *pager_prog;int isatty();struct header *buffhd=NULL; /* header for select buffer */struct header *outhd; /* header for output file */static FILE *buffd=NULL; /* stream for select buffer */static FILE *tstrm=NULL; /* stream for temporary file */static struct fea_data *bufrec; /* select buffer data pointer */static FILE *logfd; /* fd for log file. NULL if none open */FILE *more_pipe; /* stream for piping to more */static char *logname; /* log file name. NULL if none open */static int bol_out; /* true at bol in output */static int changes; /* true if changes made to buffer since last write */static long out_count = 0; /* number of records in buffer */static int in_tty; /* true if stdin is a tty */static int out_tty; /* true if stdout is a tty */int eof_flag; /* true if eof on a non-tty */static int sigtrap; /* if signal caught, contains its code */static int piping; /* true if piping to more */extern int com_file_depth; /* nonzero if in a command file */int pr_commands; /* if nonzero, lines in command files are printed *//* Eunice has no SIGPIPE. It does have SIGCHLD though, which we use to find out if more dies.*/#ifdef Eunice#define WHICH_SIG SIGCHLD#else#define WHICH_SIG SIGPIPE#endif/* The trap routine catches signals resulting from the pager terminating early.*/static int trap (n) { sigtrap = n;#ifdef Eunice /* For Eunice, we must close the broken pipe */ if (piping) pclose (more_pipe);#endif}voidinit_output() { bol_out = 1; in_tty = isatty(0); out_tty = isatty(1); logfd = NULL; logname = NULL; eof_flag = 0; if ((pager_prog = getenv ("PAGER")) == NULL) pager_prog = PAGER;}/* more_begin causes output to be passed through "more" if the argument is greater than or equal to SCREEN_HEIGHT. It does not call more unless both stdin and stdout are a terminal. */voidmore_begin(nlines)int nlines;{ if (nlines < SCREEN_HEIGHT || !out_tty || !in_tty || piping) return; sigtrap = 0; piping++; more_pipe = popen(pager_prog, "w"); (void) signal (WHICH_SIG, trap);}/* more_end closes the "more" pipe, if one is open. */voidmore_end() { if (!piping) return; piping = 0; (void) signal (WHICH_SIG, SIG_IGN); if (!sigtrap) (void) pclose (more_pipe); sigtrap = 0;}/* show_file displays a file using the pager. */show_file (file)char *file;{ char line[BUFSIZ]; (void) sprintf (line, "%s %s", pager_prog, file); return !system (line);}voidmessage(msg)char *msg;{ if (piping) { if (!sigtrap) (void) fputs (msg, more_pipe); } else (void) fputs (msg, stdout); if (logfd) Fprintf(logfd, bol_out ? "# %s" : "%s", msg); bol_out = (msg[strlen(msg)-1] == '\n');}voiderrmsg(msg)char *msg;{ (void) fputs(msg, stderr); if (logfd) (void) Fprintf(logfd, bol_out ? "# %s" : "%s", msg); bol_out = (msg[strlen(msg)-1] == '\n');}/* close_logfile msg flag: 0: No messages. 1: Message if we close a file. 2: Message regardless. */close_logfile(msg)int msg;{ if (logfd) { if (msg) message1("Closing log file \"%s\"\n", logname); (void) fclose(logfd); logfd = NULL; logname = NULL; return 1; } else if (msg == 2) message("Logging is already off\n"); return 0;}open_logfile(name)char *name;{ (void) close_logfile(1); logfd = fopen(name, "w"); if (logfd == NULL) { errmsg1("Cannot open log file %s\n", name); return 0; } else message1("Opening log file %s\n", name); logname = name; /* Assumes name will stay around! */ return 1;}show_log_status(){ if (logfd) message1("Writing to log file \"%s\"\n", logname); else message("No log file open\n"); return 1;}/* This is the function called by the lexical analyzer to read input. It reads a line at a time, but returns a character at a time. It prints prompts and copies typed commands to the log file, if one is open. */char line[LINSIZ];static char *lp = line;Getc(fd)FILE *fd;{ if (*lp) return * lp++; if (in_tty && !com_file_depth) Fprintf(stderr, "-> "); else if (eof_flag) exit(0); if (fgets (line, LINSIZ - 1, fd)) { if (logfd) (void) fputs (line, logfd); if (com_file_depth && pr_commands) (void) fputs (line, stdout); lp = line; return * lp++; } else { if (in_tty && !com_file_depth) clearerr (fd); return EOF; }}/* This function pushes back a string onto the standard input */voidpush_back_line (text, addnl)char *text;int addnl;{ if (text == NULL) return; /* safety */ if (*lp == 0) { lp = line; (void) strncpy (line, text, LINSIZ - 2); } else (void) strncat (line, text, LINSIZ - strlen (line) - 2); if (addnl) (void) strcat (line, "\n"); return;}/* this function runs psps on file using flags s*/voidrun_psps(s, file)char *s, *file;{ char *flags = " "; if (debug_level) Fprintf(stderr, "run_psps: called with %s and %s\n", s, file); if (s) flags = s; if (flags[0] != ' ' && flags[0] != '-') { (void) sprintf(msgbuf, "-%s", flags); flags = savestring(msgbuf); } (void)sprintf(msgbuf,"%s %s %s | %s\n",PSPS, flags, file, pager_prog); if (debug_level) Fprintf(stderr, msgbuf); (void) system(msgbuf);}int tags_to_seg = NO;/* open the output file. The file might already exist */open_outfile(s)char *s;{ int i = 0, tags; char *name; if (debug_level) Fprintf(stderr, "open_outfile: called with %s\n", s); if (n_ifiles == 0) { errmsg( "Cannot specify output file when no input files are open.\n"); n_wfields = n_wofields = 0; return 0; } ofile = NULL; for (i = 0; i < n_ifiles; i++) { if (strcmp(s, ifile[i++]) == 0) { errmsg1( "Output file %s cannot be the same as an input file.\n", s); n_wfields = n_wofields = 0; return 0; } }/* check and see if the output file exists, if so create header for buffer that is the same as the output file. */ if ((ostrm = fopen(s, "r+")) != NULL) { if ((outhd = read_header(ostrm)) == NULL || outhd->common.type != FT_FEA || get_esignal_hdr(outhd) != NULL || get_sphere_hdr(outhd) != NULL) { errmsg1("Output file %s is not an ESPS FEA file.\n", s); return 0; } if (!check_names(outhd, ihd[0])){ errmsg1("Output file %s has no field names in common with the first input file.\n",s); return 0; } errmsg1("Output file %s exists; will add records to it.\n",s); ofile = savestring(s); if (n_wfields != 0 || n_wofields != 0) errmsg( "WITH or WITHOUT clause ignored since file already exists.\n"); buffhd = copy_header(outhd); set_header(outhd); n_wfields = n_wofields = 0; (void) clear_buffer(); if (fseek(ostrm,0L,2) != 0) { Fprintf(stderr,"fseek failed!"); exit (1); } return 1; }/* the output files doesn't exist. Open it, and set up the header of the buffer, depending on fields in with_fields and wo_fields.*/ if ((ostrm = fopen(s, "w+")) == NULL) { errmsg1("Cannot open output file %s\n", s); return 0; } if (n_wfields == 0 && n_wofields == 0) buffhd = copy_header(ihd[0]); else if (n_wfields != 0) { buffhd = new_header(FT_FEA); buffhd->common.tag = ihd[0]->common.tag; for (i = 0; i < n_wfields; i++) { name = with_fields[i]; if (lin_search2(ihd[0]->hd.fea->names, name) == -1) errmsg1("Field %s not in first input file\n", name); else if (copy_fea_fld(buffhd, ihd[0], name) == -1) { errmsg1("Trouble adding field %s\n", name); exit(1); } } } else if (n_wofields != 0) { buffhd = new_header(FT_FEA); buffhd->common.tag = ihd[0]->common.tag; for (i = 0; i < ihd[0]->hd.fea->field_count; i++) { name = ihd[0]->hd.fea->names[i]; if (lin_search2(wo_fields, name) == -1) if (copy_fea_fld(buffhd, ihd[0], name) == -1) { errmsg1("Trouble adding field %s\n", name); exit(1); } } } n_wfields = n_wofields = 0; if (buffhd->hd.fea->field_count == 0) { errmsg("No fields in output file; file not open.\n"); buffhd = NULL; return 1; } tags = 0; for(i=0; i<n_ifiles; i++) if(ihd[i]->common.tag) tags++; if(tags >1) { char **names = (char **)malloc((n_ifiles+1)*sizeof(char *)); int j=1; names[0] = "NONE"; for (i=0; i<n_ifiles; i++) if(ihd[i]->common.tag) names[j++] = savestring(ihd[i]->variable.refer); names[j] = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -