tools/nxstyle.c: Add -r option to check specified range lines

Usage: ./nxstyle -r start,count filename

nxstyle with -r option used to parse the range lines rather than the whole file.

Change-Id: I58ec56511fde14d6ec914400a7849e69960a3711
Signed-off-by: liuhaitao <liuhaitao@xiaomi.com>
This commit is contained in:
liuhaitao 2020-01-02 17:22:08 +08:00 committed by Gregory Nutt
parent 9b2fa8152c
commit 7da409f3b5
2 changed files with 36 additions and 6 deletions

View File

@ -316,7 +316,7 @@ nxstyle.c
Prints formatted messages that are classified as info, warn, error,
fatal. In a parsable format that can be used by editors and IDEs.
Usage: nxstyle [-m <maxline>] [-v <level>] <filename>
Usage: nxstyle [-m <maxline>] [-v <level>] [-r <start,count>] <filename>
nxstyle -h this help
nxstyle -v <level> where level is
0 - no output

View File

@ -53,6 +53,7 @@
#define NXSTYLE_VERSION "0.01"
#define LINE_SIZE 512
#define RANGE_NUMBER 4096
#define FATAL(m, l, o) message(FATAL, (m), (l), (o))
#define FATALFL(m,s) message(FATAL, (m), -1, -1)
@ -96,6 +97,9 @@ static enum file_e g_file_type = UNKNOWN;
static int g_maxline = 78;
static int g_status = 0;
static int g_verbose = 2;
static int g_rangenumber = 0;
static int g_rangestart[RANGE_NUMBER];
static int g_rangecount[RANGE_NUMBER];
/****************************************************************************
* Private Functions
@ -109,7 +113,8 @@ static void show_usage(char *progname, int exitcode, char *what)
fprintf(stderr, "%s\n", what);
}
fprintf(stderr, "Usage: %s [-m <maxline>] [-v <level>] <filename>\n", basename(progname));
fprintf(stderr, "Usage: %s [-m <maxline>] [-v <level>] [-r <start,count>] <filename>\n",
basename(progname));
fprintf(stderr, " %s -h this help\n", basename(progname));
fprintf(stderr, " %s -v <level> where level is\n", basename(progname));
fprintf(stderr, " 0 - no output\n");
@ -118,10 +123,30 @@ static void show_usage(char *progname, int exitcode, char *what)
exit(exitcode);
}
static int skip(int lineno)
{
int i;
for (i = 0; i < g_rangenumber; i++)
{
if (lineno >= g_rangestart[i] && lineno < g_rangestart[i] + g_rangecount[i])
{
return 0;
}
}
return g_rangenumber != 0;
}
static int message(enum class_e class, const char *text, int lineno, int ndx)
{
FILE *out = stdout;
if (skip(lineno))
{
return g_status;
}
if (class > INFO)
{
out = stderr;
@ -217,7 +242,7 @@ int main(int argc, char **argv, char **envp)
int i;
int c;
while ((c = getopt(argc, argv, ":hv:gm:")) != -1)
while ((c = getopt(argc, argv, ":hv:gm:r:")) != -1)
{
switch (c)
{
@ -237,6 +262,11 @@ int main(int argc, char **argv, char **envp)
}
break;
case 'r':
g_rangestart[g_rangenumber] = atoi(strtok(optarg, ","));
g_rangecount[g_rangenumber++] = atoi(strtok(NULL, ","));
break;
case 'h':
show_usage(argv[0], 0, NULL);
break;