Debian Codegraph @

texk/web2c/synctexdir/synctex_main.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 Acknowledgments:
40 ----------------
41 The author received useful remarks from the pdfTeX developers, especially Hahn The Thanh,
42 and significant help from XeTeX developer Jonathan Kew
43
44 Nota Bene:
45 ----------
46 If you include or use a significant part of the synctex package into a software,
47 I would appreciate to be listed as contributor and see "SyncTeX" highlighted.
48
49 Version 1.2
50 Thu Jun 19 09:39:21 UTC 2008
51
52 History:
53 --------
54
55 - the -d option for an input directory
56
57 Important notice:
58 -----------------
59 This file is named "synctex_main.c".
60 This is the command line interface to the synctex_parser.c.
61 */
62
63 # ifdef __linux__
64 # define _ISOC99_SOURCE /* to get the fmax() prototype */
65 # endif
66
67 # ifdef __SYNCTEX_WORK__
68 # include <synctex_parser_c-auto.h>
69 /* for inline && HAVE_xxx */
70 # else
71 # include <w2c/c-auto.h>
72 /* for inline && HAVE_xxx */
73 # endif
74
75 # include <stdlib.h>
76 # include <stdio.h>
77 # include <string.h>
78 # include <stdarg.h>
79 # include <math.h>
80 # include "synctex_version.h"
81 # include "synctex_parser_advanced.h"
82 # include "synctex_parser_utils.h"
83
84 /* The code below uses strlcat and strlcpy, which avoids security warnings with some compilers.
85 However, if these are not available we simply use the old, unchecked versions;
86 this is OK because all the uses in this code are working with a buffer that's been
87 allocated based on measuring the strings involved.
88 */
89 #ifndef HAVE_STRLCAT
90 # define strlcat(dst, src, size) strcat((dst), (src))
91 #endif
92 #ifndef HAVE_STRLCPY
93 # define strlcpy(dst, src, size) strcpy((dst), (src))
94 #endif
95 #ifndef HAVE_FMAX
96 # define fmax my_fmax
97 inline static double my_fmax(double x, double y) { return (x < y) ? y : x; }
98 #endif
99
100 /* I use the definition in kpathsea --ak
101 #ifdef WIN32
102 # define snprintf _snprintf
103 #endif
104 */
105
106 #ifdef WIN32
107 # include <kpathsea/progname.h>
108 #endif
109
110
111 #if SYNCTEX_DEBUG
112 # ifdef WIN32
113 # include <direct.h>
114 # define getcwd _getcwd
115 # else
116 # include <unistd.h>
117 # endif
118 #endif
119
120 int main(int argc, char *argv[]);
121
122 void synctex_help(const char * error,...);
123 void synctex_help_view(const char * error,...);
124 void synctex_help_edit(const char * error,...);
125 void synctex_help_update(const char * error,...);
126
127 int synctex_view(int argc, char *argv[]);
128 int synctex_edit(int argc, char *argv[]);
129 int synctex_update(int argc, char *argv[]);
130 int synctex_test(int argc, char *argv[]);
131
132 int main(int argc, char *argv[])
133 {
134 int arg_index = 1;
135 #ifdef WIN32
136 kpse_set_program_name(argv[0], "synctex");
137 #endif
138 printf("This is SyncTeX command line utility, version " SYNCTEX_CLI_VERSION_STRING "\n");
139 if(arg_index<argc) {
140 if(0==strcmp("help",argv[arg_index])) {
141 if(++arg_index<argc) {
142 if(0==strcmp("view",argv[arg_index])) {
143 synctex_help_view(NULL);
144 return 0;
145 } else if(0==strcmp("edit",argv[arg_index])) {
146 synctex_help_edit(NULL);
147 return 0;
148 } else if(0==strcmp("update",argv[arg_index])) {
149 synctex_help_update(NULL);
150 return 0;
151 }
152 }
153 synctex_help(NULL);
154 return 0;
155 } else if(0==strcmp("view",argv[arg_index])) {
156 return synctex_view(argc-arg_index-1,argv+arg_index+1);
157 } else if(0==strcmp("edit",argv[arg_index])) {
158 return synctex_edit(argc-arg_index-1,argv+arg_index+1);
159 } else if(0==strcmp("update",argv[arg_index])) {
160 return synctex_update(argc-arg_index-1,argv+arg_index+1);
161 } else if(0==strcmp("test",argv[arg_index])) {
162 return synctex_test(argc-arg_index-1,argv+arg_index+1);
163 }
164 }
165 synctex_help("Missing options");
166 return 0;
167 }
168
169 static void synctex_usage(const char * error,va_list ap) {
170 if(error) {
171 fprintf(stderr,"SyncTeX ERROR: ");
172 vfprintf(stderr,error,ap);
173 fprintf(stderr,"\n");
174 }
175 fprintf((error?stderr:stdout),
176 "usage: synctex <subcommand> [options] [args]\n"
177 "Synchronize TeXnology command-line client, version " SYNCTEX_VERSION_STRING "\n\n"
178 "The Synchronization TeXnology by Jérôme Laurens is a rather new feature of recent TeX engines.\n"
179 "It allows to synchronize between input and output, which means to\n"
180 "navigate from the source document to the typeset material and vice versa.\n\n"
181 );
182 return;
183 }
184
185 void synctex_help(const char * error,...) {
186 va_list v;
187 va_start(v, error);
188 synctex_usage(error, v);
189 va_end(v);
190 fprintf((error?stderr:stdout),
191 "Available subcommands:\n"
192 " view to perform forwards synchronization\n"
193 " edit to perform backwards synchronization\n"
194 " update to update a synctex file after a dvi/xdv to pdf filter\n"
195 " help this help\n\n"
196 "Type 'synctex help <subcommand>' for help on a specific subcommand.\n"
197 "There is also an undocumented test subcommand.\n"
198 );
199 return;
200 }
201
202 void synctex_help_view(const char * error,...) {
203 va_list v;
204 va_start(v, error);
205 synctex_usage(error, v);
206 va_end(v);
207 fputs("synctex view: forwards or direct synchronization,\n"
208 "command sent by the editor to view the output corresponding to the position under the mouse\n"
209 "\n"
210 "usage: synctex view -i line:column:[page_hint:]input -o output [-d directory] [-x viewer-command] [-h before/offset:middle/after]\n"
211 "\n"
212 "-i line:column:[page_hint:]input\n"
213 " specify the line, column, optional page hint and input file.\n"
214 " The line and column are 1 based integers,\n"
215 " they allow to identify every character in a file.\n"
216 " column is the offset of a character relative to the containing line.\n"
217 " Pass 0 if this information is not relevant.\n"
218 " page_hint is the currently displayed page number.\n"
219 " If there is an answer on that page, it will be returned.\n"
220 " Pass 0 if this information is not available to you.\n"
221 " input is either the name of the main source file or an included document.\n"
222 " It must be the very name as understood by TeX, id est the name exactly as it appears in the log file.\n"
223 " It does not matter if the file actually exists or not, except that the command is not really useful.\n"
224 " \n"
225 "-o output\n"
226 " is the full or relative path of the output file (with any relevant path extension).\n"
227 " This file must exist.\n"
228 " \n"
229 "-d directory\n"
230 " is the directory containing the synctex file, in case it is different from the directory of the output.\n"
231 " This directory must exist.\n"
232 " An example will explain how things work: for synctex -o ...:bar.tex -d foo,\n"
233 " the chosen synctex file is the most recent among bar.synctex, bar.synctex.gz, foo/bar.synctex and foo/bar.synctex.gz.\n"
234 " The other ones are simply removed, if the authorization is granted\n"
235 " \n"
236 "-x viewer-command\n"
237 " Normally the synctex tool outputs its result to the stdout.\n"
238 " It is possible to launch an external tool with the result.\n"
239 " The viewer-command is a printf like format string with following specifiers.\n"
240 " %{output} is the name specifier of the main document, without path extension.\n"
241 " %{page} is the 0 based page number specifier, %{page+1} is the 1 based page number specifier.\n"
242 " To synchronize by point, %{x} is the x coordinate specifier, %{y} is the y coordinate specifier,\n"
243 " both in dots and relative to the top left corner of the page.\n"
244 " To synchronize by box,\n"
245 " %{h} is the horizontal coordinate specifier of the origin of the enclosing box,\n"
246 " %{v} is the vertical coordinate specifier of the origin of the enclosing box,\n"
247 " both in dots and relative to the upper left corner of the page.\n"
248 " They may be different from the preceding pair of coordinates.\n"
249 " %{width} is the width specifier, %{height} is the height specifier of the enclosing box.\n"
250 " The latter dimension is naturally counted from bottom to top.\n"
251 " There is no notion of depth for such a box.\n"
252 " To synchronize by content, %{before} is the word before,\n"
253 " %{offset} is the offset specifier, %{middle} is the middle word, and %{after} is the word after.\n"
254 "\n"
255 " If no viewer command is provided, the content of the SYNCTEX_VIEWER environment variable is used instead.\n"
256 "\n"
257 "-h before/offset:middle/after\n"
258 " This hint allows a forwards synchronization by contents.\n"
259 " Instead of giving a character offset in a line, you can give full words.\n"
260 " A full word is a sequence of characters (excepting '/').\n"
261 " You will choose full words in the source document that will certainly appear unaltered in the output.\n"
262 " The \"middle\" word contains the character under the mouse at position offset.\n"
263 " \"before\" is a full word preceding middle and \"after\" is following it.\n"
264 " The before or after word can be missing, they are then considered as void strings.\n"
265 " \n"
266 "The result is a list of records. In general the first one is the most accurate but\n"
267 "it is the responsibility of the client to decide which one best fits the user needs.\n",
268 (error?stderr:stdout)
269 );
270 return;
271 }
272
273 typedef struct {
274 int line;
275 int column;
276 int page;
277 unsigned int offset;
278 char * input;
279 char * output;
280 char * directory;
281 char * viewer;
282 char * before;
283 char * middle;
284 char * after;
285 } synctex_view_params_t;
286
287 int synctex_view_proceed(synctex_view_params_t * paramsRef);
288
289 /* "usage: synctex view -i line:column:input -o output [-d directory] [-x viewer-command] [-h before/offset:middle/after]\n" */
290 int synctex_view(int argc, char *argv[]) {
291 int arg_index = 0;
292 char * start = NULL;
293 char * end = NULL;
294 synctex_view_params_t Ps = {-1,0,0,-1,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
295
296 /* required */
297 if((arg_index>=argc) || strcmp("-i",argv[arg_index]) || (++arg_index>=argc)) {
298 synctex_help_view("Missing -i required argument");
299 return -1;
300 }
301 start = argv[arg_index];
302 Ps.line = (int)strtol(start,&end,10);
303 if(end>start && strlen(end)>0 && *end==':') {
304 start = end+1;
305 Ps.column = (int)strtol(start,&end,10);
306 if(end == start || Ps.column < 0) {
307 Ps.column = 0;
308 }
309 if(strlen(end)>1 && *end==':') {
310 Ps.input = end+1;
311 goto scan_output;
312 }
313 }
314 synctex_help_view("Bad -i argument");
315 return -1;
316 scan_output:
317 if((++arg_index>=argc) || strcmp("-o",argv[arg_index]) || (++arg_index>=argc)) {
318 synctex_help_view("Missing -o required argument");
319 return -1;
320 }
321 Ps.output = argv[arg_index];
322 /* now scan the optional arguments */
323 if(++arg_index<argc) {
324 if(0 == strcmp("-d",argv[arg_index])) {
325 if(++arg_index<argc) {
326 Ps.directory = argv[arg_index];
327 if(++arg_index<argc) {
328 goto option_command;
329 } else {
330 return synctex_view_proceed(&Ps);
331 }
332 } else {
333 Ps.directory = getenv("SYNCTEX_BUILD_DIRECTORY");
334 return synctex_view_proceed(&Ps);
335 }
336 }
337 option_command:
338 if(0 == strcmp("-x",argv[arg_index])) {
339 if(++arg_index<argc) {
340 if(strcmp("-",argv[arg_index])) {
341 /* next option does not start with '-', this is a command */
342 Ps.viewer = argv[arg_index];
343 if(++arg_index<argc) {
344 goto option_hint;
345 } else {
346 return synctex_view_proceed(&Ps);
347 }
348 } else {
349 /* retrieve the environment variable */
350 Ps.viewer = getenv("SYNCTEX_VIEWER");
351 goto option_hint;
352 }
353 } else {
354 Ps.viewer = getenv("SYNCTEX_VIEWER");
355 return synctex_view_proceed(&Ps);
356 }
357 }
358 option_hint:
359 if(0 == strcmp("-h",argv[arg_index]) && ++arg_index<argc) {
360 /* modify the argument */;
361 Ps.after = strstr(argv[arg_index],"/");
362 if(NULL != Ps.after) {
363 Ps.before = argv[arg_index];
364 *Ps.after = '\0';
365 ++Ps.after;
366 Ps.offset = (int)strtoul(Ps.after,&Ps.middle,10);
367 if(Ps.middle>Ps.after && strlen(Ps.middle)>2) {
368 Ps.after = strstr(++Ps.middle,"/");
369 if(NULL != Ps.after) {
370 *Ps.after = '\0';
371 if(Ps.offset<strlen(Ps.middle)) {
372 ++Ps.after;
373 return synctex_view_proceed(&Ps);
374 }
375 }
376 }
377 }
378 synctex_help_view("Bad hint");
379 return -1;
380 }
381 }
382 return synctex_view_proceed(&Ps);
383 }
384
385 int synctex_view_proceed(synctex_view_params_t * Ps) {
386 synctex_scanner_p scanner = NULL;
387 size_t size = 0;
388 #if SYNCTEX_DEBUG
389 printf("line:%i\n",Ps->line);
390 printf("column:%i\n",Ps->column);
391 printf("page:%i\n",Ps->page);
392 printf("input:%s\n",Ps->input);
393 printf("viewer:%s\n",Ps->viewer);
394 printf("before:%s\n",Ps->before);
395 printf("offset:%u\n",Ps->offset);
396 printf("middle:%s\n",Ps->middle);
397 printf("after:%s\n",Ps->after);
398 printf("output:%s\n",Ps->output);
399 printf("cwd:%s\n",getcwd(NULL,0));
400 #endif
401 /* We assume that viewer is not so big: */
402 # define SYNCTEX_STR_SIZE 65536
403 if(Ps->viewer && strlen(Ps->viewer)>=SYNCTEX_STR_SIZE) {
404 synctex_help_view("Viewer command is too long");
405 return -1;
406 }
407 scanner = synctex_scanner_new_with_output_file(Ps->output,Ps->directory,1);
408 if(scanner && synctex_display_query(scanner,Ps->input,Ps->line,Ps->column,Ps->page)) {
409 synctex_node_p node = NULL;
410 if((node = synctex_scanner_next_result(scanner)) != NULL) {
411 /* filtering the command */
412 if(Ps->viewer && strlen(Ps->viewer)) {
413 char * viewer = Ps->viewer;
414 char * where = NULL;
415 char * buffer = NULL;
416 char * buffer_cur = NULL;
417 int status = 0;
418 /* Preparing the buffer where everything will be printed */
419 size = strlen(viewer)+3*sizeof(int)+6*sizeof(float)+4*(SYNCTEX_STR_SIZE);
420 buffer = malloc(size+1);
421 if(NULL == buffer) {
422 synctex_help_view("No memory available");
423 return -1;
424 }
425 /* Properly terminate the buffer, no bad access for string related functions. */
426 buffer[size] = '\0';
427 /* Replace %{ by &{, then remove all unescaped '%'*/
428 while((where = strstr(viewer,"%{")) != NULL) {
429 *where = '&';
430 }
431 /* find all the unescaped '%', change to a safe character */
432 where = viewer;
433 while(where && (where = strstr(where,"%"))) {
434 /* Find the next occurrence of a "%",
435 * if it is not followed by another "%",
436 * replace it by a "&" */
437 if(strlen(++where)) {
438 if(*where == '%') {
439 ++where;
440 } else {
441 *(where-1)='&';
442 }
443 }
444 }
445 buffer_cur = buffer;
446 /* find the next occurrence of a format key */
447 where = viewer;
448 while(viewer && (where = strstr(viewer,"&{"))) {
449 # define TEST(KEY,FORMAT,WHAT)\
450 if(!strncmp(where,KEY,strlen(KEY))) {\
451 size_t printed = where-viewer;\
452 if(buffer_cur != memcpy(buffer_cur,viewer,(size_t)printed)) {\
453 synctex_help_view("Memory copy problem");\
454 free(buffer);\
455 return -1;\
456 }\
457 buffer_cur += printed;size-=printed;\
458 printed = snprintf(buffer_cur,size,FORMAT,WHAT);\
459 if((unsigned)printed >= (unsigned)size) {\
460 synctex_help_view("Snprintf problem");\
461 free(buffer);\
462 return -1;\
463 }\
464 buffer_cur += printed;size-=printed;\
465 *buffer_cur='\0';\
466 viewer = where+strlen(KEY);\
467 continue;\
468 }
469 TEST("&{output}","%s",synctex_scanner_get_output(scanner));
470 TEST("&{page}", "%i",synctex_node_page(node)-1);
471 TEST("&{page+1}","%i",synctex_node_page(node));
472 TEST("&{x}", "%f",synctex_node_visible_h(node));
473 TEST("&{y}", "%f",synctex_node_visible_v(node));
474 TEST("&{h}", "%f",synctex_node_box_visible_h(node));
475 TEST("&{v}", "%f",synctex_node_box_visible_v(node)+synctex_node_box_visible_depth(node));
476 TEST("&{width}", "%f",fabs(synctex_node_box_visible_width(node)));
477 TEST("&{height}","%f",fmax(synctex_node_box_visible_height(node)+synctex_node_box_visible_depth(node),1));
478 TEST("&{before}","%s",(Ps->before && strlen(Ps->before)<SYNCTEX_STR_SIZE?Ps->before:""));
479 TEST("&{offset}","%u",Ps->offset);
480 TEST("&{middle}","%s",(Ps->middle && strlen(Ps->middle)<SYNCTEX_STR_SIZE?Ps->middle:""));
481 TEST("&{after}", "%s",(Ps->after && strlen(Ps->after)<SYNCTEX_STR_SIZE?Ps->after:""));
482 # undef TEST
483 break;
484 }
485 /* copy the rest of viewer into the buffer */
486 if(buffer_cur != strncpy(buffer_cur,viewer,size + 1)) {
487 synctex_help_view("Memory copy problem");
488 free(buffer);
489 return -1;
490 }
491 buffer_cur[size] = '\0';
492 printf("SyncTeX: Executing\n%s\n",buffer);
493 status = system(buffer);
494 free(buffer);
495 buffer = NULL;
496 return status;
497 } else {
498 /* just print out the results */
499 puts("SyncTeX result begin");
500 do {
501 printf("Output:%s\n"
502 "Page:%i\n"
503 "x:%f\n"
504 "y:%f\n"
505 "h:%f\n"
506 "v:%f\n"
507 "W:%f\n"
508 "H:%f\n"
509 "before:%s\n"
510 "offset:%i\n"
511 "middle:%s\n"
512 "after:%s\n",
513 Ps->output,
514 synctex_node_page(node),
515 synctex_node_visible_h(node),
516 synctex_node_visible_v(node),
517 synctex_node_box_visible_h(node),
518 synctex_node_box_visible_v(node)+synctex_node_box_visible_depth(node),
519 synctex_node_box_visible_width(node),
520 synctex_node_box_visible_height(node)+synctex_node_box_visible_depth(node),
521 (Ps->before?Ps->before:""),
522 Ps->offset,
523 (Ps->middle?Ps->middle:""),
524 (Ps->after?Ps->after:""));
525 } while((node = synctex_scanner_next_result(scanner)) != NULL);
526 puts("SyncTeX result end");
527 }
528 }
529 synctex_scanner_free(scanner);
530 }
531 return 0;
532 }
533
534 void synctex_help_edit(const char * error,...) {
535 va_list v;
536 va_start(v, error);
537 synctex_usage(error, v);
538 va_end(v);
539 fputs(
540 "synctex edit: backwards or reverse synchronization,\n"
541 "command sent by the viewer to edit the source corresponding to the position under the mouse\n\n"
542 "\n"
543 "usage: synctex edit -o page:x:y:file [-d directory] [-x editor-command] [-h offset:context]\n"
544 "\n"
545 "-o page:x:y:file\n"
546 " specify the page and coordinates of the point under the mouse.\n"
547 " page is 1 based.\n"
548 " Coordinates x and y are counted from the top left corner of the page.\n"
549 " Their unit is the big point (72 dpi).\n"
550 " \n"
551 " file is in general the path of a pdf or dvi file.\n"
552 " It can be either absolute or relative to the current directory.\n"
553 " This named file must always exist.\n"
554 " \n"
555 "-d directory\n"
556 " is the directory containing the synctex file, in case it is different from the directory of the output.\n"
557 " This directory must exist.\n"
558 " An example will explain how things work: for synctex -o ...:bar.tex -d foo,\n"
559 " the chosen synctex file is the most recent among bar.synctex, bar.synctex.gz, foo/bar.synctex and foo/bar.synctex.gz.\n"
560 " The other ones are simply removed, if the authorization is granted\n"
561 " \n"
562 "-x editor-command\n"
563 " Normally the synctex tool outputs its result to the stdout.\n"
564 " It is possible to execute an external tool with the result of the query.\n"
565 " The editor-command is a printf like format string with following specifiers.\n"
566 " They will be replaced by their value before the command is executed.\n"
567 " %{output} is the full path specifier of the output document, with no extension.\n"
568 " %{input} is the name specifier of the input document.\n"
569 " %{line} is the 0 based line number specifier. %{line+1} is the 1 based line number specifier.\n"
570 " %{column} is the 0 based column number specifier or -1. %{column+1} is the 1 based column number or -1.\n"
571 " %{offset} is the 0 based offset specifier and %{context} is the context specifier of the hint.\n"
572 " \n"
573 " If no editor-command is provided, the content of the SYNCTEX_EDITOR environment variable is used instead.\n"
574 " \n"
575 "-h offset:context\n"
576 " This hint allows a backwards or reverse synchronization by contents.\n"
577 " You give a context including the character at the mouse location, and\n"
578 " the offset of this character relative to the beginning of this bunch of text.\n"
579 " \n",
580 (error?stderr:stdout)
581 );
582 return;
583 }
584
585 typedef struct {
586 int page;
587 float x;
588 float y;
589 unsigned int offset;
590 char * output;
591 char * directory;
592 char * editor;
593 char * context;
594 } synctex_edit_params_t;
595
596 int synctex_edit_proceed(synctex_edit_params_t * Ps);
597
598 /* "usage: synctex edit -o page:x:y:output [-d directory] [-x editor-command] [-h offset:context]\n" */
599 int synctex_edit(int argc, char *argv[]) {
600 int arg_index = 0;
601 char * start = NULL;
602 char * end = NULL;
603 synctex_edit_params_t Ps = {0,0,0,0,NULL,NULL,NULL,NULL};
604 /* required */
605 if((arg_index>=argc) || strcmp("-o",argv[arg_index]) || (++arg_index>=argc)) {
606 synctex_help_edit("Missing -o required argument");
607 return -1;
608 }
609 start = argv[arg_index];
610 Ps.page = (int)strtol(start,&end,10);
611 if(end>start && strlen(end)>1 && *end==':') {
612 start = end+1;
613 Ps.x = strtod(start,&end);
614 if(end>start && strlen(end)>1 && *end==':') {
615 start = end+1;
616 Ps.y = strtod(start,&end);
617 if(end>start && strlen(end)>1 && *end==':') {
618 Ps.output = ++end;
619 goto scan_execute;
620 }
621 }
622 }
623 synctex_help_edit("Bad -o argument");
624 return -1;
625 scan_execute:
626 /* now scan the optional arguments */
627 if(++arg_index<argc) {
628 if(0 == strcmp("-d",argv[arg_index])) {
629 if(++arg_index<argc) {
630 Ps.directory = argv[arg_index];
631 if(++arg_index<argc) {
632 goto option_command;
633 } else {
634 return synctex_edit_proceed(&Ps);
635 }
636 } else {
637 Ps.directory = getenv("SYNCTEX_BUILD_DIRECTORY");
638 return synctex_edit_proceed(&Ps);
639 }
640 }
641 option_command:
642 if(0 == strcmp("-x",argv[arg_index])) {
643 if(++arg_index<argc) {
644 if(strcmp("-",argv[arg_index])) {
645 /* next option does not start with '-', this is a command */
646 Ps.editor = argv[arg_index];
647 if(++arg_index<argc) {
648 goto option_hint;
649 } else {
650 return synctex_edit_proceed(&Ps);
651 }
652 } else {
653 /* retrieve the environment variable */
654 Ps.editor = getenv("SYNCTEX_EDITOR");
655 goto option_hint;
656 }
657 } else {
658 Ps.editor = getenv("SYNCTEX_EDITOR");
659 return synctex_edit_proceed(&Ps);
660 }
661 }
662 option_hint:
663 if(0 == strcmp("-h",argv[arg_index]) && ++arg_index<argc) {
664
665 start = argv[arg_index];
666 end = NULL;
667 Ps.offset = (int)strtol(start,&end,10);
668 if(end>start && strlen(end)>1 && *end==':') {
669 Ps.context = end+1;
670 return synctex_edit_proceed(&Ps);
671 }
672 synctex_help_edit("Bad -h argument");
673 return -1;
674 }
675 }
676 return synctex_edit_proceed(&Ps);
677 }
678
679 int synctex_edit_proceed(synctex_edit_params_t * Ps) {
680 synctex_scanner_p scanner = NULL;
681 #if SYNCTEX_DEBUG
682 printf("page:%i\n",Ps->page);
683 printf("x:%f\n",Ps->x);
684 printf("y:%f\n",Ps->y);
685 printf("almost output:%s\n",Ps->output);
686 printf("editor:%s\n",Ps->editor);
687 printf("offset:%u\n",Ps->offset);
688 printf("context:%s\n",Ps->context);
689 printf("cwd:%s\n",getcwd(NULL,0));
690 #endif
691 scanner = synctex_scanner_new_with_output_file(Ps->output,Ps->directory,1);
692 if(NULL == scanner) {
693 synctex_help_edit("No SyncTeX available for %s",Ps->output);
694 return -1;
695 }
696 if(synctex_edit_query(scanner,Ps->page,Ps->x,Ps->y)) {
697 synctex_node_p node = NULL;
698 const char * input = NULL;
699 if(NULL != (node = synctex_scanner_next_result(scanner))
700 && NULL != (input = synctex_scanner_get_name(scanner,synctex_node_tag(node)))) {
701 /* filtering the command */
702 if(Ps->editor && strlen(Ps->editor)) {
703 size_t size = 0;
704 char * where = NULL;
705 char * buffer = NULL;
706 char * buffer_cur = NULL;
707 int status;
708 size = strlen(Ps->editor)+3*sizeof(int)+3*SYNCTEX_STR_SIZE;
709 buffer = malloc(size+1);
710 if(NULL == buffer) {
711 printf("SyncTeX ERROR: No memory available\n");
712 return -1;
713 }
714 buffer[size]='\0';
715 /* Replace %{ by &{, then remove all unescaped '%'*/
716 while((where = strstr(Ps->editor,"%{")) != NULL) {
717 *where = '&';
718 }
719 where = Ps->editor;
720 while(where &&(where = strstr(where,"%"))) {
721 if(strlen(++where)) {
722 if(*where == '%') {
723 ++where;
724 } else {
725 *(where-1)='&';
726 }
727 }
728 }
729 buffer_cur = buffer;
730 /* find the next occurrence of a format key */
731 where = Ps->editor;
732 while(Ps->editor && (where = strstr(Ps->editor,"&{"))) {
733 # define TEST(KEY,FORMAT,WHAT)\
734 if(!strncmp(where,KEY,strlen(KEY))) {\
735 size_t printed = where-Ps->editor;\
736 if(buffer_cur != memcpy(buffer_cur,Ps->editor,(size_t)printed)) {\
737 synctex_help_edit("Memory copy problem");\
738 free(buffer);\
739 return -1;\
740 }\
741 buffer_cur += printed;size-=printed;\
742 printed = snprintf(buffer_cur,size,FORMAT,WHAT);\
743 if((unsigned)printed >= (unsigned)size) {\
744 synctex_help_edit("Snprintf problem");\
745 free(buffer);\
746 return -1;\
747 }\
748 buffer_cur += printed;size-=printed;\
749 *buffer_cur='\0';\
750 Ps->editor = where+strlen(KEY);\
751 continue;\
752 }
753 TEST("&{output}", "%s",Ps->output);
754 TEST("&{input}", "%s",input);
755 TEST("&{line}", "%i",synctex_node_line(node));
756 TEST("&{column}", "%i",-1);
757 TEST("&{offset}", "%u",Ps->offset);
758 TEST("&{context}","%s",Ps->context);
759 # undef TEST
760 break;
761 }
762 /* copy the rest of editor into the buffer */
763 if(buffer_cur != memcpy(buffer_cur,Ps->editor,strlen(Ps->editor))) {
764 fputs("! synctex_edit: Memory copy problem",stderr);
765 free(buffer);
766 return -1;
767 }\
768 printf("SyncTeX: Executing\n%s\n",buffer);
769 status = system(buffer);
770 free(buffer);
771 buffer = NULL;
772 return status;
773 } else {
774 /* just print out the results */
775 puts("SyncTeX result begin");
776 do {
777 printf( "Output:%s\n"
778 "Input:%s\n"
779 "Line:%i\n"
780 "Column:%i\n"
781 "Offset:%i\n"
782 "Context:%s\n",
783 Ps->output,
784 input,
785 synctex_node_line(node),
786 synctex_node_column(node),
787 Ps->offset,
788 (Ps->context?Ps->context:""));
789 } while((node = synctex_scanner_next_result(scanner)) != NULL);
790 puts("SyncTeX result end");
791 }
792 }
793 }
794 synctex_scanner_free(scanner);
795 return 0;
796 }
797
798 void synctex_help_update(const char * error,...) {
799 va_list v;
800 va_start(v, error);
801 synctex_usage(error, v);
802 va_end(v);
803 fputs(
804 "synctex update: up to date synctex file,\n"
805 "Use this command to update the synctex file once a dvi/xdv to pdf filter is applied.\n\n"
806 "\n"
807 "usage: synctex update -o output [-d directory] [-m number] [-x dimension] [-y dimension]\n"
808 "\n"
809 "-o output is the full or relative path of an existing file,\n"
810 " either the real synctex file you wish to update\n"
811 " or a related file: foo.tex, foo.pdf, foo.dvi...\n"
812 "-d directory is the directory containing the synctex file, in case it is different from the directory of the output.\n"
813 " This directory must exist.\n"
814 " An example will explain how things work: for synctex -o ...:bar.tex -d foo,\n"
815 " the chosen synctex file is the most recent among bar.synctex, bar.synctex.gz, foo/bar.synctex and foo/bar.synctex.gz.\n"
816 " The other ones are simply removed, if the authorization is granted\n"
817 " \n"
818 "-m number Set additional magnification\n"
819 "-x dimension Set horizontal offset\n"
820 "-y dimension Set vertical offset\n"
821 "In general, these are exactly the same options provided to the dvi/xdv to pdf filter.\n",
822 (error?stderr:stdout)
823 );
824 return;
825 }
826
827 /* "usage: synctex update -o output [-d directory] [-m number] [-x dimension] [-y dimension]\n" */
828 int synctex_update(int argc, char *argv[]) {
829 int arg_index = 0;
830 synctex_updater_p updater = NULL;
831 char * magnification = NULL;
832 char * x = NULL;
833 char * y = NULL;
834 char * output = NULL;
835 char * directory = NULL;
836 #define SYNCTEX_fprintf (*synctex_fprintf)
837 if(arg_index>=argc) {
838 synctex_help_update("Bad update command");
839 return -1;
840 }
841 /* required */
842 if((arg_index>=argc) || strcmp("-o",argv[arg_index]) || (++arg_index>=argc)) {
843 synctex_help_update("Missing -o required argument");
844 return -1;
845 }
846 output = argv[arg_index];
847 if(++arg_index>=argc) {
848 return 0;
849 }
850 next_argument:
851 if(0 == strcmp("-m",argv[arg_index])) {
852 if(++arg_index>=argc) {
853 synctex_help_update("Missing magnification");
854 return -1;
855 }
856 magnification = argv[arg_index];
857 prepare_next_argument:
858 if(++arg_index<argc) {
859 goto next_argument;
860 }
861 } else if(0 == strcmp("-x",argv[arg_index])) {
862 if(++arg_index>=argc) {
863 synctex_help_update("Missing x offset");
864 return -1;
865 }
866 x = argv[arg_index];
867 goto prepare_next_argument;
868 } else if(0 == strcmp("-y",argv[arg_index])) {
869 if(++arg_index>=argc) {
870 synctex_help_update("Missing y offset");
871 return -1;
872 }
873 y = argv[arg_index];
874 goto prepare_next_argument;
875 } else if(0 == strcmp("-d",argv[arg_index])) {
876 if(++arg_index<argc) {
877 directory = argv[arg_index];
878 } else {
879 directory = getenv("SYNCTEX_BUILD_DIRECTORY");
880 }
881 goto prepare_next_argument;
882 }
883
884 /* Arguments parsed */
885 updater = synctex_updater_new_with_output_file(output,directory);
886 synctex_updater_append_magnification(updater,magnification);
887 synctex_updater_append_x_offset(updater,x);
888 synctex_updater_append_y_offset(updater,y);
889 synctex_updater_free(updater);
890 return 0;
891 }
892
893 int synctex_test_file (int argc, char *argv[]);
894
895 /* "usage: synctex test subcommand options\n" */
896 int synctex_test(int argc, char *argv[]) {
897 if(argc) {
898 if(0==strcmp("file",argv[0])) {
899 return synctex_test_file(argc-1,argv+1);
900 }
901 }
902 return 0;
903 }
904
905 int synctex_test_file (int argc, char *argv[])
906 {
907 int arg_index = 0;
908 char * output = NULL;
909 char * directory = NULL;
910 char * synctex_name = NULL;
911 synctex_io_mode_t mode = 0;
912 if(arg_index>=argc) {
913 _synctex_error("! usage: synctex test file -o output [-d directory]\n");
914 return -1;
915 }
916 /* required */
917 if((arg_index>=argc) || strcmp("-o",argv[arg_index]) || (++arg_index>=argc)) {
918 _synctex_error("! usage: synctex test file -o output [-d directory]\n");
919 return -1;
920 }
921 output = argv[arg_index];
922 /* optional */
923 if(++arg_index<argc) {
924 if(0 == strcmp("-d",argv[arg_index])) {
925 if(++arg_index<argc) {
926 directory = argv[arg_index];
927 } else {
928 directory = getenv("SYNCTEX_BUILD_DIRECTORY");
929 }
930 }
931 }
932 /* Arguments parsed */
933 if(_synctex_get_name(output, directory, &synctex_name, &mode)) {
934 _synctex_error("! TEST FAILED\n");
935 } else {
936 printf("output:%s\n"
937 "directory:%s\n"
938 "file name:%s\n"
939 "io mode:%s\n",
940 output,
941 directory,
942 synctex_name,
943 _synctex_get_io_mode_name(mode));
944 }
945 return 0;
946 }