summaryrefslogtreecommitdiff
path: root/web/noweb/src/c/markmain.c
blob: 5e63bba06ea60d4c6ef2aa207f933c6383d036fb (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
#line 66 "markmain.nw"
static char rcsid[] = "$Id: markmain.nw,v 2.29 2008/10/06 01:03:05 nr Exp nr $";
static char rcsname[] = "$Name: v2_12 $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "errors.h"
#include "markup.h"
#include "getline.h"
#include "columns.h"

#line 35 "markmain.nw"
typedef enum state {Code=1, Docs=2, CodeIndex=3} State;
typedef enum mark {Begin=1, End=2} Mark;
typedef enum index {Defn=1, Use=2, Newline=3} Index;

static char *states[]  = { "bad state", "code", "docs", "code" };
static char *marks[]   = { "bad mark", "begin", "end" };
static char *indices[] = { "bad index", "defn", "use", "nl" };
static char low_at_sign = '@';

static void print_state(FILE *out, Mark be, State state, int count) {
    fprintf(out, "%c%s %s %d\n", low_at_sign, marks[be], states[state], count);
}

static void print_index(FILE *out, Index idx, char *arg) {
    if (arg)
        fprintf(out, "%cindex %s %s\n", low_at_sign, indices[idx], arg);
    else
        fprintf(out, "%cindex %s\n",    low_at_sign, indices[idx]);
}

static void print_pair(FILE *out, char *name, char *value) {
    if (value) {
        int last=strlen(value)-1;
        if (last>=0 && value[last]=='\n')
            fprintf(out, "%c%s %s%cnl\n", low_at_sign, name, value, low_at_sign);
        else
            fprintf(out, "%c%s %s\n", low_at_sign, name, value);
    } else
        fprintf(out, "%c%s\n", low_at_sign, name);
}

#line 80 "markmain.nw"
void markup (FILE* in, FILE *out, char *filename) {
    State state = Docs;         /* what we are reading */
    int quoting = 0;            /* currently quoting code? */
    int count = 0;              /* number of current chunk, from 0 */
    int missing_newline = 0;    /* was last line missing a trailing \n? */
    int lineno = 0;             /* number of lines read */
    int last_open_quote = -1;   /* line number of last unmatched open quote */

    char *line;                 /* part of line up to mark (or end) */
#define MAX_MODNAME 255
    char modname[MAX_MODNAME+1] = ""; /* name of module currently being read, 
                                         [[""]] if no module is being read */ 

    
#line 120 "markmain.nw"
print_pair(out, "file", filename);
print_state(out, Begin, state, count);
while ((line = getline_expand(in)) != NULL) {
    lineno++;
    
#line 171 "markmain.nw"
missing_newline = line[strlen(line)-1] != '\n';
#line 125 "markmain.nw"
    if (starts_code(line, filename, lineno)) {
        
#line 541 "markmain.nw"
if (quoting) {
    errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
    quoting = 0;
}
#line 127 "markmain.nw"
        print_state(out, End, state, count);
        count++;
        state = Code;
        print_state(out, Begin, state, count);
        getmodname(modname,MAX_MODNAME,line);
        print_pair(out,"defn",modname);
        print_pair(out,"nl",0);     /* could be implicit but this is better */
    } else if (is_def(line)) {
        
#line 527 "markmain.nw"
line = remove_def_marker(line);
while (*line && isspace(*line)) line++;
while (*line) {
  char tmp;
  char *s = line+1;
  while (*s && !isspace(*s)) s++;
  tmp = *s; *s = 0;
  print_index(out, Defn, line);
  *s = tmp;
  while (isspace(*s)) s++;
  line = s;
}
print_index(out, Newline, 0);
#line 136 "markmain.nw"
        if (state == Code)
            state = CodeIndex;
    } else {
        if (starts_doc(line) || state == CodeIndex) {
            
#line 541 "markmain.nw"
if (quoting) {
    errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
    quoting = 0;
}
#line 141 "markmain.nw"
            print_state(out, End, state, count);
            count++;
            state = Docs;       /* always reading docs after a stop */
            print_state(out, Begin, state, count);
            if (starts_doc(line))
                line = first_doc_line(line);
        }
        {   
#line 180 "markmain.nw"
char *p, *s, *t, c;
static char *buf;
static int buflen = 0;
#line 149 "markmain.nw"
            
#line 258 "markmain.nw"
#define LA1 '<'
#define LA2 '<'
#define RA1 '>'
#define RA2 '>'
#define LS1 '['
#define LS2 '['
#define RS1 ']'
#define RS2 ']'
#define ESC '@'
#line 271 "markmain.nw"
#define next(L) do {           c = *t++; goto L; } while (0)
#define copy(L) do { *s++ = c; c = *t++; goto L; } while (0)
#line 277 "markmain.nw"
#define lexassert(E) ((void)0)
#line 150 "markmain.nw"
            
#line 504 "markmain.nw"
if (buf == NULL)
  checkptr(buf = (char *) malloc(buflen = 128));
if (buflen < strlen(line) + 1 + 2) {
  while (buflen < strlen(line) + 1 + 2) 
    buflen *= 2;
  checkptr(buf = (char *) realloc(buf, buflen));
}
#line 185 "markmain.nw"
p = s = buf+2;
t = line;
c = *t++;
#line 151 "markmain.nw"
            
#line 205 "markmain.nw"
if (c == ESC && *t == ESC) { *s++ = ESC; c = *++t; ++t; }
#line 152 "markmain.nw"
            if (state == Code || quoting) 
                goto convert_code;
            else
                goto convert_docs;
            
#line 295 "markmain.nw"
convert_docs:
t: lexassert(state == Docs && !quoting);
   if (c == ESC) next(at);
   if (c == LA1) next(la);
   if (c == LS1) next(ls);
   
#line 241 "markmain.nw"
if (c == '\0') {
  
#line 512 "markmain.nw"
if (s > p) {
  *s = 0;
  print_pair(out, "text", p);
}
s = p = buf + 2;
#line 243 "markmain.nw"
  goto done_converting;
}
#line 301 "markmain.nw"
   copy(t);
#line 309 "markmain.nw"
at: if (c == LA1) next(atla);
    if (c == LS1) next(atls);
    if (c == RA1) next(atra);
    if (c == RS1) next(atrs);
    *s++ = ESC; goto t;
#line 318 "markmain.nw"
atls: if (c == LS2) { *s++ = LS1; *s++ = LS2; next(t); }
     *s++ = ESC; *s++ = LS1; goto t;
#line 324 "markmain.nw"
atla: if (c == LA2) { *s++ = LA1; *s++ = LA2; next(t); }
      *s++ = ESC; *s++ = LA1; goto t;
#line 330 "markmain.nw"
atrs: if (c == RS2) { *s++ = RS1; *s++ = RS2; next(t); }
     *s++ = ESC; *s++ = RS1; goto t;
#line 336 "markmain.nw"
atra: if (c == RA2) { *s++ = RA1; *s++ = RA2; next(t); }
      *s++ = ESC; *s++ = RA1; goto t;
#line 342 "markmain.nw"
la: if (c == LA2) {
      
#line 524 "markmain.nw"
errorat(filename, lineno, Error, "unescaped << in documentation chunk");
#line 344 "markmain.nw"
      *s++ = LA1; *s++ = LA2; next(t);
    }
    *s++ = LA1; goto t;
#line 354 "markmain.nw"
ls: lexassert(state == Docs);
    if (c == LS2) {
      
#line 512 "markmain.nw"
if (s > p) {
  *s = 0;
  print_pair(out, "text", p);
}
s = p = buf + 2;
#line 357 "markmain.nw"
      quoting = 1; last_open_quote = lineno; print_pair(out, "quote", 0); 
      next(c);
    }
    *s++ = LS1;
    goto t;
#line 371 "markmain.nw"
convert_code:
c: lexassert(state == Code || quoting);
   if (c == RS1 && quoting) next(crs);
   if (c == LA1) next(cla);
   if (c == ESC) next(cat);
   
#line 241 "markmain.nw"
if (c == '\0') {
  
#line 512 "markmain.nw"
if (s > p) {
  *s = 0;
  print_pair(out, "text", p);
}
s = p = buf + 2;
#line 243 "markmain.nw"
  goto done_converting;
}
#line 377 "markmain.nw"
   copy(c);
#line 385 "markmain.nw"
cat: if (c == LA1) next(catla);
     if (c == RA1) next(catra);
     *s++ = ESC; goto c;
#line 392 "markmain.nw"
catla: if (c == LA2) { *s++ = LA1; *s++ = LA2; next(c); }
       *s++ = ESC; *s++ = LA1; goto c;
#line 398 "markmain.nw"
catra: if (c == RA2) { *s++ = RA1; *s++ = RA2; next(c); }
       *s++ = ESC; *s++ = RA1; goto c;
#line 406 "markmain.nw"
crs: if (c == RS2) next(ce);
     *s++ = RS1; goto c;
#line 412 "markmain.nw"
ce: lexassert(quoting);
    if (c == RS2) copy(ce);
    quoting = 0; 
#line 512 "markmain.nw"
if (s > p) {
  *s = 0;
  print_pair(out, "text", p);
}
s = p = buf + 2;
#line 414 "markmain.nw"
                                                 print_pair(out, "endquote", 0);
    goto t;
#line 422 "markmain.nw"
cla: if (c == LA2) { 
#line 512 "markmain.nw"
if (s > p) {
  *s = 0;
  print_pair(out, "text", p);
}
s = p = buf + 2;
#line 422 "markmain.nw"
                                                     next(u); }
     *s++ = LA1; goto c;
#line 433 "markmain.nw"
u: lexassert(state == Code || quoting);
   if (c == LS1) copy(uls);
   if (c == RS1 && quoting) next(urs);
   if (c == RA1) next(ura);
   if (c == '\0') { /* premature end --- it's not a use after all */
     
#line 499 "markmain.nw"
lexassert(p == buf + 2);
p -= 2;
p[0] = LA1;
p[1] = LA2;
#line 439 "markmain.nw"
     goto c;
   }
   copy(u);
#line 446 "markmain.nw"
urs: lexassert(quoting);
     if (c == RS2) { /* premature end --- it's not a use after all */
       
#line 499 "markmain.nw"
lexassert(p == buf + 2);
p -= 2;
p[0] = LA1;
p[1] = LA2;
#line 449 "markmain.nw"
       next(ce); 
     }
     *s++ = RS1; goto u;
#line 456 "markmain.nw"
uls: if (c == LS2) copy(uc);
     goto u;
#line 463 "markmain.nw"
uc: lexassert(quoting || state == Code);
    if (c == RS1) copy(ucrs);
    if (c == '\0') {
      
#line 499 "markmain.nw"
lexassert(p == buf + 2);
p -= 2;
p[0] = LA1;
p[1] = LA2;
#line 467 "markmain.nw"
      goto c;
    }
    copy(uc);
#line 474 "markmain.nw"
ucrs: if (c == RS2) copy(uce);
      goto uc;
#line 480 "markmain.nw"
uce: if (c == RS2) copy(uce);
     goto u;
#line 486 "markmain.nw"
ura: if (c == RA2) { 
#line 519 "markmain.nw"
*s = 0;
print_pair(out, "use", p);
s = p = buf + 2;
#line 486 "markmain.nw"
                                                    next(c); }
     *s++ = RA1; goto u;
#line 157 "markmain.nw"
          done_converting: 
            (void)0;
        }
    }
}
#line 541 "markmain.nw"
if (quoting) {
    errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
    quoting = 0;
}
#line 173 "markmain.nw"
if (missing_newline) print_pair(out, "nl",0);
#line 164 "markmain.nw"
print_state(out, End, state, count);
#line 94 "markmain.nw"
}
#line 550 "markmain.nw"
int main(int argc, char **argv) {
    FILE *fp;
    int i;

    progname = argv[0];	
    for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
        switch(argv[i][1]) {
            case 't': 
#line 583 "markmain.nw"
if (isdigit(argv[i][2]))
    tabsize = atoi(argv[i]+2);
else if (argv[i][2]==0)
    tabsize = 0;                /* no tabs */
else 
    errormsg(Error, "%s: ill-formed option %s", progname, argv[i]);
#line 557 "markmain.nw"
                                        break;
            default : errormsg(Error, "%s: unknown option -%c", progname, argv[i][1]);
                      break;
        }
    if (i < argc)
        for (; i < argc; i++) {
            if (!strcmp(argv[i], "-")) {
                markup(stdin,stdout,"");
            } else 
                if ((fp=fopen(argv[i],"r"))==NULL) {
                    errormsg(Error, "%s: couldn't open file %s", progname, argv[i]);
                    fprintf(stdout, "@fatal couldn't open file %s\n", argv[i]);
                } else {
                    markup(fp,stdout,argv[i]);
                    fclose(fp);
                }
        }
    else
        markup(stdin,stdout,"");
    nowebexit(NULL);
    (void)rcsid; /* avoid a warning */
    (void)rcsname; /* avoid a warning */
    return errorlevel;          /* slay warning */
}