Annotation of researchv10no/cmd/gre/lt1.c, revision 1.1.1.1

1.1       root        1: /*
                      2: From pegasus.ATT.COM!hansen Sun Oct 14 23:38 EDT 1990
                      3: Received: by pyxis; Sun Oct 14 23:38 EDT 1990
                      4: FROM:       [email protected] (t.l.hansen)
                      5: TO:         research!andrew
                      6: DATE:       14 Oct 1990  23:35 EDT
                      7: SUBJECT:    regex bug take 2
                      8: 
                      9: Something's getting lost when writing or reading the compiled expression
                     10: to/from a file. The following program displays the problem. It is similar to
                     11: the previous one except that it writes the regex to a temp file in the
                     12: middle and then reads it back in.
                     13: 
                     14:                                        Tony
                     15: */
                     16: #include <stdio.h>
                     17: #include <ctype.h>
                     18: #define USE_STDIO
                     19: #include "re.h"
                     20: 
                     21: void prc(c)
                     22: unsigned char c;
                     23: {
                     24:     if (c >= 0200)
                     25:        {
                     26:        (void) printf("M-");
                     27:        c -= 0200;
                     28:        }
                     29:     if (isprint(c)) putchar(c);
                     30:     else
                     31:        {
                     32:        putchar('^');
                     33:        putchar(c ^ 0100);
                     34:        }
                     35: }
                     36: 
                     37: void pr(i, mb, me)
                     38: int i;
                     39: char *mb;
                     40: char *me;
                     41: {
                     42:     (void) printf("%d: %#x - %#x\n", i, mb, me);
                     43:     putchar('"');
                     44:     for (; mb < me; mb++)
                     45:        {
                     46:        if (!*mb)
                     47:            break;
                     48:        prc(*mb);
                     49:        }
                     50:     putchar('"');
                     51:     putchar('\n');
                     52: }
                     53: 
                     54: main()
                     55: {
                     56:     re_re *regex;
                     57:     int i;
                     58:     unsigned char re_map[256];
                     59:     static char lname[] = "!nosuchsystem!testing";
                     60:     static char pat[] = "!([^!]+)!(.+)";
                     61:     char *match[10][2];
                     62:     int parens;
                     63:     FILE *fp;
                     64: 
                     65:     for (i = 0; i < 256; i++)
                     66:        re_map[i] = i;
                     67:     regex = re_recomp(pat, pat+strlen(pat), re_map);
                     68:     parens = re_paren(regex);
                     69:     (void) printf("parens = %d\n", parens);
                     70:     if (parens != 2)
                     71:         {
                     72:        (void) printf("!= 2!!\n");
                     73:        return 0;
                     74:        }
                     75:     fp = fopen("/tmp/junk.re", "w");
                     76:     if (!fp)
                     77:         {
                     78:        (void) printf("error opening output file\n");
                     79:        return 0;
                     80:        }
                     81: 
                     82:     re_refile(regex, fp);
                     83:     fclose(fp);
                     84:     if (ferror(fp))
                     85:         {
                     86:        (void) printf("error writing\n");
                     87:        return 0;
                     88:        }
                     89: 
                     90:     fp = fopen("/tmp/junk.re", "r");
                     91:     if (!fp)
                     92:         {
                     93:        (void) printf("error opening input file\n");
                     94:        return 0;
                     95:        }
                     96: 
                     97:     regex = re_filere(fp);
                     98:     if (!regex)
                     99:         {
                    100:        (void) printf("error reading regex\n");
                    101:        return 0;
                    102:        }
                    103: 
                    104:     fclose(fp);
                    105: 
                    106:     parens = re_paren(regex);
                    107:     (void) printf("parens = %d\n", parens);
                    108:     if (parens != 2)
                    109:         {
                    110:        (void) printf("!= 2!!\n");
                    111:        return 0;
                    112:        }
                    113:     
                    114:     if (!re_reexec(regex, lname, lname+strlen(lname), match))
                    115:        {
                    116:        (void) printf("no match\n");
                    117:        return 0;
                    118:        }
                    119: 
                    120:     (void) printf("matched\n");
                    121:     for (i = 0; i <= parens; i++)
                    122:        pr(i, match[i][0], match[i][1]);
                    123:     return 0;
                    124: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.