misc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. *
  3. * General purpose routines.
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <ctype.h>
  8. #include <sys/types.h>
  9. #include <fcntl.h>
  10. #include "gen.h"
  11. #include "ext.h"
  12. #include "path.h"
  13. int nolist = 0; /* number of specified ranges */
  14. int olist[50]; /* processing range pairs */
  15. /*****************************************************************************/
  16. out_list(str)
  17. char *str;
  18. {
  19. int start, stop;
  20. /*
  21. *
  22. * Grab page ranges from str, save them in olist[], and update the nolist
  23. * count. Range syntax matches nroff/troff syntax.
  24. *
  25. */
  26. while ( *str && nolist < sizeof(olist) - 2 ) {
  27. start = stop = str_convert(&str, 0);
  28. if ( *str == '-' && *str++ )
  29. stop = str_convert(&str, 9999);
  30. if ( start > stop )
  31. error(FATAL, "illegal range %d-%d", start, stop);
  32. olist[nolist++] = start;
  33. olist[nolist++] = stop;
  34. if ( *str != '\0' ) str++;
  35. } /* End while */
  36. olist[nolist] = 0;
  37. } /* End of out_list */
  38. /*****************************************************************************/
  39. in_olist(num)
  40. int num;
  41. {
  42. int i;
  43. /*
  44. *
  45. * Return ON if num is in the current page range list. Print everything if
  46. * there's no list.
  47. *
  48. */
  49. if ( nolist == 0 )
  50. return(ON);
  51. for ( i = 0; i < nolist; i += 2 )
  52. if ( num >= olist[i] && num <= olist[i+1] )
  53. return(ON);
  54. return(OFF);
  55. } /* End of in_olist */
  56. /*****************************************************************************/
  57. setencoding(name)
  58. char *name;
  59. {
  60. char path[150];
  61. /*
  62. *
  63. * Include the font encoding file selected by name. It's a full pathname if
  64. * it begins with /, otherwise append suffix ".enc" and look for the file in
  65. * ENCODINGDIR. Missing files are silently ignored.
  66. *
  67. */
  68. if ( name == NULL )
  69. name = "Default";
  70. if ( *name == '/' )
  71. strcpy(path, name);
  72. else sprintf(path, "%s/%s.enc", ENCODINGDIR, name);
  73. if ( cat(path) == TRUE )
  74. writing = strncmp(name, "UTF", 3) == 0;
  75. } /* End of setencoding */
  76. /*****************************************************************************/
  77. cat(file)
  78. char *file;
  79. {
  80. int fd_in;
  81. int fd_out;
  82. char buf[512];
  83. int count;
  84. /*
  85. *
  86. * Copy *file to stdout. Return FALSE is there was a problem.
  87. *
  88. */
  89. fflush(stdout);
  90. if ( (fd_in = open(file, O_RDONLY)) == -1 )
  91. return(FALSE);
  92. fd_out = fileno(stdout);
  93. while ( (count = read(fd_in, buf, sizeof(buf))) > 0 )
  94. write(fd_out, buf, count);
  95. close(fd_in);
  96. return(TRUE);
  97. } /* End of cat */
  98. /*****************************************************************************/
  99. str_convert(str, err)
  100. char **str;
  101. int err;
  102. {
  103. int i;
  104. /*
  105. *
  106. * Grab the next integer from **str and return its value or err if *str
  107. * isn't an integer. *str is modified after each digit is read.
  108. *
  109. */
  110. if ( ! isdigit(**str) )
  111. return(err);
  112. for ( i = 0; isdigit(**str); *str += 1 )
  113. i = 10 * i + **str - '0';
  114. return(i);
  115. } /* End of str_convert */
  116. /*****************************************************************************/
  117. error(kind, mesg, a1, a2, a3)
  118. int kind;
  119. char *mesg;
  120. unsigned a1, a2, a3;
  121. {
  122. /*
  123. *
  124. * Print an error message and quit if kind is FATAL.
  125. *
  126. */
  127. if ( mesg != NULL && *mesg != '\0' ) {
  128. fprintf(stderr, "%s: ", prog_name);
  129. fprintf(stderr, mesg, a1, a2, a3);
  130. if ( lineno > 0 )
  131. fprintf(stderr, " (line %d)", lineno);
  132. if ( position > 0 )
  133. fprintf(stderr, " (near byte %d)", position);
  134. putc('\n', stderr);
  135. } /* End if */
  136. if ( kind == FATAL && ignore == OFF ) {
  137. if ( temp_file != NULL )
  138. unlink(temp_file);
  139. exit(x_stat | 01);
  140. } /* End if */
  141. } /* End of error */
  142. /*****************************************************************************/
  143. void interrupt(sig)
  144. int sig;
  145. {
  146. /*
  147. *
  148. * Signal handler for translators.
  149. *
  150. */
  151. if ( temp_file != NULL )
  152. unlink(temp_file);
  153. exit(1);
  154. } /* End of interrupt */
  155. /*****************************************************************************/