-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathprint_main.cc
215 lines (195 loc) · 6.61 KB
/
print_main.cc
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
/*
* Copyright ViewTouch, Inc., 1995, 1996, 1997, 1998
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* print_main.cc - revision 8 (9/8/98)
* Startup for printing process 'vt_print'
*/
#include <string.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include "socket.hh"
#include "utility.hh"
#include <string>
#include <iostream>
#include <sstream>
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#ifdef BSD
#define DEVPORT "/dev/lpt0"
#else
#define DEVPORT "/dev/lp0"
#endif
constexpr int default_port_number = 65530; // the default socket on which to listen
struct Parameter
{
std::string PrinterDevName = DEVPORT; // the parallel device file
int InetPortNumber = default_port_number; // the socket on which to listen
bool verbose = false; // verbose mode
};
/*********************************************************************
* PROTOTYPES
********************************************************************/
int PrintFromRemote(const int my_socket, const int printer);
Parameter ParseArguments(const int argc, const char* argv[]);
void ShowHelp(const std::string &progname);
/*********************************************************************
* MAIN LOOP
********************************************************************/
int main(int argc, const char* argv[])
{
int my_socket;
int lock;
int printer;
int connection;
// parse the arguments for printer device
Parameter param = ParseArguments(argc, argv);
if (param.verbose)
{
std::cout << "Listening on port " << param.InetPortNumber << std::endl;
std::cout << "Writing to printer at " << param.PrinterDevName << std::endl;
}
exit(2);
// listen for connections
my_socket = Listen(param.InetPortNumber);
while (my_socket > -1)
{
if (param.verbose)
std::cout << "Waiting to accept connection..." << std::endl;
connection = Accept(my_socket);
if (connection > -1)
{
lock = LockDevice(param.PrinterDevName.c_str());
if (lock > 0)
{
printer = open(param.PrinterDevName.c_str(), O_WRONLY | O_APPEND);
if (printer >= 0)
{
PrintFromRemote(connection, printer);
if (param.verbose)
std::cout << "Closing Printer" << std::endl;
close(printer);
}
else
{
const std::string msg =
std::string("Failed to open ") + param.PrinterDevName;
perror(msg.c_str());
}
UnlockDevice(lock);
}
if (param.verbose)
std::cout << "Closing socket" << std::endl;
close(connection);
}
}
return 0;
}
/*********************************************************************
* SUBROUTINES
********************************************************************/
/****
* PrintFromRemote: given an open socket and an open printer connection,
* reads from the socket and passes the data to the printer.
****/
int PrintFromRemote(const int my_socket, const int printer)
{
char buffer[STRLENGTH];
int active = 1;
while (active > 0)
{ // read from socket, print to printer until we get an error
active = recv(my_socket, buffer, STRLENGTH, 0);
if (active > 0)
{
active = write(printer, buffer, active); // active contains bytes read
if (active < 0)
perror("Failed to write");
}
else if (active < 0)
perror("Failed to read");
}
return active;
}
/****
* ShowHelp:
****/
void ShowHelp(const std::string &progname)
{
std::cout << std::endl
<< "Usage: " << progname << " [OPTIONS]" << std::endl
<< " -d<device> Printer device (default " << DEVPORT << ")" << std::endl
<< " -h Show this help screen" << std::endl
<< " -p<port> Set the listening port (default " << default_port_number << ")" << std::endl
<< " -v Verbose mode" << std::endl
<< std::endl
<< "Note: there can be no spaces between an option and the associated" << std::endl
<< "argument. AKA, it's \"-p6555\" not \"-p 6555\"." << std::endl
<< std::endl;
exit(1);
}
/****
* ParseArguments: Walk through the arguments setting global
* variables as necessary.
****/
Parameter ParseArguments(const int argc, const char* argv[])
{
Parameter param;
// start at 1, first command line argument past binary name
for (int idx = 1; idx < argc; idx++)
{
std::string arg = argv[idx];
std::string prefix = arg.substr(0,2);
std::string val = arg.substr(2);
if (prefix == "-d")
{
if (val.empty())
{
std::cout << "Error parsing argument '" << arg << "'."
<< " No printer specified" << std::endl;
ShowHelp(argv[0]);
}
param.PrinterDevName = val;
} else if (prefix == "-h")
{
ShowHelp(argv[0]);
} else if (prefix == "-p")
{
if (val.empty())
{
std::cout << "Error parsing argument '" << arg << "'."
<< " No port number specified" << std::endl;
ShowHelp(argv[0]);
}
std::istringstream ss(val);
ss >> param.InetPortNumber;
if (ss.fail())
{
std::cout << "Can't parse port number: " << val << std::endl;
ShowHelp(argv[0]);
}
} else if (prefix == "-v")
{
param.verbose = true;
} else
{
std::cout << "Unrecognized parameter '" << arg << "'" << std::endl;
ShowHelp(argv[0]);
}
}
return param;
}