]> bicyclesonthemoon.info Git - ott/enhance/blob - bluenh-cgi.c
diff online
[ott/enhance] / bluenh-cgi.c
1 /*
2 bluenh-cgi.c
3 Online interface for npb
4 04.12.2022
5
6 Copyright (C) 2013, 2014, 2022  Balthasar SzczepaƄski
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU Affero General Public License as
10 published by the Free Software Foundation, either version 3 of the
11 License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Affero General Public License for more details.
17
18 You should have received a copy of the GNU Affero General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 Requires cgilib (http://www.infodrom.org/projects/cgilib/)
22 */
23
24 #include <stdio.h>
25 #include <stdint.h>
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <stdint.h>
30 #include <inttypes.h>
31
32 #include <cgi.h>
33
34 #include "online-core.h"
35 #include "nh.h"
36
37 int bluenh (
38         char *inpix, char *outpix,
39         uint_fast8_t a, uint_fast8_t b, uint_fast8_t c,
40         uint_fast8_t d, uint_fast8_t e, uint_fast8_t f
41 );
42
43 int main (int argc, char *argv[])
44 {
45         s_cgi *cgi;
46         char in_tmp[256];
47         char out_tmp[256];
48         char *in_path;
49         char *t;
50         uint_fast8_t a, b, c, d, e, f;
51         
52         int r=0;
53         int r1=0;
54         int r2=0;
55         int r3=0;
56         
57         do {
58                 make_tmp_path(in_tmp, 256, 0, "");
59                 make_tmp_path(out_tmp, 256, 1, ".png");
60                 
61                 cgi=cgiInit();
62                 
63                 r = get_file(cgi,"inpix2", in_tmp, &in_path);
64                 if (r)
65                         r = get_file(cgi,"inpix", in_tmp, &in_path);
66                 if (r)
67                         break;
68                 
69                 t = cgiGetValue(cgi, "a");
70                 if (t != NULL)
71                         sscanf(t,"%"SCNuFAST8, &a);
72                 else
73                         a = 0;
74                 
75                 t = cgiGetValue(cgi, "b");
76                 if (t != NULL)
77                         sscanf(t,"%"SCNuFAST8, &b);
78                 else
79                         b = 0;
80                 
81                 t = cgiGetValue(cgi, "c");
82                 if (t != NULL)
83                         sscanf(t,"%"SCNuFAST8, &c);
84                 else
85                         c = 0;
86                 
87                 t = cgiGetValue(cgi, "d");
88                 if (t != NULL)
89                         sscanf(t,"%"SCNuFAST8, &d);
90                 else
91                         d = 0;
92                 
93                 t = cgiGetValue(cgi, "e");
94                 if (t != NULL)
95                         sscanf(t,"%"SCNuFAST8, &e);
96                 else
97                         e = 0;
98                 
99                 t = cgiGetValue(cgi, "f");
100                 if (t != NULL)
101                         sscanf(t,"%"SCNuFAST8, &f);
102                 else
103                         f = 0;
104                 
105                 r = bluenh(in_path, out_tmp, a, b, c, d, e, f);
106                 if (r)
107                         break;
108                 
109                 r1 = send_file(out_tmp, "image/png", 0);
110         } while (0);
111         if (r)
112         {
113                 r1 = send_data(nh, nh_size, "image/png", 500);
114         }
115         r2 = rm(in_tmp);
116         r3 = rm(out_tmp);
117         if (r)
118                 return r;
119         if (r1)
120                 return r1;
121         if (r2)
122                 return r2;
123         if (r3)
124                 return r3;
125         return 0;
126 }
127
128 int bluenh (
129         char *inpix, char *outpix,
130         uint_fast8_t a, uint_fast8_t b, uint_fast8_t c,
131         uint_fast8_t d, uint_fast8_t e, uint_fast8_t f
132 )
133 {
134         char s_a[4];
135         char s_b[4];
136         char s_c[4];
137         char s_d[4];
138         char s_e[4];
139         char s_f[4];
140         
141         pid_t sub;
142         int r;
143         
144         snprintf(s_a, 4, "%"SCNuFAST8, a);
145         snprintf(s_b, 4, "%"SCNuFAST8, b);
146         snprintf(s_c, 4, "%"SCNuFAST8, c);
147         snprintf(s_d, 4, "%"SCNuFAST8, d);
148         snprintf(s_e, 4, "%"SCNuFAST8, e);
149         snprintf(s_f, 4, "%"SCNuFAST8, f);
150         
151         sub = fork();
152         if (sub == 0)
153         {
154                 r = execl(
155                         BLUENH_PATH, BLUENH_PATH,
156                         inpix, outpix,
157                         s_a, s_b, s_c,
158                         s_d, s_e, s_f,
159                         (char *)0
160                 );
161                 exit(r);
162         }
163         waitpid(sub, &r, 0);
164         return r;
165 }