1 / 28

Getopt()

Getopt(). The getopt() function parses command lines that correspond to the standard IEEE Std 1003.2-1992 ISO/IEC DIS 9945-2 syntax. IEEE Std 1003.2-1992 ISO/IEC DIS 9945-2, §B.7; X/Open Portability Guide Issue 3: XSI System Interfaces and Headers, Page 203.

Télécharger la présentation

Getopt()

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getopt() • The getopt() function parses command lines that correspond to the • standard IEEE Std 1003.2-1992 ISO/IEC DIS 9945-2 syntax. • IEEE Std 1003.2-1992 ISO/IEC DIS 9945-2, §B.7; • X/Open Portability Guide Issue 3: XSI System Interfaces and Headers, Page 203. • X/Open CAE Specification, System Interfaces and Headers, Issue 4, Page 178 [121].

  2. POSIX • POSIX is the term for a suite of applications program interface standards to provide for the portability of source code applications where operating systems services are required. POSIX is based on the UNIX (tm adminstrated by X/Open) Operating System, and is the basis for the X/Open specifications of The Open Group. • POSIX is intended to be one part of the suite of standards (a "profile") that a user might require of a complete & coherent open system. This concept is developed in IEEE Std 1003.0-1994: Guide to the POSIX Open System Environment.

  3. C Format • #include <unistd.h> • int getopt(int argc, • const char *const argv[], • const char *optstring); • extern char *optarg; • extern int optind, opterr, optopt;

  4. Description • The getopt() function returns the next option letter from argv[] that matches a letter in optstring. • The getopt() function recognizes if options were concatenated on the command line (for example, -a -b can be combined into -ab) and returns 1 character that represents an option, as defined in optstring.

  5. Description cont. • Successive calls to getopt() obtain successive options from the command line. To keep track of the argv[] element to be processed next, the variable optind is used. This is the index to the next element of the argv[] vector to be processed and is initialized to 1 by the system. The getopt() function updates it when it finishes with each element of argv[].

  6. Description cont. • If the option takes an argument, getopt() sets the variable optarg to point to the option argument as follows: • o If the option was the last letter in the string pointed to by an element of argv, optarg contains the next element of argv, which is assumed to be the associated argument; optind is incremented by 2. If the resulting value of optind is not less then argc, this indciates a missing option argument and getopt() returns an error.

  7. Description cont. • o Otherwise, if the option letter is not at the end of an argv[] element, optarg points to the string following the option letter in that element of argv, since the argument value is assumed to follow immediately after the argument letter. The argument value is the rest of the argv[] element, and optind is incremented by 1. • Note that this function is not thread-safe: it is meaningless to call it in a threads context.

  8. Return Value • The function getopt() returns the next option letter specified in the command line. The value -1 is returned when all command line options have been parsed. The same value is returned when argv[optind] is null, or when the string, pointed to by argv[optind], is composed only of either a single minus sign (-) or a double minus sign (- -), or when a command line argument does not begin with a minus sign. If argv[optind] is a double minus sign (- -), getopt() increments optind by 1; otherwise, it does not increment optind.

  9. Return Value cont. • If getopt() encounters an invalid option (one whose character does not appear in optstring) or an option that requires an argument and no argument was found, the getopt() function writes an error message, sets the variable optopt to the unknown option letter that caused the error, and returns a question mark (?). The optind variable is not updated. • Normally, getopt() writes an error message to the standard error stream if it encounters an error. An application can disable the error message by setting opterr to zero. Initially the system sets the variable opterr to 1.

  10. Shells and Signals • trap ‘sequence of commands’ list of signal numbers • Some signal numbers relevant to shells: • 0 shell exit • 1 hangup • 2 interrupt • 3 quit and do core dump • 9 kill (non-maskable) • 15 terminate

  11. Trap Example • … • trap ‘rm -f $new $old; exit 1’ 1 2 15 • while : • … • In this case, the “rm” and “exit” shell commands will be executed whenever the shell containing the “trap” command gets a signal of 1, 2 or 15. This is a nice way to clean up after a shell program.

  12. Saving Processes • Normally, when you invoke a shell command from a shell, the operating system starts a child process and your original shell process waits for the child to terminate. • If you are invoking the last command from your shell, why have the parent process wait for the child process? • exec replaces the process of the current shell with the program that is named by its argument, thus saving a process. • For example, when you login successfully, the last thing the login program does is “exec” a terminal program, so that it turns into your terminal session.

  13. Object Oriented Programming • Shell programming is procedural. Each shell command is executed in order, and special commands must be used to change the flow of control. • C programming is also procedural. In a compiled program, once a statement has been executed, the instruction register will point to the address of the next statement. Again, special keywords are needed to change the flow of control. • Ada and Modula 2 are procedural languages that have added the concept of modular program design. This means that data encapsulation is used to prevent “unauthorized” manipulation of data.

  14. Modular Program Design • package complex_number_package • begin • complex_number_type complex_number; • function add(complex_number, complex_number); • private • … • end • main(input, output) • begin • complex_number Array[100]; • ...

  15. OOP Cont. • Modular program design led to the idea that functions should be encapsulated with data types, and that function declarations should be in the same data structure as the associated data types. • C++ uses the class to encapsulate data types and functions. Along with some other features, this makes it convenient to do Object Oriented Programming in C++. Note: C++ is backwards compatible with C, and thus it is possible (in fact, it is likely) to write procedural programs in C++.

  16. C++ Class • Class class_name • { • public: • class_name(int n = 10); // constructor • ~class_name(){ delete [] p; } // destructor • … • private: • int *p; • … • };

  17. C++ Class Access • Access to member functions and data is determined by the keywords public, private, protected and friend. • public is what is available for use by any other code. • friend functions allow non-member functions access to hidden class members. • private is available only to class member functions and friend functions. • protected is available only to class members and classes immediately derived from this class.

  18. Accessors & Mutators • Public member functions which read private or protected data member values are called accessors. • Public member functions which carefully change private or protected data are called mutators. • The idea is not to completely hide information, but rather to keep access to that information predictable. This makes it easier to find bugs.

  19. Constructor & Destructor • A constructor is a member function whose name is the same as the class name. • Example: class_name::class_name(){…} • A constructor function with no arguments or with arguments that have explicit defaults is a default constructor, and can be used as the element type of an array declaration. • A destructor looks like a constructor preceded by ~. • Constructors and destructors are used to dynamically allocate and delete resources for classes.

  20. Copy Constructor • A constructor of the form: • class_name :: class_name(const class_name& x) • is called a copy constructor, and if not given explicitly it is compiler generated! • Classes that use pointers as part of their implementation should provide explicit default and copy constructors. Otherwise, the compiler generated function may copy a pointer value without creating a new instance of the pointer, which can be a very nasty bug.

  21. Member Functions • An ordinary member function has an explicit and an implicit argument list. • Example: object.mem(i, j, k); • i, j, k are the explicit arguments. • The members of object are the implicit arguments. The member function “mem” can access the other members, both function and data, by use of the this pointer. • However, a function declared static cannot access members with this. • A function declared const can read, but not modify, the other members.

  22. Class Design Rules • Classes should be as simple and as orthogonal as possible. “Orthogonal” is the minimal set that gives complete access to the class. • Classes should be easy for the user to use, rather than easy for the implementor to implement. • Class reuse is better than invention.

  23. Inheritance • class class_name : public base_class_name • { • member declarations • } • The class “class_name” is a subtype of the class “base_class_name”. This means that pointers to objects of type “base_class_name” will also point to objects of the derived type “class_name”.

  24. Multiple Inheritance • class shape {…}; • class tview {…}; • class tshape : public shape, private tview {…}; • In this example, the class shape is an interface and the class tshape is a subtype of this interface. The class tview is an implementation of text view, and the class tshape adapts text view for the shape interface.

  25. It Gets Worse Base0 Base1a Base1b Derived

  26. Multiple Inheritance Problem • In the previous diagram, classes Base1a and Base1b are each derived from class Base0. • The class Derived inherits from each of its parent classes a copy of the ancestor, Base0. • So, if a member of Derived makes reference to a member of Base0, which member does it mean, the one inherited from Base1a or the one inherited from Base1b? • Java takes the easy way out, and says: “Don’t do this!”

  27. More Java Simplifications • No templates - e.g., the Stack class is instantiated as Stack<int> or as Stack<double>. • No operator overloading - e.g., ‘+’ is able to add two matrices to return a third matrix. • No object manipulation by value. Manipulation by value is default in C and C++, and special syntax must be used to do manipulation by reference.

  28. Overriding Methods • When a derived class defines a method using the same name, return type, and arguments of a method in the base class, then the base class method is overridden. This is not overloading! • Say a base class Circle has a method area(), and a derived class Ellipse overrides that method area(). Now, a variable “surface” of type pointer to Circle can also point to Ellipse variables (why?), and so, what does “surface.area()” mean? • Which method to invoke must be determined at run time via dynamic method lookup.

More Related