-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPMain.cpp
280 lines (247 loc) · 6.72 KB
/
CPMain.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "PlatDef.h"
#include "WinPlatDef.h"
#include "CZPing.h"
#include <stdio.h>
#include <WinINet.h>
#include <string> // stl string support.
// String constants.
//
#define STR_HelpFlag "-h\0"
#define STR_SiteNameFlag "-s\0"
#define STR_SiteURLFlag "-u\0"
#define STR_SiteToPingFlag "-p\0"
#define LEN_HelpFlag 2
#define LEN_SiteNameFlag 2
#define LEN_SiteURLFlag 2
#define LEN_SiteToPingFlag 2
//#define SKIP_THE_PING_PART 1
#ifndef SKIP_THE_PING_PART
#define PingSite(stp, lstp, sn, lsn, url, lurl, eb, leb) WeblogsDotComPing(stp, lstp, sn, lsn, url, lurl, eb, leb)
#else
#define PingSite(stp, lstp, sn, lsn, url, lurl, eb, leb) // moot, test mode.
#endif // SKIP_THE_PING_PART
static void displayBanner(void)
{
printf("-----------------------------------------------------\n");
printf(" CheezyPing, version 0.25\n");
printf(" Copyright 2001-2003 Rob Fahrni, all rights reserved.\n");
printf(" http://rob.crabapples.net/\n");
printf("-----------------------------------------------------\n\n");
} // displayBanner
static void displayUsage(void)
{
printf("+ Usage: czp -s\"My Site Name\" -u\"http://www.myurl.xyz\" -p\"http://rpc.weblogs.com/RPC2\"\n");
printf("\tWhere...\n");
printf("\t-s == REQUIRED: Text name for site. NOT the URL\n");
printf("\t-u == REQUIRED: URL for the site\n");
printf("\t-p == OPTIONAL: Site to ping, defaults to %s\n", SITE_WeblogsDotCom);
} // displayUsage
static void displaySiteInfo(
IN String SiteName,
IN String SiteURL,
IN String SiteToPing)
{
// Tell the user what we're doing.
//
printf("Updating...\n");
printf("\tSite Name: %s\n", SiteName);
printf("\tSite URL: %s\n", SiteURL);
printf("\tSite to Ping: %s\n", SiteToPing);
} // displaySiteInfo
static void copyAndStrip(
IN UInt FlagLen,
IN String Source,
IN UInt SourceLen,
IN OUT String Target,
IN UInt TargetLen)
{
if ((FlagLen > 0) &&
(Source != NULL && SourceLen > 0) &&
(Target != NULL && TargetLen > 0))
{
UInt CopySize = (SourceLen - FlagLen);
if (CopySize < TargetLen)
{
Memset(Target, 0, TargetLen);
StrNCpy(Target, &Source[FlagLen], CopySize);
}
}
} // copyAndStrip
#define IS_FLAG(s, f, l) StrStr(s, f) != NULL && StrLen(s) <= l
static Boolean parseArgs(
IN String* Arguments,
IN Int NumArgs,
IN OUT String SiteToPing,
IN UInt SiteToPingLen,
IN OUT String SiteName,
IN UInt SiteNameLen,
IN OUT String SiteURL,
IN UInt SiteURLLen)
{
Boolean Ok(False);
if (NumArgs > 0 && SiteName != NULL && SiteURL != NULL)
{
// Clean up the args we received, set SiteToPing to the default value.
//
Memset(SiteName, 0, SiteNameLen);
Memset(SiteURL, 0, SiteURLLen);
Memset(SiteToPing, 0, SiteToPingLen);
if (SiteToPing != NULL)
{
// Pre-load the SiteToPing with weblogs.com.
//
StrCpy(SiteToPing, SITE_WeblogsDotCom);
}
for (int x(NumArgs/*-1*/);
x >= 0;
x--)
{
// Look for one of our flags in the string.
//
if (Arguments[x] != NULL)
{
if (IS_FLAG(Arguments[x], STR_SiteNameFlag, SiteNameLen))
{
copyAndStrip(LEN_SiteNameFlag,
Arguments[x], StrLen(Arguments[x]),
SiteName, SiteNameLen);
}
else if (IS_FLAG(Arguments[x], STR_SiteURLFlag, SiteURLLen))
{
copyAndStrip(LEN_SiteURLFlag,
Arguments[x], StrLen(Arguments[x]),
SiteURL, SiteURLLen);
}
else if (IS_FLAG(Arguments[x], STR_SiteToPingFlag, SiteToPingLen))
{
copyAndStrip(LEN_SiteToPingFlag,
Arguments[x], StrLen(Arguments[x]),
SiteToPing, SiteToPingLen);
}
}
}
}
return Ok;
}
#define STR_SpaceReplacement "%20\0"
#define STR_Space " \0"
static Boolean fixupSpacesInSiteURL(
IN OUT String SiteURL,
IN Int SiteURLLen)
{
Boolean fOk(False);
// Make sure we should continue.
//
if (SiteURL != NULL && SiteURLLen > 0)
{
Boolean fDoCopy(False);
// I'm coping out and using an STL string here. It's so much nicer to use
// when you need to do replacements on a buffer.
//
std::string replaceBuff(SiteURL);
int loc(std::string::npos);
while ((loc = replaceBuff.find(STR_Space)) != std::string::npos)
{
// Replace on character at offset loc with STR_SpaceReplacement.
// This changes the space character to the "%20".
//
replaceBuff.replace(loc, 1, STR_SpaceReplacement);
// Yes this assignment is happening over and over, but we've got
// to do it somewhere. Right?
//
fDoCopy = True;
}
// Now that replacement has taken place jam it back into the
// SiteURL string, IF IT'S LARGE ENOUGH!
//
if (replaceBuff.size() < SiteURLLen && fDoCopy == True)
{
// Cram it back in, otherwise we're returning the same string
// we got and it may be goofy.
//
StrNCpy(SiteURL, replaceBuff.c_str(), replaceBuff.size());
}
}
return fOk;
} // fixupSpacesInSiteURL
static Boolean wantsHelp(
IN String* Arguments,
IN Int NumArgs)
{
Boolean needHelp(False);
for (int x(NumArgs-1);
x > 0 && needHelp == False;
x--)
{
// Look for -h in the string.
//
if (Arguments[x] != NULL &&
StrStr(Arguments[x], STR_HelpFlag) != NULL)
{
// We've found the -h (Help) flag, time to bail.
//
needHelp = True;
}
}
return needHelp;
}
void main(
IN Int NumArgs, // Number of args.
IN String* Arguments) // Args array.
{
displayBanner();
if (NumArgs < 2 || wantsHelp(Arguments, NumArgs) == True)
{
displayUsage();
}
else
{
Char SiteToPing[MAX_PATH];
Char SiteName[MAX_PATH];
Char SiteURL[MAX_PATH];
// Get the arguments
//
parseArgs(Arguments, NumArgs,
SiteToPing, MAX_PATH,
SiteName, MAX_PATH,
SiteURL, MAX_PATH);
// If all the arguments seem to be filled in properly then we
// can get on with pinging the site, otherwise show the
// help.
//
if (SiteName[0] != NULL &&
SiteURL[0] != NULL &&
SiteToPing[0] != NULL)
{
// Display some information to the user about what's going on here!
//
displaySiteInfo(SiteName, SiteURL, SiteToPing);
// Fixup the path to the .HTM file if there's a space in the name
//
fixupSpacesInSiteURL(SiteURL, MAX_PATH);
// PingSite can return us a result if we give it a buffer
// to work with.
//
// We're realy calling WeblogsDotComPing, PinSite is a macro
// wrapped around it so we can test this code without actually
// doing a ping.
//
Char ResultBuffer[MONSTER_BUFFER];
Long ResultBufferLen(MONSTER_BUFFER);
// Finally get around to pinging the site.
//
PingSite(SiteToPing, StrLen(SiteToPing),
SiteName, StrLen(SiteName),
SiteURL, StrLen(SiteURL),
ResultBuffer, &ResultBufferLen);
// Regardless of the error we inform the user of our result string.
// It could be an error code.
//
printf(ResultBuffer);
}
else
{
displayUsage();
}
}
} // main