nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

auto-str.c (1225B)


      1 #include <ctype.h>
      2 #include "substdio.h"
      3 #include "readwrite.h"
      4 #include "exit.h"
      5 
      6 char buf1[256];
      7 substdio ss1 = SUBSTDIO_FDBUF(write,1,buf1,sizeof(buf1));
      8 
      9 void puts(s)
     10 char *s;
     11 {
     12   if (substdio_puts(&ss1,s) == -1) _exit(111);
     13 }
     14 
     15 // check if a given character can be printed unquoted in a C string
     16 // does not accept digits as they may be hardly visible between octal encoded chars
     17 static int is_legible(unsigned char ch)
     18 {
     19   if (isascii(ch))
     20     return 1;
     21   if (ch == '/' || ch == '_' || ch == '-' || ch == '.')
     22     return 1;
     23   return 0;
     24 }
     25 
     26 int main(int argc, char **argv)
     27 {
     28   char *name;
     29   char *value;
     30   unsigned char ch;
     31   char octal[4];
     32 
     33   if (argc != 3) return 100;
     34   name = argv[1];
     35   if (!name) return 100;
     36   value = argv[2];
     37   if (!value) return 100;
     38 
     39   puts("char ");
     40   puts(name);
     41   puts("[] = \"\\\n");
     42 
     43   while ((ch = *value++)) {
     44     if (is_legible(ch)) {
     45       if (substdio_put(&ss1, (char *)&ch, 1) == -1)
     46         _exit(111);
     47     } else {
     48       puts("\\");
     49       octal[3] = 0;
     50       octal[2] = '0' + (ch & 7); ch >>= 3;
     51       octal[1] = '0' + (ch & 7); ch >>= 3;
     52       octal[0] = '0' + (ch & 7);
     53       puts(octal);
     54     }
     55   }
     56 
     57   puts("\\\n\";\n");
     58   if (substdio_flush(&ss1) == -1) return 111;
     59   return 0;
     60 }