-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuiltins.cpp
1241 lines (963 loc) · 26.9 KB
/
builtins.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "mpw-shell.h"
#include "fdset.h"
#include "value.h"
#include "environment.h"
#include "error.h"
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdarg>
#include <unistd.h>
#include <strings.h>
#include "cxx/string_splitter.h"
#include "cxx/filesystem.h"
#include "cxx/mapped_file.h"
//MAXPATHLEN
#include <sys/param.h>
#include <fcntl.h>
#include "version.h"
#include "config.h"
namespace ToolBox {
std::string MacToUnix(const std::string path);
std::string UnixToMac(const std::string path);
}
namespace fs = filesystem;
namespace {
std::string &lowercase(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c){ return std::tolower(c); });
return s;
}
// doesn't handle flag arguments but builtins don't have arguments.
template<class FX>
std::vector<std::string> getopt(const std::vector<std::string> &argv, FX fx) {
std::vector<std::string> out;
out.reserve(argv.size());
std::copy_if(argv.begin()+1, argv.end(), std::back_inserter(out), [&fx](const std::string &s){
if (s.empty()) return false; // ?
if (s.front() == '-') {
std::for_each(s.begin() + 1, s.end(), fx);
return false;
}
return true;
});
return out;
}
template <class T>
class offset_range {
public:
typedef typename T::iterator iterator;
typedef typename T::const_iterator const_iterator;
offset_range(T &t, size_t offset) : _t(t), _offset(offset)
{}
offset_range(const offset_range &) = default;
offset_range(offset_range &&) = default;
offset_range() = delete;
auto begin() { return std::next(_t.begin(), _offset); }
auto begin() const { return std::next(_t.begin(), _offset); }
auto cbegin() { return std::next(_t.begin(), _offset); }
auto end() { return _t.end(); }
auto end() const { return _t.end(); }
auto cend() const { return _t.cend(); }
private:
T &_t;
size_t _offset;
};
template<class T>
offset_range<T> make_offset_range(T &t, size_t offset) {
return offset_range<T>(t, offset);
}
}
#undef stdin
#undef stdout
#undef stderr
#define stdin fds[0]
#define stdout fds[1]
#define stderr fds[2]
#undef fprintf
#undef fputs
#undef fputc
#define fprintf DO_NOT_USE_FPRINTF
#define fputs DO_NOT_USE_FPUTS
#define fputc DO_NOT_USE_FPUTC
inline int fdputs(const char *data, int fd) {
auto rv = write(fd, data, strlen(data));
return rv < 0 ? EOF : rv;
}
inline int fdputs(const std::string &s, int fd) {
auto rv = write(fd, s.data(), s.size());
return rv < 0 ? EOF : rv;
}
inline int fdputc(int c, int fd) {
unsigned char tmp = c;
auto rv = write(fd, &tmp, 1);
return rv < 0 ? EOF : c;
}
#ifdef HAVE_DPRINTF
#define fdprintf dprintf
#else
inline int fdprintf(int fd, const char *format, ...) {
char *cp = nullptr;
va_list ap;
va_start(ap, format);
int len = vasprintf(&cp, format, ap);
va_end(ap);
fdputs(cp, fd);
free(cp);
return len;
}
#endif
std::vector<std::string> load_argv(Environment &env) {
std::vector<std::string> rv;
int n = env.pound();
if ( n <= 0 ) return rv;
n = std::min(n, (int)255);
rv.reserve(n);
for (int i = 1; i <= n; ++i) {
rv.push_back(env.get(std::to_string(i)));
}
return rv;
}
int builtin_shift(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
int n = 1;
if (tokens.size() > 3) {
fdputs("### Shift - Too many parameters were specified.\n", stderr);
fdputs("# Usage - Shift [number]\n", stderr);
return 1;
}
if (tokens.size() == 2) {
value v(tokens[1]);
if (v.is_number() && v.number >= 0) n = v.number;
else {
fdputs("### Shift - The parameter must be a positive number.\n", stderr);
fdputs("# Usage - Shift [number]\n", stderr);
return 1;
}
}
if (n == 0) return 0;
env.shift(n);
#if 0
auto argv = load_argv(env);
if (argv.empty()) return 0;
std::move(argv.begin() + n , argv.end(), argv.begin());
do {
env.unset(std::to_string(argv.size()));
argv.pop_back();
} while (--n);
env.set_argv(argv);
#endif
return 0;
}
int builtin_unset(Environment &env, const std::vector<std::string> &tokens, const fdmask &) {
for (const auto &s : make_offset_range(tokens, 1)) {
env.unset(s);
}
// unset [no arg] removes ALL variables
if (tokens.size() == 1) {
env.unset();
}
return 0;
}
int builtin_set(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// set var name -- set
// set var -- just print the value
// 3.5 supports -e to also export it.
//io_helper io(fds);
if (tokens.size() == 1) {
for (const auto &kv : env) {
std::string name = quote(kv.first);
std::string value = quote(kv.second);
fdprintf(stdout, "Set %s%s %s\n",
bool(kv.second) ? "-e " : "",
name.c_str(), value.c_str());
}
return 0;
}
if (tokens.size() == 2) {
std::string name = tokens[1];
auto iter = env.find(name);
if (iter == env.end()) {
fdprintf(stderr, "### Set - No variable definition exists for %s.\n", name.c_str());
return 2;
}
name = quote(name);
std::string value = quote(iter->second);
fdprintf(stdout, "Set %s%s %s\n",
bool(iter->second) ? "-e " : "",
name.c_str(), value.c_str());
return 0;
}
bool exported = false;
if (tokens.size() == 4 && tokens[1] == "-e") {
exported = true;
}
if (tokens.size() > 3 && !exported) {
fdputs("### Set - Too many parameters were specified.\n", stderr);
fdputs("# Usage - set [name [value]]\n", stderr);
return 1;
}
std::string name = tokens[1+exported];
std::string value = tokens[2+exported];
env.set(name, value, exported);
return 0;
}
static int export_common(Environment &env, bool export_or_unexport, const std::vector<std::string> &tokens, const fdmask &fds) {
const char *name = export_or_unexport ? "Export" : "Unexport";
bool _r = false;
bool _s = false;
bool error = false;
auto argv = getopt(tokens, [&](char c){
switch(tolower(c)) {
case 'r': _r = true; break;
case 's': _s = true; break;
default:
fdprintf(stderr, "### %s - \"-%c\" is not an option.\n", name, c);
error = true;
break;
}
});
if (error) {
fdprintf(stderr, "# Usage - %s [-r | -s | name...]\n", name);
return 1;
}
if (argv.empty()) {
if (_r && _s) goto conflict;
// list of exported vars.
// -r will generate unexport commands for exported variables.
// -s will only print the names.
name = export_or_unexport ? "Export " : "Unexport ";
for (const auto &kv : env) {
const std::string& vname = kv.first;
if (kv.second == export_or_unexport)
fdprintf(stdout, "%s%s\n", _s ? "" : name, quote(vname).c_str());
}
return 0;
}
else {
// mark as exported.
if (_r || _s) goto conflict;
for (std::string s : argv) {
auto iter = env.find(s);
if (iter != env.end()) iter->second = export_or_unexport;
}
return 0;
}
conflict:
fdprintf(stderr, "### %s - Conflicting options or parameters were specified.\n", name);
fdprintf(stderr, "# Usage - %s [-r | -s | name...]\n", name);
return 1;
}
int builtin_export(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
return export_common(env, true, tokens, fds);
}
int builtin_unexport(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
return export_common(env, false, tokens, fds);
}
int builtin_alias(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// alias -> lists all aliases
// alias name -> list single alias
// alias name parms... -> add a new alias.
if (tokens.size() == 1) {
for (const auto &p : env.aliases()) {
fdprintf(stdout, "Alias %s %s\n", quote(p.first).c_str(), quote(p.second).c_str());
}
return 0;
}
std::string name = tokens[1];
if (tokens.size() == 2) {
const auto as = env.find_alias(name);
if (as.empty()) {
fdprintf(stderr, "### Alias - No alias exists for %s\n", quote(name).c_str());
return 1;
}
fdprintf(stdout, "Alias %s %s\n", quote(name).c_str(), quote(as).c_str());
return 0;
}
std::string as;
for (const auto &s : make_offset_range(tokens, 2)) {
as += s;
as.push_back(' ');
}
as.pop_back();
// add/remove it to the alias table...
if (as.empty()) {
env.remove_alias(name);
}
else {
env.add_alias(std::move(name), std::move(as));
}
return 0;
}
int builtin_unalias(Environment &env, const std::vector<std::string> &tokens, const fdmask &) {
// unalias -> remove all aliases.
// unalias name -> remove single alias.
if (tokens.size() == 1) {
env.remove_alias();
return 0;
}
for (const auto &x : make_offset_range(tokens, 1)) {
env.remove_alias(x);
}
return 0;
}
int builtin_echo(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
//io_helper io(fds);
bool space = false;
bool n = false;
for (const auto &s : make_offset_range(tokens, 1)) {
if (s == "-n" || s == "-N") {
n = true;
continue;
}
if (space) {
fdputs(" ", stdout);
}
fdputs(s.c_str(), stdout);
space = true;
}
if (!n) fdputs("\n", stdout);
return 0;
}
int builtin_quote(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// todo...
//io_helper io(fds);
bool space = false;
bool n = false;
for (const auto &s : make_offset_range(tokens, 1)) {
if (s == "-n" || s == "-N") {
n = true;
continue;
}
if (space) {
fdputs(" ", stdout);
}
fdputs(quote(s).c_str(), stdout);
space = true;
}
if (!n) fdputs("\n", stdout);
return 0;
}
int builtin_parameters(Environment &env, const std::vector<std::string> &argv, const fdmask &fds) {
//io_helper io(fds);
int i = 0;
for (const auto &s : argv) {
fdprintf(stdout, "{%d} %s\n", i++, s.c_str());
}
return 0;
}
int builtin_directory(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// directory [-q]
// directory path
// for relative names, uses {DirectoryPath} (if set) rather than .
// set DirectoryPath ":,{MPW},{MPW}Projects:"
/*
* Parameters:
* ----------
* directory
* Sets the default directory to directory. If you specify directory
* as a leafname (that is, the final portion of a full pathname), the
* MPW Shell searches for the directory in the current directory path
* (for example, searching "{MPW}Examples:" for CExamples). However, if
* the MPW Shell fails to find the directory in the current directory
* path, it searches the directories listed in the {DirectoryPath} MPW
* Shell variable, which contains a list of directories to be searched
* in order of precedence. The last example illustrates how to do this.
*
* Options:
* -------
* -q
* Inhibits quoting the directory pathname written to standard
* output. This option applies only if you omit the directory
* parameter Normally the MPW Shell quotes the current default
* directory name if it contains spaces or other special characters
*
* Status:
* ------
* Directory can return the following status codes:
*
* 0 no errors
* 1 directory not found; command aborted; or parameter error
*
*/
//io_helper io(fds);
bool q = false;
bool error = false;
std::vector<std::string> argv = getopt(tokens, [&](char c){
switch(tolower(c))
{
case 'q': q = true; break;
default:
fdprintf(stderr, "### Directory - \"-%c\" is not an option.\n", c);
error = true;
break;
}
});
if (error) {
fdputs("# Usage - Directory [-q | directory]\n", stderr);
return 1;
}
if (argv.size() > 1) {
fdputs("### Directory - Too many parameters were specified.\n", stderr);
fdputs("# Usage - Directory [-q | directory]\n", stderr);
return 1;
}
if (argv.size() == 1) {
//cd
if (q) {
fdputs("### Directory - Conflicting options or parameters were specified.\n", stderr);
return 1;
}
// todo -- if relative path does not exist, check {DirectoryPath}
fs::path path = ToolBox::MacToUnix(argv.front());
std::error_code ec;
current_path(path, ec);
if (ec) {
fdputs("### Directory - Unable to set current directory.\n", stderr);
fdprintf(stderr, "# %s\n", ec.message().c_str());
return 1;
}
}
else {
// pwd
std::error_code ec;
fs::path path = fs::current_path(ec);
if (ec) {
fdputs("### Directory - Unable to get current directory.\n", stderr);
fdprintf(stderr, "# %s\n", ec.message().c_str());
return 1;
}
// todo -- pathname translation?
std::string s = path;
if (!q) s = quote(std::move(s));
fdprintf(stdout, "%s\n", s.c_str());
}
return 0;
}
int builtin_exists(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
bool _a = false; // print if alias/symlink
bool _d = false; // print if directory
bool _f = false; // print if normal file
bool _n = false; // don't follow alias
bool _w = false; // print if normal file + writable
bool _q = false; // don't quote names
bool error = false;
std::vector<std::string> argv = getopt(tokens, [&](char c){
switch(tolower(c))
{
case 'a': _a = true; break;
case 'd': _d = true; break;
case 'f': _f = true; break;
case 'n': _n = true; break;
case 'q': _q = true; break;
case 'w': _w = true; break;
default:
fdprintf(stderr, "### Exists - \"-%c\" is not an option.\n", c);
error = true;
break;
}
});
if (_w) _f = false;
if (_a + _d + _f + _w > 1) {
fdputs("### Exists - Conflicting options were specified.\n", stderr);
error = true;
}
if (argv.size() < 1) {
fdputs("### Exists - Not enough parameters were specified.\n", stderr);
error = true;
}
if (error) {
fdputs("# Usage - Exists [-a | -d | -f | -w] [-n] [-q] name...\n", stderr);
return 1;
}
for (auto &s : argv) {
std::string path = ToolBox::MacToUnix(s);
std::error_code ec;
fs::file_status st = _n ? fs::symlink_status(path, ec) : fs::status(path, ec);
if (ec) continue;
if (_d && !fs::is_directory(st)) continue;
if (_a && !fs::is_symlink(st)) continue;
if (_f && !fs::is_regular_file(st)) continue;
if (_w && !fs::is_regular_file(st)) continue;
if (_w && (access(path.c_str(), W_OK | F_OK) < 0)) continue;
if (!_q) s = quote(std::move(s));
fdprintf(stdout, "%s\n", s.c_str());
}
return 0;
}
static bool is_assignment(int type) {
switch(type)
{
case '=':
case '+=':
case '-=':
return true;
default:
return false;
}
}
int builtin_evaluate(Environment &env, std::vector<token> &&tokens, const fdmask &fds) {
// evaluate expression
// evaluate variable = expression
// evaluate variable += expression
// evaluate variable -= expression
// flags -- -h -o -b -- print in hex, octal, or binary
// convert the arguments to a stack.
int output = 'd';
//io_helper io(fds);
std::reverse(tokens.begin(), tokens.end());
// remove 'Evaluate'
tokens.pop_back();
// check for -h -x -o
if (tokens.size() >= 2 && tokens.back().type == '-') {
const token &t = tokens[tokens.size() - 2];
if (t.type == token::text && t.string.length() == 1) {
int flag = tolower(t.string[0]);
switch(flag) {
case 'o':
case 'h':
case 'b':
output = flag;
tokens.pop_back();
tokens.pop_back();
}
}
}
if (tokens.size() >= 2 && tokens.back().type == token::text)
{
int type = tokens[tokens.size() -2].type;
if (is_assignment(type)) {
std::string name = tokens.back().string;
tokens.pop_back();
tokens.pop_back();
int32_t i = evaluate_expression(env, "Evaluate", std::move(tokens));
switch(type) {
case '=':
env.set(name, i);
break;
case '+=':
case '-=':
{
value old;
auto iter = env.find(name);
if (iter != env.end()) old = (const std::string &)iter->second;
switch(type) {
case '+=':
i = old.to_number() + i;
break;
case '-=':
i = old.to_number() - i;
break;
}
env.set(name, i);
}
break;
}
return 0;
}
}
int32_t i = evaluate_expression(env, "Evaluate", std::move(tokens));
if (output == 'h') {
fdprintf(stdout, "0x%08x\n", i);
return 0;
}
if (output == 'b') {
std::string tmp("0b");
for (int j = 0; j < 32; ++j) {
tmp.push_back(i & 0x80000000 ? '1' : '0');
i <<= 1;
}
tmp.push_back('\n');
fdputs(tmp.c_str(), stdout);
return 0;
}
if (output == 'o') {
// octal.
fdprintf(stdout, "0%o\n", i);
return 0;
}
fdprintf(stdout, "%d\n", i);
return 0;
}
int builtin_which(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// which [-a] [-p] [command]
bool _a = false;
bool _p = false;
bool error = false;
std::vector<std::string> argv = getopt(tokens, [&](char c){
switch(tolower(c))
{
case 'a': _a = true; break;
case 'p': _p = true; break;
default:
fdprintf(stderr, "### Which - \"-%c\" is not an option.\n", c);
error = true;
break;
}
});
if (argv.size() > 1) {
fdprintf(stderr, "### Which - Too many parameters were specified.\n");
error = true;
}
if (error) {
fdprintf(stderr, "# Usage - Which [-a] [-p] [name]\n");
return 1;
}
std::string s = env.get("commands");
string_splitter ss(s, ',');
if (argv.empty()) {
// just print the paths.
for (; ss; ++ss) {
fdprintf(stdout, "%s\n", ss->c_str());
}
return 0;
}
std::string target = argv[0];
bool found = false;
// if absolute or relative path, check that.
if (target.find_first_of("/:") != target.npos) {
std::error_code ec;
fs::path p(ToolBox::MacToUnix(target));
if (fs::exists(p, ec)) {
fdprintf(stdout, "%s\n", quote(p).c_str());
return 0;
}
else {
fdprintf(stderr, "### Which - File \"%s\" not found.\n", target.c_str());
return 2;
}
}
for(; ss; ++ss) {
if (_p) fdprintf(stderr, "checking %s\n", ss->c_str());
std::error_code ec;
fs::path p(ToolBox::MacToUnix(ss->c_str()));
p /= target;
if (fs::exists(p, ec)) {
found = true;
fdprintf(stdout, "%s\n", quote(p).c_str());
if (!_a) break;
}
}
// check builtins...
if (!found || _a) {
static const char *builtins[] = {
"aboutbox",
"alias",
"catenate",
"directory",
"echo",
"execute",
"exists",
"export",
"help",
"false", // not in MPW
"parameters",
"quote",
"set",
"shift",
"true", // not in MPW
"unalias",
"unexport",
"unset",
"version",
"which",
};
lowercase(target);
auto iter = std::find(std::begin(builtins), std::end(builtins), target);
if (iter != std::end(builtins)) {
fdprintf(stdout, "%s\n", *iter);
found = true;
}
}
if (found) return 0;
// also check built-ins?
fdprintf(stderr, "### Which - Command \"%s\" was not found.\n", target.c_str());
return 2; // not found.
}
int cat_helper(int in, int out) {
static uint8_t buffer[4096];
for(;;) {
ssize_t rcount = read(in, buffer, sizeof(buffer));
if (rcount < 0) {
if (errno == EINTR) continue;
return 2;
}
if (rcount == 0) break;
for (;;) {
ssize_t wcount = write(out, buffer, rcount);
if (wcount < 0) {
if (errno == EINTR) continue;
return 2;
}
if (wcount != rcount) return 2;
break;
}
}
return 0;
}
int builtin_catenate(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
if (tokens.size() == 1) {
int rv = cat_helper(stdin, stdout);
if (rv) fdputs("### Catenate - I/O Error\n", stderr);
return rv;
}
for (const auto &s : make_offset_range(tokens, 1)) {
std::string path = ToolBox::MacToUnix(s);
int fd = open(path.c_str(), O_RDONLY);
if (fd < 0) {
fdprintf(stderr, "### Catenate - Unable to open \"%s\".\n", path.c_str());
return 1;
}
int rv = cat_helper(fd, stdout);
close(fd);
if (rv) {
fdputs("### Catenate - I/O Error\n", stderr);
return rv;
}
}
return 0;
}
int builtin_version(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
bool _v = false;
bool error = false;
auto argv = getopt(tokens, [&](char c){
switch(tolower(c))
{
case 'v': _v = true; break;
default:
fdprintf(stderr, "### Version - \"-%c\" is not an option.\n", c);
error = true;
break;
}
});
if (argv.size() != 0) {
fdprintf(stderr, "### Version - Too many parameters were specified.\n");
error = true;
}
if (error) {
fdprintf(stderr, "# Usage - Version [-v]\n");
return 1;
}
//fdputs("MPW Shell 3.5, Copyright Apple Computer, Inc. 1985-99. All rights reserved.\n", stdout);
fdputs("MPW Shell " VERSION ", Copyright Kelvin W Sherlock 2016. All rights reserved.\n", stdout);
fdputs("based on MPW Shell 3.5, Copyright Apple Computer, Inc. 1985-99. All rights reserved.\n", stdout);
if (_v) {
fdputs("This version built on " __DATE__ " at " __TIME__ ".\n", stdout);
}
return 0;
}
int builtin_aboutbox(Environment &env, const std::vector<std::string> &tokens, const fdmask &fds) {
// the most important command of all!
if (tokens.size() == 2 && tokens[1] == "--moof") {
fdputs(
"\n"
" ## \n"
" ## ## #### \n"
" ## #### ## \n"
" ## ## \n"
" ## ## ## ## \n"
" ## ## #### \n"
" ## ## ## ## \n"
" ######## #### ## ## \n"
" ## #################### ## \n"
" ## ############## ## \n"
" #### ############ ## \n"
" ###### ###### ## \n"
" ###### ## \n"
" #### ## \n"
" ## ## \n"
" ## ################ ## \n"
" ## ## ## ## \n"
" ## ## ## ## \n"
" ## ## ## ## \n"
" ## ## ## ## \n"
" ## ## ## ## \n"
" ###### ###### \n"
"\n"
,stdout);
return 0;
}