|
2069 | 2069 | %%%%% |
2070 | 2070 |
|
2071 | 2071 | \begin{slide} |
2072 | | -\sltitle{Zpracování argumentù programu} |
2073 | | -\begin{itemize} |
2074 | | -\item obvyklý zápis v shellu: \verb#program -pøepínaèe argumenty# |
2075 | | -\item pøepínaèe tvaru \texttt{-x} nebo \texttt{-x \emph{hodnota}}, kde |
2076 | | -\texttt{x} je jedno písmeno nebo èíslice, \texttt{\emph{hodnota}} je libovolný |
2077 | | -øetìzec |
2078 | | -\item nìkolik pøepínaèù lze slouèit dohromady: \texttt{ls -lRa} |
2079 | | -\item argument '\texttt{--}' nebo první argument nezaèínající '\texttt{-}' |
2080 | | -ukonèuje pøepínaèe, následující argumenty nejsou pova¾ovány za pøepínaèe, i |
2081 | | -kdy¾ zaèínají znakem '\texttt{-}'. |
2082 | | -\item tento tvar argumentù po¾aduje norma a lze je zpracovávat automaticky |
2083 | | -funkcí \texttt{getopt}. |
| 2072 | +\sltitle{Command arguments processing} |
| 2073 | +\begin{itemize} |
| 2074 | +\item common notation for shell: \verb#program -option arguments# |
| 2075 | +\item options are in the form of \texttt{-x} or \texttt{-x \emph{value}}, |
| 2076 | +where \texttt{x} is alphanumeric, \texttt{\emph{value}} is arbitrary string |
| 2077 | +\item multiple options can be unified: \texttt{ls -lRa} |
| 2078 | +\item '\texttt{--}' or first argument not starting with '\texttt{-}' |
| 2079 | +marks end of options, the following arguments are not considered options even |
| 2080 | +if they start with '\texttt{-}'. |
| 2081 | +\item this form of arguments is required by the standard and can be processed |
| 2082 | +using the \texttt{getopt} function. |
2084 | 2083 | \end{itemize} |
2085 | 2084 | \end{slide} |
2086 | 2085 |
|
2087 | 2086 | \begin{itemize} |
2088 | | -\item argumenty lze samozøejmì zpracovávat vlastní funkcí, ale standardní |
2089 | | -funkce je pohodlnìj¹í. |
2090 | | -\item argumenty se typicky mohou opakovat, ale to má smysl jen v nìkterých |
2091 | | -situacích |
2092 | | -\item poøadí pøepínaèù mù¾e být dùle¾ité a je na aplikaci, aby toto |
2093 | | -specifikovala |
2094 | | -\item UNIX norma definuje pomocí 13 pravidel velmi pøesnì, jak by mìly vypadat |
2095 | | -názvy pøíkazù a formát pøepínaèù. Napøíklad jméno pøíkazu by mìlo být pouze |
2096 | | -malými písmeny, dlouhé 2--9 znakù, z pøenositelné znakové sady. Pøepínaèe bez |
2097 | | -argumentù by mìlo být mo¾né dát do skupiny za jedním znakem '--'. Atd. |
2098 | | -\item pou¾ívat èíslice jako pøepínaèe je zastaralé; je to nìkde v normì |
2099 | | -SUSv3, i kdy¾ já to v ní nena¹el. |
2100 | | -\item pozor na Linux a jeho (ponìkud zvlá¹tní a nestandardní) permutování |
2101 | | -argumentù |
2102 | | -\item pøepínaè \texttt{-W} by mìl být rezervovaný pro vendor options, tj. pro |
2103 | | -nepøenositelná roz¹íøení |
| 2087 | +\item The arguments can of course be processed using custom function however |
| 2088 | +the standard function is sufficient for the overwhelming majority of use cases. |
| 2089 | +\item The options can be repeated however it makes sense only in specific |
| 2090 | +cases. |
| 2091 | +\item The ordering of the options can be important and it depends on the |
| 2092 | +application to specify the ordering. |
| 2093 | +\item The UNIX standard defines 13 rules that very precisely define the naming |
| 2094 | +of commands and the format of options. For example the name of the command |
| 2095 | +should be lower case, 2--9 characters long and using only chracters from |
| 2096 | +portable character set. Arguments without options should be possible to group |
| 2097 | +after '--', etc. |
| 2098 | +\item Using numbers of options is old fashioned; reportedly SUSv3 mentions that. |
| 2099 | +\item Watch out for GNU command line utilities and its strange permutations |
| 2100 | +of options and arguments. |
| 2101 | +\item The \texttt{-W} option should be reserved for vendor options, i.e. for |
| 2102 | +non-portable extensions. |
2104 | 2103 | \end{itemize} |
2105 | 2104 |
|
2106 | 2105 | %%%%% |
|
2109 | 2108 |
|
2110 | 2109 | \begin{slide} |
2111 | 2110 | \setlength{\baselineskip}{0.8\baselineskip} |
2112 | | -\sltitle{Zpracování pøepínaèù: \texttt{getopt()}} |
| 2111 | +\sltitle{Options processing: \texttt{getopt()}} |
2113 | 2112 | \begin{tabbing} |
2114 | 2113 | \texttt{int \funnm{getopt}(}\=\texttt{int \emph{argc}, |
2115 | 2114 | char *const \emph{argv}[],}\\ |
|
2118 | 2117 | \texttt{extern int \emph{optind}, \emph{opterr}, \emph{optopt};} |
2119 | 2118 | \end{tabbing} |
2120 | 2119 | \begin{itemize} |
2121 | | -\item funkce dostane parametry z pøíkazového øádku, pøi ka¾dém |
2122 | | -volání zpracuje a vrátí dal¹í pøepínaè. Pokud má pøepínaè hodnotu, |
2123 | | -vrátí ji v \texttt{optarg}. |
2124 | | -\item kdy¾ jsou vyèerpány v¹echny pøepínaèe, vrátí -1 a v |
2125 | | -\texttt{optind} je èíslo prvního nezpracovaného argumentu. |
2126 | | -\item mo¾né pøepínaèe jsou zadány v \texttt{optstring}, kdy¾ za |
2127 | | -znakem pøepínaèe následuje '\texttt{:}', má pøepínaè povinnou |
2128 | | -hodnotu. |
2129 | | -\item pøi chybì (neznámý pøepínaè, chybí hodnota) vrátí |
2130 | | -'\texttt{?}', ulo¾í znak pøepínaèe do \texttt{optopt} a kdy¾ |
2131 | | -\texttt{opterr} nebylo nastaveno na nulu, vypí¹e chybové hlá¹ení. |
| 2120 | +\item the function receives command line parameters and processes and returns |
| 2121 | +one option. If given option has an argument, it is set in the |
| 2122 | +\texttt{optarg} variable. |
| 2123 | +\item when all options are processed, it returns -1 and the \texttt{optind} |
| 2124 | +variable contains the index of first unprocessed argument of the program. |
| 2125 | +\item possible options are passed in the \texttt{optstring} string. |
| 2126 | +If an options has mandatory argument, its character is followed by '\texttt{:}' |
| 2127 | +in the string. |
| 2128 | +\item on error (unknown option, argument missing) the function returns |
| 2129 | +'\texttt{?}', saves the option character into the \texttt{optopt} variable. |
2132 | 2130 | \end{itemize} |
2133 | 2131 | \end{slide} |
2134 | 2132 |
|
2135 | 2133 | \begin{itemize} |
2136 | | -\item obvykle se nejprve pomocí \texttt{getopt} naètou pøepínaèe a pak se |
2137 | | -vlastními pro\-støed\-ky zpracují ostatní argumenty; èasto jsou to jména |
2138 | | -souborù. |
2139 | | -\item je konvencí, ¾e volby v parametru \texttt{optstring} jsou setøídìné. |
| 2134 | +\item Usually the \texttt{getopt} function is used first to process options |
| 2135 | +and then the rest is processed using custom functions; the remaining arguments |
| 2136 | +are often file names and such. |
| 2137 | +\item By convention the optinons in the \texttt{optstring} variable are sorted. |
2140 | 2138 | \end{itemize} |
2141 | 2139 |
|
2142 | 2140 | %%%%% |
2143 | 2141 |
|
2144 | 2142 | \begin{slide} |
2145 | | -\sltitle{Pøíklad pou¾ití \texttt{getopt()}} |
| 2143 | +\sltitle{Example of using \texttt{getopt()}} |
2146 | 2144 | \setlength{\baselineskip}{0.8\baselineskip} |
2147 | 2145 | \begin{alltt} |
2148 | 2146 | struct \{ |
|
2154 | 2152 | switch(opt) \{ |
2155 | 2153 | case 'a': opts.a = 1; break; |
2156 | 2154 | case 'b': opts.b = 1; break; |
2157 | | - case 'c': \sout{strcpy}(opts.c, \emprg{optarg}); break; |
| 2155 | + case 'c': strncpy(opts.c, \emprg{optarg}, |
| 2156 | + sizeof (opts.c) - 1); |
| 2157 | + opts.c[sizeof (opts.c) - 1] = '\textbackslash0'; break; |
2158 | 2158 | case '?': fprintf(stderr, |
2159 | 2159 | "usage: %s [-ab] [-c Carg] arg1 arg2 ...\bs{}n", |
2160 | 2160 | basename(argv[0])); break; |
|
2164 | 2164 | \end{slide} |
2165 | 2165 |
|
2166 | 2166 | \begin{itemize} |
2167 | | -\item dobrým zvykem je pøi detekování neznámého pøepínaèe nebo ¹patného |
2168 | | -zá\-pi\-su parametrù programu vypsat struènou nápovìdu, pøípadnì s odkazem na |
2169 | | -podrobnìj¹í dokumentaci, a ukonèit program s chybou, tj. s nenulovou |
2170 | | -ná\-vra\-to\-vou hodnotou. |
2171 | | -\item pøíklad také ukazuje nebezpeèné pou¾ití funkce \texttt{strcpy} |
2172 | | -\item z pou¾ití funkce \texttt{getopt} je vidìt, ¾e je stavová. Zpracovat dal¹í |
2173 | | -pole argumentù, pøípadnì zaèít opìt od zaèátku, je mo¾né nastavením externí |
2174 | | -pro\-mìn\-né \texttt{optreset} na 1. |
2175 | | -\item standardní \texttt{getopt} zachová poøadí pøepínaèù pøi zpracování |
2176 | | -\item pøi pou¾ití nedefinovaného pøepínaèe funkce vypí¹e chybu; to lze potlaèit |
2177 | | -na\-sta\-ve\-ním \texttt{opterr} na 0. |
2178 | | -\item \label{GETOPT} pøíklad: shellový skript \example{getopt/getopts.sh} |
2179 | | -pøepsaný do jazyka C pomocí \texttt{getopt} funkce, \example{getopt/getopt.c} |
| 2167 | +\item It is a good custom while detecting unknown switch or incorrect use |
| 2168 | +of options to write simple usage, optionally with a reference to documentation, |
| 2169 | +and exit the program with error, i.e. non zero return value. |
| 2170 | +\item Note how the \texttt{opts.c} string is properly terminated. Utmost care |
| 2171 | +is necessary when processing potentially unbound output, especially when |
| 2172 | +handling options of a program running with elevated privileges. |
| 2173 | +\item It is evident that \texttt{getopt} is stateful function. In order to |
| 2174 | +process next array of arguments or start from scratch, it is necessary to |
| 2175 | +set the \texttt{optreset} variable to 1. |
| 2176 | +\item Standard version of \texttt{getopt} retains the order of arguments when |
| 2177 | +processing. |
| 2178 | +\item When undefined option is used, \texttt{getopt} will print an error; |
| 2179 | +this can be suppressed by setting the \texttt{opterr} variable to 0. |
| 2180 | +\item \label{GETOPT} Example: shell script \example{getopt/getopts.sh} |
| 2181 | +rewritten to C language using the \texttt{getopt} function: |
| 2182 | +\example{getopt/getopt.c} |
2180 | 2183 | \end{itemize} |
2181 | 2184 |
|
2182 | 2185 | %%%%% |
|
0 commit comments