1 /* xstring.c */
2 /*****************************************************************************
3 �ԣ�������У�������أԣ�
4
5 �ΣģϣӰ�¸�ɣϴ�Ϣ��
6 *****************************************************************************/
7
8 #include "xtr.h"
9
10 #if UNIX
11 # if !__human68k__
12 # if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
13 # include <termios.h>
14 # else
15 # if HAVE_TERMIO_H
16 # include <termio.h>
17 # else
18 # include <sgtty.h>
19 # if HAVE_SYS_IOCTL_H
20 # include <sys/ioctl.h>
21 # endif
22 # endif
23 # endif
24 # if HAVE_SYS_SELECT_H
25 # include <sys/select.h>
26 # endif
27 # endif
28 #endif /* UNIX */
29
30 #if __human68k__
31 #define __DOS_INLINE__
32 #include <sys/dos.h>
33 #endif
34
35 #if UNIX
36 #ifndef __human68k__
37 static int close_tty = 0;
38 #endif
39 #endif
40
41 static RETSIGTYPE CDECL
CBreakHandler(int sig)42 CBreakHandler(int sig)
43 {
44 #if UNIX
45 #if !__human68k__
46 if (RawMode(-1))
47 RawMode(OFF);
48 #endif
49 #endif
50 assert(sig == SIGINT);
51 signal(sig, SIG_DFL);
52 FError("USER ABORT");
53 }
54
55 static int cbrk_save;
56
57 static int
GetCBrk(void)58 GetCBrk(void)
59 /* ����ȥ���֥졼�������å���������֤�������� */
60 {
61 #if MSDOS
62 union REGS regs;
63 regs.x.ax = 0x3300;
64 intdos(®s, ®s);
65 return regs.h.dl;
66 #else
67 #if __human68k__
68 return _dos_breakck(-1);
69 #else
70 return 0;
71 #endif
72 #endif
73 }
74
75 static int
SetCBrk(int cval)76 SetCBrk(int cval)
77 /* ����ȥ���֥졼�������å��åȤ��� */
78 {
79 #if MSDOS
80 union REGS regs;
81 regs.x.ax = 0x3301;
82 regs.h.dl = cval;
83 intdos(®s, ®s);
84 return regs.h.dl;
85 #else
86 #if __human68k__
87 return _dos_breakck(cval);
88 #else
89 return 0;
90 #endif
91 #endif
92 }
93
94 void
InitCBreak(void)95 InitCBreak(void)
96 /* Ctrl-C �����߽���� */
97 {
98
99 signal(SIGINT, CBreakHandler); /* Ctrl-C �����ߥϥ�ɥ���Ͽ */
100
101 cbrk_save = GetCBrk();
102 SetCBrk(1); /* Enable Ctrl-C */
103 }
104
105 void
TermCBreak(void)106 TermCBreak(void)
107 {
108 SetCBrk(cbrk_save);
109 }
110
111 /**************/
112
113 void
ChgStdin(void)114 ChgStdin(void)
115 /* stdin ��������쥯�Ȥ���Ƥ����硢
116 * ������쥯�����ϤΥե�����ݥ��� stdin ���� ifp ���Ѥ��ơ�
117 * ���������Ϥ� stdin �ˤ���
118 */
119 {
120 #if MSDOS || WINNT || __human68k__
121 #define TTY "CON"
122 #else
123 #define TTY "/dev/tty"
124 #endif
125
126 int fh;
127
128 if ((fh = dup(fileno(stdin))) == -1) {
129 perror("dup");
130 FError("");
131 }
132 if ((ifp = fdopen(fh, "r")) == NULL) {
133 perror("fdopen");
134 FError("");
135 }
136 SetInMode(ifp);
137 if (freopen(TTY, "r", stdin) == NULL) {
138 #if MSDOS || WINNT || __human68k__
139 perror("freopen");
140 FError("");
141 #else
142 close_tty = 1;
143 #endif
144 }
145 #ifdef UNIX
146 setbuf(stdin, NULL);
147 #endif
148 }
149
150 void
SetModeB(FILE * fp,int binflag)151 SetModeB(FILE *fp, int binflag)
152 {
153 #if MSDOS || WINNT
154 setmode(fileno(fp), (binflag ? O_BINARY : O_TEXT));
155 #ifdef __TURBOC__
156 if (binflag)
157 fp->flags |= _F_BIN;
158 else
159 fp->flags &= ~_F_BIN;
160 #endif
161 #else
162 #if __human68k__
163 fmode(fp, binflag ? _IOBIN : _IOTEXT);
164 #endif
165 #endif
166 }
167
168 void
SetFModeB(int binflag)169 SetFModeB(int binflag)
170 {
171 #if MSDOS || WINNT || __human68k__
172 _fmode = (binflag ? O_BINARY : O_TEXT);
173 #endif
174 }
175
176 void
SetInMode(FILE * fp)177 SetInMode(FILE *fp)
178 /* ���Ϥ������뤫�����å� */
179 {
180 #if __human68k__
181 fmode(fp, _fmode == O_BINARY ? _IOBIN : _IOTEXT);
182 #endif
183 #if MSDOS || WINNT || UNIX
184 coninmode = isatty(fileno(fp));
185 #endif
186 }
187
188
189 void
SetOutMode(FILE * fp)190 SetOutMode(FILE *fp)
191 /* ���Ϥ������뤫�ץ�������å� */
192 {
193 #if MSDOS
194 int fn = fileno(fp);
195 union REGS regs;
196
197 regs.x.bx = fn;
198 regs.x.ax = 0x4400; /* Get IOCTL Data */
199 intdos(®s, ®s);
200 assert((isatty(fn) == 0) == ((regs.x.dx & 0x80) == 0));
201 if ((regs.x.dx & 0x80) == 0) {
202 /* �ǥХ����ǤϤʤ� */
203 conoutmode = prnoutmode = OFF;
204 } else if (regs.x.dx & 0x01) {
205 /* ��������ϤǤ��� */
206 conoutmode = ON;
207 prnoutmode = OFF;
208 } else {
209 /* �����Ǥʤ���Хץ���Ϥ��� */
210 regs.h.dh = 0;
211 regs.h.dl |= 0x20; /* raw mode */
212 regs.x.bx = fn;
213 regs.x.ax = 0x4401; /* Set IOCTL Data */
214 intdos(®s, ®s);
215 conoutmode = OFF;
216 prnoutmode = ON;
217 }
218 #else
219 #if WINNT
220 conoutmode = isatty(fileno(fp));
221 #else
222 #if UNIX
223 conoutmode = isatty(fileno(fp));
224 prnoutmode = OFF;
225 #endif /* UNIX */
226 #endif /* !WINNT */
227 #endif /* !MSDOS */
228 }
229
230 #if UNIX
231 #if !__human68k__
232
233 #ifndef FD_SET
234 #define fd_set long
235 #define FD_SET(n,p) (*(p) |= (1 << (n)))
236 #define FD_ZERO(p) (*(p) = 0)
237 #endif
238
239 #if !HAVE_FLUSHALL
240 int
flushall()241 flushall()
242 {
243 fflush(stdout);
244 fflush(stderr);
245 if (ofp != NULL)
246 fflush(ofp);
247
248 return 0;
249 }
250 #endif
251
252 #if !HAVE_SELECT
253 int
select(int nfds,fd_set * rfds,fd_set * wfds,fd_set * efds,struct timeval * timeout)254 select(int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *timeout)
255 {
256 return 1;
257 }
258 #endif
259
260 int
kbhit()261 kbhit()
262 {
263 struct timeval zero;
264 fd_set fds;
265
266 zero.tv_sec = 0;
267 zero.tv_usec = 0;
268 FD_ZERO(&fds);
269 FD_SET(fileno(stdin), &fds);
270 return select(fileno(stdin) + 1, &fds, (fd_set *)NULL, (fd_set *)NULL, &zero) <= 0
271 ? 0 : 1 ;
272 }
273
274 int
getch()275 getch()
276 {
277 uchar c;
278
279 flushall();
280 while (read(fileno(stdin), &c, 1) != 1)
281 ;
282 return c;
283 }
284
285 int
RawMode(int flag)286 RawMode(int flag)
287 {
288 static int raw_mode = 0;
289
290 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
291 struct termios s;
292 static struct termios os;
293 #endif
294
295 #if HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS
296 if (close_tty)
297 FError("There is no control-terminal.");
298
299 if (flag > 0) {
300 tcgetattr(fileno(stdin), &s);
301 memcpy(&os, &s, sizeof(struct termios));
302 s.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
303 s.c_cc[VMIN] = 1;
304 s.c_cc[VTIME] = 0;
305 raw_mode = 1;
306 } else if (flag == 0) {
307 memcpy(&s, &os, sizeof(struct termios));
308 raw_mode = 0;
309 }
310 tcsetattr(fileno(stdin), TCSADRAIN, &s);
311 #else /* !(HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) */
312 #if HAVE_TERMIO_H
313 struct termio s;
314 static struct termio os;
315
316 if (flag > 0) {
317 ioctl(fileno(stdin), TCGETA, &s);
318 memcpy(&os, &s, sizeof(struct termio));
319 s.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
320 s.c_cc[VMIN] = 1;
321 s.c_cc[VTIME] = 0;
322 raw_mode = 1;
323 } else if (flag == 0) {
324 memcpy(&s, &os, sizeof(struct timeio));
325 raw_mode = 0;
326 }
327 tcsetattr(fileno(stdin), TCSETAW, &s);
328 #else /* !HAVE_TERMIO_H */
329 struct sgttyb s;
330 static struct sgttyb os;
331
332 if (flag > 0) {
333 ioctl(fileno(stdin), TIOCGETP, &s);
334 memcpy(&os, &s, sizeof(struct sgttyb));
335 s.sg_flags |= CBREAK;
336 s.sg_flags &= ~ECHO;
337 raw_mode = 1;
338 } else if (flag == 0) {
339 memcpy(&s, &os, sizeof(struct sgttyb));
340 raw_mode = 0;
341 }
342 ioctl(fileno(stdin), TIOCSETN, &s);
343 #endif /* !HAVE_TERMIO_H */
344 #endif /* !(HAVE_TERMIOS_H && HAVE_TERMIOS_FUNCS) */
345
346 return raw_mode;
347 }
348 #endif /* !__human68k__ */
349 #endif /* UNIX */
350
351 /********** �ե�����̾�˴ؤ������ ******************************/
352
353
354 int
IsFileName(const uchar * s)355 IsFileName(const uchar *s)
356 /* ���ʤ顢ʸ���� s �ϥե�����̾�λ���Ǥ��뤫�⤷��ʤ� */
357 {
358 #ifdef UNIX
359 return *s == DIRSLASH ? *(s+1) != '\0' :
360 *s == '.' ||
361 isalnum(*s) ||
362 (*s && strchr("!#$%&()+,.^_`{}~", *s) != NULL);
363 #else
364 return (*s == DIRSLASH || *s == ALTDIRSLASH) ? *(s+1) != '\0' :
365 *s == '.' ||
366 *s >= 0x80 ||
367 isalnum(*s) ||
368 (*s && strchr("$&#%'_^(){}~!", *s) != NULL);
369 /* ��DOS�Dz�ǽ�ʥե�����̾���� '-' �� '@' �Ͻ����Ƥ��� */
370 #endif
371 }
372
373 int
FileExists(const uchar * fname)374 FileExists(const uchar *fname)
375 /* �ե����뤬¸�ߤ��뤫Ĵ�٤� */
376 {
377 #ifndef F_OK
378 #define F_OK 0
379 #endif
380 #if UNIX
381 return fname[0] != '\0' && access((const char *)fname, F_OK) == 0;
382 #else
383 return access(fname, F_OK) == 0;
384 #endif
385 }
386
387 uchar *
FNameExt(const uchar * fname)388 FNameExt(const uchar *fname)
389 /* �ե�����̾�˳�ĥ�Ҥ����뤫Ĵ�١����ä��餽�� '.' �ؤΥݥ����֤� */
390 /* �ʤ���С�NULL ���֤� */
391 {
392 const uchar *p;
393
394 p = (const uchar *)strrchr((const char *)fname, '.');
395 #if MSDOS || WINNT || __human68k__
396 if (p && (jstrchr(p, DIRSLASH) || jstrchr(p, ALTDIRSLASH)))
397 #else
398 if (p && strchr((const char *)p, DIRSLASH))
399 #endif
400 p = NULL;
401 return (uchar*)p;
402 }
403
404 uchar *
FNameName(const uchar * fname)405 FNameName(const uchar *fname)
406 /* �ǥ��쥯�ȥ��դ��Υѥ�̾������ե�����̾�Τߤ��֤� */
407 {
408 #if MSDOS || WINNT || __human68k__
409 const uchar *p;
410 const uchar *p2;
411
412 p = (fname[0] && fname[1] == ':') ? fname+2 : fname;
413 p2 = jstrrchr(p, DIRSLASH);
414 if (p2) p = p2 + 1;
415 p2 = jstrrchr(p, ALTDIRSLASH);
416 if (p2) p = p2 + 1;
417 return (uchar*)p;
418 #else
419 const uchar *p;
420
421 p = (const uchar *)strrchr((const char *)fname, DIRSLASH);
422 return (uchar *)(p != NULL ? p + 1 : fname);
423 #endif
424 }
425
426 int
FNameHasPath(const uchar * fname)427 FNameHasPath(const uchar *fname)
428 /* �ե�����̾���ѥ��դ���Ĵ�٤� */
429 {
430 #if MSDOS || WINNT || __human68k__
431 return fname[0] == '.' ||
432 fname[0] == DIRSLASH ||
433 fname[0] == ALTDIRSLASH ||
434 fname[0] && fname[1] == ':';
435 #else
436 return fname[0] == '.' || fname[0] == DIRSLASH;
437 #endif
438 }
439
440 uchar *
PathCat(uchar * path,const uchar * fname)441 PathCat(uchar *path, const uchar *fname)
442 /* �ѥ��˥ե�����̾��Ĥʤ��� */
443 {
444 RTrimSpace(path);
445
446 if (!*path) {
447 return (uchar *)strcpy((char *)path, (const char *)fname);
448 } else {
449 int c = *StrLast(path);
450 #if MSDOS || WINNT || __human68k__
451 if (c != DIRSLASH && c != ALTDIRSLASH)
452 #else
453 if (c != DIRSLASH)
454 #endif
455 strcat((char *)path, DIRSLASHS);
456 return (uchar *)strcat((char *)path, (const char *)fname);
457 }
458 }
459
460 uchar *
PathFName(const uchar * path,const uchar * fname)461 PathFName(const uchar *path, const uchar *fname)
462 #if 0 /* �ܥ� */
463 /* �ե����� fname ��ѥ�̾�ꥹ�� path �Υǥ��쥯�ȥ�Ǹ�����
464 * ���Ĥ��ä��顢���Υѥ��դ��ե�����̾���Ĥ���ʤ���� NULL ���֤�
465 */
466 {
467 uchar *paths;
468 uchar *p;
469 int fnamelen = strlen(fname);
470 uchar *pfname = NULL;
471
472 paths = DupStr(pathlist);
473
474 for (p = strtok(paths, PATH_DELIMITS);
475 p && !pfname;
476 p = strtok((char *)NULL, PATH_DELIMITS)) {
477 pfname = (uchar *)XMalloc(strlen(p) + fnamelen + 16);
478 strcpy(pfname, p);
479 PathCat(pfname, fname);
480 if (!FileExists(pfname)) {
481 XFree((voidstar)pfname);
482 pfname = NULL;
483 }
484 }
485 XFree((voidstar)paths);
486 return (uchar *)XRealloc((voidstar)pfname, strlen(pfname) + 1);
487 }
488 #else
489 #if UNIX
490 {
491 #if __human68k__
492 #define ISCOLON(c) ((c) == ':' || (c) == ';')
493 #define DRIVEPASS(p) (isalpha((p)[0]) && (p)[1] == ':' ? (p) + 2 : (p))
494 #else
495 #define ISCOLON(c) ((c) == ':')
496 #define DRIVEPASS(p) (p)
497 #endif
498 const uchar *p, *end;
499 uchar *fullpath;
500 int fnamelen, len;
501
502 fnamelen = strlen((const char *)fname);
503 p = path;
504 for (;;) {
505 while (ISCOLON(*p))
506 p++;
507 if (*p == '\0')
508 return NULL;
509 end = DRIVEPASS(p);
510 while (!ISCOLON(*end) && *end != '\0')
511 end++;
512 len = end - p;
513 fullpath = (uchar *)XMalloc(len + fnamelen + 2);
514 memcpy(fullpath, p, len);
515 fullpath[len] = '\0';
516 PathCat(fullpath, fname);
517 if (FileExists(fullpath))
518 return fullpath;
519 XFree((voidstar)fullpath);
520 p = end;
521 }
522 }
523 #else /* !UNIX */
524 /* path �Υǥ��쥯�ȥ�ϣ��Ĥ��� */
525 {
526 uchar *pfname = (uchar *)XMalloc(strlen(path) + strlen(fname) + 16);
527
528 strcpy(pfname, path);
529 PathCat(pfname, fname);
530 return (uchar *)XRealloc((voidstar)pfname, strlen(pfname) + 1);
531 }
532 #endif /* !UNIX */
533 #endif
534
535 uchar *
ChgExt(const uchar * fnam,const uchar * fext)536 ChgExt(const uchar *fnam, const uchar *fext)
537 /* ��ĥ�Ҥ��դ��ؤ��� */
538 {
539 const uchar *p;
540 uchar *fname;
541 int dotpos;
542
543 if (*fext == '.')
544 fext++;
545
546 p = FNameExt(fnam);
547 dotpos = p ? p - fnam : strlen((const char *)fnam);
548
549 fname = (uchar *)XMalloc(dotpos + strlen((const char *)fext) + 16);
550
551 memcpy(fname, fnam, dotpos);
552 fname[dotpos] = '.';
553 strcpy((char *)fname + dotpos + 1, (const char *)fext);
554 return (uchar *)XRealloc((voidstar)fname, strlen((const char *)fname) + 1);
555 }
556
557 int
SameFile(const uchar * fnam1,const uchar * fnam2)558 SameFile(const uchar *fnam1, const uchar *fnam2)
559 {
560 #if UNIX
561 #if __human68k__
562 return (*STRCMP)(fnam1, fnam2) == 0;
563 #else
564 return (strcmp((const char *)fnam1, (const char *)fnam2) == 0);
565 #endif
566 #else
567 return (stricmp(fnam1, fnam2) == 0);
568 #endif
569 }
570
571 /*
572 * Local variables:
573 * mode: c
574 * c-indent-level: 4
575 * c-continued-statement-offset: 4
576 * c-brace-offset: -4
577 * c-argdecl-indent: 4
578 * c-label-offset: -4
579 * tab-width: 4
580 * tab-stop-list: (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80)
581 * End:
582 */
583