📄 more.c
字号:
} switch (*fmt++) { case 'd': ccount += printd (va_arg(ap, int)); break; case 's': ccount += pr (va_arg(ap, char *)); break; case '%': ccount++; putchar ('%'); break; case '0': return (ccount); default: break; } } va_end(ap); return (ccount);}/*** Print an integer as a string of decimal digits,** returning the length of the print representation.*/printd (n)int n;{ int a, nchars; if (a = n/10) nchars = 1 + printd(a); else nchars = 1; putchar (n % 10 + '0'); return (nchars);}/* Put the print representation of an integer into a string */static char *sptr;scanstr (n, str)int n;char *str;{ sptr = str; Sprintf (n); *sptr = '\0';}Sprintf (n){ int a; if (a = n/10) Sprintf (a); *sptr++ = n % 10 + '0';}static char bell = ctrl('G');/* See whether the last component of the path name "path" is equal to the** string "string"*/tailequ (path, string)char *path;register char *string;{ register char *tail; tail = path + strlen(path); while (tail >= path) if (*(--tail) == '/') break; ++tail; while (*tail++ == *string++) if (*tail == '\0') return(1); return(0);}prompt (filename)char *filename;{ if (clreol) cleareol (); else if (promptlen > 0) kill_line (); if (!hard) { promptlen = 8; if (Senter && Sexit) { tputs (Senter, 1, putch); promptlen += (2 * soglitch); } if (clreol) cleareol (); pr("--More--"); if (filename != NULL) { promptlen += prtf ("(Next file: %s)", filename); } else if (!no_intty) { promptlen += prtf ("(%d%%)", (int)((file_pos * 100) / file_size)); } if (dum_opt) { promptlen += pr("[Press space to continue, 'q' to quit.]"); } if (Senter && Sexit) tputs (Sexit, 1, putch); if (clreol) clreos (); fflush(stdout); } else write (2, &bell, 1); inwait++;}/*** Get a logical line*/getline(f, length)register FILE *f;int *length;{ register int c; register char *p; register int column; static int colflg; p = Line; column = 0; c = Getc (f); if (colflg && c == '\n') { Currline++; c = Getc (f); } while (p < &Line[LINSIZ - 1]) { if (c == EOF) { if (p > Line) { *p = '\0'; *length = p - Line; return (column); } *length = p - Line; return (EOF); } if (c == '\n') { Currline++; break; } *p++ = c; if (c == '\t') if (!hardtabs || column < promptlen && !hard) { if (hardtabs && eraseln && !dumb) { column = 1 + (column | 7); tputs (eraseln, 1, putch); promptlen = 0; } else { for (--p; p < &Line[LINSIZ - 1];) { *p++ = ' '; if ((++column & 7) == 0) break; } if (column >= promptlen) promptlen = 0; } } else column = 1 + (column | 7); else if (c == '\b' && column > 0) column--; else if (c == '\r') column = 0; else if (c == '\f' && stop_opt) { p[-1] = '^'; *p++ = 'L'; column += 2; Pause++; } else if (c == EOF) { *length = p - Line; return (column); } else if (c >= ' ' && c != RUBOUT) column++; if (column >= Mcol && fold_opt) break; c = Getc (f); } if (column >= Mcol && Mcol > 0) { if (!Wrap) { *p++ = '\n'; } } colflg = column == Mcol && fold_opt; if (colflg && eatnl && Wrap) { *p++ = '\n'; /* simulate normal wrap */ } *length = p - Line; *p = 0; return (column);}/*** Erase the rest of the prompt, assuming we are starting at column col.*/erase (col)register int col;{ if (promptlen == 0) return; if (hard) { putchar ('\n'); } else { if (col == 0) putchar ('\r'); if (!dumb && eraseln) tputs (eraseln, 1, putch); else for (col = promptlen - col; col > 0; col--) putchar (' '); } promptlen = 0;}/*** Erase the current line entirely*/kill_line (){ erase (0); if (!eraseln || dumb) putchar ('\r');}/* * force clear to end of line */cleareol(){ tputs(eraseln, 1, putch);}clreos(){ tputs(EodClr, 1, putch);}/*** Print string and return number of characters*/pr(s1)char *s1;{ register char *s; register char c; for (s = s1; c = *s++; ) putchar(c); return (s - s1 - 1);}/* Print a buffer of n characters */prbuf (s, n)register char *s;register int n;{ register char c; /* next output character */ register int state; /* next output char's UL state */#define wouldul(s,n) ((n) >= 2 && (((s)[0] == '_' && (s)[1] == '\b') || ((s)[1] == '\b' && (s)[2] == '_'))) while (--n >= 0) if (!ul_opt) putchar (*s++); else { if (*s == ' ' && pstate == 0 && ulglitch && wouldul(s+1, n-1)) { s++; continue; } if (state = wouldul(s, n)) { c = (*s == '_')? s[2] : *s ; n -= 2; s += 3; } else c = *s++; if (state != pstate) { if (c == ' ' && state == 0 && ulglitch && wouldul(s, n-1)) state = 1; else tputs(state ? ULenter : ULexit, 1, putch); } if (c != ' ' || pstate == 0 || state != 0 || ulglitch == 0) putchar(c); if (state && *chUL) { pr(chBS); tputs(chUL, 1, putch); } pstate = state; }}/*** Clear the screen*/doclear(){ if (Clear && !hard) { tputs(Clear, 1, putch); /* Put out carriage return so that system doesn't ** get confused by escape sequences when expanding tabs */ putchar ('\r'); promptlen = 0; }}/* * Go to home position */home(){ tputs(Home,1,putch);}static int lastcmd, lastarg, lastp;static int lastcolon;char shell_line[132];/*** Read a command and do it. A command consists of an optional integer** argument followed by the command character. Return the number of lines** to display in the next screenful. If there is nothing more to display** in the current file, zero is returned.*/command (filename, f)char *filename;register FILE *f;{ register int nlines; register int retval; register char c; char colonch; FILE *helpf; int done; char comchar, cmdbuf[80], *p;#define ret(val) retval=val;done++;break done = 0; if (!errors) prompt (filename); else errors = 0; if (MBIT == RAW && slow_tty) { otty.sg_flags |= MBIT; stty(fileno(stderr), &otty); } for (;;) { nlines = number (&comchar); lastp = colonch = 0; if (comchar == '.') { /* Repeat last command */ lastp++; comchar = lastcmd; nlines = lastarg; if (lastcmd == ':') colonch = lastcolon; } lastcmd = comchar; lastarg = nlines; if (comchar == otty.sg_erase) { kill_line (); prompt (filename); continue; } switch (comchar) { case ':': retval = colon (filename, colonch, nlines); if (retval >= 0) done++; break; case 'b': case ctrl('B'): { register int initline; if (no_intty) { write(2, &bell, 1); return (-1); } if (nlines == 0) nlines++; putchar ('\r'); erase (0); prtf ("\n"); if (clreol) cleareol (); prtf ("...back %d page", nlines); if (nlines > 1) pr ("s\n"); else pr ("\n"); if (clreol) cleareol (); pr ("\n"); initline = Currline - dlines * (nlines + 1); if (! noscroll) --initline; if (initline < 0) initline = 0; Fseek(f, 0L); Currline = 0; /* skiplns() will make Currline correct */ skiplns(initline, f); if (! noscroll) { ret(dlines + 1); } else { ret(dlines); } } case ' ': case 'z': if (nlines == 0) nlines = dlines; else if (comchar == 'z') dlines = nlines; ret (nlines); case 'd': case ctrl('D'): if (nlines != 0) nscroll = nlines; ret (nscroll); case 'q': case 'Q': end_it (); case 's': case 'f': if (nlines == 0) nlines++; if (comchar == 'f') nlines *= dlines; putchar ('\r'); erase (0); prtf ("\n"); if (clreol) cleareol (); prtf ("...skipping %d line", nlines); if (nlines > 1) pr ("s\n"); else pr ("\n"); if (clreol) cleareol (); pr ("\n"); while (nlines > 0) { while ((c = Getc (f)) != '\n') if (c == EOF) { retval = 0; done++; goto endsw; } Currline++; nlines--; } ret (dlines); case '\n': if (nlines != 0) dlines = nlines; else nlines = 1; ret (nlines); case '\f': if (!no_intty) { doclear (); Fseek (f, screen_start.chrctr); Currline = screen_start.line; ret (dlines); } else { write (2, &bell, 1); break; } case '\'': if (!no_intty) { kill_line (); pr ("\n***Back***\n\n"); Fseek (f, context.chrctr); Currline = context.line; ret (dlines); } else { write (2, &bell, 1); break; } case '=': kill_line (); promptlen = printd (Currline); fflush (stdout); break; case 'n': lastp++; case '/': if (nlines == 0) nlines++; kill_line (); pr ("/"); promptlen = 1; fflush (stdout); if (lastp) { write (2,"\r", 1); search (NULL, f, nlines); /* Use previous r.e. */ } else { ttyin (cmdbuf, 78, '/'); write (2, "\r", 1); search (cmdbuf, f, nlines); } ret (dlines-1); case '!': do_shell (filename); break; case '?': case 'h': if ((helpf = fopen (HELPFILE, "r")) == NULL) error ("Can't open help file"); if (noscroll) doclear (); copy_file (helpf); fclose (helpf); prompt (filename); break; case 'v': /* This case should go right before default */ if (!no_intty) { kill_line (); cmdbuf[0] = '+'; scanstr (Currline - dlines < 0 ? 0 : Currline - (dlines + 1) / 2, &cmdbuf[1]); pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]); execute (filename, _PATH_VI, "vi", cmdbuf, fnames[fnum], 0); break; } default: if (dum_opt) { kill_line (); if (Senter && Sexit) { tputs (Senter, 1, putch); promptlen = pr ("[Press 'h' for instructions.]") + (2 * soglitch); tputs (Sexit, 1, putch); } else promptlen = pr ("[Press 'h' for instructions.]"); fflush (stdout); } else write (2, &bell, 1); break; } if (done) break; } putchar ('\r');endsw: inwait = 0; notell++; if (MBIT == RAW && slow_tty) { otty.sg_flags &= ~MBIT; stty(fileno(stderr), &otty); } return (retval);}char ch;/* * Execute a colon-prefixed command. * Returns <0 if not a command that should cause * more of the file to be printed. */colon (filename, cmd, nlines)char *filename;int cmd;int nlines;{ if (cmd == 0) ch = readch (); else ch = cmd; lastcolon = ch; switch (ch) { case 'f': kill_line (); if (!no_intty) promptlen = prtf ("\"%s\" line %d", fnames[fnum], Currline); else promptlen = prtf ("[Not a file] line %d", Currline); fflush (stdout); return (-1); case 'n': if (nlines == 0) { if (fnum >= nfiles - 1) end_it (); nlines++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -