synctex_parser_utils.m 19 KB

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