Global Command Line Options
Global Command Line Options

Parse global command line options.


Declare argument parsing variables
Variable declaration
Segment Source
1719:   char *s;
1720:   int i, t, t2;

Parse command line arguments
Code insertion
Segment Source
1778:   for (i = 1; i < argc; ++i)
1779:     {
1780:       if (argv[i][0] == '+')
1781:         {
1782:           if (key)
1783:             insertkey (key);
1784:           key = (struct keyfield *) xmalloc (sizeof (struct keyfield));
1785:           key_init (key);
1786:           s = argv[i] + 1;
1787:           if (! (ISDIGIT (*s) || (*s == '.' && ISDIGIT (s[1]))))
1788:             badfieldspec (argv[i]);
1789:           for (t = 0; ISDIGIT (*s); ++s)
1790:             t = 10 * t + *s - '0';
1791:           t2 = 0;
1792:           if (*s == '.')
1793:             for (++s; ISDIGIT (*s); ++s)
1794:               t2 = 10 * t2 + *s - '0';
1795:           if (t2 || t)
1796:             {
1797:               key->sword = t;
1798:               key->schar = t2;
1799:             }
1800:           else
1801:             key->sword = -1;
1802:           s = set_ordering (s, key, bl_start);
1803:           if (*s)
1804:             badfieldspec (argv[i]);
1805:         }
1806:       else if (argv[i][0] == '-' && argv[i][1])
1807:         {
1808:           s = argv[i] + 1;
1809:           if (ISDIGIT (*s) || (*s == '.' && ISDIGIT (s[1])))
1810:             {
1811:               if (!key)
1812:                 {
1813:                   /* Provoke with `sort -9'.  */
1814:                   error (0, 0, _("when using the old-style +POS and -POS \
1815: key specifiers,\nthe +POS specifier must come first"));
1816:                   usage (SORT_FAILURE);
1817:                 }
1818:               for (t = 0; ISDIGIT (*s); ++s)
1819:                 t = t * 10 + *s - '0';
1820:               t2 = 0;
1821:               if (*s == '.')
1822:                 for (++s; ISDIGIT (*s); ++s)
1823:                   t2 = t2 * 10 + *s - '0';
1824:               key->eword = t;
1825:               key->echar = t2;
1826:               s = set_ordering (s, key, bl_end);
1827:               if (*s)
1828:                 badfieldspec (argv[i]);
1829:               insertkey (key);
1830:               key = NULL;
1831:             }
1832:           else
1833:             while (*s)
1834:               {
1835:                 s = set_ordering (s, &gkey, bl_both);
1836:                 switch (*s)
1837:                   {
1838:                   case '\0':
1839:                     break;
1840:                   case 'c':
1841:                     checkonly = 1;
1842:                     break;
1843:                   case 'k':
1844:                     if (s[1])
1845:                       ++s;
1846:                     else
1847:                       {
1848:                         if (i == argc - 1)
1849:                           error (SORT_FAILURE, 0,
1850:                                  _("option `-k' requires an argument"));
1851:                         else
1852:                           s = argv[++i];
1853:                       }
1854:                     if (key)
1855:                       insertkey (key);
1856:                     key = (struct keyfield *)
1857:                       xmalloc (sizeof (struct keyfield));
1858:                     key_init (key);
1859:                     /* Get POS1. */
1860:                     if (!ISDIGIT (*s))
1861:                       badfieldspec (argv[i]);
1862:                     for (t = 0; ISDIGIT (*s); ++s)
1863:                       t = 10 * t + *s - '0';
1864:                     if (t == 0)
1865:                       {
1866:                         /* Provoke with `sort -k0' */
1867:                         error (0, 0, _("the starting field number argument \
1868: to the `-k' option must be positive"));
1869:                         badfieldspec (argv[i]);
1870:                       }
1871:                     --t;
1872:                     t2 = 0;
1873:                     if (*s == '.')
1874:                       {
1875:                         if (!ISDIGIT (s[1]))
1876:                           {
1877:                             /* Provoke with `sort -k1.' */
1878:                             error (0, 0, _("starting field spec has `.' but \
1879: lacks following character offset"));
1880:                             badfieldspec (argv[i]);
1881:                           }
1882:                         for (++s; ISDIGIT (*s); ++s)
1883:                           t2 = 10 * t2 + *s - '0';
1884:                         if (t2 == 0)
1885:                           {
1886:                             /* Provoke with `sort -k1.0' */
1887:                             error (0, 0, _("starting field character offset \
1888: argument to the `-k' option\nmust be positive"));
1889:                             badfieldspec (argv[i]);
1890:                           }
1891:                         --t2;
1892:                       }
1893:                     if (t2 || t)
1894:                       {
1895:                         key->sword = t;
1896:                         key->schar = t2;
1897:                       }
1898:                     else
1899:                       key->sword = -1;
1900:                     s = set_ordering (s, key, bl_start);
1901:                     if (*s == 0)
1902:                       {
1903:                         key->eword = -1;
1904:                         key->echar = 0;
1905:                       }
1906:                     else if (*s != ',')
1907:                       badfieldspec (argv[i]);
1908:                     else if (*s == ',')
1909:                       {
1910:                         /* Skip over comma.  */
1911:                         ++s;
1912:                         if (*s == 0)
1913:                           {
1914:                             /* Provoke with `sort -k1,' */
1915:                             error (0, 0, _("field specification has `,' but \
1916: lacks following field spec"));
1917:                             badfieldspec (argv[i]);
1918:                           }
1919:                         /* Get POS2. */
1920:                         for (t = 0; ISDIGIT (*s); ++s)
1921:                           t = t * 10 + *s - '0';
1922:                         if (t == 0)
1923:                           {
1924:                             /* Provoke with `sort -k1,0' */
1925:                             error (0, 0, _("ending field number argument \
1926: to the `-k' option must be positive"));
1927:                             badfieldspec (argv[i]);
1928:                           }
1929:                         --t;
1930:                         t2 = 0;
1931:                         if (*s == '.')
1932:                           {
1933:                             if (!ISDIGIT (s[1]))
1934:                               {
1935:                                 /* Provoke with `sort -k1,1.' */
1936:                                 error (0, 0, _("ending field spec has `.' \
1937: but lacks following character offset"));
1938:                                 badfieldspec (argv[i]);
1939:                               }
1940:                             for (++s; ISDIGIT (*s); ++s)
1941:                               t2 = t2 * 10 + *s - '0';
1942:                           }
1943:                         else
1944:                           {
1945:                             /* `-k 2,3' is equivalent to `+1 -3'.  */
1946:                             ++t;
1947:                           }
1948:                         key->eword = t;
1949:                         key->echar = t2;
1950:                         s = set_ordering (s, key, bl_end);
1951:                         if (*s)
1952:                           badfieldspec (argv[i]);
1953:                       }
1954:                     insertkey (key);
1955:                     key = NULL;
1956:                     goto outer;
1957:                   case 'm':
1958:                     mergeonly = 1;
1959:                     break;
1960:                   case 'o':
1961:                     if (s[1])
1962:                       outfile = s + 1;
1963:                     else
1964:                       {
1965:                         if (i == argc - 1)
1966:                           error (SORT_FAILURE, 0,
1967:                                  _("option `-o' requires an argument"));
1968:                         else
1969:                           outfile = argv[++i];
1970:                       }
1971:                     goto outer;
1972:                   case 's':
1973:                     stable = 1;
1974:                     break;
1975:                   case 't':
1976:                     if (s[1])
1977:                       tab = *++s;
1978:                     else if (i < argc - 1)
1979:                       {
1980:                         tab = *argv[++i];
1981:                         goto outer;
1982:                       }
1983:                     else
1984:                       error (SORT_FAILURE, 0,
1985:                              _("option `-t' requires an argument"));
1986:                     break;
1987:                   case 'T':
1988:                     if (s[1])
1989:                       temp_file_prefix = ++s;
1990:                     else
1991:                       {
1992:                         if (i < argc - 1)
1993:                           temp_file_prefix = argv[++i];
1994:                         else
1995:                           error (SORT_FAILURE, 0,
1996:                                  _("option `-T' requires an argument"));
1997:                       }
1998:                     goto outer;
1999:                     /* break; */
2000:                   case 'u':
2001:                     unique = 1;
2002:                     break;
2003:                   case 'z':
2004:                     eolchar = 0;
2005:                     break;
2006:                   case 'y':
2007:                     /* Accept and ignore e.g. -y0 for compatibility with
2008:                        Solaris 2.  */
2009:                     goto outer;
2010:                   default:
2011:                     fprintf (stderr, _("%s: unrecognized option `-%c'\n"),
2012:                              argv[0], *s);
2013:                     usage (SORT_FAILURE);
2014:                   }
2015:                 if (*s)
2016:                   ++s;
2017:               }
2018:         }
2019:       else                      /* Not an option. */
2020:         {
2021:           files[nfiles++] = argv[i];
2022:         }
2023:     outer:;
2024:     }
2025: