commit 0a309421ac0c69b570fd63f12c20702fe253da52
Author: Hiltjo Posthuma <[email protected]>
Date:   Sat Mar 21 13:48:51 2020 +0100

    build-page: some additional improvements
    
    - just use PATH_MAX instead of a defined DIR_MAX.
    - add a print_gopher_name function to escape characters in the gopher
      "username" field for geomyidae gph format, just in case.
    - change off-by-one in strncpy and just NUL terminate the character instead 
of
      memset() for one character.
    - use options preferably before arguments.
    - bit more verbose and strict options parsing.
    - duplicate the memory for parsed argv arguments because it is modified.  
This
      makes a difference on some BSD's in the listed processes.
    
    Thanks for working on the gopher version for the wiki David!

diff --git a/Makefile b/Makefile
index 6cbf89bc..a0376530 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ LDFLAGS = -static -s
 all: html
 
 gph: build-page
-       find * -type d -exec sh -ec './build-page "$$0" -g >$$0/index.gph' {} \;
+       find * -type d -exec sh -ec './build-page -g "$$0" >$$0/index.gph' {} \;
 
 html: build-page
        find * -type d -exec sh -ec './build-page "$$0" >$$0/index.html' {} \;
diff --git a/build-page.c b/build-page.c
index 1bfd1cac..4b8cdaab 100644
--- a/build-page.c
+++ b/build-page.c
@@ -16,7 +16,6 @@
 #define LEN(x) (sizeof(x) / sizeof(x[0]))
 #define TITLE_MAX 1024
 #define TITLE_DEFAULT "suckless.org"
-#define DIR_MAX 1024
 
 #define GOPHER_ROW_MAX 80
 #define GOPHER_PORT 70
@@ -176,6 +175,32 @@ print_name(const char *name)
                putchar((c == '_' || c == '-') ? ' ' : c);
 }
 
+void
+print_gopher_name(const char *name)
+{
+       int c;
+
+       for (; (c = *name); ++name) {
+               switch (c) {
+               case '
': /* ignore CR */
+               case '
': /* ignore LF */
+                       break;
+               case '_':
+               case '-':
+                       putchar(' ');
+                       break;
+               case '  ':
+                       printf("        ");
+                       break;
+               case '|': /* escape separators */
+                       printf("\|");
+                       break;
+               default:
+                       putchar(c);
+               }
+       }
+}
+
 void
 print_header(void)
 {
@@ -241,7 +266,7 @@ menu_panel(char *domain, char *page, char *this, int depth)
        DIR *dp;
        struct dirent *de;
        char newdir[PATH_MAX];
-       char *d_list[DIR_MAX], *d;
+       char *d_list[PATH_MAX], *d;
        size_t d_len, l;
        int i, highlight;
 
@@ -342,14 +367,14 @@ print_gopher_item(char type, char *disp, char *domain, 
char *path,
        char d[GOPHER_ROW_MAX];
        int l;
 
-       strncpy(d, disp, sizeof d);
-       memset(d+GOPHER_ROW_MAX-1, '+   strncpy(d, disp, sizeof(d) - 1);
+       d[sizeof(d) - 1] = ' 
        printf("[%c|", type);
 
        for (l = 0; l < level; ++l)
                printf("  ");
-       print_name(d);
+       print_gopher_name(d);
        if (type == '1')
                putchar('/');
        putchar('|');
@@ -401,7 +426,7 @@ print_gopher_menu(char *domain, char *this)
        DIR *dp;
        struct dirent *de;
        char newdir[PATH_MAX];
-       char *d_list[DIR_MAX], *d;
+       char *d_list[PATH_MAX], *d;
        size_t d_len, l;
        int depth = this ? 1 : 0;
 
@@ -460,23 +485,44 @@ print_gopher_nav(char *domain)
                          GOPHER_PORT, 0);
 }
 
+void
+usage(char *argv0)
+{
+       die("usage: %s [-g] directory", argv0);
+}
+
 int
 main(int argc, char *argv[])
 {
-       char *domain, *page;
-       int gopher = 0;
-
-       if (argc != 2) {
-               if (argc != 3 || (strcmp(argv[2], "-g") != 0))
-                       die("usage: %s directory [-g]", argv[0]);
-               gopher = 1;
+       char *domain = NULL, *page;
+       int gopher = 0, i, j;
+
+       for (i = 1; i < argc; i++) {
+               if (argv[i][0] != '-') {
+                       if (domain)
+                               usage(argv[0]);
+                       domain = argv[i];
+                       continue;
+               }
+               for (j = 1; j < argv[i][j]; j++) {
+                       switch (argv[i][j]) {
+                       case 'g':
+                               gopher = 1;
+                               break;
+                       default:
+                               usage(argv[0]);
+                       }
+               }
        }
-       if ((page = strchr(argv[1], '/'))) {
+       if (domain == NULL)
+               usage(argv[0]);
+
+       domain = xstrdup(domain);
+       if ((page = strchr(domain, '/'))) {
                *page++ = '             if (strlen(page) == 0)
                        page = NULL;
        }
-       domain = argv[1];
        if (chdir(domain) == -1)
                die_perror("chdir: %s", domain);
 


Reply via email to