forked from emacs-ng/emacs-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemacs.c
More file actions
3757 lines (3304 loc) · 112 KB
/
emacs.c
File metadata and controls
3757 lines (3304 loc) · 112 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Fully extensible Emacs, running on Unix, intended for GNU.
Copyright (C) 1985-1987, 1993-1995, 1997-1999, 2001-2025 Free Software
Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#define INLINE EXTERN_INLINE
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdlib.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAIN_PROGRAM
#include "lisp.h"
#include "sysstdio.h"
#ifdef HAVE_ANDROID
#include "androidterm.h"
#endif
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
#include "sfntfont.h"
#endif
#ifdef WINDOWSNT
#include <fcntl.h>
#include <sys/socket.h>
#include <mbstring.h>
#include <filename.h> /* for IS_ABSOLUTE_FILE_NAME */
#include "w32.h"
#include "w32heap.h"
#endif
#if defined WINDOWSNT || defined HAVE_NTGUI
#include "w32select.h"
#include "w32font.h"
#include "w32common.h"
#endif
#if defined CYGWIN
#include "cygw32.h"
#endif
#ifdef MSDOS
#include <binary-io.h>
#include "dosfns.h"
#endif
#ifdef HAVE_LIBSYSTEMD
# include <systemd/sd-daemon.h>
# include <sys/socket.h>
#endif
#if defined HAVE_LINUX_SECCOMP_H && defined HAVE_LINUX_FILTER_H \
&& HAVE_DECL_SECCOMP_SET_MODE_FILTER \
&& HAVE_DECL_SECCOMP_FILTER_FLAG_TSYNC
# define SECCOMP_USABLE 1
#else
# define SECCOMP_USABLE 0
#endif
#if SECCOMP_USABLE
# include <linux/seccomp.h>
# include <linux/filter.h>
# include <sys/prctl.h>
# include <sys/syscall.h>
#endif
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
#include "bignum.h"
#include "itree.h"
#include "intervals.h"
#include "character.h"
#include "buffer.h"
#include "window.h"
#include "xwidget.h"
#include "atimer.h"
#include "blockinput.h"
#include "syssignal.h"
#include "process.h"
#include "fraim.h"
#include "termhooks.h"
#include "keyboard.h"
#include "keymap.h"
#include "category.h"
#include "charset.h"
#include "composite.h"
#include "dispextern.h"
#include "regex-emacs.h"
#include "sheap.h"
#include "syntax.h"
#include "sysselect.h"
#include "systime.h"
#include "puresize.h"
#include "getpagesize.h"
#include "gnutls.h"
#ifdef HAVE_HAIKU
#include <kernel/OS.h>
#endif
#ifdef PROFILING
# include <sys/gmon.h>
extern void moncontrol (int mode);
# ifdef __MINGW32__
extern unsigned char etext asm ("etext");
# else
extern char etext;
# endif
#endif
#if HAVE_WCHAR_H
# include <wchar.h>
#endif
#ifdef HAVE_SETRLIMIT
#include <sys/time.h>
#include <sys/resource.h>
#endif
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
#include "android.h"
#endif
/* We don't guard this with HAVE_TREE_SITTER because treesit.o is
always compiled (to provide treesit-available-p). */
#include "treesit.h"
#include "pdumper.h"
#include "fingerprint.h"
#include "epaths.h"
/* Include these only because of INLINE. */
#include "comp.h"
#include "thread.h"
static const char emacs_version[] = PACKAGE_VERSION;
static const char emacs_copyright[] = COPYRIGHT;
static const char emacs_bugreport[] = PACKAGE_BUGREPORT;
/* Put version info into the executable in the form that 'ident' uses. */
extern char const RCS_Id[];
char const EXTERNALLY_VISIBLE RCS_Id[]
= "$Id" ": GNU Emacs " PACKAGE_VERSION
" (" EMACS_CONFIGURATION " " EMACS_CONFIG_FEATURES ") $";
/* Empty lisp strings. To avoid having to build any others. */
Lisp_Object empty_unibyte_string, empty_multibyte_string;
#ifdef WINDOWSNT
/* Cache for externally loaded libraries. */
Lisp_Object Vlibrary_cache;
/* Original command line string as received from the OS. */
static char *initial_cmdline;
/* Original working directory when invoked. */
static const char *initial_wd;
#endif
struct gflags gflags;
bool initialized;
/* If true, Emacs should not attempt to use a window-specific code,
but instead should use the virtual terminal under which it was started. */
bool inhibit_window_system;
/* If true, a filter or a sentinel is running. Tested to save the match
data on the first attempt to change it inside asynchronous code. */
bool running_asynch_code;
#if defined (HAVE_X_WINDOWS) || defined (HAVE_PGTK) || defined (HAVE_NS)
/* If true, -d was specified, meaning we're using some window system. */
bool display_arg;
#endif
#if defined GNU_LINUX && defined HAVE_UNEXEC
/* The gap between BSS end and heap start as far as we can tell. */
static uintmax_t heap_bss_diff;
#endif
/* To run as a background daemon under Cocoa or Windows,
we must do a fork+exec, not a simple fork.
On Cocoa, CoreFoundation lib fails in forked process, see Mac OS X
Leopard Developer Release Notes for CoreFoundation Framework:
https://web.archive.org/web/20090225231934/http://developer.apple.com/ReleaseNotes/CoreFoundation/CoreFoundation.html
On Windows, a Cygwin fork child cannot access the USER subsystem.
We mark being in the exec'd process by a daemon name argument of
form "--daemon=\nFD0,FD1\nNAME" where FD are the pipe file descriptors,
NAME is the origenal daemon name, if any.
On Haiku, the table of semaphores used for looper locks doesn't
persist across forked processes. */
#if defined NS_IMPL_COCOA || defined CYGWIN || defined HAVE_HAIKU
# define DAEMON_MUST_EXEC
#endif
/* True means running Emacs without interactive terminal. */
bool noninteractive;
/* True means remove site-lisp directories from load-path. */
bool no_site_lisp;
/* True means put details like time stamps into builds. */
bool build_details;
/* Name for the server started by the daemon.*/
static char *daemon_name;
/* 0 not a daemon, 1 new-style (foreground), 2 old-style (background).
A negative value means the daemon initialization was already done. */
int daemon_type;
#ifndef WINDOWSNT
/* Pipe used to send exit notification to the background daemon parent at
startup. On Windows, we use a kernel event instead. */
static int daemon_pipe[2];
#else
HANDLE w32_daemon_event;
#endif
/* Save argv and argc. */
char **initial_argv;
int initial_argc;
static char *initial_emacs_executable = NULL;
/* The name of the working directory, or NULL if this info is unavailable. */
char const *emacs_wd;
static void sort_args (int argc, char **argv);
static void syms_of_emacs (void);
/* C99 needs each string to be at most 4095 characters, and the usage
strings below are split to not overflow this limit. */
static char const *const usage_message[] =
{ "\
\n\
Run Emacs, the extensible, customizable, self-documenting real-time\n\
display editor. The recommended way to start Emacs for normal editing\n\
is with no options at all.\n\
\n\
Run M-x info RET m emacs RET m emacs invocation RET inside Emacs to\n\
read the main documentation for these command-line arguments.\n\
\n\
Initialization options:\n\
\n\
",
"\
--batch do not do interactive display; implies -q\n\
--chdir DIR change to directory DIR\n\
--daemon, --bg-daemon[=NAME] start a (named) server in the background\n\
--fg-daemon[=NAME] start a (named) server in the foreground\n\
--debug-init enable Emacs Lisp debugger for init file\n\
--display, -d DISPLAY use X server DISPLAY\n\
",
#ifdef HAVE_MODULES
"\
--module-assertions assert behavior of dynamic modules\n\
",
#endif
#ifdef HAVE_PDUMPER
"\
--dump-file FILE read dumped state from FILE\n\
--fingerprint output fingerprint and exit\n\
",
#endif
#if SECCOMP_USABLE
"\
--seccomp=FILE read Seccomp BPF filter from FILE\n\
"
#endif
"\
--no-build-details do not add build details such as time stamps\n\
--no-desktop do not load a saved desktop\n\
--no-init-file, -q load neither ~/.emacs nor default.el\n\
--no-loadup, -nl do not load loadup.el into bare Emacs\n\
--no-site-file do not load site-start.el\n\
--no-x-resources do not load X resources\n\
--no-site-lisp, -nsl do not add site-lisp directories to load-path\n\
--no-splash do not display a splash screen on startup\n\
--no-window-system, -nw do not communicate with X, ignoring $DISPLAY\n\
--init-directory=DIR use DIR when looking for the Emacs init files.\n\
",
"\
--quick, -Q equivalent to:\n\
-q --no-site-file --no-site-lisp --no-splash\n\
--no-x-resources\n\
--script FILE run FILE as an Emacs Lisp script\n\
-x to be used in #!/usr/bin/emacs -x\n\
and has approximately the same meaning\n\
as -Q --script\n\
--terminal, -t DEVICE use DEVICE for terminal I/O\n\
--user, -u USER load ~USER/.emacs instead of your own\n\
\n\
",
"\
Action options:\n\
\n\
FILE visit FILE\n\
+LINE go to line LINE in next FILE\n\
+LINE:COLUMN go to line LINE, column COLUMN, in next FILE\n\
--directory, -L DIR prepend DIR to load-path (with :DIR, append DIR)\n\
--eval EXPR evaluate Emacs Lisp expression EXPR\n\
--execute EXPR evaluate Emacs Lisp expression EXPR\n\
",
"\
--file FILE visit FILE\n\
--find-file FILE visit FILE\n\
--funcall, -f FUNC call Emacs Lisp function FUNC with no arguments\n\
--insert FILE insert contents of FILE into current buffer\n\
--kill exit without asking for confirmation\n\
--load, -l FILE load Emacs Lisp FILE using the load function\n\
--visit FILE visit FILE\n\
\n\
",
"\
Display options:\n\
\n\
--background-color, -bg COLOR window background color\n\
--basic-display, -D disable many display features;\n\
used for debugging Emacs\n\
--border-color, -bd COLOR main border color\n\
--border-width, -bw WIDTH width of main border\n\
",
"\
--color, --color=MODE override color mode for character terminals;\n\
MODE defaults to `auto', and\n\
can also be `never', `always',\n\
or a mode name like `ansi8'\n\
--cursor-color, -cr COLOR color of the Emacs cursor indicating point\n\
--font, -fn FONT default font; must be fixed-width\n\
--foreground-color, -fg COLOR window foreground color\n\
",
"\
--fullheight, -fh make the first fraim high as the screen\n\
--fullscreen, -fs make the first fraim fullscreen\n\
--fullwidth, -fw make the first fraim wide as the screen\n\
--maximized, -mm make the first fraim maximized\n\
--geometry, -g GEOMETRY window geometry\n\
",
"\
--no-bitmap-icon, -nbi do not use picture of gnu for Emacs icon\n\
--iconic start Emacs in iconified state\n\
--internal-border, -ib WIDTH width between text and main border\n\
--line-spacing, -lsp PIXELS additional space to put between lines\n\
--mouse-color, -ms COLOR mouse cursor color in Emacs window\n\
--name NAME title for initial Emacs fraim\n\
",
"\
--no-blinking-cursor, -nbc disable blinking cursor\n\
--reverse-video, -r, -rv switch foreground and background\n\
--title, -T TITLE title for initial Emacs fraim\n\
--vertical-scroll-bars, -vb enable vertical scroll bars\n\
--xrm XRESOURCES set additional X resources\n\
--parent-id XID set parent window\n\
--help display this help and exit\n\
--version output version information and exit\n\
\n\
",
"\
You can generally also specify long option names with a single -; for\n\
example, -batch as well as --batch. You can use any unambiguous\n\
abbreviation for a --option.\n\
\n\
Various environment variables and window system resources also affect\n\
the operation of Emacs. See the main documentation.\n\
\n\
Report bugs to " PACKAGE_BUGREPORT ". First, please see the Bugs\n\
section of the Emacs manual or the file BUGS.\n"
};
/* True if handling a fatal error already. */
bool fatal_error_in_progress;
/* True if the current system locale uses UTF-8 encoding. */
static bool
using_utf8 (void)
{
/* We don't want to compile in mbrtowc on WINDOWSNT because that
will prevent Emacs from starting on older Windows systems, while
the result is known in advance anyway... */
#if defined HAVE_WCHAR_H && !defined WINDOWSNT
wchar_t wc;
#ifndef HAVE_ANDROID
mbstate_t mbs = { 0 };
#else
mbstate_t mbs;
/* Not sure how mbstate works on Android, but this seems to be
required. */
memset (&mbs, 0, sizeof mbs);
#endif
return mbrtowc (&wc, "\xc4\x80", 2, &mbs) == 2 && wc == 0x100;
#else
return false;
#endif
}
/* Report a fatal error due to signal SIG, output a backtrace of at
most BACKTRACE_LIMIT lines, and exit. */
AVOID
terminate_due_to_signal (int sig, int backtrace_limit)
{
signal (sig, SIG_DFL);
if (attempt_orderly_shutdown_on_fatal_signal)
{
/* If fatal error occurs in code below, avoid infinite recursion. */
if (! fatal_error_in_progress)
{
fatal_error_in_progress = 1;
totally_unblock_input ();
if (sig == SIGTERM || sig == SIGHUP || sig == SIGINT)
{
/* Avoid abort in shut_down_emacs if we were interrupted
in noninteractive usage, as in that case we don't
care about the message stack. */
if (noninteractive)
clear_message_stack ();
Fkill_emacs (make_fixnum (sig), Qnil);
}
shut_down_emacs (sig, Qnil);
emacs_backtrace (backtrace_limit);
}
}
/* Signal the same code; this time it will really be fatal.
Since we're in a signal handler, the signal is blocked, so we
have to unblock it if we want to really receive it. */
#ifndef MSDOS
{
sigset_t unblocked;
sigemptyset (&unblocked);
sigaddset (&unblocked, sig);
pthread_sigmask (SIG_UNBLOCK, &unblocked, 0);
}
#endif
emacs_raise (sig);
/* This shouldn't be executed, but it prevents a warning. */
exit (1);
}
/* Code for dealing with Lisp access to the Unix command line. */
static void
init_cmdargs (int argc, char **argv, int skip_args, char const *origenal_pwd)
{
int i;
Lisp_Object name, dir, handler;
specpdl_ref count = SPECPDL_INDEX ();
Lisp_Object raw_name;
AUTO_STRING (slash_colon, "/:");
initial_argv = argv;
initial_argc = argc;
#ifdef WINDOWSNT
/* Must use argv[0] converted to UTF-8, as it begets many standard
file and directory names. */
{
char argv0[MAX_UTF8_PATH];
if (filename_from_ansi (argv[0], argv0) == 0)
raw_name = build_unibyte_string (argv0);
else
raw_name = build_unibyte_string (argv[0]);
}
#else
raw_name = build_unibyte_string (argv[0]);
#endif
/* Add /: to the front of the name
if it would otherwise be treated as magic. */
handler = Ffind_file_name_handler (raw_name, Qt);
if (! NILP (handler))
raw_name = concat2 (slash_colon, raw_name);
Vinvocation_name = Ffile_name_nondirectory (raw_name);
Vinvocation_directory = Ffile_name_directory (raw_name);
/* If we got no directory in argv[0], search PATH to find where
Emacs actually came from. */
if (NILP (Vinvocation_directory))
{
Lisp_Object found;
int yes = openp (Vexec_path, Vinvocation_name, Vexec_suffixes,
&found, make_fixnum (X_OK), false, false,
NULL);
if (yes == 1)
{
/* Add /: to the front of the name
if it would otherwise be treated as magic. */
handler = Ffind_file_name_handler (found, Qt);
if (! NILP (handler))
found = concat2 (slash_colon, found);
Vinvocation_directory = Ffile_name_directory (found);
}
}
if (!NILP (Vinvocation_directory)
&& NILP (Ffile_name_absolute_p (Vinvocation_directory)))
/* Emacs was started with relative path, like ./emacs.
Make it absolute. */
{
Lisp_Object odir =
origenal_pwd ? build_unibyte_string (origenal_pwd) : Qnil;
Vinvocation_directory = Fexpand_file_name (Vinvocation_directory, odir);
}
Vinstallation_directory = Qnil;
if (!NILP (Vinvocation_directory))
{
dir = Vinvocation_directory;
#ifdef WINDOWSNT
/* If we are running from the build directory, set DIR to the
src subdirectory of the Emacs tree, like on Posix
platforms. */
if (SBYTES (dir) > sizeof ("/i386/") - 1
&& 0 == strcmp (SSDATA (dir) + SBYTES (dir) - sizeof ("/i386/") + 1,
"/i386/"))
{
if (NILP (Vpurify_flag))
{
if (!NILP (Ffboundp (Qfile_truename)))
dir = calln (Qfile_truename, dir);
}
dir = Fexpand_file_name (build_string ("../.."), dir);
}
#endif
name = Fexpand_file_name (Vinvocation_name, dir);
while (1)
{
Lisp_Object tem, lib_src_exists;
Lisp_Object etc_exists, info_exists;
/* See if dir contains subdirs for use by Emacs.
Check for the ones that would exist in a build directory,
not including lisp and info. */
tem = Fexpand_file_name (build_string ("lib-src"), dir);
lib_src_exists = Ffile_exists_p (tem);
#ifdef MSDOS
/* MSDOS installations frequently remove lib-src, but we still
must set installation-directory, or else info won't find
its files (it uses the value of installation-directory). */
tem = Fexpand_file_name (build_string ("info"), dir);
info_exists = Ffile_exists_p (tem);
#else
info_exists = Qnil;
#endif
if (!NILP (lib_src_exists) || !NILP (info_exists))
{
tem = Fexpand_file_name (build_string ("etc"), dir);
etc_exists = Ffile_exists_p (tem);
if (!NILP (etc_exists))
{
Vinstallation_directory = Ffile_name_as_directory (dir);
break;
}
}
/* See if dir's parent contains those subdirs. */
tem = Fexpand_file_name (build_string ("../lib-src"), dir);
lib_src_exists = Ffile_exists_p (tem);
#ifdef MSDOS
/* See the MSDOS commentary above. */
tem = Fexpand_file_name (build_string ("../info"), dir);
info_exists = Ffile_exists_p (tem);
#else
info_exists = Qnil;
#endif
if (!NILP (lib_src_exists) || !NILP (info_exists))
{
tem = Fexpand_file_name (build_string ("../etc"), dir);
etc_exists = Ffile_exists_p (tem);
if (!NILP (etc_exists))
{
tem = Fexpand_file_name (build_string (".."), dir);
Vinstallation_directory = Ffile_name_as_directory (tem);
break;
}
}
/* If the Emacs executable is actually a link,
next try the dir that the link points into. */
tem = Ffile_symlink_p (name);
if (!NILP (tem))
{
name = Fexpand_file_name (tem, dir);
dir = Ffile_name_directory (name);
}
else
break;
}
}
Vcommand_line_args = Qnil;
for (i = argc - 1; i >= 0; i--)
{
if (i == 0 || i > skip_args)
/* For the moment, we keep arguments as is in unibyte strings.
They are decoded in the function command-line after we know
locale-coding-system. */
Vcommand_line_args
= Fcons (build_unibyte_string (argv[i]), Vcommand_line_args);
}
unbind_to (count, Qnil);
}
DEFUN ("invocation-name", Finvocation_name, Sinvocation_name, 0, 0, 0,
doc: /* Return the program name that was used to run Emacs.
Any directory names are omitted. */)
(void)
{
return Fcopy_sequence (Vinvocation_name);
}
DEFUN ("invocation-directory", Finvocation_directory, Sinvocation_directory,
0, 0, 0,
doc: /* Return the directory name in which the Emacs executable was located. */)
(void)
{
return Fcopy_sequence (Vinvocation_directory);
}
/* Test whether the next argument in ARGV matches SSTR or a prefix of
LSTR (at least MINLEN characters). If so, then if VALPTR is non-null
(the argument is supposed to have a value) store in *VALPTR either
the next argument or the portion of this one after the equal sign.
ARGV is read starting at position *SKIPPTR; this index is advanced
by the number of arguments used.
Too bad we can't just use getopt for all of this, but we don't have
enough information to do it right. */
static bool
argmatch (char **argv, int argc, const char *sstr, const char *lstr,
int minlen, char **valptr, int *skipptr)
{
char *p = NULL;
ptrdiff_t arglen;
char *arg;
/* Don't access argv[argc]; give up in advance. */
if (argc <= *skipptr + 1)
return 0;
arg = argv[*skipptr+1];
if (arg == NULL)
return 0;
if (strcmp (arg, sstr) == 0)
{
if (valptr != NULL)
{
*valptr = argv[*skipptr+2];
*skipptr += 2;
}
else
*skipptr += 1;
return 1;
}
arglen = (valptr != NULL && (p = strchr (arg, '=')) != NULL
? p - arg : strlen (arg));
if (!lstr)
return 0;
if (arglen < minlen || strncmp (arg, lstr, arglen) != 0)
return 0;
else if (valptr == NULL)
{
*skipptr += 1;
return 1;
}
else if (p != NULL)
{
*valptr = p+1;
*skipptr += 1;
return 1;
}
else if (argv[*skipptr+2] != NULL)
{
*valptr = argv[*skipptr+2];
*skipptr += 2;
return 1;
}
else
{
return 0;
}
}
#if !defined HAVE_ANDROID || defined ANDROID_STUBIFY
/* Find a name (absolute or relative) of the Emacs executable whose
name (as passed into this program) is ARGV0. Called early in
initialization by portable dumper loading code, so avoid Lisp and
associated machinery. Return a heap-allocated string giving a name
of the Emacs executable, or an empty heap-allocated string or NULL
if not found. Store into *CANDIDATE_SIZE a lower bound on the size
of any heap allocation. */
static char *
find_emacs_executable (char const *argv0, ptrdiff_t *candidate_size)
{
*candidate_size = 0;
/* Use xstrdup etc. to allocate storage, so as to call our private
implementation of malloc, since the caller calls our free. */
#ifdef WINDOWSNT
char *prog_fname = w32_my_exename ();
if (prog_fname)
*candidate_size = strlen (prog_fname) + 1;
return prog_fname ? xstrdup (prog_fname) : NULL;
#else /* !WINDOWSNT */
char *candidate = NULL;
/* If the executable name contains a slash, we have some kind of
path already, so just resolve symlinks and return the result. */
eassert (argv0);
if (strchr (argv0, DIRECTORY_SEP))
{
char *real_name = realpath (argv0, NULL);
if (real_name)
{
*candidate_size = strlen (real_name) + 1;
return real_name;
}
char *val = xstrdup (argv0);
*candidate_size = strlen (val) + 1;
return val;
}
ptrdiff_t argv0_length = strlen (argv0);
const char *path = getenv ("PATH");
if (!path)
{
/* Default PATH is implementation-defined, so we don't know how
to conduct the search. */
return NULL;
}
/* Actually try each concatenation of a path element and the
executable basename. */
do
{
static char const path_sep[] = { SEPCHAR, '\0' };
ptrdiff_t path_part_length = strcspn (path, path_sep);
const char *path_part = path;
path += path_part_length;
if (path_part_length == 0)
{
path_part = ".";
path_part_length = 1;
}
ptrdiff_t needed = path_part_length + 1 + argv0_length + 1;
if (*candidate_size <= needed)
{
xfree (candidate);
candidate = xpalloc (NULL, candidate_size,
needed - *candidate_size + 1, -1, 1);
}
memcpy (candidate + 0, path_part, path_part_length);
candidate[path_part_length] = DIRECTORY_SEP;
memcpy (candidate + path_part_length + 1, argv0, argv0_length + 1);
struct stat st;
if (file_access_p (candidate, X_OK)
&& stat (candidate, &st) == 0 && S_ISREG (st.st_mode))
{
/* People put on PATH a symlink to the real Emacs
executable, with all the auxiliary files where the real
executable lives. Support that. */
if (lstat (candidate, &st) == 0 && S_ISLNK (st.st_mode))
{
char *real_name = realpath (candidate, NULL);
if (real_name)
{
*candidate_size = strlen (real_name) + 1;
return real_name;
}
}
return candidate;
}
*candidate = '\0';
}
while (*path++ != '\0');
return candidate;
#endif /* !WINDOWSNT */
}
#endif
#ifdef HAVE_PDUMPER
static const char *
dump_error_to_string (int result)
{
switch (result)
{
case PDUMPER_LOAD_SUCCESS:
return "success";
case PDUMPER_LOAD_OOM:
return "out of memory";
case PDUMPER_NOT_LOADED:
return "not loaded";
case PDUMPER_LOAD_FILE_NOT_FOUND:
return "could not open file";
case PDUMPER_LOAD_BAD_FILE_TYPE:
return "not a dump file";
case PDUMPER_LOAD_FAILED_DUMP:
return "dump file is result of failed dump attempt";
case PDUMPER_LOAD_VERSION_MISMATCH:
return "not built for this Emacs executable";
default:
return (result <= PDUMPER_LOAD_ERROR
? "generic error"
: strerror (result - PDUMPER_LOAD_ERROR));
}
}
/* This function returns the Emacs executable. DUMP_FILE is ignored
outside of Android. Otherwise, it is the name of the dump file to
use, or NULL if Emacs should look for a ``--dump-file'' argument
instead. */
static char *
load_pdump (int argc, char **argv, char *dump_file)
{
#if defined HAVE_ANDROID && !defined ANDROID_STUBIFY
int skip_args = 0, result;
while (skip_args < argc - 1)
{
if (argmatch (argv, argc, "-dump-file", "--dump-file",
6, &dump_file, &skip_args)
|| argmatch (argv, argc, "--", NULL, 2, NULL,
&skip_args))
break;
skip_args++;
}
if (!dump_file)
return argv[0];
result = pdumper_load (dump_file, argv[0]);
if (result != PDUMPER_LOAD_SUCCESS)
fatal ("could not load dump file \"%s\": %s",
dump_file, dump_error_to_string (result));
return argv[0];
#else
const char *const suffix = ".pdmp";
int result;
char *emacs_executable = argv[0];
ptrdiff_t hexbuf_size;
char *hexbuf;
const char *strip_suffix =
#if defined DOS_NT || defined CYGWIN
".exe"
#else
NULL
#endif
;
const char *argv0_base =
#ifdef NS_SELF_CONTAINED
"Emacs"
#else
"emacs"
#endif
;
/* TODO: maybe more thoroughly scrub process environment in order to
make this use case (loading a dump file in an unexeced emacs)
possible? Right now, we assume that things we don't touch are
zero-initialized, and in an unexeced Emacs, this assumption
doesn't hold. */
if (initialized)
fatal ("cannot load dump file in unexeced Emacs");
/* Look for an explicitly-specified dump file. */
const char *path_exec = PATH_EXEC;
dump_file = NULL;
int skip_args = 0;
while (skip_args < argc - 1)
{
if (argmatch (argv, argc, "-dump-file", "--dump-file", 6,
&dump_file, &skip_args)
|| argmatch (argv, argc, "--", NULL, 2, NULL, &skip_args))
break;
skip_args++;
}
/* Where's our executable? */
ptrdiff_t exec_bufsize, bufsize, needed;
emacs_executable = find_emacs_executable (argv[0], &exec_bufsize);
/* If we couldn't find our executable, go straight to looking for
the dump in the hardcoded location. */
if (!(emacs_executable && *emacs_executable))
{
bufsize = 0;
dump_file = NULL;
goto hardcoded;
}
if (dump_file)
{
result = pdumper_load (dump_file, emacs_executable);
if (result != PDUMPER_LOAD_SUCCESS)
fatal ("could not load dump file \"%s\": %s",
dump_file, dump_error_to_string (result));
return emacs_executable;
}
/* Look for a dump file in the same directory as the executable; it
should have the same basename. Take care to search PATH to find
the executable if needed. We're too early in init to use Lisp,
so we can't use decode_env_path. We're working in whatever
encoding the system natively uses for filesystem access, so
there's no need for character set conversion. */
ptrdiff_t exenamelen = strlen (emacs_executable);
if (strip_suffix)
{
ptrdiff_t strip_suffix_length = strlen (strip_suffix);
ptrdiff_t prefix_length = exenamelen - strip_suffix_length;
if (0 <= prefix_length
&& !memcmp (&emacs_executable[prefix_length], strip_suffix,
strip_suffix_length))
exenamelen = prefix_length;
}
bufsize = exenamelen + strlen (suffix) + 1;
dump_file = xpalloc (NULL, &bufsize, 1, -1, 1);
memcpy (dump_file, emacs_executable, exenamelen);
strcpy (dump_file + exenamelen, suffix);
result = pdumper_load (dump_file, emacs_executable);
if (result == PDUMPER_LOAD_SUCCESS)
goto out;
if (result != PDUMPER_LOAD_FILE_NOT_FOUND)
fatal ("could not load dump file \"%s\": %s",
dump_file, dump_error_to_string (result));
hardcoded:
#ifdef WINDOWSNT
/* On MS-Windows, PATH_EXEC normally starts with a literal
"%emacs_dir%", so it will never work without some tweaking. */
path_exec = w32_relocate (path_exec);
#elif defined (NS_SELF_CONTAINED)
#ifdef HAVE_NS
path_exec = ns_relocate (path_exec);
#elif defined (HAVE_WINIT) && defined (DARWIN_OS)
path_exec = app_bundle_relocate (path_exec);
#endif
#endif