* same with xv6
[mascara-docs.git] / i386 / ucla / src / lab5 / inc / args.h
blobd48ace5a694ee35a3c21231c7bbe9a7ca2389a61
1 #ifndef JOS_INC_ARGS_H
2 #define JOS_INC_ARGS_H
4 struct Argstate;
6 // JOS command-line parsing functions.
8 // Initializes the Argstate buffer from argc and argv.
9 // (Note: it is safe to use a 'const char **' for argv.)
10 void argstart(int *argc, char **argv, struct Argstate *args);
12 // Returns the next flag in the argument list,
13 // or -1 if there are no more flags.
15 // Flags stop at a non-flag (anything that doesn't start with '-'),
16 // at the end of the argument list, before "-", or after "--",
17 // whichever comes first. Any "--" argument is not returned.
19 // Consumes arguments from the argc/argv array passed in to argstart.
20 // If you argstart with an argc/argv array of ["sh", "-i", "foo"],
21 // the first call to argnext will return 'i' and change the array to
22 // ["sh", "foo"]. Thus, when argnext returns -1, the argc/argv array
23 // contains just the non-flag arguments.
24 int argnext(struct Argstate *);
26 // Returns the next value for the current flag, or 0 if it has no value.
27 // For example, given an argument list ["-fval1", "val2", "val3"],
28 // a call to argnext() will return 'f', after which repeated calls to
29 // argnextvalue will return "val1", "val2", and "val3".
30 // Consumes arguments from the argc/argv array.
31 char *argnextvalue(struct Argstate *);
33 // Returns the current flag's value, or 0 if it has no value.
34 // Behaves like argnextvalue, except that repeated calls to argvalue will
35 // return the same value.
36 char *argvalue(struct Argstate *);
38 // Used by panic() to print the program name.
39 extern const char *argv0;
41 // Example:
43 // #include <inc/lib.h>
45 // void
46 // umain(int argc, char **argv)
47 // {
48 // int i;
49 // struct Argstate args;
51 // argstart(&argc, argv, &args);
52 // while ((i = argnext(&args)) >= 0)
53 // switch (i) {
54 // case 'r':
55 // case 'x':
56 // cprintf("'-%c' flag\n", i);
57 // break;
58 // case 'f':
59 // cprintf("'-f %s' flag\n", argvalue(&args));
60 // break;
61 // default:
62 // cprintf("unknown flag\n");
63 // }
65 // for (i = 1; i < argc; i++)
66 // cprintf("argument '%s'\n", argv[i]);
67 // }
69 // If this program is run with the arguments
70 // ["-rx", "-f", "foo", "--", "-r", "duh"]
71 // it will print out
72 // '-r' flag
73 // '-x' flag
74 // '-f foo' flag
75 // argument '-r'
76 // argument 'duh'
78 struct Argstate {
79 int *argc;
80 const char **argv;
81 const char *curarg;
82 const char *argvalue;
85 #endif