making the output here a bit more real
[feisty_meow.git] / nucleus / applications / nechung / cgi_nechung.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : CGI nechung                                                       *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1997-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 //! @file cgi_nechung.cpp Spits out a CGI appropriate chunk of text with a fortune in it.
16
17 #include "nechung_oracle.h"
18
19 #include <basis/astring.h>
20 #include <basis/environment.h>
21 #include <basis/guards.h>
22 #include <filesystem/filename.h>
23 #include <loggers/console_logger.h>
24 #include <loggers/critical_events.h>
25 #include <loggers/program_wide_logger.h>
26
27 #include <stdio.h>
28
29 //using namespace application;
30 using namespace basis;
31 using namespace filesystem;
32 using namespace loggers;
33
34 #undef LOG
35 #define LOG(s) program_wide_logger::get().log(s, 0)
36
37 ////HOOPLE_STARTUP_CODE;
38 //hmmm: need this back when things are ready!
39
40 #define DEFAULT_FORTUNE_FILE "fortunes.dat"
41
42 int main(int argc, char *argv[])
43 {
44   SETUP_CONSOLE_LOGGER;
45
46   astring name;
47   astring index;
48   if (argc > 1) {
49     // use the first command line argument.
50     name = argv[1];
51   } else {
52     // if nothing on the command line, then use our defaults.
53     name = environment::get("NECHUNG");
54       // first try the environment variable.
55     if (!name) name = DEFAULT_FORTUNE_FILE;
56       // next, use the hardwired default.
57   }
58
59   if (name.length() < 5) {
60     LOG(astring("nechung:: file name is too short (") + name + ").");
61     return 1;
62   }
63   filename index_file_name(name);
64   astring extension(index_file_name.extension());
65   int end = index_file_name.raw().end();
66 #ifdef DEBUG_NECHUNG
67   LOG(astring("fortune filename is ") + name);
68   LOG(astring("extension is ") + extension);
69 #endif
70   astring tmp = index_file_name;
71   tmp.zap( (end + 1) - extension.length(), end);
72   tmp += "idx";
73   astring base_part = filename(tmp).basename();
74   index_file_name = environment::TMP() + "/" + base_part;
75 #ifdef DEBUG_NECHUNG
76   LOG(astring("index file is ") + index_file_name);
77 #endif
78   index = index_file_name;
79
80   nechung_oracle some_fortunes(name, index);
81   // send the header for html text.
82   printf("<!DOCTYPE html>\n");
83   printf("<html>\n");
84   // send the preliminary gunk.
85   printf("<body text=\"#00ee00\" bgcolor=\"#000000\" link=\"#66ff99\" "
86       "vlink=\"#cc33cc\" alink=\"#ff9900\">\n");
87 //old text color #33ccff
88   printf("<tt style=\"font-weight: bold;\">\n");
89   printf("<font size=\"+1\">\n");
90
91   astring to_show = some_fortunes.pick_random();
92   int line_posn = 0;
93   for (int i = 0; i < to_show.length(); i++) {
94     if (to_show[i] == ' ') {
95       // spaces get translated to one non-breaking space.
96       printf("&nbsp;");
97       line_posn++;
98     } else if (to_show[i] == '\t') {
99       // tabs get translated to positioning at tab stops based on eight.
100       int to_add = 8 - line_posn % 8;
101       for (int j = 0; j < to_add; j++) printf("&nbsp;");
102       line_posn += to_add;
103     } else if (to_show[i] == '\r')
104       continue;
105     else if (to_show[i] == '\n') {
106       printf("<br>%c", to_show[i]);
107       line_posn = 0;
108     } else {
109       printf("%c", to_show[i]);
110       line_posn++;
111     }
112   }
113   printf("\n");
114   printf("</font>\n");
115   printf("</tt>\n");
116   printf("</body>\n");
117   printf("</html>\n");
118   return 0;
119 }
120
121 #ifdef __BUILD_STATIC_APPLICATION__
122   // static dependencies found by buildor_gen_deps.sh:
123   #include <application/application_shell.cpp>
124   #include <basis/astring.cpp>
125   #include <basis/common_outcomes.cpp>
126   #include <basis/environment.cpp>
127   #include <basis/mutex.cpp>
128   #include <basis/utf_conversion.cpp>
129   #include <configuration/application_configuration.cpp>
130   #include <configuration/configurator.cpp>
131   #include <configuration/ini_configurator.cpp>
132   #include <configuration/ini_parser.cpp>
133   #include <configuration/table_configurator.cpp>
134   #include <configuration/variable_tokenizer.cpp>
135   #include <filesystem/byte_filer.cpp>
136   #include <filesystem/directory.cpp>
137   #include <filesystem/filename.cpp>
138   #include <filesystem/file_time.cpp>
139   #include <loggers/combo_logger.cpp>
140   #include <loggers/console_logger.cpp>
141   #include <loggers/critical_events.cpp>
142   #include <loggers/file_logger.cpp>
143   #include <loggers/program_wide_logger.cpp>
144   #include <structures/bit_vector.cpp>
145   #include <structures/checksums.cpp>
146   #include <structures/object_packers.cpp>
147   #include <structures/static_memory_gremlin.cpp>
148   #include <structures/string_hasher.cpp>
149   #include <structures/string_table.cpp>
150   #include <structures/version_record.cpp>
151   #include <textual/byte_formatter.cpp>
152   #include <textual/parser_bits.cpp>
153   #include <textual/string_manipulation.cpp>
154   #include <timely/earth_time.cpp>
155   #include <timely/time_stamp.cpp>
156 #endif // __BUILD_STATIC_APPLICATION__
157