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