diff options
-rw-r--r-- | ChangeLog | 18 | ||||
-rw-r--r-- | awk.h | 8 | ||||
-rw-r--r-- | awkgram.c | 338 | ||||
-rw-r--r-- | awkgram.y | 8 | ||||
-rw-r--r-- | io.c | 483 | ||||
-rw-r--r-- | re.c | 52 |
6 files changed, 454 insertions, 453 deletions
@@ -1,3 +1,21 @@ +2012-04-24 Arnold D. Robbins <arnold@skeeve.com> + + io.c cleanup and some speedup for RS as regexp parsing. + + * awk.h (Regexp): New members has_meta and maybe_long. + (enum redirval): Add redirect_none as value 0. + (remaybelong): Remove function declaration. + * awkgram.y: Use redirect_none instead of 0 for no redirect cases. + * io.c (go_getline_redir): Second arg now of type enum redirval. + Changed intovar into into_variable. + (comments and whitespace): Lots of general cleanup. + (socket_open): readle changed to read_len. + (two_way_open): Add additional calls to os_close_on_exec. + (rsrescan): Simplify code a bit and use RS->maybe_long. + * re.c (make_regexp): Set up new members in Regexp struct. + (remaybelong): Remove function. + (reisstring): Simplified code. + 2012-04-16 Eli Zaretskii <eliz@gnu.org> * io.c (read_with_timeout) [__MINGW32__]: Just call the blocking @@ -216,6 +216,8 @@ typedef struct Regexp { short dfa; short has_anchor; /* speed up of avoid_dfa kludge, temporary */ short non_empty; /* for use in fpat_parse_field */ + short has_meta; /* re has meta chars so (probably) isn't simple string */ + short maybe_long; /* re has meta chars that can match long text */ } Regexp; #define RESTART(rp,s) (rp)->regs.start[0] #define REEND(rp,s) (rp)->regs.end[0] @@ -683,7 +685,8 @@ typedef enum opcodeval { enum redirval { /* I/O redirections */ - redirect_output = 1, + redirect_none = 0, + redirect_output, redirect_append, redirect_pipe, redirect_pipein, @@ -1532,7 +1535,7 @@ extern int close_io(int *stdio_problem); extern int devopen(const char *name, const char *mode); extern int srcopen(SRCFILE *s); extern char *find_source(const char *src, struct stat *stb, int *errcode, int is_extlib); -extern NODE *do_getline_redir(int intovar, int redirtype); +extern NODE *do_getline_redir(int intovar, enum redirval redirtype); extern NODE *do_getline(int intovar, IOBUF *iop); extern struct redirect *getredirect(const char *str, int len); extern int inrec(IOBUF *iop, int *errcode); @@ -1631,7 +1634,6 @@ extern void resyntax(int syntax); extern void resetup(void); extern int avoid_dfa(NODE *re, char *str, size_t len); extern int reisstring(const char *text, size_t len, Regexp *re, const char *buf); -extern int remaybelong(const char *text, size_t len); extern int get_numbase(const char *str, int use_locale); /* symbol.c */ @@ -2039,7 +2039,7 @@ yyreduce: { case 3: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 195 "awkgram.y" { rule = 0; @@ -2049,7 +2049,7 @@ yyreduce: case 5: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 201 "awkgram.y" { next_sourcefile(); @@ -2058,7 +2058,7 @@ yyreduce: case 6: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 205 "awkgram.y" { rule = 0; @@ -2072,7 +2072,7 @@ yyreduce: case 7: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 217 "awkgram.y" { (void) append_rule((yyvsp[(1) - (2)]), (yyvsp[(2) - (2)])); @@ -2081,7 +2081,7 @@ yyreduce: case 8: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 221 "awkgram.y" { if (rule != Rule) { @@ -2097,7 +2097,7 @@ yyreduce: case 9: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 232 "awkgram.y" { in_function = NULL; @@ -2108,7 +2108,7 @@ yyreduce: case 10: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 238 "awkgram.y" { want_source = FALSE; @@ -2118,7 +2118,7 @@ yyreduce: case 11: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 246 "awkgram.y" { if (include_source((yyvsp[(1) - (1)])) < 0) @@ -2131,35 +2131,35 @@ yyreduce: case 12: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 254 "awkgram.y" { (yyval) = NULL; } break; case 13: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 256 "awkgram.y" { (yyval) = NULL; } break; case 14: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 261 "awkgram.y" { (yyval) = NULL; rule = Rule; } break; case 15: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 263 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); rule = Rule; } break; case 16: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 265 "awkgram.y" { INSTRUCTION *tp; @@ -2190,7 +2190,7 @@ yyreduce: case 17: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 291 "awkgram.y" { static int begin_seen = 0; @@ -2206,7 +2206,7 @@ yyreduce: case 18: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 302 "awkgram.y" { static int end_seen = 0; @@ -2222,7 +2222,7 @@ yyreduce: case 19: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 313 "awkgram.y" { (yyvsp[(1) - (1)])->in_rule = rule = BEGINFILE; @@ -2233,7 +2233,7 @@ yyreduce: case 20: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 319 "awkgram.y" { (yyvsp[(1) - (1)])->in_rule = rule = ENDFILE; @@ -2244,7 +2244,7 @@ yyreduce: case 21: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 328 "awkgram.y" { if ((yyvsp[(2) - (5)]) == NULL) @@ -2256,21 +2256,21 @@ yyreduce: case 22: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 338 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 23: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 340 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 24: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 342 "awkgram.y" { yyerror(_("`%s' is a built-in function, it cannot be redefined"), @@ -2281,14 +2281,14 @@ yyreduce: case 25: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 348 "awkgram.y" { (yyval) = (yyvsp[(2) - (2)]); } break; case 28: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 358 "awkgram.y" { (yyvsp[(1) - (6)])->source_file = source; @@ -2304,14 +2304,14 @@ yyreduce: case 29: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 376 "awkgram.y" { ++want_regexp; } break; case 30: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 378 "awkgram.y" { NODE *n, *exp; @@ -2345,21 +2345,21 @@ yyreduce: case 31: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 410 "awkgram.y" { bcfree((yyvsp[(1) - (1)])); } break; case 33: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 416 "awkgram.y" { (yyval) = NULL; } break; case 34: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 418 "awkgram.y" { if ((yyvsp[(2) - (2)]) == NULL) @@ -2377,28 +2377,28 @@ yyreduce: case 35: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 431 "awkgram.y" { (yyval) = NULL; } break; case 38: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 441 "awkgram.y" { (yyval) = NULL; } break; case 39: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 443 "awkgram.y" { (yyval) = (yyvsp[(2) - (3)]); } break; case 40: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 445 "awkgram.y" { if (do_pretty_print) @@ -2410,7 +2410,7 @@ yyreduce: case 41: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 452 "awkgram.y" { INSTRUCTION *dflt, *curr = NULL, *cexp, *cstmt; @@ -2505,7 +2505,7 @@ yyreduce: case 42: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 542 "awkgram.y" { /* @@ -2552,7 +2552,7 @@ yyreduce: case 43: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 584 "awkgram.y" { /* @@ -2599,7 +2599,7 @@ yyreduce: case 44: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 626 "awkgram.y" { INSTRUCTION *ip; @@ -2717,7 +2717,7 @@ regular_loop: case 45: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 739 "awkgram.y" { (yyval) = mk_for_loop((yyvsp[(1) - (12)]), (yyvsp[(3) - (12)]), (yyvsp[(6) - (12)]), (yyvsp[(9) - (12)]), (yyvsp[(12) - (12)])); @@ -2729,7 +2729,7 @@ regular_loop: case 46: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 746 "awkgram.y" { (yyval) = mk_for_loop((yyvsp[(1) - (11)]), (yyvsp[(3) - (11)]), (INSTRUCTION *) NULL, (yyvsp[(8) - (11)]), (yyvsp[(11) - (11)])); @@ -2741,7 +2741,7 @@ regular_loop: case 47: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 753 "awkgram.y" { if (do_pretty_print) @@ -2753,7 +2753,7 @@ regular_loop: case 48: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 763 "awkgram.y" { if (! break_allowed) @@ -2767,7 +2767,7 @@ regular_loop: case 49: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 772 "awkgram.y" { if (! continue_allowed) @@ -2781,7 +2781,7 @@ regular_loop: case 50: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 781 "awkgram.y" { /* if inside function (rule = 0), resolve context at run-time */ @@ -2795,7 +2795,7 @@ regular_loop: case 51: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 790 "awkgram.y" { if (do_traditional) @@ -2815,7 +2815,7 @@ regular_loop: case 52: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 805 "awkgram.y" { /* Initialize the two possible jump targets, the actual target @@ -2835,7 +2835,7 @@ regular_loop: case 53: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 820 "awkgram.y" { if (! in_function) @@ -2845,7 +2845,7 @@ regular_loop: case 54: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 823 "awkgram.y" { if ((yyvsp[(3) - (4)]) == NULL) { @@ -2871,14 +2871,14 @@ regular_loop: case 56: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 855 "awkgram.y" { in_print = TRUE; in_parens = 0; } break; case 57: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 856 "awkgram.y" { /* @@ -2927,7 +2927,7 @@ regular_loop: (yyvsp[(1) - (4)])->expr_count = 0; (yyvsp[(1) - (4)])->opcode = Op_K_print_rec; if ((yyvsp[(4) - (4)]) == NULL) { /* no redircetion */ - (yyvsp[(1) - (4)])->redir_type = 0; + (yyvsp[(1) - (4)])->redir_type = redirect_none; (yyval) = list_create((yyvsp[(1) - (4)])); } else { INSTRUCTION *ip; @@ -2951,12 +2951,12 @@ regular_print: if ((yyvsp[(4) - (4)]) == NULL) { /* no redirection */ if ((yyvsp[(3) - (4)]) == NULL) { /* printf without arg */ (yyvsp[(1) - (4)])->expr_count = 0; - (yyvsp[(1) - (4)])->redir_type = 0; + (yyvsp[(1) - (4)])->redir_type = redirect_none; (yyval) = list_create((yyvsp[(1) - (4)])); } else { INSTRUCTION *t = (yyvsp[(3) - (4)]); (yyvsp[(1) - (4)])->expr_count = count_expressions(&t, FALSE); - (yyvsp[(1) - (4)])->redir_type = 0; + (yyvsp[(1) - (4)])->redir_type = redirect_none; (yyval) = list_append(t, (yyvsp[(1) - (4)])); } } else { @@ -2980,14 +2980,14 @@ regular_print: case 58: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 953 "awkgram.y" { sub_counter = 0; } break; case 59: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 954 "awkgram.y" { char *arr = (yyvsp[(2) - (4)])->lextok; @@ -3017,7 +3017,7 @@ regular_print: case 60: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 983 "awkgram.y" { static short warned = FALSE; @@ -3041,35 +3041,35 @@ regular_print: case 61: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1002 "awkgram.y" { (yyval) = optimize_assignment((yyvsp[(1) - (1)])); } break; case 62: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1007 "awkgram.y" { (yyval) = NULL; } break; case 63: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1009 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 64: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1014 "awkgram.y" { (yyval) = NULL; } break; case 65: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1016 "awkgram.y" { if ((yyvsp[(1) - (2)]) == NULL) @@ -3081,14 +3081,14 @@ regular_print: case 66: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1023 "awkgram.y" { (yyval) = NULL; } break; case 67: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1028 "awkgram.y" { INSTRUCTION *casestmt = (yyvsp[(5) - (5)]); @@ -3105,7 +3105,7 @@ regular_print: case 68: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1040 "awkgram.y" { INSTRUCTION *casestmt = (yyvsp[(4) - (4)]); @@ -3121,14 +3121,14 @@ regular_print: case 69: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1054 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 70: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1056 "awkgram.y" { NODE *n = (yyvsp[(2) - (2)])->memory; @@ -3141,7 +3141,7 @@ regular_print: case 71: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1064 "awkgram.y" { bcfree((yyvsp[(1) - (2)])); @@ -3151,14 +3151,14 @@ regular_print: case 72: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1069 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 73: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1071 "awkgram.y" { (yyvsp[(1) - (1)])->opcode = Op_push_re; @@ -3168,21 +3168,21 @@ regular_print: case 74: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1079 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 75: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1081 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 77: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1091 "awkgram.y" { (yyval) = (yyvsp[(2) - (3)]); @@ -3191,7 +3191,7 @@ regular_print: case 78: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1098 "awkgram.y" { in_print = FALSE; @@ -3202,14 +3202,14 @@ regular_print: case 79: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1103 "awkgram.y" { in_print = FALSE; in_parens = 0; } break; case 80: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1104 "awkgram.y" { if ((yyvsp[(1) - (3)])->redir_type == redirect_twoway @@ -3222,7 +3222,7 @@ regular_print: case 81: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1115 "awkgram.y" { (yyval) = mk_condition((yyvsp[(3) - (6)]), (yyvsp[(1) - (6)]), (yyvsp[(6) - (6)]), NULL, NULL); @@ -3231,7 +3231,7 @@ regular_print: case 82: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1120 "awkgram.y" { (yyval) = mk_condition((yyvsp[(3) - (9)]), (yyvsp[(1) - (9)]), (yyvsp[(6) - (9)]), (yyvsp[(7) - (9)]), (yyvsp[(9) - (9)])); @@ -3240,14 +3240,14 @@ regular_print: case 87: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1137 "awkgram.y" { (yyval) = NULL; } break; case 88: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1139 "awkgram.y" { bcfree((yyvsp[(1) - (2)])); @@ -3257,21 +3257,21 @@ regular_print: case 89: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1147 "awkgram.y" { (yyval) = NULL; } break; case 90: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1149 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]) ; } break; case 91: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1154 "awkgram.y" { (yyvsp[(1) - (1)])->param_count = 0; @@ -3281,7 +3281,7 @@ regular_print: case 92: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1159 "awkgram.y" { (yyvsp[(3) - (3)])->param_count = (yyvsp[(1) - (3)])->lasti->param_count + 1; @@ -3292,63 +3292,63 @@ regular_print: case 93: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1165 "awkgram.y" { (yyval) = NULL; } break; case 94: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1167 "awkgram.y" { (yyval) = (yyvsp[(1) - (2)]); } break; case 95: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1169 "awkgram.y" { (yyval) = (yyvsp[(1) - (3)]); } break; case 96: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1175 "awkgram.y" { (yyval) = NULL; } break; case 97: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1177 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 98: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1182 "awkgram.y" { (yyval) = NULL; } break; case 99: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1184 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 100: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1189 "awkgram.y" { (yyval) = mk_expression_list(NULL, (yyvsp[(1) - (1)])); } break; case 101: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1191 "awkgram.y" { (yyval) = mk_expression_list((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)])); @@ -3358,35 +3358,35 @@ regular_print: case 102: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1196 "awkgram.y" { (yyval) = NULL; } break; case 103: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1198 "awkgram.y" { (yyval) = NULL; } break; case 104: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1200 "awkgram.y" { (yyval) = NULL; } break; case 105: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1202 "awkgram.y" { (yyval) = NULL; } break; case 106: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1208 "awkgram.y" { if (do_lint && (yyvsp[(3) - (3)])->lasti->opcode == Op_match_rec) @@ -3398,21 +3398,21 @@ regular_print: case 107: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1215 "awkgram.y" { (yyval) = mk_boolean((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 108: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1217 "awkgram.y" { (yyval) = mk_boolean((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 109: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1219 "awkgram.y" { if ((yyvsp[(1) - (3)])->lasti->opcode == Op_match_rec) @@ -3433,7 +3433,7 @@ regular_print: case 110: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1235 "awkgram.y" { if (do_lint_old) @@ -3448,7 +3448,7 @@ regular_print: case 111: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1245 "awkgram.y" { if (do_lint && (yyvsp[(3) - (3)])->lasti->opcode == Op_match_rec) @@ -3460,35 +3460,35 @@ regular_print: case 112: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1252 "awkgram.y" { (yyval) = mk_condition((yyvsp[(1) - (5)]), (yyvsp[(2) - (5)]), (yyvsp[(3) - (5)]), (yyvsp[(4) - (5)]), (yyvsp[(5) - (5)])); } break; case 113: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1254 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 114: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1259 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 115: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1261 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 116: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1263 "awkgram.y" { (yyvsp[(2) - (2)])->opcode = Op_assign_quotient; @@ -3498,49 +3498,49 @@ regular_print: case 117: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1271 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 118: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1273 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 119: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1278 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 120: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1280 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 121: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1285 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 122: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1287 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 123: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1289 "awkgram.y" { int count = 2; @@ -3592,49 +3592,49 @@ regular_print: case 125: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1341 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 126: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1343 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 127: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1345 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 128: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1347 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 129: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1349 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 130: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1351 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 131: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1353 "awkgram.y" { /* @@ -3662,7 +3662,7 @@ regular_print: case 132: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1376 "awkgram.y" { (yyvsp[(2) - (2)])->opcode = Op_postincrement; @@ -3672,7 +3672,7 @@ regular_print: case 133: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1381 "awkgram.y" { (yyvsp[(2) - (2)])->opcode = Op_postdecrement; @@ -3682,7 +3682,7 @@ regular_print: case 134: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1386 "awkgram.y" { if (do_lint_old) { @@ -3707,7 +3707,7 @@ regular_print: case 135: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1411 "awkgram.y" { (yyval) = mk_getline((yyvsp[(3) - (4)]), (yyvsp[(4) - (4)]), (yyvsp[(1) - (4)]), (yyvsp[(2) - (4)])->redir_type); @@ -3717,49 +3717,49 @@ regular_print: case 136: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1417 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 137: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1419 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 138: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1421 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 139: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1423 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 140: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1425 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 141: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1427 "awkgram.y" { (yyval) = mk_binary((yyvsp[(1) - (3)]), (yyvsp[(3) - (3)]), (yyvsp[(2) - (3)])); } break; case 142: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1432 "awkgram.y" { (yyval) = list_create((yyvsp[(1) - (1)])); @@ -3768,7 +3768,7 @@ regular_print: case 143: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1436 "awkgram.y" { if ((yyvsp[(2) - (2)])->opcode == Op_match_rec) { @@ -3805,14 +3805,14 @@ regular_print: case 144: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1468 "awkgram.y" { (yyval) = (yyvsp[(2) - (3)]); } break; case 145: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1470 "awkgram.y" { (yyval) = snode((yyvsp[(3) - (4)]), (yyvsp[(1) - (4)])); @@ -3823,7 +3823,7 @@ regular_print: case 146: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1476 "awkgram.y" { (yyval) = snode((yyvsp[(3) - (4)]), (yyvsp[(1) - (4)])); @@ -3834,7 +3834,7 @@ regular_print: case 147: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1482 "awkgram.y" { static short warned1 = FALSE; @@ -3852,7 +3852,7 @@ regular_print: case 150: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1497 "awkgram.y" { (yyvsp[(1) - (2)])->opcode = Op_preincrement; @@ -3862,7 +3862,7 @@ regular_print: case 151: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1502 "awkgram.y" { (yyvsp[(1) - (2)])->opcode = Op_predecrement; @@ -3872,7 +3872,7 @@ regular_print: case 152: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1507 "awkgram.y" { (yyval) = list_create((yyvsp[(1) - (1)])); @@ -3881,7 +3881,7 @@ regular_print: case 153: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1511 "awkgram.y" { (yyval) = list_create((yyvsp[(1) - (1)])); @@ -3890,7 +3890,7 @@ regular_print: case 154: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1515 "awkgram.y" { if ((yyvsp[(2) - (2)])->lasti->opcode == Op_push_i @@ -3910,7 +3910,7 @@ regular_print: case 155: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1530 "awkgram.y" { /* @@ -3925,7 +3925,7 @@ regular_print: case 156: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1543 "awkgram.y" { func_use((yyvsp[(1) - (1)])->lasti->func_name, FUNC_USE); @@ -3935,7 +3935,7 @@ regular_print: case 157: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1548 "awkgram.y" { /* indirect function call */ @@ -3973,7 +3973,7 @@ regular_print: case 158: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1584 "awkgram.y" { param_sanity((yyvsp[(3) - (4)])); @@ -3992,42 +3992,42 @@ regular_print: case 159: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1601 "awkgram.y" { (yyval) = NULL; } break; case 160: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1603 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 161: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1608 "awkgram.y" { (yyval) = NULL; } break; case 162: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1610 "awkgram.y" { (yyval) = (yyvsp[(1) - (2)]); } break; case 163: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1615 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 164: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1617 "awkgram.y" { (yyval) = list_merge((yyvsp[(1) - (2)]), (yyvsp[(2) - (2)])); @@ -4036,7 +4036,7 @@ regular_print: case 165: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1624 "awkgram.y" { INSTRUCTION *ip = (yyvsp[(1) - (1)])->lasti; @@ -4055,7 +4055,7 @@ regular_print: case 166: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1641 "awkgram.y" { INSTRUCTION *t = (yyvsp[(2) - (3)]); @@ -4074,14 +4074,14 @@ regular_print: case 167: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1658 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); } break; case 168: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1660 "awkgram.y" { (yyval) = list_merge((yyvsp[(1) - (2)]), (yyvsp[(2) - (2)])); @@ -4090,14 +4090,14 @@ regular_print: case 169: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1667 "awkgram.y" { (yyval) = (yyvsp[(1) - (2)]); } break; case 170: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1672 "awkgram.y" { char *var_name = (yyvsp[(1) - (1)])->lextok; @@ -4110,7 +4110,7 @@ regular_print: case 171: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1680 "awkgram.y" { char *arr = (yyvsp[(1) - (2)])->lextok; @@ -4122,7 +4122,7 @@ regular_print: case 172: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1690 "awkgram.y" { INSTRUCTION *ip = (yyvsp[(1) - (1)])->nexti; @@ -4139,7 +4139,7 @@ regular_print: case 173: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1702 "awkgram.y" { (yyval) = list_append((yyvsp[(2) - (3)]), (yyvsp[(1) - (3)])); @@ -4150,7 +4150,7 @@ regular_print: case 174: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1711 "awkgram.y" { (yyvsp[(1) - (1)])->opcode = Op_postincrement; @@ -4159,7 +4159,7 @@ regular_print: case 175: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1715 "awkgram.y" { (yyvsp[(1) - (1)])->opcode = Op_postdecrement; @@ -4168,49 +4168,49 @@ regular_print: case 176: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1718 "awkgram.y" { (yyval) = NULL; } break; case 178: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1726 "awkgram.y" { yyerrok; } break; case 179: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1730 "awkgram.y" { yyerrok; } break; case 182: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1739 "awkgram.y" { yyerrok; } break; case 183: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1743 "awkgram.y" { (yyval) = (yyvsp[(1) - (1)]); yyerrok; } break; case 184: -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 1747 "awkgram.y" { yyerrok; } break; -/* Line 1821 of yacc.c */ +/* Line 1806 of yacc.c */ #line 4227 "awkgram.c" default: break; } @@ -7773,7 +7773,7 @@ mk_getline(INSTRUCTION *op, INSTRUCTION *var, INSTRUCTION *redir, int redirtype) else ip = list_create(op); op->into_var = (var != NULL); - op->redir_type = (redir != NULL) ? redirtype : 0; + op->redir_type = (redir != NULL) ? redirtype : redirect_none; return (asgn == NULL ? ip : list_append(ip, asgn)); } @@ -900,7 +900,7 @@ simple_stmt $1->expr_count = 0; $1->opcode = Op_K_print_rec; if ($4 == NULL) { /* no redircetion */ - $1->redir_type = 0; + $1->redir_type = redirect_none; $$ = list_create($1); } else { INSTRUCTION *ip; @@ -924,12 +924,12 @@ regular_print: if ($4 == NULL) { /* no redirection */ if ($3 == NULL) { /* printf without arg */ $1->expr_count = 0; - $1->redir_type = 0; + $1->redir_type = redirect_none; $$ = list_create($1); } else { INSTRUCTION *t = $3; $1->expr_count = count_expressions(&t, FALSE); - $1->redir_type = 0; + $1->redir_type = redirect_none; $$ = list_append(t, $1); } } else { @@ -5076,7 +5076,7 @@ mk_getline(INSTRUCTION *op, INSTRUCTION *var, INSTRUCTION *redir, int redirtype) else ip = list_create(op); op->into_var = (var != NULL); - op->redir_type = (redir != NULL) ? redirtype : 0; + op->redir_type = (redir != NULL) ? redirtype : redirect_none; return (asgn == NULL ? ip : list_append(ip, asgn)); } @@ -72,11 +72,15 @@ #include <netdb.h> #endif /* HAVE_NETDB_H */ +#if defined(HAVE_POPEN_H) +#include "popen.h" +#endif + #ifndef HAVE_GETADDRINFO #include "missing_d/getaddrinfo.h" #endif -#ifndef AI_ADDRCONFIG /* This is a recent symbol, not everyone has it */ +#ifndef AI_ADDRCONFIG /* not everyone has this symbol */ #define AI_ADDRCONFIG 0 #endif /* AI_ADDRCONFIG */ @@ -142,44 +146,47 @@ typedef enum { CLOSE_ALL, CLOSE_TO, CLOSE_FROM } two_way_close_type; -/* Several macros make the code a bit clearer: */ -/* */ -/* */ -/* <defines and enums>= */ +/* Several macros to make the code a bit clearer. */ #define at_eof(iop) (((iop)->flag & IOP_AT_EOF) != 0) #define has_no_data(iop) ((iop)->dataend == NULL) #define no_data_left(iop) ((iop)->off >= (iop)->dataend) -/* The key point to the design is to split out the code that searches through */ -/* a buffer looking for the record and the terminator into separate routines, */ -/* with a higher-level routine doing the reading of data and buffer management. */ -/* This makes the code easier to manage; the buffering code is the same independent */ -/* of how we find a record. Communication is via the return value: */ -/* */ -/* */ -/* <defines and enums>= */ + +/* + * The key point to the design is to split out the code that searches through + * a buffer looking for the record and the terminator into separate routines, + * with a higher-level routine doing the reading of data and buffer management. + * This makes the code easier to manage; the buffering code is the same + * independent of how we find a record. Communication is via the return + * value: + */ + typedef enum recvalues { REC_OK, /* record and terminator found, recmatch struct filled in */ NOTERM, /* no terminator found, give me more input data */ TERMATEND, /* found terminator at end of buffer */ - TERMNEAREND /* found terminator close to end of buffer, for RE might be bigger */ + TERMNEAREND /* found terminator close to end of buffer, for when + the RE might be match more data further in + the file. */ } RECVALUE; -/* Between calls to a scanning routine, the state is stored in */ -/* an [[enum scanstate]] variable. Not all states apply to all */ -/* variants, but the higher code doesn't really care. */ -/* */ -/* */ -/* <defines and enums>= */ + +/* + * Between calls to a scanning routine, the state is stored in + * an enum scanstate variable. Not all states apply to all + * variants, but the higher code doesn't really care. + */ + typedef enum scanstate { NOSTATE, /* scanning not started yet (all) */ INLEADER, /* skipping leading data (RS = "") */ INDATA, /* in body of record (all) */ INTERM /* scanning terminator (RS = "", RS = regexp) */ } SCANSTATE; -/* When a record is seen ([[REC_OK]] or [[TERMATEND]]), the following */ -/* structure is filled in. */ -/* */ -/* */ -/* <recmatch>= */ + +/* + * When a record is seen (REC_OK or TERMATEND), the following + * structure is filled in. + */ + struct recmatch { char *start; /* record start */ size_t len; /* length of record */ @@ -218,28 +225,21 @@ static NODE *in_PROCINFO(const char *pidx1, const char *pidx2, NODE **full_idx); static long get_read_timeout(IOBUF *iop); static ssize_t read_with_timeout(int fd, char *buf, size_t size); -#if defined(HAVE_POPEN_H) -#include "popen.h" -#endif - static int read_can_timeout = FALSE; static long read_timeout; static long read_default_timeout; static struct redirect *red_head = NULL; static NODE *RS = NULL; -static Regexp *RS_re_yes_case; -static Regexp *RS_re_no_case; +static Regexp *RS_re_yes_case; /* regexp for RS when ignoring case */ +static Regexp *RS_re_no_case; /* regexp for RS when not ignoring case */ static Regexp *RS_regexp; int RS_is_null; -extern int output_is_tty; extern NODE *ARGC_node; extern NODE *ARGV_node; extern NODE *ARGIND_node; -extern NODE *ERRNO_node; -extern NODE **fields_arr; /* init_io --- set up timeout related variables */ @@ -301,6 +301,8 @@ static int vmsrtl_fileno(fp) FILE *fp; { return fileno(fp); } #define fileno(FP) (((FP) && *(FP)) ? vmsrtl_fileno(FP) : -1) #endif /* VMS */ +/* after_beginfile --- reset necessary state after BEGINFILE has run */ + void after_beginfile(IOBUF **curfile) { @@ -347,14 +349,14 @@ nextfile(IOBUF **curfile, int skipping) static IOBUF mybuf; const char *fname; int fd = INVALID_HANDLE; - int errcode; + int errcode = 0; IOBUF *iop = *curfile; long argc; if (skipping) { /* for 'nextfile' call */ errcode = 0; if (iop != NULL) { - errcode = iop->errcode; + errcode = iop->errcode; (void) iop_close(iop); } *curfile = NULL; @@ -396,7 +398,6 @@ nextfile(IOBUF **curfile, int skipping) if (! do_traditional) update_ERRNO(); - /* This is a kludge. */ unref(FILENAME_node->var_value); FILENAME_node->var_value = dupnode(arg); #ifdef HAVE_MPFR @@ -404,7 +405,7 @@ nextfile(IOBUF **curfile, int skipping) mpz_set_ui(MFNR, 0); #endif FNR = 0; - iop = *curfile = iop_alloc(fd, fname, &mybuf, FALSE); + iop = *curfile = iop_alloc(fd, fname, & mybuf, FALSE); if (fd == INVALID_HANDLE) iop->errcode = errcode; else @@ -425,7 +426,7 @@ nextfile(IOBUF **curfile, int skipping) FILENAME_node->var_value = make_string("-", 1); FILENAME_node->var_value->flags |= MAYBE_NUM; /* be pedantic */ fname = "-"; - iop = *curfile = iop_alloc(fileno(stdin), fname, &mybuf, FALSE); + iop = *curfile = iop_alloc(fileno(stdin), fname, & mybuf, FALSE); iop->flag |= IOP_NOFREE_OBJ; if (iop->fd == INVALID_HANDLE) { @@ -487,7 +488,7 @@ inrec(IOBUF *iop, int *errcode) else if ((iop->flag & IOP_CLOSED) != 0) cnt = EOF; else - cnt = get_a_record(&begin, iop, errcode); + cnt = get_a_record(& begin, iop, errcode); if (cnt == EOF) { retval = 1; @@ -518,7 +519,7 @@ remap_std_file(int oldfd) if (newfd == INVALID_HANDLE) newfd = open("/dev/null", O_RDWR); if (newfd >= 0) { - /* dup2() will close oldfd for us first. */ + /* if oldfd is open, dup2() will close oldfd for us first. */ ret = dup2(newfd, oldfd); if (ret == 0) close(newfd); @@ -561,7 +562,7 @@ iop_close(IOBUF *iop) ret = close(iop->fd); if (iop->close_func != NULL) - (*iop->close_func)(iop); + iop->close_func(iop); if (ret == -1) warning(_("close of fd %d (`%s') failed (%s)"), iop->fd, @@ -581,7 +582,7 @@ iop_close(IOBUF *iop) unref(fields_arr[0]); fields_arr[0] = t; /* - * 1/27/2003: This used to be here: + * This used to be here: * * reset_record(); * @@ -689,11 +690,11 @@ redirect(NODE *redir_exp, int redirtype, int *errflg) lintwarn(_("filename `%s' for `%s' redirection may be result of logical expression"), str, what); +#ifdef HAVE_SOCKETS /* - * XXX: Use /inet4 and /inet6 with plain /inet being whatever - * we get back from the system. + * Use /inet4 to force IPv4, /inet6 to force IPv6, and plain + * /inet will be whatever we get back from the system. */ -#ifdef HAVE_SOCKETS if (inetfile(str, & len, NULL)) { tflag |= RED_SOCKET; if (strncmp(str + len, "tcp/", 4) == 0) @@ -709,7 +710,7 @@ redirect(NODE *redir_exp, int redirtype, int *errflg) * if at all possible. Messing with signal() for * SIGCLD leads to lots of headaches. However, if * we've gotten EOF from a child input pipeline, it's - * good bet that the child has died. So recover it. + * a good bet that the child has died. So recover it. */ if ((rp->flag & RED_EOF) && redirtype == redirect_pipein) { if (rp->pid != -1) @@ -759,7 +760,7 @@ redirect(NODE *redir_exp, int redirtype, int *errflg) while (rp->fp == NULL && rp->iop == NULL) { if (! new_rp && rp->flag & RED_EOF) { /* - * encountered EOF on file or pipe -- must be cleared + * Encountered EOF on file or pipe -- must be cleared * by explicit close() before reading more */ save_rp = NULL; @@ -869,7 +870,7 @@ redirect(NODE *redir_exp, int redirtype, int *errflg) /* too many files open -- close one and try again */ if (errno == EMFILE || errno == ENFILE) close_one(); -#if defined __MINGW32__ || defined __sun +#if defined __MINGW32__ else if (errno == 0) /* HACK! */ close_one(); #endif @@ -892,9 +893,8 @@ redirect(NODE *redir_exp, int redirtype, int *errflg) */ if (errflg != NULL) *errflg = errno; - if (redirtype == redirect_output - || redirtype == redirect_append - ) { + if ( redirtype == redirect_output + || redirtype == redirect_append) { /* multiple messages make life easier for translators */ if (*direction == 'f') fatal(_("can't redirect from `%s' (%s)"), @@ -966,7 +966,7 @@ close_one() if ((rp->flag & (RED_FILE|RED_WRITE)) == (RED_FILE|RED_WRITE)) { rp->flag |= RED_USED; errno = 0; - if (/* do_lint && */ fclose(rp->fp) != 0) + if (fclose(rp->fp) != 0) warning(_("close of `%s' failed (%s)."), rp->value, strerror(errno)); rp->fp = NULL; @@ -1039,7 +1039,7 @@ do_close(int nargs) */ if (do_posix) { unref(tmp); - return make_number((AWKNUM) 0); + tmp = make_number((AWKNUM) 0); } return tmp; } @@ -1081,7 +1081,8 @@ close_rp(struct redirect *rp, two_way_close_type how) rp->iop = NULL; } - } else if ((rp->flag & (RED_PIPE|RED_WRITE)) == (RED_PIPE|RED_WRITE)) { /* write to pipe */ + } else if ((rp->flag & (RED_PIPE|RED_WRITE)) == (RED_PIPE|RED_WRITE)) { + /* write to pipe */ status = pclose(rp->fp); if ((BINMODE & 1) != 0) os_setbinmode(fileno(stdin), O_BINARY); @@ -1121,14 +1122,15 @@ close_redir(struct redirect *rp, int exitwarn, two_way_close_type how) status = close_rp(rp, how); - /* SVR4 awk checks and warns about status of close */ if (status != 0) { int save_errno = errno; char *s = strerror(save_errno); /* - * Too many people have complained about this. - * As of 2.15.6, it is now under lint control. + * BWK's awk, as far back as SVR4 (1989) would check + * and warn about the status of close. However, when + * we did this we got too many complaints, so we moved + * it to be under lint control. */ if (do_lint) { if ((rp->flag & RED_PIPE) != 0) @@ -1233,7 +1235,7 @@ close_io(int *stdio_problem) for (rp = red_head; rp != NULL; rp = next) { next = rp->next; /* - * close_redir() will print a message if needed + * close_redir() will print a message if needed. * if do_lint, warn about lack of explicit close */ if (close_redir(rp, do_lint, CLOSE_ALL)) @@ -1241,7 +1243,7 @@ close_io(int *stdio_problem) rp = NULL; } /* - * Some of the non-Unix os's have problems doing an fclose + * Some of the non-Unix os's have problems doing an fclose() * on stdout and stderr. Since we don't really need to close * them, we just flush them, and do that across the board. */ @@ -1311,34 +1313,34 @@ socketopen(int family, int type, const char *localpname, struct addrinfo *rres, *rres0; struct addrinfo rhints; - int lerror; - int rerror; + int lerror, rerror; int socket_fd = INVALID_HANDLE; int any_remote_host = (strcmp(remotehostname, "0") == 0); - memset (&lhints, '\0', sizeof (lhints)); + memset(& lhints, '\0', sizeof (lhints)); lhints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; lhints.ai_socktype = type; lhints.ai_family = family; - lerror = getaddrinfo (NULL, localpname, &lhints, &lres); + lerror = getaddrinfo(NULL, localpname, & lhints, & lres); if (lerror) { if (strcmp(localpname, "0") != 0) fatal(_("local port %s invalid in `/inet'"), localpname); lres0 = NULL; - lres = &lhints; + lres = & lhints; } else lres0 = lres; while (lres != NULL) { - memset (&rhints, '\0', sizeof (rhints)); + memset (& rhints, '\0', sizeof (rhints)); rhints.ai_flags = lhints.ai_flags; rhints.ai_socktype = lhints.ai_socktype; rhints.ai_family = lhints.ai_family; rhints.ai_protocol = lhints.ai_protocol; - rerror = getaddrinfo (any_remote_host ? NULL : remotehostname, remotepname, &rhints, &rres); + rerror = getaddrinfo(any_remote_host ? NULL : remotehostname, + remotepname, & rhints, & rres); if (rerror) { if (lres0 != NULL) freeaddrinfo(lres0); @@ -1362,7 +1364,8 @@ socketopen(int family, int type, const char *localpname, (char *) & on, sizeof(on)); #ifdef SO_LINGER linger.l_onoff = 1; - linger.l_linger = 30; /* linger for 30/100 second */ + /* linger for 30/100 second */ + linger.l_linger = 30; setsockopt(socket_fd, SOL_SOCKET, SO_LINGER, (char *) & linger, sizeof(linger)); #endif @@ -1378,12 +1381,12 @@ socketopen(int family, int type, const char *localpname, int clientsocket_fd = INVALID_HANDLE; struct sockaddr_storage remote_addr; - socklen_t namelen = sizeof (remote_addr); + socklen_t namelen = sizeof(remote_addr); if (listen(socket_fd, 1) >= 0 && (clientsocket_fd = accept(socket_fd, - (struct sockaddr *) &remote_addr, - &namelen)) >= 0) { + (struct sockaddr *) & remote_addr, + & namelen)) >= 0) { close(socket_fd); socket_fd = clientsocket_fd; break; @@ -1392,15 +1395,15 @@ socketopen(int family, int type, const char *localpname, #ifdef MSG_PEEK char buf[10]; struct sockaddr_storage remote_addr; - socklen_t readle; + socklen_t read_len; if (recvfrom(socket_fd, buf, 1, MSG_PEEK, (struct sockaddr *) & remote_addr, - & readle) >= 0 - && readle + & read_len) >= 0 + && read_len && connect(socket_fd, - (struct sockaddr *)& remote_addr, - readle) == 0) + (struct sockaddr *) & remote_addr, + read_len) == 0) break; #endif } @@ -1441,11 +1444,10 @@ devopen(const char *name, const char *mode) int len; int family; - flag = str2mode(mode); - if (strcmp(name, "-") == 0) return fileno(stdin); + flag = str2mode(mode); openfd = INVALID_HANDLE; if (do_traditional) @@ -1469,9 +1471,9 @@ devopen(const char *name, const char *mode) struct stat sbuf; cp += 3; - openfd = (int) strtoul(cp, &ptr, 10); + openfd = (int) strtoul(cp, & ptr, 10); if (openfd <= INVALID_HANDLE || ptr == cp - || fstat(openfd, &sbuf) < 0) + || fstat(openfd, & sbuf) < 0) openfd = INVALID_HANDLE; } /* do not set close-on-exec for inherited fd's */ @@ -1534,7 +1536,7 @@ devopen(const char *name, const char *mode) cp++; /* * The remote port ends the special file name. - * This means there already is a 0 at the end of the string. + * This means there already is a '\0' at the end of the string. * Therefore no need to patch any string ending. * * Here too, require a port, let them explicitly put 0 if @@ -1548,48 +1550,48 @@ devopen(const char *name, const char *mode) { #define DEFAULT_RETRIES 20 - static unsigned long def_retries = DEFAULT_RETRIES; - static int first_time = TRUE; - unsigned long retries = 0; - static long msleep = 1000; - - if (first_time) { - char *cp, *end; - unsigned long count = 0; - char *ms2; - - first_time = FALSE; - if ((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) { - count = strtoul(cp, &end, 10); - if (end != cp && count > 0) - def_retries = count; - } - - /* - * Env var is in milliseconds, paramter to usleep() - * is microseconds, make the conversion. Default is - * 1 millisecond. - */ - if ((ms2 = getenv("GAWK_MSEC_SLEEP")) != NULL) { - msleep = strtol(ms2, &end, 10); - if (end == ms2 || msleep < 0) - msleep = 1000; - else - msleep *= 1000; - } + static unsigned long def_retries = DEFAULT_RETRIES; + static int first_time = TRUE; + unsigned long retries = 0; + static long msleep = 1000; + + if (first_time) { + char *cp, *end; + unsigned long count = 0; + char *ms2; + + first_time = FALSE; + if ((cp = getenv("GAWK_SOCK_RETRIES")) != NULL) { + count = strtoul(cp, & end, 10); + if (end != cp && count > 0) + def_retries = count; } - retries = def_retries; - do { - openfd = socketopen(family, protocol, localpname, cp, hostname); - retries--; - } while (openfd == INVALID_HANDLE && retries > 0 && usleep(msleep) == 0); + /* + * Env var is in milliseconds, paramter to usleep() + * is microseconds, make the conversion. Default is + * 1 millisecond. + */ + if ((ms2 = getenv("GAWK_MSEC_SLEEP")) != NULL) { + msleep = strtol(ms2, & end, 10); + if (end == ms2 || msleep < 0) + msleep = 1000; + else + msleep *= 1000; + } } + retries = def_retries; - *localpnamelastcharp = '/'; - *hostnameslastcharp = '/'; + do { + openfd = socketopen(family, protocol, localpname, cp, hostname); + retries--; + } while (openfd == INVALID_HANDLE && retries > 0 && usleep(msleep) == 0); + } + + *localpnamelastcharp = '/'; + *hostnameslastcharp = '/'; #else /* ! HAVE_SOCKETS */ - fatal(_("TCP/IP communications are not supported")); + fatal(_("TCP/IP communications are not supported")); #endif /* HAVE_SOCKETS */ } @@ -1601,8 +1603,8 @@ strictopen: /* on OS/2 directory access via open() is not permitted */ struct stat buf; - if (stat(name, &buf) == 0 && S_ISDIR(buf.st_mode)) - errno = EISDIR; + if (stat(name, & buf) == 0 && S_ISDIR(buf.st_mode)) + errno = EISDIR; } #endif if (openfd != INVALID_HANDLE) { @@ -1648,9 +1650,11 @@ two_way_open(const char *str, struct redirect *rp) fclose(rp->fp); return FALSE; } + os_close_on_exec(fd, str, "socket", "to/from"); os_close_on_exec(newfd, str, "socket", "to/from"); rp->iop = iop_alloc(newfd, str, NULL, TRUE); if (rp->iop == NULL) { + close(newfd); fclose(rp->fp); return FALSE; } @@ -1675,20 +1679,20 @@ two_way_open(const char *str, struct redirect *rp) pid_t pid; struct stat statb; struct termios st; - /* Use array of chars to avoid ascii / ebcdic issues */ + /* Use array of chars to avoid ASCII / EBCDIC issues */ static char pty_chars[] = "pqrstuvwxyzabcdefghijklmno"; int i; if (! initialized) { initialized = TRUE; #ifdef HAVE_GRANTPT - have_dev_ptmx = (stat("/dev/ptmx", &statb) >= 0); + have_dev_ptmx = (stat("/dev/ptmx", & statb) >= 0); #endif i = 0; do { c = pty_chars[i++]; sprintf(slavenam, "/dev/pty%c0", c); - if (stat(slavenam, &statb) >= 0) { + if (stat(slavenam, & statb) >= 0) { first_pty_letter = c; break; } @@ -1716,8 +1720,9 @@ two_way_open(const char *str, struct redirect *rp) if (first_pty_letter) { /* * Assume /dev/ptyXNN and /dev/ttyXN naming system. - * The FIRST_PTY_LETTER gives the first X to try. We try in the - * sequence FIRST_PTY_LETTER, .., 'z', 'a', .., FIRST_PTY_LETTER. + * The FIRST_PTY_LETTER gives the first X to try. + * We try in the sequence FIRST_PTY_LETTER, .., + * 'z', 'a', .., FIRST_PTY_LETTER. * Is this worthwhile, or just over-zealous? */ c = first_pty_letter; @@ -1727,7 +1732,7 @@ two_way_open(const char *str, struct redirect *rp) for (i = 0; i < 16; i++) { sprintf(slavenam, "/dev/pty%c%x", c, i); - if (stat(slavenam, &statb) < 0) { + if (stat(slavenam, & statb) < 0) { no_ptys = TRUE; /* bypass all this next time */ goto use_pipes; } @@ -1769,7 +1774,7 @@ two_way_open(const char *str, struct redirect *rp) ioctl(slave, I_PUSH, "ldterm"); #endif - tcgetattr(slave, &st); + tcgetattr(slave, & st); st.c_iflag &= ~(ISTRIP | IGNCR | INLCR | IXOFF); st.c_iflag |= (ICRNL | IGNPAR | BRKINT | IXON); st.c_oflag &= ~OPOST; @@ -1777,10 +1782,6 @@ two_way_open(const char *str, struct redirect *rp) st.c_cflag |= CREAD | CS8 | CLOCAL; st.c_lflag &= ~(ECHO | ECHOE | ECHOK | NOFLSH | TOSTOP); st.c_lflag |= ISIG; -#if 0 - st.c_cc[VMIN] = 1; - st.c_cc[VTIME] = 0; -#endif /* Set some control codes to default values */ #ifdef VINTR @@ -1798,9 +1799,9 @@ two_way_open(const char *str, struct redirect *rp) #ifdef VEOF st.c_cc[VEOF] = '\004'; /* ^d */ #endif - tcsetattr(slave, TCSANOW, &st); + tcsetattr(slave, TCSANOW, & st); - switch (pid = fork ()) { + switch (pid = fork()) { case 0: /* Child process */ setsid(); @@ -1840,9 +1841,9 @@ two_way_open(const char *str, struct redirect *rp) } /* parent */ - if (close(slave)) { + if (close(slave) != 0) { close(master); - (void) kill(pid, SIGKILL); /* overkill? (pardon pun) */ + (void) kill(pid, SIGKILL); fatal(_("close of slave pty failed (%s)"), strerror(errno)); } @@ -1850,7 +1851,7 @@ two_way_open(const char *str, struct redirect *rp) rp->iop = iop_alloc(master, str, NULL, TRUE); if (rp->iop == NULL) { (void) close(master); - (void) kill(pid, SIGKILL); /* overkill? (pardon pun) */ + (void) kill(pid, SIGKILL); return FALSE; } @@ -1863,7 +1864,7 @@ two_way_open(const char *str, struct redirect *rp) iop_close(rp->iop); rp->iop = NULL; (void) close(master); - (void) kill(pid, SIGKILL); /* overkill? (pardon pun) */ + (void) kill(pid, SIGKILL); if (dup_master > 0) (void) close(dup_master); return FALSE; @@ -1903,7 +1904,7 @@ use_pipes: save_stdout = dup(1); /* duplicate stdout */ if (save_stdout == -1 || save_stdin == -1) { - /* if an error occurrs close all open file handles */ + /* if an error occurs close all open file handles */ save_errno = errno; if (save_stdin != -1) close(save_stdin); @@ -2006,7 +2007,7 @@ use_pipes: (void) close(ctop[1]); (void) close(ptoc[0]); (void) close(ptoc[1]); - (void) kill(pid, SIGKILL); /* overkill? (pardon pun) */ + (void) kill(pid, SIGKILL); return FALSE; } @@ -2018,7 +2019,7 @@ use_pipes: (void) close(ctop[1]); (void) close(ptoc[0]); (void) close(ptoc[1]); - (void) kill(pid, SIGKILL); /* overkill? (pardon pun) */ + (void) kill(pid, SIGKILL); return FALSE; } @@ -2060,9 +2061,9 @@ wait_any(int interesting) /* pid of interest, if any */ qstat = signal(SIGQUIT, SIG_IGN); for (;;) { #ifdef HAVE_SYS_WAIT_H /* POSIX compatible sys/wait.h */ - pid = wait(&status); + pid = wait(& status); #else - pid = wait((union wait *)&status); + pid = wait((union wait *) & status); #endif if (interesting && pid == interesting) { break; @@ -2095,27 +2096,31 @@ gawk_popen(const char *cmd, struct redirect *rp) #endif /* - * used to wait for any children to synchronize input and output, + * We used to wait for any children to synchronize input and output, * but this could cause gawk to hang when it is started in a pipeline - * and thus has a child process feeding it input (shell dependent) + * and thus has a child process feeding it input (shell dependent). + * + * (void) wait_any(0); // wait for outstanding processes */ - /*(void) wait_any(0);*/ /* wait for outstanding processes */ if (pipe(p) < 0) fatal(_("cannot open pipe `%s' (%s)"), cmd, strerror(errno)); #ifdef __EMX__ - save_stdout = dup(1); /* save stdout */ rp->iop = NULL; + save_stdout = dup(1); /* save stdout */ if (save_stdout == -1) { - close(p[0]); close(p[1]); - return rp->iop; /* failed */ + close(p[0]); + close(p[1]); + return NULL; /* failed */ } close(1); /* close stdout */ if (dup(p[1]) != 1) { - close(p[0]); close(p[1]); - fatal(_("moving pipe to stdout in child failed (dup: %s)"), strerror(errno)); + close(p[0]); + close(p[1]); + fatal(_("moving pipe to stdout in child failed (dup: %s)"), + strerror(errno)); } /* none of these handles must be inherited by the child process */ @@ -2236,7 +2241,7 @@ gawk_pclose(struct redirect *rp) /* do_getline --- read in a line, into var and with redirection */ NODE * -do_getline_redir(int intovar, int redirtype) +do_getline_redir(int into_variable, enum redirval redirtype) { struct redirect *rp = NULL; IOBUF *iop; @@ -2247,12 +2252,12 @@ do_getline_redir(int intovar, int redirtype) NODE **lhs = NULL; int redir_error = 0; - if (intovar) + if (into_variable) lhs = POP_ADDRESS(); - assert(redirtype != 0); + assert(redirtype != redirect_none); redir_exp = TOP(); - rp = redirect(redir_exp, redirtype, &redir_error); + rp = redirect(redir_exp, redirtype, & redir_error); DEREF(redir_exp); decr_sp(); if (rp == NULL) { @@ -2267,9 +2272,9 @@ do_getline_redir(int intovar, int redirtype) return make_number((AWKNUM) 0.0); errcode = 0; - cnt = get_a_record(&s, iop, &errcode); + cnt = get_a_record(& s, iop, & errcode); if (errcode != 0) { - if (! do_traditional && (errcode != -1)) + if (! do_traditional && errcode != -1) update_ERRNO_saved(errcode); return make_number((AWKNUM) -1.0); } @@ -2302,24 +2307,24 @@ do_getline_redir(int intovar, int redirtype) /* do_getline --- read in a line, into var and without redirection */ NODE * -do_getline(int intovar, IOBUF *iop) +do_getline(int into_variable, IOBUF *iop) { int cnt = EOF; char *s = NULL; int errcode; if (iop == NULL) { /* end of input */ - if (intovar) + if (into_variable) (void) POP_ADDRESS(); return make_number((AWKNUM) 0.0); } errcode = 0; - cnt = get_a_record(&s, iop, &errcode); + cnt = get_a_record(& s, iop, & errcode); if (errcode != 0) { - if (! do_traditional && (errcode != -1)) + if (! do_traditional && errcode != -1) update_ERRNO_saved(errcode); - if (intovar) + if (into_variable) (void) POP_ADDRESS(); return make_number((AWKNUM) -1.0); } @@ -2329,7 +2334,7 @@ do_getline(int intovar, IOBUF *iop) INCREMENT_REC(NR); INCREMENT_REC(FNR); - if (! intovar) /* no optional var. */ + if (! into_variable) /* no optional var. */ set_record(s, cnt); else { /* assignment to variable */ NODE **lhs; @@ -2382,7 +2387,7 @@ init_awkpath(char *path) memcpy(p, start, len); /* add directory punctuation if necessary */ - if (! isdirpunct(*(end - 1))) + if (! isdirpunct(end[-1])) p[len++] = '/'; p[len] = '\0'; @@ -2556,23 +2561,12 @@ int srcopen(SRCFILE *s) { if (s->stype == SRC_STDIN) - return (0); + return fileno(stdin); if (s->stype == SRC_FILE || s->stype == SRC_INC) return devopen(s->fullpath, "r"); return INVALID_HANDLE; } -#ifdef TEST -int bufsize = 8192; - -void -fatal(const char *s) -{ - printf("%s\n", s); - exit(EXIT_FAILURE); -} -#endif - /* open hooks, mainly for use by extension functions */ static struct open_hook { @@ -2659,12 +2653,12 @@ iop_alloc(int fd, const char *name, IOBUF *iop, int do_openhooks) (void)(! do_traditional && (unref(RT_node->var_value), \ RT_node->var_value = make_string(str, len))) -/* grow must increase size of buffer, set end, make sure off and dataend point at */ -/* right spot. */ -/* */ -/* */ -/* <growbuffer>= */ -/* grow_iop_buffer --- grow the buffer */ +/* + * grow_iop_buffer: + * + * grow must increase size of buffer, set end, make sure off and dataend + * point at the right spot. + */ static void grow_iop_buffer(IOBUF *iop) @@ -2698,10 +2692,8 @@ grow_iop_buffer(IOBUF *iop) iop->end = iop->buf + iop->size; } -/* Here are the routines. */ -/* */ -/* */ -/* <rs1scan>= */ +/* Here are the routines. */ + /* rs1scan --- scan for a single character record terminator */ static RECVALUE @@ -2782,19 +2774,19 @@ rs1scan(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) int len = iop->dataend - bp; int found = 0; - memset(&mbs, 0, sizeof(mbstate_t)); + memset(& mbs, 0, sizeof(mbstate_t)); do { if (*bp == rs) found = 1; if (is_valid_character(*bp)) mbclen = 1; else - mbclen = mbrlen(bp, len, &mbs); - if ( (mbclen == 1) - || (mbclen == (size_t) -1) - || (mbclen == (size_t) -2) - || (mbclen == 0)) { - /* We treat it as a singlebyte character. */ + mbclen = mbrlen(bp, len, & mbs); + if ( mbclen == 1 + || mbclen == (size_t) -1 + || mbclen == (size_t) -2 + || mbclen == 0) { + /* We treat it as a single-byte character. */ mbclen = 1; } len -= mbclen; @@ -2804,8 +2796,8 @@ rs1scan(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) /* Check that newline found isn't the sentinel. */ if (found && (bp - mbclen) < iop->dataend) { /* - * set len to what we have so far, in case this is - * all there is + * Set len to what we have so far, in case this is + * all there is. */ recm->len = bp - recm->start - mbclen; recm->rt_start = bp - mbclen; @@ -2839,7 +2831,6 @@ rs1scan(IOBUF *iop, struct recmatch *recm, SCANSTATE *state) } } -/* <rsrescan>= */ /* rsrescan --- search for a regex match in the buffer */ static RECVALUE @@ -2929,21 +2920,15 @@ again: * This matches the "xyz" and ends up putting the * "abc" into the front of the next record. Ooops. * - * The remaybelong() function looks to see if the + * The re->maybe_long member is true if the * regex contains one of: + * ? |. This is a very * simple heuristic, but in combination with the * "end of match within a few bytes of end of buffer" * check, should keep things reasonable. */ - /* - * XXX: The reisstring and remaybelong tests should - * really be done once when RS is assigned to and - * then tested as flags here. Maybe one day. - */ - /* succession of tests is easier to trace in GDB. */ - if (remaybelong(RS->stptr, RS->stlen)) { + if (RSre->maybe_long) { char *matchend = iop->off + reend; if (iop->dataend - matchend < RS->stlen) @@ -2953,7 +2938,6 @@ again: return REC_OK; } -/* <rsnullscan>= */ /* rsnullscan --- handle RS = "" */ static RECVALUE @@ -3030,7 +3014,6 @@ find_longest_terminator: return REC_OK; } -/* <getarecord>= */ /* get_a_record --- read a record from IOP into out, return length of EOF, set RT */ static int @@ -3052,9 +3035,9 @@ get_a_record(char **out, /* pointer to pointer to data */ read_timeout = get_read_timeout(iop); if (iop->get_record != NULL) - return (*iop->get_record)(out, iop, errcode); + return iop->get_record(out, iop, errcode); - /* <fill initial buffer>= */ + /* fill initial buffer */ if (has_no_data(iop) || no_data_left(iop)) { iop->count = iop->read_func(iop->fd, iop->buf, iop->readsize); if (iop->count == 0) { @@ -3071,10 +3054,12 @@ get_a_record(char **out, /* pointer to pointer to data */ } } - /* <loop through file to find a record>= */ + /* loop through file to find a record */ state = NOSTATE; for (;;) { size_t dataend_off; + size_t room_left; + size_t amt_to_read; ret = (*matchrec)(iop, & recm, & state); iop->flag &= ~IOP_AT_START; @@ -3082,64 +3067,62 @@ get_a_record(char **out, /* pointer to pointer to data */ break; /* need to add more data to buffer */ - /* <shift data down in buffer>= */ + /* shift data down in buffer */ dataend_off = iop->dataend - iop->off; memmove(iop->buf, iop->off, dataend_off); iop->off = iop->buf; iop->dataend = iop->buf + dataend_off; - /* <adjust recm contents>= */ + /* adjust recm contents */ recm.start = iop->off; if (recm.rt_start != NULL) recm.rt_start = iop->off + recm.len; - /* <read more data, break if EOF>= */ - { + /* read more data, break if EOF */ #define min(x, y) (x < y ? x : y) - /* subtract one in read count to leave room for sentinel */ - size_t room_left = iop->end - iop->dataend - 1; - size_t amt_to_read = min(iop->readsize, room_left); - - if (amt_to_read < iop->readsize) { - grow_iop_buffer(iop); - /* <adjust recm contents>= */ - recm.start = iop->off; - if (recm.rt_start != NULL) - recm.rt_start = iop->off + recm.len; - - /* recalculate amt_to_read */ - room_left = iop->end - iop->dataend - 1; - amt_to_read = min(iop->readsize, room_left); - } - while (amt_to_read + iop->readsize < room_left) - amt_to_read += iop->readsize; + /* subtract one in read count to leave room for sentinel */ + room_left = iop->end - iop->dataend - 1; + amt_to_read = min(iop->readsize, room_left); + + if (amt_to_read < iop->readsize) { + grow_iop_buffer(iop); + /* adjust recm contents */ + recm.start = iop->off; + if (recm.rt_start != NULL) + recm.rt_start = iop->off + recm.len; + + /* recalculate amt_to_read */ + room_left = iop->end - iop->dataend - 1; + amt_to_read = min(iop->readsize, room_left); + } + while (amt_to_read + iop->readsize < room_left) + amt_to_read += iop->readsize; #ifdef SSIZE_MAX - /* - * POSIX limits read to SSIZE_MAX. There are (bizarre) - * systems where this amount is small. - */ - amt_to_read = min(amt_to_read, SSIZE_MAX); + /* + * POSIX limits read to SSIZE_MAX. There are (bizarre) + * systems where this amount is small. + */ + amt_to_read = min(amt_to_read, SSIZE_MAX); #endif - iop->count = iop->read_func(iop->fd, iop->dataend, amt_to_read); - if (iop->count == -1) { - *errcode = errno; - iop->flag |= IOP_AT_EOF; - break; - } else if (iop->count == 0) { - /* - * hit EOF before matching RS, so end - * the record and set RT to "" - */ - iop->flag |= IOP_AT_EOF; - break; - } else - iop->dataend += iop->count; - } + iop->count = iop->read_func(iop->fd, iop->dataend, amt_to_read); + if (iop->count == -1) { + *errcode = errno; + iop->flag |= IOP_AT_EOF; + break; + } else if (iop->count == 0) { + /* + * hit EOF before matching RS, so end + * the record and set RT to "" + */ + iop->flag |= IOP_AT_EOF; + break; + } else + iop->dataend += iop->count; } - /* <set record, RT, return right value>= */ + /* set record, RT, return right value */ /* * rtval is not a static pointer to avoid dangling pointer problems @@ -33,6 +33,7 @@ static void check_bracket_exp(char *s, size_t len); Regexp * make_regexp(const char *s, size_t len, int ignorecase, int dfa, int canfatal) { + static char metas[] = ".*+(){}[]|?^$\\"; Regexp *rp; const char *rerr; const char *src = s; @@ -46,6 +47,7 @@ make_regexp(const char *s, size_t len, int ignorecase, int dfa, int canfatal) int has_anchor = FALSE; int may_have_range = 0; reg_syntax_t dfa_syn; + int i; /* * The number of bytes in the current multibyte character. @@ -90,11 +92,11 @@ make_regexp(const char *s, size_t len, int ignorecase, int dfa, int canfatal) /* The previous byte is a singlebyte character, or last byte of a multibyte character. We check the next character. */ is_multibyte = mbrlen(src, end - src, &mbs); - if ( (is_multibyte == 1) - || (is_multibyte == (size_t) -1) - || (is_multibyte == (size_t) -2 - || (is_multibyte == 0))) { - /* We treat it as a singlebyte character. */ + if ( is_multibyte == 1 + || is_multibyte == (size_t) -1 + || is_multibyte == (size_t) -2 + || is_multibyte == 0) { + /* We treat it as a single-byte character. */ is_multibyte = 0; } } @@ -233,6 +235,21 @@ make_regexp(const char *s, size_t len, int ignorecase, int dfa, int canfatal) } else rp->dfa = FALSE; rp->has_anchor = has_anchor; + + /* Additional flags that help with RS as regexp. */ + for (i = 0; i < len; i++) { + if (strchr(metas, buf[i]) != NULL) { + rp->has_meta = TRUE; + break; + } + } + + for (i = len - 1; i >= 0; i--) { + if (strchr("*+|?", buf[i]) != NULL) { + rp->maybe_long = TRUE; + break; + } + } return rp; } @@ -422,17 +439,12 @@ avoid_dfa(NODE *re, char *str, size_t len) int reisstring(const char *text, size_t len, Regexp *re, const char *buf) { - static char metas[] = ".*+(){}[]|?^$\\"; - int i; int res; const char *matched; - /* simple checking for has meta characters in re */ - for (i = 0; i < len; i++) { - if (strchr(metas, text[i]) != NULL) { - return FALSE; /* give up early, can't be string match */ - } - } + /* simple checking for meta characters in re */ + if (re->has_meta) + return FALSE; /* give up early, can't be string match */ /* make accessable to gdb */ matched = &buf[RESTART(re, buf)]; @@ -442,20 +454,6 @@ reisstring(const char *text, size_t len, Regexp *re, const char *buf) return res; } -/* remaybelong --- return TRUE if the RE contains * ? | + */ - -int -remaybelong(const char *text, size_t len) -{ - while (len--) { - if (strchr("*+|?", *text++) != NULL) { - return TRUE; - } - } - - return FALSE; -} - /* reflags2str --- make a regex flags value readable */ const char * |