Commit | Line | Data |
---|---|---|
81cea5ea KB |
1 | /*- |
2 | * Copyright (c) 1991 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Kenneth Almquist. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
9 | */ | |
10 | ||
11 | #ifndef lint | |
ddba57cd | 12 | static char sccsid[] = "@(#)miscbltin.c 5.2 (Berkeley) %G%"; |
81cea5ea KB |
13 | #endif /* not lint */ |
14 | ||
15 | /* | |
16 | * Miscelaneous builtins. | |
17 | */ | |
18 | ||
19 | #include "shell.h" | |
20 | #include "options.h" | |
21 | #include "var.h" | |
22 | #include "output.h" | |
23 | #include "memalloc.h" | |
24 | #include "error.h" | |
25 | #include "mystring.h" | |
26 | ||
27 | #undef eflag | |
28 | ||
29 | extern char **argptr; /* argument list for builtin command */ | |
30 | ||
31 | ||
32 | /* | |
33 | * The read builtin. The -e option causes backslashes to escape the | |
34 | * following character. | |
35 | * | |
36 | * This uses unbuffered input, which may be avoidable in some cases. | |
37 | */ | |
38 | ||
39 | readcmd(argc, argv) char **argv; { | |
40 | char **ap; | |
41 | int backslash; | |
42 | char c; | |
43 | int eflag; | |
44 | char *prompt; | |
45 | char *ifs; | |
46 | char *p; | |
47 | int startword; | |
48 | int status; | |
49 | int i; | |
50 | ||
51 | eflag = 0; | |
52 | prompt = NULL; | |
ddba57cd | 53 | while ((i = nextopt("ep:")) != '\0') { |
81cea5ea KB |
54 | if (i == 'p') |
55 | prompt = optarg; | |
56 | else | |
57 | eflag = 1; | |
58 | } | |
59 | if (prompt && isatty(0)) { | |
60 | out2str(prompt); | |
61 | flushall(); | |
62 | } | |
63 | if ((ap = argptr) == NULL) | |
64 | error("arg count"); | |
65 | if ((ifs = bltinlookup("IFS", 1)) == NULL) | |
66 | ifs = nullstr; | |
67 | status = 0; | |
68 | startword = 1; | |
69 | backslash = 0; | |
70 | STARTSTACKSTR(p); | |
71 | for (;;) { | |
72 | if (read(0, &c, 1) != 1) { | |
73 | status = 1; | |
74 | break; | |
75 | } | |
76 | if (c == '\0') | |
77 | continue; | |
78 | if (backslash) { | |
79 | backslash = 0; | |
80 | if (c != '\n') | |
81 | STPUTC(c, p); | |
82 | continue; | |
83 | } | |
84 | if (eflag && c == '\\') { | |
85 | backslash++; | |
86 | continue; | |
87 | } | |
88 | if (c == '\n') | |
89 | break; | |
90 | if (startword && *ifs == ' ' && strchr(ifs, c)) { | |
91 | continue; | |
92 | } | |
93 | startword = 0; | |
94 | if (backslash && c == '\\') { | |
95 | if (read(0, &c, 1) != 1) { | |
96 | status = 1; | |
97 | break; | |
98 | } | |
99 | STPUTC(c, p); | |
100 | } else if (ap[1] != NULL && strchr(ifs, c) != NULL) { | |
101 | STACKSTRNUL(p); | |
102 | setvar(*ap, stackblock(), 0); | |
103 | ap++; | |
104 | startword = 1; | |
105 | STARTSTACKSTR(p); | |
106 | } else { | |
107 | STPUTC(c, p); | |
108 | } | |
109 | } | |
110 | STACKSTRNUL(p); | |
111 | setvar(*ap, stackblock(), 0); | |
112 | while (*++ap != NULL) | |
113 | setvar(*ap, nullstr, 0); | |
114 | return status; | |
115 | } | |
116 | ||
117 | ||
118 | ||
119 | umaskcmd(argc, argv) char **argv; { | |
120 | int mask; | |
121 | char *p; | |
122 | int i; | |
123 | ||
124 | if ((p = argv[1]) == NULL) { | |
125 | INTOFF; | |
126 | mask = umask(0); | |
127 | umask(mask); | |
128 | INTON; | |
129 | out1fmt("%.4o\n", mask); /* %#o might be better */ | |
130 | } else { | |
131 | mask = 0; | |
132 | do { | |
133 | if ((unsigned)(i = *p - '0') >= 8) | |
134 | error("Illegal number: %s", argv[1]); | |
135 | mask = (mask << 3) + i; | |
136 | } while (*++p != '\0'); | |
137 | umask(mask); | |
138 | } | |
139 | return 0; | |
140 | } |