home *** CD-ROM | disk | FTP | other *** search
- /* textps.c */
-
- #ifndef lint
- static char rcsid[] = "$Header: /usr/jjc/lprps/RCS/textps.c,v 1.2 90/12/18 10:01:58 jjc Rel $";
- #endif
-
- #include <stdio.h>
- #include <ctype.h>
-
- extern char *malloc();
- extern char *optarg;
- extern int optind;
-
- typedef struct output_char {
- struct output_char *next;
- char c;
- char is_bold;
- int pos;
- } output_char;
-
- output_char *free_output_char_list = 0;
-
- int tab_width = 8;
- int lines_per_page = 66;
- #ifdef A4
- int page_length = 842; /* in points */
- #else
- int page_length = 792; /* in points */
- #endif
-
- int baseline_offset = 8; /* distance in points from top of page to */
- /* first baseline */
-
- int line_spacing; /* in points */
-
- int left_margin = 18; /* in points */
- int char_width;
-
- int pageno = 0;
-
- char *font = "Courier";
- char *bold_font = "Courier-Bold";
- enum { NONE, ROMAN, BOLD } current_font;
-
- void do_file();
- void prologue();
- void trailer();
- char *prog;
-
- void usage()
- {
- fprintf(stderr, "usage: %s [-l n] [ files ... ]\n", prog);
- exit(1);
- }
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int bad_files = 0;
- int cpi = 12; /* characters per inch */
- int lpi = 6; /* lines per inch */
- int opt;
-
- prog = argv[0];
-
- while ((opt = getopt(argc, argv, "l:")) != EOF)
- switch (opt) {
- case 'l':
- if (sscanf(optarg, "%d", &lines_per_page) != 1)
- usage();
- break;
- case '?':
- usage();
- default:
- abort();
- }
-
- char_width = 72/cpi;
- line_spacing = 72/lpi;
- prologue();
- if (optind >= argc)
- do_file(stdin);
- else {
- int i;
- for (i = optind; i < argc; i++)
- if (strcmp(argv[i], "-") == 0)
- do_file(stdin);
- else {
- FILE *fp;
- if ((fp = fopen(argv[i], "r")) == NULL) {
- perror(argv[i]);
- bad_files++;
- }
- else {
- do_file(fp);
- fclose(fp);
- }
- }
- }
- trailer();
- exit(bad_files);
- }
-
-
- output_char *new_output_char()
- {
- if (free_output_char_list) {
- output_char *tem = free_output_char_list;
- free_output_char_list = free_output_char_list->next;
- return tem;
- }
- else {
- output_char *tem;
- if ((tem = (output_char *)malloc(sizeof(output_char))) == NULL) {
- fprintf(stderr, "%s: out of memory\n", prog);
- exit(1);
- }
- return tem;
- }
- }
-
- void delete_output_char(p)
- output_char *p;
- {
- p->next = free_output_char_list;
- free_output_char_list = p;
- }
-
-
- void pschar(c)
- int c;
- {
- if (!isascii(c) || iscntrl(c))
- printf("\\%03o", c &0377);
- else if (c == '(' || c == ')' || c == '\\') {
- putchar('\\');
- putchar(c);
- }
- else
- putchar(c);
- }
-
- /* output_line is ordered greatest position first */
-
- void print_line(output_line, vpos)
- output_char *output_line;
- int vpos;
- {
- output_char *rev = output_line;
- output_line = 0;
- while (rev != 0) {
- output_char *tem = rev;
- rev = rev->next;
- tem->next = output_line;
- output_line = tem;
- }
- while (output_line != NULL) {
- output_char *tem;
- output_char **p = &output_line;
- int start_pos = output_line->pos;
- int is_bold = output_line->is_bold;
- int pos;
- if (is_bold) {
- if (current_font != BOLD) {
- printf("B");
- current_font = BOLD;
- }
- }
- else {
- if (current_font != ROMAN) {
- printf("R");
- current_font = ROMAN;
- }
- }
- putchar('(');
- pschar(output_line->c);
- pos = output_line->pos + 1;
- tem = output_line;
- output_line = output_line->next;
- delete_output_char(tem);
- for (;;) {
- while (*p != NULL
- && ((*p)->pos < pos || (*p)->is_bold != is_bold))
- p = &(*p)->next;
- if (*p == NULL)
- break;
- while (pos < (*p)->pos) {
- pschar(' ');
- pos++;
- }
- pschar((*p)->c);
- pos++;
- tem = *p;
- *p = tem->next;
- delete_output_char(tem);
- }
- putchar(')');
- printf("%d %d L\n",
- left_margin + start_pos*char_width,
- page_length - baseline_offset - vpos*line_spacing);
- }
- }
-
- void page_start()
- {
- printf("%%%%Page: ? %d\nPS\n", ++pageno);
- current_font = NONE;
- }
-
- void page_end()
- {
- printf("PE\n");
- }
-
- void prologue()
- {
- printf("%%!PS-Adobe-2.0\n");
- printf("%%%%DocumentFonts: %s %s\n", font, bold_font);
- printf("%%%%Pages: (atend)\n");
- printf("%%%%EndComments\n");
- printf("/L { moveto show } bind def\n");
- printf("/R [ /%s findfont %d 6 div scalefont /setfont load ] cvx def\n",
- font, char_width*10);
- printf("/B [ /%s findfont %d 6 div scalefont /setfont load ] cvx def\n",
- bold_font, char_width*10);
- printf("/PS { /level0 save def } bind def\n");
- printf("/PE { level0 restore showpage } bind def\n");
- printf("%%%%EndProlog\n");
- }
-
- void trailer()
- {
- printf("%%%%Trailer\n%%%%Pages: %d\n", pageno);
- }
-
- /* p is ordered greatest position first */
-
- void add_char(c, pos, p)
- int c;
- int pos;
- output_char **p;
- {
- for (;; p = &(*p)->next) {
- if (*p == NULL || (*p)->pos < pos) {
- output_char *tem = new_output_char();
- tem->next = *p;
- *p = tem;
- tem->c = c;
- tem->is_bold = 0;
- tem->pos = pos;
- break;
- }
- else if ((*p)->pos == pos) {
- if (c == (*p)->c) {
- (*p)->is_bold = 1;
- break;
- }
- }
- }
- }
-
- void do_file(fp)
- FILE *fp;
- {
- int c;
- int vpos = 0;
- int hpos = 0;
- int page_started = 0;
- int esced = 0;
- output_char *output_line = 0;
- while ((c = getc(fp)) != EOF)
- if (esced)
- switch(c) {
- case '7':
- if (vpos > 0) {
- if (output_line != NULL) {
- if (!page_started) {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- vpos -= 1;
- }
- /* hpos = 0; */
- esced = 0;
- break;
- default:
- /* silently ignore */
- esced = 0;
- break;
- }
- else
- switch (c) {
- case '\033':
- esced = 1;
- break;
- case '\b':
- if (hpos > 0)
- hpos--;
- break;
- case '\f':
- if (!page_started)
- page_start();
- print_line(output_line, vpos);
- output_line = 0;
- page_end();
- hpos = 0; /* ?? */
- vpos = 0;
- page_started = 0;
- break;
- case '\r':
- hpos = 0;
- break;
- case '\n':
- if (output_line != NULL) {
- if (!page_started) {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- vpos += 1;
- if (vpos >= lines_per_page) {
- if (!page_started)
- page_start();
- page_end();
- page_started = 0;
- vpos = 0;
- }
- hpos = 0;
- break;
- case ' ':
- hpos++;
- break;
- case '\t':
- hpos = ((hpos + tab_width)/tab_width)*tab_width;
- break;
- default:
- if (!(isascii(c) && iscntrl(c))) {
- add_char(c, hpos, &output_line);
- hpos++;
- }
- break;
- }
- if (output_line != NULL) {
- if (!page_started) {
- page_started = 1;
- page_start();
- }
- print_line(output_line, vpos);
- output_line = 0;
- }
- if (page_started)
- page_end();
- }
-