Back to index

courier  0.68.2
cgitextarea.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2007 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 /*
00007 */
00008 
00009 #include      "cgi.h"
00010 #include      <stdio.h>
00011 #include      <string.h>
00012 #include      <stdlib.h>
00013 #include      <ctype.h>
00014 
00015 extern void cgi_output_unicode_escapes(const unicode_char *value,
00016                                    const char *escapes,
00017                                    void (*output_func)(const char *,
00018                                                     size_t,
00019                                                     void *),
00020                                    void *output_arg);
00021 
00022 static void do_cgi_textarea(const char *name,
00023                          int rows,
00024                          int cols,
00025                          const unicode_char *value,
00026                          const char *opts,
00027                          const char *wrap,
00028                          void (*output_func)(const char *, size_t,
00029                                           void *),
00030                          void *output_arg)
00031 {
00032        (*output_func)("<textarea name='", 0, output_arg);
00033        (*output_func)(name, 0, output_arg);
00034        (*output_func)("'", 0, output_arg);
00035 
00036        if (strchr(opts, 'r'))
00037               (*output_func)(" readonly='readonly'", 0, output_arg);
00038        if (strchr(opts, 'd'))
00039               (*output_func)(" disabled='disabled'", 0, output_arg);
00040 
00041        (*output_func)("'", 0, output_arg);
00042 
00043        if (rows)
00044        {
00045               char buf[100];
00046 
00047               sprintf(buf, " rows='%d'", rows);
00048 
00049               (*output_func)(buf, 0, output_arg);
00050        }
00051 
00052        if (cols)
00053        {
00054               char buf[100];
00055 
00056               sprintf(buf, " cols='%d'", cols);
00057 
00058               (*output_func)(buf, 0, output_arg);
00059        }
00060 
00061        if (wrap)
00062        {
00063               (*output_func)(" wrap='", 0, output_arg);
00064               (*output_func)(wrap, 0, output_arg);
00065               (*output_func)("'", 0, output_arg);
00066        }
00067 
00068        (*output_func)(">", 0, output_arg);
00069 
00070        cgi_output_unicode_escapes(value, "<>'&", output_func, output_arg);
00071 
00072        (*output_func)("</textarea>", 0, output_arg);
00073 }
00074 
00075 static void cnt_bytes(const char *str, size_t cnt, void *arg)
00076 {
00077        if (!cnt)
00078               cnt=strlen(str);
00079 
00080        *(size_t *)arg += cnt;
00081 }
00082 
00083 static void save_bytes(const char *str, size_t cnt, void *arg)
00084 {
00085        char **p=(char **)arg;
00086 
00087        if (!cnt)
00088               cnt=strlen(str);
00089 
00090        memcpy(*p, str, cnt);
00091 
00092        *p += cnt;
00093 }
00094 
00095 char *cgi_textarea(const char *name,
00096                  int rows,
00097                  int cols,
00098                  const unicode_char *value,
00099                  const char *wrap,
00100                  const char *opts)
00101 {
00102        size_t cnt=1;
00103        char *buf;
00104        char *ptr;
00105 
00106        if (!opts)
00107               opts="";
00108 
00109        do_cgi_textarea(name, rows, cols, value, opts, wrap, cnt_bytes, &cnt);
00110 
00111        buf=malloc(cnt);
00112 
00113        if (!buf)
00114               return NULL;
00115 
00116        ptr=buf;
00117        do_cgi_textarea(name, rows, cols, value, opts, wrap, save_bytes, &ptr);
00118        *ptr=0;
00119        return buf;
00120 }