source: trunk/FACT++/dim/src/webDid/webDid.c@ 14937

Last change on this file since 14937 was 14937, checked in by tbretz, 12 years ago
Added in v20r5
File size: 37.1 KB
Line 
1#include <stdio.h>
2#include <ctype.h>
3#include <time.h>
4#include <dim.h>
5#include <dic.h>
6#include <dis.h>
7
8extern int WebDID_Debug;
9
10typedef struct item{
11 struct item *next;
12 DNS_SERVER_INFO server;
13 DNS_SERVICE_INFO *service_ptr;
14 char name[MAX_NAME];
15 int match;
16 int busy;
17}SERVER;
18
19typedef struct nitem{
20 struct nitem *next;
21 SERVER *server_head;
22 char name[MAX_NAME];
23 int match;
24 int busy;
25}NODE;
26NODE *Node_head = (NODE *)0;
27
28typedef struct sitem{
29 struct sitem *next;
30 char name[MAX_NAME];
31 int sid;
32 void *buffer;
33 int buffer_size;
34 int size;
35 time_t timestamp;
36 void *buffer_str;
37 int buffer_str_size;
38 int str_size;
39 int conn_id;
40 time_t last_subscribed;
41 time_t last_updated;
42 int n_browsers;
43}CURR_SERVICE;
44CURR_SERVICE *Curr_service_head = (CURR_SERVICE *)0;
45
46typedef struct bitem{
47 struct bitem *next;
48 int id;
49 int subscribe;
50 time_t last_subscribed;
51 time_t last_updated;
52 time_t last_polled;
53 int conn_id;
54 int n_services;
55 int n_servers;
56 int n_nodes;
57 CURR_SERVICE *servicep;
58 char *JSONBuffer;
59 int JSONBufferSize;
60 char pattern[256];
61 char curr_command[MAX_NAME];
62 char *service_format_ptr;
63}BROWSER;
64BROWSER *Browser_head = (BROWSER *)0;
65
66char *JSONBuffer = 0;
67int JSONBufferSize = 0;
68char JSONHeader[256] = {'\0'};
69
70int First_time = 1;
71int Curr_view_opt = -1;
72char Curr_view_opt_par[80];
73char Curr_service_name[132];
74char Curr_service_format[256];
75int Curr_service_print_type = 0;
76int N_nodes = 0;
77int N_servers = 0;
78int N_services = 0;
79static char no_link = -1;
80int no_link_int = -1;
81FILE *fptr;
82
83char *Service_content_str;
84char *Curr_service_list = 0;
85char *Curr_client_list = 0;
86int Curr_service_id = 0;
87SERVER *Got_Service_List = 0;
88SERVER *Got_Client_List = 0;
89
90int Timer_q;
91char Title[128];
92
93int did_init(char *local_node, int dns_port)
94{
95 void update_servers();
96 char icon_title[128];
97 char dns_node[128];
98 int ret;
99
100 dim_init();
101 dic_disable_padding();
102 dis_disable_padding();
103
104 ret = dim_get_dns_node(dns_node);
105 if(!ret)
106 {
107 strcpy(dns_node, local_node);
108 dim_set_dns_node(dns_node);
109 }
110 dns_port = dic_get_dns_port();
111 if(dns_port != DNS_PORT)
112 {
113 sprintf(Title,"DIM DNS: %s:%d",dns_node,dns_port);
114 }
115 else
116 {
117 sprintf(Title,"DIM DNS: %s",dns_node);
118 }
119 sprintf(icon_title,"DID %s",dns_node);
120dim_print_date_time();
121printf("webDid Starting up on %s\n\t serving %s\n", local_node, Title);
122 Timer_q = dtq_create();
123 dic_info_service("DIS_DNS/SERVER_INFO",MONITORED,0,0,0,update_servers,0,
124 &no_link,1);
125 return 1;
126}
127
128SERVER *find_server(NODE *nodep, int pid)
129{
130 SERVER *servp;
131 DNS_SERVER_INFO *ptr;
132
133 servp = nodep->server_head;
134 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
135 {
136 ptr = &servp->server;
137 if(ptr->pid == pid)
138 {
139 return(servp);
140 }
141 }
142 return ((SERVER *)0);
143}
144
145NODE *find_node(char *node)
146{
147 NODE *nodep;
148
149 nodep = Node_head;
150 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
151 {
152 if(!strcmp(nodep->name,node))
153 {
154 return(nodep);
155 }
156 }
157 return ((NODE *)0);
158}
159
160int find_server_service_pattern(SERVER *servp, char *pattern)
161{
162 DNS_SERVICE_INFO *servicep;
163 int n_services, i;
164 int n_found = 0;
165
166 servicep = servp->service_ptr;
167 n_services = servp->server.n_services;
168 for(i = 0; i < n_services; i++)
169 {
170 if(strstr(servicep->name, pattern))
171 {
172 n_found++;
173 }
174 servicep++;
175 }
176 return(n_found);
177}
178
179int find_service_pattern(NODE *nodep, SERVER *servpp, char *pattern, int *n_servers)
180{
181 SERVER *servp;
182 int ret, n_found = 0;
183 int n_servers_found = 0;
184
185 if(!servpp)
186 {
187 servp = nodep->server_head;
188 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
189 {
190 if((ret = find_server_service_pattern(servp, pattern)))
191 {
192 n_found += ret;
193 n_servers_found++;
194 }
195 }
196 }
197 else
198 {
199 if((ret = find_server_service_pattern(servpp, pattern)))
200 {
201 n_found += ret;
202 }
203 }
204 if(n_servers != 0)
205 *n_servers = n_servers_found;
206 return(n_found);
207}
208
209CURR_SERVICE *find_curr_service(char *service)
210{
211 CURR_SERVICE *servicep;
212
213 servicep = Curr_service_head ;
214 while( (servicep = (CURR_SERVICE *)sll_get_next((SLL *)servicep)) )
215 {
216 if(!strcmp(servicep->name,service))
217 {
218 return(servicep);
219 }
220 }
221 return ((CURR_SERVICE *)0);
222}
223
224BROWSER *find_browser(int id)
225{
226 BROWSER *browserp;
227
228 browserp = Browser_head;
229 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
230 {
231 if(browserp->id == id)
232 {
233 return(browserp);
234 }
235 }
236 return ((BROWSER *)0);
237}
238/*
239void set_browser_changes(int n_services, int n_servers, int n_nodes)
240{
241 BROWSER *browserp;
242
243 browserp = Browser_head;
244 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
245 {
246 if(browserp->n_services != n_services)
247 browserp->n_services_changed = 1;
248 if(browserp->n_servers != n_servers)
249 browserp->n_servers_changed = 1;
250 if(browserp->n_nodes != n_nodes)
251 browserp->n_nodes_changed = 1;
252 }
253}
254*/
255void prepare_browser_tree()
256{
257 BROWSER *browserp;
258 int prepareJSONTree();
259
260 browserp = Browser_head;
261 while( (browserp = (BROWSER *)sll_get_next((SLL *)browserp)) )
262 {
263 if(browserp->pattern[0] != '\0')
264 prepareJSONTree(browserp);
265 }
266}
267/*
268void clear_browser_changes(BROWSER *browserp)
269{
270 browserp->n_services_changed = 0;
271 browserp->n_servers_changed = 0;
272 browserp->n_nodes_changed = 0;
273}
274*/
275void update_servers(int *tag, DNS_DID *buffer, int *size)
276{
277int n_services, service_size;
278SERVER *servp;
279NODE *nodep;
280int j;
281char str[MAX_NAME], sname[MAX_NAME], *ptr;
282int prepareJSONTree();
283int prepareJSONHeader();
284
285 if(tag){}
286 if(!Node_head)
287 {
288 Node_head = (NODE *)malloc(sizeof(NODE));
289 sll_init((SLL *)Node_head);
290 }
291 if(First_time)
292 {
293 First_time = 0;
294 }
295
296 if(!*size)
297 return;
298 if(*(char *)buffer == -1)
299 {
300 N_servers = 0;
301 N_services = 0;
302 return;
303 }
304 buffer->server.n_services = vtohl(buffer->server.n_services);
305 buffer->server.pid = vtohl(buffer->server.pid);
306 n_services = buffer->server.n_services;
307
308 if(n_services == 1)
309 return;
310 strcpy(sname, buffer->server.task);
311 if(n_services > 1)
312 {
313 for(j = 0; j < n_services; j++)
314 {
315 buffer->services[j].type = vtohl(
316 buffer->services[j].type);
317 buffer->services[j].status = vtohl(
318 buffer->services[j].status);
319 buffer->services[j].n_clients = vtohl(
320 buffer->services[j].n_clients);
321 if(strlen(sname) == MAX_TASK_NAME-4-1)
322 {
323 strcpy(str,buffer->services[j].name);
324 if( (ptr = strstr(str,"/CLIENT_LIST")) )
325 {
326 *ptr = '\0';
327 strcpy(sname,str);
328 }
329 }
330 }
331 }
332 if (!(nodep = find_node(buffer->server.node)))
333 {
334 if(n_services)
335 {
336 N_nodes++;
337 nodep = (NODE *)malloc(sizeof(NODE));
338 strcpy(nodep->name,buffer->server.node);
339 nodep->server_head = (SERVER *)malloc(sizeof(SERVER));
340 sll_init((SLL *)nodep->server_head);
341 sll_insert_queue((SLL *)Node_head,(SLL *)nodep);
342 }
343 }
344 if (!(servp = find_server(nodep,buffer->server.pid)))
345 {
346 if(n_services)
347 {
348 servp = (SERVER *)malloc(sizeof(SERVER));
349 strcpy(servp->name,sname);
350 servp->next = 0;
351 servp->busy = 0;
352 servp->server.n_services = 0;
353 servp->service_ptr = 0;
354 sll_insert_queue((SLL *)nodep->server_head,(SLL *)servp);
355 }
356 }
357 if(n_services != 0)
358 {
359 if(n_services == servp->server.n_services)
360 {
361 return;
362 }
363 if(servp->server.n_services == 0)
364 N_servers++;
365 if(servp->server.n_services != -1)
366 N_services -= servp->server.n_services;
367 memcpy(&servp->server,&buffer->server,sizeof(DNS_SERVER_INFO));
368 if(servp->service_ptr)
369 {
370 free(servp->service_ptr);
371 servp->service_ptr = 0;
372 }
373 if(n_services != -1)
374 {
375 service_size = n_services*sizeof(DNS_SERVICE_INFO);
376 servp->service_ptr = (DNS_SERVICE_INFO *)malloc(service_size);
377 memcpy(servp->service_ptr, buffer->services, service_size);
378 N_services += n_services;
379 }
380 servp->busy = 1;
381 }
382 else
383 {
384 if(servp)
385 {
386 N_servers--;
387 if(servp->server.n_services != -1)
388 {
389 N_services -= servp->server.n_services;
390 }
391 servp->server.n_services = 0;
392 servp->busy = -1;
393 }
394 }
395 if(JSONHeader[0])
396 prepareJSONHeader();
397}
398
399void got_update_services(BROWSER **tag, char *buffer, int *size)
400{
401 BROWSER *browserp;
402
403 if(size){}
404 browserp = (BROWSER *)*tag;
405 if(browserp->service_format_ptr)
406 free(browserp->service_format_ptr);
407 browserp->service_format_ptr = (char *)malloc(strlen(buffer)+1);
408 strcpy(browserp->service_format_ptr, buffer);
409}
410
411char *update_services(char *node, char *server, int pid, int browser)
412{
413 char str[MAX_NAME];
414 NODE *nodep;
415 SERVER *servp;
416 char *ptr;
417 BROWSER *browserp;
418 char *prepareJSONServiceList();
419 BROWSER *create_browser();
420
421 if(!(browserp = find_browser(browser)))
422 browserp = create_browser(browser);
423
424 if(server){}
425 sprintf(str,"%s/SERVICE_LIST",server);
426 dic_info_service(str,ONCE_ONLY,20,0,0,
427 got_update_services,(dim_long)browserp,"None",5);
428 if((nodep = find_node(node)))
429 {
430 if((servp = find_server(nodep, pid)))
431 {
432 ptr = prepareJSONServiceList(servp, node, pid, browserp);
433 }
434 }
435 return ptr;
436}
437
438void get_curr_service_format()
439{
440 char *format;
441 char *dic_get_format();
442/*
443 char str[256], *ptr, *ptr1;
444 int rpc_flag;
445
446 strcpy(str,Curr_service_name);
447 rpc_flag = 0;
448 if( (ptr = strstr(str,"/RpcIn")) )
449 {
450 *ptr = '\0';
451 rpc_flag = 1;
452 }
453 if( (ptr = strstr(str,"/RpcOut")) )
454 {
455 *ptr = '\0';
456 rpc_flag = 2;
457 }
458 strcat(str,"|");
459*/
460 format = dic_get_format(0);
461/*
462 if( (ptr = strstr(Curr_service_list,str)) )
463 {
464 if(!rpc_flag)
465 {
466 ptr += strlen(str);
467 ptr1 = strchr(ptr,'|');
468 }
469 else if(rpc_flag == 1)
470 {
471 ptr += strlen(str);
472 ptr1 = strchr(ptr,',');
473 }
474 else
475 {
476 ptr += strlen(str);
477 ptr = strchr(ptr,',');
478 ptr++;
479 ptr1 = strchr(ptr,'|');
480 }
481 strncpy(Curr_service_format,ptr,(int)(ptr1 - ptr));
482 Curr_service_format[(int)(ptr1-ptr)] = '\0';
483 }
484*/
485 if(format)
486 strcpy(Curr_service_format,format);
487 else
488 Curr_service_format[0] = '\0';
489}
490
491void get_service_format(char *buffer, char *service, char *format)
492{
493 char str[256], *ptr, *ptr1;
494 int rpc_flag;
495
496 strcpy(str, service);
497 rpc_flag = 0;
498 *format = '\0';
499 if( (ptr = strstr(str,"/RpcIn")) )
500 {
501 *ptr = '\0';
502 rpc_flag = 1;
503 }
504 if( (ptr = strstr(str,"/RpcOut")) )
505 {
506 *ptr = '\0';
507 rpc_flag = 2;
508 }
509 strcat(str,"|");
510 if( (ptr = strstr(buffer, str)) )
511 {
512 if(!rpc_flag)
513 {
514 ptr += strlen(str);
515 ptr1 = strchr(ptr,'|');
516 }
517 else if(rpc_flag == 1)
518 {
519 ptr += strlen(str);
520 ptr1 = strchr(ptr,',');
521 }
522 else
523 {
524 ptr += strlen(str);
525 ptr = strchr(ptr,',');
526 ptr++;
527 ptr1 = strchr(ptr,'|');
528 }
529 strncpy(format,ptr,(int)(ptr1 - ptr));
530 format[(int)(ptr1-ptr)] = '\0';
531 }
532}
533
534int delete_curr_service(CURR_SERVICE *servicep)
535{
536
537if(WebDID_Debug)
538printf("\nUnsubscribing %s\n\n",servicep->name);
539 dic_release_service(servicep->sid);
540 if(servicep->buffer_size)
541 free(servicep->buffer);
542 if(servicep->buffer_str_size)
543 free(servicep->buffer_str);
544 sll_remove((SLL *)Curr_service_head, (SLL *)servicep);
545 free(servicep);
546 return(1);
547}
548
549int delete_browser(BROWSER *browserp)
550{
551 CURR_SERVICE *servicep;
552
553 if((servicep = browserp->servicep))
554 {
555 servicep->n_browsers--;
556 if(!servicep->n_browsers)
557 delete_curr_service(servicep);
558 }
559 if(browserp->service_format_ptr)
560 free(browserp->service_format_ptr);
561 sll_remove((SLL *)Browser_head, (SLL *)browserp);
562 free(browserp);
563 return(1);
564}
565
566void check_browser( BROWSER *tag)
567{
568 BROWSER *browserp;
569 time_t tsecs;
570
571 browserp = (BROWSER *)tag;
572if(WebDID_Debug)
573printf("\nCheck_browser %d\n",browserp->id);
574 tsecs = time((time_t *)0);
575 if((tsecs - browserp->last_polled) > 20)
576 {
577if(WebDID_Debug)
578printf("\nDeleting browser %d\n\n",browserp->id);
579 delete_browser(browserp);
580 }
581 else
582 dtq_start_timer(10, check_browser, browserp);
583}
584
585BROWSER *create_browser(int id)
586{
587 BROWSER *browserp;
588
589 if(!Browser_head)
590 {
591 Browser_head = (BROWSER *)malloc(sizeof(BROWSER));
592 sll_init((SLL *)Browser_head);
593 }
594 browserp = (BROWSER *)malloc(sizeof(BROWSER));
595 browserp->id = id;
596 browserp->last_subscribed = 0;
597 browserp->last_updated = 0;
598 browserp->last_polled = 0;
599 browserp->n_nodes = 0;
600 browserp->n_servers = 0;
601 browserp->n_services = 0;
602 browserp->servicep = 0;
603 browserp->JSONBuffer = 0;
604 browserp->JSONBufferSize = 0;
605 browserp->pattern[0] = '\0';
606 browserp->service_format_ptr = 0;
607 browserp->curr_command[0] = '\0';
608 sll_insert_queue((SLL *)Browser_head,(SLL *)browserp);
609 dtq_start_timer(10, check_browser, browserp);
610 return browserp;
611}
612
613int update_command_data(char *service, int conn_id, BROWSER *browserp)
614{
615 char format[MAX_NAME];
616 char answer[MAX_NAME*3];
617 extern void sendData();
618
619 if(browserp->service_format_ptr)
620 {
621 get_service_format(browserp->service_format_ptr, service, format);
622if(WebDID_Debug)
623printf("CMD data %s %s\n",service,format);
624 }
625 else
626 {
627 strcpy(format,"?");
628 }
629 strcpy(browserp->curr_command, service);
630 sprintf(answer,"To %s (%s)",service, format);
631 sendData(conn_id, answer, 4);
632 return 1;
633}
634
635int update_service_data(char *service, int conn_id, int subscribe, int req, int browser, int force)
636{
637 CURR_SERVICE *servicep;
638 time_t tsecs;
639 void recv_service_info();
640 extern void sendData();
641 BROWSER *browserp;
642
643 if(req){}
644 if(!Curr_service_head)
645 {
646 Curr_service_head = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
647 sll_init((SLL *)Curr_service_head);
648 }
649 if(!(browserp = find_browser(browser)))
650 browserp = create_browser(browser);
651 if(force == -1)
652 {
653 update_command_data(service, conn_id, browserp);
654 return 1;
655 }
656 if((servicep = browserp->servicep))
657 {
658 servicep->n_browsers--;
659 if(!servicep->n_browsers)
660 delete_curr_service(servicep);
661 }
662 if(!(servicep = find_curr_service(service)))
663 {
664 servicep = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
665 strcpy(servicep->name,service);
666 servicep->conn_id = conn_id;
667 servicep->buffer = 0;
668 servicep->buffer_size = 0;
669 servicep->size = 0;
670 servicep->buffer_str = 0;
671 servicep->buffer_str_size = 0;
672 servicep->str_size = 0;
673 servicep->last_updated = 0;
674 tsecs = time((time_t *)0);
675 browserp->last_subscribed = tsecs;
676 browserp->last_updated = tsecs;
677 servicep->last_subscribed = tsecs;
678 servicep->n_browsers = 0;
679 sll_insert_queue((SLL *)Curr_service_head,(SLL *)servicep);
680 servicep->sid = dic_info_service_stamped( service, MONITORED, subscribe, 0, 0,
681 recv_service_info, servicep, &no_link_int, 4);
682 }
683 else
684 {
685 if(servicep->size)
686 {
687 if((servicep->timestamp > browserp->last_updated) || (force))
688 {
689 sendData(conn_id, servicep->buffer_str, 4);
690 }
691 else
692 {
693 sendData(conn_id, "", 4);
694 }
695 browserp->last_updated = servicep->timestamp;
696 }
697 }
698 if(force)
699 {
700 browserp->servicep = servicep;
701 servicep->n_browsers++;
702 }
703 return 1;
704}
705
706int check_browser_changes(char *service, int conn_id, int subscribe, int req, int browser, int force)
707{
708 CURR_SERVICE *servicep;
709 time_t tsecs;
710 void recv_service_info();
711 extern void sendData();
712 BROWSER *browserp;
713 char answer[256];
714 int service_changed = 0;
715
716 if(req){}
717 if(subscribe){}
718 if(!(browserp = find_browser(browser)))
719 browserp = create_browser(browser);
720 if(!Curr_service_head)
721 {
722 Curr_service_head = (CURR_SERVICE *)malloc(sizeof(CURR_SERVICE));
723 sll_init((SLL *)Curr_service_head);
724 }
725 if(service[0] != '\0')
726 {
727 if((servicep = find_curr_service(service)))
728 {
729 if(servicep->size)
730 {
731 if((servicep->timestamp > browserp->last_updated) || (force))
732 {
733 service_changed = 1;
734 }
735 }
736 }
737 }
738/*
739 sprintf(answer,"%d %d %d %d\n",
740 browserp->n_services_changed, browserp->n_servers_changed,
741 browserp->n_nodes_changed, service_changed);
742*/
743 sprintf(answer,"%d %d %d %d %d %d %d\n",
744 N_services, N_servers, N_nodes, service_changed,
745 browserp->n_services, browserp->n_servers, browserp->n_nodes);
746 sendData(conn_id, answer, 4);
747 tsecs = time((time_t *)0);
748 browserp->last_polled = tsecs;
749 return 1;
750}
751
752int find_services(char *pattern, int conn_id, int browser, int force)
753{
754 void recv_service_info();
755 extern void sendData();
756 BROWSER *browserp;
757 char format[MAX_NAME];
758 int prepareJSONTree();
759 void did_prepare_command();
760
761 if(conn_id){}
762 if(!(browserp = find_browser(browser)))
763 browserp = create_browser(browser);
764 if(force == -1)
765 {
766 if(browserp->service_format_ptr)
767 {
768 get_service_format(browserp->service_format_ptr, browserp->curr_command, format);
769 did_prepare_command(pattern, browserp->curr_command, format);
770 }
771 return 1;
772 }
773 if(conn_id){}
774 if(!(browserp = find_browser(browser)))
775 browserp = create_browser(browser);
776 strcpy(browserp->pattern, pattern);
777 return 1;
778}
779
780void recv_service_info(void **tag, int *buffer, int *size)
781{
782 int conn_id;
783 void print_service_formatted();
784 extern void sendData();
785 CURR_SERVICE *servicep;
786 time_t tsecs;
787
788 servicep = *tag;
789 conn_id = servicep->conn_id;
790 if (servicep->buffer_size < *size)
791 {
792 if(servicep->buffer_size)
793 free(servicep->buffer);
794 servicep->buffer = malloc(*size);
795 servicep->buffer_size = *size;
796 }
797 memcpy(servicep->buffer, (char *)buffer, *size);
798 servicep->size = *size;
799 if (servicep->buffer_str_size < (1024 + (*size)*16))
800 {
801 if(servicep->buffer_str_size)
802 free(servicep->buffer_str);
803 servicep->buffer_str = malloc(1024 + (*size)*16);
804 servicep->buffer_str_size = 1024 + (*size)*16;
805 }
806 Service_content_str = servicep->buffer_str;
807 strcpy(Curr_service_name, servicep->name);
808 get_curr_service_format();
809 if((*size == 4 ) && (*buffer == -1))
810 {
811 sprintf(Service_content_str,
812 "Service %s Not Available", Curr_service_name);
813 }
814 else
815 {
816 print_service_formatted(servicep, buffer, *size);
817 }
818 if(servicep->last_updated == 0)
819 {
820 sendData(conn_id, Service_content_str, 4);
821 tsecs = time((time_t *)0);
822 servicep->last_updated = tsecs;
823 }
824}
825
826void print_service_formatted(CURR_SERVICE *servicep, void *buff, int size)
827{
828char type;
829int num, ret;
830char str[256];
831char *ptr;
832void *buffer_ptr;
833char timestr[256], aux[64], sizestr[64];
834int quality = 0, secs = 0, mili = 0;
835int did_write_string(char, int, void **, int);
836time_t tsecs;
837
838 if(size < 1024)
839 sprintf(sizestr,"%d bytes",size);
840 else if (size < 1024*1024)
841 sprintf(sizestr,"%2.2f Kb",(float)size/1024);
842 else
843 sprintf(sizestr,"%2.2f Mb",(float)size/(1024*1024));
844
845 sprintf(Service_content_str,
846 "<FONT FACE=\"consolas\">Service %s (%s) Contents :<br /> <br />", Curr_service_name,
847 Curr_service_format);
848 dic_get_timestamp(0, &secs, &mili);
849 quality = dic_get_quality(0);
850 tsecs = secs;
851 servicep->timestamp = tsecs;
852 my_ctime(&tsecs, timestr, 128);
853 ptr = strrchr(timestr,' ');
854 strcpy(aux, ptr);
855 sprintf(ptr,".%03d",mili);
856 strcat(timestr, aux);
857 timestr[strlen(timestr)-1] = '\0';
858
859 sprintf(str," Timestamp: %s&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Quality: %d&nbsp&nbsp&nbsp Size: %s<br /><br />",
860 timestr, quality, sizestr);
861
862 strcat(Service_content_str,str);
863 ptr = Curr_service_format;
864 buffer_ptr = buff;
865 while(*ptr)
866 {
867 type = *ptr++;
868 if(*ptr == ':')
869 {
870 ptr++;
871 sscanf(ptr, "%d", &num);
872 ret = did_write_string(type, num, &buffer_ptr, size);
873 size -= ret;
874 if( (ptr = strchr(ptr,';')) )
875 ptr++;
876 else
877 break;
878 }
879 else
880 {
881 ret = did_write_string(type, 0, &buffer_ptr, size);
882 size -= ret;
883 break;
884 }
885 }
886 strcat(Service_content_str,"</FONT>");
887}
888
889
890int did_write_string(char type, int num, void **buffer_ptr, int ssize)
891{
892void *ptr;
893int size, psize;
894
895 void print_service_standard();
896 void print_service_char();
897 void print_service_short();
898 void print_service_float();
899 void print_service_double();
900
901 ptr = *buffer_ptr;
902 switch(type)
903 {
904 case 'L':
905 case 'l':
906 strcat(Service_content_str," L");
907 if(!num)
908 size = ssize/sizeof(int);
909 else
910 size = num;
911 psize = size * sizeof(int);
912 print_service_standard(ptr, size);
913 break;
914 case 'I':
915 case 'i':
916 strcat(Service_content_str," I");
917 if(!num)
918 size = ssize/sizeof(int);
919 else
920 size = num;
921 psize = size * sizeof(int);
922 print_service_standard(ptr, size);
923 break;
924 case 'S':
925 case 's':
926 strcat(Service_content_str," S");
927 if(!num)
928 size = ssize/sizeof(short);
929 else
930 size = num;
931 psize = size * sizeof(short);
932 print_service_short(ptr, size);
933 break;
934 case 'F':
935 case 'f':
936 strcat(Service_content_str," F");
937 if(!num)
938 size = ssize/sizeof(float);
939 else
940 size = num;
941 psize = size * sizeof(float);
942 print_service_float(ptr, size);
943 break;
944 case 'D':
945 case 'd':
946 strcat(Service_content_str," D");
947 if(!num)
948 size = ssize/sizeof(double);
949 else
950 size = num;
951 psize = size * sizeof(double);
952 print_service_double(ptr, size);
953 break;
954 case 'X':
955 case 'x':
956 strcat(Service_content_str," X");
957 if(!num)
958 size = ssize/sizeof(longlong);
959 else
960 size = num;
961 psize = size * sizeof(longlong);
962 print_service_standard(ptr, size*2);
963 break;
964 case 'C':
965 case 'c':
966 default:
967 strcat(Service_content_str," C");
968 if(!num)
969 size = ssize;
970 else
971 size = num;
972 psize = size;
973 print_service_char(ptr, size);
974 }
975 ptr = (char *)ptr + psize;
976 *buffer_ptr = ptr;
977 return psize;
978}
979
980void sprintf_html(char *str, int n, int value)
981{
982 char tmp[80];
983 int min, i;
984
985 str[0] = '\0';
986 min = sprintf(tmp,"%d",value);
987 for(i = 0; i < (n-min); i++)
988 {
989 strcat(str,"&nbsp");
990 }
991 strcat(str, tmp);
992}
993
994void print_service_standard(int *buff, int size)
995{
996int i,j;
997char *ptr, str[80], tmp[256];
998int last[4];
999
1000 ptr = Service_content_str;
1001 ptr += strlen(Service_content_str);
1002 for( i = 0; i < size; i++)
1003 {
1004 strcpy(tmp,"");
1005 if(i%4 == 0)
1006 {
1007 if(i != 0)
1008 {
1009 strcat(tmp,"&nbsp");
1010 }
1011 sprintf_html(str, 7, i);
1012 strcat(tmp,str);
1013 }
1014 if(!(i%4))
1015 strcat(tmp,"H: ");
1016 sprintf(str,"&nbsp&nbsp&nbsp %08X",buff[i]);
1017 strcat(tmp,str);
1018 last[i%4] = buff[i];
1019 if((i%4 == 3) || (i == (size-1)))
1020 {
1021 strcat(tmp,"<br />");
1022 for(j = 0; j <= (i%4); j++)
1023 {
1024 if(j == 0)
1025 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1026 sprintf_html(str, 12, last[j]);
1027 strcat(tmp,str);
1028 }
1029 strcat(tmp,"<br />");
1030 }
1031 strcpy(ptr, tmp);
1032 ptr += strlen(tmp);
1033 }
1034 strcpy(tmp,"<br />");
1035 strcpy(ptr, tmp);
1036}
1037
1038void print_service_longlong(longlong *buff, int size)
1039{
1040int i,j;
1041char *ptr, str[80], tmp[256];
1042longlong last[4];
1043
1044 ptr = Service_content_str;
1045 ptr += strlen(Service_content_str);
1046 for( i = 0; i < size; i++)
1047 {
1048 strcpy(tmp,"");
1049 if(i%4 == 0)
1050 {
1051 if(i != 0)
1052 {
1053 strcat(tmp,"&nbsp");
1054 }
1055 sprintf_html(str, 7, i);
1056 strcat(tmp,str);
1057 }
1058 if(!(i%4))
1059 strcat(tmp,"H: ");
1060 sprintf(str,"&nbsp&nbsp&nbsp %08X",(unsigned)buff[i]);
1061 strcat(tmp,str);
1062 last[i%4] = buff[i];
1063 if((i%4 == 3) || (i == (size-1)))
1064 {
1065 strcat(tmp,"<br />");
1066 for(j = 0; j <= (i%4); j++)
1067 {
1068 if(j == 0)
1069 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1070 sprintf_html(str, 12, last[j]);
1071 strcat(tmp,str);
1072 }
1073 strcat(tmp,"<br />");
1074 }
1075 strcpy(ptr, tmp);
1076 ptr += strlen(tmp);
1077 }
1078 strcpy(tmp,"<br />");
1079 strcpy(ptr, tmp);
1080}
1081
1082void print_service_short(short *buff, int size)
1083{
1084int i,j;
1085char *ptr, str[80], tmp[256];
1086short last[8];
1087
1088 ptr = Service_content_str;
1089 ptr += strlen(Service_content_str);
1090 for( i = 0; i < size; i++)
1091 {
1092 strcpy(tmp,"");
1093 if(i%8 == 0)
1094 {
1095 if(i != 0)
1096 {
1097 strcat(tmp,"&nbsp");
1098 }
1099 sprintf_html(str, 7, i);
1100 strcat(tmp,str);
1101 }
1102 if(!(i%8))
1103 strcat(tmp,"H: ");
1104 sprintf(str,"&nbsp %04X",buff[i]);
1105 strcat(tmp,str);
1106 last[i%8] = buff[i];
1107 if((i%8 == 7) || (i == (size-1)))
1108 {
1109 strcat(tmp,"<br />");
1110 for(j = 0; j <= (i%8); j++)
1111 {
1112 if(j == 0)
1113 strcat(tmp,"&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp D: ");
1114 sprintf_html(str, 6, last[j]);
1115 strcat(tmp,str);
1116 }
1117 strcat(tmp,"<br />");
1118 }
1119 strcpy(ptr, tmp);
1120 ptr += strlen(tmp);
1121 }
1122 strcpy(tmp,"<br />");
1123 strcpy(ptr, tmp);
1124}
1125
1126void print_service_char(char *buff, int size)
1127{
1128int i,j;
1129char *asc, *ptr, str[80], tmp[256];
1130
1131 asc = (char *)buff;
1132 ptr = Service_content_str;
1133 ptr += strlen(Service_content_str);
1134 for( i = 0; i < size; i++)
1135 {
1136 strcpy(tmp,"");
1137 if(i%16 == 0)
1138 {
1139 if(i != 0)
1140 {
1141 strcat(tmp,"&nbsp");
1142 }
1143 sprintf_html(str, 7, i);
1144 strcat(tmp,str);
1145 }
1146 if(!(i%16))
1147 strcat(tmp,"H: ");
1148 sprintf(str,"%02X",buff[i]);
1149/* strcat(tmp,str);
1150*/
1151 strcat(tmp," ");
1152 strcat(tmp,&str[strlen(str)-2]);
1153 /*
1154 if(i%4 == 3)
1155 strcat(tmp," ");
1156 */
1157 if((i%16 == 15) || (i == (size-1)))
1158 {
1159 if(i%16 != 15)
1160 {
1161 for(j = 1; j < 16 - (i%16); j++)
1162 strcat(tmp,"&nbsp&nbsp ");
1163 }
1164 strcat(tmp,"&nbsp&nbsp&nbsp '");
1165 for(j = 0; j <= (i%16) ; j++)
1166 {
1167 if(isprint(asc[j]))
1168 {
1169 sprintf(str,"%c",asc[j]);
1170 strcat(tmp,str);
1171 }
1172 else
1173 {
1174 sprintf(str,".");
1175 strcat(tmp,str);
1176 }
1177 }
1178 strcat(tmp,"'<br />");
1179 asc = (char *)&buff[i+1];
1180 }
1181 strcpy(ptr, tmp);
1182 ptr += strlen(tmp);
1183 }
1184 strcpy(tmp,"<br />");
1185 strcpy(ptr, tmp);
1186}
1187
1188void print_service_float(float *buff, int size)
1189{
1190int i;
1191char *ptr, str[80], tmp[256];
1192
1193 ptr = Service_content_str;
1194 ptr += strlen(Service_content_str);
1195 for( i = 0; i < size; i++)
1196 {
1197 strcpy(tmp,"");
1198 if(i%4 == 0)
1199 {
1200 if(i != 0)
1201 {
1202 strcat(tmp,"&nbsp");
1203 }
1204 sprintf_html(str, 7, i);
1205 strcat(tmp,str);
1206 }
1207 sprintf(str,"%12.3G",*(buff++));
1208 strcat(tmp,str);
1209 if((i%4 == 3) || (i == size-1))
1210 {
1211 strcat(tmp,"<br />");
1212 }
1213 strcpy(ptr, tmp);
1214 ptr += strlen(tmp);
1215 }
1216 strcpy(tmp,"<br />");
1217 strcpy(ptr, tmp);
1218 ptr += strlen(tmp);
1219}
1220
1221void print_service_double(double *buff, int size)
1222{
1223int i;
1224char *ptr, str[80], tmp[256];
1225
1226 ptr = Service_content_str;
1227 ptr += strlen(Service_content_str);
1228 for( i = 0; i < size; i++)
1229 {
1230 strcpy(tmp,"");
1231 if(i%4 == 0)
1232 {
1233 if(i != 0)
1234 {
1235 strcat(tmp,"&nbsp");
1236 }
1237 sprintf_html(str, 7, i);
1238 strcat(tmp,str);
1239 }
1240 sprintf(str,"%12.3G",*(buff++));
1241 strcat(tmp,str);
1242 if((i%4 == 3) || (i == size-1))
1243 {
1244 strcat(tmp,"<br />");
1245 }
1246 strcpy(ptr, tmp);
1247 ptr += strlen(tmp);
1248 }
1249 strcpy(tmp,"<br />");
1250 strcpy(ptr, tmp);
1251 ptr += strlen(tmp);
1252}
1253
1254char *addJSONStart(char *ptr)
1255{
1256 char *ptro;
1257
1258 strcat(ptr,"{\n");
1259 ptro = ptr + strlen(ptr);
1260 return ptro;
1261}
1262
1263char *addJSONEnd(char *ptr)
1264{
1265 char *ptro;
1266
1267 strcat(ptr,"}\n");
1268 ptro = ptr + strlen(ptr);
1269 return ptro;
1270}
1271
1272char *addJSONNodeStart(char *ptr, char *node)
1273{
1274 char *ptro;
1275
1276 sprintf(ptr,"%s: [\n", node);
1277 ptro = ptr + strlen(ptr);
1278 return ptro;
1279}
1280
1281char *addJSONNodeEnd(char *ptr)
1282{
1283 char *ptro;
1284
1285 strcat(ptr,"]\n");
1286 ptro = ptr + strlen(ptr);
1287 return ptro;
1288}
1289
1290char *addJSONChildStart(char *ptr, char *child, int sep)
1291{
1292 char *ptro;
1293
1294 if(sep)
1295 sprintf(ptr,"{ %s, ", child);
1296 else
1297 sprintf(ptr,"{ %s", child);
1298 ptro = ptr + strlen(ptr);
1299 return ptro;
1300}
1301
1302char *addJSONChildEnd(char *ptr, int sep)
1303{
1304 char *ptro;
1305
1306 if(sep)
1307 strcat(ptr," },\n");
1308 else
1309 strcat(ptr," }\n");
1310 ptro = ptr + strlen(ptr);
1311 return ptro;
1312}
1313
1314char *getJSONBuffer(char *node, int browser)
1315{
1316 BROWSER *browserp;
1317 int prepareJSONTree();
1318
1319 if(browser)
1320 {
1321 if((browserp = find_browser(browser)))
1322 {
1323 if(browserp->pattern[0] != '\0')
1324 {
1325 prepareJSONTree(node, browserp);
1326 return(browserp->JSONBuffer);
1327 }
1328 browserp->n_services = 0;
1329 browserp->n_servers = 0;
1330 browserp->n_nodes = 0;
1331 }
1332 }
1333 prepareJSONTree(node, 0);
1334 return(JSONBuffer);
1335}
1336
1337char *getJSONHeader()
1338{
1339 int prepareJSONHeader();
1340
1341 if(JSONHeader[0] == '\0')
1342 prepareJSONHeader();
1343 return(JSONHeader);
1344}
1345
1346int getNodeLabel(char *name, char *label)
1347{
1348 int i;
1349 extern int web_get_node_name();
1350
1351 web_get_node_name(name, label);
1352 for(i = 0; i < ((int)strlen(label) + 1); i++)
1353 {
1354 label[i] = tolower(label[i]);
1355 if(label[i] == '.')
1356 {
1357 label[i] = '\0';
1358 break;
1359 }
1360 }
1361 return 1;
1362}
1363
1364int prepareJSONTree(char *node, BROWSER *browserp)
1365{
1366 char *ptr;
1367 NODE *nodep;
1368 SERVER *servp;
1369 char str[256], aux[128];
1370 int selective = 0;
1371 int n_nodes, tot_n_nodes;
1372 int n_servers, tot_n_servers;
1373 int ret, n_found = 0;
1374
1375 if(browserp)
1376 {
1377 if(browserp->pattern[0] != '\0')
1378 selective = 1;
1379 else
1380 return(0);
1381 }
1382 if(!selective)
1383 {
1384 if(JSONBufferSize == 0)
1385 {
1386 JSONBuffer = malloc(N_nodes*128+N_servers*128);
1387 }
1388 else if (JSONBufferSize < N_nodes*128+N_servers*128)
1389 {
1390 free(JSONBuffer);
1391 JSONBuffer = malloc(N_nodes*128+N_servers*128);
1392 }
1393 ptr = JSONBuffer;
1394 }
1395 else
1396 {
1397 if(browserp->JSONBufferSize == 0)
1398 {
1399 browserp->JSONBuffer = malloc(N_nodes*128+N_servers*128);
1400 }
1401 else if (browserp->JSONBufferSize < N_nodes*128+N_servers*128)
1402 {
1403 free(browserp->JSONBuffer);
1404 browserp->JSONBuffer = malloc(N_nodes*128+N_servers*128);
1405 }
1406 ptr = browserp->JSONBuffer;
1407 }
1408 *ptr = '\0';
1409 if(!strcmp(node, "src"))
1410 {
1411 ptr = addJSONStart(ptr);
1412 ptr = addJSONNodeStart(ptr,"children");
1413 sprintf(str,"text: \"%s\", id: \"Nodes\", expanded: false", Title);
1414 ptr = addJSONChildStart(ptr,str,1);
1415 ptr = addJSONNodeStart(ptr,"children");
1416 nodep = Node_head;
1417 tot_n_nodes = 0;
1418 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
1419 {
1420 nodep->match = 1;
1421 if(selective)
1422 {
1423 if(!(ret = find_service_pattern(nodep, 0, browserp->pattern, &tot_n_servers)))
1424 {
1425 nodep->match = 0;
1426 continue;
1427 }
1428 else
1429 {
1430 n_found += ret;
1431 }
1432 }
1433 tot_n_nodes++;
1434 }
1435 n_nodes = 0;
1436 nodep = Node_head;
1437 while( (nodep = (NODE *)sll_get_next((SLL *)nodep)) )
1438 {
1439 if(!nodep->match)
1440 continue;
1441 getNodeLabel(nodep->name, aux);
1442 sprintf(str,"text: \"%s\", id: \"%s\", qtip: \"%s\"",
1443 aux, nodep->name, nodep->name);
1444 ptr = addJSONChildStart(ptr,str,0);
1445 n_nodes++;
1446if(WebDID_Debug)
1447 printf("adding %s %d %d\n",nodep->name, n_nodes, tot_n_nodes);
1448 if(n_nodes < tot_n_nodes)
1449 ptr = addJSONChildEnd(ptr,1);
1450 else
1451 ptr = addJSONChildEnd(ptr,0);
1452 }
1453 ptr = addJSONNodeEnd(ptr);
1454 ptr = addJSONChildEnd(ptr,0);
1455 ptr = addJSONNodeEnd(ptr);
1456 ptr = addJSONEnd(ptr);
1457 if(selective)
1458 {
1459 browserp->n_services = n_found;
1460 browserp->n_servers = tot_n_servers;
1461 browserp->n_nodes = tot_n_nodes;
1462 }
1463 }
1464 else
1465 {
1466 if((nodep = find_node(node)))
1467 {
1468 ptr = addJSONStart(ptr);
1469 ptr = addJSONNodeStart(ptr,"children");
1470 servp = nodep->server_head;
1471 tot_n_servers = 0;
1472 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
1473 {
1474 servp->match = 1;
1475 if(servp->busy != 1)
1476 {
1477 servp->match = 0;
1478 continue;
1479 }
1480 if(selective)
1481 {
1482 if(!(ret = find_service_pattern(nodep, servp, browserp->pattern, 0)))
1483 {
1484 servp->match = 0;
1485 continue;
1486 }
1487 else
1488 {
1489 n_found += ret;
1490 }
1491 }
1492 tot_n_servers++;
1493 }
1494 n_servers = 0;
1495 servp = nodep->server_head;
1496 while( (servp = (SERVER *)sll_get_next((SLL *)servp)) )
1497 {
1498 if(!servp->match)
1499 continue;
1500 sprintf(str,"text: \"%s\", id: \"%d\", leaf: true, icon: \"server.png\"",servp->name, servp->server.pid);
1501 ptr = addJSONChildStart(ptr,str,0);
1502 n_servers++;
1503 if(n_servers < tot_n_servers)
1504 ptr = addJSONChildEnd(ptr,1);
1505 else
1506 ptr = addJSONChildEnd(ptr,0);
1507 }
1508 ptr = addJSONNodeEnd(ptr);
1509 ptr = addJSONEnd(ptr);
1510 }
1511 }
1512 /*
1513if(!selective)
1514printf(" Nodes&Servers %s\n",JSONBuffer);
1515else
1516printf(" Nodes&Servers %s\n",browserp->JSONBuffer);
1517 */
1518 return(1);
1519}
1520
1521int prepareJSONHeader()
1522{
1523 char *ptr;
1524 char str[128];
1525
1526 ptr = JSONHeader;
1527 *ptr = '\0';
1528 ptr = addJSONStart(ptr);
1529 ptr = addJSONNodeStart(ptr,"items");
1530 sprintf(str,"text: \"%s\"",Title);
1531 ptr = addJSONChildStart(ptr,str,0);
1532 ptr = addJSONChildEnd(ptr,1);
1533 sprintf(str,"text: \"%d Servers Known - %d Services Available\"",N_servers, N_services);
1534 ptr = addJSONChildStart(ptr,str,0);
1535 ptr = addJSONChildEnd(ptr,0);
1536 ptr = addJSONNodeEnd(ptr);
1537 ptr = addJSONEnd(ptr);
1538if(WebDID_Debug)
1539printf(" Header %s\n",JSONHeader);
1540 return(1);
1541}
1542
1543char *JSONServices = 0;
1544int JSONServicesSize = 0;
1545char *prepareJSONServiceList(SERVER *servp, char *node, int pid, BROWSER *browserp)
1546{
1547 DNS_SERVICE_INFO *servicep;
1548 char *ptr;
1549 int n_services, i;
1550 char str[256], type_str[256];
1551 int selective = 0;
1552 int n_found = 0, n;
1553
1554 servicep = servp->service_ptr;
1555 n_services = servp->server.n_services;
1556 if(JSONServicesSize == 0)
1557 {
1558 JSONServices = malloc(n_services*256);
1559 }
1560 else if (JSONServicesSize < n_services*256)
1561 {
1562 free(JSONServices);
1563 JSONServices = malloc(n_services*256);
1564 }
1565 if(browserp)
1566 {
1567 if(browserp->pattern[0] != '\0')
1568 selective = 1;
1569 }
1570 n_found = n_services;
1571 if(selective)
1572 {
1573 n_found = find_server_service_pattern(servp, browserp->pattern);
1574 }
1575 ptr = JSONServices;
1576 *ptr = '\0';
1577 ptr = addJSONStart(ptr);
1578 ptr = addJSONNodeStart(ptr,"children");
1579 if(selective)
1580 sprintf(str,"text: \"%s (%d/%d services, pid %d)\"",servp->name, n_found, n_services, servp->server.pid);
1581 else
1582 sprintf(str,"text: \"%s (%d services, pid %d)\"",servp->name, n_services, servp->server.pid);
1583 ptr = addJSONChildStart(ptr,str,1);
1584 ptr = addJSONNodeStart(ptr,"children");
1585 servicep = servp->service_ptr;
1586 n = 0;
1587 for(i = 0; i < n_services; i++)
1588 {
1589/*
1590printf("Service type = %d\n",servicep->type);
1591*/
1592 if((!selective) || (strstr(servicep->name, browserp->pattern)))
1593 {
1594 if(servicep->type == 1)
1595 {
1596 sprintf(type_str,"%d@%s|%s|CMD", pid, node, servicep->name);
1597 sprintf(str,"text: \"%s\", id: \"%s\", leaf: true, icon: \"leaf_cmd.gif\"",servicep->name, type_str);
1598 }
1599 else
1600 {
1601 sprintf(type_str,"%d@%s|%s", pid, node, servicep->name);
1602 sprintf(str,"text: \"%s\", id: \"%s\", leaf: true",servicep->name, type_str);
1603 }
1604 ptr = addJSONChildStart(ptr,str,0);
1605 n++;
1606 if(n < n_found)
1607 ptr = addJSONChildEnd(ptr,1);
1608 else
1609 ptr = addJSONChildEnd(ptr,0);
1610 }
1611 servicep++;
1612 }
1613 ptr = addJSONNodeEnd(ptr);
1614 ptr = addJSONChildEnd(ptr,0);
1615 ptr = addJSONNodeEnd(ptr);
1616 ptr = addJSONEnd(ptr);
1617 return JSONServices;
1618}
1619
1620int matchString( char *wzString, char *wzPattern )
1621{
1622 switch (*wzPattern){
1623 case '\0':
1624 return !*wzString;
1625 case '*':
1626 return matchString(wzString, wzPattern+1) ||
1627 ( *wzString && matchString(wzString+1, wzPattern) );
1628 case '?':
1629 return *wzString &&
1630 matchString(wzString+1, wzPattern+1);
1631 default:
1632 return (*wzPattern == *wzString) &&
1633 matchString(wzString+1, wzPattern+1);
1634 }
1635}
1636
1637int get_type_size(char type)
1638{
1639 int size;
1640
1641 switch(type)
1642 {
1643 case 'L':
1644 case 'l':
1645 size = sizeof(long);
1646 break;
1647 case 'I':
1648 case 'i':
1649 size = sizeof(int);
1650 break;
1651 case 'S':
1652 case 's':
1653 size = sizeof(short);
1654 break;
1655 case 'F':
1656 case 'f':
1657 size = sizeof(float);
1658 break;
1659 case 'D':
1660 case 'd':
1661 size = sizeof(double);
1662 break;
1663 case 'C':
1664 case 'c':
1665 default:
1666 size = 1;
1667 }
1668 return(size);
1669}
1670
1671void did_prepare_command(char *str, char *service, char *format)
1672{
1673char type;
1674int num;
1675int size, full_size = 0;
1676char *ptr;
1677static int last_size = 0;
1678static void *last_buffer = 0;
1679void *buffer_ptr;
1680char *str_ptr;
1681void did_read_string(char, int, void **, char **);
1682
1683 str_ptr = str;
1684 ptr = format;
1685 while(*ptr)
1686 {
1687 type = *ptr++;
1688 if(*ptr == ':')
1689 {
1690 ptr++;
1691 size = get_type_size(type);
1692 sscanf(ptr, "%d", &num);
1693 full_size += size * num;
1694 if( (ptr = strchr(ptr,';')) )
1695 ptr++;
1696 else
1697 break;
1698 }
1699 }
1700
1701 full_size += 256;
1702 if(full_size > last_size)
1703 {
1704 if(last_size)
1705 free(last_buffer);
1706 last_buffer = malloc(full_size);
1707 last_size = full_size;
1708 }
1709 memset(last_buffer, 0, last_size);
1710 buffer_ptr = last_buffer;
1711 ptr = format;
1712 while(*ptr)
1713 {
1714 type = *ptr++;
1715 if(*ptr == ':')
1716 {
1717 ptr++;
1718 sscanf(ptr, "%d", &num);
1719 did_read_string(type, num, &buffer_ptr, &str_ptr);
1720 if(!str_ptr)
1721 break;
1722 if( (ptr = strchr(ptr,';')) )
1723 ptr++;
1724 else
1725 break;
1726 }
1727 else
1728 {
1729 did_read_string(type, 0, &buffer_ptr, &str_ptr);
1730 break;
1731 }
1732 }
1733 full_size = (int) ((char *)buffer_ptr - (char *)last_buffer);
1734 dic_cmnd_service(service,last_buffer,full_size);
1735}
1736
1737int read_str_int(char *str)
1738{
1739 int i;
1740 if((str[0] == '0') && (str[1] == 'x'))
1741 sscanf(str+2,"%x",&i);
1742 else
1743 sscanf(str,"%d",&i);
1744 return(i);
1745}
1746
1747int read_str_char(char *str, char *cc)
1748{
1749 int num;
1750
1751 if(str[0] == '\'')
1752 *cc = str[1];
1753 else if(str[0] == '\"')
1754 return(0);
1755 else if((str[0] == '0') && (str[1] == 'x'))
1756 {
1757 sscanf(str+2,"%x",&num);
1758 if(num <= 0xff)
1759 *cc = (char)num;
1760 else
1761 return(-1);
1762 }
1763 else if(isalpha(str[0]))
1764 return(-1);
1765 else
1766 {
1767 sscanf(str,"%d",&num);
1768 if(num <= 0xff)
1769 *cc = (char)num;
1770 else
1771 return(-1);
1772 }
1773 return(1);
1774}
1775
1776void did_read_string(char type, int num, void **buffer_ptr, char **str_ptr)
1777{
1778int i, ret = 0;
1779float ff;
1780double dd;
1781void *ptr;
1782char *strp, *ptr1;
1783char cc;
1784 short s;
1785
1786 strp = *str_ptr;
1787 ptr = *buffer_ptr;
1788 if(!num)
1789 num = 1000000;
1790 switch(type)
1791 {
1792 case 'L':
1793 case 'l':
1794 case 'I':
1795 case 'i':
1796 for(i = 0; i<num; i++)
1797 {
1798 *(int *)ptr = read_str_int(strp);
1799 ptr = (int *)ptr +1;
1800 if( (strp = strchr(strp,' ')) )
1801 strp++;
1802 else
1803 break;
1804 }
1805 break;
1806 case 'S':
1807 case 's':
1808 for(i = 0; i<num; i++)
1809 {
1810 s = (short)read_str_int(strp);
1811 *((short *)ptr) = s;
1812 ptr = (short *)ptr +1;
1813 if( (strp = strchr(strp,' ')) )
1814 strp++;
1815 else
1816 break;
1817 }
1818 break;
1819 case 'F':
1820 case 'f':
1821 for(i = 0; i<num; i++)
1822 {
1823 sscanf(strp,"%f",&ff);
1824 *(float *)ptr = ff;
1825 ptr = (float *)ptr +1;
1826 if( (strp = strchr(strp,' ')) )
1827 strp++;
1828 else
1829 break;
1830 }
1831 break;
1832 case 'D':
1833 case 'd':
1834 for(i = 0; i<num; i++)
1835 {
1836 sscanf(strp,"%f",&ff);
1837 dd = (double)ff;
1838 *(double *)ptr = dd;
1839 ptr = (double *)ptr +1;
1840 if( (strp = strchr(strp,' ')) )
1841 strp++;
1842 else
1843 break;
1844 }
1845 break;
1846 case 'C':
1847 case 'c':
1848 default:
1849 for(i = 0; i<num; i++)
1850 {
1851 if((ret = read_str_char(strp, &cc)) <= 0)
1852 break;
1853 *(char *)ptr = cc;
1854 ptr = (char *)ptr +1;
1855 if( (strp = strchr(strp,' ')) )
1856 strp++;
1857 else
1858 break;
1859 }
1860 if(ret <= 0)
1861 {
1862 if(!ret)
1863 {
1864 strp++;
1865 }
1866 num = strlen(strp)+1;
1867 strncpy((char *)ptr,strp,num);
1868 if( (ptr1 = (char *)strchr((char *)ptr,'\"')) )
1869 {
1870 num--;
1871 *ptr1 = '\0';
1872 }
1873 ptr = (char *)ptr + num;
1874 if( (strp = strchr(strp,' ')) )
1875 strp++;
1876 else
1877 break;
1878 }
1879 }
1880 *buffer_ptr = ptr;
1881 *str_ptr = strp;
1882}
Note: See TracBrowser for help on using the repository browser.