w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

definitions.c
Go to the documentation of this file.
1 /* definitions.c - Routines to handle TeX \def and LaTeX \newcommand
2 
3 Copyright (C) 2001-2002 The Free Software Foundation
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 This file is available from http://sourceforge.net/projects/latex2rtf/
20 */
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include "main.h"
25 #include "convert.h"
26 #include "definitions.h"
27 #include "parser.h"
28 #include "funct1.h"
29 #include "util.h"
30 #include "cfg.h"
31 #include "counters.h"
32 #include "funct1.h"
33 
34 #define MAX_DEFINITIONS 200
35 #define MAX_ENVIRONMENTS 20
36 #define MAX_THEOREMS 20
37 
38 struct {
39  char * name;
40  char * opt_param;
41  char * def;
42  int params;
44 
45 struct {
46  char * name;
47  char * opt_param;
48  char * begname;
49  char * endname;
50  char * begdef;
51  char * enddef;
52  int params;
54 
55 struct {
56  char * name;
57  char * numbered_like;
58  char * caption;
59  char * within;
61 
62 static int iDefinitionCount = 0;
63 static int iNewEnvironmentCount = 0;
64 static int iNewTheoremCount = 0;
65 
66 static int
67 strequal(char *a, char *b)
68 {
69  if (a==NULL || b==NULL)
70  return 0;
71 
72  while (*a && *b && *a==*b) {a++;b++;}
73 
74  if (*a || *b)
75  return 0;
76  else
77  return 1;
78 }
79 
80 /* static void printDefinitions(void)
81 {
82 int i=0;
83  fprintf(stderr, "\n");
84  while(i < iDefinitionCount ) {
85  fprintf(stderr, "[%d] name =<%s>\n",i, Definitions[i].name);
86  fprintf(stderr, " opt_param=<%s>\n", Definitions[i].opt_param);
87  fprintf(stderr, " def =<%s>\n", Definitions[i].def);
88  fprintf(stderr, " params =<%d>\n", Definitions[i].params);
89  i++;
90  }
91 }
92 
93 static void printTheorems(void)
94 {
95 int i=0;
96  fprintf(stderr, "\n");
97  for (i=0; i< iNewTheoremCount; i++) {
98  fprintf(stderr, "[%d] name =<%s>\n",i, NewTheorems[i].name);
99  fprintf(stderr, " caption =<%s>\n", NewTheorems[i].caption);
100  fprintf(stderr, " like =<%s>\n", NewTheorems[i].numbered_like);
101  fprintf(stderr, " within =<%s>\n", NewTheorems[i].within);
102  }
103 }
104 */
105 
106 static char *
107 expandmacro(char *macro, char *opt_param, int params)
108 /**************************************************************************
109  purpose: retrieves and expands a defined macro
110 **************************************************************************/
111 {
112  int i=0,param;
113  char * args[9], *dmacro, *macro_piece, *next_piece, *expanded, buffer[1024], *cs;
114 
115  if (params<=0)
116  return strdup(macro);
117 
118  if (opt_param) {
119  args[i++] = getBracketParam();
120  if (!args[0]) args[0] = strdup(opt_param);
121  }
122 
123  for (; i<params; i++) {
124  args[i] = getBraceParam();
125  diagnostics(3, "argument #%d <%s>", i+1, args[i]);
126  }
127 
128  *buffer='\0';
129  expanded = buffer;
130  dmacro = strdup(macro);
131  macro_piece = dmacro;
132 
133  /* convert "\csname" to "\" */
134  while ((cs=strstr(dmacro, "\\csname")) != NULL) strcpy(cs+1,cs+7);
135 
136  /* remove "\endcsname" */
137  while ((cs=strstr(dmacro, "\\endcsname")) != NULL) strcpy(cs,cs+10);
138 
139  /* do not use strtok because it may be used elsewhere */
140  while (macro_piece && *macro_piece) {
141 
142  next_piece = strchr(macro_piece, '#');
143  if (next_piece) {
144  *next_piece = '\0';
145  next_piece++;
146  if (*next_piece=='#')
147  param = 101; /* just a flag for below */
148  else
149  param = *next_piece - '1';
150  next_piece++;
151  } else
152  param = -1;
153 
154  diagnostics(3, "expandmacro piece =<%s>", macro_piece);
155  strcpy(expanded,macro_piece);
156  expanded += strlen(macro_piece);
157  if (param > -1) {
158  if (param==101) {
159  diagnostics(3, "expandmacro ## = #");
160  strcpy(expanded,"#");
161  expanded ++;
162  } else if (param<params) {
163  diagnostics(3, "expandmacro arg =<%s>", args[param]);
164  strcpy(expanded,args[param]);
165  expanded += strlen(args[param]);
166  } else
167  diagnostics(WARNING__,"confusing definition in macro=<%s>", macro);
168  }
169 
170  macro_piece = next_piece;
171  }
172 
173 
174 /* ConvertString(buffer);*/
175  for (i=0; i< params; i++)
176  if (args[i]) free(args[i]);
177 
178  if (dmacro) free(dmacro);
179 
180  diagnostics(3, "expandmacro expanded=<%s>", buffer);
181  return strdup(buffer);
182 }
183 
184 int
185 maybeDefinition(char * s, size_t n)
186 /**************************************************************************
187  purpose: checks to see if a named TeX definition possibly exists
188  returns: the array index of the named TeX definition
189 **************************************************************************/
190 {
191  int i;
192 
193  if (n==0) return TRUE;
194 
195  for (i=0; i<iDefinitionCount; i++) {
196  diagnostics(6, "seeking=<%s>, i=%d, current=<%s>", s,i,Definitions[i].name);
197  if (strncmp(s,Definitions[i].name,n) == 0)
198  return TRUE;
199  }
200 
201  return FALSE;
202 }
203 
204 int
206 /**************************************************************************
207  purpose: checks to see if a named TeX definition exists
208  returns: the array index of the named TeX definition
209 **************************************************************************/
210 {
211  int i;
212 
213  for (i=0; i<iDefinitionCount; i++) {
214  diagnostics(6, "seeking=<%s>, i=%d, current=<%s>", s,i,Definitions[i].name);
215  if (strcmp(s,Definitions[i].name) == 0) break;
216  }
217 
218  if (i==iDefinitionCount)
219  return -1;
220  else
221  return i;
222 }
223 
224 void
225 newDefinition(char *name, char * opt_param, char *def, int params)
226 /**************************************************************************
227  purpose: allocates and initializes a named TeX definition
228  name should not begin with a '\' for example to
229  define \hd, name = "hd"
230 **************************************************************************/
231 {
232  diagnostics(3,"Adding macro <%s>=<%s>",name,def);
233 
234  if (strcmp(name,"LaTeX")==0) return;
235  if (strcmp(name,"TeX")==0) return;
236  if (strcmp(name,"AmSTeX")==0) return;
237  if (strcmp(name,"BibTex")==0) return;
238  if (strcmp(name,"LaTeXe")==0) return;
239  if (strcmp(name,"AmSLaTeX")==0) return;
240 
242  diagnostics(WARNING__,"Too many definitions, ignoring %s", name);
243  return;
244  }
245 
247 
249 
251  diagnostics(ERROR, "\nCannot allocate name for definition \\%s\n", name);
252  }
253 
254  if (opt_param) {
256 
258  diagnostics(ERROR, "\nCannot allocate opt_param for definition \\%s\n", name);
259  }
260  }
261  else {
262  Definitions[iDefinitionCount].opt_param=NULL;
263  }
264 
266 
268  diagnostics(ERROR, "\nCannot allocate def for definition \\%s\n", name);
269  }
270 
272  diagnostics(3,"Successfully added macro #%d",iDefinitionCount);
273 }
274 
275 void
276 renewDefinition(char * name, char * opt_param, char * def, int params)
277 /**************************************************************************
278  purpose: allocates (if necessary) and sets a named TeX definition
279 **************************************************************************/
280 {
281  int i;
282 
283  diagnostics(3,"renewDefinition seeking <%s>\n",name);
285 
286  if (i<0) {
288  diagnostics(WARNING__, "No existing definition for \\%s", name);
289 
290  } else {
291  free(Definitions[i].def);
293  Definitions[i].params = params;
294  if (opt_param) {
295  Definitions[i].opt_param=strdup(opt_param);
296  if (Definitions[i].opt_param==NULL) {
297  diagnostics(ERROR, "\nCannot allocate opt_param for definition \\%s\n", name);
298  }
299  }
300  else {
301  Definitions[i].opt_param=NULL;
302  }
303 
304  Definitions[i].def = strdup(def);
305  if (Definitions[i].def==NULL) {
306  diagnostics(WARNING__, "\nCannot allocate def for definition \\%s\n", name);
307  exit(1);
308  }
309  }
310 }
311 
312 char *
313 expandDefinition(int thedef)
314 /**************************************************************************
315  purpose: retrieves and expands a \newcommand macro
316 **************************************************************************/
317 {
318 
319  if (thedef<0 || thedef>=iDefinitionCount)
320  return NULL;
321 
322  diagnostics(3, "expandDefinition name =<%s>", Definitions[thedef].name);
323  diagnostics(3, "expandDefinition opt_param=<%s>",
324  (Definitions[thedef].opt_param) ? Definitions[thedef].opt_param : "");
325  diagnostics(3, "expandDefinition def =<%s>", Definitions[thedef].def);
326  diagnostics(3, "expandDefinition params =<%d>", Definitions[thedef].params);
327 
328  return expandmacro(Definitions[thedef].def, Definitions[thedef].opt_param, Definitions[thedef].params);
329 }
330 
331 int
333 /**************************************************************************
334  purpose: checks to see if a user created environment exists
335  returns: the array index of the \newenvironment
336 **************************************************************************/
337 {
338  int i=0;
339  size_t n;
340 
341  n = strlen(s);
343  diagnostics(4, "e seeking=<%s>, i=%d, current=<%s>", s,i,NewEnvironments[i].name);
344  i++;
345  }
346 
347  if (i==iNewEnvironmentCount)
348  return -1;
349  else
350  return i;
351 }
352 
353 int
354 maybeEnvironment(char * s, size_t n)
355 /**************************************************************************
356  purpose: checks to see if a named TeX environment possibly exists
357  returns: the array index of the named TeX definition
358 **************************************************************************/
359 {
360  int i;
361 
362  if (n==0) return TRUE;
363 
364  for (i=0; i<iNewEnvironmentCount; i++) {
365  diagnostics(6, "seeking=<%s>, i=%d, current=<%s>", s,i,NewEnvironments[i].name);
366  if (strncmp(s,NewEnvironments[i].begname,n) == 0 ||
367  strncmp(s,NewEnvironments[i].endname,n) == 0) {
368  diagnostics(6,"possible");
369  return TRUE;
370  }
371  }
372 
373  diagnostics(6,"not possible");
374  return FALSE;
375 }
376 
377 void
378 newEnvironment(char *name, char *opt_param, char *begdef, char *enddef, int params)
379 /**************************************************************************
380  purpose: allocates and initializes a \newenvironment
381  name should not begin with a '\'
382 **************************************************************************/
383 {
385  diagnostics(WARNING__,"Too many newenvironments, ignoring %s", name);
386  return;
387  }
388 
395 
396  if (opt_param) {
398 
400  diagnostics(ERROR, "\nCannot allocate opt_param for \\newenvironment{%s}", name);
401  }
402  }
403  else {
405  }
406 
407 
413  diagnostics(ERROR, "Cannot allocate memory for \\newenvironment{%s}", name);
414  }
415 
417 }
418 
419 void
420 renewEnvironment(char *name, char *opt_param, char *begdef, char *enddef, int params)
421 /**************************************************************************
422  purpose: allocates and initializes a \renewenvironment
423 **************************************************************************/
424 {
425  int i;
427 
428  if (i<0) {
430  diagnostics(WARNING__, "No existing \\newevironment{%s}", name);
431 
432  } else {
438  if (opt_param) {
439  NewEnvironments[i].opt_param=strdup(opt_param);
441  diagnostics(ERROR, "\nCannot allocate opt_param for \\renewenvironment{%s}", name);
442  }
443  }
444  else {
445  NewEnvironments[i].opt_param=NULL;
446  }
447  NewEnvironments[i].params = params;
448  NewEnvironments[i].begdef = strdup(begdef);
449  NewEnvironments[i].enddef = strdup(enddef);
451  diagnostics(ERROR, "Cannot allocate memory for \\renewenvironment{%s}", name);
452  }
453  }
454 }
455 
456 char *
457 expandEnvironment(int thedef, int code)
458 /**************************************************************************
459  purpose: retrieves and expands a \newenvironment
460 **************************************************************************/
461 {
462  if (thedef<0 || thedef>=iNewEnvironmentCount)
463  return NULL;
464 
465  if (code == CMD_BEGIN) {
466 
467  diagnostics(3, "\\begin{%s} <%s>", NewEnvironments[thedef].name, \
468  NewEnvironments[thedef].begdef);
469  return expandmacro(NewEnvironments[thedef].begdef,
470  NewEnvironments[thedef].opt_param,
471  NewEnvironments[thedef].params);
472 
473  } else {
474 
475  diagnostics(3, "\\end{%s} <%s>", NewEnvironments[thedef].name, \
476  NewEnvironments[thedef].enddef);
477  return expandmacro(NewEnvironments[thedef].enddef, NULL, 0);
478  }
479 }
480 
481 void
482 newTheorem(char *name, char *caption, char *numbered_like, char *within)
483 /**************************************************************************
484  purpose: allocates and initializes a \newtheorem
485 **************************************************************************/
486 {
488  diagnostics(WARNING__,"Too many \\newtheorems, ignoring %s", name);
489  return;
490  }
491 
493 
495 
496  if (numbered_like)
498  else
499  NewTheorems[iNewTheoremCount].numbered_like=strdup(name);
500 
501  if (within)
503  else
505 
507 
509 }
510 
511 int
513 /**************************************************************************
514  purpose: checks to see if a user created environment exists
515  returns: the array index of the \newtheorem
516 **************************************************************************/
517 {
518  int i=0;
519 
520  while(i < iNewTheoremCount && !strequal(s,NewTheorems[i].name)) {
521  diagnostics(6, "seeking=<%s>, i=%d, current=<%s>", s,i,NewTheorems[i].name);
522  i++;
523  }
524 
525  if (i==iNewTheoremCount)
526  return -1;
527  else
528  return i;
529 }
530 
531 char *
532 expandTheorem(int i, char *option)
533 /**************************************************************************
534  purpose: retrieves and expands a \newtheorem into a string
535 **************************************************************************/
536 {
537  char s[128], *num;
538  int ithm;
539 
540  if (i<0 || i>=iNewTheoremCount)
541  return strdup("");
542 
545 
546  if (NewTheorems[i].within) {
548  if (option)
549  snprintf(s,128,"%s %s.%d (%s)", NewTheorems[i].caption, num, ithm, option);
550  else
551  snprintf(s,128,"%s %s.%d", NewTheorems[i].caption, num, ithm);
552  free(num);
553  } else {
554  if (option)
555  snprintf(s,128,"%s %d (%s)", NewTheorems[i].caption, ithm, option);
556  else
557  snprintf(s,128,"%s %d", NewTheorems[i].caption, ithm);
558  }
559 
560  return strdup(s);
561 }
562 
563 void
565 /**************************************************************************
566  purpose: resets theorem counters based on unit
567 **************************************************************************/
568 {
569  int i;
570 
571  for (i=0; i<iNewTheoremCount; i++) {
574  }
575 }
576 
#define strdup
Definition: Utility.h:167
#define param(a, b)
#define n
Definition: t4ht.c:1290
#define b
Definition: jpegint.h:372
void incrementCounter(char *s)
Definition: counters.c:78
void setCounter(char *s, int n)
Definition: counters.c:93
int getCounter(char *s)
Definition: counters.c:108
@ FALSE
Definition: dd.h:101
@ TRUE
Definition: dd.h:102
#define free(a)
Definition: decNumber.cpp:310
int params
Definition: definitions.c:42
void renewEnvironment(char *name, char *opt_param, char *begdef, char *enddef, int params)
Definition: definitions.c:420
void newTheorem(char *name, char *caption, char *numbered_like, char *within)
Definition: definitions.c:482
#define MAX_THEOREMS
Definition: definitions.c:36
int maybeDefinition(char *s, size_t n)
Definition: definitions.c:185
char * begname
Definition: definitions.c:48
char * caption
Definition: definitions.c:58
char * enddef
Definition: definitions.c:51
static int iNewEnvironmentCount
Definition: definitions.c:63
char * def
Definition: definitions.c:41
char * opt_param
Definition: definitions.c:40
static char * expandmacro(char *macro, char *opt_param, int params)
Definition: definitions.c:107
int existsDefinition(char *s)
Definition: definitions.c:205
static int iDefinitionCount
Definition: definitions.c:62
void newEnvironment(char *name, char *opt_param, char *begdef, char *enddef, int params)
Definition: definitions.c:378
char * name
Definition: definitions.c:39
char * expandTheorem(int i, char *option)
Definition: definitions.c:532
static int strequal(char *a, char *b)
Definition: definitions.c:67
char * endname
Definition: definitions.c:49
static int iNewTheoremCount
Definition: definitions.c:64
void resetTheoremCounter(char *unit)
Definition: definitions.c:564
#define MAX_ENVIRONMENTS
Definition: definitions.c:35
int existsTheorem(char *s)
Definition: definitions.c:512
char * numbered_like
Definition: definitions.c:57
struct @181 NewTheorems[20]
int maybeEnvironment(char *s, size_t n)
Definition: definitions.c:354
char * expandDefinition(int thedef)
Definition: definitions.c:313
void renewDefinition(char *name, char *opt_param, char *def, int params)
Definition: definitions.c:276
char * expandEnvironment(int thedef, int code)
Definition: definitions.c:457
struct @179 Definitions[200]
void newDefinition(char *name, char *opt_param, char *def, int params)
Definition: definitions.c:225
int existsEnvironment(char *s)
Definition: definitions.c:332
char * begdef
Definition: definitions.c:50
#define MAX_DEFINITIONS
Definition: definitions.c:34
char * within
Definition: definitions.c:59
struct @180 NewEnvironments[20]
int strcmp()
Definition: coll.cpp:143
char * strcpy()
char * FormatUnitNumber(char *name)
Definition: funct1.c:642
#define CMD_BEGIN
Definition: funct1.h:24
#define s
Definition: afcover.h:80
#define a(n)
Definition: gpos-common.c:148
#define strchr
Definition: gsftopk.c:59
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
void exit()
int num
Definition: disdvi.c:621
#define ERROR(string)
Definition: error.h:36
char * strstr()
void diagnostics(int level, char *format,...)
Definition: main.c:469
#define WARNING__
Definition: main.h:32
#define macro
Definition: ctangleboot.c:153
int strncmp()
#define snprintf
Definition: snprintf.c:41
char * getBraceParam(void)
char * getBracketParam(void)
static struct colourSystem * cs
Definition: ppmcie.c:241
Definition: usprintf.c:39
Definition: utils.c:300
Definition: inftrees.h:24
Definition: pst1form.c:310
Definition: getopt.h:95
Definition: fio.h:71
*job_name strlen((char *) job_name) - 4)
char * strdup_together(char *s, char *t)
Definition: util.c:95
#define buffer
Definition: xmlparse.c:611