summaryrefslogtreecommitdiff
path: root/sys/win/msvc/winfun.c
blob: f1c4b9cc4765d27e8705948b7abd6e794b1b4e0a (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* winfun.c - windows user interface functions for xlisp */
/* Written by Brian Kendig. */
/*Windows Console and DirectSound code added by Morgan Green and Chris Tchou*/

//#include <Quickdraw.h>
//#include <Windows.h>
//#include <Memory.h>
#include <windows.h>
#include <switches.h>
#include "xlisp.h"
#include "sound.h"

/* Added by Ning Hu		May.2001 
xsetdir - set current directory of the process */
LVAL xsetdir() {
    TCHAR ssCurDir[MAX_PATH], szCurDir[MAX_PATH];
    int verbose = TRUE;

    strcpy(ssCurDir, getstring(xlgastring()));
    if (moreargs()) {
        verbose = (xlgetarg() != NIL);
    }
    xllastarg();
    if (SetCurrentDirectory(ssCurDir)) {
        if (GetCurrentDirectory(
            sizeof(szCurDir)/sizeof(TCHAR), szCurDir)) {
            return cvstring(szCurDir);
        /* create the result string
            stdputstr("Current Directory: ");
            stdputstr(szCurDir);
            stdputstr("\n"); */
        }	
    }
    if (verbose) stdputstr("Directory Setting Error\n");

    /* return nil on error*/
    return NIL;
}

/* xget_temp_path -- get a path to create temp files */
LVAL xget_temp_path()
{
    char *p;
    char szDir[MAX_PATH];
    char szDirLC[MAX_PATH];
    int rslt = GetTempPath(MAX_PATH, szDir);
    if (rslt > MAX_PATH || rslt <= 0) {
        return cvstring("");
    } else {
        /* Vista apparently treats c:\windows with
         * special semantics, so just don't allow
         * GetTempPath to put us in c:\windows...
         */
        strcpy(szDirLC, szDir); /* convert to lower case */
        for (p = szDirLC; *p; p++) { 
            *p = tolower(*p);
        }
        if (strstr(szDirLC, "c:\\windows")) {
            /* c:\windows is bad. */
            return cvstring("");
        }
        return cvstring(szDir);
    }
}

//Updated End


/* osbgetc - get a character from a binary file ======  added for console*/
/* int osbgetc(fp) FILE *fp; {return (getc(fp));} */
int osbgetc(FILE *fp) 
{ 
  int c; c = (getc(fp));
/*  if (dbgflg) printf("osbgetc: got %d from FILE %x\n", c, fp); */
  return c;
}



LVAL xsystem (V) { return NIL; }
LVAL xgetkey (V) { return NIL; }


void ossymbols() 
{
    HWND mywin;
#ifdef WIN32_SNAZZY_CONSOLE
    HANDLE myhandle;
    COORD winsize, origin;
    WORD textattrib;
    DWORD n;
#endif
    mywin = GetForegroundWindow();
    SetConsoleTitle("Nyquist");

#ifdef WIN32_SNAZZY_CONSOLE     // -eub
    myhandle = GetStdHandle(STD_OUTPUT_HANDLE);
    origin.X = 0;
    origin.Y = 0;
    winsize.X = 100;
    winsize.Y = 40;
    textattrib = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;

    FillConsoleOutputAttribute(myhandle, textattrib, winsize.X * winsize.Y, origin, &n);
    SetConsoleScreenBufferSize(myhandle, winsize);
    FillConsoleOutputAttribute(myhandle, textattrib, winsize.X * winsize.Y, origin, &n);
    SetConsoleTextAttribute(myhandle, textattrib); 
#endif

    setvbuf(stdout, NULL, _IONBF, 0);   // makes it work under NT emacs 20.3   -eub

}


LVAL xsetupconsole()
{
    HWND mywin;
    HANDLE myhandle;
    COORD winsize, origin;
    WORD textattrib;
    DWORD n;
    mywin = GetForegroundWindow();
    SetConsoleTitle("Nyquist");

    myhandle = GetStdHandle(STD_OUTPUT_HANDLE);
    origin.X = 0;
    origin.Y = 0;
    winsize.X = 100;
    winsize.Y = 40;
    textattrib = BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;

    FillConsoleOutputAttribute(myhandle, textattrib, winsize.X * winsize.Y, origin, &n);
    SetConsoleScreenBufferSize(myhandle, winsize);
    FillConsoleOutputAttribute(myhandle, textattrib, winsize.X * winsize.Y, origin, &n);
    SetConsoleTextAttribute(myhandle, textattrib);
    return NIL;
}

void get_xlisp_path(char *p, long p_max)
{
    HKEY hkey;
    DWORD dwType;
    LVAL lval;
    extern LVAL s_search_path;

    *p = 0; /* for simplicity, we assume if !*p that path was not found */
    /* therefore, no path is equivalent to an empty string path */

    /* first, look for path in global variable *SEARCH-PATH* */
    lval = getvalue(s_search_path);
    if (lval && stringp(lval)) {
        strncpy(p, getstring(lval), p_max);
        p[p_max - 1] = 0; /* make sure string is terminated, even if truncated */
    }
    if (*p) return; /* we got search path, so don't look in registry */

    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE", 0, KEY_READ, &hkey) !=
        ERROR_SUCCESS) {
        return;
    }
    if (RegOpenKeyEx(hkey, "CMU", 0, KEY_READ, &hkey) !=
        ERROR_SUCCESS) {
        return;
    }
    if (RegOpenKeyEx(hkey, "Nyquist", 0, KEY_READ, &hkey) !=
        ERROR_SUCCESS) {
        return;
    }
    if (RegQueryValueEx(hkey, "XLISPPATH", NULL, &dwType, p, &p_max) !=
           ERROR_SUCCESS) {
        *p = 0;	
        return;
    }
}

LVAL xget_user()
{
    // not implemented for Windows, just use "nyquist"
    return cvstring("nyquist");
}


#ifdef WINGUI
/* NOTE: define WINGUI in the Project Settings for the NyqWin projects.
 * Do not define WINGUI for console versions of Nyquist
 */

/****************************************************************************
*               fileopen
* Inputs:
*    char *deflt: the default file name (e.g. from command line)
*    char *extension: default extension
*    char *mode: read ("r") or write ("w")
*    char *prompt: prompt for user
* Returns:
*    opened file pointer
* Effect: 
*    opens file, prompts for user input if necessary and warns about
*    possible confusion.  If deflt is a null string or NULL, the user will
*    be prompted for a name.     The routine loops until a file is opened.
*    If the mode is "r", a check is made to see if the file exists
*    with and without the extension.     If both exist a warning is given.
*    For mode "w", a check is made to see if the file will be overwritten.
*    The extension is automatically added if the default or user-typed
*    file has no "."     At the bottom of the loop body, if no file has
*    been opened, the user is prompted for another file name.
****************************************************************************/

char fileopen_name[100];        /* name of the opened file */

HINSTANCE hInst;            /* current instance */
HWND hMainWindow;           /* main window handle */


char *getfilename(char *deflt, char *extension, char *mode, char *prompt)
{
    char filter[64];
    char *filter_ptr = NULL;
    OPENFILENAME open_file_name;

    if (extension && extension[0] == 0) extension = NULL;
    if (strcmp(extension, "lsp") == 0) {
        strcpy(filter, "Lisp files");
    } else if (extension) {
        sprintf(filter, "%s files", extension);
    }
    if (extension) {
        sprintf(filter + strlen(filter), "%c*.%s%cAll files%c*.*%c",
                0, extension, 0, 0, 0);
        filter_ptr = filter;
    }
    if (!deflt) deflt = "";     /* treat NULL as the empty string */
    strcpy(fileopen_name, deflt);

    open_file_name.lStructSize = sizeof(OPENFILENAME);
    open_file_name.hwndOwner = hMainWindow;

    open_file_name.hInstance = hInst;
    open_file_name.lpstrFilter = filter_ptr; 
    open_file_name.lpstrCustomFilter = NULL; 
    open_file_name.nMaxCustFilter = 0; 
    open_file_name.nFilterIndex = 0; 
    open_file_name.lpstrFile = fileopen_name; 
    open_file_name.nMaxFile = 100; 
    open_file_name.lpstrFileTitle = NULL;
    open_file_name.nMaxFileTitle = 0; 
    open_file_name.lpstrInitialDir = NULL; 
    open_file_name.lpstrTitle = prompt;
    open_file_name.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; 
    open_file_name.nFileOffset = 0; 
    open_file_name.nFileExtension = 0; 
    open_file_name.lpstrDefExt = extension; 
    open_file_name.lCustData = 0; 
    open_file_name.lpfnHook = 0; 
    open_file_name.lpTemplateName = 0;
    if (((*mode == 'r') && GetOpenFileName(&open_file_name)) ||
        ((*mode == 'w') && GetSaveFileName(&open_file_name))) {
        return open_file_name.lpstrFile;
    }
    return NULL;
}



FILE *fileopen(char *deflt, char *extension, char *mode, char *prompt)
{
    FILE *fp = NULL;            /* file corresponding to filename */
    if (getfilename(deflt, extension, mode, prompt)) {
        fp = fopen(fileopen_name, mode);
    }
    return fp;
}

#endif