summaryrefslogtreecommitdiff
path: root/contrib/webcscope/hilite.c
blob: 4f5af07035ffa3d29fd9ccd4eb9594584796d447 (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
/*
 CopyRight (C) 1999, Dmitry Obukhov, dso@usa.net
 mailto: dso@usa.net
 http://www.EmbeddedStuff.com

 ----------------------------------------------
 Last modified 6 Apr 97
 ----------------------------------------------
 Converts C (C++) source to HTML code fragment
 with syntax highlighting.
 Since program written for personal purpose
 the <TABLE> tags generated. This is optional
 page format specific thing.

 Usage: CTHM <input_file>. All output is done
 to STDOUTPUT, error messages to STDERR.
 For HTML fragment generation:
     CHTM file.c > file.htm

 - Some input convertion required to use this
   code as CGI module. Will be done soon.
 - Optimization required for blocks of EOL
   comments
*/

#include <stdio.h>

// ------------------- Decoding status values

#define START        0
#define INLINE       1
#define DEFINE       2
// ------------------- Decoding Remark
#define REM1        20
#define REM2        21
#define REM_END     22
#define REM_STAR    23
#define REM_STAR_1  24
#define STRING      25 // String is "like" remark


// ------------------- HTML TAG Generation
#define ON  1
#define OFF 0

// ------------------- HTML TAG type
#define MODE_KEYWORD     0
#define MODE_REMARK      2
#define MODE_REMARK_EOL  4
#define MODE_DEFINE      6
#define MODE_STRING      8


int is_delimeter(char c)
{
    int ii=0;
    char dlms[] =
    "\t\r\n (){}[]+-*/%\"'&|^~:;<>.,";
    //--------------------------------
    while (dlms[ii])
    {
        if (c==dlms[ii++]) return 1;
    }
    return 0;
}

int is_keyword(char * str)
{
    char * kwords[] =
    {
        "asm",          "auto",
        "break",        "case",
        "cdecl",        "char",
        "class",        "const",
        "continue",     "default",
        "delete",       "do",
        "double",       "else",
        "enum",         "extern",
        "far",          "float",
        "for",          "friend",
        "goto",         "huge",
        "if",           "inline",
        "int",          "interrupt",
        "long",         "near",
        "new",          "operator",
        "pascal",       "private",
        "protected",    "public",
        "register",     "return",
        "short",        "signed",
        "sizeof",       "static",
        "struct",       "switch",
        "template",     "this",
        "typedef",      "union",
        "unsigned",     "virtual",
        "void",         "volatile",
        "while",        NULL
    };
    int ii=0;
    int jj;
    int check;

    while (kwords[ii])
    {
        jj = 0;
        check = 1;
        while (kwords[ii][jj] && check)
        {
            if (str[jj] != kwords[ii][jj])
            {
               check = 0;
            }
            jj++;
        }
        if (check) return 1;
        ii++;
    }
    return 0;
}


void set_mode(int on_off, int mode)
{
    char * tags[] =
    {
        //-------------------- KEYWORD
        "<strong>",
        "</strong>",
        //-------------------- Classic remarks
        "<font color=\"#336600\">",
        "</font>",
        //-------------------- EOL Remarks
        "<font color=\"#336600\">",
        "</font>",
        //-------------------- #DEFINE
        "<font color=\"#663300\"><strong>",
        "</strong></font>",
        //-------------------- "string"
        "<font color=\"#0000CC\">",
        "</font>",
        NULL,  NULL
    };
    fprintf(stdout,tags[mode + 1 - on_off]);
}

void print_char_html(char c)
{
    switch (c)
    {
        case '<':
             fprintf(stdout,"&lt;");
             break;
        case '>':
             fprintf(stdout,"&gt;");
             break;
        case '"':
             fprintf(stdout,"&quot;");
             break;
        case '&':
             fprintf(stdout,"&amp;");
             break;
        case '|':
             fprintf(stdout,"&brvbar;");
             break;
        default:
             fprintf(stdout,"%c",c);
    }
}



int main(int _argc, char** _argv)
{
   FILE *in, *out;
   char c;
   int  mode;
   char buf[80];
   int  bufidx = 0;
   int  progress = 1;
   int  echo;
   int  saved_mode;
   int  kw;
   char tmpc;
   char prevc;

   if (_argc < 2)
   {
      fprintf(stderr,
      "USAGE: c2html <file>\n");
      return 1;
   }


   if ((in = fopen(_argv[1], "rt")) == NULL)
   {
      fprintf(stderr,
      "Cannot open input file.\n");
      return 1;
   }

	fprintf(stdout, "<pre>");
   mode = START;

   while (!feof(in) && progress)
   {
        echo = 1;
        prevc = c;
        c = fgetc(in);

        if (c=='/' && (mode < REM1))
        {
            saved_mode = mode;
            mode = REM1;
        }

        switch (mode)
        {
            case REM1:
                 echo = 0;
                 mode = REM2;
                 break;

            case REM2:
                 if (c=='/')
                 {
                    if (saved_mode == DEFINE)
                    {
                      set_mode(OFF, MODE_DEFINE);
                    }
                    mode = REM_END;
                    set_mode(ON, MODE_REMARK_EOL);
                 }
                 else if (c=='*')
                 {
                    if (saved_mode == DEFINE)
                    {
                      set_mode(OFF, MODE_DEFINE);
                    }
                    mode = REM_STAR;
                    set_mode(ON, MODE_REMARK);
                 }
                 else
                 {
                    mode = saved_mode;
                 }
                 printf("/");
                 break;

            case REM_END:
                 if (c=='\n')
                 {
                   set_mode(OFF, MODE_REMARK_EOL);
                 }
                 break;

            case REM_STAR:
                 if (c=='*')
                 {
                    mode = REM_STAR_1;
                 }
                 break;

            case REM_STAR_1:
                 if (c=='/')
                 {
                    mode = INLINE;
                    fprintf(stdout,"/");
                    echo = 0;
                    set_mode(OFF, MODE_REMARK);
                 }
                 else mode = REM_STAR;
                 break;

            case START:
                 if (c=='#')
                 {
                    mode = DEFINE;
                    set_mode(ON, MODE_DEFINE);
                    break;
                 }
                 else if (c==' ') break;

                 mode = INLINE;
                 // and continue in next case

            case INLINE:
                 if (c=='"' &&        //
                     prevc != 0x27 && //
                     prevc != '\\')   //
                 {
                    set_mode(ON, MODE_STRING);
                    mode = STRING;
                 }
                 break;

            case STRING:
                 if (c=='"' && prevc != '\\')
                 {
                    print_char_html('"');
                    set_mode(OFF, MODE_STRING);
                    echo = 0;
                    mode = INLINE;
                 }
                 break;

            case DEFINE:
                 if (c=='\n')
                 {
                    set_mode(OFF, MODE_DEFINE);
                 }
                 break;

        }

        if (echo && //
            (mode == INLINE || //
             (mode!=INLINE &&  //
              bufidx)))        //
        {
            buf[bufidx++] = c;
            buf[bufidx]   = 0;
            if (is_delimeter(c))
            {
                kw = 0;
                if (bufidx>2)
                {
                  kw = is_keyword(buf);
                }
                if (kw)
                {
                  set_mode(ON, MODE_KEYWORD);
                }
                tmpc = buf[bufidx-1];
                buf[bufidx-1] = 0;
                fprintf(stdout,"%s",buf);
                if (kw)
                {
                  set_mode(OFF, MODE_KEYWORD);
                }
                print_char_html(tmpc);
                bufidx = 0;
                buf[0] = 0;
            }
        }
        else if (echo) print_char_html(c);

        if (c=='\n' && mode != REM_STAR)
        {
            mode = START;
        }
   }

   fclose(in);
   fprintf(stdout,"</pre>\n");
   fprintf(stdout,
   "<!-- == Generated by CHTM convertor -->\n");
   fprintf(stdout,
   "<!-- == CopyRight (C) 1999, Dmitry Obukhov, dso@usa.net -->\n");

   return 0;
}