/* Generate a table with some Unicode characters, in HTML Copyright 2003, R.Harmsen Added Google Adsense advertisement, May 2009 (disabled in 2012) February 25, 2020: Added URL invocation parms start and range. Example: https://rudhar.com/cgi-bin/shunicod.cgi?start=1f300&range=4096 10 May 2021: Removed last vestiges of Adsense code. Was already disabled since 2012. 5 February 2023: Better CSS mechanism. */ #include #include #include #include #include #include "../cgitools/cgitools.h" #include "../utftools/utftools.h" static void showform (char *script, long x, long t, int UtfOut); static void htmlhead (int UtfOut); static void htmlfoot (void); static void getparms (int len, long *px, long *pt, int *pUtfOut); static void generate (long x, long t, int UtfOut); static void get_invocation_parms (long *px, long *tx); int main (void) { long x = 0x0100; long t = 256; int len; int UtfOut = 1; char *scriptname = "../cgi-bin/shunicod.cgi"; get_invocation_parms(&x, &t); if ((len = userinput()) != 0) getparms(len, &x, &t, &UtfOut); /* Limit resources */ if (t > 8 * 2048L) t = 8 * 2048L; if (x > 0x10ffff) x = 0x10ffff - t; htmlhead(UtfOut); showform(scriptname, x, t, UtfOut); generate(x, t, UtfOut); showform(scriptname, x, t, UtfOut); htmlfoot(); return 0; } static void showform (char *script, long x, long t, int UtfOut) { printf( "
\n" "

Enter the Unicode scalar value to start with (in hex): \n" ",\n" "
and the number of codes to be generated (decimal): " ".\n" "
" "" " Output as ISO-8859-1, entities above hex ff\n" "
" "" " Output as UTF-8\n" "

\n" "

\n" "

\n" "
\n\n", script, x, t, x, t, !UtfOut ? "Checked" : "", UtfOut ? "Checked" : ""); } static void htmlhead (int UtfOut) { printf( "Content-type: text/html\n\n" "" "\n" "\n" "\n" "\n" "Test tool: display Unicode characters\n" "\n" "\n" "\n" "\n", UtfOut ? "UTF-8" : "iso-8859-1"); printf("

\n"); printf( "

To Unicode links page
\n" "Return to previous page

\n" ); printf("

Test tool: display Unicode characters

\n"); fflush(stdout); } static void htmlfoot (void) { printf("

To Unicode links page
\n"); printf("Return to previous page

\n"); printf("

\n"); printf("\n"); fflush(stdout); } static void getparms (int len, long *px, long *pt, int *pUtfOut) { char *buf = calloc(len, 1); char *eq1, *eq2, *eq3; /* Defaults */ *px = 0x0100; *pt = 256; if (!buf || fread(buf, len, 1, stdin) != 1) { free(buf); return; } eq1 = strchr(buf, '='); if (!eq1) { free(buf); return; } eq2 = strchr(eq1 + 1, '='); if (!eq2) { free(buf); return; } eq3 = strchr(eq2 + 1, '='); if (!eq3) { free(buf); return; } *px = strtol(eq1 + 1, NULL, 16); *pt = atol(eq2 + 1); eq3++; if (strncmp(eq3, "UTF", strlen("UTF")) == 0) *pUtfOut = 1; else *pUtfOut = 0; free(buf); } static void generate (long x, long t, int UtfOut) { long blk, bmx, row, col, l; x = x / 8 * 8; t = t / 128 * 128; bmx = t / 8 / 16; if (bmx <= 0) bmx = 1; for (blk = 0; blk < bmx; blk++) { printf("\n"); for (row = 0; row < 16; row++) { printf("\n"); for (col = 0; col < 8; col++) { l = blk * 128 + col * 16 + row + x; printf("\n"); } printf("\n"); } printf("
%04lx
%5ld

", l, l); if (UtfOut) printf("%s", scalar2utf8(l)); else { if (l > 0xff) printf("&#x%04lx;", l); else printf("%c", (int)(l & 0xff)); } printf("
\n"); } } static void get_invocation_parms (long *px, long *pt) { char *query; char **Vars = NULL, **V; query = getenv("QUERY_STRING"); if (query) { Vars = CgiConvVars(query, NULL); for (V = Vars; V[0]; V += 2) { if (strcmp(V[0], "start") == 0 && strlen(V[1])) { *px = strtol(V[1], (char **)NULL, 16); } else if (strcmp(V[0], "range") == 0 && strlen(V[1])) { *pt = atol(V[1]); } } } }