Translation update done using Pootle.
[gammu.git] / gammu / common.c
blob4ac4304063e7785564d0d20f7e7e837d52973f2c
1 #include <string.h>
2 #include <stdarg.h>
3 #include <signal.h>
4 #include <stdlib.h>
6 #include "../helper/locales.h"
8 #include "common.h"
9 #include "../helper/formats.h"
10 #include "../helper/printing.h"
11 #include "../helper/cmdline.h"
13 #ifdef CURL_FOUND
14 #include <curl/curl.h>
15 #endif
17 GSM_StateMachine *gsm = NULL;
18 INI_Section *cfg = NULL;
20 gboolean always_answer_yes = FALSE;
21 gboolean always_answer_no = FALSE;
22 volatile gboolean gshutdown = FALSE;
23 gboolean batch = FALSE;
24 gboolean batchConn = FALSE;
26 void interrupt(int sign)
28 signal(sign, SIG_IGN);
29 GSM_AbortOperation(gsm);
30 gshutdown = TRUE;
33 void PrintSecurityStatus(void)
35 GSM_SecurityCodeType Status;
36 GSM_Error error;
38 error = GSM_GetSecurityStatus(gsm, &Status);
39 Print_Error(error);
40 switch (Status) {
41 case SEC_SecurityCode:
42 printf("%s\n", _("Waiting for Security Code."));
43 break;
44 case SEC_Pin:
45 printf("%s\n", _("Waiting for PIN."));
46 break;
47 case SEC_Pin2:
48 printf("%s\n", _("Waiting for PIN2."));
49 break;
50 case SEC_Puk:
51 printf("%s\n", _("Waiting for PUK."));
52 break;
53 case SEC_Puk2:
54 printf("%s\n", _("Waiting for PUK2."));
55 break;
56 case SEC_Phone:
57 printf("%s\n", _("Waiting for phone code."));
58 break;
59 case SEC_Network:
60 printf("%s\n", _("Waiting for network code."));
61 break;
62 case SEC_None:
63 printf("%s\n", _("Nothing to enter."));
64 break;
65 #ifndef CHECK_CASES
66 default:
67 printf("%s\n", _("Unknown security status."));
68 #endif
72 const char *GetMonthName(const int month)
74 static char result[100];
76 switch (month) {
77 case 1:
78 strcpy(result, _("January"));
79 break;
80 case 2:
81 strcpy(result, _("February"));
82 break;
83 case 3:
84 strcpy(result, _("March"));
85 break;
86 case 4:
87 strcpy(result, _("April"));
88 break;
89 case 5:
90 strcpy(result, _("May"));
91 break;
92 case 6:
93 strcpy(result, _("June"));
94 break;
95 case 7:
96 strcpy(result, _("July"));
97 break;
98 case 8:
99 strcpy(result, _("August"));
100 break;
101 case 9:
102 strcpy(result, _("September"));
103 break;
104 case 10:
105 strcpy(result, _("October"));
106 break;
107 case 11:
108 strcpy(result, _("November"));
109 break;
110 case 12:
111 strcpy(result, _("December"));
112 break;
113 default:
114 strcpy(result, _("Bad month!"));
115 break;
117 return result;
120 const char *GetDayName(const int day)
122 static char result[100];
124 switch (day) {
125 case 1:
126 strcpy(result, _("Monday"));
127 break;
128 case 2:
129 strcpy(result, _("Tuesday"));
130 break;
131 case 3:
132 strcpy(result, _("Wednesday"));
133 break;
134 case 4:
135 strcpy(result, _("Thursday"));
136 break;
137 case 5:
138 strcpy(result, _("Friday"));
139 break;
140 case 6:
141 strcpy(result, _("Saturday"));
142 break;
143 case 7:
144 strcpy(result, _("Sunday"));
145 break;
146 default:
147 strcpy(result, _("Bad day!"));
148 break;
150 return result;
153 NORETURN
154 void Terminate(int code)
156 Cleanup();
157 exit(code);
160 void Cleanup(void)
162 GSM_Debug_Info *di;
164 if (gsm != NULL) {
165 /* Disconnect from phone */
166 if (GSM_IsConnected(gsm)) {
167 GSM_TerminateConnection(gsm);
170 /* Free state machine */
171 GSM_FreeStateMachine(gsm);
173 gsm = NULL;
175 /* Close debug output if opened */
176 di = GSM_GetGlobalDebug();
177 GSM_SetDebugFileDescriptor(NULL, FALSE, di);
179 #ifdef CURL_FOUND
180 /* Free CURL memory */
181 curl_global_cleanup();
182 #endif
184 INI_Free(cfg);
185 cfg = NULL;
188 void Print_Error(GSM_Error error)
190 if (error != ERR_NONE) {
191 printf("%s\n", GSM_ErrorString(error));
192 /* Check for security error */
193 if (error == ERR_SECURITYERROR) {
194 printf(LISTFORMAT, _("Security status"));
195 PrintSecurityStatus();
198 Terminate(100 + error);
203 * Callback from CURL to get data.
205 size_t write_mem(void *ptr, size_t size, size_t nmemb, void *data) {
206 size_t realsize = size * nmemb;
207 GSM_File *file = (GSM_File *)data;
209 file->Buffer = realloc(file->Buffer,file->Used + realsize + 1);
211 if (file->Buffer) {
212 memcpy(file->Buffer + file->Used, ptr, realsize);
213 file->Used += realsize;
214 file->Buffer[file->Used] = 0;
215 return realsize;
217 return 0;
221 * Downloads file from arbitrary URL.
223 * \param url URL to download.
224 * \param file Storage for data.
226 * \returns ERR_NONE on success.
228 GSM_Error GSM_ReadHTTPFile(const char *url, GSM_File *file)
230 #ifdef CURL_FOUND
231 CURL *dl_handle = NULL;
232 CURLcode result;
234 dl_handle = curl_easy_init();
235 if (dl_handle == NULL) return FALSE;
237 curl_easy_setopt(dl_handle, CURLOPT_URL, url);
238 curl_easy_setopt(dl_handle, CURLOPT_USERAGENT, "Gammu/" GAMMU_VERSION);
239 curl_easy_setopt(dl_handle, CURLOPT_WRITEFUNCTION, write_mem);
240 curl_easy_setopt(dl_handle, CURLOPT_WRITEDATA, file);
241 curl_easy_setopt(dl_handle, CURLOPT_FOLLOWLOCATION, 1);
242 curl_easy_setopt(dl_handle, CURLOPT_MAXREDIRS, 10);
243 #if 0
244 /* Enable verbose outpuf from CURL */
245 curl_easy_setopt(dl_handle, CURLOPT_VERBOSE, 1);
246 #endif
248 result = curl_easy_perform(dl_handle);
250 curl_easy_cleanup(dl_handle);
252 switch (result) {
253 case CURLE_OK:
254 return ERR_NONE;
255 case CURLE_URL_MALFORMAT:
256 return ERR_BUG;
257 case CURLE_COULDNT_CONNECT:
258 case CURLE_RECV_ERROR:
259 return ERR_COULDNT_CONNECT;
260 case CURLE_COULDNT_RESOLVE_HOST:
261 return ERR_COULDNT_RESOLVE;
262 default:
263 printf_err("Unknown curl error: %d\n", result);
264 return ERR_UNKNOWN;
266 #else
267 return ERR_DISABLED;
268 #endif
272 * Initiates connection to phone.
274 * \param checkerror Whether we should check for error.
276 void GSM_Init(gboolean checkerror)
278 GSM_Error error;
280 /* No checks on existing batch mode */
281 if (batch && batchConn)
282 return;
284 /* Connect to phone */
285 error = GSM_InitConnection(gsm, 1);
286 if (checkerror)
287 Print_Error(error);
289 /* Check for batch mode */
290 if (batch) {
291 if (error == ERR_NONE) {
292 batchConn = TRUE;
297 void GSM_Terminate(void)
299 GSM_Error error;
301 if (!batch) {
302 error = GSM_TerminateConnection(gsm);
303 Print_Error(error);
307 void GetStartStop(int *start, int *stop, int num, int argc, char *argv[])
309 int tmp;
311 if (argc <= num) {
312 printf_err("%s\n", _("More parameters required!"));
313 Terminate(2);
316 *start = GetInt(argv[num]);
317 if (*start == 0) {
318 printf_err("%s\n", _("Please enumerate locations from 1"));
319 Terminate(2);
322 if (stop != NULL) {
323 *stop = *start;
324 if (argc >= num + 2)
325 *stop = GetInt(argv[num + 1]);
326 if (*stop == 0) {
327 printf_err("%s\n",
328 _("Please enumerate locations from 1"));
329 Terminate(2);
331 if (*stop < *start) {
332 printf_warn("%s\n",
333 _("Swapping start and end location"));
334 tmp = *stop;
335 *stop = *start;
336 *start = tmp;
341 PRINTF_STYLE(1, 2)
342 gboolean answer_yes(const char *format, ...)
344 int len;
345 char ans[99];
346 char buffer[1000];
347 va_list ap;
349 va_start(ap, format);
350 vsnprintf(buffer, sizeof(buffer) - 1, format, ap);
351 va_end(ap);
353 while (1) {
354 fprintf(stderr, "%s (%s/%s/%s/%s/%s) ", buffer,
355 _("yes"), _("no"),
356 _("ALL"), _("ONLY"), _("NONE"));
357 if (always_answer_yes) {
358 fprintf(stderr, "%s\n", _("YES (always)"));
359 return TRUE;
361 if (always_answer_no) {
362 fprintf(stderr, "%s\n", _("NO (always)"));
363 return FALSE;
365 len = GetLine(stdin, ans, 99);
366 if (len == -1)
367 Terminate(3);
368 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
369 if (!strcmp(ans, _("NONE"))) {
370 always_answer_no = TRUE;
371 return FALSE;
373 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
374 if (!strcmp(ans, _("ONLY"))) {
375 always_answer_no = TRUE;
376 return TRUE;
378 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
379 if (!strcmp(ans, _("ALL"))) {
380 always_answer_yes = TRUE;
381 return TRUE;
383 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
384 if (strcasecmp(ans, _("yes")) == 0)
385 return TRUE;
386 /* l10n: Answer to (yes/no/ALL/ONLY/NONE) question */
387 if (strcasecmp(ans, _("no")) == 0)
388 return FALSE;
392 /* How should editor hadle tabs in this file? Add editor commands here.
393 * vim: noexpandtab sw=8 ts=8 sts=8: