Debian Codegraph @

texk/web2c/synctexdir/synctex_parser_utils.c

raw
1 /*
2 Copyright (c) 2008-2017 jerome DOT laurens AT u-bourgogne DOT fr
3
4 This file is part of the __SyncTeX__ package.
5
6 [//]: # (Latest Revision: Fri Jul 14 16:20:41 UTC 2017)
7 [//]: # (Version: 1.21)
8
9 See `synctex_parser_readme.md` for more details
10
11 ## License
12
13 Permission is hereby granted, free of charge, to any person
14 obtaining a copy of this software and associated documentation
15 files (the "Software"), to deal in the Software without
16 restriction, including without limitation the rights to use,
17 copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the
19 Software is furnished to do so, subject to the following
20 conditions:
21
22 The above copyright notice and this permission notice shall be
23 included in all copies or substantial portions of the Software.
24
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
27 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
29 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
30 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 OTHER DEALINGS IN THE SOFTWARE
33
34 Except as contained in this notice, the name of the copyright holder
35 shall not be used in advertising or otherwise to promote the sale,
36 use or other dealings in this Software without prior written
37 authorization from the copyright holder.
38
39 */
40
41 /* In this file, we find all the functions that may depend on the operating system. */
42
43 #include <synctex_parser_utils.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48
49 #include <limits.h>
50 #include <ctype.h>
51
52 #include <sys/stat.h>
53
54 #if defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__) || defined(__WINDOWS__)
55 #define SYNCTEX_WINDOWS 1
56 #endif
57
58 #if defined(__OS2__)
59 #define SYNCTEX_OS2 1
60 #endif
61
62 #if defined(_WIN32)
63 #define SYNCTEX_RECENT_WINDOWS 1
64 #endif
65
66 #ifdef SYNCTEX_WINDOWS
67 #include <windows.h>
68 #include <shlwapi.h> /* Use shlwapi.lib */
69 #endif
70
71 void *_synctex_malloc(size_t size) {
72 void * ptr = malloc(size);
73 if(ptr) {
74 memset(ptr,0, size);/* ensures null termination of strings */
75 }
76 return (void *)ptr;
77 }
78
79 void _synctex_free(void * ptr) {
80 if (ptr) {
81 free(ptr);
82 }
83 }
84
85 #if !defined(_WIN32)
86 # include <syslog.h>
87 #endif
88
89 SYNCTEX_ATTRIBUTE_FORMAT_PRINTF(3, 0)
90 static int _synctex_log(int level, const char * prompt, const char * reason,va_list arg) {
91 int result;
92 # ifdef SYNCTEX_RECENT_WINDOWS
93 {/* This code is contributed by William Blum.
94 As it does not work on some older computers,
95 the _WIN32 conditional here is replaced with a SYNCTEX_RECENT_WINDOWS one.
96 According to http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx
97 Minimum supported client Windows 2000 Professional
98 Minimum supported server Windows 2000 Server
99 People running Windows 2K standard edition will not have OutputDebugStringA.
100 JL.*/
101 char *buff;
102 size_t len;
103 OutputDebugStringA(prompt);
104 len = _vscprintf(reason, arg) + 1;
105 buff = (char*)malloc( len * sizeof(char) );
106 result = vsprintf(buff, reason, arg) +strlen(prompt);
107 OutputDebugStringA(buff);
108 OutputDebugStringA("\n");
109 free(buff);
110 }
111 # elif SYNCTEX_USE_SYSLOG
112 char * buffer1 = NULL;
113 char * buffer2 = NULL;
114 openlog ("SyncTeX", LOG_CONS | LOG_PID | LOG_PERROR | LOG_NDELAY, LOG_LOCAL0);
115 if (vasprintf(&buffer1,reason,arg)>=0
116 && asprintf(&buffer2,"%s%s",prompt, buffer1)>=0) {
117 syslog (level, "%s", buffer2);
118 result = (int)strlen(buffer2);
119 } else {
120 syslog (level, "%s",prompt);
121 vsyslog(level,reason,arg);
122 result = (int)strlen(prompt);
123 }
124 free(buffer1);
125 free(buffer2);
126 closelog();
127 # else
128 FILE * where = level == LOG_ERR? stderr: stdout;
129 result = fputs(prompt,where);
130 result += vfprintf(where, reason, arg);
131 result += fprintf(where,"\n");
132 # endif
133 return result;
134 }
135
136 SYNCTEX_ATTRIBUTE_FORMAT_PRINTF(1, 2)
137 int _synctex_error(const char * reason,...) {
138 va_list arg;
139 int result;
140 va_start (arg, reason);
141 #if defined(SYNCTEX_RECENT_WINDOWS) /* LOG_ERR is not used */
142 result = _synctex_log(0, "! SyncTeX Error : ", reason, arg);
143 #else
144 result = _synctex_log(LOG_ERR, "! SyncTeX Error : ", reason, arg);
145 #endif
146 va_end (arg);
147 return result;
148 }
149
150 SYNCTEX_ATTRIBUTE_FORMAT_PRINTF(1, 2)
151 int _synctex_debug(const char * reason,...) {
152 va_list arg;
153 int result;
154 va_start (arg, reason);
155 #if defined(SYNCTEX_RECENT_WINDOWS) /* LOG_DEBUG is not used */
156 result = _synctex_log(0, "! SyncTeX Error : ", reason, arg);
157 #else
158 result = _synctex_log(LOG_DEBUG, "! SyncTeX Error : ", reason, arg);
159 #endif
160 va_end (arg);
161 return result;
162 }
163
164 /* strip the last extension of the given string, this string is modified! */
165 void _synctex_strip_last_path_extension(char * string) {
166 if(NULL != string){
167 char * last_component = NULL;
168 char * last_extension = NULL;
169 # if defined(SYNCTEX_WINDOWS)
170 last_component = PathFindFileName(string);
171 last_extension = PathFindExtension(string);
172 if(last_extension == NULL)return;
173 if(last_component == NULL)last_component = string;
174 if(last_extension>last_component){/* filter out paths like "my/dir/.hidden" */
175 last_extension[0] = '\0';
176 }
177 # else
178 char * next = NULL;
179 /* first we find the last path component */
180 if(NULL == (last_component = strstr(string,"/"))){
181 last_component = string;
182 } else {
183 ++last_component;
184 while((next = strstr(last_component,"/"))){
185 last_component = next+1;
186 }
187 }
188 # if defined(SYNCTEX_OS2)
189 /* On OS2, the '\' is also a path separator. */
190 while((next = strstr(last_component,"\\"))){
191 last_component = next+1;
192 }
193 # endif /* SYNCTEX_OS2 */
194 /* then we find the last path extension */
195 if((last_extension = strstr(last_component,"."))){
196 ++last_extension;
197 while((next = strstr(last_extension,"."))){
198 last_extension = next+1;
199 }
200 --last_extension;/* back to the "." */
201 if(last_extension>last_component){/* filter out paths like ....my/dir/.hidden"*/
202 last_extension[0] = '\0';
203 }
204 }
205 # endif /* SYNCTEX_WINDOWS */
206 }
207 }
208
209 synctex_bool_t synctex_ignore_leading_dot_slash_in_path(const char ** name_ref)
210 {
211 if (SYNCTEX_IS_DOT((*name_ref)[0]) && SYNCTEX_IS_PATH_SEPARATOR((*name_ref)[1])) {
212 do {
213 (*name_ref) += 2;
214 while (SYNCTEX_IS_PATH_SEPARATOR((*name_ref)[0])) {
215 ++(*name_ref);
216 }
217 } while(SYNCTEX_IS_DOT((*name_ref)[0]) && SYNCTEX_IS_PATH_SEPARATOR((*name_ref)[1]));
218 return synctex_YES;
219 }
220 return synctex_NO;
221 }
222
223 /* The base name is necessary to deal with the 2011 file naming convention...
224 * path is a '\0' terminated string
225 * The return value is the trailing part of the argument,
226 * just following the first occurrence of the regexp pattern "[^|/|\].[\|/]+".*/
227 const char * _synctex_base_name(const char *path) {
228 const char * ptr = path;
229 do {
230 if (synctex_ignore_leading_dot_slash_in_path(&ptr)) {
231 return ptr;
232 }
233 do {
234 if (!*(++ptr)) {
235 return path;
236 }
237 } while (!SYNCTEX_IS_PATH_SEPARATOR(*ptr));
238 } while (*(++ptr));
239 return path;
240 }
241
242 /* Compare two file names, windows is sometimes case insensitive... */
243 synctex_bool_t _synctex_is_equivalent_file_name(const char *lhs, const char *rhs) {
244 /* Remove the leading regex '(\./+)*' in both rhs and lhs */
245 synctex_ignore_leading_dot_slash_in_path(&lhs);
246 synctex_ignore_leading_dot_slash_in_path(&rhs);
247 next_character:
248 if (SYNCTEX_IS_PATH_SEPARATOR(*lhs)) {/* lhs points to a path separator */
249 if (!SYNCTEX_IS_PATH_SEPARATOR(*rhs)) {/* but not rhs */
250 return synctex_NO;
251 }
252 ++lhs;
253 ++rhs;
254 synctex_ignore_leading_dot_slash_in_path(&lhs);
255 synctex_ignore_leading_dot_slash_in_path(&rhs);
256 goto next_character;
257 } else if (SYNCTEX_IS_PATH_SEPARATOR(*rhs)) {/* rhs points to a path separator but not lhs */
258 return synctex_NO;
259 } else if (SYNCTEX_ARE_PATH_CHARACTERS_EQUAL(*lhs,*rhs)){/* uppercase do not match */
260 return synctex_NO;
261 } else if (!*lhs) {/* lhs is at the end of the string */
262 return *rhs ? synctex_NO : synctex_YES;
263 } else if(!*rhs) {/* rhs is at the end of the string but not lhs */
264 return synctex_NO;
265 }
266 ++lhs;
267 ++rhs;
268 goto next_character;
269 }
270
271 synctex_bool_t _synctex_path_is_absolute(const char * name) {
272 if(!strlen(name)) {
273 return synctex_NO;
274 }
275 # if defined(SYNCTEX_WINDOWS) || defined(SYNCTEX_OS2)
276 if(strlen(name)>2) {
277 return (name[1]==':' && SYNCTEX_IS_PATH_SEPARATOR(name[2]))?synctex_YES:synctex_NO;
278 }
279 return synctex_NO;
280 # else
281 return SYNCTEX_IS_PATH_SEPARATOR(name[0])?synctex_YES:synctex_NO;
282 # endif
283 }
284
285 /* We do not take care of UTF-8 */
286 const char * _synctex_last_path_component(const char * name) {
287 const char * c = name+strlen(name);
288 if(c>name) {
289 if(!SYNCTEX_IS_PATH_SEPARATOR(*c)) {
290 do {
291 --c;
292 if(SYNCTEX_IS_PATH_SEPARATOR(*c)) {
293 return c+1;
294 }
295 } while(c>name);
296 }
297 return c;/* the last path component is the void string*/
298 }
299 return c;
300 }
301
302 int _synctex_copy_with_quoting_last_path_component(const char * src, char ** dest_ref, size_t size) {
303 if(src && dest_ref) {
304 const char * lpc;
305 # define dest (*dest_ref)
306 dest = NULL; /* Default behavior: no change and success. */
307 lpc = _synctex_last_path_component(src);
308 if(strlen(lpc)) {
309 if(strchr(lpc,' ') && lpc[0]!='"' && lpc[strlen(lpc)-1]!='"') {
310 /* We are in the situation where adding the quotes is allowed. */
311 /* Time to add the quotes. */
312 /* Consistency test: we must have dest+size>dest+strlen(dest)+2
313 * or equivalently: strlen(dest)+2<size (see below) */
314 if(strlen(src)<size) {
315 if((dest = (char *)malloc(size+2))) {
316 char * dpc = dest + (lpc-src); /* dpc is the last path component of dest. */
317 if(dest != strncpy(dest,src,size)) {
318 _synctex_error("! _synctex_copy_with_quoting_last_path_component: Copy problem");
319 free(dest);
320 dest = NULL;/* Don't forget to reinitialize. */
321 return -2;
322 }
323 memmove(dpc+1,dpc,strlen(dpc)+1); /* Also move the null terminating character. */
324 dpc[0]='"';
325 dpc[strlen(dpc)+1]='\0';/* Consistency test */
326 dpc[strlen(dpc)]='"';
327 return 0; /* Success. */
328 }
329 return -1; /* Memory allocation error. */
330 }
331 _synctex_error("! _synctex_copy_with_quoting_last_path_component: Internal inconsistency");
332 return -3;
333 }
334 return 0; /* Success. */
335 }
336 return 0; /* No last path component. */
337 # undef dest
338 }
339 return 1; /* Bad parameter, this value is subject to changes. */
340 }
341
342 /* The client is responsible of the management of the returned string, if any. */
343 char * _synctex_merge_strings(const char * first,...);
344
345 char * _synctex_merge_strings(const char * first,...) {
346 va_list arg;
347 size_t size = 0;
348 const char * temp;
349 /* First retrieve the size necessary to store the merged string */
350 va_start (arg, first);
351 temp = first;
352 do {
353 size_t len = strlen(temp);
354 if(UINT_MAX-len<size) {
355 _synctex_error("! _synctex_merge_strings: Capacity exceeded.");
356 va_end(arg);
357 return NULL;
358 }
359 size+=len;
360 } while( (temp = va_arg(arg, const char *)) != NULL);
361 va_end(arg);
362 if(size>0) {
363 char * result = NULL;
364 ++size;
365 /* Create the memory storage */
366 if(NULL!=(result = (char *)malloc(size))) {
367 char * dest = result;
368 va_start (arg, first);
369 temp = first;
370 do {
371 if((size = strlen(temp))>0) {
372 /* There is something to merge */
373 if(dest != strncpy(dest,temp,size)) {
374 _synctex_error("! _synctex_merge_strings: Copy problem");
375 free(result);
376 result = NULL;
377 va_end(arg);
378 return NULL;
379 }
380 dest += size;
381 }
382 } while( (temp = va_arg(arg, const char *)) != NULL);
383 va_end(arg);
384 dest[0]='\0';/* Terminate the merged string */
385 return result;
386 }
387 _synctex_error("! _synctex_merge_strings: Memory problem");
388 return NULL;
389 }
390 return NULL;
391 }
392
393 /* The purpose of _synctex_get_name is to find the name of the synctex file.
394 * There is a list of possible filenames from which we return the most recent one and try to remove all the others.
395 * With two runs of pdftex or xetex we are sure the synctex file is really the most appropriate.
396 */
397 int _synctex_get_name(const char * output, const char * build_directory, char ** synctex_name_ref, synctex_io_mode_t * io_mode_ref)
398 {
399 if(output && synctex_name_ref && io_mode_ref) {
400 /* If output is already absolute, we just have to manage the quotes and the compress mode */
401 size_t size = 0;
402 char * synctex_name = NULL;
403 synctex_io_mode_t io_mode = *io_mode_ref;
404 const char * base_name = _synctex_last_path_component(output); /* do not free, output is the owner. base name of output*/
405 /* Do we have a real base name ? */
406 if(strlen(base_name)>0) {
407 /* Yes, we do. */
408 const char * temp = NULL;
409 char * core_name = NULL; /* base name of output without path extension. */
410 char * dir_name = NULL; /* dir name of output */
411 char * quoted_core_name = NULL;
412 char * basic_name = NULL;
413 char * gz_name = NULL;
414 char * quoted_name = NULL;
415 char * quoted_gz_name = NULL;
416 char * build_name = NULL;
417 char * build_gz_name = NULL;
418 char * build_quoted_name = NULL;
419 char * build_quoted_gz_name = NULL;
420 struct stat buf;
421 time_t the_time = 0;
422 /* Create core_name: let temp point to the dot before the path extension of base_name;
423 * We start form the \0 terminating character and scan the string upward until we find a dot.
424 * The leading dot is not accepted. */
425 if((temp = strrchr(base_name,'.')) && (size = temp - base_name)>0) {
426 /* There is a dot and it is not at the leading position */
427 if(NULL == (core_name = (char *)malloc(size+1))) {
428 _synctex_error("! _synctex_get_name: Memory problem 1");
429 return -1;
430 }
431 if(core_name != strncpy(core_name,base_name,size)) {
432 _synctex_error("! _synctex_get_name: Copy problem 1");
433 free(core_name);
434 dir_name = NULL;
435 return -2;
436 }
437 core_name[size] = '\0';
438 } else {
439 /* There is no path extension,
440 * Just make a copy of base_name */
441 core_name = _synctex_merge_strings(base_name);
442 }
443 /* core_name is properly set up, owned by "self". */
444 /* creating dir_name. */
445 size = strlen(output)-strlen(base_name);
446 if(size>0) {
447 /* output contains more than one path component */
448 if(NULL == (dir_name = (char *)malloc(size+1))) {
449 _synctex_error("! _synctex_get_name: Memory problem");
450 free(core_name);
451 return -1;
452 }
453 if(dir_name != strncpy(dir_name,output,size)) {
454 _synctex_error("! _synctex_get_name: Copy problem");
455 free(dir_name);
456 dir_name = NULL;
457 free(core_name);
458 dir_name = NULL;
459 return -2;
460 }
461 dir_name[size] = '\0';
462 }
463 /* dir_name is properly set up. It ends with a path separator, if non void. */
464 /* creating quoted_core_name. */
465 if(strchr(core_name,' ')) {
466 quoted_core_name = _synctex_merge_strings("\"",core_name,"\"");
467 }
468 /* quoted_core_name is properly set up. */
469 if(dir_name &&strlen(dir_name)>0) {
470 basic_name = _synctex_merge_strings(dir_name,core_name,synctex_suffix,NULL);
471 if(quoted_core_name && strlen(quoted_core_name)>0) {
472 quoted_name = _synctex_merge_strings(dir_name,quoted_core_name,synctex_suffix,NULL);
473 }
474 } else {
475 basic_name = _synctex_merge_strings(core_name,synctex_suffix,NULL);
476 if(quoted_core_name && strlen(quoted_core_name)>0) {
477 quoted_name = _synctex_merge_strings(quoted_core_name,synctex_suffix,NULL);
478 }
479 }
480 if(!_synctex_path_is_absolute(output) && build_directory && (size = strlen(build_directory))) {
481 temp = build_directory + size - 1;
482 if(_synctex_path_is_absolute(temp)) {
483 build_name = _synctex_merge_strings(build_directory,basic_name,NULL);
484 if(quoted_core_name && strlen(quoted_core_name)>0) {
485 build_quoted_name = _synctex_merge_strings(build_directory,quoted_name,NULL);
486 }
487 } else {
488 build_name = _synctex_merge_strings(build_directory,"/",basic_name,NULL);
489 if(quoted_core_name && strlen(quoted_core_name)>0) {
490 build_quoted_name = _synctex_merge_strings(build_directory,"/",quoted_name,NULL);
491 }
492 }
493 }
494 if(basic_name) {
495 gz_name = _synctex_merge_strings(basic_name,synctex_suffix_gz,NULL);
496 }
497 if(quoted_name) {
498 quoted_gz_name = _synctex_merge_strings(quoted_name,synctex_suffix_gz,NULL);
499 }
500 if(build_name) {
501 build_gz_name = _synctex_merge_strings(build_name,synctex_suffix_gz,NULL);
502 }
503 if(build_quoted_name) {
504 build_quoted_gz_name = _synctex_merge_strings(build_quoted_name,synctex_suffix_gz,NULL);
505 }
506 /* All the others names are properly set up... */
507 /* retain the most recently modified file */
508 # define TEST(FILENAME,COMPRESS_MODE) \
509 if(FILENAME) {\
510 if (stat(FILENAME, &buf)) { \
511 free(FILENAME);\
512 FILENAME = NULL;\
513 } else if (buf.st_mtime>the_time) { \
514 the_time=buf.st_mtime; \
515 synctex_name = FILENAME; \
516 if (COMPRESS_MODE) { \
517 io_mode |= synctex_io_gz_mask; \
518 } else { \
519 io_mode &= ~synctex_io_gz_mask; \
520 } \
521 } \
522 }
523 TEST(basic_name,synctex_DONT_COMPRESS);
524 TEST(gz_name,synctex_COMPRESS);
525 TEST(quoted_name,synctex_DONT_COMPRESS);
526 TEST(quoted_gz_name,synctex_COMPRESS);
527 TEST(build_name,synctex_DONT_COMPRESS);
528 TEST(build_gz_name,synctex_COMPRESS);
529 TEST(build_quoted_name,synctex_DONT_COMPRESS);
530 TEST(build_quoted_gz_name,synctex_COMPRESS);
531 # undef TEST
532 /* Free all the intermediate filenames, except the one that will be used as returned value. */
533 # define CLEAN_AND_REMOVE(FILENAME) \
534 if(FILENAME && (FILENAME!=synctex_name)) {\
535 remove(FILENAME);\
536 printf("synctex tool info: %s removed\n",FILENAME);\
537 free(FILENAME);\
538 FILENAME = NULL;\
539 }
540 CLEAN_AND_REMOVE(basic_name);
541 CLEAN_AND_REMOVE(gz_name);
542 CLEAN_AND_REMOVE(quoted_name);
543 CLEAN_AND_REMOVE(quoted_gz_name);
544 CLEAN_AND_REMOVE(build_name);
545 CLEAN_AND_REMOVE(build_gz_name);
546 CLEAN_AND_REMOVE(build_quoted_name);
547 CLEAN_AND_REMOVE(build_quoted_gz_name);
548 # undef CLEAN_AND_REMOVE
549 /* set up the returned values */
550 * synctex_name_ref = synctex_name;
551 /* synctex_name won't always end in .gz, even when compressed. */
552 FILE * F = fopen(synctex_name, "r");
553 if (F != NULL) {
554 if (!feof(F)
555 && 31 == fgetc(F)
556 && !feof(F)
557 && 139 == fgetc(F)) {
558 io_mode = synctex_compress_mode_gz;
559 }
560 fclose(F);
561 }
562 * io_mode_ref = io_mode;
563 return 0;
564 }
565 return -1;/* bad argument */
566 }
567 return -2;
568 }
569
570 const char * _synctex_get_io_mode_name(synctex_io_mode_t io_mode) {
571 static const char * synctex_io_modes[4] = {"r","rb","a","ab"};
572 unsigned index = ((io_mode & synctex_io_gz_mask)?1:0) + ((io_mode & synctex_io_append_mask)?2:0);// bug pointed out by Jose Alliste
573 return synctex_io_modes[index];
574 }