d14e1d55db9c4bbe3d879f0b152703d9ada482fd
[chise/kage.git] / kagecgi / kagecgi.c
1 //kagecgi.c
2 //
3
4 #include "kage.h"
5 #include "kagecgi.h"
6 #include "sysdep.h"
7
8 int main(int argc, char *argv[]){
9   KGString *tmp1, *tmp2, *test1, *test2, *filename;
10   FILE *err;
11   char errbuf[errorFileSize];
12   char *pos, *cur;
13   int dummy;
14   int type;
15   
16   dummy = initDB();
17   
18   //set default
19   kShotai = kMincho;
20   //kShotai = kGothic;
21   kSize = 200;
22   kType = 0; //png
23   kInput = 0; //ids or direct
24   kResultText = kg_string_new("");
25   kMode = 0;
26   
27   //confirm request
28   type = 0;
29   //GET request
30   if(type == 0){
31     tmp1 = kg_string_new((kgchar *)getenv("QUERY_STRING"));
32     if(tmp1->len != 0) type = 2;
33   }
34   //argv(detect after GET request)
35   if(type == 0){
36     tmp1 = kg_string_new((kgchar *)argv[1]);
37     if(tmp1->len != 0) type = 1;
38   }
39   //redirect
40   if(type == 0){
41     tmp1 = kg_string_new((kgchar *)getenv("REDIRECT_URL"));
42     if(tmp1->len != 0) type = 3;
43   }
44   //error
45   if(type == 0){
46     fprintf(stderr, "Request Error.\n");
47     return 0;
48   }
49   pos = tmp1->str;
50   
51   //separate token
52   if(type == 1 || type == 2){ //argv or GET request
53     while(1){
54       cur = strchr(pos, '&');
55       tmp2 = kg_string_new(pos);
56       if(cur != NULL) kg_string_set_size(tmp2, cur - pos);
57       //got request string
58       if(strncmp(tmp2->str, "shotai=mincho", 13) == 0) kShotai = kMincho;
59       else if(strncmp(tmp2->str, "shotai=gothic", 13) == 0) kShotai = kGothic;
60       else if(strncmp(tmp2->str, "shotai=skeleton", 15) == 0) kShotai = kGothic;
61       else if(strncmp(tmp2->str, "type=png", 8) == 0) kType = 0;
62       else if(strncmp(tmp2->str, "type=svg", 8) == 0) kType = 1;
63       else if(strncmp(tmp2->str, "type=eps", 8) == 0) kType = 2;
64       else if(strncmp(tmp2->str, "type=raw", 8) == 0) kType = 3;
65       else if(strncmp(tmp2->str, "input=ids", 9) == 0) kInput = 0;
66       else if(strncmp(tmp2->str, "input=directwithadjust", 22) == 0) kInput = 2;
67       else if(strncmp(tmp2->str, "input=direct", 12) == 0) kInput = 1;
68       else if(strncmp(tmp2->str, "size=24", 7) == 0) kSize = 24;
69       else if(strncmp(tmp2->str, "size=200", 8) == 0) kSize = 200;
70       else test1 = kg_string_new(tmp2->str);
71       if(cur == NULL) break;
72       pos = cur + 1;
73     }
74   }
75   else{ // redirected request
76     kInput = 0;
77     while(1){
78       cur = strchr(pos, '/');
79       tmp2 = kg_string_new(pos);
80       if(cur != NULL) kg_string_set_size(tmp2, cur - pos);
81       //got request string
82       if(strncmp(tmp2->str, "mincho", 6) == 0) kShotai = kMincho;
83       else if(strncmp(tmp2->str, "gothic", 6) == 0) kShotai = kGothic;
84       else if(strncmp(tmp2->str, "skeleton", 8) == 0) kShotai = kGothic;
85       else if(strncmp(tmp2->str, "v0.4", 4) == 0);
86       else test1 = kg_string_new(tmp2->str);
87       if(cur == NULL) break;
88       pos = cur + 1;
89     }
90     if(strncmp(test1->str + test1->len - 4, ".png", 4) == 0) kType = 0;
91     if(strncmp(test1->str + test1->len - 4, ".svg", 4) == 0) kType = 1;
92     if(strncmp(test1->str + test1->len - 4, ".eps", 4) == 0) kType = 2;
93     if(strncmp(test1->str + test1->len - 4, ".raw", 4) == 0) kType = 3;
94     kg_string_set_size(test1, test1->len - 4);
95   }
96   
97   //clear result buffer
98   test2 = kg_string_new("");
99   if(kType == 1){ //svg
100     kg_string_append(kResultText, "<?xml version=\"1.0\"?>\n");
101     kg_string_append(kResultText, "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.0//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\">\n");
102     kg_string_append(kResultText, "<svg width=\"1024\" height=\"1024\" viewBox=\"0 0 1024 1024\">");
103     kg_string_append(kResultText, "<g style=\"fill: black; stroke: black\">");
104   }
105   else if(kType == 2){ //eps
106     kg_string_append(kResultText, "%!PS-Adobe-3.0 EPSF-3.0\n");
107     kg_string_append(kResultText, "%%BoundingBox: 0 -208 1024 816\n");
108     kg_string_append(kResultText, "%%Pages: 0\n");
109     kg_string_append(kResultText, "%%Title: ");
110     kg_string_append(kResultText, test1->str);
111     kg_string_append(kResultText, "\n");
112     kg_string_append(kResultText, "%%Creator: KAGE System\n");
113     kg_string_append(kResultText, "%%CreationDate: 00:00 1-1-2004\n");
114     kg_string_append(kResultText, "%%EndComments\n");
115     kg_string_append(kResultText, "%%EndProlog\n");
116     kg_string_append(kResultText, "%%Page \"");
117     kg_string_append(kResultText, test1->str);
118     kg_string_append(kResultText, "\" 1\n");
119     kg_string_append(kResultText, "newpath\n");
120   }
121   kageCanvas = initPng(canvasWidth, canvasHeight);
122   if(kInput == 0) generateGlyph(test1, test2);
123   else{
124     convert99(test1, test2);
125     //    kg_string_append(test2, test1->str);
126   }
127   
128   if(test2->len != 0) test2 = finalAdjustment(test2);
129   
130   if(kType == 0){ //png(image)
131     if(test2->len != 0){
132       if(kInput != 1){ //0 and 2
133         test2 = CalcSizes(test2, 1);
134       }
135       DrawBox();
136       drawGlyph(test2, DRAW_GLYPH_MODE_NORMAL);
137       //output to file
138       filename = kg_string_new(pngFilePath);
139       if(kShotai == kMincho) kg_string_append(filename, "mincho/");
140       else if(kShotai == kGothic) kg_string_append(filename, "gothic/");//skeleton??
141       kg_string_append(filename, test1->str);
142       kg_string_append(filename, ".png");
143       
144       //skip for adjustment mode
145       //fp = fopen(filename->str, "w");
146       //writePng(pngWidth, pngHeight, kageCanvas, fp);
147       //fclose(fp);
148       //output to stdout
149       if(type != 1) fprintf(stdout, "Content-type: image/png\n\n");
150       writePng(pngWidth, pngHeight, kageCanvas, stdout);
151       //done
152       closePng(pngWidth, pngHeight, kageCanvas);
153     }
154     else{
155       err = fopen(errorFileName, "r");
156       fread(errbuf, sizeof(char), errorFileSize, err);
157       //printf("An error occurred.\r\n");
158       if(type != 1) fprintf(stdout, "Content-type: image/png\n\n");
159       fwrite(errbuf, sizeof(char), errorFileSize, stdout);
160       fclose(err);
161     }
162   }
163   else if(kType == 1){ //svg(vector graphics)
164     if(test2->len != 0){
165       test2 = CalcSizes(test2, 1);
166       kMode = 1;
167       drawGlyph(test2, DRAW_GLYPH_MODE_NORMAL);
168       kg_string_append(kResultText, "</g></svg>\n");
169       if(type != 1) fprintf(stdout, "Content-type: image/svg-xml\n\n");
170       fprintf(stdout, "%s", kResultText->str);
171     }
172     else{
173       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
174       fprintf(stdout, "An error occurred.");
175     }
176   }
177   else if(kType == 2){ //eps(vector graphics)
178     if(test2->len != 0){
179       test2 = CalcSizes(test2, 1);
180       kMode = 2;
181       drawGlyph(test2, DRAW_GLYPH_MODE_NORMAL);
182       kg_string_append(kResultText, "fill\n");
183       kg_string_append(kResultText, "%%EOF\n");
184       if(type != 1) fprintf(stdout, "Content-type: application/postscript\n\n");
185       fprintf(stdout, "%s", kResultText->str);
186     }
187     else{
188       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
189       fprintf(stdout, "An error occurred.");
190     }
191   }
192   else{ //raw(text)
193     
194     if(test2->len != 0){
195       test2 = CalcSizes(test2, 1);
196       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
197       fprintf(stdout, "result=%s", test2->str);
198     }
199     else{
200       if(type != 1) fprintf(stdout, "Content-type: text/plain\n\n");
201       fprintf(stdout, "result=nodata");
202     }
203   }
204   dummy = closeDB();
205   
206   return 0;
207 }
208