3535#include "ssl.h"
3636#include "iconv/iconv_mini.h"
3737#ifdef __human68k__
38+ #include <x68k/dos.h>
3839#include <x68k/iocs.h>
3940#endif
4041
@@ -304,6 +305,7 @@ void help(const char *name)
304305 " -H, --show-header Show HTTP response header\n"
305306 " -r, --raw Display raw HTML without parsing\n"
306307 " -s, --sjis No UTF-8 -> SJIS conversion\n"
308+ " -p, --page Paging mode\n"
307309 , name );
308310 exit (0 );
309311}
@@ -322,6 +324,7 @@ int main(int argc, char **argv)
322324 int raw_mode = 0 ;
323325 int sjis_mode = 0 ;
324326 int http_mode = 0 ;
327+ int paging_mode = 0 ;
325328
326329 path = "/" ;
327330 port = 443 ;
@@ -335,6 +338,8 @@ int main(int argc, char **argv)
335338 raw_mode = 1 ;
336339 } else if (strcmp (argv [i ], "-s" ) == 0 || strcmp (argv [i ], "--sjis" ) == 0 ) {
337340 sjis_mode = 1 ;
341+ } else if (strcmp (argv [i ], "-p" ) == 0 || strcmp (argv [i ], "--page" ) == 0 ) {
342+ paging_mode = 1 ;
338343 } else if (argv [i ][0 ] == '-' ) {
339344 fprintf (stderr , "Error: Unknown option %s\n" , argv [i ]);
340345 return 1 ;
@@ -534,20 +539,26 @@ int main(int argc, char **argv)
534539
535540 // HTTPレスポンスからHTMLボディを抽出
536541 char * html_body = extract_html_body ((char * )response );
537- if (show_header ) {
542+ if (show_header || raw_mode ) {
538543 printf ("HTML Header:\n" );
539544 printf ("--------------------\n" );
540545 fwrite (response , 1 , html_body - (char * )response , stdout );
541546 printf ("--------------------\n\n" );
542547 }
543548
549+ uint8_t * sbuffer = NULL ;
550+
544551 // HTMLのパースと表示
545552 if (!raw_mode ) {
546553 parse_html (html_body , sjis_mode );
547554 size_t html_body_len = strlen (html_body );
548555
549556 if (!sjis_mode ) {
550- uint8_t * sbuffer = malloc (html_body_len );
557+ sbuffer = malloc (html_body_len );
558+ if (sbuffer == NULL ) {
559+ fprintf (stderr , "Error: Memory allocation failed\n" );
560+ return 1 ;
561+ }
551562
552563 uint8_t * inbuf = (uint8_t * )html_body ;
553564 size_t inbytesleft = html_body_len ;
@@ -563,13 +574,52 @@ int main(int argc, char **argv)
563574 } while ((char * )inbuf < html_body + html_body_len );
564575 * outbuf = 0 ;
565576
566- printf ("%s" , sbuffer );
567- free (sbuffer );
568- } else {
569- printf ("%s" , html_body );
577+ html_body = (char * )sbuffer ;
578+ }
579+ }
580+
581+ if (paging_mode ) {
582+ char * p = html_body ;
583+ char * q = p ;
584+ int page_len = 28 ;
585+ while (1 ) {
586+ int count = 0 ;
587+ do {
588+ q = strchr (q , '\n' );
589+ if (q == NULL ) {
590+ break ;
591+ }
592+ q ++ ;
593+ count ++ ;
594+ } while (count < page_len );
595+ if (q == NULL ) {
596+ printf ("%s" , p );
597+ break ;
598+ }
599+ fwrite (p , 1 , q - p , stdout );
600+ p = q ;
601+ printf ("-- More --" );
602+ fflush (stdout );
603+ #ifdef __human68k__
604+ int c = _dos_getc ();
605+ #else
606+ int c = getchar ();
607+ #endif
608+ printf ("\r \r" );
609+ if (c == 'q' || c == 'Q' ) {
610+ break ;
611+ } else if (c == ' ' ) {
612+ page_len = 1 ;
613+ } else {
614+ page_len = 28 ;
615+ }
570616 }
571617 } else {
572- printf ("%s" , html_body );
618+ printf ("%s\n" , html_body );
619+ }
620+
621+ if (sbuffer ) {
622+ free (sbuffer );
573623 }
574624
575625 return 0 ;
0 commit comments