Subversion Repositories Keine

Rev

Rev 5 | Rev 7 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 6
Line 1... Line 1...
1
/* $Id: cgi.c 5 2024-09-11 10:27:37Z nishi $ */
1
/* $Id: cgi.c 6 2024-09-11 15:45:32Z nishi $ */
2
 
2
 
3
#include "../config.h"
3
#include "../config.h"
4
 
4
 
5
#include "kn_cgi.h"
5
#include "kn_cgi.h"
6
 
6
 
-
 
7
#include <dirent.h>
7
#include <stdlib.h>
8
#include <stdlib.h>
8
#include <string.h>
9
#include <string.h>
9
#include <stdbool.h>
10
#include <stdbool.h>
-
 
11
#include <sys/stat.h>
10
#include <stdio.h>
12
#include <stdio.h>
11
 
13
 
12
#include "kn_version.h"
14
#include "kn_version.h"
13
#include "kn_util.h"
15
#include "kn_util.h"
14
#include "kn_man.h"
16
#include "kn_man.h"
Line 32... Line 34...
32
	return NULL;
34
	return NULL;
33
}
35
}
34
 
36
 
35
char* kn_null(const char* a) { return a == NULL ? "" : (char*)a; }
37
char* kn_null(const char* a) { return a == NULL ? "" : (char*)a; }
36
 
38
 
-
 
39
void manpage_scan(const char* root) {
-
 
40
	struct dirent** nl;
-
 
41
	int n = scandir(root, &nl, NULL, alphasort);
-
 
42
	if(n < 0) return;
-
 
43
	int i;
-
 
44
	for(i = 0; i < n; i++) {
-
 
45
		if(strcmp(nl[i]->d_name, ".") != 0 && strcmp(nl[i]->d_name, "..") != 0) {
-
 
46
			char* path = kn_strcat3(root, "/", nl[i]->d_name);
-
 
47
			struct stat s;
-
 
48
			if(stat(path, &s) == 0) {
-
 
49
				if(S_ISDIR(s.st_mode)) {
-
 
50
					manpage_scan(path);
-
 
51
				} else {
-
 
52
					char* name = kn_strdup(nl[i]->d_name);
-
 
53
					char* desc = kn_strdup("dadsandasndasdsadmadsadmsmdsmdmdksmdkamdkmdksmdsmdmskdmdmsakdsdmsakdmskdmsdmsadkmsakdmdsdmsdkamdkmdkamdksmdksamkdsmdkmadmsakdmsakdmmdakmdsm");
-
 
54
 
-
 
55
					int incr = 0;
-
 
56
					FILE* f = fopen(path, "r");
-
 
57
					char* b = malloc(s.st_size + 1);
-
 
58
					b[s.st_size] = 0;
-
 
59
					fread(b, s.st_size, 1, f);
-
 
60
					fclose(f);
-
 
61
 
-
 
62
					int j;
-
 
63
					for(j = 0;; j++){
-
 
64
						if(b[j] == '\n' || b[j] == 0){
-
 
65
							char* line = malloc(j - incr + 1);
-
 
66
							line[j - incr] = 0;
-
 
67
							memcpy(line, b + incr, j - incr);
-
 
68
 
-
 
69
							int k;
-
 
70
							for(k = 0; line[k] != 0 && k < 4; k++){
-
 
71
								if(line[k] == ' '){
-
 
72
									line[k] = 0;
-
 
73
									if(strcasecmp(line, ".Nd") == 0){
-
 
74
										free(desc);
-
 
75
										desc = kn_strdup(line + k + 1);
-
 
76
										int l;
-
 
77
										for(l = 0; desc[l] != 0; l++){
-
 
78
											if(desc[l] == '\\'){
-
 
79
												l++;
-
 
80
												if(desc[l] == '"'){
-
 
81
													l--;
-
 
82
													desc[l] = 0;
-
 
83
													break;
-
 
84
												}
-
 
85
											}
-
 
86
										}
-
 
87
									}
-
 
88
									break;
-
 
89
								}
-
 
90
							}
-
 
91
 
-
 
92
							free(line);
-
 
93
							incr = j + 1;
-
 
94
							if(b[j] == 0) break;
-
 
95
						}
-
 
96
					}
-
 
97
 
-
 
98
					free(b);
-
 
99
 
-
 
100
					if(strlen(desc) > 70){
-
 
101
						desc[70] = 0;
-
 
102
						desc[69] = '.';
-
 
103
						desc[68] = '.';
-
 
104
						desc[67] = '.';
-
 
105
					}
-
 
106
 
-
 
107
					printf("<tr>\n");
-
 
108
					printf("	<td><a href=\"?page=%s\">%s</a></td>\n", name, name);
-
 
109
					printf("	<td><code>%s</code></td>\n", desc);
-
 
110
					printf("</tr>\n");
-
 
111
 
-
 
112
					free(name);
-
 
113
					free(desc);
-
 
114
				}
-
 
115
			}
-
 
116
			free(path);
-
 
117
		}
-
 
118
		free(nl[i]);
-
 
119
	}
-
 
120
	free(nl);
-
 
121
}
-
 
122
 
-
 
123
void list_manpages(void) {
-
 
124
#ifdef MANPAGE_DIRS
-
 
125
	int i;
-
 
126
	const char* dirs[] = MANPAGE_DIRS;
-
 
127
	for(i = 0; i < sizeof(dirs) / sizeof(*dirs); i++){
-
 
128
		manpage_scan(dirs[i]);
-
 
129
	}
-
 
130
#else
-
 
131
	manpage_scan(MANPAGE_DIR);
-
 
132
#endif
-
 
133
}
-
 
134
 
37
void kn_parse_query(void) {
135
void kn_parse_query(void) {
38
	char* query = getenv("QUERY_STRING");
136
	char* query = getenv("QUERY_STRING");
39
	if(query != NULL) {
137
	if(query != NULL) {
40
		entries = malloc(sizeof(*entries));
138
		entries = malloc(sizeof(*entries));
41
		entries[0] = NULL;
139
		entries[0] = NULL;
Line 81... Line 179...
81
	}
179
	}
82
	if(kn_get_query("page") == NULL) {
180
	if(kn_get_query("page") == NULL) {
83
		printf("Status: 200 OK\n\n");
181
		printf("Status: 200 OK\n\n");
84
		showmain = true;
182
		showmain = true;
85
	} else {
183
	} else {
-
 
184
		bool cond = false;
-
 
185
#ifdef MANPAGE_DIRS
-
 
186
		int i;
-
 
187
		const char* dirs[] = MANPAGE_DIRS;
-
 
188
		for(i = 0; i < sizeof(dirs) / sizeof(*dirs); i++){
-
 
189
			cond = (path = kn_find(dirs[i], kn_get_query("page"))) != NULL;
-
 
190
			if(cond) break;
-
 
191
		}
-
 
192
#else
86
		if((path = kn_find(MANPAGE_DIR, kn_get_query("page"))) != NULL) {
193
		cond = (path = kn_find(MANPAGE_DIR, kn_get_query("page"))) != NULL;
-
 
194
#endif
-
 
195
		if(cond) {
87
			printf("Status: 200 OK\n\n");
196
			printf("Status: 200 OK\n\n");
88
		} else {
197
		} else {
89
			printf("Status: 404 Not Found\n\n");
198
			printf("Status: 404 Not Found\n\n");
90
			no = true;
199
			no = true;
91
		}
200
		}
Line 124... Line 233...
124
	printf("	</head>\n");
233
	printf("	</head>\n");
125
	printf("	<body>\n");
234
	printf("	<body>\n");
126
	printf("		<div style=\"text-align: center;\">\n");
235
	printf("		<div style=\"text-align: center;\">\n");
127
	printf("			<form action=\"%s%s\">\n", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
236
	printf("			<form action=\"%s%s\">\n", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
128
	printf("				<a href=\"%s%s\">Main</a> | ", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
237
	printf("				<a href=\"%s%s\">Main</a> | ", getenv("SCRIPT_NAME"), kn_null(getenv("PATH_INFO")));
129
	printf("				Name: <input name=\"page\">\n");
238
	printf("				Name: <input name=\"page\"%s%s%s>\n", kn_get_query("page") == NULL ? "" : " value=\"", kn_get_query("page") == NULL ? "" : kn_get_query("page"), kn_get_query("page") == NULL ? "" : "\"");
130
	printf("				<input type=\"submit\">\n");
239
	printf("				<input type=\"submit\">\n");
131
	printf("			</form>\n");
240
	printf("			</form>\n");
132
	printf("		</div>\n");
241
	printf("		</div>\n");
133
	printf("		<hr>\n");
242
	printf("		<hr>\n");
134
	if(no) {
243
	if(no) {
135
		printf("		Not found.\n");
244
		printf("		Not found.\n");
136
	} else if(showmain) {
245
	} else if(showmain) {
-
 
246
#ifdef MAIN_HTML
137
		printf("%s", MAIN_HTML);
247
		printf("%s", MAIN_HTML);
-
 
248
#else
-
 
249
		printf("<h1>Index</h1>\n");
-
 
250
		printf("<table border=\"0\" style=\"width: 900px;\">\n");
-
 
251
		printf("	<tr>\n");
-
 
252
		printf("		<th style=\"width: 50px;\">Name</th>\n");
-
 
253
		printf("		<th>Description</th>\n");
-
 
254
		printf("	</tr>\n");
-
 
255
		list_manpages();
-
 
256
		printf("</table>\n");
-
 
257
#endif
138
	} else {
258
	} else {
139
		printf("<pre>");
259
		printf("<pre>");
140
		char* c = kn_manpage_process(path);
260
		char* c = kn_manpage_process(path);
141
		if(c != NULL) {
261
		if(c != NULL) {
142
			printf("%s", c);
262
			printf("%s", c);