]>
Commit | Line | Data |
---|---|---|
21006e63 CW |
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; | |
a18d2869 | 91 | bool rawFlag = false; |
21006e63 CW |
92 | |
93 | int optionChar; | |
a18d2869 | 94 | while ((optionChar = getopt(argc, argv, ":Dd:hlP:")) != EOF) |
21006e63 CW |
95 | { |
96 | switch (optionChar) | |
97 | { | |
98 | case 'h': | |
99 | // accept for compatibility | |
100 | break; | |
101 | case 'D': | |
102 | debugFlag = true; | |
103 | break; | |
104 | case 'd': | |
105 | case 'P': | |
106 | printerName = optarg; | |
107 | break; | |
a18d2869 CW |
108 | case 'l': |
109 | rawFlag = true; | |
110 | break; | |
21006e63 CW |
111 | default: |
112 | usage(string("unknown option: -") + char(optionChar)); | |
113 | } | |
114 | } | |
115 | ||
116 | // Can't proceed without a printer name. | |
117 | if (printerName == "") | |
118 | { | |
119 | cerr << progName << ": no printer specified" << endl; | |
120 | return 1; | |
121 | } | |
122 | ||
123 | char winPrinter[MAX_PATH]; | |
124 | cygwin_conv_to_win32_path(printerName.c_str(), winPrinter); | |
125 | if (debugFlag) | |
126 | cout << "Windows printer name = '" << winPrinter << "'" << endl; | |
127 | ||
128 | try | |
129 | { | |
130 | Printer pr(winPrinter, debugFlag); | |
a18d2869 CW |
131 | pr.setRawFlag(rawFlag); |
132 | ||
21006e63 CW |
133 | if (optind >= argc) |
134 | pr.print(cin, "stdin"); | |
135 | else | |
136 | { | |
137 | for (int ii = optind; ii < argc; ++ii) | |
138 | { | |
139 | ifstream in (argv[ii]); | |
140 | if (!in) | |
141 | cerr << progName << ": can't open '" << argv[ii] << "' for input." | |
142 | << endl; | |
143 | else | |
144 | pr.print(in, argv[ii]); | |
145 | } | |
146 | } | |
147 | pr.close(); | |
148 | } | |
149 | catch (const PrinterException & ex) | |
150 | { | |
151 | cerr << progName << ": printer error: " << ex << endl; | |
152 | return 1; | |
153 | } | |
154 | ||
155 | return 0; | |
156 | } |