summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/ng-input.cc
blob: 43815aafb29e8a8f851cae2f2091941a3d000c27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "AppHdr.h"

#include <wctype.h>
#include "ng-input.h"

#include "cio.h"
#include "end.h"
#include "files.h"
#include "format.h"
#include "libutil.h"
#include "options.h"
#include "strings.h"
#include "unicode.h"
#include "version.h"

extern string init_file_error; // defined in main.cc

// Eventually, this should be something more grand. {dlb}
void opening_screen()
{
    string msg =
    "<yellow>Hello, welcome to " CRAWL " " + string(Version::Long) + "!</yellow>\n"
    "<brown>(c) Copyright 1997-2002 Linley Henzell, "
    "2002-2014 Crawl DevTeam\n"
    "Read the instructions for legal details."
    "</brown> " ;

    const bool init_found = init_file_error.empty();

    if (!init_found)
        msg += "<lightred>(No init file ";
    else
        msg += "<lightgrey>(Read options from ";

    if (init_found)
    {
#ifdef DGAMELAUNCH
        // For dgl installs, show only the last segment of the .crawlrc
        // file name so that we don't leak details of the directory
        // structure to (untrusted) users.
        msg += get_base_filename(Options.filename);
#else
        msg += Options.filename;
#endif
        msg += ".)";
    }
    else
    {
        msg += init_file_error;
        msg += ", using defaults.)";
    }

    msg += "\n";

    formatted_string::parse_string(msg).display();
    textcolor(LIGHTGREY);
}

static void _show_name_prompt(int where)
{
    cgotoxy(1, where);
    textcolor(CYAN);

    cprintf("\nWhat is your name today? ");

    textcolor(LIGHTGREY);
}

bool is_good_name(const string& name, bool blankOK, bool verbose)
{
    // verification begins here {dlb}:
    if (name.empty())
    {
        if (blankOK)
            return true;

        if (verbose)
            cprintf("\nThat's a silly name!\n");
        return false;
    }

    return validate_player_name(name, verbose);
}

static bool _read_player_name(string &name)
{
    const int name_x = wherex(), name_y = wherey();
    char buf[kNameLen + 1]; // FIXME: make line_reader handle widths
    // XXX: Prompt displays garbage otherwise, but don't really know why.
    //      Other places don't do this. --rob
    buf[0] = '\0';
    line_reader reader(buf, sizeof(buf));

    while (true)
    {
        cgotoxy(name_x, name_y);
        if (name_x <= 80)
            cprintf("%-*s", 80 - name_x + 1, "");

        cgotoxy(name_x, name_y);
        int ret = reader.read_line(false);
        if (!ret)
        {
            name = buf;
            return true;
        }

        if (key_is_escape(ret))
            return false;

        // Go back and prompt the user.
    }
}

// Reads a valid name from the player, writing it to ng.name.
void enter_player_name(newgame_def *ng)
{
    int prompt_start = wherey();

    do
    {
        // Prompt for a new name if current one unsatisfactory {dlb}:
        _show_name_prompt(prompt_start);

        // If the player wants out, we bail out.
        if (!_read_player_name(ng->name))
            end(0);
        trim_string(ng->name);
    }
    while (!is_good_name(ng->name, false, true));
}

bool validate_player_name(const string &name, bool verbose)
{
#if defined(TARGET_OS_WINDOWS)
    // Quick check for CON -- blows up real good under DOS/Windows.
    if (strcasecmp(name.c_str(), "con") == 0
        || strcasecmp(name.c_str(), "nul") == 0
        || strcasecmp(name.c_str(), "prn") == 0
        || strnicmp(name.c_str(), "LPT", 3) == 0)
    {
        if (verbose)
            cprintf("\nSorry, that name gives your OS a headache.\n");
        return false;
    }
#endif

    if (strwidth(name) > kNameLen)
    {
        if (verbose)
            cprintf("\nThat name is too long.\n");
        return false;
    }

    ucs_t c;
    for (const char *str = name.c_str(); int l = utf8towc(&c, str); str += l)
    {
        // The technical reasons are gone, but enforcing some sanity doesn't
        // hurt.
        if (!iswalnum(c) && c != '-' && c != '.' && c != '_' && c != ' ')
        {
            if (verbose)
            {
                cprintf("\n"
                        "Alpha-numerics, spaces, dashes, periods "
                        "and underscores only, please."
                        "\n");
            }
            return false;
        }
    }

    return true;
}