compounds literal and c99

andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

compounds literal and c99

Post by andromeda92 »

Hi,
I saw that c99 was not supported on either compiler, compound literals error ...
Is it planned to allow it in the compiler (c99)? zcc -cpm ...
because I have to stop my project, I really need to initialize literals in a structure

unless you have an idea how to fix this:

Code: Select all

typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;    
    int item_default;
    int item_selected;
    Color bg;
    Color fg;
    Color bg_sel;
    Color fg_sel;
    char **item_menu;
} MNU;

static MNU main_menu = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black,
                   (char*[]){
					"System Color        ",
					"Test input          ",
					"Ascii chart         ",
					"Horizontal menu     ",
					"Submenu test demo > ",
					"Test box frame      ",
					"Quit application    ",
					NULL 
				   }
                 };
Thank for your help.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

adding --std-c99 on command line is a same.
stefano
Well known member
Posts: 2218
Joined: Mon Jul 16, 2007 7:39 pm

Re: compounds literal and c99

Post by stefano »

Z88dk provides two different compiler engines, try building your program with :
Zcc +cpm -compiler=sdcc. .....
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

that's what I did but I still get the error, I have the latest version of z88dk:
Makefile:

Code: Select all

CFLAGS = -compiler=sdcc --std-c99 -O3 -DAMALLOC 

all: menuvga.com

menuvga.com: src/menuvga. c zcc +cpm -vn $(CFLAGS) -create-app src/menuvga.c src/cpmconio.c src/app.c src/menu.c src/gui.c src/input.c src/cpmkey.c - obin/menuvga.bin 

clean: del bin/*.com
Last edited by andromeda92 on Fri Aug 30, 2024 10:34 pm, edited 2 times in total.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

Error:

Code: Select all

zcc +cpm -vn --std-c99 -compiler=sdcc -O3 -DAMALLOC -create-app src/menuvga.c src/cpmconio.c src/app.c src/menu.c src/gui.c src/input.c src/cpmkey.c -obin/menuvga.bin
srcapp.h:23: error 254: compound literals require ISO C99 or later and are not implemented
make: *** [menuvga.com] Error 1
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

app.h

Code: Select all

#ifndef _APP_H
#define _APP_H

#include <stdbool.h>
#include "mnu.h"

static MNU main_menu = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black,
                   (char*[]){
					"System Color        ",
					"Test input          ",
					"Ascii chart         ",
					"Horizontal menu     ",
					"Submenu test demo > ",
					"Test box frame      ",
					"Quit application    ",
					NULL 
				   }
                 };
                 ...
                 ...
User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

I’m afk at the moment and can’t try it out. But if you remove the “cast” then I think it works.

Given that the struct is defined I can’t see why compound literals would be needed.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

removing cast i have this:
zcc +cpm -vn -compiler=sdcc -O3 -DAMALLOC -create-app src/menuvga.c src/cpmconio.c src/app.c src/menu.c src/gui.c src/input.c src/cpmkey.c -obin/menuvga.bin
src/menuvga.c:76: warning 126: unreachable code
srcapp.h:13: warning 147: excess elements in scalar initializer after 'main_menu'
srcapp.h:13: warning 147: excess elements in scalar initializer after '__xinit_main_menu'
srcapp.h:13: warning 147: excess elements in scalar initializer after 'main_menu'
srcapp.h:13: warning 147: excess elements in scalar initializer after '__xinit_main_menu'
xcopy /Y /D bin\menuvga.com D:\\EMULATOR\\RUNCPM\\B\\0\\
bin\MENUVGA.COM
1 fichier(s) copié(s)
* Terminal will be reused by tasks, press any key to close it.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

adding const and changing the { and }, i don't have error anymore

Code: Select all

const static MNU main_menu = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black,
                                {"System Color       "},
                                {"Test input         "},
                                {"Ascii chart        "},
                                {"Horizontal menu    "},
                                {"Submenu test demo >"},
                                {"Test box frame     "},
                                {"Quit application   "},
                                {NULL}
                                
                        };
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

even though it compiles at the operating level it does not work,
it displays hieroglyphic characters,
I put the source of a simple program, you can test it.

Code: Select all

#include <stdio.h>

typedef struct 
{
    char **tab;
} PTAB;

PTAB ar_tab = {
                {"un"},
                {"deux"},
                {"trois"},
                {"quatre"},
                {"cinq"},
                {"six"},
                {NULL}        
            };

 int main(void)
 {
    char *item = NULL;
    int i=0;
    while(ar_tab.tab[i] != NULL)
    {
        item = (char *)ar_tab.tab[i];
        printf("%s\r\n", item);
        i++;
    }
  

    return 0;
}
Makefile

Code: Select all

NAME=parray
RUNCPM_PATH=D:\\EMULATOR\\RUNCPM\\B\\0\\
CFLAGS = -compiler=sdcc -O3 -DAMALLOC 
all: parray.com 

parray.com: src/main.c 
	zcc +cpm -vn $(CFLAGS) -create-app src/main.c -obin/$(NAME).bin
	xcopy /Y /D bin\$(NAME).com $(RUNCPM_PATH)
clean:
	del bin/*.com

User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

I’m back in a couple of days so I’ll be able to check then.

Please hold on! If there is an issue I’ll get it sorted.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

thank you very much,
I would wait
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

I just put the solution that I found without error and that works, I don't know if it's the right one, you will tell me what you think.

I have to force the declaration of the table in the structure by putting an index and the initialization changes at the level of the braces.

Code: Select all

typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;    
    int item_default;
    int item_selected;
    Color bg;
    Color fg;
    Color bg_sel;
    Color fg_sel;
    const char *item_menu[15];    
} MNU;

static MNU main_menu = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black,
                                {{"System Color        "},
                                {"Test input          "},
                                {"Ascii chart         "},
                                {"Horizontal menu     "},
                                {"Submenu test demo > "},
                                {"Test box frame      "},
                                {"Quit application    "},                              
                                {NULL}}
                                
                        };
normally I have to put zero the structure before using it but I don't know how to do it with memset as there is a pointer array.

even if there are 15 indices on the array I don't fill it entirely with NULL, because I stop at the number of elements (nb_item).
Thanks
User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

An alternative is to use a variable length array as the last member of the struct:

Code: Select all

typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;
    int item_default;
    int item_selected;
    Color bg;
    Color fg;
    Color bg_sel;
    Color fg_sel;
    const char *item_menu[];
} MNU;
The downside being that the size can't be determined, so it really only works with global structures.

Initialisation can be the same:

Code: Select all

static MNU main_menu = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black,
                                {{"System Color        "},
                                {"Test input          "},
                                {"Ascii chart         "},
                                {"Horizontal menu     "},
                                {"Submenu test demo > "},
                                {"Test box frame      "},
                                {"Quit application    "},
                                {NULL}}
                        };
To cut down the braces, you can then used a designated initialiser:

Code: Select all

static MNU main_menu2 = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black, .item_menu = {
                                "System Color        ",
                                "Test input          ",
                                "Ascii chart         ",
                                "Horizontal menu     ",
                                "Submenu test demo > ",
                                "Test box frame      ",
                                "Quit application    ",
                                NULL
                           }
                        };
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

Thank your for your informations.
i tested with
const char *item_menu[];
it don't work, i have hieroglyph on all screen.

My structure is global, is in .h file.

i don't need that size can be determined because i use nb_item.

maybe you mean to declare a global variable length_array and do
const char *item_menu[length_global]; ?

[edit]
i tested witdh global variable length, it don't work, it compil bu i have hieroglyph

static int length_array = 15;
const char *item_menu[length_array];
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

with this it work.

Code: Select all

#define LENGTH_ARRAY 15

typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;    
    int item_default;
    int item_selected;
    Color bg;
    Color fg;
    Color bg_sel;
    Color fg_sel;
    const char *item_menu[LENGTH_ARRAY];    
} MNU;
User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

andromeda92 wrote: Mon Sep 02, 2024 10:21 pm Thank your for your informations.
i tested with
const char *item_menu[];
it don't work, i have hieroglyph on all screen.
I can't replicate that, the following:

Code: Select all

// zcc +test eg.c
// z88dk-ticks a.bin

#include <stdio.h>

typedef enum {
  Black,
  White,
  Cyan,
} Color;


typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;
    int item_default;
    int item_selected;
    Color bg;
    Color fg;
    Color bg_sel;
    Color fg_sel;
    const char *item_menu[];
} MNU;

static MNU main_menu2 = {5, 5, 7, 20, 1, -1, Black, White, Cyan, Black, .item_menu = {
                                "System Color        ",
                                "Test input          ",
                                "Ascii chart         ",
                                "Horizontal menu     ",
                                "Test box frame      ",
                                "Quit application    ",
                                NULL
                           }
                        };



int main(void)
 {
    const char *item = NULL;
    int i=0;
    while(main_menu2.item_menu[i] != NULL)
    {
        item = main_menu2.item_menu[i];
        printf("%s\r\n", item);
        i++;
    }


    return 0;
}
Generates this:

Code: Select all

System Color

Test input

Ascii chart

Horizontal menu

Test box frame

Quit application
Which is as expected. Can you try out the above (commands at the top of the code snippet) and see if that works. If it does then we need to compare your code and the example and see what went wrong.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

when I do your program it works, in my case it's a little different because I have to display character by character
the menu item which is a string, below is the code that works:

Code: Select all

#include <stdio.h>

typedef struct
{
    int x;
    int y;
    int nb_item;
    int width_item_max;    
    int item_default;
    int item_selected;
    int bg;
    int fg;
    int bg_sel;
    int fg_sel;
    const char *item_menu[];    
} MNU;

static MNU main_menu = {5, 5, 7, 20, 1, -1, 0, 7, 4, 0, .item_menu = {
                                "System Color        ",
                                "Test input          ",
                                "Ascii chart         ",
                                "Horizontal menu     ",
                                "Submenu test demo > ",
                                "Test box frame      ",
                                "Quit application    ",                              
                                NULL
                              }
                                
                        };



 int main(void)
 {
    const char *item = NULL;
    int i=0;
    while(main_menu.item_menu[i] != NULL)
    {
        item = main_menu.item_menu[i];
        printf("%s\r\n", item);
        i++;
    }


    return 0;
}
]
Capture2.JPG
What I want is this but it not work:

Code: Select all

...
...
 item = menu.item_menu[i];
           
            for (j = 0; j < width; j++)
            {
                /*if (item == NULL)
                {
                    item = menu.item_menu[i];                                    
                } */               
                 
                if (item && item[0] )
                {                
                    //cputch(*item++);  
                    cputch(item[j]);                  
                }
                else
                {                    
                    cputs(" ");
                }
            }
            ...
            ...
[attachment=1]Capture.JPG[/attachment
You do not have the required permissions to view the files attached to this post.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

my Makefile is

Code: Select all

NAME=menuvga
RUNCPM_PATH=D:\\EMULATOR\\RUNCPM\\B\\0\\
CFLAGS = -compiler=sdcc -O3 -DAMALLOC 
all: menuvga.com 

menuvga.com: src/menuvga.c 
	zcc +cpm -vn $(CFLAGS) -create-app src/menuvga.c src/cpmconio.c src/app.c src/menu.c src/gui.c src/input.c src/cpmkey.c -obin/menuvga.bin
	xcopy /Y /D bin\$(NAME).com $(RUNCPM_PATH)
clean:
	del bin/*.com
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

putting your program that work on my source not work, nitems = 7

Code: Select all

for (i = 0; i < nitems; i++)
        {
            gotoxy(menu.x, menu.y + i + 1);
            cputch(ACS_VLINE);
            cputch(' ');
            
            if (i == itemno) {               
                textattr(menu.bg_sel, menu.fg_sel);
            }
           
            item = menu.item_menu[i];			//<== your program
            printf("%s\r\n", item);				// <== your program
                      
           /* for (j = 0; j < width; j++)
            {
                //if (item == NULL)
                //{
                //    item = menu.item_menu[i];                                    
                //}               
                 
                if (item)
                {                
                    //cputch(*item++);  
                    cputch(item[j]);                  
                }
                else
                {                    
                    cputs(" ");
                }
                
            }*/
           
            textattr(menu.bg, menu.fg);            
            cputs(" ");
            cputch(ACS_VLINE);
        }
User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

I can't reproduce it with this function which is equivalent to yours:

Code: Select all

int main(void)
 {
    const char *item = NULL;
    int i=0;
    while(main_menu2.item_menu[i] != NULL)
    {
        item = main_menu2.item_menu[i];
        int width = main_menu2.width_item_max;
        for (int j = 0; j < width; j++)
            {
                if (item && item[0] )
                {
                    putchar(*item++);
                }
                else
                {
                    cputs(" ");
                }
        }
        i++;
    }


    return 0;
}
Produces:

Code: Select all

System Color        Test input          Ascii chart         Horizontal menu     Test box frame      Quit application
The difference being that I've not got a cputch() function so I've replaced it with a z88dk builtin - can you share the implementation of cputch()?

This is an odd one, we'll get to the bottom of it - probably easiest to drop an email (dom@) with the project and I'll figure it out.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

i will test your program.
cputch invoke bdos

Code: Select all

void cputch(int ch)
{       
  write_console(ch);
}
write_console is:

Code: Select all

void write_console(int ch)
{
 bios(bios_write_char, ch, 0);
}
bios_write is:

Code: Select all

void setup_devices(void) {
    bios_char_avail = use_aux ? AUXIST : CONST;
    bios_write_char = use_aux ? AUXOUT : CONOUT;
    bios_read_char  = use_aux ? AUXIN  : CONIN;
}
all code for bdos:

Code: Select all

int use_aux = false;

/* BIOS functions for send/receive via the console device. */
#define CONST 2
#define CONIN 3
#define CONOUT 4

/* BIOS functions for send/receive via the auxillary device. */
#define AUXOUT  6
#define AUXIN   7
#define AUXIST 18

/* Use the console by default. */
int bios_char_avail = CONST;
int bios_write_char = CONOUT;
int bios_read_char  = CONIN;

void setup_devices(void) {
    bios_char_avail = use_aux ? AUXIST : CONST;
    bios_write_char = use_aux ? AUXOUT : CONOUT;
    bios_read_char  = use_aux ? AUXIN  : CONIN;
}

int console_char_available(void) { return bios(bios_char_avail, 0, 0) != 0; }
void write_console(int ch) { bios(bios_write_char, ch, 0); }
int read_console(void) { return bios(bios_read_char, 0, 0); }
I use this code on all my program and it work:
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

I think I may have found the problem, by integrating your code, with putchar or cputch or printf it does not work, by modifying in the MNU structure

const char *item_menu[15];

your code works, so I deduce that it can come from either:

a memory saturation for CP/M or a memory overflow
a compiler optimization, a compiler parameter.
pointer problème maybe 32bit or 16bit or other, i don't know

this code works by modifying the structure on RunCPM windows,
Agonlight2 CP/M 2.2, and RuncPM on esp32

there is a problem with CP/M at the memory level.
I am not technical enough to know where the problem comes from.

on the other hand the same program modified for agonLight2 no CP/M with ez80 not Z80 on terminal mode, without using CP/M of course.
i have not limitation with AgDev.
User avatar
dom
Well known member
Posts: 2194
Joined: Sun Jul 15, 2007 10:01 pm

Re: compounds literal and c99

Post by dom »

I'm baffled by this one, looking at the generated assembler the only difference is the extra padding for the item_menu member.

If you don't mind, I'd really appreciate it if you sent me a zip of your project - I'm obviously missing something in my reproduction attempts.
andromeda92
Member
Posts: 62
Joined: Wed Apr 17, 2024 1:05 am

Re: compounds literal and c99

Post by andromeda92 »

I attached the zip of the project:
what is useful is the source directory, the Makefile and .vscode, because I use vscode just for the editor
the tasks.json file is just to call make all.
the python .py files will not be used.
you have to change Make file, because i transfert .com file to directory RunCPM it'a same on tasks.json
You do not have the required permissions to view the files attached to this post.
Post Reply