File: txt2c.c

package info (click to toggle)
clisp 1:2.27-0.5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 49,860 kB
  • ctags: 20,752
  • sloc: ansic: 123,781; lisp: 67,533; asm: 19,633; xml: 11,766; sh: 9,788; fortran: 8,307; makefile: 3,570; objc: 2,481; perl: 1,744; java: 341; yacc: 318; sed: 117
file content (155 lines) | stat: -rw-r--r-- 5,357 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
/* Wandelt ein Textfile mit eingestreuten #if - Bedingungen
   in ein C-Programm um, das dieses Textfile ausgibt.
   Bruno Haible 21.2.1993, 3.1.1995, 15.6.1996
*/

#include <stdlib.h> /* für malloc(), realloc(), exit() */
#include <string.h> /* für strcpy(), strcat() */

#include <stdio.h>
#define fopen_read_ascii  "r"

#define local static
#define global
#define var

/* Directory in which to search files for #include <...> */
char* standard_include_path = NULL;

/* get a line, terminated with '\n', or NULL if EOF is encountered */
/* c is its first character */
local unsigned char * get_line (fp,c)
  FILE * fp;
  int c;
  { int len = 1;
    unsigned char * line = (unsigned char *) malloc(len);
    int index = 0;
    while (1)
      { if (!line) { fprintf(stderr,"Out of memory.\n"); exit(1); }
        line[index++] = c;
        if (c=='\n') break;
        c = getc(fp); if (c==EOF) { free(line); return (unsigned char *) 0; }
        if (index >= len)
          { len = 2*len; line = (unsigned char *) realloc((void*)line,len); }
      }
    return line;
  }

/* put back a line to the pool */
local void put_line (line)
  unsigned char * line;
  { free(line); }

local void process_file (fp)
  FILE * fp;
  { int c;
    while (1)
      { c = getc(fp); if (c==EOF) goto eof;
        if (c=='#')
          { c = getc(fp); if (c==EOF) { putchar('#'); goto eof; }
            if ((c=='i') || (c=='e')) /* Heuristik für #if, #else, #endif, #include */
              /* Zeile unverändert durchlassen, außer bei #include */
              { unsigned char * line = get_line(fp,c);
                if (!line) goto eof;
                if (line[0]=='i' && line[1]=='n' && line[2]=='c' && line[3]=='l'
                    && line[4]=='u' && line[5]=='d' && line[6]=='e' && line[7]==' '
                   )
                  { char pathname[10240];
                    unsigned char expect;
                    unsigned char * ptr = &line[8];
                    if (!(*ptr == '"' || *ptr == '<')) goto illegal_include;
                    expect = (*ptr == '"' ? '"' : '>');
                    ptr++;
                    while (!(*ptr == '"' || *ptr == '>' || *ptr == '\n')) { ptr++; }
                    if (*ptr != expect) goto illegal_include;
                    if (!(ptr[1] == '\n'))
                      illegal_include:
                      { fprintf(stderr,"Invalid #include syntax\n"); exit(1); }
                    ptr[0] = '\0'; /* replace '"' by '\0' */
                    if (expect == '"')
                      /* Search for the file in the current directory. */
                      { strcpy(pathname, (const char *) &line[9]); }
                    else
                      /* Search for the file in the standard_include_path. */
                      { strcpy(pathname,standard_include_path); strcat(pathname, (const char *) &line[9]); }
                   {FILE * ifp = fopen(pathname,fopen_read_ascii);
                    if (!ifp)
                      { fprintf(stderr,"Could not open include file %s\n",pathname); exit(1); }
                    process_file(ifp);
                    fclose(ifp);
                  }}
                  else
                  /* Zeile unverändert durchlassen */
                  { unsigned char * ptr = line;
                    putchar('#');
                    do { c = *ptr++; putchar(c); } while (!(c=='\n'));
                  }
                put_line(line);
                goto line_ok;
              }
            printf("printf(\"#\");\n");
          }
        /* Zeile in eine printf()-Anweisung umwandeln */
        printf("  printf(\"");
        while (1)
          { if ((c=='\\') || (c=='\"'))
              { putchar('\\'); putchar(c); }
#if defined(sun) && !defined(__GNUC__)
            else if (c==0377)
              { putchar('\\'); putchar(c); }
#endif
#ifdef QUOTE_QUOTES
            else if (c=='\'')
              { /* statt "'" ein "\047" ausgeben: */
                putchar('\\');
                putchar('0'+((((unsigned char)'\'')/64)%8));
                putchar('0'+((((unsigned char)'\'')/8)%8));
                putchar('0'+(((unsigned char)'\'')%8));
              }
#endif
            else if (c=='%')
              { putchar(c); putchar(c); }
            else if (c!='\n')
              { putchar(c); }
            else
              { putchar('\\'); putchar('n'); break; }
            c = getc(fp); if (c==EOF) { printf("\");\n"); goto eof; }
          }
        printf("\");\n");
        line_ok: ;
      }
    eof: ;
  }

global int main (argc,argv)
  int argc;
  char* argv[];
  {
    int i;
    for (i = 1; i < argc; i++)
      {
        char* arg = argv[i];
        if (arg[0]=='-' && arg[1]=='I')
          { standard_include_path = &arg[2]; }
        else
          { fprintf(stderr,"Bad option.\n"); exit(1); }
      }
#ifdef CROSS
    printf("#include \"lispbibl.h\"\n");
#else
    printf("#include \"lispbibl.c\"\n");
#endif
    printf("#include <stdio.h>\n");
    printf("#ifdef __cplusplus\n");
    printf("extern \"C\" void exit(int);\n");
    printf("#endif\n");
    printf("\n");
    printf("int main () {\n");
    process_file(stdin);
    printf("  if (fflush(stdout) || ferror(stdout)) { exit(1); }\n");
    printf("  exit(0);\n");
    printf("}\n");
    if (fflush(stdout) || ferror(stdout)) { exit(1); }
    exit(0);
  }