📄 shorthand_cgi.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/shorthand_cgi.cpp 7 2/08/03 6:40a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// shorthand_cgi.cpp: ShortHand CGI main
///////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//##include <process.h>
#include <sys/stat.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include "shorthand.h"
#include "utils.h"
#include "module.h"
#include "datetime.h"
#include "sherr.h"
#include "version.h"
#ifdef WIN32
#include "process.h"
#endif
extern int yydebug;
int main(int argc, char** argv, char** envp)
{
#ifdef _DEBUG
trace_level = 2;
//yydebug = 3;
#endif
TRACE((2, "ShortHand exe [%d] started\n", getpid()));
string s; s.printf("SHORTHAND_VERSION=%s", shorthand_version);
putenv((char*) s.cstr());
char* file_name = NULL;
if (argc > 1)
{
file_name = argv[1];
TRACE((2, "script name is passed through the command line: %s\n", file_name));
}
if (file_name == NULL)
{
string tmp;
file_name = strdup(safe_getenv("PATH_TRANSLATED", tmp));
TRACE((2, "script name is passed through PATH_TRANSLATED: %s\n", file_name));
if (file_name == NULL) {
fprintf(stderr, "invalid script usage: script name must be passed through command line argument or PATH_TRANSLATED\n");
return -1;
}
}
char* short_name;
char* backslash = strrchr(file_name, '\\');
char* slash = strrchr(file_name, '/');
if (slash == NULL && backslash != NULL)
short_name = backslash + 1;
else if (slash != NULL && backslash == NULL)
short_name = slash + 1;
else if (slash != NULL && backslash != NULL)
short_name = slash > backslash ? slash + 1 : backslash + 1;
else
short_name = file_name;
#ifdef SOURCE_VIEW
bool source_only = false;
if (strnicmp(short_name, "src.", 4) == 0)
{
memmove(short_name, short_name+4, strlen(short_name+4)+1);
source_only = true;
}
#endif SOURCE_VIEW
ShhModule l_module(short_name);
FILE* f = NULL;
if (file_name != NULL && *file_name != '\0') f = fopen(file_name, "rb");
if (f == NULL)
{
string content;
content.printf("<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\"><H1>404 Document not found</H1></BODY></HTML><!-- %s -->", file_name);
//fprintf(stderr, "cannot open script file '%s'\n", file_name);
l_module.stream()->http_set_response_code(404, "Not Found");
l_module.stream()->http_write_chunk(content.cstr(), content.length());
l_module.stream()->http_finalize();
return 404;
}
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
char* content = (char*) malloc(size+1);
fread(content, 1, size, f);
content[size] = '\0';
fclose(f);
#ifdef SOURCE_VIEW
if (source_only)
{
l_module.printf("<HTML><HEAD><TITLE>ShortHand Source of %s</TITLE></HEAD>\n", short_name);
l_module.printf("<BODY BGCOLOR=\"#FFFFFF\">\n");
l_module.printf("<PRE>\n");
const char* p = content;
char ch2[2] = {0,0};
while(*p)
{
char ch = *p;
if (ch == '<') l_module.puts("<");
else if (ch == '>') l_module.puts(">");
else if (ch == '\r') {}
else {
ch2[0] = ch;
l_module.puts(ch2);
}
p++;
}
l_module.printf("</PRE>\n");
l_module.printf("</BODY></HTML>\n");
return 0;
}
#endif
#ifdef _DEBUG
trace_level = 20;
#endif
//yydebug = 1;
/*
yyinitscan();
void* yybuffer = yy_scan_string( content );
int status = yyparse( NULL );
yy_delete_buffer( (struct yy_buffer_state*) yybuffer );
TRACE((2, "yyparse(ARGS) returned %d\n", status));
if (status != 0)
{
return status;
}*/
try
{
TRACE((2, "execution started\n"));
l_module.execute_script(content);
TRACE((2, "execution finished\n"));
}
catch(ShhException* ex)
{
TRACE((2, "execution exception %04d: %s\n", ex->code(), ex->message()));
l_module.printf("\n<br><br><b>Error %s:</b> %s\r\n", ex->href(), ex->message());
const char* logfile = getenv("SHH_ERROR_LOG");
if (logfile != NULL)
{
FILE* lf = fopen(logfile, "a+");
if (lf != NULL)
{
datetime now; string s; now.common_print(s);
fprintf(lf, "%s %s\n", s.cstr(), file_name);
fprintf(lf, "Error %04d: %s\n\n", ex->code(), ex->message());
fclose(lf);
}
}
else
{
#ifndef WIN32
// print error to stderr so it gets into server's error log
// when the program is used as CGI module
//
datetime now; string s; now.common_print(s);
fprintf(stderr, "%s %s\n", s.cstr(), file_name);
fprintf(stderr, "Error %04d: %s\n", ex->code(), ex->message());
fclose(stderr);
#endif
}
}
/*
catch(...)
{
printf("Unhandled exception in %s\r\n", short_name);
}*/
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -