]>
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 = | |
807fb049 | 51 | " [-h] [-D] [-d device] [-l] [-P device]\n" |
21006e63 CW |
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" | |
807fb049 CW |
58 | " -l prevent <LF> -> <CR><LF> processing. By default, standalone" |
59 | " <LF> characters are converted to <CR><LF>." | |
21006e63 CW |
60 | " -P device spools to the specified device.\n" |
61 | "\n" | |
62 | "Notes:\n" | |
63 | "\n" | |
64 | "-d and -P are aliases of each other and perform the same function.\n" | |
65 | "Device names may take the form of DOS devices (e.g., lpt1:) if the printer\n" | |
66 | "is connected locally. Network printers can be accessed using the form\n" | |
67 | "'\\\\server\\printer_name'. Forward slashes can be used as well, e.g.,\n" | |
68 | "'//server/printer_name'.\n" | |
69 | "\n" | |
70 | "The environment variable PRINTER can be used to set the default printer\n" | |
71 | "device.\n"; | |
72 | ||
73 | void usage(const string & msg = "") | |
74 | { | |
75 | if (msg != "") | |
76 | cerr << progName << ": " << msg << endl << endl; | |
77 | ||
78 | cerr << "Usage: " << progName << usageMessage; | |
79 | exit(1); | |
80 | } | |
81 | ||
82 | int main(int argc, char *argv[]) | |
83 | { | |
84 | progName = getProgramName(argv[0]); | |
85 | string printerName = ""; | |
86 | ||
87 | // Is the printer specified in the environment? | |
88 | const char *p = getenv("PRINTER"); | |
89 | if (p != 0) | |
90 | printerName = p; | |
91 | ||
92 | bool debugFlag = false; | |
a18d2869 | 93 | bool rawFlag = false; |
21006e63 CW |
94 | |
95 | int optionChar; | |
a18d2869 | 96 | while ((optionChar = getopt(argc, argv, ":Dd:hlP:")) != EOF) |
21006e63 CW |
97 | { |
98 | switch (optionChar) | |
99 | { | |
100 | case 'h': | |
101 | // accept for compatibility | |
102 | break; | |
103 | case 'D': | |
104 | debugFlag = true; | |
105 | break; | |
106 | case 'd': | |
107 | case 'P': | |
108 | printerName = optarg; | |
109 | break; | |
a18d2869 CW |
110 | case 'l': |
111 | rawFlag = true; | |
112 | break; | |
21006e63 CW |
113 | default: |
114 | usage(string("unknown option: -") + char(optionChar)); | |
115 | } | |
116 | } | |
117 | ||
118 | // Can't proceed without a printer name. | |
119 | if (printerName == "") | |
120 | { | |
121 | cerr << progName << ": no printer specified" << endl; | |
122 | return 1; | |
123 | } | |
124 | ||
125 | char winPrinter[MAX_PATH]; | |
126 | cygwin_conv_to_win32_path(printerName.c_str(), winPrinter); | |
127 | if (debugFlag) | |
128 | cout << "Windows printer name = '" << winPrinter << "'" << endl; | |
129 | ||
130 | try | |
131 | { | |
132 | Printer pr(winPrinter, debugFlag); | |
a18d2869 CW |
133 | pr.setRawFlag(rawFlag); |
134 | ||
21006e63 CW |
135 | if (optind >= argc) |
136 | pr.print(cin, "stdin"); | |
137 | else | |
138 | { | |
139 | for (int ii = optind; ii < argc; ++ii) | |
140 | { | |
141 | ifstream in (argv[ii]); | |
142 | if (!in) | |
143 | cerr << progName << ": can't open '" << argv[ii] << "' for input." | |
144 | << endl; | |
145 | else | |
146 | pr.print(in, argv[ii]); | |
147 | } | |
148 | } | |
149 | pr.close(); | |
150 | } | |
151 | catch (const PrinterException & ex) | |
152 | { | |
153 | cerr << progName << ": printer error: " << ex << endl; | |
154 | return 1; | |
155 | } | |
156 | ||
157 | return 0; | |
158 | } |