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)}
    ,
  };
}
$
  • Which Linux version? – harrymc Mar 27 '15 at 12:18
  • The OS version does not matter for me. I'm using GNU indent 2.2.9. I can compile the latest version of indent if that would help. – pynexj Mar 27 '15 at 12:45
  • You might need to go into the sources to find out why -nbc does not work as recommended by @DavidPostill. – harrymc Mar 27 '15 at 13:09
  • -[n]bc only affects the var names as in int a, b, c;. It does not affect the initialization code as in = { ... };. – pynexj Mar 27 '15 at 13:42
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* */
}
  • Just tried but -nbc does not work for me. – pynexj Dec 23 '14 at 14:06
  • Did you try the Disabling Formatting option? – DavidPostill Dec 23 '14 at 15:07
  • It's not practical for me to insert such Disabling Formating strings in our production code and I may need to run indent regularly. – pynexj Dec 24 '14 at 2:00
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},
  };
}
$
  • I must say that this sounds very unlikely. The problem is probably more general than that. – harrymc Mar 27 '15 at 18:34
  • I'm not sure. You can try it. – pynexj Mar 28 '15 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.