@@ -63,16 +63,16 @@ struct vlbuf vlbuf_make(size_t es) {
6363 return ib ;
6464}
6565
66- size_t vlbuf_expand (struct vlbuf * ib , size_t minsize ) {
66+ static size_t vlbuf_expand (struct vlbuf * ib , size_t minsize ) {
6767 do {
6868 ib -> len *= 2 ;
6969 } while (ib -> len <= minsize );
7070 ib -> d .vd = realloc (ib -> d .vd , ib -> len * ib -> esize );
7171 return ib -> len ;
7272}
7373
74- size_t vlbuf_append (struct vlbuf * ib , const char * str , size_t countedlen ,
75- FILE * out ) {
74+ static size_t vlbuf_append (struct vlbuf * ib , const char * str , size_t countedlen ,
75+ FILE * out ) {
7676 int l = strlen (str );
7777 if (ib ) {
7878 if (ib -> len <= l + countedlen + 1 ) {
@@ -86,13 +86,23 @@ size_t vlbuf_append(struct vlbuf *ib, const char *str, size_t countedlen,
8686 return l + countedlen ;
8787}
8888
89- void vlbuf_free (struct vlbuf * ib ) {
89+ static void vlbuf_free (struct vlbuf * ib ) {
9090 free (ib -> d .vd );
9191 ib -> d .vd = 0 ;
9292 ib -> len = 0 ;
9393}
9494
95- const char * tok_to_string (int tok ) {
95+ static size_t strapp (char * target , const char * app ) {
96+ size_t delta = 0 ;
97+ while (* app != '\0' ) {
98+ target [delta ] = * app ;
99+ delta ++ ;
100+ app ++ ;
101+ }
102+ return delta ;
103+ }
104+
105+ static const char * tok_to_string (int tok ) {
96106 switch (tok ) {
97107 case TOK_LABEL :
98108 return "LAB" ;
@@ -133,7 +143,7 @@ const char *tok_to_string(int tok) {
133143 }
134144}
135145
136- const char * ls_to_string (int ls ) {
146+ static const char * ls_to_string (int ls ) {
137147 switch (ls ) {
138148 case LINE_IS_BLANK :
139149 return "LINE_BLNK" ;
@@ -148,7 +158,7 @@ const char *ls_to_string(int ls) {
148158 }
149159}
150160
151- int isalpha_lead (char c ) {
161+ static int isalpha_lead (char c ) {
152162 if (c > 127 )
153163 return 1 ;
154164 if ('a' <= c && c <= 'z' )
@@ -159,28 +169,28 @@ int isalpha_lead(char c) {
159169 return 1 ;
160170 return 0 ;
161171}
162- int isnumeric_lead (char c ) {
172+ static int isnumeric_lead (char c ) {
163173 if ('0' <= c && c <= '9' )
164174 return 1 ;
165175 if ('.' == c )
166176 return 1 ;
167177 return 0 ;
168178}
169179
170- int isoptype (char c ) {
180+ static int isoptype (char c ) {
171181 if (c == '=' || c == '+' || c == '-' || c == '@' || c == '|' || c == '^' ||
172182 c == '&' || c == '*' || c == '/' || c == '<' || c == '>' || c == '!' ||
173183 c == '~' || c == '%' )
174184 return 1 ;
175185 return 0 ;
176186}
177187
178- const char * specnames [] = {"if" , "then" , "else" , "import" , "except" ,
179- "for" , "while" , "return" , "yield" , "from" ,
180- "as" , "else" , "finally" , NULL };
188+ static const char * specnames [] = {"if" , "then" , "else" , "import" , "except" ,
189+ "for" , "while" , "return" , "yield" , "from" ,
190+ "as" , "else" , "finally" , NULL };
181191
182- void pyformat (FILE * file , FILE * out , struct vlbuf * origfile ,
183- struct vlbuf * formfile ) {
192+ static void pyformat (FILE * file , FILE * out , struct vlbuf * origfile ,
193+ struct vlbuf * formfile ) {
184194 struct vlbuf linebuf = vlbuf_make (sizeof (char ));
185195 struct vlbuf tokbuf = vlbuf_make (sizeof (char ));
186196 struct vlbuf toks = vlbuf_make (sizeof (int ));
@@ -572,7 +582,6 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
572582
573583 /* Line wrapping & printing, oh joy */
574584 char * tokpos = tokbuf .d .ch ;
575- // buildpt += sprintf(buildpt, "%s", lsp);
576585 int nests = 0 ;
577586 for (int i = 0 ; i < ntoks ; i ++ ) {
578587 int pptok = i > 0 ? toks .d .in [i - 1 ] : TOK_INBETWEEN ;
@@ -583,7 +592,7 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
583592 if (pretok == TOK_LCONT ) {
584593 /* ignore line breaks */
585594 } else if (pretok == TOK_COMMENT ) {
586- char * eos = tokpos + toklen ;
595+ char * eos = tokpos + toklen - 1 ;
587596 char * sos = tokpos ;
588597 while (* sos == ' ' ) {
589598 sos ++ ;
@@ -593,16 +602,18 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
593602 eos -- ;
594603 }
595604 if (sos [0 ] == '!' ) {
596- buildpt += sprintf (buildpt , "#%s !#" , sos );
605+ buildpt += strapp (buildpt , "#" );
597606 } else {
598- buildpt += sprintf (buildpt , "# %s !#" , sos );
607+ buildpt += strapp (buildpt , "# " );
599608 }
609+ buildpt += strapp (buildpt , sos );
610+ buildpt += strapp (buildpt , "!#" );
600611 splitpoints .d .in [nsplits ] = buildpt - laccum .d .ch ;
601612 split_ratings .d .in [nsplits ] = -1 ;
602613 split_nestings .d .in [nsplits ] = nests ;
603614 nsplits ++ ;
604615 } else {
605- buildpt += sprintf (buildpt , "%s" , tokpos );
616+ buildpt += strapp (buildpt , tokpos );
606617 splitpoints .d .in [nsplits ] = buildpt - laccum .d .ch ;
607618 split_ratings .d .in [nsplits ] = 0 ;
608619 split_nestings .d .in [nsplits ] = nests ;
@@ -611,7 +622,9 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
611622 tokpos += toklen + 1 ;
612623
613624 int space ;
614- if (pretok == TOK_LCONT ) {
625+ if (pretok == TOK_COMMENT ) {
626+ space = 0 ;
627+ } else if (pretok == TOK_LCONT ) {
615628 space = 0 ;
616629 } else if (pretok == TOK_EQUAL || postok == TOK_EQUAL ) {
617630 space = nests == 0 ;
@@ -669,7 +682,7 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
669682 space = 1 ;
670683 }
671684 if (space && i < ntoks - 1 ) {
672- buildpt += sprintf (buildpt , " " );
685+ buildpt += strapp (buildpt , " " );
673686 }
674687
675688 if (pretok == TOK_OBRACE ) {
@@ -749,6 +762,9 @@ void pyformat(FILE *file, FILE *out, struct vlbuf *origfile,
749762}
750763
751764int main (int argc , char * * argv ) {
765+ (void )ls_to_string ;
766+ (void )tok_to_string ;
767+
752768 int inplace = 0 ;
753769 if (argv [0 ][strlen (argv [0 ]) - 1 ] == 'i' ) {
754770 inplace = 1 ;
0 commit comments