@@ -142,6 +142,9 @@ static void dostate(void);
142142static void addwhile (int * ptr );
143143static void delwhile (void );
144144static int * readwhile (void );
145+ static void remember_builtin_string (const char * name ,const char * value );
146+ static void register_builtin_string (void );
147+ static void delete_builtin_string (void );
145148
146149typedef void (SC_FASTCALL * OPCODE_PROC )(char * name );
147150typedef struct {
@@ -796,6 +799,7 @@ int pc_compile(int argc, char *argv[])
796799 #endif
797800 delete_autolisttable ();
798801 delete_heaplisttable ();
802+ delete_builtin_string ();
799803 if (errnum != 0 ) {
800804 if (strlen (errfname )== 0 )
801805 pc_printf ("\n%d Error%s.\n" ,errnum ,(errnum > 1 ) ? "s" : "" );
@@ -1252,8 +1256,17 @@ static void parseoptions(int argc,char **argv,char *oname,char *ename,char *pnam
12521256 error (200 ,argv [arg ],sNAMEMAX ); /* symbol too long, truncated to sNAMEMAX chars */
12531257 } /* if */
12541258 strlcpy (str ,argv [arg ],i + 1 ); /* str holds symbol name */
1255- i = atoi (ptr + 1 );
1256- add_builtin_constant (str ,i ,sGLOBAL ,0 );
1259+ if (* (ptr + 1 )== '\"' ) { /* check if value starts with quote */
1260+ char strval [_MAX_PATH ];
1261+ strlcpy (strval ,ptr + 2 ,sizeof (strval )); /* copy string value (skip =") */
1262+ int len = strlen (strval );
1263+ if (len > 0 && strval [len - 1 ]== '\"' ) /* remove trailing quote */
1264+ strval [len - 1 ]= '\0' ;
1265+ remember_builtin_string (str ,strval ); /* store as string constant */
1266+ } else {
1267+ i = atoi (ptr + 1 ); /* convert numeric value */
1268+ add_builtin_constant (str ,i ,sGLOBAL ,0 ); /* store as integer constant */
1269+ }
12571270 } else if (oname ) {
12581271 strlcpy (str ,argv [arg ],sizeof (str )- 2 ); /* -2 because default extension is ".p" */
12591272 set_extension (str ,".p" ,FALSE);
@@ -1592,6 +1605,8 @@ static void setstringconstants()
15921605 add_builtin_string_constant ("__time" ,timebuf ,sGLOBAL );
15931606 strftime (datebuf ,sizeof (datebuf ),"%d %b %Y" ,localtime (& now ));
15941607 add_builtin_string_constant ("__date" ,datebuf ,sGLOBAL );
1608+
1609+ register_builtin_string ();
15951610}
15961611
15971612static int getclassspec (int initialtok ,int * fpublic ,int * fstatic ,int * fstock ,int * fconst )
@@ -1664,6 +1679,59 @@ static int getclassspec(int initialtok,int *fpublic,int *fstatic,int *fstock,int
16641679 return err == 0 ;
16651680}
16661681
1682+ static void remember_builtin_string (const char * name ,const char * value )
1683+ {
1684+ builtinstring * tbl = & builtin_strings ;
1685+ int newsize ;
1686+ void * p ;
1687+
1688+ assert (tbl != NULL );
1689+
1690+ /* Expand the array if necessary */
1691+ if (tbl -> count >= tbl -> size ) {
1692+ newsize = (tbl -> size == 0 ) ? 2 : tbl -> size * 2 ;
1693+ p = realloc (tbl -> entries ,newsize * sizeof (* tbl -> entries ));
1694+
1695+ if (p == NULL ) {
1696+ error (103 ); /* insufficient memory */
1697+ return ;
1698+ }
1699+
1700+ tbl -> entries = p ;
1701+ tbl -> size = newsize ;
1702+ }
1703+
1704+ /* Copy the name and value into the next available slot */
1705+ strlcpy (tbl -> entries [tbl -> count ].name ,name ,sizeof (tbl -> entries [tbl -> count ].name ));
1706+ strlcpy (tbl -> entries [tbl -> count ].value ,value ,sizeof (tbl -> entries [tbl -> count ].value ));
1707+ tbl -> count ++ ;
1708+ }
1709+
1710+ static void register_builtin_string (void )
1711+ {
1712+ builtinstring * tbl = & builtin_strings ;
1713+
1714+ assert (tbl != NULL );
1715+
1716+ /* Add each remembered string constant to the global symbol table */
1717+ for (int i = 0 ; i < tbl -> count ; ++ i ) {
1718+ add_builtin_string_constant (tbl -> entries [i ].name ,tbl -> entries [i ].value ,sGLOBAL );
1719+ }
1720+ }
1721+
1722+ static void delete_builtin_string (void )
1723+ {
1724+ builtinstring * tbl = & builtin_strings ;
1725+
1726+ assert (tbl != NULL );
1727+
1728+ /* Release the entries array and reset counters */
1729+ free (tbl -> entries );
1730+ tbl -> entries = NULL ;
1731+ tbl -> count = 0 ;
1732+ tbl -> size = 0 ;
1733+ }
1734+
16671735/* parse - process all input text
16681736 *
16691737 * At this level, only static declarations and function definitions are legal.
0 commit comments