[BACK]Return to mygetnstr.c CVS log [TXT][DIR] Up to [contributed] / dgamelaunch-openbsd

Annotation of dgamelaunch-openbsd/mygetnstr.c, Revision 1.1

1.1     ! rubenllo    1: /*
        !             2:  * Copyright (c) 2004, Jilles Tjoelker
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms, with
        !             6:  * or without modification, are permitted provided that the
        !             7:  * following conditions are met:
        !             8:  *
        !             9:  * 1. Redistributions of source code must retain the above
        !            10:  *    copyright notice, this list of conditions and the
        !            11:  *    following disclaimer.
        !            12:  * 2. Redistributions in binary form must reproduce the
        !            13:  *    above copyright notice, this list of conditions and
        !            14:  *    the following disclaimer in the documentation and/or
        !            15:  *    other materials provided with the distribution.
        !            16:  *
        !            17:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
        !            18:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
        !            19:  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
        !            20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
        !            21:  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
        !            22:  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
        !            23:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        !            24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
        !            25:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
        !            26:  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        !            27:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        !            28:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
        !            29:  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
        !            30:  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
        !            31:  * OF SUCH DAMAGE.
        !            32:  */
        !            33:
        !            34: #include <curses.h>
        !            35:
        !            36: /* Assumes noecho(). */
        !            37: /* As with getnstr(), maxlen does not include the '\0' character. */
        !            38:
        !            39: int
        !            40: mygetnstr(char *buf, int maxlen, int doecho)
        !            41: {
        !            42:     int c, i;
        !            43:
        !            44:     i = 0;
        !            45:     for (;;)
        !            46:     {
        !            47:        c = getch();
        !            48:        if (c == 8 || c == 127 || c == KEY_BACKSPACE || c == KEY_DC ||
        !            49:                c == KEY_LEFT)
        !            50:        {
        !            51:            if (i > 0)
        !            52:            {
        !            53:                i--;
        !            54:                if (doecho)
        !            55:                    addstr("\010 \010");
        !            56:            }
        !            57:            else
        !            58:                beep();
        !            59:        }
        !            60:        else if (c == 21 || c == 24 || c == KEY_DL) /* ^U/^X */
        !            61:        {
        !            62:            while (i > 0)
        !            63:            {
        !            64:                i--;
        !            65:                if (doecho)
        !            66:                    addstr("\010 \010");
        !            67:            }
        !            68:        }
        !            69:        else if (c == 23) /* ^W */
        !            70:        {
        !            71:            while (i > 0 && buf[i - 1] == ' ')
        !            72:            {
        !            73:                i--;
        !            74:                if (doecho)
        !            75:                    addstr("\010 \010");
        !            76:            }
        !            77:            while (i > 0 && buf[i - 1] != ' ')
        !            78:            {
        !            79:                i--;
        !            80:                if (doecho)
        !            81:                    addstr("\010 \010");
        !            82:            }
        !            83:        }
        !            84:        else if ((c >= ' ' && c <= '~'))
        !            85:        {
        !            86:            if (i < maxlen)
        !            87:            {
        !            88:                buf[i] = c;
        !            89:                i++;
        !            90:                if (doecho)
        !            91:                    addch(c);
        !            92:            }
        !            93:            else
        !            94:                beep();
        !            95:        }
        !            96:        else if (c == 10 || c == 13 || c == KEY_ENTER || c == KEY_RESIZE)
        !            97:            break;
        !            98:        else if (c == ERR)
        !            99:        {
        !           100:            buf[i] = 0;
        !           101:            return ERR;
        !           102:        }
        !           103:        else
        !           104:            beep();
        !           105:     }
        !           106:     buf[i] = 0;
        !           107:     return OK;
        !           108: }
        !           109:
        !           110: /* vim:ts=8:cin:sw=4
        !           111:  */

CVSweb