2 * semind - semantic indexer for C.
4 * Copyright (C) 2020 Alexey Gladkov
23 #define U_DEF (0x100 << U_SHIFT)
24 #define SINDEX_DATABASE_VERSION 1
26 #define message(fmt, ...) semind_error(0, 0, (fmt), ##__VA_ARGS__)
28 static const char *progname
;
29 static const char *semind_command
= NULL
;
32 static const char *semind_dbfile
= "semind.sqlite";
33 static int semind_verbose
= 0;
34 static char cwd
[PATH_MAX
];
37 // 'add' command options
38 static struct string_list
*semind_filelist
= NULL
;
39 static int semind_include_local_syms
= 0;
41 struct semind_streams
{
45 static struct semind_streams
*semind_streams
= NULL
;
46 static int semind_streams_nr
= 0;
48 // 'search' command options
49 static int semind_search_modmask
;
50 static int semind_search_modmask_defined
= 0;
51 static int semind_search_kind
= 0;
52 static char *semind_search_path
= NULL
;
53 static char *semind_search_symbol
= NULL
;
54 static const char *semind_search_format
= "(%m) %f\t%l\t%c\t%C\t%s";
56 #define EXPLAIN_LOCATION 1
57 #define USAGE_BY_LOCATION 2
58 static int semind_search_by_location
;
59 static char *semind_search_filename
;
60 static int semind_search_line
;
61 static int semind_search_column
;
63 static sqlite3
*semind_db
= NULL
;
64 static sqlite3_stmt
*lock_stmt
= NULL
;
65 static sqlite3_stmt
*unlock_stmt
= NULL
;
66 static sqlite3_stmt
*insert_rec_stmt
= NULL
;
67 static sqlite3_stmt
*select_file_stmt
= NULL
;
68 static sqlite3_stmt
*insert_file_stmt
= NULL
;
69 static sqlite3_stmt
*delete_file_stmt
= NULL
;
74 void (*parse_cmdline
)(int argc
, char **argv
);
75 void (*handler
)(int argc
, char **argv
);
78 static void show_usage(void)
81 printf("Try '%s %s --help' for more information.\n",
82 progname
, semind_command
);
84 printf("Try '%s --help' for more information.\n",
89 static void show_help(int ret
)
92 "Usage: %1$s [options]\n"
93 " or: %1$s [options] add [command options] [--] [compiler options] [files...]\n"
94 " or: %1$s [options] rm [command options] pattern\n"
95 " or: %1$s [options] search [command options] pattern\n"
97 "These are common %1$s commands used in various situations:\n"
98 " add Generate or updates semantic index file for c-source code;\n"
99 " rm Remove files from the index by pattern;\n"
100 " search Make index queries.\n"
103 " -D, --database=FILE Specify database file (default: %2$s);\n"
104 " -B, --basedir=DIR Define project top directory (default is the current directory);\n"
105 " -v, --verbose Show information about what is being done;\n"
106 " -h, --help Show this text and exit.\n"
109 " SINDEX_DATABASE Database file location.\n"
110 " SINDEX_BASEDIR Project top directory.\n"
112 "Report bugs to authors.\n"
114 progname
, semind_dbfile
);
118 static void show_help_add(int ret
)
121 "Usage: %1$s add [options] [--] [compiler options] files...\n"
123 "Utility creates or updates a symbol index.\n"
126 " --include-local-syms Include into the index local symbols;\n"
127 " -v, --verbose Show information about what is being done;\n"
128 " -h, --help Show this text and exit.\n"
130 "Report bugs to authors.\n"
137 static void show_help_rm(int ret
)
140 "Usage: %1$s rm [options] pattern\n"
142 "Utility removes source files from the index.\n"
143 "The pattern is a glob(7) wildcard pattern.\n"
146 " -v, --verbose Show information about what is being done;\n"
147 " -h, --help Show this text and exit.\n"
149 "Report bugs to authors.\n"
155 static void show_help_search(int ret
)
158 "Usage: %1$s search [options] [pattern]\n"
159 " or: %1$s search [options] (-e|-l) filename[:linenr[:column]]\n"
161 "Utility searches information about symbol by pattern.\n"
162 "The pattern is a glob(7) wildcard pattern.\n"
165 " -f, --format=STRING Specify an output format;\n"
166 " -p, --path=PATTERN Search symbols only in specified directories;\n"
167 " -m, --mode=MODE Search only the specified type of access;\n"
168 " -k, --kind=KIND Specify a kind of symbol;\n"
169 " -e, --explain Show what happens in the specified file position;\n"
170 " -l, --location Show usage of symbols from a specific file position;\n"
171 " -v, --verbose Show information about what is being done;\n"
172 " -h, --help Show this text and exit.\n"
174 "The KIND can be one of the following: `s', `f', `v', `m'.\n"
176 "Report bugs to authors.\n"
182 static void semind_print_progname(void)
184 fprintf(stderr
, "%s: ", progname
);
186 fprintf(stderr
, "%s: ", semind_command
);
189 static void semind_error(int status
, int errnum
, const char *fmt
, ...)
192 semind_print_progname();
195 vfprintf(stderr
, fmt
, ap
);
199 fprintf(stderr
, ": %s", strerror(errnum
));
201 fprintf(stderr
, "\n");
207 static void set_search_modmask(const char *v
)
209 size_t n
= strlen(v
);
211 if (n
!= 1 && n
!= 3)
212 semind_error(1, 0, "the length of mode value must be 1 or 3: %s", v
);
214 semind_search_modmask_defined
= 1;
215 semind_search_modmask
= 0;
219 case 'r': v
= "rrr"; break;
220 case 'w': v
= "ww-"; break;
221 case 'm': v
= "mmm"; break;
222 case '-': v
= "---"; break;
223 default: semind_error(1, 0, "unknown modificator: %s", v
);
225 } else if (!strcmp(v
, "def")) {
226 semind_search_modmask
= U_DEF
;
230 static const int modes
[] = {
231 U_R_AOF
, U_W_AOF
, U_R_AOF
| U_W_AOF
,
232 U_R_VAL
, U_W_VAL
, U_R_VAL
| U_W_VAL
,
233 U_R_PTR
, U_W_PTR
, U_R_PTR
| U_W_PTR
,
236 for (int i
= 0; i
< 3; i
++) {
238 case 'r': semind_search_modmask
|= modes
[i
* 3]; break;
239 case 'w': semind_search_modmask
|= modes
[i
* 3 + 1]; break;
240 case 'm': semind_search_modmask
|= modes
[i
* 3 + 2]; break;
242 default: semind_error(1, 0,
243 "unknown modificator in the mode value"
244 " (`r', `w', `m' or `-' expected): %c", v
[i
]);
249 static void parse_cmdline(int argc
, char **argv
)
251 static const struct option long_options
[] = {
252 { "database", required_argument
, NULL
, 'D' },
253 { "basedir", required_argument
, NULL
, 'B' },
254 { "verbose", no_argument
, NULL
, 'v' },
255 { "help", no_argument
, NULL
, 'h' },
259 char *basedir
= getenv("SINDEX_BASEDIR");
262 if ((env
= getenv("SINDEX_DATABASE")) != NULL
)
265 while ((c
= getopt_long(argc
, argv
, "+B:D:vh", long_options
, NULL
)) != -1) {
268 semind_dbfile
= optarg
;
281 if (optind
== argc
) {
282 message("command required");
287 if (!realpath(basedir
, cwd
))
288 semind_error(1, errno
, "unable to get project base directory");
293 static void parse_cmdline_add(int argc
, char **argv
)
295 static const struct option long_options
[] = {
296 { "include-local-syms", no_argument
, NULL
, 1 },
297 { "verbose", no_argument
, NULL
, 'v' },
298 { "help", no_argument
, NULL
, 'h' },
305 while ((c
= getopt_long(argc
, argv
, "+vh", long_options
, NULL
)) != -1) {
308 semind_include_local_syms
= 1;
320 if (optind
== argc
) {
321 message("more arguments required");
328 // step back since sparse_initialize will ignore argv[0].
331 sparse_initialize(argc
- optind
, argv
+ optind
, &semind_filelist
);
334 static void parse_cmdline_rm(int argc
, char **argv
)
336 static const struct option long_options
[] = {
337 { "verbose", no_argument
, NULL
, 'v' },
338 { "help", no_argument
, NULL
, 'h' },
343 while ((c
= getopt_long(argc
, argv
, "+vh", long_options
, NULL
)) != -1) {
353 if (optind
== argc
) {
354 message("more arguments required");
359 static void parse_cmdline_search(int argc
, char **argv
)
361 static const struct option long_options
[] = {
362 { "explain", no_argument
, NULL
, 'e' },
363 { "format", required_argument
, NULL
, 'f' },
364 { "path", required_argument
, NULL
, 'p' },
365 { "location", no_argument
, NULL
, 'l' },
366 { "mode", required_argument
, NULL
, 'm' },
367 { "kind", required_argument
, NULL
, 'k' },
368 { "verbose", no_argument
, NULL
, 'v' },
369 { "help", no_argument
, NULL
, 'h' },
374 while ((c
= getopt_long(argc
, argv
, "+ef:m:k:p:lvh", long_options
, NULL
)) != -1) {
377 semind_search_by_location
= EXPLAIN_LOCATION
;
380 semind_search_by_location
= USAGE_BY_LOCATION
;
383 semind_search_format
= optarg
;
386 set_search_modmask(optarg
);
389 semind_search_kind
= tolower(optarg
[0]);
392 semind_search_path
= optarg
;
402 if (semind_search_by_location
) {
406 semind_error(1, 0, "one argument required");
413 if ((ptr
= strchr(str
, ':')) != NULL
)
417 if (!semind_search_filename
) {
418 semind_search_filename
= str
;
419 } else if (!semind_search_line
) {
420 semind_search_line
= atoi(str
);
421 } else if (!semind_search_column
) {
422 semind_search_column
= atoi(str
);
427 } else if (optind
< argc
)
428 semind_search_symbol
= argv
[optind
++];
431 static int query_appendf(sqlite3_str
*query
, const char *fmt
, ...)
437 sqlite3_str_vappendf(query
, fmt
, args
);
440 if ((status
= sqlite3_str_errcode(query
)) == SQLITE_OK
)
443 if (status
== SQLITE_NOMEM
)
444 message("not enough memory");
446 if (status
== SQLITE_TOOBIG
)
447 message("string too big");
452 static inline void sqlite_bind_text(sqlite3_stmt
*stmt
, const char *field
, const char *var
, int len
)
454 if (sqlite3_bind_text(stmt
, sqlite3_bind_parameter_index(stmt
, field
), var
, len
, SQLITE_STATIC
) != SQLITE_OK
)
455 semind_error(1, 0, "unable to bind value for %s: %s", field
, sqlite3_errmsg(semind_db
));
458 static inline void sqlite_bind_int64(sqlite3_stmt
*stmt
, const char *field
, long long var
)
460 if (sqlite3_bind_int64(stmt
, sqlite3_bind_parameter_index(stmt
, field
), var
) != SQLITE_OK
)
461 semind_error(1, 0, "unable to bind value for %s: %s", field
, sqlite3_errmsg(semind_db
));
464 static inline void sqlite_prepare(const char *sql
, sqlite3_stmt
**stmt
)
468 ret
= sqlite3_prepare_v2(semind_db
, sql
, -1, stmt
, NULL
);
469 if (ret
!= SQLITE_OK
&& ret
!= SQLITE_BUSY
)
470 semind_error(1, 0, "unable to prepare query: %s: %s", sqlite3_errmsg(semind_db
), sql
);
471 } while (ret
== SQLITE_BUSY
);
474 static inline void sqlite_prepare_persistent(const char *sql
, sqlite3_stmt
**stmt
)
478 ret
= sqlite3_prepare_v3(semind_db
, sql
, -1, SQLITE_PREPARE_PERSISTENT
, stmt
, NULL
);
479 if (ret
!= SQLITE_OK
&& ret
!= SQLITE_BUSY
)
480 semind_error(1, 0, "unable to prepare query: %s: %s", sqlite3_errmsg(semind_db
), sql
);
481 } while (ret
== SQLITE_BUSY
);
484 static inline void sqlite_reset_stmt(sqlite3_stmt
*stmt
)
486 // Contrary to the intuition of many, sqlite3_reset() does not reset the
487 // bindings on a prepared statement. Use this routine to reset all host
488 // parameters to NULL.
489 sqlite3_clear_bindings(stmt
);
493 static int sqlite_run(sqlite3_stmt
*stmt
)
495 int ret
= sqlite3_step(stmt
);
496 if (ret
!= SQLITE_DONE
&& ret
!= SQLITE_ROW
)
497 semind_error(1, 0, "unable to process query: %s: %s", sqlite3_errmsg(semind_db
), sqlite3_sql(stmt
));
501 static void sqlite_command(const char *sql
)
504 sqlite_prepare(sql
, &stmt
);
506 sqlite3_finalize(stmt
);
509 static sqlite3_int64
get_db_version(void)
512 sqlite3_int64 dbversion
;
514 sqlite_prepare("PRAGMA user_version", &stmt
);
516 dbversion
= sqlite3_column_int64(stmt
, 0);
517 sqlite3_finalize(stmt
);
522 static void set_db_version(void)
525 sqlite3_str
*query
= sqlite3_str_new(semind_db
);
527 if (query_appendf(query
, "PRAGMA user_version = %d", SINDEX_DATABASE_VERSION
) < 0)
530 sql
= sqlite3_str_finish(query
);
535 static void open_temp_database(void)
537 static const char *database_schema
[] = {
538 "ATTACH ':memory:' AS tempdb",
539 "CREATE TABLE tempdb.semind ("
540 " file INTEGER NOT NULL,"
541 " line INTEGER NOT NULL,"
542 " column INTEGER NOT NULL,"
543 " symbol TEXT NOT NULL,"
544 " kind INTEGER NOT NULL,"
546 " mode INTEGER NOT NULL"
551 for (int i
= 0; database_schema
[i
]; i
++)
552 sqlite_command(database_schema
[i
]);
555 static void open_database(const char *filename
, int flags
)
557 static const char *database_schema
[] = {
558 "CREATE TABLE file ("
559 " id INTEGER PRIMARY KEY AUTOINCREMENT,"
560 " name TEXT UNIQUE NOT NULL,"
561 " mtime INTEGER NOT NULL"
563 "CREATE TABLE semind ("
564 " file INTEGER NOT NULL REFERENCES file(id) ON DELETE CASCADE,"
565 " line INTEGER NOT NULL,"
566 " column INTEGER NOT NULL,"
567 " symbol TEXT NOT NULL,"
568 " kind INTEGER NOT NULL,"
570 " mode INTEGER NOT NULL"
572 "CREATE UNIQUE INDEX semind_0 ON semind (symbol, kind, mode, file, line, column)",
573 "CREATE INDEX semind_1 ON semind (file)",
577 int exists
= !access(filename
, R_OK
);
579 if (sqlite3_open_v2(filename
, &semind_db
, flags
, NULL
) != SQLITE_OK
)
580 semind_error(1, 0, "unable to open database: %s: %s", filename
, sqlite3_errmsg(semind_db
));
582 sqlite_command("PRAGMA journal_mode = WAL");
583 sqlite_command("PRAGMA synchronous = OFF");
584 sqlite_command("PRAGMA secure_delete = FAST");
585 sqlite_command("PRAGMA busy_timeout = 2147483647");
586 sqlite_command("PRAGMA foreign_keys = ON");
589 if (get_db_version() < SINDEX_DATABASE_VERSION
)
590 semind_error(1, 0, "%s: Database too old. Please rebuild it.", filename
);
596 for (int i
= 0; database_schema
[i
]; i
++)
597 sqlite_command(database_schema
[i
]);
600 struct index_record
{
615 static void insert_record(struct index_record
*rec
)
617 sqlite_bind_text(insert_rec_stmt
, "@context", rec
->context
, rec
->ctx_len
);
618 sqlite_bind_text(insert_rec_stmt
, "@symbol", rec
->symbol
, rec
->sym_len
);
619 sqlite_bind_int64(insert_rec_stmt
, "@kind", rec
->kind
);
620 sqlite_bind_int64(insert_rec_stmt
, "@mode", rec
->mode
);
621 sqlite_bind_int64(insert_rec_stmt
, "@file", rec
->file
);
622 sqlite_bind_int64(insert_rec_stmt
, "@line", rec
->line
);
623 sqlite_bind_int64(insert_rec_stmt
, "@column", rec
->col
);
624 sqlite_run(insert_rec_stmt
);
625 sqlite_reset_stmt(insert_rec_stmt
);
628 static void update_stream(void)
630 if (semind_streams_nr
>= input_stream_nr
)
633 semind_streams
= realloc(semind_streams
, input_stream_nr
* sizeof(struct semind_streams
));
635 semind_error(1, errno
, "realloc");
637 sqlite_run(lock_stmt
);
639 for (int i
= semind_streams_nr
; i
< input_stream_nr
; i
++) {
641 const char *filename
;
642 char fullname
[PATH_MAX
];
643 sqlite3_int64 cur_mtime
= 0;
645 if (input_streams
[i
].fd
!= -1) {
647 * FIXME: Files in the input_streams may be duplicated.
649 if (stat(input_streams
[i
].name
, &st
) < 0)
650 semind_error(1, errno
, "stat: %s", input_streams
[i
].name
);
652 cur_mtime
= st
.st_mtime
;
654 if (!realpath(input_streams
[i
].name
, fullname
))
655 semind_error(1, errno
, "realpath: %s", input_streams
[i
].name
);
657 if (!strncmp(fullname
, cwd
, n_cwd
) && fullname
[n_cwd
] == '/') {
658 filename
= fullname
+ n_cwd
+ 1;
659 semind_streams
[i
].id
= 0;
661 semind_streams
[i
].id
= -1;
665 semind_streams
[i
].id
= -1;
669 if (semind_verbose
> 1)
670 message("filename: %s", filename
);
672 sqlite_bind_text(select_file_stmt
, "@name", filename
, -1);
674 if (sqlite_run(select_file_stmt
) == SQLITE_ROW
) {
675 sqlite3_int64 old_mtime
;
677 semind_streams
[i
].id
= sqlite3_column_int64(select_file_stmt
, 0);
678 old_mtime
= sqlite3_column_int64(select_file_stmt
, 1);
680 sqlite_reset_stmt(select_file_stmt
);
682 if (cur_mtime
== old_mtime
)
685 sqlite_bind_text(delete_file_stmt
, "@name", filename
, -1);
686 sqlite_run(delete_file_stmt
);
687 sqlite_reset_stmt(delete_file_stmt
);
690 sqlite_reset_stmt(select_file_stmt
);
692 sqlite_bind_text(insert_file_stmt
, "@name", filename
, -1);
693 sqlite_bind_int64(insert_file_stmt
, "@mtime", cur_mtime
);
694 sqlite_run(insert_file_stmt
);
695 sqlite_reset_stmt(insert_file_stmt
);
697 semind_streams
[i
].id
= sqlite3_last_insert_rowid(semind_db
);
700 sqlite_run(unlock_stmt
);
702 semind_streams_nr
= input_stream_nr
;
705 static void r_symbol(unsigned mode
, struct position
*pos
, struct symbol
*sym
)
707 static struct ident null
;
708 struct ident
*ctx
= &null
;
709 struct index_record rec
;
713 if (semind_streams
[pos
->stream
].id
== -1)
716 if (!semind_include_local_syms
&& sym_is_local(sym
))
720 warning(*pos
, "empty ident");
725 ctx
= dissect_ctx
->ident
;
727 rec
.context
= ctx
->name
;
728 rec
.ctx_len
= ctx
->len
;
729 rec
.symbol
= sym
->ident
->name
;
730 rec
.sym_len
= sym
->ident
->len
;
731 rec
.kind
= sym
->kind
;
733 rec
.file
= semind_streams
[pos
->stream
].id
;
734 rec
.line
= pos
->line
;
740 static void r_member(unsigned mode
, struct position
*pos
, struct symbol
*sym
, struct symbol
*mem
)
742 static struct ident null
;
743 static char memname
[1024];
744 struct ident
*ni
, *si
, *mi
;
745 struct ident
*ctx
= &null
;
746 struct index_record rec
;
750 if (semind_streams
[pos
->stream
].id
== -1)
753 if (!semind_include_local_syms
&& sym_is_local(sym
))
756 ni
= built_in_ident("?");
757 si
= sym
->ident
?: ni
;
758 /* mem == NULL means entire struct accessed */
759 mi
= mem
? (mem
->ident
?: ni
) : built_in_ident("*");
762 ctx
= dissect_ctx
->ident
;
764 snprintf(memname
, sizeof(memname
), "%.*s.%.*s", si
->len
, si
->name
, mi
->len
, mi
->name
);
766 rec
.context
= ctx
->name
;
767 rec
.ctx_len
= ctx
->len
;
768 rec
.symbol
= memname
;
769 rec
.sym_len
= si
->len
+ mi
->len
+ 1;
772 rec
.file
= semind_streams
[pos
->stream
].id
;
773 rec
.line
= pos
->line
;
779 static void r_symdef(struct symbol
*sym
)
781 r_symbol(U_DEF
, &sym
->pos
, sym
);
784 static void r_memdef(struct symbol
*sym
, struct symbol
*mem
)
786 r_member(U_DEF
, &mem
->pos
, sym
, mem
);
789 static void command_add(int argc
, char **argv
)
791 static struct reporter reporter
= {
792 .r_symdef
= r_symdef
,
793 .r_symbol
= r_symbol
,
794 .r_memdef
= r_memdef
,
795 .r_member
= r_member
,
798 open_temp_database();
800 sqlite_prepare_persistent(
804 sqlite_prepare_persistent(
808 sqlite_prepare_persistent(
809 "INSERT OR IGNORE INTO tempdb.semind "
810 "(context, symbol, kind, mode, file, line, column) "
811 "VALUES (@context, @symbol, @kind, @mode, @file, @line, @column)",
814 sqlite_prepare_persistent(
815 "SELECT id, mtime FROM file WHERE name == @name",
818 sqlite_prepare_persistent(
819 "INSERT INTO file (name, mtime) VALUES (@name, @mtime)",
822 sqlite_prepare_persistent(
823 "DELETE FROM file WHERE name == @name",
826 dissect(&reporter
, semind_filelist
);
828 sqlite_run(lock_stmt
);
829 sqlite_command("INSERT OR IGNORE INTO semind SELECT * FROM tempdb.semind");
830 sqlite_run(unlock_stmt
);
832 sqlite3_finalize(insert_rec_stmt
);
833 sqlite3_finalize(select_file_stmt
);
834 sqlite3_finalize(insert_file_stmt
);
835 sqlite3_finalize(delete_file_stmt
);
836 sqlite3_finalize(lock_stmt
);
837 sqlite3_finalize(unlock_stmt
);
838 free(semind_streams
);
841 static void command_rm(int argc
, char **argv
)
845 sqlite_command("BEGIN IMMEDIATE");
846 sqlite_prepare("DELETE FROM file WHERE name GLOB @file", &stmt
);
848 if (semind_verbose
> 1)
849 message("SQL: %s", sqlite3_sql(stmt
));
851 for (int i
= 0; i
< argc
; i
++) {
852 sqlite_bind_text(stmt
, "@file", argv
[i
], -1);
854 sqlite_reset_stmt(stmt
);
857 sqlite3_finalize(stmt
);
858 sqlite_command("COMMIT");
861 static inline void print_mode(char *value
)
871 #define U(m) "-rwm"[(v / m) & 3]
880 static char *semind_file_name
;
881 static FILE *semind_file_fd
;
882 static int semind_file_lnum
;
883 static char *semind_line
;
884 static size_t semind_line_buflen
;
885 static int semind_line_len
;
887 static void print_file_line(const char *name
, int lnum
)
890 * All files are sorted by name and line number. So, we can reopen
891 * the file and read it line by line.
893 if (!semind_file_name
|| strcmp(semind_file_name
, name
)) {
894 if (semind_file_fd
) {
895 fclose(semind_file_fd
);
896 free(semind_file_name
);
899 semind_file_name
= strdup(name
);
901 if (!semind_file_name
)
902 semind_error(1, errno
, "strdup");
904 semind_file_fd
= fopen(name
, "r");
907 semind_error(1, errno
, "fopen: %s", name
);
909 semind_file_lnum
= 0;
913 if (semind_file_lnum
== lnum
) {
914 if (semind_line
[semind_line_len
-1] == '\n')
916 printf("%.*s", semind_line_len
, semind_line
);
921 } while((semind_line_len
= getline(&semind_line
, &semind_line_buflen
, semind_file_fd
)) != -1);
923 if (errno
&& errno
!= EOF
)
924 semind_error(1, errno
, "getline");
927 static int search_query_callback(void *data
, int argc
, char **argv
, char **colname
)
929 char *fmt
= (char *) semind_search_format
;
934 while (*fmt
!= '\0') {
940 case 't': c
= '\t'; break;
941 case 'r': c
= '\r'; break;
942 case 'n': c
= '\n'; break;
944 } else if (c
== '%') {
951 semind_error(1, 0, "unexpected end of format string");
954 case 'f': colnum
= 0; goto print_string
;
955 case 'l': colnum
= 1; goto print_string
;
956 case 'c': colnum
= 2; goto print_string
;
957 case 'C': colnum
= 3; goto print_string
;
958 case 'n': colnum
= 4; goto print_string
;
961 printf("%.*s", n
, buf
);
969 printf("%.*s", n
, buf
);
972 printf("%c", atoi(argv
[6]));
977 printf("%.*s", n
, buf
);
980 print_file_line(argv
[0], atoi(argv
[1]));
986 printf("%.*s", n
, buf
);
989 printf("%s", argv
[colnum
]);
998 semind_error(1, 0, "invalid format specification: %%%c", c
);
1001 } else if (c
== '\\') {
1007 if (n
== sizeof(buf
)) {
1008 printf("%.*s", n
, buf
);
1017 printf("%.*s", n
, buf
);
1023 static void command_search(int argc
, char **argv
)
1027 sqlite3_str
*query
= sqlite3_str_new(semind_db
);
1030 semind_error(1, errno
, "unable to change directory: %s", cwd
);
1032 if (query_appendf(query
,
1041 "FROM semind, file "
1042 "WHERE semind.file == file.id") < 0)
1045 if (semind_search_kind
) {
1046 if (query_appendf(query
, " AND semind.kind == %d", semind_search_kind
) < 0)
1050 if (semind_search_symbol
) {
1053 if (query_appendf(query
, " AND ") < 0)
1056 if (strpbrk(semind_search_symbol
, "*?[]"))
1057 ret
= query_appendf(query
, "semind.symbol GLOB %Q", semind_search_symbol
);
1059 ret
= query_appendf(query
, "semind.symbol == %Q", semind_search_symbol
);
1065 if (semind_search_modmask_defined
) {
1066 if (!semind_search_modmask
) {
1067 if (query_appendf(query
, " AND semind.mode == %d", semind_search_modmask
) < 0)
1069 } else if (query_appendf(query
, " AND (semind.mode & %d) != 0", semind_search_modmask
) < 0)
1073 if (semind_search_path
) {
1074 if (query_appendf(query
, " AND file.name GLOB %Q", semind_search_path
) < 0)
1078 if (semind_search_by_location
== EXPLAIN_LOCATION
) {
1079 if (query_appendf(query
, " AND file.name == %Q", semind_search_filename
) < 0)
1081 if (semind_search_line
&&
1082 query_appendf(query
, " AND semind.line == %d", semind_search_line
) < 0)
1084 if (semind_search_column
&&
1085 query_appendf(query
, " AND semind.column == %d", semind_search_column
) < 0)
1087 } else if (semind_search_by_location
== USAGE_BY_LOCATION
) {
1088 if (query_appendf(query
, " AND semind.symbol IN (") < 0)
1090 if (query_appendf(query
,
1091 "SELECT semind.symbol FROM semind, file WHERE"
1092 " semind.file == file.id AND"
1093 " file.name == %Q", semind_search_filename
) < 0)
1095 if (semind_search_line
&&
1096 query_appendf(query
, " AND semind.line == %d", semind_search_line
) < 0)
1098 if (semind_search_column
&&
1099 query_appendf(query
, " AND semind.column == %d", semind_search_column
) < 0)
1101 if (query_appendf(query
, ")") < 0)
1105 if (query_appendf(query
, " ORDER BY file.name, semind.line, semind.column ASC", semind_search_path
) < 0)
1108 sql
= sqlite3_str_value(query
);
1110 if (semind_verbose
> 1)
1111 message("SQL: %s", sql
);
1113 sqlite3_exec(semind_db
, sql
, search_query_callback
, NULL
, &dberr
);
1115 semind_error(1, 0, "sql query failed: %s", dberr
);
1117 sql
= sqlite3_str_finish(query
);
1120 if (semind_file_fd
) {
1121 fclose(semind_file_fd
);
1122 free(semind_file_name
);
1128 int main(int argc
, char **argv
)
1130 static const struct command commands
[] = {
1133 .dbflags
= SQLITE_OPEN_READWRITE
| SQLITE_OPEN_CREATE
,
1134 .parse_cmdline
= parse_cmdline_add
,
1135 .handler
= command_add
1139 .dbflags
= SQLITE_OPEN_READWRITE
,
1140 .parse_cmdline
= parse_cmdline_rm
,
1141 .handler
= command_rm
1145 .dbflags
= SQLITE_OPEN_READONLY
,
1146 .parse_cmdline
= parse_cmdline_search
,
1147 .handler
= command_search
1151 const struct command
*cmd
;
1153 if (!(progname
= rindex(argv
[0], '/')))
1158 if (!realpath(".", cwd
))
1159 semind_error(1, errno
, "unable to get current directory");
1160 n_cwd
= strlen(cwd
);
1162 parse_cmdline(argc
, argv
);
1164 for (cmd
= commands
; cmd
->name
&& strcmp(argv
[optind
], cmd
->name
); cmd
++);
1166 semind_error(1, 0, "unknown command: %s", argv
[optind
]);
1169 semind_command
= cmd
->name
;
1171 if (cmd
->parse_cmdline
)
1172 cmd
->parse_cmdline(argc
, argv
);
1174 open_database(semind_dbfile
, cmd
->dbflags
);
1175 cmd
->handler(argc
- optind
, argv
+ optind
);
1177 sqlite3_close(semind_db
);