/* Modified 24/2/84 to add Environment option flag selection */ /* and -T flag for setting tab size - PLS */ #include "ded.h" #include "lines.h" #include "char.h" #include "file.h" /************************************************************************ * * * copyright Richard Bornat 1981 * * * ************************************************************************/ /* array of strings which disable automatic 'text input' mode */ char *notxtails[] = { ".c", ".h", ".m", ".m11", ".s", ".p", ".pn", ".out", ".make", ".Make", "makefile", "Makefile", ".mk", 0 }; /* array of strings which indicate automatic 'tab output' mode */ char *tbtails[] = { ".c", ".h", ".m", ".m11", ".s", ".p", ".pn", ".make", ".Make", "makefile", "Makefile", ".mk", 0 }; char *e_str; char e_arg[ENOUGH]; char *env_argsequence() /* UMIST */ { register char *argp = e_arg, *strp = e_str; register char c = *strp; printf("In env_args %s\n",e_str); if (strp == (char *)0) return((char *)0); while (c == ' ') c = *++strp; if (c == '\0') return((char *)0); else { do *argp++ = c = *strp++; while (c!='\0' && c!=' '); if (c == ' ') *--argp = '\0'; else strp--; e_str = strp; return(e_arg); } } setupedit(sequence, error) char *(*sequence)(); int (*error)(); { register char *arg; struct stat statbuf; int txopt=false, tbopt=false; int dlogging=false, dlog_ignore=false; int orig_opt=false, orig_line=0, orig_adjust=0, orig_col=0; int orig_too_high=false; int eof_opt=false; int num; char filen[ENOUGH], pathn[ENOUGH], tailn[ENOUGH]; extern char *stod(); extern char *getenv(); filen[0]=0; e_str = getenv("DEDOPT"); /* get flags from env first, then cmd line args - UMIST */ while ((arg = env_argsequence()) != (char *)0 || (arg = (*sequence)()) != (char *)0) { if (*arg++=='-') switch(*arg++) { case 'o': if (*arg==0) (*error)("no debug option follows -o"); else if (set_dbug(*arg)) break; else (*error)("invalid debug option %c", *arg); case 'b': txopt = true; txin = (*arg!='-'); break; case 'd': if (*arg=='-') dlog_ignore = true; else dlogging = true; break; case 'i': /* UMIST */ tab_preserve = (*arg!='-'); break; case 'l': /* select line number */ orig_opt = true; arg = stod(arg, &orig_line); orig_line--; /* fall through to SET_COL */ SET_COL: /* sorry */ { char c = *arg++; if (c=='+') { orig_adjust = 1; c = *arg++; } else if (c=='-') { orig_adjust = -1; c = *arg++; } if (c=='.') { if ((c = *arg++)=='$') orig_col = -1; else { arg = stod(--arg, &orig_col); /* returns 0 if no number */ if (orig_col!=0) orig_col -= 1; } c = *arg; } if (c!=0) (*error)("invalid -l or -$ option"); } break; case 'm': { char c = *arg++; if (c == 0 || *arg!= 0) (*error)("'-m' must be followed by single character"); else c_MODIFY = c; break; } case 'P': /* io from pipe */ Pswitch = true; tty_input = std_in = STD_IN; std_out = STD_OUT; arg = stod(arg, &Pcols); if (*arg++!='.') (*error)(". expected after -P"); arg = stod(arg, &Prows); if (setup_done && (Pcols!=ncols || Prows!=nrows)) (*error)("attempt to alter size of screen [setupedit]"); break; case 's': ringing = false; break; case 't': tbopt = true; tbout = (*arg!='-'); break; case 'T': /* UMIST */ arg = stod(arg, &num); if (*arg == 0 && num > 0) tabsiz = num; else (*error)("invalid -T option"); break; case '$': orig_opt = eof_opt = true; orig_col = -1; goto SET_COL; /* sorry */ case '\0': (*error)("no option follows '-'"); default: (*error)("unknown option %s", arg-2); } else { if (filen[0]==0) cat(--arg, filen); else (*error)("two filenames specified"); } } if (filen[0]==0) (*error)("no file specified"); if (!txopt) txin = !anytail(filen, notxtails); if (!tbopt) tbout = anytail(filen, tbtails); dtos(getpid(), pidname); cat3("/tmp/",pidname,".ded",tempname); /* find pathname in pathn array */ { char *path = pathn, *tail = pathn; char *file = filen; char c; while ((c = *path++ = *file++) != 0) if (c=='/') tail = path; cat(tail,tailn); *tail = 0; /* put end of string after last slash in pathn */ } /* set up temporary file - first set up tty to find out how * big screen is */ ttysetup(); if (stat(filen,&statbuf) == -1) { if (errno==ENOTDIR) (*error)("invalid directory name in %s", filen); else if (errno==ENOENT) { char lbuf[ENOUGH]; cat2(pathn, ".", lbuf); if (stat(lbuf,&statbuf) == -1 || #ifdef V7 (statbuf.st_mode & 040000) == 0) /* test for directory */ #else (statbuf.flags & 040000) == 0) /* test for directory */ #endif (*error)("invalid pathname %s", pathn); } else (*error)("unix io error %d [setupedit]", errno); maketemp(tempname); orig_line = orig_col = 0; } else { /* from this point on, errors cause the edit to abort */ infile(filen, tempname); if (orig_opt && orig_line>maxl && orig_line>LASTINROW) { orig_line=maxl; orig_col = -1; orig_adjust=0; orig_too_high=true; } else if (eof_opt) orig_line=maxl; } /* from this point on, errors cause the edit to abort */ cat(filen,filename); cat(pathn,pathname); cat(tailn,tailname); /* close earlier dlog file */ if (dlogger != -1) { close(dlogger); unlink(dlogname); } ncat2(tailn, ".dlog", dlogname); if (dlogging) { if ((tty_input = dlogger = open_dlog(dlogname)) == -1) editerror("can't open .dlog file"); } else { orig_line += orig_adjust; topl = (orig_opt ? orig_line-(NINROWS/2) : 0); adj_origin(orig_line); if (orig_col == -1) { if (l_offset[orig_line] == (unsigned)-1) orig_col = 0; else { char lbuf[ENOUGH]; getfline(orig_line, lbuf); orig_col = strlength(lbuf)-MARGIN+1; } } set_c(orig_line-topl, MARGIN+orig_col, &in_c); set_c(EDITROW, 1, &edit_c); if (!dlog_ignore && open_dlog(dlogname) != -1) editerror("dlog file exists - use -d or -d- option"); else if ((dlogger = creat_dlog(dlogname)) == -1) editerror("can't create .dlog file"); } cat(overtype? "\335" : ">", erow); /* "\0335" = ']'|0200 !! */ setupscreen(); if (stat(filen,&statbuf) == -1) diag("new file - empty screen"); else #ifdef V7 if (statbuf.st_nlink > 1) #else if (statbuf.nlinks > 1) #endif diag("overwriting file with more than one link"); else if (orig_too_high) diag("cursor positioned at end of file"); } infile(fname, tname) char *fname, *tname; { char linebuf[ENOUGH]; extern quit(); maketemp(tname); openinput(fname, quit); while(inline(linebuf, quit)) if (++maxl>=NLINES) quit("[infile] too many lines - maximum is %d", NLINES); else addline(maxl, linebuf); closeinput(); }