gowser

Check-in [a3663a20b2]

Overview
Comment: Initial transfer from Bitbucket Tarball | ZIP archive | SQL archive family | ancestors | files | file ages | folders a3663a20b2d53690b97d1e619231134d6d376cefebd973999830a3b923511e58 Cthulhux 2019-11-16 00:38:00
Context
 2019-11-16 00:38 Initial transfer from Bitbucket Leaf check-in: a3663a20b2 user: Cthulhux tags: trunk 00:11 initial empty check-in check-in: 842ed08b30 user: Cthulhux tags: trunk
Changes

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 ``` ``` DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2017-2018 Cthulhux Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO. ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ``` ```# gowser: a Gopher browser. ## build requirements: * [IUP](http://webserver2.tecgraf.puc-rio.br/iup/) * [curl](http://curl.haxx.se/) Requires MS Windows or the GTK+ libraries. ## build.cmd for Windows with Clang: @echo off cd repo REM build clang-cl -m32 /Wall /D_CRT_SECURE_NO_WARNINGS /DCURL_STATICLIB /DU_STATIC_IMPLEMENTATION /Ox /MT /GS /Febin\gowser.exe src\3rdparty\stringbuilder\str_builder.c src\gopherfuncs.c src\osfuncs.c src\html.c src\gowser.c /I"..\includes" /I"..\includes\IUP" /I "src\3rdparty\stringbuilder" /link /libpath:..\libs libcurl_a.lib iup.lib iupole.lib iupweb.lib gdi32.lib shell32.lib ole32.lib comdlg32.lib comctl32.lib user32.lib /SAFESEH /DYNAMICBASE /SUBSYSTEM:WINDOWS /OPT:REF,ICF REM Cleanup if exist *.obj del *.obj cd .. I'll add proper Makefiles later or something. ```

 ``` ``` ``` ``` ```> > > > > > > ``` ```1 2 3 4 5 6 7 ``` ```Copyright 2017 John Schember Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/* str_builder.h * * Copyright (C) 2017 John Schember * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ #include #include #include #include #include #include "str_builder.h" /* - - - - */ static const size_t str_builder_min_size = 32; struct str_builder { char *str; size_t alloced; size_t len; }; /* - - - - */ str_builder_t *str_builder_create(void) { str_builder_t *sb; sb = calloc(1, sizeof(*sb)); sb->str = malloc(str_builder_min_size); *sb->str = '\0'; sb->alloced = str_builder_min_size; sb->len = 0; return sb; } void str_builder_destroy(str_builder_t *sb) { if (sb == NULL) return; free(sb->str); free(sb); } /* - - - - */ /*! Ensure there is enough space for data being added plus a NULL terminator. * * \param[in,out] sb Builder. * \param[in] add_len The length that needs to be added *not* including a NULL terminator. */ static void str_builder_ensure_space(str_builder_t *sb, size_t add_len) { if (sb == NULL || add_len == 0) return; if (sb->alloced >= sb->len+add_len+1) return; while (sb->alloced < sb->len+add_len+1) { /* Doubling growth strategy. */ sb->alloced <<= 1; if (sb->alloced == 0) { /* Left shift of max bits will go to 0. An unsigned type set to * -1 will return the maximum possible size. However, we should * have run out of memory well before we need to do this. Since * this is the theoretical maximum total system memory we don't * have a flag saying we can't grow any more because it should * be impossible to get to this point. */ sb->alloced--; } } sb->str = realloc(sb->str, sb->alloced); } /* - - - - */ void str_builder_add_str(str_builder_t *sb, const char *str, size_t len) { if (sb == NULL || str == NULL || *str == '\0') return; if (len == 0) len = strlen(str); str_builder_ensure_space(sb, len); memmove(sb->str+sb->len, str, len); sb->len += len; sb->str[sb->len] = '\0'; } void str_builder_add_char(str_builder_t *sb, char c) { if (sb == NULL) return; str_builder_ensure_space(sb, 1); sb->str[sb->len] = c; sb->len++; sb->str[sb->len] = '\0'; } void str_builder_add_int(str_builder_t *sb, int val) { char str[12]; if (sb == NULL) return; snprintf(str, sizeof(str), "%d", val); str_builder_add_str(sb, str, 0); } /* - - - - */ void str_builder_clear(str_builder_t *sb) { if (sb == NULL) return; str_builder_truncate(sb, 0); } void str_builder_truncate(str_builder_t *sb, size_t len) { if (sb == NULL || len >= sb->len) return; sb->len = len; sb->str[sb->len] = '\0'; } void str_builder_drop(str_builder_t *sb, size_t len) { if (sb == NULL || len == 0) return; if (len >= sb->len) { str_builder_clear(sb); return; } sb->len -= len; /* +1 to move the NULL. */ memmove(sb->str, sb->str+len, sb->len+1); } /* - - - - */ size_t str_builder_len(const str_builder_t *sb) { if (sb == NULL) return 0; return sb->len; } const char *str_builder_peek(const str_builder_t *sb) { if (sb == NULL) return NULL; return sb->str; } char *str_builder_dump(const str_builder_t *sb, size_t *len) { char *out; if (sb == NULL) return NULL; if (len != NULL) *len = sb->len; out = malloc(sb->len+1); memcpy(out, sb->str, sb->len+1); return out; } ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/* str_builder.h * * Copyright (C) 2017 John Schember * * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ #ifndef __STR_BUILDER_H__ #define __STR_BUILDER_H__ #include /*! \addtogroup str_builder String Builder * * A mutable string of characters used to dynamically build a string. * * @{ */ struct str_builder; typedef struct str_builder str_builder_t; /* - - - - */ /*! Create a str builder. * * \return str builder. */ str_builder_t *str_builder_create(void); /*! Destroy a str builder. * * \param[in,out] sb Builder. */ void str_builder_destroy(str_builder_t *sb); /* - - - - */ /*! Add a string to the builder. * * \param[in,out] sb Builder. * \param[in] str String to add. * \param[in] len Length of string to add. If 0, strlen will be called * internally to determine length. */ void str_builder_add_str(str_builder_t *sb, const char *str, size_t len); /*! Add a character to the builder. * * \param[in,out] sb Builder. * \param[in] c Character. */ void str_builder_add_char(str_builder_t *sb, char c); /*! Add an integer as to the builder. * * \param[in,out] sb Builder. * \param[in] val Int to add. */ void str_builder_add_int(str_builder_t *sb, int val); /* - - - - */ /*! Clear the builder. * * \param[in,out] sb Builder. */ void str_builder_clear(str_builder_t *sb); /*! Remove data from the end of the builder. * * \param[in,out] sb Builder. * \param[in] len The new length of the string. * Anything after this length is removed. */ void str_builder_truncate(str_builder_t *sb, size_t len); /*! Remove data from the beginning of the builder. * * \param[in,out] sb Builder. * \param[in] len The length to remove. */ void str_builder_drop(str_builder_t *sb, size_t len); /* - - - - */ /*! The length of the string contained in the builder. * * \param[in] sb Builder. * * \return Length. */ size_t str_builder_len(const str_builder_t *sb); /*! A pointer to the internal buffer with the builder's string data. * * The data is guaranteed to be NULL terminated. * * \param[in] sb Builder. * * \return Pointer to internal string data. */ const char *str_builder_peek(const str_builder_t *sb); /*! Return a copy of the string data. * * \param[in] sb Builder. * \param[out] len Length of returned data. Can be NULL if not needed. * * \return Copy of the internal string data. */ char *str_builder_dump(const str_builder_t *sb, size_t *len); /*! @} */ #endif /* __STR_BUILDER_H__ */ ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_CONSTS #define GOWSER_CONSTS /* Constants for use throughout Gowser */ const char* GOWSER_REPO = "https://bitbucket.org/tux_/gowser/"; const char* GOWSER_WEB = "https://gowser.xyz"; /* TODO */ #endif ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_DEBUG #define GOWSER_DEBUG #define __STDC_WANT_LIB_EXT1__ 1 #include #include #define DEBUG 1 #define DEBUGFILE "debug.txt" /* Debug macro: Prints some debug output to DEBUGFILE (or to stderr if DEBUGFILE could not be appended to) if enabled. */ #ifdef __STDC_LIB_EXT1__ /* We can use fopen_s() */ #define debug_print(fmt, ...) \ do { \ if (DEBUG) { \ FILE* fDebug; \ errno_t err; \ if ((err = fopen_s(&fDebug, DEBUGFILE, "a+")) == 0) { \ if (fDebug != NULL) { \ fprintf(fDebug, "%s:%d:%s(): " fmt, __FILE__, \ __LINE__, __func__, __VA_ARGS__); \ fclose(fDebug); \ } else { \ fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \ __LINE__, __func__, __VA_ARGS__); \ } \ } \ } \ } while (0) #else /* C11 is not welcome here. :-( */ #define debug_print(fmt, ...) \ do { \ if (DEBUG) { \ FILE* fDebug = fopen(DEBUGFILE, "a+"); \ if (fDebug != NULL) { \ fprintf(fDebug, "%s:%d:%s(): " fmt, __FILE__, \ __LINE__, __func__, __VA_ARGS__); \ fclose(fDebug); \ } else { \ fprintf(stderr, "%s:%d:%s(): " fmt, __FILE__, \ __LINE__, __func__, __VA_ARGS__); \ } \ } \ } while (0) #endif #endif /* GOWSER_DEBUG */ ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #include #include #include #include "debug.h" #include "types.h" #include "gopherfuncs.h" gopher_line_t parse_gopher_line(char* inputstring) { /* Formats a Gopher line for output in the main window. */ gopher_line_t ret; gopher_link_t link; if (inputstring == NULL) { /* Bad server is bad. */ debug_print("Erroneous input NULL string found.\n", NULL); link.displaystring = NULL; } else { switch (inputstring[0]) { case '0': case '1': /* Some link. */ link = generate_link(inputstring); break; case 'i': /* Informational messages. */ default: /* Whatever. */ char** line = split_gopher_line(inputstring); link.displaystring = line[1]; } link.type = inputstring[0]; } ret.text = link; return ret; } gopher_link_t generate_link(char inputstring[500]) { /* Tries to squeeze all link elements out of the inputstring. */ gopher_link_t ret; char** tokenarray = split_gopher_line(inputstring); strncpy_s(&ret.type, 1, tokenarray[0], 1); ret.displaystring = tokenarray[1]; strncpy_s(ret.selector, sizeof(ret.selector), tokenarray[2], sizeof(ret.selector)); strncpy_s(ret.hostname, sizeof(ret.hostname), tokenarray[3], sizeof(ret.hostname)); ret.port = atoi(tokenarray[4]); return ret; } char** split_gopher_line(char* inputstring) { /** Returns an array of all 5 regular elements of a Gopher line: * [0] = link type * [1] = display text * [2] = selector * [3] = hostname * [4] = port * * Note: The selector is optional. If there is none, [2] will be * skipped. */ static char* tokenarray[5] = {"", "", "", "", ""}; /* Get the first character as "type" and move the string to the right: */ char linktype = inputstring[0]; tokenarray[0] = &linktype; inputstring++; /* Let us assume that there is a tab character between the elements ... */ int i = 1; char* tokens = strtok(inputstring, "\t"); while (tokens != NULL) { tokenarray[i++] = tokens; tokens = strtok(NULL, "\t"); } switch (linktype) { /* Some links don't have selectors. Those have only 4 elements. Detect them and move them to the right where applicable: */ case '0': case '1': if (i == 4) { debug_print("Found a link without a selector. I'll shift the elements.\n", NULL); tokenarray[4] = tokenarray[3]; tokenarray[3] = tokenarray[2]; tokenarray[2] = ""; } break; } return tokenarray; } ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_UI #define GOWSER_UI #include "types.h" /* line-related: */ gopher_line_t parse_gopher_line(char* inputstring); gopher_link_t generate_link(char inputstring[500]); char** split_gopher_line(char* inputstring); #endif ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #include #include #include #include #include #include #include #include "types.h" #include "debug.h" #include "gopherfuncs.h" #include "html.h" #include "gowser.h" #include "osfuncs.h" #include "constants.h" Ihandle *browser, *urlbar, *histbutton; /* We need to interact with them later. */ int main(int argc, char **argv) { /* Initialize navigation history */ nav_number = -1; /* Initialize GUI */ Ihandle *dlg, *vbox, *hbox, *urllabel; IupOpen(&argc, &argv); IupWebBrowserOpen(); urllabel = IupLabel(NULL); IupSetStrAttribute(urllabel, "TITLE", "Go to gopher://"); urlbar = IupText(NULL); IupSetAttribute(urlbar, "EXPAND", "HORIZONTAL"); IupSetAttribute(urlbar, "MARGIN", "5x2"); IupSetAttribute(urlbar, "BORDER", "NO"); IupSetCallback(urlbar, "K_ANY", (Icallback) urlbar_callback); histbutton = IupButton("back", "history_callback"); IupSetAttribute(histbutton, "FLAT", "YES"); IupSetAttribute(histbutton, "ACTIVE", "NO"); IupSetCallback(histbutton, "ACTION", (Icallback) history_callback); hbox = IupHbox(urllabel, urlbar, histbutton, NULL); IupSetAttribute(hbox, "ALIGNMENT", "ACENTER"); IupSetAttribute(hbox, "MARGIN", "8x3"); browser = IupWebBrowser(); vbox = IupVbox(hbox, browser, NULL); IupSetAttribute(browser, "MULTILINE", "YES"); IupSetAttribute(browser, "EXPAND", "YES"); IupSetAttribute(browser, "READONLY", "YES"); IupSetCallback(browser, "NAVIGATE_CB", (Icallback) navigate_callback); dlg = IupDialog(vbox); IupSetStrAttribute(dlg, "TITLE", "Gowser, a Gopher Browser"); IupSetAttribute(dlg, "SIZE", "HALFx280"); IupShowXY(dlg, IUP_CENTER, IUP_CENTER); IupSetAttribute(dlg, "USERSIZE", NULL); /* argv[1] can be a valid Gopher URL. */ if (argc > 1) { /* At least one argument has been passed. */ if (argc > 2) { /* As Gowser does not have a multi-window functionality (yet?), having more than one URL parameter is rather pointless. Inform the user about that. */ IupMessage("Parameter Fuckup", "Gowser accepts a Gopher URL as a parameter. You gave it more than one parameter. Please don't."); } go_to_gopher_page(argv[1], 1); } else { /* Visit the start page, */ go_to_gopher_page("welcome", 1); } IupMainLoop(); IupClose(); return EXIT_SUCCESS; } static void go_to_gopher_page(char* gopherurl, int save_history) { /** * Downloads and fills the requested Gopher page into the window. * Builds the HTML string on the way. */ int hasRawText = 0; download_result_t rawstring; /* Gowser has a build-in "start page" under the URI "welcome". */ int is_welcome_page = (strncmp(gopherurl, "welcome", strlen(gopherurl)) == 0); if (is_welcome_page) { hasRawText = 1; rawstring.success = 1; char welcomepage[4096]; sprintf(welcomepage, "\ ` `.-/-`.:+-\n\ `-/hyooos+ossysshyssshyo+oooo/` ______________________________________________________\n\ :oosssso++////+s///++o++//////+osysod- / \\\n\ osshyd++shhhho////sdhhyyhh+/////ooo++yo | _ _ |\n\ y+++sd+sy++hys///+hyo/ymhos////+dys+oh. | | | | | |\n\ .sssds/s.+mmm+////y-.dmmmy//////sdso/` | __ _____| | ___ ___ _ __ ___ ___ | |_ ___ |\n\ .od+s:-m/dms///+y yy:mmm//////yd: | \\ \\ /\\ / / _ \\ |/ __/ _ \\| '_ ` _ \\ / _ \\ | __/ _ \\ |\n\ +d+h-shomNs///oh:msdmmms+///+sd/ | \\ V V / __/ | (_| (_) | | | | | | __/ | || (_) | |\n\ :ossooysoooshyo+/+sso++++ooss////ohh: | \\_/\\_/ \\___|_|\\___\\___/|_| |_| |_|\\___| \\__\\___/ |\n\ `+yo///////////hsyms+ymo//////////////+yh- | |\n\ yy/////////////hydmmdhd+///////////s+///+h: | __ _ _____ _____ ___ _ __ |\n\ .mo//yo//////////+odho+///////////+syh+///ym. | / _` |/ _ \\ \\ /\\ / / __|/ _ \\ '__| |\n\ :Nh//yssso+///////+ho/////////+osso+/o+///om- | | (_| | (_) \\ V V /\\__ \\ __/ | |\n\ `ys//////ohmmo+oo++ho+oooooohdds//////////om` | \\__, |\\___/ \\_/\\_/ |___/\\___|_| |\n\ ods///////sm. o- -ms////////////yy | __/ | |\n\ -sh////////yo :: so///////////odd. | |___/ A Gopher browser. |\n\ oy////////h- .o h///////////sh+: \\_____________________________________________________/\n\ +hss/////+y+///+so+//++s////////+sh+\n\ `+dh+/////////+ss+//////////oshy/` hg repository: %s\n\ `.oyso+//////////////+ymhyo:` www: coming soon(ish)\n\ ./oyhys+//////oyy+//`\n\ ./oyysoydo.\n\ `:ys`\n\ ", GOWSER_REPO, GOWSER_REPO); int len = strlen(welcomepage); rawstring.text = (char*)malloc(len+1); strcpy(rawstring.text, welcomepage); } else { rawstring = download_gopher_page(gopherurl); /* If the requested page is not a directory (type 1), do not try * to parse it. Just fill it into the HTML view and we're good. * As strtok() would basically throw away our original Gopher * URL, we need a copy first. */ if (rawstring.success > 0) { char* gopherurl_copy = calloc(strlen(gopherurl)+1, sizeof(char)); strncpy_s(gopherurl_copy, strlen(gopherurl) + 1, gopherurl, strlen(gopherurl)); char* urlparts = strtok(gopherurl_copy, "/"); char* urlarray[4]; int i = 0; while (urlparts != NULL && i < 4) { urlarray[i++] = urlparts; urlparts = strtok(NULL, "/"); } free(gopherurl_copy); /* If we have a gopher:// URL, the link type is urlarray[2]; else, urlarray[1]. */ int typefound = (strncmp(gopherurl, "gopher://", 9) == 0) ? 2 : 1; if (i > typefound) { if (urlarray[typefound] != NULL && strncmp(urlarray[typefound], "1", 1) != 0) { /* Not a directory. */ hasRawText = 1; } } } } str_builder_t* htmlsb = str_builder_create(); /* Build the HTML: */ html_head(gopherurl, htmlsb); /* Gowser might have had a problem. */ if (rawstring.success == 0) { /* And it actually did. */ str_builder_add_str(htmlsb, rawstring.text, 0); str_builder_add_str(htmlsb, "\n", 0); } else { char* gopher_contents = rawstring.text; while (gopher_contents) { char* next_line = strchr(gopher_contents, '\n'); int linelength = next_line ? (next_line - gopher_contents) : strlen(gopher_contents); char* temp_str = (char *)malloc(linelength + 1); if (temp_str) { memcpy(temp_str, gopher_contents, linelength); temp_str[linelength] = '\0'; if (hasRawText) { /* Raw text is raw. */ str_builder_add_str(htmlsb, temp_str, 0); str_builder_add_str(htmlsb, "\n", 0); } else { gopher_line_t this_line = parse_gopher_line(temp_str); html_from_gopherline(this_line, htmlsb); } free(temp_str); } else { debug_print("malloc() error: Couldn't malloc a new temp_str of length %d!\n", linelength + 1); } gopher_contents = next_line ? (next_line + 1) : NULL; } } html_foot(htmlsb); /* Memo by myself for myself: The resulting contents can be /really fucking large/. Using IupSetAttribute() will probably exceed the poin- ter's range and crash. Use IupSetStrAttribute() instead which uses the static string contents. */ char* htmlstring = str_builder_dump(htmlsb, NULL); IupSetStrAttribute(browser, "HTML", htmlstring); IupSetStrAttribute(urlbar, "VALUE", gopherurl); /* Update history: */ if (save_history == 1 && rawstring.success > 0) { if (nav_number+1 < GOWSERHISTORY) { strcpy(nav_history[++nav_number], gopherurl); debug_print("Added %s to your nav_history[%d].\n", gopherurl, nav_number); if (nav_number >= 1) { /* We have at least one previous page. */ IupSetAttribute(histbutton, "ACTIVE", "YES"); } } else { /* People seriously use more than 250 Gopher pages in one session? */ debug_print("The maximum history size of %d is reached. Consider a higher number for the next version.\n", sizeof(nav_history)); } } /* Clean up: */ str_builder_destroy(htmlsb); if (rawstring.success == 1 && rawstring.text != NULL) { free(rawstring.text); } } static download_result_t download_gopher_page(char gopherurl[200]) { /* Tries to download and return its contents. */ download_result_t ret; CURL *curl_handle; CURLcode res; struct memstruct chunk; chunk.memory = malloc(1); chunk.size = 0; curl_global_init(CURL_GLOBAL_ALL); curl_handle = curl_easy_init(); /* * Don't assume that the user was wise. * We have several possibilities for what could have been * passed as a Gopher URL: * * - gopher://some.host.name/... << perfect * - some.host.name/... << add gopher:// * - something completely different << add gopher:// and hope for the best */ const char* gopherprot = "gopher://"; if (strncmp(gopherurl, gopherprot, 9) != 0) { /* no "gopher://". :-( */ char gopherurl_final[250]; snprintf(gopherurl_final, sizeof(gopherurl_final), "%s%s", gopherprot, gopherurl); curl_easy_setopt(curl_handle, CURLOPT_URL, gopherurl_final); } else { /* with gopher:// :-) */ curl_easy_setopt(curl_handle, CURLOPT_URL, gopherurl); } curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_memory_callback); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); /* get it! */ res = curl_easy_perform(curl_handle); if (res != CURLE_OK) { /* Show the user a hint that we failed. */ debug_print("Error downloading page %s: %s\n", gopherurl, curl_easy_strerror(res)); ret.success = 0; ret.text = "Sorry - this did not work."; } else { /* * Now, our chunk.memory points to a memory block that is chunk.size * bytes big and contains the remote file. */ ret.success = 1; /* Signal that everything is fine. */ ret.text = malloc(chunk.size); snprintf(ret.text, chunk.size, "%s", chunk.memory); } /* cleanup */ curl_easy_cleanup(curl_handle); free(chunk.memory); curl_global_cleanup(); return ret; } static size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp) { /* cURL -> Memory Callback. Shamelessly borrowed from the cURL web page: https://curl.haxx.se/libcurl/c/getinmemory.html */ size_t realsize = size * nmemb; struct memstruct *mem = (struct memstruct *)userp; mem->memory = realloc(mem->memory, mem->size + realsize + 1); if (mem->memory == NULL) { /* out of memory! */ debug_print("%s\n", "not enough memory (realloc returned NULL)"); return 0; } memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } void go_back(void) { /* Remove the last history entry and move one up (if applicable) */ if (nav_number > 0 && nav_history[nav_number] != NULL) { go_to_gopher_page(nav_history[--nav_number], 0); strcpy(nav_history[nav_number], "\0"); if (nav_number == 0) { /* Disable the "back" button again */ IupSetAttribute(histbutton, "ACTIVE", "NO"); } } } /* --------------------------- User interface callbacks: --------------------------- */ static int urlbar_callback(Ihandle *self, int c) { /* React on */ if (c == K_CR) { /* The CarriageReturn was pressed. Pass the input text. */ char* inputtext = IupGetAttribute(urlbar, "VALUE"); go_to_gopher_page(inputtext, 1); IupSetAttribute(browser, "SCROLLTOPOS", "0,0"); } return IUP_DEFAULT; } static int navigate_callback(Ihandle *self, char* url) { /* React on link click */ if (strncmp(url, "gopher://", 9) == 0) { /* Clicked a Gopher link. */ go_to_gopher_page(url, 1); } else { /* Clicked a different link. */ if (strncmp(url, previous_url, strlen(url)) != 0) { /* It is probably unwanted to open an URL multiple times in a row. */ if (open_external_URL(url) == 1) { /* Something went horribly wrong, */ IupMessage("Oops!", "Gowser failed to start your default application. Would you mind to report this issue?"); } IupSetAttribute(browser, "VALUE", url); strncpy(previous_url, url, sizeof(previous_url)); } } return IUP_IGNORE; } static int history_callback(Ihandle *self) { /* React on "back" button click */ go_back(); return IUP_DEFAULT; } ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_H #define GOWSER_H #include #include "types.h" struct memstruct { char* memory; size_t size; }; /* History-related: */ #define MAXURLLENGTH 250 #define GOWSERHISTORY 250 char previous_url[MAXURLLENGTH]; /* not for internal history */ char nav_history[GOWSERHISTORY][MAXURLLENGTH]; int nav_number; void go_back(void); /* Other functions: */ static void go_to_gopher_page(char* gopherurl, int save_history); static download_result_t download_gopher_page(char gopherurl[200]); /* Callbacks: */ static size_t write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp); static int urlbar_callback(Ihandle *self, int c); static int navigate_callback(Ihandle *self, char* url); static int history_callback(Ihandle *self); #endif ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #include #include #include #include "html.h" void html_from_gopherline(gopher_line_t gopherline, str_builder_t* sb) { /* Returns the HTML version of this particular Gopher line. */ if (gopherline.text.displaystring != NULL && strncmp(gopherline.text.displaystring, " ", strlen(gopherline.text.displaystring)) != 0) { /* Empty lines happen sometimes. Only process non-empty lines here. */ char line[500]; switch (gopherline.text.type) { /* Text file icons are one character wide. Directory icons are two characters wide. Leave space accordingly. */ case '0': /* Text file */ snprintf(line, sizeof(line), "🖹 %s", gopherline.text.hostname, gopherline.text.port, gopherline.text.selector, gopherline.text.displaystring); str_builder_add_str(sb, line, 0); break; case '1': /* Directory */ snprintf(line, sizeof(line), "🗁 %s", gopherline.text.hostname, gopherline.text.port, gopherline.text.selector, gopherline.text.displaystring); str_builder_add_str(sb, line, 0); break; case '4': case '5': case '9': case 's': /* Binary file */ /* TODO */ break; case '7': /* Full-Text Search */ /* TODO */ break; case 'g': case 'I': /* Image file */ /* TODO */ break; case 'h': /* HTML link */ /* TODO: HTML formatting? */ snprintf(line, sizeof(line), "🌐 %s", gopherline.text.hostname, gopherline.text.port, gopherline.text.selector, gopherline.text.displaystring); str_builder_add_str(sb, line, 0); break; case 'i': /* Informational message */ snprintf(line, sizeof(line), " %s", gopherline.text.displaystring); str_builder_add_str(sb, line, 0); break; default: /* Unsupported */ str_builder_add_str(sb, gopherline.text.displaystring, 0); break; } } str_builder_add_str(sb, "\n", 0); /* Note that we are in a
- no
", 0); }   void html_foot(str_builder_t* sb) {     /* Returns the usual HTML footer. */     str_builder_add_str(sb, "

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_HTML #define GOWSER_HTML #include #include "types.h" void html_from_gopherline(gopher_line_t gopherline, str_builder_t* sb); void html_head(const char* gopherurl, str_builder_t* sb); void html_foot(str_builder_t* sb); #endif ```

 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ /* OS-specific functions */ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include #include #else /* Assume POSIX: */ #include #include #include #endif #include "osfuncs.h" int open_external_URL(char url[250]) { /* Opens a http(s) URL. Returns 0 on success, else 1. */ #ifdef _WIN32 return (int)(ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOW)) > 32 ? 0 : 1; #else /* Assume xdg-open */ char command_string[260]; snprintf(command_string, sizeof(command_string), "xdg-open %s", url); int retval = system(command_string); return retval != NULL ? retval : 1; #endif } ```
 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > ``` ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef OSFUNCS_H #define OSFUNCS_H /* OS-specific functions */ int open_external_URL(char url[250]); #endif /* OSFUNCS_H */ ```
 ``` ``` ``` ``` ```> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ``` ```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 ``` ```/** * This is Gowser, a Gopher Browser. * https://bitbucket.org/tux_/gowser/ * * This work is free. You can redistribute it and/or modify it under the * terms of the Do What The Fuck You Want To Public License, Version 2, * as published by Sam Hocevar. See the COPYING file for more details. */ #ifndef GOWSER_TYPES #define GOWSER_TYPES /* Content types: */ typedef struct gopher_link { char type; char* displaystring; char selector[100]; char hostname[100]; int port; } gopher_link_t; typedef struct gopher_line { gopher_link_t text; int colorpair; /* unused */ } gopher_line_t; /* Download types: */ typedef struct download_result { int success; char* text; } download_result_t; #endif ```