POSIX Field Specification
POSIX Field Specification
Support the POSIX standard field specification syntax [-k<pos>[,<pos>]]
Notes
The core role for POSIX is to define which
characters are used for the next comparision.
The type of sort used (set_ordering() a.k.a.
FieldOpts) is an extension of this capability.
- Insert user message text
-
Text insertion
- Segment Source
-
227: -k POS1[,POS2] same as +POS1 [-POS2], but all positions counted from 1\n\
- Declare parsing variables (t, t2)
-
Variable declaration
- Segment Source
- 1720: int i, t, t2;
- Parse POSIX option (-k)
-
Code insertion
- Segment Source
-
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;