Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

512 lines
17 KiB

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