📄 edit.c
字号:
lp = &ln[1]; /* use it. */ lp = skipwhite(lp); if (*lp) filename = lp; quit = 1; } else { printf("huh?\n"); } } if (filename) { int err; char buf[16]; if (tfp) { int link; /* If filename and TFS_NAME(tfp) differ, then we are editing * a file through a symbolic link. If it is a link, then we * use the info/flags/filename from the source file (using the * file pointer and ignoring user input stuff). */ link = strcmp(filename,TFS_NAME(tfp)); if ((!flags) || (link)) flags = (char *)tfsctrl(TFS_FBTOA,TFS_FLAGS(tfp),(long)buf); if ((!info) || (link)) info = TFS_INFO(tfp); if (link) { printf("Updating source file '%s' thru link '%s'\n", TFS_NAME(tfp),filename); filename = TFS_NAME(tfp); } } if (!flags) flags = (char *)0; if (!info) info = (char *)0; err = tfsadd(filename,info,flags,buffer,eob-buffer); if (err != TFS_OKAY) printf("%s: %s\n",filename,(char *)tfsctrl(TFS_ERRMSG,err,0)); } efree(bsize,buffer); return(CMD_SUCCESS);}static voidlnprbuf(char *buffer,char *bp,char *eob){ int lno, currentline; if (buffer == eob) return; lno = 1; currentline = whatlineisthis(buffer,bp); prlno(lno++,currentline); while(buffer < eob) { putchar(*buffer); if ((*buffer == '\n') && ((buffer + 1) != eob)) prlno(lno++,currentline); buffer++; }}static voidprlno(int lno,int currentline){ char *fmt; if (lno == currentline) fmt = ">%2d: "; else fmt = "%3d: "; printf(fmt,lno);}static voidprbuf(char *bp,char *eob){ while(bp < eob) putchar(*bp++);}char *prline(char *bp,char *eob){ while(bp < eob) { putchar(*bp); if (*bp == '\n') break; bp++; } return(bp+1);}/* searchfor(): * Step through the buffer starting at 'bp' and search for a memory match * with the incoming string pointed to by 'srch'. If not found, simply * return after the entire buffer has been searched (wrap to start if * necessary). If found, then adjust '*bp' to point to the beginning of * of the line that contains the match. */static intsearchfor(char *srch,char *buffer,char **bp,char *eob){ static char *lastsrchstring; char *startedhere, *tbp; int len; tbp = *bp; if (tbp < eob) startedhere = *bp; else tbp = startedhere = buffer; if (*srch) { len = strlen(srch); if (lastsrchstring) free(lastsrchstring); lastsrchstring = malloc(len+1); if (lastsrchstring) strcpy(lastsrchstring,srch); } else if (lastsrchstring) { len = strlen(lastsrchstring); srch = lastsrchstring; } else return(-1); do { if ((tbp + len) > eob) { tbp = buffer; } else { if (!memcmp(tbp,srch,len)) { while(1) { if (tbp <= buffer) { *bp = buffer; break; } if (*tbp == '\n') { *bp = tbp+1; break; } tbp--; } prline(*bp,eob); return(0); } else tbp++; } } while(tbp != startedhere); printf("'%s' not found\n",srch); return(-1);}static intgotoline(char *ln,char *buffer,char **bp,char *eob,int verbose){ int lno, i, moveforward; char *tbp, *base; base = buffer; moveforward = 1; /* If destination line number is '.', assume you're already there. * If the '.' is followed by a '+' or '-' then the following number * is considered an offset from the current line instead of from the * start of the buffer. */ if (ln[0] == '.') { base = *bp; if (ln[1] == '+') lno = atoi(&ln[2]) + 1; else if (ln[1] == '-') { lno = atoi(&ln[2]) + 2; moveforward = 0; } else goto end; } else if (ln[0] == '$') { lno = 99999999; } else lno = atoi(ln); if (lno < 1) { printf("Invalid line\n"); return(-1); } if (moveforward) { for(tbp=base,i=1;i<lno&&tbp<eob;tbp++) if (*tbp == '\n') i++; if (tbp == eob) { if (verbose) printf("Pointer set to end of buffer.\n"); /* If out of range, set pointer to end of buffer. */ *bp = eob; return(-1); } } else { for(tbp=base,i=1;i<lno&&tbp>=buffer;tbp--) if (*tbp == '\n') i++; if (tbp < buffer) { if (verbose) printf("Pointer set to start of buffer.\n"); /* If out of range, set pointer to beginning of buffer. */ *bp = buffer; return(-1); } tbp+=2; } *bp = tbp;end: if (verbose) { printf("%3d: ",whatlineisthis(buffer,*bp)); prline(*bp,eob); } return(0);}static intwhatlineisthis(char *buffer,char *bp){ int lno; char *cp; cp = buffer; lno = 1; while(cp < bp) { if (*cp == '\n') lno++; cp++; } return(lno);}static intgetrange(char *cp,int *begin,int *end,char *buffer,char *bp,char *eob){ char *dash; int b, e, lastline, thisline; if (!*cp) return(0); lastline = whatlineisthis(buffer,eob) - 1; thisline = whatlineisthis(buffer,bp); if (*cp == '.') b = thisline; else b = atoi(cp); dash = strchr(cp,'-'); if (dash) { if (*(dash+1) == '$') e = lastline; else if (*(dash+1) == '.') e = thisline; else e = atoi(dash+1); } else e = b; if (e > lastline) e = lastline; if (begin) *begin = b; if (end) *end = e; if ((e <= 0) || (b <=0) || (e < b)) printf("Bad range\n"); return(e - b + 1);}static voiddeletelines(char *lp,int lcnt,char *buffer,char **bp,char **eob){ char *cp, *cp1, *t_bp, *t_eob; if (lcnt <= 0) return; t_bp = *bp; t_eob = *eob; if (gotoline(lp,buffer,&t_bp,t_eob,0) == -1) return; cp = cp1 = t_bp; while(lcnt>0) { if (cp >= t_eob) { printf("Pointer set to end of buffer.\n"); break; } if (*cp == '\n') lcnt--; cp++; } while(cp != t_eob) *cp1++ = *cp++; *eob = cp1; *bp = t_bp;}static char *skipwhite(char *cp){ while((*cp == ' ') || (*cp == '\t')) cp++; return(cp);}/* rmCR(): * Given the source and size of the buffer, remove all instances of 0x0d * (carriage return) from the buffer. Return the number of CRs removed.*/static intrmCR(char *src,int size){ int i; /* Index into src array. */ int tot; /* Keep track of total # of 0x0d's removed. */ int remaining; /* Keep track of how far to go. */ tot = 0; remaining = size; for (i=0;i<size;i++) { remaining--; if (src[i] == 0x0d) { src[i] = 0; /* Make sure memory is writeable. */ if (src[i] != 0) continue; memcpy(&src[i],&src[i+1],remaining); tot++; } } if (tot) printf("Removed %d CRs\n",tot); return(tot);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -