/contrib/tcsh/sh.print.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 315 lines · 227 code · 31 blank · 57 comment · 60 complexity · 975fe0c49820ab90225068ceb9dbd227 MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/sh.print.c,v 3.36 2011/05/25 20:17:20 christos Exp $ */
  2. /*
  3. * sh.print.c: Primitive Output routines.
  4. */
  5. /*-
  6. * Copyright (c) 1980, 1991 The Regents of the University of California.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "sh.h"
  34. RCSID("$tcsh: sh.print.c,v 3.36 2011/05/25 20:17:20 christos Exp $")
  35. #include "ed.h"
  36. extern int Tty_eight_bit;
  37. int lbuffed = 1; /* true if line buffered */
  38. static void p2dig (unsigned int);
  39. /*
  40. * C Shell
  41. */
  42. #if defined(BSDLIMIT) || defined(RLIMIT_CPU)
  43. void
  44. psecs(unsigned long l)
  45. {
  46. int i;
  47. i = (int) (l / 3600);
  48. if (i) {
  49. xprintf("%d:", i);
  50. i = (int) (l % 3600);
  51. p2dig(i / 60);
  52. goto minsec;
  53. }
  54. i = (int) l;
  55. xprintf("%d", i / 60);
  56. minsec:
  57. i %= 60;
  58. xprintf(":");
  59. p2dig(i);
  60. }
  61. #endif
  62. void /* PWP: print mm:ss.dd, l is in sec*100 */
  63. #ifdef BSDTIMES
  64. pcsecs(unsigned long l)
  65. #else /* BSDTIMES */
  66. # ifndef POSIX
  67. pcsecs(time_t l)
  68. # else /* POSIX */
  69. pcsecs(clock_t l)
  70. # endif /* POSIX */
  71. #endif /* BSDTIMES */
  72. {
  73. int i;
  74. i = (int) (l / 360000);
  75. if (i) {
  76. xprintf("%d:", i);
  77. i = (int) ((l % 360000) / 100);
  78. p2dig(i / 60);
  79. goto minsec;
  80. }
  81. i = (int) (l / 100);
  82. xprintf("%d", i / 60);
  83. minsec:
  84. i %= 60;
  85. xprintf(":");
  86. p2dig(i);
  87. xprintf(".");
  88. p2dig((int) (l % 100));
  89. }
  90. static void
  91. p2dig(unsigned i)
  92. {
  93. xprintf("%u%u", i / 10, i % 10);
  94. }
  95. char linbuf[2048]; /* was 128 */
  96. char *linp = linbuf;
  97. int output_raw = 0; /* PWP */
  98. int xlate_cr = 0; /* HE */
  99. /* For cleanup_push() */
  100. void
  101. output_raw_restore(void *xorig)
  102. {
  103. int *orig;
  104. orig = xorig;
  105. output_raw = *orig;
  106. }
  107. #ifdef WIDE_STRINGS
  108. void
  109. putwraw(Char c)
  110. {
  111. char buf[MB_LEN_MAX];
  112. size_t i, len;
  113. len = one_wctomb(buf, c & CHAR);
  114. for (i = 0; i < len; i++)
  115. putraw((unsigned char)buf[i] | (c & ~CHAR));
  116. }
  117. void
  118. xputwchar(Char c)
  119. {
  120. char buf[MB_LEN_MAX];
  121. size_t i, len;
  122. len = one_wctomb(buf, c & CHAR);
  123. for (i = 0; i < len; i++)
  124. xputchar((unsigned char)buf[i] | (c & ~CHAR));
  125. }
  126. #endif
  127. void
  128. xputchar(int c)
  129. {
  130. int atr;
  131. atr = c & ATTRIBUTES & TRIM;
  132. c &= CHAR | QUOTE;
  133. if (!output_raw && (c & QUOTE) == 0) {
  134. if (iscntrl(c) && (ASC(c) < 0x80 || MB_CUR_MAX == 1)) {
  135. if (c != '\t' && c != '\n'
  136. #ifdef COLORCAT
  137. && !(adrof(STRcolorcat) && c == CTL_ESC('\033'))
  138. #endif
  139. && (xlate_cr || c != '\r'))
  140. {
  141. xputchar('^' | atr);
  142. if (c == CTL_ESC('\177'))
  143. c = '?';
  144. else
  145. /* Note: for IS_ASCII, this compiles to: c = c | 0100 */
  146. c = CTL_ESC(ASC(c)|0100);
  147. }
  148. }
  149. else if (!isprint(c) && (ASC(c) < 0x80 || MB_CUR_MAX == 1)) {
  150. xputchar('\\' | atr);
  151. xputchar((((c >> 6) & 7) + '0') | atr);
  152. xputchar((((c >> 3) & 7) + '0') | atr);
  153. c = (c & 7) + '0';
  154. }
  155. (void) putraw(c | atr);
  156. }
  157. else {
  158. c &= TRIM;
  159. if (haderr ? (didfds ? is2atty : isdiagatty) :
  160. (didfds ? is1atty : isoutatty))
  161. SetAttributes(c | atr);
  162. (void) putpure(c);
  163. }
  164. if (lbuffed && (c & CHAR) == '\n')
  165. flush();
  166. }
  167. int
  168. putraw(int c)
  169. {
  170. if (haderr ? (didfds ? is2atty : isdiagatty) :
  171. (didfds ? is1atty : isoutatty)) {
  172. if (Tty_eight_bit == -1)
  173. ed_set_tty_eight_bit();
  174. if (!Tty_eight_bit && (c & META)) {
  175. c = (c & ~META) | STANDOUT;
  176. }
  177. SetAttributes(c);
  178. }
  179. return putpure(c);
  180. }
  181. int
  182. putpure(int c)
  183. {
  184. c &= CHAR;
  185. *linp++ = (char) c;
  186. if (linp >= &linbuf[sizeof linbuf - 10])
  187. flush();
  188. return (1);
  189. }
  190. void
  191. drainoline(void)
  192. {
  193. linp = linbuf;
  194. }
  195. void
  196. flush(void)
  197. {
  198. int unit, oldexitset = exitset;
  199. static int interrupted = 0;
  200. /* int lmode; */
  201. if (linp == linbuf)
  202. return;
  203. if (GettingInput && !Tty_raw_mode && linp < &linbuf[sizeof linbuf - 10])
  204. return;
  205. if (handle_intr)
  206. exitset = 1;
  207. if (interrupted) {
  208. interrupted = 0;
  209. linp = linbuf; /* avoid recursion as stderror calls flush */
  210. if (handle_intr)
  211. fixerror();
  212. else
  213. stderror(ERR_SILENT);
  214. }
  215. interrupted = 1;
  216. if (haderr)
  217. unit = didfds ? 2 : SHDIAG;
  218. else
  219. unit = didfds ? 1 : SHOUT;
  220. #ifdef COMMENT
  221. #ifdef TIOCLGET
  222. if (didfds == 0 && ioctl(unit, TIOCLGET, (ioctl_t) & lmode) == 0 &&
  223. lmode & LFLUSHO) {
  224. lmode = LFLUSHO;
  225. (void) ioctl(unit, TIOCLBIC, (ioclt_t) & lmode);
  226. (void) xwrite(unit, "\n", 1);
  227. }
  228. #endif
  229. #endif
  230. if (xwrite(unit, linbuf, linp - linbuf) == -1)
  231. switch (errno) {
  232. #ifdef EIO
  233. /* We lost our tty */
  234. case EIO:
  235. #endif
  236. #ifdef ENXIO
  237. /*
  238. * Deal with Digital Unix 4.0D bogocity, returning ENXIO when
  239. * we lose our tty.
  240. */
  241. case ENXIO:
  242. #endif
  243. /*
  244. * IRIX 6.4 bogocity?
  245. */
  246. #ifdef ENOTTY
  247. case ENOTTY:
  248. #endif
  249. #ifdef EBADF
  250. case EBADF:
  251. #endif
  252. #ifdef ESTALE
  253. /*
  254. * Lost our file descriptor, exit (IRIS4D)
  255. */
  256. case ESTALE:
  257. #endif
  258. #ifdef ENOENT
  259. /*
  260. * Deal with SoFS bogocity: returns ENOENT instead of ESTALE.
  261. */
  262. case ENOENT:
  263. #endif
  264. /*
  265. * Over our quota, writing the history file
  266. */
  267. #ifdef EDQUOT
  268. case EDQUOT:
  269. #endif
  270. /* Nothing to do, but die */
  271. if (handle_intr == 0)
  272. xexit(1);
  273. /*FALLTHROUGH*/
  274. default:
  275. if (handle_intr)
  276. fixerror();
  277. else
  278. stderror(ERR_SILENT);
  279. break;
  280. }
  281. exitset = oldexitset;
  282. linp = linbuf;
  283. interrupted = 0;
  284. }