📄 pg.c
字号:
break; } kill_line(); sopr("saving file ",1); sopr(cmdptr,1); fflush(stdout); save_input(sf); error("saved"); } fclose(sf); break; case 'q': case 'Q': CHECKEND(); inwait = 0; end_it(); case 'f': /* skip forward screenfuls */ CHECKEND(); if (sign == 0) sign++; /* skips are always relative */ if (nlines == 0) nlines++; nlines = nlines * (window - 1); if (sign == 1) new_ss.first_line = old_ss.last_line + nlines; else new_ss.first_line = old_ss.first_line - nlines; new_ss.last_line = new_ss.first_line + window - 1; inwait = 0; return(0); case 'l': /* get a line */ CHECKEND(); if (nlines == 0) { nlines++; if (sign == 0) sign = 1; } switch(sign){ case 1: new_ss.last_line = old_ss.last_line + nlines; new_ss.first_line = new_ss.last_line - window + 1; break; case 0: /* leave addressed line at top */ new_ss.first_line = nlines; new_ss.last_line = nlines + window - 1; break; case -1: new_ss.first_line = old_ss.first_line - nlines; new_ss.last_line = new_ss.first_line + window - 1; break; } inwait = 0; return(0); case '\0': /* \n or blank */ if (nlines == 0){ nlines++; if (sign == 0) sign = 1; } nlines = (nlines - 1) * (window - 1); switch(sign) { case 1: new_ss.first_line = old_ss.last_line + nlines; new_ss.last_line = new_ss.first_line + window - 1; break; case 0: new_ss.first_line = nlines + 1; new_ss.last_line = nlines + window; break; case -1: new_ss.last_line = old_ss.first_line - nlines; new_ss.first_line = new_ss.last_line - window + 1; break; } inwait = 0; return(0); case 'n': /* switch to next file in arglist */ CHECKEND(); if (sign == 0) sign = 1; if (nlines == 0) nlines++; if ((skip = skipf(sign *nlines)) == 0) { BEEP(); break; } inwait = 0; return(skip); case 'p': /* switch to previous file in arglist */ CHECKEND(); if (sign == 0) sign = 1; if (nlines == 0) nlines++; if ((skip = skipf(-sign * nlines)) == 0) { BEEP(); break; } inwait = 0; return(skip); case '$': /* go to end of file */ CHECKEND(); sign = 1; while(find(1,10000) != EOF) /* any large number will do */; new_ss.last_line = eofl_no - 1; new_ss.first_line = eofl_no - window; inwait = 0; return(0); case '/': /* search forward for r.e. */ case '?': /* " backwards */ case '^': /* this ones a ? for regent100s */ if(sign < 0) { BEEP(); break; } if (nlines == 0) nlines++; cmdptr--; cmdend = cmdptr + (strlen(cmdptr) - 1); if ( (cmdend > cmdptr + 1) && (*cmdptr == *(cmdend - 1)) && ( ((c = *cmdend) == 't') || (c == 'm') || (c == 'b') ) ) { leave_search = c; cmdend--; } if ((cmdptr < cmdend) && (*cmdptr == *cmdend)) *cmdend = '\0'; if (*cmdptr != '/') /* signify back search by - */ nlines = -nlines; if (!search(++cmdptr, nlines)) break; else { inwait = 0; return(0); } case '!': /* shell escape */ if (!hard_copy) { /* redisplay the command */ pr(cmdbuf); pr("\n"); } if ((id = fork ()) < 0) { error("cannot fork, try again later"); break; } if (id == 0) { /* * if stdin is a pipe, need to close it so * that the terminal is really stdin for * the command */ fclose(stdin); dup(fileno(stdout)); execl(shell, shell, "-c", cmdptr, 0); fprintf(stderr,"exec failed\n"); exit(1); } signal (SIGINT, SIG_IGN); signal (SIGQUIT, SIG_IGN); wait ((int *) 0); pr("!\n"); fflush(stdout); signal(SIGINT,on_brk); signal(SIGQUIT,on_brk); break; default: BEEP(); break; } }}number(){ register int i; register char *p; i = 0; sign = 0; p = cmdptr; BLANKS(p); if (*p == '+') { p++; sign = 1; } else if (*p == '-') { p++; sign = -1; } while (isdigit(*p)) i = i * 10 + *p++ - '0'; cmdptr = p; return(i);}ttyin (){ register char *sptr; register char ch; register int slash = 0; int state = 0; fixterm(); set_state(&state,' ',NULL); /* initialize state processing */ sptr = cmdbuf; while(state != 10) { ch = readch(); if (ch == '\n' && !slash) break; if (ch == erasechar() && !slash) { if (sptr > cmdbuf) { --promptlen; pr("\b \b"); --sptr; if (*sptr < ' ') { --promptlen; pr("\b \b"); } } set_state(&state,ch,sptr); fflush(stdout); continue; } else if (ch == killchar() && !slash) { if (hard_copy) putchar(ch); resetterm(); return(1); } if (slash) { slash = 0; pr("\b \b"); sptr--; promptlen--; } else /* is there room to keep this character? */ if (sptr>=cmdbuf + sizeof(cmdbuf) || promptlen + 2 >= columns) { BEEP(); continue; } else if (ch == '\\') slash++; if (set_state(&state,ch,sptr) == 0) { BEEP(); continue; } *sptr++ = ch; if (ch < ' ') { ch += 0100; putchar('^'); promptlen++; } putchar(ch); promptlen++; fflush(stdout); } *sptr = '\0'; kill_line(); fflush(stdout); resetterm(); return(0);}set_state(pstate,c,pc)register int *pstate;register int c;register char *pc;{ static char *psign; static char *pnumber; static char *pcommand; static int slash; if (*pstate == 0) { psign = NULL; pnumber = NULL; pcommand = NULL; *pstate = 1; slash = 0; return(1); } if (c == '\\' && !slash) { slash++; return(1); } if (c == erasechar() && !slash) switch(*pstate) { case 4: if (pc > pcommand) return(1); pcommand = NULL; case 3: if (pnumber && pc > pnumber) { *pstate = 3; return(1); } pnumber = NULL; case 2: if (psign && pc > psign) { *pstate = 2; return(1); } psign = NULL; case 1: *pstate = 1; return(1); } slash = 0; switch(*pstate) { case 1: /* before recieving anything interesting */ if (c == '\t' || (!nflag && c == ' ')) return(1); if (c == '+' || c == '-') { psign = pc; *pstate = 2; return(1); } case 2: /* recieved sign, waiting for digit */ if (isdigit(c)) { pnumber = pc; *pstate = 3; return(1); } case 3: /* recieved digit, waiting for the rest of the number */ if (isdigit(c)) return(1); if (strchr("h\014.wz\004dqQfl np$",c)) { pcommand = pc; if (nflag) *pstate = 10; else *pstate = 4; return(1); } if (strchr("s/^?!",c)) { pcommand = pc; *pstate = 4; return(1); } return(0); case 4: return(1); }}readch (){ char ch; read (fileno(stdout), &ch, 1); return (ch);}help(){ if (clropt) doclear(); pr("-------------------------------------------------------\n"); pr(" h help\n"); pr(" q or Q quit\n"); pr(" <blank> or \\n next page\n"); pr(" l next line\n"); pr(" d or ^D display half a page more\n"); pr(" . or ^L redisplay current page\n"); pr(" f skip the next page forward\n"); pr(" n next file\n"); pr(" p previous file\n"); pr(" $ last page\n"); pr(" w or z set window size and display next page\n"); pr(" s savefile save current file in savefile\n"); pr(" /pattern/ search forward for pattern\n"); pr(" ?pattern? or\n"); pr(" ^pattern^ search backward for pattern\n"); pr(" !command execute command\n"); pr("\n"); pr("Most commands can be preceeded by a number, as in:\n"); pr("+1\\n (next page); -1\\n (previous page); 1\\n (page 1).\n"); pr("\n"); pr("See the manual page for more detail.\n"); pr("-------------------------------------------------------\n");}/* * Skip nskip files in the file list (from the command line). Nskip may be * negative. */skipf (nskip)register int nskip;{ if (fnum + nskip < 0) { nskip = -fnum; if (nskip == 0) error("No previous file"); } else if (fnum + nskip > nfiles - 1) { nskip = (nfiles - 1) - fnum; if (nskip == 0) error("No next file"); } return(nskip);}/* * Check whether the file named by fs is a file which the user may * access. If it is, return the opened file. Otherwise return NULL. */FILE *checkf (fs)register char *fs;{ struct stat stbuf; register FILE *f; int fd; int c; pipe_in = 0; if (strcmp(fs,"-") == 0) { if (tmp_fin == NULL) f = stdin; else { rewind(tmp_fin); f = tmp_fin; } } else { if ((f=fopen(fs, "r")) == NULL) { fflush(stdout); perror(fs); return (NULL); } } if (fstat(fileno(f), &stbuf) == -1) { fflush(stdout); perror(fs); return (NULL); } if (stbuf.st_mode & S_IFDIR) { fprintf(stderr,"pg: %s is a directory\n",fs); return (NULL); } if (stbuf.st_mode & S_IFREG) { if (f == stdin) /* It may have been read from */ rewind(f); /* already, and not reopened */ } else { if (f != stdin) { fprintf(stderr,"pg: special files only handled as standard input\n"); return(NULL); } else { mktemp(tmp_name); if ((fd=creat(tmp_name,0600)) < 0) { fprintf(stderr,"pg: Can't create temp file\n"); return(NULL); } close(fd); if ((tmp_fou = fopen(tmp_name, "w")) == NULL) { fprintf(stderr,"pg: Can't get temp file for writing\n"); return(NULL); } if ((tmp_fin = fopen(tmp_name, "r")) == NULL) { fprintf(stderr,"pg: Can't get temp file for reading\n"); return(NULL); } pipe_in = 1; } } lineset(BOF); return(f);}copy_file(f, out)register FILE *f, *out;{ register int c; while ((c = getc(f)) != EOF) putc(c,out);}#define INIT register char *sp = instring;#define GETC() (*sp++)#define PEEKC() (*sp)#define UNGETC() (--sp)#define RETURN(c) return#define ERROR(c) re_error(c)#include <regexp.h>re_error(i)int i;{ int j; static struct messages { char *message; int number; } re_errmsg[] = { "Pattern not found", 1, "Range endpoint too large", 11, "Bad number", 16, "`\\digit' out of range", 25, "Illegal or missing delimeter", 36, "No remembered search string", 41, "\\( \\) imbalance", 42, "Too many \\(", 43, "More than two numbers given in \\{ \\}", 44, "} expected after \\", 45, "First number exceeds second in \\{ \\}", 46,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -