49#include <config_auto.h>
53#include "allheaders.h"
55#define L_BUF_SIZE 2048
57static l_int32 getNextNonCommentLine(
SARRAY *sa, l_int32 start, l_int32 *pnext);
58static l_int32 getNextNonBlankLine(
SARRAY *sa, l_int32 start, l_int32 *pnext);
59static l_int32 getNextNonDoubleSlashLine(
SARRAY *sa, l_int32 start,
61static l_int32 searchForProtoSignature(
SARRAY *sa, l_int32 begin,
62 l_int32 *pstart, l_int32 *pstop, l_int32 *pcharindex,
64static char * captureProtoSignature(
SARRAY *sa, l_int32 start, l_int32 stop,
66static char * cleanProtoSignature(
char *str);
67static l_int32 skipToEndOfFunction(
SARRAY *sa, l_int32 start,
68 l_int32 charindex, l_int32 *pnext);
69static l_int32 skipToMatchingBrace(
SARRAY *sa, l_int32 start,
70 l_int32 lbindex, l_int32 *prbline, l_int32 *prbindex);
71static l_int32 skipToSemicolon(
SARRAY *sa, l_int32 start,
72 l_int32 charindex, l_int32 *pnext);
73static l_int32 getOffsetForCharacter(
SARRAY *sa, l_int32 start,
char tchar,
74 l_int32 *psoffset, l_int32 *pboffset, l_int32 *ptoffset);
75static l_int32 getOffsetForMatchingRP(
SARRAY *sa, l_int32 start,
76 l_int32 soffsetlp, l_int32 boffsetlp, l_int32 toffsetlp,
77 l_int32 *psoffset, l_int32 *pboffset, l_int32 *ptoffset);
150parseForProtos(
const char *filein,
151 const char *prestring)
153char *strdata, *str, *newstr, *parsestr, *secondword;
154l_int32 start, next, stop, charindex, found;
156SARRAY *sa, *saout, *satest;
159 return (
char *)ERROR_PTR(
"filein not defined", __func__, NULL);
163 strdata = (
char *)l_binaryRead(filein, &nbytes);
164 sa = sarrayCreateLinesFromString(strdata, 0);
166 saout = sarrayCreate(0);
169 searchForProtoSignature(sa, next, &start, &stop, &charindex, &found);
174 str = captureProtoSignature(sa, start, stop, charindex);
192 satest = sarrayCreateWordsFromString(str);
193 secondword = sarrayGetString(satest, 1,
L_NOCOPY);
194 if (strcmp(secondword,
"static") &&
195 strcmp(secondword,
"extern") &&
196 strcmp(secondword,
"typedef")) {
198 newstr = stringJoin(prestring, str);
199 sarrayAddString(saout, newstr,
L_INSERT);
202 sarrayAddString(saout, str,
L_INSERT);
207 sarrayDestroy(&satest);
209 skipToEndOfFunction(sa, stop, charindex, &next);
210 if (next == -1)
break;
214 parsestr = sarrayToString(saout, 1);
217 sarrayDestroy(&saout);
238getNextNonCommentLine(
SARRAY *sa,
246 return ERROR_INT(
"sa not defined", __func__, 1);
248 return ERROR_INT(
"&pnext not defined", __func__, 1);
253 n = sarrayGetCount(sa);
254 for (i = start; i < n; i++) {
255 if ((str = sarrayGetString(sa, i,
L_NOCOPY)) == NULL)
256 return ERROR_INT(
"str not returned; shouldn't happen", __func__, 1);
283getNextNonBlankLine(
SARRAY *sa,
291 return ERROR_INT(
"sa not defined", __func__, 1);
293 return ERROR_INT(
"&pnext not defined", __func__, 1);
298 n = sarrayGetCount(sa);
299 for (i = start; i < n; i++) {
300 if ((str = sarrayGetString(sa, i,
L_NOCOPY)) == NULL)
301 return ERROR_INT(
"str not returned; shouldn't happen", __func__, 1);
303 for (j = 0; j < len; j++) {
304 if (str[j] !=
' ' && str[j] !=
'\t'
305 && str[j] !=
'\n' && str[j] !=
'\r') {
331getNextNonDoubleSlashLine(
SARRAY *sa,
339 return ERROR_INT(
"sa not defined", __func__, 1);
341 return ERROR_INT(
"&pnext not defined", __func__, 1);
347 n = sarrayGetCount(sa);
348 for (i = start; i < n; i++) {
349 if ((str = sarrayGetString(sa, i,
L_NOCOPY)) == NULL)
350 return ERROR_INT(
"str not returned; shouldn't happen", __func__, 1);
352 if (len < 2 || str[0] !=
'/' || str[1] !=
'/') {
395searchForProtoSignature(
SARRAY *sa,
402l_int32 next, rbline, rbindex, scline;
403l_int32 soffsetlp, soffsetrp, soffsetlb, soffsetsc;
404l_int32 boffsetlp, boffsetrp, boffsetlb, boffsetsc;
405l_int32 toffsetlp, toffsetrp, toffsetlb, toffsetsc;
408 return ERROR_INT(
"sa not defined", __func__, 1);
410 return ERROR_INT(
"&start not defined", __func__, 1);
412 return ERROR_INT(
"&stop not defined", __func__, 1);
414 return ERROR_INT(
"&charindex not defined", __func__, 1);
416 return ERROR_INT(
"&found not defined", __func__, 1);
423 getNextNonCommentLine(sa, begin, &next);
424 if (next == -1)
return 0;
431 getNextNonBlankLine(sa, begin, &next);
432 if (next == -1)
return 0;
439 getNextNonDoubleSlashLine(sa, begin, &next);
440 if (next == -1)
return 0;
449 getOffsetForCharacter(sa, next,
'(', &soffsetlp, &boffsetlp,
453 getOffsetForMatchingRP(sa, next, soffsetlp, boffsetlp, toffsetlp,
454 &soffsetrp, &boffsetrp, &toffsetrp);
455 getOffsetForCharacter(sa, next,
'{', &soffsetlb, &boffsetlb,
457 getOffsetForCharacter(sa, next,
';', &soffsetsc, &boffsetsc,
462 if (soffsetrp == -1 || soffsetlb == -1)
467 if (toffsetlb < toffsetlp) {
468 skipToMatchingBrace(sa, next + soffsetlb, boffsetlb,
470 skipToSemicolon(sa, rbline, rbindex, &scline);
477 if ((soffsetsc != -1) &&
478 (toffsetsc < toffsetlb || toffsetsc < toffsetlp)) {
479 skipToSemicolon(sa, next, 0, &scline);
490 *pstop = next + soffsetrp;
491 *pcharindex = boffsetrp;
515captureProtoSignature(
SARRAY *sa,
520char *str, *newstr, *protostr, *cleanstr;
525 return (
char *)ERROR_PTR(
"sa not defined", __func__, NULL);
527 sap = sarrayCreate(0);
528 for (i = start; i < stop; i++) {
529 str = sarrayGetString(sa, i,
L_COPY);
530 sarrayAddString(sap, str,
L_INSERT);
532 str = sarrayGetString(sa, stop,
L_COPY);
533 str[charindex + 1] =
'\0';
534 newstr = stringJoin(str,
";");
535 sarrayAddString(sap, newstr,
L_INSERT);
537 protostr = sarrayToString(sap, 2);
539 cleanstr = cleanProtoSignature(protostr);
559cleanProtoSignature(
char *instr)
563char externstring[] =
"extern";
564l_int32 i, j, nwords, nchars, index, len;
568 return (
char *)ERROR_PTR(
"instr not defined", __func__, NULL);
570 sa = sarrayCreateWordsFromString(instr);
571 nwords = sarrayGetCount(sa);
572 saout = sarrayCreate(0);
573 sarrayAddString(saout, externstring,
L_COPY);
574 for (i = 0; i < nwords; i++) {
575 str = sarrayGetString(sa, i,
L_NOCOPY);
576 nchars = strlen(str);
578 for (j = 0; j < nchars; j++) {
579 if (index > L_BUF_SIZE - 6) {
581 sarrayDestroy(&saout);
582 return (
char *)ERROR_PTR(
"token too large", __func__, NULL);
588 }
else if (str[j] ==
')') {
592 buf[index++] = str[j];
596 sarrayAddString(saout, buf,
L_COPY);
601 cleanstr = sarrayToString(saout, 2);
602 len = strlen(cleanstr);
603 cleanstr[len - 1] =
'\0';
606 sarrayDestroy(&saout);
621skipToEndOfFunction(
SARRAY *sa,
627l_int32 soffsetlb, boffsetlb, toffsetlb;
630 return ERROR_INT(
"sa not defined", __func__, 1);
632 return ERROR_INT(
"&next not defined", __func__, 1);
634 getOffsetForCharacter(sa, start,
'{', &soffsetlb, &boffsetlb,
636 skipToMatchingBrace(sa, start + soffsetlb, boffsetlb, &end, &rbindex);
664skipToMatchingBrace(
SARRAY *sa,
671l_int32 i, j, jstart, n, sumbrace, found, instring, nchars;
674 return ERROR_INT(
"sa not defined", __func__, 1);
676 return ERROR_INT(
"&stop not defined", __func__, 1);
678 return ERROR_INT(
"&rbindex not defined", __func__, 1);
682 n = sarrayGetCount(sa);
685 for (i = start; i < n; i++) {
686 str = sarrayGetString(sa, i,
L_NOCOPY);
689 jstart = lbindex + 1;
690 nchars = strlen(str);
691 for (j = jstart; j < nchars; j++) {
694 if (j == jstart && str[j] ==
'\"')
695 instring = 1 - instring;
696 if (j > jstart && str[j] ==
'\"' && str[j-1] !=
'\\')
697 instring = 1 - instring;
700 if (str[j] ==
'{' && str[j+1] !=
'\'' && !instring) {
702 }
else if (str[j] ==
'}' && str[j+1] !=
'\'' && !instring) {
717 return ERROR_INT(
"matching right brace not found", __func__, 1);
739skipToSemicolon(
SARRAY *sa,
745l_int32 i, j, n, jstart, nchars, found;
748 return ERROR_INT(
"sa not defined", __func__, 1);
750 return ERROR_INT(
"&next not defined", __func__, 1);
753 n = sarrayGetCount(sa);
755 for (i = start; i < n; i++) {
756 str = sarrayGetString(sa, i,
L_NOCOPY);
759 jstart = charindex + 1;
760 nchars = strlen(str);
761 for (j = jstart; j < nchars; j++) {
773 return ERROR_INT(
"semicolon not found", __func__, 1);
804getOffsetForCharacter(
SARRAY *sa,
812l_int32 i, j, n, nchars, totchars, found;
815 return ERROR_INT(
"sa not defined", __func__, 1);
817 return ERROR_INT(
"&soffset not defined", __func__, 1);
819 return ERROR_INT(
"&boffset not defined", __func__, 1);
821 return ERROR_INT(
"&toffset not defined", __func__, 1);
824 *pboffset = 100000000;
825 *ptoffset = 100000000;
827 n = sarrayGetCount(sa);
830 for (i = start; i < n; i++) {
831 if ((str = sarrayGetString(sa, i,
L_NOCOPY)) == NULL)
832 return ERROR_INT(
"str not returned; shouldn't happen", __func__, 1);
833 nchars = strlen(str);
834 for (j = 0; j < nchars; j++) {
835 if (str[j] == tchar) {
846 *psoffset = i - start;
848 *ptoffset = totchars + j;
892getOffsetForMatchingRP(
SARRAY *sa,
902l_int32 i, j, n, nchars, totchars, leftmatch, firstline, jstart, found;
905 return ERROR_INT(
"sa not defined", __func__, 1);
907 return ERROR_INT(
"&soffset not defined", __func__, 1);
909 return ERROR_INT(
"&boffset not defined", __func__, 1);
911 return ERROR_INT(
"&toffset not defined", __func__, 1);
914 *pboffset = 100000000;
915 *ptoffset = 100000000;
917 n = sarrayGetCount(sa);
919 totchars = toffsetlp;
921 firstline = start + soffsetlp;
922 for (i = firstline; i < n; i++) {
923 if ((str = sarrayGetString(sa, i,
L_NOCOPY)) == NULL)
924 return ERROR_INT(
"str not returned; shouldn't happen", __func__, 1);
925 nchars = strlen(str);
928 jstart = boffsetlp + 1;
929 for (j = jstart; j < nchars; j++) {
932 else if (str[j] ==
')')
934 if (leftmatch == 0) {
942 totchars += nchars - boffsetlp;
948 *psoffset = i - start;
950 *ptoffset = totchars + j;