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