Fix memory leak.
[gnutls.git] / src / tls_test-gaa.c
blob9183f0dc8df9e8efe2d5706416b895b239b2d3db
1 /* File generated by GAA 1.6.6
2 */
3 #define GAA_NO_WIN32
4 #line 1 "tls_test.gaa"
7 /* C declarations */
9 #include <config.h>
10 #ifdef _WIN32
11 # include <io.h>
12 #endif
14 void tls_test_version(void);
16 #include <stdio.h>
17 #include <string.h>
18 #include <stdlib.h>
20 #ifndef GAA_NO_WIN32
21 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS)
22 #define GAA_WIN32
23 #endif
24 #endif
26 static void* gaa_malloc( size_t size) {
27 void* ret;
28 ret = malloc(size);
29 if (ret==NULL) {
30 fprintf(stderr, "gaa: could not allocate memory");
31 exit(1);
33 return ret;
36 static void __gaa_helpsingle(char short_name, char *name,
37 char *arg_desc, char *opt_help)
39 int col1, col3, col4, tabsize = 3, curr;
40 int i;
42 col1 = 5; /* Default values */
43 col3 = 30;
44 col4 = 70;
46 curr = 0;
47 for(i = 0; i < col1; i++)
49 printf(" ");
50 curr++;
52 if(short_name)
54 if(name && *name)
56 printf("-%c, ", short_name);
57 curr += 4;
59 else
61 printf("-%c ", short_name);
62 curr += 3;
65 if(name && *name)
67 printf("--%s ", name);
68 curr += 3 + strlen(name);
70 if(arg_desc && *arg_desc)
72 printf("%s ", arg_desc);
73 curr += 1 + strlen(arg_desc);
75 if(curr >= col3)
77 printf("\n");
78 curr = 0;
80 if(opt_help) /* let's print the option's help body */
82 const char *str = opt_help;
83 while(*str)
85 while(curr < col3)
87 printf(" ");
88 curr++;
90 switch(*str)
92 case '\n':
93 printf("\n");
94 curr = 0;
95 break;
96 case '\t':
99 printf(" ");
100 curr++;
102 while((curr - col3) % tabsize != 0 && curr < col4);
103 case ' ':
104 if(*str == ' ')
106 curr++;
107 printf(" ");
109 for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n'
110 && str[i] != '\t'; i++);
111 if(curr + i - 1 >= col4)
112 curr = col4;
113 break;
114 default:
115 printf("%c", *str);
116 curr++;
118 if(curr >= col4)
120 printf("\n");
121 curr = 0;
123 str++;
126 printf("\n");
129 void gaa_help(void)
131 printf("GNU TLS debug client\nUsage: gnutls-cli-debug [options] hostname\n\n\n");
132 __gaa_helpsingle('p', "port", "integer ", "The port to connect to.");
133 __gaa_helpsingle('d', "debug", "integer ", "Enable debugging");
134 __gaa_helpsingle('V', "verbose", "", "More verbose output");
135 __gaa_helpsingle('v', "version", "", "prints the program's version number");
136 __gaa_helpsingle('h', "help", "", "prints this help");
138 #line 100 "gaa.skel"
140 /* Copy of C area */
142 #line 104 "gaa.skel"
143 /* GAA HEADER */
144 #ifndef GAA_HEADER_POKY
145 #define GAA_HEADER_POKY
147 typedef struct _gaainfo gaainfo;
149 struct _gaainfo
151 #line 29 "tls_test.gaa"
152 char *rest_args;
153 #line 22 "tls_test.gaa"
154 int more_info;
155 #line 19 "tls_test.gaa"
156 int debug;
157 #line 16 "tls_test.gaa"
158 int pp;
160 #line 114 "gaa.skel"
163 #ifdef __cplusplus
164 extern "C"
166 #endif
168 int gaa(int argc, char *argv[], gaainfo *gaaval);
170 void gaa_help(void);
172 int gaa_file(const char *name, gaainfo *gaaval);
174 #ifdef __cplusplus
176 #endif
179 #endif
181 #line 135 "gaa.skel"
183 /* C declarations */
185 #define GAAERROR(x) \
187 gaa_error = 1; \
188 return x; \
191 static char *gaa_current_option;
192 static int gaa_error = 0;
194 /* Generated by gaa */
196 #include <string.h>
197 #include <stdlib.h>
200 #define GAA_OK -1
202 #define GAA_ERROR_NOMATCH 0
203 #define GAA_ERROR_NOTENOUGH_ARGS 1
204 #define GAA_ERROR_INVALID_ARG 2
205 #define GAA_ERROR_UNKNOWN 3
207 #define GAA_NOT_AN_OPTION 0
208 #define GAA_WORD_OPTION 1
209 #define GAA_LETTER_OPTION 2
210 #define GAA_MULTIPLE_OPTION 3
212 #define GAA_REST 0
213 #define GAA_NB_OPTION 5
214 #define GAAOPTID_help 1
215 #define GAAOPTID_version 2
216 #define GAAOPTID_verbose 3
217 #define GAAOPTID_debug 4
218 #define GAAOPTID_port 5
220 #line 168 "gaa.skel"
222 #define GAA_CHECK1STR(a,b) \
223 if(a[0] == str[0]) \
225 gaa_current_option = a; \
226 return b; \
229 #define GAA_CHECKSTR(a,b) \
230 if(strcmp(a,str) == 0) \
232 gaa_current_option = a; \
233 return b; \
236 #define GAA_TESTMOREARGS \
237 if(!OK) \
239 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
240 gaa_index++; \
241 if(gaa_last_non_option == gaa_index) \
242 return GAA_ERROR_NOTENOUGH_ARGS; \
245 #define GAA_TESTMOREOPTIONALARGS \
246 if(!OK) \
248 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
249 gaa_index++; \
250 if(gaa_last_non_option == gaa_index) \
251 OK = 1; \
254 #define GAA_FILL_2ARGS(target, func) \
255 target = func(GAAargv[gaa_index]); \
256 gaa_arg_used[gaa_index] = 1; \
257 if(gaa_error == 1) \
259 gaa_error = 0; \
260 return GAA_ERROR_INVALID_ARG; \
265 #define GAA_FILL(target, func, num) \
266 if(!OK) \
268 target = func(GAAargv[gaa_index]); \
269 gaa_arg_used[gaa_index] = 1; \
270 if(gaa_error == 1) \
272 gaa_error = 0; \
273 return GAA_ERROR_INVALID_ARG; \
275 num = 1; \
277 else \
279 num = 0; \
282 #define GAA_LIST_FILL(target, func, type ,num) \
283 if(!OK) \
285 num = 0; \
286 target = NULL; \
287 if ( gaa_last_non_option - gaa_index > 0) \
288 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
289 for(; gaa_index < gaa_last_non_option; gaa_index++) \
291 if(gaa_arg_used[gaa_index] == 0) \
293 GAA_FILL_2ARGS(target[num], func); \
294 num++; \
297 if(num == 0) \
298 return GAA_ERROR_NOTENOUGH_ARGS; \
301 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
302 if(!OK) \
304 num = 0; \
305 target = NULL; \
306 if ( gaa_last_non_option - gaa_index > 0) \
307 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
308 for(; gaa_index < gaa_last_non_option; gaa_index++) \
310 if(gaa_arg_used[gaa_index] == 0) \
312 GAA_FILL_2ARGS(target[num], func); \
313 num++; \
318 #define GAA_OBLIGAT(str) \
319 k = 0; \
320 for(i = 0; i < strlen(str); i++) \
322 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
323 if(j == GAA_ERROR_NOMATCH) \
325 printf("Error: invalid 'obligat' set\n"); \
326 exit(-1); \
328 if(opt_list[j] == 1) \
329 k = 1; \
331 if(k == 0) \
333 if(strlen(str) == 1) \
334 printf("You must give the -%s option\n", str); \
335 else \
336 printf("You must give at least one option of '%s'\n", str); \
337 return 0; \
340 #define GAA_INCOMP(str) \
341 k = 0; \
342 for(i = 0; i < strlen(str); i++) \
344 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
345 if(j == GAA_ERROR_NOMATCH) \
347 printf("Error: invalid 'obligat' set\n"); \
348 exit(-1); \
350 if(opt_list[j] == 1) \
351 k++; \
353 if(k > 1) \
355 printf("The options '%s' are incompatible\n", str); \
356 return 0; \
360 static char **GAAargv;
361 static int GAAargc;
362 static char *gaa_arg_used;
363 static int gaa_processing_file = 0;
364 static int inited = 0;
366 static int gaa_getint(char *arg)
368 int tmp;
369 char a;
370 if(sscanf(arg, "%d%c", &tmp, &a) < 1)
372 printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg);
373 GAAERROR(-1);
375 return tmp;
378 static char gaa_getchar(char *arg)
380 if(strlen(arg) != 1)
382 printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg);
383 GAAERROR(-1);
385 return arg[0];
388 static char* gaa_getstr(char *arg)
390 return arg;
392 static float gaa_getfloat(char *arg)
394 float tmp;
395 char a;
396 if(sscanf(arg, "%f%c", &tmp, &a) < 1)
398 printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg);
399 GAAERROR(-1);
401 return tmp;
403 /* option structures */
405 struct GAAOPTION_debug
407 int arg1;
408 int size1;
411 struct GAAOPTION_port
413 int arg1;
414 int size1;
416 #define GAA_REST_EXISTS
418 struct GAAREST
420 char* arg1;
421 int size1;
424 #line 349 "gaa.skel"
425 static int gaa_is_an_argument(char *str)
427 #ifdef GAA_WIN32
428 if(str[0] == '/' && str[1] != 0)
429 return GAA_MULTIPLE_OPTION;
430 #endif
431 if(str[0] != '-')
432 return GAA_NOT_AN_OPTION;
433 if(str[1] == 0)
434 return GAA_NOT_AN_OPTION;
435 if(str[1] == '-')
437 if(str[2] != 0)
438 return GAA_WORD_OPTION;
439 else
440 return GAA_NOT_AN_OPTION;
442 if(str[2] == 0)
443 return GAA_LETTER_OPTION;
444 else
445 return GAA_MULTIPLE_OPTION;
448 static int gaa_get_option_num(char *str, int status)
450 switch(status)
452 case GAA_LETTER_OPTION:
453 GAA_CHECK1STR("d", GAAOPTID_debug);
454 GAA_CHECK1STR("p", GAAOPTID_port);
455 case GAA_MULTIPLE_OPTION:
456 #line 375 "gaa.skel"
457 GAA_CHECK1STR("h", GAAOPTID_help);
458 GAA_CHECK1STR("v", GAAOPTID_version);
459 GAA_CHECK1STR("V", GAAOPTID_verbose);
461 #line 277 "gaa.skel"
462 break;
463 case GAA_WORD_OPTION:
464 GAA_CHECKSTR("help", GAAOPTID_help);
465 GAA_CHECKSTR("version", GAAOPTID_version);
466 GAA_CHECKSTR("verbose", GAAOPTID_verbose);
467 GAA_CHECKSTR("debug", GAAOPTID_debug);
468 GAA_CHECKSTR("port", GAAOPTID_port);
470 #line 281 "gaa.skel"
471 break;
472 default: break;
474 return GAA_ERROR_NOMATCH;
477 static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
479 int OK = 0;
480 int gaa_last_non_option;
481 struct GAAOPTION_debug GAATMP_debug;
482 struct GAAOPTION_port GAATMP_port;
484 #line 393 "gaa.skel"
485 #ifdef GAA_REST_EXISTS
486 struct GAAREST GAAREST_tmp;
487 #endif
489 opt_list[gaa_num] = 1;
491 for(gaa_last_non_option = gaa_index;
492 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
493 gaa_last_non_option++);
495 if(gaa_num == GAA_REST)
497 gaa_index = 1;
498 gaa_last_non_option = GAAargc;
501 switch(gaa_num)
503 case GAAOPTID_help:
504 OK = 0;
505 #line 26 "tls_test.gaa"
506 { gaa_help(); exit(0); ;};
508 return GAA_OK;
509 break;
510 case GAAOPTID_version:
511 OK = 0;
512 #line 25 "tls_test.gaa"
513 { tls_test_version(); exit(0); ;};
515 return GAA_OK;
516 break;
517 case GAAOPTID_verbose:
518 OK = 0;
519 #line 23 "tls_test.gaa"
520 { gaaval->more_info += 1 ;};
522 return GAA_OK;
523 break;
524 case GAAOPTID_debug:
525 OK = 0;
526 GAA_TESTMOREARGS;
527 GAA_FILL(GAATMP_debug.arg1, gaa_getint, GAATMP_debug.size1);
528 gaa_index++;
529 #line 20 "tls_test.gaa"
530 { gaaval->debug = GAATMP_debug.arg1 ;};
532 return GAA_OK;
533 break;
534 case GAAOPTID_port:
535 OK = 0;
536 GAA_TESTMOREARGS;
537 GAA_FILL(GAATMP_port.arg1, gaa_getint, GAATMP_port.size1);
538 gaa_index++;
539 #line 17 "tls_test.gaa"
540 { gaaval->pp = GAATMP_port.arg1 ;};
542 return GAA_OK;
543 break;
544 case GAA_REST:
545 GAA_TESTMOREARGS;
546 GAA_FILL(GAAREST_tmp.arg1, gaa_getstr, GAAREST_tmp.size1);
547 gaa_index++;
548 #line 30 "tls_test.gaa"
549 { gaaval->rest_args = GAAREST_tmp.arg1; ;};
551 return GAA_OK;
552 break;
554 #line 413 "gaa.skel"
555 default: break;
557 return GAA_ERROR_UNKNOWN;
560 int gaa(int argc, char **argv, gaainfo *gaaval)
562 int tmp1, tmp2;
563 int i, j;
564 char *opt_list;
566 GAAargv = argv;
567 GAAargc = argc;
569 opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1);
571 for(i = 0; i < GAA_NB_OPTION + 1; i++)
572 opt_list[i] = 0;
573 /* initialization */
574 if(inited == 0)
577 #line 32 "tls_test.gaa"
578 { gaaval->rest_args=NULL; gaaval->pp = 443; gaaval->more_info = 0; ;};
581 inited = 1;
582 #line 438 "gaa.skel"
583 gaa_arg_used = NULL;
585 if (argc > 0) {
586 gaa_arg_used = gaa_malloc(argc * sizeof(char));
589 for(i = 1; i < argc; i++)
590 gaa_arg_used[i] = 0;
591 for(i = 1; i < argc; i++)
593 if(gaa_arg_used[i] == 0)
595 j = 0;
596 tmp1 = gaa_is_an_argument(GAAargv[i]);
597 switch(tmp1)
599 case GAA_WORD_OPTION:
600 j++;
601 case GAA_LETTER_OPTION:
602 j++;
603 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
604 if(tmp2 == GAA_ERROR_NOMATCH)
606 printf("Invalid option '%s'\n", argv[i]+j);
607 return 0;
609 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
611 case GAA_ERROR_NOTENOUGH_ARGS:
612 printf("'%s': not enough arguments\n",gaa_current_option);
613 return 0;
614 case GAA_ERROR_INVALID_ARG:
615 printf("Invalid arguments\n");
616 return 0;
617 case GAA_OK:
618 break;
619 default:
620 printf("Unknown error\n");
622 gaa_arg_used[i] = 1;
623 break;
624 case GAA_MULTIPLE_OPTION:
625 for(j = 1; j < strlen(argv[i]); j++)
627 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
628 if(tmp2 == GAA_ERROR_NOMATCH)
630 printf("Invalid option '%c'\n", *(argv[i]+j));
631 return 0;
633 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
635 case GAA_ERROR_NOTENOUGH_ARGS:
636 printf("'%s': not enough arguments\n",gaa_current_option);
637 return 0;
638 case GAA_ERROR_INVALID_ARG:
639 printf("Invalid arguments\n");
640 return 0;
641 case GAA_OK:
642 break;
643 default:
644 printf("Unknown error\n");
647 gaa_arg_used[i] = 1;
648 break;
649 default: break;
653 if(gaa_processing_file == 0)
656 #line 507 "gaa.skel"
657 #ifdef GAA_REST_EXISTS
658 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
660 case GAA_ERROR_NOTENOUGH_ARGS:
661 printf("Rest: not enough arguments\n");
662 return 0;
663 case GAA_ERROR_INVALID_ARG:
664 printf("Invalid arguments\n");
665 return 0;
666 case GAA_OK:
667 break;
668 default:
669 printf("Unknown error\n");
671 #endif
673 for(i = 1; i < argc; i++)
675 if(gaa_arg_used[i] == 0)
677 printf("Too many arguments\n");
678 return 0;
681 free(gaa_arg_used);
682 free(opt_list);
683 return -1;
686 struct gaastrnode
688 char *str;
689 struct gaastrnode *next;
692 typedef struct gaastrnode gaa_str_node;
694 static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
696 int pos_ini;
697 int a;
698 int i = 0, len = 0, newline = 0;
700 if(argc == 1) {
701 newline = 1;
702 len = 2;
705 a = fgetc( file);
706 if (a == EOF) return 0;
708 while(a == ' ' || a == 9 || a == '\n')
710 if(a == '\n')
712 newline=1;
713 len = 2;
715 a = fgetc( file);
716 if (a == EOF) return 0;
719 pos_ini = ftell(file) - 1;
721 while(a != ' ' && a != 9 && a != '\n')
724 len++;
725 a = fgetc( file);
726 if(a==EOF) return 0; /* a = ' '; */
729 len += 1;
730 tmp_str->str = gaa_malloc((len) * sizeof(char));
732 if(newline == 1)
734 tmp_str->str[0] = '-';
735 tmp_str->str[1] = '-';
736 i = 2;
738 else
740 i = 0;
743 fseek(file,pos_ini, SEEK_SET);
746 a = fgetc( file);
748 if (a == EOF) {
749 i+=2;
750 break;
752 tmp_str->str[i] = a;
753 i++;
755 while(a != ' ' && a != 9 && a != '\n' && i < len);
757 tmp_str->str[i - 1] = 0;
759 fseek(file,- 1, SEEK_CUR);
760 /* printf("%d\n", ftell(file)); */
762 return -1;
765 int gaa_file(const char *name, gaainfo *gaaval)
767 gaa_str_node *first_str, **tmp_str, *tmp_str2;
768 int rval, i;
769 char **argv;
770 int argc = 0;
771 FILE *file;
773 gaa_processing_file = 1;
775 if((file = fopen(name, "r")) == NULL)
777 printf("Couldn't open '%s' configuration file for reading\n", name);
778 return 1;
781 tmp_str = &first_str;
784 argc++;
785 *tmp_str = gaa_malloc(sizeof(gaa_str_node));
787 (*tmp_str)->str = NULL;
788 (*tmp_str)->next = NULL;
790 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
791 tmp_str = &((*tmp_str)->next);
793 while(rval == -1);
795 if(rval == 1)
796 return 0;
798 argv = gaa_malloc((1 + argc) * sizeof(char*));
800 tmp_str2 = first_str;
801 argv[0] = "cfg";
802 for(i = 1; i < argc; i++)
804 argv[i] = tmp_str2->str;
805 tmp_str2 = tmp_str2->next;
808 rval = gaa(argc, argv, gaaval);
809 gaa_processing_file = 0;
810 return rval;