summaryrefslogtreecommitdiff
path: root/web/schemeweb/sweb.c
blob: fa882e91821de0fc07d799efdc135a24f0082f40 (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
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
/* SchemeWEB -- WEB for Lisp.  John D. Ramsdell.
 * Simple support for literate programming in Lisp.
 */

/* 	$Id: sweb.c,v 2.1 94/07/21 11:30:36 ramsdell Exp $	 */

#ifndef lint
static char vcid[] = "$Id: sweb.c,v 2.1 94/07/21 11:30:36 ramsdell Exp $";
static char copyright[] = "Copyright 1994 by The MITRE Corporation.";
#endif /* lint */

#define VERSION "2.1"

/*
 * Copyright 1994 by The MITRE Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 1, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * For a copy of the GNU General Public License, write to the 
 * Free Software Foundation, Inc., 675 Mass Ave, 
 * Cambridge, MA 02139, USA.
 */

/*
This program processes SchemeWEB files.  A SchemeWEB file is a Lisp
source file which contains code sections and comment sections, but
each section is identified in a novel way.  A code section begins with
a line whose first character is a left parenthesis.  It continues
until a line is found which contains the parenthesis that matches the
one which started the code section.  The remaining lines of text in
the source file are treated as comments.  Several operations involving
SchemeWEB files are provided by the this program.  See the manual
page for a complete description of the various operations.
*/

/* SchemeWEB is currently set up for use with LaTeX. */

/* Define TANGLE to make a program which translates SchemeWEB source
into Scheme source by default. */

/* Define SAVE_LEADING_SEMICOLON if you want text lines to be copied 
with any leading semicolon while weaving. */

#include <stdio.h>

typedef enum {FALSE, TRUE} bool;

/* Runtime flags */
bool weaving;			/* Weaving or tangling? */
bool strip_comments;		/* Strip comments while tangling. */

/* Formatting commands added into weaved documents. */
char *begin_comment = "\\mbox{"; /* This pair is used */
char *end_comment = "}";	/* to surround comments in code. */
char *begin_code = "\\begin{flushleft}\n"; /* This pair is used */
char *end_code = "\\end{flushleft}\n"; /* to surround code. */
char *code_line_separator = "\\\\ ";
char *begin_code_line = "\\verb|"; /* This pair is used */
char *end_code_line = "|";	/* to surround code lines. */

/* Information for error messages. */
char *prog = NULL;		/* Name of program. */
char *src = NULL;		/* Name of input file. */
int lineno = 1;			/* Line number. */

/* Output occurs through putchar, putstring, and code_putchar. */

#define putstring(s) (fputs(s, stdout))

int				/* Used while printing */
code_putchar(c)			/* a code section. */
     int c;
{
  if (c == '|' && weaving) return putstring("|\\verb-|-\\verb|");
  else return putchar(c);
}

/* All input occurs in the following routines so that TAB characters
can be expanded while weaving. TeX treats TAB characters as a
space--not what is wanted. */

int ch_buf;			/* Used to implement */
bool buf_used = FALSE;		/* one character push back. */

int 
getchr()
{
  int c;
  static int spaces = 0;	/* Spaces left to print a TAB. */
  static int column = 0;	/* Current input column. */
  if (buf_used) {
    buf_used = FALSE;
    return ch_buf;
  }
  if (spaces > 0) {
    spaces--;
    return ' ';
  }
  switch (c = getc(stdin)) {
  case '\t':
    if (!weaving) return c;
    spaces = 7 - (7&column);	/* Maybe this should be 7&(~column). */
    column += spaces + 1;
    return ' ';
  case '\n':
    lineno++;
    column = 0;
    return c;
  default:
    column++;
    return c;
  }
}

void 
ungetchr(c)
     int c;
{
  buf_used = TRUE;
  ch_buf = c;
}

/* Error message for end of file found in code. */
bool 
report_eof_in_code()
{
  fprintf(stderr, "End of file within a code section.\n");
  return TRUE;
}

bool 
copy_text_saw_eof()		/* Copies a line of text out. */
{				/* Used while printing */
  int c;			/* a text section. */
  while (1) {
    c = getchr();
    if (c == EOF) return TRUE;
    if (c == '\n') return FALSE;
    putchar(c);
  }
}

bool 
strip_text_saw_eof()		/* Gobbles up a line of input. */
{
  int c;
  while (1) {
    c = getchr();
    if (c == EOF) return TRUE;
    if (c == '\n') return FALSE;
  }
}

bool				/* This copies comments */
copy_comment_saw_eof()		/* within code sections. */
{				
  if (weaving) putstring(begin_comment);
  putchar(';');
  if (copy_text_saw_eof()) return TRUE;
  if (weaving) putstring(end_comment);
  return FALSE;
}

bool				/* Copies a string found */
copy_string_saw_eof()		/* within a code section. */
{
  int c;
  while (1) {
    c = getchr();
    if (c == EOF) return TRUE;
    if (c == '\n') {		/* Found a string which continues on */
      putstring(end_code_line);	/* a new line. */
      putchar(c);		/* Close existing line, and then */
      putstring(code_line_separator); /* begin copying the rest of */
      putstring(begin_code_line); /* on the next line. */
      continue;
    }
    code_putchar(c);
    switch (c) {
    case '"': return FALSE;
    case '\\':
      c = getchr();
      if (c == EOF) return TRUE;
      code_putchar(c);
    }
  }
}

bool 
maybe_char_syntax_saw_eof()
{				/* Makes sure that the character */
  int c;			/* #\( does not get counted in */
  c = getchr();			/* balancing parentheses. */
  if (c == EOF) return TRUE;
  if (c != '\\') {
    ungetchr(c);
    return FALSE;
  }
  code_putchar(c);
  c = getchr();
  if (c == EOF) return TRUE;
  code_putchar(c);
  return FALSE;
}

bool				/* Copies a code section */
copy_code_failed()		/* containing S-exprs. */
{
  int parens = 1;		/* Used to balance parentheses. */
  int c;
  while (1) {			/* While parens are not balanced, */
    c = getchr();
    if (c == EOF)		/* Report failure on EOF. */
      return report_eof_in_code();
    if (c == '\n' && weaving)
      putstring(end_code_line);
    if (c == ';') { 		/* Report failure on EOF in a comment. */
      if (weaving) putstring(end_code_line);
      if (strip_comments
	  ? strip_text_saw_eof()
	  : copy_comment_saw_eof())
	return report_eof_in_code();
      else
	c = '\n';
    }
    code_putchar(c);		/* Write the character and then see */
    switch (c) {		/* if it requires special handling. */
    case '(':
      parens++;
      break;
    case ')':
      parens--;			
      if (parens < 0) {
	fprintf(stderr, "Too many right parentheses found.\n");
	return TRUE;
      }
      break;
    case '"':			/* Report failure on EOF in a string. */
      if (copy_string_saw_eof()) {
	fprintf(stderr, "End of file found within a string.\n");
	return TRUE;
      }
      break;
    case '#':			/* Report failure on EOF in a character. */
      if (maybe_char_syntax_saw_eof())
	return report_eof_in_code();
      break;
    case '\n':
      if (parens == 0) return FALSE;
      if (weaving) {
	putstring(code_line_separator);
	putstring(begin_code_line);
      }
    }
  }
}

int 
schemeweb()
{
  int c;
  while (1) {			/* At loop start it's in text mode */
    c = getchr();		/* and at the begining of a line. */
    if (c == '(') {		/* text mode changed to code mode. */
      if (weaving) putstring(begin_code);
      do {			/* Copy code. */
	if (weaving) putstring(begin_code_line);
	putchar(c);
	if (copy_code_failed()) {
	  fputs(prog, stderr);
	  if (src != NULL)
	    fprintf(stderr, ":%s:", src);
	  else
	    fputs(":<stdin>:", stderr);
	  fprintf(stderr,
		  "%d: Error in a code section.\n",
		  lineno);
	  return 1;
	}
	c = getchr();		/* Repeat when there is code */
      } while (c == '(');	/* immediately after some code. */
      if (weaving) putstring(end_code);
    }
    /* Found a text line--now in text mode. */
#if !defined SAVE_LEADING_SEMICOLON
    if (c == ';' && weaving)
      c = getchr();
#endif
    if (c == EOF) return 0;	/* Files that do not end with */
    ungetchr(c);		/* a newline are okay. */

    if (strip_comments) {
      if (strip_text_saw_eof()) return 0;
    }
    else {
      if (c != '\n' && !weaving) putchar(';');
      if (copy_text_saw_eof()) return 0; /* Copy a text line. */
      putchar('\n');
    }
  }
}

int				/* Removes any semicolons */
untangle()			/* than start a line of text. */
{
  int c;
  
  while (1) {			/* At a beginning of a line of text */
    c = getchar();		/* when at this point in the code. */
    if (c == EOF) return 0;
    if (c != ';') putchar(c);
    while (c != '\n') {
      c = getchar();
      if (c == EOF) return 0;
      putchar(c);
    }
  }
}

bool				/* Open the file arguments */
open_file_args_failed(argc, argv)
     int argc;
     char *argv[];
{
  switch (argc) {
  case 2:
  case 1:
    src = argv[0];		/* Save for error messages. */
    if (NULL == freopen(argv[0], "r", stdin)) {
      fprintf(stderr, "Cannot open %s for reading.\n", argv[0]);
      break;
    }
    if (argc == 2 && NULL == freopen(argv[1], "w", stdout)) {
      fprintf(stderr, "Cannot open %s for writing.\n", argv[1]);
      break;
    }
  case 0:
    return FALSE;
  }
  return TRUE;
}

int 
usage()
{
  fprintf(stderr, 
	  "Usage: %s [-stuvwx] [input_file [output_file]]\n%s%s%s%s%s%s",
	  prog,
	  "\t-s:  tangle input stripping comments\n",
	  "\t-t:  tangle input retaining comments\n",
	  "\t-u:  untangle input\n",
	  "\t-v:  print version information\n",
	  "\t-w:  weave input\n",
	  "\t-x:  weave input and exclude line breaks in code sections\n");
  fprintf(stderr, "The default option is %s.\n",
#if defined TANGLE
	  "-t"
#else
	  "-w"
#endif
	  );
  return 1;
}

int 
main (argc, argv)
     int argc;
     char *argv[];
{
  bool untangling = FALSE;
#if defined TANGLE
  weaving = FALSE;
#else
  weaving = TRUE;
#endif
  strip_comments = FALSE;

  prog = argv[0];		/* Save program name for error messages. */

  /* Option processing.  Note only one option can be requested at a time. */
  /* -s: tangle input stripping comments. */
  /* -t: tangle input retaining comments. */
  /* -u: untangle input. */
  /* -v: print version information. */
  /* -w: weave input. */
  /* -x: weave input and exclude line breaks in code sections. */
  if (argc > 1 && argv[1][0] == '-') {
    switch (argv[1][1]) {
    case 's': weaving = FALSE; strip_comments = TRUE; break;
    case 't': weaving = FALSE; break;
    case 'u': untangling = TRUE; break;
    case 'v':
      fprintf(stderr, "This is SchemeWEB version %s.\n", VERSION);
      return 0;
    case 'w': weaving = TRUE; break;
    case 'x': weaving = TRUE; code_line_separator = "\\\\* "; break;
    default:
      fprintf(stderr, "Bad option: -%c.\n", argv[1][1]);
      return usage();
    }
    if (argv[1][2] != '\0') {
      fprintf(stderr, "Only one option allowed.\n");
      return usage();
    }
    argc--; argv++;
  }

  if (open_file_args_failed(argc - 1, argv + 1)) return usage();

  if (untangling) return untangle();
  return schemeweb();
}