Thread: Indent command

  1. #1
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115

    Indent command

    Hi,

    I am trying a custom indent command for C source codes. I used that on a solution for k&r problem. Do you like this indent ? Any flaws to be fixed ?

    Code:
    $ indent -as -nut -ts4 -bli0 -i4 -blf -nbfda -npsl -c0 -cd0 -nip -nlp -nprs -npcs -ncs -nsaf -nsai -nsaw  -l100 try3.c
    Example from here:

    Code:
    /* This program converts its input to upper case
     * (if argv[0] begins with U or u) or lower case.
     * If argc is 0, it prints an error and quits.
     */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    
    
    int main(int argc, char **argv)
    {
        int (*convcase[2])(int) = { toupper, tolower };
        int func;
        int result = EXIT_SUCCESS;
    
    
        int ch;
    
    
        if(argc > 0)
        {
            if(toupper((unsigned char)argv[0][0]) == 'U')
            {
                func = 0;
            }
            else
            {
                func = 1;
            }
    
    
            while((ch = getchar()) != EOF)
            {
                ch = (*convcase[func]) ((unsigned char)ch);
                putchar(ch);
            }
        }
        else
        {
            fprintf(stderr, "Unknown name. Can't decide what to do.\n");
            result = EXIT_FAILURE;
        }
    
    
        return result;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    "Indent" usually means something like "place the text at some offset relative to the current margin", hence the common notion of indenting code by a certain number of spaces. What you're doing here, on the other hand, isn't indenting but rather changing the case of alphabetic characters based on the program name.

    I think you can simplify a little:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    int main(int argc, char **argv)
    {
        int (*convcase)(int);
        int result = EXIT_SUCCESS;
    
        int ch;
    
        if(argc > 0)
        {
            if(toupper(argv[0][0]) == 'U')
            {
                convcase = toupper;
            }
            else
            {
                convcase = tolower;
            }
    
            while((ch = getchar()) != EOF)
            {
                putchar(convcase(ch));
            }
        }
        else
        {
            fprintf(stderr, "Unknown name. Can't decide what to do.\n");
            result = EXIT_FAILURE;
        }
    
        return result;
    }
    However, checking argv[0][0] alone may be a little too simplistic: the format of the program name is implementation defined, so there's no guarantee that it will be file name itself rather than say, a file path.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    41
    Is it possible that this portion of the code actually executes?

    Code:
        {
            fprintf(stderr, "Unknown name. Can't decide what to do.\n");
            result = EXIT_FAILURE;
        }
    It looks to me like 'argc' will always either be 1 or more than one.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by ordak View Post
    Code:
    /* This program converts its input to upper case
     * (if argv[0] begins with U or u) or lower case.
     * If argc is 0, it prints an error and quits.
     */
    The C18 Standard does state:
    5.1.2.2.1 Program startup
    ...

    The value of argc shall be nonnegative.
    In most O/Ss, argc is at least 1, and argv[0] is the program name, or full file path. If argc > 1, then the remainder of the command line arguments are options, file names, or other text passed to the program.
    Last edited by rstanley; 4 Weeks Ago at 07:46 AM.

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    This topic is about code indenting. the supplied code was just an example for that. Indent is a Linux command. First let me try another code from the same page:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    char *getbasename(char *);
    
    
    /* convert input to lower or upper case depending on argv[0]*/
    int main(int argc, char *argv[])
    {
        int c, (*convert) (int) = NULL;
        char *bn;
    
    
        if(strcmp((bn = getbasename(argv[0])), "lower") == 0)
            convert = tolower;
        else if(strcmp(bn, "upper") == 0)
            convert = toupper;
        else
            return 1;
        while((c = getchar()) != EOF)
            putchar((*convert) (c));
        return 0;
    }
    
    
    char *getbasename(char *s)
    {
        char *p;
        char *base;
    
    
        for(base = p = s; *p != '\0'; p++)
            if(*p == '/')
                base = p;
        return base + 1;
    }
    I hope this one is more to point.

    For some reasons I wanted to indent some codes. In detail github.com shows tabs as 8 characters instead of expected 4. One solution is to use spaces instead of tabs. So I decided to use this indent command to indent some codes at my reach. I wonder whether others would like the indent that I supplied in the original post. I expect to be informed about the way some rest codes are indented to see whether this indentation is broken.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    EDIT:
    Oh wait, I think I understand now.

    You're not talking about programming in C to improve some sample solutions that you encountered. You're talking about whether calling this command:
    Code:
    indent -as -nut -ts4 -bli0 -i4 -blf -nbfda -npsl -c0 -cd0 -nip -nlp -nprs -npcs -ncs -nsaf -nsai -nsaw  -l100 try3.c
    to indent C code looks nice to other C programmers. My answer is that it looks fine. Don't sweat the small stuff like this kind of subjective code style as precise details usually matter much less than reasonable consistency, which of course you'll achieve by auto-formatting with an indent command.
    Last edited by laserlight; 4 Weeks Ago at 08:07 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    Quote Originally Posted by laserlight View Post
    No, it isn't. The supplied code does not do any indenting at all.


    I'm really confused: isn't it obvious that these are examples of programs that change the alphabetic case of the input? They have nothing to do with indenting of code. You cannot ident anything merely by calling toupper or tolower on each input character and then printing the result.

    I mean, this is the question stated in the page you linked to:

    Nothing at all about indenting.
    I just indented the first and second code with the given command. Let's see code as it is on link provided:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    char *getbasename(char *);
    
    
    /* convert input to lower or upper case depending on argv[0]*/
    int main(int argc, char *argv[])
    {
        int c, (*convert)(int) = NULL;
        char* bn;
    
    
        if (strcmp((bn = getbasename(argv[0])), "lower") == 0)
            convert = tolower;
        else if (strcmp(bn, "upper") == 0)
            convert = toupper;
        else
            return 1;
        while ((c = getchar()) != EOF)
            putchar((*convert)(c));
        return 0;
    }
    
    
    char *getbasename(char *s)
    {
        char *p;
        char *base;
    
    
        for (base = p = s; *p != '\0'; p++)
            if (*p == '/')
                base = p;
        return base + 1;
    }
    Tabs before some statements are 8 spaces (on some sites anyway). With the first command they became 4:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    char *getbasename(char *);
    
    
    /* convert input to lower or upper case depending on argv[0]*/
    int main(int argc, char *argv[])
    {
        int c, (*convert) (int) = NULL;
        char *bn;
    
    
        if(strcmp((bn = getbasename(argv[0])), "lower") == 0)
            convert = tolower;
        else if(strcmp(bn, "upper") == 0)
            convert = toupper;
        else
            return 1;
        while((c = getchar()) != EOF)
            putchar((*convert) (c));
        return 0;
    }
    
    
    char *getbasename(char *s)
    {
        char *p;
        char *base;
    
    
        for(base = p = s; *p != '\0'; p++)
            if(*p == '/')
                base = p;
        return base + 1;
    }
    Last edited by ordak; 4 Weeks Ago at 08:12 AM. Reason: added some info

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by ordak View Post
    This topic is about code indenting. the supplied code was just an example for that. Indent is a Linux command. First let me try another code from the same page:
    ...
    My response is also valid to the second code example you provided. It is important to point out errors in any code posted, no matter who actual wrote the code, or if it actually demonstrated a possible answer to the original questions. There many people besides yourself that read the postings here.

    Run the following code:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
       printf("argc == %d\n", argc);
    
       for(int i = 0; i < argc; ++i)
       {
          printf("argv[%2d] == %s\n", i, argv[i]);
       }
    
       return 0;
    }
    Output:
    Code:
    ./foo one two three
    argc == 4
    argv[ 0] == ./foo
    argv[ 1] == one
    argv[ 2] == two
    argv[ 3] == three

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Quote Originally Posted by ordak
    I just indented the first and second code with the given command. Let's see code as it is on link provided:
    Yeah, refer to my edit: I realised that I misread the post because I was led astray by the question and sample solutions given in the linked page instead of reading your explanation carefully. No matter now, but it would have been clearer to me if you had posted an unindented or deliberately badly indented snippet of code, and then posted the indented version for comparison (or posted the comparison in your most recent post, but then that goes back to the same thing: it is just a matter of subjective style).
    Last edited by laserlight; 4 Weeks Ago at 08:19 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    Quote Originally Posted by rstanley View Post
    My response is also valid to the second code example you provided. It is important to point out errors in any code posted, no matter who actual wrote the code, or if it actually demonstrated a possible answer to the original questions. There many people besides yourself that read the postings here.

    Run the following code:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
       printf("argc == %d\n", argc);
    
       for(int i = 0; i < argc; ++i)
       {
          printf("argv[%2d] == %s\n", i, argv[i]);
       }
    
       return 0;
    }
    Output:
    Code:
    ./foo one two three
    argc == 4
    argv[ 0] == ./foo
    argv[ 1] == one
    argv[ 2] == two
    argv[ 3] == three
    I accept pointing out mistakes in codes. Thanks.

  11. #11
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    Let's start with a simple program:

    $ indent try6.c
    Code:
    #include <stdio.h>
    
    
    // Comment 1
    int
    main (void)
    {
      printf ("Hello world.\n");    // Comment 2
      return 0;            /* Comment 3 */
    }
    I do not like some aspects of indentation in code above. So I use a customized indent:

    $ indent -as -nut -ts4 -bli0 -i4 -blf -nbfda -npsl -c0 -cd0 -nip -nlp -nprs -npcs -ncs -nsaf -nsai -nsaw -l100 try6.c

    Code:
    #include <stdio.h>
    
    
    // Comment 1
    int main(void)
    {
        printf("Hello world.\n");   // Comment 2
        return 0;   /* Comment 3 */
    }
    Possibilities are a lot here.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,351
    Yeah, but isn't this futile? Everyone has their own preferences, e.g., I feel three spaces before a trailing comment is unnecessary, but frankly, it looks alright.

    When I think of flaws, I think of something like the misleading indent that led to the goto fail bug. One solution to that is to make the use of braces mandatory so that such a bug becomes obvious. But if you're using a tool like indent as part of an automated pre-commit process, then the misleading indent would be fixed by the tool, so mandatory braces no longer have that value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    You have to experiment with the various options to indent, until you get the code to look like you want.

    Work with one or two related options at a time, make notes, then another one or two by themselves, then combine your chosen options and run on a more complex code file.

    Also look at the Wikipedia article on Indentation Style.

  14. #14
    Registered User
    Join Date
    Oct 2019
    Posts
    41
    Quote Originally Posted by laserlight View Post

    Everyone has their own preferences
    It looks a bit like what Microsoft Visual Studio editor produces by default. Definitely okay if you ask me.

    The question was also not immediately clear to me. The 'indent' command was unknown to me prior to this post.

  15. #15
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    115
    There is also another LLVM Clang utility: clang-format

    Code:
    clang-format -style=Microsoft -i try6.c
    Code:
    #include <stdio.h>
    
    
    struct str
    {
        int i;
        char c;
        double d[10];
    };
    
    
    void ifs(void)
    {
        if (1)
        {
            if (1)
            {
                return;
            }
        }
        return;
    }
    
    
    void loop(void)
    {
        while (0)
        {
            for (; 0;)
            {
                return;
            }
        }
        return;
    }
    
    
    // Comment 1
    int main(void)
    {
        printf("Hello world.\n"); // Comment 2
        return 0;                 /* Comment 3 */
    }
    /* Comment 4 */
    And

    Code:
    clang-format -style=WebKit -i try6.c
    Code:
    #include <stdio.h>
    
    
    struct str {
        int i;
        char c;
        double d[10];
    };
    
    
    void ifs(void)
    {
        if (1) {
            if (1) {
                return;
            }
        }
        return;
    }
    
    
    void loop(void)
    {
        while (0) {
            for (; 0;) {
                return;
            }
        }
        return;
    }
    
    
    // Comment 1
    int main(void)
    {
        printf("Hello world.\n"); // Comment 2
        return 0; /* Comment 3 */
    }
    /* Comment 4 */
    Details are here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to properly indent and format a source code?
    By nerio in forum C Programming
    Replies: 10
    Last Post: 02-05-2016, 11:17 AM
  2. How do you indent: spaces or tabs?
    By EVOEx in forum General Discussions
    Replies: 51
    Last Post: 09-24-2009, 07:10 AM
  3. How far do you indent?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 09-17-2005, 04:36 PM
  4. how to indent a file.
    By hakim12 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2003, 02:55 PM
  5. Indent code
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 01-23-2003, 11:27 AM

Tags for this Thread