expand.c (221602) | expand.c (221646) |
---|---|
1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1997-2005 5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. --- 24 unchanged lines hidden (view full) --- 33 */ 34 35#ifndef lint 36#if 0 37static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 38#endif 39#endif /* not lint */ 40#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 1997-2005 5 * Herbert Xu <herbert@gondor.apana.org.au>. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Kenneth Almquist. --- 24 unchanged lines hidden (view full) --- 33 */ 34 35#ifndef lint 36#if 0 37static char sccsid[] = "@(#)expand.c 8.5 (Berkeley) 5/15/95"; 38#endif 39#endif /* not lint */ 40#include <sys/cdefs.h> |
41__FBSDID("$FreeBSD: head/bin/sh/expand.c 221602 2011-05-07 14:32:16Z jilles $"); | 41__FBSDID("$FreeBSD: head/bin/sh/expand.c 221646 2011-05-08 11:32:20Z jilles $"); |
42 43#include <sys/types.h> 44#include <sys/time.h> 45#include <sys/stat.h> 46#include <dirent.h> 47#include <errno.h> 48#include <inttypes.h> 49#include <limits.h> 50#include <pwd.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> | 42 43#include <sys/types.h> 44#include <sys/time.h> 45#include <sys/stat.h> 46#include <dirent.h> 47#include <errno.h> 48#include <inttypes.h> 49#include <limits.h> 50#include <pwd.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> |
55#include <wchar.h> |
|
55 56/* 57 * Routines to expand arguments to commands. We have to deal with 58 * backquotes, shell variables, and file metacharacters. 59 */ 60 61#include "shell.h" 62#include "main.h" --- 43 unchanged lines hidden (view full) --- 106static void removerecordregions(int); 107static void ifsbreakup(char *, struct arglist *); 108static void expandmeta(struct strlist *, int); 109static void expmeta(char *, char *); 110static void addfname(char *); 111static struct strlist *expsort(struct strlist *); 112static struct strlist *msort(struct strlist *, int); 113static char *cvtnum(int, char *); | 56 57/* 58 * Routines to expand arguments to commands. We have to deal with 59 * backquotes, shell variables, and file metacharacters. 60 */ 61 62#include "shell.h" 63#include "main.h" --- 43 unchanged lines hidden (view full) --- 107static void removerecordregions(int); 108static void ifsbreakup(char *, struct arglist *); 109static void expandmeta(struct strlist *, int); 110static void expmeta(char *, char *); 111static void addfname(char *); 112static struct strlist *expsort(struct strlist *); 113static struct strlist *msort(struct strlist *, int); 114static char *cvtnum(int, char *); |
114static int collate_range_cmp(int, int); | 115static int collate_range_cmp(wchar_t, wchar_t); |
115 116static int | 116 117static int |
117collate_range_cmp(int c1, int c2) | 118collate_range_cmp(wchar_t c1, wchar_t c2) |
118{ | 119{ |
119 static char s1[2], s2[2]; | 120 static wchar_t s1[2], s2[2]; |
120 121 s1[0] = c1; 122 s2[0] = c2; | 121 122 s1[0] = c1; 123 s2[0] = c2; |
123 return (strcoll(s1, s2)); | 124 return (wcscoll(s1, s2)); |
124} 125 126/* 127 * Expand shell variables and backquotes inside a here document. 128 * union node *arg the document 129 * int fd; where to write the expanded version 130 */ 131 --- 1240 unchanged lines hidden (view full) --- 1372 } 1373 } 1374 } 1375 return list; 1376} 1377 1378 1379 | 125} 126 127/* 128 * Expand shell variables and backquotes inside a here document. 129 * union node *arg the document 130 * int fd; where to write the expanded version 131 */ 132 --- 1240 unchanged lines hidden (view full) --- 1373 } 1374 } 1375 } 1376 return list; 1377} 1378 1379 1380 |
1381static wchar_t 1382get_wc(const char **p) 1383{ 1384 wchar_t c; 1385 int chrlen; 1386 1387 chrlen = mbtowc(&c, *p, 4); 1388 if (chrlen == 0) 1389 return 0; 1390 else if (chrlen == -1) 1391 c = 0; 1392 else 1393 *p += chrlen; 1394 return c; 1395} 1396 1397 |
|
1380/* 1381 * Returns true if the pattern matches the string. 1382 */ 1383 1384int 1385patmatch(const char *pattern, const char *string, int squoted) 1386{ 1387 const char *p, *q; 1388 char c; | 1398/* 1399 * Returns true if the pattern matches the string. 1400 */ 1401 1402int 1403patmatch(const char *pattern, const char *string, int squoted) 1404{ 1405 const char *p, *q; 1406 char c; |
1407 wchar_t wc, wc2; |
|
1389 1390 p = pattern; 1391 q = string; 1392 for (;;) { 1393 switch (c = *p++) { 1394 case '\0': 1395 goto breakloop; 1396 case CTLESC: 1397 if (squoted && *q == CTLESC) 1398 q++; 1399 if (*q++ != *p++) 1400 return 0; 1401 break; 1402 case CTLQUOTEMARK: 1403 continue; 1404 case '?': 1405 if (squoted && *q == CTLESC) 1406 q++; | 1408 1409 p = pattern; 1410 q = string; 1411 for (;;) { 1412 switch (c = *p++) { 1413 case '\0': 1414 goto breakloop; 1415 case CTLESC: 1416 if (squoted && *q == CTLESC) 1417 q++; 1418 if (*q++ != *p++) 1419 return 0; 1420 break; 1421 case CTLQUOTEMARK: 1422 continue; 1423 case '?': 1424 if (squoted && *q == CTLESC) 1425 q++; |
1407 if (*q++ == '\0') | 1426 if (localeisutf8) 1427 wc = get_wc(&q); 1428 else 1429 wc = *q++; 1430 if (wc == '\0') |
1408 return 0; 1409 break; 1410 case '*': 1411 c = *p; 1412 while (c == CTLQUOTEMARK || c == '*') 1413 c = *++p; 1414 if (c != CTLESC && c != CTLQUOTEMARK && 1415 c != '?' && c != '*' && c != '[') { --- 13 unchanged lines hidden (view full) --- 1429 return 1; 1430 if (squoted && *q == CTLESC) 1431 q++; 1432 } while (*q++ != '\0'); 1433 return 0; 1434 case '[': { 1435 const char *endp; 1436 int invert, found; | 1431 return 0; 1432 break; 1433 case '*': 1434 c = *p; 1435 while (c == CTLQUOTEMARK || c == '*') 1436 c = *++p; 1437 if (c != CTLESC && c != CTLQUOTEMARK && 1438 c != '?' && c != '*' && c != '[') { --- 13 unchanged lines hidden (view full) --- 1452 return 1; 1453 if (squoted && *q == CTLESC) 1454 q++; 1455 } while (*q++ != '\0'); 1456 return 0; 1457 case '[': { 1458 const char *endp; 1459 int invert, found; |
1437 char chr; | 1460 wchar_t chr; |
1438 1439 endp = p; 1440 if (*endp == '!' || *endp == '^') 1441 endp++; 1442 for (;;) { 1443 while (*endp == CTLQUOTEMARK) 1444 endp++; 1445 if (*endp == '\0') --- 4 unchanged lines hidden (view full) --- 1450 break; 1451 } 1452 invert = 0; 1453 if (*p == '!' || *p == '^') { 1454 invert++; 1455 p++; 1456 } 1457 found = 0; | 1461 1462 endp = p; 1463 if (*endp == '!' || *endp == '^') 1464 endp++; 1465 for (;;) { 1466 while (*endp == CTLQUOTEMARK) 1467 endp++; 1468 if (*endp == '\0') --- 4 unchanged lines hidden (view full) --- 1473 break; 1474 } 1475 invert = 0; 1476 if (*p == '!' || *p == '^') { 1477 invert++; 1478 p++; 1479 } 1480 found = 0; |
1458 chr = *q++; 1459 if (squoted && chr == CTLESC) | 1481 if (squoted && *q == CTLESC) 1482 q++; 1483 if (localeisutf8) 1484 chr = get_wc(&q); 1485 else |
1460 chr = *q++; 1461 if (chr == '\0') 1462 return 0; 1463 c = *p++; 1464 do { 1465 if (c == CTLQUOTEMARK) 1466 continue; 1467 if (c == CTLESC) 1468 c = *p++; | 1486 chr = *q++; 1487 if (chr == '\0') 1488 return 0; 1489 c = *p++; 1490 do { 1491 if (c == CTLQUOTEMARK) 1492 continue; 1493 if (c == CTLESC) 1494 c = *p++; |
1495 if (localeisutf8 && c & 0x80) { 1496 p--; 1497 wc = get_wc(&p); 1498 if (wc == 0) /* bad utf-8 */ 1499 return 0; 1500 } else 1501 wc = c; |
|
1469 if (*p == '-' && p[1] != ']') { 1470 p++; 1471 while (*p == CTLQUOTEMARK) 1472 p++; 1473 if (*p == CTLESC) 1474 p++; | 1502 if (*p == '-' && p[1] != ']') { 1503 p++; 1504 while (*p == CTLQUOTEMARK) 1505 p++; 1506 if (*p == CTLESC) 1507 p++; |
1475 if ( collate_range_cmp(chr, c) >= 0 1476 && collate_range_cmp(chr, *p) <= 0 | 1508 if (localeisutf8) { 1509 wc2 = get_wc(&p); 1510 if (wc2 == 0) /* bad utf-8 */ 1511 return 0; 1512 } else 1513 wc2 = *p++; 1514 if ( collate_range_cmp(chr, wc) >= 0 1515 && collate_range_cmp(chr, wc2) <= 0 |
1477 ) 1478 found = 1; | 1516 ) 1517 found = 1; |
1479 p++; | |
1480 } else { | 1518 } else { |
1481 if (chr == c) | 1519 if (chr == wc) |
1482 found = 1; 1483 } 1484 } while ((c = *p++) != ']'); 1485 if (found == invert) 1486 return 0; 1487 break; 1488 } 1489dft: default: --- 181 unchanged lines hidden --- | 1520 found = 1; 1521 } 1522 } while ((c = *p++) != ']'); 1523 if (found == invert) 1524 return 0; 1525 break; 1526 } 1527dft: default: --- 181 unchanged lines hidden --- |