]> cygwin.com Git - cygwin-apps/cygutils.git/blob - src/lpr/lpr.cc
Reimplement lpr in C++, Rick Rankin.
[cygwin-apps/cygutils.git] / src / lpr / lpr.cc
1 /*
2 * lpr for cygwin/windows
3 *
4 * Copyright (C) 2000-2003 Rick Rankin
5 * http://www.cygwin.com/ml/cygwin/2000-07/msg00320.html
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation in version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24 #include "common.h"
25
26 #include <fstream>
27 #include <string>
28
29 #include <sys/cygwin.h>
30
31 #include "src/lpr/Printer.hh"
32
33 using namespace std;
34
35 string progName;
36
37 string getProgramName(const string & str)
38 {
39 string::size_type lastSlashPos = str.find_last_of("\\/");
40 if (lastSlashPos == string::npos)
41 lastSlashPos = 0;
42 else
43 ++lastSlashPos;
44 string::size_type dotPos = str.find_last_of(".");
45 if (dotPos == string::npos || dotPos < lastSlashPos)
46 dotPos = str.length();
47 return str.substr(lastSlashPos, dotPos - lastSlashPos);
48 }
49
50 const char * usageMessage =
51 " [-h] [-D] [-d device] [-P device]\n"
52 "\n"
53 "where:\n"
54 "\n"
55 " -h does nothing. Accepted for compatibility.\n"
56 " -d device spools to the specified device.\n"
57 " -D enable debugging output.\n"
58 " -P device spools to the specified device.\n"
59 "\n"
60 "Notes:\n"
61 "\n"
62 "-d and -P are aliases of each other and perform the same function.\n"
63 "Device names may take the form of DOS devices (e.g., lpt1:) if the printer\n"
64 "is connected locally. Network printers can be accessed using the form\n"
65 "'\\\\server\\printer_name'. Forward slashes can be used as well, e.g.,\n"
66 "'//server/printer_name'.\n"
67 "\n"
68 "The environment variable PRINTER can be used to set the default printer\n"
69 "device.\n";
70
71 void usage(const string & msg = "")
72 {
73 if (msg != "")
74 cerr << progName << ": " << msg << endl << endl;
75
76 cerr << "Usage: " << progName << usageMessage;
77 exit(1);
78 }
79
80 int main(int argc, char *argv[])
81 {
82 progName = getProgramName(argv[0]);
83 string printerName = "";
84
85 // Is the printer specified in the environment?
86 const char *p = getenv("PRINTER");
87 if (p != 0)
88 printerName = p;
89
90 bool debugFlag = false;
91
92 int optionChar;
93 while ((optionChar = getopt(argc, argv, ":hDd:P:")) != EOF)
94 {
95 switch (optionChar)
96 {
97 case 'h':
98 // accept for compatibility
99 break;
100 case 'D':
101 debugFlag = true;
102 break;
103 case 'd':
104 case 'P':
105 printerName = optarg;
106 break;
107 default:
108 usage(string("unknown option: -") + char(optionChar));
109 }
110 }
111
112 // Can't proceed without a printer name.
113 if (printerName == "")
114 {
115 cerr << progName << ": no printer specified" << endl;
116 return 1;
117 }
118
119 char winPrinter[MAX_PATH];
120 cygwin_conv_to_win32_path(printerName.c_str(), winPrinter);
121 if (debugFlag)
122 cout << "Windows printer name = '" << winPrinter << "'" << endl;
123
124 try
125 {
126 Printer pr(winPrinter, debugFlag);
127
128 if (optind >= argc)
129 pr.print(cin, "stdin");
130 else
131 {
132 for (int ii = optind; ii < argc; ++ii)
133 {
134 ifstream in (argv[ii]);
135 if (!in)
136 cerr << progName << ": can't open '" << argv[ii] << "' for input."
137 << endl;
138 else
139 pr.print(in, argv[ii]);
140 }
141 }
142 pr.close();
143 }
144 catch (const PrinterException & ex)
145 {
146 cerr << progName << ": printer error: " << ex << endl;
147 return 1;
148 }
149
150 return 0;
151 }
This page took 0.041945 seconds and 5 git commands to generate.