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