/*
* Copyright (c) 2004, 2005, 2008, 2010, 2011 Tama Communications Corporation
*
* This file is part of GNU GLOBAL.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include
#ifdef STDC_HEADERS
#include
#endif
#ifdef HAVE_STRING_H
#include
#else
#include
#endif
#include
#include
#ifdef HAVE_UNISTD_H
#include
#endif
#ifdef HAVE_FCNTL_H
#include
#else
#include
#endif
#include "global.h"
#include "anchor.h"
#include "common.h"
#include "htags.h"
#include "path2url.h"
/*
* Tag definitions
*/
const char *html_begin = "";
const char *html_end = "";
const char *html_head_begin = "";
const char *html_head_end = "";
const char *html_title_begin = "";
const char *html_title_end = "";
const char *body_begin = "";
const char *body_end = "";
const char *title_begin = "
";
static const char *fix_attr_value(const char *);
/*
* print string and new line.
*
* This function is a replacement of fprintf(op, "%s\n", s) in htags.
*/
int
fputs_nl(const char *s, FILE *op)
{
fputs(s, op);
putc('\n', op);
return 0;
}
/*
* These methods are used to tell lex() the current path information.
*/
static char current_path[MAXPATHLEN];
static char current_dir[MAXPATHLEN];
static char current_file[MAXPATHLEN];
/**
* save path infomation
*/
void
save_current_path(const char *path)
{
char *startp, *p;
strlimcpy(current_path, path, sizeof(current_path));
/* Extract directory name and file name from path */
strlimcpy(current_dir, path, sizeof(current_path));
startp = current_dir;
p = startp + strlen(current_dir);
while (p > startp) {
if (*--p == '/') {
*p = '\0';
strlimcpy(current_file, p + 1, sizeof(current_file));
return;
}
}
/* It seems that the path doesn't include '/' */
strlimcpy(current_file, path, sizeof(current_file));
}
char *
get_current_dir(void)
{
return current_dir;
}
char *
get_current_file(void)
{
return current_file;
}
/**
* Generate upper directory.
*
* Just returns the parent path of dir. (Adds "../" to it).
*/
const char *
upperdir(const char *dir)
{
STATIC_STRBUF(sb);
strbuf_clear(sb);
strbuf_sprintf(sb, "../%s", dir);
return strbuf_value(sb);
}
/*
* Load text from file with replacing @PARENT_DIR@ macro.
* Macro @PARENT_DIR@ is replaced with the parent directory
* of the "HTML" directory.
*/
static const char *
sed(FILE *ip, int place)
{
STATIC_STRBUF(sb);
const char *parent_dir = (place == SUBDIR) ? "../.." : "..";
int c, start_position = -1;
strbuf_clear(sb);
while ((c = fgetc(ip)) != EOF) {
strbuf_putc(sb, c);
if (c == '@') {
int curpos = strbuf_getlen(sb);
if (start_position == -1) {
start_position = curpos - 1;
} else {
if (!strncmp("@PARENT_DIR@",
strbuf_value(sb) + start_position,
curpos - start_position))
{
strbuf_setlen(sb, start_position);
strbuf_puts(sb, parent_dir);
start_position = -1;
} else {
start_position = curpos - 1;
}
}
} else if (!isalpha(c) && c != '_') {
if (start_position != -1)
start_position = -1;
}
}
return strbuf_value(sb);
}
/**
* Generate custom header.
*/
const char *
gen_insert_header(int place)
{
static FILE *ip;
if (ip != NULL) {
rewind(ip);
} else {
ip = fopen(insert_header, "r");
if (ip == NULL)
die("cannot open include header file '%s'.", insert_header);
}
return sed(ip, place);
}
/**
* Generate custom footer.
*/
const char *
gen_insert_footer(int place)
{
static FILE *ip;
if (ip != NULL) {
rewind(ip);
} else {
ip = fopen(insert_footer, "r");
if (ip == NULL)
die("cannot open include footer file '%s'.", insert_footer);
}
return sed(ip, place);
}
/**
* Generate beginning of generic page
*
* @param[in] title title of this page
* @param[in] place SUBDIR: this page is in sub directory,
* TOPDIR: this page is in the top directory
* @param[in] use_frameset
* use frameset document type or not
* @param[in] header_item
* item which should be inserted into the header
*/
static const char *
gen_page_generic_begin(const char *title, int place, int use_frameset, const char *header_item)
{
STATIC_STRBUF(sb);
const char *dir = NULL;
switch (place) {
case TOPDIR:
dir = "";
break;
case SUBDIR:
dir = "../";
break;
case CGIDIR:
dir = "$basedir/"; /* decided by the CGI script */
break;
}
strbuf_clear(sb);
if (enable_xhtml) {
/*
* If the --frame option are specified then we take
* 'XHTML 1.0 Frameset' for index.html
* and 'XHTML 1.0 Transitional' for other files.
*/
if (use_frameset)
strbuf_puts_nl(sb, "");
else
strbuf_puts_nl(sb, "");
}
strbuf_puts_nl(sb, html_begin);
strbuf_puts_nl(sb, html_head_begin);
strbuf_puts(sb, html_title_begin);
strbuf_puts(sb, title);
strbuf_puts_nl(sb, html_title_end);
strbuf_sprintf(sb, "\n", empty_element);
strbuf_sprintf(sb, "\n", get_version(), empty_element);
if (enable_xhtml) {
strbuf_sprintf(sb, "\n", empty_element);
strbuf_sprintf(sb, "\n", dir, empty_element);
}
if (header_item)
strbuf_puts(sb, header_item); /* internal use */
if (html_header)
strbuf_puts(sb, html_header); /* --html-header=file */
strbuf_puts(sb, html_head_end);
return strbuf_value(sb);
}
/**
* Generate beginning of normal page
*
* @param[in] title title of this page
* @param[in] place SUBDIR: this page is in sub directory,
* TOPDIR: this page is in the top directory
*/
const char *
gen_page_begin(const char *title, int place)
{
return gen_page_generic_begin(title, place, 0, NULL);
}
/**
* beginning of normal page for index page
*
* @param[in] title title of this page
* @param[in] header_item an item which should be inserted into the header
*/
const char *
gen_page_index_begin(const char *title, const char *header_item)
{
return gen_page_generic_begin(title, TOPDIR, 0, header_item);
}
/*
* Generate beginning of frameset page (