1 /*
2 * $Header$
3 *
4 * Copyright 2008 Massachusetts Institute of Technology.
5 * All Rights Reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 #include <windows.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <opts.hxx>
31
32 bool
IsValidOpt(char ch)33 ParseOpts::IsValidOpt(
34 char ch
35 )
36 {
37 return (m_ValidOpts[ch % 256] != 0);
38 }
39
40 void
PrintOpt(char ch,char * text)41 ParseOpts::PrintOpt(
42 char ch,
43 char* text
44 )
45 {
46 if (IsValidOpt(ch))
47 fprintf(stderr, " -%c %s\n", ch, text);
48 }
49
50 void
UsageOpts(char * program,int code)51 ParseOpts::UsageOpts(
52 char * program,
53 int code
54 )
55 {
56 fprintf(stderr, "Usage: %s [options]\n", program);
57 PrintOpt('k', "stop server");
58 #ifdef CCAPI_TEST_OPTIONS
59 PrintOpt('s', "string");
60 PrintOpt('e', "endpoint");
61 PrintOpt('m', "maxcalls");
62 PrintOpt('n', "mincalls");
63 PrintOpt('f', "flag_wait_op");
64 PrintOpt('u', "unprotected");
65 PrintOpt('b', "use security callback");
66 #endif
67 PrintOpt('c', "output debug info to console");
68 exit(code);
69 }
70
71 void
SetValidOpts(char * valid_opts)72 ParseOpts::SetValidOpts(
73 char* valid_opts
74 )
75 {
76 memset(m_ValidOpts, 0, sizeof(m_ValidOpts));
77 char *p = valid_opts;
78 for (p = valid_opts; *p; p++) {
79 m_ValidOpts[*p % sizeof(m_ValidOpts)] = 1;
80 }
81 }
82
83 void
Parse(Opts & opts,int argc,char ** argv)84 ParseOpts::Parse(
85 Opts& opts,
86 int argc,
87 char **argv
88 )
89 {
90 int i;
91 for (i = 1; i < argc; i++) {
92 if ((*argv[i] == '-') || (*argv[i] == '/')) {
93 char ch = tolower(*(argv[i]+1));
94 if (!IsValidOpt(ch))
95 UsageOpts(argv[0]);
96 switch (ch) {
97 case 'k':
98 opts.bShutdown = TRUE;
99 break;
100 #ifdef CCAPI_TEST_OPTIONS
101 case 's':
102 opts.pszString = argv[++i];
103 break;
104 case 'e':
105 opts.pszEndpoint = argv[++i];
106 break;
107 case 'm':
108 opts.cMaxCalls = (unsigned int) atoi(argv[++i]);
109 break;
110 case 'n':
111 opts.cMinCalls = (unsigned int) atoi(argv[++i]);
112 break;
113 case 'f':
114 opts.fDontWait = (unsigned int) atoi(argv[++i]);
115 break;
116 case 'u':
117 opts.bDontProtect = TRUE;
118 break;
119 case 'b':
120 opts.bSecCallback = TRUE;
121 break;
122 #endif
123 case 'c':
124 opts.bConsole = TRUE;
125 break;
126 case 'h':
127 case '?':
128 default:
129 UsageOpts(argv[0]);
130 }
131 }
132 else
133 UsageOpts(argv[0]);
134 }
135
136 }
137
ParseOpts(char * valid_opts)138 ParseOpts::ParseOpts(char* valid_opts)
139 {
140 SetValidOpts(valid_opts);
141 }
142
ParseOpts()143 ParseOpts::ParseOpts()
144 {
145 }
146
~ParseOpts()147 ParseOpts::~ParseOpts()
148 {
149 }
150