aboutsummaryrefslogtreecommitdiffstats
path: root/src/termcap.c
diff options
context:
space:
mode:
authorEli Zaretskii2013-09-05 11:01:04 +0300
committerEli Zaretskii2013-09-05 11:01:04 +0300
commit41306318777a942420bc4feadbfacf662ea179dc (patch)
tree669e5cca02f95d6064ce73c0d3fbbf91b8c8b563 /src/termcap.c
parent141f1ff7a40cda10f0558e891dd196a943a5082e (diff)
parent257b3b03cb1cff917e0b3b7832ad3eab5b59f257 (diff)
downloademacs-41306318777a942420bc4feadbfacf662ea179dc.tar.gz
emacs-41306318777a942420bc4feadbfacf662ea179dc.zip
Merge from trunk after a lot of time.
Diffstat (limited to 'src/termcap.c')
-rw-r--r--src/termcap.c141
1 files changed, 40 insertions, 101 deletions
diff --git a/src/termcap.c b/src/termcap.c
index 61f9d9a31ea..aa225d9b3b1 100644
--- a/src/termcap.c
+++ b/src/termcap.c
@@ -1,6 +1,6 @@
1/* Work-alike for termcap, plus extra features. 1/* Work-alike for termcap, plus extra features.
2 Copyright (C) 1985, 1986, 1993, 1994, 1995, 2000, 2001, 2002, 2003, 2 Copyright (C) 1985-1986, 1993-1995, 2000-2008, 2011, 2013 Free
3 2004, 2005, 2006, 2007, 2008, 2011 Free Software Foundation, Inc. 3 Software Foundation, Inc.
4 4
5This program is free software; you can redistribute it and/or modify 5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by 6it under the terms of the GNU General Public License as published by
@@ -13,13 +13,10 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details. 13GNU General Public License for more details.
14 14
15You should have received a copy of the GNU General Public License 15You should have received a copy of the GNU General Public License
16along with this program; see the file COPYING. If not, write to 16along with this program. If not, see <http://www.gnu.org/licenses/>. */
17the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18Boston, MA 02110-1301, USA. */
19 17
20/* Emacs config.h may rename various library functions such as malloc. */ 18/* Emacs config.h may rename various library functions such as malloc. */
21#include <config.h> 19#include <config.h>
22#include <setjmp.h>
23#include <sys/file.h> 20#include <sys/file.h>
24#include <fcntl.h> 21#include <fcntl.h>
25#include <unistd.h> 22#include <unistd.h>
@@ -153,7 +150,7 @@ tgetst1 (char *ptr, char **area)
153 p = ptr; 150 p = ptr;
154 while ((c = *p++) && c != ':' && c != '\n') 151 while ((c = *p++) && c != ':' && c != '\n')
155 ; 152 ;
156 ret = (char *) xmalloc (p - ptr + 1); 153 ret = xmalloc (p - ptr + 1);
157 } 154 }
158 else 155 else
159 ret = *area; 156 ret = *area;
@@ -216,8 +213,8 @@ tgetst1 (char *ptr, char **area)
216 abbreviation expansion makes that effort a little more hairy than 213 abbreviation expansion makes that effort a little more hairy than
217 its worth; this is cleaner. */ 214 its worth; this is cleaner. */
218 { 215 {
219 register int last_p_param = 0; 216 int last_p_param = 0;
220 int remove_p_params = 1; 217 bool remove_p_params = 1;
221 struct { char *beg; int len; } cut[11]; 218 struct { char *beg; int len; } cut[11];
222 219
223 for (cut[0].beg = p = ret; p < r - 3; p++) 220 for (cut[0].beg = p = ret; p < r - 3; p++)
@@ -321,26 +318,26 @@ struct termcap_buffer
321 char *beg; 318 char *beg;
322 ptrdiff_t size; 319 ptrdiff_t size;
323 char *ptr; 320 char *ptr;
324 int ateof; 321 bool ateof;
325 ptrdiff_t full; 322 ptrdiff_t full;
326 }; 323 };
327 324
328/* Forward declarations of static functions. */ 325/* Forward declarations of static functions. */
329 326
330static int scan_file (char *str, int fd, register struct termcap_buffer *bufp); 327static bool scan_file (char *, int, struct termcap_buffer *);
331static char *gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end); 328static char *gobble_line (int, struct termcap_buffer *, char *);
332static int compare_contin (register char *str1, register char *str2); 329static bool compare_contin (char *, char *);
333static int name_match (char *line, char *name); 330static bool name_match (char *, char *);
334 331
335#ifdef MSDOS /* MW, May 1993 */ 332static bool
336static int
337valid_filename_p (char *fn) 333valid_filename_p (char *fn)
338{ 334{
335#ifdef MSDOS
339 return *fn == '/' || fn[1] == ':'; 336 return *fn == '/' || fn[1] == ':';
340}
341#else 337#else
342#define valid_filename_p(fn) (*(fn) == '/') 338 return *fn == '/';
343#endif 339#endif
340}
344 341
345/* Find the termcap entry data for terminal type NAME 342/* Find the termcap entry data for terminal type NAME
346 and store it in the block that BP points to. 343 and store it in the block that BP points to.
@@ -363,10 +360,10 @@ tgetent (char *bp, const char *name)
363 char *tc_search_point; 360 char *tc_search_point;
364 char *term; 361 char *term;
365 ptrdiff_t malloc_size = 0; 362 ptrdiff_t malloc_size = 0;
366 register int c; 363 int c;
367 char *tcenv = NULL; /* TERMCAP value, if it contains :tc=. */ 364 char *tcenv = NULL; /* TERMCAP value, if it contains :tc=. */
368 char *indirect = NULL; /* Terminal type in :tc= in TERMCAP value. */ 365 char *indirect = NULL; /* Terminal type in :tc= in TERMCAP value. */
369 int filep; 366 bool filep;
370 367
371#ifdef INTERNAL_TERMINAL 368#ifdef INTERNAL_TERMINAL
372 /* For the internal terminal we don't want to read any termcap file, 369 /* For the internal terminal we don't want to read any termcap file,
@@ -377,7 +374,7 @@ tgetent (char *bp, const char *name)
377 if (!bp) 374 if (!bp)
378 { 375 {
379 malloc_size = 1 + strlen (term); 376 malloc_size = 1 + strlen (term);
380 bp = (char *) xmalloc (malloc_size); 377 bp = xmalloc (malloc_size);
381 } 378 }
382 strcpy (bp, term); 379 strcpy (bp, term);
383 goto ret; 380 goto ret;
@@ -396,7 +393,7 @@ tgetent (char *bp, const char *name)
396 if (termcap_name && (*termcap_name == '\\' 393 if (termcap_name && (*termcap_name == '\\'
397 || *termcap_name == '/' 394 || *termcap_name == '/'
398 || termcap_name[1] == ':')) 395 || termcap_name[1] == ':'))
399 dostounix_filename (termcap_name); 396 dostounix_filename (termcap_name, 0);
400#endif 397#endif
401 398
402 filep = termcap_name && valid_filename_p (termcap_name); 399 filep = termcap_name && valid_filename_p (termcap_name);
@@ -409,7 +406,7 @@ tgetent (char *bp, const char *name)
409 406
410 if (termcap_name && !filep && !strcmp (name, getenv ("TERM"))) 407 if (termcap_name && !filep && !strcmp (name, getenv ("TERM")))
411 { 408 {
412 indirect = tgetst1 (find_capability (termcap_name, "tc"), (char **) 0); 409 indirect = tgetst1 (find_capability (termcap_name, "tc"), 0);
413 if (!indirect) 410 if (!indirect)
414 { 411 {
415 if (!bp) 412 if (!bp)
@@ -430,23 +427,19 @@ tgetent (char *bp, const char *name)
430 427
431 /* Here we know we must search a file and termcap_name has its name. */ 428 /* Here we know we must search a file and termcap_name has its name. */
432 429
433#ifdef MSDOS 430 fd = emacs_open (termcap_name, O_RDONLY | O_TEXT, 0);
434 fd = open (termcap_name, O_RDONLY|O_TEXT, 0);
435#else
436 fd = open (termcap_name, O_RDONLY, 0);
437#endif
438 if (fd < 0) 431 if (fd < 0)
439 return -1; 432 return -1;
440 433
441 buf.size = BUFSIZE; 434 buf.size = BUFSIZE;
442 /* Add 1 to size to ensure room for terminating null. */ 435 /* Add 1 to size to ensure room for terminating null. */
443 buf.beg = (char *) xmalloc (buf.size + 1); 436 buf.beg = xmalloc (buf.size + 1);
444 term = indirect ? indirect : (char *)name; 437 term = indirect ? indirect : (char *)name;
445 438
446 if (!bp) 439 if (!bp)
447 { 440 {
448 malloc_size = indirect ? strlen (tcenv) + 1 : buf.size; 441 malloc_size = indirect ? strlen (tcenv) + 1 : buf.size;
449 bp = (char *) xmalloc (malloc_size); 442 bp = xmalloc (malloc_size);
450 } 443 }
451 tc_search_point = bp1 = bp; 444 tc_search_point = bp1 = bp;
452 445
@@ -462,7 +455,7 @@ tgetent (char *bp, const char *name)
462 /* Scan the file, reading it via buf, till find start of main entry. */ 455 /* Scan the file, reading it via buf, till find start of main entry. */
463 if (scan_file (term, fd, &buf) == 0) 456 if (scan_file (term, fd, &buf) == 0)
464 { 457 {
465 close (fd); 458 emacs_close (fd);
466 xfree (buf.beg); 459 xfree (buf.beg);
467 if (malloc_size) 460 if (malloc_size)
468 xfree (bp); 461 xfree (bp);
@@ -478,7 +471,7 @@ tgetent (char *bp, const char *name)
478 { 471 {
479 ptrdiff_t offset1 = bp1 - bp, offset2 = tc_search_point - bp; 472 ptrdiff_t offset1 = bp1 - bp, offset2 = tc_search_point - bp;
480 malloc_size = offset1 + buf.size; 473 malloc_size = offset1 + buf.size;
481 bp = termcap_name = (char *) xrealloc (bp, malloc_size); 474 bp = termcap_name = xrealloc (bp, malloc_size);
482 bp1 = termcap_name + offset1; 475 bp1 = termcap_name + offset1;
483 tc_search_point = termcap_name + offset2; 476 tc_search_point = termcap_name + offset2;
484 } 477 }
@@ -497,14 +490,14 @@ tgetent (char *bp, const char *name)
497 /* Does this entry refer to another terminal type's entry? 490 /* Does this entry refer to another terminal type's entry?
498 If something is found, copy it into heap and null-terminate it. */ 491 If something is found, copy it into heap and null-terminate it. */
499 tc_search_point = find_capability (tc_search_point, "tc"); 492 tc_search_point = find_capability (tc_search_point, "tc");
500 term = tgetst1 (tc_search_point, (char **) 0); 493 term = tgetst1 (tc_search_point, 0);
501 } 494 }
502 495
503 close (fd); 496 emacs_close (fd);
504 xfree (buf.beg); 497 xfree (buf.beg);
505 498
506 if (malloc_size) 499 if (malloc_size)
507 bp = (char *) xrealloc (bp, bp1 - bp + 1); 500 bp = xrealloc (bp, bp1 - bp + 1);
508 501
509 ret: 502 ret:
510 term_entry = bp; 503 term_entry = bp;
@@ -517,10 +510,10 @@ tgetent (char *bp, const char *name)
517 Return 1 if successful, with that line in BUFP, 510 Return 1 if successful, with that line in BUFP,
518 or 0 if no entry is found in the file. */ 511 or 0 if no entry is found in the file. */
519 512
520static int 513static bool
521scan_file (char *str, int fd, register struct termcap_buffer *bufp) 514scan_file (char *str, int fd, struct termcap_buffer *bufp)
522{ 515{
523 register char *end; 516 char *end;
524 517
525 bufp->ptr = bufp->beg; 518 bufp->ptr = bufp->beg;
526 bufp->full = 0; 519 bufp->full = 0;
@@ -551,13 +544,13 @@ scan_file (char *str, int fd, register struct termcap_buffer *bufp)
551 return 0; 544 return 0;
552} 545}
553 546
554/* Return nonzero if NAME is one of the names specified 547/* Return true if NAME is one of the names specified
555 by termcap entry LINE. */ 548 by termcap entry LINE. */
556 549
557static int 550static bool
558name_match (char *line, char *name) 551name_match (char *line, char *name)
559{ 552{
560 register char *tem; 553 char *tem;
561 554
562 if (!compare_contin (line, name)) 555 if (!compare_contin (line, name))
563 return 1; 556 return 1;
@@ -569,18 +562,18 @@ name_match (char *line, char *name)
569 return 0; 562 return 0;
570} 563}
571 564
572static int 565static bool
573compare_contin (register char *str1, register char *str2) 566compare_contin (char *str1, char *str2)
574{ 567{
575 register int c1, c2;
576 while (1) 568 while (1)
577 { 569 {
578 c1 = *str1++; 570 int c1 = *str1++;
579 c2 = *str2++; 571 int c2 = *str2++;
580 while (c1 == '\\' && *str1 == '\n') 572 while (c1 == '\\' && *str1 == '\n')
581 { 573 {
582 str1++; 574 str1++;
583 while ((c1 = *str1++) == ' ' || c1 == '\t'); 575 while ((c1 = *str1++) == ' ' || c1 == '\t')
576 continue;
584 } 577 }
585 if (c2 == '\0') 578 if (c2 == '\0')
586 { 579 {
@@ -654,57 +647,3 @@ gobble_line (int fd, register struct termcap_buffer *bufp, char *append_end)
654 } 647 }
655 return end + 1; 648 return end + 1;
656} 649}
657
658#ifdef TEST
659
660#include <stdio.h>
661
662static void
663tprint (char *cap)
664{
665 char *x = tgetstr (cap, 0);
666 register char *y;
667
668 printf ("%s: ", cap);
669 if (x)
670 {
671 for (y = x; *y; y++)
672 if (*y <= ' ' || *y == 0177)
673 printf ("\\%0o", *y);
674 else
675 putchar (*y);
676 free (x);
677 }
678 else
679 printf ("none");
680 putchar ('\n');
681}
682
683int
684main (int argc, char **argv)
685{
686 char *term;
687 char *buf;
688
689 term = argv[1];
690 printf ("TERM: %s\n", term);
691
692 buf = (char *) tgetent (0, term);
693 if ((int) buf <= 0)
694 {
695 printf ("No entry.\n");
696 return 0;
697 }
698
699 printf ("Entry: %s\n", buf);
700
701 tprint ("cm");
702 tprint ("AL");
703
704 printf ("co: %d\n", tgetnum ("co"));
705 printf ("am: %d\n", tgetflag ("am"));
706
707 return 0;
708}
709
710#endif /* TEST */