Deutsch English Français Italiano |
<v3v03l$22nnp$3@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Malcolm McLean <malcolm.arthur.mclean@gmail.com> Newsgroups: comp.lang.c Subject: Re: Running an editor from ANSI C Date: Fri, 7 Jun 2024 13:57:25 +0100 Organization: A noiseless patient Spider Lines: 135 Message-ID: <v3v03l$22nnp$3@dont-email.me> References: <v3pge7$uf2i$1@dont-email.me> <v3r2pl$16mtl$1@dont-email.me> <v3r7v8$1b57j$1@dont-email.me> <v3rek5$1c4i5$1@dont-email.me> <v3rrtm$1e6g8$1@dont-email.me> <v3ru84$1eafb$1@dont-email.me> <87o78dzw1a.fsf@nosuchdomain.example.com> <v3te2i$1ms1q$1@dont-email.me> <87frtpznoa.fsf@nosuchdomain.example.com> <v3uimm$20jte$3@dont-email.me> <871q59yty1.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 07 Jun 2024 14:57:26 +0200 (CEST) Injection-Info: dont-email.me; posting-host="3d5cf5214f2d8b45c65425247320c56d"; logging-data="2187001"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ceTKFXvWDPbUpfDXqpd5xzzRqZ7SUL4k=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:aRNFH8Z5QetJpzwTKzhbSJX7Azg= Content-Language: en-GB In-Reply-To: <871q59yty1.fsf@nosuchdomain.example.com> Bytes: 6564 On 07/06/2024 10:37, Keith Thompson wrote: > Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes: >> On 06/06/2024 23:55, Keith Thompson wrote: >>> Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes: >>>> On 06/06/2024 20:54, Keith Thompson wrote: >>>>> David Brown <david.brown@hesbynett.no> writes: >>>>>> On 06/06/2024 10:27, Malcolm McLean wrote: >>>>>>> It does work. But my compiler warns about rmpnam() being deprecated. >>>>>> >>>>>> I presume you mean "tmpnam()" here. No, it has not been deprecated - >>>>>> not even in C23. I could be wrong, but this sounds like one of MSVC's >>>>>> arbitrary self-declared deprecations, using scare tactics to encourage >>>>>> people to use MSVC's own functions rather than standard C functions, >>>>>> thus locking you into their tools and platform. >>>>> [...] >>>>> You're right, tmpnam() is not deprecated either by ISO C or by >>>>> POSIX. >>>>> But tmpfile() is likely to be better for most purposes. It creates >>>>> a >>>>> file and returns a FILE*. tmpnam() returns a string pointer, and it's >>>>> possible that some other process could create a file with the same name >>>>> before the caller has a chance to create it. >>>>> (mkstemp() is more flexible, but is not defined by ISO C.) >>>>> >>>> I want to run nano (or vi, or ed), in a shell running a pure ansi C >>>> program. So the way to do it is to create a file, write the text you >>>> want edit to it, them call system("nano readme.txt"). Nano then grabs >>>> the cobsole, which is what you want. You then read the file to get >>>> the edited data. >>>> >>>> The shell isn't just a proof og concept. It has a practical purpose, >>>> because it is FileSystem XML file editor. Whilst I'm playing about >>>> putting Basic into it for fun, the real purpose is serious. And the >>>> user must have an easy way of editing text files in the FileSystem >>>> file. >>>> >>>> But it becomes effectively a virtual computer in its own right. >>> OK -- but that has nothing at all to do with my post, which was >>> about >>> how to generate the temporary file name. >>> One suggestion: rather than always using nano (which not everyone is >>> familiar with), try reading the $EDITOR environment variable to >>> determine what editor to use. Concatenating the value of >>> getenv("EDITOR"), followed by a space, followed by the file name, is >>> likely to give you a valid command you can pass to system(). Fall back >>> to nano if getenv("EDITOR") returns a null pointer. >>> (For historical reasons, the convention is to use $VISUAL if it's >>> set, >>> otherwise $EDITOR if it's set, otherwise some default. Originally >>> $VISUAL typically referred to a full-screen editor like vi and $EDITOR >>> to a line editor like ed, to be used when full-screen editing is not >>> available. That's unlikely to be relevant nowadays, and users typically >>> either don't set $VISUAL or set it to the same thing as $EDITOR.) >>> Don't do this for me; I'm not likely to use this. But others are >>> likely >>> to find it more user-friendly if they can use a chosen editor. >> >> Ah thank you. But then main has to take an extra parameter. Now will >> the shell still be absolutely robust, and completely portable, and run >> just anywhere? > > What? Why would main need an extra parameter? > Hre's the main function for the shell. int main(int argc, char **argv) { char error[1024]; char **list; int i; FILE *fp = 0; char *xmlstring = 0; BBX_FileSystem *bbx_fs_xml = 0; int err; if (argc < 2) usage(); fp = fopen(argv[1], "r"); if (!fp) { fprintf(stderr, "Can't open xml file\n"); exit(EXIT_FAILURE); } xmlstring = fslurp(fp); if (!xmlstring) { fprintf(stderr, "Out of memory\n"); exit(EXIT_FAILURE); } fclose(fp); fp = 0; bbx_fs_xml = bbx_filesystem(); err = bbx_filesystem_set(bbx_fs_xml, xmlstring, BBX_FS_STRING); if (err) { fprintf(stderr, "Can't set up XML filessystem\n"); exit(EXIT_FAILURE); } docommand(bbx_fs_xml, argc -1, argv + 1); fp = fopen(argv[1], "w"); if (!fp) { fprintf(stderr, "Can't open xml file to write\n"); exit(EXIT_FAILURE); } err = bbx_filesystem_dump(bbx_fs_xml, fp); if (err) fprintf(stderr, "Error writing FileSystem XML file to disk\n"); fclose(fp); fp = 0; bbx_filesystem_kill(bbx_fs_xml); free(xmlstring); return 0; } Now to get the $EDITOR variable I will have to modify this function. int main(int argc, char **argv, char **envp) Now what are the implications of doing that? The point is that the shell is absolutely portable, because its filing system is stored as a single file on the host computer. And it mustn't break anwhere. I want David Brown to drop it into one of his embedded systems and see it work. -- Check out Basic Algorithms and my other books: https://www.lulu.com/spotlight/bgy1mm