*** empty log message ***
[gnutls.git] / src / prime-gaa.c
bloba1d76827c838afa7cfb48a32007bca68679bab45
1 #include <stdio.h>
2 #include <string.h>
4 #ifndef GAA_NO_WIN32
5 #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) || defined(WINDOWS)
6 #define GAA_WIN32
7 #endif
8 #endif
10 void __gaa_helpsingle(char short_name, char *name,
11 char *arg_desc, char *opt_help)
13 int col1, col3, col4, tabsize = 3, curr;
14 int i;
16 col1 = 5; /* Default values */
17 col3 = 30;
18 col4 = 70;
20 curr = 0;
21 for(i = 0; i < col1; i++)
23 printf(" ");
24 curr++;
26 if(short_name)
28 if(name && *name)
30 printf("-%c, ", short_name);
31 curr += 4;
33 else
35 printf("-%c ", short_name);
36 curr += 3;
39 if(name && *name)
41 printf("--%s ", name);
42 curr += 3 + strlen(name);
44 if(arg_desc && *arg_desc)
46 printf("%s ", arg_desc);
47 curr += 1 + strlen(arg_desc);
49 if(curr >= col3)
51 printf("\n");
52 curr = 0;
54 if(opt_help) /* let's print the option's help body */
56 const char *str = opt_help;
57 while(*str)
59 while(curr < col3)
61 printf(" ");
62 curr++;
64 switch(*str)
66 case '\n':
67 printf("\n");
68 curr = 0;
69 break;
70 case '\t':
73 printf(" ");
74 curr++;
76 while((curr - col3) % tabsize != 0 && curr < col4);
77 case ' ':
78 if(*str == ' ')
80 curr++;
81 printf(" ");
83 for(i = 1; str[i] && str[i] != ' ' && str[i] != '\n'
84 && str[i] != '\t'; i++);
85 if(curr + i - 1 >= col4)
86 curr = col4;
87 break;
88 default:
89 printf("%c", *str);
90 curr++;
92 if(curr >= col4)
94 printf("\n");
95 curr = 0;
97 str++;
100 printf("\n");
103 void gaa_help()
105 printf("Crypt help\nUsage : prime [options]""\n");
106 __gaa_helpsingle(0, "bits", """BITS"" ", "specify the number of bits for prime numbers (used only when create_conf option is specified).");
107 __gaa_helpsingle('h', "help", "", "shows this help text");
109 #line 100 "gaa.skel"
111 /* Copy of C area */
113 #line 104 "gaa.skel"
114 /* GAA HEADER */
115 #ifndef GAA_HEADER_POKY
116 #define GAA_HEADER_POKY
118 typedef struct _gaainfo gaainfo;
120 struct _gaainfo
122 #line 3 "prime.gaa"
123 int bits;
125 #line 114 "gaa.skel"
128 #ifdef __cplusplus
129 extern "C"
131 #endif
133 int gaa(int argc, char *argv[], gaainfo *gaaval);
135 void gaa_help();
137 int gaa_file(char *name, gaainfo *gaaval);
139 #ifdef __cplusplus
141 #endif
144 #endif
146 #line 135 "gaa.skel"
148 /* C declarations */
150 #define GAAERROR \
152 gaa_error = 1; \
153 return; \
156 char *gaa_current_option;
157 int gaa_error = 0;
159 /* Generated by gaa */
161 #include <string.h>
162 #include <stdlib.h>
165 #define GAA_OK -1
167 #define GAA_ERROR_NOMATCH 0
168 #define GAA_ERROR_NOTENOUGH_ARGS 1
169 #define GAA_ERROR_INVALID_ARG 2
170 #define GAA_ERROR_UNKNOWN 3
172 #define GAA_NOT_AN_OPTION 0
173 #define GAA_WORD_OPTION 1
174 #define GAA_LETTER_OPTION 2
175 #define GAA_MULTIPLE_OPTION 3
177 #define GAA_REST 0
178 #define GAA_NB_OPTION 2
179 #define GAAOPTID_help 1
180 #define GAAOPTID_bits 2
182 #line 168 "gaa.skel"
184 #define GAA_CHECK1STR(a,b) \
185 if(a[0] == str[0]) \
187 gaa_current_option = a; \
188 return b; \
191 #define GAA_CHECKSTR(a,b) \
192 if(strcmp(a,str) == 0) \
194 gaa_current_option = a; \
195 return b; \
198 #define GAA_TESTMOREARGS \
199 if(!OK) \
201 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
202 gaa_index++; \
203 if(gaa_last_non_option == gaa_index) \
204 return GAA_ERROR_NOTENOUGH_ARGS; \
207 #define GAA_TESTMOREOPTIONALARGS \
208 if(!OK) \
210 while((gaa_last_non_option != gaa_index) && (gaa_arg_used[gaa_index] == 1)) \
211 gaa_index++; \
212 if(gaa_last_non_option == gaa_index) \
213 OK = 1; \
216 #define GAA_FILL_2ARGS(target, func) \
217 target = func(GAAargv[gaa_index]); \
218 gaa_arg_used[gaa_index] = 1; \
219 if(gaa_error == 1) \
221 gaa_error = 0; \
222 return GAA_ERROR_INVALID_ARG; \
227 #define GAA_FILL(target, func, num) \
228 if(!OK) \
230 target = func(GAAargv[gaa_index]); \
231 gaa_arg_used[gaa_index] = 1; \
232 if(gaa_error == 1) \
234 gaa_error = 0; \
235 return GAA_ERROR_INVALID_ARG; \
237 num = 1; \
239 else \
241 num = 0; \
244 #define GAA_LIST_FILL(target, func, type ,num) \
245 if(!OK) \
247 num = 0; \
248 target = malloc((gaa_last_non_option - gaa_index) * sizeof(type)); \
249 for(; gaa_index < gaa_last_non_option; gaa_index++) \
251 if(gaa_arg_used[gaa_index] == 0) \
253 GAA_FILL_2ARGS(target[num], func); \
254 num++; \
257 if(num == 0) \
258 return GAA_ERROR_NOTENOUGH_ARGS; \
261 #define GAA_OPTIONALLIST_FILL(target, func, type ,num) \
262 if(!OK) \
264 num = 0; \
265 target = malloc((gaa_last_non_option - gaa_index) * sizeof(type)); \
266 for(; gaa_index < gaa_last_non_option; gaa_index++) \
268 if(gaa_arg_used[gaa_index] == 0) \
270 GAA_FILL_2ARGS(target[num], func); \
271 num++; \
276 #define GAA_OBLIGAT(str) \
277 k = 0; \
278 for(i = 0; i < strlen(str); i++) \
280 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
281 if(j == GAA_ERROR_NOMATCH) \
283 printf("Error : invalid 'obligat' set\n"); \
284 exit(-1); \
286 if(opt_list[j] == 1) \
287 k = 1; \
289 if(k == 0) \
291 if(strlen(str) == 1) \
292 printf("You must give the -%s option\n", str); \
293 else \
294 printf("You must give at least one option of '%s'\n", str); \
295 return 0; \
298 #define GAA_INCOMP(str) \
299 k = 0; \
300 for(i = 0; i < strlen(str); i++) \
302 j = gaa_get_option_num(str + i, GAA_LETTER_OPTION); \
303 if(j == GAA_ERROR_NOMATCH) \
305 printf("Error : invalid 'obligat' set\n"); \
306 exit(-1); \
308 if(opt_list[j] == 1) \
309 k++; \
311 if(k > 1) \
313 printf("The options '%s' are incompatible\n", str); \
314 return 0; \
318 char **GAAargv;
319 int GAAargc;
320 char *gaa_arg_used;
321 int gaa_processing_file = 0;
322 int inited = 0;
324 int gaa_getint(char *arg)
326 int tmp;
327 char a;
328 if(sscanf(arg, "%d%c", &tmp, &a) != 1)
330 printf("Option %s : '%s' isn't an integer\n", gaa_current_option, arg);
331 GAAERROR;
333 return tmp;
336 char gaa_getchar(char *arg)
338 if(strlen(arg) != 1)
340 printf("Option %s : '%s' isn't an character\n", gaa_current_option, arg);
341 GAAERROR;
343 return arg[0];
346 char* gaa_getstr(char *arg)
348 return arg;
350 float gaa_getfloat(char *arg)
352 float tmp;
353 char a;
354 if(sscanf(arg, "%f%c", &tmp, &a) != 1)
356 printf("Option %s : '%s' isn't a float number\n", gaa_current_option, arg);
357 GAAERROR;
359 return tmp;
361 /* option structures */
363 struct GAAOPTION_bits
365 int arg1;
366 int size1;
369 #line 349 "gaa.skel"
370 int gaa_is_an_argument(char *str)
372 #ifdef GAA_WIN32
373 if(str[0] == '/' && str[1] != 0)
374 return GAA_MULTIPLE_OPTION;
375 #endif
376 if(str[0] != '-')
377 return GAA_NOT_AN_OPTION;
378 if(str[1] == 0)
379 return GAA_NOT_AN_OPTION;
380 if(str[1] == '-')
382 if(str[2] != 0)
383 return GAA_WORD_OPTION;
384 else
385 return GAA_NOT_AN_OPTION;
387 if(str[2] == 0)
388 return GAA_LETTER_OPTION;
389 else
390 return GAA_MULTIPLE_OPTION;
393 int gaa_get_option_num(char *str, int status)
395 switch(status)
397 case GAA_LETTER_OPTION:
398 GAA_CHECK1STR("", GAAOPTID_bits);
399 case GAA_MULTIPLE_OPTION:
400 #line 375 "gaa.skel"
401 GAA_CHECK1STR("h", GAAOPTID_help);
403 #line 277 "gaa.skel"
404 break;
405 case GAA_WORD_OPTION:
406 GAA_CHECKSTR("help", GAAOPTID_help);
407 GAA_CHECKSTR("bits", GAAOPTID_bits);
409 #line 281 "gaa.skel"
410 break;
411 default: break;
413 return GAA_ERROR_NOMATCH;
416 int gaa_try(int gaa_num, int gaa_index, gaainfo *gaaval, char *opt_list)
418 int OK = 0;
419 int gaa_last_non_option;
420 struct GAAOPTION_bits GAATMP_bits;
422 #line 393 "gaa.skel"
423 #ifdef GAA_REST_EXISTS
424 struct GAAREST GAAREST_tmp;
425 #endif
427 opt_list[gaa_num] = 1;
429 for(gaa_last_non_option = gaa_index;
430 (gaa_last_non_option != GAAargc) && (gaa_is_an_argument(GAAargv[gaa_last_non_option]) == GAA_NOT_AN_OPTION);
431 gaa_last_non_option++);
433 if(gaa_num == GAA_REST)
435 gaa_index = 1;
436 gaa_last_non_option = GAAargc;
439 switch(gaa_num)
441 case GAAOPTID_help:
442 OK = 0;
443 #line 6 "prime.gaa"
444 { gaa_help(); exit(0); ;};
446 return GAA_OK;
447 break;
448 case GAAOPTID_bits:
449 OK = 0;
450 GAA_TESTMOREARGS;
451 GAA_FILL(GAATMP_bits.arg1, gaa_getint, GAATMP_bits.size1);
452 gaa_index++;
453 #line 4 "prime.gaa"
454 { gaaval->bits = GAATMP_bits.arg1 ;};
456 return GAA_OK;
457 break;
459 #line 413 "gaa.skel"
460 default: break;
462 return GAA_ERROR_UNKNOWN;
465 int gaa(int argc, char **argv, gaainfo *gaaval)
467 int tmp1, tmp2;
468 int i, j;
469 char *opt_list;
471 GAAargv = argv;
472 GAAargc = argc;
474 opt_list = (char*) malloc(GAA_NB_OPTION + 1);
475 for(i = 0; i < GAA_NB_OPTION + 1; i++)
476 opt_list[i] = 0;
477 /* initialization */
478 if(inited == 0)
481 #line 8 "prime.gaa"
482 { gaaval->bits=1040; ;};
485 inited = 1;
486 #line 438 "gaa.skel"
487 gaa_arg_used = malloc(argc * sizeof(char));
488 for(i = 1; i < argc; i++)
489 gaa_arg_used[i] = 0;
490 for(i = 1; i < argc; i++)
492 if(gaa_arg_used[i] == 0)
494 j = 0;
495 tmp1 = gaa_is_an_argument(GAAargv[i]);
496 switch(tmp1)
498 case GAA_WORD_OPTION:
499 j++;
500 case GAA_LETTER_OPTION:
501 j++;
502 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
503 if(tmp2 == GAA_ERROR_NOMATCH)
505 printf("Invalid option '%s'\n", argv[i]+j);
506 return 0;
508 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
510 case GAA_ERROR_NOTENOUGH_ARGS:
511 printf("'%s' : not enough arguments\n",gaa_current_option);
512 return 0;
513 case GAA_ERROR_INVALID_ARG:
514 printf("Invalid arguments\n");
515 return 0;
516 case GAA_OK:
517 break;
518 default:
519 printf("Unknown error\n");
521 gaa_arg_used[i] = 1;
522 break;
523 case GAA_MULTIPLE_OPTION:
524 for(j = 1; j < strlen(argv[i]); j++)
526 tmp2 = gaa_get_option_num(argv[i]+j, tmp1);
527 if(tmp2 == GAA_ERROR_NOMATCH)
529 printf("Invalid option '%c'\n", *(argv[i]+j));
530 return 0;
532 switch(gaa_try(tmp2, i+1, gaaval, opt_list))
534 case GAA_ERROR_NOTENOUGH_ARGS:
535 printf("'%s' : not enough arguments\n",gaa_current_option);
536 return 0;
537 case GAA_ERROR_INVALID_ARG:
538 printf("Invalid arguments\n");
539 return 0;
540 case GAA_OK:
541 break;
542 default:
543 printf("Unknown error\n");
546 gaa_arg_used[i] = 1;
547 break;
548 default: break;
552 if(gaa_processing_file == 0)
555 #line 507 "gaa.skel"
556 #ifdef GAA_REST_EXISTS
557 switch(gaa_try(GAA_REST, 1, gaaval, opt_list))
559 case GAA_ERROR_NOTENOUGH_ARGS:
560 printf("Rest : not enough arguments\n");
561 return 0;
562 case GAA_ERROR_INVALID_ARG:
563 printf("Invalid arguments\n");
564 return 0;
565 case GAA_OK:
566 break;
567 default:
568 printf("Unknown error\n");
570 #endif
572 for(i = 1; i < argc; i++)
574 if(gaa_arg_used[i] == 0)
576 printf("Too many arguments\n");
577 return 0;
580 free(gaa_arg_used);
581 free(opt_list);
582 return -1;
585 struct gaastrnode
587 char *str;
588 struct gaastrnode *next;
591 typedef struct gaastrnode gaa_str_node;
593 int gaa_internal_get_next_str(FILE *file, gaa_str_node *tmp_str, int argc)
595 int pos_ini;
596 char a;
597 int i = 0, len = 0, newline = 0;
599 if(argc == 1)
600 newline = 1;
602 if(fscanf(file,"%c", &a) != 1) return 0;
604 while(a == ' ' || a == 9 || a == '\n')
606 if(a == '\n')
608 newline=1;
609 len = 2;
611 if(fscanf(file,"%c", &a) != 1) return 0;
614 pos_ini = ftell(file) - 1;
616 while(a != ' ' && a != 9 && a != '\n')
619 len++;
620 if(fscanf(file,"%c", &a) != 1) a = ' ';
623 tmp_str->str = malloc((len + 1) * sizeof(char));
624 if(newline == 1)
626 tmp_str->str[0] = '-';
627 tmp_str->str[1] = '-';
628 i = 2;
630 else
632 i = 0;
635 fseek(file,pos_ini, SEEK_SET);
638 if(fscanf(file, "%c", &a) != 1)
640 i+=2;
641 break;
643 tmp_str->str[i] = a;
644 i++;
646 while(a != ' ' && a != 9 && a != '\n');
648 tmp_str->str[i - 1] = 0;
650 fseek(file,- 1, SEEK_CUR);
652 return -1;
655 int gaa_file(char *name, gaainfo *gaaval)
657 gaa_str_node *first_str, **tmp_str, *tmp_str2;
658 int rval, i;
659 char **argv;
660 int argc = 0;
661 FILE *file;
663 gaa_processing_file = 1;
665 if((file = fopen(name, "r")) == NULL)
667 printf("Couldn't open '%s' configuration file for reading\n", name);
668 return 0;
671 tmp_str = &first_str;
674 argc++;
675 *tmp_str = malloc(sizeof(gaa_str_node));
676 (*tmp_str)->str = NULL;
677 (*tmp_str)->next = NULL;
679 rval = gaa_internal_get_next_str(file, *tmp_str, argc);
680 tmp_str = &((*tmp_str)->next);
682 while(rval == -1);
684 if(rval == 1)
685 return 0;
687 argv = malloc((1 + argc) * sizeof(char*));
688 tmp_str2 = first_str;
689 argv[0] = "cfg";
690 for(i = 1; i < argc; i++)
692 argv[i] = tmp_str2->str;
693 tmp_str2 = tmp_str2->next;
696 rval = gaa(argc, argv, gaaval);
697 gaa_processing_file = 0;
698 return rval;