home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / printers / textps.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  40KB  |  1,104 lines

  1. char *version = "0.999 10-Oct-96";
  2. char *copyright = "Copyright (C) 1991, 1996, Trustees of Columbia University";
  3. /*
  4.   textps - Convert plain text to Postscript.
  5.  
  6.   DESCRIPTION:
  7.     Converts text files to PostScript Courier-11, 66 lines to the page, 80
  8.     characters to the line.  Handles pagination, tabs, line wrap, overstruck
  9.     characters (via BS) and overstruck lines (via CR).  Swallows and ignores
  10.     (rather than printing) ANSI escape sequences.  If the input file is
  11.     already Postscript (i.e. if its first line starts with "%!"), it is simply
  12.     copied to the output without alteration.  No special effects like page
  13.     headings, landscape, 2-up, etc.
  14.  
  15.     Unlike other "enscriptors", textps handles 8-bit character sets.  The
  16.     default file character set is CP437 on PCs, the NeXT character set on the
  17.     NeXT, and ISO 8859-1 Latin Alphabet 1 elsewhere.  CP850, DEC MCS, and
  18.     Apple QuickDraw are also supported.  Override the default character set
  19.     with the -c command-line option.  Shift-In/Shift-Out codes within the text
  20.     are handled, so 8-bit characters can be encoded by ^N<char-128>^O in the
  21.     7-bit environment (e.g. e-mail) and still print correctly.
  22.  
  23.   USAGE:
  24.     textps [ -h ] [ -v ] [ -c charset ] < input > output
  25.  
  26.     The input file character set is translated from the default character set
  27.     or the one given on the command line to ISO Latin Alphabet 1, and the
  28.     result is converted to Level-1 Postscript.  The -h command line option
  29.     prints a help message and exits immediately.  The -v option includes a
  30.     page showing the textps and PostScript version numbers.  The -c option
  31.     specifies the file's character set; charset may be latin1, cp437, cp850,
  32.     decmcs, apple, or next.
  33.  
  34.     UNIX example:   textps < file | lpr
  35.     MS-DOS example: textps < file > prn
  36.  
  37.     Suggestion for use with DOS.  Make a batch file called PSPRINT.BAT:
  38.       @echo off
  39.       textps < %1 > prn
  40.  
  41.     This assumes PRN is a Postscript printer.  Works with both Postscript
  42.     and non-Postscript files.  Works with Novell CAPTURE.  In VMS, make a
  43.     DCL procedure similar to this batch file.  In UNIX, use this program
  44.     as a print filter (in /etc/printcap, or alias lpr='textps | lpr').
  45.  
  46.   ERRORS:
  47.     Returns status code of 0 on success, 1 on failure.
  48.     Only fails if it is invoked with invalid command line arguments,
  49.     in which case an error and usage message is printed on stderr.
  50.  
  51.   BUGS:
  52.     Sometimes a spurious blank page is produced.
  53.  
  54.     Not all the characters print on early model laserwriters or other very
  55.     old PostScript printers: broken bar, copyright, trade mark, not sign,
  56.     fractions, Y/y-acute and Icelandic Thorn/thorn and Eth/eth, etc.  This
  57.     is because these characters were not present in early PostScript releases.
  58.  
  59.     8-bit characters are translated into an internal character set, which is
  60.     ISO Latin Alphabet 1 with a few extensions, so any file characters that
  61.     don't don't appear in this character set, such as PC line- and box-
  62.     drawing characters, are approximated with characters like '+', '-', and
  63.     '|'.  Alphabetic or punctuation characters that have no equivalents in
  64.     Latin-1 are shown as '?'.
  65.  
  66.   TO BUILD:
  67.     Just compile it.  If compiled under DOS with Microsoft C (and probably
  68.     also under Xenix?), the default character set is CP437, on the NeXT
  69.     it's the NeXT character set, otherwise it's Latin-1.
  70.  
  71.     For OS/2, use the Makefile textps.os2 ("make -f textps.os2 <object>").
  72.     See textps.os2 for a list of objects.
  73.  
  74.     For Windows NT and Windows 95, the default character set is CP437.
  75.  
  76.     To build with a particular default character set, include -DCHARSET=x
  77.     on the cc command line, where x = 0 for Latin1, 1 for CP437, 2 for
  78.     CP850, 3 for NeXT, 4 for DEC MCS, 5 for Apple QuickDraw.
  79.  
  80.     To disable ANSI-escape-sequence elimination, add -DNOESCSEQ.
  81.  
  82.   UPDATES:
  83.     0.95 5 Aug 91 
  84.       Add L procedure to output n blank lines, and don't bother to output
  85.       blank lines after last text on page.
  86.     0.96 6 Aug 91
  87.       Make sure a file that starts with Ctrl-L still outputs the PostScript
  88.       prolog.
  89.     0.97 8 Aug 91
  90.       Totally rewrite so we don't have to use backspacefont, which doesn't
  91.       work if used too much.  This also allows backspace overstriking to work
  92.       across line wrap boundaries.
  93.     0.98 1 Oct 91
  94.       Fix a few translation table entries.
  95.     0.99 7 Oct 91
  96.       Fix the top and bottom margins so 66th line doesn't sometimes have
  97.       descenders cut off.
  98.     0.995 18 Apr 93
  99.       Fix output of already-PS files to not contain an unnecessary
  100.       bufferful of blanks.  Add #ifdef for default OS/2 character set.
  101.       Remove compiler complaint about buf in printf.
  102.       Reported by Darrel Hankerson at Auburn University.
  103.     0.996 16 Jan 94
  104.       From Darrel Hankerson at Auburn University.  Improved OS/2 and MS-DOS
  105.       support: get the current PC code page from the operating system rather
  106.       than using a hardwired default.  New makefile for OS/2 and DOS.
  107.       Print usage() message if stdin isatty().
  108.     0.997 23 Feb 96
  109.       Added support for Windows NT and Windows 95.
  110.     0.998 21 May 96
  111.       Added page-length support.
  112.     0.999 10 Oct 96
  113.       From Dale R. Worley <worley@ariadne.com>.  Clear hpos and maxhpos when
  114.       printing a blank line.  Handle spaces by incrementing hpos, rather than
  115.       inserting them into the line buffer.
  116.  
  117.   TO DO:
  118.     Add support for 7-bit national ISO 646 character sets so we can
  119.     print e-mail from Sweden, France, etc.
  120.  
  121.     Add support for other PC code pages that could be translated into
  122.     Latin-1, such as CP860, CP861, CP863, etc.
  123.  
  124.     Incorporate CP437 font from pc2ps program? (Box drawing chars, etc)
  125.  
  126.   Author: Frank da Cruz, Columbia University (fdc@columbia.edu), July 1991.
  127.   Acks:   For help with reencoding bugs: Bur Davis, Adobe.
  128.           For OS/2 and DOS improvements, Darrel Hankerson, Auburn University.
  129.  
  130.   Copyright (C) 1985, 1996, Trustees of Columbia University in the City of New
  131.   York.  Permission is granted to any individual or institution to use this
  132.   software as long as it is not sold for profit.  This copyright notice must be
  133.   retained.  This software may not be included in commercial products without
  134.   written permission of the Office of Kermit Development and Distribution,
  135.   Academic Information Systems, Columbia University.
  136.  
  137. /* Defines and Includes... */
  138.  
  139. /* For portability, we can't use logical operators in the preprocessor. */
  140. /* Here we define OS2NTDOS if either OS2, MSDOS, or NT is defined, for items */
  141. /* common to OS/2, MS-DOS, and Windows NT/95 ... */
  142.  
  143. #ifdef __EMX__
  144. #ifndef OS2
  145. #define OS2
  146. #endif /* OS2 */
  147. #endif /* __EMX__ */
  148.  
  149. #ifdef OS2
  150. #ifndef OS2NTDOS
  151. #define OS2NTDOS
  152. #endif /* OS2NTDOS */
  153. #endif /* OS2 */
  154.  
  155. #ifdef NT
  156. #include <windows.h>
  157. #ifndef OS2NTDOS
  158. #define OS2NTDOS
  159. #endif
  160. #endif
  161.  
  162. #ifdef MSDOS
  163. #ifndef OS2NTDOS
  164. #define OS2NTDOS
  165. #endif /* OS2NTDOS */
  166. #endif /* MSDOS */
  167.  
  168. #ifndef NOESCSEQ    /* Swallow ANSI escape and control sequences */
  169. #define ESCSEQ        /* unless -DNOESCSEQ given on cc command line. */
  170. #endif /* NOESCSEQ */
  171.  
  172. #define WIDTH 80    /* Portrait printer line width, characters */
  173. #define LENGTH 66    /* Portrait printer page length, lines */
  174. #define MAXLENGTH 256
  175.  
  176. /* Character set translations */
  177.  
  178. #define UNK '?'      /* What to translate an untranslatable character into */
  179. #define SP ' '                /* Space character */
  180. #define DEL 0177            /* DEL character */
  181.  
  182. /* Character set symbols */
  183.  
  184. #define LATIN1 0            /* ISO Latin Alphabet 1 */
  185. #define CP437  1            /* IBM code page 437 */
  186. #define CP850  2            /* IBM code page 850 */
  187. #define NEXT   3            /* NeXT character set */
  188. #define DECMCS 4            /* DEC multinational character set */
  189. #define APPLE  5            /* Apple QuickDraw character set */
  190.  
  191. /* Default character set depends on where we're being compiled. */
  192.  
  193. #ifndef CHARSET                /* If default not already defined */
  194. #ifdef NT
  195. #define CHARSET CP437
  196. #else /* NT */
  197. #ifdef OS2                /* See also the Dos call in main() */
  198. #define CHARSET CP850            /* A little more modern for OS/2 */
  199. #else
  200. #ifdef MSDOS                /* Symbol predefined by Microsoft C */
  201. #define CHARSET CP437            /* Default character set for PCs */
  202. #else
  203. #ifdef NeXT                /* Predefined by NeXT compiler */
  204. #define CHARSET NEXT
  205. #else
  206. #define CHARSET LATIN1            /* Default character set for others */
  207. #endif /* NeXT */
  208. #endif /* OS2 */
  209. #endif /* MSDOS */
  210. #endif /* NT */
  211. #endif /* CHARSET */
  212.  
  213. #include <stdio.h>            /* For EOF definition */
  214. #ifdef OS2NTDOS
  215. #include <io.h>                /* For isatty() */
  216. #include <string.h>
  217. #endif /* OS2NTDOS */
  218.  
  219. /*
  220.   Postscript Prolog, to be inserted at the beginning of the output file.
  221.   The %% Comments are to make the file conformant with Adobe's "Postscript 
  222.   File Structuring Conventions", which allow page-oriented operations in
  223.   Postscript previewers, page pickers, etc.
  224. */
  225. char *prolog[] = {            /* Standard prolog */
  226.     "%!PS-Adobe-1.0",            /* Works with Postscript 1.0 */
  227.     "%%Title: oofa",
  228.     "%%DocumentFonts: Courier CourierLatin1", 
  229.     "%%Creator: textps",
  230.     "%%Pages: (atend)",
  231.     "%%EndComments",
  232. /*
  233.   Postscript font reencoding.  The standard encoding does not have the
  234.   characters needed for Latin-1.
  235.  
  236.   Unfortunately, the font reencoding methods listed in the Postscript
  237.   Cookbook simply do not work with the Apple Laserwriter (even though they
  238.   did work with other Postscript devices).  The method described in the
  239.   Adobe PostScript Language Reference Manual (2nd Ed) to change from the
  240.   StandardEncoding vector to the ISOLatin1Encoding vector works only with
  241.   the LaserWriter-II, but not older LaserWriters.
  242.  
  243.   This method, suggested by Bur Davis at Adobe, works around the fact that
  244.   Courier was a "stroke font" in pre-version-47.0 Postscript, in which many of
  245.   the accented characters are composed from other characters (e.g. i-grave =
  246.   dotless i + grave).  It is probably not the most efficient possible solution
  247.   (an iterative method might be better), but it works.
  248. */
  249.     "/CourierLatin1 /Courier findfont dup dup maxlength dict begin",
  250.     "{",
  251.     "    1 index /FID ne { def } { pop pop } ifelse", 
  252.     "} forall",
  253.     "/Encoding exch 1 index get 256 array copy def", 
  254. /*
  255.   The following characters are added at the C1 positions 128-153, for printing
  256.   non-Latin1 character sets such as IBM code pages, DEC MCS, NeXT, etc.  Note
  257.   that we cannot use characters from the Symbol font.  Characters from
  258.   different fonts cannot be mixed.  Anyway, the Symbol font is not fixed-width.
  259. */
  260.     "Encoding 128 /quotesingle put",
  261.     "Encoding 129 /quotedblleft put",
  262.     "Encoding 131 /fi put",
  263.     "Encoding 132 /endash put",
  264.     "Encoding 133 /dagger put",
  265.     "Encoding 134 /periodcentered put",
  266.     "Encoding 135 /bullet put",
  267.     "Encoding 136 /quotesinglbase put",
  268.     "Encoding 137 /quotedblbase put",
  269.     "Encoding 138 /quotedblright put",
  270.     "Encoding 139 /ellipsis put",
  271.     "Encoding 140 /perthousand put",
  272.     "Encoding 141 /dotaccent put",
  273.     "Encoding 142 /hungarumlaut put",
  274.     "Encoding 143 /ogonek put",
  275.     "Encoding 144 /caron put",
  276.     "Encoding 145 /fl put",
  277.     "Encoding 146 /emdash put",
  278.     "Encoding 147 /Lslash put",
  279.     "Encoding 148 /OE put",
  280.     "Encoding 149 /lslash put",
  281.     "Encoding 150 /oe put",
  282.     "Encoding 151 /florin put",
  283.     "Encoding 152 /fraction put",
  284.     "Encoding 153 /daggerdbl put",
  285. /*
  286.   The following six characters are required for pre-47.0 PostScript versions,
  287.   which compose accented Courier characters by putting together the base
  288.   character and the accent.
  289. */
  290.     "Encoding 154 /dotlessi put",
  291.     "Encoding 155 /grave put",
  292.     "Encoding 156 /circumflex put",
  293.     "Encoding 157 /tilde put",
  294.     "Encoding 158 /breve put",
  295.     "Encoding 159 /ring put",
  296. /*
  297.   The remainder follow the normal ISO 8859-1 encoding.
  298. */
  299.     "Encoding 160 /space put",
  300.     "Encoding 161 /exclamdown put",
  301.     "Encoding 162 /cent put", 
  302.     "Encoding 163 /sterling put", 
  303.     "Encoding 164 /currency put", 
  304.     "Encoding 165 /yen put", 
  305.     "Encoding 166 /brokenbar put", 
  306.     "Encoding 167 /section put", 
  307.     "Encoding 168 /dieresis put", 
  308.     "Encoding 169 /copyright put", 
  309.     "Encoding 170 /ordfeminine put", 
  310.     "Encoding 171 /guillemotleft put", 
  311.     "Encoding 172 /logicalnot put", 
  312.     "Encoding 173 /hyphen put", 
  313.     "Encoding 174 /registered put", 
  314.     "Encoding 175 /macron put", 
  315.     "Encoding 176 /degree put", 
  316.     "Encoding 177 /plusminus put", 
  317.     "Encoding 178 /twosuperior put", 
  318.     "Encoding 179 /threesuperior put", 
  319.     "Encoding 180 /acute put", 
  320.     "Encoding 181 /mu put", 
  321.     "Encoding 182 /paragraph put", 
  322.     "Encoding 183 /bullet put", 
  323.     "Encoding 184 /cedilla put", 
  324.     "Encoding 185 /onesuperior put", 
  325.     "Encoding 186 /ordmasculine put", 
  326.     "Encoding 187 /guillemotright put", 
  327.     "Encoding 188 /onequarter put", 
  328.     "Encoding 189 /onehalf put", 
  329.     "Encoding 190 /threequarters put", 
  330.     "Encoding 191 /questiondown put", 
  331.     "Encoding 192 /Agrave put", 
  332.     "Encoding 193 /Aacute put", 
  333.     "Encoding 194 /Acircumflex put", 
  334.     "Encoding 195 /Atilde put", 
  335.     "Encoding 196 /Adieresis put", 
  336.     "Encoding 197 /Aring put", 
  337.     "Encoding 198 /AE put", 
  338.     "Encoding 199 /Ccedilla put", 
  339.     "Encoding 200 /Egrave put", 
  340.     "Encoding 201 /Eacute put", 
  341.     "Encoding 202 /Ecircumflex put", 
  342.     "Encoding 203 /Edieresis put", 
  343.     "Encoding 204 /Igrave put", 
  344.     "Encoding 205 /Iacute put", 
  345.     "Encoding 206 /Icircumflex put", 
  346.     "Encoding 207 /Idieresis put", 
  347.     "Encoding 208 /Eth put", 
  348.     "Encoding 209 /Ntilde put", 
  349.     "Encoding 210 /Ograve put", 
  350.     "Encoding 211 /Oacute put", 
  351.     "Encoding 212 /Ocircumflex put", 
  352.     "Encoding 213 /Otilde put", 
  353.     "Encoding 214 /Odieresis put", 
  354.     "Encoding 215 /multiply put", 
  355.     "Encoding 216 /Oslash put", 
  356.     "Encoding 217 /Ugrave put", 
  357.     "Encoding 218 /Uacute put", 
  358.     "Encoding 219 /Ucircumflex put", 
  359.     "Encoding 220 /Udieresis put", 
  360.     "Encoding 221 /Yacute put", 
  361.     "Encoding 222 /Thorn put", 
  362.     "Encoding 223 /germandbls put", 
  363.     "Encoding 224 /agrave put", 
  364.     "Encoding 225 /aacute put", 
  365.     "Encoding 226 /acircumflex put", 
  366.     "Encoding 227 /atilde put", 
  367.     "Encoding 228 /adieresis put", 
  368.     "Encoding 229 /aring put", 
  369.     "Encoding 230 /ae put", 
  370.     "Encoding 231 /ccedilla put", 
  371.     "Encoding 232 /egrave put", 
  372.     "Encoding 233 /eacute put", 
  373.     "Encoding 234 /ecircumflex put", 
  374.     "Encoding 235 /edieresis put", 
  375.     "Encoding 236 /igrave put", 
  376.     "Encoding 237 /iacute put", 
  377.     "Encoding 238 /icircumflex put", 
  378.     "Encoding 239 /idieresis put", 
  379.     "Encoding 240 /eth put", 
  380.     "Encoding 241 /ntilde put", 
  381.     "Encoding 242 /ograve put", 
  382.     "Encoding 243 /oacute put", 
  383.     "Encoding 244 /ocircumflex put", 
  384.     "Encoding 245 /otilde put", 
  385.     "Encoding 246 /odieresis put", 
  386.     "Encoding 247 /divide put", 
  387.     "Encoding 248 /oslash put", 
  388.     "Encoding 249 /ugrave put", 
  389.     "Encoding 250 /uacute put", 
  390.     "Encoding 251 /ucircumflex put", 
  391.     "Encoding 252 /udieresis put", 
  392.     "Encoding 253 /yacute put", 
  393.     "Encoding 254 /thorn put", 
  394.     "Encoding 255 /ydieresis put", 
  395.     "currentdict end definefont", 
  396. /*
  397.   Set the font and define functions for adding lines and printing pages.
  398. */
  399.     "/CourierLatin1 findfont 11 scalefont setfont",
  400.     "/StartPage{/sv save def 48 765 moveto}def",
  401.     "/ld -11.4 def",            /* Line spacing */
  402.     "/yline 765 def",            /* Position of top line */
  403.     "/U{show",                /* Show line, don't advance */
  404.     "  48 yline moveto}def",
  405.     "/S{show",                /* Show line, move to next line */
  406.     "  /yline yline ld add def",
  407.     "  48 yline moveto}def",
  408.     "/L{ld mul yline add /yline exch def", /* Move down n lines  */
  409.     "  48 yline moveto}def",
  410.     "/EndPage{showpage sv restore}def",
  411.     "%%EndProlog",            /* End of prolog. */
  412.     "%%Page: 1 1",            /* Number the first page. */
  413.     "StartPage",            /* And start it. */
  414.     ""                    /* Empty string = end of array. */
  415. };
  416.  
  417. /*
  418.   Translation tables from local character sets into CourierLatin1.
  419. */
  420.  
  421. /*
  422.   IBM Code Page 437.  Line- and box-drawing characters are simulated with
  423.   dashes, bars, and plus signs.  Black and gray blobs (fill characters)
  424.   are replaced by X's.  Peseta is shown as P.  Greek letters that don't
  425.   exist in CourierLatin1 are shown as ?.  Untranslatable math symbols are
  426.   shown as ?.
  427. */
  428. unsigned char
  429. y43l1[] = {
  430.   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
  431.  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
  432.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  433.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  434.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  435.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  436.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  437. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  438. 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197,
  439. 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 162, 163, 165, 'P', 151,
  440. 225, 237, 243, 250, 241, 209, 170, 186, 191, '+', 172, 189, 188, 161, 171, 187,
  441. 'X', 'X', 'X', '|', '+', '+', '+', '+', '+', '+', '|', '+', '+', '+', '+', '+',
  442. '+', '+', '+', '+', '-', '+', '+', '+', '+', '+', '+', '+', '+', '-', '+', '+',
  443. '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', '+', 'X', 'X', 'X', 'X', 'X',
  444. UNK, 223, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK, UNK,
  445. UNK, 177, UNK, UNK, UNK, UNK, UNK, UNK, 176, 134, 135, UNK, 'n', 178, 'X', 160
  446. };
  447.  
  448. /*
  449.   IBM Code Page 850.  Line- and box-drawing characters are simulated with 
  450.   dashes, bars, and plus signs.  Black blobs are replaced by X's.
  451. */
  452. unsigned char
  453. y85l1[] = {
  454.   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
  455.  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
  456.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  457.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  458.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  459.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  460.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  461. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  462. 199, 252, 233, 226, 228, 224, 229, 231, 234, 235, 232, 239, 238, 236, 196, 197,
  463. 201, 230, 198, 244, 246, 242, 251, 249, 255, 214, 220, 248, 163, 216, 215, 151,
  464. 225, 237, 243, 250, 241, 209, 170, 186, 191, 174, 172, 189, 188, 161, 171, 187,
  465. 'X', 'X', 'X', '|', '+', 193, 194, 192, 169, '+', '|', '+', '+', 162, 165, '+',
  466. '+', '+', '+', '+', '-', '+', 227, 195, '+', '+', '+', '+', '+', '-', '+', 164,
  467. 240, 208, 202, 203, 200, 154, 205, 206, 207, '+', '+', 'X', 'X', 166, 204, 'X',
  468. 211, 223, 212, 210, 245, 213, 181, 254, 222, 218, 219, 217, 253, 221, 175, 180,
  469. 173, 177, '=', 190, 182, 167, 247, 184, 176, 168, 134, 185, 179, 178, 'X', 160
  470. };
  471.  
  472. /*
  473.   NeXT character set.  Here we have a full translation.
  474. */
  475. unsigned char
  476. ynel1[] = {
  477.   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
  478.  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
  479.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  480.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  481.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  482.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  483.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  484. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  485. 160, 192, 193, 194, 195, 196, 197, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  486. 208, 209, 210, 211, 212, 213, 214, 217, 218, 219, 220, 221, 222, 181, 215, 247,
  487. 169, 161, 162, 163, 152, 165, 151, 167, 164, 128, 129, 171, '<', '>', 131, 145,
  488. 174, 132, 133, 153, 134, 166, 182, 135, 136, 137, 138, 187, 139, 140, 172, 191,
  489. 185,  96, 180,  94, 126, 175, 158, 141, 168, 178, 176, 184, 179, 142, 143, 144,
  490. 146, 177, 188, 189, 190, 224, 225, 226, 227, 228, 229, 231, 232, 233, 234, 235,
  491. 236, 198, 237, 170, 238, 239, 240, 241, 147, 216, 148, 186, 242, 243, 244, 245,
  492. 246, 230, 249, 250, 251, 154, 252, 253, 149, 248, 150, 223, 254, 255, ' ', ' '
  493. };
  494.  
  495. /*
  496.   DEC Multinational Character Set (MCS).
  497. */
  498. unsigned char
  499. ydml1[] = {
  500.   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
  501.  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
  502.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  503.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  504.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  505.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  506.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  507. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  508. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  509. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  510. 160, 161, 162, 163, ' ', 165, ' ', 167, 164, 169, 170, 171, ' ', ' ', ' ', ' ',
  511. 176, 177, 178, 179, ' ', 181, 182, 134, ' ', 185, 186, 187, 188, 189, ' ', 191,
  512. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  513. ' ', 209, 210, 211, 212, 213, 214, 148, 216, 217, 218, 219, 220, 221, ' ', 223,
  514. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  515. ' ', 241, 242, 243, 244, 245, 246, 150, 248, 249, 250, 251, 252, 255, ' ', ' ',
  516. };
  517.  
  518. /*
  519.   Apple QuickDraw character set.
  520. */
  521. unsigned char
  522. yaql1[] = {
  523.   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,  15,
  524.  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,  29,  30,  31,
  525.  32,  33,  34,  35,  36,  37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,
  526.  48,  49,  50,  51,  52,  53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,
  527.  64,  65,  66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,  79,
  528.  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,  92,  93,  94,  95,
  529.  96,  97,  98,  99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  530. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  531. 196, 197, 199, 201, 209, 214, 220, 225, 224, 226, 228, 227, 229, 231, 233, 232,
  532. 234, 235, 237, 236, 238, 239, 241, 243, 242, 244, 246, 245, 250, 249, 251, 252,
  533. 133, 176, 162, 163, 167, 135, 182, 223, 174, 169, UNK, 180, 168, UNK, 198, 216,
  534. UNK, 177, UNK, UNK, 165, 181, UNK, UNK, UNK, UNK, UNK, 170, 186, UNK, 230, 248,
  535. 191, 161, 172, UNK, 151, UNK, UNK, 171, 187, 139, ' ', 193, 195, 213, 148, 150,
  536. 132, 146, 129, 138,  47,  47, 247, UNK, 255, UNK, 152, 164, '<', '>', 131, 145,
  537. 153, 134, 136, 137, 140, 194, 202, 192, 203, 200, 205, 206, 207, 204, 211, 212,
  538. UNK, 210, 218, 219, 217, 154, 156, 157, 175, 158, 141, 176, 184, 142, 143, 144
  539. };
  540.  
  541. /*
  542.   Data structures and functions for parsing and displaying character set name.
  543. */
  544. struct keytab {                /* Keyword table template */
  545.     char *kwd;                /* Pointer to keyword string */
  546.     int kwval;                /* Associated value */
  547. };
  548.  
  549. struct keytab csets[] = {        /* Character sets, alphabetical */
  550.     "apple",   APPLE,            /* Apple QuickDraw */
  551.     "cp437",   CP437,            /* IBM Code Page 437 */
  552.     "cp850",   CP850,            /* IBM Code Page 850 */
  553.     "decmcs",  DECMCS,            /* DEC Multinational Character Set */
  554.     "latin1",  LATIN1,            /* ISO 8859-1 Latin Alphabet 1 */
  555.     "next",    NEXT            /* NeXT character set */
  556. };
  557. int ncsets = (sizeof(csets) / sizeof(struct keytab));
  558.  
  559. int
  560. lower(s) char *s; {            /* Lowercase the string */
  561.     int n = 0;                /* return its length */
  562.     while (*s) {
  563.     if (*s >= 'A' && *s <= 'Z')
  564.       *s += ('A' - 'a');
  565.         s++, n++;
  566.     }
  567.     return(n);
  568. }
  569.  
  570. /* Look up keyword, return value */
  571.  
  572. int
  573. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  574.  
  575.     int i, v, cmdlen;
  576.  
  577. /* Lowercase & get length of target, if it's null return code -3. */
  578.  
  579.     if ((((cmdlen = lower(cmd))) == 0) || (n < 1)) return(-3);
  580.  
  581. /* Not null, look it up */
  582.  
  583.     for (i = 0; i < n-1; i++) {
  584.         if (!strcmp(table[i].kwd,cmd) ||
  585.            ((v = !strncmp(table[i].kwd,cmd,cmdlen)) &&
  586.              strncmp(table[i+1].kwd,cmd,cmdlen))) {
  587.                 *x = i;
  588.                 return(table[i].kwval);
  589.              }
  590.         if (v) return(-2);
  591.     }   
  592.  
  593. /* Last (or only) element */
  594.  
  595.     if (!strncmp(table[n-1].kwd,cmd,cmdlen)) {
  596.         *x = n-1;
  597.         return(table[n-1].kwval);
  598.     } else return(-1);
  599. }
  600.  
  601. /* Look up value, return keyword */
  602.  
  603. char *
  604. getname(x,table,n) int x, n; struct keytab table[]; {
  605.     int i;
  606.     for (i = 0; i < n; i++)
  607.       if (table[i].kwval == x)
  608.     return(table[i].kwd);
  609.     return("");
  610. }
  611.  
  612. /* Global data */
  613.  
  614. int charset = CHARSET,            /* Character set */
  615.   hpos = 0,                /* Character number in line buffer */
  616.   maxhpos = 0,                /* Longest line in buffer */
  617.   page = 0,                /* Page number */
  618.   line = 0,                /* Line number */
  619.   blank = 0,                /* Blank line count */
  620.   pagefull = 0,                /* Flag for page overflow */
  621.   psflag = 0,                /* Flag for file already postscript */
  622.   proflg = 0,                /* Prolog done */
  623.   shift = 0;                /* Shift state */
  624.  
  625. int width = WIDTH;            /* Paper width, characters */
  626. int length = LENGTH;            /* Paper length, characters */
  627.  
  628. /* Data structures */
  629.  
  630. /*
  631.   buf is the line buffer.  columns (indexed by hpos) are the characters
  632.   in the line, rows are overstruck lines.  At display time (see addline),
  633.   buf is treated as a 3-dimensional array, with the extra dimension being
  634.   for wraparound.  The total size of the buffer is 80 chars per line times
  635.   66 lines per page times 10 levels of overstriking = 52,800.  This allows
  636.   files that contain absolutely no linefeeds to still print correctly.
  637. */
  638. #define BUFNUM 10            /* Number of overstrike buffers */
  639. #define BUFWID 5280            /* Max characters per line */
  640.  
  641. unsigned char buf[BUFNUM][BUFWID];    /* Line buffers */
  642. unsigned char outbuf[400];        /* Output buffer */
  643. int linesize[BUFNUM];            /* Size of each line in buffer */
  644. int bufs[MAXLENGTH];            /* # overstrike buffers per line */
  645.  
  646. /* Line and page display routines */
  647.  
  648. /* Forward declarations */
  649.  
  650. void addline();                /* Add line to page */
  651. void addchar();                /* Add character to line */
  652. void newpage();                /* New page */
  653. void usage();                /* Usage message */
  654.  
  655. void
  656. clearbuf() {                /* Clear line buffer */
  657.     int i;
  658. /*
  659.   Note: if a loop is used instead of memset, this program runs
  660.   veeeery slooooooowly.
  661. */
  662.     memset(buf,SP,BUFNUM * BUFWID);    /* Clear buffers and counts */
  663.     for (i = 0; i < BUFNUM; linesize[i++] = -1) ;
  664.     for (i = 0; i < length; bufs[i++] = 0) ;
  665.     hpos = 0;                /* Reset line buffer pointer */
  666.     maxhpos = 0;            /* And maximum line length */
  667. }
  668.  
  669. void
  670. doprolog() {                /* Output the PostScript prolog */
  671.     int i;
  672.     for (i = 0; *prolog[i]; i++) {
  673.     printf("%s\n",prolog[i]);
  674.     proflg++;
  675.     }
  676. }
  677.  
  678. void
  679. addchar(c) unsigned char c; {        /* Add character to line buffer */
  680.     int i, m;
  681.     
  682.     if (c < SP || c == DEL) c = SP;    /* ASCII controls become spaces. */
  683.  
  684.     if (c > 127) {            /* 8-bit values are translated */
  685.     switch (charset) {        /* according to character set. */
  686.       case LATIN1:
  687.         if (c > 127 && c < 161)    /* C1 characters are controls */
  688.           c = SP;            /* in Latin-1. */
  689.         break;
  690.       case CP437:  c = y43l1[c]; break;
  691.       case CP850:  c = y85l1[c]; break;
  692.       case NEXT:   c = ynel1[c]; break;
  693.       case DECMCS: c = ydml1[c]; break;
  694.       case APPLE:  c = yaql1[c]; break;
  695.     }
  696.     }
  697.     for (i = 0; i < BUFNUM; i++) {    /* Find first */
  698.     if (hpos > linesize[i]) {    /* available overstrike buffer */
  699.         buf[i][hpos] = c;        /* Deposit character */
  700.         linesize[i] = hpos;        /* Remember size of this buffer. */
  701.         m = hpos / width;        /* Line-wrap segment number. */
  702.         if (i > bufs[m]) bufs[m] = i; /* Highest overstrike buffer used */
  703.         break;            /*   for this line-wrap segment. */
  704.     }
  705.     }
  706.     if (hpos > maxhpos) maxhpos = hpos;    /* Remember maximum line position. */
  707.     if (++hpos >= BUFWID)        /* Increment line position. */
  708.       addline();            /* If buffer full, dump it. */
  709. }
  710.  
  711. void
  712. addline() {                /* Add a line to the current page */
  713.     int i, j, k, m, n, y, wraps;
  714.     unsigned char *p, *q, c;
  715.  
  716.     if (line == 0 && page == 1) {    /* First line of file? */
  717.     if (!strncmp(buf[0],"%!",2)) {    /* Already Postscript? */
  718.         psflag++;            /* Yes, set this flag & just copy */
  719.         buf[0][hpos] = '\0';    /* Trim trailing blanks */
  720.         printf("%s\n",buf[0]);    /* Send this line to stdout */
  721.         return;
  722.     } else if (!proflg) {        /* Not Postscript, print prolog. */
  723.         doprolog();
  724.     }
  725.     }
  726.     if (linesize[0] < 0) {        /* If line is empty, */
  727.     blank++;            /* just count it. */
  728.     clearbuf();            /* Clear buffer reset other counters */
  729.     return;
  730.     }
  731.     if (blank > 0) {            /* Any previous blank lines? */
  732.     if (blank == 1)            /* One */
  733.       printf("()S\n");
  734.     else                /* Many */
  735.       printf("%d L\n",blank);
  736.     }
  737.     line += blank;            /* Count the blank lines */
  738.     blank = 0;                /* Reset blank line counter */
  739.  
  740.     wraps = maxhpos / width;        /* Number of times line will wrap */
  741.     if (wraps > length) wraps = length;    /* (within reason) */
  742.  
  743.     for (k = 0; k <= wraps; k++) {    /* For each wrapped line */
  744.     m = k * width;            /* Starting position in buffer */
  745.     for (i = 0; i <= bufs[k]; i++) { /* For each overstrike buffer */
  746.         y = linesize[i] + 1;    /* Actual character count */
  747.         if (y <= m)            /* Nothing there, next buffer. */
  748.           continue;
  749.         /* Ending position of this wrap region in buffer. */
  750.         n = (y < m + width) ? y : m + width;
  751.         q = outbuf;
  752.         *q++ = '(';            /* Start text arg */
  753.         for (j = m, p = buf[i]+m; j < n; j++) { /* For each character */
  754.         c = *p++;
  755.         if (c == '(' || c == ')' || c =='\\') /* Quote specials */
  756.           *q++ = '\\';        /*  with backslash. */
  757.         if ((int) c < 128)    /* Insert 7-bit character literally */
  758.           *q++ = c;
  759.         else {            /* Insert 8-bit character */
  760.             *q++ = '\\';    /* as octal backslash escape */
  761.             *q++ = (c >> 6) + '0'; /* (this avoids call to sprintf) */
  762.             *q++ = ((c >> 3) & 07) + '0';
  763.             *q++ = (c & 07) + '0';
  764.         }
  765.         }
  766.         *q = '\0';
  767.         printf("%s%s",outbuf, (i == bufs[k]) ? ")S\n" : ")U\n");
  768.     }
  769.     }
  770.     clearbuf();                /* Clear line buffer */
  771.     line += wraps + 1;            /* Increment line number */
  772.     if (line > (length - 1)) {        /* If page is full */
  773.     newpage();            /* print it and start new one */
  774.     pagefull = 1;
  775.     }
  776. }
  777.  
  778. void
  779. newpage() {                /* Print current page, start new one */
  780.     if (pagefull) {            /* If we just overflowed onto a */
  781.     pagefull = 0;            /* new page, but then got a formfeed */
  782.     return;                /* immediately after... */
  783.     }
  784.     if (!proflg)            /* Do prolog if not done already */
  785.       doprolog();            /*  (in case file starts with ^L) */
  786.     if (hpos)                /* Add any partial line */
  787.       addline();
  788.     line = hpos = 0;            /* Reset line, advance page */
  789.     page++;
  790.     printf("EndPage\n%%%%Page: %d %d\nStartPage\n",page,page);
  791.     blank = 0;
  792. }
  793.  
  794. void            /* Show program & PostScript version numbers */
  795. showver() {
  796.     printf("%%!\n/Courier findfont 11 scalefont setfont\n");
  797.     printf("/EndPage{version (PostScript version )\n");
  798.     printf("48 720 moveto show show showpage sv restore}def\n");
  799.     printf("/sv save def 48 740 moveto\n");
  800.     printf("(textps version %s) show\n",version);
  801.     printf("/sv save def 48 700 moveto\n");
  802.     printf("(textps %s) show\n",copyright);
  803.     printf("EndPage\n");
  804. }
  805.  
  806. /*
  807.    gcharset  -  Set the default character set.
  808.  
  809.    Under OS/2, use the DosQueryCp() or DosGetCp() call;
  810.    under MSDOS, use int 21h; on others just return CHARSET.
  811.  
  812.    Note that in an MSC bound program, DosGetCp() will return the
  813.    current code page and no more than one prepared code page.
  814.  
  815.    INT 21 - DOS 3.3+ - GET GLOBAL CODE PAGE TABLE
  816.               AX = 6601h
  817.    Return: CF set on error
  818.               AX = error code (see AH=59h)
  819.            CF clear if successful
  820.               BX = active code page (see AX=6602h)
  821.           DX = system code page
  822.  
  823.    Values for code page:
  824.      437 US (hardware code page on most PCs)
  825.      850 Multilingual (OS/2 default)
  826.  
  827.    The following are similar to CP437, and are treated like CP437:
  828.      857 Turkish         
  829.      860 Portugal
  830.      861 Iceland
  831.      863 Canada (French)
  832.      865 Norway/Denmark
  833.  
  834.    The following are not supported by textps,
  835.    because Courier does not have the needed characters:
  836.      852 Slavic/Latin-2 (DOS 5+)
  837.      862 Hebrew
  838.      866 Cyrillic
  839.      982 Japanese Shift-JIS
  840.      etc etc.
  841. */
  842. int
  843. gcharset() {
  844.  
  845. /* Note that charset=CHARSET is initialized above */
  846.  
  847. #ifdef OS2NTDOS
  848.     int i;
  849. #ifdef NT
  850.    i = GetConsoleCP() ;
  851. #else /* NT */
  852. #ifdef OS2                /* Then get the code page... */
  853. #define INCL_DOSNLS
  854. #include <os2.h>
  855. /*
  856.   Get the current code page and the first two prepared code pages.
  857.   Only the current code page is used in the following.
  858. */
  859. #ifdef __EMX__
  860.     ULONG CpList[3], CpSize, rc;
  861.     rc = DosQueryCp(sizeof(CpList), CpList, &CpSize);
  862. #else /* MSC */
  863.     USHORT CpList[3], CpSize, rc;
  864.     rc = DosGetCp(sizeof(CpList), CpList, &CpSize);
  865. #endif /* __EMX__ */
  866.     i = (int) CpList[0];
  867. #else /* MSDOS */
  868. #include <dos.h>
  869.     union REGS regs;
  870.  
  871.     regs.x.ax = 0x6601;
  872.     intdos(®s, ®s);
  873.     i = regs.x.bx;
  874. #endif /* OS2 */
  875. #endif /* NT */
  876.     switch (i) {
  877.       case 437: /* CP437 */
  878.       case 860: /* Portugal */
  879.       case 857: /* Turkey */
  880.       case 861: /* Iceland */
  881.       case 863: /* Canadian French */
  882.       case 865: /*  Norway and Denmark */
  883.     charset = CP437;
  884.     break;
  885.       case 850: /* Multilingual (West European) code page */
  886.     charset = CP850;
  887.     break;
  888.     }
  889. #endif /* OS2NTDOS */
  890.  
  891.     return (charset);
  892. }
  893.  
  894.  
  895. /* Main program */
  896.  
  897. void
  898. main(argc, argv) int argc; char *argv[]; {
  899.  
  900.     int i, j,                /* Worker ints */
  901.       escape;                /* Flag when Esc seen. */
  902.     unsigned char c;            /* Input character */
  903.  
  904.     gcharset();                /* Get the default character set */
  905.     while (--argc > 0) {        /* argv/argc "User interface"... */
  906.     argv++;
  907.     if (**argv == '-') {        /* Look for '-' */
  908.         c = *(*argv+1);        /* Get option letter */
  909.         switch (c) {
  910.           case 'l': case 'L':    /* Paper length */
  911.         argv++, argc--;
  912.         length = atol(*argv);
  913.         break;
  914.           case 'w': case 'W':    /* Paper width */
  915.         argv++, argc--;
  916.         width = atol(*argv);
  917.         break;
  918.           case 'h': case 'H': case '?': /* Help */
  919.         usage(-1);
  920.           case 'c': case 'C':    /* Character set */
  921.         argv++, argc--;
  922.         if (argc < 1) usage(4);
  923.         i = lookup(csets,*argv,ncsets,&j);
  924.         if (i < 0) usage(-i);
  925.         charset = i;
  926.         break;
  927.           case 'v': case 'V':    /* Show version numbers */
  928.         showver();
  929.         break;
  930.           default:
  931.         usage(5);
  932.         }
  933.     } else {            /* Options must begin with '-' */
  934.         usage(0);
  935.     }
  936.     }
  937. #ifdef OS2NTDOS
  938. #ifdef NT
  939.     if (_isatty(stdin->_file))
  940. #else
  941.     if (isatty(fileno(stdin)))
  942. #endif /* NT */
  943.       usage(-1);
  944. #endif /* OS2NTDOS */
  945.     hpos = line = psflag = 0;        /* Initialize these... */ 
  946.     escape = blank = 0;
  947.     page = 1;
  948.  
  949.     clearbuf();                /* Clear line buffer. */
  950.  
  951.     while ((i = getchar()) != EOF) {    /* Read a file character */
  952.     c = i;                /* Convert to unsigned char */
  953.     if (psflag) {            /* File already postscript? */
  954.         putchar(c);            /* Just copy the bytes. */
  955.         continue;
  956.     }
  957. #ifdef ESCSEQ
  958.     if (escape) {            /* Swallow ANSI escape sequences */
  959.         switch (escape) {
  960.           case 1:            /* ESC */
  961.         if (c < 040 || c > 057) /* Not intermediate character */
  962.           escape = 0;
  963.         continue;
  964.           case 2:            /* CSI */
  965.         if (c < 040 || c > 077)    /* Not parameter or intermediate */
  966.           escape = 0;
  967.         continue;
  968.           default:            /* Bad escape value, */
  969.         escape = 0;        /* shouldn't happen. */
  970.         break;
  971.         }
  972.     }
  973. #endif /* ESCSEQ */
  974.  
  975.     if (shift && c > 31 && c < 127) 
  976.       c |= 0200;            /* Handle shift state. */
  977.  
  978.     if (pagefull && c != 014)    /* Spurious blank page suppression */
  979.       pagefull = 0;
  980.  
  981.     switch (c) {            /* Handle the input character */
  982.  
  983.       case 010:            /* Backspace */
  984.         hpos--;
  985.         if (hpos < 0) hpos = 0;
  986.         continue;
  987.  
  988.       case 011:             /* Tab */
  989.         hpos = (hpos | 7) + 1;
  990.         continue;
  991.  
  992.       case 012:            /* Linefeed */
  993.         addline();            /* Add the line to the page */
  994.         continue;
  995.  
  996.       case 014:            /* Formfeed */
  997.         newpage();            /* Print current page */
  998.         continue;
  999.  
  1000.       case 015:            /* Carriage return */
  1001.         hpos = 0;            /* Back to left margin */
  1002.         continue;
  1003.  
  1004.       case 016:            /* Shift-Out */
  1005.         shift = 1;            /* Set shift state */
  1006.         continue;
  1007.  
  1008.       case 017:            /* Shift-In */
  1009.         shift = 0;            /* Reset shift state */
  1010.         continue;
  1011.  
  1012. #ifdef ESCSEQ                /* Swallow ANSI escape sequences */
  1013. /*
  1014.   1 = ANSI escape sequence
  1015.   2 = ANSI control sequence
  1016. */
  1017.       case 033:            /* ESC or 7-bit CSI */
  1018.         escape = ((c = getchar()) == 0133) ? 2 : 1;
  1019.         if (c != 033 && c != 0133 && c != 233) /* Not ANSI after all */
  1020.           ungetc(c,stdin);        /* put it back, avoid loops */
  1021.         continue;
  1022.  
  1023.       case 040:            /* Space */
  1024.         hpos++;
  1025.         continue;
  1026.  
  1027.       case 0233:            /* 8-bit CSI */
  1028.         if (charset == LATIN1) {
  1029.         escape = 2;        /* 0233 is graphic char on PC, etc */
  1030.         continue;
  1031.         }                /* Otherwise fall thru & print it */
  1032. #endif /* ESCSEQ */
  1033.  
  1034.       default:
  1035.         addchar(c);
  1036.     }
  1037.     }
  1038.     if (!psflag) {            /* Done. If not postscript already, */
  1039.     if (hpos)            /* if last line was not empty, */
  1040.       addline();            /* add it to the page. */
  1041.     if (page != 1 || line != 0) {    /* Add trailer. */
  1042.         printf("EndPage\n%%%%Trailer\n%%%%Pages: %d\n",page);
  1043.     }
  1044.     }
  1045.     exit(0);                /* Done, return success code. */
  1046. }
  1047.  
  1048. void
  1049. usage(x) int x; {            /* Give usage message and quit. */
  1050.     int i;
  1051.     switch (x) {
  1052.       case 0:
  1053.     fprintf(stderr,"textps: only options, not filenames, allowed;");
  1054.     fprintf(stderr," use standard input.\n");
  1055.     break;
  1056.       case 1:
  1057.     fprintf(stderr,"textps: invalid character set name\n");
  1058.     break;
  1059.       case 2:
  1060.     fprintf(stderr,"textps: ambiguous option\n");
  1061.     break;
  1062.       case 3:
  1063.     fprintf(stderr,"textps: option required after -\n");
  1064.     break;
  1065.       case 4:
  1066.     fprintf(stderr,"textps: option requires an argument\n");
  1067.     break;
  1068.       case 5:
  1069.     fprintf(stderr,"textps: invalid option\n");
  1070.     break;    
  1071.       default:
  1072.     break;
  1073.     }
  1074.     fprintf(stderr,
  1075. "textps converts standard input to PostScript on standard output.\n");
  1076.     fprintf(stderr,
  1077. "if standard input is already in PostScript format, it is simply copied.\n");
  1078.     fprintf(stderr,
  1079. "usage:  textps -h -v -c charset -l number -w number < infile > outfile\n");
  1080.     fprintf(stderr,"  -h displays this usage message.\n");
  1081.  
  1082.     fprintf(stderr,
  1083. "  -v produces a page showing textps and PostScript version numbers.\n");
  1084.     fprintf(stderr,
  1085. "  -c specifies the file's character set, one of the following:\n");
  1086.     for (i = 0; i < ncsets; i++)
  1087.       fprintf(stderr,"      %s\n",csets[i].kwd);
  1088.     gcharset();
  1089.     fprintf(stderr,"  the default character set is %s.\n",
  1090.         getname(charset,csets,ncsets));
  1091.     fprintf(stderr,"  -l number is paper length in lines (default 66).\n");
  1092.     fprintf(stderr,"  -w number is paper width in characters (default 80).\n");
  1093.     fprintf(stderr,"examples:\n");
  1094.     fprintf(stderr,"  textps < infile > outfile\n");
  1095.     fprintf(stderr,"  textps -v < infile > outfile\n");
  1096.     fprintf(stderr,"  textps -c cp850 < infile > outfile\n");
  1097.     fprintf(stderr,"  textps -v -c next < infile > outfile\n");
  1098.     fprintf(stderr,"  textps < infile | lpr (UNIX)\n");
  1099.     fprintf(stderr,"  textps < infile > prn (DOS)\n");
  1100.     fprintf(stderr,"textps version: %s.\n",version);
  1101.     fprintf(stderr,"%s\n",copyright);
  1102.     exit(1);
  1103. }
  1104.