From f1b725437db6385aa8c6818f22a4bbf88c8b136d Mon Sep 17 00:00:00 2001 From: Nishi Date: Wed, 11 Sep 2024 15:45:32 +0000 Subject: [PATCH] can show manpage git-svn-id: file:///raid/svn-personal/keine/trunk@6 a3977ea8-0dc0-2842-9144-a1a46b47fd40 --- CGI/cgi.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++- CGI/kn_util.h | 2 + CGI/man.c | 13 ++++++ config.h.tmpl | 9 +++- 4 files changed, 144 insertions(+), 4 deletions(-) diff --git a/CGI/cgi.c b/CGI/cgi.c index 0788d9c..3c2aa34 100644 --- a/CGI/cgi.c +++ b/CGI/cgi.c @@ -4,9 +4,11 @@ #include "kn_cgi.h" +#include #include #include #include +#include #include #include "kn_version.h" @@ -34,6 +36,102 @@ char* kn_get_query(const char* key) { char* kn_null(const char* a) { return a == NULL ? "" : (char*)a; } +void manpage_scan(const char* root) { + struct dirent** nl; + int n = scandir(root, &nl, NULL, alphasort); + if(n < 0) return; + int i; + for(i = 0; i < n; i++) { + if(strcmp(nl[i]->d_name, ".") != 0 && strcmp(nl[i]->d_name, "..") != 0) { + char* path = kn_strcat3(root, "/", nl[i]->d_name); + struct stat s; + if(stat(path, &s) == 0) { + if(S_ISDIR(s.st_mode)) { + manpage_scan(path); + } else { + char* name = kn_strdup(nl[i]->d_name); + char* desc = kn_strdup("dadsandasndasdsadmadsadmsmdsmdmdksmdkamdkmdksmdsmdmskdmdmsakdsdmsakdmskdmsdmsadkmsakdmdsdmsdkamdkmdkamdksmdksamkdsmdkmadmsakdmsakdmmdakmdsm"); + + int incr = 0; + FILE* f = fopen(path, "r"); + char* b = malloc(s.st_size + 1); + b[s.st_size] = 0; + fread(b, s.st_size, 1, f); + fclose(f); + + int j; + for(j = 0;; j++){ + if(b[j] == '\n' || b[j] == 0){ + char* line = malloc(j - incr + 1); + line[j - incr] = 0; + memcpy(line, b + incr, j - incr); + + int k; + for(k = 0; line[k] != 0 && k < 4; k++){ + if(line[k] == ' '){ + line[k] = 0; + if(strcasecmp(line, ".Nd") == 0){ + free(desc); + desc = kn_strdup(line + k + 1); + int l; + for(l = 0; desc[l] != 0; l++){ + if(desc[l] == '\\'){ + l++; + if(desc[l] == '"'){ + l--; + desc[l] = 0; + break; + } + } + } + } + break; + } + } + + free(line); + incr = j + 1; + if(b[j] == 0) break; + } + } + + free(b); + + if(strlen(desc) > 70){ + desc[70] = 0; + desc[69] = '.'; + desc[68] = '.'; + desc[67] = '.'; + } + + printf("\n"); + printf(" %s\n", name, name); + printf(" %s\n", desc); + printf("\n"); + + free(name); + free(desc); + } + } + free(path); + } + free(nl[i]); + } + free(nl); +} + +void list_manpages(void) { +#ifdef MANPAGE_DIRS + int i; + const char* dirs[] = MANPAGE_DIRS; + for(i = 0; i < sizeof(dirs) / sizeof(*dirs); i++){ + manpage_scan(dirs[i]); + } +#else + manpage_scan(MANPAGE_DIR); +#endif +} + void kn_parse_query(void) { char* query = getenv("QUERY_STRING"); if(query != NULL) { @@ -83,7 +181,18 @@ void kn_parse_query(void) { printf("Status: 200 OK\n\n"); showmain = true; } else { - if((path = kn_find(MANPAGE_DIR, kn_get_query("page"))) != NULL) { + bool cond = false; +#ifdef MANPAGE_DIRS + int i; + const char* dirs[] = MANPAGE_DIRS; + for(i = 0; i < sizeof(dirs) / sizeof(*dirs); i++){ + cond = (path = kn_find(dirs[i], kn_get_query("page"))) != NULL; + if(cond) break; + } +#else + cond = (path = kn_find(MANPAGE_DIR, kn_get_query("page"))) != NULL; +#endif + if(cond) { printf("Status: 200 OK\n\n"); } else { printf("Status: 404 Not Found\n\n"); @@ -126,7 +235,7 @@ void kn_cgi(void) { printf("
\n"); printf("
\n", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO"))); printf(" Main | ", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO"))); - printf(" Name: \n"); + printf(" Name: \n", kn_get_query("page") == NULL ? "" : " value=\"", kn_get_query("page") == NULL ? "" : kn_get_query("page"), kn_get_query("page") == NULL ? "" : "\""); printf(" \n"); printf("
\n"); printf("
\n"); @@ -134,7 +243,18 @@ void kn_cgi(void) { if(no) { printf(" Not found.\n"); } else if(showmain) { +#ifdef MAIN_HTML printf("%s", MAIN_HTML); +#else + printf("

Index

\n"); + printf("\n"); + printf(" \n"); + printf(" \n"); + printf(" \n"); + printf(" \n"); + list_manpages(); + printf("
NameDescription
\n"); +#endif } else { printf("
");
 		char* c = kn_manpage_process(path);
diff --git a/CGI/kn_util.h b/CGI/kn_util.h
index a43f997..cbd7148 100644
--- a/CGI/kn_util.h
+++ b/CGI/kn_util.h
@@ -3,6 +3,8 @@
 #ifndef __KN_UTIL_H__
 #define __KN_UTIL_H__
 
+#include 
+
 char* kn_strcat(const char* a, const char* b);
 char* kn_strcat3(const char* a, const char* b, const char* c);
 char* kn_strdup(const char* a);
diff --git a/CGI/man.c b/CGI/man.c
index fdcbaf9..3d637c5 100644
--- a/CGI/man.c
+++ b/CGI/man.c
@@ -44,9 +44,22 @@ char* kn_find(const char* root, const char* name) {
 }
 
 bool kn_has_manpage(const char* str) {
+#ifdef MANPAGE_DIRS
+	int i;
+	const char* dirs[] = MANPAGE_DIRS;
+	for(i = 0; i < sizeof(dirs) / sizeof(*dirs); i++){
+		char* pth = kn_find(dirs[i], str);
+		if(pth != NULL){
+			free(pth);
+			return true;
+		}
+	}
+	return false;
+#else
 	char* pth = kn_find(MANPAGE_DIR, str);
 	if(pth == NULL) return false;
 	free(pth);
+#endif
 	return true;
 }
 
diff --git a/config.h.tmpl b/config.h.tmpl
index 6653292..521127c 100644
--- a/config.h.tmpl
+++ b/config.h.tmpl
@@ -3,13 +3,18 @@
 #ifndef __CONFIG_H__
 #define __CONFIG_H__
 
-/* HTML which gets shown on the main. */
+/* HTML which gets shown on the main, undef it for the index */
 #define MAIN_HTML "

Index

" /* Manual page root */ #define MANPAGE_DIR "/usr/share/man" -/* Footer, comment it for no footer */ +/* Define this for multiple directories */ +/* +#define MANPAGE_DIRS {"/usr/share/man/man1", "/usr/share/man/man2", "/usr/share/man/man3", "/usr/share/man/man3f", "/usr/share/man/man3lua", "/usr/share/man/man4", "/usr/share/man/man5", "/usr/share/man/man6", "/usr/share/man/man7", "/usr/share/man/man8", "/usr/share/man/man9", "/usr/share/man/man9lua"} +*/ + +/* Footer, undef it for no footer */ #define FOOTER_HTML "some footer" #endif -- 2.43.0