Dist libgcrypt.supp.
[gnutls.git] / src / psk-gaa.c
blobf3e58592f7a3f345c1459ca1cbfe0c1aed67fc2c
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('n', "netconf-hint", "HINT ", "derive key from Netconf password, using HINT as the psk_identity_hint.");
135 __gaa_helpsingle('s', "keysize", "SIZE ", "specify the key size in bytes.");
136 __gaa_helpsingle('v', "version", "", "prints the program's version number");
137 __gaa_helpsingle('h', "help", "", "shows this help text");
139 #line 100 "gaa.skel"
141 /* Copy of C area */
143 #line 104 "gaa.skel"
144 /* GAA HEADER */
145 #ifndef GAA_HEADER_POKY
146 #define GAA_HEADER_POKY
148 typedef struct _gaainfo gaainfo;
150 struct _gaainfo
152 #line 25 "psk.gaa"
153 int key_size;
154 #line 22 "psk.gaa"
155 char *netconf_hint;
156 #line 19 "psk.gaa"
157 char *passwd;
158 #line 16 "psk.gaa"
159 char *username;
161 #line 114 "gaa.skel"
164 #ifdef __cplusplus
165 extern "C"
167 #endif
169 int gaa(int argc, char *argv[], gaainfo *gaaval);
171 void gaa_help(void);
173 int gaa_file(const char *name, gaainfo *gaaval);
175 #ifdef __cplusplus
177 #endif
180 #endif
182 #line 135 "gaa.skel"
184 /* C declarations */
186 #define GAAERROR(x) \
188 gaa_error = 1; \
189 return x; \
192 static char *gaa_current_option;
193 static int gaa_error = 0;
195 /* Generated by gaa */
197 #include <string.h>
198 #include <stdlib.h>
201 #define GAA_OK -1
203 #define GAA_ERROR_NOMATCH 0
204 #define GAA_ERROR_NOTENOUGH_ARGS 1
205 #define GAA_ERROR_INVALID_ARG 2
206 #define GAA_ERROR_UNKNOWN 3
208 #define GAA_NOT_AN_OPTION 0
209 #define GAA_WORD_OPTION 1
210 #define GAA_LETTER_OPTION 2
211 #define GAA_MULTIPLE_OPTION 3
213 #define GAA_REST 0
214 #define GAA_NB_OPTION 6
215 #define GAAOPTID_help 1
216 #define GAAOPTID_version 2
217 #define GAAOPTID_keysize 3
218 #define GAAOPTID_netconf_hint 4
219 #define GAAOPTID_passwd 5
220 #define GAAOPTID_username 6
222 #line 168 "gaa.skel"
224 #define GAA_CHECK1STR(a,b) \
225 if(a[0] == str[0]) \
227 gaa_current_option = a; \
228 return b; \
231 #define GAA_CHECKSTR(a,b) \
232 if(strcmp(a,str) == 0) \
234 gaa_current_option = a; \
235 return b; \
238 #define GAA_TESTMOREARGS \
239 if(!OK) \
241 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
242 gaa_index++; \
243 if(gaa_last_non_option == gaa_index) \
244 return GAA_ERROR_NOTENOUGH_ARGS; \
247 #define GAA_TESTMOREOPTIONALARGS \
248 if(!OK) \
250 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
251 gaa_index++; \
252 if(gaa_last_non_option == gaa_index) \
253 OK = 1; \
256 #define GAA_FILL_2ARGS(target, func) \
257 target = func(GAAargv[gaa_index]); \
258 gaa_arg_used[gaa_index] = 1; \
259 if(gaa_error == 1) \
261 gaa_error = 0; \
262 return GAA_ERROR_INVALID_ARG; \
267 #define GAA_FILL(target, func, num) \
268 if(!OK) \
270 target = func(GAAargv[gaa_index]); \
271 gaa_arg_used[gaa_index] = 1; \
272 if(gaa_error == 1) \
274 gaa_error = 0; \
275 return GAA_ERROR_INVALID_ARG; \
277 num = 1; \
279 else \
281 num = 0; \
284 #define GAA_LIST_FILL(target, func, type ,num) \
285 if(!OK) \
287 num = 0; \
288 target = NULL; \
289 if ( gaa_last_non_option - gaa_index > 0) \
290 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
291 for(; gaa_index < gaa_last_non_option; gaa_index++) \
293 if(gaa_arg_used[gaa_index] == 0) \
295 GAA_FILL_2ARGS(target[num], func); \
296 num++; \
299 if(num == 0) \
300 return GAA_ERROR_NOTENOUGH_ARGS; \
303 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
304 if(!OK) \
306 num = 0; \
307 target = NULL; \
308 if ( gaa_last_non_option - gaa_index > 0) \
309 target = gaa_malloc((gaa_last_non_option - gaa_index) * sizeof(type));\
310 for(; gaa_index < gaa_last_non_option; gaa_index++) \
312 if(gaa_arg_used[gaa_index] == 0) \
314 GAA_FILL_2ARGS(target[num], func); \
315 num++; \
320 #define GAA_OBLIGAT(str) \
321 k = 0; \
322 for(i = 0; i < strlen(str); i++) \
324 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
325 if(j == GAA_ERROR_NOMATCH) \
327 printf("Error: invalid 'obligat' set\n"); \
328 exit(-1); \
330 if(opt_list[j] == 1) \
331 k = 1; \
333 if(k == 0) \
335 if(strlen(str) == 1) \
336 printf("You must give the -%s option\n", str); \
337 else \
338 printf("You must give at least one option of '%s'\n", str); \
339 return 0; \
342 #define GAA_INCOMP(str) \
343 k = 0; \
344 for(i = 0; i < strlen(str); i++) \
346 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
347 if(j == GAA_ERROR_NOMATCH) \
349 printf("Error: invalid 'obligat' set\n"); \
350 exit(-1); \
352 if(opt_list[j] == 1) \
353 k++; \
355 if(k > 1) \
357 printf("The options '%s' are incompatible\n", str); \
358 return 0; \
362 static char **GAAargv;
363 static int GAAargc;
364 static char *gaa_arg_used;
365 static int gaa_processing_file = 0;
366 static int inited = 0;
368 static int gaa_getint(char *arg)
370 int tmp;
371 char a;
372 if(sscanf(arg, "%d%c", &tmp, &a) < 1)
374 printf("Option %s: '%s' isn't an integer\n", gaa_current_option, arg);
375 GAAERROR(-1);
377 return tmp;
380 static char gaa_getchar(char *arg)
382 if(strlen(arg) != 1)
384 printf("Option %s: '%s' isn't an character\n", gaa_current_option, arg);
385 GAAERROR(-1);
387 return arg[0];
390 static char* gaa_getstr(char *arg)
392 return arg;
394 static float gaa_getfloat(char *arg)
396 float tmp;
397 char a;
398 if(sscanf(arg, "%f%c", &tmp, &a) < 1)
400 printf("Option %s: '%s' isn't a float number\n", gaa_current_option, arg);
401 GAAERROR(-1);
403 return tmp;
405 /* option structures */
407 struct GAAOPTION_keysize
409 int arg1;
410 int size1;
413 struct GAAOPTION_netconf_hint
415 char* arg1;
416 int size1;
419 struct GAAOPTION_passwd
421 char* arg1;
422 int size1;
425 struct GAAOPTION_username
427 char* arg1;
428 int size1;
431 #line 349 "gaa.skel"
432 static int gaa_is_an_argument(char *str)
434 #ifdef GAA_WIN32
435 if(str[0] == '/' && str[1] != 0)
436 return GAA_MULTIPLE_OPTION;
437 #endif
438 if(str[0] != '-')
439 return GAA_NOT_AN_OPTION;
440 if(str[1] == 0)
441 return GAA_NOT_AN_OPTION;
442 if(str[1] == '-')
444 if(str[2] != 0)
445 return GAA_WORD_OPTION;
446 else
447 return GAA_NOT_AN_OPTION;
449 if(str[2] == 0)
450 return GAA_LETTER_OPTION;
451 else
452 return GAA_MULTIPLE_OPTION;
455 static int gaa_get_option_num(char *str, int status)
457 switch(status)
459 case GAA_LETTER_OPTION:
460 GAA_CHECK1STR("s", GAAOPTID_keysize);
461 GAA_CHECK1STR("n", GAAOPTID_netconf_hint);
462 GAA_CHECK1STR("p", GAAOPTID_passwd);
463 GAA_CHECK1STR("u", GAAOPTID_username);
464 case GAA_MULTIPLE_OPTION:
465 #line 375 "gaa.skel"
466 GAA_CHECK1STR("h", GAAOPTID_help);
467 GAA_CHECK1STR("v", GAAOPTID_version);
469 #line 277 "gaa.skel"
470 break;
471 case GAA_WORD_OPTION:
472 GAA_CHECKSTR("help", GAAOPTID_help);
473 GAA_CHECKSTR("version", GAAOPTID_version);
474 GAA_CHECKSTR("keysize", GAAOPTID_keysize);
475 GAA_CHECKSTR("netconf-hint", GAAOPTID_netconf_hint);
476 GAA_CHECKSTR("passwd", GAAOPTID_passwd);
477 GAA_CHECKSTR("username", GAAOPTID_username);
479 #line 281 "gaa.skel"
480 break;
481 default: break;
483 return GAA_ERROR_NOMATCH;
486 static int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
488 int OK = 0;
489 int gaa_last_non_option;
490 struct GAAOPTION_keysize GAATMP_keysize;
491 struct GAAOPTION_netconf_hint GAATMP_netconf_hint;
492 struct GAAOPTION_passwd GAATMP_passwd;
493 struct GAAOPTION_username GAATMP_username;
495 #line 393 "gaa.skel"
496 #ifdef GAA_REST_EXISTS
497 struct GAAREST GAAREST_tmp;
498 #endif
500 opt_list[gaa_num] = 1;
502 for(gaa_last_non_option = gaa_index;
503 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
504 gaa_last_non_option++);
506 if(gaa_num == GAA_REST)
508 gaa_index = 1;
509 gaa_last_non_option = GAAargc;
512 switch(gaa_num)
514 case GAAOPTID_help:
515 OK = 0;
516 #line 29 "psk.gaa"
517 { gaa_help(); exit(0); ;};
519 return GAA_OK;
520 break;
521 case GAAOPTID_version:
522 OK = 0;
523 #line 28 "psk.gaa"
524 { psktool_version(); exit(0); ;};
526 return GAA_OK;
527 break;
528 case GAAOPTID_keysize:
529 OK = 0;
530 GAA_TESTMOREARGS;
531 GAA_FILL(GAATMP_keysize.arg1, gaa_getint, GAATMP_keysize.size1);
532 gaa_index++;
533 #line 26 "psk.gaa"
534 { gaaval->key_size = GAATMP_keysize.arg1 ;};
536 return GAA_OK;
537 break;
538 case GAAOPTID_netconf_hint:
539 OK = 0;
540 GAA_TESTMOREARGS;
541 GAA_FILL(GAATMP_netconf_hint.arg1, gaa_getstr, GAATMP_netconf_hint.size1);
542 gaa_index++;
543 #line 23 "psk.gaa"
544 { gaaval->netconf_hint = GAATMP_netconf_hint.arg1 ;};
546 return GAA_OK;
547 break;
548 case GAAOPTID_passwd:
549 OK = 0;
550 GAA_TESTMOREARGS;
551 GAA_FILL(GAATMP_passwd.arg1, gaa_getstr, GAATMP_passwd.size1);
552 gaa_index++;
553 #line 20 "psk.gaa"
554 { gaaval->passwd = GAATMP_passwd.arg1 ;};
556 return GAA_OK;
557 break;
558 case GAAOPTID_username:
559 OK = 0;
560 GAA_TESTMOREARGS;
561 GAA_FILL(GAATMP_username.arg1, gaa_getstr, GAATMP_username.size1);
562 gaa_index++;
563 #line 17 "psk.gaa"
564 { gaaval->username = GAATMP_username.arg1 ;};
566 return GAA_OK;
567 break;
569 #line 413 "gaa.skel"
570 default: break;
572 return GAA_ERROR_UNKNOWN;
575 int gaa(int argc, char **argv, gaainfo *gaaval)
577 int tmp1, tmp2;
578 int i, j;
579 char *opt_list;
581 GAAargv = argv;
582 GAAargc = argc;
584 opt_list = (char*) gaa_malloc(GAA_NB_OPTION + 1);
586 for(i = 0; i < GAA_NB_OPTION + 1; i++)
587 opt_list[i] = 0;
588 /* initialization */
589 if(inited == 0)
592 #line 31 "psk.gaa"
593 { gaaval->username=NULL; gaaval->passwd=NULL; gaaval->key_size = 0; gaaval->netconf_hint = NULL; ;};
596 inited = 1;
597 #line 438 "gaa.skel"
598 gaa_arg_used = NULL;
600 if (argc > 0) {
601 gaa_arg_used = gaa_malloc(argc * sizeof(char));
604 for(i = 1; i < argc; i++)
605 gaa_arg_used[i] = 0;
606 for(i = 1; i < argc; i++)
608 if(gaa_arg_used[i] == 0)
610 j = 0;
611 tmp1 = gaa_is_an_argument(GAAargv[i]);
612 switch(tmp1)
614 case GAA_WORD_OPTION:
615 j++;
616 case GAA_LETTER_OPTION:
617 j++;
618 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
619 if(tmp2 == GAA_ERROR_NOMATCH)
621 printf("Invalid option '%s'\n", argv[i]+j);
622 return 0;
624 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
626 case GAA_ERROR_NOTENOUGH_ARGS:
627 printf("'%s': not enough arguments\n",gaa_current_option);
628 return 0;
629 case GAA_ERROR_INVALID_ARG:
630 printf("Invalid arguments\n");
631 return 0;
632 case GAA_OK:
633 break;
634 default:
635 printf("Unknown error\n");
637 gaa_arg_used[i] = 1;
638 break;
639 case GAA_MULTIPLE_OPTION:
640 for(j = 1; j < strlen(argv[i]); j++)
642 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
643 if(tmp2 == GAA_ERROR_NOMATCH)
645 printf("Invalid option '%c'\n", *(argv[i]+j));
646 return 0;
648 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
650 case GAA_ERROR_NOTENOUGH_ARGS:
651 printf("'%s': not enough arguments\n",gaa_current_option);
652 return 0;
653 case GAA_ERROR_INVALID_ARG:
654 printf("Invalid arguments\n");
655 return 0;
656 case GAA_OK:
657 break;
658 default:
659 printf("Unknown error\n");
662 gaa_arg_used[i] = 1;
663 break;
664 default: break;
668 if(gaa_processing_file == 0)
671 #line 507 "gaa.skel"
672 #ifdef GAA_REST_EXISTS
673 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
675 case GAA_ERROR_NOTENOUGH_ARGS:
676 printf("Rest: not enough arguments\n");
677 return 0;
678 case GAA_ERROR_INVALID_ARG:
679 printf("Invalid arguments\n");
680 return 0;
681 case GAA_OK:
682 break;
683 default:
684 printf("Unknown error\n");
686 #endif
688 for(i = 1; i < argc; i++)
690 if(gaa_arg_used[i] == 0)
692 printf("Too many arguments\n");
693 return 0;
696 free(gaa_arg_used);
697 free(opt_list);
698 return -1;
701 struct gaastrnode
703 char *str;
704 struct gaastrnode *next;
707 typedef struct gaastrnode gaa_str_node;
709 static int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
711 int pos_ini;
712 int a;
713 int i = 0, len = 0, newline = 0;
715 if(argc == 1) {
716 newline = 1;
717 len = 2;
720 a = fgetc( file);
721 if (a == EOF) return 0;
723 while(a == ' ' || a == 9 || a == '\n')
725 if(a == '\n')
727 newline=1;
728 len = 2;
730 a = fgetc( file);
731 if (a == EOF) return 0;
734 pos_ini = ftell(file) - 1;
736 while(a != ' ' && a != 9 && a != '\n')
739 len++;
740 a = fgetc( file);
741 if(a==EOF) return 0; /* a = ' '; */
744 len += 1;
745 tmp_str->str = gaa_malloc((len) * sizeof(char));
747 if(newline == 1)
749 tmp_str->str[0] = '-';
750 tmp_str->str[1] = '-';
751 i = 2;
753 else
755 i = 0;
758 fseek(file,pos_ini, SEEK_SET);
761 a = fgetc( file);
763 if (a == EOF) {
764 i+=2;
765 break;
767 tmp_str->str[i] = a;
768 i++;
770 while(a != ' ' && a != 9 && a != '\n' && i < len);
772 tmp_str->str[i - 1] = 0;
774 fseek(file,- 1, SEEK_CUR);
775 /* printf("%d\n", ftell(file)); */
777 return -1;
780 int gaa_file(const char *name, gaainfo *gaaval)
782 gaa_str_node *first_str, **tmp_str, *tmp_str2;
783 int rval, i;
784 char **argv;
785 int argc = 0;
786 FILE *file;
788 gaa_processing_file = 1;
790 if((file = fopen(name, "r")) == NULL)
792 printf("Couldn't open '%s' configuration file for reading\n", name);
793 return 1;
796 tmp_str = &first_str;
799 argc++;
800 *tmp_str = gaa_malloc(sizeof(gaa_str_node));
802 (*tmp_str)->str = NULL;
803 (*tmp_str)->next = NULL;
805 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
806 tmp_str = &((*tmp_str)->next);
808 while(rval == -1);
810 if(rval == 1)
811 return 0;
813 argv = gaa_malloc((1 + argc) * sizeof(char*));
815 tmp_str2 = first_str;
816 argv[0] = "cfg";
817 for(i = 1; i < argc; i++)
819 argv[i] = tmp_str2->str;
820 tmp_str2 = tmp_str2->next;
823 rval = gaa(argc, argv, gaaval);
824 gaa_processing_file = 0;
825 return rval;