Added the indent target as an early attempt at a C code beautifier
This commit is contained in:
parent
556fb27dd2
commit
5af09f8f84
1
Makefile
1
Makefile
|
@ -98,6 +98,7 @@ dist:
|
|||
$(PACKAGE)-$(VERSION)/src/sets/project.conf \
|
||||
$(PACKAGE)-$(VERSION)/src/target/as.c \
|
||||
$(PACKAGE)-$(VERSION)/src/target/graph.c \
|
||||
$(PACKAGE)-$(VERSION)/src/target/indent.c \
|
||||
$(PACKAGE)-$(VERSION)/src/target/Makefile \
|
||||
$(PACKAGE)-$(VERSION)/src/target/project.conf \
|
||||
$(PACKAGE)-$(VERSION)/Makefile \
|
||||
|
|
|
@ -30,6 +30,7 @@ typedef struct _TargetPlugin
|
|||
C99Option * options;
|
||||
int (*init)(char const * outfile, int optlevel);
|
||||
int (*exit)(void);
|
||||
int (*token)(Token * token);
|
||||
int (*section)(char const * name);
|
||||
int (*function_begin)(char const * name);
|
||||
int (*function_call)(char const * name);
|
||||
|
|
18
src/code.c
18
src/code.c
|
@ -85,6 +85,7 @@ static int _code_context_queue_identifier(Code * code, char const * identifier);
|
|||
/* target */
|
||||
static int _code_target_init(Code * code, char const * outfile, int optlevel);
|
||||
static int _code_target_exit(Code * code);
|
||||
static int _code_target_token(Code * code, Token * token);
|
||||
static int _code_target_function_begin(Code * code, char const * name);
|
||||
static int _code_target_function_call(Code * code, char const * name);
|
||||
static int _code_target_function_end(Code * code);
|
||||
|
@ -170,6 +171,16 @@ static int _code_target_exit(Code * code)
|
|||
}
|
||||
|
||||
|
||||
/* code_target_token */
|
||||
static int _code_target_token(Code * code, Token * token)
|
||||
{
|
||||
DEBUG_FUNC();
|
||||
if(code->target->token == NULL)
|
||||
return 0;
|
||||
return code->target->token(token);
|
||||
}
|
||||
|
||||
|
||||
/* code_target_function_begin */
|
||||
static int _code_target_function_begin(Code * code, char const * name)
|
||||
{
|
||||
|
@ -325,6 +336,13 @@ int code_delete(Code * code)
|
|||
|
||||
|
||||
/* useful */
|
||||
/* parsing */
|
||||
int code_token(Code * code, Token * token)
|
||||
{
|
||||
return _code_target_token(code, token);
|
||||
}
|
||||
|
||||
|
||||
/* context */
|
||||
/* code_context_get */
|
||||
CodeContext code_context_get(Code * code)
|
||||
|
|
|
@ -101,6 +101,8 @@ static int _scan_skip_meta(C99 * c99)
|
|||
|
||||
while(cpp_scan(c99->cpp, &c99->token) == 0)
|
||||
{
|
||||
if(code_token(c99->code, c99->token) != 0)
|
||||
return 1;
|
||||
if(c99->token == NULL)
|
||||
return 0;
|
||||
if((code = token_get_code(c99->token)) != C99_CODE_WHITESPACE
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
TARGETS = as.a as.so graph.a graph.so
|
||||
TARGETS = as.a as.so graph.a graph.so indent.a indent.so
|
||||
PREFIX = /usr/local
|
||||
DESTDIR =
|
||||
LIBDIR = $(PREFIX)/lib
|
||||
|
@ -40,14 +40,28 @@ graph.a: $(graph_OBJS)
|
|||
graph.so: $(graph_OBJS)
|
||||
$(LD) -o graph.so -Wl,-soname,graph.so.0 $(graph_OBJS)
|
||||
|
||||
indent_OBJS = indent.o
|
||||
indent_CFLAGS = $(CPPFLAGSF) $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
|
||||
indent_LDFLAGS = $(LDFLAGSF) $(LDFLAGS)
|
||||
|
||||
indent.a: $(indent_OBJS)
|
||||
$(AR) indent.a $(indent_OBJS)
|
||||
$(RANLIB) indent.a
|
||||
|
||||
indent.so: $(indent_OBJS)
|
||||
$(LD) -o indent.so -Wl,-soname,indent.so.0 $(indent_OBJS)
|
||||
|
||||
as.o: as.c ../../include/c99/target.h ../../config.h
|
||||
$(CC) $(as_CFLAGS) -c as.c
|
||||
|
||||
graph.o: graph.c ../../include/c99/target.h
|
||||
$(CC) $(graph_CFLAGS) -c graph.c
|
||||
|
||||
indent.o: indent.c ../../include/c99/target.h
|
||||
$(CC) $(indent_CFLAGS) -c indent.c
|
||||
|
||||
clean:
|
||||
$(RM) $(as_OBJS) $(graph_OBJS)
|
||||
$(RM) $(as_OBJS) $(graph_OBJS) $(indent_OBJS)
|
||||
|
||||
distclean: clean
|
||||
$(RM) $(TARGETS)
|
||||
|
@ -63,6 +77,11 @@ install: all
|
|||
$(INSTALL) -m 0755 graph.so $(DESTDIR)$(LIBDIR)/c99/target/graph.so.0.0
|
||||
$(LN) -s graph.so.0.0 $(DESTDIR)$(LIBDIR)/c99/target/graph.so.0
|
||||
$(LN) -s graph.so.0.0 $(DESTDIR)$(LIBDIR)/c99/target/graph.so
|
||||
$(MKDIR) $(DESTDIR)$(LIBDIR)/c99/target
|
||||
$(INSTALL) -m 0644 indent.a $(DESTDIR)$(LIBDIR)/c99/target/indent.a
|
||||
$(INSTALL) -m 0755 indent.so $(DESTDIR)$(LIBDIR)/c99/target/indent.so.0.0
|
||||
$(LN) -s indent.so.0.0 $(DESTDIR)$(LIBDIR)/c99/target/indent.so.0
|
||||
$(LN) -s indent.so.0.0 $(DESTDIR)$(LIBDIR)/c99/target/indent.so
|
||||
|
||||
uninstall:
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/as.a
|
||||
|
@ -73,5 +92,9 @@ uninstall:
|
|||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/graph.so.0.0
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/graph.so.0
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/graph.so
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/indent.a
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/indent.so.0.0
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/indent.so.0
|
||||
$(RM) $(DESTDIR)$(LIBDIR)/c99/target/indent.so
|
||||
|
||||
.PHONY: all clean distclean install uninstall
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2008 Pierre Pronchery <khorben@defora.org> */
|
||||
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS Devel c99 */
|
||||
/* c99 is not free software; you can redistribute it and/or modify it under the
|
||||
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||
|
@ -68,6 +68,7 @@ TargetPlugin target_plugin =
|
|||
_as_options,
|
||||
_as_init,
|
||||
_as_exit,
|
||||
NULL,
|
||||
_as_section,
|
||||
_as_function_begin,
|
||||
_as_function_call,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2008 Pierre Pronchery <khorben@defora.org> */
|
||||
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS Devel c99 */
|
||||
/* c99 is not free software; you can redistribute it and/or modify it under the
|
||||
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||
|
@ -47,6 +47,7 @@ TargetPlugin target_plugin =
|
|||
NULL, /* options */
|
||||
_graph_init,
|
||||
_graph_exit,
|
||||
NULL,
|
||||
NULL, /* section */
|
||||
_graph_function_begin,
|
||||
_graph_function_call,
|
||||
|
|
172
src/target/indent.c
Normal file
172
src/target/indent.c
Normal file
|
@ -0,0 +1,172 @@
|
|||
/* $Id$ */
|
||||
/* Copyright (c) 2009 Pierre Pronchery <khorben@defora.org> */
|
||||
/* This file is part of DeforaOS Devel c99 */
|
||||
/* c99 is not free software; you can redistribute it and/or modify it under the
|
||||
* terms of the Creative Commons Attribution-NonCommercial-ShareAlike 3.0
|
||||
* Unported as published by the Creative Commons organization.
|
||||
*
|
||||
* c99 is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the Creative Commons Attribution-NonCommercial-
|
||||
* ShareAlike 3.0 Unported license for more details.
|
||||
*
|
||||
* You should have received a copy of the Creative Commons Attribution-
|
||||
* NonCommercial-ShareAlike 3.0 along with c99; if not, browse to
|
||||
* http://creativecommons.org/licenses/by-nc-sa/3.0/ */
|
||||
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include "c99/target.h"
|
||||
#include "../common.h"
|
||||
|
||||
#define max(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define min(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
|
||||
/* indent */
|
||||
/* private */
|
||||
/* variables */
|
||||
static FILE * _fp;
|
||||
static char * _filename;
|
||||
static int _scope = 0;
|
||||
static int _newline = 0;
|
||||
|
||||
|
||||
/* protected */
|
||||
/* prototypes */
|
||||
static int _indent_init(char const * outfile, int optlevel);
|
||||
static int _indent_exit(void);
|
||||
static int _indent_token(Token * token);
|
||||
static int _indent_function_begin(char const * name);
|
||||
static int _indent_function_call(char const * name);
|
||||
static int _indent_function_end(void);
|
||||
|
||||
|
||||
/* public */
|
||||
/* variables */
|
||||
TargetPlugin target_plugin =
|
||||
{
|
||||
NULL, /* options */
|
||||
_indent_init,
|
||||
_indent_exit,
|
||||
_indent_token, /* parsing */
|
||||
NULL, /* section */
|
||||
_indent_function_begin,
|
||||
_indent_function_call,
|
||||
_indent_function_end,
|
||||
NULL /* label_set */
|
||||
};
|
||||
|
||||
|
||||
/* protected */
|
||||
/* functions */
|
||||
/* indent_init */
|
||||
static int _indent_init(char const * outfile, int optlevel)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s(\"%s\", %d)\n", __func__, outfile, optlevel);
|
||||
#endif
|
||||
if((_filename = strdup(outfile)) == NULL)
|
||||
return error_set_code(1, "%s", strerror(errno));
|
||||
if((_fp = fopen(outfile, "w")) == NULL)
|
||||
{
|
||||
free(_filename);
|
||||
return error_set_code(1, "%s: %s", outfile, strerror(errno));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* indent_exit */
|
||||
static int _indent_exit(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s()\n", __func__);
|
||||
#endif
|
||||
if(fclose(_fp) != 0)
|
||||
ret |= error_set_code(1, "%s: %s", _filename, strerror(errno));
|
||||
free(_filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* indent_token */
|
||||
static int _indent_token(Token * token)
|
||||
{
|
||||
char const * str;
|
||||
C99Code code;
|
||||
int i;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s()\n", __func__);
|
||||
#endif
|
||||
if(token == NULL)
|
||||
return 0;
|
||||
str = token_get_string(token);
|
||||
if((code = token_get_code(token)) == C99_CODE_WHITESPACE)
|
||||
{
|
||||
if(strchr(str, '\n') != NULL)
|
||||
_newline = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(code == C99_CODE_OPERATOR_RBRACE
|
||||
|| code == C99_CODE_OPERATOR_RBRACKET)
|
||||
_scope--;
|
||||
if(code < C99_CODE_META_FIRST || code > C99_CODE_META_LAST)
|
||||
{
|
||||
if(_newline != 0)
|
||||
{
|
||||
for(i = 0; i < _scope; i++)
|
||||
fputc('\t', _fp);
|
||||
_newline = 0;
|
||||
}
|
||||
}
|
||||
if(code == C99_CODE_OPERATOR_LBRACE
|
||||
|| code == C99_CODE_OPERATOR_LBRACKET)
|
||||
_scope++;
|
||||
}
|
||||
fputs(str, _fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* indent_function_begin */
|
||||
static int _indent_function_begin(char const * name)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s(\"%s\")\n", __func__, name);
|
||||
#endif
|
||||
_scope = 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/* indent_function_call */
|
||||
static int _indent_function_call(char const * name)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s(\"%s\")\n", __func__, name);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* indent_function_end */
|
||||
static int _indent_function_end(void)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "%s()\n", __func__);
|
||||
#endif
|
||||
_scope = 0;
|
||||
return 0;
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
targets=as,graph,indent
|
||||
cppflags=-I ../../include
|
||||
cflags_force=-W
|
||||
cflags=-Wall -g -O2 -fPIC -pedantic
|
||||
targets=as,graph
|
||||
dist=Makefile
|
||||
|
||||
[as]
|
||||
|
@ -20,3 +20,11 @@ install=$(LIBDIR)/c99/target
|
|||
|
||||
[graph.c]
|
||||
depends=../../include/c99/target.h
|
||||
|
||||
[indent]
|
||||
type=library
|
||||
sources=indent.c
|
||||
install=$(LIBDIR)/c99/target
|
||||
|
||||
[indent.c]
|
||||
depends=../../include/c99/target.h
|
||||
|
|
Loading…
Reference in New Issue
Block a user