aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

output.c (7585B)


      1 /* -
      2  * spdx-license-identifier: bsd-3-clause
      3  *
      4  * copyright (c) 1991, 1993
      5  * the regents of the university of california. all rights reserved
      6  *
      7  * this code is derived from software contributed to berkeley by
      8  * kenneth almquist
      9  *
     10  * redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. redistributions of source code must retain the above copyright
     14  * notice, this list of conditions and the following disclaimer
     15  * 2. redistributions in binary form must reproduce the above copyright
     16  * notice, this list of conditions and the following disclaimer in the
     17  * documentation and/or other materials provided with the distribution
     18  * 3. neither the name of the university nor the names of its contributors
     19  * may be used to endorse or promote products derived from this software
     20  * without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR a PARTICULAR PURPOSE
     25  * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE
     33  */
     34 
     35 /*
     36  * shell output routines. we use our own output routines because:
     37  * when a builtin command is interrupted we have to discard
     38  * any pending output
     39  * when a builtin command appears in back quotes, we want to
     40  * save the output of the command in a region obtained
     41  * via malloc, rather than doing a fork and reading the
     42  * output of the command via a pipe
     43  */
     44 
     45 #if FEATURE_SH_EXT
     46 #ifndef _DEFAULT_SOURCE
     47 #define _DEFAULT_SOURCE
     48 #endif
     49 #ifndef _GNU_SOURCE
     50 #define _GNU_SOURCE
     51 #endif
     52 #endif
     53 #include <errno.h>
     54 #include <stdarg.h>
     55 #include <stdio.h> /* defines BUFSIZ */
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 #include <wchar.h>
     60 #include <wctype.h>
     61 
     62 #include "error.h"
     63 #include "memalloc.h"
     64 #include "output.h"
     65 #include "shell.h"
     66 #include "syntax.h"
     67 #include "var.h"
     68 
     69 #define OUTBUFSIZ  BUFSIZ
     70 #define MEM_OUT    -2 /* output to dynamically allocated memory */
     71 #define OUTPUT_ERR 01 /* error occurred on output */
     72 
     73 struct output  output = {NULL, NULL, NULL, OUTBUFSIZ, 1, 0};
     74 struct output  errout = {NULL, NULL, NULL, 256, 2, 0};
     75 struct output  memout = {NULL, NULL, NULL, 64, MEM_OUT, 0};
     76 struct output *out1   = &output;
     77 struct output *out2   = &errout;
     78 
     79 void
     80 outcslow(int c, struct output *file)
     81 {
     82   outc(c, file);
     83 }
     84 
     85 void
     86 out1str(const char *p)
     87 {
     88   outstr(p, out1);
     89 }
     90 
     91 void
     92 out1qstr(const char *p)
     93 {
     94   outqstr(p, out1);
     95 }
     96 
     97 void
     98 out2str(const char *p)
     99 {
    100   outstr(p, out2);
    101 }
    102 
    103 void
    104 out2qstr(const char *p)
    105 {
    106   outqstr(p, out2);
    107 }
    108 
    109 void
    110 outstr(const char *p, struct output *file)
    111 {
    112   outbin(p, strlen(p), file);
    113 }
    114 
    115 static void
    116 byteseq(int ch, struct output *file)
    117 {
    118   char seq[4];
    119 
    120   seq[0] = '\\';
    121   seq[1] = (ch >> 6 & 0x3) + '0';
    122   seq[2] = (ch >> 3 & 0x7) + '0';
    123   seq[3] = (ch & 0x7) + '0';
    124   outbin(seq, 4, file);
    125 }
    126 
    127 static void
    128 outdqstr(const char *p, struct output *file)
    129 {
    130   const char *end;
    131   mbstate_t   mbs;
    132   size_t      clen;
    133   wchar_t     wc;
    134 
    135   memset(&mbs, '\0', sizeof(mbs));
    136   end = p + strlen(p);
    137   outstr("$'", file);
    138   while ((clen = mbrtowc(&wc, p, end - p + 1, &mbs)) != 0) {
    139     if (clen == (size_t)-2) {
    140       while (p < end)
    141         byteseq(*p++, file);
    142       break;
    143     }
    144     if (clen == (size_t)-1) {
    145       memset(&mbs, '\0', sizeof(mbs));
    146       byteseq(*p++, file);
    147       continue;
    148     }
    149     if (wc == L'\n')
    150       outcslow('\n', file), p++;
    151     else if (wc == L'\r')
    152       outstr("\\r", file), p++;
    153     else if (wc == L'\t')
    154       outstr("\\t", file), p++;
    155     else if (!iswprint(wc)) {
    156       for (; clen > 0; clen--)
    157         byteseq(*p++, file);
    158     } else {
    159       if (wc == L'\'' || wc == L'\\')
    160         outcslow('\\', file);
    161       outbin(p, clen, file);
    162       p += clen;
    163     }
    164   }
    165   outcslow('\'', file);
    166 }
    167 
    168 /* like outstr(), but quote for re-input into the shell */
    169 void
    170 outqstr(const char *p, struct output *file)
    171 {
    172   int i;
    173 
    174   if (p[0] == '\0') {
    175     outstr("''", file);
    176     return;
    177   }
    178   for (i = 0; p[i] != '\0'; i++) {
    179     if ((p[i] > '\0' && p[i] < ' ' && p[i] != '\n') || (p[i] & 0x80) != 0 || p[i] == '\'') {
    180       outdqstr(p, file);
    181       return;
    182     }
    183   }
    184 
    185   if (p[strcspn(p, "|&;<>()$`\\\" \n*?[~#=")] == '\0' || strcmp(p, "[") == 0) {
    186     outstr(p, file);
    187     return;
    188   }
    189 
    190   outcslow('\'', file);
    191   outstr(p, file);
    192   outcslow('\'', file);
    193 }
    194 
    195 void
    196 outbin(const void *data, size_t len, struct output *file)
    197 {
    198   const char *p;
    199 
    200   p = data;
    201   while (len-- > 0)
    202     outc(*p++, file);
    203 }
    204 
    205 void
    206 emptyoutbuf(struct output *dest)
    207 {
    208   int offset, newsize;
    209 
    210   if (dest->buf == NULL) {
    211     INTOFF;
    212     dest->buf    = ckmalloc(dest->bufsize);
    213     dest->nextc  = dest->buf;
    214     dest->bufend = dest->buf + dest->bufsize;
    215     INTON;
    216   } else if (dest->fd == MEM_OUT) {
    217     offset  = dest->nextc - dest->buf;
    218     newsize = dest->bufsize << 1;
    219     INTOFF;
    220     dest->buf     = ckrealloc(dest->buf, newsize);
    221     dest->bufsize = newsize;
    222     dest->bufend  = dest->buf + newsize;
    223     dest->nextc   = dest->buf + offset;
    224     INTON;
    225   } else {
    226     flushout(dest);
    227   }
    228 }
    229 
    230 void
    231 flushall(void)
    232 {
    233   flushout(&output);
    234   flushout(&errout);
    235 }
    236 
    237 void
    238 flushout(struct output *dest)
    239 {
    240   if (dest->buf == NULL || dest->nextc == dest->buf || dest->fd < 0)
    241     return;
    242   if (xwrite(dest->fd, dest->buf, dest->nextc - dest->buf) < 0)
    243     dest->flags |= OUTPUT_ERR;
    244   dest->nextc = dest->buf;
    245 }
    246 
    247 void
    248 freestdout(void)
    249 {
    250   output.nextc = output.buf;
    251 }
    252 
    253 int
    254 outiserror(struct output *file)
    255 {
    256   return (file->flags & OUTPUT_ERR);
    257 }
    258 
    259 void
    260 outclearerror(struct output *file)
    261 {
    262   file->flags &= ~OUTPUT_ERR;
    263 }
    264 
    265 void
    266 outfmt(struct output *file, const char *fmt, ...)
    267 {
    268   va_list ap;
    269 
    270   va_start(ap, fmt);
    271   doformat(file, fmt, ap);
    272   va_end(ap);
    273 }
    274 
    275 void
    276 out1fmt(const char *fmt, ...)
    277 {
    278   va_list ap;
    279 
    280   va_start(ap, fmt);
    281   doformat(out1, fmt, ap);
    282   va_end(ap);
    283 }
    284 
    285 void
    286 out2fmt_flush(const char *fmt, ...)
    287 {
    288   va_list ap;
    289 
    290   va_start(ap, fmt);
    291   doformat(out2, fmt, ap);
    292   va_end(ap);
    293   flushout(out2);
    294 }
    295 
    296 void
    297 fmtstr(char *outbuf, int length, const char *fmt, ...)
    298 {
    299   va_list ap;
    300 
    301   INTOFF;
    302   va_start(ap, fmt);
    303   vsnprintf(outbuf, length, fmt, ap);
    304   va_end(ap);
    305   INTON;
    306 }
    307 
    308 void
    309 doformat(struct output *dest, const char *f, va_list ap)
    310 {
    311   char    buf[BUFSIZ], *s;
    312   va_list aq;
    313   int     len;
    314 
    315   va_copy(aq, ap);
    316   len = vsnprintf(buf, sizeof(buf), f, aq);
    317   va_end(aq);
    318   if (len < 0)
    319     return;
    320   if ((size_t)len < sizeof(buf)) {
    321     outbin(buf, len, dest);
    322     return;
    323   }
    324   if ((s = malloc(len + 1)) == NULL)
    325     return;
    326   vsnprintf(s, len + 1, f, ap);
    327   outbin(s, len, dest);
    328   free(s);
    329 }
    330 
    331 /*
    332  * version of write which resumes after a signal is caught
    333  */
    334 
    335 int
    336 xwrite(int fd, const char *buf, int nbytes)
    337 {
    338   int ntry;
    339   int i;
    340   int n;
    341 
    342   n    = nbytes;
    343   ntry = 0;
    344   for (;;) {
    345     i = write(fd, buf, n);
    346     if (i > 0) {
    347       if ((n -= i) <= 0)
    348         return nbytes;
    349       buf += i;
    350       ntry = 0;
    351     } else if (i == 0) {
    352       if (++ntry > 10)
    353         return nbytes - n;
    354     } else if (errno != EINTR) {
    355       return -1;
    356     }
    357   }
    358 }