File: tools.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 (503 lines) | stat: -rw-r--r-- 12,689 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* {\hrulefill} *
{\ % beginning of TeX mode

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

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


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


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

extern int TXT_flag;
extern int BAS_flag;
extern int C_flag;
extern int CBL_flag;
extern int F77_flag;
extern int LISP_flag;
extern int MAKE_flag;
extern int PAS_flag;
extern int PERL_flag;
extern int SH_flag;
extern int TCL_flag;

extern int MAP_flag;
extern int MAT_flag;
extern int MLAB_flag;
extern int RED_flag;

extern int *dec_buf_ptr(int *);
extern int *inc_buf_ptr(int *);
extern int *fgetc2buffer(FILE *fptr[]);
extern int *get_phrase(int *, char *);
extern int search_line();
extern int get_comment_flag(int *);
extern int get_tex_flag(int *);
extern int str_cmp();
extern int parse_options();


/* {\hrulefill\ absorb differencies of NTT and ASCII J{\TeX}s\ \hrulefill} */
/* {\ Unfortunately, Japanese {\TeX} is splitted into NTT J{\TeX}
and ASCII J{\TeX}.
Their escape sequences are often different.
So, we write the following lines at the beginning of output {\TeX} file
and absorb those differences.\hfill} */

void merge_ntt_ascii(fptr)
FILE *fptr[];
{
#ifdef ASCII
  /* {\ NTT+ASCII J\TeX \hfill} */
  fprintf(fptr[1], "\\ifx\\gtfam\\undefined\n");
  fprintf(fptr[1], "  \\ifx\\dm\\undefined\n");
  fprintf(fptr[1], "    \\ifx\\tendm\\undefined\n");
  fprintf(fptr[1], "      \\def\\mc{\\null}\n");
  fprintf(fptr[1], "    \\else\n");
  fprintf(fptr[1], "      \\def\\mc{\\tendm}\n");
  fprintf(fptr[1], "    \\fi\n");
  fprintf(fptr[1], "  \\else\n");
  fprintf(fptr[1], "    \\def\\mc{\\dm}\n");
  fprintf(fptr[1], "  \\fi\n");
  fprintf(fptr[1], "  \\ifx\\dg\\undefined\n");
  fprintf(fptr[1], "    \\ifx\\tendg\\undefined\n");
  fprintf(fptr[1], "      \\def\\gt{\\null}\n");
  fprintf(fptr[1], "    \\else\n");
  fprintf(fptr[1], "      \\def\\gt{\\tendg}\n");
  fprintf(fptr[1], "    \\fi\n");
  fprintf(fptr[1], "  \\else\n");
  fprintf(fptr[1], "    \\def\\gt{\\dg}\n");
  fprintf(fptr[1], "  \\fi\n");
  fprintf(fptr[1], "\\fi\n");
  fprintf(fptr[1], "\\ifx\\sc\\undefined\n");
  fprintf(fptr[1], "  \\def\\sc{\\null}\n");
  fprintf(fptr[1], "\\fi\n");
#else
  /* {\ NTT J\TeX \hfill} */
  fprintf(fptr[1], "\\ifx\\dm\\undefined\n");
  fprintf(fptr[1], "  \\def\\dm{\\tendm}\n");
  fprintf(fptr[1], "\\fi\n");
  fprintf(fptr[1], "\\ifx\\dg\\undefined\n");
  fprintf(fptr[1], "  \\def\\dg{\\tendg}\n");
  fprintf(fptr[1], "\\fi\n");
  fprintf(fptr[1], "\\ifx\\sc\\undefined\n");
  fprintf(fptr[1], "  \\def\\sc{\\null}\n");
  fprintf(fptr[1], "\\fi\n");
#endif
}


/* {\hrulefill\ get flagged character\ \hrulefill} */
/* {\rm This function substitutes

$\qquad\displaystyle{\tt ptr->flag}=\cases{
	0 &when text mode\cr
	1 &when quasi-{\TeX} mdoe\cr
	2 &when {\TeX} mode\ ,\cr}$

$\qquad\displaystyle{\tt ptr->character}
	= \hbox{\rm a character read from a given file}$

\noindent
and

$\qquad\displaystyle{\tt ptr->buffer}
	= \hbox{\rm buffer address}\ .$

\noindent
After that, it returns flag\_char pointer {\tt\ ptr\ }.

\noindent
{\it Remark.\ }
In case of PASCAL, it is very important to delay getting mode flag
by observing the value of flag

\qquad{\tt prev\_flag\ .}

\noindent
Since we cannot translate a phrase

\qquad{\tt$\{\backslash$(...$\backslash$)$\}$}

\noindent
or something like that properly without delaying mode transitions. \hfill} */

flag_char *get_flag_char(fptr)
FILE *fptr[];
{
    static flag_char ptr[1];		/* {\ flagged character \hfill} */
    static int prev_flag = 0;		/* {\ previous flag \hfill} */
    int flag;
    int *buf_ptr;

    buf_ptr = fgetc2buffer(fptr);
    flag = get_comment_flag(buf_ptr);
    if ((prev_flag != 0) && (flag != 0) && (TXT_flag == 0))
      if (get_tex_flag(buf_ptr) != 0)
	++flag;
    ptr->flag = flag;
    ptr->character = *buf_ptr;
    ptr->buffer = buf_ptr;
    prev_flag = flag;
    return ptr;
}


/* {\hrulefill\ fprintf documentstyle\ \hrulefill} */
/* {\ This function simply outputs a line of the form}
    \documentstyle[{\tt LATEXOPTION}]{{{\tt LATEXSTYLE}}}
{\ where {\tt LATEXOPTION} and {\tt LATEXSTYLE} are defined
in {\sl src2tex.h}.
If there exists a string ``$\{\backslash$documentstyle....$\}$''
at the beginning of input file,
then {\bf src2latex} moves it to the top of output file.\hfill} */

void fprintf_documentstyle(buf_ptr,fptr)
int *buf_ptr;
FILE *fptr[];
{
  int i, *b_ptr, *tail_ptr;
  char c1, c2, mini_buffer[256];

  b_ptr = get_phrase(buf_ptr,"{\\documentstyle");
  tail_ptr = b_ptr;
  while ((*tail_ptr != '}') && (*tail_ptr >= ' '))
    ++tail_ptr;
  c1 = *(tail_ptr - 1);
  c2 = *tail_ptr;
  if (((char)*b_ptr == 0x00)
      || (c1 < '0') || ((c1 > '9') && (c1 < 'A'))
      || ((c1 > 'Z') && (c1 < 'a')) || (c1 > 'z')
      || (c2 != '}'))
    {
#ifdef LATEXSTYLE
      fprintf(fptr[1],"\\documentstyle");
#ifdef LATEXOPTION
      fprintf(fptr[1],"[");
      fprintf(fptr[1],LATEXOPTION);	/* {\ LaTeX option \hfill} */
      fprintf(fptr[1],"]");
#endif
      fprintf(fptr[1],"{");
      fprintf(fptr[1],LATEXSTYLE);	/* {\ LaTeX style \hfill} */
      fprintf(fptr[1],"}");
      fprintf(fptr[1],"\n");
#endif
#ifdef DEBUGGING
      printf("outputting the default \"\\documentstyle ...\"\n");
#endif
    }
  else
    {
      for (i=0; ((i < 255) && ((char)*b_ptr >= ' ')); ++i)
	{
	  mini_buffer[i] = (char)*++b_ptr;
	  /* {\ Here we replace the original string
	     \ $\backslash$documentstyle ...
	     \ with a string
	     \ $\backslash$null ...
	     . \hfill} */
	  switch(i)
	    {
	    case 0:
	      *b_ptr = '\\';
	      break;
	    case 1:
	      *b_ptr = 'n';
	      break;
	    case 2:
	      *b_ptr = 'u';
	      break;
	    case 3:
	      *b_ptr = 'l';
	      break;
	    case 4:
	      *b_ptr = 'l';
	      break;
	    default:
	      *b_ptr = 0x20;
	    }
	  if (mini_buffer[i] == '}')
	    {
	      ++i;
	      break;
	    }
	}
      mini_buffer[i] = 0x00;
      fprintf(fptr[1],"%s\n", mini_buffer);
#ifdef DEBUGGING
      printf("outputting a string \"%s...\"\n", mini_buffer);
#endif
    }
  fprintf(fptr[1],"\\begin{document}\n");
  fprintf(fptr[1],"\n");
  /* {\ We define sevenrm escape sequence here,
     since it is not defined in some LaTeX systems. \hfill} */
  fprintf(fptr[1], "\\ifx\\sevenrm\\undefined\n");
  fprintf(fptr[1], "  \\font\\sevenrm=cmr7 scaled \\magstep0\n");
  fprintf(fptr[1], "\\fi\n");
}


/* {\hrulefill\ fprintf footline\ \hrulefill} */
/* {\ This function simply outputs a line of the form}
    \footline=$\{$\rm\hfill {\sl file-name} \qquad\folio$\}$
{\ where {\sl file-name} is a string stored at cptr[0].\hfill} */

void fprintf_footline(cptr, fptr)
char *cptr[];
FILE *fptr[];
{
    char *ptr;

    fprintf(fptr[1], "\\footline={\\rm\\hfill ");
    for (ptr = cptr[0]; *ptr != '\0'; ++ptr)
      {
	  switch (*ptr)
	    {
	    case '"':
		fprintf(fptr[1], "{\\tt \"}");
		break;
	    case '#':
		fprintf(fptr[1], "{\\tt\\#}");
		break;
	    case '$':
		fprintf(fptr[1], "{\\tt\\$}");
		break;
	    case '%':
		fprintf(fptr[1], "{\\tt\\%c}", *ptr);
		break;
	    case '&':
		fprintf(fptr[1], "{\\tt\\&}");
		break;
	    case '*':
		fprintf(fptr[1], "{\\tt *}");
		break;
	    case '-':
		fprintf(fptr[1], "{\\tt -}");
		break;
	    case '/':
		fprintf(fptr[1], "{\\tt /}");
		break;
	    case '<':
		fprintf(fptr[1], "{\\tt <}");
		break;
	    case '>':
		fprintf(fptr[1], "{\\tt >}");
		break;
	    case '\\':
		fprintf(fptr[1], "$\\backslash$");
		break;
	    case '^':
		fprintf(fptr[1], "$\\hat{\\ }$");
		break;
	    case '_':
		fprintf(fptr[1], "{\\tt\\_}");
		break;
	    case '{':
		fprintf(fptr[1], "$\\{$");
		break;
	    case '|':
		fprintf(fptr[1], "{\\tt |}");
		break;
	    case '}':
		fprintf(fptr[1], "$\\}$");
		break;
	    case '~':
		fprintf(fptr[1], "$\\tilde{\\ }$");
		break;
	    default:
		fprintf(fptr[1], "%c", *ptr);
	    }
      }
    fprintf(fptr[1], "\\qquad page \\folio}\n");
}


/* {\hrulefill\ input user's style file\ \hrulefill} */
/* {\null
This function {\tt input\_usr\_style()} simply tries to input
either {\sl src2tex.s2t} or {\sl src2latex.s2t}
at the beginning of output operation.
If you want to customize src2tex [resp. src2latex],
it will suffice to write a style file
{\sl src2tex.s2t} [resp. {\sl src2latex.s2t}].\hfill} */

void input_user_style(fptr)
FILE *fptr[];
{
#ifdef LATEX
  fprintf(fptr[1], "\\newread\\MyStyle\n");
#ifndef UNIX
  fprintf(fptr[1], "\\openin\\MyStyle=src2ltex.s2t\n");
#else
  fprintf(fptr[1], "\\openin\\MyStyle=src2latex.s2t\n");
#endif
  fprintf(fptr[1], "\\ifeof\\MyStyle\n");
  fprintf(fptr[1], "  \\closein\\MyStyle\n");
  fprintf(fptr[1], "\\else\n");
#ifndef UNIX
  fprintf(fptr[1], "  \\input src2ltex.s2t\n");
#else
  fprintf(fptr[1], "  \\input src2latex.s2t\n");
#endif
  fprintf(fptr[1], "  \\closein\\MyStyle\n");
  fprintf(fptr[1], "\\fi\n");
#ifdef DEBUGGING
  printf ("input_user_style(): src2latex.s2t is included\n");
#endif
#else
  fprintf(fptr[1], "\\newread\\MyStyle\n");
  fprintf(fptr[1], "\\openin\\MyStyle=src2tex.s2t\n");
  fprintf(fptr[1], "\\ifeof\\MyStyle\n");
  fprintf(fptr[1], "  \\closein\\MyStyle\n");
  fprintf(fptr[1], "\\else\n");
  fprintf(fptr[1], "  \\input src2tex.s2t\n");
  fprintf(fptr[1], "  \\closein\\MyStyle\n");
  fprintf(fptr[1], "\\fi\n");
#ifdef DEBUGGING
  printf ("input_user_style(): src2tex.s2t is included\n");
#endif
#endif
}


/* {\hrulefill\ set TT\_flag of text2tex()\ \hrulefill} */
/* {\null
This function {\tt choose\_tt\_font()} returns 1 if and only if
it is better to choose typewriter font in quasi-{\TeX} mode.
Actually, this function tries to determine whether or not
the user prefers cmtt font to cmr font.
\hfill} */

int choose_tt_font(buf_ptr)
int *buf_ptr;
{
  int *b_ptr, char_counter, tt_flag, tex_flag;
  int line_length0, line_length1, line_length2;

  char_counter = 0;
  line_length0 = 0;
  line_length1 = 0;
  line_length2 = 0;
  tt_flag = 0;
  tex_flag = 0;
  for (b_ptr = buf_ptr; b_ptr != buf_ptr + (int)(BUFFER_SIZE / 2); ++b_ptr)
    {
      if (*b_ptr == '\t')
	char_counter += (int)(HTAB_SKIP) - (char_counter % (int)(HTAB_SKIP));
      else
	++char_counter;
      if (((*b_ptr == '{') && (*(b_ptr + 1) == '\\')) || (*b_ptr == '$'))
	++tex_flag;
#ifdef UNIX
      if (*b_ptr != '\n')
	continue;
#else
      if (*b_ptr != '\r')
	continue;
#endif
      else
	{
	  line_length0 = line_length1;
	  line_length1 = line_length2;
	  if (tex_flag == 0)
	    line_length2 = char_counter;
	  else
	    line_length2 = 0;
	  char_counter = 0;
	  tex_flag = 0;
	}
      if ((line_length0 == 0) || (line_length1 == 0) || (line_length2 == 0)
	  || (line_length0 != line_length1) || (line_length1 != line_length2))
	continue;
      if (C_flag != 0)
	{
	  if ((*(b_ptr - 2) == '*') && (*(b_ptr -1) == '/'))
	    ++tt_flag;
	  if ((*(b_ptr - 2) == '/') && (*(b_ptr -1) == '/'))
	    ++tt_flag;
	}
      if (CBL_flag != 0)
	{
	  if (*(b_ptr - 1) == '*')
	    ++tt_flag;
	}
      if (F77_flag != 0)
	{
	  if (*(b_ptr - 1) == '*')
	    ++tt_flag;
	}
      if (PAS_flag != 0)
	{
	  if ((*(b_ptr - 2) == '*') && (*(b_ptr -1) == ')'))
	    ++tt_flag;
	  if (*(b_ptr - 1) == '}')
	    ++tt_flag;
	}
      if (MAP_flag != 0)
	{
	  if (*(b_ptr - 1) == '#')
	    ++tt_flag;
	}
      if (MAT_flag != 0)
	{
	  if ((*(b_ptr - 2) == '*') && (*(b_ptr -1) == ')'))
	    ++tt_flag;
	}
      if (MLAB_flag != 0)
	{
	  if (*(b_ptr - 1) == '#')
	    ++tt_flag;
	  if (*(b_ptr - 1) == '%')
	    ++tt_flag;
	}
      if (tt_flag != 0)
	break;
    }
#ifdef DEBUGGING
  printf ("choose_tt_font():\n");
  printf ("TT_flag =%d\n", tt_flag);
#endif
  return tt_flag;
}

/* {\hrulefill\ fprintf\ line\ numer\ \hrulefill} */
/* {\null
This function {\tt fprintf\_line\_numer()} simply prints line number
at the beginning of each line.
\hfill} */

void fprintf_line_number(fptr, line_counter)
FILE *fptr[];
long line_counter;
{
  if (line_counter < 9)
    fprintf(fptr[1],
	    "\n\n{\\tt\\noindent\\phantom{00000}%d:\\ }\n",
	    line_counter + 1);
  if ((line_counter >= 9) && (line_counter < 99))
    fprintf(fptr[1],
	    "\n\n{\\tt\\noindent\\phantom{0000}%d:\\ }\n",
	    line_counter + 1);
  if ((line_counter >= 99) && (line_counter < 999))
    fprintf(fptr[1],
	    "\n\n{\\tt\\noindent\\phantom{000}%d:\\ }\n",
	    line_counter + 1);
  if ((line_counter >= 999) && (line_counter < 9999))
    fprintf(fptr[1],
	    "\n\n{\\tt\\noindent\\phantom{00}%d:\\ }\n",
	    line_counter + 1);
  if ((line_counter >= 9999) && (line_counter < 99999))
    fprintf(fptr[1],
	    "\n\n{\\tt\\noindent\\phantom{0}%d:\\ }\n",
	    line_counter + 1);
  if (line_counter >= 99999)
    fprintf(fptr[1],
	    "\n\n{\\tt %d:\\ }\n",
	    line_counter + 1);
}