/*
* $Id: c.html,v 1.1.1.1 1999/09/28 16:43:44 sbooth Exp $
*
* Copyright (C) 1998, 1999 Stephen F. Booth
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "httpd.h"
#include "http_config.h"
#include "ap_config.h"
#include "alloc.h"

#include "markup.h"


/* C++ handler */

extern FILE *cpp_yyin;
extern request_rec *cpp_gyyout;

static int 
cpp_handler(request_rec *r)
{
  if(r->method_number != M_GET) 
    return DECLINED;
  if(r->finfo.st_mode == 0) 
    return NOT_FOUND;
  
  cpp_yyin = ap_pfopen(r->pool, r->filename, "r");
  if(cpp_yyin == NULL) {
    ap_log_reason("file permissions deny server access", r->filename, r);
    return FORBIDDEN;    
  }

  if(r->args != 0)
    r->content_type = "text/plain";
  else
    r->content_type = "text/html";      
  ap_soft_timeout("send", r);
  ap_send_http_header(r);
  
  if(r->header_only) {
    ap_kill_timeout(r);
    ap_pfclose(r->pool, cpp_yyin);
    return OK;
  }

  if(r->args != 0) {
    ap_send_fd(cpp_yyin, r);
  }
  else {
    ap_rputs("<HTML>\n<HEAD><TITLE>", r);
    ap_rputs(r->uri, r);
    ap_rputs("</TITLE></HEAD>\n<BODY>\n", r);
    ap_rputs("<!-- HTML generated by mod_format -->\n", r);
    ap_rputs(START_TEXT, r);
    cpp_gyyout = r;
    cpp_yylex();
    ap_rputs(END_TEXT, r);
    ap_rputs("</BODY></HTML>", r);
  }

  ap_kill_timeout(r);
  ap_pfclose(r->pool, cpp_yyin);
  return OK;
}


/* C handler */
extern FILE *c_yyin;
extern request_rec *c_gyyout;

static int 
c_handler(request_rec *r)
{
  if(r->method_number != M_GET) 
    return DECLINED;
  if(r->finfo.st_mode == 0) 
    return NOT_FOUND;
  
  c_yyin = ap_pfopen(r->pool, r->filename, "r");
  if(c_yyin == NULL) {
    ap_log_reason("file permissions deny server access", r->filename, r);
    return FORBIDDEN;    
  }

  if(r->args != 0)
    r->content_type = "text/plain";
  else
    r->content_type = "text/html";      
  ap_soft_timeout("send", r);
  ap_send_http_header(r);
  
  if(r->header_only) {
    ap_kill_timeout(r);
    ap_pfclose(r->pool, c_yyin);
    return OK;
  }

  if(r->args != 0) {
    ap_send_fd(c_yyin, r);
  }
  else {
    ap_rputs("<HTML>\n<HEAD><TITLE>", r);
    ap_rputs(r->uri, r);
    ap_rputs("</TITLE></HEAD>\n<BODY>\n", r);
    ap_rputs("<!-- HTML generated by mod_format -->\n", r);
    ap_rputs(START_TEXT, r);
    c_gyyout = r;
    c_yylex();
    ap_rputs(END_TEXT, r);
    ap_rputs("</BODY></HTML>", r);
  }
  
  ap_kill_timeout(r);
  ap_pfclose(r->pool, c_yyin);
  return OK;
}

/* Java handler */
extern FILE *java_yyin;
extern request_rec *java_gyyout;

static int 
java_handler(request_rec *r)
{
  if(r->method_number != M_GET) 
    return DECLINED;
  if(r->finfo.st_mode == 0) 
    return NOT_FOUND;
  
  java_yyin = ap_pfopen(r->pool, r->filename, "r");
  if(java_yyin == NULL) {
    ap_log_reason("file permissions deny server access", r->filename, r);
    return FORBIDDEN;    
  }

  if(r->args != 0)
    r->content_type = "text/plain";
  else
    r->content_type = "text/html";      
  ap_soft_timeout("send", r);
  ap_send_http_header(r);
  
  if(r->header_only) {
    ap_kill_timeout(r);
    ap_pfclose(r->pool, java_yyin);
    return OK;
  }

  if(r->args != 0) {
    ap_send_fd(java_yyin, r);
  }
  else {
    ap_rputs("<HTML>\n<HEAD><TITLE>", r);
    ap_rputs(r->uri, r);
    ap_rputs("</TITLE></HEAD>\n<BODY>\n", r);
    ap_rputs("<!-- HTML generated by mod_format -->\n", r);
    ap_rputs(START_TEXT, r);
    java_gyyout = r;
    java_yylex();
    ap_rputs(END_TEXT, r);
    ap_rputs("</BODY></HTML>", r);
  }
  
  ap_kill_timeout(r);
  ap_pfclose(r->pool, java_yyin);
  return OK;
}


/* Dispatch list of content handlers */
static const handler_rec format_handlers[] = { 
  { "c-source", c_handler }, 
  { "cpp-source", cpp_handler }, 
  { "java-source", java_handler }, 
  { NULL, NULL }
};

/* Dispatch list for API hooks */
module MODULE_VAR_EXPORT format_module = {
  STANDARD_MODULE_STUFF, 
  NULL,                  /* module initializer                  */
  NULL,                  /* create per-dir    config structures */
  NULL,                  /* merge  per-dir    config structures */
  NULL,                  /* create per-server config structures */
  NULL,                  /* merge  per-server config structures */
  NULL,                  /* table of config file commands       */
  format_handlers,       /* [#8] MIME-typed-dispatched handlers */
  NULL,                  /* [#1] URI to filename translation    */
  NULL,                  /* [#4] validate user id from request  */
  NULL,                  /* [#5] check if the user is ok _here_ */
  NULL,                  /* [#2] check access by host address   */
  NULL,                  /* [#6] determine MIME type            */
  NULL,                  /* [#7] pre-run fixups                 */
  NULL,                  /* [#9] log a transaction              */
  NULL,                  /* [#3] header parser                  */
  NULL,                  /* child_init                          */
  NULL,                  /* child_exit                          */
  NULL                   /* [#0] post read-request              */
};