Tue, 21 Apr 2015 09:53:01 +0200
fixed wrong comment formatting in strings
universe@21 | 1 | /* |
universe@21 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@21 | 3 | * |
olaf@24 | 4 | * Copyright 2015 Mike Becker. All rights reserved. |
universe@21 | 5 | * |
universe@21 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@21 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@21 | 8 | * |
universe@21 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@21 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@21 | 11 | * |
universe@21 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@21 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@21 | 14 | * documentation and/or other materials provided with the distribution. |
universe@21 | 15 | * |
universe@21 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@21 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@21 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@21 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@21 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@21 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@21 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@21 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@21 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@21 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@21 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@21 | 27 | * |
universe@21 | 28 | */ |
universe@21 | 29 | |
universe@21 | 30 | #include "ccodegen.h" |
universe@21 | 31 | #include <string.h> |
universe@21 | 32 | #include <ctype.h> |
universe@21 | 33 | |
universe@21 | 34 | const char* ckeywords[] = { |
universe@21 | 35 | "auto", "break", "case", "char", "const", "continue", "default", "do", |
universe@21 | 36 | "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", |
universe@21 | 37 | "long", "register", "return", "short", "signed", "sizeof", "static", |
universe@21 | 38 | "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", |
universe@21 | 39 | "while", NULL |
universe@21 | 40 | }; |
universe@21 | 41 | |
universe@21 | 42 | int isctype(char *word, size_t len) { |
universe@21 | 43 | return (word[len-2] == '_' && word[len-1] == 't'); |
universe@21 | 44 | } |
universe@21 | 45 | |
universe@21 | 46 | int iscdirective(char *word) { |
universe@21 | 47 | return (word[0] == '#'); |
universe@21 | 48 | } |
universe@21 | 49 | |
universe@21 | 50 | void cparseline(char *src, char *dest, highlighter_t *hltr) { |
universe@21 | 51 | size_t sp = 0, dp = 0; |
universe@21 | 52 | /* indent */ |
universe@21 | 53 | while (isspace(src[sp])) { |
universe@21 | 54 | dest[dp++] = src[sp++]; |
universe@21 | 55 | } |
universe@21 | 56 | |
universe@21 | 57 | memset(hltr->word, 0, WORDBUF_SIZE); |
universe@21 | 58 | size_t wp = 0, ifp = 0; |
universe@21 | 59 | int isstring = 0, iscomment = 0, isinclude = 0, parseinclude = 0; |
universe@21 | 60 | int isescaping = 0; |
universe@21 | 61 | |
universe@21 | 62 | if (hltr->iscommentml) { |
universe@21 | 63 | iscomment = 1; |
universe@21 | 64 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); |
universe@21 | 65 | dp += 29; |
universe@21 | 66 | } |
universe@21 | 67 | |
universe@21 | 68 | for (char c = src[sp] ; c ; c=src[++sp]) { |
universe@21 | 69 | /* comments */ |
universe@26 | 70 | if (!isstring && c == '/') { |
universe@21 | 71 | if (hltr->iscommentml && sp > 0 && src[sp-1] == '*') { |
universe@21 | 72 | iscomment = 0; |
universe@21 | 73 | hltr->iscommentml = 0; |
universe@21 | 74 | memcpy(&(dest[dp]), "/</span>", 8); |
universe@21 | 75 | dp += 8; |
universe@21 | 76 | continue; |
universe@21 | 77 | } else if (!iscomment && (src[sp+1] == '/' || src[sp+1] == '*')) { |
universe@21 | 78 | iscomment = 1; |
universe@21 | 79 | hltr->iscommentml = (src[sp+1] == '*'); |
universe@21 | 80 | memcpy(&(dest[dp]), "<span class=\"c2html-comment\">", 29); |
universe@21 | 81 | dp += 29; |
universe@21 | 82 | } |
universe@21 | 83 | } |
universe@21 | 84 | |
universe@21 | 85 | if (iscomment) { |
universe@21 | 86 | if (c == '\n') { |
universe@21 | 87 | memcpy(&(dest[dp]), "</span>", 7); |
universe@21 | 88 | dp += 7; |
universe@21 | 89 | } |
universe@21 | 90 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 91 | } else if (isinclude) { |
universe@21 | 92 | if (c == '<') { |
universe@21 | 93 | memcpy(&(dest[dp]), "<span class=\"c2html-stdinclude\">", 32); |
universe@21 | 94 | dp += 32; |
universe@21 | 95 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 96 | } else if (c == '\"') { |
universe@21 | 97 | if (parseinclude) { |
universe@21 | 98 | dest[dp++] = '\"'; |
universe@21 | 99 | dest[dp++] = '>'; |
universe@21 | 100 | memcpy(&(dest[dp]), hltr->includefile, ifp); |
universe@21 | 101 | dp += ifp; |
universe@21 | 102 | |
universe@21 | 103 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 104 | memcpy(&(dest[dp]), "</a>", 4); |
universe@21 | 105 | dp += 4; |
universe@21 | 106 | parseinclude = 0; |
universe@21 | 107 | } else { |
universe@21 | 108 | memcpy(&(dest[dp]), |
universe@21 | 109 | "<a class=\"c2html-userinclude\" href=", 35); |
universe@21 | 110 | dp += 35; |
universe@21 | 111 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 112 | ifp = 0; |
universe@21 | 113 | hltr->includefile[ifp++] = '\"'; |
universe@21 | 114 | parseinclude = 1; |
universe@21 | 115 | } |
universe@21 | 116 | } else if (c == '>') { |
universe@21 | 117 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 118 | memcpy(&(dest[dp]), "</span>", 7); |
universe@21 | 119 | dp += 7; |
universe@21 | 120 | } else { |
universe@21 | 121 | if (parseinclude) { |
universe@21 | 122 | hltr->includefile[ifp++] = c; |
universe@21 | 123 | } |
universe@21 | 124 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 125 | } |
universe@21 | 126 | } else { |
universe@21 | 127 | /* strings */ |
universe@21 | 128 | if (!isescaping && (c == '\'' || c == '\"')) { |
universe@21 | 129 | isstring ^= 1; |
universe@21 | 130 | if (isstring) { |
universe@21 | 131 | memcpy(&(dest[dp]), "<span class=\"c2html-string\">", 28); |
universe@21 | 132 | dp += 28; |
universe@21 | 133 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 134 | } else { |
universe@21 | 135 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 136 | memcpy(&(dest[dp]), "</span>", 7); |
universe@21 | 137 | dp += 7; |
universe@21 | 138 | } |
universe@21 | 139 | } else { |
universe@21 | 140 | if (isstring) { |
universe@21 | 141 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 142 | } else if (!iswordcharacter(c)) { |
universe@21 | 143 | /* interpret word int_t */ |
universe@21 | 144 | if (wp > 0 && wp < WORDBUF_SIZE) { |
universe@21 | 145 | int closespan = 1; |
universe@21 | 146 | if (iskeyword(hltr->word, hltr->keywords)) { |
universe@21 | 147 | memcpy(&(dest[dp]), |
universe@21 | 148 | "<span class=\"c2html-keyword\">", 29); |
universe@21 | 149 | dp += 29; |
universe@21 | 150 | } else if (hltr->istype(hltr->word, wp)) { |
universe@21 | 151 | memcpy(&(dest[dp]), |
universe@21 | 152 | "<span class=\"c2html-type\">", 26); |
universe@21 | 153 | dp += 26; |
universe@21 | 154 | } else if (hltr->isdirective(hltr->word)) { |
universe@21 | 155 | isinclude = !strncmp( |
universe@21 | 156 | "#include", hltr->word, WORDBUF_SIZE); |
universe@21 | 157 | memcpy(&(dest[dp]), |
universe@21 | 158 | "<span class=\"c2html-directive\">", 31); |
universe@21 | 159 | dp += 31; |
universe@21 | 160 | } else if (iscapsonly(hltr->word, wp)) { |
universe@21 | 161 | memcpy(&(dest[dp]), |
universe@21 | 162 | "<span class=\"c2html-macroconst\">", 32); |
universe@21 | 163 | dp += 32; |
universe@21 | 164 | } else { |
universe@21 | 165 | closespan = 0; |
universe@21 | 166 | } |
universe@21 | 167 | for (int i = 0 ; i < wp ; i++) { |
universe@21 | 168 | dp = writeescapedchar(dest, dp, hltr->word[i]); |
universe@21 | 169 | } |
universe@21 | 170 | if (closespan) { |
universe@21 | 171 | memcpy(&(dest[dp]), "</span>", 7); |
universe@21 | 172 | dp += 7; |
universe@21 | 173 | } |
universe@21 | 174 | } |
universe@21 | 175 | memset(hltr->word, 0, WORDBUF_SIZE); |
universe@21 | 176 | wp = 0; |
universe@21 | 177 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 178 | } else { |
universe@21 | 179 | /* read word */ |
universe@21 | 180 | if (wp < WORDBUF_SIZE) { |
universe@21 | 181 | hltr->word[wp++] = c; |
universe@21 | 182 | } else if (wp == WORDBUF_SIZE) { |
universe@21 | 183 | for (int i = 0 ; i < WORDBUF_SIZE ; i++) { |
universe@21 | 184 | dp = writeescapedchar(dest, dp, hltr->word[i]); |
universe@21 | 185 | } |
universe@21 | 186 | wp++; |
universe@21 | 187 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 188 | } else { |
universe@21 | 189 | dp = writeescapedchar(dest, dp, c); |
universe@21 | 190 | } |
universe@21 | 191 | } |
universe@21 | 192 | } |
universe@21 | 193 | |
universe@21 | 194 | isescaping = !isescaping & (c == '\\'); |
universe@21 | 195 | } |
universe@21 | 196 | } |
universe@21 | 197 | dest[dp] = 0; |
universe@21 | 198 | } |