2 * ironout - A C refactoring tool
4 * Copyright (C) 2008 Ali Gholami Rudi
22 #define MAXPATHLEN 1024
24 static void require_args(int argc
, char **argv
, int required
, char *usage
)
26 if (argc
< required
+ 1) {
27 fprintf(stderr
, "Usage: %s %s\n\n", argv
[0], usage
);
28 fprintf(stderr
, "%s command requires at least %d arguments\n",
34 static int parse_cmd(int argc
, char **argv
)
37 require_args(argc
, argv
, 1, "FILENAME");
38 cfile
= cfile_init(argv
[1]);
44 static int getname_cmd(int argc
, char **argv
)
48 require_args(argc
, argv
, 2, "FILENAME OFFSET");
49 cfile
= cfile_init(argv
[1]);
50 found
= node_find(cfile
->node
, atoi(argv
[2]));
51 if (found
&& (found
->type
== AST_IDENTIFIER
||
52 found
->type
== AST_TYPENAME
))
58 static int find_cmd(int argc
, char **argv
)
61 char filename
[MAXPATHLEN
];
62 struct project
*project
;
64 struct hit
*hits
, *cur
;
66 require_args(argc
, argv
, 2, "FILENAME OFFSET");
67 dirname(dir
, argv
[1]);
68 basename(filename
, argv
[1]);
70 project
= project_init(".");
71 cfile
= project_find(project
, filename
);
74 name
= name_find(project
, cfile
, argv
[2]);
75 hits
= find_name(project
, name
);
78 printf("%s %ld %ld\n", cur
->cfile
->name
, cur
->start
, cur
->end
);
82 project_free(project
);
86 static int rename_cmd(int argc
, char **argv
)
89 char filename
[MAXPATHLEN
];
90 struct project
*project
;
93 require_args(argc
, argv
, 3, "FILENAME OFFSET NEWNAME");
94 dirname(dir
, argv
[1]);
95 basename(filename
, argv
[1]);
97 project
= project_init(".");
98 cfile
= project_find(project
, filename
);
101 name
= name_find(project
, cfile
, argv
[2]);
102 rename_name(project
, name
, argv
[3]);
103 project_free(project
);
108 "Usage: %s COMMAND [ARGS]\n\n"
110 " rename: rename a name and report the changes as a patch\n"
111 " find: find and list occurrences of a name\n"
112 " getname: print the name under point\n"
113 " version: print version number and exit\n";
115 static void print_help(int argc
, char **argv
)
117 printf(usage
, argv
[0]);
120 static char *version
=
122 "Copyright (C) 2008 Ali Gholami Rudi\n"
123 "You can redistribute and/or modify Ironout under the terms\n"
124 "of GNU General Public License. For more information, see\n"
125 "the file named COPYING.\n";
128 static void print_version(int argc
, char **argv
)
133 int main(int argc
, char **argv
)
136 while ((c
= getopt(argc
, argv
, "vh")) != -1) {
139 print_version(argc
, argv
);
142 print_help(argc
, argv
);
146 fprintf(stderr
, "Bad command line option\n");
151 char *command
= argv
[optind
];
152 char **nargv
= argv
+ optind
;
153 int nargc
= argc
- optind
;
154 if (!strcmp(command
, "parse"))
155 return parse_cmd(nargc
, nargv
);
156 if (!strcmp(command
, "getname"))
157 return getname_cmd(nargc
, nargv
);
158 if (!strcmp(command
, "find"))
159 return find_cmd(nargc
, nargv
);
160 if (!strcmp(command
, "rename"))
161 return rename_cmd(nargc
, nargv
);
162 if (!strcmp(command
, "version")) {
163 print_version(argc
, argv
);
167 print_help(argc
, argv
);