File: optparse.c

package info (click to toggle)
members 19990831-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 76 kB
  • ctags: 27
  • sloc: cpp: 355; ansic: 146; makefile: 53; sh: 35
file content (177 lines) | stat: -rw-r--r-- 3,834 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177

#define ALTPHON_OPT    'a'
#define CAPITALIZE_OPT 'c'
#define NUMERALS_OPT   'n'
#define SECURE_OPT     's'
#define HELP_OPT       'h'
int main(int argc, char **argv)
{
  int first_nonopt = 1; /* index of the first non-option in argv */
  int is_bad_opt = 0; /* true if unrecognized option */
  int cnt, len, wants_secure=0;
  char buf[20];
  int wants_alternate_phonics = 0; /* boolean from options */
  int wants_capitalize = 0; /* boolean from options */
  int wants_numerals = 0; /* boolean from options */
  int wants_help = 0; /* boolean from options */
  int getopt_result;
  int longindex = 0;

  getopt_result = 
    getopt_long
    (
      argc, 
      argv, 
      opt_string,
      longopts, 
      &longindex
    );

  while(getopt_result != EOF)
    {
      /* process opts here */
      
      switch(getopt_result)
	{
	case ALTPHON_OPT:
	  wants_alternate_phonics = 1;

	  disable_option("ash");

	  break;

	case CAPITALIZE_OPT:
	  wants_capitalize = 1;

	  disable_option("csh");

	  break;
	  
	case NUMERALS_OPT:
	  wants_numerals = 1;

	  disable_option("nsh");

	  break;

	case SECURE_OPT:
	  wants_secure = 1;
	  
	  disable_option("acnsh");
	  
	  break;
	  
	case HELP_OPT:
	  wants_help = 1;
	  
	  disable_option("acnsh");
	  
	  break;
	  
	case '?':
	default:
	  is_bad_opt = 1;
	  break;
	}
      
      if(is_bad_opt) 
	break;
      else
	getopt_result = 
	  getopt_long
	  (
	    argc, 
	    argv, 
	    opt_string, 
	    longopts, 
	    &longindex
	  );
    }
  
  if(wants_help)
    {
      usage(stdout, argv[0]);

      printf("You can use -a, -c or -n in any combination.\n");

      printf("You can't use these with -s or vise versa.\n");

      printf("\n");
      printf("\n");
      printf("OPTIONS\n");
      printf("\n");
      printf("  -a or --alt-phonics\n");
      printf("       use a slightly altered phonics table to generate\n");
      printf("       syllables.\n");
      printf("\n");
      printf("  -c or --capitalize\n");
      printf("       use (not so) random capitalizations in generated\n");
      printf("       passwords\n");
      printf("\n");
      printf("  -n or --numerals\n");
      printf("       use (not so) random placement of numerals in\n");
      printf("       generated passwords\n");
      printf("\n");
      printf("  -s or --secure\n");
      printf("       generate totally random, unmemorable, unpronouncable,\n");
      printf("       but secure and unguessable passwords\n");
      printf("\n");
      printf("  -h or --help\n");
      printf("       print this message and exit successfully\n");
      printf("\n");
      printf("\n");
      printf("ARGUMENTS\n");
      printf("\n");
      printf("  maxlength (required with -a, -c, -n or -s)\n");
      printf("       a number spec ifying the masimum length\n");
      printf("       of each generated password\n");
      printf("\n");
      printf("  count\n");
      printf("       the number of passwords generated. each will\n");
      printf("       be on its own line.\n");
      printf("\n");
      printf("SEE ALSO: man pwgen for details.\n");
      printf("\n");

      exit(0);
    }
  else if 
         ( 
	   (argc < first_nonopt + 1) 
	   || 
	   (argc > first_nonopt + 3) 
	   || (is_bad_opt) 
	 )
    {
      usage(stderr, argv[0]);
      
      return 1;
    }

  if ((len = atoi(argv[first_nonopt + 0])) < 4 || len > 16) 
    {
      fprintf
	(
	  stderr, 
	  "%s: invalid maxlength %s\n", 
	  argv[0], 
	  argv[first_nonopt + 0]
	);

      return 1;
    }

  if (argc == first_nonopt + 1)
    cnt = 1;                                      
  else if ((cnt = atoi(argv[first_nonopt + 1])) < 1) 
    {
      fprintf
	(
	  stderr, 
	  "%s: invalid count %s\n",  
	  argv[0], 
	  argv[first_nonopt + 1]
	);

      return 1;                                      
    }