Commit Graph

61 Commits

Author SHA1 Message Date
PatR
b264e17cf2 fix use-after-free bug in curses shutdown
A year ago a use-free-bug was reported for curses.  I don't use ASAN
so haven't reproduced it, but I think this should fix it.

If the RIP window is deleted after the map window has gone away, the
code from commit 5e572d3d5f (post 3.6.7)
would execute and access the internals of the deleted map window.
2026-04-11 01:02:42 -07:00
nhmall
349f3871be preproc fix 2025-01-23 19:37:19 -05:00
PatR
150b331189 analyzer lint for win/curses/*.c
There is still an issue in cursmesg.c.

The last diff band (curses_putch) isn't related to static analysis.
2025-01-23 13:52:19 -08:00
nhmall
0792e5fe9e expand implicit fallthrough detection to non-gcc compilers
gcc has recognized various "magic comments" for white-listing
occurrences of implicit fallthrough in switch statements for
a long time:

    The range and shape of "falls through" comments accepted are
    contingent upon the level of the warning. (The default level is =3.)

    -Wimplicit-fallthrough=0 disables the warning altogether.
    -Wimplicit-fallthrough=1 treats any kind of comment as a "falls through" comment.
    -Wimplicit-fallthrough=2 essentially accepts any comment that contains something
     that matches (case insensitively) "falls?[ \t-]*thr(ough|u)" regular expression.
    -Wimplicit-fallthrough=3 case sensitively matches a wide range of regular
     expressions, listed in the GCC manual. E.g., all of these are accepted:
        /* Falls through. */
        /* fall-thru */
        /* Else falls through. */
        /* FALLTHRU */
        /* ... falls through ... */
       etc.
    -Wimplicit-fallthrough=4 also, case sensitively matches a range of regular
     expressions but is much more strict than level =3.
    -Wimplicit-fallthrough=5 doesn't recognize any comments.

Plenty of other compilers did not recognize the gcc comment convention,
and up until now the compiler warning for detecting unintended
fallthrough had to be suppressed on other compilers. That's because the code
in NetHack has been relying on the gcc approach, and only the gcc approach.

The C23 standard introduces an attribute [[fallthrough]] for the
functionality, when implicit fallthrough warnings have been enabled.

Several popular compilers already support that, or a very similar attribute
style approach, today, even ahead of their C23 support:

       C compiler                       whitelist approach
       ---------------------------   -------------------------------------
       C23 conforming compilers         [[fallthrough]]

       clang versions supporting
       standards prior to
       C23                              __attribute__((__fallthrough__))

       Microsoft Visual Studio
       since VS 2022 17.4.
       The warning C5262 controls
       whether the implict
       fallthrough is detected and
       warned about with
       /std:clatest.                    [[fallthrough]]

This adds support to NetHack for the attribute approach by inserting a
macro FALLTHROUGH to the existing cases that require white-listing, so
other compilers can analyze things too.

The definition of the FALLTHROUGH macro is controlled in include/tradstdc.h.

The gcc comment approach has also been left in place at this time.
2024-11-30 14:16:27 -05:00
nhmall
63294536f6 eliminate gcc 13.2.0 warnings that recently appeared
timeout.c:550:1: warning: no previous prototype for ‘region_dialogue’ [-Wmissing-prototypes]
  550 | region_dialogue(void)
      | ^~~~~~~~~~~~~~~

../win/curses/curswins.c: In function ‘curses_destroy_win’:
../win/curses/curswins.c:202:33: warning: variable ‘dummyht’ set but not used [-Wunused-but-set-variable]
  202 |     int mapwidth = 0, winwidth, dummyht;
      |                                 ^~~~~~~
2024-09-26 09:49:42 -04:00
PatR
5e572d3d5f fix part of github issue #1285 - cursed curses
Issue reported by g-branden-robinson:  vertical status panel ended
up with an extra closing paren on the energy line, and sometimes
popups left some text and/or border to the right of the map.

I haven't been able to reproduce the energy anomaly.  It is possible
that it is dependent on the version of curses.

This fixes the leftover popup traces that the base window catches
(and hangs onto) when there is extra space to the right of the map.
Erasing a popup prior to deleting it suffices to make base window
forget it.

I have a more elaborate fix that covers the space to the right of
the map, when there is some, with an extra window and erases that
window when refreshing the map.  It works but adds a bunch of code
that we can get by without.

Issue #1285 is still open.
2024-09-19 01:41:24 -07:00
PatR
0ce2439e82 github issue #1275: curses init vs pauper
Reported by ars3niy, the curses interface could behave strangely on
the first turn if the 'pauper' option/conduct was specified.

There isn't any definitive flag indicating whether or not the game
has started.  Since 'moves' has traditionally been initialized to 1
rather than to 0, there were several instances of
|  if (moves <= 1 && invent != NULL)
being used to determine the starting state on the assumption that
once hero has inventory, the game has begun.  Introduction of the
'pauper' option made the test for non-Null invent become unreliable.
For paupers, the program would behave as if the game hadn't started
yet until the player finally made a time-consuming move.

This changes compile-time initialization of 'moves' from 1 to 0,
then sets it to 1 when initial inventory would be bestowed (even
when 'pauper' inhibits that).  That's probably not the best place
for it, but testing for 'moves==0' now should produce an identical
effect as 'moves<=1 && invent!=NULL' used to accomplish.

It would have been much simpler just to give paupers 1 gold piece,
or perhaps one rock, in place of usual starting gear so that their
initial inventory wouldn't be empty, but the moves+invent way of
checking for start-of-play has always bothered me.

Should 'pauper' be preventing 'nethack -X' from giving its starting
wand of wishing?  Conducts and explore mode don't really overlap so
maybe it doesn't matter.

Fixes #1275
2024-08-31 14:08:04 -07:00
nhmall
6c0ae092c6 distinguish global variables that get written to savefile
The g? structs had a mix of variables that were written to
the savefile, and those that were not.

For better clarity and to distinguish those that end up in
the savefile, relocate some g? variables that get written
directly to the savefile into different structs.

This updates EDITLEVEL, although technically it probably
didn't need to, since savefile contents are not changing.

Details:

    gb.bases            -> svb.bases
    gb.bbubbles         -> svb.bbubbles
    gb.branches         -> svb.branches
    gc.context          -> svc.context
    gd.disco            -> svd.disco
    gd.dndest           -> svd.dndest
    gd.doors            -> svd.doors
    gd.doors_alloc      -> svd.doors_alloc
    gd.dungeon_topology -> svd.dungeon_topology
    gd.dungeons         -> svd.dungeons
    ge.exclusion_zones  -> sve.exclusion_zones
    gh.hackpid          -> svh.hackpid
    gi.inv_pos          -> svi.inv_pos
    gk.killer           -> svk.killer
    gl.lastseentyp      -> svl.lastseentyp
    gl.level            -> svl.level
    gl.level_info       -> svl.level_info
    gm.mapseenchn       -> svm.mapseenchn
    gm.moves            -> svm.moves
    gm.mvitals          -> svm.mvitals
    gn.n_dgns           -> svn.n_dgns
    gn.n_regions        -> svn.n_regions
    gn.nroom            -> svn.nroom
    go.oracle_cnt       -> svo.oracle_cnt
    gp.pl_character     -> svp.pl_character
    gp.pl_fruit         -> svp.pl_fruit
    gp.plname           -> svp.plname
    gp.program_state    -> svp.program_state
    gq.quest_status     -> svq.quest_status
    gr.rooms            -> svr.rooms
    gs.sp_levchn        -> svs.sp_levchn
    gs.spl_book         -> svs.spl_book
    gt.timer_id         -> svt.timer_id
    gt.tune             -> svt.tune
    gu.updest           -> svu.updest
    gx.xmax             -> svx.xmax
    gx.xmin             -> svx.xmin
    gy.ymax             -> svy.ymax
    gy.ymin             -> svy.ymin

Related note:
There are some pointer variables that are heads of chains that were not
moved from 'g?' to 'sv?', because they are not actually written to the
savefile directly, but the objects/monst/trap/lightsource/timer in the
chains they point to are. That can be changed, if desired.
Examples: gi.invent, gm.migrating_objs, gb.billobjs, gm.migrating_mons,
          gf.ftrap, gl.light_base, gt.timer_base
2024-07-13 14:57:50 -04:00
Pasi Kallinen
7ecc778173 Curses: fix pile hilite background color
The curses colorpair rework I did changed the pile hilite background
color from blue to red. Change it back to blue, and use a function
to get the color pair instead of hardcoding the value.
2024-04-05 20:15:21 +03:00
Pasi Kallinen
fe2f58fbad Curses: Fix accessing illegal window id 2024-03-21 16:55:26 +02:00
nhmall
b6b9bb0f27 follow-up to curses windowcolors
Make sure the windowcolors option can be specified more than
once without a config file warning.

Make the struct holding the details a little more extendable.
2024-03-17 14:18:14 -04:00
Pasi Kallinen
0700f60983 Fix segfault in curses
Curses handles menu and text windows specially; their id numbers
keep going up, so we need to check if the id is for a menu or
text window instead of directly accessing the windows array.
2024-03-17 20:09:43 +02:00
Pasi Kallinen
4030ef13a0 Curses: implement the windowcolors option
Allow changing the curses windows foreground and background colors,
for example:

OPTIONS=windowcolor:menu #8000F0/20F080 message grey/blue
2024-03-17 18:41:52 +02:00
Pasi Kallinen
c91d8d9450 Curses: unnecessary window erasing
When removing a temporary (menu, text, popup) window, the code
was erasing it from memory and then refreshing the screen.
This is unnecessary, as we're refreshing all the windows anyway.
2024-03-16 13:58:28 +02:00
Pasi Kallinen
243f6410d2 Curses: simplify color handling
The map frame (background) colors were all over the place; the
code should be much cleaner now, and still work exactly the same
as before.

I tested this with terminals with 8, 16, and 256 COLORS.
2024-03-15 20:36:56 +02:00
RainRat
a3658f85ac fix typos 2024-02-28 20:15:56 -08:00
Pasi Kallinen
dd5ca5b058 Change menu_headings to accept color and attribute
Instead of just accepting an attribute, it's now possible to
use a color, or both color and attribute, for example:

OPTIONS=menu_headings:inverse
OPTIONS=menu_headings:red
OPTIONS=menu_headings:red&underline

Default is still just inverse.
This lets the player change the menu heading color without
needing to use menu colors for them.

Also makes it so the core uses NO_COLOR instead of 0, for all
the menu lines which don't have any prefedefined color.

Tested for tty, curses, x11, qt, and win32
2023-11-13 07:33:56 +02:00
Ray Chason
9037592ea1 parent 97edec62b5
author Ray Chason <ray.chason@protonmail.com> 1684372172 -0400
committer nhmall <nhmall@nethack.org> 1685414340 -0400

Add configuration to support Curses on WinGUI

and enable support for Unicode on Curses.

Allow NetHackW to select the Curses interface

Reorder drawing of extended command prompt

Curses on WinGUI needs this change. This may be a bug in PDCursesMod,
but it seems to be harmless to the other ports.

Avoid calling Curses after the windows are closed

Provide erase_char and kill_char for WinGUI Curses

Set Lua version to 5.4.6
2023-05-29 22:39:43 -04:00
Pasi Kallinen
201874a85a Curses: delete curses window
When NetHack deletes a window, also free the curses window data.
2023-03-14 11:16:13 +02:00
Michael Meyer
68d1db2203 Initialize scrollbar 'framecolor' before use
Caused an asan crash because the value is used to index an array.
2023-03-01 15:23:46 -05:00
nhmall
095e4ffe31 add some curses support for coloring of map frame 2023-01-01 20:05:03 -05:00
nhmall
02a48aa8cf split g into multiple structures
The consolidation of global variables from scattered source
files into decl.c and declared in decl.h was begun in 3.7.0.
Their placement in common files was done for centralized
initialization and potential re-initialization during a
"play again" scenario.

It wasn't really necessary for all of them to be housed in a
single huge structure to meet the "play again" requirement,
and the single huge structure has been a little unwieldy when
it comes to maintenance.

Following this commit, instead of one single extremely large structure
named 'g' to house all of the relocated global variables, they
are distributed into several ga through gz.

To make things easy for the developer, each variable is placed
into the struct corresponding to the starting letter of the variable.
That way, no lookup is required in order to know which struct houses
a particular variable, it is a simple match to the starting letter
for all the centralized global variables.

A global variable named 'amulets', would be found in ga.
    ga.amulets
     ^ ^
A global varable named 'move', would be found in gm.
    gm.moves
     ^ ^
A global variable named 'val_for_n_or_more' would be found in gv.
    gv.val_for_n_or_more
     ^ ^
A global variable named 'youmonst' would be found in gy.
    gy.youmonst
     ^ ^
2022-11-29 21:53:21 -05:00
Ray Chason
8b6fe9d205 Support wide Curses on MS-DOS
Also, fix IBMGraphics on Curses
2022-10-02 20:41:02 -04:00
PatR
3dea129a38 curses: scrollbars on clipped map
A change to the curses interface from three years ago to make sure
that round-off didn't make the horizontal and vertical clipped map
indicators appear to not be clipped was using ROWNO for both instead
of COLNO for the horizontal one.  For modest clipping the mistake
was unnoticeable; I don't know whether that remained true for more
extreme clipping.

[Not fixed:  the curses scrollbar stuff ignores the fact that map
column 0 is unused.]
2022-09-29 13:58:50 -07:00
Ray Chason
31862b95b0 Fix undefined behavior when exiting Curses
* When saving: curses_exit_nhwindows calls curses_uncurse_terminal,
  which calls endwin. curses_exit_nhwindows then calls raw_print,
  which calls more Curses functions after endwin has been called.
  Fix this by having curses_raw_print use puts if window_inited
  is false.

* When dying, quitting, etc.: really_done opens the "Goodbye" window,
  which refreshes the other windows when it closes. But the status
  window (and possibly the map and message windows) are gone by that
  point. The window pointers are properly NULLed, but the NULL is then
  passed to touchwin. Fix this by checking window pointers for NULL.
2022-09-28 14:23:27 -07:00
Patric Mueller
ced5c2a92f Curses: fix clipped map crash due to uninitialized memory 2022-05-23 00:03:15 +02:00
nhmall
cb0c21e91d ENHANCED_SYMBOLS
A new feature, enabled by default to maximize testing, but one which can
be disabled by commenting it out in config.h

With this, some additional information is added to the glyphmap entries
in a new optional substructure called u with these fields:
    ucolor          RGB color for use with truecolor terminals/platforms.
                    A ucolor value of zero means "not set." The actual
                    rgb value of 0 has the 0x1000000 bit set.
    u256coloridx    256 color index value for use with 256 color
                    terminals, the closest color match to ucolor.
    utf8str         Custom representation via utf-8 string (can be null).

There is a new symset included in the symbols file, called enhanced1.

Some initial code has been added to parse individual
OPTIONS=glyph:glyphid/R-G-B entries in the config file.

The glyphid can, in theory, either be an individual glyph (G_* glyphid)
for a single glyph, or it can be an existing symbol S_ value
(monster, object, or cmap symbol) to store the custom representation for
all the glyphs that match that symbol.

Examples:
   OPTIONS=glyph:G_fountain/U+03A8/0-150-255

(Your platform/terminal font needs to be able to include/display the
character, of course.)

The NetHack core code does parsing and storing the customized
entries, and adding them to the glyphmap data structure.

Any window port can utilize the additional information in the glyphinfo
that is passed to them, once code is added to do so.

Also, consolidate some symbol-related code into symbols.c, and remove it from
files.c and options.c
2022-05-07 10:25:13 -04:00
PatR
41eecdf6b3 curses menu: support backspace/delete for counts
Have curses call the core get_count() routine instead rolling its
own so that backspace and delete are supported.  That part was
trivial to accomplish.  Unfortunately it brought the disappearing
menu phenomenon back so it became more complicated overall.
2021-06-06 05:54:53 -07:00
nhmall
f963c5aca7 switch source tree from k&r to c99 2021-01-26 21:06:16 -05:00
nhmall
c9673b3d9e more window port interface adjustments
further adjustments to the window port interface to pass a pointer
to a glyph_info struct which describes not just the glyph number
itself, but also the ttychar, the color, the glyphflags, and the
symset index.

This affects two existing window port calls that get passed glyphs
and does the parameter consistently for both of them using the
glyph_info struct pointer:
	print_glyph()
	add_menu().

The recently added glyphmod parameter is now unnecessary and has been
removed.
2021-01-05 10:09:37 -05:00
nhmall
ac9ba38449 file header bump from "NetHack 3.6" to "NetHack 3.7" 2020-08-03 22:07:36 -04:00
nhmall
7012e7046f add support for MENU_ITEMFLAGS_SKIPINVERT
Able to test:
win/tty
win/win32
win/curses

Unable to test:
win/X11
win/Qt
win/Qt3
win/gem
win/gnome
2019-12-23 08:36:44 -05:00
nhmall
f48ac58561 Merge branch 'NetHack-3.6' 2019-12-13 17:37:46 -05:00
PatR
8a57029503 spelling and typo fixes
Submitted for 3.7.0; all but one also apply to 3.6.3.

I rewrote the curses terminal-too-small message instead of just
fixing the spelling of "minumum".
2019-12-11 12:35:45 -08:00
nhmall
e6b309fbc3 Merge branch 'NetHack-3.6' 2019-11-15 23:06:16 -05:00
PatR
8a7169eda9 fix curses compile warning
From hardfought; latest gcc complains that /* fall through other stuff */
doesn't match its pattern for /* fall through */ comment indicating
that omitted 'break' statement is intentional and one switch case is
deliberately continuing into the code for another.
2019-11-15 19:03:11 -08:00
nhmall
78805516be Merge branch 'NetHack-3.6' 2019-06-25 09:42:13 -04:00
PatR
4f0b1262c5 curses clipped map 'scrollbars'
The position bars shown by curses when the map is clipped weren't
being drawn as intended (integer arithmetic).  Changing parentheses
was enough to get it working, but it didn't handle the edge case
where non-zero got rounded to 0 (so when map was panned down, the
uppermost character of the vertical position bar still showed '*',
falsely indicating that top of map was currently within view.
2019-06-25 03:17:47 -07:00
nhmall
30cfcf3b12 Merge branch 'NetHack-3.6' 2019-06-18 21:43:54 -04:00
PatR
1c03d09701 curses map window
tty ignores map column #0 (0-based index), like the core, and draws
the map in screen columns 1 (1-based index) through 79, leaving screen
column 80 blank.  curses was drawing all 80 map columns and since #0
was always unused, screen column 1 was blank and the map was shown in
2 through 80.  Change curses to work like tty.

This was too easy; there may be problems lurking.  One known issue: it
should be made smarter about when clipping/panning is necessary since
it thinks that a full 80 columns are needed but 79 suffice.
2019-06-18 03:52:28 -07:00
nhmall
8e972874b2 Merge branch 'NetHack-3.6' 2019-05-18 16:30:43 -04:00
PatR
f218e3f15e fix #H8753 - curses message window anomalies
Autodescribe feedback and multi-digit count prompts are always shown
on the last line of the message window and are suppressed from message
history (both ^P and DUMPLOG).  When the message window is using all
available lines, the last one was being overwritten (until the count
or the feedback was completed or dismissed, then last line returned).
Adopt the suggestion that it be scrolled up a line instead of being
overwritten.  [I haven't been able to reproduce the reported problem
where shorter overlaid text left some of longer underlying text visible
but that should now become moot.]

Bonus fix:  while testing, I noticed that if your screen only has room
for a one-line message window and you used ESC to cancel 'pick a spot
with cursor' prompting before moving the cursor, the prompt was left
intact on the message line.  tty erases it in that situation, but the
clear_nhwindow(WIN_MESSAGE) was a no-op for curses because it usually
doesn't erase old messages.  This changes the curses behavior when the
core asks it to erase the message window:  now it forces one blank line
of fake autodesribe feedback (causing the prompt or other most recent
message to scroll off top), then removes that fake feedback (leaving
a blank message line).  For multi-line message window, the old messages
scroll up by one line sooner than they would when waiting for the next
real message but are otherwise unaffected.
2019-05-18 02:25:48 -07:00
nhmall
7ff8b21a15 Merge branch 'NetHack-3.6.2' 2019-03-25 07:26:43 -04:00
PatR
ee53a9fea6 curses message recall, memory leaks
Using ^P right after resize or 'O' of align_message, align_status,
statuslines, or windowborders would result in
'curses_display_nhmenu: attempt to display empty menu'
because some memory cleanup I added several weeks back was being
executed when the curses interface tore down and recreated its
internal windows.

This fixes ^P handling by making sure that that menu (which is just
text but uses a menu to support '>'/'<'/'^'/'|' scrolling) will never
be empty and it also fixes the window deletion to not throw away
message history until it's final deletion at exit time.

^P uses a popup window to display previous messages and it was never
deleting that window, just creating a new one each time.  Same with
the routine which displays an external help file.  Using either or
combination of both close to 5000 times would probably make internal
window creation get stuck in an infinite loop.  Delete those windows
after they're used so it'll never be put to the test.

The memory cleanup I added for map/status/messages/invent was only
being preformed at end of game, not when saving.  Fix that too.
2019-03-24 17:50:26 -07:00
nhmall
5a7d780db6 Merge branch 'NetHack-3.6.2' 2019-03-20 09:28:48 -04:00
PatR
7049145712 curses update
Miscellaenous stuff either groundwork for or noticed while updating
curses status.  The status changes themselves need some more testing.
One or two of the comments refer to that revised status which hasn't
been checked in yet.
2019-03-18 15:08:01 -07:00
nhmall
7d703fa978 Merge branch 'NetHack-3.6.2' 2019-02-10 00:08:07 -05:00
PatR
d80fd8a014 curses status cleanup
Stop the last of the memory leaks occuring during basic usage.
2019-02-09 15:30:53 -08:00
nhmall
0d63372a48 Merge branch 'NetHack-3.6.2' 2019-02-08 19:58:58 -05:00
nhmall
a3527e7eab fix some merge fallout from 3.6.2 curses changes 2019-02-08 19:54:09 -05:00