Update: orig2001_11_27_05_17 -> orig2001_11_27_22_58
[gnokii.git] / common / misc.c
1 /*
2
3   G N O K I I
4
5   A Linux/Unix toolset and driver for Nokia mobile phones.
6
7   Copyright (C) 1999, 2000 Hugh Blemings & Pavel Janík ml.
8
9   Released under the terms of the GNU GPL, see file COPYING for more details.
10
11   $Id$
12   
13   $Log$
14   Revision 1.1.1.1.12.1  2001/11/27 21:35:51  short
15   Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
16
17   Revision 1.1.1.1.8.1  2001/11/27 21:30:44  short
18   Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
19
20   Revision 1.1.1.1.2.1  2001/11/27 04:37:59  short
21   Update: orig2001_11_25_22_56 -> orig2001_11_27_05_17
22
23   Revision 1.1.1.2  2001/11/27 04:19:24  short
24   :pserver:cvs@pserver.samba.org:/cvsroot - gnokii - Tue Nov 27 05:17 CET 2001
25
26   Revision 1.20  2001/11/26 18:06:08  pkot
27   Checking for *printf functions, N_(x) for localization, generic ARRAY_LEN, SAFE_STRNCPY, G_GNUC_PRINTF (Jan Kratochvil)
28
29   Revision 1.19  2001/11/22 17:56:53  pkot
30   smslib update. sms sending
31
32   Revision 1.18  2001/09/09 21:45:49  machek
33   Cleanups from Ladislav Michl <ladis@psi.cz>:
34
35   *) do *not* internationalize debug messages
36
37   *) some whitespace fixes, do not use //
38
39   *) break is unneccessary after return
40
41   Revision 1.17  2001/08/09 12:34:34  pkot
42   3330 and 6250 support - I have no idea if it does work (mygnokii)
43
44   Revision 1.16  2001/03/21 23:36:04  chris
45   Added the statemachine
46   This will break gnokii --identify and --monitor except for 6210/7110
47
48   Revision 1.15  2001/03/06 10:38:52  machek
49   Dancall models added to the global list.
50
51   Revision 1.14  2001/02/06 13:55:23  pkot
52   Enabled authentication in 51xx models
53
54   Revision 1.13  2001/02/02 08:09:56  ja
55   New dialogs for 6210/7110 in xgnokii. Fixed the smsd for new capabilty code.
56
57
58 */
59
60 #include <string.h>
61 #include <stdlib.h>
62 #include "misc.h"
63
64 int GetLine(FILE *File, char *Line, int count)
65 {
66         char *ptr;
67
68         if (fgets(Line, count, File)) {
69                 ptr = Line + strlen(Line) - 1;
70
71                 while ( (*ptr == '\n' || *ptr == '\r') && ptr >= Line)
72                         *ptr-- = '\0';
73
74                 return strlen(Line);
75         }
76         else
77                 return 0;
78 }
79
80 static PhoneModel models[] = {
81         {NULL,    "", 0 },
82         {"2711",  "?????", PM_SMS },            /* Dancall */
83         {"2731",  "?????", PM_SMS },
84         {"1611",  "NHE-5", 0 },
85         {"2110i", "NHE-4", PM_SMS },
86         {"2148i", "NHK-4", 0 },
87         {"3110",  "0310" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-8 */
88         {"3210",  "NSE-8", PM_SMS | PM_DTMF },
89         {"3210",  "NSE-9", PM_SMS | PM_DTMF },
90         {"3310",  "NHM-5", PM_SMS | PM_DTMF },
91         {"3330",  "NHM-6", PM_SMS | PM_DTMF },
92         {"3810",  "0305" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-9 */
93         {"5110",  "NSE-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
94         {"5130",  "NSK-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
95         {"5160",  "NSW-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
96         {"5190",  "NSB-1", PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
97         {"6110",  "NSE-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
98         {"6120",  "NSC-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
99         {"6130",  "NSK-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
100         {"6150",  "NSM-1", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
101         {"616x",  "NSW-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
102         {"6185",  "NSD-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
103         {"6190",  "NSB-3", PM_CALLERGROUP | PM_CALENDAR | PM_NETMONITOR | PM_KEYBOARD | PM_SMS | PM_DTMF | PM_DATA | PM_SPEEDDIAL | PM_AUTHENTICATION },
104         {"6210",  "NPE-3", PM_CALLERGROUP | PM_CALENDAR | PM_EXTPBK | PM_SMS},
105         {"6250",  "NHM-3", PM_CALLERGROUP | PM_CALENDAR | PM_EXTPBK },
106         {"7110",  "NSE-5", PM_CALLERGROUP | PM_SPEEDDIAL | PM_EXTPBK },
107         {"8810",  "NSE-6", PM_SMS | PM_DTMF | PM_DATA },
108         {"8110i", "0423",  PM_SMS | PM_DTMF | PM_DATA }, /* Guess for NHE-6 */
109         {"8110",  "0423" , PM_SMS | PM_DTMF | PM_DATA }, /* NHE-6BX */
110         {"9000i", "RAE-4", 0 },
111         {"9110",  "RAE-2", 0 },
112         {"550",   "THF-10", 0 },
113         {"540",   "THF-11", 0 },
114         {"650",   "THF-12", 0 },
115         {"640",   "THF-13", 0 },
116         {NULL,    NULL, 0 }
117 };
118
119 PhoneModel *GetPhoneModel (const char *num)
120 {
121         register int i = 0;
122
123         while (models[i].number != NULL) {
124                 if (strcmp (num, models[i].number) == 0) {
125                         dprintf("Found model\n");
126                         return (&models[i]);
127                 }
128                 else {
129                         dprintf("comparing %s and %s\n", num, models[i].number);
130                 }
131                 i++;
132         }
133
134         return (&models[0]);
135 }
136
137 inline char *GetModel (const char *num)
138 {
139         return (GetPhoneModel(num)->model);
140 }
141
142 #ifndef HAVE_VASPRINTF
143 /* Adapted from snprintf(3) man page: */
144 int gvasprintf(char **destp, const char *fmt, va_list ap)
145 {
146         int n, size = 0x100;
147         char *p, *pnew;
148
149         if (!(p = malloc(size))) {
150                 *destp = NULL;
151                 return(-1);
152         }
153         for (;;) {
154                 /* Try to print in the allocated space. */
155                 n = gvsprintf(p, size, fmt, ap);
156                 /* If that worked, return the string. */
157                 if (n > -1 && n < size) {
158                         *destp = p;
159                         return(n);
160                 }
161                 /* Else try again with more space. */
162                 if (n > -1)     /* glibc 2.1 */
163                         size = n + 1;   /* precisely what is needed */
164                 else            /* glibc 2.0 */
165                         size *= 2;      /* twice the old size */
166                 if (!(pnew = realloc(p, size))) {
167                         free(p);
168                         *destp = NULL;
169                         return(-1);
170                 }
171                 p = pnew;
172         }
173 }
174 #endif
175
176 #ifndef HAVE_ASPRINTF
177 int gasprintf(char **destp, const char *fmt,...)
178 {
179         va_list ap;
180         int r;
181
182         va_start(ap,fmt);
183         r = gvasprintf(destp, fmt, ap);
184         va_end(ap);
185         return(r);
186 }
187 #endif