From 298cfee889cb3542b3b921b4bec2cbe3c9a954a7 Mon Sep 17 00:00:00 2001 From: Gabriel Dos Reis Date: Sat, 20 Jul 2024 07:07:09 +0900 Subject: [PATCH] [hyper] Reduce uses of `sprintf` (#52) In many cases, a better and safer alternative is provided by C++20's `std::format` functionality. --- src/hyper/ReadBitmap.c | 11 ++++---- src/hyper/event.c | 8 +++--- src/hyper/extent2.c | 7 ++--- src/hyper/htadd.c | 14 +++++----- src/hyper/htinp.c | 42 +++++++++++++---------------- src/hyper/hyper.h | 4 +-- src/hyper/keyin.c | 8 +++--- src/hyper/lex.c | 28 +++++++++---------- src/hyper/mem.c | 3 +-- src/hyper/parse-aux.c | 20 +++++--------- src/hyper/parse-types.c | 18 ++++++------- src/hyper/parse.c | 16 +++++------ src/hyper/spadint.c | 60 +++++++++++++++++------------------------ src/hyper/titlebar.c | 40 +++++++++++++-------------- src/include/pixmap.h | 6 ++--- src/lib/pixmap.c | 8 +++--- 16 files changed, 130 insertions(+), 163 deletions(-) diff --git a/src/hyper/ReadBitmap.c b/src/hyper/ReadBitmap.c index c6b28151..a7c4c82d 100644 --- a/src/hyper/ReadBitmap.c +++ b/src/hyper/ReadBitmap.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -34,6 +34,7 @@ */ #include "openaxiom-c-macros.h" +#include #include "debug.h" #include "halloc.h" #include "sockio.h" @@ -51,9 +52,9 @@ static int read_w_and_h(FILE * fd , unsigned int * width , unsigned int * height * XReadBitmapFile does not seeem to work to well (whatecer that means). */ -XImage * -HTReadBitmapFile(Display *display,int screen,char * filename, - int *width, int *height) +XImage* +HTReadBitmapFile(Display* display, int screen, const char* filename, + int* width, int* height) { XImage *image; FILE *fd; @@ -251,7 +252,7 @@ insert_image_struct(char *filename) /* strcpy(image->filename, filename); */ - sprintf(image->filename, "%s", filename); + strcpy(image->filename, filename); hash_insert(&gImageHashTable,(char *) image, image->filename); } return image; diff --git a/src/hyper/event.c b/src/hyper/event.c index f86af931..aec333a9 100644 --- a/src/hyper/event.c +++ b/src/hyper/event.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -35,6 +35,7 @@ #define _EVENT_C +#include #include "openaxiom-c-macros.h" #include @@ -264,10 +265,7 @@ paste_button(PasteNode * paste) static void killAxiomPage(HyperDocPage * page) { - char command[512]; - - sprintf(command, "(|htpDestroyPage| '%s)", page->name); - send_lisp_command(command); + send_lisp_command(std::format("(|htpDestroyPage| '{})", page->name).c_str()); } static void diff --git a/src/hyper/extent2.c b/src/hyper/extent2.c index 63ae2866..2e4a5ef3 100644 --- a/src/hyper/extent2.c +++ b/src/hyper/extent2.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,6 +42,7 @@ ****************************************************************************/ #include "openaxiom-c-macros.h" +#include #include "debug.h" #include "halloc.h" #include "sockio.h" @@ -862,7 +863,7 @@ insert_bitmap_file(TextNode * node) image->height = image->image.xi->height; image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1,"Image Filename"); /* strcpy(image->filename, filename); */ - sprintf(image->filename, "%s", filename); + strcpy(image->filename, filename); hash_insert(&gImageHashTable, (char *)image, image->filename); } node->width = image->width; @@ -909,7 +910,7 @@ insert_pixmap_file(TextNode * node) image->filename = (char *) halloc(sizeof(char) * strlen(filename) +1, "insert_pixmap--filename"); /* strcpy(image->filename, filename); */ - sprintf(image->filename, "%s", filename); + strcpy(image->filename, filename); image->image.xi = xi; hash_insert(&gImageHashTable, (char *)image, image->filename); } diff --git a/src/hyper/htadd.c b/src/hyper/htadd.c index 07b75e45..58b6d15f 100644 --- a/src/hyper/htadd.c +++ b/src/hyper/htadd.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -35,7 +35,7 @@ /* HyperDoc database file manager */ - +#include #include "openaxiom-c-macros.h" #include @@ -186,7 +186,7 @@ build_db_filename(short flag, char *db_dir, char *dbfilename) int ret_status; char *SPAD; struct stat buff; - char path[256]; + std::string path; if (flag & System) { @@ -197,15 +197,15 @@ build_db_filename(short flag, char *db_dir, char *dbfilename) exit(-1); } sprintf(dbfilename, "%s/share/hypertex/pages/%s", SPAD, db_file_name); - sprintf(path, "%s/share/hypertex/pages", SPAD); + path = std::format("{}/share/hypertex/pages", SPAD); } else if (flag & Named) { sprintf(dbfilename, "%s/%s", db_dir, db_file_name); - strcpy(path, db_dir); + path = db_dir; } else { /* use the current directory */ sprintf(dbfilename, "./%s", db_file_name); - sprintf(path, "./"); + path = "./"; } /* fprintf(stderr,"htadd:build_db_filename:dbfilename=%s\n",dbfilename);*/ /* Now see if I can write to the file */ @@ -214,7 +214,7 @@ build_db_filename(short flag, char *db_dir, char *dbfilename) if (ret_status == -1) { if (errno == ENOENT) /* If the file does not exist, check the path. */ - ret_status = stat(path, &buff); + ret_status = stat(path.c_str(), &buff); if (ret_status == -1) { perror("build_db_file"); exit(1); diff --git a/src/hyper/htinp.c b/src/hyper/htinp.c index b1f16d37..89c2ef77 100644 --- a/src/hyper/htinp.c +++ b/src/hyper/htinp.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -33,6 +33,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include + #include #include @@ -70,9 +72,6 @@ int num_active_files = 0; char *inactive_file_list[MaxInputFiles]; int num_inactive_files = 0; int include_bf = 0; -char buf_for_record_commands[256]; - - void make_record() @@ -82,9 +81,9 @@ make_record() send_lisp_command("(setq |$testingSystem| T)"); send_lisp_command("(setq |$printLoadMsgs| NIL)"); send_lisp_command("(setq |$BreakMode| '|resume|)"); - sprintf(buf_for_record_commands,"(|inputFile2RecordFile| '\"%s\")",input_file_list[i]); - fprintf(stderr,"%s\n",buf_for_record_commands); - send_lisp_command(buf_for_record_commands); + auto cmd = std::format("(|inputFile2RecordFile| '\"{}\")", input_file_list[i]); + fprintf(stderr,"%s\n",cmd.c_str()); + send_lisp_command(cmd.c_str()); } if (kill_spad){ auto status = connect_spad(); @@ -102,9 +101,9 @@ verify_record() send_lisp_command("(setq |$testingSystem| T)"); send_lisp_command("(setq |$printLoadMsgs| NIL)"); send_lisp_command("(setq |$BreakMode| '|resume|)"); - sprintf(buf_for_record_commands,"(|verifyRecordFile| '\"%s\")",input_file_list[i]); - fprintf(stderr,"%s\n",buf_for_record_commands); - send_lisp_command(buf_for_record_commands); + auto cmd = std::format("(|verifyRecordFile| '\"{}\")", input_file_list[i]); + fprintf(stderr,"%s\n", cmd.c_str()); + send_lisp_command(cmd.c_str()); } if (kill_spad) { auto status = connect_spad(); @@ -416,9 +415,9 @@ get_graph_output(char* command, const char* pagename, TokenType com_type) get_string_buf(spad_socket, buf, 1024); } unescape_string(command); - sprintf(buf, "(|processInteractive| '(|write| |%s| \"%s%d\" \"image\") NIL)", "%", + auto cmd = std::format("(|processInteractive| '(|write| |{}| \"{}{}\" \"image\") NIL)", "%", pagename, example_number); - send_lisp_command(buf); + send_lisp_command(cmd.c_str()); send_lisp_command("(|setViewportProcess|)"); send_lisp_command("(|processInteractive| '(|close| (|%%| -3)) NIL)"); send_lisp_command("(|waitForViewport|)"); @@ -427,27 +426,22 @@ get_graph_output(char* command, const char* pagename, TokenType com_type) static void send_command(char *command, TokenType com_type) { - char buf[1024]; - if (com_type != TokenType::Spadsrc) { escape_string(command); - sprintf(buf, "(|parseAndEvalToHypertex| '\"%s\")", command); - send_lisp_command(buf); + auto buf = std::format("(|parseAndEvalToHypertex| '\"{}\")", command); + send_lisp_command(buf.c_str()); } else { - FILE *f; - char name[512], str[512]/*, *c*/; - - sprintf(name, "/tmp/hyper%s.input", oa_getenv("SPADNUM")); - f = fopen(name, "w"); + auto name = std::format("/tmp/hyper{}.input", oa_getenv("SPADNUM")); + FILE* f = fopen(name.c_str(), "w"); if (f == NULL) { - fprintf(stderr, "Can't open temporary input file %s\n", name); + fprintf(stderr, "Can't open temporary input file %s\n", name.c_str()); return; } fprintf(f, "%s", command); fclose(f); - sprintf(str, "(|parseAndEvalToHypertex| '\")read %s\")", name); - send_lisp_command(str); + auto str = std::format("(|parseAndEvalToHypertex| '\")read {}\")", name); + send_lisp_command(str.c_str()); } } diff --git a/src/hyper/hyper.h b/src/hyper/hyper.h index 67ccae28..1d1fc9dd 100644 --- a/src/hyper/hyper.h +++ b/src/hyper/hyper.h @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -94,7 +94,7 @@ extern void change_input_focus(HyperLink * link); extern void next_input_focus(); extern void prev_input_focus(); extern int delete_item(char * name); -extern XImage * HTReadBitmapFile(Display * display , int screen , char * filename , int * width , int * height); +extern XImage* HTReadBitmapFile(Display* display , int screen , const char* filename , int* width , int* height); extern ImageStruct * insert_image_struct(char * filename); extern void compute_form_page(HyperDocPage * page); extern int window_width(int cols); diff --git a/src/hyper/keyin.c b/src/hyper/keyin.c index f9abb8a2..52ae350d 100644 --- a/src/hyper/keyin.c +++ b/src/hyper/keyin.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,6 +41,7 @@ * ****************************************************************************/ +#include #include #include "openaxiom-c-macros.h" #include "debug.h" @@ -132,8 +133,6 @@ handle_key(XEvent *event) /*char *head = "echo htadd -l ";*/ /*char *blank1 = " ";*/ /*char *blank2 = " \n";*/ - char buffer[180]; - charcount = XLookupString((XKeyEvent *)event, key_buffer, key_buffer_size, &keysym ,&compstatus); /* 5 args */ key_buffer[charcount] = '\0'; @@ -154,8 +153,7 @@ handle_key(XEvent *event) if (event->xkey.state & ShiftMask) { name = gWindow->page->name; filename = gWindow->page->filename; - sprintf(buffer, "htadd -l %s\n", filename); - system(buffer); + system(std::format("htadd -l {}\n", filename).c_str()); if (auto ptr = gFileHashTable.find(filename); ptr != gFileHashTable.end()) { fclose(ptr->second); gFileHashTable.erase(ptr); diff --git a/src/hyper/lex.c b/src/hyper/lex.c index 40cf2db8..881d5464 100644 --- a/src/hyper/lex.c +++ b/src/hyper/lex.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -63,6 +63,7 @@ #include #include #include +#include #include "debug.h" #include "sockio.h" @@ -876,7 +877,7 @@ token_name(TokenType type) strcpy(ebuffer, "~"); break; case TokenType::Cond: - sprintf(ebuffer, "\\%s", token.id); + strcpy(ebuffer, std::format("\\{}", token.id).c_str()); break; case TokenType::Icorrection: strcpy(ebuffer, "\\/"); @@ -888,7 +889,8 @@ token_name(TokenType type) strcpy(ebuffer, "\\begin{patch}"); break; default: - sprintf(ebuffer, " %d ", type); + strcpy(ebuffer, std::format(" {} ", static_cast(type)).c_str()); + break; } } } @@ -926,31 +928,27 @@ print_next_ten_tokens() void print_page_and_filename() { - char obuff[128]; - + std::string obuff; if (gPageBeingParsed->type == TokenType::Normal) { - /* * Now try to inform the user as close to possible where the error * occurred */ - sprintf(obuff, "(HyperDoc) While parsing %s on line %d\n\tin the file %s\n", - gPageBeingParsed->name, line_number, - gPageBeingParsed->filename); + obuff = std::format("(HyperDoc) While parsing {} on line {}\n\tin the file {}\n", + gPageBeingParsed->name, line_number, + gPageBeingParsed->filename); } else if (gPageBeingParsed->type == TokenType::SpadGen) { - sprintf(obuff, "While parsing %s from the Spad socket\n", - gPageBeingParsed->name); + obuff = std::format("While parsing {} from the Spad socket\n", gPageBeingParsed->name); } else if (gPageBeingParsed->type == TokenType::Unixfd) { - sprintf(obuff, "While parsing %s from a Unixpipe\n", - gPageBeingParsed->name); + obuff = std::format("While parsing {} from a Unixpipe\n", gPageBeingParsed->name); } else { /* Unknown page type */ - sprintf(obuff, "While parsing %s\n", gPageBeingParsed->name); + obuff = std::format("While parsing {}\n", gPageBeingParsed->name); } - fprintf(stderr, "%s", obuff); + fprintf(stderr, "%s", obuff.c_str()); } diff --git a/src/hyper/mem.c b/src/hyper/mem.c index 2ba32eb1..82b44130 100644 --- a/src/hyper/mem.c +++ b/src/hyper/mem.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reversed. Redistribution and use in source and binary forms, with or without @@ -113,7 +113,6 @@ alloc_hd_window() w->fMacroHashTable = hash_copy_table(&init_macro_hash); gWindow = w; - /*sprintf(haslisp, "%1d\0", MenuServerOpened);*/ make_special_pages(w->fPageHashTable); w->fDisplayedCursor = 0; return w; diff --git a/src/hyper/parse-aux.c b/src/hyper/parse-aux.c index 2d160ba0..84ba582c 100644 --- a/src/hyper/parse-aux.c +++ b/src/hyper/parse-aux.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reverved. Redistribution and use in source and binary forms, with or without @@ -39,6 +39,7 @@ #include #include #include +#include #include "debug.h" #include "halloc.h" @@ -90,15 +91,8 @@ window_code(Window *w, int size) char * window_id(Window w) { - char *ret; - char buff[32]; - int length; - - sprintf(buff, "%ld", w); - length = strlen(buff); - ret = (char *) halloc(length * sizeof(char) + 1, "windowid"); - strcpy(ret, buff); - return (ret); + auto buf = std::to_string(w); + return alloc_string(buf.c_str()); } // Sequence of directory pathnames. @@ -234,10 +228,8 @@ read_ht_files(HTEnvironment& env, FILE *db_fp, const std::string& dir) ret_val = stat(fullname, &fstats); if (ret_val == -1) { - char buffer[300]; - - sprintf(buffer, "(HyperDoc) read_ht_db: Unable To Open %s :", fullname); - perror(buffer); + auto buffer = std::format("(HyperDoc) read_ht_db: Unable To Open {} :", fullname); + perror(buffer.c_str()); exit(-1); } get_token(); diff --git a/src/hyper/parse-types.c b/src/hyper/parse-types.c index e77b2454..31696ff0 100644 --- a/src/hyper/parse-types.c +++ b/src/hyper/parse-types.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,6 +41,8 @@ * ****************************************************************************/ +#include +#include #include "openaxiom-c-macros.h" #include "debug.h" #include "halloc.h" @@ -82,18 +84,17 @@ static const char* errmess[] = { static void htperror(const char* msg, int erno) { - char obuff[256]; - + std::string obuff; /* The first thing I do is create the error message */ if (erno <= Numerrors) { - sprintf(obuff, "%s:%s\n", msg, errmess[erno]); + obuff = std::format("{}:{}\n", msg, errmess[erno]); } else { - sprintf(obuff, "%s:\n", msg); + obuff = std::format("{}:\n", msg); fprintf(stderr, "Unknown error type %d\n", erno); } - fprintf(stderr, "%s", obuff); + fprintf(stderr, "%s", obuff.c_str()); print_page_and_filename(); @@ -190,10 +191,9 @@ parse_condnode() break; default: { - char eb[128]; token_name(token.type); - sprintf(eb, "Unexpected Token %s\n", eb); - htperror(eb, HTCONDNODE); + auto eb = std::format("Unexpected Token {}\n", &ebuffer[0]); + htperror(eb.c_str(), HTCONDNODE); } break; } diff --git a/src/hyper/parse.c b/src/hyper/parse.c index 4e4b8196..5e8bbed5 100644 --- a/src/hyper/parse.c +++ b/src/hyper/parse.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -35,6 +35,8 @@ #include #include +#include +#include #include "debug.h" #include "halloc.h" #include "sockio.h" @@ -469,23 +471,19 @@ parse_HyperDoc() break; case TokenType::Pagename: { - char *str; - curr_node->type = TokenType::Word; curr_node->space = 0; - str = halloc(strlen(cur_page->name) + 1, "parse"); - sprintf(str, "%s", cur_page->name); + char* str = halloc(strlen(cur_page->name) + 1, "parse"); + strcpy(str, cur_page->name); curr_node->data.text = alloc_string(str); break; } case TokenType::Examplenumber: { - char *str; - curr_node->type = TokenType::Word; curr_node->space = 0; - str = halloc(5, "parse"); - sprintf(str, "%d", example_number); + char* str = halloc(5, "parse"); + strcpy(str, std::to_string(example_number).c_str()); curr_node->data.text = alloc_string(str); break; } diff --git a/src/hyper/spadint.c b/src/hyper/spadint.c index 29f66c86..68d1a008 100644 --- a/src/hyper/spadint.c +++ b/src/hyper/spadint.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -38,6 +38,8 @@ /* Communication interface for external OpenAxiom buffers */ #include "open-axiom.h" +#include +#include #include "debug.h" #include #include "halloc.h" @@ -134,19 +136,16 @@ issue_spadcommand(HyperDocPage *page, TextNode *command, int immediate, static void send_pile(openaxiom_sio *sock,char * str) { - FILE *f; - char name[512], command[512]; - - sprintf(name, "/tmp/hyper%s.input", oa_getenv("SPADNUM")); - f = fopen(name, "w"); - if (f == NULL) { - fprintf(stderr, "Can't open temporary input file %s\n", name); + auto name = std::format("/tmp/hyper{}.input", oa_getenv("SPADNUM")); + FILE* f = fopen(name.c_str(), "w"); + if (f == nullptr) { + fprintf(stderr, "Can't open temporary input file %s\n", name.c_str()); return; } fprintf(f, "%s", str); fclose(f); - sprintf(command, ")read %s\n", name); - send_string(sock, command); + auto command = std::format(")read {}\n", name.c_str()); + send_string(sock, command.c_str()); } static void issue_dependent_commands(HyperDocPage *page, TextNode *command, TokenType type) @@ -208,53 +207,42 @@ mark_as_executed(HyperDocPage *page, TextNode *command, TokenType type) static void start_user_buffer(HyperDocPage *page) { - char buf[1024], *title; - char *SPAD; - char spadbuf[250]; - char complfile[250]; + std::string buf; int ret_val; - SPAD = (char *) oa_getenv("AXIOM"); - if (SPAD == NULL) { - sprintf(SPAD, "/spad/mnt/rios"); - } - sprintf(spadbuf, "%s/lib/spadbuf", SPAD); - sprintf(complfile, "%s/lib/command.list", SPAD); - title = print_to_string(page->title); - if (access(complfile, R_OK) == 0) - + const char* SPAD = (char *) oa_getenv("AXIOM"); + if (SPAD == nullptr) + SPAD = "/spad/mnt/rios"; + std::string spadbuf = std::format("{}/lib/spadbuf", SPAD); + std::string complfile = std::format("{}/lib/command.list", SPAD); + const char* title = print_to_string(page->title); + if (access(complfile.c_str(), R_OK) == 0) /* * TTT says : why not invoke with "-name axiomclient" and set any * defaults in the usual way */ #ifdef RIOSplatform - sprintf(buf, - "aixterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s %s&", + buf = std::format("aixterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} {} {}&", title, title, spadbuf, page->name, complfile); else - sprintf(buf, - "aixterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s&", + buf = std::format("aixterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} {}&", title, title, spadbuf, page->name); #else #ifdef SUNplatform - sprintf(buf, - "xterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s %s&", + buf = std::format("xterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} {} {}&", title, title, spadbuf, page->name, complfile); else - sprintf(buf, - "xterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s&", + buf = std::format("xterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} {}&", title, title, spadbuf, page->name); #else - sprintf(buf, - "xterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s %s %s&", + buf = std::format("xterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} {} {}&", title, title, spadbuf, page->name, complfile); else - sprintf(buf, - "xterm -sb -sl 500 -name axiomclient -n '%s' -T '%s' -e %s '%s'&", + buf = std::format("xterm -sb -sl 500 -name axiomclient -n '{}' -T '{}' -e {} '{}'&", title, title, spadbuf, page->name); #endif #endif - ret_val = system(buf); + ret_val = system(buf.c_str()); if (ret_val == -1 || ret_val == 127) { /* diff --git a/src/hyper/titlebar.c b/src/hyper/titlebar.c index abe0aa82..9b332c34 100644 --- a/src/hyper/titlebar.c +++ b/src/hyper/titlebar.c @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2023, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -42,6 +42,8 @@ ****************************************************************************/ #include +#include +#include #include "openaxiom-c-macros.h" #include "debug.h" #include "halloc.h" @@ -306,48 +308,46 @@ static void readTitleBarImages() { int w, h; - char filename[128]; - char *axiomEnvVar = NULL; - - axiomEnvVar = oa_getenv("AXIOM"); + std::string filename; + auto axiomEnvVar = oa_getenv("AXIOM"); if (axiomEnvVar) - sprintf(filename, "%s/share/hypertex/bitmaps/%s", axiomEnvVar, tw1file); + filename = std::format("{}/share/hypertex/bitmaps/{}", axiomEnvVar, tw1file); else - sprintf(filename, "%s", tw1file); - tw1image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename, + filename = tw1file; + tw1image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename.c_str(), &twwidth, &twheight); if (axiomEnvVar) - sprintf(filename, "%s/share/hypertex/bitmaps/%s", axiomEnvVar, tw2file); + filename = std::format("{}/share/hypertex/bitmaps/{}", axiomEnvVar, tw2file); else - sprintf(filename, "%s", tw2file); - tw2image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename, + filename = tw2file; + tw2image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename.c_str(), &w, &h); twwidth = ((twwidth >= w) ? (twwidth) : (w)); if (axiomEnvVar) - sprintf(filename, "%s/share/hypertex/bitmaps/%s", axiomEnvVar, tw3file); + filename = std::format("{}/share/hypertex/bitmaps/{}", axiomEnvVar, tw3file); else - sprintf(filename, "%s", tw3file); - tw3image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename, + filename = tw3file; + tw3image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename.c_str(), &w, &h); twwidth = ((twwidth >= w) ? (twwidth) : (w)); if (axiomEnvVar) - sprintf(filename, "%s/share/hypertex/bitmaps/%s", axiomEnvVar, tw4file); + filename = std::format("{}/share/hypertex/bitmaps/{}", axiomEnvVar, tw4file); else - sprintf(filename, "%s", tw4file); - tw4image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename, + filename = tw4file; + tw4image = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename.c_str(), &w, &h); twwidth = ((twwidth >= w) ? (twwidth) : (w)); if (axiomEnvVar) - sprintf(filename, "%s/share/hypertex/bitmaps/%s", axiomEnvVar, noopfile); + filename = std::format("{}/share/hypertex/bitmaps/{}", axiomEnvVar, noopfile); else - sprintf(filename, "%s", noopfile); - noopimage = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename, + filename = noopfile; + noopimage = HTReadBitmapFile(gXDisplay, gXScreenNumber, filename.c_str(), &twwidth, &twheight); } diff --git a/src/include/pixmap.h b/src/include/pixmap.h index 136397ac..53829b3b 100644 --- a/src/include/pixmap.h +++ b/src/include/pixmap.h @@ -1,7 +1,7 @@ /* Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2008, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -36,8 +36,8 @@ #ifndef OPENAXIOM_PIXMAP_included #define OPENAXIOM_PIXMAP_included -extern int file_exists(char * ); -extern FILE * zzopen(char * , const char* ); +extern int file_exists(const char*); +extern FILE* zzopen(const char* , const char* ); extern void write_pixmap_file(Display*, int, const char*, Window, int, int, int, int); extern int read_pixmap_file(Display * , int , char * , XImage * * , int * , int * ); diff --git a/src/lib/pixmap.c b/src/lib/pixmap.c index d58e8152..41a66397 100644 --- a/src/lib/pixmap.c +++ b/src/lib/pixmap.c @@ -1,7 +1,7 @@ /* Copyright (c) 1991-2002, The Numerical ALgorithms Group Ltd. All rights reserved. - Copyright (C) 2007-2013, Gabriel Dos Reis. + Copyright (C) 2007-2024, Gabriel Dos Reis. All rights reserved. Redistribution and use in source and binary forms, with or without @@ -58,7 +58,7 @@ /* returns true if the file exists */ int -file_exists(char *file) +file_exists(const char *file) { FILE *f; @@ -69,8 +69,8 @@ file_exists(char *file) return 0; } -FILE * -zzopen(char *file, const char* mode) +FILE* +zzopen(const char* file, const char* mode) { char com[512], zfile[512];