Debian Codegraph @

texk/web2c/mplibdir/mpost.w

raw
1 % This file is part of MetaPost;
2 % the MetaPost program is in the public domain.
3 % See the <Show version...> code below for more info.
4
5 \font\tenlogo=logo10 % font used for the METAFONT logo
6 \def\MP{{\tenlogo META}\-{\tenlogo POST}}
7 \def\MF{{\tenlogo META}\-{\tenlogo FONT}}
8 \def\MP{{\tenlogo META}\-{\tenlogo POST}}
9 \def\pct!{{\char`\%}} % percent sign in ordinary text
10 \def\psqrt#1{\sqrt{\mathstrut#1}}
11
12
13 \def\title{MetaPost executable}
14 \def\[#1]{#1.}
15 \pdfoutput=1
16
17 @s line normal
18
19 @s MP int
20 @s MPX int
21 @s MP_options int
22 @s boolean int
23 @s const_string int
24 @s mpx_options int
25 @s option int
26 @s string int
27 @s timeb int
28 @s timeval int
29
30 @*\MP\ executable.
31
32 Now that all of \MP\ is a library, a separate program is needed to
33 have our customary command-line interface.
34
35 @ First, here are the \CEE/ includes.
36
37 @c
38 #include "mpconfig.h"
39 #include <w2c/config.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #if defined (HAVE_SYS_TIME_H)
44 #include <sys/time.h>
45 #elif defined (HAVE_SYS_TIMEB_H)
46 #include <sys/timeb.h>
47 #endif
48 #include <time.h> /* For `|struct tm|'. Moved here for Visual Studio 2005. */
49 #if HAVE_SYS_STAT_H
50 #include <sys/stat.h>
51 #endif
52 #include <mplib.h>
53 #include <mpxout.h>
54 #include <kpathsea/kpathsea.h>
55 @= /*@@null@@*/ @> static char *mpost_tex_program = NULL;
56 static int debug = 0; /* debugging for \.{makempx} */
57 static int nokpse = 0;
58 static boolean recorder_enabled = false;
59 static string recorder_name = NULL;
60 static FILE *recorder_file = NULL;
61 static char *job_name = NULL;
62 static char *job_area = NULL;
63 static int dvitomp_only = 0;
64 static int ini_version_test = false;
65 string output_directory; /* Defaults to |NULL|. */
66 static boolean restricted_mode = false;
67
68 @<Structures for |getopt|@>@;
69 @<Declarations@>@;
70
71 @ Allocating a bit of memory, with error detection:
72
73 @d mpost_xfree(A) do { if (A!=NULL) free(A); A=NULL; } while (0)
74
75 @c
76 @= /*@@only@@*/ /*@@out@@*/ @> static void *mpost_xmalloc (size_t bytes) {
77 void *w = malloc (bytes);
78 if (w==NULL) {
79 fprintf(stderr,"Out of memory!\n");
80 exit(EXIT_FAILURE);
81 }
82 return w;
83 }
84 @= /*@@only@@*/ @> static char *mpost_xstrdup(const char *s) {
85 char *w;
86 w = strdup(s);
87 if (w==NULL) {
88 fprintf(stderr,"Out of memory!\n");
89 exit(EXIT_FAILURE);
90 }
91 return w;
92 }
93 static char *mpost_itoa (int i) {
94 char res[32] ;
95 unsigned idx = 30;
96 unsigned v = (unsigned)abs(i);
97 memset(res,0,32*sizeof(char));
98 while (v>=10) {
99 char d = (char)(v % 10);
100 v = v / 10;
101 res[idx--] = d + '0';
102 }
103 res[idx--] = (char)v + '0';
104 if (i<0) {
105 res[idx--] = '-';
106 }
107 return mpost_xstrdup(res+idx+1);
108 }
109 static char *mpost_i64toa (integer64 i) {
110 char res[32] ;
111 unsigned idx = 30;
112 integer64 v = 0;
113
114 if (i==INT64_MIN){
115 return strdup("-9223372036854775808\0");
116 } else {
117 v = i>=0?i:-i;
118 }
119
120 memset(res,0,32*sizeof(char));
121 while (v>=10) {
122 char d = (char)(v % 10);
123 v = v / 10;
124 res[idx--] = d + '0';
125 }
126 res[idx--] = (char)v + '0';
127 if (i<0) {
128 res[idx--] = '-';
129 }
130 return strdup(res+idx+1);
131 }
132
133
134
135
136
137 @ @c
138 #ifdef WIN32
139 static int
140 Isspace (char c)
141 {
142 return (c == ' ' || c == '\t');
143 }
144 #endif
145 static void mpost_run_editor (MP mp, char *fname, integer64 fline) {
146 char *temp, *command, *fullcmd, *edit_value;
147 char c;
148 boolean sdone, ddone;
149
150 #ifdef WIN32
151 char *fp, *ffp, *env, editorname[256], buffer[256];
152 int cnt = 0;
153 int dontchange = 0;
154 #endif
155
156 if (restricted_mode)
157 return;
158
159 sdone = ddone = false;
160 edit_value = kpse_var_value ("MPEDIT");
161 if (edit_value == NULL)
162 edit_value = getenv("EDITOR");
163 if (edit_value == NULL) {
164 fprintf (stderr,"call_edit: can't find a suitable MPEDIT or EDITOR variable\n");
165 exit(mp_status(mp));
166 }
167 command = (string) mpost_xmalloc (strlen (edit_value) + strlen(fname) + 11 + 3);
168 temp = command;
169
170 #ifdef WIN32
171 fp = editorname;
172 if ((isalpha(*edit_value) && *(edit_value + 1) == ':'
173 && IS_DIR_SEP (*(edit_value + 2)))
174 || (*edit_value == '"' && isalpha(*(edit_value + 1))
175 && *(edit_value + 2) == ':'
176 && IS_DIR_SEP (*(edit_value + 3)))
177 )
178 dontchange = 1;
179 #endif
180
181 while ((c = *edit_value++) != (char)0) {
182 if (c == '%') {
183 switch (c = *edit_value++) {
184 case 'd':
185 if (ddone) {
186 fprintf (stderr,"call_edit: `%%d' appears twice in editor command\n");
187 exit(EXIT_FAILURE);
188 } else {
189 char *s = mpost_i64toa(fline);
190 char *ss = s;
191 if (s != NULL) {
192 while (*s != '\0')
193 *temp++ = *s++;
194 free(ss);
195 }
196 ddone = true;
197 }
198 break;
199 case 's':
200 if (sdone) {
201 fprintf (stderr,"call_edit: `%%s' appears twice in editor command\n");
202 exit(EXIT_FAILURE);
203 } else {
204 while (*fname != '\0')
205 *temp++ = *fname++;
206 *temp++ = '.';
207 *temp++ = 'm';
208 *temp++ = 'p';
209 sdone = true;
210 }
211 break;
212 case '\0':
213 *temp++ = '%';
214 /* Back up to the |NULL| to force termination. */
215 edit_value--;
216 break;
217 default:
218 *temp++ = '%';
219 *temp++ = c;
220 break;
221 }
222 } else {
223 #ifdef WIN32
224 if (dontchange)
225 *temp++ = c;
226 else { if(Isspace(c) && cnt == 0) {
227 cnt++;
228 temp = command;
229 *temp++ = c;
230 *fp = '\0';
231 } else if(!Isspace(c) && cnt == 0) {
232 *fp++ = c;
233 } else {
234 *temp++ = c;
235 }
236 }
237 #else
238 *temp++ = c;
239 #endif
240 }
241 }
242 *temp = '\0';
243
244 #ifdef WIN32
245 if (dontchange == 0) {
246 if(editorname[0] == '.' ||
247 editorname[0] == '/' ||
248 editorname[0] == '\\') {
249 fprintf(stderr, "%s is not allowed to execute.\n", editorname);
250 exit(EXIT_FAILURE);
251 }
252 env = (char *)getenv("PATH");
253 if(SearchPath(env, editorname, ".exe", 256, buffer, &ffp)==0) {
254 if(SearchPath(env, editorname, ".bat", 256, buffer, &ffp)==0) {
255 fprintf(stderr, "I cannot find %s in the PATH.\n", editorname);
256 exit(EXIT_FAILURE);
257 }
258 }
259 fullcmd = mpost_xmalloc(strlen(buffer)+strlen(command)+5);
260 strcpy(fullcmd, "\"");
261 strcat(fullcmd, buffer);
262 strcat(fullcmd, "\"");
263 strcat(fullcmd, command);
264 } else
265 #endif
266 fullcmd = command;
267
268 if (system (fullcmd) != 0)
269 fprintf (stderr, "! Trouble executing `%s'.\n", command);
270 exit(EXIT_FAILURE);
271 }
272
273 @
274 @<Register the callback routines@>=
275 options->run_editor = mpost_run_editor;
276
277 @
278 @c
279 static string normalize_quotes (const char *name, const char *mesg) {
280 boolean quoted = false;
281 boolean must_quote = (strchr(name, ' ') != NULL);
282 /* Leave room for quotes and |NULL|. */
283 string ret = (string)mpost_xmalloc(strlen(name)+3);
284 string p;
285 const_string q;
286 p = ret;
287 if (must_quote)
288 *p++ = '"';
289 for (q = name; *q != '\0'; q++) {
290 if (*q == '"')
291 quoted = !quoted;
292 else
293 *p++ = *q;
294 }
295 if (must_quote)
296 *p++ = '"';
297 *p = '\0';
298 if (quoted) {
299 fprintf(stderr, "! Unbalanced quotes in %s %s\n", mesg, name);
300 exit(EXIT_FAILURE);
301 }
302 return ret;
303 }
304
305 @ Helpers for the filename recorder.
306
307 @<Declarations@>=
308 void recorder_start(char *jobname);
309
310 @ @c
311 void recorder_start(char *jobname) {
312 char cwd[1024];
313 if (jobname==NULL) {
314 recorder_name = mpost_xstrdup("mpout.fls");
315 } else {
316 recorder_name = (string)xmalloc((unsigned int)(strlen(jobname)+5));
317 strcpy(recorder_name, jobname);
318 strcat(recorder_name, ".fls");
319 }
320 recorder_file = xfopen(recorder_name, FOPEN_W_MODE);
321
322 if(getcwd(cwd,1020) != NULL) {
323 #ifdef WIN32
324 char *p;
325 for (p = cwd; *p; p++) {
326 if (*p == '\\')
327 *p = '/';
328 else if (IS_KANJI(p))
329 p++;
330 }
331 #endif
332 fprintf(recorder_file, "PWD %s\n", cwd);
333 } else {
334 fprintf(recorder_file, "PWD <unknown>\n");
335 }
336 }
337
338
339 @ @c
340 @= /*@@null@@*/ @> static char *makempx_find_file (MPX mpx, const char *nam,
341 const char *mode, int ftype) {
342 int fmt;
343 boolean req;
344 (void) mpx;
345 if ((mode[0]=='r' && !kpse_in_name_ok(nam)) ||
346 (mode[0]=='w' && !kpse_out_name_ok(nam)))
347 return NULL; /* disallowed filename */
348 if (mode[0] != 'r') {
349 return strdup(nam);
350 }
351 req = true; fmt = -1;
352 switch(ftype) {
353 case mpx_tfm_format: fmt = kpse_tfm_format; break;
354 case mpx_vf_format: fmt = kpse_vf_format; req = false; break;
355 case mpx_trfontmap_format: fmt = kpse_mpsupport_format; break;
356 case mpx_trcharadj_format: fmt = kpse_mpsupport_format; break;
357 case mpx_desc_format: fmt = kpse_troff_font_format; break;
358 case mpx_fontdesc_format: fmt = kpse_troff_font_format; break;
359 case mpx_specchar_format: fmt = kpse_mpsupport_format; break;
360 }
361 if (fmt<0) return NULL;
362 return kpse_find_file (nam, fmt, req);
363 }
364
365 @ Invoke \.{makempx} (or \.{troffmpx}) to make sure there is an
366 up-to-date \.{.mpx} file for a given \.{.mp} file. (Original
367 from John Hobby 3/14/90)
368
369 @d default_args " --parse-first-line --interaction=nonstopmode"
370 @d TEX "tex"
371 @d TROFF "soelim | eqn -Tps -d$$ | troff -Tps"
372
373 @c
374 #ifndef MPXCOMMAND
375 #define MPXCOMMAND "makempx"
376 #endif
377 static int mpost_run_make_mpx (MP mp, char *mpname, char *mpxname) {
378 int ret;
379 char *cnf_cmd = kpse_var_value ("MPXCOMMAND");
380 if (restricted_mode) {
381 /* In the restricted mode, just return success */
382 return 0;
383 }
384 if (cnf_cmd != NULL && (strcmp (cnf_cmd, "0")==0)) {
385 /* If they turned off this feature, just return success. */
386 ret = 0;
387 } else {
388 /* We will invoke something. Compile-time default if nothing else. */
389 char *cmd, *tmp, *qmpname, *qmpxname;
390 if (job_area != NULL) {
391 char *l = mpost_xmalloc(strlen(mpname)+strlen(job_area)+1);
392 strcpy(l, job_area);
393 strcat(l, mpname);
394 tmp = normalize_quotes(l, "mpname");
395 mpost_xfree(l);
396 } else {
397 tmp = normalize_quotes(mpname, "mpname");
398 }
399 if (!kpse_in_name_ok(tmp))
400 return 0; /* disallowed filename */
401 qmpname = kpse_find_file (tmp,kpse_mp_format, true);
402 mpost_xfree(tmp);
403 if (qmpname != NULL && job_area != NULL) {
404 /* if there is a usable mpx file in the source path already,
405 simply use that and return true */
406 char *l = mpost_xmalloc(strlen(qmpname)+2);
407 strcpy(l, qmpname);
408 strcat(l, "x");
409 qmpxname = l;
410 if (qmpxname) {
411 #if HAVE_SYS_STAT_H
412 struct stat source_stat, target_stat;
413 int nothingtodo = 0;
414 if ((stat(qmpxname, &target_stat) >= 0) &&
415 (stat(qmpname, &source_stat) >= 0)) {
416 #if HAVE_ST_MTIM
417 if (source_stat.st_mtim.tv_sec < target_stat.st_mtim.tv_sec ||
418 (source_stat.st_mtim.tv_sec == target_stat.st_mtim.tv_sec &&
419 source_stat.st_mtim.tv_nsec < target_stat.st_mtim.tv_nsec))
420 nothingtodo = 1;
421 #else
422 if (source_stat.st_mtime < target_stat.st_mtime)
423 nothingtodo = 1;
424 #endif
425 }
426 if (nothingtodo == 1)
427 return 1; /* success ! */
428 #endif
429 }
430 }
431 qmpxname = normalize_quotes(mpxname, "mpxname");
432 if (cnf_cmd!=NULL && (strcmp (cnf_cmd, "1")!=0)) {
433 if (mp_troff_mode(mp)!=0)
434 cmd = concatn (cnf_cmd, " -troff ",
435 qmpname, " ", qmpxname, NULL);
436 else if (mpost_tex_program!=NULL && *mpost_tex_program != '\0')
437 cmd = concatn (cnf_cmd, " -tex=", mpost_tex_program, " ",
438 qmpname, " ", qmpxname, NULL);
439 else
440 cmd = concatn (cnf_cmd, " -tex ", qmpname, " ", qmpxname, NULL);
441
442 /* Run it. */
443 ret = system (cmd);
444 free (cmd);
445 mpost_xfree(qmpname);
446 mpost_xfree(qmpxname);
447 } else {
448 mpx_options * mpxopt;
449 char *s = NULL;
450 char *maincmd = NULL;
451 int mpxmode = mp_troff_mode(mp);
452 char *mpversion = mp_metapost_version () ;
453 mpxopt = mpost_xmalloc(sizeof(mpx_options));
454 if (mpost_tex_program != NULL && *mpost_tex_program != '\0') {
455 maincmd = mpost_xstrdup(mpost_tex_program);
456 } else {
457 if (mpxmode == mpx_tex_mode) {
458 s = kpse_var_value("TEX");
459 if (s==NULL) s = kpse_var_value("MPXMAINCMD");
460 if (s==NULL) s = mpost_xstrdup (TEX);
461 maincmd = (char *)mpost_xmalloc (strlen(s)+strlen(default_args)+1);
462 strcpy(maincmd,s);
463 strcat(maincmd,default_args);
464 free(s);
465 } else {
466 s = kpse_var_value("TROFF");
467 if (s==NULL) s = kpse_var_value("MPXMAINCMD");
468 if (s==NULL) s = mpost_xstrdup (TROFF);
469 maincmd = s;
470 }
471 }
472 mpxopt->mode = mpxmode;
473 mpxopt->cmd = maincmd;
474 mpxopt->mptexpre = kpse_var_value("MPTEXPRE");
475 mpxopt->debug = debug;
476 mpxopt->mpname = qmpname;
477 mpxopt->mpxname = qmpxname;
478 mpxopt->find_file = makempx_find_file;
479 {
480 const char *banner = "% Written by metapost version ";
481 mpxopt->banner = mpost_xmalloc(strlen(mpversion)+strlen(banner)+1);
482 strcpy (mpxopt->banner, banner);
483 strcat (mpxopt->banner, mpversion);
484 }
485 ret = mpx_makempx(mpxopt);
486 mpost_xfree(mpxopt->cmd);
487 mpost_xfree(mpxopt->mptexpre);
488 mpost_xfree(mpxopt->banner);
489 mpost_xfree(mpxopt->mpname);
490 mpost_xfree(mpxopt->mpxname);
491 mpost_xfree(mpxopt);
492 mpost_xfree(mpversion);
493 }
494 }
495
496 mpost_xfree (cnf_cmd);
497 return (int)(ret == 0);
498 }
499
500 static int mpost_run_dvitomp (char *dviname, char *mpxname) {
501 int ret;
502 size_t i;
503 char *m, *d;
504 mpx_options * mpxopt;
505 char *mpversion = mp_metapost_version () ;
506 mpxopt = mpost_xmalloc(sizeof(mpx_options));
507 memset(mpxopt,0,sizeof(mpx_options));
508 mpxopt->mode = mpx_tex_mode;
509 if (dviname == NULL)
510 return EXIT_FAILURE;
511 i = strlen(dviname);
512 if (mpxname==NULL) {
513 m = mpost_xstrdup(dviname);
514 if (i>4 && *(m+i-4)=='.'
515 && *(m+i-3)=='d' && *(m+i-2)=='v' && *(m+i-1)=='i')
516 *(m+i-4)='\0' ;
517 } else {
518 m = mpost_xstrdup(mpxname);
519 }
520 d = mpost_xstrdup(dviname);
521 if (!(i>4 && *(d+i-4)=='.'
522 && *(d+i-3)=='d' && *(d+i-2)=='v' && *(d+i-1)=='i')) {
523 char *s = malloc (i+5);
524 memset(s,0,i+5);
525 s = strcat(s, d);
526 (void)strcat(s+i-1, ".dvi");
527 mpost_xfree (d);
528 d = s ;
529 }
530
531 i = strlen(m);
532 if (i>4 && *(m+i-4)=='.'
533 && *(m+i-3)=='m' && *(m+i-2)=='p' && *(m+i-1)=='x') {
534 } else {
535 char *s = malloc (i+5);
536 memset(s,0,i+5);
537 s = strcat(s, m);
538 (void)strcat(s+i-1, ".mpx");
539 mpost_xfree (m);
540 m = s ;
541 }
542 if (!(kpse_in_name_ok(d) && kpse_out_name_ok(m)))
543 return EXIT_FAILURE; /* disallowed filename */
544 mpxopt->mpname = d;
545 mpxopt->mpxname = m;
546
547 mpxopt->find_file = makempx_find_file;
548 {
549 const char *banner = "% Written by dvitomp version ";
550 mpxopt->banner = mpost_xmalloc(strlen(mpversion)+strlen(banner)+1);
551 strcpy (mpxopt->banner, banner);
552 strcat (mpxopt->banner, mpversion);
553 }
554 ret = mpx_run_dvitomp(mpxopt);
555 mpost_xfree(mpxopt->banner);
556 mpost_xfree(mpxopt);
557 mpost_xfree(mpversion);
558 puts(""); /* nicer in case of error */
559 return ret;
560 }
561
562
563 @
564 @<Register the callback routines@>=
565 if (!nokpse)
566 options->run_make_mpx = mpost_run_make_mpx;
567
568
569 @ @c
570 static int get_random_seed (void) {
571 int ret = 0;
572 #if defined (HAVE_GETTIMEOFDAY)
573 struct timeval tv;
574 gettimeofday(&tv, NULL);
575 ret = (int)(tv.tv_usec + 1000000 * tv.tv_usec);
576 #elif defined (HAVE_FTIME)
577 struct timeb tb;
578 ftime(&tb);
579 ret = (tb.millitm + 1000 * tb.time);
580 #else
581 time_t clock = time ((time_t*)NULL);
582 struct tm *tmptr = localtime(&clock);
583 if (tmptr!=NULL)
584 ret = (tmptr->tm_sec + 60*(tmptr->tm_min + 60*tmptr->tm_hour));
585 #endif
586 return ret;
587 }
588
589 @ @<Register the callback routines@>=
590 options->random_seed = get_random_seed();
591
592
593 @ Handle -output-directory.
594
595 @c
596 static char *mpost_find_in_output_directory(const char *s,const char *fmode)
597 { (void)fmode;
598 if (output_directory && !kpse_absolute_p(s, false)) {
599 char *ftemp = concat3(output_directory, DIR_SEP_STRING, s);
600 return ftemp;
601 }
602 return NULL;
603 }
604
605
606
607 @ @c
608 static char *mpost_find_file(MP mp, const char *fname, const char *fmode, int ftype) {
609 size_t l ;
610 char *s;
611 char *ofname;
612 (void)mp;
613 s = NULL;
614 ofname = NULL ;
615
616
617 if (fname == NULL || (fmode[0]=='r' && !kpse_in_name_ok(fname)) )
618 return NULL; /* disallowed filename */
619
620
621 if (fmode[0]=='w') {
622 if (output_directory) {
623 ofname = mpost_find_in_output_directory(fname,fmode);
624 if (ofname == NULL || (fmode[0]=='w' && !kpse_out_name_ok(ofname))) {
625 mpost_xfree(ofname);
626 return NULL; /* disallowed filename */
627 }
628 } else {
629 if (!kpse_out_name_ok(fname))
630 return NULL; /* disallowed filename */
631 }
632 }
633
634
635 if (fmode[0]=='r') {
636 if ((job_area != NULL) &&
637 (ftype>=mp_filetype_text || ftype==mp_filetype_program )) {
638 char *f = mpost_xmalloc(strlen(job_area)+strlen(fname)+1);
639 strcpy(f,job_area);
640 strcat(f,fname);
641 if (ftype>=mp_filetype_text) {
642 s = kpse_find_file (f, kpse_mp_format, 0);
643 } else {
644 l = strlen(f);
645 if (l>3 && strcmp(f+l-3,".mf")==0) {
646 s = kpse_find_file (f,kpse_mf_format, 0);
647 #if HAVE_SYS_STAT_H
648 } else if (l>4 && strcmp(f+l-4,".mpx")==0) {
649 struct stat source_stat, target_stat;
650 char *mpname = mpost_xstrdup(f);
651 *(mpname + strlen(mpname) -1 ) = '\0';
652 if ((stat(f, &target_stat) >= 0) &&
653 (stat(mpname, &source_stat) >= 0)) {
654 #if HAVE_ST_MTIM
655 if (source_stat.st_mtim.tv_sec <= target_stat.st_mtim.tv_sec ||
656 (source_stat.st_mtim.tv_sec == target_stat.st_mtim.tv_sec &&
657 source_stat.st_mtim.tv_nsec <= target_stat.st_mtim.tv_nsec))
658 s = mpost_xstrdup(f);
659 #else
660 if (source_stat.st_mtime <= target_stat.st_mtime)
661 s = mpost_xstrdup(f);
662 #endif
663 }
664 mpost_xfree(mpname);
665 #endif
666 } else {
667 s = kpse_find_file (f,kpse_mp_format, 0);
668 }
669 }
670 mpost_xfree(f);
671 if (s!=NULL) {
672 return s;
673 }
674 }
675 if (ftype>=mp_filetype_text) {
676 s = kpse_find_file (fname, kpse_mp_format, 0);
677 } else {
678 switch(ftype) {
679 case mp_filetype_program:
680 l = strlen(fname);
681 if (l>3 && strcmp(fname+l-3,".mf")==0) {
682 s = kpse_find_file (fname, kpse_mf_format, 0);
683 } else {
684 s = kpse_find_file (fname, kpse_mp_format, 0);
685 }
686 break;
687 case mp_filetype_memfile:
688 s = kpse_find_file (fname, kpse_mem_format, 1);
689 break;
690 case mp_filetype_metrics:
691 s = kpse_find_file (fname, kpse_tfm_format, 0);
692 break;
693 case mp_filetype_fontmap:
694 s = kpse_find_file (fname, kpse_fontmap_format, 0);
695 break;
696 case mp_filetype_font:
697 s = kpse_find_file (fname, kpse_type1_format, 0);
698 break;
699 case mp_filetype_encoding:
700 s = kpse_find_file (fname, kpse_enc_format, 0);
701 break;
702 }
703 }
704 } else {
705 /* when writing */
706 if (ofname) {
707 s = mpost_xstrdup(ofname);
708 mpost_xfree(ofname);
709 } else {
710 s = mpost_xstrdup(fname);
711 }
712
713 }
714 return s;
715 }
716
717 @ @<Register the callback routines@>=
718 if (!nokpse)
719 options->find_file = mpost_find_file;
720
721 @ The |mpost| program supports setting of internal values
722 via a \.{-s} commandline switch. Since this switch is repeatable,
723 a structure is needed to store the found values in, which is a
724 simple linked list.
725
726 @c
727 typedef struct set_list_item {
728 int isstring;
729 char *name;
730 char *value;
731 struct set_list_item *next;
732 } set_list_item ;
733
734 @ Here is the global value that is the head of the list of \.{-s} options.
735 @c
736 struct set_list_item *set_list = NULL;
737
738 @ And |internal_set_option| is the routine that fills in the linked
739 list. The argument it receives starts at the first letter of the
740 internal, and should contain an internal name, an equals sign,
741 and the value (possibly in quotes) without any intervening spaces.
742
743 Double quotes around the right hand side are needed to make sure that
744 the right hand side is treated as a string assignment by MPlib later.
745 These outer double quote characters are stripped, but no other string
746 processing takes place.
747
748 As a special hidden feature, a missing right hand side is treated as if it
749 was the integer value |1|.
750
751 @<Declarations@>=
752 void internal_set_option(const char *opt);
753
754 @ @c
755 void internal_set_option(const char *opt) {
756 struct set_list_item *itm;
757 char *s, *v;
758 int isstring = 0;
759 s = mpost_xstrdup(opt) ;
760 v = strstr(s,"=") ;
761 if (v==NULL) {
762 v = xstrdup("1");
763 } else {
764 *v='\0'; /* terminates |s| */
765 v++;
766 if (*v && *v=='"') {
767 isstring=1;
768 v++;
769 *(v+strlen(v)-1)= '\0';
770 }
771 }
772 if (s && v && strlen(s)>0) {
773 if (set_list == NULL) {
774 set_list = xmalloc(sizeof(struct set_list_item));
775 itm = set_list;
776 } else {
777 itm = set_list;
778 while (itm->next != NULL)
779 itm = itm->next;
780 itm->next = xmalloc(sizeof(struct set_list_item));
781 itm = itm->next;
782 }
783 itm->name = s;
784 itm->value = v;
785 itm->isstring = isstring;
786 itm->next = NULL;
787 }
788 }
789
790 @ After the initialization stage is done, the next function
791 runs through the list of options and feeds them to the MPlib
792 function |mp_set_internal|.
793
794 @<Declarations@>=
795 void run_set_list (MP mp);
796
797 @ @c
798 void run_set_list (MP mp) {
799 struct set_list_item *itm;
800 itm = set_list;
801 while (itm!=NULL) {
802 mp_set_internal(mp,itm->name,itm->value, itm->isstring);
803 itm = itm->next;
804 }
805 }
806
807
808
809 @ @c
810 static void *mpost_open_file(MP mp, const char *fname, const char *fmode, int ftype) {
811 char realmode[3];
812 char *s;
813 if (ftype==mp_filetype_terminal) {
814 return (fmode[0] == 'r' ? stdin : stdout);
815 } else if (ftype==mp_filetype_error) {
816 return stderr;
817 } else {
818 s = mpost_find_file (mp, fname, fmode, ftype);
819 if (s!=NULL) {
820 void *ret = NULL;
821 realmode[0] = *fmode;
822 realmode[1] = 'b';
823 realmode[2] = '\0';
824 ret = (void *)fopen(s,realmode);
825 if (recorder_enabled) {
826 if (!recorder_file)
827 recorder_start(job_name);
828 if (*fmode == 'r')
829 fprintf(recorder_file, "INPUT %s\n", s);
830 else
831 fprintf(recorder_file, "OUTPUT %s\n", s);
832 }
833 free(s);
834 return ret;
835 }
836 }
837 return NULL;
838 }
839
840 @ @<Register the callback routines@>=
841 if (!nokpse)
842 options->open_file = mpost_open_file;
843
844 @ @d ARGUMENT_IS(a) STREQ (mpost_options[optionid].name, a)
845 @<Structures for |getopt|@>=
846
847 /* SunOS cc can't initialize automatic structs, so make this static. */
848 static struct option mpost_options[]
849 = { { "mem", 1, 0, 0 },
850 { "help", 0, 0, 0 },
851 { "debug", 0, &debug, 1 },
852 { "no-kpathsea", 0, &nokpse, 1 },
853 { "dvitomp", 0, &dvitomp_only, 1 },
854 { "ini", 0, &ini_version_test, 1 },
855 { "interaction", 1, 0, 0 },
856 { "math", 1, 0, 0 },
857 { "numbersystem", 1, 0, 0 },
858 { "halt-on-error", 0, 0, 0 },
859 { "kpathsea-debug", 1, 0, 0 },
860 { "progname", 1, 0, 0 },
861 { "version", 0, 0, 0 },
862 { "recorder", 0, &recorder_enabled, 1 },
863 { "restricted", 0, 0, 0 },
864 { "file-line-error-style", 0, 0, 0 },
865 { "no-file-line-error-style", 0, 0, 0 },
866 { "file-line-error", 0, 0, 0 },
867 { "no-file-line-error", 0, 0, 0 },
868 { "jobname", 1, 0, 0 },
869 { "output-directory", 1, 0, 0 },
870 { "s", 1, 0, 0 },
871 { "parse-first-line", 0, 0, 0 },
872 { "no-parse-first-line", 0, 0, 0 },
873 { "8bit", 0, 0, 0 },
874 { "T", 0, 0, 0 },
875 { "troff", 0, 0, 0 },
876 { "tex", 1, 0, 0 },
877 { 0, 0, 0, 0 } };
878
879
880
881 @ Parsing the commandline options.
882
883 @<Read and set command line options@>=
884 {
885 int g; /* `|getopt|' return code. */
886 int optionid;
887 for (;;) {
888 g = getopt_long_only (argc, argv, "+", mpost_options, &optionid);
889
890 if (g == -1) /* End of arguments, exit the loop. */
891 break;
892
893 if (g == '?') { /* Unknown option. */
894 exit(EXIT_FAILURE);
895 }
896
897 if (ARGUMENT_IS ("kpathsea-debug")) {
898 kpathsea_debug |= (unsigned)atoi (optarg);
899
900 } else if (ARGUMENT_IS("jobname")) {
901 if (optarg!=NULL) {
902 mpost_xfree(options->job_name);
903 options->job_name = mpost_xstrdup(optarg);
904 }
905
906 } else if (ARGUMENT_IS ("progname")) {
907 user_progname = optarg;
908
909 } else if (ARGUMENT_IS ("mem")) {
910 if (optarg!=NULL) {
911 mpost_xfree(options->mem_name);
912 options->mem_name = mpost_xstrdup(optarg);
913 if (user_progname == NULL)
914 user_progname = optarg;
915 }
916
917 } else if (ARGUMENT_IS ("interaction")) {
918 if (STREQ (optarg, "batchmode")) {
919 options->interaction = mp_batch_mode;
920 } else if (STREQ (optarg, "nonstopmode")) {
921 options->interaction = mp_nonstop_mode;
922 } else if (STREQ (optarg, "scrollmode")) {
923 options->interaction = mp_scroll_mode;
924 } else if (STREQ (optarg, "errorstopmode")) {
925 options->interaction = mp_error_stop_mode;
926 } else {
927 fprintf(stdout,"Ignoring unknown argument `%s' to --interaction\n", optarg);
928 }
929 } else if (ARGUMENT_IS ("math") || ARGUMENT_IS ("numbersystem")) {
930 if (STREQ (optarg, "scaled")) {
931 options->math_mode = mp_math_scaled_mode;
932 internal_set_option("numbersystem=\"scaled\"");
933 } else if (STREQ (optarg, "double")) {
934 options->math_mode = mp_math_double_mode;
935 internal_set_option("numbersystem=\"double\"");
936 } else if (STREQ (optarg, "decimal")) {
937 options->math_mode = mp_math_decimal_mode;
938 internal_set_option("numbersystem=\"decimal\"");
939 } else if (STREQ (optarg, "binary")) {
940 options->math_mode = mp_math_binary_mode;
941 internal_set_option("numbersystem=\"binary\"");
942 } else if (STREQ (optarg, "interval")) {
943 options->math_mode = mp_math_interval_mode;
944 internal_set_option("numbersystem=\"interval\"");
945 } else {
946 fprintf(stdout,"Ignoring unknown argument `%s' to --numbersystem\n", optarg);
947 }
948 } else if (ARGUMENT_IS ("restricted")) {
949 restricted_mode = true;
950 mpost_tex_program = NULL;
951 } else if (ARGUMENT_IS("troff") ||
952 ARGUMENT_IS("T")) {
953 options->troff_mode = (int)true;
954 } else if (ARGUMENT_IS ("tex")) {
955 if (!restricted_mode)
956 mpost_tex_program = optarg;
957 } else if (ARGUMENT_IS("file-line-error") ||
958 ARGUMENT_IS("file-line-error-style")) {
959 options->file_line_error_style=true;
960 } else if (ARGUMENT_IS("no-file-line-error") ||
961 ARGUMENT_IS("no-file-line-error-style")) {
962 options->file_line_error_style=false;
963 } else if (ARGUMENT_IS("help")) {
964 if (dvitomp_only) {
965 @<Show short help and exit@>;
966 } else {
967 @<Show help and exit@>;
968 }
969 } else if (ARGUMENT_IS("version")) {
970 @<Show version and exit@>;
971 } else if (ARGUMENT_IS("s")) {
972 if (strchr(optarg,'=')==NULL) {
973 fprintf(stdout,"fatal error: %s: missing -s argument\n", argv[0]);
974 exit (EXIT_FAILURE);
975 } else {
976 internal_set_option(optarg);
977 }
978 } else if (ARGUMENT_IS("halt-on-error")) {
979 options->halt_on_error = true;
980 } else if (ARGUMENT_IS("output-directory")) {
981 output_directory = optarg ;
982 } else if (ARGUMENT_IS("8bit") ||
983 ARGUMENT_IS("parse-first-line")) {
984 /* do nothing, these are always on */
985 fprintf(stdout,"warning: %s: option -%s is always enabled\n", argv[0], mpost_options[optionid].name);
986 } else if (ARGUMENT_IS("translate-file") ||
987 ARGUMENT_IS("no-parse-first-line")) {
988 fprintf(stdout,"warning: %s: unimplemented option -%s\n", argv[0], mpost_options[optionid].name);
989 }
990 }
991 options->ini_version = (int)ini_version_test;
992 }
993
994 @ @d option_is(a) STREQ (dvitomp_options[optionid].name, a)
995 @<Structures for |getopt|@>=
996
997 /* SunOS cc can't initialize automatic structs, so make this static. */
998 static struct option dvitomp_options[]
999 = { { "help", 0, 0, 0 },
1000 { "no-kpathsea", 0, &nokpse, 1 },
1001 { "kpathsea-debug", 1, 0, 0 },
1002 { "progname", 1, 0, 0 },
1003 { "version", 0, 0, 0 },
1004 { 0, 0, 0, 0 } };
1005
1006
1007
1008 @
1009 @<Read and set \.{dvitomp} command line options@>=
1010 {
1011 int g; /* `|getopt|' return code. */
1012 int optionid;
1013 for (;;) {
1014 g = getopt_long_only (argc, argv, "+", dvitomp_options, &optionid);
1015
1016 if (g == -1) /* End of arguments, exit the loop. */
1017 break;
1018
1019 if (g == '?') { /* Unknown option. */
1020 fprintf(stdout,"fatal error: %s: unknown option %s\n", argv[0], argv[optind]);
1021 exit(EXIT_FAILURE);
1022 }
1023 if (option_is ("kpathsea-debug")) {
1024 if (optarg!=NULL)
1025 kpathsea_debug |= (unsigned)atoi (optarg);
1026 } else if (option_is ("progname")) {
1027 user_progname = optarg;
1028 } else if (option_is("help")) {
1029 @<Show short help and exit@>;
1030 } else if (option_is("version")) {
1031 @<Show version and exit@>;
1032 }
1033 }
1034 }
1035
1036 @
1037 @<Show help...@>=
1038 {
1039 char *s = mp_metapost_version();
1040 if (dvitomp_only)
1041 fprintf(stdout, "This is dvitomp %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1042 else
1043 fprintf(stdout, "This is MetaPost %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1044 mpost_xfree(s);
1045 fprintf(stdout,
1046 "\n"
1047 "Usage: mpost [OPTION] [&MPNAME] [MPNAME[.mp]] [COMMANDS]\n"
1048 " mpost --dvitomp DVINAME[.dvi] [MPXNAME[.mpx]]\n"
1049 "\n"
1050 " Run MetaPost on MPNAME, usually creating MPNAME.NNN (and perhaps\n"
1051 " MPNAME.tfm), where NNN are the character numbers generated.\n"
1052 " Any remaining COMMANDS are processed as MetaPost input,\n"
1053 " after MPNAME is read.\n\n"
1054 " With a --dvitomp argument, MetaPost acts as DVI-to-MPX converter only.\n"
1055 " Call MetaPost with --dvitomp --help for option explanations.\n\n");
1056 fprintf(stdout,
1057 "MetaPost options:\n"
1058 " -debug print debugging info and leave temporary files\n"
1059 " in place\n"
1060 " -mem=STRING Use STRING for the name of the file that contains\n"
1061 " macros to be preloaded (same as &MPNAME)\n"
1062 " -numbersystem=STRING set number system mode\n"
1063 " (STRING=scaled/double/binary/interval/decimal)\n"
1064 " -restricted be secure: disable tex, makempx and editor commands\n"
1065 " -tex=TEXPROGRAM use TEXPROGRAM for text labels\n"
1066 " -troff set prologues:=1 and assume TEXPROGRAM is\n"
1067 " really troff\n"
1068 " -T same as -troff\n"
1069 " -s INTERNAL=\"STRING\" set internal INTERNAL to the string value STRING\n"
1070 " -s INTERNAL=NUMBER set internal INTERNAL to the integer value NUMBER\n"
1071 );
1072 fprintf(stdout,
1073 "\n"
1074 "Common options:\n"
1075 " [-no]-file-line-error disable/enable file:line:error style messages\n"
1076 " -halt-on-error stop processing at the first error\n"
1077 " -help display this help and exit\n"
1078 " -ini do not load any preload file\n"
1079 " -interaction=STRING set interaction mode (STRING=batchmode/nonstopmode/\n"
1080 " scrollmode/errorstopmode)\n"
1081 " -jobname=STRING set the job name to STRING\n"
1082 " -kpathsea-debug=NUMBER set path searching debugging flags according to\n"
1083 " the bits of NUMBER\n"
1084 " -no-kpathsea Do not use the kpathsea program to find files.\n"
1085 " All files have to be in the current directory\n"
1086 " or specified via a full path.\n"
1087 " -output-directory=DIR use existing DIR as the directory\n"
1088 " to write files in\n"
1089 " -progname=STRING set program (and mem) name to STRING\n"
1090 " -recorder enable filename recorder\n"
1091 " -version output version information and exit\n"
1092 );
1093 fprintf(stdout,
1094 "\n"
1095 "Email bug reports to mp-implementors@@tug.org.\n"
1096 "\n");
1097 exit(EXIT_SUCCESS);
1098 }
1099
1100 @
1101 @<Show short help...@>=
1102 {
1103 char *s = mp_metapost_version();
1104 if (dvitomp_only)
1105 fprintf(stdout, "This is dvitomp %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1106 else
1107 fprintf(stdout, "This is MetaPost %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1108 mpost_xfree(s);
1109 fprintf(stdout,
1110 "\n"
1111 "Usage: dvitomp DVINAME[.dvi] [MPXNAME[.mpx]]\n"
1112 " mpost --dvitomp DVINAME[.dvi] [MPXNAME[.mpx]]\n"
1113 "\n"
1114 " Convert a TeX DVI file to a MetaPost MPX file.\n\n");
1115 fprintf(stdout,
1116 " -progname=STRING set program name to STRING\n"
1117 " -kpathsea-debug=NUMBER set path searching debugging flags according to\n"
1118 " the bits of NUMBER\n"
1119 " -help display this help and exit\n"
1120 " -version output version information and exit\n"
1121 "\n"
1122 "Email bug reports to mp-implementors@@tug.org.\n"
1123 "\n");
1124 exit(EXIT_SUCCESS);
1125 }
1126
1127 @
1128 @<Show version...@>=
1129 {
1130 char *s = mp_metapost_version();
1131 if (dvitomp_only)
1132 fprintf(stdout, "dvitomp (MetaPost) %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1133 else
1134 fprintf(stdout, "MetaPost %s" WEB2CVERSION " (%s)\n", s, kpathsea_version_string);
1135 fprintf(stdout,
1136 "The MetaPost source code in the public domain.\n"
1137 "MetaPost also uses code available under the\n"
1138 "GNU Lesser General Public License (version 3 or later);\n"
1139 "therefore MetaPost executables are covered by the LGPL.\n"
1140 "There is NO warranty.\n"
1141 "For more information about these matters, see the file\n"
1142 "COPYING.LESSER or <http://gnu.org/licenses/lgpl.html>.\n"
1143 "Original author of MetaPost: John Hobby.\n"
1144 "Author of the CWEB MetaPost: Taco Hoekwater.\n"
1145 "Current maintainer of MetaPost: Luigi Scarso.\n\n"
1146 );
1147 mpost_xfree(s);
1148 if (!dvitomp_only) {
1149 mp_show_library_versions();
1150 }
1151 exit(EXIT_SUCCESS);
1152 }
1153
1154 @ The final part of the command line, after option processing, is
1155 stored in the \MP\ instance, this will be taken as the first line of
1156 input.
1157
1158 @d command_line_size 256
1159 @d max_command_line_size 0xFFFFFFF /* should be the same of |max_halfword| (see |mp_reallocate_buffer|) */
1160
1161 @<Copy the rest of the command line@>=
1162 {
1163 mpost_xfree(options->command_line);
1164 options->command_line = mpost_xmalloc(command_line_size);
1165 strcpy(options->command_line,"");
1166 if (optind<argc) {
1167 int optind_aux = optind;
1168 size_t buflen = 0;
1169 for(;optind_aux<argc;optind_aux++) {
1170 buflen +=(strlen(argv[optind_aux])+1); /* reserve space for |' '| as separator */
1171 }
1172 /* Last char is |' '|, no need to reserve space for final |'\0'| */
1173 if (buflen > max_command_line_size) {
1174 fprintf(stderr,"length of command line too long!\n");
1175 exit(EXIT_FAILURE);
1176 }
1177 mpost_xfree(options->command_line);
1178 options->command_line = mpost_xmalloc(buflen);
1179 k=0;
1180 for(;optind<argc;optind++) {
1181 char *c = argv[optind];
1182 while (*c != '\0') {
1183 options->command_line[k++] = *c;
1184 c++;
1185 }
1186 options->command_line[k++] = ' ';
1187 }
1188 while (k>0) {
1189 if (options->command_line[(k-1)] == ' ')
1190 k--;
1191 else
1192 break;
1193 }
1194 options->command_line[k] = '\0';
1195 }
1196 }
1197
1198 @ A simple function to get numerical \.{texmf.cnf} values
1199 @c
1200 static int setup_var (int def, const char *var_name, boolean nokpse) {
1201 if (!nokpse) {
1202 char * expansion = kpse_var_value (var_name);
1203 if (expansion) {
1204 int conf_val = atoi (expansion);
1205 free (expansion);
1206 if (conf_val > 0) {
1207 return conf_val;
1208 }
1209 }
1210 }
1211 return def;
1212 }
1213
1214 @ @<Set up the banner line@>=
1215 {
1216 char * mpversion = mp_metapost_version () ;
1217 const char * banner = "This is MetaPost, version ";
1218 const char * kpsebanner_start = " (";
1219 const char * kpsebanner_stop = ")";
1220 mpost_xfree(options->banner);
1221 options->banner = mpost_xmalloc(strlen(banner)+
1222 strlen(mpversion)+
1223 strlen(WEB2CVERSION)+
1224 strlen(kpsebanner_start)+
1225 strlen(kpathsea_version_string)+
1226 strlen(kpsebanner_stop)+1);
1227 strcpy (options->banner, banner);
1228 strcat (options->banner, mpversion);
1229 strcat (options->banner, WEB2CVERSION);
1230 strcat (options->banner, kpsebanner_start);
1231 strcat (options->banner, kpathsea_version_string);
1232 strcat (options->banner, kpsebanner_stop);
1233 mpost_xfree(mpversion);
1234 }
1235
1236 @ Precedence order is:
1237
1238 \item {} \.{-mem=MEMNAME} on the command line
1239 \item {} \.{\&MEMNAME} on the command line
1240 \item {} \.{\%\&MEM} as first line inside input file
1241 \item {} \.{argv[0]} if all else fails
1242
1243 @<Discover the mem name@>=
1244 {
1245 char *m = NULL; /* head of potential |mem_name| */
1246 char *n = NULL; /* a moving pointer */
1247 if (options->command_line != NULL && *(options->command_line) == '&'){
1248 m = mpost_xstrdup(options->command_line+1);
1249 n = m;
1250 while (*n != '\0' && *n != ' ') n++;
1251 while (*n == ' ') n++;
1252 if (*n != '\0') { /* more command line to follow */
1253 char *s = mpost_xstrdup(n);
1254 if (n>m) n--;
1255 while (*n == ' ' && n>m) n--;
1256 n++;
1257 *n ='\0'; /* this terminates |m| */
1258 mpost_xfree(options->command_line);
1259 options->command_line = s;
1260 } else { /* only \.{\&MEMNAME} on command line */
1261 if (n>m) n--;
1262 while (*n == ' ' && n>m) n--;
1263 n++;
1264 *n ='\0'; /* this terminates |m| */
1265 mpost_xfree(options->command_line);
1266 }
1267 if ( options->mem_name == NULL && *m != '\0') {
1268 mpost_xfree(options->mem_name); /* for lint only */
1269 options->mem_name = m;
1270 } else {
1271 mpost_xfree(m);
1272 }
1273 }
1274 }
1275 if ( options->mem_name == NULL ) {
1276 char *m = NULL; /* head of potential |job_name| */
1277 char *n = NULL; /* a moving pointer */
1278 if (options->command_line != NULL && *(options->command_line) != '\\'){
1279 m = mpost_xstrdup(options->command_line);
1280 n = m;
1281 while (*n != '\0' && *n != ' ') n++;
1282 if (n>m) {
1283 char *fname;
1284 *n='\0';
1285 fname = m;
1286 if (!nokpse)
1287 fname = kpse_find_file(m,kpse_mp_format,true);
1288 if (fname == NULL) {
1289 mpost_xfree(m);
1290 } else {
1291 FILE *F = fopen(fname,"r");
1292 if (F==NULL) {
1293 mpost_xfree(fname);
1294 } else {
1295 char *line = mpost_xmalloc(256);
1296 if (fgets(line,255,F) == NULL) {
1297 (void)fclose(F);
1298 mpost_xfree(fname);
1299 mpost_xfree(line);
1300 } else {
1301 (void)fclose(F);
1302 while (*line != '\0' && *line == ' ') line++;
1303 if (*line == '%') {
1304 n = m = line+1;
1305 while (*n != '\0' && *n == ' ') n++;
1306 if (*n == '&') {
1307 m = n+1;
1308 while (*n != '\0' && *n != ' ') n++;
1309 if (n>(m+1)) {
1310 n--;
1311 while (*n == ' ' && n>m) n--;
1312 *n ='\0'; /* this terminates |m| */
1313 options->mem_name = mpost_xstrdup(m);
1314 mpost_xfree(fname);
1315 } else {
1316 mpost_xfree(fname);
1317 mpost_xfree(line);
1318 }
1319 }
1320 }
1321 }
1322 }
1323 }
1324 } else {
1325 mpost_xfree(m);
1326 }
1327 }
1328 }
1329 if ( options->mem_name == NULL )
1330 if (kpse_program_name!=NULL)
1331 options->mem_name = mpost_xstrdup(kpse_program_name);
1332
1333
1334 @ The job name needs to be known for the recorder to work,
1335 so we have to fix up |job_name| and |job_area|. If there
1336 was a \.{--jobname} on the command line, we have to reset
1337 the options structure as well.
1338
1339 @<Discover the job name@>=
1340 {
1341 char *tmp_job = NULL;
1342 if (options->job_name != NULL) {
1343 tmp_job = mpost_xstrdup(options->job_name);
1344 mpost_xfree(options->job_name);
1345 options->job_name = NULL;
1346 } else {
1347 char *m = NULL; /* head of potential |job_name| */
1348 char *n = NULL; /* a moving pointer */
1349 if (options->command_line != NULL){
1350 m = mpost_xstrdup(options->command_line);
1351 n = m;
1352 if (*(options->command_line) != '\\') { /* this is the simple case */
1353 while (*n != '\0' && *n != ' ') n++;
1354 if (n>m) {
1355 *n='\0';
1356 tmp_job = mpost_xstrdup(m);
1357 }
1358 } else { /* this is still not perfect, but better */
1359 char *mm = strstr(m,"input ");
1360 if (mm != NULL) {
1361 mm += 6;
1362 n = mm;
1363 while (*n != '\0' && *n != ' ' && *n!=';') n++;
1364 if (n>mm) {
1365 *n='\0';
1366 tmp_job = mpost_xstrdup(mm);
1367 }
1368 }
1369 }
1370 free(m);
1371 }
1372 if (tmp_job == NULL) {
1373 if (options->ini_version == 1 &&
1374 options->mem_name != NULL) {
1375 tmp_job = mpost_xstrdup(options->mem_name);
1376 }
1377 }
1378 if (tmp_job == NULL) {
1379 tmp_job = mpost_xstrdup("mpout");
1380 } else {
1381 char *ext = strrchr(tmp_job,'.');
1382 if (ext != NULL)
1383 *ext = '\0';
1384 }
1385 }
1386 /* now split |tmp_job| into |job_area| and |job_name| */
1387 {
1388 char *s = tmp_job + strlen(tmp_job);
1389 if (!IS_DIR_SEP(*s)) { /* just in case */
1390 while (s>tmp_job) {
1391 if (IS_DIR_SEP(*s)) {
1392 break;
1393 }
1394 s--;
1395 }
1396 if (s>tmp_job) {
1397 /* there was a directory part */
1398 if (strlen(s)>1) {
1399 job_name = mpost_xstrdup((s+1));
1400 *(s+1) = '\0';
1401 job_area = tmp_job;
1402 }
1403 } else {
1404 job_name = tmp_job;
1405 /* |job_area| stays |NULL| */
1406 }
1407 }
1408 }
1409 }
1410 options->job_name = job_name;
1411
1412 @ We |
1413 #define DLLPROC dllmpostmain
1414 | in order to build \MP\ as DLL for W32\TeX.
1415
1416 @<Declarations@>=
1417 #define DLLPROC dllmpostmain
1418 #if defined(WIN32) && !defined(__MINGW32__) && defined(DLLPROC)
1419 extern __declspec(dllexport) int DLLPROC (int argc, char **argv);
1420 #else
1421 #undef DLLPROC
1422 #endif
1423
1424 @ Now this is really it: \MP\ starts and ends here.
1425
1426 @c
1427 static char *cleaned_invocation_name(char *arg)
1428 {
1429 char *ret, *dot;
1430 const char *start = xbasename(arg);
1431 ret = xstrdup(start);
1432 dot = strrchr(ret, '.');
1433 if (dot != NULL) {
1434 *dot = 0; /* chop */
1435 }
1436 return ret;
1437 }
1438 int
1439 #if defined(DLLPROC)
1440 DLLPROC (int argc, char **argv)
1441 #else
1442 main (int argc, char **argv)
1443 #endif
1444 @;
1445 { @t\1@> /* |start_here| */
1446 int k; /* index into buffer */
1447 int history; /* the exit status */
1448 MP mp; /* a metapost instance */
1449 struct MP_options * options; /* instance options */
1450 char *user_progname = NULL; /* If the user overrides |argv[0]| with \.{-progname}. */
1451 options = mp_options();
1452 options->ini_version = (int)false;
1453 options->print_found_names = (int)true;
1454 {
1455 const char *base = cleaned_invocation_name(argv[0]);
1456 if (FILESTRCASEEQ(base, "rmpost")){
1457 base++;
1458 restricted_mode = true;
1459 } else if (FILESTRCASEEQ(base, "r-mpost")){
1460 base += 2;
1461 restricted_mode = true;
1462 }
1463 if (FILESTRCASEEQ(base, "dvitomp"))
1464 dvitomp_only=1;
1465 }
1466 if (dvitomp_only) {
1467 @<Read and set \.{dvitomp} command line options@>;
1468 } else {
1469 @<Read and set command line options@>;
1470 }
1471 if (dvitomp_only) {
1472 char *mpx = NULL, *dvi = NULL;
1473 if (optind>=argc) {
1474 /* error ? */
1475 } else {
1476 dvi = argv[optind++];
1477 if (optind<argc) {
1478 mpx = argv[optind++];
1479 }
1480 }
1481 if (dvi == NULL) {
1482 @<Show short help and exit@>;
1483 } else {
1484 if (!nokpse)
1485 kpse_set_program_name(argv[0],
1486 user_progname ? user_progname : "dvitomp");
1487 exit (mpost_run_dvitomp(dvi, mpx));
1488 }
1489 }
1490
1491 @= /*@@-nullpass@@*/ @>
1492 if (!nokpse) {
1493 kpse_set_program_enabled (kpse_mem_format, MAKE_TEX_FMT_BY_DEFAULT,
1494 kpse_src_compile);
1495 kpse_set_program_name(argv[0], user_progname);
1496 if (FILESTRCASEEQ(kpse_program_name, "rmpost"))
1497 kpse_program_name++;
1498 else if (FILESTRCASEEQ(kpse_program_name, "r-mpost"))
1499 kpse_program_name += 2;
1500 }
1501 @= /*@@=nullpass@@*/ @>
1502 if(putenv(xstrdup("engine=metapost")))
1503 fprintf(stdout,"warning: could not set up $engine\n");
1504 options->error_line = setup_var (79,"error_line",nokpse);
1505 options->half_error_line = setup_var (50,"half_error_line",nokpse);
1506 options->max_print_line = setup_var (100,"max_print_line",nokpse);
1507 @<Set up the banner line@>;
1508 @<Copy the rest of the command line@>;
1509 @<Discover the mem name@>;
1510 @<Discover the job name@>;
1511 @<Register the callback routines@>;
1512 mp = mp_initialize(options);
1513 mpost_xfree(options->command_line);
1514 mpost_xfree(options->mem_name);
1515 mpost_xfree(options->job_name);
1516 mpost_xfree(options->banner);
1517 free(options);
1518 if (mp==NULL)
1519 exit(EXIT_FAILURE);
1520 history = mp_status(mp);
1521 if (history!=0 && history!=mp_warning_issued)
1522 exit(history);
1523 if (set_list!=NULL) {
1524 run_set_list(mp);
1525 }
1526 history = mp_run(mp);
1527 (void)mp_finish(mp);
1528 if (history!=0 && history!=mp_warning_issued)
1529 exit(history);
1530 else
1531 exit(0);
1532 @t\8@>
1533 }
1534
1535 @* Index.