1

See following example. I don't want the , char in a separate line. I go through all indent options but seems like no one is for this.

$ indent -version
GNU indent 2.2.9
$ cat foo.c
void
foo ()
{
  struct_a arr[] = {
    {&a, sizeof (a)},
    {&b, sizeof (b)},
    {&c, sizeof (c)},
    {&d, sizeof (d)},
  };
}
$ indent -st foo.c
void
foo ()
{
  struct_a arr[] = {
    {&a, sizeof (a)}
    ,
    {&b, sizeof (b)}
    ,
    {&c, sizeof (c)}
    ,
    {&d, sizeof (d)}
    ,
  };
}
$
su.root
  • 1,080

2 Answers2

0

Reference 1.7 Declarations:

If the ‘-bc’ option is specified, a newline is forced after each comma in a declaration. For example,

int a,
  b,
  c;

With the ‘-nbc’ option this would look like

int a, b, c;

You need to use the -nbc option to get the output you want.

Note that this will disable newlines after every , in a declaration.

You may want to look at 1.10 Disabling Formatting to turn off formating for a particular section of code.

For example:

void
foo ()
{
/* *INDENT-OFF* */
  struct_a arr[] = {
    {&a, sizeof (a)},
    {&b, sizeof (b)},
    {&c, sizeof (c)},
    {&d, sizeof (d)},
  };
/* *INDENT-ON* */
}
DavidPostill
  • 162,382
0

Seems like it's sizeof() that confused indent. So I have a workaround: First, change all occurrences of sizeof with SIZEOF (e.g. using sed), then invoke indent, and then change SIZEOF back to sizeof.

$ cat foo.c
void foo() {
    struct_a arr[] = {
        {&a, sizeof (a), 1},
        {&b, sizeof (b), 1},
        {&c, sizeof (c), 1},
        {&d, sizeof (d), 1},
    };
}
$ indent -st foo.c
void
foo ()
{
  struct_a arr[] = {
    {&a, sizeof (a), 1}
    ,
    {&b, sizeof (b), 1}
    ,
    {&c, sizeof (c), 1}
    ,
    {&d, sizeof (d), 1}
    ,
  };
}
$ sed s/sizeof/SIZEOF/g foo.c | indent -st | sed s/SIZEOF/sizeof/g
void
foo ()
{
  struct_a arr[] = {
    {&a, sizeof (a), 1},
    {&b, sizeof (b), 1},
    {&c, sizeof (c), 1},
    {&d, sizeof (d), 1},
  };
}
$
su.root
  • 1,080