Add.
[gnutls.git] / src / psk-gaa.c
blob5a77c3c3ee5108ef794186719bddaba41ddc89f8
1 /* File generated by GAA 1.6.6
2 */
3 #define GAA_NO_WIN32
4 #line 1 "psk.gaa"
7 /* C declarations */
9 #include <config.h>
10 #ifdef _WIN32
11 # include <io.h>
12 #endif
14 void psktool_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("PSKtool help\nUsage : psktool [options]\n");
132 __gaa_helpsingle('u', "username", "username ", "specify username.");
133 __gaa_helpsingle('p', "passwd", "FILE ", "specify a password file.");
134 __gaa_helpsingle('s', "keysize", "SIZE ", "specify the key size in bytes.");
135 __gaa_helpsingle('v', "version", "", "prints the program's version number");
136 __gaa_helpsingle('h', "help", "", "shows this help text");
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 22 "psk.gaa"
152 int key_size;
153 #line 19 "psk.gaa"
154 char *passwd;
155 #line 16 "psk.gaa"
156 char *username;
158 #line 114 "gaa.skel"
161 #ifdef __cplusplus
162 extern "C"
164 #endif
166 int gaa(int argc, char *argv[], gaainfo *gaaval);
168 void gaa_help(void);
170 int gaa_file(const char *name, gaainfo *gaaval);
172 #ifdef __cplusplus
174 #endif
177 #endif
179 #line 135 "gaa.skel"
181 /* C declarations */
183 #define GAAERROR(x) \
185 gaa_error = 1; \
186 return x; \
189 static char *gaa_current_option;
190 static int gaa_error = 0;
192 /* Generated by gaa */
194 #include <string.h>
195 #include <stdlib.h>
198 #define GAA_OK -1
200 #define GAA_ERROR_NOMATCH 0
201 #define GAA_ERROR_NOTENOUGH_ARGS 1
202 #define GAA_ERROR_INVALID_ARG 2
203 #define GAA_ERROR_UNKNOWN 3
205 #define GAA_NOT_AN_OPTION 0
206 #define GAA_WORD_OPTION 1
207 #define GAA_LETTER_OPTION 2
208 #define GAA_MULTIPLE_OPTION 3
210 #define GAA_REST 0
211 #define GAA_NB_OPTION 5
212 #define GAAOPTID_help 1
213 #define GAAOPTID_version 2
214 #define GAAOPTID_keysize 3
215 #define GAAOPTID_passwd 4
216 #define GAAOPTID_username 5
218 #line 168 "gaa.skel"
220 #define GAA_CHECK1STR(a,b) \
221 if(a[0] == str[0]) \
223 gaa_current_option = a; \
224 return b; \
227 #define GAA_CHECKSTR(a,b) \
228 if(strcmp(a,str) == 0) \
230 gaa_current_option = a; \
231 return b; \
234 #define GAA_TESTMOREARGS \
235 if(!OK) \
237 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
238 gaa_index++; \
239 if(gaa_last_non_option == gaa_index) \
240 return GAA_ERROR_NOTENOUGH_ARGS; \
243 #define GAA_TESTMOREOPTIONALARGS \
244 if(!OK) \
246 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
247 gaa_index++; \
248 if(gaa_last_non_option == gaa_index) \
249 OK = 1; \
252 #define GAA_FILL_2ARGS(target, func) \
253 target = func(GAAargv[gaa_index]); \
254 gaa_arg_used[gaa_index] = 1; \
255 if(gaa_error == 1) \
257 gaa_error = 0; \
258 return GAA_ERROR_INVALID_ARG; \
263 #define GAA_FILL(target, func, num) \
264 if(!OK) \
266 target = func(GAAargv[gaa_index]); \
267 gaa_arg_used[gaa_index] = 1; \
268 if(gaa_error == 1) \
270 gaa_error = 0; \
271 return GAA_ERROR_INVALID_ARG; \
273 num = 1; \
275 else \
277 num = 0; \
280 #define GAA_LIST_FILL(target, func, type ,num) \
281 if(!OK) \
283 num = 0; \
284 target = NULL; \
285 if ( gaa_last_non_option - gaa_index > 0) \
286 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
287 for(; gaa_index < gaa_last_non_option; gaa_index++) \
289 if(gaa_arg_used[gaa_index] == 0) \
291 GAA_FILL_2ARGS(target[num], func); \
292 num++; \
295 if(num == 0) \
296 return GAA_ERROR_NOTENOUGH_ARGS; \
299 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
300 if(!OK) \
302 num = 0; \
303 target = NULL; \
304 if ( gaa_last_non_option - gaa_index > 0) \
305 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
306 for(; gaa_index < gaa_last_non_option; gaa_index++) \
308 if(gaa_arg_used[gaa_index] == 0) \
310 GAA_FILL_2ARGS(target[num], func); \
311 num++; \
316 #define GAA_OBLIGAT(str) \
317 k = 0; \
318 for(i = 0; i < strlen(str); i++) \
320 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
321 if(j == GAA_ERROR_NOMATCH) \
323 printf("Error: invalid 'obligat' set\n"); \
324 exit(-1); \
326 if(opt_list[j] == 1) \
327 k = 1; \
329 if(k == 0) \
331 if(strlen(str) == 1) \
332 printf("You must give the -%s option\n", str); \
333 else \
334 printf("You must give at least one option of '%s'\n", str); \
335 return 0; \
338 #define GAA_INCOMP(str) \
339 k = 0; \
340 for(i = 0; i < strlen(str); i++) \
342 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
343 if(j == GAA_ERROR_NOMATCH) \
345 printf("Error: invalid 'obligat' set\n"); \
346 exit(-1); \
348 if(opt_list[j] == 1) \
349 k++; \
351 if(k > 1) \
353 printf("The options '%s' are incompatible\n", str); \
354 return 0; \
358 static char **GAAargv;
359 static int GAAargc;
360 static char *gaa_arg_used;
361 static int gaa_processing_file = 0;
362 static int inited = 0;
364 static int gaa_getint(char *arg)
366 int tmp;
367 char a;
368 if(sscanf(arg, "%d%c", &tmp, &a) < 1)
370 printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg);
371 GAAERROR(-1);
373 return tmp;
376 static char gaa_getchar(char *arg)
378 if(strlen(arg) != 1)
380 printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg);
381 GAAERROR(-1);
383 return arg[0];
386 static char* gaa_getstr(char *arg)
388 return arg;
390 static float gaa_getfloat(char *arg)
392 float tmp;
393 char a;
394 if(sscanf(arg, "%f%c", &tmp, &a) < 1)
396 printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg);
397 GAAERROR(-1);
399 return tmp;
401 /* option structures */
403 struct GAAOPTION_keysize
405 int arg1;
406 int size1;
409 struct GAAOPTION_passwd
411 char* arg1;
412 int size1;
415 struct GAAOPTION_username
417 char* arg1;
418 int size1;
421 #line 349 "gaa.skel"
422 static int gaa_is_an_argument(char *str)
424 #ifdef GAA_WIN32
425 if(str[0] == '/' && str[1] != 0)
426 return GAA_MULTIPLE_OPTION;
427 #endif
428 if(str[0] != '-')
429 return GAA_NOT_AN_OPTION;
430 if(str[1] == 0)
431 return GAA_NOT_AN_OPTION;
432 if(str[1] == '-')
434 if(str[2] != 0)
435 return GAA_WORD_OPTION;
436 else
437 return GAA_NOT_AN_OPTION;
439 if(str[2] == 0)
440 return GAA_LETTER_OPTION;
441 else
442 return GAA_MULTIPLE_OPTION;
445 static int gaa_get_option_num(char *str, int status)
447 switch(status)
449 case GAA_LETTER_OPTION:
450 GAA_CHECK1STR("s", GAAOPTID_keysize);
451 GAA_CHECK1STR("p", GAAOPTID_passwd);
452 GAA_CHECK1STR("u", GAAOPTID_username);
453 case GAA_MULTIPLE_OPTION:
454 #line 375 "gaa.skel"
455 GAA_CHECK1STR("h", GAAOPTID_help);
456 GAA_CHECK1STR("v", GAAOPTID_version);
458 #line 277 "gaa.skel"
459 break;
460 case GAA_WORD_OPTION:
461 GAA_CHECKSTR("help", GAAOPTID_help);
462 GAA_CHECKSTR("version", GAAOPTID_version);
463 GAA_CHECKSTR("keysize", GAAOPTID_keysize);
464 GAA_CHECKSTR("passwd", GAAOPTID_passwd);
465 GAA_CHECKSTR("username", GAAOPTID_username);
467 #line 281 "gaa.skel"
468 break;
469 default: break;
471 return GAA_ERROR_NOMATCH;
474 static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
476 int OK = 0;
477 int gaa_last_non_option;
478 struct GAAOPTION_keysize GAATMP_keysize;
479 struct GAAOPTION_passwd GAATMP_passwd;
480 struct GAAOPTION_username GAATMP_username;
482 #line 393 "gaa.skel"
483 #ifdef GAA_REST_EXISTS
484 struct GAAREST GAAREST_tmp;
485 #endif
487 opt_list[gaa_num] = 1;
489 for(gaa_last_non_option = gaa_index;
490 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
491 gaa_last_non_option++);
493 if(gaa_num == GAA_REST)
495 gaa_index = 1;
496 gaa_last_non_option = GAAargc;
499 switch(gaa_num)
501 case GAAOPTID_help:
502 OK = 0;
503 #line 26 "psk.gaa"
504 { gaa_help(); exit(0); ;};
506 return GAA_OK;
507 break;
508 case GAAOPTID_version:
509 OK = 0;
510 #line 25 "psk.gaa"
511 { psktool_version(); exit(0); ;};
513 return GAA_OK;
514 break;
515 case GAAOPTID_keysize:
516 OK = 0;
517 GAA_TESTMOREARGS;
518 GAA_FILL(GAATMP_keysize.arg1, gaa_getint, GAATMP_keysize.size1);
519 gaa_index++;
520 #line 23 "psk.gaa"
521 { gaaval->key_size = GAATMP_keysize.arg1 ;};
523 return GAA_OK;
524 break;
525 case GAAOPTID_passwd:
526 OK = 0;
527 GAA_TESTMOREARGS;
528 GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1);
529 gaa_index++;
530 #line 20 "psk.gaa"
531 { gaaval->passwd = GAATMP_passwd.arg1 ;};
533 return GAA_OK;
534 break;
535 case GAAOPTID_username:
536 OK = 0;
537 GAA_TESTMOREARGS;
538 GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1);
539 gaa_index++;
540 #line 17 "psk.gaa"
541 { gaaval->username = GAATMP_username.arg1 ;};
543 return GAA_OK;
544 break;
546 #line 413 "gaa.skel"
547 default: break;
549 return GAA_ERROR_UNKNOWN;
552 int gaa(int argc, char **argv, gaainfo *gaaval)
554 int tmp1, tmp2;
555 int i, j;
556 char *opt_list;
558 GAAargv = argv;
559 GAAargc = argc;
561 opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1);
563 for(i = 0; i < GAA_NB_OPTION + 1; i++)
564 opt_list[i] = 0;
565 /* initialization */
566 if(inited == 0)
569 #line 28 "psk.gaa"
570 { gaaval->username=NULL; gaaval->passwd=NULL; gaaval->key_size = NULL; ;};
573 inited = 1;
574 #line 438 "gaa.skel"
575 gaa_arg_used = NULL;
577 if (argc > 0) {
578 gaa_arg_used = gaa_malloc(argc * sizeof(char));
581 for(i = 1; i < argc; i++)
582 gaa_arg_used[i] = 0;
583 for(i = 1; i < argc; i++)
585 if(gaa_arg_used[i] == 0)
587 j = 0;
588 tmp1 = gaa_is_an_argument(GAAargv[i]);
589 switch(tmp1)
591 case GAA_WORD_OPTION:
592 j++;
593 case GAA_LETTER_OPTION:
594 j++;
595 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
596 if(tmp2 == GAA_ERROR_NOMATCH)
598 printf("Invalid option '%s'\n", argv[i]+j);
599 return 0;
601 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
603 case GAA_ERROR_NOTENOUGH_ARGS:
604 printf("'%s': not enough arguments\n",gaa_current_option);
605 return 0;
606 case GAA_ERROR_INVALID_ARG:
607 printf("Invalid arguments\n");
608 return 0;
609 case GAA_OK:
610 break;
611 default:
612 printf("Unknown error\n");
614 gaa_arg_used[i] = 1;
615 break;
616 case GAA_MULTIPLE_OPTION:
617 for(j = 1; j < strlen(argv[i]); j++)
619 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
620 if(tmp2 == GAA_ERROR_NOMATCH)
622 printf("Invalid option '%c'\n", *(argv[i]+j));
623 return 0;
625 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
627 case GAA_ERROR_NOTENOUGH_ARGS:
628 printf("'%s': not enough arguments\n",gaa_current_option);
629 return 0;
630 case GAA_ERROR_INVALID_ARG:
631 printf("Invalid arguments\n");
632 return 0;
633 case GAA_OK:
634 break;
635 default:
636 printf("Unknown error\n");
639 gaa_arg_used[i] = 1;
640 break;
641 default: break;
645 if(gaa_processing_file == 0)
648 #line 507 "gaa.skel"
649 #ifdef GAA_REST_EXISTS
650 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
652 case GAA_ERROR_NOTENOUGH_ARGS:
653 printf("Rest: not enough arguments\n");
654 return 0;
655 case GAA_ERROR_INVALID_ARG:
656 printf("Invalid arguments\n");
657 return 0;
658 case GAA_OK:
659 break;
660 default:
661 printf("Unknown error\n");
663 #endif
665 for(i = 1; i < argc; i++)
667 if(gaa_arg_used[i] == 0)
669 printf("Too many arguments\n");
670 return 0;
673 free(gaa_arg_used);
674 free(opt_list);
675 return -1;
678 struct gaastrnode
680 char *str;
681 struct gaastrnode *next;
684 typedef struct gaastrnode gaa_str_node;
686 static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
688 int pos_ini;
689 int a;
690 int i = 0, len = 0, newline = 0;
692 if(argc == 1) {
693 newline = 1;
694 len = 2;
697 a = fgetc( file);
698 if (a == EOF) return 0;
700 while(a == ' ' || a == 9 || a == '\n')
702 if(a == '\n')
704 newline=1;
705 len = 2;
707 a = fgetc( file);
708 if (a == EOF) return 0;
711 pos_ini = ftell(file) - 1;
713 while(a != ' ' && a != 9 && a != '\n')
716 len++;
717 a = fgetc( file);
718 if(a==EOF) return 0; /* a = ' '; */
721 len += 1;
722 tmp_str->str = gaa_malloc((len) * sizeof(char));
724 if(newline == 1)
726 tmp_str->str[0] = '-';
727 tmp_str->str[1] = '-';
728 i = 2;
730 else
732 i = 0;
735 fseek(file,pos_ini, SEEK_SET);
738 a = fgetc( file);
740 if (a == EOF) {
741 i+=2;
742 break;
744 tmp_str->str[i] = a;
745 i++;
747 while(a != ' ' && a != 9 && a != '\n' && i < len);
749 tmp_str->str[i - 1] = 0;
751 fseek(file,- 1, SEEK_CUR);
752 /* printf("%d\n", ftell(file)); */
754 return -1;
757 int gaa_file(const char *name, gaainfo *gaaval)
759 gaa_str_node *first_str, **tmp_str, *tmp_str2;
760 int rval, i;
761 char **argv;
762 int argc = 0;
763 FILE *file;
765 gaa_processing_file = 1;
767 if((file = fopen(name, "r")) == NULL)
769 printf("Couldn't open '%s' configuration file for reading\n", name);
770 return 1;
773 tmp_str = &first_str;
776 argc++;
777 *tmp_str = gaa_malloc(sizeof(gaa_str_node));
779 (*tmp_str)->str = NULL;
780 (*tmp_str)->next = NULL;
782 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
783 tmp_str = &((*tmp_str)->next);
785 while(rval == -1);
787 if(rval == 1)
788 return 0;
790 argv = gaa_malloc((1 + argc) * sizeof(char*));
792 tmp_str2 = first_str;
793 argv[0] = "cfg";
794 for(i = 1; i < argc; i++)
796 argv[i] = tmp_str2->str;
797 tmp_str2 = tmp_str2->next;
800 rval = gaa(argc, argv, gaaval);
801 gaa_processing_file = 0;
802 return rval;