URI:
       tcommon.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tcommon.c (6748B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <ctype.h>
            5 #include "common.h"
            6 #include "comments.h"
            7 #include "path.h"
            8 
            9 struct strtab charcode[FONTSIZE] = {
           10         {4, "\\000"}, {4, "\\001"}, {4, "\\002"}, {4, "\\003"},
           11         {4, "\\004"}, {4, "\\005"}, {4, "\\006"}, {4, "\\007"},
           12         {4, "\\010"}, {4, "\\011"}, {4, "\\012"}, {4, "\\013"},
           13         {4, "\\014"}, {4, "\\015"}, {4, "\\016"}, {4, "\\017"},
           14         {4, "\\020"}, {4, "\\021"}, {4, "\\022"}, {4, "\\023"},
           15         {4, "\\024"}, {4, "\\025"}, {4, "\\026"}, {4, "\\027"},
           16         {4, "\\030"}, {4, "\\031"}, {4, "\\032"}, {4, "\\033"},
           17         {4, "\\034"}, {4, "\\035"}, {4, "\\036"}, {4, "\\037"},
           18         {1, " "}, {1, "!"}, {1, "\""}, {1, "#"},
           19         {1, "$"}, {1, "%"}, {1, "&"}, {1, "'"},
           20         {2, "\\("}, {2, "\\)"}, {1, "*"}, {1, "+"},
           21         {1, ","}, {1, "-"}, {1, "."}, {1, "/"},
           22         {1, "0"}, {1, "1"}, {1, "2"}, {1, "3"},
           23         {1, "4"}, {1, "5"}, {1, "6"}, {1, "7"},
           24         {1, "8"}, {1, "9"}, {1, ":"}, {1, ";"},
           25         {1, "<"}, {1, "="}, {1, ">"}, {1, "?"},
           26         {1, "@"}, {1, "A"}, {1, "B"}, {1, "C"},
           27         {1, "D"}, {1, "E"}, {1, "F"}, {1, "G"},
           28         {1, "H"}, {1, "I"}, {1, "J"}, {1, "K"},
           29         {1, "L"}, {1, "M"}, {1, "N"}, {1, "O"},
           30         {1, "P"}, {1, "Q"}, {1, "R"}, {1, "S"},
           31         {1, "T"}, {1, "U"}, {1, "V"}, {1, "W"},
           32         {1, "X"}, {1, "Y"}, {1, "Z"}, {1, "["},
           33         {2, "\\\\"}, {1, "]"}, {1, "^"}, {1, "_"},
           34         {1, "`"}, {1, "a"}, {1, "b"}, {1, "c"},
           35         {1, "d"}, {1, "e"}, {1, "f"}, {1, "g"},
           36         {1, "h"}, {1, "i"}, {1, "j"}, {1, "k"},
           37         {1, "l"}, {1, "m"}, {1, "n"}, {1, "o"},
           38         {1, "p"}, {1, "q"}, {1, "r"}, {1, "s"},
           39         {1, "t"}, {1, "u"}, {1, "v"}, {1, "w"},
           40         {1, "x"}, {1, "y"}, {1, "z"}, {1, "{"},
           41         {1, "|"}, {1, "}"}, {1, "~"}, {4, "\\177"},
           42         {4, "\\200"}, {4, "\\201"}, {4, "\\202"}, {4, "\\203"},
           43         {4, "\\204"}, {4, "\\205"}, {4, "\\206"}, {4, "\\207"},
           44         {4, "\\210"}, {4, "\\211"}, {4, "\\212"}, {4, "\\213"},
           45         {4, "\\214"}, {4, "\\215"}, {4, "\\216"}, {4, "\\217"},
           46         {4, "\\220"}, {4, "\\221"}, {4, "\\222"}, {4, "\\223"},
           47         {4, "\\224"}, {4, "\\225"}, {4, "\\226"}, {4, "\\227"},
           48         {4, "\\230"}, {4, "\\231"}, {4, "\\232"}, {4, "\\233"},
           49         {4, "\\234"}, {4, "\\235"}, {4, "\\236"}, {4, "\\237"},
           50         {4, "\\240"}, {4, "\\241"}, {4, "\\242"}, {4, "\\243"},
           51         {4, "\\244"}, {4, "\\245"}, {4, "\\246"}, {4, "\\247"},
           52         {4, "\\250"}, {4, "\\251"}, {4, "\\252"}, {4, "\\253"},
           53         {4, "\\254"}, {4, "\\255"}, {4, "\\256"}, {4, "\\257"},
           54         {4, "\\260"}, {4, "\\261"}, {4, "\\262"}, {4, "\\263"},
           55         {4, "\\264"}, {4, "\\265"}, {4, "\\266"}, {4, "\\267"},
           56         {4, "\\270"}, {4, "\\271"}, {4, "\\272"}, {4, "\\273"},
           57         {4, "\\274"}, {4, "\\275"}, {4, "\\276"}, {4, "\\277"},
           58         {4, "\\300"}, {4, "\\301"}, {4, "\\302"}, {4, "\\303"},
           59         {4, "\\304"}, {4, "\\305"}, {4, "\\306"}, {4, "\\307"},
           60         {4, "\\310"}, {4, "\\311"}, {4, "\\312"}, {4, "\\313"},
           61         {4, "\\314"}, {4, "\\315"}, {4, "\\316"}, {4, "\\317"},
           62         {4, "\\320"}, {4, "\\321"}, {4, "\\322"}, {4, "\\323"},
           63         {4, "\\324"}, {4, "\\325"}, {4, "\\326"}, {4, "\\327"},
           64         {4, "\\330"}, {4, "\\331"}, {4, "\\332"}, {4, "\\333"},
           65         {4, "\\334"}, {4, "\\335"}, {4, "\\336"}, {4, "\\337"},
           66         {4, "\\340"}, {4, "\\341"}, {4, "\\342"}, {4, "\\343"},
           67         {4, "\\344"}, {4, "\\345"}, {4, "\\346"}, {4, "\\347"},
           68         {4, "\\350"}, {4, "\\351"}, {4, "\\352"}, {4, "\\353"},
           69         {4, "\\354"}, {4, "\\355"}, {4, "\\356"}, {4, "\\357"},
           70         {4, "\\360"}, {4, "\\361"}, {4, "\\362"}, {4, "\\363"},
           71         {4, "\\364"}, {4, "\\365"}, {4, "\\366"}, {4, "\\367"},
           72         {4, "\\370"}, {4, "\\371"}, {4, "\\372"}, {4, "\\373"},
           73         {4, "\\374"}, {4, "\\375"}, {4, "\\376"}, {4, "\\377"}
           74 };
           75 
           76 static BOOLEAN in_string = FALSE;
           77 int char_no = 0;
           78 int line_no = 0;
           79 int page_no = 0;                /* page number in a document */
           80 int pages_printed = 0;
           81 static int pplistmaxsize=0;
           82 
           83 static unsigned char *pplist=0;        /* bitmap list for storing pages to print */
           84 
           85 void
           86 pagelist(char *list) {
           87         char c;
           88         int n, m;
           89         int state, start;
           90 
           91         if (list == 0) return;
           92         state = 1;
           93         start = 0;
           94         while ((c=*list) != '\0') {
           95                 n = 0;
           96                 while (isdigit((uchar)c)) {
           97                         n = n * 10 + c - '0';
           98                         c = *++list;
           99                 }
          100                 switch (state) {
          101                 case 1:
          102                         start = n;
          103                 case 2:
          104                         if (n/8+1 > pplistmaxsize) {
          105                                 pplistmaxsize = n/8+1;
          106                                 pplist = galloc(pplist, n/8+1, "page list");
          107                         }
          108                         for (m=start; m<=n; m++)
          109                                 pplist[m/8] |= 1<<(m%8);
          110                         break;
          111                 }
          112                 switch (c) {
          113                 case '-':
          114                         state = 2;
          115                         list++;
          116                         break;
          117                 case ',':
          118                         state = 1;
          119                         list++;
          120                         break;
          121                 case '\0':
          122                         break;
          123                 }
          124         }
          125 }
          126 
          127 BOOLEAN
          128 pageon(void) {
          129         extern BOOLEAN debug;
          130         static BOOLEAN privdebug = FALSE;
          131 
          132         if (pplist == 0 && page_no != 0) {
          133                 if (privdebug && !debug) {
          134                         privdebug = FALSE;
          135                         debug = TRUE;
          136                 }
          137                 return(TRUE);        /* no page list, print all pages */
          138         }
          139         if (page_no/8 < pplistmaxsize && (pplist[page_no/8] & 1<<(page_no%8))) {
          140                 if (privdebug && !debug) {
          141                         privdebug = FALSE;
          142                         debug = TRUE;
          143                 }
          144                 return(TRUE);
          145         } else {
          146                 if (!privdebug && debug) {
          147                         privdebug = TRUE;
          148                         debug = FALSE;
          149                 }
          150                 return(FALSE);
          151         }
          152 }
          153 
          154 static int stringhpos, stringvpos;
          155 
          156 void
          157 startstring(void) {
          158         if (!in_string) {
          159                 stringhpos = hpos;
          160                 stringvpos = vpos;
          161                 if (pageon()) Bprint(Bstdout, "(");
          162                 in_string = 1;
          163         }
          164 }
          165 
          166 void
          167 endstring(void) {
          168         if (in_string) {
          169                 if (pageon()) Bprint(Bstdout, ") %d %d w\n", stringhpos, stringvpos);
          170                 in_string = 0;
          171         }
          172 }
          173 
          174 BOOLEAN
          175 isinstring(void) {
          176         return(in_string);
          177 }
          178 
          179 void
          180 startpage(void) {
          181         ++char_no;
          182         ++line_no;
          183         ++page_no;
          184         if (pageon()) {
          185                 ++pages_printed;
          186                 Bprint(Bstdout, "%s %d %d\n", PAGE, page_no, pages_printed);
          187                 Bprint(Bstdout, "/saveobj save def\n");
          188                 Bprint(Bstdout, "mark\n");
          189                 Bprint(Bstdout, "%d pagesetup\n", pages_printed);
          190         }
          191 }
          192 
          193 void
          194 endpage(void) {
          195         endstring();
          196         curpostfontid = -1;
          197         line_no = 0;
          198         char_no = 0;
          199         if (pageon()) {
          200                 Bprint(Bstdout, "cleartomark\n");
          201                 Bprint(Bstdout, "showpage\n");
          202                 Bprint(Bstdout, "saveobj restore\n");
          203                 Bprint(Bstdout, "%s %d %d\n", ENDPAGE, page_no, pages_printed);
          204         }
          205 }
          206 
          207 /* This was taken from postprint */
          208 
          209 int
          210 cat(char *filename) {
          211         Biobuf *bfile;
          212         Biobuf *Bfile;
          213         int n;
          214         static char buf[Bsize];
          215 
          216         if ((bfile = Bopen(filename, OREAD)) == 0) {
          217                 return(1);
          218         }
          219         Bfile = bfile; /* &(bfile->Biobufhdr); */
          220         while ((n=Bread(Bfile, buf, Bsize)) > 0) {
          221                 if (Bwrite(Bstdout, buf, n) != n)
          222                         break;
          223         }
          224         Bterm(Bfile);
          225         if (n != 0) {
          226                 return(1);
          227         }
          228         return(0);
          229 }
          230 extern int debug;
          231 void *
          232 galloc(void *ptr, int size, char *perstr) {
          233         void *x;
          234 
          235         if ((x=realloc(ptr, size)) == 0) {
          236                 perror(perstr);
          237                 exits("malloc");
          238         }
          239         return(x);
          240 }
          241 
          242 static char *errorstrings[] = {
          243         "",        /* NONE */
          244         "WARNING",
          245         "FATAL"
          246 };
          247 
          248 char *programname;
          249 char *inputfilename = "<stdin>";
          250 int inputlineno;
          251 
          252 void
          253 error(int errtype, char *fmt, ...) {
          254         va_list arg;
          255 
          256         Bflush(Bstdout);
          257         Bflush(Bstderr);
          258         fprint(2, "%s: %s:%d :%s: ", programname, inputfilename, inputlineno, errorstrings[errtype]);
          259         va_start(arg, fmt);
          260         vfprint(2, fmt, arg);
          261         va_end(arg);
          262         if (errtype == FATAL)
          263                 exits("fatal error");
          264 }