better script using python
[tridge/junkcode.git] / roglib.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <termios.h>
7
8 #define FN
9 #define MAX_TRACKS 100
10
11 typedef char str[256];
12
13 int tty_fd=-1;
14 str device="/dev/gps";
15
16 char in_buff[1024];
17 int in_size=1024;
18 char *in_ptr=in_buff;
19 int in_cnt=0;
20
21
22 char out_buff[1024];
23 int out_size=1024;
24 char *out_ptr=in_buff;
25 int out_cnt=0;
26
27 extern FILE *dbf;
28
29 char *tracks[MAX_TRACKS];
30 int num_tracks=0;
31
32
33 FN void tty_puts(char *s);
34 FN void tty_close(void);
35
36
37 void dbg(char *s)
38 {
39 fprintf(dbf,"Debug: %s\n",s);
40 fflush(dbf);
41 }
42
43
44 void delay(int t)
45 {
46 usleep(10000*t);
47 }
48
49 int receive(char *fname)
50 {
51   str command;
52   fprintf(stderr,"%c",7);
53   fflush(stderr);
54   while (1);
55   sprintf(command,"rx %s < %s > %s",fname,device,device);
56   return(system(command));
57 }
58
59 void exittelix(int retcode, int hangup)
60 {
61   if (hangup)
62     {
63       tty_close();
64     }
65   exit(retcode);
66 }
67
68 /*******************************************************************
69 add a track to the set of current tracks
70 ********************************************************************/
71 FN int track(char *s,int tracknum)
72 {
73   int i;
74   
75   for (i=0;i<num_tracks;i++)
76     if (!tracks[i])
77       {
78         tracks[i] = (char *)malloc(strlen(s)+1);
79         strcpy(tracks[i],s);
80         dbg("reusing track");
81         return(i);
82       }
83   
84   if (num_tracks == MAX_TRACKS) 
85     {
86       dbg("no more tracks");
87       return(-1);
88     }
89   tracks[num_tracks] = (char *)malloc(strlen(s)+1);
90   strcpy(tracks[num_tracks],s);
91   
92   dbg("added track");
93   
94   num_tracks++;
95   return(num_tracks - 1);
96 }
97
98 /*******************************************************************
99 check the list of tracks and return the index of a found track from 
100 "file"
101 ********************************************************************/
102 FN int track_hit(int tracknum)
103 {
104   char s[1024];
105   int count=0;
106   int i;
107   int tries=30;
108
109   memset(s,0,1024);
110
111   while (tries)
112     {
113       int got = tty_gets(&s[count],1023-count); 
114       count += got;
115       if (got == 0) tries--;
116
117       for (i=0;i<num_tracks;i++)
118         if (tracks[i])
119           {
120             if ((strstr(s,tracks[i])!=NULL)) 
121               {
122                 fprintf(dbf,"Got: [%s]\n",s);
123                 fflush(dbf);
124                 return(i);
125               }
126           }
127       if (count > 512)
128         {
129           dbg("wrapping");
130           memcpy(s,s+(count-80),80);
131           count = 80;
132           memset(s+count,0,1024-count);
133         }
134     }
135   return(-1);
136 }
137
138
139 /*******************************************************************
140 free a track
141 ********************************************************************/
142 FN void track_free(int tracknum)
143 {
144   fprintf(dbf,"freeing track %d\n",tracknum);
145   if (tracks[tracknum]) free(tracks[tracknum]);
146   tracks[tracknum] = NULL;
147 }
148
149
150
151
152 void set_terminal(char *term)
153 {
154 }
155
156 void set_port(int port)
157 {
158   sprintf(device,"/dev/cua%d",port-1);
159 }
160
161 int tty_open(char *device)
162 {
163   int i,speed;
164   int fd;
165   str command;
166   struct termios tty;
167
168   sprintf(command,"stty 0:1805:cbb:0:3:1c:7f:15:4:0:1:0:11:13:1a:0:12:f:17:16:0 < %s",device);
169   system(command);
170   
171   fd = open(device,O_RDWR);
172   
173   ioctl(fd,TCGETS,&tty);
174   tty.c_cflag &= ~CBAUD;
175   tty.c_cflag |= B19200;
176   ioctl(fd,TCSETS,&tty);
177
178   ioctl(fd,FIONBIO);
179
180   return(fd);
181 }
182
183
184 void terminal(void)
185 {
186   dbg("terminal");
187   if (tty_fd<0)
188     {
189       tty_fd = tty_open(device);
190       if (tty_fd<0)
191         {
192           fprintf(stderr,"Couldn't open port %s\n",device);
193           exittelix(1,1);       
194         }
195     }
196 }
197
198 void cputs(char *s)
199 {
200   fprintf(dbf,"Sending: [%s]\n",s);
201   fflush(dbf);
202   tty_puts(s);
203 }
204
205
206 int timeout=0;
207
208 /*******************************************************************
209 signal this fn if something times out
210 ********************************************************************/
211 FN void TimeOut(int sig)
212 {
213   (void) sig;
214   timeout = 1;
215   dbg("timed out");
216 }
217
218
219 /*******************************************************************
220 get a string from the terminal
221 ********************************************************************/
222 FN int tty_gets(char *s,int n)
223 {
224   char c, c2, *p;
225   int howlong=1;
226   int count=0;
227   void (*oldsig)(int);
228
229   oldsig = signal(SIGALRM, TimeOut);
230   (void) alarm(howlong);
231
232   timeout = 0;
233   while(!timeout && count<n) {
234         c = (char) tty_getc();
235         if (c == -1) continue;
236         if (c==0) c=1;
237         *s++ = c;
238         count++;
239   }
240   (void) alarm(0);
241   (void) signal(SIGALRM, oldsig);
242   return(count);
243 }
244
245
246
247 /*******************************************************************
248 Read one character (byte) from the TTY link.
249 ********************************************************************/
250 FN int tty_getc(void)
251 {
252   int s;
253
254   if (in_cnt == 0) {
255     s = read(tty_fd, in_buff, in_size);
256     if (s < 0) return(-1);
257     in_cnt = s;
258     in_ptr = in_buff;
259   }
260
261   if (in_cnt < 0) {
262     dbg(" tty_getc: I/O error.\n");
263     return(-1);
264   }
265   
266   s = (int) *in_ptr;
267   s &= 0xFF;
268   in_ptr++;
269   in_cnt--;
270   putchar(s);
271   return(s);
272 }
273
274
275 /*******************************************************************
276 Write one character (byte) to the TTY link.
277 ********************************************************************/
278 FN int tty_putc(int c)
279 {
280   int s;
281   
282   if ((out_cnt == out_size) || (c == -1)) {
283     s = write(tty_fd, out_buff, out_cnt);
284     out_cnt = 0;
285     out_ptr = out_buff;
286     if (s < 0) return(-1);
287   }
288   
289   if (c != -1) {
290     *out_ptr = (char) c;
291     out_ptr++;
292     out_cnt++;
293   }
294   
295   return(0);
296 }
297
298
299
300 /*******************************************************************
301 Output a string of characters to the TTY link.
302 ********************************************************************/
303 FN void tty_puts(char *s)
304 {
305   while(*s != '\0') tty_putc((int) *s++);
306   tty_putc(-1); /* flush */
307 }
308
309 /*******************************************************************
310 close the tty
311 ********************************************************************/
312 FN void tty_close(void)
313 {
314   if (tty_fd>=0) close(tty_fd);
315   tty_fd = -1;
316 }