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