home *** CD-ROM | disk | FTP | other *** search
- #line 66 "markmain.nw"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "errors.h"
- #include "markup.h"
- #include "getline.h"
- #include "columns.h"
-
- #line 34 "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 77 "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; /* was last line missing a trailing \n? */
- int lineno = 0; /* number of lines read */
- int last_open_quote; /* line number of last unmatched open quote */
-
- char *line; /* part of line up to mark (or end) */
- char *mark; /* potentially marked part of line */
- #define MAX_MODNAME 255
- char modname[MAX_MODNAME+1] = ""; /* name of module currently being read,
- [[""]] if no module is being read */
-
-
- #line 118 "markmain.nw"
- print_pair(out, "file", filename);
- print_state(out, Begin, state, count);
- while ((line = getline_expand(in)) != NULL) {
- lineno++;
-
- #line 161 "markmain.nw"
- missing_newline = line[strlen(line)-1] != '\n';
- #line 123 "markmain.nw"
- if (starts_code(line, filename, lineno)) {
-
- #line 216 "markmain.nw"
- if (quoting) {
- errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
- quoting = 0;
- }
- #line 125 "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 202 "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 134 "markmain.nw"
- if (state == Code)
- state = CodeIndex;
- } else {
- if (starts_doc(line) || state == CodeIndex) {
-
- #line 216 "markmain.nw"
- if (quoting) {
- errorat(filename, last_open_quote, Warning, "open quote `[[' never closed");
- quoting = 0;
- }
- #line 139 "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);
- }
- switch(state) {
- case Code:
- #line 168 "markmain.nw"
- while ((mark = mod_start(line,1)) != NULL) {
- if (*line) print_pair(out, "text",unescape(line));
- /* emit string before module */
- if ((line=mod_end(mark,1))==NULL) { /* no matching >>; treat << as text */
- line = mark - 2; /* back up; point to << */
- *line = '<'; /* restore missing < (was '\0') */
- break;
- } else {
- print_pair(out, "use",mark); /* emit the module */
- }
- }
- if (*line) print_pair(out, "text",unescape(line));
- /* contribute the trailing string */
- #line 147 "markmain.nw"
- break;
- case Docs:
- #line 184 "markmain.nw"
- for (; (mark = (quoting ? quote_end : quote_start)(line,1)) != NULL;
- line = mark) {
- if (!quoting) {
- #line 197 "markmain.nw"
- if (mod_start(line,0) != NULL)
- errorat(filename, lineno, Warning,
- "Warning: chunk name in documentation (or missing = in definition)");
- #line 186 "markmain.nw"
- }
- if (*line) print_pair(out, "text", unescape(line));
- print_pair(out, quoting ? "endquote" : "quote", 0);
- if (quoting = !quoting) last_open_quote = lineno;
- }
- if (!quoting) {
- #line 197 "markmain.nw"
- if (mod_start(line,0) != NULL)
- errorat(filename, lineno, Warning,
- "Warning: chunk name in documentation (or missing = in definition)");
- #line 191 "markmain.nw"
- }
- if (*line) print_pair(out, "text", unescape(line));
- #line 148 "markmain.nw"
- break;
- default: impossible("bad state");
- }
- }
- }
- #line 163 "markmain.nw"
- if (missing_newline) print_pair(out, "nl",0);
- #line 154 "markmain.nw"
- print_state(out, End, state, count);
- #line 92 "markmain.nw"
- }
- #line 225 "markmain.nw"
- #ifdef macintosh
- #include <QuickDraw.h>
- #endif
-
- main(int argc, char **argv) {
- FILE *fp;
- char *myself=*argv;
- int i;
-
- #ifdef macintosh
- InitGraf(&qd.thePort);
- #endif
-
- for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1] != 0; i++)
- switch(argv[i][1]) {
- case 't':
- #line 256 "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\n", myself, argv[i]);
- #line 233 "markmain.nw"
- break;
- default : errormsg(Error, "%s: unknown option -%c\n", myself, 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\n", myself, argv[i]);
- else {
- markup(fp,stdout,argv[i]);
- fclose(fp);
- }
- }
- else
- markup(stdin,stdout,"");
- exit(errorlevel);
- return errorlevel; /* slay warning */
- }
-