📄 servlet3.c
字号:
/*
* Copyright (c) 2004, Dennis Kuschel.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/**
* @file servlet3.c
* @author Dennis Kuschel
* @brief This servlet is a configurable character generator.
*
* This software is from http://mycpu.mikrocontroller.net.
* Please send questions and bug reports to dennis_k@freenet.de.
*/
#include "../src/sys.h"
#include "../src/httpd.h"
/* function prototypes */
sint_t my_chargen_servlet1(SERVLET_t so);
static void cgPrintButton(SERVLET_t so, char *name, u32_t sc, u32_t ln);
/*
* This servlet generates a character pattern.
* It can be confiugured by pressing some buttons
* that are embedded into the generated html page.
*/
sint_t my_chargen_servlet1(SERVLET_t so)
{
char buf[90];
char *p;
u32_t nsc, psc, sc;
u32_t nln, pln, ln;
sint_t status;
u32_t x, y, c, d;
static const char ctab[] =
"1234567890+-*/=?abcdefghijklmnopqrstuvwxyz(#)ABCDEFGHIJKLMNOPQRSTUVWXYZ_[$]{:}.;";
/* get parameters that are attached to the URI */
sc = 0;
p = hsl_getParameterByName(so, "sc");
if (p != NULL) sscanf(p, "%u", &sc);
if (sc >= 79) sc = 0;
ln = 4;
p = hsl_getParameterByName(so, "ln");
if (p != NULL) sscanf(p, "%u", &ln);
if (ln >= 100000) ln = 100000;
if (ln == 0) ln = 1;
/* Allocate memory for the html page that will be generated
by this servlet. Note: The default memory size is only 1KB. */
status = hsl_setStreamBufSize(so, (ln+5) * 85 + 2048);
if (status == SLERR_FAIL)
{
/* The http demon is not able to provide the requested ammount
of memory. The servlet can not proceed. Note:
When we would not test for SLERR_FAIL and would simply
return status, the user will see a page saying
"500 Internal Server Error" */
hsl_printf(so, "<html><body><br><br>OUT OF MEMORY<br></body></html>\n");
return SLERR_OK;
}
if (status != SLERR_OK)
{
/* Print debug message and exit. Note: The http demon will call
this servlet again as soon as there is enough memory available. */
printf("mem failed for request sc=%u ln=%u, %u bytes\n", sc, ln, (ln+5) * 85 + 2048);
return status;
}
nsc = (sc >= 78) ? 0 : sc + 1;
psc = (sc == 0) ? 78 : sc - 1;
nln = (ln >= 300) ? 0 : ln + 1;
pln = (ln == 1) ? 300 : ln - 1;
/* create html header */
hsl_printf(so, "<html><head><title>My Chargen Servlet 1</title></head><body>\n");
hsl_printf(so, "<a href=\"index.html\">Index</a><br><br>");
/* insert control buttons to the html page (encapsulated within a table) */
hsl_printf(so, "<table><tr><td>\n");
cgPrintButton(so, "inc startchar", nsc, ln);
hsl_printf(so, "</td><td> </td><td>\n");
cgPrintButton(so, "dec startchar", psc, ln);
hsl_printf(so, "</td><td> </td><td>\n");
cgPrintButton(so, "inc linecount", sc, nln);
hsl_printf(so, "</td><td> </td><td>\n");
cgPrintButton(so, "dec linecount", sc, pln);
hsl_printf(so, "</td></tr></table>\n");
/* generate character pattern and insert it into the html page */
hsl_printf(so, "<code><font size=\"1\"><br>\n");
d = sc;
for (y = 0; y < ln; y++)
{
c = d;
if (++d >= 79) d = 0;
for (x = 0; x < 79; x++)
{
buf[x] = ctab[c];
if (++c >= 79) c = 0;
}
buf[x] = 0;
hsl_printf(so, "%s<br>\n", buf);
}
hsl_printf(so, "</font></code>\n");
/* create html footer */
hsl_printf(so, "</body></html>\n");
return SLERR_OK;
}
/* Tool function. Places a button in the html page.
*/
static void cgPrintButton(SERVLET_t so, char *name, u32_t sc, u32_t ln)
{
hsl_printf(so, "<form action=\"%s\" method=\"get\">\n",hsl_getOwnName(so));
hsl_printf(so, "<input type=\"hidden\" name=\"sc\" value=\"%u\">\n", sc);
hsl_printf(so, "<input type=\"hidden\" name=\"ln\" value=\"%u\">\n", ln);
hsl_printf(so, "<input type=\"submit\" value=\"%s\">\n", name);
hsl_printf(so, "</form>\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -