enable output options for the CGI mode - chess-puzzles - chess puzzle book generator
 (HTM) git clone git://git.codemadness.org/chess-puzzles
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 6554fcb56e12be089dbbf0e700f6777924e8b5b6
 (DIR) parent cd3acc17b7b82e112bd5abbc65324283a7c5bf7f
 (HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
       Date:   Thu,  4 Jan 2024 17:04:22 +0100
       
       enable output options for the CGI mode
       
       Diffstat:
         M fen.1                               |       4 ++--
         M fen.c                               |      74 +++++++++++++++++++++----------
       
       2 files changed, 53 insertions(+), 25 deletions(-)
       ---
 (DIR) diff --git a/fen.1 b/fen.1
       @@ -60,8 +60,8 @@ Use a colour theme for certain output formats, supported are the names: brown
        If this option is set
        .Nm
        will run in "CGI" mode suitable for a web server / HTTP daemon.
       -This accepts the parameters: fen, moves, flip, coords and theme, similar to the
       -command-line flags.
       +This accepts the parameters: fen, moves, output, flip, coords and theme,
       +similar to the command-line flags.
        It will serve a SVG of the chess board and moves.
        .El
        .Sh EXIT STATUS
 (DIR) diff --git a/fen.c b/fen.c
       @@ -15,7 +15,7 @@
        #define SETFGCOLOR(r,g,b)    printf("\x1b[38;2;%d;%d;%dm", r, g, b)
        #define SETBGCOLOR(r,g,b)    printf("\x1b[48;2;%d;%d;%dm", r, g, b)
        
       -enum outputmode { ModeASCII = 0, ModeCGI, ModeFEN, ModePGN, ModeTTY, ModeSVG };
       +enum outputmode { ModeInvalid = 0, ModeASCII, ModeCGI, ModeFEN, ModePGN, ModeTTY, ModeSVG };
        enum outputmode outputmode = ModeSVG;
        
        /* localization of letter for PGN pieces */
       @@ -1316,6 +1316,36 @@ decodeparam(char *buf, size_t bufsiz, const char *s)
                return i;
        }
        
       +enum outputmode
       +outputnametomode(const char *s)
       +{
       +        if (!strcmp(s, "ascii"))
       +                return ModeASCII;
       +        else if (!strcmp(s, "fen"))
       +                return ModeFEN;
       +        else if (!strcmp(s, "pgn"))
       +                return ModePGN;
       +        else if (!strcmp(s, "svg"))
       +                return ModeSVG;
       +        else if (!strcmp(s, "tty"))
       +                return ModeTTY;
       +        else
       +                return ModeInvalid;
       +}
       +
       +void
       +output(struct board *b)
       +{
       +        switch (outputmode) {
       +        case ModeASCII: output_ascii(b); break;
       +        case ModeFEN:   output_fen(b);   break;
       +        case ModePGN:                    break; /* handled in parsemoves() */
       +        case ModeSVG:   output_svg(b);   break;
       +        case ModeTTY:   output_tty(b);   break;
       +        default:        break;
       +        }
       +}
       +
        /* CGI mode */
        int
        cgi_mode(void)
       @@ -1331,6 +1361,14 @@ cgi_mode(void)
                        board.flipboard = *p == '1' ? 1 : 0;
                if ((p = getparam(query, "coords")) && (*p == '0' || *p == '1'))
                        board.showcoords = *p == '1' ? 1 : 0;
       +
       +        if ((p = getparam(query, "output"))) {
       +                if (decodeparam(buf, sizeof(buf), p) == -1)
       +                        goto badrequest;
       +                outputmode = outputnametomode(buf);
       +                if (outputmode == ModeInvalid)
       +                        goto badrequest;
       +        }
                if ((p = getparam(query, "theme"))) {
                        if (decodeparam(buf, sizeof(buf), p) == -1)
                                goto badrequest;
       @@ -1349,8 +1387,13 @@ cgi_mode(void)
                        board_playmoves(&board, buf);
                }
        
       -        fputs("Status: 200 OK\r\nContent-Type: image/svg+xml\r\n\r\n", stdout);
       -        output_svg(&board);
       +        fputs("Status: 200 OK\r\n", stdout);
       +        if (outputmode == ModeSVG)
       +                fputs("Content-Type: image/svg+xml\r\n\r\n", stdout);
       +        else
       +                fputs("Content-Type: text/plain\r\n\r\n", stdout);
       +
       +        output(&board);
        
                return 0;
        
       @@ -1358,7 +1401,7 @@ badrequest:
                fputs("Status: 400 Bad Request\r\n", stdout);
                fputs("Content-Type: text/plain\r\n", stdout);
                fputs("\r\n", stdout);
       -        fputs("Bad request: fen parameter use a valid format or \"startpos\" (default)\n", stdout);
       +        fputs("Bad request: make sure to use valid parameters\n", stdout);
        
                return 1;
        }
       @@ -1404,17 +1447,9 @@ main(int argc, char *argv[])
                                        if (i + 1 >= argc)
                                                usage(argv[0]);
                                        i++;
       -                                if (!strcmp(argv[i], "ascii"))
       -                                        outputmode = ModeASCII;
       -                                else if (!strcmp(argv[i], "fen"))
       -                                        outputmode = ModeFEN;
       -                                else if (!strcmp(argv[i], "pgn"))
       -                                        outputmode = ModePGN;
       -                                else if (!strcmp(argv[i], "svg"))
       -                                        outputmode = ModeSVG;
       -                                else if (!strcmp(argv[i], "tty"))
       -                                        outputmode = ModeTTY;
       -                                else
       +
       +                                outputmode = outputnametomode(argv[i]);
       +                                if (outputmode == ModeInvalid)
                                                usage(argv[0]);
                                        goto next;
                                case 't': /* theme name */
       @@ -1443,14 +1478,7 @@ next:
                board_setup_fen(&board, fen);
                board_playmoves(&board, moves);
        
       -        switch (outputmode) {
       -        case ModeASCII: output_ascii(&board); break;
       -        case ModeFEN:   output_fen(&board);   break;
       -        case ModePGN:                         break; /* handled in parsemoves() */
       -        case ModeSVG:   output_svg(&board);   break;
       -        case ModeTTY:   output_tty(&board);   break;
       -        default:        usage(argv[0]); break;
       -        }
       +        output(&board);
        
                return 0;
        }