]>
Commit | Line | Data |
---|---|---|
fe3a7d70 | 1 | /* |
21006e63 CW |
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 | ||
a5139f37 | 29 | #ifdef __CYGWIN__ |
21006e63 | 30 | #include <sys/cygwin.h> |
a5139f37 | 31 | #endif |
21006e63 CW |
32 | |
33 | #include "src/lpr/Printer.hh" | |
34 | ||
35 | using namespace std; | |
36 | ||
37 | string progName; | |
38 | ||
39 | string getProgramName(const string & str) | |
40 | { | |
41 | string::size_type lastSlashPos = str.find_last_of("\\/"); | |
42 | if (lastSlashPos == string::npos) | |
43 | lastSlashPos = 0; | |
44 | else | |
45 | ++lastSlashPos; | |
46 | string::size_type dotPos = str.find_last_of("."); | |
47 | if (dotPos == string::npos || dotPos < lastSlashPos) | |
48 | dotPos = str.length(); | |
49 | return str.substr(lastSlashPos, dotPos - lastSlashPos); | |
50 | } | |
51 | ||
52 | const char * usageMessage = | |
a5139f37 CW |
53 | " [--help|-help] [-h] [-D] [-d device] [-l] [-P device]\n"; |
54 | ||
55 | const char * helpMessage = | |
fe3a7d70 CW |
56 | "\n" |
57 | "where:\n" | |
58 | "\n" | |
59 | " -h does nothing. Accepted for compatibility.\n" | |
60 | " -d device spools to the specified device.\n" | |
61 | " -D enable debugging output.\n" | |
a5139f37 CW |
62 | " -l prevent <LF> -> <CR><LF> processing. By default, standalone\n" |
63 | " <LF> characters are converted to <CR><LF>.\n" | |
fe3a7d70 | 64 | " -P device spools to the specified device.\n" |
a5139f37 CW |
65 | " --help print this message.\n" |
66 | " -help print this message.\n" | |
fe3a7d70 CW |
67 | "\n" |
68 | "Notes:\n" | |
69 | "\n" | |
70 | "-d and -P are aliases of each other and perform the same function.\n" | |
71 | "Device names may take the form of DOS devices (e.g., lpt1:) if the printer\n" | |
72 | "is connected locally. Network printers can be accessed using the form\n" | |
73 | "'\\\\server\\printer_name'. Forward slashes can be used as well, e.g.,\n" | |
74 | "'//server/printer_name'.\n" | |
75 | "\n" | |
76 | "The environment variable PRINTER can be used to set the default printer\n" | |
77 | "device.\n"; | |
21006e63 | 78 | |
a5139f37 | 79 | void usage(int errcode, const string & msg = "") |
21006e63 | 80 | { |
a5139f37 CW |
81 | // always use cerr even if errcode is 0, because lpr is |
82 | // often in a pipeline | |
21006e63 CW |
83 | if (msg != "") |
84 | cerr << progName << ": " << msg << endl << endl; | |
85 | ||
86 | cerr << "Usage: " << progName << usageMessage; | |
a5139f37 CW |
87 | exit(errcode); |
88 | } | |
89 | ||
90 | void help(void) | |
91 | { | |
92 | // always use cerr because lpr is often in a pipeline | |
93 | cerr << "Usage: " << progName << usageMessage << helpMessage; | |
94 | exit(0); | |
95 | } | |
96 | ||
97 | void scan_for_help(int argc, char *argv[]) | |
98 | { | |
99 | for (char **scan = (char **)argv; *scan != NULL; ++scan) | |
100 | { | |
101 | if ( (strncmp (*scan, "--help", 6) == 0) | |
102 | ||(strncmp (*scan, "-help", 5) == 0)) | |
103 | { | |
104 | help (); | |
105 | } | |
106 | } | |
21006e63 CW |
107 | } |
108 | ||
109 | int main(int argc, char *argv[]) | |
110 | { | |
111 | progName = getProgramName(argv[0]); | |
a5139f37 CW |
112 | scan_for_help(argc, argv); |
113 | ||
21006e63 CW |
114 | string printerName = ""; |
115 | ||
116 | // Is the printer specified in the environment? | |
117 | const char *p = getenv("PRINTER"); | |
118 | if (p != 0) | |
119 | printerName = p; | |
120 | ||
121 | bool debugFlag = false; | |
a18d2869 | 122 | bool rawFlag = false; |
21006e63 CW |
123 | |
124 | int optionChar; | |
a5139f37 | 125 | optopt = '\0'; |
a18d2869 | 126 | while ((optionChar = getopt(argc, argv, ":Dd:hlP:")) != EOF) |
21006e63 | 127 | { |
fe3a7d70 CW |
128 | switch (optionChar) |
129 | { | |
130 | case 'h': | |
131 | // accept for compatibility | |
132 | break; | |
133 | case 'D': | |
134 | debugFlag = true; | |
135 | break; | |
136 | case 'd': | |
137 | case 'P': | |
138 | printerName = optarg; | |
139 | break; | |
140 | case 'l': | |
141 | rawFlag = true; | |
142 | break; | |
a5139f37 CW |
143 | case ':': |
144 | usage(1, string("option `-") + char(optopt) + string("' missing required argument")); | |
145 | break; | |
146 | case '?': | |
147 | usage(1, string("unknown option: -") + char(optopt)); | |
148 | break; | |
fe3a7d70 | 149 | default: |
a5139f37 | 150 | usage(1, string("unknown option: -") + char(optionChar)); |
fe3a7d70 | 151 | } |
a5139f37 | 152 | optopt='\0'; |
21006e63 | 153 | } |
21006e63 CW |
154 | |
155 | // Can't proceed without a printer name. | |
156 | if (printerName == "") | |
fe3a7d70 CW |
157 | { |
158 | cerr << progName << ": no printer specified" << endl; | |
159 | return 1; | |
160 | } | |
161 | ||
21006e63 CW |
162 | char winPrinter[MAX_PATH]; |
163 | cygwin_conv_to_win32_path(printerName.c_str(), winPrinter); | |
164 | if (debugFlag) | |
165 | cout << "Windows printer name = '" << winPrinter << "'" << endl; | |
fe3a7d70 | 166 | |
21006e63 | 167 | try |
21006e63 | 168 | { |
fe3a7d70 CW |
169 | Printer pr(winPrinter, debugFlag); |
170 | pr.setRawFlag(rawFlag); | |
171 | ||
172 | if (optind >= argc) | |
173 | pr.print(cin, "stdin"); | |
174 | else | |
175 | { | |
176 | for (int ii = optind; ii < argc; ++ii) | |
177 | { | |
178 | ifstream in (argv[ii]); | |
179 | if (!in) | |
180 | cerr << progName << ": can't open '" << argv[ii] << "' for input." | |
181 | << endl; | |
182 | else | |
183 | pr.print(in, argv[ii]); | |
184 | } | |
185 | } | |
186 | pr.close(); | |
21006e63 | 187 | } |
21006e63 | 188 | catch (const PrinterException & ex) |
fe3a7d70 CW |
189 | { |
190 | cerr << progName << ": printer error: " << ex << endl; | |
191 | return 1; | |
192 | } | |
21006e63 CW |
193 | |
194 | return 0; | |
195 | } |