move includes for 4.1c directory layout
[unix-history] / usr / src / local / local.cmd / cpr.c
CommitLineData
250f363a
EA
1# include <stdio.h>
2# include <sgtty.h>
3# include <signal.h>
4
f0c6e57d 5static 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
21typedef char bool;
22#define TRUE 1
23#define FALSE 0
24
250f363a 25struct sgttyb tbuf;
f0c6e57d
EA
26int SysLinePid; /* pid of sysline process */
27bool FormFeedFollowing; /* print form feed following print */
28bool EchoDuringPrint; /* echo on terminal while printing */
250f363a
EA
29
30main(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
86copyfile()
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
108setupterm()
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
124cleanterm()
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
135getack()
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
145resetmodes()
146{
147 stty(1, &tbuf);
f0c6e57d
EA
148 if (SysLinePid > 0)
149 kill(SysLinePid, SIGCONT);
250f363a 150}