Commit | Line | Data |
---|---|---|
250f363a EA |
1 | # include <stdio.h> |
2 | # include <sgtty.h> | |
3 | # include <signal.h> | |
4 | ||
39f5fe56 | 5 | static char SccsId[] = "@(#)cpr.c 1.8 %G%"; |
250f363a EA |
6 | |
7 | /* | |
8 | ** CPR -- print on concept 108 | |
9 | ** | |
10 | ** This filter arranges to output onto a printer connected | |
11 | ** to a Concept 108 terminal. It probably works on other | |
12 | ** models in the Concept 100 series also. | |
13 | ** | |
14 | ** Usage: | |
771e5602 EA |
15 | ** cpr [-f] [file ...] |
16 | ** | |
17 | ** Flags: | |
18 | ** -f form feed following to print. | |
250f363a EA |
19 | */ |
20 | ||
39f5fe56 EA |
21 | #define LINELEN 132 /* carriage width */ |
22 | ||
771e5602 EA |
23 | typedef char bool; |
24 | #define TRUE 1 | |
25 | #define FALSE 0 | |
26 | ||
250f363a | 27 | struct sgttyb tbuf; |
f0c6e57d EA |
28 | int SysLinePid; /* pid of sysline process */ |
29 | bool FormFeedFollowing; /* print form feed following print */ | |
30 | bool EchoDuringPrint; /* echo on terminal while printing */ | |
250f363a EA |
31 | |
32 | main(argc, argv) | |
33 | int argc; | |
34 | char **argv; | |
35 | { | |
36 | register char *p; | |
37 | extern cleanterm(); | |
e9e8c060 | 38 | extern char *getenv(); |
250f363a | 39 | |
771e5602 EA |
40 | /* arrange to stop the sysline program during printing */ |
41 | p = getenv("SYSLINE"); | |
42 | if (p != NULL) | |
f0c6e57d | 43 | SysLinePid = atoi(p); |
771e5602 EA |
44 | |
45 | /* process arguments */ | |
46 | while (--argc > 0) | |
47 | { | |
48 | p = *++argv; | |
6feda4da EA |
49 | if (*p != '-') |
50 | break; | |
51 | switch (*++p) | |
771e5602 | 52 | { |
6feda4da EA |
53 | case 'f': |
54 | FormFeedFollowing = TRUE; | |
55 | break; | |
f0c6e57d EA |
56 | |
57 | case 'e': | |
58 | EchoDuringPrint = TRUE; | |
59 | break; | |
771e5602 EA |
60 | } |
61 | } | |
62 | ||
250f363a EA |
63 | /* be nice on interrupts, etc. */ |
64 | signal(SIGINT, cleanterm); | |
65 | ||
66 | /* set the terminal to output to printer */ | |
67 | setupterm(); | |
68 | ||
69 | /* print the appropriate files */ | |
6feda4da | 70 | if (argc < 1) |
250f363a EA |
71 | copyfile(); |
72 | else | |
73 | { | |
6feda4da | 74 | while (argc-- > 0) |
250f363a | 75 | { |
6feda4da EA |
76 | p = *argv++; |
77 | if (freopen(p, "r", stdin) == NULL) | |
78 | perror(p); | |
250f363a EA |
79 | else |
80 | copyfile(); | |
81 | } | |
82 | } | |
83 | ||
84 | /* reset terminal to a nice state */ | |
85 | cleanterm(); | |
86 | } | |
87 | ||
88 | copyfile() | |
89 | { | |
2f31d114 EA |
90 | { |
91 | p = index(buf, '\n'); | |
92 | if (p == NULL) | |
93 | continue; | |
94 | *p = '\r'; | |
95 | printf("\033 5%s\033|", buf); | |
96 | if (getack() < 0) | |
97 | { | |
98 | fprintf(stderr, "Lost printer\n"); | |
99 | cleanterm(); | |
100 | } | |
101 | fputs("\n", stdout); | |
102 | } | |
250f363a EA |
103 | } |
104 | ||
105 | setupterm() | |
106 | { | |
107 | int oldflags; | |
108 | ||
109 | if (gtty(1, &tbuf) < 0) | |
110 | { | |
111 | perror("cpr: stdout"); | |
112 | exit(1); | |
113 | } | |
114 | oldflags = tbuf.sg_flags; | |
115 | tbuf.sg_flags &= ~ECHO; | |
92c82992 | 116 | tbuf.sg_flags |= CBREAK | XTABS; |
250f363a EA |
117 | stty(1, &tbuf); |
118 | tbuf.sg_flags = oldflags; | |
250f363a EA |
119 | } |
120 | ||
121 | cleanterm() | |
122 | { | |
771e5602 EA |
123 | /* output trailing formfeed */ |
124 | if (FormFeedFollowing) | |
125 | fputs("\r\f", stdout); | |
126 | ||
127 | /* disconnect printer */ | |
250f363a EA |
128 | resetmodes(); |
129 | exit(0); | |
130 | } | |
131 | ||
132 | getack() | |
133 | { | |
134 | char buf[1]; | |
135 | ||
136 | fflush(stdout); | |
137 | if (read(2, buf, 1) <= 0 || buf[0] != '\006') | |
138 | return (-1); | |
139 | return (0); | |
140 | } | |
141 | ||
142 | resetmodes() | |
143 | { | |
144 | stty(1, &tbuf); | |
f0c6e57d EA |
145 | if (SysLinePid > 0) |
146 | kill(SysLinePid, SIGCONT); | |
250f363a | 147 | } |