forked from KermitProject/ckermit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckucmd.c
8310 lines (7747 loc) · 225 KB
/
ckucmd.c
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 "ckcsym.h"
char *cmdv = "Command package 10.0.179, 12 Oct 2022";
/* C K U C M D -- Interactive command package for Unix */
/* (In reality, it's for all platforms, not just Unix) */
/*
Author: Frank da Cruz ([email protected]),
Formerly of Columbia University Academic Information Systems, New York City.
Since 1 July 2011, Open Source Kermit Project.
Most recent update: Fri Oct 14 13:44:53 2022
Copyright (C) 1985, 2022,
Trustees of Columbia University in the City of New York.
All rights reserved. See the C-Kermit COPYING.TXT file or the
copyright text in the ckcmai.c module for disclaimer and permissions.
Note: the name of these files really should be ckccmd.h and ckccmd.c
because they are for all platforms, not just Unix. But "don't fix
what ain't broke".
*/
#define FUNCTIONTEST
#define TOKPRECHECK
#define DOCHKVAR
/* Command-terminal-to-C-Kermit character mask */
#ifdef OS2 /* K95 */
int cmdmsk = 255; /* (always was 255) */
#else /* All others... */
int cmdmsk = 255; /* 31 Dec 2000 (was 127) */
#endif /* OS2 */
#ifdef BS_DIRSEP /* Directory separator is backslash */
#undef BS_DIRSEP
#endif /* BS_DIRSEP */
#ifdef OS2
#define BS_DIRSEP
#endif /* BS_DIRSEP */
#define CKUCMD_C
#include "ckcdeb.h" /* Formats for debug(), etc. */
#include "ckcker.h" /* Needed for BIGBUFOK definition */
#include "ckcnet.h" /* Needed for server-side Telnet */
#include "ckucmd.h" /* Needed for everything */
#include "ckuusr.h" /* Needed for prompt length */
#ifndef NOARROWKEYS
#ifndef NOESCSEQ
#ifdef VMSORUNIX
#define USE_ARROWKEYS /* Use arrow keys for command recall */
#endif /* VMSORUNIX */
#endif /* NOESCSEQ */
#endif /* NOARROWKEYS */
#undef CKUCMD_C
_PROTOTYP( int nlookup, (struct keytab [], char *, int, int *) );
_PROTOTYP( int unhex, (char) );
_PROTOTYP( static VOID cmdclrscn, (void) );
#ifdef CKLEARN
_PROTOTYP( VOID learncmd, (char *) );
#endif /* CKLEARN */
static char *moname[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
static char *fullmonthname[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
struct keytab cmonths[] = {
{ "april", 4, 0 },
{ "august", 8, 0 },
{ "december", 12, 0 },
{ "february", 2, 0 },
{ "january", 1, 0 },
{ "july", 7, 0 },
{ "june", 6, 0 },
{ "march", 3, 0 },
{ "may", 5, 0 },
{ "november", 11, 0 },
{ "october", 10, 0 },
{ "september", 9, 0 }
};
#ifndef NOICP /* The rest only if interactive command parsing selected */
#ifndef NOSPL
_PROTOTYP( int chkvar, (char *) );
extern int askflag, echostars;
#endif /* NOSPL */
#ifdef CKROOT
extern int ckrooterr;
#endif /* CKROOT */
#ifdef IKSD
extern int inserver;
#endif /* IKSD */
int cmfldflgs = 0; /* Flags for cmfld() */
int cmkwflgs = 0; /* Flags from last keyword parse */
static int nomsg = 0;
static int blocklvl = 0; /* Block nesting level */
static int linebegin = 0; /* Flag for at start of a line */
static int quoting = 1; /* Quoting is allowed */
static int swarg = 0; /* Parsing a switch argument */
static int xcmfdb = 0; /* Flag for parsing chained fdbs... */
static int chsrc = 0; /* Source of character, 1 = tty */
static int newcmd = 0; /* See addcmd() */
#ifdef BS_DIRSEP
static int dirnamflg = 0;
#endif /* BS_DIRSEP */
/*
Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:
. parses and verifies keywords, filenames, text strings, numbers, other data
. displays appropriate menu or help message when user types "?"
. does keyword and filename completion when user types ESC or TAB
. does partial keyword and filename completion
. accepts any unique abbreviation for a keyword
. allows keywords to have attributes, like "invisible" and "abbreviation"
. can supply defaults for fields omitted by user
. provides command retry and recall
. provides character, word, and line deletion (but only from the end)
. accepts input from keyboard, command files, macros, or redirected stdin
. allows for full or half duplex operation, character or line input
. allows \-escapes for special characters
. allows specification of a user exit to expand variables, etc.
. settable prompt, protected from deletion, dynamically re-evaluated each time
. allows chained parse functions.
Functions:
cmsetp - Set prompt (cmprom is prompt string)
cmsavp - Save current prompt
cmgetp = Get current prompt
prompt - Issue prompt
cmini - Clear the command buffer (before parsing a new command)
cmres - Reset command buffer pointers (before reparsing)
cmkey - Parse a keyword or token (also cmkey2)
cmswi - Parse a switch
cmnum - Parse a number
cmifi - Parse an input file name
cmofi - Parse an output file name (also cmifip, cmifi2, ...)
cmdir - Parse a directory name (also cmdirp)
cmfld - Parse an arbitrary field
cmtxt - Parse a text string
cmdate - Parse a date-time string
cmcfm - Parse command confirmation (end of line)
cmfdb - Parse any of a list of the foregoing (chained parse functions)
Return codes:
-9: like -2 except this module already printed the error message
-3: no input provided when required
-2: input was invalid (e.g. not a number when a number was required)
-1: reparse required (user deleted into a preceding field)
0 or greater: success
See individual functions for greater detail.
Before using these routines, the caller should #include "ckucmd.h" and set the
program's prompt by calling cmsetp(). If the file parsing functions cmifi,
cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
system support module for the appropriate system, e.g. ckufio for Unix. If
the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
then these functions will provide line editing -- character, word, and line
deletion, as well as keyword and filename completion upon ESC and help
strings, keyword, or file menus upon '?'. If the caller puts the terminal
into character wakeup/noecho mode, care should be taken to restore it before
exit from or interruption of the program. If the character wakeup mode is not
set, the system's own line editor may be used.
NOTE: Contrary to expectations, many #ifdef's have been added to this module.
Any operation requiring an #ifdef (like clear screen, get character from
keyboard, erase character from screen, etc) should eventually be turned into a
call to a function that is defined in ck?tio.c, but then all the ck?tio.c
modules would have to be changed...
*/
/* Includes */
#include "ckcker.h"
#include "ckcasc.h" /* ASCII character symbols */
#include "ckucmd.h" /* Command parsing definitions */
#ifdef OSF13
#ifdef CK_ANSIC
#ifdef _NO_PROTO
#undef _NO_PROTO
#endif /* _NO_PROTO */
#endif /* CK_ANSIC */
#endif /* OSF13 */
#ifndef OS2
#ifndef HPUXPRE65
#include <errno.h> /* Error number symbols */
#else
#ifndef ERRNO_INCLUDED
#include <errno.h> /* Error number symbols */
#endif /* ERRNO_INCLUDED */
#endif /* HPUXPRE65 */
#endif /* OS2 */
/* Error number symbols - any compiler targeting Windows
* and non-watcom compilers targeting OS/2. */
#ifdef OS2
#ifdef NT
#include <errno.h>
#else /* NT */
#ifndef __WATCOMC__
#include <errno.h>
#endif /* __WATCOMC__ */
#endif /* NT */
#endif /* OS2 */
#ifdef OS2
#ifndef NT
#define INCL_NOPM
#define INCL_VIO /* Needed for ckocon.h */
#include <os2.h>
#undef COMMENT
#else
#define APIRET ULONG
#include <windows.h>
#endif /* NT */
#include "ckocon.h"
#include <io.h>
#endif /* OS2 */
#ifdef OSK
#define cc ccount /* OS-9/68K compiler bug */
#endif /* OSK */
#ifdef GEMDOS /* Atari ST */
#ifdef putchar
#undef putchar
#endif /* putchar */
#define putchar(x) conoc(x)
#endif /* GEMDOS */
#ifdef CK_AUTODL
extern int cmdadl, justone;
#endif /* CK_AUTODL */
extern int timelimit, nzxopts, nopush, nolocal, xcmdsrc, keepallchars;
#ifdef CKSYSLOG
#ifdef UNIX
#ifdef CKXPRINTF /* Our printf macro conflicts with */
#undef printf /* use of "printf" in syslog.h */
#endif /* CKXPRINTF */
#ifdef RTAIX
#include <sys/syslog.h>
#else /* RTAIX */
#include <syslog.h>
#endif /* RTAIX */
#ifdef CKXPRINTF
#define printf ckxprintf
#endif /* CKXPRINTF */
#endif /* UNIX */
#endif /* CKSYSLOG */
/* Local variables */
static
int psetf = 0, /* Flag that prompt has been set */
cc = 0, /* Character count */
dpx = 0, /* Duplex (0 = full) */
inword = 0; /* In the middle of getting a word */
char *dfprom = "Command? "; /* Default prompt */
#ifndef NOLASTFILE
char *lastfile = NULL; /* Last filespec */
static char *tmplastfile = NULL; /* Last filespec candidate */
#endif /* NOLASTFILE */
int cmflgs; /* Command flags */
int cmfsav; /* A saved version of them */
static char pushc = NUL;
static char brkchar = NUL;
#define CMDEFAULT 1023
static char cmdefault[CMDEFAULT+1];
#ifdef DCMDBUF
char *cmdbuf = NULL; /* Command buffer */
char *savbuf = NULL; /* Buffer to save copy of command */
char *atmbuf = NULL; /* Atom buffer - for current field */
char *atxbuf = NULL; /* For expanding the atom buffer */
char *prevcmd = NULL;
static char *atybuf = NULL; /* For copying atom buffer */
static char *filbuf = NULL; /* File name buffer */
static char *cmprom = NULL; /* Program's prompt */
static char *cmprxx = NULL; /* Program's prompt, unevaluated */
#ifdef CK_RECALL
/*
Command recall is available only if we can make profligate use of malloc().
*/
#define R_MAX 10 /* How many commands to save */
int cm_recall = R_MAX; /* Size of command recall buffer */
int on_recall = 1; /* Recall feature is ON */
static int no_recall = 0; /* Recall OFF for this cmd only */
static int force_add = 0; /* Force cmd into recall buffer */
static int last_recall = -1; /* Last recall-related action */
/*
-1 = none
0 = CR (a command was entered)
1 = Up
2 = Down
*/
int in_recall = 0; /* Recall buffers are init'd */
static int
current = -1, /* Pointer to current command */
rlast = -1; /* Index of last command in buffer */
static char **recall = NULL; /* Array of recall buffer pointers */
#endif /* CK_RECALL */
#else /* !DCMDBUF */
char cmdbuf[CMDBL+4]; /* Command buffer */
char savbuf[CMDBL+4]; /* Buffer to save copy of command */
char atmbuf[ATMBL+4]; /* Atom buffer */
char atxbuf[CMDBL+4]; /* For expanding the atom buffer */
char prevcmd[CMDBL+4]; /* For displaying the last command */
static char atybuf[ATMBL+4]; /* For copying atom buffer */
static char filbuf[ATMBL+4]; /* File name buffer */
static char cmprom[PROMPTL+1]; /* Program's prompt */
static char cmprxx[PROMPTL+1]; /* Program's prompt, unevaluated */
#endif /* DCMDBUF */
/* Command buffer pointers */
#define PPVLEN VNAML /* 20080305 Wolfram Sang (was 24) */
char ppvnambuf[PPVLEN+1] = { NUL, NUL };
char * cmbptr = NULL; /* Current position (for export) */
static char *bp, /* Current command buffer position */
*pp, /* Start of current field */
*np; /* Start of next field */
static int ungw, /* For ungetting words */
atxn; /* Expansion buffer (atxbuf) length */
#ifdef OS2
extern int wideresult;
#endif /* OS2 */
extern int cmd_cols, cmd_rows, local, quiet;
#ifdef TNCODE
#ifdef IAC
#undef IAC
#endif /* IAC */
#define IAC 255
#endif /* TNCODE */
_PROTOTYP( static int gtword, (int) );
_PROTOTYP( static int addbuf, (char *) );
_PROTOTYP( static int setatm, (char *, int) );
_PROTOTYP( static VOID cmdnewl, (char) );
_PROTOTYP( static VOID cmdchardel, (void) );
_PROTOTYP( static VOID cmdecho, (char, int) );
_PROTOTYP( static int test, (int, int) );
#ifdef GEMDOS
_PROTOTYP( extern char *strchr, (char *, int) );
#endif /* GEMDOS */
extern char * dftty;
/* The following are for use with chained FDB's */
static int crflag = 0; /* Carriage return was typed */
static int qmflag = 0; /* Question mark was typed */
static int esflag = 0; /* Escape was typed */
/* Directory separator */
#ifdef GEMDOS
static char dirsep = '\\';
#else
#ifdef datageneral
static char dirsep = ':';
#else
#ifdef MAC
static char dirsep = ':';
#else
#ifdef VMS
static char dirsep = '.';
#else
#ifdef STRATUS
static char dirsep = '>';
#else
static char dirsep = '/'; /* UNIX, OS/2, OS-9, Amiga, etc. */
#endif /* STRATUS */
#endif /* VMS */
#endif /* MAC */
#endif /* datageneral */
#endif /* GEMDOS */
/* H A S N O P A T H */
/* Returns 0 if filespec s includes any path segments; 1 if it doesn't. */
int
hasnopath(s) char * s; {
char * p = NULL;
if (!s) return(0);
if (!*s) return(0);
zstrip(s,&p);
return(ckstrcmp(s,p,CKMAXPATH,filecase) == 0 ? 1 : 0);
}
/* C K S P R E A D -- Print string double-spaced */
static char * sprptr = NULL;
static char *
ckspread(s) char * s; {
int n = 0;
char * p;
n = strlen(s);
if (sprptr)
free(sprptr);
sprptr = malloc(n + n + 3);
if (sprptr) {
p = sprptr;
while (*s) {
*p++ = *s++;
*p++ = SP;
}
*p = NUL;
}
return(sprptr ? sprptr : "");
}
/* T E S T -- Bit test */
static int
test(x,m) int x, m; { /* Returns 1 if any bits from m are on in x, else 0 */
return((x & m) ? 1 : 0);
}
/* K W D H E L P -- Given a keyword table, print keywords in columns. */
/*
Call with:
s - keyword table
n - number of entries
pat - pattern (left substring) that must match for each keyword
pre - prefix to add to each keyword
post - suffix to add to each keyword
off - offset on first screenful, allowing room for introductory text
xhlp - 1 to print any CM_INV keywords that are not also abbreviations.
2 to print CM_INV keywords if CM_HLP also set
4 if it's a switch table (to show ':' if CM_ARG)
8 print any keywords that CONTAIN the pattern
Arranges keywords in columns with width based on longest keyword.
Does "more?" prompting at end of screen.
Uses global cmd_rows and cmd_cols for screen size.
*/
VOID
kwdhelp(s,n,pat,pre,post,off,xhlp)
struct keytab s[]; int n, off, xhlp; char *pat, *pre, *post;
/* kwdhelp */ {
int width = 0;
int cc;
int cols, height, i, j, k, lc, n2 = 0;
char *b = NULL, *p, *q;
char *pa, *px;
char **s2 = NULL;
char *tmpbuf = NULL;
cc = strlen(pat);
if (!s) return; /* Nothing to do */
if (n < 1) return; /* Ditto */
if (off < 0) off = 0; /* Offset for first page */
if (!pre) pre = ""; /* Handle null string pointers */
if (!post) post = "";
lc = off; /* Screen-line counter */
if (xhlp & 4) /* For switches */
tmpbuf = (char *)malloc(TMPBUFSIZ+1);
if ((s2 = (char **) malloc(n * sizeof(char *)))) {
for (i = 0; i < n; i++) { /* Find longest keyword */
s2[i] = NULL;
if (xhlp & 8) {
if (ckindex(pat,s[i].kwd,0,0,0) < 1) /* for SHOW FUNCTIONS */
continue;
} else if (ckstrcmp(s[i].kwd,pat,cc,0)) /* for regular keywords */
continue;
if (s[i].flgs & CM_PSH /* NOPUSH or nopush screening */
#ifndef NOPUSH
&& nopush
#endif /* NOPUSH */
)
continue;
if (s[i].flgs & CM_LOC /* NOLOCAL or nolocal screening */
#ifndef NOLOCAL
&& nolocal
#endif /* NOLOCAL */
)
continue;
if (s[i].flgs & CM_INV) {
#ifdef COMMENT
/* This code does not show invisible keywords at all except for "help ?" */
/* and then only help topics (CM_HLP) in the top-level keyword list. */
if ((xhlp & 2) == 0)
continue;
else if ((s[i].flgs & CM_HLP) == 0)
continue;
#else
/* This code shows invisible keywords that are not also abbreviations when */
/* ? was typed AFTER the beginning of the field so the user can find out */
/* what they are and (for example) why completion doesn't work at this point */
if (s[i].flgs & CM_ABR)
continue;
else if ((xhlp & 3) == 0)
continue;
else if ((xhlp & 2) && ((s[i].flgs & CM_HLP) == 0))
continue;
#endif /* COMMENT */
}
j = strlen(s[i].kwd);
if (!(xhlp & 4) || !tmpbuf) { /* Regular keyword table */
s2[n2++] = s[i].kwd; /* Copy pointers to visible ones */
} else { /* Switches */
ckmakmsg(tmpbuf, /* Make a copy that shows ":" if */
TMPBUFSIZ, /* the switch takes an argument. */
s[i].kwd,
(s[i].flgs & CM_ARG) ? ":" : "",
NULL,
NULL
);
makestr(&(s2[n2]),tmpbuf);
if (s[i].flgs & CM_ARG) j++;
n2++;
}
if (j > width)
width = j;
}
/* Column width */
n = n2;
}
if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
char * bx;
bx = b + cmd_cols;
width += (int)strlen(pre) + (int)strlen(post) + 2;
cols = cmd_cols / width; /* How many columns? */
if (cols < 1) cols = 1;
height = n / cols; /* How long is each column? */
if (n % cols) height++; /* Add one for remainder, if any */
for (i = 0; i < height; i++) { /* Loop for each row */
for (j = 0; j < cmd_cols; j++) /* First fill row with blanks */
b[j] = SP;
for (j = 0; j < cols; j++) { /* Loop for each column in row */
k = i + (j * height); /* Index of next keyword */
if (k < n) { /* In range? */
pa = pre;
px = post;
p = s2[k]; /* Point to verb name */
q = b + (j * width) + 1; /* Where to copy it to */
while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
q--; /* Back up over NUL */
while ((q < bx) && (*q++ = *p++)) ; /* Copy filename */
q--; /* Back up over NUL */
while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
if (j < cols - 1) {
q--;
*q = SP; /* Replace the space */
}
}
}
p = b + cmd_cols - 1; /* Last char in line */
while (*p-- == SP) ; /* Trim */
*(p+2) = NUL;
printf("%s\n",b); /* Print the line */
if (++lc > (cmd_rows - 2)) { /* Screen full? */
if (!askmore()) /* Do more-prompting... */
goto xkwdhelp;
else
lc = 0;
}
}
/* printf("\n"); */ /* Blank line at end of report */
} else { /* Malloc failure, no columns */
for (i = 0; i < n; i++) {
if (s[i].flgs & CM_INV) /* Use original keyword table */
continue; /* skipping invisible entries */
printf("%s%s%s\n",pre,s[i].kwd,post);
if (++lc > (cmd_rows - 2)) { /* Screen full? */
if (!askmore()) /* Do more-prompting... */
goto xkwdhelp;
else
lc = 0;
}
}
}
xkwdhelp:
if (xhlp & 4) {
if (tmpbuf) free((char *)tmpbuf);
for (i = 0; i < n; i++)
if (s2[i]) free(s2[i]);
}
if (s2) free(s2); /* Free array copy */
if (b) free(b); /* Free line buffer */
return;
}
/* X F I L H E L P -- Given a file list, print names in columns. */
/*
Call with:
n - number of entries
pre - prefix to add to each filename
post - suffix to add to each filename
off - offset on first screenful, allowing room for introductory text
cmdirflg - 1 if only directory names should be listed, 0 to list all files
fs - call fileselect() to decide whether to include each file.
The rest of the args are the same as for fileselect().
Arranges filenames in columns with width based on longest filename.
Does "more?" prompting at end of screen.
Uses global cmd_rows and cmd_cols for screen size.
*/
int
#ifdef CK_ANSIC
xfilhelp(
int n, char *pre, char *post, int off, int cmdirflag,
int fs, char *sa, char *sb, char *sna, char *snb,
CK_OFF_T minsiz, CK_OFF_T maxsiz,
int nbu, int nxlist,
char ** xlist
)
#else
xfilhelp(n,pre,post,off,cmdirflg,
fs,sa,sb,sna,snb,minsiz,maxsiz,nbu,nxlist,xlist)
int n, off; char *pre, *post; int cmdirflg;
int fs; char *sa,*sb,*sna,*snb; CK_OFF_T minsiz,maxsiz;
int nbu,nxlist; char ** xlist;
#endif /* CK_ANSIC */
{
char filbuf[CKMAXPATH + 1]; /* Temp buffer for one filename */
int width = 0;
int cols, height, i, j, k, lc, n2 = 0, rc = 0, itsadir = 0;
char *b = NULL, *p, *q;
char *pa, *px;
char **s2 = NULL;
#ifdef VMS
char * cdp = zgtdir();
#endif /* VMS */
if (n < 1) return(0);
if (off < 0) off = 0; /* Offset for first page */
if (!pre) pre = ""; /* Handle null string pointers */
if (!post) post = "";
lc = off; /* Screen-line counter */
if ((s2 = (char **) malloc(n * sizeof(char *)))) {
for (i = 0; i < n; i++) { /* Loop through filenames */
itsadir = 0;
s2[i] = NULL; /* Initialize each pointer to NULL */
znext(filbuf); /* Get next filename */
if (!filbuf[0]) /* Shouldn't happen */
break;
#ifdef COMMENT
itsadir = isdir(filbuf); /* Is it a directory? */
if (cmdirflg && !itsadir) /* No, listing directories only? */
continue; /* So skip this one. */
#endif /* COMMENT */
if (fs) if (fileselect(filbuf,
sa,sb,sna,snb,
minsiz,maxsiz,nbu,nxlist,xlist) < 1) {
continue;
}
#ifdef VMS
ckstrncpy(filbuf,zrelname(filbuf,cdp),CKMAXPATH);
#endif /* VMS */
j = strlen(filbuf);
#ifndef VMS
if (itsadir && j < CKMAXPATH - 1 && j > 0) {
if (filbuf[j-1] != dirsep) {
filbuf[j++] = dirsep;
filbuf[j] = NUL;
}
}
#endif /* VMS */
if (!(s2[n2] = malloc(j+1))) {
printf("?Memory allocation failure\n");
rc = -9;
goto xfilhelp;
}
if (j <= CKMAXPATH) {
strcpy(s2[n2],filbuf);
n2++;
} else {
printf("?Name too long - %s\n", filbuf);
rc = -9;
goto xfilhelp;
}
if (j > width) /* Get width of widest one */
width = j;
}
n = n2; /* How many we actually got */
}
sh_sort(s2,NULL,n,0,0,filecase); /* Alphabetize the list */
rc = 1;
if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
char * bx;
bx = b + cmd_cols;
width += (int)strlen(pre) + (int)strlen(post) + 2;
cols = cmd_cols / width; /* How many columns? */
if (cols < 1) cols = 1;
height = n / cols; /* How long is each column? */
if (n % cols) height++; /* Add one for remainder, if any */
for (i = 0; i < height; i++) { /* Loop for each row */
for (j = 0; j < cmd_cols; j++) /* First fill row with blanks */
b[j] = SP;
for (j = 0; j < cols; j++) { /* Loop for each column in row */
k = i + (j * height); /* Index of next filename */
if (k < n) { /* In range? */
pa = pre;
px = post;
p = s2[k]; /* Point to filename */
q = b + (j * width) + 1; /* and destination */
while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
q--; /* Back up over NUL */
while ((q < bx) && (*q++ = *p++)) ; /* Copy filename */
q--; /* Back up over NUL */
while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
if (j < cols - 1) {
q--;
*q = SP; /* Replace the space */
}
}
}
p = b + cmd_cols - 1; /* Last char in line */
while (*p-- == SP) ; /* Trim */
*(p+2) = NUL;
printf("%s\n",b); /* Print the line */
if (++lc > (cmd_rows - 2)) { /* Screen full? */
if (!askmore()) { /* Do more-prompting... */
rc = 0;
goto xfilhelp;
} else
lc = 0;
}
}
printf("\n"); /* Blank line at end of report */
goto xfilhelp;
} else { /* Malloc failure, no columns */
for (i = 0; i < n; i++) {
znext(filbuf);
if (!filbuf[0]) break;
printf("%s%s%s\n",pre,filbuf,post);
if (++lc > (cmd_rows - 2)) { /* Screen full? */
if (!askmore()) { /* Do more-prompting... */
rc = 0;
goto xfilhelp;
} else lc = 0;
}
}
xfilhelp:
if (b) free(b);
for (i = 0; i < n2; i++)
if (s2[i]) free(s2[i]);
if (s2) free((char *)s2);
return(rc);
}
}
/*
Simpler front end for xfilhelp() with shorter arg list when no
file selection is needed.
*/
int
filhelp(n,pre,post,off,cmdirflg) int n, off; char *pre, *post; int cmdirflg; {
return(xfilhelp(n,pre,post,off,cmdirflg,
0,NULL,NULL,NULL,NULL,
(CK_OFF_T)0,(CK_OFF_T)0,0,0,(char **)NULL));
}
/* C M S E T U P -- Set up command buffers */
#ifdef DCMDBUF
int
cmsetup() {
if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
if (!(savbuf = malloc(CMDBL + 4))) return(-1);
savbuf[0] = '\0';
if (!(prevcmd = malloc(CMDBL + 4))) return(-1);
prevcmd[0] = '\0';
if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
if (!(atybuf = malloc(ATMBL + 4))) return(-1);
if (!(filbuf = malloc(ATMBL + 4))) return(-1);
if (!(cmprom = malloc(PROMPTL + 4))) return(-1);
if (!(cmprxx = malloc(PROMPTL + 4))) return(-1);
#ifdef CK_RECALL
cmrini(cm_recall);
#endif /* CK_RECALL */
return(0);
}
#endif /* DCMDBUF */
/* C M S E T P -- Set the program prompt. */
VOID
cmsetp(s) char *s; {
if (!s) s = "";
ckstrncpy(cmprxx,s,PROMPTL);
psetf = 1; /* Flag that prompt has been set. */
}
/* C M S A V P -- Save a copy of the current prompt. */
VOID
#ifdef CK_ANSIC
cmsavp(char s[], int n)
#else
cmsavp(s,n) char s[]; int n;
#endif /* CK_ANSIC */
/* cmsavp */ {
if (psetf) /* But not if no prompt is set. */
ckstrncpy(s,cmprxx,n);
}
char *
cmgetp() {
return(cmprxx);
}
int
cmgbrk() {
return(brkchar);
}
int
cmgkwflgs() {
return(cmkwflgs);
}
/* P R O M P T -- Issue the program prompt. */
VOID
prompt(f) xx_strp f; {
char *sx, *sy; int n;
#ifdef CK_SSL
extern int ssl_active_flag, tls_active_flag;
#endif /* CK_SSL */
if (psetf == 0) /* If no prompt set, set default. */
cmsetp(dfprom);
sx = cmprxx; /* Unevaluated copy */
if (f) { /* If conversion function given */
sy = cmprom; /* Evaluate it */
#ifdef COMMENT
debug(F101,"prompt sx","",sx);
debug(F101,"prompt sy","",sy);
#endif /* COMMENT */
n = PROMPTL;
if ((*f)(sx,&sy,&n) < 0) /* If evaluation failed */
sx = cmprxx; /* revert to unevaluated copy */
else if (!*cmprom) /* ditto if it came up empty */
sx = cmprxx;
else
sx = cmprom;
} else
ckstrncpy(cmprom,sx,PROMPTL);
cmprom[PROMPTL-1] = NUL;
if (!*sx) /* Don't print if empty */
return;
#ifdef OSK
fputs(sx, stdout);
#else
#ifdef MAC
printf("%s", sx);
#else
#ifdef IKSD
if (inserver) { /* Print the prompt. */
ttoc(CK_CR); /* If TELNET Server */
ttoc(NUL); /* must folloW CR by NUL */
printf("%s",sx);
} else
#endif /* IKSD */
printf("\r%s",sx);
#ifdef CK_SSL
if (!(ssl_active_flag || tls_active_flag))
#endif /* CK_SSL */
fflush(stdout); /* Now! */
#endif /* MAC */
#endif /* OSK */
}
#ifndef NOSPL
VOID
pushcmd(s) char * s; { /* For use with IF command. */
if (!s) s = np;
ckstrncpy(savbuf,s,CMDBL); /* Save the dependent clause, */
cmres(); /* and clear the command buffer. */
debug(F110, "pushcmd savbuf", savbuf, 0);
}
VOID
pushqcmd(s) char * s; { /* For use with ELSE command. */
char c, * p = savbuf; /* Dest */
if (!s) s = np; /* Source */
while (*s) { /* Get first nonwhitespace char */
if (*s != SP)
break;
else
s++;
}
if (*s != '{') { /* If it's not "{" */
pushcmd(s); /* do regular pushcmd */
return;
}
while ((c = *s++)) { /* Otherwise insert quotes */
if (c == CMDQ)
*p++ = CMDQ;
*p++ = c;
}
cmres(); /* and clear the command buffer. */
debug(F110, "pushqcmd savbuf", savbuf, 0);
}
#endif /* NOSPL */
#ifdef COMMENT
/* no longer used... */
VOID
popcmd() {
ckstrncpy(cmdbuf,savbuf,CMDBL); /* Put back the saved material */
*savbuf = '\0'; /* and clear the save buffer */
cmres();
}
#endif /* COMMENT */
/* C M R E S -- Reset pointers to beginning of command buffer. */
VOID
cmres() {
inword = 0; /* We're not in a word */
cc = 0; /* Character count is zero */
/* Initialize pointers */
pp = cmdbuf; /* Beginning of current field */
bp = cmdbuf; /* Current position within buffer */
np = cmdbuf; /* Where to start next field */
cmfldflgs = 0;
cmflgs = -5; /* Parse not yet started. */
ungw = 0; /* Don't need to unget a word. */
}
/* C M I N I -- Clear the command and atom buffers, reset pointers. */
/*
The argument specifies who is to echo the user's typein --
1 means the cmd package echoes
0 somebody else (system, front end, terminal) echoes
*/
VOID
cmini(d) int d; {
#ifdef DCMDBUF
if (!atmbuf)
if (cmsetup()<0)
fatal("fatal error: unable to allocate command buffers");
#endif /* DCMDBUF */
#ifdef USE_MEMCPY
memset(cmdbuf,0,CMDBL);
memset(atmbuf,0,ATMBL);
#else
for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
for (bp = atmbuf; bp < atmbuf+ATMBL; bp++) *bp = NUL;