-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprinter.c
310 lines (263 loc) · 9.38 KB
/
printer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* Copyright (c) 2002 Berliner Speicherring-Gesellschaft fuer Synchrotron-
* Strahlung mbH (BESSY).
* Copyright (c) 2002 Southeastern Universities Research Association, as
* Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
/************************DESCRIPTION***********************************
Printer routine. Work like independent process. Albert
**********************************************************************/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "alh.h"
#ifdef HAVE_SYSV_IPC
#include <sys/msg.h>
#endif
#include <errno.h>
#include <alarm.h>
#include "printer.h"
#define DEBUG 0
char *colorStart[ALARM_NSEV];
int colorStartLen[ALARM_NSEV];
char *colorEnd;
int colorEndLen;
char msg[250];
char buff[250];
int printerInit(char *,int);
int put2printer(char *,int,char *,int);
int compressMsg(char * msg, char *compress_buff);
int main(int argc,char *argv[])
{
#ifdef HAVE_SYSV_IPC
int sev; /* define color for printer */
struct msqid_ds infoBuf;
int printerMsgQId;
int printerMsgQKey;
int port;
int bytes=250;
int socket;
/* OS specific initialization */
#ifdef HP_UX
_main();
#endif
if (argc != 5) {
fprintf(stderr,"usage:%s TCPName TCPport Key ColorModel\nColor model={bw,bw_bold,oki_bold,hp_color}\n",argv[0]);
exit(1);
}
if ( !(printerMsgQKey=atoi(argv[3])) || !(port=atoi(argv[2])) ) {
fprintf(stderr,"usage:%s TCPName TCPport Key ColorModel\nColor model={bw,bw_bold,oki_bold,hp_color}\n",argv[0]);
exit(1);
}
if (strcmp(argv[4],"hp_color") == 0) {
colorStart[NO_ALARM]=&colorStartNoHpColor[0];
colorStartLen[NO_ALARM]=colorStartNoHpColorLen;
colorStart[MINOR_ALARM]=&colorStartMinorHpColor[0];
colorStartLen[MINOR_ALARM]=colorStartMinorHpColorLen;
colorStart[MAJOR_ALARM]=&colorStartMajorHpColor[0];
colorStartLen[MAJOR_ALARM]=colorStartMajorHpColorLen;
colorStart[INVALID_ALARM]=&colorStartMinorHpColor[0];
colorStartLen[INVALID_ALARM]=colorStartInvalidHpColorLen;
colorEnd=&colorEndHpColor[0];
colorEndLen=colorEndHpColorLen;
}
else if (strcmp(argv[4],"bw_bold") == 0) {
colorStart[NO_ALARM]=&colorStartNoMonoBold[0];
colorStartLen[NO_ALARM]=colorStartNoMonoBoldLen;
colorStart[MINOR_ALARM]=&colorStartMinorMonoBold[0];
colorStartLen[MINOR_ALARM]=colorStartMinorMonoBoldLen;
colorStart[MAJOR_ALARM]=&colorStartMajorMonoBold[0];
colorStartLen[MAJOR_ALARM]=colorStartMajorMonoBoldLen;
colorStart[INVALID_ALARM]=&colorStartMinorMonoBold[0];
colorStartLen[INVALID_ALARM]=colorStartInvalidMonoBoldLen;
colorEnd=&colorEndMonoBold[0];
colorEndLen=colorEndMonoBoldLen;
}
else if (strcmp(argv[4],"oki_bold") == 0) {
colorStart[NO_ALARM]=&colorStartNoOkiBold[0];
colorStartLen[NO_ALARM]=colorStartNoOkiBoldLen;
colorStart[MINOR_ALARM]=&colorStartMinorOkiBold[0];
colorStartLen[MINOR_ALARM]=colorStartMinorOkiBoldLen;
colorStart[MAJOR_ALARM]=&colorStartMajorOkiBold[0];
colorStartLen[MAJOR_ALARM]=colorStartMajorOkiBoldLen;
colorStart[INVALID_ALARM]=&colorStartMinorOkiBold[0];
colorStartLen[INVALID_ALARM]=colorStartInvalidOkiBoldLen;
colorEnd=&colorEndOkiBold[0];
colorEndLen=colorEndOkiBoldLen;
}
else {
colorStart[NO_ALARM]=&colorStartNoMono[0];
colorStartLen[NO_ALARM]=colorStartNoMonoLen;
colorStart[MINOR_ALARM]=&colorStartMinorMono[0];
colorStartLen[MINOR_ALARM]=colorStartMinorMonoLen;
colorStart[MAJOR_ALARM]=&colorStartMajorMono[0];
colorStartLen[MAJOR_ALARM]=colorStartMajorMonoLen;
colorStart[INVALID_ALARM]=&colorStartMinorMono[0];
colorStartLen[INVALID_ALARM]=colorStartInvalidMonoLen;
colorEnd=&colorEndMono[0];
colorEndLen=colorEndMonoLen;
}
printerMsgQId = msgget (printerMsgQKey, 0600|IPC_CREAT);
if(printerMsgQId == -1) {perror("msgQ_create"); exit(1);}
else
{
fprintf(stderr,"msgQ with key=%d is OK\n",printerMsgQKey);
if (msgctl(printerMsgQId,IPC_STAT,&infoBuf) != 1)
{
fprintf(stderr,"owner = %d.%d, perms = %04o, max bytes = %ld\n",
infoBuf.msg_perm.uid,infoBuf.msg_perm.gid,
infoBuf.msg_perm.mode,infoBuf.msg_qbytes);
fprintf(stderr,"%ld msgs = %ld bytes on queue\n",
infoBuf.msg_qnum, infoBuf.msg_cbytes);
}
else {perror("msgctl()"); exit(1);}
}
fprintf(stderr,"Please wait ...\n");
if( (socket=printerInit(argv[1],port)) <= 0 ) {
perror("can't connect to TCPprinter");
exit(1);
}
else fprintf(stderr,"printerInit for %s port=%d is OK\n",argv[1],port);
close(socket);
sleep(2);
while(1)
{
if( msgrcv(printerMsgQId,msg,bytes,0,0) >= 0) /* got a new message */
{
if ( (sev=compressMsg(msg+2,buff)) < 0 ) /* msg's big for 80char printer:cut it*/
{ perror("bad format"); continue;}
put2printer(argv[1],port,buff,strlen(buff));
}
else usleep(1000); /* # in microsec */
}
}
int printerInit(char *hostname,int port)
{
int printerSocket;
struct sockaddr_in s_name;
struct hostent *h_ptr;
int true=1;
if ((printerSocket=socket(AF_INET, SOCK_STREAM, 0)) == -1){
perror( "socket");
return(-1);
}
if ((h_ptr = gethostbyname(hostname)) == NULL )
{
perror("gethostbyname");
exit(1);
}
memset(&s_name, 0, sizeof(s_name));
s_name.sin_family = AF_INET;
s_name.sin_port = htons(port);
memcpy(&s_name.sin_addr, h_ptr->h_addr, sizeof (s_name.sin_addr));
true=1;
if(setsockopt(printerSocket,SOL_SOCKET,SO_KEEPALIVE,(char *)&true,sizeof(true))) {
perror("Keepalive option set failed");
return(-1);
}
true=1;
if(setsockopt(printerSocket,IPPROTO_TCP,TCP_NODELAY,(char *)&true,sizeof(true))){
perror("nodelay option set failed");
return(-1);
}
true=128;
if(setsockopt(printerSocket,SOL_SOCKET,SO_SNDBUF,(char *)&true,sizeof(true))){
perror("sendbuf option set failed");
return(-1);
}
if (connect(printerSocket, (struct sockaddr *) &s_name, sizeof (s_name)) ) {
perror("connect");
close(printerSocket);
return(-1);
}
return(printerSocket);
}
int put2printer(char *hostname,int port,char *string,int len)
{
int printerSock;
int ret;
while(1)
{
if( (printerSock=printerInit(hostname,port)) <= 0) {
sleep(1);
continue;
}
ret=send(printerSock,string,len,0);
if (ret != strlen(string)) {
fprintf(stderr,"Printer:Send_byte=%d need%d\n",ret,len);
}
close(printerSock);
usleep(1000);
return(0);
}
}
int compressMsg(char * msg, char *compress_buff)
{
int sev,type;
char *blank;
/* We assume that mess has next format:
"typeOfRecord+1 time_stamp buff"
in regular case buff =(name al al_type h_al h_al_type value)
then we calculate sevirity: if typeOfRecord > 0 sev=MAJOR=2 -- service error;
if typeOfRecord=0 we calculate 3-rd word in buff
if word = NO_ALARM sev=0
MINOR sev=1;
MAJOR sev=2;
INVALID sev =3;
all other sev=2;
in regular case (typeOfRecord = 0) we cut al al_type h_al h_al_type till 5 char
in begin of compress_buff we add ColorStart-symbols and finish it ColorEnd-symbols,
so printer will print it in color!!!
*/
if ( ( type = atoi(msg) ) == 0 ) {
fprintf(stderr,"%s: first element must be number\n",msg);
return(-1);
}
if (type > 1) {
sprintf(compress_buff,"%s%s%s",colorStart[MAJOR_ALARM],msg+2,colorEnd);
if(DEBUG) fprintf(stderr,"buff=%s;",compress_buff);
return(MAJOR_ALARM);
}
/* EXAMPLE:0 01-Apr-1999 12:39:49 AHTST:out2_ao NO_ALARM NO_ALARM HIHI MAJOR 0.00
+ 20 +1 +28 +1 +12 +1 +16 +1+12+1+16 +1
| | | | | | | |
0 2 23 52 65 82 95 112
*/
if (strncmp(msg+52,"NO_ALARM",7) == 0) sev=0;
else if (strncmp(msg+52,"MINOR" ,5) == 0) sev=1;
else if (strncmp(msg+52,"INVALID", 6) == 0) sev=3;
else sev=2;
strcpy (compress_buff,colorStart[sev]); /* color symb */
strncat(compress_buff,msg+2,20); /* TS */
if ( (blank = strchr( (const char *) msg+23, ' ')) == NULL) {
fprintf(stderr,"%s: bad 1 blank in msg \n",msg);
return(-1);
}
strncat(compress_buff,msg+22,blank - msg-22 ); /* blank + name */
strncat(compress_buff,msg+51,6); /* blank + first 5 symbols of alarm */
strncat(compress_buff,msg+64,6); /* blank + first 5 symbols of sev */
strcat (compress_buff,msg+111); /* blank +value */
strcat (compress_buff,colorEnd); /* color symb */
strcat (compress_buff,"\n"); /* \n-it's important for printer */
if(DEBUG) fprintf(stderr,"uncompress buff=%s;\n compress buff=%s;\n",msg,compress_buff);
return(sev);
}
#else /* HAVE_SYSV_IPC */
return 1;
}
#endif /* HAVE_SYSV_IPC */