clean up error handling
[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
e9e8c060 5static char SccsId[] = "@(#)cpr.c 1.3 %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:
15** cpr [file ...]
16*/
17
18struct sgttyb tbuf;
e9e8c060 19int SysLine;
250f363a
EA
20
21main(argc, argv)
22 int argc;
23 char **argv;
24{
25 register char *p;
26 extern cleanterm();
e9e8c060 27 extern char *getenv();
250f363a
EA
28
29 /* be nice on interrupts, etc. */
30 signal(SIGINT, cleanterm);
31
32 /* set the terminal to output to printer */
e9e8c060
EA
33 p = getenv("SYSLINE");
34 if (p != NULL)
35 SysLine = atoi(p);
250f363a
EA
36 setupterm();
37
38 /* print the appropriate files */
39 if (argc < 2)
40 copyfile();
41 else
42 {
43 while (--argc > 0)
44 {
45 if (freopen(*++argv, "r", stdin) == NULL)
46 perror(*argv);
47 else
48 copyfile();
49 }
50 }
51
52 /* reset terminal to a nice state */
53 cleanterm();
54}
55
56copyfile()
57{
58 char buf[200];
2f31d114
EA
59 register char *p;
60 extern char *index();
61
250f363a 62 while (fgets(buf, sizeof buf, stdin) != NULL)
2f31d114
EA
63 {
64 p = index(buf, '\n');
65 if (p == NULL)
66 continue;
67 *p = '\r';
68 printf("\033 5%s\033|", buf);
69 if (getack() < 0)
70 {
71 fprintf(stderr, "Lost printer\n");
72 cleanterm();
73 }
74 fputs("\n", stdout);
75 }
250f363a
EA
76}
77
78setupterm()
79{
80 int oldflags;
81
82 if (gtty(1, &tbuf) < 0)
83 {
84 perror("cpr: stdout");
85 exit(1);
86 }
87 oldflags = tbuf.sg_flags;
88 tbuf.sg_flags &= ~ECHO;
89 tbuf.sg_flags |= CBREAK;
90 stty(1, &tbuf);
91 tbuf.sg_flags = oldflags;
e9e8c060
EA
92 if (SysLine > 0)
93 kill(SysLine, SIGSTOP);
250f363a
EA
94}
95
96cleanterm()
97{
250f363a
EA
98 resetmodes();
99 exit(0);
100}
101
102getack()
103{
104 char buf[1];
105
106 fflush(stdout);
107 if (read(2, buf, 1) <= 0 || buf[0] != '\006')
108 return (-1);
109 return (0);
110}
111
112resetmodes()
113{
114 stty(1, &tbuf);
e9e8c060
EA
115 if (SysLine > 0)
116 kill(SysLine, SIGCONT);
250f363a 117}