6. LCC-CPP of C language Preprocessor: Include. C code analysis

Source: Internet
Author: User

D: \ thunder Download \ lj1020-1022-hb-pd-win2kxp-sc (1) \ lj1020-1022-HB-pd-win2kxp-sc

6. LCC-CPP of C language Preprocessor: Include. C code analysis

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include "CPP. H"
# Include <windows. h>

Includelist [ninclude];

Extern char * objname;

Void // process include files
Doinclude (tokenrow * Trp)
{
Char fname [512] = {0}, INAME [512] = {0 };
Includelist * IP;
Int angled, Len, I;
File * FD;

TRP-> TP + = 1;
If (Trp-> TP> = Trp-> LP)
Goto syntax;
If (Trp-> TP-> type! = String & Trp-> TP-> type! = Lt) // if it is not a string or <, replace the macro first.
{
Len = Trp-> TP-Trp-> bp;
Expandrow (Trp, "<include> ");
TRP-> TP = Trp-> bp + Len;
}
// # Include "XXX"
If (Trp-> TP-> type = string)
{
Len = Trp-> TP-> len-2;
If (LEN> sizeof (fname)-1)
Len = sizeof (fname)-1;
Strncpy (fname, (char *) TRP-> TP-> t + 1, Len );
Angled = 0;
}
// # Include <XXX>
Else if (Trp-> TP-> type = lt)
{
Len = 0;
TRP-> TP ++;
While (Trp-> TP-> type! = Gt)
{
If (Trp-> TP> Trp-> LP | Len + Trp-> TP-> Len + 2> = sizeof (fname ))
Goto syntax;
// Copy the <> internal string.
Strncpy (fname + Len, (char *) TRP-> TP-> T, Trp-> TP-> Len );
Len + = Trp-> TP-> Len;
TRP-> TP ++;
}
Angled = 1;
}
// Others are incorrect.
Else
Goto syntax;
TRP-> TP + = 2;
If (Trp-> TP <Trp-> LP | Len = 0)
Goto syntax;
Fname [Len] = '\ 0 ';
// Do fopen if it is absolute path
// Jedi path.
If (fname [0] = '/' | fname [0] = '\' |
(Fname [1] = ':'&&
(Fname [0]> = 'A' & fname [0] <= 'Z' |
Fname [0]> = 'A' & fname [0] <= 'Z ')
)
)
{
Outputdebugstring (fname );
Outputdebugstring ("\ n ");
// Open directly.
FD = fopen (fname, "R ");
Strcpy (INAME, fname );
}
Else
{
If (! Angled & cursource-> filename)
{// Search in the current path.
// Search in current file directory first
Source * s = cursource;
Char * P;
P = strrchr (S-> filename ,'/');
If (! P)
P = strrchr (S-> filename ,'\\');
If (P)
{
Strncpy (INAME, S-> filename, p-S-> filename );
INAME [p-S-> filename] = 0;
Strcat (INAME ,"\\");
Strcat (INAME, fname );
Outputdebugstring (INAME );
Outputdebugstring ("\ n ");
If (FD = fopen (INAME, "R "))! = NULL)
Goto found_file;
}
}
For (FD = NULL, I = NINCLUDE-1; I> = 0; I --)
{// Start from all-I settings.
IP = & includelist [I];
If (IP-> file = NULL | IP-> deleted | (angled & IP-> always = 0 ))
Continue;
If (strlen (fname) + strlen (IP-> file) + 2> sizeof (INAME ))
Continue;
Strcpy (INAME, IP-> file );
Strcat (INAME ,"\\");
Strcat (INAME, fname );
Outputdebugstring (INAME );
Outputdebugstring ("\ n ");
// Find the owner.
If (FD = fopen (INAME, "R "))! = NULL)
Break;
}
}
Found_file:
If (/* mflag> 1 | */! Angled & mflag = 2) // print the dependent file.
{
// Fwrite (objname, 1, strlen (objname), stdout );
Fwrite (INAME, 1, strlen (INAME), stdout );
Fwrite ("\\\ N", 1, 4, stdout );
}
Else if (mflag = 1 & * INAME)
{
// Fwrite (objname, 1, strlen (objname), stdout );
Fwrite (INAME, 1, strlen (INAME), stdout );
Fwrite ("\\\ N", 1, 4, stdout );
}
If (FD! = NULL)
{
If (++ incdepth> 10)
Error (e_fatal, "# include too deeply nested ");
Setsource (char *) newstring (uchar *) INAME, strlen (INAME), 0), FD, null );
Genline ();
}
Else
{
TRP-> TP = Trp-> bp + 2;
Error (e_error, "cocould not find include file % R", TRP );
}
Return;
Syntax:
If (! Skipping)
Error (e_error, "syntax error in # include ");
Return;
}

/*
* Generate a line directive for cursource
*/
Void // generate # Line numbler filename
Genline (void)
{
Static token TA = {unclass };
Static tokenrow tr ={& Ta, & TA, & ta + 1, 1 };
Uchar * P;
If (cursource-> line <= 0) return;
Ta. t = P = (uchar *) outp;
Strcpy (char *) P, "# Line ");
P + = sizeof ("# Line")-1;
P = (uchar *) outnum (char *) P, cursource-> line );
* P ++ = ''; * P ++ = '"';
Strcpy (char *) P, cursource-> filename );
P + = strlen (char *) P );
* P ++ = '"'; * P ++ = '\ n ';
Ta. Len = (char *) P-outp;
Outp = (char *) P;
Tr. Tp = tr. BP;
Puttokens (& TR );
}

/*
* Generate n' \ n' characters
*/
Void // generate n linefeeds.
Genblankline (int n)
{
Static token TA = {unclass };
Static tokenrow tr ={& Ta, & TA, & ta + 1, 1 };
Uchar * P;
Int I;
Ta. t = P = (uchar *) outp;
For (I = 0; I <n; I ++)
* P ++ = '\ n ';
Ta. Len = (char *) P-outp;
Outp = (char *) P;
Tr. Tp = tr. BP;
Puttokens (& TR );
}
/*
Set the dependence file of the. o file, for example:
Test. O :\
Test. c \
*/
Void
Setobjname (char * F)
{
Int n = strlen (f );
Const char * P = "O:\\\ N";

Objname = (char *) domalloc (n + strlen (p) + 1 );
Strcpy (objname, F );
If (objname [N-2] = '.'){
Strcpy (objname + n-1, P );
} Else {
Strcpy (objname + n, p); // copy directly if no. exists.
}
Fwrite (objname, 1, strlen (objname), stdout );
Fwrite (F, 1, strlen (F), stdout );
Fwrite ("\\\ N", 1, 4, stdout );
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.