Thread: How do I pass options at the prompt.

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    How do I pass options at the prompt.

    First the code

    Code:
    [cdalten@localhost YAPP]$ more main.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int 
    main(int argc, char *argv[])
    {
    
      conference(&argc, &argv);
    
      /*cleanup();*/
      exit(EXIT_SUCCESS);
    }
    [cdalten@localhost YAPP]$
    Code:
    [cdalten@localhost YAPP]$ more driver.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char buf[BUFSIZ];
    
    int strip_line(char line[]) {
      int n;
    
      for (n = strlen(line)-1; n >= 0; n--)
        if (line[n] != ' ' && line[n] != '\t')
          break;
      line[n+1] = '\0';
      return n;
    }
    
    void conference_prompt(void) {
      fputs("Type Help for more information\n", stdout);
    
      do {
        strip_line(buf);
        fputs("OK:", stdout);
        fflush(stdout);
      } while (fgets(buf, BUFSIZ, stdin) != NULL); 
    }
    
    
    void conference(int *argc, char *argv[]) {
      if (*argc == 1) {
        conference_prompt();
      }
    }
    
    [cdalten@localhost YAPP]$
    [cdalten@localhost YAPP]$ gcc main.c driver.c -o yapp
    [cdalten@localhost YAPP]$ ./yapp
    Type Help for more information
    OK:
    What I want to do is for a user to get a help menu when they type 'h' or 'help' at the 'OK:' prompt. I looked at getopt() but that only seems to work with command line args. And the switch statement doesn't seem to want to work when it see something like 'help'. Ideas?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    You do it the same way for any function.
    Code:
    int foo(int argc, char **argv)
    {
            printf("argc = %i, argv[0] = %s\n", argc, argv[0]);
            return 0;
    }
    int main(int argc, char **argv)
    {
            return foo(argc, argv);
    }

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    But I don't want 'h' passed at the command line. Ie, I don't want

    ./yapp h
    I just want 'h' passed a the 'OK:' prompt.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Ah, I see. I gotta bad habit about not reading the whole sentence. . .

    Code:
    char *cmds[] = { "H", "HELP", "ETC", "\0" };
    int match(const char *which)
    {
            int i;
            for (i = 0; cmds[i][0] != '\0'; i++){
                    if (!strcmp(cmds[i], upcase(which)){
                            break;
                    }
            }
            if (cmds[i][0] != '\0'){
                    return -1;
            }
            return i;
    }
    You'll have to write upcase.

    [edit] switch(match(userinput)){[/edit]

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The GNU C Reference Manual

    Pretty much a "must read" if you are on linux.

    [edit] okay, I just looked over your first post and you know how to use fgets. Maybe you need to read about strcmp()...also, there is a strncmp() which will work with the first n characters in a string, alleviating the need for your "strip line" function*... kind of strange you are having a problem with this IMO[/edit]

    * which you should not use strlen in a for loop like that, it's a waste to do it over and over. Just set a variable to the strlen before the loop.
    Last edited by MK27; 10-13-2009 at 04:12 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You might want to read the manpage for getopt as that will do you option handling for you. Saves reinventing the wheel.

    Oops, I misread your post >_<

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like we hit 3 for 3 misreading your post Overworked_Phd. Maybe it is time for:

    How To Ask Questions The Smart Way*

    Anyway, my other thought was that you could use a restricted scanf template to read the input and lose the "strip_line function":
    Code:
    scanf("%32[a-zA-Z]",buf);
    I used 32 in place of BUFSIZE since you cannot use a define in the scanf template. This will read only contiguous characters in the set a-z and A-Z (no spaces, tabs, newlines, etc. Then use strcmp().

    *how did you not do this? Well, you said you were having a problem reading user input, which is not true. A common error is the desire is to post code demonstrating what you have done correctly, then tacking a vague question on, instead of focussing on what you are having a problem with.
    Last edited by MK27; 10-14-2009 at 06:35 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to pass function pointer as arg in c++
    By umen242 in forum C++ Programming
    Replies: 13
    Last Post: 10-21-2008, 02:09 AM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. endless loop for scanf - plz help
    By owi_just in forum C Programming
    Replies: 18
    Last Post: 03-20-2005, 01:41 PM
  4. using pointers to pass multi-d arrays
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 04-27-2004, 03:07 AM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM