root / src / quote_fmt_parse.y @ 6
History | View | Annotate | Download (7.4 kB)
| 1 | %{
|
|---|---|
| 2 | |
| 3 | #include "defs.h" |
| 4 | |
| 5 | #include <glib.h> |
| 6 | #include <ctype.h> |
| 7 | |
| 8 | #include "procmsg.h" |
| 9 | #include "procmime.h" |
| 10 | #include "utils.h" |
| 11 | |
| 12 | #include "quote_fmt.h" |
| 13 | #include "quote_fmt_lex.h" |
| 14 | |
| 15 | /* decl */ |
| 16 | /* |
| 17 | flex quote_fmt.l |
| 18 | bison -p quote_fmt quote_fmt.y |
| 19 | */ |
| 20 | |
| 21 | int yylex(void); |
| 22 | |
| 23 | static MsgInfo *msginfo = NULL; |
| 24 | static gboolean *visible = NULL; |
| 25 | static gint maxsize = 0; |
| 26 | static gint stacksize = 0; |
| 27 | |
| 28 | static gchar *buffer = NULL; |
| 29 | static gint bufmax = 0; |
| 30 | static gint bufsize = 0; |
| 31 | static const gchar *quote_str = NULL; |
| 32 | static const gchar *body = NULL; |
| 33 | static gint error = 0; |
| 34 | |
| 35 | static void add_visibility(gboolean val) |
| 36 | {
|
| 37 | stacksize++; |
| 38 | if (maxsize < stacksize) {
|
| 39 | maxsize += 128; |
| 40 | visible = g_realloc(visible, maxsize * sizeof(gboolean)); |
| 41 | if (visible == NULL) |
| 42 | maxsize = 0; |
| 43 | } |
| 44 | |
| 45 | visible[stacksize - 1] = val; |
| 46 | } |
| 47 | |
| 48 | static void remove_visibility(void) |
| 49 | {
|
| 50 | stacksize--; |
| 51 | } |
| 52 | |
| 53 | static void add_buffer(const gchar *s) |
| 54 | {
|
| 55 | gint len; |
| 56 | |
| 57 | len = strlen(s); |
| 58 | if (bufsize + len + 1 > bufmax) {
|
| 59 | if (bufmax == 0) |
| 60 | bufmax = 128; |
| 61 | while (bufsize + len + 1 > bufmax) |
| 62 | bufmax *= 2; |
| 63 | buffer = g_realloc(buffer, bufmax); |
| 64 | } |
| 65 | strcpy(buffer + bufsize, s); |
| 66 | bufsize += len; |
| 67 | } |
| 68 | |
| 69 | #if 0 |
| 70 | static void flush_buffer(void) |
| 71 | {
|
| 72 | if (buffer != NULL) |
| 73 | *buffer = '\0'; |
| 74 | bufsize = 0; |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | gchar *quote_fmt_get_buffer(void) |
| 79 | {
|
| 80 | if (error != 0) |
| 81 | return NULL; |
| 82 | else |
| 83 | return buffer; |
| 84 | } |
| 85 | |
| 86 | #define INSERT(buf) \ |
| 87 | if (stacksize != 0 && visible[stacksize - 1]) \ |
| 88 | add_buffer(buf) |
| 89 | |
| 90 | #define INSERT_CHARACTER(chr) \ |
| 91 | if (stacksize != 0 && visible[stacksize - 1]) { \
|
| 92 | gchar tmp[2]; \ |
| 93 | tmp[0] = (chr); \ |
| 94 | tmp[1] = '\0'; \ |
| 95 | add_buffer(tmp); \ |
| 96 | } |
| 97 | |
| 98 | void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str, |
| 99 | const gchar *my_body) |
| 100 | {
|
| 101 | quote_str = my_quote_str; |
| 102 | body = my_body; |
| 103 | msginfo = info; |
| 104 | stacksize = 0; |
| 105 | add_visibility(TRUE); |
| 106 | if (buffer != NULL) |
| 107 | *buffer = 0; |
| 108 | bufsize = 0; |
| 109 | error = 0; |
| 110 | } |
| 111 | |
| 112 | void quote_fmterror(char *str) |
| 113 | {
|
| 114 | g_warning("Error: %s\n", str);
|
| 115 | error = 1; |
| 116 | } |
| 117 | |
| 118 | int quote_fmtwrap(void) |
| 119 | {
|
| 120 | return 1; |
| 121 | } |
| 122 | |
| 123 | static int isseparator(int ch) |
| 124 | {
|
| 125 | return isspace(ch) || ch == '.' || ch == '-'; |
| 126 | } |
| 127 | %} |
| 128 | |
| 129 | %union {
|
| 130 | char chr; |
| 131 | } |
| 132 | |
| 133 | %token SHOW_NEWSGROUPS |
| 134 | %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME |
| 135 | %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID |
| 136 | %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE |
| 137 | %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB |
| 138 | %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE |
| 139 | %token SHOW_EOL SHOW_QUESTION_MARK SHOW_OPARENT SHOW_CPARENT |
| 140 | %token QUERY_DATE QUERY_FROM |
| 141 | %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS |
| 142 | %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES |
| 143 | %token OPARENT CPARENT |
| 144 | %token CHARACTER |
| 145 | |
| 146 | %start quote_fmt |
| 147 | |
| 148 | %token <chr> CHARACTER |
| 149 | %type <chr> character |
| 150 | |
| 151 | %% |
| 152 | |
| 153 | quote_fmt: |
| 154 | character_or_special_or_query_list; |
| 155 | |
| 156 | character_or_special_or_query_list: |
| 157 | character_or_special_or_query character_or_special_or_query_list |
| 158 | | character_or_special_or_query ; |
| 159 | |
| 160 | character_or_special_or_query: |
| 161 | special |
| 162 | | character |
| 163 | {
|
| 164 | INSERT_CHARACTER($1); |
| 165 | } |
| 166 | | query ; |
| 167 | |
| 168 | |
| 169 | character: |
| 170 | CHARACTER |
| 171 | ; |
| 172 | |
| 173 | special: |
| 174 | SHOW_NEWSGROUPS |
| 175 | {
|
| 176 | if (msginfo->newsgroups) |
| 177 | INSERT(msginfo->newsgroups); |
| 178 | } |
| 179 | | SHOW_DATE |
| 180 | {
|
| 181 | if (msginfo->date) |
| 182 | INSERT(msginfo->date); |
| 183 | } |
| 184 | | SHOW_FROM |
| 185 | {
|
| 186 | if (msginfo->from) |
| 187 | INSERT(msginfo->from); |
| 188 | } |
| 189 | | SHOW_FULLNAME |
| 190 | {
|
| 191 | if (msginfo->fromname) |
| 192 | INSERT(msginfo->fromname); |
| 193 | } |
| 194 | | SHOW_FIRST_NAME |
| 195 | {
|
| 196 | if (msginfo->fromname) {
|
| 197 | guchar *p; |
| 198 | gchar *str; |
| 199 | |
| 200 | str = alloca(strlen(msginfo->fromname) + 1); |
| 201 | if (str != NULL) {
|
| 202 | strcpy(str, msginfo->fromname); |
| 203 | p = str; |
| 204 | while (*p && !isspace(*p)) p++; |
| 205 | *p = '\0'; |
| 206 | INSERT(str); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | | SHOW_SENDER_INITIAL |
| 211 | {
|
| 212 | #define MAX_SENDER_INITIAL 20 |
| 213 | if (msginfo->fromname) {
|
| 214 | gchar tmp[MAX_SENDER_INITIAL]; |
| 215 | guchar *p; |
| 216 | gchar *cur; |
| 217 | gint len = 0; |
| 218 | |
| 219 | p = msginfo->fromname; |
| 220 | cur = tmp; |
| 221 | while (*p) {
|
| 222 | if (*p && isalnum(*p)) {
|
| 223 | *cur = toupper(*p); |
| 224 | cur++; |
| 225 | len++; |
| 226 | if (len >= MAX_SENDER_INITIAL - 1) |
| 227 | break; |
| 228 | } else |
| 229 | break; |
| 230 | while (*p && !isseparator(*p)) p++; |
| 231 | while (*p && isseparator(*p)) p++; |
| 232 | } |
| 233 | *cur = '\0'; |
| 234 | INSERT(tmp); |
| 235 | } |
| 236 | } |
| 237 | | SHOW_SUBJECT |
| 238 | {
|
| 239 | if (msginfo->subject) |
| 240 | INSERT(msginfo->subject); |
| 241 | } |
| 242 | | SHOW_TO |
| 243 | {
|
| 244 | if (msginfo->to) |
| 245 | INSERT(msginfo->to); |
| 246 | } |
| 247 | | SHOW_MESSAGEID |
| 248 | {
|
| 249 | if (msginfo->msgid) |
| 250 | INSERT(msginfo->msgid); |
| 251 | } |
| 252 | | SHOW_PERCENT |
| 253 | {
|
| 254 | INSERT("%");
|
| 255 | } |
| 256 | | SHOW_CC |
| 257 | {
|
| 258 | if (msginfo->cc) |
| 259 | INSERT(msginfo->cc); |
| 260 | } |
| 261 | | SHOW_REFERENCES |
| 262 | {
|
| 263 | /* if (msginfo->references) |
| 264 | INSERT(msginfo->references); */ |
| 265 | } |
| 266 | | SHOW_MESSAGE |
| 267 | {
|
| 268 | if (msginfo->folder || body) {
|
| 269 | gchar buf[BUFFSIZE]; |
| 270 | FILE *fp; |
| 271 | |
| 272 | if (body) |
| 273 | fp = str_open_as_stream(body); |
| 274 | else |
| 275 | fp = procmime_get_first_text_content(msginfo); |
| 276 | |
| 277 | if (fp == NULL) |
| 278 | g_warning("Can't get text part\n");
|
| 279 | else {
|
| 280 | while (fgets(buf, sizeof(buf), fp) != NULL) {
|
| 281 | strcrchomp(buf); |
| 282 | INSERT(buf); |
| 283 | } |
| 284 | fclose(fp); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | | SHOW_QUOTED_MESSAGE |
| 289 | {
|
| 290 | if (msginfo->folder || body) {
|
| 291 | gchar buf[BUFFSIZE]; |
| 292 | FILE *fp; |
| 293 | |
| 294 | if (body) |
| 295 | fp = str_open_as_stream(body); |
| 296 | else |
| 297 | fp = procmime_get_first_text_content(msginfo); |
| 298 | |
| 299 | if (fp == NULL) |
| 300 | g_warning("Can't get text part\n");
|
| 301 | else {
|
| 302 | while (fgets(buf, sizeof(buf), fp) != NULL) {
|
| 303 | strcrchomp(buf); |
| 304 | if (quote_str) |
| 305 | INSERT(quote_str); |
| 306 | INSERT(buf); |
| 307 | } |
| 308 | fclose(fp); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | | SHOW_MESSAGE_NO_SIGNATURE |
| 313 | {
|
| 314 | if (msginfo->folder || body) {
|
| 315 | gchar buf[BUFFSIZE]; |
| 316 | FILE *fp; |
| 317 | |
| 318 | if (body) |
| 319 | fp = str_open_as_stream(body); |
| 320 | else |
| 321 | fp = procmime_get_first_text_content(msginfo); |
| 322 | |
| 323 | if (fp == NULL) |
| 324 | g_warning("Can't get text part\n");
|
| 325 | else {
|
| 326 | while (fgets(buf, sizeof(buf), fp) != NULL) {
|
| 327 | strcrchomp(buf); |
| 328 | if (strncmp(buf, "-- \n", 4) == 0) |
| 329 | break; |
| 330 | INSERT(buf); |
| 331 | } |
| 332 | fclose(fp); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | | SHOW_QUOTED_MESSAGE_NO_SIGNATURE |
| 337 | {
|
| 338 | if (msginfo->folder || body) {
|
| 339 | gchar buf[BUFFSIZE]; |
| 340 | FILE *fp; |
| 341 | |
| 342 | if (body) |
| 343 | fp = str_open_as_stream(body); |
| 344 | else |
| 345 | fp = procmime_get_first_text_content(msginfo); |
| 346 | |
| 347 | if (fp == NULL) |
| 348 | g_warning("Can't get text part\n");
|
| 349 | else {
|
| 350 | while (fgets(buf, sizeof(buf), fp) != NULL) {
|
| 351 | strcrchomp(buf); |
| 352 | if (strncmp(buf, "-- \n", 4) == 0) |
| 353 | break; |
| 354 | if (quote_str) |
| 355 | INSERT(quote_str); |
| 356 | INSERT(buf); |
| 357 | } |
| 358 | fclose(fp); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | | SHOW_BACKSLASH |
| 363 | {
|
| 364 | INSERT("\\");
|
| 365 | } |
| 366 | | SHOW_TAB |
| 367 | {
|
| 368 | INSERT("\t");
|
| 369 | } |
| 370 | | SHOW_EOL |
| 371 | {
|
| 372 | INSERT("\n");
|
| 373 | } |
| 374 | | SHOW_QUESTION_MARK |
| 375 | {
|
| 376 | INSERT("?");
|
| 377 | } |
| 378 | | SHOW_OPARENT |
| 379 | {
|
| 380 | INSERT("{");
|
| 381 | } |
| 382 | | SHOW_CPARENT |
| 383 | {
|
| 384 | INSERT("}");
|
| 385 | }; |
| 386 | |
| 387 | query: |
| 388 | QUERY_DATE |
| 389 | {
|
| 390 | add_visibility(msginfo->date != NULL); |
| 391 | } |
| 392 | OPARENT quote_fmt CPARENT |
| 393 | {
|
| 394 | remove_visibility(); |
| 395 | } |
| 396 | | QUERY_FROM |
| 397 | {
|
| 398 | add_visibility(msginfo->from != NULL); |
| 399 | } |
| 400 | OPARENT quote_fmt CPARENT |
| 401 | {
|
| 402 | remove_visibility(); |
| 403 | } |
| 404 | | QUERY_FULLNAME |
| 405 | {
|
| 406 | add_visibility(msginfo->fromname != NULL); |
| 407 | } |
| 408 | OPARENT quote_fmt CPARENT |
| 409 | {
|
| 410 | remove_visibility(); |
| 411 | } |
| 412 | | QUERY_SUBJECT |
| 413 | {
|
| 414 | add_visibility(msginfo->subject != NULL); |
| 415 | } |
| 416 | OPARENT quote_fmt CPARENT |
| 417 | {
|
| 418 | remove_visibility(); |
| 419 | } |
| 420 | | QUERY_TO |
| 421 | {
|
| 422 | add_visibility(msginfo->to != NULL); |
| 423 | } |
| 424 | OPARENT quote_fmt CPARENT |
| 425 | {
|
| 426 | remove_visibility(); |
| 427 | } |
| 428 | | QUERY_NEWSGROUPS |
| 429 | {
|
| 430 | add_visibility(msginfo->newsgroups != NULL); |
| 431 | } |
| 432 | OPARENT quote_fmt CPARENT |
| 433 | {
|
| 434 | remove_visibility(); |
| 435 | } |
| 436 | | QUERY_MESSAGEID |
| 437 | {
|
| 438 | add_visibility(msginfo->msgid != NULL); |
| 439 | } |
| 440 | OPARENT quote_fmt CPARENT |
| 441 | {
|
| 442 | remove_visibility(); |
| 443 | } |
| 444 | | QUERY_CC |
| 445 | {
|
| 446 | add_visibility(msginfo->cc != NULL); |
| 447 | } |
| 448 | OPARENT quote_fmt CPARENT |
| 449 | {
|
| 450 | remove_visibility(); |
| 451 | } |
| 452 | | QUERY_REFERENCES |
| 453 | {
|
| 454 | /* add_visibility(msginfo->references != NULL); */ |
| 455 | } |
| 456 | OPARENT quote_fmt CPARENT |
| 457 | {
|
| 458 | remove_visibility(); |
| 459 | }; |