math.c File Reference

Generic LaTeX renderer. More...

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <wordexp.h>
#include "../serializer.h"
#include "../migrator.h"
#include "../out.h"
#include "math.h"

Include dependency graph for math.c:

Include dependency graph

Go to the source code of this file.

Defines

#define EVAL(expr, err, errnum)   if (expr) { errno = err; perror(pcerr); iret = errnum; goto cleanup; }
 Evaluates expression, sets error fields and starts cleanup.
#define ALLOC(str, vector)   EVAL((str = (char *) realloc(str, vector * sizeof(char) + sizeof('\0'))) == NULL, ENOMEM, ERRGENERAL)
 Allocates a string with given vector, calling EVAL to error-process as necessary.
#define NONE(a)   a = (char *) malloc(0)
 Allocates a string suitably for realloc.

Functions

int math (char *pcin, char *pcdigest, pair_t *ppa)
 Default LaTeX renderer; may be extended to other classes by including their templates in the inc folder.


Detailed Description

Generic LaTeX renderer.

Definition in file math.c.


Define Documentation

#define ALLOC str,
vector   )     EVAL((str = (char *) realloc(str, vector * sizeof(char) + sizeof('\0'))) == NULL, ENOMEM, ERRGENERAL)
 

Allocates a string with given vector, calling EVAL to error-process as necessary.

Parameters:
str String to be allocated; should be initialized by NONE .
vector size to be allocated
See also:
EVAL, NONE

Definition at line 25 of file math.c.

#define EVAL expr,
err,
errnum   )     if (expr) { errno = err; perror(pcerr); iret = errnum; goto cleanup; }
 

Evaluates expression, sets error fields and starts cleanup.

Parameters:
expr evaluable
err errno
errnum interal error eserialerr

Definition at line 19 of file math.c.

#define NONE  )     a = (char *) malloc(0)
 

Allocates a string suitably for realloc.

Parameters:
a to-be-allocated string

Definition at line 29 of file math.c.


Function Documentation

int math char *  pcin,
char *  pcdigest,
pair_t ppa
 

Default LaTeX renderer; may be extended to other classes by including their templates in the inc folder.

Parameters:
pcin the translated source
pcdigest hash of the source, whether MD5
ppa set of content tokens, whose first is class -> content
Returns:
status according to eserialerr .

Definition at line 37 of file math.c.

References ALLOC, cache(), ERRMATH, ERRMATHWRITE, ERROK, ERRPROCESS, EVAL, pair_t::i, migrate(), NONE, out(), pcdigest(), pairnode_t::pckey, pairnode_t::pcvalue, ppa, pair_t::ppan, and PREFIX.

Referenced by serialize().

00038 {
00039   int i, j, iret = ERROK;
00040   const char *pctagbase = "<span class=\"%s\"><img alt=\"%s\" src=\"%s\" /></span>";
00041   const char *pccommandbase = "latex --interaction=batchmode --jobname=%s %s >&2";
00042   const char *pcerr = "math.c math()";
00043   const char *pcaltkey = "alt";
00044   const char *pcoutbase = "%s.tex";
00045   char NONE(*pccommand), NONE(*pcjob), NONE(*pcout), *pcalt;
00046   wordexp_t w;
00047   FILE *pfproc, *pfout;
00048 
00049   ALLOC(pcjob, strlen(PREFIX) + strlen(pcdigest));
00050   strcpy(pcjob, PREFIX);
00051   strcat(pcjob, pcdigest);
00052 
00053   if (!cache(pcjob, &w)) {
00054     ALLOC(pcout, strlen(pcoutbase) + strlen(pcjob));
00055     sprintf(pcout, pcoutbase, pcjob);
00056 
00057     EVAL((pfout = fopen(pcout, "w")) == NULL, EIO, ERRMATHWRITE);
00058     EVAL(out(pcin, pfout) != EXIT_SUCCESS, EIO, ERRMATHWRITE);
00059     EVAL(fclose(pfout) == EOF, errno, ERRMATHWRITE);
00060 
00061     ALLOC(pccommand, strlen(pccommandbase) + strlen(pcjob) + strlen(pcout));
00062     sprintf(pccommand, pccommandbase, pcjob, pcout);
00063 
00064     EVAL((pfproc = popen(pccommand, "r")) == NULL, EIO, ERRPROCESS);
00065     EVAL(pclose(pfproc) == -1, EINVAL, ERRMATH);
00066 
00067     EVAL((i = migrate(pcjob, &w)) != ERROK, EINVAL, i);
00068   }
00069   
00070   for (i = 0; i < w.we_wordc; i++) {
00071     /* alternate text points to unadorned content */
00072     pcalt = ppa->ppan->pcvalue;
00073     /* test for extant alt->content pair */
00074     for (j = 0; j < ppa->i; j++) {
00075       if (strcmp(pcaltkey, (ppa->ppan + i)->pckey) == 0) {
00076         pcalt = (ppa->ppan + i)->pcvalue;
00077         break;
00078       }
00079     }
00080     /* terminal interface */
00081     printf(pctagbase, ppa->ppan->pckey, pcalt, *(w.we_wordv + i));
00082   }
00083   wordfree(&w);
00084 
00085  cleanup:
00086   free(pcjob);
00087   free(pccommand);
00088   free(pcout);
00089   return iret;
00090 }

Here is the call graph for this function:


Generated on Tue Dec 7 06:38:25 2004 for CSCI101:ProjectLatex by  doxygen 1.3.9.1