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