static void search_all_files(char *fnames[], int num_of_files, const char *key) { SUFARY *ary[FILE_NUM_MAX]; int i; if (array_file_name) { if ((ary[0] = sa_open(fnames[0], array_file_name)) == NULL) { fprintf(stderr, "%s or %s: File open error\n", fnames[0], array_file_name); exit(EXIT_FAILURE); } search_for_the_given_pattern(ary[0], key, NULL); sa_close(ary[0]); return; } for (i = 0; i < num_of_files; i++) { if ((ary[i] = sa_open(fnames[i], NULL)) == NULL) { fprintf(stderr, "%s: File open error\n", fnames[i]); exit(EXIT_FAILURE); } if (num_of_files == 1) search_for_the_given_pattern(ary[i], key, NULL); else search_for_the_given_pattern(ary[i], key, fnames[i]); sa_close(ary[i]); } }
static string_ty * strip(string_ty *s) { static stracc sa; char *sp; int space; sa_open(&sa); sp = s->str_text; while (*sp && isspace(*sp)) ++sp; space = 0; while (*sp) { if (isspace(*sp)) space = 1; else { if (space) sa_char(&sa, ' '); sa_char(&sa, *sp); space = 0; } ++sp; } return sa_close(&sa); }
static int catenate_interpret(string_list_ty *result, const string_list_ty *args, const expr_position_ty *pp, const struct opcode_context_ty *ocp) { static stracc sa; size_t j; string_ty *s; trace(("catenate\n")); (void)pp; (void)ocp; assert(result); assert(args); assert(args->nstrings); if (args->nstrings < 2) return 0; if (args->nstrings == 2) { string_list_append(result, args->string[1]); return 0; } sa_open(&sa); for (j = 1; j < args->nstrings; j++) { s = args->string[j]; sa_chars(&sa, s->str_text, s->str_length); } s = sa_close(&sa); string_list_append(result, s); str_free(s); return 0; }
string_ty * str_downcase(string_ty * s) { static stracc sa; char *cp1; sa_open(&sa); for (cp1 = s->str_text; *cp1; ++cp1) { int c; c = (unsigned char)*cp1; if (isupper(c)) c = tolower(c); sa_char(&sa, c); } return sa_close(&sa); }
int main(int argc, char *argv[]) { SUFARY *ary; SUF_RESULT sr; ary = sa_open(argv[2], NULL); if (ary == NULL) exit(1); sa_set_debug_mode(1); sr = sa_find(ary, 0, sa_get_array_size(ary) - 1, argv[1], strlen(argv[1]), 0); printf("%ld %ld\n", sr.left, sr.right); if (sr.stat == SUCCESS) { SA_INDEX tmp; for (tmp = sr.left; tmp <= sr.right; tmp++) { /* '<=' に注意 */ SA_INDEX pos = sa_aryidx2txtidx(ary, tmp); char *txt_ptr; printf("★テキストファイルの %ld 文字目からマッチ\n", pos); printf("★先頭二文字表示(1)\n%.*s\n", 2, sa_aryidx2txtptr(ary, tmp)); txt_ptr = sa_get_string(ary, pos, 2); printf("★先頭二文字表示(2)\n%s\n", txt_ptr); free(txt_ptr); txt_ptr = sa_get_line(ary, pos); printf("★一行表示\n%s\n", txt_ptr); free(txt_ptr); txt_ptr = sa_get_lines(ary, pos, 1, 1); printf("★前後の一行も表示\n%s\n", txt_ptr); free(txt_ptr); txt_ptr = sa_get_block(ary, pos, "<DOC>", "</DOC>"); printf("★DOC タグに囲まれた領域を表示\n%s\n", txt_ptr); free(txt_ptr); } } sa_close(ary); return 0; }
int gram_lex(void) { static char *paren; static long paren_max; long paren_depth; int c; long linum; int bol_was; int first_was; string_ty *s; int token; int start_of_line = 0; trace(("gram_lex()\n{\n")); for (;;) { linum = line_number; bol_was = bol; first_was = first; c = byte(); switch (c) { case INPUT_EOF: token = 0; goto done; case '\t': if (!bol_was || within_define) continue; sa_open(); for (;;) { c = byte(); switch (c) { case INPUT_EOF: case '\n': break; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (sa_data_length) sa_char(c); continue; default: sa_char(c); continue; } break; } gram_lval.lv_line = blob_alloc(sa_close(), input_filename(input), linum); token = COMMAND; goto done; case '#': sa_open(); more_comment: start_of_line = 1; for (;;) { c = byte(); switch (c) { case INPUT_EOF: case '\n': break; case '#': if (!start_of_line) sa_char(c); continue; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (!start_of_line) sa_char(' '); continue; default: sa_char(c); start_of_line = 0; continue; } break; } if (!first_was) { /* * If the comment did not start at the * beginning of the line, throw it away. */ byte_undo('\n'); continue; } token = COMMENT; if (c == '\n') { /* * Take a peek at the next character. * If it is '#', we have more comment. * If it is '\t', we have a code comment. */ c = byte(); if (c == '#') { sa_char('\n'); goto more_comment; } if (c == '\t') token = COMMAND_COMMENT; byte_undo(c); /* need to restore this state, too */ bol = 1; first = 1; colon_special = 1; } gram_lval.lv_line = blob_alloc(sa_close(), input_filename(input), linum); goto done; case ' ': case '\f': #if __STDC__ >= 1 case '\v': #endif break; case '\n': token = EOLN; goto done; case ';': if (!colon_special) goto normal; byte_undo('\t'); bol = 1; first = 1; colon_special = 1; token = EOLN; goto done; case ':': if (!colon_special) goto normal; c = byte(); if (c == ':') { token = COLON_COLON; goto done; } if (c == '=') { token = COLON_EQUALS; colon_special = 0; goto done; } byte_undo(c); token = COLON; goto done; case '=': token = EQUALS; colon_special = 0; goto done; case '+': c = byte(); if (c == '=') { token = PLUS_EQUALS; colon_special = 0; goto done; } byte_undo(c); c = '+'; /* fall through... */ default: normal: sa_open(); paren_depth = 0; for (;;) { switch (c) { case INPUT_EOF: case '\n': break; case ' ': case '\t': case '\f': #if __STDC__ >= 1 case '\v': #endif if (!within_define && !paren_depth) break; sa_char(c); c = byte(); continue; case ';': case ':': case '=': if (colon_special && !within_define && !paren_depth) break; sa_char(c); c = byte(); continue; default: sa_char(c); c = byte(); continue; case '(': sa_char(c); if (paren_depth >= paren_max) { paren_max = paren_max * 2 + 16; paren = mem_change_size(paren, paren_max); } paren[paren_depth++] = ')'; c = byte(); continue; case ')': case '}': sa_char(c); if (paren_depth && c == paren[paren_depth - 1]) --paren_depth; c = byte(); continue; case '{': sa_char(c); if (paren_depth >= paren_max) { paren_max = paren_max * 2 + 16; paren = mem_change_size(paren, paren_max); } paren[paren_depth++] = '}'; c = byte(); continue; } break; } byte_undo(c); s = sa_close(); if (first_was && (token = reserved(s)) != 0) { switch (token) { case DEFINE: str_free(s); ++within_define; break; case ENDDEF: str_free(s); --within_define; break; case IF: gram_lval.lv_line = blob_alloc(s, input_filename(input), linum); break; case VPATH: colon_special = 0; break; default: str_free(s); break; } goto done; } gram_lval.lv_line = blob_alloc(s, input_filename(input), linum); token = WORD; goto done; } } /* * here for all exits */ done: #ifdef DEBUG if (token == WORD || token == COMMENT || token == COMMAND) trace(("text = \"%s\";\n", gram_lval.lv_line->text->str_text)); #endif trace(("return %d;\n", token)); trace(("}\n")); return token; }