Debian Codegraph @

ext/gsmsiexfer.cc

raw symbols SCIP
1 // *************************************************************************
2 // * GSM TA/ME library
3 // *
4 // * File: gsmsiexfer.cc
5 // *
6 // * Purpose: Siemens ME file transfer program
7 // *
8 // * Author: Christian W. Zuckschwerdt <[email protected]>
9 // *
10 // * Created: 2001-12-16
11 // *************************************************************************
12
13 #ifdef HAVE_CONFIG_H
14 #include <gsm_config.h>
15 #endif
16 #include <gsmlib/gsm_nls.h>
17 #include <string>
18 #ifdef WIN32
19 #include <gsmlib/gsm_win32_serial.h>
20 #else
21 #include <gsmlib/gsm_unix_serial.h>
22 #include <unistd.h>
23 #endif
24 #if defined(HAVE_GETOPT_LONG) || defined(WIN32)
25 #include <getopt.h>
26 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <gsmlib/gsm_me_ta.h>
31 #include <gsm_sie_me.h>
32 #include <gsmlib/gsm_util.h>
33 #include <gsmlib/gsm_sorted_phonebook.h>
34 #include <iostream>
35
36 using namespace std;
37 using namespace gsmlib;
38
39 #ifdef HAVE_GETOPT_LONG
40 static struct option longOpts[] =
41 {
42 {"xonxoff", no_argument, (int*)NULL, 'X'},
43 {"init", required_argument, (int*)NULL, 'I'},
44 {"destination", required_argument, (int*)NULL, 'd'},
45 {"source", required_argument, (int*)NULL, 's'},
46 {"baudrate", required_argument, (int*)NULL, 'b'},
47 {"type", required_argument, (int*)NULL, 't'},
48 {"subtype", required_argument, (int*)NULL, 'i'},
49 {"help", no_argument, (int*)NULL, 'h'},
50 {"version", no_argument, (int*)NULL, 'v'},
51 {"verbose", no_argument, (int*)NULL, 'V'},
52 {(char*)NULL, 0, (int*)NULL, 0}
53 };
54 #else
55 #define getopt_long(argc, argv, options, longopts, indexptr) \
56 getopt(argc, argv, options)
57 #endif
58
59 // I f*ck up this file IO thing.
60
61 // read binary object from stdin
62 BinaryObject readBinaryFile(istream &ifs, string filename)
63 {
64 size_t size = 10000; // Bad coder, no biscuits!
65 BinaryObject bnr;
66 bnr._data = new unsigned char[size];
67 ifs.read((char*)bnr._data, size);
68 bnr._size = ifs.gcount();
69 return bnr;
70 }
71
72 // read binary object from file
73 BinaryObject readFile(string filename)
74 {
75 // open the file
76 ifstream ifs(filename.c_str());
77 if (ifs.bad())
78 throw GsmException(stringPrintf(_("cannot open file '%s'"),
79 filename.c_str()),
80 OSError);
81 // and read the file
82 return readBinaryFile(ifs, filename);
83 }
84
85 // read binary object from stdin
86 BinaryObject readFile(bool fromStdin)
87 {
88 // read from stdin
89 // if (fromStdin)
90 return readBinaryFile(cin, (string)_("<STDIN>"));
91 }
92
93 // write binary object to file
94 void writeBinaryFile(ostream &ofs, string filename, BinaryObject bnw)
95 {
96 // well just dump the data
97 ofs.write((char*)bnw._data, bnw._size);
98 }
99
100 // write binary object
101 void writeFile(string filename, BinaryObject obj)
102 {
103 // open the file
104 ofstream ofs(filename.c_str());
105 if (ofs.bad())
106 throw GsmException(stringPrintf(_("cannot open file '%s'"),
107 filename.c_str()),
108 OSError);
109 // and read the file
110 writeBinaryFile(ofs, filename, obj);
111 }
112
113 // write binary object to stdout
114 void writeFile(bool toStdout, BinaryObject obj)
115 {
116 // if (toStdout)
117 writeBinaryFile(cout, (string)_("<STDIN>"), obj);
118 }
119
120 // *** main program
121
122 int main(int argc, char *argv[])
123 {
124 try
125 {
126 // handle command line options
127 string destination;
128 string source;
129 string baudrate;
130 string type;
131 string subtype;
132 int subtypeN;
133 bool verbose;
134 string initString = DEFAULT_INIT_STRING;
135 bool swHandshake = false;
136 Ref<SieMe> sourceMeTa, destMeTa;
137 BinaryObject sourceObject;
138
139 int opt;
140 int dummy;
141 while((opt = getopt_long(argc, argv, "XI:s:d:b:hvVt:i:", longOpts,
142 &dummy))
143 != -1)
144 switch (opt)
145 {
146 case 'X':
147 swHandshake = true;
148 break;
149 case 'I':
150 initString = optarg;
151 break;
152 case 'V':
153 verbose = true;
154 break;
155 case 't':
156 type = optarg;
157 break;
158 case 'i':
159 subtype = optarg;
160 subtypeN = atoi(optarg);
161 break;
162 case 'd':
163 destination = optarg;
164 break;
165 case 's':
166 source = optarg;
167 break;
168 case 'b':
169 baudrate = optarg;
170 break;
171 case 'v':
172 cerr << argv[0] << stringPrintf(_(": version %s [compiled %s]"),
173 VERSION, __DATE__) << endl;
174 exit(0);
175 break;
176 case 'h':
177 cerr << argv[0] << _(": [-b baudrate][-c][-d device or file][-h]"
178 "[-I init string]\n"
179 " [-p phonebook name][-s device or file]"
180 "[-t charset][-v]"
181 "[-V][-y][-X]") << endl
182 << endl
183 << _(" -b, --baudrate baudrate to use for device "
184 "(default: 38400)")
185 << endl
186 << _(" -c, --copy copy source entries to destination")
187 << endl
188 << _(" -d, --destination sets the destination device to "
189 "connect \n"
190 " to, or the file to write") << endl
191 << _(" -D, --destination-backend sets the destination backend")
192 << endl
193 << _(" -h, --help prints this message") << endl
194 << _(" -i, --index takes index positions into account")
195 << endl
196 << _(" -I, --init device AT init sequence") << endl
197 << _(" -p, --phonebook name of phonebook to use") << endl
198 << _(" -s, --source sets the source device to connect to,\n"
199 " or the file to read") << endl
200 << _(" -t, --charset sets the character set to use for\n"
201 " phonebook entries") << endl
202 << _(" -S, --source-backend sets the source backend")
203 << endl
204 << _(" -v, --version prints version and exits") << endl
205 << _(" -V, --verbose print detailed progress messages")
206 << endl
207 << _(" -X, --xonxoff switch on software handshake") << endl
208 << _(" -y, --synchronize synchronize destination with source\n"
209 " entries (destination is overwritten)\n"
210 " (see gsmpb(1) for details)")
211 << endl << endl;
212 exit(0);
213 break;
214 case '?':
215 throw GsmException(_("unknown option"), ParameterError);
216 break;
217 }
218
219 // check if all parameters all present
220 if (destination == "" || source == "")
221 throw GsmException(_("both source and destination must be given"),
222 ParameterError);
223
224 // start accessing source mobile phone or file
225 if (source == "-")
226 sourceObject = readFile(true);
227 else if (isFile(source))
228 sourceObject = readFile(source);
229 else
230 {
231 if (type == "")
232 throw GsmException(_("type be given"), ParameterError);
233 if (subtype == "")
234 throw GsmException(_("subtype be given"), ParameterError);
235
236 sourceMeTa = new SieMe(new
237 #ifdef WIN32
238 Win32SerialPort
239 #else
240 UnixSerialPort
241 #endif
242 (source,
243 baudrate == "" ? DEFAULT_BAUD_RATE :
244 baudRateStrToSpeed(baudrate), initString,
245 swHandshake));
246 sourceObject = sourceMeTa->getBinary(type, subtypeN);
247 }
248
249 // make sure destination.c_str file exists
250 if (destination != "")
251 {
252 try
253 {
254 ofstream f(destination.c_str(), ios::out | ios::app);
255 }
256 catch (exception)
257 {
258 }
259 }
260
261 // start accessing destination mobile phone or file
262 if (destination == "-")
263 writeFile(true, sourceObject);
264 else if (isFile(destination))
265 writeFile(destination, sourceObject);
266 else
267 {
268 if (type == "")
269 throw GsmException(_("type must be given"), ParameterError);
270 if (subtype == "")
271 throw GsmException(_("subtype must be given"), ParameterError);
272
273 destMeTa = new SieMe(new
274 #ifdef WIN32
275 Win32SerialPort
276 #else
277 UnixSerialPort
278 #endif
279 (destination,
280 baudrate == "" ? DEFAULT_BAUD_RATE :
281 baudRateStrToSpeed(baudrate), initString,
282 swHandshake));
283 destMeTa->setBinary(type, subtypeN, sourceObject);
284 }
285 }
286 catch (GsmException &ge)
287 {
288 cerr << argv[0] << _("[ERROR]: ") << ge.what() << endl;
289 return 1;
290 }
291 return 0;
292 }