Merge branch 'release-2.140.101'
[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("content-type: text/html\n\n");
83   // send the preliminary gunk.
84   printf("<body>\n");
85 //text=\"#00ee00\" bgcolor=\"#000000\" link=\"#66ff99\" "
86 //      "vlink=\"#cc33cc\" alink=\"#ff9900\">\n");
87   printf("<tt style=\"font-weight: bold;\">\n");
88   printf("<font size=\"+1\">\n");
89
90   astring to_show = some_fortunes.pick_random();
91   int line_posn = 0;
92   for (int i = 0; i < to_show.length(); i++) {
93     if (to_show[i] == ' ') {
94       // spaces get translated to one non-breaking space.
95       printf("&nbsp;");
96       line_posn++;
97     } else if (to_show[i] == '\t') {
98       // tabs get translated to positioning at tab stops based on eight.
99       int to_add = 8 - line_posn % 8;
100       for (int j = 0; j < to_add; j++) printf("&nbsp;");
101       line_posn += to_add;
102     } else if (to_show[i] == '\r')
103       continue;
104     else if (to_show[i] == '\n') {
105       printf("<br>%c", to_show[i]);
106       line_posn = 0;
107     } else {
108       printf("%c", to_show[i]);
109       line_posn++;
110     }
111   }
112   printf("\n");
113   printf("</font>\n");
114   printf("</tt>\n");
115   printf("</body>\n");
116   printf("</html>\n");
117   return 0;
118 }
119
120 #ifdef __BUILD_STATIC_APPLICATION__
121   // static dependencies found by buildor_gen_deps.sh:
122   #include <application/application_shell.cpp>
123   #include <basis/astring.cpp>
124   #include <basis/common_outcomes.cpp>
125   #include <basis/environment.cpp>
126   #include <basis/mutex.cpp>
127   #include <basis/utf_conversion.cpp>
128   #include <configuration/application_configuration.cpp>
129   #include <configuration/configurator.cpp>
130   #include <configuration/ini_configurator.cpp>
131   #include <configuration/ini_parser.cpp>
132   #include <configuration/table_configurator.cpp>
133   #include <configuration/variable_tokenizer.cpp>
134   #include <filesystem/byte_filer.cpp>
135   #include <filesystem/directory.cpp>
136   #include <filesystem/filename.cpp>
137   #include <filesystem/file_time.cpp>
138   #include <loggers/combo_logger.cpp>
139   #include <loggers/console_logger.cpp>
140   #include <loggers/critical_events.cpp>
141   #include <loggers/file_logger.cpp>
142   #include <loggers/program_wide_logger.cpp>
143   #include <structures/bit_vector.cpp>
144   #include <structures/checksums.cpp>
145   #include <structures/object_packers.cpp>
146   #include <structures/static_memory_gremlin.cpp>
147   #include <structures/string_hasher.cpp>
148   #include <structures/string_table.cpp>
149   #include <structures/version_record.cpp>
150   #include <textual/byte_formatter.cpp>
151   #include <textual/parser_bits.cpp>
152   #include <textual/string_manipulation.cpp>
153   #include <timely/earth_time.cpp>
154   #include <timely/time_stamp.cpp>
155 #endif // __BUILD_STATIC_APPLICATION__
156