summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/PDCurses/demos/tuidemo.c
blob: 6f4d9f21df430f6296c2121ccb53f1fa9f2cf6b9 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * $Id: tuidemo.c,v 1.22 2008/07/14 12:35:23 wmcbrine Exp $
 *
 * Author : P.J. Kunst <kunst@prl.philips.nl>
 * Date   : 25-02-93
 *
 * Purpose: This program demonstrates the use of the 'curses' library
 *          for the creation of (simple) menu-operated programs.
 *          In the PDCurses version, use is made of colors for the
 *          highlighting of subwindows (title bar, status bar etc).
 *
 * Acknowledgement: some ideas were borrowed from Mark Hessling's
 *                  version of the 'testcurs' program.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include "tui.h"

/* change this if source at other location */

#ifdef XCURSES
# define FNAME  "../demos/tui.c"
#else
# define FNAME  "..\\demos\\tui.c"
#endif

/**************************** strings entry box ***************************/

void address(void)
{
    char *fieldname[6] =
    {
        "Name", "Street", "City", "State", "Country", (char *)0
    };

    char *fieldbuf[5];
    WINDOW *wbody = bodywin();
    int i, field = 50;

    for (i = 0; i < 5; i++)
        fieldbuf[i] = calloc(1, field + 1);

    if (getstrings(fieldname, fieldbuf, field) != KEY_ESC)
    {
        for (i = 0; fieldname[i]; i++)
            wprintw(wbody, "%10s : %s\n",
                fieldname[i], fieldbuf[i]);

        wrefresh(wbody);
    }

    for (i = 0; i < 5; i++)
        free(fieldbuf[i]);
}

/**************************** string entry box ****************************/

char *getfname(char *desc, char *fname, int field)
{
    char *fieldname[2];
    char *fieldbuf[1];

    fieldname[0] = desc;
    fieldname[1] = 0;
    fieldbuf[0] = fname;

    return (getstrings(fieldname, fieldbuf, field) == KEY_ESC) ? NULL : fname;
}

/**************************** a very simple file browser ******************/

void showfile(char *fname)
{
    int i, bh = bodylen();
    FILE *fp;
    char buf[MAXSTRLEN];
    bool ateof = FALSE;

    statusmsg("FileBrowser: Hit key to continue, Q to quit");

    if ((fp = fopen(fname, "r")) != NULL)   /* file available? */
    {
        while (!ateof)
        {
            clsbody();

            for (i = 0; i < bh - 1 && !ateof; i++)
            {
                buf[0] = '\0';
                fgets(buf, MAXSTRLEN, fp);

                if (strlen(buf))
                    bodymsg(buf);
                else
                    ateof = TRUE;
            }

            switch (waitforkey())
            {
            case 'Q':
            case 'q':
            case 0x1b:
                ateof = TRUE;
            }
        }

        fclose(fp);
    }
    else
    {
        sprintf(buf, "ERROR: file '%s' not found", fname);
        errormsg(buf);
    }
}

/***************************** forward declarations ***********************/

void sub0(void), sub1(void), sub2(void), sub3(void);
void func1(void), func2(void);
void subfunc1(void), subfunc2(void);
void subsub(void);

/***************************** menus initialization ***********************/

menu MainMenu[] =
{
    { "Asub", sub0, "Go inside first submenu" },
    { "Bsub", sub1, "Go inside second submenu" },
    { "Csub", sub2, "Go inside third submenu" },
    { "Dsub", sub3, "Go inside fourth submenu" },
    { "", (FUNC)0, "" }   /* always add this as the last item! */
};

menu SubMenu0[] =
{
    { "Exit", DoExit, "Terminate program" },
    { "", (FUNC)0, "" }
};

menu SubMenu1[] =
{
    { "OneBeep", func1, "Sound one beep" },
    { "TwoBeeps", func2, "Sound two beeps" },
    { "", (FUNC)0, "" }
};

menu SubMenu2[] =
{
    { "Browse", subfunc1, "Source file lister" },
    { "Input", subfunc2, "Interactive file lister" },
    { "Address", address, "Get address data" },
    { "", (FUNC)0, "" }
};

menu SubMenu3[] =
{
    { "SubSub", subsub, "Go inside sub-submenu" },
    { "", (FUNC)0, "" }
};

/***************************** main menu functions ************************/

void sub0(void)
{
    domenu(SubMenu0);
}

void sub1(void)
{
    domenu(SubMenu1);
}

void sub2(void)
{
    domenu(SubMenu2);
}

void sub3(void)
{
    domenu(SubMenu3);
}

/***************************** submenu1 functions *************************/

void func1(void)
{
    beep();
    bodymsg("One beep! ");
}

void func2(void)
{
    beep();
    bodymsg("Two beeps! ");
    beep();
}

/***************************** submenu2 functions *************************/

void subfunc1(void)
{
    showfile(FNAME);
}

void subfunc2(void)
{
    char fname[MAXSTRLEN];

    strcpy(fname, FNAME);
    if (getfname ("File to browse:", fname, 50))
        showfile(fname);
}

/***************************** submenu3 functions *************************/

void subsub(void)
{
    domenu(SubMenu2);
}

/***************************** start main menu  ***************************/

int main(int argc, char **argv)
{
    setlocale(LC_ALL, "");

    startmenu(MainMenu, "TUI - 'textual user interface' demonstration program");

    return 0;
}