From ed8f166f70d847d318e9a076f61fa0ca9f172e25 Mon Sep 17 00:00:00 2001 From: John Haugabook Date: Tue, 29 Jul 2025 22:41:58 -0400 Subject: [PATCH] curl: add -h cheat-sheet using curl-cheat-sheet as text Rebase and resolve failing checks. --- src/tool_help.c | 190 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) diff --git a/src/tool_help.c b/src/tool_help.c index 3cdd522c29..540426210d 100644 --- a/src/tool_help.c +++ b/src/tool_help.c @@ -248,6 +248,8 @@ void tool_help(const char *category) /* Lets handle the string "category" differently to not print an errormsg */ else if(curl_strequal(category, "category")) get_categories(); + else if (curl_strequal(category, "cheat-sheet")) + tool_cheat_sheet(); else if(category[0] == '-') { #ifdef USE_MANUAL /* command line option help */ @@ -405,3 +407,191 @@ void tool_list_engines(void) curl_slist_free_all(engines); curl_easy_cleanup(curl); } + +/* Functions for outputting cheat-sheet argument. */ +/* Cheat sheet data structure organized by heading and value. */ +static const struct cheat_table { + const char *heading[2]; +} cheat_items[] = { + {{"Verbose", "-v, --trace-ascii file"}}, + {{"Hide progress", "-s"}}, + {{"extra info", "-w format"}}, + {{"Write output", "-O, -o file"}}, + {{"Timeout", "-m secs"}}, + {{"POST", "-d string, -d @file"}}, + {{"multipart", "-F name=value, -F name=@file"}}, + {{"PUT", "-T file"}}, + {{"HEAD", "-I"}}, + {{"custom", "-X METHOD"}}, + {{"Basic auth", "-u user:password"}}, + {{"read cookies", "-b "}}, + {{"write cookies", "-c "}}, + {{"send cookies", "-b \"c=1; d=2\""}}, + {{"user-agent", "-A string"}}, + {{"Use proxy", "-x host:port"}}, + {{"Headers add/remove", "-H \"name: value\", -H name:"}}, + {{"follow redirs", "-L"}}, + {{"gzip", "--compressed"}}, + {{"insecure", "-k"}}, + {{"", ""}}, + {{NULL, NULL}} +}; + +static int incre_i(int i, int j) { + if (j == 0) + return i + 1; + else + return i + 1 + j; +} + +/* Support to print number of columns per screen width. */ +static void print_cheatsheet(int i, int col_num) { + /* Spacer variable for heading. */ + const char *spacer = "------------------------------"; + + /* Use consistent width for each column. */ + int width = 30; + + if (col_num == 1) { + if(cheat_items[i].heading[0] && + cheat_items[i].heading[0] != NULL && + strcmp(cheat_items[i].heading[0], "") != 0) + printf("%-20s\n%s\n%s\n\n", + cheat_items[i].heading[0], spacer, cheat_items[i].heading[1]); + } + else if (col_num == 2) { + if(cheat_items[i+1].heading[0] && + cheat_items[i+1].heading[0] == NULL) + print_cheatsheet(i, 1); + else + if(cheat_items[i].heading[0] && + cheat_items[i].heading[0] != NULL && + strcmp(cheat_items[i].heading[0], "") != 0) + printf("%-*s %-*s\n%s %s\n%-*s %-*s\n\n", + width, cheat_items[i].heading[0], + width, cheat_items[i+1].heading[0], + spacer, spacer, + width, cheat_items[i].heading[1], + width, cheat_items[i+1].heading[1]); + } + else if (col_num == 3) { + if(cheat_items[i+1].heading[0] && + cheat_items[i+1].heading[0] == NULL) + print_cheatsheet(i, 1); + else if(cheat_items[i+2].heading[0] && + cheat_items[i+2].heading[0] == NULL) + print_cheatsheet(i, 2); + else + if(cheat_items[i].heading[0] && + cheat_items[i].heading[0] != NULL && + strcmp(cheat_items[i].heading[0], "") != 0) + printf("%-*s %-*s %-*s\n%s %s %s\n%-*s %-*s %-*s\n\n", + width, cheat_items[i].heading[0], + width, cheat_items[i+1].heading[0], + width, cheat_items[i+2].heading[0], + spacer, spacer, spacer, + width, cheat_items[i].heading[1], + width, cheat_items[i+1].heading[1], + width, cheat_items[i+2].heading[1]); + } + else if (col_num == 4) { + if(cheat_items[i+1].heading[0] && + cheat_items[i+1].heading[0] == NULL) + print_cheatsheet(i, 1); + else if(cheat_items[i+2].heading[0] && + cheat_items[i+2].heading[0] == NULL) + print_cheatsheet(i, 2); + else if(cheat_items[i+3].heading[0] && + cheat_items[i+3].heading[0] == NULL) + print_cheatsheet(i, 3); + else + if (cheat_items[i].heading[0] && + cheat_items[i].heading[0] != NULL && + strcmp(cheat_items[i].heading[0], "") != 0) + printf("%-*s %-*s %-*s %-*s\n%s %s %s %s\n" + "%-*s %-*s %-*s %-*s\n\n", + width, cheat_items[i].heading[0], + width, cheat_items[i+1].heading[0], + width, cheat_items[i+2].heading[0], + width, cheat_items[i+3].heading[0], + spacer, spacer, spacer, spacer, + width, cheat_items[i].heading[1], + width, cheat_items[i+1].heading[1], + width, cheat_items[i+2].heading[1], + width, cheat_items[i+3].heading[1]); + } + else if (col_num == 5) { + if(cheat_items[i+1].heading[0] && + cheat_items[i+1].heading[0] == NULL) + print_cheatsheet(i, 1); + else if(cheat_items[i+2].heading[0] && + cheat_items[i+2].heading[0] == NULL) + print_cheatsheet(i, 2); + else if(cheat_items[i+3].heading[0] && + cheat_items[i+3].heading[0] == NULL) + print_cheatsheet(i, 3); + else if(cheat_items[i+4].heading[0] && + cheat_items[i+4].heading[0] == NULL) + print_cheatsheet(i, 4); + else + if(cheat_items[i].heading[0] && + cheat_items[i].heading[0] != NULL && + strcmp(cheat_items[i].heading[0], "") != 0) + printf("%-*s %-*s %-*s %-*s %-*s\n%s %s %s %s %s\n" + "%-*s %-*s %-*s %-*s %-*s\n\n", + width, cheat_items[i].heading[0], + width, cheat_items[i+1].heading[0], + width, cheat_items[i+2].heading[0], + width, cheat_items[i+3].heading[0], + width, cheat_items[i+4].heading[0], + spacer, spacer, spacer, spacer, spacer, + width, cheat_items[i].heading[1], + width, cheat_items[i+1].heading[1], + width, cheat_items[i+2].heading[1], + width, cheat_items[i+3].heading[1], + width, cheat_items[i+4].heading[1]); + } +} + +/* Output cheat-sheet. */ +void tool_cheat_sheet(void) +{ + /* Get terminal width for proper formatting */ + unsigned int cols = get_terminal_columns(); + + /* Get the table length. */ + unsigned int table_length = sizeof(cheat_items) / sizeof(cheat_items[0]); + + /* Handle for loop according to cols */ + unsigned int i, j; + + /* Set j based on col width, setting once, not in a loop. */ + if (cols <= 75) + j = 0; + else if (cols > 75 && cols <= 125) + j = 1; + else if (cols > 125 && cols <= 175) + j = 2; + else if (cols > 175 && cols <= 225) + j = 3; + else + j = 4; + + /* Loop through cheat sheet sections */ + for(i = 0; i < table_length; i = incre_i(i, j)) { + if (cheat_items[i+j].heading[0] == NULL) { + break; + } else { + if (cols <= 75) /* Output one columns. */ + print_cheatsheet(i, 1); + else if(cols > 75 && cols <= 125) /* output two columns */ + print_cheatsheet(i, 2); + else if(cols > 125 && cols <= 175) /* output three columns */ + print_cheatsheet(i, 3); + else if(cols > 175 && cols <= 225) /* output four columns */ + print_cheatsheet(i, 4); + else + print_cheatsheet(i, 5); /* output five columns */ + } + } +}