Skip to content

Commit f0bd8e0

Browse files
authored
Merge pull request #540 from Interlisp/nhb-string-handling-cleanup
2 parents 428e7cf + c9e2759 commit f0bd8e0

File tree

14 files changed

+103
-111
lines changed

14 files changed

+103
-111
lines changed

bin/makefile-tail

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ $(OBJECTDIR)initsout.o: $(SRCDIR)initsout.c $(REQUIRED-INCS) \
674674

675675
$(OBJECTDIR)kbdsubrs.o: $(SRCDIR)kbdsubrs.c $(REQUIRED-INCS) \
676676
$(INCDIR)lispemul.h $(INCDIR)kbdsubrsdefs.h \
677-
$(INCDIR)commondefs.h $(INCDIR)lisp2cdefs.h $(INCDIR)xwinmandefs.h \
677+
$(INCDIR)commondefs.h $(INCDIR)xwinmandefs.h \
678678
$(INCDIR)devif.h
679679
$(CC) $(RFLAGS) $(SRCDIR)kbdsubrs.c -o $(OBJECTDIR)kbdsubrs.o
680680

inc/locfile.h

Lines changed: 67 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <limits.h> /* for NAME_MAX */
1313
#include <dirent.h> /* for MAXNAMLEN */
1414
#include "lispemul.h" /* for DLword */
15+
#include "commondefs.h" /* for error */
1516

1617
#define FDEV_PAGE_SIZE 512 /* 1 page == 512 byte */
1718

@@ -44,18 +45,17 @@
4445
/* For getfileinfo. For WDATE&RDATE */
4546
/* 29969152 == (timer.c)LISP_UNIX_TIME_DIFF */
4647

47-
#define StrNCpyFromCToLisp(lispbuf, cbuf ,len) do { \
48-
char *lf_sptr = (cbuf); \
49-
char *lf_dptr = (lispbuf); \
50-
for(size_t lf_i=0;lf_i<(len);lf_i++)\
51-
GETBYTE(lf_dptr++) = *lf_sptr++; \
52-
} while (0)
53-
54-
#define StrNCpyFromLispToC(cbuf , lispbuf, len) do { \
55-
char *lf_sptr = (lispbuf); \
56-
char *lf_dptr = (cbuf); \
57-
for(size_t lf_i=0;lf_i<(len);lf_i++)\
58-
*lf_dptr++ = GETBYTE(lf_sptr++); \
48+
/*
49+
* Copy memory between native memory locations accounting for potential
50+
* byte-swapping necessary when then destination is within Lisp memory space
51+
* though the provided destination pointer is a native address within the
52+
* Lisp space.
53+
*/
54+
#define MemCpyToLispFromNative(lispbuf, cbuf, len) \
55+
do { \
56+
char *lf_sptr = (cbuf); \
57+
char *lf_dptr = (lispbuf); \
58+
for (size_t lf_i = 0; lf_i < (len); lf_i++) *BYTEPTR(lf_dptr++) = *lf_sptr++; \
5959
} while (0)
6060

6161
#define FGetNum(ptr, place) do { \
@@ -64,6 +64,10 @@
6464
else {return(NIL);}} while (0)
6565

6666

67+
#ifndef min
68+
#define min(a, b) (((a) <= (b))?(a):(b))
69+
#endif /* min */
70+
6771
/************************************************************************/
6872
/* */
6973
/* L i s p S t r i n g T o C S t r i n g */
@@ -79,65 +83,52 @@
7983
/* */
8084
/************************************************************************/
8185
#ifndef BYTESWAP
82-
#define LispStringToCString(Lisp, C, MaxLen) \
83-
do { \
84-
OneDArray *lf_arrayp; \
85-
char *lf_base, *lf_dp; \
86-
short *lf_sbase; \
87-
size_t lf_length; \
88-
lf_arrayp = (OneDArray *)NativeAligned4FromLAddr(Lisp); \
89-
lf_length = min(MaxLen, lf_arrayp->fillpointer); \
90-
switch(lf_arrayp->typenumber) \
91-
{ \
92-
case THIN_CHAR_TYPENUMBER: \
93-
lf_base = ((char *)(NativeAligned2FromLAddr(lf_arrayp->base))) \
94-
+ ((int)(lf_arrayp->offset)); \
95-
strncpy(C, lf_base, lf_length); \
96-
(C)[lf_length] = '\0'; \
97-
break; \
98-
\
99-
case FAT_CHAR_TYPENUMBER: \
100-
lf_sbase = ((short *)(NativeAligned2FromLAddr(lf_arrayp->base))) \
101-
+ ((int)(lf_arrayp->offset)); \
102-
lf_dp = C; \
103-
for(size_t lf_i=0;lf_i<(lf_length);lf_i++) \
104-
*lf_dp++ = (char)(*lf_sbase++); \
105-
*lf_dp = '\0'; \
106-
break; \
107-
default: \
108-
error("LispStringToCString: Not a character array.\n"); \
109-
} \
110-
} while (0)
86+
static void LispStringToCString(LispPTR Lisp, char *C, size_t MaxLen) {
87+
OneDArray *lf_arrayp;
88+
char *lf_base, *lf_dp;
89+
short *lf_sbase;
90+
size_t lf_length;
91+
lf_arrayp = (OneDArray *)NativeAligned4FromLAddr(Lisp);
92+
lf_length = min(MaxLen - 1, lf_arrayp->fillpointer);
93+
lf_dp = (C);
94+
switch (lf_arrayp->typenumber) {
95+
case THIN_CHAR_TYPENUMBER:
96+
lf_base = ((char *)(NativeAligned2FromLAddr(lf_arrayp->base))) + ((int)(lf_arrayp->offset));
97+
strncpy(lf_dp, lf_base, lf_length);
98+
lf_dp[lf_length] = '\0';
99+
break;
100+
101+
case FAT_CHAR_TYPENUMBER:
102+
lf_sbase = ((short *)(NativeAligned2FromLAddr(lf_arrayp->base))) + ((int)(lf_arrayp->offset));
103+
for (size_t lf_i = 0; lf_i < (lf_length); lf_i++) *lf_dp++ = (char)(*lf_sbase++);
104+
*lf_dp = '\0';
105+
break;
106+
default: error("LispStringToCString: Not a character array.\n");
107+
}
108+
}
111109
#else /* BYTESWAP == T CHANGED-BY-TAKE */
112-
#define LispStringToCString(Lisp, C, MaxLen) \
113-
do { \
114-
OneDArray *lf_arrayp; \
115-
char *lf_base, *lf_dp; \
116-
short *lf_sbase; \
117-
size_t lf_length; \
118-
lf_arrayp = (OneDArray *)(NativeAligned4FromLAddr(Lisp)); \
119-
lf_length = min(MaxLen, lf_arrayp->fillpointer); \
120-
switch(lf_arrayp->typenumber) \
121-
{ \
122-
case THIN_CHAR_TYPENUMBER: \
123-
lf_base = ((char *)(NativeAligned2FromLAddr(lf_arrayp->base))) \
124-
+ ((int)(lf_arrayp->offset)); \
125-
StrNCpyFromLispToC(C , lf_base , lf_length ); \
126-
(C)[lf_length] = '\0'; \
127-
break; \
128-
\
129-
case FAT_CHAR_TYPENUMBER: \
130-
lf_sbase = ((short *)(NativeAligned2FromLAddr(lf_arrayp->base))) \
131-
+ ((int)(lf_arrayp->offset)); \
132-
lf_dp = C; \
133-
for(size_t lf_ii=0;lf_ii<(lf_length);lf_ii++,lf_sbase++) \
134-
*lf_dp++ = (char)(GETWORD(lf_sbase)); \
135-
*lf_dp = '\0'; \
136-
break; \
137-
default: \
138-
error("LispStringToCString: Not a character array.\n"); \
139-
} \
140-
} while (0)
110+
static void LispStringToCString(LispPTR Lisp, char *C, size_t MaxLen) {
111+
OneDArray *lf_arrayp;
112+
char *lf_base, *lf_dp;
113+
short *lf_sbase;
114+
size_t lf_length;
115+
lf_arrayp = (OneDArray *)(NativeAligned4FromLAddr(Lisp));
116+
lf_length = min(MaxLen - 1, lf_arrayp->fillpointer);
117+
lf_dp = (C);
118+
switch (lf_arrayp->typenumber) {
119+
case THIN_CHAR_TYPENUMBER:
120+
lf_base = ((char *)(NativeAligned2FromLAddr(lf_arrayp->base))) + ((int)(lf_arrayp->offset));
121+
for (size_t lf_i = 0; lf_i < lf_length; lf_i++) *lf_dp++ = GETBYTE(lf_base++);
122+
break;
123+
124+
case FAT_CHAR_TYPENUMBER:
125+
lf_sbase = ((short *)(NativeAligned2FromLAddr(lf_arrayp->base))) + ((int)(lf_arrayp->offset));
126+
for (size_t lf_ii = 0; lf_ii < lf_length; lf_ii++) *lf_dp++ = (char)(GETWORD(lf_sbase++));
127+
break;
128+
default: error("LispStringToCString: Not a character array.\n");
129+
}
130+
*lf_dp = '\0';
131+
}
141132

142133
#endif /* BYTESWAP */
143134

@@ -189,10 +180,6 @@ do { \
189180
(cstringp) = (char *)(NativeAligned2FromLAddr(((OneDArray *)lf_naddress)->base)); \
190181
} while (0)
191182

192-
#ifndef min
193-
#define min(a, b) (((a) <= (b))?(a):(b))
194-
#endif /* min */
195-
196183
#define LispNumToCInt(Lisp) \
197184
( (((Lisp) & SEGMASK) == S_POSITIVE) ? ((Lisp) & 0xFFFF) : \
198185
(((Lisp) & SEGMASK) == S_NEGATIVE) ? ((Lisp) | 0xFFFF0000) : \
@@ -299,7 +286,7 @@ do { \
299286
* Argument: char *pathname
300287
* Xerox Lisp syntax pathname.
301288
*
302-
* Value: If succeed, returns 1, otherwise 0.
289+
* Value: On success returns 1, otherwise 0.
303290
*
304291
* Side Effect: The version part of pathname is destructively modified.
305292
*
@@ -312,7 +299,7 @@ do { \
312299
* code.
313300
* This macro should be called at the top of the routines which accept the
314301
* file name from lisp before converting it into UNIX file name, because
315-
* locating the version part, the informations about quoted characters are needed.
302+
* locating the version part, the information about quoted characters are needed.
316303
* They might be lost in the course of the conversion.
317304
*
318305
*/
@@ -337,19 +324,18 @@ do { \
337324
* If 0, versionless file is converted to version 1.
338325
* Otherwise, remains as versionless.
339326
*
340-
* Value: If succeed, returns 1, otherwise 0.
327+
* Value: On success returns 1, otherwise 0.
341328
*
342329
* Side Effect: The version part of pathname is destructively modified.
343330
*
344331
* Description:
345332
*
346-
* Destructively modify the version part of pathname which is following the
333+
* Destructively modifies the version part of pathname which is following the
347334
* UNIX file naming convention to Xerox Lisp one.
348335
* This macro should be called, in the routines which convert the UNIX pathname
349336
* to Lisp one, just before it returns the result to Lisp, because converting
350-
* version field will append a semicolon and it might make the routine be
351-
* confused.
352-
* The file which has not a valid version field, that is ".~##~" form, is
337+
* version field will append a semicolon which may confuse the routine
338+
* The file which does not have a valid version field, that is ".~##~" form, is
353339
* dealt with as version 1.
354340
*/
355341

inc/vmemsavedefs.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef VMEMSAVEDEFS_H
22
#define VMEMSAVEDEFS_H 1
3-
#include "lispemul.h" /* for LispPTR, DLword */
4-
int lispstringP(LispPTR Lisp);
3+
#include "lispemul.h" /* for LispPTR */
54
LispPTR vmem_save(char *sysout_file_name);
65
LispPTR vmem_save0(LispPTR *args);
76
void lisp_finish(int exit_status);

src/dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ LispPTR COM_next_file(LispPTR *args)
21592159
#ifndef BYTESWAP
21602160
strncpy(base, fp->lname, fp->lname_len);
21612161
#else
2162-
StrNCpyFromCToLisp(base, fp->lname, fp->lname_len);
2162+
MemCpyToLispFromNative(base, fp->lname, fp->lname_len);
21632163
#endif /* BYTESWAP */
21642164

21652165
if (!propp) return (GetPosSmallp(fp->lname_len));
@@ -2175,7 +2175,7 @@ LispPTR COM_next_file(LispPTR *args)
21752175
#ifndef BYTESWAP
21762176
strncpy(base, pp->author, pp->au_len);
21772177
#else
2178-
StrNCpyFromCToLisp(base, pp->author, pp->au_len);
2178+
MemCpyToLispFromNative(base, pp->author, pp->au_len);
21792179
#endif /* BYTESWAP */
21802180

21812181
gfsp->aulen = pp->au_len;

src/dsk.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,9 @@ LispPTR DSK_getfilename(LispPTR *args)
10321032
len = strlen(lfname);
10331033

10341034
#ifndef BYTESWAP
1035-
strncpy(base, lfname, len + 1);
1035+
strncpy(base, lfname, len);
10361036
#else
1037-
StrNCpyFromCToLisp(base, lfname, len + 1);
1037+
MemCpyToLispFromNative(base, lfname, len);
10381038
#endif /* BYTESWAP */
10391039

10401040
return (GetPosSmallp(len));
@@ -1068,9 +1068,9 @@ LispPTR DSK_getfilename(LispPTR *args)
10681068
len = strlen(lfname);
10691069

10701070
#ifndef BYTESWAP
1071-
strncpy(base, lfname, len + 1);
1071+
strncpy(base, lfname, len);
10721072
#else
1073-
StrNCpyFromCToLisp(base, lfname, len + 1);
1073+
MemCpyToLispFromNative(base, lfname, len);
10741074
#endif /* BYTESWAP */
10751075

10761076
return (GetPosSmallp(len));
@@ -1507,9 +1507,9 @@ LispPTR DSK_directorynamep(LispPTR *args)
15071507
STRING_BASE(args[1], base);
15081508

15091509
#ifndef BYTESWAP
1510-
strncpy(base, dirname, len + 1);
1510+
strncpy(base, dirname, len);
15111511
#else
1512-
StrNCpyFromCToLisp(base, dirname, len + 1);
1512+
MemCpyToLispFromNative(base, dirname, len);
15131513
#endif /* BYTESWAP */
15141514

15151515
return (GetPosSmallp(len));
@@ -1671,7 +1671,7 @@ LispPTR COM_getfileinfo(LispPTR *args)
16711671
#ifndef BYTESWAP
16721672
strncpy(base, pwd->pw_name, len);
16731673
#else
1674-
StrNCpyFromCToLisp(base, pwd->pw_name, len);
1674+
MemCpyToLispFromNative(base, pwd->pw_name, len);
16751675
#endif /* BYTESWAP */
16761676
#endif /* DOS */
16771677
return (GetPosSmallp(len));
@@ -1710,7 +1710,7 @@ LispPTR COM_getfileinfo(LispPTR *args)
17101710
#ifndef BYTESWAP
17111711
strncpy(base, pwd->pw_name, len);
17121712
#else
1713-
StrNCpyFromCToLisp(base, pwd->pw_name, len);
1713+
MemCpyToLispFromNative(base, pwd->pw_name, len);
17141714
#endif /* BYTESWAP */
17151715
#endif /* DOS */
17161716
return (GetPosSmallp(len));

src/ether_nethub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ LispPTR ether_get(LispPTR args[])
468468
log_debug(("ether_get() - begin\n"));
469469

470470
target = (u_char *)NativeAligned2FromLAddr(args[1]);
471-
maxByteCount = 2 * LispIntToCInt(args[0]); /* words to bytes */
471+
maxByteCount = BYTESPER_DLWORD * LispIntToCInt(args[0]); /* words to bytes */
472472
log_debug((" target = %p maxBytecount: %d bytes\n", (void *)target, maxByteCount));
473473

474474
ether_buf = target;
@@ -501,7 +501,7 @@ LispPTR ether_send(LispPTR args[])
501501
log_debug(("ether_send() - begin\n"));
502502

503503
u_char *source = (u_char *)NativeAligned2FromLAddr(args[1]);
504-
int byteCount = 2 * LispIntToCInt(args[0]); /* words to bytes */
504+
int byteCount = BYTESPER_DLWORD * LispIntToCInt(args[0]); /* words to bytes */
505505

506506
log_debug((" source = %p , bytecount: %d bytes\n", (void *)source, byteCount));
507507

src/ether_sunos.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ LispPTR ether_get(LispPTR args[])
366366
LispPTR MaxByteCount;
367367
sigset_t signals;
368368

369-
MaxByteCount = 2 * (0xFFFF & args[0]); /* words to bytes */
369+
MaxByteCount = BYTERSPER_DLWORD * (0xFFFF & args[0]); /* words to bytes */
370370

371371
DBPRINT(("Ether Get. "));
372372

@@ -408,7 +408,7 @@ LispPTR ether_send(LispPTR args[])
408408
LispPTR MaxByteCount;
409409
u_char *BufferAddr; /* buffer address pointer(in native address) */
410410

411-
MaxByteCount = 2 * (0xFFFF & args[0]); /* words to bytes */
411+
MaxByteCount = BYTESPER_DLWORD * (0xFFFF & args[0]); /* words to bytes */
412412
BufferAddr = (u_char *)NativeAligned2FromLAddr(args[1]);
413413

414414
if (ether_fd > 0) {

src/kbdsubrs.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include "kbdsubrsdefs.h"
2929
#include "commondefs.h"
3030
#ifdef XWINDOW
31-
#include "lisp2cdefs.h"
3231
#include "xwinmandefs.h"
3332
#endif
3433

src/kprint.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ void prindatum(LispPTR x) {
9898
break;
9999
case TYPE_ONED_ARRAY:
100100
case TYPE_GENERAL_ARRAY:
101+
/* this should probably use array.h's arrayheader */
101102
newstring = (NEWSTRINGP *)NativeAligned4FromLAddr(x);
102103
if (newstring->stringp) {
103104
print_NEWstring(x);
104105
}
106+
/* it would be useful to print non-string arrays, too */
105107
break;
106108
default: dtd_base = (struct dtd *)GetDTD(typen); printf("{");
107109
#ifdef BIGVM
@@ -173,14 +175,20 @@ void print_string(LispPTR x) {
173175
void print_NEWstring(LispPTR x) {
174176
NEWSTRINGP *string_point;
175177
DLword st_length;
178+
DLword st_offset;
176179
DLbyte *string_base;
177180

178181
int i;
179182

180183
string_point = (NEWSTRINGP *)NativeAligned4FromLAddr(x);
181184
st_length = string_point->fillpointer;
185+
st_offset = string_point->offset;
186+
if (string_point->indirectp) {
187+
/* base points to another array header not the raw storage */
188+
string_point = (NEWSTRINGP *)NativeAligned4FromLAddr(string_point->base);
189+
}
182190
string_base = (DLbyte *)NativeAligned2FromLAddr(string_point->base);
183-
string_base += string_point->offset;
191+
string_base += st_offset;
184192

185193
printf("%c", DOUBLEQUOTE); /* print %" */
186194

src/ldsout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ unsigned sysout_loader(const char *sysout_file_name, unsigned sys_size) {
216216
}
217217

218218
if ((stat_buf.st_size & (BYTESPER_PAGE - 1)) != 0)
219-
printf("CAUTION::not an integral number of pages. sysout & 0x1ff = 0x%x\n",
220-
(int)(stat_buf.st_size & (BYTESPER_PAGE - 1)));
219+
printf("CAUTION::not an integral number of pages. sysout & 0x%x = 0x%x\n",
220+
BYTESPER_PAGE - 1, (int)(stat_buf.st_size & (BYTESPER_PAGE - 1)));
221221

222222
if (ifpage.nactivepages != (sysout_size / 2)) {
223223
printf("sysout_loader:IFPAGE says sysout size is %d\n", ifpage.nactivepages);

0 commit comments

Comments
 (0)