summaryrefslogtreecommitdiff
path: root/support/texproc/texproc.c
blob: aab672b3f32aa99f10f6e6e816b3a0871a1dc085 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * texproc: embedded command preprocessor for TeX and LaTeX
 * (c) 1992 Dougal Scott
 * Any comments, criticisms, patches to 
 * Dougal.Scott@FCIT.monash.edu.au
 *
 * Convert LaTeX:
 * ....
 * %# gnuplot
 * plot sin, cos, and tan
 * %#
 * ....
 *
 * to 
 *
 * ....
 * \begin{picture}
 * \lotsadots
 * \end{picture}
 * ....
 * 
 * Makefile commands:
 * 
 * CAT=/bin/cat
 * PROC=texproc
 *
 * .SUFFIXES:.zd .tex
 *
 * .zd.tex:
 *	$(CAT) $< | $(PROC) > $*.tex
 *
 */

#define TRACE(x)	/* x */

#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>

extern int errno;
FILE *tmp;

main(argc,argv)
int argc;
char **argv;
{
char buff[BUFSIZ];

while(gets(buff)!=NULL) {
	   if(buff[0]=='%' && buff[1]=='#')
		   process(buff);
	   else
		   fprintf(stdout,"%s\n",buff);
	   }
return(0);
}

process(buff)
char *buff;
{
char 	tmpname[80],		/* Name of tmp file */
		   progname[80],		/* Name of program to execute */	
		   cmdline[80],		/* What to feed to popen */
		   outbuff[BUFSIZ];	/* What to take output of prgram from */
FILE	*p;

strcpy(tmpname,"/tmp/PrcXXXXXX");
mktemp(tmpname);
if((tmp=fopen(tmpname,"w"))==NULL) {
	   fprintf(stderr,"Could not open %s for writing\n",tmpname);
	   fprintf(stderr,"Program aborting\n");
	   exit(-1);
	   }
TRACE(fprintf(stderr,"Saving to tmp file %s\n",tmpname));
strcpy(progname,&buff[3]);
fprintf(stdout,"%% Including output from %s\n",progname);
/* Put buffer to file for executing */
while(gets(buff)!=NULL) {
	   if(buff[0]=='%' && buff[1]=='#') {
		   fclose(tmp);
		   sprintf(cmdline,"%s %s",progname,tmpname);
		   fprintf(stderr,"%s\n",progname);
		   if((p=popen(cmdline,"r"))==NULL) {
			   fprintf(stderr,"Could not open pipe to %s\n",cmdline);
			   exit(-1);
			   }
		   while(fgets(outbuff,BUFSIZ,p)!=NULL)
			   fprintf(stdout,"%s",outbuff);
		   pclose(p);
		   unlink(tmpname);
		   return(0);
		   }
	   else {
		   TRACE(fprintf(stderr,"%s\n",buff));
		   fprintf(tmp,"%s\n",buff);
		   }
	   }
}