summaryrefslogtreecommitdiff
path: root/source/luametatex/source/utilities/auxfile.c
blob: 1aae0e69150793596446e01103bcfb08c557a05d (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
    See license.txt in the root of this project.
*/

# include <stdio.h>
# include <sys/stat.h>

# include "auxfile.h"
# include "auxmemory.h"

# ifdef _WIN32

    # include <windows.h>
    # include <ctype.h>
    # include <io.h>
    # include <shellapi.h>

    LPWSTR aux_utf8_to_wide(const char *utf8str) {
        if (utf8str) {
            int length = MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, NULL, 0); /* preroll */
            LPWSTR wide = (LPWSTR) lmt_memory_malloc(sizeof(WCHAR) * length);
            MultiByteToWideChar(CP_UTF8, 0, utf8str, -1, wide, length);
            return wide;
        } else {
            return NULL;
        }
    }

    char *aux_utf8_from_wide(LPWSTR widestr) {
        if (widestr) {
            int length = WideCharToMultiByte(CP_UTF8, 0, widestr, -1, NULL, 0, NULL, NULL);
            char * utf8str = (char *) lmt_memory_malloc(sizeof(char) * length);
            WideCharToMultiByte(CP_UTF8, 0, widestr, -1, utf8str, length, NULL, NULL);
            return (char *) utf8str;
        } else {
            return NULL;
        }
    }

    FILE *aux_utf8_fopen(const char *path, const char *mode) {
        if (path && mode) {
            LPWSTR wpath = aux_utf8_to_wide(path);
            LPWSTR wmode = aux_utf8_to_wide(mode);
            FILE *f = _wfopen(wpath,wmode);
            lmt_memory_free(wpath);
            lmt_memory_free(wmode);
            return f;
        } else {
            return NULL;
        }
    }

    FILE *aux_utf8_popen(const char *path, const char *mode) {
        if (path && mode) {
            LPWSTR wpath = aux_utf8_to_wide(path);
            LPWSTR wmode = aux_utf8_to_wide(mode);
            FILE *f = _wpopen(wpath,wmode);
            lmt_memory_free(wpath);
            lmt_memory_free(wmode);
            return f;
        } else {
            return NULL;
        }
    }

    int aux_utf8_system(const char *cmd)
    {
        LPWSTR wcmd = aux_utf8_to_wide(cmd);
        int result = _wsystem(wcmd);
        lmt_memory_free(wcmd);
        return result;
    }

    int aux_utf8_remove(const char *name)
    {
        LPWSTR wname = aux_utf8_to_wide(name);
        int result = _wremove(wname);
        lmt_memory_free(wname);
        return result;
    }

    int aux_utf8_rename(const char *oldname, const char *newname)
    {
        LPWSTR woldname = aux_utf8_to_wide(oldname);
        LPWSTR wnewname = aux_utf8_to_wide(newname);
        int result = _wrename(woldname, wnewname);
        lmt_memory_free(woldname);
        lmt_memory_free(wnewname);
        return result;
    }

    int aux_utf8_setargv(char * **av, char **argv, int argc)
    {
        if (argv) {
            int c = 0;
            LPWSTR *l = CommandLineToArgvW(GetCommandLineW(), &c);
            if (l != NULL) {
                char **v = lmt_memory_malloc(sizeof(char *) * c);
                for (int i = 0; i < c; i++) {
                    v[i] = aux_utf8_from_wide(l[i]);
                }
                *av = v;
                /*tex Let's be nice with path names: |c:\\foo\\etc| */
                if (c > 1) {
                    if ((strlen(v[c-1]) > 2) && isalpha(v[c-1][0]) && (v[c-1][1] == ':') && (v[c-1][2] == '\\')) {
                        for (char *p = v[c-1]+2; *p; p++) {
                            if (*p == '\\') {
                                *p = '/';
                            }
                        }
                    }
                }
            }
            return c;
        } else {
            *av = NULL;
            return argc;
        }
    }

    char *aux_utf8_getownpath(const char *file)
    {
        if (file) {
            char *path = NULL;
            char buffer[MAX_PATH];
            GetModuleFileName(NULL,buffer,sizeof(buffer));
            path = lmt_memory_strdup(buffer);
            if (strlen(path) > 0) {
                for (size_t i = 0; i < strlen(path); i++) {
                    if (path[i] == '\\') {
                        path[i] = '/';
                    }
                }
                return path;
            }
        }
        return lmt_memory_strdup(".");
    }

# else

    # include <string.h>
    # include <stdlib.h>
    # include <unistd.h>

    int aux_utf8_setargv(char * **av, char **argv, int argc)
    {
        *av = argv;
        return argc;
    }

    char *aux_utf8_getownpath(const char *file)
    {
        if (strchr(file, '/')) {
            return lmt_memory_strdup(file);
        } else {
            const char *esp;
            size_t prefixlen = 0;
            size_t totallen = 0;
            size_t filelen = strlen(file);
            char *path = NULL;
            char *searchpath = lmt_memory_strdup(getenv("PATH"));
            const char *index = searchpath;
            if (index) {
                do {
                    esp = strchr(index, ':');
                    if (esp) {
                        prefixlen = (size_t) (esp - index);
                    } else {
                        prefixlen = strlen(index);
                    }
                    if (prefixlen == 0 || index[prefixlen - 1] == '/') {
                        totallen = prefixlen + filelen;
# ifdef PATH_MAX
                        if (totallen >= PATH_MAX) {
                            continue;
                        }
# endif
                        path = lmt_memory_malloc(totallen + 1);
                        memcpy(path, index, prefixlen);
                        memcpy(path + prefixlen, file, filelen);
                    } else {
                        totallen = prefixlen + filelen + 1;
# ifdef PATH_MAX
                        if (totallen >= PATH_MAX) {
                            continue;
                        }
# endif
                        path = lmt_memory_malloc(totallen + 1);
                        memcpy(path, index, prefixlen);
                        path[prefixlen] = '/';
                        memcpy(path + prefixlen + 1, file, filelen);
                    }
                    path[totallen] = '\0';
                    if (access(path, X_OK) >= 0) {
                        break;
                    }
                    lmt_memory_free(path);
                    path = NULL;
                    index = esp + 1;
                } while (esp);
            }
            lmt_memory_free(searchpath);
            if (path) {
                return path;
            } else {
                return lmt_memory_strdup("."); /* ok? */
            }
        }
    }

# endif

# ifndef S_ISREG
    # define S_ISREG(mode) (mode & _S_IFREG)
# endif

# ifdef _WIN32

    char *aux_basename(const char *name) {
        char base[256+1];
        char suff[256+1];
        _splitpath(name,NULL,NULL,base,suff);
        {
            size_t b = strlen((const char*)base);
            size_t s = strlen((const char*)suff);
            char *result = (char *) lmt_memory_malloc(sizeof(char) * (b+s+1));
            if (result) {
                memcpy(&result[0], &base[0], b);
                memcpy(&result[b], &suff[0], s);
                result[b + s] = '\0';
            }
            return result;
        }
    }

    char *aux_dirname(const char *name) {
        char driv[256 + 1];
        char path[256 + 1];
        _splitpath(name,driv,path,NULL,NULL);
        {
            size_t d = strlen((const char*)driv);
            size_t p = strlen((const char*)path);
            char *result = (char *) lmt_memory_malloc(sizeof(char) * (d+p+1));
            if (result) {
                if (path[p - 1] == '/' || path[p - 1] == '\\') {
                    --p;
                }
                memcpy(&result[0], &driv[0], d);
                memcpy(&result[d], &path[0], p);
                result[d + p] = '\0';
            }
            return result;
        }
    }

 //  int aux_is_readable(const char *filename)
 //  {
 //      struct stat finfo;
 //      FILE *f;
 //      return (stat(filename, &finfo) == 0)
 //          && S_ISREG(finfo.st_mode)
 //          && ((f = aux_utf8_fopen(filename, "r")) != NULL)
 //          && ! fclose(f);
 //  }

    int aux_is_readable(const char *filename)
    {
        struct _stati64 info;
        LPWSTR w = aux_utf8_to_wide(filename);
        int r = _wstati64(w, &info);
        FILE *f;
        lmt_memory_free(w);
        return (r == 0)
            && (S_ISREG(info.st_mode))
            && ((f = aux_utf8_fopen(filename, "r")) != NULL)
            && ! fclose(f);
    }

# else

    # include <libgen.h>

    int aux_is_readable(const char *filename)
    {
        struct stat finfo;
        FILE *f;
        return (stat(filename, &finfo) == 0)
            && S_ISREG(finfo.st_mode)
            && ((f = fopen(filename, "r")) != NULL)
            && ! fclose(f);
    }

# endif