git/clic/commit/25487c3c561873fda304042df02ed8c68575ce20.html (24528 bytes)
1 <!DOCTYPE html> 2 <html lang="en"><head><meta charset="UTF-8" /> 3 <meta name="viewport" content="width=device-width, initial-scale=1" /> 4 <title>clic 25487c3c - Recently Written</title> 5 <link rel="stylesheet" href="../../stagit.css" /> 6 </head><body> 7 <div id="top"><a href="../../../index.html">Recently Written</a> · <a href="../../index.html">git</a></div> 8 <h1>clic</h1><span class="desc">MIRROR ONLY Gopher client with pretty colours in Common Lisp</span><p class="url">git clone https://github.com/equwal/clic</p><p><a href="../../clic/index.html">Log</a> | <a href="../../clic/files.html">Files</a> | <a href="../../clic/refs.html">Refs</a></p><hr/> 9 <div id="content"> 10 <pre>commit 25487c3c561873fda304042df02ed8c68575ce20 11 Solene Rapenne <solene@perso.pw> 12 2017-12-28 11:45:34 +0100 13 14 Display unknown types. Replace tab with spaces 15 16 </pre><pre> clic.lisp | 314 ++++++++++++++++++++++++++++++++------------------------------ 17 1 file changed, 160 insertions(+), 154 deletions(-) 18 </pre><pre>diff --git a/clic.lisp b/clic.lisp 19 index d09dadc..cfc276c 100644 20 --- a/clic.lisp 21 +++ b/clic.lisp 22 <span class="h">@@ -62,7 +62,7 @@</span> 23 ;;; List of allowed item types 24 (defparameter *allowed-selectors* 25 (list "0" "1" "2" "3" "4" "5" "6" "i" 26 <span class="d">- "h" "7" "8" "9" "+" "T" "g" "I"))</span> 27 <span class="a">+ "h" "7" "8" "9" "+" "T" "g" "I"))</span> 28 29 ;;;; BEGIN CUSTOMIZABLE 30 ;;; keep files visited on disk when t 31 <span class="h">@@ -78,7 +78,7 @@</span> 32 (defun add-color(name type hue) 33 "Storing a ANSI color string into *colors*" 34 (setf (gethash name *colors*) 35 <span class="d">- (format nil "~a[~a;~am" #\Escape type hue)))</span> 36 <span class="a">+ (format nil "~a[~a;~am" #\Escape type hue)))</span> 37 38 (defun get-color(name) (gethash name *colors*)) 39 (add-color 'red 1 31) 40 <span class="h">@@ -112,118 +112,124 @@</span> 41 "this function split a string with separator and return a list" 42 (let ((text (concatenate 'string text (string separator)))) 43 (loop for char across text 44 <span class="d">- counting char into count</span> 45 <span class="d">- when (char= char separator)</span> 46 <span class="d">- collect</span> 47 <span class="d">- ;; we look at the position of the left separator from right to left</span> 48 <span class="d">- (let ((left-separator-position (position separator text :from-end t :end (- count 1))))</span> 49 <span class="d">- (subseq text</span> 50 <span class="d">- ;; if we can't find a separator at the left of the current, then it's the start of</span> 51 <span class="d">- ;; the string</span> 52 <span class="d">- (if left-separator-position (+ 1 left-separator-position) 0)</span> 53 <span class="d">- (- count 1))))))</span> 54 <span class="a">+ counting char into count</span> 55 <span class="a">+ when (char= char separator)</span> 56 <span class="a">+ collect</span> 57 <span class="a">+ ;; we look at the position of the left separator from right to left</span> 58 <span class="a">+ (let ((left-separator-position (position separator text :from-end t :end (- count 1))))</span> 59 <span class="a">+ (subseq text</span> 60 <span class="a">+ ;; if we can't find a separator at the left of the current, then it's the start of</span> 61 <span class="a">+ ;; the string</span> 62 <span class="a">+ (if left-separator-position (+ 1 left-separator-position) 0)</span> 63 <span class="a">+ (- count 1))))))</span> 64 65 (defun formatted-output(line) 66 "Used to display gopher response with color one line at a time" 67 (let ((line-type (subseq line 0 1)) 68 <span class="d">- (infos (split (subseq line 1) #\Tab)))</span> 69 <span class="a">+ (infos (split (subseq line 1) #\Tab)))</span> 70 71 <span class="d">- ;; see RFC 1436</span> 72 <span class="d">- ;; section 3.8</span> 73 <span class="d">- (when (and</span> 74 <span class="d">- (= (length infos) 4)</span> 75 <span class="d">- (member line-type *allowed-selectors* :test #'equal))</span> 76 <span class="a">+ ;; if split worked</span> 77 <span class="a">+ (when (= (length infos) 4)</span> 78 79 (let ((line-number (+ 1 (hash-table-count *links*))) 80 <span class="d">- (text (car infos))</span> 81 <span class="d">- (uri (cadr infos))</span> 82 <span class="d">- (host (caddr infos))</span> 83 <span class="d">- (port (parse-integer (cadddr infos))))</span> 84 <span class="d">-</span> 85 <span class="d">- ;; RFC, page 4</span> 86 <span class="d">- (check "i"</span> 87 <span class="d">- (print-with-color text))</span> 88 <span class="d">-</span> 89 <span class="d">- ;; 0 text file</span> 90 <span class="d">- (check "0"</span> 91 <span class="d">- (setf (gethash line-number *links*)</span> 92 <span class="d">- (make-location :host host :port port :uri uri :type line-type ))</span> 93 <span class="d">- (print-with-color text 'file line-number))</span> 94 <span class="d">-</span> 95 <span class="d">- ;; 1 directory</span> 96 <span class="d">- (check "1"</span> 97 <span class="d">- (setf (gethash line-number *links*)</span> 98 <span class="d">- (make-location :host host :port port :uri uri :type line-type ))</span> 99 <span class="d">- (print-with-color text 'folder line-number))</span> 100 <span class="d">-</span> 101 <span class="d">- ;; 2 CSO phone-book</span> 102 <span class="d">- ;; WE SKIP</span> 103 <span class="d">- (check "2")</span> 104 <span class="d">-</span> 105 <span class="d">- ;; 3 Error</span> 106 <span class="d">- (check "3"</span> 107 <span class="d">- (print-with-color "error" 'red line-number))</span> 108 <span class="d">-</span> 109 <span class="d">- ;; 4 BinHexed Mac file</span> 110 <span class="d">- (check "4"</span> 111 <span class="d">- (print-with-color text))</span> 112 <span class="d">-</span> 113 <span class="d">- ;; 5 DOS Binary archive</span> 114 <span class="d">- (check "5"</span> 115 <span class="d">- (print-with-color "selector 5 not implemented" 'red))</span> 116 <span class="d">-</span> 117 <span class="d">- ;; 6 Unix uuencoded file</span> 118 <span class="d">- (check "6"</span> 119 <span class="d">- (print-with-color "selector 6 not implemented" 'red))</span> 120 <span class="d">-</span> 121 <span class="d">- ;; 7 Index search server</span> 122 <span class="d">- (check "7"</span> 123 <span class="d">- (print-with-color "selector 7 not implemented" 'red))</span> 124 <span class="d">-</span> 125 <span class="d">- ;; 8 Telnet session</span> 126 <span class="d">- (check "8"</span> 127 <span class="d">- (print-with-color "selector 8 not implemented" 'red))</span> 128 <span class="d">-</span> 129 <span class="d">- ;; 9 Binary</span> 130 <span class="d">- (check "9"</span> 131 <span class="d">- (print-with-color "selector 9 not implemented" 'red))</span> 132 <span class="d">-</span> 133 <span class="d">- ;; + redundant server</span> 134 <span class="d">- (check "+"</span> 135 <span class="d">- (print-with-color "selector + not implemented" 'red))</span> 136 <span class="d">-</span> 137 <span class="d">- ;; T text based tn3270 session</span> 138 <span class="d">- (check "T"</span> 139 <span class="d">- (print-with-color "selector T not implemented" 'red))</span> 140 <span class="d">-</span> 141 <span class="d">- ;; g GIF file</span> 142 <span class="d">- (check "g"</span> 143 <span class="d">- (print-with-color "selector g not implemented" 'red))</span> 144 <span class="d">-</span> 145 <span class="d">- ;; I image</span> 146 <span class="d">- (check "I"</span> 147 <span class="d">- (print-with-color "selector I not implemented" 'red))</span> 148 <span class="d">-</span> 149 <span class="d">- ;; h http link</span> 150 <span class="d">- (check "h"</span> 151 <span class="d">- (print-with-color (concatenate 'string</span> 152 <span class="d">- text " " uri)</span> 153 <span class="d">- 'http "url"))))))</span> 154 <span class="a">+ (text (car infos))</span> 155 <span class="a">+ (uri (cadr infos))</span> 156 <span class="a">+ (host (caddr infos))</span> 157 <span class="a">+ (port (parse-integer (cadddr infos))))</span> 158 <span class="a">+</span> 159 <span class="a">+ ;; see RFC 1436</span> 160 <span class="a">+ ;; section 3.8</span> 161 <span class="a">+ (if (member line-type *allowed-selectors* :test #'equal)</span> 162 <span class="a">+ (progn</span> 163 <span class="a">+</span> 164 <span class="a">+ ;; RFC, page 4</span> 165 <span class="a">+ (check "i"</span> 166 <span class="a">+ (print-with-color text))</span> 167 <span class="a">+</span> 168 <span class="a">+ ;; 0 text file</span> 169 <span class="a">+ (check "0"</span> 170 <span class="a">+ (setf (gethash line-number *links*)</span> 171 <span class="a">+ (make-location :host host :port port :uri uri :type line-type ))</span> 172 <span class="a">+ (print-with-color text 'file line-number))</span> 173 <span class="a">+</span> 174 <span class="a">+ ;; 1 directory</span> 175 <span class="a">+ (check "1"</span> 176 <span class="a">+ (setf (gethash line-number *links*)</span> 177 <span class="a">+ (make-location :host host :port port :uri uri :type line-type ))</span> 178 <span class="a">+ (print-with-color text 'folder line-number))</span> 179 <span class="a">+</span> 180 <span class="a">+ ;; 2 CSO phone-book</span> 181 <span class="a">+ ;; WE SKIP</span> 182 <span class="a">+ (check "2")</span> 183 <span class="a">+</span> 184 <span class="a">+ ;; 3 Error</span> 185 <span class="a">+ (check "3"</span> 186 <span class="a">+ (print-with-color "error" 'red line-number))</span> 187 <span class="a">+</span> 188 <span class="a">+ ;; 4 BinHexed Mac file</span> 189 <span class="a">+ (check "4"</span> 190 <span class="a">+ (print-with-color text))</span> 191 <span class="a">+</span> 192 <span class="a">+ ;; 5 DOS Binary archive</span> 193 <span class="a">+ (check "5"</span> 194 <span class="a">+ (print-with-color "selector 5 not implemented" 'red))</span> 195 <span class="a">+</span> 196 <span class="a">+ ;; 6 Unix uuencoded file</span> 197 <span class="a">+ (check "6"</span> 198 <span class="a">+ (print-with-color "selector 6 not implemented" 'red))</span> 199 <span class="a">+</span> 200 <span class="a">+ ;; 7 Index search server</span> 201 <span class="a">+ (check "7"</span> 202 <span class="a">+ (print-with-color "selector 7 not implemented" 'red))</span> 203 <span class="a">+</span> 204 <span class="a">+ ;; 8 Telnet session</span> 205 <span class="a">+ (check "8"</span> 206 <span class="a">+ (print-with-color "selector 8 not implemented" 'red))</span> 207 <span class="a">+</span> 208 <span class="a">+ ;; 9 Binary</span> 209 <span class="a">+ (check "9"</span> 210 <span class="a">+ (print-with-color "selector 9 not implemented" 'red))</span> 211 <span class="a">+</span> 212 <span class="a">+ ;; + redundant server</span> 213 <span class="a">+ (check "+"</span> 214 <span class="a">+ (print-with-color "selector + not implemented" 'red))</span> 215 <span class="a">+</span> 216 <span class="a">+ ;; T text based tn3270 session</span> 217 <span class="a">+ (check "T"</span> 218 <span class="a">+ (print-with-color "selector T not implemented" 'red))</span> 219 <span class="a">+</span> 220 <span class="a">+ ;; g GIF file</span> 221 <span class="a">+ (check "g"</span> 222 <span class="a">+ (print-with-color "selector g not implemented" 'red))</span> 223 <span class="a">+</span> 224 <span class="a">+ ;; I image</span> 225 <span class="a">+ (check "I"</span> 226 <span class="a">+ (print-with-color "selector I not implemented" 'red))</span> 227 <span class="a">+</span> 228 <span class="a">+ ;; h http link</span> 229 <span class="a">+ (check "h"</span> 230 <span class="a">+ (print-with-color (concatenate 'string</span> 231 <span class="a">+ text " " uri)</span> 232 <span class="a">+ 'http "url")))</span> 233 <span class="a">+ ;; unknown type</span> 234 <span class="a">+ (print-with-color (format nil</span> 235 <span class="a">+ "invalid type ~a : ~a" line-type text)</span> 236 <span class="a">+ 'red))))))</span> 237 238 (defun getpage(host port uri) 239 "connect and display" 240 241 ;; we reset the buffer 242 (setf *buffer* 243 <span class="d">- (make-array 200</span> 244 <span class="d">- :fill-pointer 0</span> 245 <span class="d">- :initial-element nil</span> 246 <span class="d">- :adjustable t))</span> 247 <span class="a">+ (make-array 200</span> 248 <span class="a">+ :fill-pointer 0</span> 249 <span class="a">+ :initial-element nil</span> 250 <span class="a">+ :adjustable t))</span> 251 252 ;; we prepare informations about the connection 253 (let* ((address (sb-bsd-sockets:get-host-by-name host)) 254 <span class="d">- (host (car (sb-bsd-sockets:host-ent-addresses address)))</span> 255 <span class="d">- (socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp)))</span> 256 <span class="a">+ (host (car (sb-bsd-sockets:host-ent-addresses address)))</span> 257 <span class="a">+ (socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp)))</span> 258 259 (sb-bsd-sockets:socket-connect socket host port) 260 261 <span class="h">@@ -237,9 +243,9 @@</span> 262 263 ;; for each line we receive we display it 264 (loop for line = (read-line stream nil nil) 265 <span class="d">- while line</span> 266 <span class="d">- do</span> 267 <span class="d">- (vector-push line *buffer*)))))</span> 268 <span class="a">+ while line</span> 269 <span class="a">+ do</span> 270 <span class="a">+ (vector-push line *buffer*)))))</span> 271 272 (defun g(key) 273 "browse to the N-th link" 274 <span class="h">@@ -263,14 +269,14 @@</span> 275 "Restore the bookmark from file" 276 (when (probe-file *bookmark-file*) 277 (with-open-file (x *bookmark-file* :direction :input) 278 <span class="d">- (setf *bookmarks* (read x)))))</span> 279 <span class="a">+ (setf *bookmarks* (read x)))))</span> 280 281 (defun save-bookmark() 282 "Dump the bookmark to file" 283 (with-open-file (x *bookmark-file* 284 <span class="d">- :direction :output</span> 285 <span class="d">- :if-does-not-exist :create</span> 286 <span class="d">- :if-exists :supersede)</span> 287 <span class="a">+ :direction :output</span> 288 <span class="a">+ :if-does-not-exist :create</span> 289 <span class="a">+ :if-exists :supersede)</span> 290 (print *bookmarks* x))) 291 292 (defun add-bookmark() 293 <span class="h">@@ -289,13 +295,13 @@</span> 294 while bookmark 295 do 296 (progn 297 <span class="d">- (setf (gethash line-number *links*) bookmark)</span> 298 <span class="d">- (print-with-color (concatenate 'string</span> 299 <span class="d">- (location-host bookmark)</span> 300 <span class="d">- " "</span> 301 <span class="d">- (location-type bookmark)</span> 302 <span class="d">- (location-uri bookmark))</span> 303 <span class="d">- 'file line-number))))</span> 304 <span class="a">+ (setf (gethash line-number *links*) bookmark)</span> 305 <span class="a">+ (print-with-color (concatenate 'string</span> 306 <span class="a">+ (location-host bookmark)</span> 307 <span class="a">+ " "</span> 308 <span class="a">+ (location-type bookmark)</span> 309 <span class="a">+ (location-uri bookmark))</span> 310 <span class="a">+ 'file line-number))))</span> 311 (defun help-shell() 312 "show help for the shell" 313 (format t "number : go to link n~%") 314 <span class="h">@@ -310,13 +316,13 @@</span> 315 (defun parse-url(url) 316 "parse a gopher url and return a location" 317 (let ((url (if (and 318 <span class="d">- ;; if it contains more chars than gopher://</span> 319 <span class="d">- (<= (length "gopher://") (length url))</span> 320 <span class="d">- ;; if it starts with gopher// with return without it</span> 321 <span class="d">- (string= "gopher://" (subseq url 0 9)))</span> 322 <span class="d">- ;; we keep the url as is</span> 323 <span class="d">- (subseq url 9)</span> 324 <span class="d">- url)))</span> 325 <span class="a">+ ;; if it contains more chars than gopher://</span> 326 <span class="a">+ (<= (length "gopher://") (length url))</span> 327 <span class="a">+ ;; if it starts with gopher// with return without it</span> 328 <span class="a">+ (string= "gopher://" (subseq url 0 9)))</span> 329 <span class="a">+ ;; we keep the url as is</span> 330 <span class="a">+ (subseq url 9)</span> 331 <span class="a">+ url)))</span> 332 333 ;; splitting by / to get host:port and uri 334 (let ((infos (split url #\/))) 335 <span class="h">@@ -324,20 +330,20 @@</span> 336 ;; splitting host and port to get them 337 (let ((host-port (split (pop infos) #\:))) 338 339 <span class="d">- ;; create the location to visit</span> 340 <span class="d">- (make-location :host (pop host-port)</span> 341 <span class="a">+ ;; create the location to visit</span> 342 <span class="a">+ (make-location :host (pop host-port)</span> 343 344 <span class="d">- ;; default to port 70 if not supplied</span> 345 <span class="d">- :port (if host-port</span> 346 <span class="d">- (parse-integer (car host-port))</span> 347 <span class="d">- 70)</span> 348 <span class="a">+ ;; default to port 70 if not supplied</span> 349 <span class="a">+ :port (if host-port</span> 350 <span class="a">+ (parse-integer (car host-port))</span> 351 <span class="a">+ 70)</span> 352 353 <span class="d">- ;; if type is empty we use "1"</span> 354 <span class="d">- :type (let ((type (pop infos)))</span> 355 <span class="d">- (if (< 0 (length type)) type "1"))</span> 356 <span class="a">+ ;; if type is empty we use "1"</span> 357 <span class="a">+ :type (let ((type (pop infos)))</span> 358 <span class="a">+ (if (< 0 (length type)) type "1"))</span> 359 360 <span class="d">- ;; glue remaining args between them</span> 361 <span class="d">- :uri (format nil "~{/~a~}" infos))))))</span> 362 <span class="a">+ ;; glue remaining args between them</span> 363 <span class="a">+ :uri (format nil "~{/~a~}" infos))))))</span> 364 365 366 (defun get-argv() 367 <span class="h">@@ -450,8 +456,8 @@</span> 368 "visit a location" 369 370 (getpage (location-host destination) 371 <span class="d">- (location-port destination)</span> 372 <span class="d">- (location-uri destination))</span> 373 <span class="a">+ (location-port destination)</span> 374 <span class="a">+ (location-uri destination))</span> 375 376 ;; we reset the links table ONLY if we have a new folder 377 (when (string= "1" (location-type destination)) 378 <span class="h">@@ -462,21 +468,21 @@</span> 379 380 (when *offline* 381 (let ((path (concatenate 'string 382 <span class="d">- "history/" (location-host destination)</span> 383 <span class="d">- "/" (location-uri destination) "/")))</span> 384 <span class="a">+ "history/" (location-host destination)</span> 385 <span class="a">+ "/" (location-uri destination) "/")))</span> 386 (ensure-directories-exist path) 387 388 (with-open-file 389 <span class="d">- (save-offline (concatenate</span> 390 <span class="d">- 'string path (location-type destination))</span> 391 <span class="d">- :direction :output</span> 392 <span class="d">- :if-does-not-exist :create</span> 393 <span class="d">- :if-exists :supersede)</span> 394 <span class="a">+ (save-offline (concatenate</span> 395 <span class="a">+ 'string path (location-type destination))</span> 396 <span class="a">+ :direction :output</span> 397 <span class="a">+ :if-does-not-exist :create</span> 398 <span class="a">+ :if-exists :supersede)</span> 399 400 <span class="d">- (loop for line across *buffer*</span> 401 <span class="d">- while line</span> 402 <span class="d">- do</span> 403 <span class="d">- (format save-offline "~a~%" line)))))</span> 404 <span class="a">+ (loop for line across *buffer*</span> 405 <span class="a">+ while line</span> 406 <span class="a">+ do</span> 407 <span class="a">+ (format save-offline "~a~%" line)))))</span> 408 409 (display-buffer (location-type destination))) 410 411 <span class="h">@@ -489,24 +495,24 @@</span> 412 ;; we loop until X or Q is typed 413 (loop for input = (format nil "~a" (read-line nil nil)) 414 while (not (or 415 <span class="d">- (string= "exit" input)</span> 416 <span class="d">- (string= "x" input)</span> 417 <span class="d">- (string= "q" input)))</span> 418 <span class="a">+ (string= "exit" input)</span> 419 <span class="a">+ (string= "x" input)</span> 420 <span class="a">+ (string= "q" input)))</span> 421 do 422 (when (eq 'end (user-input input)) 423 <span class="d">- (loop-finish))</span> 424 <span class="a">+ (loop-finish))</span> 425 (format t "clic => ") 426 (force-output))) 427 428 (defun main() 429 "fetch argument, display page and go to shell if type is 1" 430 (let ((destination 431 <span class="d">- (let ((argv (get-argv)))</span> 432 <span class="d">- (if argv</span> 433 <span class="d">- ;; url as argument</span> 434 <span class="d">- (parse-url argv)</span> 435 <span class="d">- ;; default url</span> 436 <span class="d">- (make-location :host "gopherproject.org" :port 70 :uri "/" :type "1")))))</span> 437 <span class="a">+ (let ((argv (get-argv)))</span> 438 <span class="a">+ (if argv</span> 439 <span class="a">+ ;; url as argument</span> 440 <span class="a">+ (parse-url argv)</span> 441 <span class="a">+ ;; default url</span> 442 <span class="a">+ (make-location :host "gopherproject.org" :port 70 :uri "/" :type "1")))))</span> 443 444 ;; if user want to drop from first page we need 445 ;; to look it here 446 <span class="h">@@ -514,7 +520,7 @@</span> 447 ;; we continue to the shell if the type was 1 and we are in a terminal 448 (when (and (ttyp) 449 (string= "1" (location-type destination))) 450 <span class="d">- (shell)))))</span> 451 <span class="a">+ (shell)))))</span> 452 453 ;; we allow ecl to use a new kind of argument 454 ;; not sure how it works but that works</pre> 455 </div></body></html>