From 2c7da2eff5baa4c09cae36f12c1d99690113ae2e Mon Sep 17 00:00:00 2001 From: kristapsdz Date: Thu, 27 Feb 2020 21:43:01 +0000 Subject: [PATCH] Use style(9) and pkg-config. --- tutorial3.xml | 4 ++-- tutorial5.xml | 14 ++++++-------- tutorial6.xml | 36 ++++++++++++++++++------------------ 3 files changed, 26 insertions(+), 28 deletions(-) diff --git a/tutorial3.xml b/tutorial3.xml index 4527460..bb1eade 100644 --- a/tutorial3.xml +++ b/tutorial3.xml @@ -51,7 +51,7 @@ kvalid_png(struct kpair *kp) gdImagePtr im; int rc; im = gdImageCreateFromPngPtr(kp->valsz, kp->val); - if (0 != (rc = (NULL != im))) + if ((rc = (im != NULL))) gdImageDestroy(im); kp->type = KPAIR__MAX; return rc; @@ -63,7 +63,7 @@ kvalid_jpeg(struct kpair *kp) gdImagePtr im; int rc; im = gdImageCreateFromJpegPtr(kp->valsz, kp->val); - if (0 != (rc = (NULL != im))) + if ((rc = (im != NULL))) gdImageDestroy(im); kp->type = KPAIR__MAX; return rc; diff --git a/tutorial5.xml b/tutorial5.xml index dfc21b9..5e83618 100644 --- a/tutorial5.xml +++ b/tutorial5.xml @@ -47,14 +47,14 @@
int 
 main(void) {
-    enum kcgi_err	  er;
-    struct kreq	  r;
+    enum kcgi_err er;
+    struct kreq r;
     const char *const pages[1] = { "index" };
 
     /* Set up our main HTTP context. */
 
     er = khttp_parse(&r, NULL, 0, pages, 1, 0);
-    if (KCGI_OK != er)
+    if (er != KCGI_OK)
         return 0;
     khttp_head(&r, kresps[KRESP_STATUS], 
         "%s", khttps[KHTTP_200]);
@@ -72,19 +72,17 @@ main(void) {
 		The difference is that we've used some C++ code to emit Said hello! to the error log.
 		The next part is how we can compile this code.
 		For that, we'll need to use a C++ compiler instead of the C compiler we've been using to date.
-		For the sake of argument, let's assume that the kcgi headers are libraries are installed in the system directories.
-		However, mostly they'll need -L/usr/local/lib and -I/usr/local/include.
 	

-
% c++ -W -Wall -Wextra -c -o tutorial5.o tutorial5.cc
-% c++ -static -W -Wall -Wextra -o tutorial5 tutorial5.cc -lkcgi -lz
+
% c++ `pkg-config --cflags kcgi` -c -o tutorial5.o tutorial5.cc
+% c++ -static -o tutorial5 tutorial5.cc `pkg-config --libs kcgi`

Or just… (and noting again the -static, which we need by being in our file-system jail)

-
% c++ -static -W -Wall -Wextra -o tutorial5 tutorial5.cc -lkcgi -lz
+
% c++ -static `pkg-config --cflags --libs kcgi` -o tutorial5 tutorial5.cc

Now you can install your compiled CGI script just like any CGI script. diff --git a/tutorial6.xml b/tutorial6.xml index dbb4b55..84d97cb 100644 --- a/tutorial6.xml +++ b/tutorial6.xml @@ -68,7 +68,7 @@ main(void) { struct kreq r; const char *const pages[1] = { "index" }; - if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, 1, 0)) + if (khttp_parse(&r, NULL, 0, pages, 1, 0) != KCGI_OK) return 0; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); @@ -94,9 +94,9 @@ main(void) { We'll start with the most significant protection: by constraining our application after the context has been parsed.

-
if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, 1, 0))
+		
if (khttp_parse(&r, NULL, 0, pages, 1, 0) != KCGI_OK)
     return 0;
-if (-1 == pledge("stdio", NULL))
+if (pledge("stdio", NULL) == -1)
     return 0;

@@ -113,22 +113,22 @@ if (-1 == pledge("stdio", NULL)) href="https://man.openbsd.org/fork.2">fork(2) its internal handlers.

-
if (-1 == pledge("stdio proc", NULL))
+		
if (pledge("stdio proc", NULL) == -1)
     return 0;
-if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, 1, 0))
+if (khttp_parse(&r, NULL, 0, pages, 1, 0) != KCGI_OK)
     return 0;
-if (-1 == pledge("stdio", NULL))
+if (pledge("stdio", NULL) == -1)
     return 0;

Or if one is using khttp_template(3) to marshall a response:

-
if (-1 == pledge("stdio rpath proc", NULL))
+		
if (pledge("stdio rpath proc", NULL) == -1)
     return 0;
-if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, 1, 0))
+if (khttp_parse(&r, NULL, 0, pages, 1, 0) != KCGI_OK)
     return 0;
-if (-1 == pledge("stdio rpath", NULL))
+if (pledge("stdio rpath", NULL) == -1)
     return 0;

@@ -149,11 +149,11 @@ main(void) { struct kreq r; const char *const pages[1] = { "index" }; - if (-1 == pledge("stdio proc", NULL)) + if (pledge("stdio proc", NULL) == -1) return 0; - if (KCGI_OK != khttp_parse(&r, NULL, 0, pages, 1, 0)) + if (khttp_parse(&r, NULL, 0, pages, 1, 0) != KCGI_OK) return 0; - if (-1 == pledge("stdio", NULL)) + if (pledge("stdio", NULL) == -1) return 0; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); @@ -189,10 +189,10 @@ main(void) { struct kfcgi *fcgi; enum kcgi_err er; - if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0)) + if (khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0) != KCGI_OK) return 0; for (;;) { - if (KCGI_OK != khttp_fcgi_parse(fcgi, &req)) + if (khttp_fcgi_parse(fcgi, &req) != KCGI_OK) break; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]); @@ -220,14 +220,14 @@ main(void) { struct kfcgi *fcgi; enum kcgi_err er; - if (-1 == pledge("unix sendfd recvfd proc stdio", NULL)) + if (pledge("unix sendfd recvfd proc stdio", NULL) == -1) return 0; - if (KCGI_OK != khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0)) + if (khttp_fcgi_init(&fcgi, NULL, 0, pages, 1, 0) != KCGI_OK) return 0; - if (-1 == pledge("stdio recvfd", NULL)) + if (pledge("stdio recvfd", NULL) == -1) return 0; for (;;) { - if (KCGI_OK != khttp_fcgi_parse(fcgi, &req)) + if (khttp_fcgi_parse(fcgi, &req) != KCGI_OK) break; khttp_head(&r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);