Sujet : Re: C23 thoughts and opinions
De : malcolm.arthur.mclean (at) *nospam* gmail.com (Malcolm McLean)
Groupes : comp.lang.cDate : 01. Jun 2024, 01:53:02
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v3drdg$2f1pg$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
User-Agent : Mozilla Thunderbird
On 31/05/2024 13:55, bart wrote:
On 30/05/2024 16:03, Michael S wrote:
On Thu, 30 May 2024 15:48:39 +0100
bart <bc@freeuk.com> wrote:
>
>
Where do the _binary_logo_bmp_start and ...-size symbols come from?
That is, how do they get into the object file.
>
>
objcopy generates names of the symbols from the name of input binary
file. I would think that it is possible to change these symbols to
something else, but I am not sure that it is possible withing the same
invocation of objcopy. It certainly is possible with a second pass.
Lawrence probably can give more authoritative answer.
Or as a last resort you can RTFM.
>
I gave myself the simple task of incorporating the source text of hello.c into a program, and printing it out.
Here's how builtin embedding worked using a feature of my older C compiler:
#include <stdio.h>
#include <string.h>
char hello[] = strinclude("hello.c");
int main(void) {
printf("hello =\n%s\n", hello);
printf("strlen(hello) = %zu\n", strlen(hello));
printf("sizeof(hello) = %zu\n", sizeof(hello));
}
I build it and run it like this:
C:\c>bcc c
Compiling c.c to c.exe
C:\c>c
hello =
#include "stdio.h"
int main(void) {
printf("Hello, World!\n");
}
strlen(hello) = 70
sizeof(hello) = 71
C:\c>dir hello.c
31/05/2024 13:48 70 hello.c
It just works; no messing about with objcopy parameters; no long unwieldy names; no link errors due to unsupported file formats; no problems with missing terminators for embedded text files imported as strings; no funny ways of getting size info.
Here's my solution. It's a bit more complicated.
int bbx_write_source (const char *source_xml, char *path, const char *source_xml_file, const char *source_xml_name)
{
XMLDOC *doc = 0;
char error[1024];
char buff[1024];
XMLNODE *root;
XMLNODE *node;
const char *name;
FILE *fpout;
FILE *fpin;
int ch;
doc = xmldocfromstring(source_xml, error, 1024);
if (!doc)
{
fprintf(stderr, "%s\n", error);
return -1;
}
root = xml_getroot(doc);
if (strcmp(xml_gettag(root), "FileSystem"))
return -1;
if (!root->child)
return -1;
if (strcmp(xml_gettag(root->child), "directory"))
return -1;
for (node = root->child->child; node != NULL; node = node->next)
{
if (!strcmp(xml_gettag(node), "file"))
{
name = xml_getattribute(node, "name");
snprintf(buff, 1024, "%s%s", path, name);
fpout = fopen(buff, "w");
if (!fpout)
break;
fpin = file_fopen(node);
if (!fpin)
break;
if (!strcmp(name, source_xml_file))
{
char *escaped = texttostring(source_xml);
if (!escaped)
break;
fprintf(fpout, "char %s[] = %s;\n", source_xml_name, escaped);
free(escaped);
}
else
{
while ((ch = fgetc(fpin)) != EOF)
fputc(ch, fpout);
}
fclose(fpout);
fclose(fpin);
fpout = 0;
fpin = 0;
}
}
if (fpin || fpout)
{
fclose(fpin);
fclose(fpout);
return -1;
}
return 0;
}
It's leveraging the Baby X resource compiler, the xmparser, and my filesystem programs. You can't include the source of a program in the program as a C string, because then the source changes to include that string. So what you do is this.
You first place a placeholder C source file containing a short dummy string.
The you convert the source to an XML file, and turn it into a string with the Baby X Resource compiler. Then you drop the source into the
file, removing the placeholder.
Then the program walks the file list, detects that file, and replaces it with the xml string it has been passed.
And this system works, and it's an easy way of adding source output to
ptograms. Of course the function now needs to be modified to walk the entire tree recursively and I will need a makedirectory function. I've got it to work for flat source directories.
-- Check out Basic Algorithms and my other books:https://www.lulu.com/spotlight/bgy1mm