--- pgp/src/more.c 2018/04/24 16:37:52 1.1 +++ pgp/src/more.c 2018/04/24 16:40:45 1.1.1.5 @@ -1,21 +1,22 @@ /* more.c - Unix-style "more" paging output for PGP. PGP: Pretty Good(tm) Privacy - public key cryptography for the masses. - (c) Copyright 1990-1992 by Philip Zimmermann. All rights reserved. + (c) Copyright 1990-1994 by Philip Zimmermann. All rights reserved. The author assumes no liability for damages resulting from the use of this software, even if the damage results from defects in this software. No warranty is expressed or implied. - All the source code Philip Zimmermann wrote for PGP is available for - free under the "Copyleft" General Public License from the Free - Software Foundation. A copy of that license agreement is included in - the source release package of PGP. Code developed by others for PGP - is also freely available. Other code that has been incorporated into - PGP from other sources was either originally published in the public - domain or was used with permission from the various authors. See the - PGP User's Guide for more complete information about licensing, - patent restrictions on certain algorithms, trademarks, copyrights, - and export controls. + Note that while most PGP source modules bear Philip Zimmermann's + copyright notice, many of them have been revised or entirely written + by contributors who frequently failed to put their names in their + code. Code that has been incorporated into PGP from other authors + was either originally published in the public domain or is used with + permission from the various authors. + + PGP is available for free to the public under certain restrictions. + See the PGP User's Guide (included in the release package) for + important information about licensing, patent restrictions on + certain algorithms, trademarks, copyrights, and export controls. */ #include @@ -25,23 +26,30 @@ #ifdef UNIX #include #endif +#ifdef sco +#include +#include +FILE *popen(); +#endif +#include "system.h" #include "mpilib.h" #include "language.h" #include "fileio.h" #include "pgp.h" - -/* Prototype for getch() */ - -int getch( void ); +#include "more.h" +#include "charset.h" #ifdef MSDOS +#ifndef __GO32__ +#include +#endif #define DEFAULT_LINES 25 /* MSDOS actually has a 25-line screen */ #else #define DEFAULT_LINES 24 #endif /* MSDOS */ #define DEFAULT_COLUMNS 80 -int screen_lines = DEFAULT_LINES, screen_columns = DEFAULT_COLUMNS; +static int screen_lines = DEFAULT_LINES, screen_columns = DEFAULT_COLUMNS; #define TAB 0x09 /* ASCII tab char */ #define CR '\r' /* Carriage return char */ @@ -83,14 +91,14 @@ int screen_lines = DEFAULT_LINES, screen /* Try to get TIOCGWINSZ from termios.h, then from sys/ioctl.h */ #ifndef NOTERMIO -#ifndef M_XENIX -#include -#else +#ifdef SVR2 #include -#endif /* not M_XENIX */ +#else +#include +#endif /* SVR2 */ #endif -#ifndef M_XENIX +#ifndef SVR2 #ifndef TIOCGWINSZ #ifndef TIOCGSIZE #ifndef WIOCGETD @@ -107,13 +115,15 @@ int screen_lines = DEFAULT_LINES, screen #endif /* not WIOCGETD */ #endif /* not TIOCGSIZE */ #endif /* not TIOCGWINSZ */ -#endif /* not M_XENIX */ +#endif /* not SVR2 */ +#endif /* UNIX */ static void getScreenSize(void) /* Rot bilong kargo */ /* Return the screen size */ { char *envLines, *envColumns; long rowTemp = 0, colTemp = 0; +#ifdef UNIX #ifdef USE_TERMCAP char termBuffer[TERMBUFSIZ], *termInfo; #endif @@ -130,13 +140,13 @@ static void getScreenSize(void) /* Rot b #endif /* TIOCGWINSZ */ /* Make sure that we're outputting to a terminal */ - if (!isatty(fileno(stderr))) - { + if (!isatty(fileno(stderr))) { screen_lines = DEFAULT_LINES; screen_columns = DEFAULT_COLUMNS; return; } screen_lines = screen_columns = 0; +#endif /* UNIX */ /* LINES & COLUMNS environment variables override everything else */ envLines = getenv("LINES"); @@ -147,10 +157,12 @@ static void getScreenSize(void) /* Rot b if (envColumns != NULL && (colTemp = atol(envColumns)) > 0 ) screen_columns = (int)colTemp; +#ifdef UNIX #ifdef TIOCGWINSZ /* See what ioctl() has to say (overrides terminfo & termcap) */ if ((!screen_lines || !screen_columns) && ioctl(fileno(stderr),TIOCGWINSZ,&windowInfo) != -1) - { if (!screen_lines && windowInfo.ws_row > 0) + { + if (!screen_lines && windowInfo.ws_row > 0) screen_lines = (int)windowInfo.ws_row; if (!screen_columns && windowInfo.ws_col > 0 ) @@ -160,7 +172,8 @@ static void getScreenSize(void) /* Rot b #ifdef TIOCGSIZE /* See what ioctl() has to say (overrides terminfo & termcap) */ if ((!screen_lines || !screen_columns) && ioctl(fileno(stderr),TIOCGSIZE,&windowInfo) != -1) - { if (!screen_lines && windowInfo.ts_lines > 0) + { + if (!screen_lines && windowInfo.ts_lines > 0) screen_lines = (int)windowInfo.ts_lines; if (!screen_columns && windowInfo.ts_cols > 0) @@ -170,7 +183,8 @@ static void getScreenSize(void) /* Rot b #ifdef WIOCGETD /* See what ioctl() has to say (overrides terminfo & termcap) */ if ((!screen_lines || !screen_columns) && ioctl(fileno(stderr),WIOCGETD,&windowInfo) != -1) - { if (!screen_lines && windowInfo.uw_height > 0) + { + if (!screen_lines && windowInfo.uw_height > 0) screen_lines = (int)(windowInfo.uw_height / windowInfo.uw_vs); if (!screen_columns && windowInfo.uw_width > 0) @@ -183,7 +197,8 @@ static void getScreenSize(void) /* Rot b #ifdef USE_TERMCAP /* See what terminfo/termcap has to say */ if (!screen_lines || !screen_columns) - { if ((termInfo = getenv("TERM")) == (char *)NULL) + { + if ((termInfo = getenv("TERM")) == (char *)NULL) termInfo = UNKNOWN_TERM; if ((tgetent(termBuffer, termInfo) <= 0)) @@ -200,27 +215,24 @@ static void getScreenSize(void) /* Rot b screen_lines = DEFAULT_LINES; if (screen_columns == 0) screen_columns = DEFAULT_COLUMNS; +#endif /* UNIX */ } -#else -#define getScreenSize() -#endif /* UNIX */ -/* Certain systems need to go into a "break" mode */ -#ifdef UNIX -#define NEEDBREAK -#endif -#ifdef AMIGA -#define NEEDBREAK -#endif #ifdef ATARI -#define reverse_attr() printf("\033p") -#define norm_attr() printf("\033q") +#define reverse_attr() printf("\033p") +#define norm_attr() printf("\033q") #else #define reverse_attr() #define norm_attr() #endif + +#ifdef VMS +char pager[80] = "Type/Page"; /* default pager for VMS */ +#else /* not VMS */ char pager[80] = ""; +#endif /* not VMS */ + int more_file(char *fileName) /* Blort a file to the screen with page breaks, intelligent handling of line @@ -235,32 +247,40 @@ int more_file(char *fileName) int lineno; char *p; - if ((inFile = fopen(fileName,"rb")) == NULL) + if ((inFile = fopen(fileName,FOPRBIN)) == NULL) /* Can't see how this could fail since we just created the file */ - return(-1); + return -1; fread(buf, 1, 16, inFile); - if (compressSignature(buf) >= 0) - { fprintf(pgpout, PSTR("\n\007File '%s' is not a text file; cannot display.\n"), + if (compressSignature( (byte *) buf) >= 0) + { + fprintf(pgpout, LANG("\n\007File '%s' is not a text file; cannot display.\n"), fileName); - return(-1); + return -1; } /* PAGER set in config.txt overrides environment variable, set PAGER in config.txt to 'pgp' to use builtin pager */ - if (pager[0] == '\0') - { + if (pager[0] == '\0') { if ((p = getenv("PAGER")) != NULL) strncpy(pager, p, sizeof(pager) - 1); } - /* Use built-in pager if PAGER is not set or if this is for your eyes only */ + if (strcmp(pager, "cat") == 0) { + fclose(inFile); + writePhantomOutput(fileName); + return 0; + } + + /* Use built-in pager if PAGER is not set or if this is for your eyes only, + this currently doesn't work, the _CONSOLE filename isn't used as the real + filename anymore */ if ((strcmp(fileName,CONSOLE_FILENAME) != 0) && (strlen(pager) != 0) && strcmp("pgp", pager)) { fclose(inFile); #ifdef UNIX if (strchr(fileName, '\'') != NULL) - return(-1); + return -1; sprintf(cmd, "%s '%s'", pager, fileName); #else sprintf(cmd, "%s %s", pager, fileName); @@ -270,9 +290,18 @@ int more_file(char *fileName) *p = '\\'; #endif #endif - return(system(cmd)); + fflush(pgpout); + return system(cmd); } +#ifdef UNIX + if (!isatty(fileno(stdout))) { + fclose(inFile); + writePhantomOutput(fileName); + return 0; + } +#endif /* UNIX */ + getScreenSize(); /* Get file length */ @@ -281,71 +310,69 @@ int more_file(char *fileName) rewind(inFile); lineno = 1; -#ifdef NEEDBREAK ttycbreak(); -#endif + putchar('\n'); - while (TRUE) - { ch = getc(inFile); - if (ch == LF) - { lines++; + for (;;) { + ch = getc(inFile); + if (ch == LF) { + lines++; + putchar('\n'); + chars = 0; + ++lineno; + } else if (ch == CR) { + lines++; putchar('\n'); chars = 0; ++lineno; + + /* Skip following LF if there is one */ + if ((ch = getc(inFile)) != LF && ch != EOF) + ungetc(ch,inFile); + } else if (((unsigned char) ch >= ' ' && ch != EOF) || ch == TAB) + { + /* Legal char or tab, print it */ + putchar(ch); + chars += (ch == TAB) ? 8 : 1; } - else - if (ch == CR) - { lines++; - putchar('\n'); - chars = 0; - ++lineno; - - /* Skip following LF if there is one */ - if ((ch = getc(inFile)) != LF && ch != EOF) - ungetc(ch,inFile); - } - else - if (((unsigned char) ch >= ' ' && ch != EOF) || ch == TAB) - { /* Legal char or tab, print it */ - putchar(ch); - chars += (ch == TAB) ? 8 : 1; - } /* If we've reach the max.no of columns we can handle, skip the rest of the line */ - if (chars == screen_columns - 1) - { chars = 0; - while ((ch = getc(inFile)) != CR && ch != LF && ch != EOF ); + if (chars == screen_columns - 1) { + chars = 0; + while ((ch = getc(inFile)) != CR && ch != LF && ch != EOF ) + ; if (ch != EOF) ungetc(ch,inFile); } /* If we've reached the max.no of rows we can handle, wait for the user to hit a key */ - while (ch == EOF || lines == screen_lines - 1) - { /* Print prompt at end of screen */ + while (ch == EOF || lines == screen_lines - 1) { + /* Print prompt at end of screen */ reverse_attr(); if (ch == EOF) - printf(PSTR("\nDone...hit any key\r")); + printf(LANG("\nDone...hit any key\r")); else - printf(PSTR("More -- %d%% -- Hit space for next screen, Enter for new line, 'Q' to quit --\r"), + printf(LANG("More -- %d%% -- Hit space for next screen, Enter for new line, 'Q' to quit --\r"), ( 100 * ftell( inFile ) ) / fileLen ); norm_attr(); fflush(stdout); c = getch(); - c = toupper(c); + c = to_upper(c); /* Blank out prompt */ for (i=0; i<79; i++) putchar(' '); putchar('\r'); fflush(stdout); - if (c == 'B' && lineno > screen_lines) /* go Back a page */ - { int seek_line = lineno - 2*screen_lines + 3; + if (c == 'B' && lineno > screen_lines) { + /* go Back a page */ + int seek_line = lineno - 2*screen_lines + 3; lineno = 1; rewind(inFile); - if (seek_line > 1) - { printf("...skipping\n"); + if (seek_line > 1) { + printf("...skipping\n"); while ((ch = getc(inFile)) != EOF) if (ch == '\n') if (++lineno == seek_line) @@ -353,9 +380,8 @@ int more_file(char *fileName) } ch = '\0'; lines = 0; - } - else - { if (c == 'Q' || ch == EOF) + } else { + if (c == 'Q' || ch == EOF) goto done; if (c == ' ' || c == '\n' || c == '\r' || c == 'J') lines -= (c == ' ') ? screen_lines - 2 : 1; /* Do n more lines */ @@ -363,10 +389,83 @@ int more_file(char *fileName) } } done: -#ifdef NEEDBREAK ttynorm(); -#endif + fclose(inFile); - return(0); + return 0; } /* more_file */ + +/* + * open_more() and close_more() redirect pgpout to the pager. + * + */ + +static char *mfile = NULL; +static boolean piping = FALSE; +static FILE *savepgpout; + + +int +open_more(void) +{ +#ifdef UNIX + char *p; +#endif + + if (mfile || piping) + close_more(); + + savepgpout = pgpout; +#ifdef UNIX + fflush(pgpout); + if (pager[0] == '\0') { + if ((p = getenv("PAGER")) != NULL) + strncpy(pager, p, sizeof(pager) - 1); + } + /* Use built-in pager if PAGER is not set or set to "pgp" */ + if ((strlen(pager) != 0) && strcmp("pgp", pager)) { + if ((pgpout = popen(pager, "w")) != NULL) { + piping = TRUE; + return 0; + } + perror("popen"); + pgpout = savepgpout; + } +#endif + if ((mfile = tempfile(TMP_TMPDIR|TMP_WIPE)) == NULL) + return -1; + if ((pgpout = fopen(mfile, FOPWTXT)) == NULL) { + pgpout = savepgpout; + rmtemp(mfile); + return -1; + } + /* user will not see anything until close_more() is called */ + fprintf(savepgpout,LANG("Just a moment...")); + fflush(savepgpout); + return 0; +} + +int +close_more(void) +{ + if (!mfile && !piping) + return 0; + +#ifdef UNIX + if (piping) + pclose(pgpout); + else +#endif + fclose(pgpout); + pgpout = savepgpout; + if (mfile) + { + fprintf(pgpout,"\n"); + more_file(mfile); + rmtemp(mfile); + mfile = NULL; + } + piping = FALSE; + return 0; +}