Files
taiji/sys/lib/ghostscript/pdfeof.ps
T
Russ Cox 535bf48a25 all: remove trailing spaces in text files
To avoid acme making spurious edits in future changes,
edited all text files to remove end-of-line spaces and tabs,
remove trailing blank lines, and add missing final newlines,
using this Go program.

package main

import (
	"bytes"
	"fmt"
	"log"
	"os"
	"path/filepath"
	"regexp"
	"unicode/utf8"
)

var blank = []byte("\n\n")

func main() {
	log.SetPrefix("fixspace: ")
	log.SetFlags(0)

	re := regexp.MustCompile(`(?m)[ \t]+$`)
	filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Print(err)
			return nil
		}
		if info.IsDir() {
			return nil
		}
		old, err := os.ReadFile(path)
		if err != nil {
			return err
		}
		if !utf8.Valid(old) || bytes.ContainsAny(old, "\x00\r") {
			return nil
		}

		// Remove trailing spaces at ends of lines.
		new := re.ReplaceAll(old, nil)

		// Remove trailing blank lines.
		for len(new) > 0 && bytes.HasSuffix(new, blank) {
			new = new[:len(new)-1]
		}
		if len(new) == 1 && new[0] == '\n' {
			new = nil
		}

		// Add newline at end of line if needed.
		if len(new) > 0 && new[len(new)-1] != '\n' {
			new = append(new, '\n')
		}

		if !bytes.Equal(old, new) {
			if err := os.WriteFile(path, new, 0666); err != nil {
				log.Print(err)
				return nil
			}
			fmt.Printf("%s\n", path)
		}
		return nil
	})
}
2025-03-09 08:26:49 -04:00

60 lines
2.4 KiB
PostScript
Executable File

% Copyright (C) 2000 Aladdin Enterprises. All rights reserved.
%
% This file is part of Aladdin Ghostscript.
%
% Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author
% or distributor accepts any responsibility for the consequences of using it,
% or for whether it serves any particular purpose or works at all, unless he
% or she says so in writing. Refer to the Aladdin Ghostscript Free Public
% License (the "License") for full details.
%
% Every copy of Aladdin Ghostscript must include a copy of the License,
% normally in a plain ASCII text file named PUBLIC. The License grants you
% the right to copy, modify and redistribute Aladdin Ghostscript, but only
% under certain conditions described in the License. Among other things, the
% License requires that the copyright notice and this notice be preserved on
% all copies.
% $Id: pdfeof.ps,v 1.1 2000/03/09 08:40:40 lpd Exp $
% Patch to allow garbage on the end of PDF files.
.currentglobal true .setglobal
pdfdict begin
% Skip backward over the %%EOF at the end of the PDF file.
% We put this in a separate procedure so we can easily offer the option
% of accepting garbage after the %%EOF, which the PDF specification
% unambiguously forbids but which some applications (including, apparently,
% Acrobat Distiller on the Macintosh) produce, and of accepting the
% xref position on the same line as startxref.
/findxref { % - findxref <xrefpos>
PDFfile dup dup 0 setfileposition bytesavailable
dup /PDFfilelen exch def
% Find the last %%EOF string (within 2048 bytes)
2048 sub PDFoffset .max
2 copy setfileposition
PDFfilelen exch sub string 1 index exch readstring pop {
(\015%%EO) search { % Adobe can handle truncated key string
pop pop % if found, keep searching 'post' string
} {
(\012%%EO) search
{ pop pop } { exit } ifelse % exit if neither string found
} ifelse
} loop
PDFfilelen exch length sub 4 sub PDFoffset .max setfileposition
% Now read the startxref and xref start position.
prevline token not { null } if dup type /integertype eq {
exch pop cvi % xref start position
exch PDFfile exch setfileposition
prevline (startxref) linene { findxreferror } if
pop
} { % else, this file has 'startxref #####' format
(startxref) ne { findxreferror } if
cvi % xref start position
exch PDFfile exch setfileposition
} ifelse
} bind def
end % pdfdict
.setglobal