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