File: options.c

package info (click to toggle)
src2tex 2.12h-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,136 kB
  • sloc: ansic: 5,679; sh: 405; makefile: 95; lisp: 46; sed: 39
file content (276 lines) | stat: -rw-r--r-- 7,965 bytes parent folder | download
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* {\hrulefill} *
{\ % beginning of TeX mode

\input fonts.tex % define fonts
\input title.tex % title and authors

% end of TeX mode}
* {\hrulefill} */


/* {\hrulefill\ options.c\ \hrulefill} */


#include <stdio.h>
#include <stdlib.h>
#include "src2tex.h"

extern int *inc_buf_ptr(int *);
extern int *dec_buf_ptr(int *);

int Htab_Size = HTAB_SKIP;		/*{\ horizontal tabulation skip
					  amount \hfill} */
int Vtab_Size = VTAB_SKIP;		/*{\ vertical tabulation skip
					  amount \hfill} */
#ifdef ASCII
/* {\ ASCII J\TeX \hfill} */
char Bold[] = "\\bf\\gt ";		/* bold type {\hfill} */
char Italic[] = "\\it\\mc ";		/* italic type {\hfill} */
char Roman[] = "\\rm\\mc ";		/* roman type {\hfill} */
char SmallCaps[] = "\\sc\\gt ";		/* small caps type {\hfill} */
char Slant[] = "\\sl\\mc ";		/* slanted type {\hfill} */
char Typewriter[] = "\\tt\\mc ";	/* typewriter type {\hfill} */
#else
#ifdef NTT
/* {\ NTT J\TeX \hfill} */
char Bold[] = "\\bf\\dg ";		/* bold type {\hfill} */
char Italic[] = "\\it\\dm ";		/* italic type {\hfill} */
char Roman[] = "\\rm\\dm ";		/* roman type {\hfill} */
char SmallCaps[] = "\\sc\\dg ";		/* small caps type {\hfill} */
char Slant[] = "\\sl\\dm ";		/* slanted type {\hfill} */
char Typewriter[] = "\\tt\\dm ";	/* typewriter type {\hfill} */
#else
/* {\ \TeX \hfill} */
char Bold[] = "\\bf ";			/* bold type {\hfill} */
char Italic[] = "\\it ";		/* italic type {\hfill} */
char Roman[] = "\\rm ";			/* roman type {\hfill} */
char SmallCaps[] = "\\sc ";		/* small caps type {\hfill} */
char Slant[] = "\\sl ";			/* slanted type {\hfill} */
char Typewriter[] = "\\tt ";		/* typewriter type {\hfill} */
#endif
#endif
char *TextModeFont = Typewriter;	/* text mode font {\hfill} */
char *TeXModeFont = Roman;		/*{\ quasi-\TeX\ and \TeX\ mode
					   font \hfill} */


/* {\hrulefill\ string comparison\ \hrulefill} */
/* {\ Like {\tt strcmp()}, this function {\tt str\_cmp()}
compares two strings stored at integer pointer {\tt iptr}
and character pointer {\tt cptr}.
{\tt Str\_cmp()} returns 0 if and only if given two strings are identical.
\hfill} */

int str_cmp(iptr, cptr)
int  *iptr;
char *cptr;
{
  while ((char)*iptr == *cptr)
    {
      if (*(cptr + 1) == '\0')
	break;
      iptr = inc_buf_ptr(iptr);
      ++cptr;
    }
  return (char)*iptr - *cptr;
}


/* {\hrulefill\ ascii to integer\ \hrulefill} */
/* {\null{\tt Asc2int()} is a substitute of {\tt atoi()}.
We just want to keep our source code closed under {\sl stdio.h}
in order to remain it highly portable.\hfill} */

int asc2int(iptr)
int *iptr;
{
  int value = 0;

  while (((char)*iptr >= '0') && ((char)*iptr <= '9'))
    {
      value = 10 * value + (int)((char)*iptr - '0');
      iptr = inc_buf_ptr(iptr);
    }
  return value;
}


/* {\hrulefill\ search next word\ \hrulefill} */
/* {\ This function {\tt next\_word(iptr1,iptr2)}
simply increases buffer pointer {\tt iptr1}
until either it comes to the first character of the next word
or it coincides with iptr2, and returns {\tt iptr1}.\hfill} */

int *next_word(iptr1, iptr2)
int *iptr1, *iptr2;
{
  if (iptr1 == iptr2)
    return iptr1;
  while ((iptr1 = inc_buf_ptr(iptr1)) != iptr2)
    {
      if (((((char)*(iptr1 - 1) < 'a') || ((char)*(iptr1 - 1) > 'z'))
	   && (((char)*(iptr1 - 1) < '0') || ((char)*(iptr1 - 1) > '9')))
	   && ((((char)*iptr1 >= 'a') && ((char)*iptr1 <= 'z'))
	      || (((char)*iptr1 >= '0') && ((char)*iptr1 <='9'))))
	break;
    }
  return iptr1;
}


/* {\hrulefill\ parse options\ \hrulefill} */
/* {\ {\tt\ Parse\_options()\ } changes several environment variables
of {\bf src2tex} and {\bf src2latex} dynamically.
Since using src2tex escape sequence is pretty tricky,
we have decided to make very strict syntax checking
before parsing options. 
Function {\tt\ parse\_options()\ } returns \ 1\ ,
if src2tex escape sequence is found,
and returns \ 0\  otherwise.\hfill}*/

int parse_options(ptr)
flag_char *ptr;
{
  int brace_counter = 0;
  int error_flag = 0;
  int i, *iptr1, *iptr2, *iptr3;

  iptr1 = ptr->buffer;
  while (((char)*iptr1 != '\\') && ((char)*iptr1 != '\n') && (*iptr1 != EOF))
    {
      if ((((char)*iptr1 >= 'A') && ((char)*iptr1 <= 'Z'))
	  || (((char)*iptr1 >= 'a') && ((char)*iptr1 <= 'z')))
	++error_flag;
      iptr1 = inc_buf_ptr(iptr1);
    }
  /* checking syntax {\hfill} */
  if (str_cmp(iptr1, "\\src2tex{") != 0)
    return 0;
  if (error_flag != 0)
    {
      fprintf(stderr,
	      "\nError: syntax error of src2tex escape sequence\n");
      fprintf(stderr,
	      "       \\src2tex{...} is not written properly\n");
      fprintf(stderr,
	      "       junks are found before \\src2tex sequence\n");
      exit(EXIT_FAILURE);
    }
  if (ptr->flag == 1)
    {
      iptr2 = dec_buf_ptr(iptr1);
      if (*iptr2 != '{')
	{
	  fprintf(stderr,
		  "\nError: syntax error of src2tex escape sequence\n");
	  fprintf(stderr,
		  "       \\src2tex{...} is not written properly\n");
	  fprintf(stderr,
		  "       missing a left brace { \n");
	  exit(EXIT_FAILURE);
	}
    }
  for (i = 0, iptr2 = iptr1; i < 256; ++i, iptr2 = inc_buf_ptr(iptr2))
    {
      if ((char)*iptr2 == '{')
	++brace_counter;
      if ((char)*iptr2 == '}')
	--brace_counter;
      if (((char)*iptr2 == '}') && (brace_counter == 0))
	break;
      if (((char)*iptr2 == '\n') || (*iptr2 == EOF))
	break;
    }
  if (((char)*iptr2 != '}') || (brace_counter != 0))
    {
      fprintf(stderr,
	      "\nError: syntax error of src2tex escape sequence\n");
      fprintf(stderr,
	      "       \\src2tex{...} is not written properly\n");
      fprintf(stderr,
	      "       missing right brace } \n");
      exit(EXIT_FAILURE);
    }
  iptr3 = iptr2;
  while (((char)*iptr3 != '\n') && (*iptr3 != EOF))
    {
      if ((((char)*iptr3 >= 'A') && ((char)*iptr3 <= 'Z'))
	  || (((char)*iptr3 >= 'a') && ((char)*iptr3 <= 'z')))
	{
	  fprintf(stderr,
		  "\nError: syntax error of src2tex escape sequence\n");
	  fprintf(stderr,
		  "       \\src2tex{...} is not written properly\n");
	  fprintf(stderr,
		  "       junks are found after \\src2tex sequence\n");
	  exit(EXIT_FAILURE);
	}
      iptr3 = inc_buf_ptr(iptr3);
    }
#ifdef DEBUGGING
  printf("parse_options():\n");
#endif
  /* start parsing {\hfill} */
  for (i = 1; i == 8; ++i)
    iptr1 = inc_buf_ptr(iptr1);
  iptr1 = next_word(iptr1, iptr2);
  while(iptr1 != iptr2)
    {
      /* parsing htab option {\hfill} */
      if (str_cmp(iptr1, "htab") == 0)
	{
	  iptr1 = next_word(iptr1, iptr2);
	  if (((char)*iptr1 >= '0') && ((char)*iptr1 <= '9'))
	    Htab_Size = asc2int(iptr1);
	}
      /* parsing vtab option {\hfill} */
      if (str_cmp(iptr1, "vtab") == 0)
	{
	  iptr1 = next_word(iptr1, iptr2);
	  if (((char)*iptr1 >= '0') && ((char)*iptr1 <= '9'))
	    Vtab_Size = asc2int(iptr1);
	}
      /* TeXt font {\hfill} */
      if (str_cmp(iptr1, "textfont") == 0)
	{
	  iptr1 = next_word(iptr1, iptr2);
	  if (str_cmp(iptr1, "bf") == 0)
	    TextModeFont = Bold;
	  if (str_cmp(iptr1, "it") == 0)
	    TextModeFont = Italic;
	  if (str_cmp(iptr1, "rm") == 0)
	    TextModeFont = Roman;
	  if (str_cmp(iptr1, "sc") == 0)
	    TextModeFont = SmallCaps;
	  if (str_cmp(iptr1, "sl") == 0)
	    TextModeFont = Slant;
	  if (str_cmp(iptr1, "tt") == 0)
	    TextModeFont = Typewriter;
	}
      /* TeX font {\hfill} */
      if (str_cmp(iptr1, "texfont") == 0)
	{
	  iptr1 = next_word(iptr1, iptr2);
	  if (str_cmp(iptr1, "bf") == 0)
	    TeXModeFont = Bold;
	  if (str_cmp(iptr1, "it") == 0)
	    TeXModeFont = Italic;
	  if (str_cmp(iptr1, "rm") == 0)
	    TeXModeFont = Roman;
	  if (str_cmp(iptr1, "sc") == 0)
	    TeXModeFont = SmallCaps;
	  if (str_cmp(iptr1, "sl") == 0)
	    TeXModeFont = Slant;
	  if (str_cmp(iptr1, "tt") == 0)
	    TeXModeFont = Typewriter;
	}
      iptr1 = next_word(iptr1, iptr2);
    }
#ifdef DEBUGGING
  printf("Htab_Size=%d\n",Htab_Size);
  printf("Vtab_Size=%d\n",Vtab_Size);
  printf("TextModeFont=%s\n",TextModeFont);
  printf("TeXModeFont=%s\n",TeXModeFont);
#endif
  return 1;
}