1 /* $OpenBSD: main.c,v 1.95 2023/04/14 15:34:08 tb Exp $ */ 2 3 /* This file is in the public domain. */ 4 5 /* 6 * Mainline. 7 */ 8 9 #include <sys/queue.h> 10 #include <err.h> 11 #include <limits.h> 12 #include <locale.h> 13 #include <signal.h> 14 #include <stdio.h> 15 #include <stdlib.h> 16 #include <string.h> 17 #include <termios.h> 18 #include <unistd.h> 19 #include <util.h> 20 21 #include "def.h" 22 #include "kbd.h" 23 #include "funmap.h" 24 #include "macro.h" 25 26 #ifdef MGLOG 27 #include "log.h" 28 #endif 29 30 int thisflag; /* flags, this command */ 31 int lastflag; /* flags, last command */ 32 int curgoal; /* goal column */ 33 int startrow; /* row to start */ 34 int doaudiblebell; /* audible bell toggle */ 35 int dovisiblebell; /* visible bell toggle */ 36 int dblspace; /* sentence end #spaces */ 37 int allbro; /* all buffs read-only */ 38 int batch; /* for regress tests */ 39 struct buffer *curbp; /* current buffer */ 40 struct buffer *bheadp; /* BUFFER list head */ 41 struct mgwin *curwp; /* current window */ 42 struct mgwin *wheadp; /* MGWIN listhead */ 43 struct vhead varhead; /* Variable list head */ 44 char pat[NPAT]; /* pattern */ 45 46 static void edinit(struct buffer *); 47 static void pty_init(void); 48 static __dead void usage(void); 49 50 extern char *__progname; 51 extern void closetags(void); 52 53 static __dead void 54 usage(void) 55 { 56 fprintf(stderr, "usage: %s [-nR] [-b file] [-f mode] [-u file] " 57 "[+number] [file ...]\n", 58 __progname); 59 exit(1); 60 } 61 62 int 63 main(int argc, char **argv) 64 { 65 FILE *ffp; 66 char file[NFILEN]; 67 char *cp, *conffile = NULL, *init_fcn_name = NULL; 68 char *batchfile = NULL; 69 PF init_fcn = NULL; 70 int o, i, nfiles; 71 int nobackups = 0; 72 struct buffer *bp = NULL; 73 74 if (pledge("stdio rpath wpath cpath fattr chown getpw tty proc exec", 75 NULL) == -1) 76 err(1, "pledge"); 77 78 while ((o = getopt(argc, argv, "nRb:f:u:")) != -1) 79 switch (o) { 80 case 'b': 81 batch = 1; 82 batchfile = optarg; 83 break; 84 case 'R': 85 allbro = 1; 86 break; 87 case 'n': 88 nobackups = 1; 89 break; 90 case 'f': 91 if (init_fcn_name != NULL) 92 errx(1, "cannot specify more than one " 93 "initial function"); 94 init_fcn_name = optarg; 95 break; 96 case 'u': 97 conffile = optarg; 98 break; 99 default: 100 usage(); 101 } 102 103 if (batch && (conffile != NULL)) { 104 fprintf(stderr, "%s: -b and -u are mutually exclusive.\n", 105 __progname); 106 exit(1); 107 } 108 if (batch) { 109 pty_init(); 110 conffile = batchfile; 111 } 112 if ((ffp = startupfile(NULL, conffile, file, sizeof(file))) == NULL && 113 conffile != NULL) { 114 fprintf(stderr, "%s: Problem with file: %s\n", __progname, 115 conffile); 116 exit(1); 117 } 118 119 argc -= optind; 120 argv += optind; 121 122 setlocale(LC_CTYPE, ""); 123 124 maps_init(); /* Keymaps and modes. */ 125 funmap_init(); /* Functions. */ 126 127 #ifdef MGLOG 128 if (!mgloginit()) 129 errx(1, "Unable to create logging environment."); 130 #endif 131 132 /* 133 * This is where we initialize standalone extensions that should 134 * be loaded dynamically sometime in the future. 135 */ 136 { 137 extern void grep_init(void); 138 extern void cmode_init(void); 139 extern void dired_init(void); 140 141 dired_init(); 142 grep_init(); 143 cmode_init(); 144 } 145 146 if (init_fcn_name && 147 (init_fcn = name_function(init_fcn_name)) == NULL) 148 errx(1, "Unknown function `%s'", init_fcn_name); 149 150 vtinit(); /* Virtual terminal. */ 151 dirinit(); /* Get current directory. */ 152 edinit(bp); /* Buffers, windows. */ 153 ttykeymapinit(); /* Symbols, bindings. */ 154 bellinit(); /* Audible and visible bell. */ 155 dblspace = 1; /* two spaces for sentence end. */ 156 157 /* 158 * doing update() before reading files causes the error messages from 159 * the file I/O show up on the screen. (and also an extra display of 160 * the mode line if there are files specified on the command line.) 161 */ 162 update(CMODE); 163 164 /* user startup file. */ 165 if (ffp != NULL) { 166 (void)load(ffp, file); 167 ffclose(ffp, NULL); 168 } 169 170 if (batch) 171 return (0); 172 173 /* 174 * Now ensure any default buffer modes from the startup file are 175 * given to any files opened when parsing the startup file. 176 * Note *scratch* will also be updated. 177 */ 178 for (bp = bheadp; bp != NULL; bp = bp->b_bufp) { 179 bp->b_flag = defb_flag; 180 for (i = 0; i <= defb_nmodes; i++) { 181 bp->b_modes[i] = defb_modes[i]; 182 } 183 } 184 185 /* Force FFOTHARG=1 so that this mode is enabled, not simply toggled */ 186 if (init_fcn) 187 init_fcn(FFOTHARG, 1); 188 189 if (nobackups) 190 makebkfile(FFARG, 0); 191 192 for (nfiles = 0, i = 0; i < argc; i++) { 193 if (argv[i][0] == '+' && strlen(argv[i]) >= 2) { 194 long long lval; 195 const char *errstr; 196 197 lval = strtonum(&argv[i][1], INT_MIN, INT_MAX, &errstr); 198 if (argv[i][1] == '\0' || errstr != NULL) 199 goto notnum; 200 startrow = lval; 201 } else { 202 notnum: 203 cp = adjustname(argv[i], FALSE); 204 if (cp != NULL) { 205 if (nfiles == 1) 206 splitwind(0, 1); 207 208 if (fisdir(cp) == TRUE) { 209 (void)do_dired(cp); 210 continue; 211 } 212 if ((curbp = findbuffer(cp)) == NULL) { 213 vttidy(); 214 errx(1, "Can't find current buffer!"); 215 } 216 (void)showbuffer(curbp, curwp, 0); 217 if (readin(cp) != TRUE) 218 killbuffer(curbp); 219 else { 220 /* Ensure enabled, not just toggled */ 221 if (init_fcn_name) 222 init_fcn(FFOTHARG, 1); 223 nfiles++; 224 } 225 if (allbro) 226 curbp->b_flag |= BFREADONLY; 227 } 228 } 229 } 230 231 if (nfiles > 2) 232 listbuffers(0, 1); 233 234 /* fake last flags */ 235 thisflag = 0; 236 for (;;) { 237 if (epresf == KCLEAR) 238 eerase(); 239 if (epresf == TRUE) 240 epresf = KCLEAR; 241 if (winch_flag) { 242 do_redraw(0, 0, TRUE); 243 winch_flag = 0; 244 } 245 update(CMODE); 246 lastflag = thisflag; 247 thisflag = 0; 248 249 switch (doin()) { 250 case TRUE: 251 break; 252 case ABORT: 253 ewprintf("Quit"); 254 /* FALLTHRU */ 255 case FALSE: 256 default: 257 macrodef = FALSE; 258 } 259 } 260 } 261 262 /* 263 * Initialize default buffer and window. Default buffer is called *scratch*. 264 */ 265 static void 266 edinit(struct buffer *bp) 267 { 268 struct mgwin *wp; 269 270 bheadp = NULL; 271 bp = bfind("*scratch*", TRUE); /* Text buffer. */ 272 if (bp == NULL) 273 panic("edinit"); 274 275 wp = new_window(bp); 276 if (wp == NULL) 277 panic("edinit: Out of memory"); 278 279 curbp = bp; /* Current buffer. */ 280 wheadp = wp; 281 curwp = wp; 282 wp->w_wndp = NULL; /* Initialize window. */ 283 wp->w_linep = wp->w_dotp = bp->b_headp; 284 wp->w_ntrows = nrow - 2; /* 2 = mode, echo. */ 285 wp->w_rflag = WFMODE | WFFULL; /* Full. */ 286 } 287 288 /* 289 * Create pty for batch mode. 290 */ 291 static void 292 pty_init(void) 293 { 294 struct winsize ws; 295 int master; 296 int slave; 297 298 memset(&ws, 0, sizeof(ws)); 299 ws.ws_col = 80, 300 ws.ws_row = 24; 301 302 openpty(&master, &slave, NULL, NULL, &ws); 303 login_tty(slave); 304 305 return; 306 } 307 308 /* 309 * Quit command. If an argument, always quit. Otherwise confirm if a buffer 310 * has been changed and not written out. Normally bound to "C-x C-c". 311 */ 312 int 313 quit(int f, int n) 314 { 315 int s; 316 317 if ((s = anycb(FALSE)) == ABORT) 318 return (ABORT); 319 if (s == FIOERR || s == UERROR) 320 return (FALSE); 321 if (s == FALSE 322 || eyesno("Modified buffers exist; really exit") == TRUE) { 323 vttidy(); 324 closetags(); 325 exit(0); 326 } 327 return (TRUE); 328 } 329 330 /* 331 * User abort. Should be called by any input routine that sees a C-g to abort 332 * whatever C-g is aborting these days. Currently does nothing. 333 */ 334 int 335 ctrlg(int f, int n) 336 { 337 return (ABORT); 338 } 339