Skip to content

Commit c04c51f

Browse files
committed
Update command line parser
1 parent 4256469 commit c04c51f

File tree

1 file changed

+60
-7
lines changed

1 file changed

+60
-7
lines changed

httpsget.c

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,26 +273,79 @@ char* extract_html_body(char *response) {
273273

274274
/****************************************************************************/
275275

276+
void help(const char *name)
277+
{
278+
printf(
279+
"httpsget.x - HTTPS simple client for X68000\n"
280+
"Usage: %s [options] [URL]\n"
281+
"Options:\n"
282+
" -h, --help Show this help message and exit\n"
283+
" -H, --show-header Show HTTP response header\n"
284+
" -r, --raw Display raw HTML without parsing\n"
285+
, name);
286+
exit(0);
287+
}
288+
276289
int main(int argc, char **argv)
277290
{
278291
int client_fd;
279292
struct sockaddr_in server_addr;
280293
struct hostent *host_entry;
281-
char *hostname = NULL;
294+
char hostname[256] = {0};
295+
char *url = NULL;
282296
char *path = NULL;
283297
int port = 0;
284298
int bytes_sent;
299+
int show_header = 0;
300+
int raw_mode = 0;
301+
int http_mode = 0;
285302

286-
// デフォルト値のリセット(オプション処理後)
287-
hostname = "www.yahoo.co.jp";
288303
path = "/";
289304
port = 443;
290305

291-
if (argc >= 2) {
292-
hostname = argv[1];
306+
for (int i = 1; i < argc; i++) {
307+
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
308+
help(argv[0]);
309+
} else if (strcmp(argv[i], "-H") == 0 || strcmp(argv[i], "--show-header") == 0) {
310+
show_header = 1;
311+
} else if (strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--raw") == 0) {
312+
raw_mode = 1;
313+
} else if (argv[i][0] == '-') {
314+
fprintf(stderr, "Error: Unknown option %s\n", argv[i]);
315+
return 1;
316+
} else if (url == NULL) {
317+
url = argv[i];
318+
319+
if (strncmp(url, "http://", 7) == 0) {
320+
http_mode = 1;
321+
url += 7;
322+
port = 80;
323+
} else if (strncmp(url, "https://", 8) == 0) {
324+
url += 8;
325+
port = 443;
326+
}
327+
328+
char *slash = strchr(url, '/');
329+
if (slash) {
330+
path = slash;
331+
} else {
332+
slash = strchr(url, '\0');
333+
}
334+
memcpy(hostname, url, slash - url);
335+
hostname[slash - url] = '\0';
336+
if (strrchr(hostname, ':')) {
337+
char *colon = strrchr(hostname, ':');
338+
*colon = '\0';
339+
port = atoi(colon + 1);
340+
}
341+
} else {
342+
fprintf(stderr, "Error: Too many arguments\n");
343+
return 1;
344+
}
293345
}
294-
if (argc >= 3) {
295-
path = argv[2];
346+
347+
if (url == NULL) {
348+
help(argv[0]);
296349
}
297350

298351
printf("Connecting to %s:%d%s\n", hostname, port, path);

0 commit comments

Comments
 (0)